Fix rat reproduction collision check

Replace the brittle 'self.position == other.position_before' check
with a symmetric cell-intersection test: two rats are considered
colliding if any of their current/previous cells overlap. This handles
both 'both rats in same cell' and 'one rat enters the cell the other
just left'.

Also remove the erroneous hasattr(other_unit, 'fuck') guard that
prevented Male.fuck() from being called on Female (Female has no fuck
method, but only Male should initiate reproduction).

Add tests/test_rat_reproduction.py with 8 scenarios covering overlapping
rats, baby rats, already-pregnant females, far-apart rats, female
self-initiation, sound playback, and procreate interval spawning.
This commit is contained in:
2026-06-17 10:29:26 +02:00
parent 2eccf504f5
commit 319801d6e5
2 changed files with 307 additions and 3 deletions
+7 -3
View File
@@ -138,8 +138,12 @@ class Rat(Unit):
if other_unit.age < AGE_THRESHOLD:
continue
# Check if units are actually moving towards each other
if self.position != other_unit.position_before:
# Check if units are actually overlapping (same cell now or in transition).
# Accept any of: same current cell, same previous cell, or each entered
# the other's previous cell.
self_here = (self.position, self.position_before)
other_here = (other_unit.position, other_unit.position_before)
if not (set(self_here) & set(other_here)):
continue
# Both units still exist in game
@@ -148,7 +152,7 @@ class Rat(Unit):
# Same sex + fight mode = combat
self.die(other_unit)
elif self.sex != other_unit.sex:
# Different sex = reproduction
# Different sex = reproduction (only Male has fuck method)
if hasattr(self, 'fuck'):
self.fuck(other_unit)