Invasions should happen even if loading from save.

This commit is contained in:
Storm Dragon
2026-01-18 23:37:03 -05:00
parent 489408a181
commit 5aebd94431
4 changed files with 144 additions and 7 deletions

View File

@@ -37,3 +37,115 @@ void keep_base_fires_fed() {
break;
}
}
// Resident defense functions
int get_available_defense_weapons() {
int count = storage_spears;
// Slings only count if stones are available
if (storage_slings > 0 && storage_stones > 0) {
count += storage_slings;
}
return count;
}
bool can_residents_defend() {
if (residents_count <= 0) return false;
return get_available_defense_weapons() > 0;
}
bool choose_defense_weapon() {
// Returns true for spear, false for sling
int spearCount = storage_spears;
int slingCount = (storage_slings > 0 && storage_stones > 0) ? storage_slings : 0;
int total = spearCount + slingCount;
if (total == 0) return true;
if (slingCount == 0) return true;
if (spearCount == 0) return false;
int roll = random(1, total);
return roll <= spearCount;
}
int perform_resident_defense() {
if (!can_residents_defend()) return 0;
// Choose weapon type randomly weighted by availability
bool useSpear = choose_defense_weapon();
int damage = 0;
if (useSpear && storage_spears > 0) {
damage = RESIDENT_SPEAR_DAMAGE;
play_1d_with_volume_step("sounds/weapons/spear_swing.ogg", x, BASE_END + 1, false, 3.0);
} else if (storage_slings > 0 && storage_stones > 0) {
damage = random(RESIDENT_SLING_DAMAGE_MIN, RESIDENT_SLING_DAMAGE_MAX);
storage_stones--;
play_1d_with_volume_step("sounds/weapons/sling_hit.ogg", x, BASE_END + 1, false, 3.0);
}
return damage;
}
void process_daily_weapon_breakage() {
if (residents_count <= 0) return;
int totalWeapons = storage_spears + storage_slings;
if (totalWeapons == 0) return;
// Number of breakage checks = min(residents, weapons)
int checksToPerform = (residents_count < totalWeapons) ? residents_count : totalWeapons;
// Distribute checks among available weapons
int spearChecks = 0;
int slingChecks = 0;
for (int i = 0; i < checksToPerform; i++) {
int remainingSpears = storage_spears - spearChecks;
int remainingSlings = storage_slings - slingChecks;
int remaining = remainingSpears + remainingSlings;
if (remaining <= 0) break;
int roll = random(1, remaining);
if (roll <= remainingSpears && remainingSpears > 0) {
spearChecks++;
} else if (remainingSlings > 0) {
slingChecks++;
}
}
// Perform breakage checks
int spearsBroken = 0;
int slingsBroken = 0;
for (int i = 0; i < spearChecks; i++) {
if (random(1, 100) <= RESIDENT_WEAPON_BREAK_CHANCE) {
spearsBroken++;
}
}
for (int i = 0; i < slingChecks; i++) {
if (random(1, 100) <= RESIDENT_WEAPON_BREAK_CHANCE) {
slingsBroken++;
}
}
// Apply breakage
if (spearsBroken > 0) {
storage_spears -= spearsBroken;
if (storage_spears < 0) storage_spears = 0;
string msg = (spearsBroken == 1)
? "A resident's spear broke from wear."
: spearsBroken + " spears broke from wear.";
notify(msg);
}
if (slingsBroken > 0) {
storage_slings -= slingsBroken;
if (storage_slings < 0) storage_slings = 0;
string msg = (slingsBroken == 1)
? "A resident's sling broke from wear."
: slingsBroken + " slings broke from wear.";
notify(msg);
}
}