aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--world.cpp53
1 files changed, 32 insertions, 21 deletions
diff --git a/world.cpp b/world.cpp
index ff13d46..f71eefe 100644
--- a/world.cpp
+++ b/world.cpp
@@ -1,7 +1,8 @@
+#include <unordered_map>
+#include <utility>
#include <cstdlib>
#include <cassert>
#include <cstring>
-#include <unordered_map>
#include "world.h"
@@ -301,29 +302,39 @@ void World::removeTeam(const Team *team){
}
void World::tick(){
+ vector<pair<int,int>> candidates;
+ candidates.reserve(SIZE*SIZE);
+
for(int y=0;y<SIZE;y++){
for(int x=0;x<SIZE;x++){
- if(!board[y][x])continue;
- switch(board[y][x]->tick(*this)){
- case WorldAction::none: break;
-
- case WorldAction::die:
- delete board[y][x];
- board[y][x]=nullptr;
- break;
-
- case WorldAction::move:{
- Robot** ptr=nullptr;
- switch(board[y][x]->heading%4){
- case 0: ptr=&board[(y+SIZE-1)%SIZE][x]; break;
- case 1: ptr=&board[y][(x+1)%SIZE]; break;
- case 2: ptr=&board[(y+1)%SIZE][x]; break;
- case 3: ptr=&board[y][(x+SIZE-1)%SIZE]; break;
- }
- *ptr=board[y][x];
- board[y][x]=nullptr;
- break;
+ if(board[y][x]&&board[y][x]->active){
+ candidates.emplace_back(x,y);
+ }
+ }
+ }
+
+ for(const pair<int,int> &p : candidates){
+ int x=p.first,y=p.second;
+ if(board[y][x]->active==0)continue;
+ switch(board[y][x]->tick(*this)){
+ case WorldAction::none: break;
+
+ case WorldAction::die:
+ delete board[y][x];
+ board[y][x]=nullptr;
+ break;
+
+ case WorldAction::move:{
+ Robot** ptr=nullptr;
+ switch(board[y][x]->heading%4){
+ case 0: ptr=&board[(y+SIZE-1)%SIZE][x]; break;
+ case 1: ptr=&board[y][(x+1)%SIZE]; break;
+ case 2: ptr=&board[(y+1)%SIZE][x]; break;
+ case 3: ptr=&board[y][(x+SIZE-1)%SIZE]; break;
}
+ *ptr=board[y][x];
+ board[y][x]=nullptr;
+ break;
}
}
}