License | BSD-3-Clause |
---|---|
Safe Haskell | Safe-Inferred |
Language | Haskell2010 |
A world refers to the grid on which the game takes place, and the things in it (besides robots). A world has a base, immutable terrain layer, where each cell contains a terrain type, and a mutable entity layer, with at most one entity per cell.
A world is technically finite but practically infinite (worlds are indexed by 32-bit signed integers, so they correspond to a \( 2^{32} \times 2^{32} \) torus).
Synopsis
- newtype Coords = Coords {}
- locToCoords :: Location -> Coords
- coordsToLoc :: Coords -> Location
- type BoundsRectangle = (Coords, Coords)
- newtype WorldFun t e = WF {}
- runWF :: WorldFun t e -> Coords -> (t, Maybe e)
- worldFunFromArray :: Monoid t => Array (Int32, Int32) (t, Erasable e) -> WorldFun t e
- data World t e
- type MultiWorld t e = Map SubworldName (World t e)
- loadCell :: IArray UArray t => Coords -> World t e -> World t e
- loadRegion :: forall t e. IArray UArray t => (Coords, Coords) -> World t e -> World t e
- newWorld :: WorldFun t e -> World t e
- lookupCosmicTerrain :: IArray UArray Int => Cosmic Coords -> MultiWorld Int e -> TerrainType
- lookupTerrain :: IArray UArray t => Coords -> World t e -> t
- lookupCosmicEntity :: Cosmic Coords -> MultiWorld t e -> Maybe e
- lookupEntity :: Coords -> World t e -> Maybe e
- update :: Coords -> (Maybe Entity -> Maybe Entity) -> World t Entity -> (World t Entity, CellUpdate Entity)
- lookupTerrainM :: forall t e sig m. (Has (State (World t e)) sig m, IArray UArray t) => Coords -> m t
- lookupEntityM :: forall t e sig m. (Has (State (World t e)) sig m, IArray UArray t) => Coords -> m (Maybe e)
- updateM :: forall t sig m. (Has (State (World t Entity)) sig m, IArray UArray t) => Coords -> (Maybe Entity -> Maybe Entity) -> m (CellUpdate Entity)
- data WorldUpdate e = ReplaceEntity {
- updatedLoc :: Cosmic Location
- originalEntity :: e
- newEntity :: Maybe e
World coordinates
World coordinates use (row,column)
format, with the row
increasing as we move down the screen. We use this format for
indexing worlds internally, since it plays nicely with things
like drawing the screen, and reading maps from configuration
files. The locToCoords
and coordsToLoc
functions convert back
and forth between this type and Location
, which is used when
presenting coordinates externally to the player.
Instances
Generic Coords Source # | |
Ix Coords Source # | |
Show Coords Source # | |
Eq Coords Source # | |
Ord Coords Source # | |
Wrapped Coords Source # | |
Rewrapped Coords t Source # | |
Defined in Swarm.Game.World.Coords | |
type Rep Coords Source # | |
Defined in Swarm.Game.World.Coords | |
type Unwrapped Coords Source # | |
Defined in Swarm.Game.World.Coords |
locToCoords :: Location -> Coords Source #
Convert an external (x,y)
location to an internal Coords
value.
coordsToLoc :: Coords -> Location Source #
Convert an internal Coords
value to an external (x,y)
location.
type BoundsRectangle = (Coords, Coords) Source #
Represents the top-left and bottom-right coordinates of a bounding rectangle of cells in the world map
Worlds
A WorldFun t e
represents a 2D world with terrain of type t
(exactly one per cell) and entities of type e
(at most one per
cell).
worldFunFromArray :: Monoid t => Array (Int32, Int32) (t, Erasable e) -> WorldFun t e Source #
Create a world function from a finite array of specified cells.
A World
consists of a WorldFun
that specifies the initial
world, a cache of loaded square tiles to make lookups faster, and
a map storing locations whose entities have changed from their
initial values.
Right now the World
simply holds on to all the tiles it has
ever loaded. Ideally it would use some kind of LRU caching
scheme to keep memory usage bounded, but it would be a bit
tricky, and in any case it's probably not going to matter much
for a while. Once tile loads can trigger robots to spawn, it
would also make for some difficult decisions in terms of how to
handle respawning.
type MultiWorld t e = Map SubworldName (World t e) Source #
Tile management
loadCell :: IArray UArray t => Coords -> World t e -> World t e Source #
Load the tile containing a specific cell.
loadRegion :: forall t e. IArray UArray t => (Coords, Coords) -> World t e -> World t e Source #
Load all the tiles which overlap the given rectangular region (specified as an upper-left and lower-right corner, inclusive).
World functions
lookupCosmicTerrain :: IArray UArray Int => Cosmic Coords -> MultiWorld Int e -> TerrainType Source #
lookupTerrain :: IArray UArray t => Coords -> World t e -> t Source #
Look up the terrain value at certain coordinates: try looking it
up in the tile cache first, and fall back to running the WorldFun
otherwise.
This function does not ensure that the tile containing the
given coordinates is loaded. For that, see lookupTerrainM
.
lookupCosmicEntity :: Cosmic Coords -> MultiWorld t e -> Maybe e Source #
lookupEntity :: Coords -> World t e -> Maybe e Source #
Look up the entity at certain coordinates: first, see if it is in
the map of locations with changed entities; then try looking it
up in the tile cache first; and finally fall back to running the
WorldFun
.
This function does not ensure that the tile containing the
given coordinates is loaded. For that, see lookupEntityM
.
update :: Coords -> (Maybe Entity -> Maybe Entity) -> World t Entity -> (World t Entity, CellUpdate Entity) Source #
Monadic variants
lookupTerrainM :: forall t e sig m. (Has (State (World t e)) sig m, IArray UArray t) => Coords -> m t Source #
A stateful variant of lookupTerrain
, which first loads the tile
containing the given coordinates if it is not already loaded,
then looks up the terrain value.
lookupEntityM :: forall t e sig m. (Has (State (World t e)) sig m, IArray UArray t) => Coords -> m (Maybe e) Source #
A stateful variant of lookupEntity
, which first loads the tile
containing the given coordinates if it is not already loaded,
then looks up the terrain value.
updateM :: forall t sig m. (Has (State (World t Entity)) sig m, IArray UArray t) => Coords -> (Maybe Entity -> Maybe Entity) -> m (CellUpdate Entity) Source #
A stateful variant of update
, which also ensures the tile
containing the given coordinates is loaded.
Runtime updates
data WorldUpdate e Source #
Enumeration of world updates. This type is used for changes by
e.g. the drill
command which must be carried out at a later
tick. Using a first-order representation (as opposed to e.g.
just a World -> World
function) allows us to serialize and
inspect the updates.
ReplaceEntity | |
|