Skip to content

Commit d3db3a4

Browse files
Merge pull request #33 from Stephenson-Software/copilot/fix-32928199-1319-4888-929c-a837efd21522
Add early-game survival mechanism to prevent instant player death
2 parents 0fe995c + 3d3dffa commit d3db3a4

File tree

5 files changed

+449
-4
lines changed

5 files changed

+449
-4
lines changed

.coverage

0 Bytes
Binary file not shown.

src/config/config.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,8 @@ def __init__(self):
99
self.godMode = False # Disable god mode
1010
self.maxTicks = 1000
1111
self.tickLength = 0.1
12+
13+
# Early-game survival settings
14+
self.earlyGameGracePeriod = 50 # Number of ticks of protection for player
15+
self.playerDamageReduction = 0.4 # 40% damage reduction for player during grace period
16+
# During grace period, other creatures have 85% chance to avoid attacking player

src/entity/livingEntity.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,11 @@ def fight(self, kreature):
5757
# This creature attacks first
5858
if self.health > 0:
5959
damage = random.randint(15, 25) # Random damage between 15-25
60+
# Apply damage reduction if target has it
61+
if hasattr(kreature, 'damageReduction') and kreature.damageReduction > 0:
62+
damage = int(damage * (1 - kreature.damageReduction))
63+
damage = max(damage, 1) # Ensure at least 1 damage
64+
6065
kreature.health -= damage
6166
if kreature.health <= 0:
6267
self.log.append(
@@ -80,6 +85,11 @@ def fight(self, kreature):
8085
# Target creature counter-attacks if still alive
8186
if kreature.health > 0:
8287
damage = random.randint(15, 25) # Random damage between 15-25
88+
# Apply damage reduction if target has it
89+
if hasattr(self, 'damageReduction') and self.damageReduction > 0:
90+
damage = int(damage * (1 - self.damageReduction))
91+
damage = max(damage, 1) # Ensure at least 1 damage
92+
8393
self.health -= damage
8494
if self.health <= 0:
8595
kreature.log.append(

src/kreatures.py

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ def __init__(self):
3737
self.running = True
3838
self.config = Config()
3939
self.tick = 0
40+
41+
# Initialize player early-game protection
42+
self.playerCreature.damageReduction = self.config.playerDamageReduction
43+
self.playerCreature.log.append("%s has early-game protection!" % self.playerCreature.name)
4044

4145
def initiateEntityActions(self):
4246
entities_to_remove = [] # Track entities that die this turn
@@ -59,8 +63,18 @@ def initiateEntityActions(self):
5963
entity.decreaseChanceToFight()
6064
self.createChildEntity(parents[0], parents[1])
6165
elif decision == "fight":
62-
if target == self.playerCreature and self.config.godMode:
63-
continue
66+
# Enhanced protection: during grace period, reduce attacks on player
67+
if target == self.playerCreature:
68+
if self.config.godMode:
69+
continue
70+
# During grace period, 85% chance to skip attacking the player
71+
if (self.tick < self.config.earlyGameGracePeriod and
72+
random.randint(1, 100) <= 85):
73+
entity.log.append(
74+
"%s decided not to attack %s." % (entity.name, target.name)
75+
)
76+
continue
77+
6478
entity.increaseChanceToFight()
6579
entity.decreaseChanceToBefriend()
6680
entity.fight(target)
@@ -78,6 +92,14 @@ def initiateEntityActions(self):
7892
for entity in entities_to_remove:
7993
self.environment.removeEntity(entity)
8094

95+
def updatePlayerProtection(self):
96+
"""Update player protection based on current tick"""
97+
if self.tick >= self.config.earlyGameGracePeriod:
98+
# Grace period has ended
99+
if hasattr(self.playerCreature, 'damageReduction') and self.playerCreature.damageReduction > 0:
100+
self.playerCreature.damageReduction = 0
101+
self.playerCreature.log.append("%s's protection has worn off!" % self.playerCreature.name)
102+
81103
def regenerateAllEntities(self):
82104
"""Regenerate health for all living entities"""
83105
for entity in self.environment.getEntities():
@@ -186,6 +208,12 @@ def printSummary(self):
186208
"%s's chance to be nice was %d percent."
187209
% (self.playerCreature.name, self.playerCreature.chanceToBefriend)
188210
)
211+
212+
# Show protection status
213+
if hasattr(self.playerCreature, 'damageReduction') and self.playerCreature.damageReduction > 0:
214+
protection_percent = int(self.playerCreature.damageReduction * 100)
215+
print("%s still has %d%% damage reduction." % (self.playerCreature.name, protection_percent))
216+
189217
if self.playerCreature.isAlive():
190218
print(
191219
"%s ended with %d health (out of %d max)."
@@ -198,6 +226,7 @@ def printSummary(self):
198226
else:
199227
print("%s died during the simulation." % self.playerCreature.name)
200228
print("Kreatures still alive: %d" % self.environment.getNumEntities())
229+
print("Simulation ran for %d ticks." % self.tick)
201230

202231
def printStats(self):
203232
print("=== Stats ===")
@@ -224,6 +253,7 @@ def run(self):
224253
pass
225254

226255
self.initiateEntityActions()
256+
self.updatePlayerProtection() # Update player protection status
227257
self.regenerateAllEntities() # Regenerate health for all entities
228258
time.sleep(self.config.tickLength)
229259
self.tick += 1
@@ -238,5 +268,6 @@ def run(self):
238268
self.printStats()
239269

240270

241-
kreatures = Kreatures()
242-
kreatures.run()
271+
if __name__ == "__main__":
272+
kreatures = Kreatures()
273+
kreatures.run()

0 commit comments

Comments
 (0)