Skip to content

Commit

Permalink
KeyMap -> Map
Browse files Browse the repository at this point in the history
  • Loading branch information
kostmo committed Jul 23, 2024
1 parent 32dca48 commit 36a61fc
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ module Swarm.Game.Scenario.Topography.WorldPalette where

import Control.Arrow (first)
import Control.Lens hiding (from, (.=), (<.>))
import Data.Aeson.Key qualified as K
import Data.Aeson.KeyMap qualified as KM
import Data.Map qualified as M
import Data.Maybe (catMaybes)
Expand Down Expand Up @@ -105,9 +106,8 @@ prepForJson (PaletteAndMaskChar (StructurePalette suggestedPalette) maybeMaskCha
where
preassignments :: [(Char, TerrainWith EntityFacade)]
preassignments =
map (first T.head . fmap (cellToTerrainPair . standardCell)) $
M.toList $
KM.toMapText suggestedPalette
map (first (T.head . K.toText) . fmap (cellToTerrainPair . standardCell)) $
M.toList suggestedPalette

entityCells :: M.Map (TerrainWith EntityName) (TerrainWith EntityFacade)
entityCells = getUniqueTerrainFacadePairs $ catMaybes $ allMembers cellGrid
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,21 @@ module Swarm.Game.Scenario.Topography.ProtoCell (
) where

import Control.Applicative ((<|>))
import Data.Aeson.KeyMap (KeyMap)
import Data.Aeson.Key qualified as K
import Data.Aeson.KeyMap qualified as KM
import Data.Map (Map)
import Data.Yaml as Y
import Swarm.Game.Scenario.Topography.Navigation.Waypoint (WaypointConfig)
import Swarm.Util.Yaml

newtype StructurePalette e = StructurePalette
{unPalette :: KeyMap (SignpostableCell e)}
{unPalette :: Map K.Key (SignpostableCell e)}
deriving (Eq, Show)

instance (FromJSONE e a) => FromJSONE e (StructurePalette a) where
parseJSONE =
withObjectE "palette" $
fmap StructurePalette . mapM parseJSONE
fmap (StructurePalette . KM.toMap) . mapM parseJSONE

-- | Supplements a cell with waypoint information
data SignpostableCell c = SignpostableCell
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,8 @@ paintMap maskChar pal g = do
let usedChars = Set.fromList $ map T.singleton $ allMembers g
unusedChars =
filter (`Set.notMember` usedChars)
. map Key.toText
. M.keys
. KeyMap.toMapText
$ unPalette pal

unless (null unusedChars) $
Expand All @@ -158,6 +158,6 @@ paintMap maskChar pal g = do
toCell c =
if Just c == maskChar
then return Nothing
else case KeyMap.lookup (Key.fromString [c]) (unPalette pal) of
else case M.lookup (Key.fromString [c]) (unPalette pal) of
Nothing -> failT ["Char not in world palette:", showT c]
Just cell -> return $ Just cell
18 changes: 10 additions & 8 deletions src/swarm-tui/Swarm/TUI/Editor/Palette.hs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ module Swarm.TUI.Editor.Palette where

import Control.Lens
import Control.Monad (guard)
import Data.Aeson.Key qualified as K
import Data.Aeson.KeyMap qualified as KM
import Data.Bifunctor (first)
import Data.List (sortOn)
import Data.List.NonEmpty qualified as NE
import Data.Map (Map)
Expand Down Expand Up @@ -41,13 +43,13 @@ import Swarm.Util.Erasable

makeSuggestedPalette ::
TerrainMap ->
KM.KeyMap (AugmentedCell Entity) ->
Map KM.Key (AugmentedCell Entity) ->
Grid (Maybe CellPaintDisplay) ->
KM.KeyMap (AugmentedCell EntityFacade)
Map KM.Key (AugmentedCell EntityFacade)
makeSuggestedPalette tm originalScenarioPalette cellGrid =
KM.fromMapText
. M.map (SignpostableCell Nothing)
M.map (SignpostableCell Nothing)
. M.fromList
. map (first K.fromText)
. M.elems
-- NOTE: the left-most maps take precedence!
$ paletteCellsByKey <> pairsWithDisplays <> terrainOnlyPalette
Expand Down Expand Up @@ -89,19 +91,19 @@ makeSuggestedPalette tm originalScenarioPalette cellGrid =

paletteCellsByKey :: Map (TerrainWith EntityName) (T.Text, CellPaintDisplay)
paletteCellsByKey =
M.map (NE.head . NE.sortWith toSortVal)
M.map (first K.toText . NE.head . NE.sortWith toSortVal)
. binTuples
. invertPaletteMapToDedupe
$ KM.toMapText originalPalette
$ originalPalette
where
toSortVal (symbol, Cell _terrain _maybeEntity robots) = Down (null robots, symbol)

excludedPaletteChars :: Set Char
excludedPaletteChars = Set.fromList [' ']

originalPalette :: KM.KeyMap CellPaintDisplay
originalPalette :: Map KM.Key CellPaintDisplay
originalPalette =
KM.map (toCellPaintDisplay . standardCell) originalScenarioPalette
M.map (toCellPaintDisplay . standardCell) originalScenarioPalette

pairsWithDisplays :: Map (TerrainWith EntityName) (T.Text, CellPaintDisplay)
pairsWithDisplays = M.fromList $ mapMaybe g entitiesWithModalTerrain
Expand Down

0 comments on commit 36a61fc

Please sign in to comment.