aboutsummaryrefslogtreecommitdiff
path: root/src/ch/epfl
diff options
context:
space:
mode:
authorPacien TRAN-GIRARD2016-04-07 22:12:35 +0200
committerPacien TRAN-GIRARD2016-04-07 22:12:35 +0200
commit3f322fd077aae6323ac162d5b6af5b1cfeda7d67 (patch)
tree9efbc1c84884b6bd744b65c22c8703da2d0f95e9 /src/ch/epfl
parent8ce0ed0c2f2837641cc7de8c328a9182f8825f22 (diff)
downloadxblast-3f322fd077aae6323ac162d5b6af5b1cfeda7d67.tar.gz
Make explosions great again
Diffstat (limited to 'src/ch/epfl')
-rw-r--r--src/ch/epfl/xblast/server/GameState.java52
1 files changed, 33 insertions, 19 deletions
diff --git a/src/ch/epfl/xblast/server/GameState.java b/src/ch/epfl/xblast/server/GameState.java
index c9a23ac..31bb902 100644
--- a/src/ch/epfl/xblast/server/GameState.java
+++ b/src/ch/epfl/xblast/server/GameState.java
@@ -245,6 +245,33 @@ public final class GameState {
245 } 245 }
246 246
247 /** 247 /**
248 * Returns a list of exploding bombs (reached by a blast or consumed fuse).
249 *
250 * @param bombs the list of bombs
251 * @param blastedCells the set of blasted cells
252 * @return the list of exploding bombs
253 */
254 private static List<Bomb> explodingBombs(List<Bomb> bombs, Set<Cell> blastedCells) {
255 return bombs.stream()
256 .filter(b -> blastedCells.contains(b.position()) || b.fuseLength() == 0)
257 .collect(Collectors.toList());
258 }
259
260 /**
261 * Computes the consumption of the bombs.
262 *
263 * @param bombs0 the list of bombs
264 * @param explodingBombs the list of exploding bombs
265 * @return the new bombs states
266 */
267 private static List<Bomb> nextBombs(List<Bomb> bombs0, List<Bomb> explodingBombs) {
268 return bombs0.stream()
269 .filter(b -> !explodingBombs.contains(b))
270 .map(b -> new Bomb(b.ownerId(), b.position(), b.fuseLengths().tail(), b.range()))
271 .collect(Collectors.toList());
272 }
273
274 /**
248 * Computes and returns the list of newly dropped bombs by players according to the given player states and events. 275 * Computes and returns the list of newly dropped bombs by players according to the given player states and events.
249 * Given bomb drop events must be conflict-free. 276 * Given bomb drop events must be conflict-free.
250 * 277 *
@@ -255,7 +282,7 @@ public final class GameState {
255 */ 282 */
256 private static List<Bomb> newlyDroppedBombs(List<Player> players0, Set<PlayerID> bombDropEvents, List<Bomb> bombs0) { 283 private static List<Bomb> newlyDroppedBombs(List<Player> players0, Set<PlayerID> bombDropEvents, List<Bomb> bombs0) {
257 HashSet<Cell> bombedCells = new HashSet<>(GameState.bombedCells(bombs0).keySet()); 284 HashSet<Cell> bombedCells = new HashSet<>(GameState.bombedCells(bombs0).keySet());
258 List<Bomb> bombs1 = new ArrayList<>(); 285 List<Bomb> bombs1 = new ArrayList<>(bombs0);
259 286
260 for (Player p : GameState.alivePlayers(players0)) { 287 for (Player p : GameState.alivePlayers(players0)) {
261 if (!bombDropEvents.contains(p.id())) continue; 288 if (!bombDropEvents.contains(p.id())) continue;
@@ -409,23 +436,23 @@ public final class GameState {
409 436
410 // 4.1. existing bombs evolution 437 // 4.1. existing bombs evolution
411 Set<Cell> blastedCells0 = this.blastedCells(); 438 Set<Cell> blastedCells0 = this.blastedCells();
412 List<Bomb> explodingBombs = this.filterExplodingBombs(this.bombs, blastedCells0); 439 List<Bomb> explodingBombs = GameState.explodingBombs(this.bombs, blastedCells0);
413 List<Bomb> bombs = this.bombs.stream().filter(b -> !explodingBombs.contains(b)).collect(Collectors.toList()); 440 List<Bomb> existingBombs = GameState.nextBombs(this.bombs, explodingBombs);
414 441
415 // 4.2. subsequent explosions addition 442 // 4.2. subsequent explosions addition
416 explodingBombs.forEach(b -> explosions1.addAll(b.explosion())); 443 explodingBombs.forEach(b -> explosions1.addAll(b.explosion()));
417 444
418 // 4.3. newly dropped bombs addition 445 // 4.3. newly dropped bombs addition
419 Set<PlayerID> topPriorityBombDropEvents = discardConflictingBombDropEvents(bombDropEvents); 446 Set<PlayerID> topPriorityBombDropEvents = discardConflictingBombDropEvents(bombDropEvents);
420 bombs.addAll(GameState.newlyDroppedBombs(this.players, topPriorityBombDropEvents, this.bombs)); 447 List<Bomb> bombs1 = GameState.newlyDroppedBombs(this.players, topPriorityBombDropEvents, existingBombs);
421 448
422 // 5. players evolution 449 // 5. players evolution
423 Map<PlayerID, Bonus> playerBonuses = mapPlayersToBonuses(consumedBonuses); 450 Map<PlayerID, Bonus> playerBonuses = mapPlayersToBonuses(consumedBonuses);
424 Set<Cell> bombedCells1 = GameState.bombedCells(bombs).keySet(); 451 Set<Cell> bombedCells1 = GameState.bombedCells(bombs1).keySet();
425 List<Player> players1 = GameState.nextPlayers( 452 List<Player> players1 = GameState.nextPlayers(
426 this.players, playerBonuses, bombedCells1, board1, blastedCells1, speedChangeEvents); 453 this.players, playerBonuses, bombedCells1, board1, blastedCells1, speedChangeEvents);
427 454
428 return new GameState(this.ticks, board1, players1, bombs, explosions1, blasts1); 455 return new GameState(this.ticks, board1, players1, bombs1, explosions1, blasts1);
429 } 456 }
430 457
431 /** 458 /**
@@ -523,19 +550,6 @@ public final class GameState {
523 return bombDropMap.isEmpty() ? EnumSet.noneOf(PlayerID.class) : EnumSet.copyOf(bombDropMap.values()); 550 return bombDropMap.isEmpty() ? EnumSet.noneOf(PlayerID.class) : EnumSet.copyOf(bombDropMap.values());
524 } 551 }
525 552
526 /**
527 * Returns a list of exploding bombs (reached by a blast or consumed fuse).
528 *
529 * @param bombs the list of bombs
530 * @param blastedCells the set of blasted cells
531 * @return the list of exploding bombs
532 */
533 private List<Bomb> filterExplodingBombs(List<Bomb> bombs, Set<Cell> blastedCells) {
534 return bombs.stream()
535 .filter(b -> blastedCells.contains(b.position()) || b.fuseLength() == 0)
536 .collect(Collectors.toList());
537 }
538
539 @Override 553 @Override
540 public String toString() { 554 public String toString() {
541 return "GameState{" + 555 return "GameState{" +