Exemplo n.º 1
0
 public boolean upgradeWeapon(String weapon) {
   Debug.debug(username + " upgrading his " + weapon);
   if (primaryWeapon.equals(weapon)) {
     Debug.stub("upgrading primary weapon (" + weapon + ")");
     boolean success = subtractResourcesForWeaponUpgrade(weapon, primaryWeaponLevel);
     if (success) {
       primaryWeaponLevel++;
       Debug.guiMessage(username + " upgrades his " + weapon);
       return true;
     } else {
       return false;
     }
   } else if (secondaryWeapon.equals(weapon)) {
     boolean success = subtractResourcesForWeaponUpgrade(weapon, secondaryWeaponLevel);
     if (success) {
       secondaryWeaponLevel++;
       Debug.guiMessage(username + " upgrades his " + weapon);
       return true;
     } else {
       return false;
     }
   } else {
     Debug.warn(username + " tried to upgrade weapon '" + weapon + "', but doesn't have it.");
     return false;
   }
 }
Exemplo n.º 2
0
 public boolean mineResource() {
   TileType tileType = this.position.tileType;
   Debug.game("Player " + username + " mining " + tileType);
   boolean minedResource = this.position.mineTile();
   if (minedResource) {
     if (tileType == TileType.RUBIDIUM) {
       rubidiumResources++;
     }
     if (tileType == TileType.EXPLOSIUM) {
       explosiumResources++;
     }
     if (tileType == TileType.SCRAP) {
       scrapResources++;
     }
     Debug.debug(
         "Resources of player "
             + username
             + " are now: Rubidium: "
             + rubidiumResources
             + ", Explosium: "
             + explosiumResources
             + ", Scrap: "
             + scrapResources);
     return true;
   }
   return false;
 }
Exemplo n.º 3
0
 public void damagePlayer(int hitpoints, AIConnection dealingPlayer) {
   if (health <= 0) {
     Debug.warn("Player is already dead.");
     return;
   }
   Debug.stub(
       "'"
           + this.username
           + "' received "
           + hitpoints
           + " damage from '"
           + dealingPlayer.username
           + "'!");
   health -= hitpoints;
   if (!(dealingPlayer.username.equals(this.username))) {
     dealingPlayer.givePoints(hitpoints); // damaged user other than
     // self, award points
   }
   if (health <= 0) {
     Debug.game(this.username + " got killed by " + dealingPlayer.username);
     if (!(dealingPlayer.username.equals(this.username))) {
       dealingPlayer.givePoints(20); // 20 bonus points for killing
       // someone
     }
     score -= 40;
     health = 0;
     hasToPass = true;
     needsRespawn = true;
   }
 }
Exemplo n.º 4
0
 public boolean shootDroid(JSONObject action, int turnsLeft) {
   int droidLevel = 1; // TODO
   if (primaryWeapon.equals("droid")) {
     droidLevel = primaryWeaponLevel;
   } else if (secondaryWeapon.equals("droid")) {
     droidLevel = secondaryWeaponLevel;
   } else {
     Debug.warn("User '" + username + "' attempted to shoot the droid, but doesn't have it");
     return false;
   }
   int range = 3;
   if (droidLevel == 2) {
     range = 4;
   }
   if (droidLevel == 3) {
     range = 5;
   } // replicated here for more friendly error messages
   try {
     JSONArray directionSequence = action.getJSONArray("sequence");
     Droid droid = new Droid(this, turnsLeft);
     if (directionSequence.length() > range) {
       Debug.warn(
           "Got "
               + directionSequence.length()
               + " commands for the droid, but your droids level ("
               + droidLevel
               + ") only supports "
               + range
               + " steps.");
       sendError(
           "Got "
               + directionSequence.length()
               + " commands for the droid, but your droids level ("
               + droidLevel
               + ") only supports "
               + range
               + " steps.");
     }
     if (droid.setDirections(directionSequence, droidLevel)) {
       droid.setPosition(position);
       int stepsTaken = droid.performShot();
       JSONArray truncatedArray = new JSONArray();
       for (int i = 0; i < stepsTaken; i++) {
         truncatedArray.put(directionSequence.get(i));
       }
       action.put("sequence", truncatedArray);
       Debug.debug("droid steps taken: " + stepsTaken);
     } else {
       sendError("Invalid shot: unknown direction in droid sequence");
       return false;
     }
     return true;
   } catch (JSONException e) {
     sendError("Invalid shot: lacks a direction key");
   }
   return false;
 }
Exemplo n.º 5
0
 public void sendMessage(JSONObject o) throws IOException {
   if (!isAlive) {
     Debug.debug("player '" + this.username + "' disconnected, not sending...");
     return;
   }
   socket.getOutputStream().write((o.toString() + "\n").getBytes());
 }
Exemplo n.º 6
0
 private void parseLoadout(JSONObject o) throws ProtocolException {
   try {
     if (!(o.get("message").equals("loadout"))) {
       throw new ProtocolException("Expected 'loadout', but got '" + o.get("message") + "' key");
     }
     if (!Util.validateWeapon(o.getString("primary-weapon"))) {
       throw new ProtocolException(
           "Invalid primary weapon: '" + o.getString("primary-weapon") + "'");
     }
     if (!Util.validateWeapon(o.getString("secondary-weapon"))) {
       throw new ProtocolException(
           "Invalid secondary weapon: '" + o.getString("secondary-weapon") + "'");
     }
     if (o.getString("primary-weapon").equals(o.getString("secondary-weapon"))) {
       throw new ProtocolException("Invalid loadout: Can't have the same weapon twice.");
     }
     primaryWeapon = o.getString("primary-weapon");
     secondaryWeapon = o.getString("secondary-weapon");
     Debug.info(
         username + " selected loadout: " + primaryWeapon + " and " + secondaryWeapon + ".");
     gotLoadout.set(true);
   } catch (JSONException e) {
     throw new ProtocolException("Invalid or incomplete packet: " + e.getMessage());
   }
 }
