| 1 | <?php |
| 2 | |
| 3 | class GameHelper |
| 4 | { |
| 5 | |
| 6 | /** |
| 7 | * Get stat display label from numeric value |
| 8 | */ |
| 9 | public static function getStatLabel($value) |
| 10 | { |
| 11 | $value = max(0, min(100, $value)); |
| 12 | |
| 13 | if ($value >= 90) |
| 14 | return 'godlike'; |
| 15 | if ($value >= 80) |
| 16 | return 'divine'; |
| 17 | if ($value >= 70) |
| 18 | return 'master'; |
| 19 | if ($value >= 60) |
| 20 | return 'fantastic'; |
| 21 | if ($value >= 50) |
| 22 | return 'excellent'; |
| 23 | if ($value >= 40) |
| 24 | return 'good'; |
| 25 | if ($value >= 30) |
| 26 | return 'decent'; |
| 27 | if ($value >= 20) |
| 28 | return 'unskilled'; |
| 29 | if ($value >= 10) |
| 30 | return 'bad'; |
| 31 | return 'terrible'; |
| 32 | } |
| 33 | |
| 34 | /** |
| 35 | * Roll 1d100 under value |
| 36 | */ |
| 37 | public static function rollUnder($value) |
| 38 | { |
| 39 | $value = max(0, min(100, $value)); |
| 40 | $roll = rand(1, 100); |
| 41 | return $roll <= $value; |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * Get age category for display |
| 46 | */ |
| 47 | public static function getAgeCategory($age) |
| 48 | { |
| 49 | if ($age < 0) |
| 50 | return 'egg'; |
| 51 | if ($age < 4) |
| 52 | return 'baby'; |
| 53 | if ($age < 17) |
| 54 | return 'young'; |
| 55 | if ($age < 40) |
| 56 | return 'adult'; |
| 57 | |
| 58 | return 'old'; |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Get sprite name for age category |
| 63 | */ |
| 64 | public static function getAgeSprite($ageCategory) |
| 65 | { |
| 66 | $spriteMap = [ |
| 67 | 'egg' => 'egg', |
| 68 | 'baby' => 'baby', |
| 69 | 'young' => 'young', |
| 70 | 'adult' => 'adult', |
| 71 | 'old' => 'old' |
| 72 | ]; |
| 73 | |
| 74 | return $spriteMap[$ageCategory] ?? 'adult'; |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Get season for month number |
| 79 | */ |
| 80 | public static function getSeason($month) |
| 81 | { |
| 82 | $seasons = ['spring', 'summer', 'autumn', 'winter']; |
| 83 | return $seasons[($month / 3) % 4]; |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * Check if kobold can have old-age injury |
| 88 | */ |
| 89 | public static function shouldCheckOldAgeInjury($age) |
| 90 | { |
| 91 | return $age >= 35; |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Get stat value with age scaling |
| 96 | */ |
| 97 | public static function getEffectiveStat($baseStat, $age) |
| 98 | { |
| 99 | $ageCategory = self::getAgeCategory($age); |
| 100 | |
| 101 | // For babies and eggs, scale by age |
| 102 | if ($ageCategory === 'egg') { |
| 103 | return 0; // Eggs can't do anything |
| 104 | } |
| 105 | |
| 106 | if ($ageCategory === 'baby') { |
| 107 | // Progress from 0 to 4 years = 0% to 100% |
| 108 | $progress = $age / 4; |
| 109 | return (int) ($baseStat * $progress); |
| 110 | } |
| 111 | |
| 112 | // Adults at full value |
| 113 | return $baseStat; |
| 114 | } |
| 115 | |
| 116 | /** |
| 117 | * Get random name from list |
| 118 | */ |
| 119 | public static function getRandomKoboldName() |
| 120 | { |
| 121 | $names = [ |
| 122 | 'Zyx', |
| 123 | 'Krellin', |
| 124 | 'Tik', |
| 125 | 'Borp', |
| 126 | 'Schnax', |
| 127 | 'Grell', |
| 128 | 'Vix', |
| 129 | 'Nok', |
| 130 | 'Pix', |
| 131 | 'Grin', |
| 132 | 'Skitter', |
| 133 | 'Blort', |
| 134 | 'Zap', |
| 135 | 'Clicket', |
| 136 | 'Snort', |
| 137 | 'Gik', |
| 138 | 'Spork', |
| 139 | 'Krunk', |
| 140 | 'Vik', |
| 141 | 'Tock', |
| 142 | 'Blix', |
| 143 | 'Zazzle', |
| 144 | 'Sprocket', |
| 145 | 'Klik', |
| 146 | 'Fizz', |
| 147 | 'Gorp', |
| 148 | 'Skronk', |
| 149 | 'Thrall', |
| 150 | 'Glint', |
| 151 | 'Zink', |
| 152 | 'Plock', |
| 153 | 'Bzzzt' |
| 154 | ]; |
| 155 | |
| 156 | return $names[array_rand($names)]; |
| 157 | } |
| 158 | |
| 159 | /** |
| 160 | * Generate random stats |
| 161 | */ |
| 162 | public static function generateRandomStats() |
| 163 | { |
| 164 | $stats = []; |
| 165 | $statNames = ['vitality', 'intelligence', 'tinkering', 'medicine', 'crafting', 'personality', 'will', 'fighting']; |
| 166 | |
| 167 | foreach ($statNames as $stat) { |
| 168 | $stats[$stat] = rand(0, 100); |
| 169 | } |
| 170 | |
| 171 | return $stats; |
| 172 | } |
| 173 | |
| 174 | /** |
| 175 | * Format story text (for narrative generation) |
| 176 | */ |
| 177 | public static function formatStory($text) |
| 178 | { |
| 179 | // Could add markdown parsing, formatting, etc. |
| 180 | return nl2br(htmlspecialchars($text)); |
| 181 | } |
| 182 | |
| 183 | /** |
| 184 | * Get injury cure type |
| 185 | */ |
| 186 | public static function getInjuryCure($injuryCategory) |
| 187 | { |
| 188 | $cureMap = [ |
| 189 | 'wound' => 'basic_medicine', |
| 190 | 'fracture' => 'bone_setting', |
| 191 | 'infection' => 'healing_herbs', |
| 192 | 'poison' => 'antidote', |
| 193 | 'burn' => 'burn_salve', |
| 194 | 'malnourished' => 'food', |
| 195 | 'old-age' => null |
| 196 | ]; |
| 197 | |
| 198 | return $cureMap[$injuryCategory] ?? null; |
| 199 | } |
| 200 | |
| 201 | /** |
| 202 | * Check if injury is treatable |
| 203 | */ |
| 204 | public static function isInjuryTreatable($injuryCategory) |
| 205 | { |
| 206 | return self::getInjuryCure($injuryCategory) !== null; |
| 207 | } |
| 208 | |
| 209 | /** |
| 210 | * Capitalize words |
| 211 | */ |
| 212 | public static function humanize($text) |
| 213 | { |
| 214 | return ucfirst(str_replace('_', ' ', $text)); |
| 215 | } |
| 216 | |
| 217 | /** |
| 218 | * Load item from JSON file by ID |
| 219 | * |
| 220 | * @param string $itemId The filename without extension (e.g. 'book') |
| 221 | * @return Item|null The loaded item or null if file not found |
| 222 | */ |
| 223 | public static function loadItem($itemId) |
| 224 | { |
| 225 | require_once __DIR__ . '/../Classes/Item.php'; |
| 226 | |
| 227 | $itemPath = __DIR__ . '/../../assets/items/' . $itemId . '.json'; |
| 228 | |
| 229 | if (!file_exists($itemPath)) { |
| 230 | return null; |
| 231 | } |
| 232 | |
| 233 | $json = file_get_contents($itemPath); |
| 234 | $data = json_decode($json, true); |
| 235 | |
| 236 | if (!$data) { |
| 237 | return null; |
| 238 | } |
| 239 | |
| 240 | // Create Item with filename as ID |
| 241 | $item = new Item( |
| 242 | $itemId, |
| 243 | $data['name'] ?? '', |
| 244 | $data['description'] ?? '', |
| 245 | $data['itemType'] ?? 'generic', |
| 246 | $data['statModifications'] ?? [] |
| 247 | ); |
| 248 | |
| 249 | return $item; |
| 250 | } |
| 251 | |
| 252 | /** |
| 253 | * Load injury from JSON file by ID |
| 254 | * |
| 255 | * @param string $injuryId filename without extension (e.g. 'brokenLeg') |
| 256 | * @return array|null normalized injury array or null if not found |
| 257 | */ |
| 258 | public static function loadInjury(string $injuryId) |
| 259 | { |
| 260 | $injuryPath = __DIR__ . '/../../assets/injuries/' . $injuryId . '.json'; |
| 261 | |
| 262 | if (!file_exists($injuryPath)) { |
| 263 | return null; |
| 264 | } |
| 265 | |
| 266 | $json = file_get_contents($injuryPath); |
| 267 | $data = json_decode($json, true); |
| 268 | if (!$data) |
| 269 | return null; |
| 270 | |
| 271 | // Normalize to snake_case keys matching internal storage |
| 272 | $inj = []; |
| 273 | $inj['id'] = $injuryId; |
| 274 | $inj['injury_type'] = $data['injuryType'] ?? ($data['injury_type'] ?? $injuryId); |
| 275 | $inj['description'] = $data['description'] ?? ''; |
| 276 | $inj['category'] = $data['category'] ?? null; |
| 277 | $inj['severity'] = $data['severity'] ?? ($data['severity'] ?? 1); |
| 278 | $inj['stat_modifications'] = $data['statModifications'] ?? $data['stat_modifications'] ?? []; |
| 279 | $inj['required_medicine'] = Injury::getCureForCategory($inj['category']); |
| 280 | |
| 281 | return $inj; |
| 282 | } |
| 283 | |
| 284 | public static function findBestKoboldForStat(array $kobolds, string $skillId): Kobold |
| 285 | { |
| 286 | $highest = -1; |
| 287 | $selected = null; |
| 288 | |
| 289 | for ($k = 0; $k < count($kobolds); $k++) { |
| 290 | $kobold = $kobolds[$k]; |
| 291 | if ($kobold->getStat($skillId) > $highest) { |
| 292 | $highest = $kobold->getStat($skillId); |
| 293 | $selected = $kobold; |
| 294 | } |
| 295 | } |
| 296 | |
| 297 | return $selected; |
| 298 | } |
| 299 | } |