-
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
12 changed files
with
288 additions
and
30 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
{-# 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.IntMap qualified as IM | ||
import Data.Text qualified as T | ||
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, robotInfo) | ||
import Swarm.Game.State.Robot (viewCenter) | ||
import Swarm.Game.Universe (planar) | ||
import Swarm.Game.World.Render | ||
import Swarm.TUI.View.CellDisplay (getTerrainEntityColor) | ||
import Swarm.Util.OccurrenceEncoder | ||
|
||
data GridResponse = GridResponse | ||
{ isPlaying :: Bool | ||
, grid :: Maybe CellGrid | ||
} | ||
deriving (Generic, ToJSON) | ||
|
||
getCellGrid :: | ||
Scenario -> | ||
GameState -> | ||
AreaDimensions -> | ||
CellGrid | ||
getCellGrid myScenario gs requestedSize = | ||
CellGrid indexGrid $ getIndices encoding | ||
where | ||
vc = gs ^. robotInfo . viewCenter | ||
dg = getDisplayGrid (vc ^. planar) 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, encoding) = runState (mapM (encodeOccurrence . f) dg) emptyEncoder | ||
|
||
data CellGrid = CellGrid | ||
{ coords :: Grid IM.Key | ||
, colors :: [HexColor] | ||
} | ||
deriving (Generic, ToJSON) | ||
|
||
instance SD.ToSample GridResponse where | ||
toSamples _ = SD.noSamples |
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,41 @@ | ||
-- | | ||
-- SPDX-License-Identifier: BSD-3-Clause | ||
module Swarm.Util.OccurrenceEncoder ( | ||
Encoder, | ||
encodeOccurrence, | ||
getIndices, | ||
emptyEncoder, | ||
) where | ||
|
||
import Control.Monad.Trans.State | ||
import Data.List (sortOn) | ||
import Data.Map (Map) | ||
import Data.Map qualified as M | ||
|
||
type OccurrenceEncoder a = State (Encoder a) | ||
|
||
newtype Encoder a = Encoder (Map a Int) | ||
|
||
emptyEncoder :: Ord a => Encoder a | ||
emptyEncoder = Encoder mempty | ||
|
||
-- | Map indices are guaranteed to be contiguous | ||
-- from @[0..N]@, so we may convert to a list | ||
-- with no loss of information. | ||
getIndices :: Encoder a -> [a] | ||
getIndices (Encoder m) = map fst $ sortOn snd $ M.toList m | ||
|
||
-- | Translate each the first occurrence in the structure | ||
-- 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 | ||
Encoder currentMap <- get | ||
maybe (cacheNewIndex currentMap) return $ | ||
M.lookup c currentMap | ||
where | ||
cacheNewIndex currentMap = do | ||
put $ Encoder $ 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
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
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,32 @@ | ||
<!doctype html> | ||
<html> | ||
<head> | ||
<title>Web frontend</title> | ||
<script src="https://pixijs.download/release/pixi.js"></script> | ||
|
||
<script src="script/display.js"></script> | ||
<script src="script/fetch.js"></script> | ||
|
||
<script> | ||
|
||
function startLoop() { | ||
const button = document.getElementById('restart-button'); | ||
button.style.display = 'none'; | ||
|
||
let displayWidth = 600; | ||
let displayHeight = 300; | ||
let out = setupGraphics(button, displayWidth, displayHeight); | ||
let appView = out[0]; | ||
let graphics = out[1]; | ||
doFetch(button, appView, graphics, displayWidth, displayHeight); | ||
} | ||
|
||
window.onload=()=>{ | ||
startLoop(); | ||
} | ||
</script> | ||
</head> | ||
<body> | ||
<button id="restart-button" onclick="startLoop();" style="display: block;">Restart</button> | ||
</body> | ||
</html> |
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,15 @@ | ||
function setupGraphics(button, displayWidth, displayHeight) { | ||
|
||
// Create the application helper and add its render target to the page | ||
let app = new PIXI.Application({ | ||
width: displayWidth, | ||
height: displayHeight, | ||
backgroundColor: 0xFFFFFF | ||
}); | ||
|
||
document.body.insertBefore(app.view, button); | ||
|
||
const graphics = new PIXI.Graphics(); | ||
app.stage.addChild(graphics); | ||
return [app.view, graphics]; | ||
} |
Oops, something went wrong.