Skip to content

Commit 9eb9117

Browse files
Merge pull request #372 from C7-Game/323-Fix-Zombie-Unit-Crash
2 parents adcca35 + 95dee7d commit 9eb9117

File tree

3 files changed

+25
-10
lines changed

3 files changed

+25
-10
lines changed

C7Engine/AI/BarbarianAI.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,9 @@ public void PlayTurn(Player player, GameData gameData) {
3434
//if it tries to move e.g. north from the north pole. Hence, this check.
3535
if (newLocation != Tile.NONE) {
3636
log.Debug("Moving barbarian at " + unit.location + " to " + newLocation);
37-
unit.move(unit.location.directionTo(newLocation));
37+
if (!unit.move(unit.location.directionTo(newLocation))) {
38+
break;
39+
}
3840
} else {
3941
//Avoid potential infinite loop.
4042
break;

C7Engine/MapUnitExtensions.cs

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -327,10 +327,18 @@ public static bool CanEnterTile(this MapUnit unit, Tile tile, bool allowCombat)
327327
return true;
328328
}
329329

330-
public static void move(this MapUnit unit, TileDirection dir, bool wait = false)
330+
/// <summary>
331+
/// Moves the unit in the given direction
332+
/// </summary>
333+
/// <param name="unit"></param>
334+
/// <param name="dir">Which direction to move, e.g. northeast, west, etc.</param>
335+
/// <param name="wait">Whether the method should wait to return until animations complete</param>
336+
/// <returns>True if the unit is alive after the movement, false otherwise</returns>
337+
/// <exception cref="Exception"></exception>
338+
public static bool move(this MapUnit unit, TileDirection dir, bool wait = false)
331339
{
332340
(int dx, int dy) = dir.toCoordDiff();
333-
var newLoc = EngineStorage.gameData.map.tileAt(dx + unit.location.xCoordinate, dy + unit.location.yCoordinate);
341+
Tile newLoc = EngineStorage.gameData.map.tileAt(dx + unit.location.xCoordinate, dy + unit.location.yCoordinate);
334342
if ((newLoc != Tile.NONE) && unit.CanEnterTile(newLoc, true) && (unit.movementPoints.canMove)) {
335343
unit.facingDirection = dir;
336344
unit.wake();
@@ -342,27 +350,31 @@ public static void move(this MapUnit unit, TileDirection dir, bool wait = false)
342350
CombatResult combatResult = unit.fight(defender);
343351
// If we were killed then of course there's nothing more to do. If the combat couldn't happen for whatever
344352
// reason, just give up on trying to move.
345-
if (combatResult == CombatResult.AttackerKilled || combatResult == CombatResult.Impossible)
346-
return;
353+
if (combatResult == CombatResult.AttackerKilled) {
354+
return false;
355+
}
356+
if (combatResult == CombatResult.Impossible) {
357+
return true;
358+
}
347359

348360
// If the enemy was defeated, check if there is another enemy on the tile. If so we can't complete the move
349361
// but still pay one movement point for the combat.
350362
else if (combatResult == CombatResult.DefenderKilled || combatResult == CombatResult.DefenderRetreated) {
351363
if (!unit.CanEnterTile(newLoc, false)) {
352364
unit.movementPoints.onUnitMove(1);
353-
return;
365+
return true;
354366
}
355367

356368
// Similarly if we retreated, pay one MP for the combat but don't move.
357369
} else if (combatResult == CombatResult.AttackerRetreated) {
358370
unit.movementPoints.onUnitMove(1);
359-
return;
371+
return true;
360372
}
361373
} else if (unit.unitType.bombard > 0) {
362374
unit.bombard(newLoc);
363-
return;
375+
return true;
364376
} else {
365-
return;
377+
return true;
366378
}
367379
}
368380

@@ -375,6 +387,7 @@ public static void move(this MapUnit unit, TileDirection dir, bool wait = false)
375387
unit.movementPoints.onUnitMove(movementCost);
376388
unit.animate(MapUnit.AnimatedAction.RUN, wait);
377389
}
390+
return true;
378391
}
379392

380393
public static float getMovementCost(Tile from, TileDirection dir, Tile newLocation) {

C7GameData/MapUnit.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public bool IsBusy() {
6161
public override string ToString()
6262
{
6363
if (this != MapUnit.NONE) {
64-
return this.owner + " " + unitType.name + " with " + movementPoints.remaining + " movement points and " + hitPointsRemaining + " hit points, guid = " + guid;
64+
return this.owner + " " + unitType.name + "at (" + location.xCoordinate + ", " + location.yCoordinate + ") with " + movementPoints.remaining + " MP and " + hitPointsRemaining + " HP, guid = " + guid;
6565
}
6666
else {
6767
return "This is the NONE unit";

0 commit comments

Comments
 (0)