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;
 }
 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;
   }
 }
 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;
   }
 }
 public void clearAllMessages() {
   if (messages.size() > 0) {
     Debug.warn(
         "Message inbox of "
             + username
             + " contained "
             + messages.size()
             + " extra messages, discarding...");
   }
   messages.clear();
 }
 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;
 }
 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;
   }
 }
 public void givePenality(int points) {
   Debug.warn(username + " got " + points + " penality");
   score -= points;
 }