コード例 #1
0
    @Override
    public Input handle(Action action, String actorID) {
      final Hunt act = (Hunt) action;
      final Food food = dmodel.getFoodById(act.getFoodTypeId());

      // This line gets the agents datamodel
      // Just trust me on that one :P
      final PublicAgentDataModel am = ((AbstractAgent) sim.getPlayer(actorID)).getDataModel();
      if (am.getHuntingTeam() == null) {
        Input result;
        if (food.getHuntersRequired() <= 1) {
          result =
              new HuntResult(actorID, food.getNutrition(), food.getNutrition(), dmodel.getTime());
        } else {
          result = new HuntResult(actorID, 0, 0, dmodel.getTime());
        }
        sim.getPlayer(actorID).enqueueInput(result);
      } else {
        if (!storedHuntResults.containsKey(am.getHuntingTeam())) {
          storedHuntResults.put(am.getHuntingTeam(), new LinkedList<TeamHuntEvent>());
        }
        storedHuntResults.get(am.getHuntingTeam()).add(new TeamHuntEvent(act, actorID));
      }
      logger.log(
          Level.FINE,
          "Agent {0} hunted {1}{2}",
          new Object[] {
            nameOf(actorID),
            food.getName(),
            am.getHuntingTeam() == null ? " alone." : " with team " + am.getHuntingTeam().hashCode()
          });
      return null;
    }
コード例 #2
0
  /**
   * Processes the {@link Hunt} actions of {@link AbstractAgent agents} that were hunting as part of
   * {@link HuntingTeam HuntingTeams} whose hunt records were added to {@link #storedHuntResults}
   */
  private void processTeamHunts() {
    for (HuntingTeam team : storedHuntResults.keySet()) {
      // Reorganise each team into what they hunted
      // And who hunted it
      Map<Food, List<String>> hunters = new HashMap<Food, List<String>>();
      for (TeamHuntEvent h : storedHuntResults.get(team)) {
        if (!hunters.containsKey(h.getFood())) {
          hunters.put(h.getFood(), new LinkedList<String>());
        }
        hunters.get(h.getFood()).add(h.getAgent());
      }

      // Now, for each food, see if they got a unit on it
      for (Food f : hunters.keySet()) {
        List<String> agents = hunters.get(f);
        double foodGained;

        int count = 0;
        while ((count + 1) * f.getHuntersRequired() <= agents.size()) {
          ++count;
        }

        foodGained = count * f.getNutrition() / agents.size();

        // Then, for each agent, send the message
        String groupID = dmodel.getAgentById(agents.get(0)).getGroupId();
        if (groupID == null) {
          for (String agent : agents) {
            sim.getPlayer(agent)
                .enqueueInput(new HuntResult(agent, foodGained, foodGained, dmodel.time));
          }
        } else {
          Participant g = sim.getPlayer(groupID);
          for (String agent : agents) {
            g.enqueueInput(new HuntResult(agent, foodGained, 0, dmodel.time));
          }
        }
      }
    }

    storedHuntResults.clear();
  }