aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authortomsmeding <tom.smeding@gmail.com>2017-03-22 20:55:25 +0100
committertomsmeding <tom.smeding@gmail.com>2017-03-22 20:55:25 +0100
commit9970586deb17a5e8623619d79245fa4c7a5d9b55 (patch)
tree35e99abd00f8c0c9c6c6e10f789d867ddddc6f1b
parent936f0109093d2f75408e87a5b6d0492af097164f (diff)
FIX CRITICAL BUG: timing issue where simulation was subtly incorrect
-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;
}
}
}