Exemplo n.º 1
0
  public static class FindNewMaster extends Quest {
    private final HashMap<Clan, Double> respectMemo = new HashMap<Clan, Double>();
    private int attemptsLeft = 1 + Me.useBeh(M_.PATIENCE);

    public FindNewMaster(Clan P) {
      super(P);
    }

    @Override
    public String description() {
      return "Find new master";
    }

    @Override
    public void avatarPursue() {
      if (Me.getBoss() != Me) {
        throw new IllegalStateException("this quest is only for RONIN");
      }
      avatarConsole()
          .showChoices(
              "Choose new master",
              Me,
              Me.myShire().getCensus().toArray(),
              SubjectiveType.RESPECT_ORDER,
              new Calc.Listener() {
                @Override
                public void call(Object arg) {
                  Clan clan = (Clan) arg;
                  if (clan == Me && Me.myOrder() == null) {
                    Order.createBy(Me);
                    success(Me);
                  } else {
                    Me.join(clan);
                    success(clan);
                  }
                }
              });
    }

    @Override
    public void pursue() {
      final Clan currBoss = Me.getBoss();
      if (currBoss != Me) {
        success(currBoss);
        Me.addReport(GobLog.findSomeone(currBoss, "same old master"));
        return;
      }
      if (attemptsLeft-- > 0) {
        Clan candidate = Me.myShire().getRandOfCensus();
        final double resp = Me.FB.randomValueInPriority().compare(Me, candidate, Me);
        if (Me != candidate && !Me.isSomeBossOf(candidate)) {
          Double d = respectMemo.get(candidate);
          if (d == null) {
            respectMemo.put(candidate, resp);
          } else {
            respectMemo.put(candidate, resp + d);
          }
        }
      } else {
        Clan bestClan = null;
        double max = 0;
        for (Clan c : respectMemo.keySet()) {
          double d = respectMemo.get(c);
          if (d > max) {
            max = d;
            bestClan = c;
          }
        }
        if (max > 0) {
          Me.join(
              bestClan); // TODO boss should be able to reject (cuz minions cost money) ... in that
                         // case just offer service?
          success(bestClan);
          Me.addReport(GobLog.findSomeone(bestClan, "new master"));
        } else {
          if (Me.myOrder() == null) {
            Order.createBy(Me);
            success(Me);
            Me.addReport(GobLog.createOrder(false));
          } else {
            success();
            Me.addReport(GobLog.createOrder(true));
          }
        }
      }
    }
  }