diff --git a/data/scenarios/03Challenges/test.yaml b/data/scenarios/03Challenges/00-test.yaml similarity index 100% rename from data/scenarios/03Challenges/test.yaml rename to data/scenarios/03Challenges/00-test.yaml diff --git a/data/scenarios/03Challenges/01-chess_horse.yaml b/data/scenarios/03Challenges/01-chess_horse.yaml new file mode 100644 index 000000000..3f77d6341 --- /dev/null +++ b/data/scenarios/03Challenges/01-chess_horse.yaml @@ -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: | + ┌────────┐ + │.#.#.#.#│ + │#.#.#.#.│ + │.#.#.#.#│ + │#.#.#.#.│ + │.#.#.#.#│ + │#.#.#.#.│ + │.#.#.#.#│ + │#.#.#.#.│ + └────────┘ diff --git a/src/Swarm/Game/Step.hs b/src/Swarm/Game/Step.hs index 69dbff508..6527d95d0 100644 --- a/src/Swarm/Game/Step.hs +++ b/src/Swarm/Game/Step.hs @@ -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) @@ -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 diff --git a/src/Swarm/Language/Capability.hs b/src/Swarm/Language/Capability.hs index c2c6aa368..3851b915f 100644 --- a/src/Swarm/Language/Capability.hs +++ b/src/Swarm/Language/Capability.hs @@ -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] diff --git a/src/Swarm/Language/Syntax.hs b/src/Swarm/Language/Syntax.hs index 1ae08928a..743fcb5fa 100644 --- a/src/Swarm/Language/Syntax.hs +++ b/src/Swarm/Language/Syntax.hs @@ -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] @@ -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} diff --git a/src/Swarm/Language/Typecheck.hs b/src/Swarm/Language/Typecheck.hs index 505fd4333..210ff7c54 100644 --- a/src/Swarm/Language/Typecheck.hs +++ b/src/Swarm/Language/Typecheck.hs @@ -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 |]