Skip to content

Commit

Permalink
[BROKEN] use splitLookup for waiting robots
Browse files Browse the repository at this point in the history
This causes multiple solutions to fail, e.g. the 2048 scenario.
  • Loading branch information
kostmo committed Jan 23, 2024
1 parent 6941973 commit 51f246d
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions src/swarm-engine/Swarm/Game/State/Robot.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -298,20 +298,25 @@ 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 ->
wakeLog %= (WakeLogEvent rid time DoneSleeping :)

-- 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
Expand Down Expand Up @@ -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 ->
Expand Down

0 comments on commit 51f246d

Please sign in to comment.