Skip to content

Commit

Permalink
new modules
Browse files Browse the repository at this point in the history
  • Loading branch information
kostmo committed Nov 18, 2023
1 parent 0453ed1 commit ab9fb5b
Show file tree
Hide file tree
Showing 8 changed files with 92 additions and 63 deletions.
2 changes: 2 additions & 0 deletions data/scenarios/Testing/1034-custom-attributes.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,12 @@ attrs:
- name: blueBackround
bg: "#0000ff"
- name: italicAndUnderline
fg: "#ffffff"
style:
- Italic
- Underline
- name: boldAndStrikethrough
fg: "#ffffff"
style:
- Bold
- Strikethrough
Expand Down
32 changes: 32 additions & 0 deletions src/Swarm/Game/Scenario/Topography/Center.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
-- |
-- SPDX-License-Identifier: BSD-3-Clause
--
-- Utility for determining the center of
-- the map, outside the context of a
-- running game
module Swarm.Game.Scenario.Topography.Center where

import Control.Lens (view)
import Data.List.NonEmpty (NonEmpty)
import Data.Maybe (fromMaybe, listToMaybe)
import Swarm.Game.Location (Location, origin)
import Swarm.Game.Robot (trobotLocation)
import Swarm.Game.Scenario (Scenario)
import Swarm.Game.State (SubworldDescription, genRobotTemplates)
import Swarm.Game.Universe (Cosmic (..), SubworldName (DefaultRootSubworld))

determineViewCenter ::
Scenario ->
NonEmpty SubworldDescription ->
Cosmic Location
determineViewCenter s worldTuples =
fromMaybe defaultVC baseRobotLoc
where
theRobots = genRobotTemplates s worldTuples
defaultVC = Cosmic DefaultRootSubworld origin

-- The first robot is guaranteed to be the base.
baseRobotLoc :: Maybe (Cosmic Location)
baseRobotLoc = do
theBaseRobot <- listToMaybe theRobots
view trobotLocation theBaseRobot
2 changes: 1 addition & 1 deletion src/Swarm/Game/World/Render.hs
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@ import Swarm.Game.Scenario (Scenario, area, scenarioCosmetics, scenarioWorlds, u
import Swarm.Game.Scenario.Status (seedLaunchParams)
import Swarm.Game.Scenario.Topography.Area (AreaDimensions (..), getAreaDimensions, isEmpty, upperLeftToBottomRight)
import Swarm.Game.Scenario.Topography.Cell
import Swarm.Game.Scenario.Topography.Center
import Swarm.Game.Scenario.Topography.EntityFacade (EntityFacade (..), mkFacade)
import Swarm.Game.State
import Swarm.Game.Terrain (getTerrainWord)
import Swarm.Game.Universe
import Swarm.Game.World qualified as W
import Swarm.TUI.Editor.Util (getContentAt, getMapRectangle)
import Swarm.TUI.View.Util (determineViewCenter)
import Swarm.Util (surfaceEmpty)
import Swarm.Util.Effect (simpleErrorHandle)
import Swarm.Util.Erasable (erasableToMaybe)
Expand Down
17 changes: 2 additions & 15 deletions src/Swarm/TUI/View.hs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ import Swarm.Game.Scenario.Scoring.CodeSize
import Swarm.Game.Scenario.Scoring.ConcreteMetrics
import Swarm.Game.Scenario.Scoring.GenericMetrics
import Swarm.Game.Scenario.Status
import Swarm.Game.Scenario.Topography.Center
import Swarm.Game.Scenario.Topography.Structure.Recognition (automatons)
import Swarm.Game.Scenario.Topography.Structure.Recognition.Type
import Swarm.Game.ScenarioInfo (
Expand Down Expand Up @@ -121,6 +122,7 @@ import Swarm.TUI.Panel
import Swarm.TUI.View.Achievement
import Swarm.TUI.View.Attribute.Attr
import Swarm.TUI.View.CellDisplay
import Swarm.TUI.View.Logo
import Swarm.TUI.View.Objective qualified as GR
import Swarm.TUI.View.Structure qualified as SR
import Swarm.TUI.View.Util as VU
Expand Down Expand Up @@ -176,21 +178,6 @@ newVersionWidget = \case
Left (NoMainUpstreamRelease _fails) -> Nothing
Left (OldUpstreamRelease _up _my) -> Nothing

drawLogo :: Text -> Widget Name
drawLogo = centerLayer . vBox . map (hBox . T.foldr (\c ws -> drawThing c : ws) []) . T.lines
where
drawThing :: Char -> Widget Name
drawThing c = withAttr (attrFor c) $ str [c]

attrFor :: Char -> AttrName
attrFor c
| c `elem` ("<>v^" :: String) = robotAttr
attrFor 'T' = plantAttr
attrFor '@' = rockAttr
attrFor '~' = waterAttr
attrFor '' = dirtAttr
attrFor _ = defAttr

-- | When launching a game, a modal prompt may appear on another layer
-- to input seed and/or a script to run.
drawNewGameMenuUI ::
Expand Down
22 changes: 2 additions & 20 deletions src/Swarm/TUI/View/Attribute/Attr.hs
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,11 @@ module Swarm.TUI.View.Attribute.Attr (
messageAttributeNames,
toAttrName,
getWorldAttrName,

-- ** Terrain attributes
dirtAttr,
grassAttr,
stoneAttr,
waterAttr,
iceAttr,
getTerrainAttrName,

-- ** Common attributes
entityAttr,
robotAttr,
rockAttr,
plantAttr,

-- ** Swarm TUI Attributes
highlightAttr,
Expand Down Expand Up @@ -130,15 +122,6 @@ getWorldAttrName (WorldAttr n) = worldPrefix <> attrName n
entityAttr :: AttrName
entityAttr = getWorldAttrName $ fst entity

waterAttr :: AttrName
waterAttr = getWorldAttrName $ fst water

rockAttr :: AttrName
rockAttr = getWorldAttrName $ fst rock

plantAttr :: AttrName
plantAttr = getWorldAttrName $ fst rock

robotMessagePrefix :: AttrName
robotMessagePrefix = attrName "robotMessage"

Expand Down Expand Up @@ -169,8 +152,7 @@ meterAttributeNames = NE.map fst activityMeterAttributes
robotAttr :: AttrName
robotAttr = attrName "robot"

dirtAttr, grassAttr, stoneAttr, iceAttr :: AttrName
dirtAttr = getTerrainAttrName $ fst dirt
grassAttr, stoneAttr, iceAttr :: AttrName
grassAttr = getTerrainAttrName $ fst grass
stoneAttr = getTerrainAttrName $ fst stone
iceAttr = getTerrainAttrName $ fst ice
Expand Down
42 changes: 42 additions & 0 deletions src/Swarm/TUI/View/Logo.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
{-# LANGUAGE OverloadedStrings #-}

-- |
-- SPDX-License-Identifier: BSD-3-Clause
--
-- Code for drawing the Swarm logo.
module Swarm.TUI.View.Logo where

import Brick
import Brick.Widgets.Center (centerLayer)
import Data.Text (Text)
import Data.Text qualified as T
import Swarm.Game.Entity.Cosmetic.Specimen
import Swarm.TUI.Model.Name
import Swarm.TUI.View.Attribute.Attr

drawLogo :: Text -> Widget Name
drawLogo = centerLayer . vBox . map (hBox . T.foldr (\c ws -> drawThing c : ws) []) . T.lines
where
drawThing :: Char -> Widget Name
drawThing c = withAttr (attrFor c) $ str [c]

attrFor :: Char -> AttrName
attrFor c
| c `elem` ("<>v^" :: String) = robotAttr
attrFor 'T' = plantAttr
attrFor '@' = rockAttr
attrFor '~' = waterAttr
attrFor '' = dirtAttr
attrFor _ = defAttr

waterAttr :: AttrName
waterAttr = getWorldAttrName $ fst water

rockAttr :: AttrName
rockAttr = getWorldAttrName $ fst rock

plantAttr :: AttrName
plantAttr = getWorldAttrName $ fst rock

dirtAttr :: AttrName
dirtAttr = getTerrainAttrName $ fst dirt
22 changes: 2 additions & 20 deletions src/Swarm/TUI/View/Util.hs
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,16 @@ import Control.Lens hiding (Const, from)
import Control.Monad.Reader (withReaderT)
import Data.List.NonEmpty (NonEmpty (..))
import Data.Map.Strict qualified as M
import Data.Maybe (catMaybes, fromMaybe, listToMaybe)
import Data.Maybe (catMaybes, fromMaybe)
import Data.Text (Text)
import Data.Text qualified as T
import Graphics.Vty qualified as V
import Swarm.Game.Entity as E
import Swarm.Game.Location
import Swarm.Game.Robot (trobotLocation)
import Swarm.Game.Scenario (Scenario, scenarioName)
import Swarm.Game.Scenario (scenarioName)
import Swarm.Game.ScenarioInfo (scenarioItemName)
import Swarm.Game.State
import Swarm.Game.Terrain
import Swarm.Game.Universe (Cosmic (..), SubworldName (DefaultRootSubworld))
import Swarm.Language.Pretty (prettyTextLine)
import Swarm.Language.Syntax (Syntax)
import Swarm.Language.Text.Markdown qualified as Markdown
Expand Down Expand Up @@ -234,19 +232,3 @@ drawLabelledEntityName e =
[ padRight (Pad 2) (renderDisplay (e ^. entityDisplay))
, txt (e ^. entityName)
]

determineViewCenter ::
Scenario ->
NonEmpty SubworldDescription ->
Cosmic Location
determineViewCenter s worldTuples =
fromMaybe defaultVC baseRobotLoc
where
theRobots = genRobotTemplates s worldTuples
defaultVC = Cosmic DefaultRootSubworld origin

-- The first robot is guaranteed to be the base.
baseRobotLoc :: Maybe (Cosmic Location)
baseRobotLoc = do
theBaseRobot <- listToMaybe theRobots
view trobotLocation theBaseRobot
16 changes: 9 additions & 7 deletions swarm.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,6 @@ library
Swarm.Game.ResourceLoading
Swarm.Game.Robot
Swarm.Game.Scenario
Swarm.Game.Scenario.Topography.Cell
Swarm.Game.Universe
Swarm.Effect
Swarm.Effect.Time
Expand All @@ -146,6 +145,8 @@ library
Swarm.Game.Scenario.Scoring.GenericMetrics
Swarm.Game.Scenario.Status
Swarm.Game.Scenario.Style
Swarm.Game.Scenario.Topography.Cell
Swarm.Game.Scenario.Topography.Center
Swarm.Game.Scenario.Topography.EntityFacade
Swarm.Game.Scenario.Topography.Navigation.Portal
Swarm.Game.Scenario.Topography.Navigation.Util
Expand Down Expand Up @@ -210,10 +211,17 @@ library
Swarm.Language.Types
Swarm.Language.Value
Swarm.ReadableIORef
Swarm.TUI.View
Swarm.TUI.View.Achievement
Swarm.TUI.View.Attribute.Attr
Swarm.TUI.View.Attribute.CustomStyling
Swarm.TUI.View.Attribute.Color
Swarm.TUI.View.Attribute.Util
Swarm.TUI.View.CellDisplay
Swarm.TUI.View.Logo
Swarm.TUI.View.Objective
Swarm.TUI.View.Structure
Swarm.TUI.View.Util
Swarm.TUI.Border
Swarm.Game.Scenario.Topography.Area
Swarm.TUI.Editor.Controller
Expand All @@ -236,12 +244,6 @@ library
Swarm.TUI.Model.Structure
Swarm.TUI.Model.UI
Swarm.TUI.Panel
Swarm.TUI.View
Swarm.TUI.View.Achievement
Swarm.TUI.View.CellDisplay
Swarm.TUI.View.Objective
Swarm.TUI.View.Structure
Swarm.TUI.View.Util
Swarm.Util
Swarm.Util.Effect
Swarm.Util.Erasable
Expand Down

0 comments on commit ab9fb5b

Please sign in to comment.