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

Add isempty : cmd bool to check whether current cell has an entity #968

Merged
merged 2 commits into from
Jan 6, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
add isempty : cmd bool to check whether current cell has an entity
  • Loading branch information
byorgey committed Jan 6, 2023
commit 48bd13af634acde0d8d2b749692a0c551c4f99bc
8 changes: 6 additions & 2 deletions data/entities.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -914,9 +914,13 @@
about their surroundings. Simply give `scan` a direction in which to scan,
and information about the scanned item (if any) will be added to the robot's
inventory."
- "A scanner also enables the `blocked` command, which returns a
- "A scanner also enables `blocked : cmd bool`, which returns a
boolean value indicating whether the robot's path is blocked
(i.e. whether executing a `move` command would fail)."
(i.e. whether executing a `move` command would fail);
`ishere : text -> cmd bool` for checking whether the current
cell contains a particular entity; and `isempty : cmd bool` for
checking whether the current cell is empty of entities. Note that
`ishere` and `isempty` do not detect robots, only entities."
- "Finally, robots can use the `upload` command to copy their accumulated
knowledge to another nearby robot; for example, `upload base`."
properties: [portable]
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 @@ -20,3 +20,4 @@
397-wrong-missing.yaml
961-custom-capabilities.yaml
956-GPS.yaml
958-isempty.yaml
57 changes: 57 additions & 0 deletions data/scenarios/Testing/958-isempty.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
version: 1
name: Test isempty
description: |
Test the `isempty` command.
objectives:
- condition: |
as base {has "thneed"}
goal:
- |
The goal is to pick up the thing right after the empty
spot in the row.
solution: |
def find_flower =
e <- isempty;
move;
if e {} {find_flower}
end;
find_flower;
grab
robots:
- name: base
dir: [1,0]
devices:
- scanner
- branch predictor
- grabber
- strange loop
- lambda
- dictionary
- logger
- treads
- solar panel
entities:
- name: thneed
display:
attr: gold
char: 'Y'
description:
- A thing that everyone needs!
properties: [portable]
world:
default: [blank]
palette:
'.': [grass]
'>': [grass, tree, base]
'o': [grass, rock]
'$': [grass, LaTeX]
'i': [grass, cotton]
'0': [grass, bit (0)]
'1': [grass, bit (1)]
'R': [grass, pixel (R)]
'G': [grass, pixel (G)]
'B': [grass, pixel (B)]
'Y': [grass, thneed]
upperleft: [-5, 5]
map: |
>o$i01R.YGB
1 change: 1 addition & 0 deletions editors/emacs/swarm-mode.el
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@
"scan"
"upload"
"ishere"
"isempty"
"meet"
"meetall"
"whoami"
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|noop|wait|selfdestruct|move|turn|grab|harvest|place|give|install|equip|unequip|make|has|installed|count|drill|build|salvage|reprogram|say|listen|log|view|appear|create|time|whereami|heading|blocked|scan|upload|ishere|meet|meetall|whoami|setname|random|run|return|try|swap|atomic|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|noop|wait|selfdestruct|move|turn|grab|harvest|place|give|install|equip|unequip|make|has|installed|count|drill|build|salvage|reprogram|say|listen|log|view|appear|create|time|whereami|heading|blocked|scan|upload|ishere|isempty|meet|meetall|whoami|setname|random|run|return|try|swap|atomic|teleport|as|robotnamed|robotnumbered|knows)\\b"
}
]
},
Expand Down
4 changes: 4 additions & 0 deletions src/Swarm/Game/Step.hs
Original file line number Diff line number Diff line change
Expand Up @@ -1267,6 +1267,10 @@ execConst c vs s k = do
Nothing -> return $ Out (VBool False) s k
Just e -> return $ Out (VBool (T.toLower (e ^. entityName) == T.toLower name)) s k
_ -> badConst
Isempty -> do
loc <- use robotLocation
me <- entityAt loc
return $ Out (VBool (isNothing me)) s k
Self -> do
rid <- use robotID
return $ Out (VRobot rid) s k
Expand Down
3 changes: 2 additions & 1 deletion src/Swarm/Language/Capability.hs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ data Capability
CSenseloc
| -- | Execute the 'Blocked' command
CSensefront
| -- | Execute the 'Ishere' command
| -- | Execute the 'Ishere' and 'Isempty' commands
CSensehere
| -- | Execute the 'Scan' command
CScan
Expand Down Expand Up @@ -192,6 +192,7 @@ constCaps = \case
Blocked -> Just CSensefront
Scan -> Just CScan
Ishere -> Just CSensehere
Isempty -> Just CSensehere
Upload -> Just CScan
Build -> Just CBuild
Salvage -> Just CSalvage
Expand Down
11 changes: 9 additions & 2 deletions src/Swarm/Language/Syntax.hs
Original file line number Diff line number Diff line change
Expand Up @@ -294,8 +294,10 @@ data Const
Scan
| -- | Upload knowledge to another robot
Upload
| -- | See if a specific entity is here. (This may be removed.)
| -- | See if a specific entity is here.
Ishere
| -- | Check whether the current cell is empty
Isempty
| -- | Get a reference to oneself
Self
| -- | Get the robot's parent
Expand Down Expand Up @@ -633,12 +635,17 @@ constInfo c = case c of
Heading -> command 0 Intangible "Get the current heading."
Blocked -> command 0 Intangible "See if the robot can move forward."
Scan ->
command 0 Intangible . doc "Scan a nearby location for entities." $
command 1 Intangible . doc "Scan a nearby location for entities." $
[ "Adds the entity (not actor) to your inventory with count 0 if there is any."
, "If you can use sum types, you can also inspect the result directly."
]
Upload -> command 1 short "Upload a robot's known entities and log to another robot."
Ishere -> command 1 Intangible "See if a specific entity is in the current location."
Isempty ->
command 0 Intangible . doc "Check if the current location is empty." $
[ "Detects whether or not the current location contains an entity."
, "Does not detect robots or other actors."
]
Self -> function 0 "Get a reference to the current robot."
Parent -> function 0 "Get a reference to the robot's parent."
Base -> function 0 "Get a reference to the base."
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 @@ -494,6 +494,7 @@ inferConst c = case c of
Scan -> [tyQ| dir -> cmd (unit + text) |]
Upload -> [tyQ| actor -> cmd unit |]
Ishere -> [tyQ| text -> cmd bool |]
Isempty -> [tyQ| cmd bool |]
Self -> [tyQ| actor |]
Parent -> [tyQ| actor |]
Base -> [tyQ| actor |]
Expand Down
1 change: 1 addition & 0 deletions test/integration/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,7 @@ testScenarioSolution _ci _em =
any ("GPS receiver" `T.isInfixOf`) msgs
, testSolution Default "Testing/961-custom-capabilities"
, testSolution Default "Testing/956-GPS"
, testSolution Default "Testing/958-isempty"
]
]
where
Expand Down