 
     @IBAction func setPlayer(sender: Any) {
 
-        needToSave = false
+        // Initialise the game
+        needToSave = true
         firstRun = false
-        player.isDead = false
-
-        var gameType: Int = kGameNone;
-
+        gameInProgress = true
+
+        // Create a new player instance
         player = FFPlayer()
-        player.gold = startGoldField.integerValue
-        player.provisions = startFoodField.integerValue
-        player.potion = potionPopup.indexOfSelectedItem - 1
-        player.drinks = 2
-
-        player.fear = 0
-        player.stamina = startStaminaField.integerValue
-        player.initialStamina = player.stamina
-        player.luck = startLuckField.integerValue
-        player.initialLuck = player.luck
+        player!.gold = startGoldField.integerValue
+        player!.provisions = startFoodField.integerValue
+        player!.potion = startPotionPopup.indexOfSelectedItem - 1
+        player!.drinks = 2
+
+        player!.skill = startSkillField.integerValue
+        player!.initialSkill = player!.skill
+        player!.stamina = startStaminaField.integerValue
+        player!.initialStamina = player!.stamina
+        player!.luck = startLuckField.integerValue
+        player!.initialLuck = player!.luck
 
         // We make sure elsewhere that only one or none of these options are seleced
         // Note that all of these 'non-default' game types do not use potions, so
         // remove them if the user has selected one
 
-        gameType = gamePopup.indexOfSelectedItem
-
+        let gameType: Int = startGamePopup.indexOfSelectedItem
+        player!.gamekind = gameType
         if gameType == kGameWarlock || gameType == kGameDeathtrap || gameType == kGameCityThieves || gameType == kGameForestDoom {
-            // Standard games
-
-            if gameType == kGameWarlock {
-                player.gameName = "The Warlock of Firetop Mountain";
-            }
-
-            if gameType == kGameDeathtrap {
-                player.gameName = "Deathtrap Dungeon";
-            }
-
-            if gameType == kGameCityThieves {
-                player.gameName = "City of Thieves";
-            }
-
-            if gameType == kGameForestDoom {
-                player.gameName = "Forest of Doom";
-            }
+            if gameType == kGameWarlock { player!.gameName = "The Warlock of Firetop Mountain" }
+            if gameType == kGameDeathtrap { player!.gameName = "Deathtrap Dungeon" }
+            if gameType == kGameCityThieves { player!.gameName = "City of Thieves" }
+            if gameType == kGameForestDoom { player!.gameName = "Forest of Doom" }
+        }
+
+        if gameType != kGameCitadel && gameType != kGameTempleTerror {
+            // Remove the Magic tab for all but Citadel of Chaos and Temple of Terror
+            heldTabs["magictab"] = magicTab!
+            if tabs.tabViewItems.contains(magicTab) { tabs.removeTabViewItem(magicTab) }
+        } else {
+            // Add back the Magic tab if necessary
+            if !tabs.tabViewItems.contains(magicTab) { tabs.insertTabViewItem(heldTabs["magictab"]!, at: 2) }
         }
 
         if gameType == kGameCitadel {
-            player.magic = Int((arc4random() % 6) + (arc4random() % 6)) + 8
-            player.potion = kPotionNone
-            player.drinks = 0
-            player.gold = 0
-            player.gameName = "Citadel of Chaos"
+            player!.magic = Int(arc4random() % 6 + arc4random() % 6) + 8
+            player!.potion = kPotionNone
+            player!.drinks = 0
+            player!.gold = 0
+            player!.gameName = "Citadel of Chaos"
             citadelBox.isHidden = false
-            hellBox.isHidden = true
 
             // Place an image in the gap to the left
             let image: NSImageView = NSImageView.init(frame: NSMakeRect(161, 10, 267, 106))
             image.image = NSImage.init(named: NSImage.Name("coc_stats"))
             statsTabView.addSubview(image)
 
-            // Add back the Magic tab if necessary
-            if !tabs.tabViewItems.contains(magicTab) { tabs.insertTabViewItem(heldTabs["magictab"]!, at: 2) }
+            magicSpellsValue.stringValue = "\(player!.magic)"
         }
 
         if gameType == kGameHouseHell {
-            player.skill = player.skill - 3
-            player.maxFear = Int(arc4random() % 6) + 7
-            player.fear = 0
-            player.potion = kPotionNone
-            player.drinks = 0
-            player.gold = 0
-            player.provisions = 0
-            player.gameName = "House of Hell"
+            player!.skill = player!.skill - 3
+            player!.initialSkill = player!.skill + 3
+            player!.maxFear = Int(arc4random() % 6) + 7
+            player!.fear = 0
+            player!.potion = kPotionNone
+            player!.drinks = 0
+            player!.gold = 0
+            player!.provisions = 0
+            player!.gameName = "House of Hell"
             hellBox.isHidden = false
-            citadelBox.isHidden = true
 
             // Place an image in the gap to the left
             let image: NSImageView = NSImageView.init(frame: NSMakeRect(8, 14, 146, 96))
             image.image = NSImage.init(named: NSImage.Name("hell"))
             statsTabView.addSubview(image)
-
-            // Remove the Magic tab
-            heldTabs["magictab"] = magicTab!
-            if tabs.tabViewItems.contains(magicTab) { tabs.removeTabViewItem(magicTab) }
         }
 
         if gameType == kGameCavernsSnow {
-            player.drinks = 1
-            player.gold = 0
-            player.gameName = "Caverns of the Snow Witch"
+            player!.drinks = 1
+            player!.gold = 0
+            player!.gameName = "Caverns of the Snow Witch"
         }
 
         if gameType == kGameReturnFiretop {
-            player.potion = kPotionNone
-            player.drinks = 0
-            player.gold = 0
-            player.provisions = 0
-            player.gameName = "Return to Firetop Mountain"
+            player!.potion = kPotionNone
+            player!.drinks = 0
+            player!.gold = 0
+            player!.provisions = 0
+            player!.gameName = "Return to Firetop Mountain"
 
         }
 
         if gameType == kGameTempleTerror {
-            player.potion = kPotionNone
-            player.drinks = 0
-            player.gold = 25
-            player.gameName = "Temple of Terror"
+            player!.potion = kPotionNone
+            player!.drinks = 0
+            player!.gold = 25
+            player!.gameName = "Temple of Terror"
         }
 
         if gameType == kGameEyeDragon {
-            player.potion = kPotionNone
-            player.drinks = 0
-            player.gold = 10
-            player.gameName = "Eye of the Dragon"
+            player!.potion = kPotionNone
+            player!.drinks = 0
+            player!.gold = 10
+            player!.gameName = "Eye of the Dragon"
         }
 
         // Sorcery! games - always the last two in the menu
 
         if gameType == kGameSorceryWizard {
-            player.skill = player.skill - 2
-            player.potion = kPotionNone
-            player.drinks = 0
-            player.gold = 20
-            player.provisions = 2
-            player.gameName = "Sorcery! Wizard"
+            player!.skill = player!.skill - 2
+            player!.potion = kPotionNone
+            player!.drinks = 0
+            player!.gold = 20
+            player!.provisions = 2
+            player!.gameName = "Sorcery! Wizard"
 
         }
 
         if gameType == kGameSorceryFighter {
-            player.potion = kPotionNone
-            player.drinks = 0
-            player.gold = 20
-            player.provisions = 2
-            player.gameName = "Sorcery! Fighter"
-        }
-
-        player.gamekind = gameType
-        gameInProgress = true
-        player.skill = startSkillField.integerValue
-        player.initialSkill = player.skill
-
-        if gameType == kGameHouseHell {
-            player.initialSkill = player.skill + 3
+            player!.potion = kPotionNone
+            player!.drinks = 0
+            player!.gold = 20
+            player!.provisions = 2
+            player!.gameName = "Sorcery! Fighter"
         }
 
         // Initialise the backpack
-
-        if player.pack.count > 0 {
-            player.pack.removeAll()
-        }
-
+        if player!.pack.count > 0 { player!.pack.removeAll() }
+
+        // Only House of Hell starts you out with no kit
         if gameType != kGameHouseHell {
-            player.pack.append("Lantern")
-            player.pack.append("Sword")
-            player.pack.append("Leather armour")
-        }
-
+            player!.pack.append("Lantern")
+            player!.pack.append("Sword")
+            player!.pack.append("Leather armour")
+        }
+
+        updateStatsSteppers()
         updateStats()
 
-        // Hide the sheet
+        window.title = "Fighting Fantasy - " + player!.gameName
         window.endSheet(createSheet)
     }
 }

        window.endSheet(aboutSheet)
    }

    // MARK: Player Creation Functions

    @IBAction func newPlayer(_ sender: Any) {
        showPlayerCreate()
    }

    func showPlayerCreate() {
        window.beginSheet(createSheet, completionHandler: nil)
    }



//  FightingFantasy
//  Created by Tony Smith on 26/10/2017.
//  Copyright © 2005-2017 Tony Smith. All rights reserved.


import Cocoa
