Skip to content

Commit

Permalink
WIP compiles
Browse files Browse the repository at this point in the history
  • Loading branch information
kostmo committed Nov 3, 2024
1 parent b565a2a commit bbc8df7
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 26 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
version: 1
name: Structure recognition - piecewise lines
description: |
General solution for transparency
creative: false
objectives:
- teaser: Recognize structure
goal:
- |
`spaceship`{=structure} structure should be recognized upon completion,
even with an extraneous entity within its bounds.
condition: |
def isRight = \x. case x (\_. false) (\_. true); end;
foundStructure <- structure "spaceship" 0;
return $ isRight foundStructure;
robots:
- name: base
dir: east
devices:
- ADT calculator
- blueprint
- fast grabber
- logger
- treads
inventory:
- [1, rock]
solution: |
move; move; move;
swap "rock";
structures:
- name: spaceship
recognize: [north]
structure:
palette:
'p': [stone, board]
'x': [stone, rock]
mask: '.'
map: |
xxx
ppp
xxx
- name: damage
description: A single-cell overwrite of the spaceship
structure:
palette:
't': [stone, tree]
map: |
t
- name: modified ship
description: A spaceship with a single cell replaced by a `tree`{=entity}
structure:
placements:
- src: spaceship
- src: damage
map: ""
known: [board, mountain, rock, tree]
world:
dsl: |
{blank}
palette:
'.': [grass, erase]
'B': [grass, erase, base]
'p':
structure:
name: modified ship
cell: [grass]
upperleft: [0, 0]
map: |
.........
B..p.....
.........
.........
.........
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import Data.Maybe (catMaybes, mapMaybe)
import Data.Semigroup (sconcat)
import Data.Tuple (swap)
import Swarm.Game.Scenario.Topography.Structure.Recognition.Type
import Text.AhoCorasick (makeStateMachine, makeSimpleStateMachine)
import Text.AhoCorasick (makeStateMachine)
import Data.List.Split (wordsBy)

-- | Given all candidate structures, explode them into annotated rows.
Expand Down Expand Up @@ -82,7 +82,7 @@ mkEntityLookup grids =
-- is encountered.
mkRowAutomatons neList =
AutomatonNewInfo bounds sm searchPatternsAndSubAutomatons $
PiecewiseRecognition smPiecewise 3
PiecewiseRecognition smPiecewise extractedChunksForLookup
where
searchPatternsAndSubAutomatons = NE.map (\(a, b) -> (a, mkSmValue a b)) groupedByUniqueRow
where
Expand All @@ -102,21 +102,18 @@ mkEntityLookup grids =
bounds = sconcat $ NE.map expandedOffsets neList
sm = makeStateMachine $ NE.toList searchPatternsAndSubAutomatons

-- groupedByUniqueChunk =
-- binTuplesHMasListNE $
-- NE.map (rowContent . myRow &&& id) neList

extractedChunksForStateMachine = HS.fromList $ NE.toList $
extractedChunksForStateMachine = HS.fromList . concat . NE.toList $
NE.map (map chunkContents . contiguousChunks) neList

extractedChunksForLookup = NE.map
extractedChunksForLookup = HM.fromList $ NE.toList $ NE.map
(HS.fromList . map chunkContents . contiguousChunks &&& mkRightMap)
neList
where
mkRightMap sreo = binTuplesHM $ map (chunkContents &&& chunkStartPos) $ contiguousChunks sreo

smPiecewise = makeStateMachine $ map (NE.toList . fmap Just &&& id) $
HS.toList extractedChunksForStateMachine

smPiecewise = makeSimpleStateMachine $ HS.toList extractedChunksForStateMachine

-- The values of this map are guaranteed to contain only one
-- entry per row of each structure, even if some of those
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,13 +168,28 @@ registerRowMatches ::
AutomatonNewInfo a (StructureSearcher b a) ->
RecognitionState b a ->
s (RecognitionState b a)
registerRowMatches entLoader cLoc (AutomatonNewInfo horizontalOffsets sm _ _) rState = do
maskChoices <- attemptSearchWithEntityMask
registerRowMatches entLoader cLoc (AutomatonNewInfo horizontalOffsets sm _ pwMatcher) rState = do

entitiesRow <-
getWorldRow
entLoader
registry
cLoc
horizontalOffsets
0

-- All of the eligible structure rows found
-- within this horizontal swath of world cells
let maskChoices = (entitiesRow, findAll sm entitiesRow)

let logEntry = uncurry logRowCandidates maskChoices
rState2 = rState & recognitionLog %~ (logEntry :)
candidates = snd maskChoices


let PiecewiseRecognition pwSM pwMap = pwMatcher
let candidatesChunked = findAll pwSM entitiesRow

candidates2Dpairs <-
forM candidates $
checkVerticalMatch entLoader registry cLoc horizontalOffsets
Expand All @@ -187,19 +202,6 @@ registerRowMatches entLoader cLoc (AutomatonNewInfo horizontalOffsets sm _ _) rS
where
registry = rState ^. foundStructures

attemptSearchWithEntityMask = do
entitiesRow <-
getWorldRow
entLoader
registry
cLoc
horizontalOffsets
0

-- All of the eligible structure rows found
-- within this horizontal swath of world cells
return (entitiesRow, findAll sm entitiesRow)

-- | Examines contiguous rows of entities, accounting
-- for the offset of the initially found row.
checkVerticalMatch ::
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import Swarm.Game.Scenario.Topography.Area
import Swarm.Game.Scenario.Topography.Structure.Named (NamedGrid)
import Swarm.Game.Scenario.Topography.Structure.Recognition.Static
import Swarm.Game.Universe (Cosmic, offsetBy)
import Data.HashSet (HashSet)
import Swarm.Language.Syntax.Direction (AbsoluteDir)
import Text.AhoCorasick (StateMachine)

Expand Down Expand Up @@ -95,8 +96,8 @@ data PositionWithinRow b a = PositionWithinRow


data PiecewiseRecognition a = PiecewiseRecognition {
piecewiseSM :: StateMachine (NonEmpty a) [NonEmpty a]
, picewiseLookup :: Int
piecewiseSM :: StateMachine (AtomicKeySymbol a) (NonEmpty a)
, picewiseLookup :: HashMap (HashSet (NonEmpty a)) (HashMap (NonEmpty a) (NonEmpty Int))
}

data PositionedChunk a = PositionedChunk {
Expand Down

0 comments on commit bbc8df7

Please sign in to comment.