death and old age
@@ -0,0 +1,16 @@
1
{
2
"injuryType": "Bad back",
3
"description": "This kobold's back isn't what it used to be.",
4
"category": "old-age",
5
"severity": 2,
6
"statModifications": {
7
"vitality": -4,
8
"intelligence": 0,
9
"tinkering": -1,
10
"medicine": 0,
11
"crafting": -1,
12
"personality": -1,
13
"will": -2,
14
"fighting": -2
15
}
16
}
@@ -0,0 +1,16 @@1
{2
"injuryType": "Bad back",3
"description": "This kobold's back isn't what it used to be.",4
"category": "old-age",5
"severity": 2,6
"statModifications": {7
"vitality": -4,8
"intelligence": 0,9
"tinkering": -1,10
"medicine": 0,11
"crafting": -1,12
"personality": -1,13
"will": -2,14
"fighting": -215
}16
}@@ -0,0 +1,16 @@
1
{
2
"injuryType": "Bad liver",
3
"description": "This kobold's liver is failing, causing weakness and malaise.",
4
"category": "old-age",
5
"severity": 3,
6
"statModifications": {
7
"vitality": -5,
8
"intelligence": -1,
9
"tinkering": 0,
10
"medicine": 0,
11
"crafting": 0,
12
"personality": -1,
13
"will": -2,
14
"fighting": -2
15
}
16
}
@@ -0,0 +1,16 @@1
{2
"injuryType": "Bad liver",3
"description": "This kobold's liver is failing, causing weakness and malaise.",4
"category": "old-age",5
"severity": 3,6
"statModifications": {7
"vitality": -5,8
"intelligence": -1,9
"tinkering": 0,10
"medicine": 0,11
"crafting": 0,12
"personality": -1,13
"will": -2,14
"fighting": -215
}16
}@@ -10,6 +10,7 @@
10
10
"medicine": 0,
11
11
"crafting": -3,
12
12
"personality": -1,
13
"will": -2
13
"will": -2,
14
"fighting": -5
14
15
}
15
16
}
@@ -10,6 +10,7 @@10
"medicine": 0,10
"medicine": 0,11
"crafting": -3,11
"crafting": -3,12
"personality": -1,12
"personality": -1,13
"will": -213
"will": -2,14
"fighting": -514
}15
}15
}16
}@@ -0,0 +1,16 @@
1
{
2
"injuryType": "Chronic fatigue",
3
"description": "This kobold is always tired, struggling to keep up with tribe life.",
4
"category": "old-age",
5
"severity": 3,
6
"statModifications": {
7
"vitality": -4,
8
"intelligence": 0,
9
"tinkering": -2,
10
"medicine": 0,
11
"crafting": -2,
12
"personality": -1,
13
"will": -2,
14
"fighting": -4
15
}
16
}
@@ -0,0 +1,16 @@1
{2
"injuryType": "Chronic fatigue",3
"description": "This kobold is always tired, struggling to keep up with tribe life.",4
"category": "old-age",5
"severity": 3,6
"statModifications": {7
"vitality": -4,8
"intelligence": 0,9
"tinkering": -2,10
"medicine": 0,11
"crafting": -2,12
"personality": -1,13
"will": -2,14
"fighting": -415
}16
}@@ -0,0 +1,16 @@
1
{
2
"injuryType": "Dementia",
3
"description": "This kobold is often confused and forgetful, struggling with memory and focus.",
4
"category": "old-age",
5
"severity": 4,
6
"statModifications": {
7
"vitality": 0,
8
"intelligence": -4,
9
"tinkering": -2,
10
"medicine": -2,
11
"crafting": -2,
12
"personality": -2,
13
"will": -3,
14
"fighting": -2
15
}
16
}
@@ -0,0 +1,16 @@1
{2
"injuryType": "Dementia",3
"description": "This kobold is often confused and forgetful, struggling with memory and focus.",4
"category": "old-age",5
"severity": 4,6
"statModifications": {7
"vitality": 0,8
"intelligence": -4,9
"tinkering": -2,10
"medicine": -2,11
"crafting": -2,12
"personality": -2,13
"will": -3,14
"fighting": -215
}16
}@@ -0,0 +1,16 @@
1
{
2
"injuryType": "Limp",
3
"description": "This kobold walks with a limp, slowing them down and making movement painful.",
4
"category": "old-age",
5
"severity": 2,
6
"statModifications": {
7
"vitality": -2,
8
"intelligence": 0,
9
"tinkering": 0,
10
"medicine": 0,
11
"crafting": 0,
12
"personality": 0,
13
"will": -1,
14
"fighting": -2
15
}
16
}
@@ -0,0 +1,16 @@1
{2
"injuryType": "Limp",3
"description": "This kobold walks with a limp, slowing them down and making movement painful.",4
"category": "old-age",5
"severity": 2,6
"statModifications": {7
"vitality": -2,8
"intelligence": 0,9
"tinkering": 0,10
"medicine": 0,11
"crafting": 0,12
"personality": 0,13
"will": -1,14
"fighting": -215
}16
}@@ -0,0 +1,16 @@
1
{
2
"injuryType": "Poor eyesight",
3
"description": "This kobold struggles to see clearly, making tasks more difficult.",
4
"category": "old-age",
5
"severity": 2,
6
"statModifications": {
7
"vitality": 0,
8
"intelligence": 0,
9
"tinkering": -2,
10
"medicine": 0,
11
"crafting": -2,
12
"personality": -2,
13
"will": 0,
14
"fighting": -5
15
}
16
}
@@ -0,0 +1,16 @@1
{2
"injuryType": "Poor eyesight",3
"description": "This kobold struggles to see clearly, making tasks more difficult.",4
"category": "old-age",5
"severity": 2,6
"statModifications": {7
"vitality": 0,8
"intelligence": 0,9
"tinkering": -2,10
"medicine": 0,11
"crafting": -2,12
"personality": -2,13
"will": 0,14
"fighting": -515
}16
}@@ -0,0 +1,16 @@
1
{
2
"injuryType": "Poor hearing",
3
"description": "This kobold has trouble hearing, often missing important details.",
4
"category": "old-age",
5
"severity": 2,
6
"statModifications": {
7
"vitality": 0,
8
"intelligence": -1,
9
"tinkering": 0,
10
"medicine": 0,
11
"crafting": 0,
12
"personality": -3,
13
"will": 0,
14
"fighting": -3
15
}
16
}
@@ -0,0 +1,16 @@1
{2
"injuryType": "Poor hearing",3
"description": "This kobold has trouble hearing, often missing important details.",4
"category": "old-age",5
"severity": 2,6
"statModifications": {7
"vitality": 0,8
"intelligence": -1,9
"tinkering": 0,10
"medicine": 0,11
"crafting": 0,12
"personality": -3,13
"will": 0,14
"fighting": -315
}16
}@@ -132,7 +132,7 @@ class Kobold
132
132
return !empty($morphs) ? array_values($morphs)[array_rand($morphs)] : 'default';
133
133
}
134
134
135
private static function generateRandomName()
135
public static function generateRandomName()
136
136
{
137
137
// Kobold name syllables
138
138
$firstSyllables = [
@@ -132,7 +132,7 @@ class Kobold132
return !empty($morphs) ? array_values($morphs)[array_rand($morphs)] : 'default';132
return !empty($morphs) ? array_values($morphs)[array_rand($morphs)] : 'default';133
}133
}134
134
135
private static function generateRandomName()135
public static function generateRandomName()136
{136
{137
// Kobold name syllables137
// Kobold name syllables138
$firstSyllables = [138
$firstSyllables = [@@ -129,14 +129,18 @@ class Tribe
129
129
// Save kobolds
130
130
$koboldsData = [];
131
131
foreach ($this->kobolds as $kobold) {
132
$kobold->save();
133
$koboldsData[] = $kobold->toArray();
132
if (!$kobold->isDead()) {
133
$kobold->save();
134
$koboldsData[] = $kobold->toArray();
135
}
134
136
}
135
137
136
138
$this->saveManager->saveJson($savePath . '/kobolds.json', $koboldsData);
137
139
138
140
// Save items
139
141
$this->saveManager->saveJson($savePath . '/items.json', $this->storage);
142
143
GameHelper::cleanupUnusedKoboldSprites($this->userId);
140
144
}
141
145
142
146
// Getters and Setters
@@ -415,25 +419,10 @@ class Tribe
415
419
$kobold->incrementAge();
416
420
417
421
if ($kobold->getAge() >= 0) {
422
$kobold->setName(Kobold::generateRandomName());
418
423
$foodStory .= "An egg hatched! {$kobold->getName()} was born.\n";
419
424
}
420
425
421
// Check for old-age injuries
422
if ($kobold->getAge() >= 35 && $this->shouldAddOldAgeInjury($kobold)) {
423
$oldAgeInjuries = ['bad_back', 'bad_liver', 'poor_eyesight', 'dementia'];
424
$injury = $oldAgeInjuries[array_rand($oldAgeInjuries)];
425
$kobold->addInjury($injury, 'old-age', 1);
426
}
427
428
// Old-age death roll: if roll < (age + total injury severity), kobold dies
429
if ($kobold->getAge() >= 35) {
430
$roll = rand(1, 100);
431
$totalSeverity = $kobold->getTotalInjurySeverity();
432
if ($roll < ($kobold->getAge() + $totalSeverity)) {
433
$kobold->die();
434
}
435
}
436
437
426
$kobold->save();
438
427
}
439
428
}
@@ -448,9 +437,9 @@ class Tribe
448
437
449
438
// Check for old-age injuries
450
439
if ($kobold->getAge() >= 35 && $this->shouldAddOldAgeInjury($kobold)) {
451
$oldAgeInjuries = ['bad_back', 'bad_liver', 'poor_eyesight', 'dementia'];
440
$oldAgeInjuries = ['bad_back', 'bad_liver', 'poor_eyesight', 'dementia', 'limp', 'poor_hearing', 'chronic_fatigue'];
452
441
$injury = $oldAgeInjuries[array_rand($oldAgeInjuries)];
453
$kobold->addInjury($injury, 'old-age', 1);
442
$kobold->addInjury($injury);
454
443
}
455
444
456
445
// Old-age death roll: if roll < (age + total injury severity), kobold dies
@@ -458,6 +447,7 @@ class Tribe
458
447
$roll = rand(1, 100);
459
448
$totalSeverity = $kobold->getTotalInjurySeverity();
460
449
if ($roll < ($kobold->getAge() + $totalSeverity)) {
450
$foodStory .= $kobold->getName() . " dies of old age.\n";
461
451
$kobold->die();
462
452
}
463
453
}
@@ -468,6 +458,12 @@ class Tribe
468
458
}
469
459
$foodStory .= "\nIt's {$this->currentSeason}.\n";
470
460
461
foreach ($this->kobolds as $kobold) {
462
if ($kobold->isDead()) {
463
$story = $story . "\n" . $kobold->getName() . " has died.";
464
}
465
}
466
471
467
$this->save();
472
468
473
469
return $foodStory;
@@ -129,14 +129,18 @@ class Tribe129
// Save kobolds129
// Save kobolds130
$koboldsData = [];130
$koboldsData = [];131
foreach ($this->kobolds as $kobold) {131
foreach ($this->kobolds as $kobold) {132
$kobold->save();132
if (!$kobold->isDead()) {133
$koboldsData[] = $kobold->toArray();133
$kobold->save();134
$koboldsData[] = $kobold->toArray();135
}134
}136
}135
137
136
$this->saveManager->saveJson($savePath . '/kobolds.json', $koboldsData);138
$this->saveManager->saveJson($savePath . '/kobolds.json', $koboldsData);137
139
138
// Save items140
// Save items139
$this->saveManager->saveJson($savePath . '/items.json', $this->storage);141
$this->saveManager->saveJson($savePath . '/items.json', $this->storage);142
143
GameHelper::cleanupUnusedKoboldSprites($this->userId);140
}144
}141
145
142
// Getters and Setters146
// Getters and Setters@@ -415,25 +419,10 @@ class Tribe415
$kobold->incrementAge();419
$kobold->incrementAge();416
420
417
if ($kobold->getAge() >= 0) {421
if ($kobold->getAge() >= 0) {422
$kobold->setName(Kobold::generateRandomName());418
$foodStory .= "An egg hatched! {$kobold->getName()} was born.\n";423
$foodStory .= "An egg hatched! {$kobold->getName()} was born.\n";419
}424
}420
425
421
// Check for old-age injuries422
if ($kobold->getAge() >= 35 && $this->shouldAddOldAgeInjury($kobold)) {423
$oldAgeInjuries = ['bad_back', 'bad_liver', 'poor_eyesight', 'dementia'];424
$injury = $oldAgeInjuries[array_rand($oldAgeInjuries)];425
$kobold->addInjury($injury, 'old-age', 1);426
}427
428
// Old-age death roll: if roll < (age + total injury severity), kobold dies429
if ($kobold->getAge() >= 35) {430
$roll = rand(1, 100);431
$totalSeverity = $kobold->getTotalInjurySeverity();432
if ($roll < ($kobold->getAge() + $totalSeverity)) {433
$kobold->die();434
}435
}436
437
$kobold->save();426
$kobold->save();438
}427
}439
}428
}@@ -448,9 +437,9 @@ class Tribe448
437
449
// Check for old-age injuries438
// Check for old-age injuries450
if ($kobold->getAge() >= 35 && $this->shouldAddOldAgeInjury($kobold)) {439
if ($kobold->getAge() >= 35 && $this->shouldAddOldAgeInjury($kobold)) {451
$oldAgeInjuries = ['bad_back', 'bad_liver', 'poor_eyesight', 'dementia'];440
$oldAgeInjuries = ['bad_back', 'bad_liver', 'poor_eyesight', 'dementia', 'limp', 'poor_hearing', 'chronic_fatigue'];452
$injury = $oldAgeInjuries[array_rand($oldAgeInjuries)];441
$injury = $oldAgeInjuries[array_rand($oldAgeInjuries)];453
$kobold->addInjury($injury, 'old-age', 1);442
$kobold->addInjury($injury);454
}443
}455
444
456
// Old-age death roll: if roll < (age + total injury severity), kobold dies445
// Old-age death roll: if roll < (age + total injury severity), kobold dies@@ -458,6 +447,7 @@ class Tribe458
$roll = rand(1, 100);447
$roll = rand(1, 100);459
$totalSeverity = $kobold->getTotalInjurySeverity();448
$totalSeverity = $kobold->getTotalInjurySeverity();460
if ($roll < ($kobold->getAge() + $totalSeverity)) {449
if ($roll < ($kobold->getAge() + $totalSeverity)) {450
$foodStory .= $kobold->getName() . " dies of old age.\n";461
$kobold->die();451
$kobold->die();462
}452
}463
}453
}@@ -468,6 +458,12 @@ class Tribe468
}458
}469
$foodStory .= "\nIt's {$this->currentSeason}.\n";459
$foodStory .= "\nIt's {$this->currentSeason}.\n";470
460
461
foreach ($this->kobolds as $kobold) {462
if ($kobold->isDead()) {463
$story = $story . "\n" . $kobold->getName() . " has died.";464
}465
}466
471
$this->save();467
$this->save();472
468
473
return $foodStory;469
return $foodStory;@@ -1,88 +0,0 @@
1
<?php
2
3
use TribeGen\Database\Database;
4
use TribeGen\Classes\Tribe;
5
6
class EventManager
7
{
8
private $tribe;
9
private $enabledEvents = [];
10
private $eventClasses = [];
11
12
public function __construct(Tribe $tribe)
13
{
14
$this->tribe = $tribe;
15
$this->loadEnabledEvents();
16
$this->registerEventClasses();
17
}
18
19
private function loadEnabledEvents()
20
{
21
$db = Database::getInstance();
22
$pdo = $db->getPDO();
23
24
$stmt = $pdo->prepare("SELECT event_class FROM events_enabled WHERE save_id = ? AND enabled = 1");
25
$stmt->execute([$this->tribe->getSaveId()]);
26
$rows = $stmt->fetchAll();
27
28
foreach ($rows as $row) {
29
$this->enabledEvents[] = $row['event_class'];
30
}
31
}
32
33
private function registerEventClasses()
34
{
35
// Register available event classes here
36
// This list should be expandable
37
$this->eventClasses = [
38
'hunting' => HuntingEvent::class,
39
'exploration' => ExplorationEvent::class,
40
'farming' => FarmingEvent::class,
41
];
42
}
43
44
public function getAvailableEvents()
45
{
46
$available = [];
47
48
foreach ($this->eventClasses as $key => $class) {
49
if (in_array($class, $this->enabledEvents)) {
50
$available[$key] = $class;
51
}
52
}
53
54
return $available;
55
}
56
57
public function executeEvent($eventClass)
58
{
59
if (!class_exists($eventClass)) {
60
throw new \Exception("Event class not found: $eventClass");
61
}
62
63
$event = new $eventClass($this->tribe);
64
return $event->execute();
65
}
66
67
public function getRandomMonthEvent()
68
{
69
// Get random month progression event based on season
70
// This should be expandable
71
$seasonEvents = [
72
'spring' => [],
73
'summer' => [],
74
'autumn' => [],
75
'winter' => []
76
];
77
78
$season = $this->tribe->getCurrentSeason();
79
$events = $seasonEvents[$season] ?? [];
80
81
if (empty($events)) {
82
return null;
83
}
84
85
$eventClass = $events[array_rand($events)];
86
return $this->executeEvent($eventClass);
87
}
88
}
@@ -1,88 +0,0 @@1
<?php2
3
use TribeGen\Database\Database;4
use TribeGen\Classes\Tribe;5
6
class EventManager7
{8
private $tribe;9
private $enabledEvents = [];10
private $eventClasses = [];11
12
public function __construct(Tribe $tribe)13
{14
$this->tribe = $tribe;15
$this->loadEnabledEvents();16
$this->registerEventClasses();17
}18
19
private function loadEnabledEvents()20
{21
$db = Database::getInstance();22
$pdo = $db->getPDO();23
24
$stmt = $pdo->prepare("SELECT event_class FROM events_enabled WHERE save_id = ? AND enabled = 1");25
$stmt->execute([$this->tribe->getSaveId()]);26
$rows = $stmt->fetchAll();27
28
foreach ($rows as $row) {29
$this->enabledEvents[] = $row['event_class'];30
}31
}32
33
private function registerEventClasses()34
{35
// Register available event classes here36
// This list should be expandable37
$this->eventClasses = [38
'hunting' => HuntingEvent::class,39
'exploration' => ExplorationEvent::class,40
'farming' => FarmingEvent::class,41
];42
}43
44
public function getAvailableEvents()45
{46
$available = [];47
48
foreach ($this->eventClasses as $key => $class) {49
if (in_array($class, $this->enabledEvents)) {50
$available[$key] = $class;51
}52
}53
54
return $available;55
}56
57
public function executeEvent($eventClass)58
{59
if (!class_exists($eventClass)) {60
throw new \Exception("Event class not found: $eventClass");61
}62
63
$event = new $eventClass($this->tribe);64
return $event->execute();65
}66
67
public function getRandomMonthEvent()68
{69
// Get random month progression event based on season70
// This should be expandable71
$seasonEvents = [72
'spring' => [],73
'summer' => [],74
'autumn' => [],75
'winter' => []76
];77
78
$season = $this->tribe->getCurrentSeason();79
$events = $seasonEvents[$season] ?? [];80
81
if (empty($events)) {82
return null;83
}84
85
$eventClass = $events[array_rand($events)];86
return $this->executeEvent($eventClass);87
}88
}@@ -2,6 +2,71 @@
2
2
3
3
class GameHelper
4
4
{
5
/**
6
* Delete cached kobold sprite sets for kobolds not present in any save for a user
7
* @param int $userId
8
* @return array List of deleted kobold IDs
9
*/
10
11
public static function cleanupUnusedKoboldSprites($userId)
12
{
13
$deleted = [];
14
$saveManager = new \SaveManager();
15
$userSavesPath = $saveManager->getUserSavesPath($userId);
16
$koboldsDir = $userSavesPath . '/kobolds';
17
if (!is_dir($koboldsDir))
18
return $deleted;
19
20
// 1. Collect all kobold IDs in all saves for this user
21
$activeKoboldIds = [];
22
$saveDirs = glob($userSavesPath . '/*', GLOB_ONLYDIR);
23
foreach ($saveDirs as $saveDir) {
24
$koboldsJson = $saveDir . '/kobolds.json';
25
if (file_exists($koboldsJson)) {
26
$data = json_decode(file_get_contents($koboldsJson), true);
27
if (is_array($data)) {
28
foreach ($data as $kobold) {
29
if (isset($kobold['id'])) {
30
$activeKoboldIds[$kobold['id']] = true;
31
}
32
}
33
}
34
}
35
}
36
37
// 2. Scan kobolds/ for all sprite sets
38
$dirs = glob($koboldsDir . '/kob_*', GLOB_ONLYDIR);
39
foreach ($dirs as $koboldDir) {
40
$koboldId = basename($koboldDir);
41
if (!isset($activeKoboldIds[$koboldId])) {
42
// Delete the entire kobold sprite directory
43
self::rrmdir($koboldDir);
44
$deleted[] = $koboldId;
45
}
46
}
47
return $deleted;
48
}
49
50
/**
51
* Recursively remove a directory
52
*/
53
private static function rrmdir($dir)
54
{
55
if (!is_dir($dir))
56
return;
57
$items = scandir($dir);
58
foreach ($items as $item) {
59
if ($item === '.' || $item === '..')
60
continue;
61
$path = $dir . DIRECTORY_SEPARATOR . $item;
62
if (is_dir($path)) {
63
self::rrmdir($path);
64
} else {
65
@unlink($path);
66
}
67
}
68
@rmdir($dir);
69
}
5
70
6
71
/**
7
72
* Get stat display label from numeric value
@@ -2,6 +2,71 @@2
2
3
class GameHelper3
class GameHelper4
{4
{5
/**6
* Delete cached kobold sprite sets for kobolds not present in any save for a user7
* @param int $userId8
* @return array List of deleted kobold IDs9
*/10
11
public static function cleanupUnusedKoboldSprites($userId)12
{13
$deleted = [];14
$saveManager = new \SaveManager();15
$userSavesPath = $saveManager->getUserSavesPath($userId);16
$koboldsDir = $userSavesPath . '/kobolds';17
if (!is_dir($koboldsDir))18
return $deleted;19
20
// 1. Collect all kobold IDs in all saves for this user21
$activeKoboldIds = [];22
$saveDirs = glob($userSavesPath . '/*', GLOB_ONLYDIR);23
foreach ($saveDirs as $saveDir) {24
$koboldsJson = $saveDir . '/kobolds.json';25
if (file_exists($koboldsJson)) {26
$data = json_decode(file_get_contents($koboldsJson), true);27
if (is_array($data)) {28
foreach ($data as $kobold) {29
if (isset($kobold['id'])) {30
$activeKoboldIds[$kobold['id']] = true;31
}32
}33
}34
}35
}36
37
// 2. Scan kobolds/ for all sprite sets38
$dirs = glob($koboldsDir . '/kob_*', GLOB_ONLYDIR);39
foreach ($dirs as $koboldDir) {40
$koboldId = basename($koboldDir);41
if (!isset($activeKoboldIds[$koboldId])) {42
// Delete the entire kobold sprite directory43
self::rrmdir($koboldDir);44
$deleted[] = $koboldId;45
}46
}47
return $deleted;48
}49
50
/**51
* Recursively remove a directory52
*/53
private static function rrmdir($dir)54
{55
if (!is_dir($dir))56
return;57
$items = scandir($dir);58
foreach ($items as $item) {59
if ($item === '.' || $item === '..')60
continue;61
$path = $dir . DIRECTORY_SEPARATOR . $item;62
if (is_dir($path)) {63
self::rrmdir($path);64
} else {65
@unlink($path);66
}67
}68
@rmdir($dir);69
}5
70
6
/**71
/**7
* Get stat display label from numeric value72
* Get stat display label from numeric value@@ -395,6 +395,12 @@ if ($method === 'POST') {
395
395
$event?->execute();
396
396
$story = $event?->getStory() ?? 'No event occurred.';
397
397
398
foreach ($participants as $participant) {
399
if ($participant->isDead()) {
400
$story = $story . "\n" . $participant->getName() . " has died.";
401
}
402
}
403
398
404
$tribe->save();
399
405
400
406
echo json_encode([
@@ -395,6 +395,12 @@ if ($method === 'POST') {395
$event?->execute();395
$event?->execute();396
$story = $event?->getStory() ?? 'No event occurred.';396
$story = $event?->getStory() ?? 'No event occurred.';397
397
398
foreach ($participants as $participant) {399
if ($participant->isDead()) {400
$story = $story . "\n" . $participant->getName() . " has died.";401
}402
}403
398
$tribe->save();404
$tribe->save();399
405
400
echo json_encode([406
echo json_encode([