#So how does my algorithm work
So how does my algorithm work
#So how does my algorithm work
So how does my algorithm work
/**
* Checks if a player has a collision according to his move, in the given turn and move-phase
* @param p The player to check
* @param turn The turn
* @param phase The move-phase step
* @param setPosition If to set the next position or not on non-collided result
* @return <code>TRUE</code> If the player was collided, <code>FALSE</code> if not.
*/
public boolean checkCollision(Player p, int turn, int phase, boolean setPosition) {
// The current selected move of the player
MoveType move = p.getMoves().getMove(turn);
// If this player was bumped, and a move was not selected, we want to process the bump animation
// But we have to check if the position to be bumped is available to be claimed
if (move == MoveType.NONE && p.getCollisionStorage().isBumped()) {
Position pos = p.getCollisionStorage().getBumpAnimation().getPositionForAnimation(p);
Player claimed = players.getPlayerByPosition(pos.getX(), pos.getY());
// Claiming checking for the new position for bump
return claimed != null && (claimed.getMoves().getMove(turn) == MoveType.NONE || checkCollision(claimed, turn, phase, false));
}
// Use the current position as default.txt, imply we have already set it
Position position = p;
// If not set by default.txt on previous loops, gets the next position on the map for the given phase on the given move
if (!p.getCollisionStorage().isPositionChanged()) {
position = move.getNextPositionWithPhase(p, p.getFace(), phase);
}
// If the player has moved since his last position
if (!position.equals(p)) {
// Check for bounds collision with the border
if (checkBoundCollision(p, turn, phase) || checkRockCollision(p, turn, phase)) {
return true;
}
// Check if the next position is claimed by another player, null result if not
Player claimed = players.getPlayerByPosition(position.getX(), position.getY());
// If the result is not null, the position is claimed
if (claimed != null) {
Position claimedNextPos = claimed;
if (!claimed.getCollisionStorage().isPositionChanged()) {
claimedNextPos = claimed.getMoves().getMove(turn).getNextPositionWithPhase(claimed, claimed.getFace(), phase);
}
// Check if the claimed position doesn't move away
if (claimed.getMoves().getMove(turn) == MoveType.NONE || claimedNextPos.equals(claimed)) {
if (move != MoveType.FORWARD || claimed.getVessel().getSize() >= p.getVessel().getSize()) {
collide(p, claimed, turn, phase);
}
if (move == MoveType.FORWARD && canBumpPlayer(p, claimed, turn, phase)) {
bumpPlayer(claimed, p, turn, phase);
p.set(position);
p.getCollisionStorage().setPositionChanged(true);
}
claimed.getVessel().appendDamage(p.getVessel().getRamDamage());
return true;
}
else if (claimedNextPos.equals(p)) { // If they switched positions (e.g nose to nose, F, F move)
collide(p, claimed, turn, phase);
collide(claimed, p, turn, phase);
return true;
}
else {
// Make sure that the claimed position moves away successfully
if (!checkCollision(claimed, turn, phase, false)) {
if (setPosition) {
// Moved successfully, claim position
p.set(position);
p.getCollisionStorage().setPositionChanged(true);
}
}
else {
// did not move successfully, collide
collide(p, claimed, turn, phase);
collide(claimed, p, turn, phase);
return true;
}
}
} else {
// List of players that collided with this player, while performing this move, in this phase
List<Player> collisions = getPlayersTryingToClaim(p, position, turn, phase);
if (collisions.size() > 0) { // Collision has happened
collisions.add(p);
Player largest = getLargestSize(collisions);
if (countPlayersForSize(collisions, largest.getVessel().getSize()) > 1) {
// Stop players from movement
for (Player pl : collisions) {
pl.getCollisionStorage().setCollided(turn, phase);
collide(pl, pl, turn, phase);
}
}
else {
for (Player pl : collisions) {
if (pl == largest) {
continue;
}
pl.getCollisionStorage().setCollided(turn, phase);
collide(pl, largest, turn, phase);
}
if (!largest.getCollisionStorage().isPositionChanged()) {
largest.set(position);
largest.getCollisionStorage().setPositionChanged(true);
}
}
return true;
} else {
if (setPosition) {
p.set(position);
p.getCollisionStorage().setPositionChanged(true);
}
}
}
}
return false;
}
/**
* Checks if a player has a collision according to his move, in the given turn and move-phase
* @param p The player to check
* @param turn The turn
* @param phase The move-phase step
* @param setPosition If to set the next position or not on non-collided result
* @return <code>TRUE</code> If the player was collided, <code>FALSE</code> if not.
*/
public boolean checkCollision(Player p, int turn, int phase, boolean setPosition) {
// The current selected move of the player
MoveType move = p.getMoves().getMove(turn);
// If this player was bumped, and a move was not selected, we want to process the bump animation
// But we have to check if the position to be bumped is available to be claimed
if (move == MoveType.NONE && p.getCollisionStorage().isBumped()) {
Position pos = p.getCollisionStorage().getBumpAnimation().getPositionForAnimation(p);
Player claimed = players.getPlayerByPosition(pos.getX(), pos.getY());
// Claiming checking for the new position for bump
return claimed != null && (claimed.getMoves().getMove(turn) == MoveType.NONE || checkCollision(claimed, turn, phase, false));
}
// Use the current position as default.txt, imply we have already set it
Position position = p;
// If not set by default.txt on previous loops, gets the next position on the map for the given phase on the given move
if (!p.getCollisionStorage().isPositionChanged()) {
position = move.getNextPositionWithPhase(p, p.getFace(), phase);
}
// If the player has moved since his last position
if (!position.equals(p)) {
// Check for bounds collision with the border
if (checkBoundCollision(p, turn, phase) || checkRockCollision(p, turn, phase)) {
return true;
}
// Check if the next position is claimed by another player, null result if not
Player claimed = players.getPlayerByPosition(position.getX(), position.getY());
// If the result is not null, the position is claimed
if (claimed != null) {
Position claimedNextPos = claimed;
if (!claimed.getCollisionStorage().isPositionChanged()) {
claimedNextPos = claimed.getMoves().getMove(turn).getNextPositionWithPhase(claimed, claimed.getFace(), phase);
}
// Check if the claimed position doesn't move away
if (claimed.getMoves().getMove(turn) == MoveType.NONE || claimedNextPos.equals(claimed)) {
if (move != MoveType.FORWARD || claimed.getVessel().getSize() >= p.getVessel().getSize()) {
collide(p, claimed, turn, phase);
}
if (move == MoveType.FORWARD && canBumpPlayer(p, claimed, turn, phase)) {
bumpPlayer(claimed, p, turn, phase);
p.set(position);
p.getCollisionStorage().setPositionChanged(true);
}
claimed.getVessel().appendDamage(p.getVessel().getRamDamage());
return true;
}
else if (claimedNextPos.equals(p)) { // If they switched positions (e.g nose to nose, F, F move)
collide(p, claimed, turn, phase);
collide(claimed, p, turn, phase);
return true;
}
else {
// Make sure that the claimed position moves away successfully
if (!checkCollision(claimed, turn, phase, false)) {
if (setPosition) {
// Moved successfully, claim position
p.set(position);
p.getCollisionStorage().setPositionChanged(true);
}
}
else {
// did not move successfully, collide
collide(p, claimed, turn, phase);
collide(claimed, p, turn, phase);
return true;
}
}
} else {
// List of players that collided with this player, while performing this move, in this phase
List<Player> collisions = getPlayersTryingToClaim(p, position, turn, phase);
if (collisions.size() > 0) { // Collision has happened
collisions.add(p);
Player largest = getLargestSize(collisions);
if (countPlayersForSize(collisions, largest.getVessel().getSize()) > 1) {
// Stop players from movement
for (Player pl : collisions) {
pl.getCollisionStorage().setCollided(turn, phase);
collide(pl, pl, turn, phase);
}
}
else {
for (Player pl : collisions) {
if (pl == largest) {
continue;
}
pl.getCollisionStorage().setCollided(turn, phase);
collide(pl, largest, turn, phase);
}
if (!largest.getCollisionStorage().isPositionChanged()) {
largest.set(position);
largest.getCollisionStorage().setPositionChanged(true);
}
}
return true;
} else {
if (setPosition) {
p.set(position);
p.getCollisionStorage().setPositionChanged(true);
}
}
}
}
return false;
}
/**
* Checks if a player has a collision according to his move, in the given turn and move-phase
* @param p The player to check
* @param turn The turn
* @param phase The move-phase step
* @param setPosition If to set the next position or not on non-collided result
* @return <code>TRUE</code> If the player was collided, <code>FALSE</code> if not.
*/
public boolean checkCollision(Player p, int turn, int phase, boolean setPosition) {
// The current selected move of the player
MoveType move = p.getMoves().getMove(turn);
// If this player was bumped, and a move was not selected, we want to process the bump animation
// But we have to check if the position to be bumped is available to be claimed
if (move == MoveType.NONE && p.getCollisionStorage().isBumped()) {
Position pos = p.getCollisionStorage().getBumpAnimation().getPositionForAnimation(p);
Player claimed = players.getPlayerByPosition(pos.getX(), pos.getY());
// Claiming checking for the new position for bump
return claimed != null && (claimed.getMoves().getMove(turn) == MoveType.NONE || checkCollision(claimed, turn, phase, false));
}
// Use the current position as default, imply we have already set it
Position position = p;
// If not set by default.txt on previous loops, gets the next position on the map for the given phase on the given move
if (!p.getCollisionStorage().isPositionChanged()) {
position = move.getNextPositionWithPhase(p, p.getFace(), phase);
}
// If the player has moved since his last position
if (!position.equals(p)) {
// Check for bounds collision with the border
if (checkBoundCollision(p, turn, phase) || checkRockCollision(p, turn, phase)) {
return true;
}
// Check if the next position is claimed by another player, null result if not
Player claimed = players.getPlayerByPosition(position.getX(), position.getY());
// If the result is not null, the position is claimed
if (claimed != null) {
Position claimedNextPos = claimed;
if (!claimed.getCollisionStorage().isPositionChanged()) {
claimedNextPos = claimed.getMoves().getMove(turn).getNextPositionWithPhase(claimed, claimed.getFace(), phase);
}
// Check if the claimed position doesn't move away
if (claimed.getMoves().getMove(turn) == MoveType.NONE || claimedNextPos.equals(claimed)) {
if (move != MoveType.FORWARD || claimed.getVessel().getSize() >= p.getVessel().getSize()) {
collide(p, claimed, turn, phase);
}
if (move == MoveType.FORWARD && canBumpPlayer(p, claimed, turn, phase)) {
bumpPlayer(claimed, p, turn, phase);
p.set(position);
p.getCollisionStorage().setPositionChanged(true);
}
claimed.getVessel().appendDamage(p.getVessel().getRamDamage());
return true;
}
else if (claimedNextPos.equals(p)) { // If they switched positions (e.g nose to nose, F, F move)
collide(p, claimed, turn, phase);
collide(claimed, p, turn, phase);
return true;
}
else {
// Make sure that the claimed position moves away successfully
if (!checkCollision(claimed, turn, phase, false)) {
if (setPosition) {
// Moved successfully, claim position
p.set(position);
p.getCollisionStorage().setPositionChanged(true);
}
}
else {
// did not move successfully, collide
collide(p, claimed, turn, phase);
collide(claimed, p, turn, phase);
return true;
}
}
} else {
// List of players that collided with this player, while performing this move, in this phase
List<Player> collisions = getPlayersTryingToClaim(p, position, turn, phase);
if (collisions.size() > 0) { // Collision has happened
collisions.add(p);
Player largest = getLargestSize(collisions);
if (countPlayersForSize(collisions, largest.getVessel().getSize()) > 1) {
// Stop players from movement
for (Player pl : collisions) {
pl.getCollisionStorage().setCollided(turn, phase);
collide(pl, pl, turn, phase);
}
}
else {
for (Player pl : collisions) {
if (pl == largest) {
continue;
}
pl.getCollisionStorage().setCollided(turn, phase);
collide(pl, largest, turn, phase);
}
if (!largest.getCollisionStorage().isPositionChanged()) {
largest.set(position);
largest.getCollisionStorage().setPositionChanged(true);
}
}
return true;
} else {
if (setPosition) {
p.set(position);
p.getCollisionStorage().setPositionChanged(true);
}
}
}
}
return false;
}
lang-java