Make AI destory gates
authorNeil <neil@dip.sun.ac.za>
Thu, 18 Apr 2013 15:08:32 +0000 (17:08 +0200)
committerNeil <neil@dip.sun.ac.za>
Thu, 18 Apr 2013 15:08:32 +0000 (17:08 +0200)
erdslangetjie/__main__.py
erdslangetjie/level.py
erdslangetjie/player.py

index ad07468b327a48859cb6bd7ad109985cccfd7e84..462d79535cd30289ed19eb34a327cf1b6e68182f 100644 (file)
@@ -188,7 +188,7 @@ class GameWindow(RelativeLayout):
         self.do_nemesis_move()
 
     def do_nemesis_move(self):
-        self.nemesis.move(self.level_obj)
+        self.nemesis.move(self.level_obj, self.player.pos)
         self.check_state()
         self.reset_timer()
         self.draw_nemesis()
index fc23cc626238bb965d69eea370098cffad96293f..092ef7bb874840420d44854a438ea8725ae76676 100644 (file)
@@ -229,7 +229,7 @@ class Level(object):
             return True
         if tile == GATE:
             print tile, pos, self._gates[pos]
-            if self._gates[pos] != -1:
+            if self._gates[pos] > 0:
                 return True
         return False
 
@@ -237,11 +237,20 @@ class Level(object):
         return abs(pos1[0] - pos2[0]) + abs(pos1[1] - pos2[1])
 
     def is_gate(self, pos):
+        if not self._in_limits(pos):
+            return False
         return self._data[pos[1]][pos[0]] == GATE
 
     def is_button(self, pos):
+        if not self._in_limits(pos):
+            return False
         return self._data[pos[1]][pos[0]] == BUTTON
 
+    def is_wall(self, pos):
+        if not self._in_limits(pos):
+            return True
+        return self._data[pos[1]][pos[0]] == WALL
+
     def trigger_button(self, pos):
         if not self.is_button(pos):
             return False
@@ -256,20 +265,18 @@ class Level(object):
                 gate_pos = cand
                 mindist = dist
         if gate_pos:
-            self._buttons[pos] == 'pressed'
+            self._buttons[pos] = 'pressed'
             self._gates[gate_pos] = 3  # Raise gate
             self._fix_tile(pos)
             self._fix_tile(gate_pos)
 
     def damage_gate(self, pos):
         if not self.is_gate(pos):
-            return False
+            return
         if self._gates[pos] == -1 or self._gates[pos] == 0:
-            return False
+            return
         self._gates[pos] = self._gates[pos] - 1
-        self._fix_gate_tile(pos)
-        self._changed.append((pos, self.get_single_tile(pos)))
-        return True
+        self._fix_tile(pos)
 
     def get_changed_tiles(self):
         ret = self._changed[:]
index e0ac771d3af9e046f265cab3efc03525015ab2cb..7c80263243c094b611b432c1ab70053e9510c3be 100644 (file)
@@ -43,9 +43,10 @@ class Nemesis(FigureSprite):
         super(Nemesis, self).__init__()
         self.sprite = load_image('sprites/nemesis.png')
         self.reset_pos()
+        self.deadends = []
         self.been = []
 
-    def move(self, level):
+    def move(self, level, player_pos):
         if not self.on_board():
             # Advance towards the map a step at a time
             self.pos = (self.pos[0] + 1, self.pos[1])
@@ -53,6 +54,15 @@ class Nemesis(FigureSprite):
                 self.pos = level.enter_pos
             return False
         # AI goes here
+        # First, if we're standing next to a gate, we attack it
+        neighbours = [(self.pos[0] + 1, self.pos[1]),
+                (self.pos[0] - 1, self.pos[1]),
+                (self.pos[0], self.pos[1] + 1),
+                (self.pos[0], self.pos[1] - 1)]
+        for cand in neighbours:
+            if level.is_gate(cand) and level.blocked(cand):
+                level.damage_gate(cand)
+                return True
         steps = 0
         self.been.append(self.pos)
         for direction in [(1, 0), (-1, 0), (0, -1), (0, 1)]: