Skip to content

Commit

Permalink
implement robots in subworlds
Browse files Browse the repository at this point in the history
  • Loading branch information
kostmo committed Jul 17, 2023
1 parent c468483 commit 4785e4b
Show file tree
Hide file tree
Showing 8 changed files with 228 additions and 23 deletions.
3 changes: 2 additions & 1 deletion data/scenarios/Testing/144-subworlds/00-ORDER.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
basic-subworld.yaml
subworld-shared-structures.yaml
subworld-robots.yaml
subworld-mapped-robots.yaml
subworld-located-robots.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@

def doN = \n. \f. if (n > 0) {f; doN (n - 1) f} {}; end;

doN 3 move;
f <- grab;

doN 5 move;
r <- meet;
case r return $ \j. give j f;

Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@

def getRobotNumber = \n.
r <- robotnumbered n;
if (r == self) {
return n;
} {getRobotNumber $ n + 1};
end;

def amLowestRecursive = \targetName. \idx.
r <- robotnumbered idx;
thisName <- as r {whoami};
if (thisName == targetName) {
return $ r == self;
} {amLowestRecursive targetName $ idx + 1};
end;

/**
Iterates through robots by increasing index.
If we encounter a robot, fetched by index,
with the same name as me, but I am not that robot,
then we return false.
*/
def amFirstOfMyName =
myName <- whoami;
amLowestRecursive myName 0;
end;

def waitToGiveThing = \thing.
r <- meet;
case r (\_. wait 1; waitToGiveThing thing) $ \b. give b thing;
end;

def waitToGive =
let thing = "bitcoin" in
create thing;
waitToGiveThing thing;
end;

def waitToReceive =
noop;
end;

def go =
myNumber <- getRobotNumber 0;
log $ "My number: " ++ format myNumber;
amFirst <- amFirstOfMyName;
log $ "Am first with this name? " ++ format amFirst;

if amFirst {waitToReceive} {waitToGive};
end;

go;
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

def doN = \n. \f. if (n > 0) {f; doN (n - 1) f} {}; end;

doN 16 move;

