Skip to content

Commit

Permalink
Pied piper scenario
Browse files Browse the repository at this point in the history
  • Loading branch information
kostmo committed Sep 24, 2024
1 parent b2983b9 commit e3a0bbe
Show file tree
Hide file tree
Showing 3 changed files with 328 additions and 0 deletions.
116 changes: 116 additions & 0 deletions data/scenarios/Challenges/_pied-piper/rat.sw
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
def checkExpireSelf =
loc <- whereami;
if (snd loc > 6) {selfdestruct} {};
end;


def moveWithMortality =
move;
instant checkExpireSelf;
end;

def goFoodDir = \f. \r.
// say "in goFoodDir";
let d = fst r in
if (d == down) {
foodHere <- ishere "oats";
if foodHere {
grab; return ()
} {};
f;
return ()
} {
turn d;

// An obstruction might arise after
// navigation direction is determined
// but before we move.
try {
moveWithMortality;
} {};
f;
}
end;

def goHomeDir = \f. \r.
// say "in goHomeDir";
let d = fst r in
if (d == down) {
return ()
} {
turn d;

// An obstruction might arise after
// navigation direction is determined
// but before we move.
try {
moveWithMortality;
} {};
f;
}
end;

def findGoodDirection =
// say "in findGoodDirection";
isBlocked <- blocked;
if isBlocked {
turn left;
findGoodDirection;
} {};
end;

def moveUntilBlocked =
isBlocked <- blocked;
if isBlocked {
} {
moveWithMortality;
moveUntilBlocked;
};
end;

def pauseAtRandom =
r <- random 3;
if (r == 0) {
r2 <- random 12;
wait $ 6 + r2;
} {}
end;

def returnHome = \homeLoc.
// say "in returnHome";
nextDir <- path (inL ()) (inL homeLoc);
case nextDir return $ goHomeDir $ returnHome homeLoc;
end;

def pursueFood = \hadSensedFood. \homeLoc.
// say $ "in pursueFood. hadSensedFood? " ++ format hadSensedFood;
nextDir <- path (inR 5) (inR "oats");
case nextDir (\_. if hadSensedFood {returnHome homeLoc} {return ()}) $
goFoodDir $ pursueFood true homeLoc;
end;


def doMovement = \startLoc.
// say "in doMovement";
checkExpireSelf;

findGoodDirection;
moveUntilBlocked;
pauseAtRandom;
pursueFood false startLoc;
end;

/** Loop */
def go = \startLoc.
doMovement startLoc;
go startLoc;
end;

startLoc <- whereami;
// let shouldDestruct = !(snd startLoc == -17 && fst startLoc < 19) in
let shouldDestruct = false in
if shouldDestruct {
selfdestruct;
} {
go startLoc;
}
39 changes: 39 additions & 0 deletions data/scenarios/Challenges/_pied-piper/solution.sw
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
def doN = \n. \f. if (n > 0) {f; doN (n - 1) f} {}; end;

def makeOatsCrumb = \spacing.
place "oats";
doN spacing move;
end;


def waitUntilDisappeared =
found <- scout north;
if found {
wait 1;
waitUntilDisappeared;
} {};
end;

/**
Place a trail of breadcrumbs, and return
one crumb short of the first crumb.
*/
def makeOatsTrail = \spacing. \segments.
doN segments $ makeOatsCrumb spacing;
turn back;
doN ((segments - 1)*spacing) move;
turn back;
watch down;
wait 2000;
end;

def go =
let spacing = 4 in
let segments = 5 in
makeOatsTrail spacing segments;
// Has probably awakened due to rat eating food here.
waitUntilDisappeared;
makeOatsTrail spacing $ segments;
end;

go;
173 changes: 173 additions & 0 deletions data/scenarios/Challenges/pied-piper.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
version: 1
name: Pied Piper
author: Karl Ostmo
description: |
Rid the village of pests.
creative: false
attrs:
- name: oats
fg: "#444444"
bg: "#F5DEB3"
- name: bridge
bg: "#806040"
terrains:
- name: bridge
attr: bridge
description: |
Wooden raised path
robots:
- name: base
loc: [-1, 7]
dir: south
devices:
- 3D printer
- ADT calculator
- antenna
- binoculars
- branch predictor
- clock
- comparator
- compass
- counter
- dictionary
- grabber
- hearing aid
- lambda
- logger
- net
- rolex
- scanner
- strange loop
- string
- treads
- tweezers
- workbench
inventory:
- [200, oats]
walkable:
never:
- door
- name: rat
system: true
dir: west
display:
invisible: false
char: 'r'
devices:
- logger
program: |
run "scenarios/Challenges/_pied-piper/rat.sw"
objectives:
- goal:
- Rats have infested the village dwellings. Clear them out!
condition: |
try {
r <- robotNamed "rat";
return false;
} {return true}
solution: |
run "scenarios/Challenges/_pied-piper/solution.sw"
entities:
- name: door
display:
char: 'D'
description:
- door
properties: [known]
- name: wall
display:
char: 'W'
attr: wood
description:
- wall
properties: [known, unwalkable]
- name: oats
display:
attr: oats
char: 'a'
description:
- Grain
properties: [known, pickable, growable]
growth:
duration: [20, 30]
known: [water, boulder]
seed: 0
world:
structures:
- name: burrow
structure:
mask: '.'
palette:
'x': [stone, boulder]
map: |
.xxxx.
xx..xx
x.....
xx..xx
.xxxx.
- name: house
structure:
palette:
'd': [stone, door]
'x': [stone, wall]
'.': [stone]
'r': [stone, erase, rat]
map: |
xxxxxxxxx
x.......x
d.......x
x.......x
x....r..x
x.......x
xxxxxxxxx
- name: street pair
structure:
placements:
- src: house
offset: [0, 0]
orient:
up: south
- src: house
offset: [16, 0]
orient:
flip: true
map: ""
- name: block
structure:
placements:
- src: street pair
offset: [0, 0]
orient:
flip: true
- src: street pair
offset: [0, 8]
map: ""
palette:
'b': [bridge, erase]
placements:
- src: block
offset: [-10, -14]
- src: block
offset: [-16, -30]
orient:
up: east
- src: block
offset: [6, -30]
orient:
up: east
- src: burrow
offset: [-7, 10]
upperleft: [-2, 2]
dsl: |
overlay
[ {grass}
, mask (y > -2 && y < 2) {water}
, mask (x > -3 && x < 3) {stone}
, mask (y > -35 && y < -29) {stone}
]
map: |
bbbbb
bbbbb
bbbbb
bbbbb
bbbbb

0 comments on commit e3a0bbe

Please sign in to comment.