diff --git a/src/swarm-engine/Swarm/Game/State/Robot.hs b/src/swarm-engine/Swarm/Game/State/Robot.hs index da65c481d3..5530dc1c91 100644 --- a/src/swarm-engine/Swarm/Game/State/Robot.hs +++ b/src/swarm-engine/Swarm/Game/State/Robot.hs @@ -68,7 +68,7 @@ import Data.Maybe (fromMaybe, mapMaybe) import Data.Set qualified as S import Data.Tuple (swap) import GHC.Generics (Generic) -import Swarm.Game.CESK (CESK (Waiting), TickNumber (..), addTicks) +import Swarm.Game.CESK (CESK (Waiting), TickNumber (..)) import Swarm.Game.Location import Swarm.Game.ResourceLoading (NameGenerator) import Swarm.Game.Robot @@ -298,12 +298,15 @@ activateRobot rid = internalActiveRobots %= IS.insert rid -- if they still exist in the keys of 'robotMap'. wakeUpRobotsDoneSleeping :: (Has (State Robots) sig m) => TickNumber -> m () wakeUpRobotsDoneSleeping time = do - mrids <- internalWaitingRobots . at time <<.= Nothing + waitingMap <- use waitingRobots + let (beforeMap, maybeAt, futureMap) = M.splitLookup time waitingMap + mrids = NE.nonEmpty $ concat $ fromMaybe [] maybeAt : M.elems beforeMap + case mrids of Nothing -> return () Just rids -> do robots <- use robotMap - let aliveRids = filter (`IM.member` robots) rids + let aliveRids = filter (`IM.member` robots) $ NE.toList rids internalActiveRobots %= IS.union (IS.fromList aliveRids) forM_ aliveRids $ \rid -> @@ -311,7 +314,9 @@ wakeUpRobotsDoneSleeping time = do -- These robots' wake times may have been moved "forward" -- by 'wakeWatchingRobots'. - clearWatchingRobots rids + clearWatchingRobots $ NE.toList rids + + internalWaitingRobots .= futureMap -- | Clear the "watch" state of all of the -- awakened robots @@ -363,7 +368,8 @@ wakeWatchingRobots currentTick loc = do -- Step 4: Re-add the watching bots to be awakened at the next tick: wakeableBotIds = map fst wakeTimes - newWakeTime = addTicks 1 currentTick + -- newWakeTime = addTicks 1 currentTick + newWakeTime = currentTick newInsertions = M.singleton newWakeTime wakeableBotIds forM_ wakeableBotIds $ \rid ->