Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

'use' command #1287

Merged
merged 1 commit into from
May 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions data/scenarios/Testing/00-ORDER.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
961-custom-capabilities.yaml
956-GPS.yaml
958-isempty.yaml
1007-use-command.yaml
1024-sand.yaml
1034-custom-attributes.yaml
1140-detect-command.yaml
Expand Down
83 changes: 83 additions & 0 deletions data/scenarios/Testing/1007-use-command.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
version: 1
name: The Use command
creative: false
description: Open a gate
objectives:
- goal:
- Grab the flower on the other side of the gate.
condition: |
as base {has "flower"}
solution: |
move;
k <- grab;
equip k;
use k forward;
move;
move;
grab;
robots:
- name: base
dir: [1,0]
display:
char: Ω
attr: robot
devices:
- ADT calculator
- grabber
- logger
- treads
- welder
entities:
- name: fence
display:
attr: wood
char: '#'
description:
- Impassable barrier
properties: [known, unwalkable]
- name: gate key
display:
attr: iron
char: 'k'
description:
- Can open a closed gate
capabilities: [drill]
kostmo marked this conversation as resolved.
Show resolved Hide resolved
properties: [known, portable]
- name: closed gate
display:
attr: wood
char: '|'
description:
- Cannot pass through this
properties: [known, unwalkable]
- name: open gate
display:
attr: wood
char: '/'
description:
- Can pass through this
properties: [known]
recipes:
- in:
- [1, closed gate]
out:
- [1, open gate]
required:
- [1, gate key]
known: [flower]
world:
default: [blank]
palette:
'Ω': [grass, null, base]
'.': [grass]
'#': [grass, fence]
'|': [grass, closed gate]
'*': [grass, flower]
'k': [grass, gate key]
upperleft: [0, 0]
map: |
......
..###.
Ωk|*#.
..###.
......
1 change: 1 addition & 0 deletions editors/emacs/swarm-mode.el
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
"equipped"
"count"
"drill"
"use"
"build"
"salvage"
"reprogram"
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|build|salvage|reprogram|say|listen|log|view|appear|create|halt|time|scout|whereami|detect|resonate|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|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|detect|resonate|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
24 changes: 17 additions & 7 deletions src/Swarm/Game/Step.hs
Original file line number Diff line number Diff line change
Expand Up @@ -1464,6 +1464,13 @@ execConst c vs s k = do
Drill -> case vs of
[VDir d] -> doDrill d
_ -> badConst
Use -> case vs of
[VText deviceName, VDir d] -> do
ins <- use equippedDevices
equippedEntity <- ensureEquipped deviceName
let verbPhrase = T.unwords ["use", deviceName, "on"]
applyDevice ins verbPhrase d equippedEntity
_ -> badConst
Blocked -> do
loc <- use robotLocation
orient <- use robotOrientation
Expand Down Expand Up @@ -2048,30 +2055,33 @@ execConst c vs s k = do
in throwError . Fatal $ msg <> badConstMsg
where
doDrill d = do
inv <- use robotInventory
ins <- use equippedDevices

let equippedDrills = extantElemsWithCapability CDrill ins
-- Heuristic: choose the drill with the more elaborate name.
-- E.g. "metal drill" vs. "drill"
preferredDrill = listToMaybe $ sortOn (Down . T.length . (^. entityName)) equippedDrills

drill <- preferredDrill `isJustOr` Fatal "Drill is required but not equipped?!"
tool <- preferredDrill `isJustOr` Fatal "Drill is required but not equipped?!"
applyDevice ins "drill" d tool

(nextLoc, nextE) <- getDrillTarget "drill" d
applyDevice ins verbPhrase d tool = do
(nextLoc, nextE) <- getDrillTarget verbPhrase d
kostmo marked this conversation as resolved.
Show resolved Hide resolved
inRs <- use recipesIn

let recipes = filter drilling (recipesFor inRs nextE)
drilling = any ((== drill) . snd) . view recipeRequirements
drilling = any ((== tool) . snd) . view recipeRequirements

not (null recipes) `holdsOrFail` ["There is no way to drill", indefinite (nextE ^. entityName) <> "."]
not (null recipes) `holdsOrFail` ["There is no way to", verbPhrase, indefinite (nextE ^. entityName) <> "."]

inv <- use robotInventory

-- add the drilled entity so it can be consumed by the recipe
-- add the targeted entity so it can be consumed by the recipe
let makeRecipe r = (,r) <$> make' (insert nextE inv, ins) r
chosenRecipe <- weightedChoice (\((_, _), r) -> r ^. recipeWeight) (rights (map makeRecipe recipes))
((invTaken, outs), recipe) <-
chosenRecipe
`isJustOrFail` ["You don't have the ingredients to drill", indefinite (nextE ^. entityName) <> "."]
`isJustOrFail` ["You don't have the ingredients to", verbPhrase, indefinite (nextE ^. entityName) <> "."]

let (out, down) = L.partition ((`hasProperty` Portable) . snd) outs
let learn = map (LearnEntity . snd) down
Expand Down
1 change: 1 addition & 0 deletions src/Swarm/Language/Capability.hs
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,7 @@ constCaps = \case
Meet -> Just CMeet
MeetAll -> Just CMeet
Drill -> Just CDrill
Use -> Nothing -- Recipes alone shall dictate whether things can be "used"
kostmo marked this conversation as resolved.
Show resolved Hide resolved
Neg -> Just CArith
Add -> Just CArith
Sub -> Just CArith
Expand Down
7 changes: 7 additions & 0 deletions src/Swarm/Language/Syntax.hs
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,8 @@ data Const
Count
| -- | Drill through an entity.
Drill
| -- | Use an entity with another.
Use
| -- | Construct a new robot.
Build
| -- | Deconstruct an old robot.
Expand Down Expand Up @@ -608,6 +610,11 @@ constInfo c = case c of
, "See what recipes with drill you have available."
, "The `drill` command may return the name of an entity added to your inventory."
]
Use ->
command 2 long . doc "Use one entity upon another." $
[ "Which entities you can `use` with others depends on the available recipes."
, "The object being used must be a 'required' entity in a recipe."
]
Build ->
command 1 long . doc "Construct a new robot." $
[ "You can specify a command for the robot to execute."
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 @@ -579,6 +579,7 @@ inferConst c = case c of
Reprogram -> [tyQ| actor -> {cmd a} -> cmd unit |]
Build -> [tyQ| {cmd a} -> cmd actor |]
Drill -> [tyQ| dir -> cmd (unit + text) |]
Use -> [tyQ| text -> dir -> cmd (unit + text) |]
Salvage -> [tyQ| cmd unit |]
Say -> [tyQ| text -> cmd unit |]
Listen -> [tyQ| cmd text |]
Expand Down
1 change: 1 addition & 0 deletions test/integration/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,7 @@ testScenarioSolution _ci _em =
, testSolution Default "Testing/961-custom-capabilities"
, testSolution Default "Testing/956-GPS"
, testSolution Default "Testing/958-isempty"
, testSolution Default "Testing/1007-use-command"
, testSolution Default "Testing/1024-sand"
, testSolution Default "Testing/1140-detect-command"
, testSolution Default "Testing/1157-drill-return-value"
Expand Down