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 print + erase commands, and a challenge scenario showing off print #2245

Merged
merged 13 commits into from
Jan 3, 2025
2 changes: 1 addition & 1 deletion data/entities.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@
char: '■'
description:
- A flat material made of pressed and dried wood fibers, used as a surface on which to inscribe symbols.
properties: [pickable, combustible]
properties: [pickable, combustible, printable]
combustion:
ignition: 0.5
duration: [10, 20]
Expand Down
2 changes: 1 addition & 1 deletion data/scenarios/Challenges/_telephone/solution.sw
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ def go =
res <- scanAt (h-1) (v-1);
case res
(\_. return ())
(\t. atTerminal (p <- print (format ((h-1,v-1),t)); waitToPlace p))
(\t. atTerminal (p <- print "paper" (format ((h-1,v-1),t)); waitToPlace p))
)
)
end
Expand Down
33 changes: 18 additions & 15 deletions src/swarm-engine/Swarm/Game/Step/Const.hs
Original file line number Diff line number Diff line change
Expand Up @@ -1215,27 +1215,30 @@
Just v -> return (mkReturn v)
_ -> badConst
Print -> case vs of
[VText txt] -> do
paper <- ensureItem "paper" "print"
let newEntityName = "paper: " <> txt
robotInventory %= delete paper
robotInventory %= insert (paper & entityName .~ newEntityName)
[VText printableName, VText txt] -> do
printable <- ensureItem printableName "print"
(printable `hasProperty` Printable)
`holdsOrFail` ["You cannot print on", indefinite printableName <> "!"]
let newEntityName = printableName <> ": " <> txt
robotInventory %= delete printable
robotInventory %= insert (printable & entityName .~ newEntityName)
return $ mkReturn newEntityName
_ -> badConst
Erase -> case vs of
[VText paperName] -> do
toErase <- ensureItem paperName "erase"
(paperName /= "paper")
`holdsOrFail` ["That is already blank!"]
("paper: " `T.isPrefixOf` paperName)
`holdsOrFail` ["Can only erase paper, not", indefinite paperName <> "."]
[VText printableName] -> do
toErase <- ensureItem printableName "erase"
let (baseName, printedMatter) = T.break (==':') printableName
em <- use $ landscape . terrainAndEntities . entityMap
paper <-
lookupEntityName "paper" em
`isJustOrFail` ["Can't erase, I don't know what paper is."]
erased <-
lookupEntityName baseName em
`isJustOrFail` ["I've never heard of", indefiniteQ baseName <> "."]
(erased `hasProperty` Printable)
`holdsOrFail` ["You cannot erase", indefinite baseName <> "!"]
(not $ T.null printedMatter)

Check warning on line 1237 in src/swarm-engine/Swarm/Game/Step/Const.hs

View workflow job for this annotation

GitHub Actions / HLint

Suggestion in execConst in module Swarm.Game.Step.Const: Move brackets to avoid $ ▫︎ Found: "(not $ T.null printedMatter)\n `holdsOrFail` [\"That is already blank!\"]" ▫︎ Perhaps: "not (T.null printedMatter) `holdsOrFail` [\"That is already blank!\"]"
`holdsOrFail` ["That is already blank!"]
byorgey marked this conversation as resolved.
Show resolved Hide resolved

robotInventory %= delete toErase
robotInventory %= insert paper
robotInventory %= insert erased
return $ mkReturn ()
byorgey marked this conversation as resolved.
Show resolved Hide resolved
_ -> badConst
Chars -> case vs of
Expand Down
16 changes: 8 additions & 8 deletions src/swarm-lang/Swarm/Language/Syntax/Constants.hs
Original file line number Diff line number Diff line change
Expand Up @@ -821,22 +821,22 @@ constInfo c = case c of
Format -> function 1 $ shortDoc Set.empty "Turn an arbitrary value into a string."
Read -> function 2 $ shortDoc Set.empty "Try to read a string into a value of the expected type."
Print ->
command 1 short
command 2 short
. doc
(Set.singleton $ Mutation $ RobotChange InventoryChange)
"Print text onto a piece of paper."
$ [ "Consumes one `paper` entity from your inventory, and produces an entity"
, "whose name is \"paper: \" concatenated with the given text."
, "In conjunction with `format`, this can be used to print values onto paper"
"Print text onto an entity."
$ [ "`print p txt` Consumes one printable `p` entity from your inventory, and produces an entity"
, "whose name is concatenated with a colon and the given text."
, "In conjunction with `format`, this can be used to print values onto entities such as `paper`{=entity}"
, "and give them to other robots, which can reconstitute the values with `read`."
]
Erase ->
command 1 short
. doc
(Set.singleton $ Mutation $ RobotChange InventoryChange)
"Erase a piece of paper."
$ [ "Consumes the named entity from your inventory, whose name must begin with"
, "\"paper: \", and produces a \"paper\" entity. This can be used to undo"
"Erase an entity."
$ [ "Consumes the named printable entity from your inventory, which must have something"
, "printed on it, and produces an erased entity. This can be used to undo"
, "the effects of a `print` command."
]
Concat -> binaryOp "++" 6 R $ shortDoc Set.empty "Concatenate the given strings."
Expand Down
2 changes: 1 addition & 1 deletion src/swarm-lang/Swarm/Language/Typecheck.hs
Original file line number Diff line number Diff line change
Expand Up @@ -1100,7 +1100,7 @@ inferConst c = run . runReader @TVCtx Ctx.empty . quantify $ case c of
Exp -> arithBinT
Format -> [tyQ| a -> Text |]
Read -> [tyQ| Text -> a |]
Print -> [tyQ| Text -> Cmd Text |]
Print -> [tyQ| Text -> Text -> Cmd Text |]
Erase -> [tyQ| Text -> Cmd Unit |]
Concat -> [tyQ| Text -> Text -> Text |]
Chars -> [tyQ| Text -> Int |]
Expand Down
4 changes: 4 additions & 0 deletions src/swarm-scenario/Swarm/Game/Entity.hs
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,10 @@ data EntityProperty
Liquid
| -- | Robots automatically know what this is without having to scan it.
Known
| -- | Text can be printed on this entity with the
-- 'Swarm.Language.Syntax.Print' command (and erased with
-- 'Swarm.Language.Syntax.Erase')
Printable
deriving (Eq, Ord, Show, Read, Enum, Bounded, Generic, Hashable)

instance ToJSON EntityProperty where
Expand Down
1 change: 1 addition & 0 deletions src/swarm-tui/Swarm/TUI/View.hs
Original file line number Diff line number Diff line change
Expand Up @@ -1122,6 +1122,7 @@ displayProperties = displayList . mapMaybe showProperty
-- in challenge scenarios and not really something the player needs
-- to know.
showProperty Known = Nothing
showProperty Printable = Just "printable"

displayList [] = emptyWidget
displayList ps =
Expand Down
Loading