Skip to content

Commit

Permalink
Implement 'backup' command
Browse files Browse the repository at this point in the history
  • Loading branch information
kostmo committed Aug 1, 2023
1 parent 2d67a22 commit f6db394
Show file tree
Hide file tree
Showing 10 changed files with 74 additions and 11 deletions.
10 changes: 10 additions & 0 deletions data/entities.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -771,6 +771,16 @@
capabilities: [move, turn, moveheavy]
properties: [portable]


- name: tape drive
display:
attr: device
char: '%'
description:
- A "tape drive" allows you to `backup`; that is, to 'drive' in reverse.
capabilities: [backup]
properties: [portable]

- name: dozer blade
display:
attr: silver
Expand Down
1 change: 1 addition & 0 deletions data/scenarios/Testing/00-ORDER.txt
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,4 @@
1356-portals
144-subworlds
1379-single-world-portal-reorientation.yaml
1399-backup-command.yaml
32 changes: 32 additions & 0 deletions data/scenarios/Testing/1399-backup-command.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
version: 1
name: Portal reorientation within a single subworld
description: |
Turning without turning
objectives:
- goal:
- |
`grab` the "flower".
condition: |
as base {has "flower"}
solution: |
backup; backup; grab;
robots:
- name: base
dir: [0, 1]
devices:
- tape drive
- grabber
known: [flower]
world:
default: [blank]
palette:
'.': [grass]
'f': [grass, flower]
'B': [grass, null, base]
upperleft: [-1, 1]

map: |
.B.
...
.f.
1 change: 1 addition & 0 deletions editors/emacs/swarm-mode.el
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
"wait"
"selfdestruct"
"move"
"backup"
"push"
"stride"
"turn"
Expand Down
2 changes: 1 addition & 1 deletion editors/vscode/syntaxes/swarm.tmLanguage.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
},
{
"name": "keyword.other",
"match": "\\b(?i)(self|parent|base|if|inl|inr|case|fst|snd|force|undefined|fail|not|format|chars|split|charat|tochar|key|noop|wait|selfdestruct|move|push|stride|turn|grab|harvest|place|give|equip|unequip|make|has|equipped|count|drill|use|build|salvage|reprogram|say|listen|log|view|appear|create|halt|time|scout|whereami|waypoint|detect|resonate|density|sniff|chirp|watch|surveil|heading|blocked|scan|upload|ishere|isempty|meet|meetall|whoami|setname|random|run|return|try|swap|atomic|instant|installkeyhandler|teleport|as|robotnamed|robotnumbered|knows)\\b"
"match": "\\b(?i)(self|parent|base|if|inl|inr|case|fst|snd|force|undefined|fail|not|format|chars|split|charat|tochar|key|noop|wait|selfdestruct|move|backup|push|stride|turn|grab|harvest|place|give|equip|unequip|make|has|equipped|count|drill|use|build|salvage|reprogram|say|listen|log|view|appear|create|halt|time|scout|whereami|waypoint|detect|resonate|density|sniff|chirp|watch|surveil|heading|blocked|scan|upload|ishere|isempty|meet|meetall|whoami|setname|random|run|return|try|swap|atomic|instant|installkeyhandler|teleport|as|robotnamed|robotnumbered|knows)\\b"
}
]
},
Expand Down
31 changes: 21 additions & 10 deletions src/Swarm/Game/Step.hs
Original file line number Diff line number Diff line change
Expand Up @@ -545,6 +545,10 @@ traceLogShow = void . traceLog Logged . from . show
constCapsFor :: Const -> Robot -> Maybe Capability
constCapsFor Move r
| r ^. robotHeavy = Just CMoveheavy
constCapsFor Backup r
| r ^. robotHeavy = Just CMoveheavy
constCapsFor Stride r
| r ^. robotHeavy = Just CMoveheavy
constCapsFor c _ = constCaps c

