Skip to content

Commit

Permalink
Don't step robots that are waiting, even during instant (#1322)
Browse files Browse the repository at this point in the history
This fixes #1267 in the best possible way (option 3): if a `wait` command is executed inside `instant`, then it goes into effect immediately, and any subsequent commands will be executed `instant`ly once the robot wakes up. For example, `instant (move; move; wait 1; move; move)` moves twice in one tick and then twice in the next tick.  Even calls to `wait` nested inside recursive function calls inside a call to `instant` work fine.
  • Loading branch information
byorgey authored Jun 10, 2023
1 parent 987ddd6 commit a83aa99
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
9 changes: 9 additions & 0 deletions src/Swarm/Game/Robot.hs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ module Swarm.Game.Robot (
-- ** Query
robotKnows,
isActive,
wantsToStep,
waitingUntil,
getResult,

Expand Down Expand Up @@ -517,6 +518,14 @@ isActive :: Robot -> Bool
{-# INLINE isActive #-}
isActive = isNothing . getResult

-- | "Active" robots include robots that are waiting; 'wantsToStep' is
-- true if the robot actually wants to take another step right now
-- (this is a *subset* of active robots).
wantsToStep :: TickNumber -> Robot -> Bool
wantsToStep now robot
| not (isActive robot) = False
| otherwise = maybe True (now >=) (waitingUntil robot)

-- | The time until which the robot is waiting, if any.
waitingUntil :: Robot -> Maybe TickNumber
waitingUntil robot =
Expand Down
9 changes: 5 additions & 4 deletions src/Swarm/Game/Step.hs
Original file line number Diff line number Diff line change
Expand Up @@ -584,10 +584,11 @@ tickRobot r = do
-- runs it for one step, then calls itself recursively to continue
-- stepping the robot.
tickRobotRec :: (Has (State GameState) sig m, Has (Lift IO) sig m) => Robot -> m Robot
tickRobotRec r
| isActive r && (r ^. runningAtomic || r ^. tickSteps > 0) =
stepRobot r >>= tickRobotRec
| otherwise = return r
tickRobotRec r = do
time <- use ticks
case wantsToStep time r && (r ^. runningAtomic || r ^. tickSteps > 0) of
True -> stepRobot r >>= tickRobotRec
False -> return r

-- | Single-step a robot by decrementing its 'tickSteps' counter and
-- running its CESK machine for one step.
Expand Down

0 comments on commit a83aa99

Please sign in to comment.