Commits

Lars Yencken  committed aac06e0

Update ants to use torus module.

  • Participants
  • Parent commits dfb9ee7
  • Branches scent

Comments (0)

Files changed (1)

 from math import sqrt
 import numpy as np
 
+import torus
+
+Loc = torus.Loc
+
 MY_ANT = 0
 ANTS = 0
 DEAD = -1
 MAP_OBJECT = '?%*.!'
 MAP_RENDER = PLAYER_ANT + HILL_ANT + PLAYER_HILL + MAP_OBJECT
 
-AIM = {'n': (-1, 0),
-       'e': (0, 1),
-       's': (1, 0),
-       'w': (0, -1)}
+AIM = {'n': Loc(-1, 0),
+       'e': Loc(0, 1),
+       's': Loc(1, 0),
+       'w': Loc(0, -1)}
 RIGHT = {'n': 'e',
          'e': 's',
          's': 'w',
                 elif key == 'turns':
                     self.turns = int(tokens[1])
         self.map = np.ones((self.rows, self.cols))*LAND
+        torus.init(self.rows, self.cols)
 
     def update(self, data):
         'parse engine input and update the game state'
         
         # clear hill, ant and food data
         self.hill_list = {}
-        for row, col in self.ant_list.keys():
-            self.map[row, col] = LAND
+        for l in self.ant_list.keys():
+            self.map[l] = LAND
         self.ant_list = {}
-        for row, col in self.dead_list.keys():
-            self.map[row, col] = LAND
+        for l in self.dead_list.keys():
+            self.map[l] = LAND
         self.dead_list = defaultdict(list)
-        for row, col in self.food_list:
-            self.map[row, col] = LAND
+        for l in self.food_list:
+            self.map[l] = LAND
         self.food_list = []
         
         # update map and create new ant and food lists
                 if len(tokens) >= 3:
                     row = int(tokens[1])
                     col = int(tokens[2])
+                    l = Loc(row, col)
                     if tokens[0] == 'w':
                         self.map[row, col] = WATER
                     elif tokens[0] == 'f':
                         self.map[row, col] = FOOD
-                        self.food_list.append((row, col))
+                        self.food_list.append(l)
                     else:
                         owner = int(tokens[3])
                         if tokens[0] == 'a':
-                            self.map[row, col] = owner
-                            self.ant_list[(row, col)] = owner
+                            self.map[l] = owner
+                            self.ant_list[l] = owner
                         elif tokens[0] == 'd':
                             # food could spawn on a spot where an ant just died
                             # don't overwrite the space unless it is land
-                            if self.map[row, col] == LAND:
-                                self.map[row, col] = DEAD
+                            if self.map[l] == LAND:
+                                self.map[l] = DEAD
                             # but always add to the dead list
-                            self.dead_list[(row, col)].append(owner)
+                            self.dead_list[l].append(owner)
                         elif tokens[0] == 'h':
                             owner = int(tokens[3])
-                            self.hill_list[(row, col)] = owner
+                            self.hill_list[l] = owner
                         
     def time_remaining(self):
         return self.turntime - int(1000 * (time.clock() - self.turn_start_time))
         
     def my_ants(self):
         'return a list of all my ants'
-        return [(row, col) for (row, col), owner in self.ant_list.items()
-                    if owner == MY_ANT]
+        return [loc for loc, owner in self.ant_list.items() if owner == MY_ANT]
 
     def enemy_ants(self):
         'return a list of all visible enemy ants'
-        return [((row, col), owner)
-                    for (row, col), owner in self.ant_list.items()
+        return [(loc, owner) for loc, owner in self.ant_list.items()
                     if owner != MY_ANT]
 
     def food(self):
 
     def passable(self, loc):
         'true if not water'
-        row, col = loc
-        return self.map[row, col] != WATER
+        return self.map[loc] != WATER
     
     def unoccupied(self, loc):
         'true if no ants are at the location'
-        row, col = loc
-        return self.map[row, col] in (LAND, DEAD)
+        return self.map[loc] in (LAND, DEAD)
 
     def destination(self, loc, direction):
         'calculate a new location given the direction and wrap correctly'
-        row, col = loc
-        d_row, d_col = AIM[direction]
-        return ((row + d_row) % self.rows, (col + d_col) % self.cols)        
-
-    def distance(self, loc1, loc2):
-        'calculate the closest distance between to locations'
-        row1, col1 = loc1
-        row2, col2 = loc2
-        d_col = min(abs(col1 - col2), self.cols - abs(col1 - col2))
-        d_row = min(abs(row1 - row2), self.rows - abs(row1 - row2))
-        return d_row + d_col
+        return loc + AIM[direction]
 
     def direction(self, loc1, loc2):
         'determine the 1 or 2 fastest (closest) directions to reach a location'
                 # precalculate squares around an ant to set as visible
                 self.vision_offsets_2 = []
                 mx = int(sqrt(self.viewradius2))
+                print >> sys.stderr, self.viewradius2
                 for d_row in range(-mx,mx+1):
                     for d_col in range(-mx,mx+1):
                         d = d_row**2 + d_col**2
                                 d_row%self.rows-self.rows,
                                 d_col%self.cols-self.cols
                             ))
+                print >> sys.stderr, self.vision_offsets_2
             # set all spaces as not visible
             # loop through ants and set all squares around ant as visible
-            self.vision = [[False]*self.cols for row in range(self.rows)]
+            self.vision = np.zeros((self.rows, self.cols), dtype=np.bool_)
             for ant in self.my_ants():
                 a_row, a_col = ant
                 for v_row, v_col in self.vision_offsets_2:
-                    self.vision[a_row+v_row][a_col+v_col] = True
-        row, col = loc
-        return self.vision[row][col]
+                    self.vision[Loc(a_row+v_row, a_col+v_col)] = True
+        return self.vision[loc]
     
     def render_text_map(self):
         'return a pretty string representing the map'