/ benchmark / pep / BehaviorManager.java
BehaviorManager.java
 1  package pep;
 2  
 3  import hlt.*;
 4  
 5  import java.util.ArrayList;
 6  import java.util.Collection;
 7  import java.util.HashMap;
 8  import java.util.Map;
 9  
10  /**
11   * Maintains the behaviors of each ship
12   */
13  public class BehaviorManager {
14      private static HashMap<EntityId, Behavior> behaviors = new HashMap<>();
15  
16      // static instances of each behavior for purpose of checking criteria
17      private static Depositor depositor = new Depositor();
18      private static Harvester harvester = new Harvester();
19  
20      public static void updateBehaviors(Game game){
21          for (Ship ship : game.me.ships.values()){
22              if (!behaviors.containsKey(ship.id)){
23                  behaviors.put(ship.id, new Harvester());
24              }
25              Behavior currentBehavior = behaviors.get(ship.id);
26              if (currentBehavior.meetsCriteria(ship, currentBehavior.getType(), game)){
27                  continue;
28              }
29  
30              if (harvester.meetsCriteria(ship, currentBehavior.getType(), game)){
31                  behaviors.put(ship.id, new Harvester());
32              } else if (depositor.meetsCriteria(ship, currentBehavior.getType(), game)){
33                  behaviors.put(ship.id, new Depositor());
34              }
35          }
36      }
37  
38      public static Behavior behaviorForShip(Ship ship){
39          return behaviors.get(ship.id);
40      }
41  
42      public static Collection<EntityId> shipsForBehavior(Behavior.Type behavior){
43          ArrayList<EntityId> shipIds = new ArrayList<>();
44          for (Map.Entry<EntityId, Behavior> entry : behaviors.entrySet()){
45              if (entry.getValue().getType() == behavior){
46                  shipIds.add(entry.getKey());
47              }
48          }
49          return shipIds;
50      }
51  }