-- | Ensure that a robot is capable of executing a certain constant
Expand Down Expand Up @@ -1128,17 +1132,11 @@ execConst c vs s k = do
flagRedraw
return $ Out VUnit s k
Move -> do
-- Figure out where we're going
loc <- use robotLocation
orient <- use robotOrientation
let nextLoc = loc `offsetBy` (orient ? zero)
checkMoveAhead nextLoc $
MoveFailure
{ failIfBlocked = ThrowExn
, failIfDrown = Destroy
}
updateRobotLocation loc nextLoc
return $ Out VUnit s k
moveInDirection $ orient ? zero
Backup -> do
orient <- use robotOrientation
moveInDirection $ applyTurn (DRelative $ DPlanar DBack) $ orient ? zero
Push -> do
-- Figure out where we're going
loc <- use robotLocation
Expand Down Expand Up @@ -2467,6 +2465,19 @@ execConst c vs s k = do
["You consider destroying your base, but decide not to do it after all."]
mAch
selfDestruct .= True

moveInDirection :: (HasRobotStepState sig m, Has (Lift IO) sig m) => Heading -> m CESK
moveInDirection orientation = do
-- Figure out where we're going
loc <- use robotLocation
let nextLoc = loc `offsetBy` orientation
checkMoveAhead nextLoc $
MoveFailure
{ failIfBlocked = ThrowExn
, failIfDrown = Destroy
}
updateRobotLocation loc nextLoc
return $ Out VUnit s k

-- Make sure nothing is in the way. Note that system robots implicitly ignore
-- and base throws on failure.
Expand Down
3 changes: 3 additions & 0 deletions src/Swarm/Language/Capability.hs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ data Capability
CPower
| -- | Execute the 'Move' command
CMove
| -- | Execute the 'Backup' command
CBackup
| -- | Execute the 'Push' command
CPush
| -- | Execute the 'Stride' command
Expand Down Expand Up @@ -207,6 +209,7 @@ constCaps = \case
Log -> Just CLog
Selfdestruct -> Just CSelfdestruct
Move -> Just CMove
Backup -> Just CBackup
Push -> Just CPush
Stride -> Just CMovemultiple
Turn -> Just CTurn
Expand Down
3 changes: 3 additions & 0 deletions src/Swarm/Language/Syntax.hs
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,8 @@ data Const

-- | Move forward one step.
Move
| -- | Move backward one step.
Backup
| -- | Push an entity forward one step.
Push
| -- | Move forward multiple steps.
Expand Down Expand Up @@ -520,6 +522,7 @@ constInfo c = case c of
, "This destroys the robot's inventory, so consider `salvage` as an alternative."
]
Move -> command 0 short "Move forward one step."
Backup -> command 0 short "Move backward one step."
Push ->
command 1 short . doc "Push an entity forward one step." $
[ "Both entity and robot moves forward one step."
Expand Down
1 change: 1 addition & 0 deletions src/Swarm/Language/Typecheck.hs
Original file line number Diff line number Diff line change
Expand Up @@ -713,6 +713,7 @@ inferConst c = case c of
Noop -> [tyQ| cmd unit |]
Selfdestruct -> [tyQ| cmd unit |]
Move -> [tyQ| cmd unit |]
Backup -> [tyQ| cmd unit |]
Push -> [tyQ| cmd unit |]
Stride -> [tyQ| int -> cmd unit |]
Turn -> [tyQ| dir -> cmd unit |]
Expand Down
1 change: 1 addition & 0 deletions test/integration/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,7 @@ testScenarioSolution rs ui _ci _em =
, testSolution Default "Testing/144-subworlds/subworld-mapped-robots"
, testSolution Default "Testing/144-subworlds/subworld-located-robots"
, testSolution Default "Testing/1379-single-world-portal-reorientation"
, testSolution Default "Testing/1399-backup-command"
]
]
where
Expand Down

0 comments on commit f6db394

Please sign in to comment.