From 146cd4b9a4a8aa1f3f8d00a5ba7d2ab480503439 Mon Sep 17 00:00:00 2001 From: Karl Ostmo Date: Sat, 5 Nov 2022 14:43:50 -0700 Subject: [PATCH] New ranching scenario --- data/scenarios/Challenges/00-ORDER.txt | 1 + .../Challenges/Ranching/00-ORDER.txt | 1 + .../Challenges/Ranching/gated-paddock.yaml | 163 ++++++++++++++++++ .../gated-paddock/enclosure-checking.sw | 87 ++++++++++ .../gated-paddock/fence-construction.sw | 64 +++++++ .../gated-paddock/meandering-sheep.sw | 36 ++++ 6 files changed, 352 insertions(+) create mode 100644 data/scenarios/Challenges/Ranching/00-ORDER.txt create mode 100644 data/scenarios/Challenges/Ranching/gated-paddock.yaml create mode 100644 data/scenarios/Challenges/Ranching/gated-paddock/enclosure-checking.sw create mode 100644 data/scenarios/Challenges/Ranching/gated-paddock/fence-construction.sw create mode 100644 data/scenarios/Challenges/Ranching/gated-paddock/meandering-sheep.sw diff --git a/data/scenarios/Challenges/00-ORDER.txt b/data/scenarios/Challenges/00-ORDER.txt index 5dd1d8a9..b662ac0f 100644 --- a/data/scenarios/Challenges/00-ORDER.txt +++ b/data/scenarios/Challenges/00-ORDER.txt @@ -3,3 +3,4 @@ teleport.yaml 2048.yaml hanoi.yaml Mazes +Ranching diff --git a/data/scenarios/Challenges/Ranching/00-ORDER.txt b/data/scenarios/Challenges/Ranching/00-ORDER.txt new file mode 100644 index 00000000..87ae5484 --- /dev/null +++ b/data/scenarios/Challenges/Ranching/00-ORDER.txt @@ -0,0 +1 @@ +gated-paddock.yaml diff --git a/data/scenarios/Challenges/Ranching/gated-paddock.yaml b/data/scenarios/Challenges/Ranching/gated-paddock.yaml new file mode 100644 index 00000000..92ca04da --- /dev/null +++ b/data/scenarios/Challenges/Ranching/gated-paddock.yaml @@ -0,0 +1,163 @@ +version: 1 +name: Pastoral Island +description: | + Start a wool industry from the local fauna. +creative: false +objectives: + - goal: + - | + You've homesteaded on a small Pacific island. + It's time to gather resources to trade. + - | + You encounter some feral sheep. They look like a promising + source of wool. + - | + First, paddock the sheep so they don't drown. + Construct a fence using the nearby gate as a starting point. + Make sure there are no gaps! + - | + Note that you can use the "drill" command (by way of the "post puller" tool) + to demolish a fence that has been "placed". + condition: | + r <- robotnamed "sheep"; + as r {run "/home/kostmo/github/swarm-scripts/characters/enclosure-checking.sw"}; +robots: + - name: base + dir: [0, 1] + devices: + - treads + - scanner + - dictionary + - branch predictor + - strange loop + - clock + - ADT calculator + - comparator + - workbench + - lodestone + - grabber + - post puller + - name: sheep + description: + - meandering livestock + display: + invisible: false + char: '@' + system: true + dir: [0, 1] + program: | + run "/home/kostmo/github/swarm-scripts/characters/meandering-sheep.sw"; +entities: + - name: fence + display: + char: '#' + description: + - A fence. + properties: [known, portable, unwalkable] + - name: solid fence + display: + char: '#' + attr: rock + description: + - An unmovable fence. + properties: [known, unwalkable] + - name: post puller + display: + char: 'P' + attr: rock + capabilities: [drill] + description: + - Good for dismantling fences. + properties: [known] + - name: scrap wood + display: + char: '\' + description: + - Scrap wood, good for nothing, really. + properties: [known] + - name: gate + display: + char: '/' + attr: rock + description: + - A gate. + properties: [known] + - name: cabin + display: + char: 🏠 + attr: rock + description: + - A cabin. + properties: [known, unwalkable] + - name: fresh breadcrumb + display: + char: '.' + description: + - A marker that can be put down and found again. + properties: [portable] + - name: treaded breadcrumb + display: + char: 'x' + description: + - A marker that can be put down and found again. + properties: [portable] +recipes: + - in: + - [2, board] + out: + - [1, fence] + - in: + - [0, fresh breadcrumb] + out: + - [1, fresh breadcrumb] + - in: + - [1, fresh breadcrumb] + out: + - [1, treaded breadcrumb] + - in: + - [1, fence] + out: + - [1, scrap wood] + required: + - [1, post puller] +known: [mountain, tree, water, fresh breadcrumb, treaded breadcrumb] +seed: 0 +solution: | + run "/home/kostmo/github/swarm-scripts/characters/fence-construction.sw" +world: + default: [dirt, water] + palette: + 'B': [grass, null, base] + '.': [grass] + 'G': [grass, gate] + 'f': [grass, solid fence] + 't': [dirt, tree] + 'x': [stone, mountain] + 'c': [stone, cabin] + 's': [grass, null, sheep] + '~': [dirt, water] + upperleft: [-34, 11] + map: | + ~~.........~~~~~~~~~~~~~.......................~~~~~ + ~..............~~~~...............................~~ + ...................................................~ + .................................................... + .........................................tttttttt... + .........................................tttttttt... + ..............................x..........tttttttt... + .........................................tttttttt... + .........s...............................tttttttt... + .........................................tttttttt... + ...................................f................ + ..xx...............................G.....B......c... + ...x...............................f................ + ..............................s..................... + .................................................... + .................................................... + ~........x.....s.................................... + ~~.................................................. + ~~~..........................x..............~~~~.... + ~~~....................................~~~~~~~~~...~ + ~~......................................~~~~~~~~~~~~ + ~~..............................~~~~~~~~~~~~~~~~~~~~ + ~~~~...................~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ \ No newline at end of file diff --git a/data/scenarios/Challenges/Ranching/gated-paddock/enclosure-checking.sw b/data/scenarios/Challenges/Ranching/gated-paddock/enclosure-checking.sw new file mode 100644 index 00000000..aa80e19c --- /dev/null +++ b/data/scenarios/Challenges/Ranching/gated-paddock/enclosure-checking.sw @@ -0,0 +1,87 @@ +// Algorithm: +// Maintain current direction until a wall is encountered. +// Then enter "wall-following mode". +// This mode presumes the wall is not a loop. +// (If it was a loop, then an "oriented breadcrumb" would need to be left. +// The breadcrumb is oriented in case a single-width passage is backtracked +// along the opposite wall.) + +def isFenced = + s <- scan forward; + return ( + case s + (\_. false) + (\x. x == "fence" || x == "gate" || x == "solid fence") + ); + end; + +def isBlockedOrFenced = + b <- blocked; + f <- isFenced; + return (b || f); + end; + +// Returns true if we've already placed two +// breadcrumbs on a given tile, false otherwise. +def leaveBreadcrumbs = + + let bc1 = "fresh breadcrumb" in + let bc2 = "treaded breadcrumb" in + + wasTraversedOnce <- ishere bc1; + if wasTraversedOnce { + _crumb <- grab; + make bc2; + place bc2; + return false; + } { + wasTraversedTwice <- ishere bc2; + if wasTraversedTwice { + return true; + } { + // Make sure nothing's in the way before we place + // our breadcrumb: + x <- scan down; + case x return (\_. grab; return ()); + + make bc1; + place bc1; + return false; + }; + }; + end; + +def goForwardToPatrol = \wasBlocked. + b <- isBlockedOrFenced; + if b { + turn left; + goForwardToPatrol true; + turn right; + goForwardToPatrol false; + } { + if wasBlocked { + isLoop <- leaveBreadcrumbs; + if isLoop { + fail "loop"; + } {}; + } {}; + move; + }; + end; + +def checkIsEnclosed = + try { + goForwardToPatrol false; + // Water is the outer boundary + hasWater <- ishere "water"; + if hasWater { + return false; + } { + checkIsEnclosed; + }; + } { + return true; + }; + end; + +checkIsEnclosed; \ No newline at end of file diff --git a/data/scenarios/Challenges/Ranching/gated-paddock/fence-construction.sw b/data/scenarios/Challenges/Ranching/gated-paddock/fence-construction.sw new file mode 100644 index 00000000..4db54a25 --- /dev/null +++ b/data/scenarios/Challenges/Ranching/gated-paddock/fence-construction.sw @@ -0,0 +1,64 @@ +def doN = \n. \f. if (n > 0) {f; doN (n - 1) f} {}; end; + + +def processTree = + grab; + make "log"; + make "board"; + doN 2 (make "fence"); + end; + + +def grabTwoRows = + doN 7 (processTree; move); + processTree; + turn right; + move; + turn right; + + doN 7 (processTree; move); + processTree; + end; + + +def grabTrees = + doN 7 move; + turn right; + + grabTwoRows; + + turn left; + move; + turn left; + + grabTwoRows; + + turn left; + move; + turn left; + + grabTwoRows; + + end; + + +def buildFence = + doN 6 move; + turn right; + doN 4 (place "fence"; move); + turn left; + doN 30 (place "fence"; move); + turn left; + doN 15 (place "fence"; move); + turn left; + doN 30 (place "fence"; move); + turn left; + doN 7 (place "fence"; move); + place "fence"; + turn right; + doN 6 move; + end; + +// buildFence; +grabTrees; +buildFence; \ No newline at end of file diff --git a/data/scenarios/Challenges/Ranching/gated-paddock/meandering-sheep.sw b/data/scenarios/Challenges/Ranching/gated-paddock/meandering-sheep.sw new file mode 100644 index 00000000..3d91eeb6 --- /dev/null +++ b/data/scenarios/Challenges/Ranching/gated-paddock/meandering-sheep.sw @@ -0,0 +1,36 @@ +// A "sheep" that wanders around randomly. + +let forever : cmd unit -> cmd unit = \c. c ; forever c in +let repeat : int -> cmd unit -> cmd unit = + \n. \c. if (n == 0) {} {c ; repeat (n-1) c} in +let randdir : cmd dir = + d <- random 4; + return ( + if (d == 0) {north} + {if (d == 1) {east} + {if (d == 2) {south} {west}}}) + in + +forever ( + n <- random 30; + wait (30 + n); + d <- randdir; + turn d; + dist <- random 3; + repeat dist ( + b <- blocked; + if b {} { + move; + + // Sheep can drown. + hasWater <- ishere "water"; + if hasWater { + say "whoops!"; + selfdestruct; + } {}; + + } + ); + r <- random 5; + if (r == 0) { say "baaa" } {} +)