Skip to content

Commit

Permalink
fishing scenario (#1628)
Browse files Browse the repository at this point in the history
This scenario makes use of several relatively recent features/commands:
* structure placement
* structure queries (for goal checking)
* density command (for goal checking)
* combustion
* tags
* goal prerequisites with boolean expressions

## Demo

    scripts/play.sh -i Challenges/Ranching/fishing.yaml --autoplay

![image](https://github.com/swarm-game/swarm/assets/261693/a9f932a2-7481-4ee4-992a-a4dcd8b7cfd5)
  • Loading branch information
kostmo authored Feb 19, 2024
1 parent e851f44 commit 95a9014
Show file tree
Hide file tree
Showing 6 changed files with 855 additions and 0 deletions.
1 change: 1 addition & 0 deletions data/scenarios/Challenges/Ranching/00-ORDER.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
beekeeping.yaml
capture.yaml
powerset.yaml
fishing.yaml
gated-paddock.yaml
90 changes: 90 additions & 0 deletions data/scenarios/Challenges/Ranching/_fishing/hauler.sw
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
def doN = \n. \f. if (n > 0) {f; doN (n - 1) f} {}; end;

def intersperse = \n. \f2. \f1. if (n > 0) {
f1;
if (n > 1) {
f2;
} {};
intersperse (n - 1) f2 f1;
} {};
end;

def isEnclosureFull = \idx.
foundBox <- structure "rubbish enclosure" idx;
case foundBox (\_. return false) (\enclosure.
let boxPos = snd enclosure in

prevLoc <- whereami;

dims <- floorplan "rubbish enclosure";
teleport self boxPos;

c <- density ((0, 0), dims);
let area = fst dims * snd dims in
let notFull = c < area in

teleport self prevLoc;
return $ not notFull;
);
end;

def isEitherEnclosureFull =
full1 <- isEnclosureFull 0;
full2 <- isEnclosureFull 1;
return $ full1 || full2;
end;

def tryGrab =
try {
grab;
return ()
} {};
end;


def turnAround = \d.
intersperse 2 move $ turn d;
end;

def waitUntilEnclosureFull =

isFull <- instant isEitherEnclosureFull;
if isFull {
// Drive down the road
turn south;
doN 13 move;
turn right;
doN 3 move;
turn right;
doN 2 move;
turn left;

// North enclosure
intersperse 2 (turnAround left) $ intersperse 3 move tryGrab;

intersperse 2 (doN 3 move) $ turn right;

// South enclosure
intersperse 2 (turnAround right) $ intersperse 3 move tryGrab;

turn left;
move;

// Leave again
turn right;
doN 3 move;
turn left;
doN 13 move;
turn back;
} {
wait 10;
waitUntilEnclosureFull;
}
end;

def go =
waitUntilEnclosureFull;
go;
end;

go;
24 changes: 24 additions & 0 deletions data/scenarios/Challenges/Ranching/_fishing/shark.sw
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/**
Swims back and forth forever.
*/

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

def swim =
appear "^";
doN 3 (move; wait 3);
turn back;
wait 15;
doN 3 (move; wait 3);
turn back;
appear " ";
end;

def go =
waitR <- random 100;
wait $ 50 + waitR;
swim;
go;
end;

go;
195 changes: 195 additions & 0 deletions data/scenarios/Challenges/Ranching/_fishing/solution.sw
Original file line number Diff line number Diff line change
@@ -0,0 +1,195 @@
def doN = \n. \f. if (n > 0) {f; doN (n - 1) f} {}; end;

def intersperse = \n. \f2. \f1. if (n > 0) {
f1;
if (n > 1) {
f2;
} {};
intersperse (n - 1) f2 f1;
} {};
end;

def makeRoll =
make "nori";
make "california roll";
end;

def checkIngredients =
hasTuna <- has "crab";
hasSeaweed <- has "seaweed";
return $ hasTuna && hasSeaweed;
end;

def catchFish = \rod.
use rod forward;
ready <- checkIngredients;
if ready {
makeRoll;
} {
catchFish rod;
};
end;

def turnAround = \d.
intersperse 2 move $ turn d;
end;

/*
Precondition:
At the top-right corner
*/
def harvestRectangle =
intersperse 4 move $ harvest; return ();
turnAround left;
intersperse 4 move $ harvest; return ();
end;

def harvestIngredients =
turn north;
doN 2 move;
turn left;
doN 3 move;

intersperse 3 (turn right; doN 2 move; turn right;) harvestRectangle;
wait 400;
turn left;
doN 7 move;
turn left;
intersperse 3 (turn right; doN 2 move; turn right;) harvestRectangle;

doN 6 move;
turn left;
move;
end;

def getJunkItem = \idx.
result <- tagmembers "junk" idx;
let totalCount = fst result in
let member = snd result in
let nextIdx = idx + 1 in

hasProhibited <- has member;
if hasProhibited {
return $ inr member;
} {
if (nextIdx < totalCount) {
getJunkItem nextIdx;
} {
return $ inl ();
}
}
end;

def tryPlace = \item.
try {
place item;
} {};
end;

/**
Precondition: facing north in lower-left corner of enclosure
Navigates a serpentine pattern through the space to
place items.
*/
def placeSerpentine = \placeFunc.
placeFunc;

move;
placeFunc;

turn right;

move;
placeFunc;

turn right;

move;
placeFunc;

turn left;

move;
placeFunc;

turn left;

move;
placeFunc;

end;

def returnToCorner =
turn back;
move; move;
turn right;
move; move;
turn right;
end;

def unloadTrash =
try {
placeSerpentine (
item <- getJunkItem 0;
case item (\_. fail "done") (\item. place item);
);
watch down;
wait 1000;

// Go back to corner
turn back;
move;
turn right;
move; move;
turn right;

wait 50;
unloadTrash;
} {};
end;

def burnTires =
hasCarTire <- has "car tire";
if hasCarTire {

intersperse 2 move $ (
placeSerpentine $ tryPlace "car tire";
returnToCorner;
ignite forward;

turn right;
move; move;
turn right;
);

wait 60;
move;

burnTires;
} {}
end;

def disposeTrash =
turn back;
doN 5 move;
turn left;
doN 11 move;
turn left;
move;

burnTires;

unloadTrash;
end;

def go =
harvestIngredients;
let rod = "fishing tackle" in
make rod;
equip rod;
doN 16 $ catchFish rod;
disposeTrash;
end;

go;
Loading

0 comments on commit 95a9014

Please sign in to comment.