-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
19 changed files
with
257 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
{-# LANGUAGE OverloadedStrings #-} | ||
|
||
-- | | ||
-- SPDX-License-Identifier: BSD-3-Clause | ||
module Swarm.Web.Worldview where | ||
|
||
import Control.Lens ((^.)) | ||
import Control.Monad.Trans.State | ||
import Data.Aeson (ToJSON) | ||
import Data.Colour.Palette.BrewerSet (Kolor) | ||
import Data.Colour.SRGB (RGB (..), sRGB24, sRGB24show) | ||
import Data.Map (Map) | ||
import Data.Map qualified as M | ||
import Data.Text qualified as T | ||
import Data.Tuple (swap) | ||
import GHC.Generics (Generic) | ||
import Servant.Docs qualified as SD | ||
import Swarm.Game.Entity.Cosmetic (RGBColor, flattenBg) | ||
import Swarm.Game.Scenario (Scenario, scenarioCosmetics) | ||
import Swarm.Game.Scenario.Style | ||
import Swarm.Game.Scenario.Topography.Area (AreaDimensions (..), Grid) | ||
import Swarm.Game.State (GameState) | ||
import Swarm.Game.World.Render | ||
import Swarm.TUI.View.CellDisplay (getTerrainEntityColor) | ||
|
||
data GridResponse = GridResponse | ||
{ isPlaying :: Bool | ||
, grid :: Maybe CellGrid | ||
} | ||
deriving (Generic, ToJSON) | ||
|
||
getCellGrid :: | ||
Scenario -> | ||
GameState -> | ||
AreaDimensions -> | ||
CellGrid | ||
getCellGrid myScenario gs requestedSize = | ||
CellGrid indexGrid invertedMap | ||
where | ||
dg = getDisplayGrid myScenario gs (Just requestedSize) | ||
aMap = myScenario ^. scenarioCosmetics | ||
|
||
asColour :: RGBColor -> Kolor | ||
asColour (RGB r g b) = sRGB24 r g b | ||
asHex = HexColor . T.pack . sRGB24show . asColour | ||
|
||
f = asHex . maybe (RGB 0 0 0) (flattenBg . fromHiFi) . getTerrainEntityColor aMap | ||
(indexGrid, indexMap) = runState (mapM (encodeOccurrence . f) dg) mempty | ||
invertedMap = M.fromList . map swap . M.toList $ indexMap | ||
|
||
data CellGrid = CellGrid | ||
{ coords :: Grid Int | ||
, colors :: Map Int HexColor | ||
} | ||
deriving (Generic, ToJSON) | ||
|
||
instance SD.ToSample GridResponse where | ||
toSamples _ = SD.noSamples | ||
|
||
-- ** Utility | ||
|
||
type OccurrenceEncoder a = State (Map a Int) | ||
|
||
-- | Translate each the first occurrence in the | ||
-- world map to a new integer as it is encountered. | ||
-- Subsequent encounters re-use the allocated integer. | ||
encodeOccurrence :: Ord a => a -> OccurrenceEncoder a Int | ||
encodeOccurrence c = do | ||
currentMap <- get | ||
case M.lookup c currentMap of | ||
Just entry -> return entry | ||
Nothing -> do | ||
put $ M.insert c newIdx currentMap | ||
return newIdx | ||
where | ||
newIdx = M.size currentMap |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.