Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
kostmo committed Feb 9, 2025
1 parent 4ebcc9b commit 3901d69
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 16 deletions.
16 changes: 14 additions & 2 deletions src/swarm-render/Swarm/Render/Structures.hs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ module Swarm.Render.Structures where

import Codec.Picture as JP
import Control.Carrier.Throw.Either
import Swarm.Game.Scenario.Topography.WorldDescription
import Control.Effect.Lift
import Data.List.NonEmpty qualified as NE
import Data.GraphViz (GraphvizParams (..))
import Data.GraphViz qualified as GV
import Data.GraphViz.Attributes.Complete as GVA
Expand Down Expand Up @@ -40,6 +42,12 @@ renderStructuresGraph ::
renderStructuresGraph imgRendering sMap = do
g' <- layoutGraph' params Dot g

putStrLn "Structure keys:"
print $ M.keys sMap
putStrLn "Edge list:"
print edgeList

putStrLn "------"
putStrLn . LT.unpack . GV.printDotGraph $ GV.graphToDot params g
let drawing =
drawGraph
Expand Down Expand Up @@ -138,7 +146,7 @@ renderStructuresGraph imgRendering sMap = do
where
i = genStructureImage imgRendering sMap x

gEdges = makeGraphEdges $ M.elems sMap
gEdges = makeStructureGraphEdges $ M.elems sMap

edgeList = [(m, n) | (_, n, neighbors) <- gEdges, m <- neighbors]
nodeList = [a | (_, a, _) <- gEdges]
Expand Down Expand Up @@ -171,12 +179,16 @@ doRenderStructures scenarioFilepath outputFilepath = do
(scenario, _) <- loadStandaloneScenario scenarioFilepath

let sMap = scenario ^. scenarioDiagnostic . scenarioStructureMap

Check warning on line 181 in src/swarm-render/Swarm/Render/Structures.hs

View workflow job for this annotation

GitHub Actions / Haskell-CI - windows-latest - ghc-9.8.2

Defined but not used: ‘sMap’
sMapWorld = view worldStructureMap . worldDiagnostic . NE.head $
scenario ^. scenarioLandscape . scenarioWorlds
aMap = scenario ^. scenarioLandscape . scenarioCosmetics

sendIO $ do
g <-
renderStructuresGraph (ImgRendering 8 DiagonalIndicators) $
M.map (applyStructureColors aMap) sMap
M.map (applyStructureColors aMap)
-- sMap
sMapWorld
putStrLn $ "Rendering to path: " ++ outputFilepath
renderRasterific outputFilepath (mkWidth 2000) g
putStrLn "Finished rendering."
Expand Down
2 changes: 1 addition & 1 deletion src/swarm-scenario/Swarm/Game/Scenario.hs
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ newtype ScenarioDiagnostic = ScenarioDiagnostic

makeLensesNoSigs ''ScenarioDiagnostic

-- | Authorship information about scenario not used at play-time
-- | Raw structure definitions at the scenario level
scenarioStructureMap :: Lens' ScenarioDiagnostic (M.Map Structure.StructureName (Structure.NamedStructure (Maybe Cell)))

-- | A 'Scenario' contains all the information to describe a
Expand Down
6 changes: 3 additions & 3 deletions src/swarm-scenario/Swarm/Game/Scenario/Objective/Graph.hs
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,8 @@ makeGraph edges =
where
(myGraph, _, _) = graphFromEdges edges

makeGraphEdges :: [Objective] -> Edges
makeGraphEdges objectives =
makeObjectiveGraphEdges :: [Objective] -> Edges
makeObjectiveGraphEdges objectives =
rootTuples <> negatedTuples
where
rootTuples = map f $ M.toList $ assignIds objectives
Expand All @@ -142,6 +142,6 @@ makeGraphInfo oc =
connectedComponents
(M.keys $ assignIds objs)
where
edges = makeGraphEdges objs
edges = makeObjectiveGraphEdges objs
connectedComponents = stronglyConnComp edges
objs = oc ^.. allObjectives
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,5 @@ validateObjectives objectives = do

return objectives
where
edges = makeGraphEdges objectives
edges = makeObjectiveGraphEdges objectives
allIds = Set.fromList $ mapMaybe (view objectiveId) objectives
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DerivingVia #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE RecordWildCards #-}

-- |
Expand All @@ -9,6 +10,7 @@ module Swarm.Game.Scenario.Topography.WorldDescription where

