name = $name; $this->description = $description; $this->stats = $stats; } public function getName(): string { return $this->name; } public function setName(string $name): void { $this->name = $name; $this->save_to_db(); } public function getDescription(): string { return $this->description; } public function getStats(): array { return $this->stats; } public function getStat(string $stat_name): ?int { return $this->stats[$stat_name] ?? null; } public function setStat(string $stat_name, $value): void { $this->stats[$stat_name] = $value; $this->save_to_db(); } public function isAlive(): bool { // if MaxHealth is not set or 0, the combattant is always alive if (!isset($this->stats['MaxHealth']) || $this->stats['MaxHealth'] <= 0) { return true; } return ($this->stats['CurHealth'] ?? 0) > 0; } public function takeDamage(int $amount): void { if (isset($this->stats['CurHealth'])) { $this->stats['CurHealth'] -= $amount; if ($this->stats['CurHealth'] < 0) { $this->stats['CurHealth'] = 0; } // save the combattant to the database if applicable if (method_exists($this, 'save_to_db')) { $this->save_to_db(); } } } public function heal(int $amount): int { if( $amount == -1 ) { // full heal $amount = $this->stats['MaxHealth'] - ($this->stats['CurHealth'] ?? 0); } $healed_amount = 0; if (isset($this->stats['CurHealth']) && isset($this->stats['MaxHealth'])) { $healed_amount = $amount; $this->stats['CurHealth'] += $amount; if ($this->stats['CurHealth'] > $this->stats['MaxHealth']) { // get the correct amount healed $healed_amount = $this->stats['MaxHealth'] - ($this->stats['CurHealth'] - $amount); $this->stats['CurHealth'] = $this->stats['MaxHealth']; } // save the combattant to the database if applicable if (method_exists($this, 'save_to_db')) { $this->save_to_db(); } } return $healed_amount; } public function getNextAction(): ?string { return $this->next_action; } public function setNextAction(string $action): void { $this->next_action = $action; $this->save_to_db(); } public function getDefense(): int { return $this->stats['defense'] ?? 0; } public function getArmor(): int { // check all equipped items for an "Armor" stat and sum them up $armor = 0; $equipped_items = $this->getEquippedItems(); foreach ($equipped_items as $item_code) { $item = Item_Definition::get_item_definition_by_code( $item_code ); if ( $item ) { $item_stats = $item->get_stats(); $armor += intval( $item_stats['Armor'] ?? 0 ); } } // add any base armor from the combattant's stats $armor += intval( $this->stats['Armor'] ?? 0 ); return $armor; } public function getAttackPower(): int { return $this->stats['attack'] ?? 0; } public function getInitiative(): int { return $this->stats['initiative'] ?? 0; } /** * Check if the combattant has a specific slot for equipping items. * The slots are defined in the combattandt's stats under 'Slots'. * The value is a comma-separated list of slot names, e.g. "Weapon,Armor,Accessory" * but it can contain an already equipped item in the form "Weapon:sword" * * @param string $slot_name The name of the slot to check. * @return bool True if the combattant has the slot, false otherwise. */ public function hasSlot(string $slot_name): bool { $combattant_slots = $this->stats['Slots'] ?? ''; $slots_array = array_map('trim', explode(',', $combattant_slots)); foreach ($slots_array as $slot) { $parts = explode(':', $slot); if (count($parts) >= 1 && $parts[0] === $slot_name) { return true; // slot found } } return false; // slot not found } public function getItemCodeInSlot(string $slot_name): ?string { $combattant_slots = $this->stats['Slots'] ?? ''; $slots_array = array_map('trim', explode(',', $combattant_slots)); foreach ($slots_array as $slot) { $parts = explode(':', $slot); if (count($parts) === 2 && $parts[0] === $slot_name) { return $parts[1]; // return the item code } } return null; // no item equipped in this slot } /** * Determine if the combattant can equip a given item. * This is possible if the combattant has the required slot for the item. * The slots are defined in the combattandt's stats under 'Slots'. * There has to be a slot with the same name as the item's slot stat. * For example, if the item has a stat 'Slot' with value 'Weapon', * the combattant must have 'Weapon' in its 'Slots' stat to equip it * * @param string $item_code the code of the item to check. * @return bool True if the item can be equipped, false otherwise. */ public function canEquipItem(string $item_code): bool { // get the item's slot and check if the combattant has this slot $item = Item_Definition::get_item_definition_by_code($item_code); $item_stats = $item->get_stats(); $item_slot = $item_stats['Slot'] ?? null; if ($item_slot === null) { return false; // item has no slot defined } if (!$this->hasSlot($item_slot)) { return false; // combattant does not have the required slot } return true; } /** * Equip an item to the combattant * This removes one instance of the item from the inventory * and adds it to the character's stats in the Slots stat * e.g. if the item has a Slot stat with value "Weapon", * the character's Slots stat will be updated to "Weapon:short_sword" * * @param string $item_code The code of the item to equip. * @return bool True if the item was successfully equipped, false otherwise. */ public function equipItem(string $item_code): bool { if (!$this->canEquipItem($item_code)) { return false; // cannot equip the item } // check if the item is in the inventory if( $this->get_item_count_by_code($item_code) <= 0 ) { return false; // item not in inventory } // get the item's slot $item = Item_Definition::get_item_definition_by_code($item_code); $item_slot = $item->getItemSlot(); if ($item_slot === null) { return false; // item has no slot defined } // unequip any item currently in this slot and add it to the inventory again $this->unEquipSlot($item_slot); // now remove the item from the inventory $this->remove_item_from_inventory($item_code, 1); // add the item to the slot $combattant_slots = $this->stats['Slots'] ?? ''; $slots_array = array_map('trim', explode(',', $combattant_slots)); foreach ($slots_array as &$slot) { $parts = explode(':', $slot); if (count($parts) === 1 && $parts[0] === $item_slot) { $slot = $item_slot . ':' . $item_code; // equip the item break; } } // save the updated slots back to the stats $this->stats['Slots'] = implode(',', $slots_array); // save the combattant to the database if applicable if (method_exists($this, 'save_to_db')) { $this->save_to_db(); } return true; } /** * Get all equipped items as an associative array * with slot names as keys and item codes as values * * @return array An associative array of equipped items. * e.g. ['Weapon' => 'short_sword', 'Armor' => 'leather_armor'] */ public function getEquippedItems(): array { $equipped_items = []; $combattant_slots = $this->stats['Slots'] ?? ''; $slots_array = array_map('trim', explode(',', $combattant_slots)); foreach ($slots_array as $slot) { $parts = explode(':', $slot); if (count($parts) === 2) { $equipped_items[$parts[0]] = $parts[1]; // slot_name => item_code } } return $equipped_items; } /** * Unequip all items in a given slot * This removes the "Equipped" stat from all items in this slot * * @param string $slot_name The name of the slot to unequip. * @return void */ public function unEquipSlot(string $slot_name): void { // check if the combattant has this slot if (!$this->hasSlot($slot_name)) { return; // combattant does not have the slot } // get the item code currently equipped in this slot $current_item_code = $this->getItemCodeInSlot($slot_name); if ($current_item_code !== null) { // add the item back to the inventory $this->add_item_to_inventory( $current_item_code, 1 ); // remove the item from the slot // setting the slot to just the slot name (without item code) $combattant_slots = $this->stats['Slots'] ?? ''; $slots_array = array_map('trim', explode(',', $combattant_slots)); foreach ($slots_array as &$slot) { $parts = explode( ':', $slot ); if ( count( $parts ) === 2 && $parts[0] === $slot_name ) { $slot = $slot_name; // remove the item code break; } } // save the updated slots back to the stats $this->stats['Slots'] = implode( ',', $slots_array ); // save the combattant to the database if applicable if ( method_exists( $this, 'save_to_db' ) ) { $this->save_to_db(); } } } // region Inventory Management /** * Whenever an item is purchased or picked up by a character it is added to the inventory by increasing the count of the item if it already exists in the inventory. * The inventory is an array with entries in the form item_code:count * e.g. sword:2,shield:1,potion:5 * This means the character has 2 swords, 1 shield and 5 potions. * * when equipping an item, we need to remove it from the inventory (decrease the count or remove the entry if count is 0) * and add it to the Slots stat Info of the character. * * If we need to save mutable item data in the future, we have to refactor this so that each item in the inventory is an object with its own id and data. * (instance of Item class). At the moment we only use Item_Definition for item data, which is immutable. */ public function add_item_to_inventory(string $item_code, int $count = 1): void { // check if the item already exists in the inventory foreach ($this->inventory as &$inventory_item) { $parts = explode(':', $inventory_item); if ($parts[0] === $item_code) { // item already exists, increase the count $parts[1] = intval($parts[1]) + $count; $inventory_item = implode(':', $parts); $this->save_to_db(); return; } } // item does not exist, add a new entry $this->inventory[] = $item_code . ':' . $count; $this->save_to_db(); } public function remove_item_from_inventory(string $item_code, int $count = 1): bool { foreach ($this->inventory as $inventory_item ) { $parts = explode(':', $inventory_item); if ($parts[0] == $item_code) { // item found, decrease the count $current_count = intval($parts[1]); if ($current_count <= $count) { // remove the item completely $this->inventory = array_filter($this->inventory, fn($item) => explode(':', $item)[0] !== $item_code); } else { // decrease the count $parts[1] = $current_count - $count; $inventory_item = implode(':', $parts); // update the inventory item in the array $this->inventory = array_map(fn($item) => explode(':', $item)[0] === $item_code ? $inventory_item : $item, $this->inventory); } $this->save_to_db(); return true; } } return false; // item not found } public function get_item_count_by_code(string $code): int { foreach ($this->get_inventory() as $inventory_item) { if ($inventory_item["item_code"] == $code) { return intval($inventory_item["count"]); } } return 0; // item not found } public function get_inventory(): array { $inventory_items = []; foreach ($this->inventory as $inventory_item) { $parts = explode(':', $inventory_item); if (count($parts) === 2) { $inventory_items[] = [ 'item_code' => $parts[0], 'count' => intval($parts[1]) ]; } } return $inventory_items; } /** * sorts the given $inventory_items by name of their Item_Definitions * * @param array $inventory_items (as returned by get_inventory()) * * @return array */ public function sortInventoryByTranslatedName(array $inventory_items): array { // we have $inventory_items in the form {["item_code"] => "xyz", ["count"] => 1} // get all Item_Definitions for the $inventory_items $sorted_inventory_item_definitions = []; $sorted_inventory = []; foreach($inventory_items as $inventory_item) { $item_definition = Item_Definition::get_item_definition_by_code($inventory_item["item_code"]); $sorted_inventory_item_definitions[] = $item_definition; } // sort $sorted_inventory_item_definitions by the name of the item definition usort($sorted_inventory_item_definitions, function($a, $b) { return strcmp($a->name, $b->name); }); // now we have $sorted_inventory_item_definitions sorted by name // we need to create the sorted_inventory array in the same form as $inventory_items // i.e. {["item_code"] => "xyz", ["count"] => 1} // so we need to look up the count for each item_code // we can use the get_item_count_by_code() method for this foreach($sorted_inventory_item_definitions as $item_definition) { $sorted_inventory[] = [ 'item_code' => $item_definition->code, 'count' => $this->get_item_count_by_code($item_definition->code) ]; } return $sorted_inventory; } // endregion Inventory Management public function addXP( int $xp ): string { // if the character has no "XP" stat, we cannot add XP if ( ! isset( $this->stats['XP'] ) ) { return ''; } $this->stats['XP'] += $xp; $this->save_to_db(); /* translators: %d is the amount of XP gained */ return esc_html( sprintf( __( 'You gain %d XP!', 'adventurebook' ), $xp ) ); } public function addWealth( int $amount, string $unit = ''): string { // if the character has no "Wealth" stat, we cannot add wealth if ( ! isset( $this->stats['Wealth'] ) ) { return ''; } $this->stats['Wealth'] += $amount; $this->save_to_db(); /* translators: %1$d is the amount of wealth gained, %2$s is the unit (e.g. gold) */ return esc_html( sprintf( __( 'You gain %1$d %2$s!', 'adventurebook' ), $amount, $unit ) ); } public function getDamage(): int { $damage = 0; // determine damage from equipped weapon // the character must have a stat "Slots" with "Weapon" in it // if a weapon is equipped in the "Weapon" slot, the format is "Weapon:ITEMCODE" // get the "Damage" stat from the item definition $item_code = $this->getItemCodeInSlot("Weapon"); if ($item_code) { $item = Item_Definition::get_item_definition_by_code( $item_code ); if ( $item ) { $damage += intval($item->getStat("Damage")) ?? 0; } } else { // no weapon equipped, use Damage stat if available (e.g. for enemies) $damage += intval($this->getStat("Damage")) ?? 0; } return $damage; } }