Exemplo n.º 7
0
 public AIConnection(Socket clientSocket) {
   messages = new ConcurrentLinkedQueue<JSONObject>();
   socket = clientSocket;
   try {
     inputReader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
   } catch (IOException e) {
     Debug.error("error creating connection handler: " + e);
   }
 }
Exemplo n.º 8
0
 public void sendDeadline() {
   JSONObject o = new JSONObject();
   try {
     o.put("message", "endturn");
     sendMessage(o);
   } catch (JSONException e) {
   } catch (IOException e) {
     Debug.error("Error writing to '" + username + "': " + e.getMessage());
   }
 }
Exemplo n.º 9
0
 public void clearAllMessages() {
   if (messages.size() > 0) {
     Debug.warn(
         "Message inbox of "
             + username
             + " contained "
             + messages.size()
             + " extra messages, discarding...");
   }
   messages.clear();
 }
Exemplo n.º 10
0
 boolean subtractResourcesForWeaponUpgrade(String weapon, int currentLevel) {
   int resourcesToSubtract = 4;
   if (currentLevel == 2) {
     resourcesToSubtract = 5;
   }
   if (currentLevel == 3) {
     Debug.warn(username + " tried to upgrade his " + weapon + ", but it is already level 3.");
     return false;
   }
   if (weapon.equals("laser")) {
     if (rubidiumResources >= resourcesToSubtract) {
       rubidiumResources -= resourcesToSubtract;
       return true;
     } else {
       Debug.warn("Tried to upgrade the laser, but not enough rubidium");
       return false;
     }
   }
   if (weapon.equals("mortar")) {
     if (explosiumResources >= resourcesToSubtract) {
       explosiumResources -= resourcesToSubtract;
       return true;
     } else {
       Debug.warn("Tried to upgrade the mortar, but not enough explosium");
       return false;
     }
   }
   if (weapon.equals("droid")) {
     if (scrapResources >= resourcesToSubtract) {
       scrapResources -= resourcesToSubtract;
       return true;
     } else {
       Debug.warn("Tried to upgrade the droid, but not enough scrap");
       return false;
     }
   }
   return false;
 }
Exemplo n.º 11
0
  public void sendGamestate(
      int turnNumber, int dimension, String mapData[][], AIConnection playerlist[]) {
    JSONObject root = new JSONObject();
    try {
      root.put("message", "gamestate");
      root.put("turn", turnNumber);
      JSONArray players = new JSONArray();

      for (AIConnection ai : playerlist) {
        JSONObject playerObject = new JSONObject();
        playerObject.put("name", ai.username);
        if (turnNumber != 0) {
          playerObject.put("health", ai.health);
          playerObject.put("score", ai.score);
          playerObject.put("position", ai.position.coords.getCompactString());
          JSONObject primaryWeaponObject = new JSONObject();
          primaryWeaponObject.put("name", ai.primaryWeapon);
          primaryWeaponObject.put("level", ai.primaryWeaponLevel);
          playerObject.put("primary-weapon", primaryWeaponObject);

          JSONObject secondaryWeaponObject = new JSONObject();
          secondaryWeaponObject.put("name", ai.secondaryWeapon);
          secondaryWeaponObject.put("level", ai.secondaryWeaponLevel);
          playerObject.put("secondary-weapon", secondaryWeaponObject);
        }

        players.put(playerObject);
      }
      root.put("players", players);

      JSONObject map = new JSONObject();
      map.put("j-length", dimension);
      map.put("k-length", dimension);
      map.put("data", new JSONArray(mapData));
      root.put("map", map);
    } catch (JSONException e) {
    }
    try {
      sendMessage(root);
    } catch (IOException e) {
      Debug.error("Error writing to '" + username + "': " + e.getMessage());
    }
  }
Exemplo n.º 12
0
 public boolean shootMortar(JSONObject action, int turnsLeft) {
   int mortarLevel = 1;
   if (primaryWeapon.equals("mortar")) {
     mortarLevel = primaryWeaponLevel;
   } else if (secondaryWeapon.equals("mortar")) {
     mortarLevel = secondaryWeaponLevel;
   } else {
     Debug.warn("User '" + username + "' attempted to shoot the mortar, but doesn't have it");
     return false;
   }
   try {
     Coordinate relativeTargetCoordinates = new Coordinate(action.getString("coordinates"));
     Mortar mortar = new Mortar(this, turnsLeft);
     mortar.setPosition(this.position);
     mortar.setTarget(relativeTargetCoordinates, mortarLevel);
     return mortar.performShot();
   } catch (JSONException e) {
     sendError("Invalid shot: lacks 'coordinates' key");
     return false;
   }
 }
Exemplo n.º 13
0
 public void givePenality(int points) {
   Debug.warn(username + " got " + points + " penality");
   score -= points;
 }
Exemplo n.º 14
0
 void givePoints(int points) {
   Debug.info("got awarded " + points + " points");
   score += points;
 }
Exemplo n.º 15
0
 public synchronized void setSpawnpoint(Tile spawnpoint) {
   Debug.info("Player '" + username + "' spawns at " + spawnpoint.coords.getString());
   position = spawnpoint;
   spawnTile = spawnpoint;
   position.playerOnTile = this;
 }