Browse Source

improve ship class

olinox 6 years ago
parent
commit
66b12b067a
1 changed files with 21 additions and 30 deletions
  1. 21 30
      carribean/script.py

+ 21 - 30
carribean/script.py

@@ -545,6 +545,14 @@ class Ship(Entity):
         self.next_pos = self.get_next_pos()
         self.next_area = Ship.get_area(*self.next_pos, self.orientation)
         
+        self.front = Grid.next_cell(*self.prow, self.orientation)
+        self.front_left = Grid.next_cell(*self.prow, Grid.add_directions(self.orientation, 1))
+        self.left = Grid.next_cell(*self.prow, Grid.add_directions(self.orientation, 2))
+        self.front_right = Grid.next_cell(*self.prow, Grid.add_directions(self.orientation, -1))
+        self.right = Grid.next_cell(*self.prow, Grid.add_directions(self.orientation, -2))
+        self.back_left = Grid.next_cell(*self.stern, Grid.add_directions(self.orientation, 2))
+        self.back_right = Grid.next_cell(*self.stern, Grid.add_directions(self.orientation, -2))
+        
         self.mobility_zone = list(set(self.area + self.next_area))
             
         if self.traject() != previous_traject:
@@ -565,21 +573,12 @@ class Ship(Entity):
 
     @classmethod
     def get_pos_in(cls, current, speed, orientation, in_=1):
-        x, y = current
-        for _ in range(in_):
-            for _ in range(speed):
-                dx, dy = Grid.directions(y)[orientation]
-                x, y = x + dx, y + dy
-        return x, y
+        return Grid.next_cell(*current, orientation, repeat=speed * in_)
 
     @classmethod
     def get_area(cls, x, y, orientation):
-        dx, dy = Grid.directions(y)[Grid.add_directions(orientation, 3)]
-        stern = (x + dx, y + dy)
-        
-        dx, dy = Grid.directions(y)[orientation]
-        prow = (x + dx, y + dy)
-        
+        prow = Grid.next_cell(x, y, orientation)
+        stern = Grid.next_cell(x, y, Grid.add_directions(orientation, 3))
         return [prow, (x, y), stern]
 
     def get_next_pos(self, in_=1):
@@ -635,26 +634,17 @@ class Ship(Entity):
     
     def cant_move(self):
         
-        front = Grid.next_cell(*self.prow, self.orientation)
-        front_left = Grid.next_cell(*self.prow, Grid.add_directions(self.orientation, 1))
-        left = Grid.next_cell(*self.prow, Grid.add_directions(self.orientation, 2))
-        front_right = Grid.next_cell(*self.prow, Grid.add_directions(self.orientation, -1))
-        right = Grid.next_cell(*self.prow, Grid.add_directions(self.orientation, -2))
+        blocked = {c: (self.moving_cost(*c) >= 1000) for c in [self.front, self.front_left, self.left,
+                                                                 self.front_right, self.right, 
+                                                                 self.back_left, self.back_right]}
         
-        back_left = Grid.next_cell(*self.stern, Grid.add_directions(self.orientation, 2))
-        back_right = Grid.next_cell(*self.stern, Grid.add_directions(self.orientation, -2))
-        
-        blocked = {c: (self.moving_cost(*c) >= 1000) for c in [front, front_left, left,
-                                                                 front_right, right, 
-                                                                 back_left, back_right]}
-        
-        if all(blocked[i] for i in [front, front_left, front_right, left, right]):
+        if all(blocked[i] for i in [self.front, self.front_left, self.front_right, self.left, self.right]):
             # surrounded
             return True
-        elif (blocked[front_left] and blocked[left]) or (blocked[front_right] and blocked[right]):
+        elif (blocked[self.front_left] and blocked[self.left]) or (blocked[self.front_right] and blocked[self.right]):
             # side by side
             return True
-        elif blocked[front] and ((blocked[front_left] and blocked[back_right]) or (blocked[front_right] and blocked[back_left])):
+        elif blocked[self.front] and ((blocked[self.front_left] and blocked[self.back_right]) or (blocked[self.front_right] and blocked[self.back_left])):
             # cannot go front or turn
             return True
         
@@ -896,6 +886,8 @@ while True:
     log(f"Cannonballs: {grid.cannonballs}")
 
 
+    max_it = 6000 // len(grid.owned_ships)
+
     ### Acquire
     log("# Acquiring")
     
@@ -933,7 +925,7 @@ while True:
                               ship.goto, 
                               moving_costs=ship._moving_costs, 
                               inertia=ship.speed, 
-                              limit=6000 // len(grid.owned_ships))
+                              limit=max_it)
         
         if ship.objective and ship.path and len(ship.path) <= 5:
             # what to do next
@@ -950,8 +942,7 @@ while True:
                                        ship.path[-1].orientation, 
                                        ship.objective_next.target.pos, 
                                        ship._moving_costs,
-                                       limit=6000 // len(grid.owned_ships)) or []
- 
+                                       limit=max_it) or []
  
     for ship in grid.owned_ships:
         log(f"---- ship {ship.id} ---")