private void advancedStrategy( Agent a) { // afstand wordt bevoordeeld tov padlengte omwille van complexiteit if (a.getCarrying() > -1) { a.calcPath( calcXFromCoor(a.getCampLocation()), calcYFromCoor(a.getCampLocation())); // if carrying a Tilia, go to camp } else { // go pick up a tilia closest to position int coin = 0; if (a.isRed()) { if (coins_red.size() >= 1) { coin = coins_red.extractMin().getlocation(); } } else { if (coins_blue.size() >= 1) { coin = coins_blue.extractMin().getlocation(); } } if (coin == 0) { coin = a.getCampLocation(); } a.calcPath(calcXFromCoor(coin), calcYFromCoor(coin)); } }
private void hierarchicalStrategy(Agent a) { if (a.getCarrying() > -1) { a.calcPath( calcXFromCoor(a.getCampLocation()), calcYFromCoor(a.getCampLocation())); // if carrying a Tilia, go to camp } else { // go pick up a tilia closest to position int coin = 0; if (a.isRed()) { if (coins_red.size() >= 1) { coin = coins_red.extractMin().getlocation(); } } else { if (coins_blue.size() >= 1) { coin = coins_blue.extractMin().getlocation(); } } if (coin == 0) { coin = a.getCampLocation(); } a.calcPath(calcXFromCoor(coin), calcYFromCoor(coin)); } }
private boolean executeMovementAgent(Agent a, int wantsToMove) { int currentLocation = a.getLocation(); int xCurrent = this.calcXFromCoor(currentLocation); int yCurrent = this.calcYFromCoor(currentLocation); int xWants = this.calcXFromCoor(wantsToMove); int yWants = this.calcYFromCoor(wantsToMove); boolean isRed = a.isRed(); // check if this is a valid movement request, otherwhise crash the game // check if wantsToMove is in bounds if (wantsToMove < 0 || wantsToMove >= this.gridIdentifier.length) { System.err.println("REQUESTED MOVE OUT OF BOUNDS. EXITING."); System.exit(-1); } // we can either move up, down, left or right, just check the four cases // crash the game if the move is not one of these choices if (!((xCurrent == xWants - 1 && yCurrent == yWants) || (xCurrent == xWants + 1 && yCurrent == yWants) || (yCurrent == yWants + 1 && xCurrent == xWants) || (yCurrent == yWants - 1 && xCurrent == xWants))) { System.err.println(xCurrent); System.err.println(yCurrent); System.err.println(xWants); System.err.println(yWants); System.err.println("REQUESTED MOVE NOT UP,DOWN,RIGHT OR LEFT."); System.exit(-1); } // move is not legal because there is a rock if (gridIdentifier[wantsToMove].getIdentifier().equals(Tile.ROCK_TILE)) { System.err.println("REQUESTED TO MOVE ON ROCK SQUARE."); System.exit(-1); } // move is also illegal if you're tyring to move into the enemies camp if ((gridIdentifier[wantsToMove].getIdentifier().equals(Tile.BLUE_CAMP_TILE) && isRed) || (gridIdentifier[wantsToMove].getIdentifier().equals(Tile.RED_CAMP_TILE) && !isRed)) { System.err.println("REQUESTED TO MOVE ON ENEMIES CAMP."); System.exit(-1); } // I guess if we get here, we can actually move! // Then there are three options, either the goto tile is a coin or else // its a grass field or its own camp // if its a camp, the sprite shouldnt move, but if is carrying a coin it // should drop of the coin and the movement list should go empty if (isRed && (gridIdentifier[wantsToMove].getIdentifier().equals(Tile.RED_CAMP_TILE))) { int coin = a.getCarrying(); if (coin != -1) { coinDrop(isRed, a.getCarrying()); a.setCarrying(-1); } a.clearMovementList(); return false; } else if (!isRed && (gridIdentifier[wantsToMove].getIdentifier().equals(Tile.BLUE_CAMP_TILE))) { int coin = a.getCarrying(); if (coin != -1) { coinDrop(isRed, a.getCarrying()); a.setCarrying(-1); } a.clearMovementList(); return false; } // if its a coin, two things can happen, either the actor is already // carrying a coin, in which case the coin being held is dropped at the // current location of the actor, then the actor moves to destination // and removed the coin there and puts it in inventory // else if he is not carrying anything, he removes the coin and moves to // the target square if ((gridIdentifier[wantsToMove].getIdentifier().equals(Tile.COIN_TILE))) { int carrying = a.getCarrying(); // if he is carrying something, drop the coint if (carrying != -1) { // current location becomes a coin gridIdentifier[currentLocation] = mapper.get(Tile.COIN_TILE); // update coint location of coin dropped this.coinLocations.put(currentLocation, carrying); } else { gridIdentifier[currentLocation] = mapper.get(Tile.GRASS_TILE); } // targetlocation becomes sprite (red or blue) if (isRed) { gridIdentifier[wantsToMove] = mapper.get(Tile.RED_SPRITE_TILE); } else { gridIdentifier[wantsToMove] = mapper.get(Tile.BLUE_SPRITE_TILE); } // pick up the coin int pickup = this.coinLocations.get(wantsToMove); // give it to sprite a.setCarrying(pickup); // remove it from coint map this.coinLocations.remove(wantsToMove); // remove the action a.popMovement(); a.setLocation(wantsToMove); return true; } if ((gridIdentifier[wantsToMove].getIdentifier().equals(Tile.GRASS_TILE))) { if (isRed) { gridIdentifier[wantsToMove] = mapper.get(Tile.RED_SPRITE_TILE); } else { gridIdentifier[wantsToMove] = mapper.get(Tile.BLUE_SPRITE_TILE); } gridIdentifier[currentLocation] = mapper.get(Tile.GRASS_TILE); a.popMovement(); a.setLocation(wantsToMove); return true; } // this is still fatal if there is a head on collision, it is easier to // just allow for passing movements if (gridIdentifier[wantsToMove].getIdentifier().equals(Tile.BLUE_SPRITE_TILE) || gridIdentifier[wantsToMove].getIdentifier().equals(Tile.RED_SPRITE_TILE)) { if (isRed) { gridIdentifier[wantsToMove] = mapper.get(Tile.RED_SPRITE_TILE); } else { gridIdentifier[wantsToMove] = mapper.get(Tile.BLUE_SPRITE_TILE); } gridIdentifier[currentLocation] = mapper.get(Tile.GRASS_TILE); a.popMovement(); a.setLocation(wantsToMove); return true; } throw new RuntimeErrorException(new Error("Ran out of options.")); }
private void executeGameLogic() { // do processing logic // check if there is something in the queue and we are not processing // anything // note that at this stage, no threads can be running as we specifically // wait until a thread signals it is either // finished or at is stuck because it has no credits left // as this is the case we should not worry about other threads updating // these variables! // first check if something is finished processing for blue if (this.blueToAddScore > -1) { // add the score this.scoreBlue += this.blueToAddScore; // indicate that nothing is processing this.blueToAddScore = -1; this.processingBlue = -1; } // next check if something is finished processing for red if (this.redToAddScore > -1) { // add the score this.scoreRed += this.redToAddScore; // indicate that nothing is processing this.redToAddScore = -1; this.processingRed = -1; } // next check if blue can start processing something or it is in need of // new credits if (this.processingBlue == -1) { // check if there is something in the queue if (!this.queueBlue.isEmpty()) { // items are always processed in the order they were submitted this.processingBlue = this.queueBlue.poll(); blueProcStrategy(collectionBlue.getGrid(this.processingBlue)); } } else { CreditChecker.giveNewCredit(CreditChecker.BLUE_PROCESSING_THREAD); } // next check if red can start processing something or it is in need of // new credits if (this.processingRed == -1) { // check if there is something in the queue if (!this.queueRed.isEmpty()) { // items are always processed in the order they were submitted this.processingRed = this.queueRed.poll(); redProcStrategy(collectionRed.getGrid(this.processingRed)); } } else { CreditChecker.giveNewCredit(CreditChecker.RED_PROCESSING_THREAD); } // first decide the order of the agents to perform actions Collections.shuffle(this.allSprites); boolean[] actionPerformed = new boolean[this.allSprites.size()]; for (int i = 0; i < actionPerformed.length; i++) { actionPerformed[i] = false; } // first check all the movements for (int i = 0; i < this.allSprites.size(); i++) { Agent a = this.allSprites.get(i); // check if agent wants to move int wantsToMove = a.wantsToMove(); // yes it does want to move , then spent this turn moving if (wantsToMove != -1) { executeMovementAgent(a, wantsToMove); actionPerformed[i] = true; } } // now reset all the sprites as there is a bug with overlap for (int i = 0; i < this.allSprites.size(); i++) { Agent a = this.allSprites.get(i); if (a.isRed()) { gridIdentifier[a.getLocation()] = mapper.get(Tile.RED_SPRITE_TILE); } else { gridIdentifier[a.getLocation()] = mapper.get(Tile.BLUE_SPRITE_TILE); } } // now, all the agents which did not move and are not performing an // action can get a new command for (int i = 0; i < this.allSprites.size(); i++) { if (actionPerformed[i]) { continue; } Agent a = this.allSprites.get(i); Boolean bool = CreditChecker.isIdling(a.getId()); if (bool) { CreditChecker.giveNewCredit(a.getId()); } else { if (a.isRed()) { redStrategy(a); } else { blueStrategy(a); } } } }