function consumeStatIncrease($charData, $statName) { $charData->{$statName}++; $statEquivalent = getStatEquivalent($statName); echo "Your {$statName} increases, improving your {$statEquivalent}. You go back to Adventuring.\n"; StateManager::ChangeState($charData, GameStates::Adventuring); }
} // Check the status of the rest $resting->commands[] = new InputFragment("check", function ($charData, $mapData) { global $traitMap; $isPray = $traitMap->ClassHasTrait($charData, TraitName::Pray); $restEnd = $charData->restEnd; $date = new DateTime(); $date->setTimestamp($restEnd); $status = "You will wake up at " . $date->format("H:i") . ". To wake up now enter 'wake'. (" . getCurrentStats($charData) . ")\n"; if ($isPray) { $status = "You will stop praying at " . $date->format("H:i") . ". To stop now, enter 'wake'. (" . getCurrentStats($charData, false, true) . ")\n"; } echo $status; }); $resting->commands[] = new InputFragment("wake", function ($charData, $mapData) { global $traitMap; $isPray = $traitMap->ClassHasTrait($charData, TraitName::Pray); $restString = ""; if (!$isPray) { $restString = "You wake up, ready to start the new day. (" . getCurrentStats($charData, true, $isPray) . ")"; } else { $restString = "You stand up, feeling thoroughly refreshed. (" . getCurrentStats($charData, true, $isPray) . ")"; } echo $restString . "\n"; $charData->restStart = 0; $charData->restEnd = 0; StateManager::ChangeState($charData, GameStates::Adventuring); }); // Add unique identifiers to commands. $allocator = new UIDAllocator($resting->commands); $allocator->Allocate();
public function generateInputFragments(&$charData, $nonCombat = false) { $inventory = lazyGetInventory($charData); $inventoryItems = $inventory->items; // We only want one InputFragment per item TYPE. $dedupedNames = []; foreach ($inventoryItems as $itemName) { $item = findItem($itemName); if (is_null($item)) { echo "ERROR: Bad item in inventory ({$itemName})!\n"; exit(14); } $useLocation = $item->useLocation; if ($nonCombat && $useLocation == ItemUse::CombatOnly) { continue; } if (!$nonCombat && $useLocation == ItemUse::NonCombatOnly) { continue; } if ($this->deDupeItemFragments($itemName, $dedupedNames)) { continue; } $this->commands[] = new InputFragment($itemName, function ($charData, $mapData) use($itemName, $nonCombat) { global $usingItem; return $usingItem->useItem($itemName, $charData, $mapData, $nonCombat); }); } $this->commands[] = new InputFragment("cancel", function ($charData, $mapData) use($nonCombat) { echo $nonCombat . "\n\n"; if (!$nonCombat) { echo "You close your bag, and go back to the fight.\n"; StateManager::ChangeState($charData, GameStates::Combat); } else { echo "Deciding against using an item, you go back to Adventuring.\n"; StateManager::ChangeState($charData, GameStates::Adventuring); } }); // Add unique identifiers to commands. $allocator = new UIDAllocator($this->commands); $allocator->Allocate(); }
function addShopFragmentIfNeeded($charData, $mapData) { global $adventuring; $currentRoom = $mapData->map->GetRoom($mapData->playerX, $mapData->playerY); if (is_null($currentRoom)) { return; } $containsShop = isset($currentRoom->occupant) && get_class($currentRoom->occupant) == "Shop"; if ($containsShop) { $adventuring->commands[] = new InputFragment("browse shop", function ($charData, $mapData) use($currentRoom) { $shop = $currentRoom->occupant; if ($shop->isEmpty()) { echo "\"Sorry, I'm all sold out right now!\"\n"; return; } global $shopping; $shopStr = $shopping->getShopString($shop, $charData, $mapData); echo $shopStr; StateManager::ChangeState($charData, GameStates::Shopping); }); $allocator = new UIDAllocator($adventuring->commands); $allocator->Allocate(); } }
public function generateInputFragments($charData, $outOfCombat = false) { $spellList = $charData->spellbook; $spellNum = 1; foreach ($spellList as $spellName) { // Can only cast heal spells. if ($outOfCombat) { $spell = $this->findSpellOrAbility($spellName, $charData); if (!$spell->isHeal) { continue; } } $this->commands[] = new InputFragment($spellName, function ($charData, $mapData, $dynData) use($spellName, $outOfCombat) { $this->castSpell($spellName, $charData, $mapData, $dynData, $outOfCombat); }); ++$spellNum; } $this->commands[] = new InputFragment("cancel", function ($charData, $mapData) use($outOfCombat) { if (!$outOfCombat) { echo "You decide against casting a spell, and go back to the fight.\n"; StateManager::ChangeState($charData, GameStates::Combat); } else { echo "You decide not to cast a spell, and go back to Adventuring.\n"; StateManager::ChangeState($charData, GameStates::Adventuring); } }); // Add unique identifiers to commands. $allocator = new UIDAllocator($this->commands); $allocator->Allocate(); }
} $reflexesString = getSingleStatString("reflexes", $dynData); if (!is_null($reflexesString)) { $outString .= $reflexesString . ", "; } $strengthString = getSingleStatString("strength", $dynData); if (!is_null($strengthString)) { $outString .= $strengthString . ", "; } $oddnessString = getSingleStatString("oddness", $dynData); if (!is_null($oddnessString)) { $outString .= $oddnessString . ", "; } $nerveString = getSingleStatString("nerve", $dynData); if (!is_null($nerveString)) { $outString .= $nerveString . ", "; } $acuityString = getSingleStatString("acuity", $dynData); if (!is_null($acuityString)) { $outString .= $acuityString . ", "; } $outString = rtrim($outString, ", "); $gold = $dynData->gold; $outString .= " [You have {$gold}GP]\n"; echo $outString; }); // Start the game already. $dynasty->commands[] = new InputFragment("begin game", function ($charData, $mapData, $dynData) { echo "Welcome! Please choose a name for your character:\n"; StateManager::ChangeState($charData, GameStates::NameSelect); });
function main() { $nick = getNickFromArgs(); $mapData = null; $charData = null; // This will force everyone into creating a Dynasty save. // We patching shit, yo. $dynPatch = false; $dynPath = getSaveFilePath($nick, SaveFileType::Dynasty); if (!file_exists($dynPath)) { saveGame($nick, SaveFileType::Dynasty); $dynPatch = true; } if (!checkIfNewGame($nick)) { // Load character save data. $charFilePath = getSaveFilePath($nick, SaveFileType::Character); $charData = FileIO::UnserializeFile($charFilePath); $charDataDirty = false; // Load map save data. $mapFilePath = getSaveFilePath($nick, SaveFileType::Map); $mapData = FileIO::UnserializeFile($mapFilePath); $mapDataDirty = false; // Load dynasty save data. $dynFilePath = getSaveFilePath($nick, SaveFileType::Dynasty); $dynData = FileIO::UnserializeFile($dynFilePath); $dynDataDirty = false; // Put everyone into the dynasty initialisation state, just this once. //$notYetPatched = !isset($charData->patched); $notYetPatched = !isset($dynData->initialised); $notPatching = $charData->state != GameStates::DynastyInit; if (empty($dynData) || $notYetPatched && $notPatching) { $charData->patchState = $charData->state; $charData->patchPrevState = $charData->previousState; DEBUG_echo("Patching in Dynasty..."); StateManager::ChangeState($charData, GameStates::DynastySplash); } // Patch the stat changes in. doStatPatchIfNeeded($charData); // Ensure it's sane. if (empty($charData) || empty($mapData)) { echo "ERROR: Save data's f****d.\n"; exit(3); } // Read STDIN for input. $input = readStdin(); switch ($charData->state) { case GameStates::DynastySplash: DEBUG_echo("DynastySplash"); echo "Your Dynasty begins, and needs a name. Choose your name wisely - you cannot alter history.\n"; StateManager::ChangeState($charData, GameStates::DynastyInit); $dynData->initialised = true; $charDataDirty = true; $dynDataDirty = true; break; case GameStates::DynastyInit: DEBUG_echo("DynastyInit"); // Validate input. $validName = preg_match("/^[a-zA-Z]{1,16}\$/", $input, $output); if (!$validName) { echo "Please enter a valid name. Letters only, between 1 and 16 characters.\n"; return; } $dynData->name = $input; $output = "The Dynasty of {$input} begins! Onwards, to adventure!"; echo "{$output}\n"; // Hook back up to where we were. $charData->state = $charData->patchState; $charData->previousState = $charData->patchPrevState; $charDataDirty = true; $dynDataDirty = true; break; case GameStates::NameSelect: DEBUG_echo("NameSelect"); $validName = preg_match("/^[a-zA-Z]{1,16}\$/", $input, $output); if (!$validName) { echo "Please enter a valid name. Letters only, between 1 and 16 characters.\n"; exit(13); } $output = "Please choose a class for {$input} {$dynData->name}: "; global $classSelect; foreach ($classSelect->commands as $fragment) { $output .= "{$fragment->displayString}, "; } $output = rtrim($output, ", ") . "\n"; echo $output; $charData->name = $input; StateManager::ChangeState($charData, GameStates::ClassSelect); $charDataDirty = true; break; case GameStates::ClassSelect: DEBUG_echo("ClassSelect"); $input = strtolower($input); $setClass = classSelect($input, $charData, $dynData, $charData->name); if ($setClass) { StateManager::ChangeState($charData, GameStates::FirstPlay); $charDataDirty = true; } break; // Initialise the characters // Initialise the characters case GameStates::FirstPlay: DEBUG_echo("FirstPlay"); firstPlay($charData, $dynData); StateManager::ChangeState($charData, GameStates::Adventuring); // purposeful fall-through! // The main loop for when we're romping around. // purposeful fall-through! // The main loop for when we're romping around. case GameStates::Adventuring: DEBUG_echo("Adventuring"); adventuring($input, $charData, $mapData, $dynData); $charDataDirty = true; $mapDataDirty = true; break; // Sleepy nap time. // Sleepy nap time. case GameStates::Resting: DEBUG_echo("Resting"); resting($input, $charData, $mapData); $charDataDirty = true; break; // IT'S CLOBBERING TIME // IT'S CLOBBERING TIME case GameStates::Combat: DEBUG_echo("Combat"); combat($input, $charData, $mapData, $dynData); $charDataDirty = true; $mapDataDirty = true; break; case GameStates::Spellcasting: DEBUG_echo("Spellcasting"); $nonCombat = isset($charData->previousState) && $charData->previousState != GameStates::Combat; spellcasting($input, $charData, $mapData, $nonCombat); $charDataDirty = true; $mapDataDirty = true; break; case GameStates::Looting: DEBUG_echo("Looting"); looting($input, $charData, $mapData); $charDataDirty = true; $mapDataDirty = true; break; case GameStates::LevelUp: DEBUG_echo("LevelUp"); levelUp($input, $charData, $mapData); $charDataDirty = true; break; case GameStates::UsingItem: DEBUG_echo("UsingItem"); $nonCombat = isset($charData->previousState) && $charData->previousState != GameStates::Combat; usingItem($input, $charData, $mapData, $nonCombat); $charDataDirty = true; break; case GameStates::Shopping: DEBUG_echo("Shopping"); shopping($input, $charData, $mapData); $charDataDirty = true; $mapDataDirty = true; break; case GameStates::Dynasty: DEBUG_echo("Dynasty"); dynasty($input, $charData, $mapData, $dynData); $charDataDirty = true; $dynDataDirty = true; break; default: break; } } else { // Initialise the character save. saveGame($nick, SaveFileType::Character); // Initialise the map save. saveGame($nick, SaveFileType::Map); // Prompt for name/dynasty select. if (!$dynPatch) { echo "How do you want to alter your Dynasty?\n"; } else { echo "Welcome to blaventure!\n"; } } if (isset($charData) && $charDataDirty) { saveGame($nick, SaveFileType::Character, $charData); } if (isset($mapData) && $mapDataDirty) { saveGame($nick, SaveFileType::Map, $mapData); } if (isset($dynData) && $dynDataDirty) { saveGame($nick, SaveFileType::Dynasty, $dynData); } }
function exitCombat(&$charData, &$mapData, &$textOutput) { // We killed the enemy. $textOutput .= " It dies! "; $charData->kills++; // Unlock once-per-combat abilities. clearAllAbilityLocks($charData); // Calm down the Barbarians. reduceRage($charData, $fightOutput, 0); // Streak management. $streak = $charData->lazyGetStreak(); $currentStreak = $streak->currentValue; alterStreak($charData, $mapData); $newStreak = $streak->currentValue; if (floor($newStreak / 5) > floor($currentStreak / 5)) { $textOutput .= "STREAK UP! "; } // Move to the looting state. StateManager::ChangeState($charData, GameStates::Looting); $textOutput .= "Check the body for loot!\n"; }
public function generateInputFragments(&$charData, &$mapData, $justItems = false) { // To be in here, we need to have a shop at our current location. $room = $mapData->map->GetRoom($mapData->playerX, $mapData->playerY); $shop = $room->occupant; if (!$justItems) { $this->commands[] = new InputFragment("check stock", function ($charData, $mapData) use($shop) { global $shopping; $shopStr = $shopping->getShopString($shop, $charData, $mapData); echo $shopStr; }); } if (!$justItems) { $this->commands[] = new InputFragment("leave", function ($charData, $mapData) { echo "\"Maybe next time, eh?\", the shopkeeper drawls at you as you walk away.\n"; StateManager::ChangeState($charData, GameStates::Adventuring); }); } if (!$justItems) { $this->commands[] = new InputFragment("equipment", function ($charData, $mapData) { global $adventuring; $equipment = $adventuring->getEquippedItemsStr($charData); echo $equipment . "\n"; }); } foreach ($shop->stock as $itemName => $quantity) { $item = findItem($itemName); $this->commands[] = new InputFragment($itemName, function ($charData, $mapData) use($item, $itemName, $shop, $room) { global $shopping; if (!$shopping->canAffordItem($charData, $item->getCost($charData))) { return; } // Remove item from shop. $shop->removeStockItem($itemName); // Add item to player inventory, and deduct gold. $inventory = lazyGetInventory($charData); $inventory->addItem($item); $shopping->takeMoney($item, $shop, $charData, $room); }); } foreach ($shop->equipment as $equipment) { $this->commands[] = new InputFragment($equipment->name, function ($charData, $mapData) use($equipment, $shop, $room) { global $shopping; if (!$shopping->canAffordItem($charData, $equipment->getCost($charData))) { return; } $equipString = "You equip your new {$equipment->name} immediately"; $isBarbarian = strcasecmp($charData->class, "Barbarian") == 0; if ($equipment->type == ShopEquipment::Weapon) { $weapon1Better = $charData->weaponVal >= $equipment->level; $weapon2Better = $charData->weapon2Val >= $equipment->level; if (!$isBarbarian && $weapon1Better || $isBarbarian && $weapon1Better && $weapon2Better) { echo "\"No point buying my wares if you've got better yourself!\"\n"; return; } // Bloody complicated Barbarians if ($isBarbarian) { // Going in the off-hand if ($weapon1Better) { $charData->weapon2 = $equipment->name; $charData->weapon2Val = $equipment->level; } else { $equipString .= ", moving your {$charData->weapon} to your off-hand"; $charData->weapon2 = $charData->weapon; $charData->weapon2Val = $charData->weaponVal; $charData->weapon = $equipment->name; $charData->weaponVal = $equipment->level; } } else { $charData->weapon = $equipment->name; $charData->weaponVal = $equipment->level; } } else { if ($equipment->type == ShopEquipment::Armour) { $armourBetter = $charData->armourVal >= $equipment->level; if ($isBarbarian) { echo "\"Not sure you'd know what to do with that!\"\n"; return; } else { if ($armourBetter) { echo "\"No point buying my wares if you've got better yourself!\"\n"; return; } } $charData->armour = $equipment->name; $charData->armourVal = $equipment->level; } } $equipString .= ". "; // Remove equipment from shop. $shop->removeEquipment($equipment->name); $shopping->takeMoney($equipment, $shop, $charData, $room, $equipString); }); } // Add unique identifiers to commands. $allocator = new UIDAllocator($this->commands); $allocator->Allocate(); }