Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Recreate GameState from scratch when starting a scenario #1277

Merged
merged 14 commits into from
May 25, 2023
14 changes: 8 additions & 6 deletions bench/Benchmark.hs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
-- SPDX-License-Identifier: BSD-3-Clause
module Main where

import Control.Lens ((&), (.~))
import Control.Lens ((&), (.~), (^.))
import Control.Monad (replicateM_)
import Control.Monad.Except (runExceptT)
import Control.Monad.State (evalStateT, execStateT)
Expand All @@ -15,13 +15,15 @@ import Swarm.Game.CESK (emptyStore, initMachine)
import Swarm.Game.Display (defaultRobotDisplay)
import Swarm.Game.Location
import Swarm.Game.Robot (TRobot, mkRobot)
import Swarm.Game.State (GameState, addTRobot, classicGame0, creativeMode, world)
import Swarm.Game.State (GameState, addTRobot, creativeMode, world)
import Swarm.Game.Step (gameTick)
import Swarm.Game.Terrain (TerrainType (DirtT))
import Swarm.Game.World (WorldFun (..), newWorld)
import Swarm.Language.Context qualified as Context
import Swarm.Language.Pipeline (ProcessedTerm)
import Swarm.Language.Pipeline.QQ (tmQ)
import Swarm.TUI.Model (gameState)
import Swarm.TUI.Model.StateUpdate (classicGame0)

-- | The program of a robot that does nothing.
idleProgram :: ProcessedTerm
Expand Down Expand Up @@ -78,10 +80,10 @@ initRobot prog loc = mkRobot () Nothing "" [] (Just loc) north defaultRobotDispl
mkGameState :: (Location -> TRobot) -> Int -> IO GameState
mkGameState robotMaker numRobots = do
let robots = [robotMaker (Location (fromIntegral x) 0) | x <- [0 .. numRobots - 1]]
Right initState <- runExceptT classicGame0
Right initAppState <- runExceptT classicGame0
execStateT
(mapM addTRobot robots)
( initState
( (initAppState ^. gameState)
& creativeMode .~ True
& world .~ newWorld (WF $ const (fromEnum DirtT, Nothing))
)
Expand Down Expand Up @@ -115,6 +117,6 @@ main = do

toBenchmarks :: [(Int, GameState)] -> [Benchmark]
toBenchmarks gameStates =
[ bench (show n) $ whnfAppIO (runGame 1000) gameState
| (n, gameState) <- gameStates
[ bench (show n) $ whnfAppIO (runGame 1000) gs
| (n, gs) <- gameStates
]
16 changes: 2 additions & 14 deletions src/Swarm/App.hs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import Swarm.ReadableIORef (mkReadonly)
import Swarm.TUI.Controller
import Swarm.TUI.Model
import Swarm.TUI.Model.StateUpdate
import Swarm.TUI.Model.UI (defaultInitLgTicksPerSecond, uiAttrMap)
import Swarm.TUI.Model.UI (uiAttrMap)
import Swarm.TUI.View
import Swarm.Version (getNewerReleaseVersion)
import Swarm.Web
Expand Down Expand Up @@ -110,19 +110,7 @@ demoWeb :: IO ()
demoWeb = do
let demoPort = 8080
res <-
runExceptT $
initAppState $
AppOpts
{ userSeed = Nothing
, userScenario = demoScenario
, scriptToRun = Nothing
, autoPlay = False
, speed = defaultInitLgTicksPerSecond
, cheatMode = False
, colorMode = Nothing
, userWebPort = Nothing
, repoGitInfo = Nothing
}
runExceptT $ initAppState (defaultAppOpts {userScenario = demoScenario})
case res of
Left errMsg -> T.putStrLn errMsg
Right s -> do
Expand Down