License | BSD-3-Clause |
---|---|
Safe Haskell | None |
Language | Haskell2010 |
Swarm.Game.Scenario.Topography.Structure.Recognition.Precompute
Description
Precomputation for structure recognizer.
Search process overview
2D structures may be defined at the
toplevel of a scenario file.
Upon scenario load, all of the predefined structures that are marked
as "recognize"
are compiled into searcher state machines.
When an entity is placed on any cell in the world, the
entityModified
function is called, which looks up a customized searcher based
on the type of placed entity.
The first searching stage looks for any member row of all participating structure definitions that contains the placed entity. If we observe a row in the world that happens to occur in a structure, we use both the horizontal found offset and the index of the row within this structure to compute the expected world location of the candidate structure. Then we perform a full scan of that candidate structure against the world to verify the match.
Upon locating a complete structure, it is added to a registry
(see FoundRegistry
), which
supports lookups by either name or by location (using two different
maps maintained in parallel). The map by location is used to remove
a structure from the registry if a member entity is changed.
Synopsis
- mkAutomatons :: (Ord a, Hashable a) => (b -> NonEmptyGrid (AtomicKeySymbol a)) -> [NamedArea b] -> Either RedundantOrientations (RecognizerAutomatons b a)
- type GenericEntLocator (s :: Type -> Type) a = Cosmic Location -> s (AtomicKeySymbol a)
- populateStaticFoundStructures :: (Eq a, Eq b) => [FoundStructure b a] -> FoundRegistry b a
- lookupStaticPlacements :: StaticStructureInfo b a -> [FoundStructure b a]
- ensureStructureIntact :: (Monad s, Hashable a) => FoundRegistry b a -> GenericEntLocator s a -> FoundStructure b a -> s (Maybe (StructureIntactnessFailure a))
Main external interface
mkAutomatons :: (Ord a, Hashable a) => (b -> NonEmptyGrid (AtomicKeySymbol a)) -> [NamedArea b] -> Either RedundantOrientations (RecognizerAutomatons b a) Source #
Create Aho-Corasick matchers that will recognize all of the provided structure definitions
Types
type GenericEntLocator (s :: Type -> Type) a = Cosmic Location -> s (AtomicKeySymbol a) Source #
Interface that provides monadic access to
querying entities at locations.
The provider may be a State
monad or just
a Reader
.
s
is the state variable, a
is the return type.
Helper functions
populateStaticFoundStructures :: (Eq a, Eq b) => [FoundStructure b a] -> FoundRegistry b a Source #
Bulk insertion of structures statically placed in the scenario definition.
See the docs for initializeRecognition
for more context.
Note that if any of these pre-placed structures overlap, we can't be sure of the author's intent as to which member of the overlap should take precedence, so perhaps it would be ideal to throw an error at scenario parse time.
However, determining whether a structure is all three of:
1. placed
2. still recognizable
3. overlapping with another recognized structure
occurs at a later phase than scenario parse; it requires access to the GameState
.
So we just use the same sorting criteria as the one used to resolve recognition conflicts at entity placement time (see [STRUCTURE RECOGNIZER CONFLICT RESOLUTION]).
lookupStaticPlacements :: StaticStructureInfo b a -> [FoundStructure b a] Source #
The output list of FoundStructure
records is not yet
vetted; the ensureStructureIntact
function will subsequently
filter this list.
ensureStructureIntact :: (Monad s, Hashable a) => FoundRegistry b a -> GenericEntLocator s a -> FoundStructure b a -> s (Maybe (StructureIntactnessFailure a)) Source #
Matches definitions against the placements. Fails fast (short-circuits) if a non-matching cell is encountered.
Returns Nothing
if there is no discrepancy between the match subject and world content.
Returns the first observed mismatch cell otherwise.