Skip to content

Commit

Permalink
Add subworlds
Browse files Browse the repository at this point in the history
  • Loading branch information
kostmo committed Jul 3, 2023
1 parent c9a89db commit 6084fca
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/Swarm/Game/Scenario.hs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ module Swarm.Game.Scenario (
scenarioEntities,
scenarioRecipes,
scenarioKnown,
secenarioSubworlds,
scenarioWorld,
scenarioRobots,
scenarioObjectives,
Expand Down Expand Up @@ -65,6 +66,7 @@ import Swarm.Game.Scenario.Objective.Validation
import Swarm.Game.Scenario.RobotLookup
import Swarm.Game.Scenario.Style
import Swarm.Game.Scenario.Topography.Cell
import Swarm.Game.Scenario.Topography.Subworld
import Swarm.Game.Scenario.Topography.WorldDescription
import Swarm.Language.Pipeline (ProcessedTerm)
import Swarm.Util (failT)
Expand All @@ -91,6 +93,7 @@ data Scenario = Scenario
, _scenarioEntities :: EntityMap
, _scenarioRecipes :: [Recipe Entity]
, _scenarioKnown :: [Text]
, _secenarioSubworlds :: [Subworld]
, _scenarioWorld :: WorldDescription
, _scenarioRobots :: [TRobot]
, _scenarioObjectives :: [Objective]
Expand All @@ -110,6 +113,8 @@ instance FromJSONE EntityMap Scenario where
Left x -> failT [x]
-- extend ambient EntityMap with custom entities

subworlds <- v ..:? "subworlds" ..!= []

withE em $ do
-- parse 'known' entity names and make sure they exist
known <- liftE (v .:? "known" .!= [])
Expand All @@ -133,6 +138,7 @@ instance FromJSONE EntityMap Scenario where
<*> pure em
<*> v ..:? "recipes" ..!= []
<*> pure known
<*> pure subworlds
<*> localE (,rsMap) (v ..: "world")
<*> pure rs
<*> (liftE (v .:? "objectives" .!= []) >>= validateObjectives)
Expand Down Expand Up @@ -178,6 +184,9 @@ scenarioRecipes :: Lens' Scenario [Recipe Entity]
-- not have to scan them.
scenarioKnown :: Lens' Scenario [Text]

-- | The subworlds of the scenario.
secenarioSubworlds :: Lens' Scenario [Subworld]

-- | The starting world for the scenario.
scenarioWorld :: Lens' Scenario WorldDescription

Expand Down
27 changes: 27 additions & 0 deletions src/Swarm/Game/Scenario/Topography/Subworld.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{-# LANGUAGE OverloadedStrings #-}

-- |
-- SPDX-License-Identifier: BSD-3-Clause
module Swarm.Game.Scenario.Topography.Subworld where

import Data.Aeson
import Swarm.Game.Entity
import Swarm.Game.Scenario.Topography.Navigation.Portal
import Swarm.Game.Scenario.RobotLookup
import Swarm.Game.Scenario.Topography.WorldDescription
import Swarm.Util.Yaml

data Subworld = Subworld
{ name :: SubworldName
, portals :: [Portal]
, world :: WorldDescription
}
deriving (Eq, Show)

instance FromJSONE EntityMap Subworld where
parseJSONE = withObjectE "subworld" $ \v -> do
n <- liftE (v .: "name")
c <- liftE (v .: "connectivity")
let rsMap = buildRobotMap []
w <- localE (,rsMap) (v ..: "world")
return $ Subworld n c w
1 change: 1 addition & 0 deletions swarm.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ library
Swarm.Game.Scenario.Scoring.ConcreteMetrics
Swarm.Game.Scenario.Scoring.GenericMetrics
Swarm.Game.Scenario.Status
Swarm.Game.Scenario.Topography.Subworld
Swarm.Game.Scenario.Style
Swarm.Game.Scenario.Topography.EntityFacade
Swarm.Game.Scenario.Topography.Navigation.Portal
Expand Down

0 comments on commit 6084fca

Please sign in to comment.