Skip to content

Commit

Permalink
Find robots
Browse files Browse the repository at this point in the history
* add robotNamed : string -> cmd robot
* add chess knight challenge

In challenges, it's impossible to access the robot
by its assigned number. We do however name it.
Seems reasonable to add a way to get robot by its name.
  • Loading branch information
xsebek committed Jun 4, 2022
1 parent 7ad65ad commit 020942b
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 0 deletions.
File renamed without changes.
70 changes: 70 additions & 0 deletions data/scenarios/03Challenges/01-chess_horse.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
name: Chess Knight
description: In this quirky challenge, you move as the chess knight piece. Can you capture the enemy king?
entities:
- name: goal
display:
attr: device
char: 'X'
description:
- |
Robots can use the 'move' command to move.
But they only 'turn' in cardinal directions.
You are special. You are a knight.
Go forth and capture the King!
properties: [portable]
- name: knownwater
display:
attr: water
char: ' '
description:
- An infinite ocean of water.
properties: [known, portable, growable, liquid]
growth: [0,0]
win: |
try {
bloc <- as base {whereami};
king <- robotNamed "king";
kloc <- as king {whereami};
return (bloc == kloc)
} { return false }
robots:
- name: horse
loc: [0,0]
dir: [2,-1]
devices:
- treads
- logger
inventory:
- [1, goal]
display:
char: ''
- name: king
loc: [7,-6]
dir: [0,0]
display:
char: ''
world:
default: [ice, knownwater]
palette:
'.': [grass, null]
'#': [ice, null]
'': [stone, upper left corner]
'': [stone, upper right corner]
'': [stone, lower left corner]
'': [stone, lower right corner]
'': [stone, horizontal wall]
'': [stone, vertical wall]
upperleft: [-1, 1]
map: |
┌────────┐
│.#.#.#.#│
│#.#.#.#.│
│.#.#.#.#│
│#.#.#.#.│
│.#.#.#.#│
│#.#.#.#.│
│.#.#.#.#│
│#.#.#.#.│
└────────┘
10 changes: 10 additions & 0 deletions src/Swarm/Game/Step.hs
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,10 @@ updateEntityAt loc upd = zoomWorld (W.updateM @Int (W.locToCoords loc) upd)
robotWithID :: (Has (State GameState) sig m) => RID -> m (Maybe Robot)
robotWithID rid = use (robotMap . at rid)

-- | Get the robot with a given name.
robotWithName :: (Has (State GameState) sig m) => Text -> m (Maybe Robot)
robotWithName rname = use (robotMap . to IM.elems . to (find $ \r -> r ^. robotName == rname))

-- | Manhattan distance between world locations.
manhattan :: V2 Int64 -> V2 Int64 -> Int64
manhattan (V2 x1 y1) (V2 x2 y2) = abs (x1 - x2) + abs (y1 - y2)
Expand Down Expand Up @@ -969,6 +973,12 @@ execConst c vs s k = do
-- Return the value returned by the hypothetical command.
return $ Out v s k
_ -> badConst
RobotNamed -> case vs of
[VString rname] -> do
r <- robotWithName rname >>= (`isJustOrFail` ["There is no robot named", rname])
let robotValue = VRobot (r ^. robotID)
return $ Out robotValue s k
_ -> badConst
Say -> case vs of
[VString msg] -> do
rn <- use robotName -- XXX use robot name + ID
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 @@ -268,6 +268,7 @@ constCaps =
Drill -> [CDrill]
-- Some God-like sensing abilities.
As -> [CGod]
RobotNamed -> [CGod]
-- String operations, which for now are enabled by CLog
Format -> [CLog]
Concat -> [CLog]
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 @@ -347,6 +347,8 @@ data Const

-- | Run a command as if you were another robot.
As
| -- | Find a robot by name.
RobotNamed
deriving (Eq, Ord, Enum, Bounded, Data, Show)

allConst :: [Const]
Expand Down Expand Up @@ -484,6 +486,7 @@ constInfo c = case c of
Concat -> binaryOp "++" 6 R
AppF -> binaryOp "$" 0 R
As -> commandLow 2
RobotNamed -> commandLow 1
where
unaryOp s p side = ConstInfo {syntax = s, fixity = p, constMeta = ConstMUnOp side}
binaryOp s p side = ConstInfo {syntax = s, fixity = p, constMeta = ConstMBinOp side}
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 @@ -499,6 +499,7 @@ inferConst c = toU $ case c of
Concat -> [tyQ| string -> string -> string |]
AppF -> [tyQ| (a -> b) -> a -> b |]
As -> [tyQ| robot -> {cmd a} -> cmd a |]
RobotNamed -> [tyQ| string -> cmd robot |]
where
cmpBinT = [tyQ| a -> a -> bool |]
arithBinT = [tyQ| int -> int -> int |]
Expand Down

0 comments on commit 020942b

Please sign in to comment.