Browse Source

Complete the the fire_at_will method

olinox 6 years ago
parent
commit
115d0a42f8
1 changed files with 18 additions and 19 deletions
  1. 18 19
      carribean/script.py

+ 18 - 19
carribean/script.py

@@ -12,7 +12,8 @@ import time
 # * Try to integer extra steps into the path algo, instead of adding independents paths
 # * Increase the shooting rate in the second part of the game (no barrels left)
 # * get_shooting_spots: try to intercept ennemy
-
+# * collisions: take in account the probable next area of near ennemies
+# * make a difference between moving cost, mines, cannonballs, out of grid and other ships
 
 debug = True
 
@@ -24,9 +25,6 @@ def log(*msg):
 
 current_turn = 0
 
-class CollisionAlert(Exception):
-    pass
-
 class Queue():
     def __init__(self):
         self.items = []
@@ -318,10 +316,8 @@ class Grid(Base):
         for ship in self.ships:
             ship._moving_costs = {}
             ship._moving_costs.update(base_costs)
-            played_before = True
             for other in self.ships:
                 if other is ship:
-                    played_before = False
                     continue
                 dist = self.manhattan(ship.pos, other.pos)
                 if dist > 6:
@@ -330,9 +326,9 @@ class Grid(Base):
                     ship._moving_costs[c] += 25
                 
                 next_positions = other.next_pos_proba()
-                for c, proba in next_positions[1 if played_before else 0].items():
+                for c, proba in next_positions[1].items():
                     if proba >= 20:
-                        ship._moving_costs[c] = ship._moving_costs.get(c, 0) + 13 * proba
+                        ship._moving_costs[c] = ship._moving_costs.get(c, 0) + 20 * proba
 
     def shooting_spot(self, ship, target, current=None):
         shooting_spots = Queue()
@@ -904,7 +900,7 @@ class Ship(Entity):
             if r:
                 log(f"/!\ Danger: planned move <{Ship.COMMANDS[move]}> could lead to collision (risk={r}, area={new_area})")
             else:
-                log(f"Safe move: {move}")
+                log(f"Safe move: {Ship.COMMANDS[move]}")
                 next_move = move
                 break
 
@@ -923,7 +919,7 @@ class Ship(Entity):
     def _follow_path(self, path):
         
         # flags represent direction changes or end of the path
-        last_flag = len(path)
+        last_flag = len(path) - 1
         next_flag = next((i for i, n in enumerate(path) if n.orientation != self.orientation), last_flag)
         afternext_flag = next((i for i, n in enumerate(path[next_flag:]) if n.orientation != path[next_flag].orientation), last_flag)
         
@@ -1007,6 +1003,7 @@ class Ship(Entity):
             if any(Grid.manhattan(ship.pos, bpos) <= Grid.manhattan(target.pos, bpos) for ship in grid.owned_ships):
                 avoid.append(bpos)
         
+        all_shots = []
         for t, probas in next_positions.items():
             
             # include mines and barrels
@@ -1020,13 +1017,12 @@ class Ship(Entity):
                     alignment = abs(Grid.diff_directions(self.orientation, Grid.direction_to(*self.pos, *mpos)))
                     if alignment <= 1:
                         continue
-                    
                     mines_next[mpos] = mines_next.get(mpos, 1) + proba
             probas.update(mines_next)
             
             for c in probas:
                 if c in barrels:
-                    probas[c] += 20
+                    probas[c] *= 2
             
             shots = sorted(probas.items(), key=lambda x: x[1], reverse=True)
             
@@ -1041,13 +1037,16 @@ class Ship(Entity):
                 
                 # time for the cannonball to reach this pos (including fire turn)
                 delay = 1 + (1 + round(dist / 3)) 
-                
                 if delay != t:
                     continue
                 
-                log(f"[x] precise shoot: pos={c}")
-                ship.fire(*c)
-                return True
+                all_shots.append((c, proba,  t))
+                
+        if all_shots:
+            best_shot = max(all_shots, key=lambda x: x[1:])[0]
+            log(f"[x] precise shoot: pos={best_shot}")
+            ship.fire(*best_shot)
+            return True
                 
         return False
         
@@ -1281,12 +1280,12 @@ while True:
 # * mines: increase moving_cost of neighbors if a cannon ball is going to hit the mine
 # * Improve the _follow_path algo
 # * anticipate the speed at 0 when blocked
-# * coeff 13 instead of 10 for the presence probability, making it impassable from env. 77% instead of 100%
-# * moving cost takes now in account the order of play of the ships
+# * coeff 20 instead of 10 for the presence probability, making it impassable from 50% instead of 100%
+# *(disactivated)  moving cost takes now in account the order of play of the ships
 # * increase the max length of the path from 10 to 15 when seeking for next objectives
 # * minor improvement to automove
 # * Avoid shooting barrels unless ennemy is nearest
 # * include distance to ennemy in the eval of the shooting spot
 # * (disactivated because of below) avoid consecutives direction changes 
 # * path: direction change moving cost changed from 10 to 20 because of the slowing effect
-
+# * Complete the the fire_at_will method