Skip to content

Commit

Permalink
Remove superfluous trobotContext lens (#1731)
Browse files Browse the repository at this point in the history
# Motivation

Want to remove dependence of `TRobot` on the `RobotContext` record.  However, a lens `trobotContext` had been added in #817 to fix #394.

# Test

    scripts/run-tests.sh --test-arguments '--pattern "394-build-drill"'
  • Loading branch information
kostmo authored Jan 16, 2024
1 parent 3d87e71 commit 40ea471
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 36 deletions.
5 changes: 0 additions & 5 deletions src/swarm-engine/Swarm/Game/Robot.hs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ module Swarm.Game.Robot (
robotCapabilities,
walkabilityContext,
robotContext,
trobotContext,
robotID,
robotParentID,
robotHeavy,
Expand Down Expand Up @@ -413,10 +412,6 @@ robotInventory = robotEntity . entityInventory
-- | The robot's context.
robotContext :: Lens' Robot RobotContext

-- | The robot's context.
trobotContext :: Lens' TRobot RobotContext
trobotContext = lens _robotContext (\r c -> r {_robotContext = c})

-- | The (unique) ID number of the robot. This is only a Getter since
-- the robot ID is immutable.
robotID :: Getter Robot RID
Expand Down
23 changes: 19 additions & 4 deletions src/swarm-engine/Swarm/Game/State/Robot.hs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ module Swarm.Game.State.Robot (
addRobot,
addRobotToLocation,
addTRobot,
addTRobotWithContext,

-- ** View
modifyViewCenter,
Expand Down Expand Up @@ -230,14 +231,28 @@ viewCenterRule = lens getter setter
-- | Add a concrete instance of a robot template to the game state:
-- First, generate a unique ID number for it. Then, add it to the
-- main robot map, the active robot set, and to to the index of
-- robots by location. Return the updated robot.
addTRobot :: (Has (State Robots) sig m) => CESK -> TRobot -> m Robot
-- robots by location.
addTRobot :: (Has (State Robots) sig m) => CESK -> TRobot -> m ()
addTRobot initialMachine r = do
rid <- robotNaming . gensym <+= 1
let r' = instantiateRobot (Just initialMachine) rid r
r' <- addTRobotCommon initialMachine r
addRobot r'

-- | Just like 'addTRobot', but also updates the 'RobotContext',
-- and returns the updated robot.
addTRobotWithContext :: (Has (State Robots) sig m) => RobotContext -> CESK -> TRobot -> m Robot
addTRobotWithContext ctx initialMachine r = do
concreteRobot <- addTRobotCommon initialMachine r
let r' = concreteRobot & robotContext .~ ctx
addRobot r'
return r'

-- | Instantiate a template robot with a machine and
-- a unique ID.
addTRobotCommon :: (Has (State Robots) sig m) => CESK -> TRobot -> m Robot
addTRobotCommon initialMachine r = do
rid <- robotNaming . gensym <+= 1
return $ instantiateRobot (Just initialMachine) rid r

-- | Add a robot to the game state, adding it to the main robot map,
-- the active robot set, and to to the index of robots by
-- location.
Expand Down
42 changes: 20 additions & 22 deletions src/swarm-engine/Swarm/Game/Step/Combustion.hs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import Control.Applicative (Applicative (..))
import Control.Carrier.State.Lazy
import Control.Effect.Lens
import Control.Lens as Lens hiding (Const, distrib, from, parts, use, uses, view, (%=), (+=), (.=), (<+=), (<>=))
import Control.Monad (forM_, void, when)
import Control.Monad (forM_, when)
import Data.Text qualified as T
import Linear (zero)
import Swarm.Effect as Effect (Time, getNow)
Expand Down Expand Up @@ -95,8 +95,7 @@ addCombustionBot inputEntity combustibility ts loc = do
return $ maybe [] (pure . (1,)) maybeE
combustionDurationRand <- uniform durationRange
let combustionProg = combustionProgram combustionDurationRand combustibility
void
. zoomRobots
zoomRobots
. addTRobot (initMachine combustionProg empty emptyStore)
$ mkRobot
()
Expand Down Expand Up @@ -211,22 +210,21 @@ addIgnitionBot ::
Cosmic Location ->
m ()
addIgnitionBot ignitionDelay inputEntity ts loc =
void $
addTRobot (initMachine (ignitionProgram ignitionDelay) empty emptyStore) $
mkRobot
()
Nothing
"firestarter"
(Markdown.fromText $ T.unwords ["Delayed ignition of", (inputEntity ^. entityName) <> "."])
(Just loc)
zero
( defaultEntityDisplay '*'
& invisible .~ True
)
Nothing
[]
[]
True
False
mempty
ts
addTRobot (initMachine (ignitionProgram ignitionDelay) empty emptyStore) $
mkRobot
()
Nothing
"firestarter"
(Markdown.fromText $ T.unwords ["Delayed ignition of", (inputEntity ^. entityName) <> "."])
(Just loc)
zero
( defaultEntityDisplay '*'
& invisible .~ True
)
Nothing
[]
[]
True
False
mempty
ts
2 changes: 1 addition & 1 deletion src/swarm-engine/Swarm/Game/Step/Const.hs
Original file line number Diff line number Diff line change
Expand Up @@ -1061,7 +1061,7 @@ execConst runChildProg c vs s k = do
-- Construct the new robot and add it to the world.
parentCtx <- use robotContext
newRobot <-
zoomRobots . addTRobot (In cmd e s [FExec]) . (trobotContext .~ parentCtx) $
zoomRobots . addTRobotWithContext parentCtx (In cmd e s [FExec]) $
mkRobot
()
(Just pid)
Expand Down
4 changes: 1 addition & 3 deletions src/swarm-engine/Swarm/Game/Step/Util/Command.hs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import Control.Effect.Lens
import Control.Effect.Lift
import Control.Lens as Lens hiding (Const, distrib, from, parts, use, uses, view, (%=), (+=), (.=), (<+=), (<>=))
import Control.Monad (forM_, unless)
import Data.Functor (void)
import Data.Map qualified as M
import Data.Sequence qualified as Seq
import Data.Set (Set)
Expand Down Expand Up @@ -373,8 +372,7 @@ addSeedBot ::
TimeSpec ->
m ()
addSeedBot e (minT, maxT) loc ts =
void
. zoomRobots
zoomRobots
. addTRobot (initMachine (seedProgram minT (maxT - minT) (e ^. entityName)) empty emptyStore)
$ mkRobot
()
Expand Down
2 changes: 1 addition & 1 deletion test/bench/Benchmark.hs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ mkGameState prog robotMaker numRobots = do
let robots = [robotMaker (Location (fromIntegral x) 0) | x <- [0 .. numRobots - 1]]
Right initAppState <- runExceptT classicGame0
execStateT
(zoomRobots $ mapM (addTRobot $ initMachine prog Context.empty emptyStore) robots)
(zoomRobots $ mapM_ (addTRobot $ initMachine prog Context.empty emptyStore) robots)
( (initAppState ^. gameState)
& creativeMode .~ True
& landscape . multiWorld .~ M.singleton DefaultRootSubworld (newWorld (WF $ const (fromEnum DirtT, ENothing)))
Expand Down

0 comments on commit 40ea471

Please sign in to comment.