r <- meet;
case r return $ \j. give j "bitcoin";

Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
version: 1
name: Subworld robots
name: Subworld robots (explicit location)
description: |
Demonstrate that system robots can be placed in any subworld
# objectives:
# - goal:
# - |
# `place` the "flower" on the white cell.
# condition: |
# j <- robotnamed "judge";
# as j {ishere "flower"}
# solution: |
# run "scenarios/Testing/144-subworlds/_basic-subworld/solution.sw"
Demonstrate that system robots can be placed in any subworld.
objectives:
- goal:
- |
`give` the "flower" to the robot underground.
condition: |
j <- robotnamed "judge";
as j {has "flower"}
solution: |
run "scenarios/Testing/144-subworlds/_subworld-located-robots/solution.sw"
attrs:
- name: portal_in
fg: "#ff9a00"
Expand All @@ -36,8 +36,12 @@ entities:
robots:
- name: base
dir: [1, 0]
loc:
subworld: root
loc: [2, 0]
devices:
- ADT calculator
- antenna
- branch predictor
- comparator
- compass
Expand All @@ -51,18 +55,21 @@ robots:
- treads
- name: judge
dir: [1, 0]
loc:
subworld: underground
loc: [4, 0]
system: true
display:
char: 'J'
invisible: true
invisible: false
known: [flower, boulder]
subworlds:
- name: underground
default: [blank]
palette:
'.': [dirt]
'f': [dirt, flower]
'b': [dirt, boulder]
't': [grass, null, judge]
'p':
cell: [dirt, telepad exit]
waypoint:
Expand All @@ -79,14 +86,15 @@ subworlds:
upperleft: [-1, 1]
map: |
b..b..b..b
.p..f...P.
.p......P.
b..b..b..b
world:
default: [blank]
palette:
'.': [grass]
'f': [grass, flower]
'B': [grass, null, base]
't': [ice, null, judge]
't': [grass, null, judge]
'p':
cell: [grass, telepad exit]
waypoint:
Expand All @@ -103,5 +111,5 @@ world:
subworldName: underground
map: |
..........
.p.Bt...P.
.p....f.P.
..........
115 changes: 115 additions & 0 deletions data/scenarios/Testing/144-subworlds/subworld-mapped-robots.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
version: 1
name: Subworld robots (map placement)
description: |
Demonstrate that system robots can be placed in any subworld.
Also demonstrates tiebreaking logic for robot numbering based
on subworld.
objectives:
- goal:
- |
`give` the "bitcoin" to the robot in the "root" world.
- |
First obtain it from the robot living underground.
condition: |
j <- robotnumbered 1;
as j {has "bitcoin"}
solution: |
run "scenarios/Testing/144-subworlds/_subworld-mapped-robots/solution.sw"
attrs:
- name: portal_in
fg: "#ff9a00"
bg: "#ff5d00"
- name: portal_out
fg: "#00a2ff"
bg: "#0065ff"
entities:
- name: telepad entrance
display:
attr: portal_in
char: "o"
description:
- Portal entrance
properties: [known]
- name: telepad exit
display:
attr: portal_out
char: "o"
description:
- Portal exit
properties: [known]
robots:
- name: base
dir: [1, 0]
devices:
- ADT calculator
- antenna
- branch predictor
- comparator
- compass
- dictionary
- GPS receiver
- grabber
- lambda
- lodestone
- logger
- strange loop
- treads
- name: judge
dir: [1, 0]
system: true
display:
char: 'J'
invisible: false
program: |
run "scenarios/Testing/144-subworlds/_subworld-mapped-robots/judges.sw";
known: [boulder]
subworlds:
- name: underground
default: [blank]
palette:
'.': [dirt]
'b': [dirt, boulder]
't': [grass, null, judge]
'p':
cell: [dirt, telepad exit]
waypoint:
name: portal_out2
'P':
cell: [dirt, telepad entrance]
waypoint:
name: portal_in2
portals:
- entrance: portal_in2
exitInfo:
exit: portal_out1
subworldName: root
upperleft: [-1, 1]
map: |
b..b..b..b
.p.t....P.
b..b..b..b
world:
default: [blank]
palette:
'.': [grass]
'B': [grass, null, base]
't': [grass, null, judge]
'p':
cell: [grass, telepad exit]
waypoint:
name: portal_out1
'P':
cell: [grass, telepad entrance]
waypoint:
name: portal_in1
upperleft: [-1, 1]
portals:
- entrance: portal_in1
exitInfo:
exit: portal_out2
subworldName: underground
map: |
..........
.p.B..t.P.
..........
21 changes: 15 additions & 6 deletions src/Swarm/Game/State.hs
Original file line number Diff line number Diff line change
Expand Up @@ -1160,6 +1160,10 @@ scenarioToGameState scenario (LaunchParams (Identity userSeed) (Identity toRun))
-- 2.a. If multiple robots are specified in the map, prefer the one that
-- is defined first within the Scenario file.
-- 2.b. If multiple robots are instantiated from the same template, then
-- prefer the one with a lower-indexed subworld. Note that the root
-- subworld is always first.
-- 2.c. If multiple robots instantiated from the same template are in the
-- same subworld, then
-- prefer the one closest to the upper-left of the screen, with higher
-- rows given precedence over columns (i.e. first in row-major order).
robotsByBasePrecedence = locatedRobots ++ map snd (sortOn fst genRobots)
Expand Down Expand Up @@ -1206,16 +1210,21 @@ scenarioToGameState scenario (LaunchParams (Identity userSeed) (Identity toRun))
(maybe True (`S.member` initialCaps) . constCaps)
allConst

-- TODO: We currently only utilize genRobots on the root world.
(genRobots, _wf) = buildWorld em $ NE.head $ scenario ^. scenarioWorlds
-- Subworld order as encountered in the scenario YAML file is preserved for
-- the purpose of numbering robots, other than the "root" subworld
-- guaranteed to be first.
genRobots = concat $ NE.toList $ NE.map (fst . snd) builtWorldTuples

builtWorldTuples = NE.map (worldName &&& buildWorld em)
$ scenario ^. scenarioWorlds

allSubworldsMap s =
M.fromList
. map (worldName &&& genWorld)
M.map genWorld
. M.fromList
. NE.toList
$ scenario ^. scenarioWorlds
$ builtWorldTuples
where
genWorld x = W.newWorld $ snd (buildWorld em x) s
genWorld x = W.newWorld $ snd x s

theWinCondition =
maybe
Expand Down
2 changes: 2 additions & 0 deletions test/integration/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,8 @@ testScenarioSolution _ci _em =
, testSolution Default "Testing/1295-density-command"
, testSolution Default "Testing/1356-portals/portals-flip-and-rotate.yaml"
, testSolution Default "Testing/144-subworlds/basic-subworld.yaml"
, testSolution Default "Testing/144-subworlds/subworld-mapped-robots.yaml"
, testSolution Default "Testing/144-subworlds/subworld-located-robots.yaml"
]
]
where
Expand Down

0 comments on commit 4785e4b

Please sign in to comment.