From 9970586deb17a5e8623619d79245fa4c7a5d9b55 Mon Sep 17 00:00:00 2001 From: tomsmeding Date: Wed, 22 Mar 2017 20:55:25 +0100 Subject: FIX CRITICAL BUG: timing issue where simulation was subtly incorrect --- world.cpp | 53 ++++++++++++++++++++++++++++++++--------------------- 1 file 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 +#include #include #include #include -#include #include "world.h" @@ -301,29 +302,39 @@ void World::removeTeam(const Team *team){ } void World::tick(){ + vector> candidates; + candidates.reserve(SIZE*SIZE); + for(int y=0;ytick(*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 &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; } } } -- cgit v1.2.3-54-g00ecf