import Control.Carrier.Reader (runReader)
import Control.Carrier.Throw.Either
import Swarm.Util.Lens (makeLensesNoSigs)
import Control.Monad (forM)
import Data.Coerce
import Data.Functor.Identity
Expand Down Expand Up @@ -41,15 +43,29 @@ import Swarm.Game.Scenario.Topography.Structure.Recognition.Static (LocatedStruc
import Swarm.Game.Scenario.Topography.WorldPalette
import Swarm.Game.Universe (SubworldName (DefaultRootSubworld))
import Swarm.Game.World.Parse ()
import Control.Lens hiding (Const, use, uses, view, (%=), (+=), (.=), (<+=), (<<.=))
import Swarm.Game.World.Syntax
import Swarm.Game.World.Typecheck
import Swarm.Pretty (prettyString)
import Swarm.Util.Yaml
import Data.Map qualified as M
import Swarm.Game.Scenario.Topography.Structure qualified as Structure
import Swarm.Game.Scenario.Topography.Structure.Named qualified as Structure

------------------------------------------------------------
-- World description
------------------------------------------------------------

newtype WorldDiagnostic = WorldDiagnostic
{ _worldStructureMap :: M.Map Structure.StructureName (Structure.NamedStructure (Maybe Cell))
}

makeLensesNoSigs ''WorldDiagnostic

-- | Raw structure definitions at the subworld level
worldStructureMap :: Lens' WorldDiagnostic (M.Map Structure.StructureName (Structure.NamedStructure (Maybe Cell)))


-- | A description of a world parsed from a YAML file.
-- This type is parameterized to accommodate Cells that
-- utilize a less stateful Entity type.
Expand All @@ -64,8 +80,8 @@ data PWorldDescription e = WorldDescription
-- the structure recognizer
, worldName :: SubworldName
, worldProg :: Maybe (TTerm '[] (World CellVal))
, worldDiagnostic :: WorldDiagnostic
}
deriving (Show)

type WorldDescription = PWorldDescription Entity

Expand All @@ -91,10 +107,10 @@ instance FromJSONE WorldParseDependencies WorldDescription where
withDeps $
v ..:? "structures" ..!= []

let initialStructureDefs = scenarioLevelStructureDefs <> subworldLocalStructureDefs
liftE $ mkWorld tem worldMap palette initialStructureDefs v
liftE $ mkWorld tem worldMap palette scenarioLevelStructureDefs subworldLocalStructureDefs v
where
mkWorld tem worldMap palette initialStructureDefs v = do
mkWorld tem worldMap palette scenarioLevelStructureDefs subworldLocalStructureDefs v = do
let initialStructureDefs = scenarioLevelStructureDefs <> subworldLocalStructureDefs
MergedStructure mergedGrid staticStructurePlacements unmergedWaypoints <- do
unflattenedStructure <- parseStructure palette initialStructureDefs v

Expand Down Expand Up @@ -128,6 +144,7 @@ instance FromJSONE WorldParseDependencies WorldDescription where
map (offsetLoc $ coerce ul) staticStructurePlacements

let area = modifyLoc ((ul .+^) . asVector) mergedGrid
let worldDiagnostic = WorldDiagnostic $ Assembly.makeStructureMap subworldLocalStructureDefs
return $ WorldDescription {..}

------------------------------------------------------------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@
module Swarm.Game.Scenario.Topography.Structure.Assembly (
mergeStructures,
makeStructureMap,
makeGraphEdges,
makeStructureGraphEdges,

-- * Exposed for unit tests:
foldLayer,
)
where

import Data.Tree
import Control.Arrow (left, (&&&))
import Control.Monad (when)
import Data.Coerce
Expand Down Expand Up @@ -67,8 +68,12 @@ makeStructureMap = M.fromList . map (name &&& id)

type GraphEdge a = (NamedStructure a, StructureName, [StructureName])

makeGraphEdges :: [NamedStructure a] -> [GraphEdge a]
makeGraphEdges =
makeStructureDefinitionTree :: NamedStructure a -> Tree (NamedStructure a)
makeStructureDefinitionTree s =

Check warning on line 72 in src/swarm-topography/Swarm/Game/Scenario/Topography/Structure/Assembly.hs

View workflow job for this annotation

GitHub Actions / Haskell-CI - windows-latest - ghc-9.8.2

Defined but not used: ‘makeStructureDefinitionTree’
Node s . map makeStructureDefinitionTree . structures $ structure s

makeStructureGraphEdges :: [NamedStructure a] -> [GraphEdge a]
makeStructureGraphEdges =
map makeGraphNodeWithEdges
where
makeGraphNodeWithEdges s =
Expand Down Expand Up @@ -96,7 +101,7 @@ mergeStructures inheritedStrucDefs parentPlacement baseStructure = do

-- deeper definitions override the outer (toplevel) ones
structureMap = M.union (makeStructureMap subStructures) inheritedStrucDefs
gEdges = makeGraphEdges $ M.elems structureMap
gEdges = makeStructureGraphEdges $ M.elems structureMap

-- | NOTE: Each successive overlay may alter the coordinate origin.
-- We make sure this new origin is propagated to subsequent sibling placements.
Expand Down
1 change: 1 addition & 0 deletions src/swarm-tui/Swarm/TUI/Editor/Palette.hs
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ constructScenario maybeOriginalScenario cellGrid =
, placedStructures = mempty
, worldName = DefaultRootSubworld
, worldProg = Nothing
, worldDiagnostic = WorldDiagnostic mempty
}

extractPalette = unPalette . palette . NE.head . (^. scenarioLandscape . scenarioWorlds)
Expand Down
2 changes: 1 addition & 1 deletion src/swarm-web/Swarm/Web/GraphRender.hs
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,5 @@ renderGoalsGraph oc =
(\k -> maybe mempty (\(_, _, c) -> c) $ M.lookup k edgeLookup)
([(a, a) | (_, a, _) <- edges])

edges = makeGraphEdges objs
edges = makeObjectiveGraphEdges objs
objs = oc ^.. allObjectives

0 comments on commit 3901d69

Please sign in to comment.