Procházet zdrojové kódy

better 'next_objectives' algo

olinox před 6 roky
rodič
revize
2b6a95adc4
1 změnil soubory, kde provedl 38 přidání a 23 odebrání
  1. 38 23
      carribean/script.py

+ 38 - 23
carribean/script.py

@@ -33,6 +33,9 @@ class Queue():
     def __bool__(self):
         return bool(self.items)
 
+    def __repr__(self):
+        return str(self.items)
+
     def put(self, item, priority):
         heapq.heappush(self.items, (priority, item))
 
@@ -63,8 +66,15 @@ class InterestQueue(Queue):
         return q
     
 class ObjectivesQueue(InterestQueue):
-    pass
-
+    
+    @classmethod
+    def re_eval(cls, q, pos=None, d=None):
+        new_q = cls()
+        while q:
+            o = q.get()
+            o.eval(pos, d)
+            new_q.put(o)
+        return new_q
     
 class Base():
     def __repr__(self):
@@ -81,7 +91,7 @@ class BaseObjective(Base):
         return self.interest < other.interest
 
     def __repr__(self):
-        return f"<{self.__class__.__name__}({self.target.id})>"
+        return f"<{self.__class__.__name__}: target={self.target.id};int={self.interest})>"
 
     def eval(self, pos = None, d = None):
         self.distance = Grid.manhattan(pos, self.target.pos) if pos is not None else 0
@@ -187,6 +197,8 @@ class Grid(Base):
             
             s._can_move = {c: (s.moving_cost(*c) < 1000) for c in [s.front, s.front_left, s.left, s.front_right,
                                                                   s.right, s.back_left, s.back_right]}
+            s.objectives = ObjectivesQueue()
+            s.ennemies = ObjectivesQueue()
             
             for b in self.barrels:
                 obj = GetBarrel(b)
@@ -523,7 +535,7 @@ class Ship(Entity):
         self.ennemies = ObjectivesQueue()
         
         self.objective = None
-        self.objective_next = None
+        self.objectives_next = []
         self.target_ennemy = None
         
         self.path = []
@@ -545,7 +557,7 @@ class Ship(Entity):
         self.ennemies = ObjectivesQueue()
         
         self.objective = None
-        self.objective_next = None
+        self.objectives_next = []
         self.target_ennemy = None
         
         self.goto = None
@@ -907,12 +919,12 @@ while True:
     
 #     log(f"Owned Ships: {grid.owned_ships}")
     log(f"Ennemy Ships: {grid.ennemy_ships}")
-#     log(f"Barrels: {grid.barrels}")
+    log(f"Barrels: {grid.barrels}")
 #     log(f"Mines: {grid.mines}")
     log(f"Cannonballs: {grid.cannonballs}")
 
-
-    max_it = 6000 // len(grid.owned_ships)
+    
+    max_it = 9000 // len(grid.owned_ships)
 
     ### Acquire
     log("# Acquiring")
@@ -937,6 +949,7 @@ while True:
     log("# Planning")
     
     for ship in grid.owned_ships:
+        it_consumed = 0
         
         if ship.objective:
             ship.goto = ship.objective.target.pos
@@ -951,29 +964,31 @@ while True:
                               ship.goto, 
                               moving_costs=ship._moving_costs, 
                               inertia=ship.speed, 
-                              limit=max_it)
+                              limit=(max_it - it_consumed))
         
-        if ship.objective and ship.path and len(ship.path) <= 5:
-            # what to do next
-            after_that = ObjectivesQueue()
-            for b in [o.target for o in s.objectives.items]:
-                obj = GetBarrel(b)
-                obj.eval(ship.path[-1], ship.path[-1].orientation)
-                after_that.put(obj)
+        if ship.objective and ship.path:
+            while ship.objectives and len(ship.path) < 10:
+                pos, d = ship.path[-1], ship.path[-1].orientation
                 
-            if after_that:
-                ship.objective_next = after_that.get()
+                ship.objectives = ObjectivesQueue.re_eval(ship.objectives, pos, d)
+                current_obj = ship.objectives.get()
+                
+                ship.objectives_next.append(current_obj)
             
-                ship.path += grid.path(ship.goto, 
-                                       ship.path[-1].orientation, 
-                                       ship.objective_next.target.pos, 
+                new_path = grid.path(ship.goto, d, 
+                                       current_obj.target.pos, 
                                        ship._moving_costs,
-                                       limit=max_it) or []
+                                       limit=(max_it - it_consumed)) or []
+                if new_path:
+                    ship.path += new_path
+                else:
+                    break     
+                                
  
     for ship in grid.owned_ships:
         log(f"---- ship {ship.id} ---")
         log(f"ship: {ship}")
-        log(f"obj: {ship.objective}; next: {ship.objective_next}; target: {ship.target_ennemy}")
+        log(f"obj: {ship.objective}; next: {ship.objectives_next}; target: {ship.target_ennemy}")
         log(f"goto: {ship.goto}")
         log(f"path: {ship.path}")