Added Engine.fortify (previously fortification would break Engine)
[pyrisk.git] / pyrisk / base.py
index 38d3b2345810d91dd307d109903ed54e8a26dcec..7f9fcef769ea398d95628e89b41605a8411dc40f 100644 (file)
@@ -673,9 +673,6 @@ class Engine (ID_CmpMixin):
                         target = self.world.territory_by_name(target_name)
                     except KeyError:
                         raise PlayerError('Invalid territory "%s"' % targer_name)
                         target = self.world.territory_by_name(target_name)
                     except KeyError:
                         raise PlayerError('Invalid territory "%s"' % targer_name)
-                    if not source.borders(target):
-                        raise PlayerError('Cannot reach %s from %s to %s'
-                                          % (target, source, mode))
                     if mode == 'attack':
                         tplayer = target.player
                         capture = self.attack(source, target, armies)
                     if mode == 'attack':
                         tplayer = target.player
                         capture = self.attack(source, target, armies)
@@ -698,6 +695,9 @@ class Engine (ID_CmpMixin):
         if armies >= source.armies:
             raise PlayerError('%s attacking %s with %d armies, but only %d are available.'
                               % (source, target, armies, source.armies-1))
         if armies >= source.armies:
             raise PlayerError('%s attacking %s with %d armies, but only %d are available.'
                               % (source, target, armies, source.armies-1))
+        if not source.borders(target):
+            raise PlayerError('Cannot reach %s from %s to attack'
+                              % (target, source))
         s_dice = sorted([random.randint(1, 6) for i in range(armies)],
                         reverse=True)
         t_dice = sorted([random.randint(1, 6) for i in range(min(2, target.armies))],
         s_dice = sorted([random.randint(1, 6) for i in range(armies)],
                         reverse=True)
         t_dice = sorted([random.randint(1, 6) for i in range(min(2, target.armies))],
@@ -738,6 +738,21 @@ class Engine (ID_CmpMixin):
                     continue
             source.armies -= support
             target.armies += support
                     continue
             source.armies -= support
             target.armies += support
+    def fortify(self, source, target, armies):
+        if source.player != target.player:
+            raise PlayerError('%s (%s) cannot fortifiy %s (%s).'
+                              % (source, source.player, target, target.player))
+        if armies == 0:
+            return
+        if armies >= source.armies:
+            raise PlayerError('%s fortifying %s with %d armies, but only %d are available.'
+                              % (source, target, armies, source.armies-1))
+        if not source.borders(target):
+            raise PlayerError('Cannot reach %s from %s to fortify'
+                              % (target, source))
+        source.armies -= armies
+        target.armies += armies
+        self.log(Fortify(source, target, armies))
     def player_killed(self, player, killer):
         player.alive = False
         killer.hand.extend(player.hand)
     def player_killed(self, player, killer):
         player.alive = False
         killer.hand.extend(player.hand)