| 1 | <?php |
| 2 | |
| 3 | require_once __DIR__ . '/../../src/System/SaveManager.php'; |
| 4 | |
| 5 | class Kobold |
| 6 | { |
| 7 | private $id; |
| 8 | private $userId; |
| 9 | private $saveId; |
| 10 | private $name; |
| 11 | private $age; |
| 12 | private $stats = []; |
| 13 | private $spriteMorph; |
| 14 | private $isLeader; |
| 15 | private $isAlive = true; |
| 16 | private $isExpelled = false; |
| 17 | private $injuries = []; |
| 18 | private $items = []; |
| 19 | private $fatherId; |
| 20 | private $motherId; |
| 21 | private $saveManager; |
| 22 | |
| 23 | private static $statNames = [ |
| 24 | 'vitality', |
| 25 | 'intelligence', |
| 26 | 'tinkering', |
| 27 | 'medicine', |
| 28 | 'crafting', |
| 29 | 'personality', |
| 30 | 'will' |
| 31 | ]; |
| 32 | |
| 33 | public function __construct($id, $userId, $saveId, $name, $age, $spriteMorph, $isLeader = false) |
| 34 | { |
| 35 | $this->id = $id; |
| 36 | $this->userId = $userId; |
| 37 | $this->saveId = $saveId; |
| 38 | $this->name = $name; |
| 39 | $this->age = $age; |
| 40 | $this->spriteMorph = $spriteMorph; |
| 41 | $this->isLeader = $isLeader; |
| 42 | |
| 43 | $this->colourPrimary = sprintf( |
| 44 | "#%02X%02X%02X", |
| 45 | random_int(50, 230), |
| 46 | random_int(50, 230), |
| 47 | random_int(50, 230) |
| 48 | ); |
| 49 | |
| 50 | $this->colourSecondary = sprintf( |
| 51 | "#%02X%02X%02X", |
| 52 | random_int(50, 230), |
| 53 | random_int(50, 230), |
| 54 | random_int(50, 230) |
| 55 | ); |
| 56 | |
| 57 | $this->colourTertiary = sprintf( |
| 58 | "#%02X%02X%02X", |
| 59 | random_int(50, 230), |
| 60 | random_int(50, 230), |
| 61 | random_int(50, 230) |
| 62 | ); |
| 63 | |
| 64 | $this->saveManager = new SaveManager(); |
| 65 | |
| 66 | // Initialize stats with random values |
| 67 | foreach (self::$statNames as $stat) { |
| 68 | $this->stats[$stat] = rand(0, 100); |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | public static function generate($userId, $saveId, $age = null) |
| 73 | { |
| 74 | if ($age === null) { |
| 75 | $age = rand(5, 20); |
| 76 | } |
| 77 | |
| 78 | $spriteMorph = self::getRandomSpriteMorph(); |
| 79 | $kobold = new self(uniqid('kob_'), $userId, $saveId, '', $age, $spriteMorph); |
| 80 | |
| 81 | // Copy and transform sprite from assets to save folder |
| 82 | $kobold->initializeSprite(); |
| 83 | |
| 84 | return $kobold; |
| 85 | } |
| 86 | |
| 87 | private static function getRandomSpriteMorph() |
| 88 | { |
| 89 | $morphsPath = __DIR__ . '/../../assets/sprites'; |
| 90 | if (!is_dir($morphsPath)) { |
| 91 | return 'default'; |
| 92 | } |
| 93 | |
| 94 | $morphs = array_filter(scandir($morphsPath), function ($item) use ($morphsPath) { |
| 95 | return is_dir($morphsPath . '/' . $item) && $item[0] !== '.'; |
| 96 | }); |
| 97 | |
| 98 | $morphs = array_values($morphs); |
| 99 | |
| 100 | return !empty($morphs) ? array_values($morphs)[array_rand($morphs)] : 'default'; |
| 101 | } |
| 102 | |
| 103 | public static function fromArray($data, $userId, $saveId) |
| 104 | { |
| 105 | $kobold = new self( |
| 106 | $data['id'] ?? null, |
| 107 | $userId, |
| 108 | $saveId, |
| 109 | $data['name'] ?? '', |
| 110 | $data['age'] ?? 0, |
| 111 | $data['sprite_morph'] ?? 'default', |
| 112 | $data['is_leader'] ?? false |
| 113 | ); |
| 114 | |
| 115 | if (isset($data['stats'])) { |
| 116 | $kobold->stats = $data['stats']; |
| 117 | } |
| 118 | |
| 119 | $kobold->isAlive = $data['is_alive'] ?? true; |
| 120 | $kobold->isExpelled = $data['is_expelled'] ?? false; |
| 121 | $kobold->injuries = $data['injuries'] ?? []; |
| 122 | $kobold->items = $data['items'] ?? []; |
| 123 | $kobold->fatherId = $data['father_id'] ?? null; |
| 124 | $kobold->motherId = $data['mother_id'] ?? null; |
| 125 | |
| 126 | return $kobold; |
| 127 | } |
| 128 | |
| 129 | public function toArray() |
| 130 | { |
| 131 | return [ |
| 132 | 'id' => $this->id, |
| 133 | 'name' => $this->name, |
| 134 | 'age' => $this->age, |
| 135 | 'stats' => $this->stats, |
| 136 | 'sprite_morph' => $this->spriteMorph, |
| 137 | 'is_leader' => $this->isLeader, |
| 138 | 'is_alive' => $this->isAlive, |
| 139 | 'is_expelled' => $this->isExpelled, |
| 140 | 'injuries' => $this->injuries, |
| 141 | 'items' => $this->items, |
| 142 | 'father_id' => $this->fatherId, |
| 143 | 'mother_id' => $this->motherId |
| 144 | ]; |
| 145 | } |
| 146 | |
| 147 | public function save() |
| 148 | { |
| 149 | // Kobolds are saved as part of the Tribe's kobolds.json |
| 150 | // Sprite files are stored in: |
| 151 | // /data/saves/{userId}/kobolds/{koboldId}/sprites/ |
| 152 | } |
| 153 | |
| 154 | /** |
| 155 | * Initialize sprite by copying base sprite from assets and applying transformations |
| 156 | * Called on kobold generation to populate the save folder |
| 157 | */ |
| 158 | private function initializeSprite() |
| 159 | { |
| 160 | $spriteDir = $this->saveManager->getKoboldSpritePath($this->userId, $this->id); |
| 161 | $ageSprite = $this->getAgeSpriteName(); |
| 162 | $sourceFile = __DIR__ . '/../../assets/sprites/' . $this->spriteMorph . '/' . $ageSprite . '.png'; |
| 163 | $destFile = $spriteDir . '/' . $ageSprite . '.png'; |
| 164 | |
| 165 | // Copy base sprite from assets |
| 166 | if (file_exists($sourceFile)) { |
| 167 | copy($sourceFile, $destFile); |
| 168 | } |
| 169 | |
| 170 | // Apply transformations to the sprite |
| 171 | $this->transformSprite(); |
| 172 | } |
| 173 | |
| 174 | /** |
| 175 | * Get the sprite filename for current age |
| 176 | */ |
| 177 | public function getAgeSpriteName() |
| 178 | { |
| 179 | $ageCategory = $this->getAgeCategory(); |
| 180 | $spriteMap = [ |
| 181 | 'egg' => 'egg', |
| 182 | 'baby' => 'baby', |
| 183 | 'adult' => 'adult', |
| 184 | 'old' => 'old', |
| 185 | 'very_old' => 'old' // Reuse old sprite for very_old |
| 186 | ]; |
| 187 | |
| 188 | return $spriteMap[$ageCategory] ?? 'adult'; |
| 189 | } |
| 190 | |
| 191 | /** |
| 192 | * Get path to the kobold's sprite file for current age |
| 193 | */ |
| 194 | public function getSpritePath() |
| 195 | { |
| 196 | $spriteDir = $this->saveManager->getKoboldSpritePath($this->userId, $this->id); |
| 197 | $ageSprite = $this->getAgeSpriteName(); |
| 198 | return $spriteDir . '/' . $ageSprite . '.png'; |
| 199 | } |
| 200 | |
| 201 | public function getId() |
| 202 | { |
| 203 | return $this->id; |
| 204 | } |
| 205 | |
| 206 | public function getName() |
| 207 | { |
| 208 | return $this->name; |
| 209 | } |
| 210 | |
| 211 | public function setName($name) |
| 212 | { |
| 213 | $this->name = $name; |
| 214 | } |
| 215 | |
| 216 | public function getAge() |
| 217 | { |
| 218 | return $this->age; |
| 219 | } |
| 220 | |
| 221 | public function setAge($age) |
| 222 | { |
| 223 | $this->age = max(0, $age); |
| 224 | } |
| 225 | |
| 226 | public function incrementAge() |
| 227 | { |
| 228 | $this->age++; |
| 229 | } |
| 230 | |
| 231 | public function getAgeCategory() |
| 232 | { |
| 233 | if ($this->age < 0) |
| 234 | return 'egg'; |
| 235 | if ($this->age < 4) |
| 236 | return 'baby'; |
| 237 | if ($this->age < 35) |
| 238 | return 'young'; |
| 239 | if ($this->age < 60) |
| 240 | return 'adult'; |
| 241 | return 'old'; |
| 242 | } |
| 243 | |
| 244 | public function getAgeProgress() |
| 245 | { |
| 246 | // Returns 0-1 indicating progress to next age tier |
| 247 | $ageTiers = [-1, 0, 4, 35, 60]; |
| 248 | |
| 249 | for ($i = 0; $i < count($ageTiers) - 1; $i++) { |
| 250 | $tierStart = $ageTiers[$i]; |
| 251 | $tierEnd = $ageTiers[$i + 1]; |
| 252 | |
| 253 | if ($this->age >= $tierStart && $this->age < $tierEnd) { |
| 254 | $duration = $tierEnd - $tierStart; |
| 255 | $progress = ($this->age - $tierStart) / $duration; |
| 256 | return $progress; |
| 257 | } |
| 258 | } |
| 259 | |
| 260 | return 1; |
| 261 | } |
| 262 | |
| 263 | public function getStat($statName) |
| 264 | { |
| 265 | if (!isset($this->stats[$statName])) { |
| 266 | return null; |
| 267 | } |
| 268 | |
| 269 | $baseStat = $this->stats[$statName]; |
| 270 | |
| 271 | // Scale stats for non-adult kobolds |
| 272 | if ($this->getAgeCategory() === 'baby' || $this->getAgeCategory() === 'egg') { |
| 273 | $baseStat = (int) ($baseStat * $this->getAgeProgress()); |
| 274 | } |
| 275 | |
| 276 | return $baseStat; |
| 277 | } |
| 278 | |
| 279 | public function setStat($statName, $value) |
| 280 | { |
| 281 | if (isset($this->stats[$statName])) { |
| 282 | $this->stats[$statName] = max(0, min(100, $value)); |
| 283 | } |
| 284 | } |
| 285 | |
| 286 | public function checkStat($statName) |
| 287 | { |
| 288 | $stat = $this->getStat($statName); |
| 289 | |
| 290 | // Apply injury modifications |
| 291 | $totalInjurySeverity = array_sum(array_column($this->injuries, 'severity', null)); |
| 292 | if ($totalInjurySeverity === null) { |
| 293 | $totalInjurySeverity = 0; |
| 294 | } |
| 295 | |
| 296 | $stat -= $totalInjurySeverity; |
| 297 | $stat = max(0, min(100, $stat)); |
| 298 | |
| 299 | // Roll under 1d100 |
| 300 | $roll = rand(1, 100); |
| 301 | return $roll <= $stat; |
| 302 | } |
| 303 | |
| 304 | public function addInjury($injuryType, $category, $severity = 1) |
| 305 | { |
| 306 | $id = uniqid('inj_'); |
| 307 | |
| 308 | $this->injuries[] = [ |
| 309 | 'id' => $id, |
| 310 | 'kobold_id' => $this->id, |
| 311 | 'injury_type' => $injuryType, |
| 312 | 'category' => $category, |
| 313 | 'severity' => $severity |
| 314 | ]; |
| 315 | } |
| 316 | |
| 317 | public function removeInjury($injuryId) |
| 318 | { |
| 319 | $this->injuries = array_filter($this->injuries, fn($inj) => $inj['id'] !== $injuryId); |
| 320 | } |
| 321 | |
| 322 | public function getInjuries() |
| 323 | { |
| 324 | return $this->injuries; |
| 325 | } |
| 326 | |
| 327 | public function getTotalInjurySeverity() |
| 328 | { |
| 329 | $sum = 0; |
| 330 | foreach ($this->injuries as $injury) { |
| 331 | $sum += $injury['severity'] ?? 0; |
| 332 | } |
| 333 | return $sum; |
| 334 | } |
| 335 | |
| 336 | public function isDead() |
| 337 | { |
| 338 | return $this->getTotalInjurySeverity() >= $this->getStat('vitality') || !$this->isAlive; |
| 339 | } |
| 340 | |
| 341 | public function isExpelled() |
| 342 | { |
| 343 | return $this->isExpelled; |
| 344 | } |
| 345 | |
| 346 | public function setExpelled($expelled) |
| 347 | { |
| 348 | $this->isExpelled = $expelled; |
| 349 | } |
| 350 | |
| 351 | public function die() |
| 352 | { |
| 353 | $this->isAlive = false; |
| 354 | } |
| 355 | |
| 356 | public function getSpriteMorph() |
| 357 | { |
| 358 | return $this->spriteMorph; |
| 359 | } |
| 360 | |
| 361 | public function getStatLabel($statValue) |
| 362 | { |
| 363 | if ($statValue >= 90) |
| 364 | return 'godlike'; |
| 365 | if ($statValue >= 80) |
| 366 | return 'divine'; |
| 367 | if ($statValue >= 70) |
| 368 | return 'master'; |
| 369 | if ($statValue >= 60) |
| 370 | return 'fantastic'; |
| 371 | if ($statValue >= 50) |
| 372 | return 'excellent'; |
| 373 | if ($statValue >= 40) |
| 374 | return 'good'; |
| 375 | if ($statValue >= 30) |
| 376 | return 'decent'; |
| 377 | if ($statValue >= 20) |
| 378 | return 'unskilled'; |
| 379 | if ($statValue >= 10) |
| 380 | return 'bad'; |
| 381 | return 'terrible'; |
| 382 | } |
| 383 | |
| 384 | public function setParents($fatherId, $motherId) |
| 385 | { |
| 386 | $this->fatherId = $fatherId; |
| 387 | $this->motherId = $motherId; |
| 388 | } |
| 389 | |
| 390 | public function getParents() |
| 391 | { |
| 392 | return ['father_id' => $this->fatherId, 'mother_id' => $this->motherId]; |
| 393 | } |
| 394 | |
| 395 | /** |
| 396 | * Apply transformations to sprite (injuries, age coloring, leadership marks) |
| 397 | * Uses PHP GD library to modify the sprite image |
| 398 | */ |
| 399 | private function transformSprite() |
| 400 | { |
| 401 | $spritePath = $this->getSpritePath(); |
| 402 | |
| 403 | if (!file_exists($spritePath)) { |
| 404 | return; // Base sprite not created yet |
| 405 | } |
| 406 | |
| 407 | // Load the image |
| 408 | $image = imagecreatefrompng($spritePath); |
| 409 | if (!$image) { |
| 410 | return; // Failed to load image |
| 411 | } |
| 412 | |
| 413 | // Apply injury markings if kobold has taken damage |
| 414 | $vitalityStat = $this->getStat('vitality'); |
| 415 | if ($vitalityStat < 50) { |
| 416 | $this->applyInjuryMarkings($image); |
| 417 | } |
| 418 | |
| 419 | // Apply age-based color adjustments (desaturate for elderly) |
| 420 | if ($this->getAgeCategory() === 'old' || $this->getAgeCategory() === 'very_old') { |
| 421 | $this->desaturateImage($image); |
| 422 | } |
| 423 | |
| 424 | // Save modified image back to the sprite file |
| 425 | imagepng($image, $spritePath); |
| 426 | imagedestroy($image); |
| 427 | } |
| 428 | |
| 429 | /** |
| 430 | * Draw injury marks (red scars) on the sprite |
| 431 | * Number of marks increases with injury level |
| 432 | */ |
| 433 | private function applyInjuryMarkings(&$image) |
| 434 | { |
| 435 | $red = imagecolorallocate($image, 255, 0, 0); |
| 436 | $darkRed = imagecolorallocate($image, 139, 0, 0); |
| 437 | |
| 438 | // Number of scars based on vitality stat |
| 439 | $vitalityStat = $this->getStat('vitality'); |
| 440 | $scarCount = intval((50 - $vitalityStat) / 10) + 1; |
| 441 | $scarCount = min(5, $scarCount); |
| 442 | |
| 443 | $imageWidth = imagesx($image); |
| 444 | $imageHeight = imagesy($image); |
| 445 | |
| 446 | // Draw scars at various positions |
| 447 | for ($i = 0; $i < $scarCount; $i++) { |
| 448 | $x = rand(10, $imageWidth - 10); |
| 449 | $y = rand(10, $imageHeight - 10); |
| 450 | $length = rand(5, 15); |
| 451 | |
| 452 | // Draw a jagged line scar |
| 453 | imageline($image, $x, $y, $x + $length, $y, $darkRed); |
| 454 | imageline($image, $x, $y + 1, $x + $length, $y + 1, $red); |
| 455 | } |
| 456 | } |
| 457 | |
| 458 | /** |
| 459 | * Desaturate the sprite image for elderly kobolds |
| 460 | * Reduces color saturation to make them appear older |
| 461 | */ |
| 462 | private function desaturateImage(&$image) |
| 463 | { |
| 464 | $imageWidth = imagesx($image); |
| 465 | $imageHeight = imagesy($image); |
| 466 | |
| 467 | for ($y = 0; $y < $imageHeight; $y++) { |
| 468 | for ($x = 0; $x < $imageWidth; $x++) { |
| 469 | $rgb = imagecolorat($image, $x, $y); |
| 470 | |
| 471 | // Extract color components |
| 472 | $r = ($rgb >> 16) & 0xFF; |
| 473 | $g = ($rgb >> 8) & 0xFF; |
| 474 | $b = $rgb & 0xFF; |
| 475 | $a = ($rgb >> 24) & 0xFF; |
| 476 | |
| 477 | // Calculate grayscale value using luminance formula |
| 478 | $gray = intval(0.299 * $r + 0.587 * $g + 0.114 * $b); |
| 479 | |
| 480 | // Blend original color with grayscale (60% desaturated) |
| 481 | $newR = intval($r * 0.4 + $gray * 0.6); |
| 482 | $newG = intval($g * 0.4 + $gray * 0.6); |
| 483 | $newB = intval($b * 0.4 + $gray * 0.6); |
| 484 | |
| 485 | $newColor = imagecolorallocatealpha($image, $newR, $newG, $newB, $a); |
| 486 | imagesetpixel($image, $x, $y, $newColor); |
| 487 | } |
| 488 | } |
| 489 | } |
| 490 | } |