From e3a0bbef972c76fe6d1dbe9d4e5d083aec326de8 Mon Sep 17 00:00:00 2001 From: Karl Ostmo Date: Sun, 18 Aug 2024 15:33:24 -0700 Subject: [PATCH] Pied piper scenario --- data/scenarios/Challenges/_pied-piper/rat.sw | 116 ++++++++++++ .../Challenges/_pied-piper/solution.sw | 39 ++++ data/scenarios/Challenges/pied-piper.yaml | 173 ++++++++++++++++++ 3 files changed, 328 insertions(+) create mode 100644 data/scenarios/Challenges/_pied-piper/rat.sw create mode 100644 data/scenarios/Challenges/_pied-piper/solution.sw create mode 100644 data/scenarios/Challenges/pied-piper.yaml diff --git a/data/scenarios/Challenges/_pied-piper/rat.sw b/data/scenarios/Challenges/_pied-piper/rat.sw new file mode 100644 index 000000000..c6d92c559 --- /dev/null +++ b/data/scenarios/Challenges/_pied-piper/rat.sw @@ -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; +} diff --git a/data/scenarios/Challenges/_pied-piper/solution.sw b/data/scenarios/Challenges/_pied-piper/solution.sw new file mode 100644 index 000000000..889f305a0 --- /dev/null +++ b/data/scenarios/Challenges/_pied-piper/solution.sw @@ -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; \ No newline at end of file diff --git a/data/scenarios/Challenges/pied-piper.yaml b/data/scenarios/Challenges/pied-piper.yaml new file mode 100644 index 000000000..419b741d7 --- /dev/null +++ b/data/scenarios/Challenges/pied-piper.yaml @@ -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