Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Whack-a-mole scenario #1026

Merged
merged 4 commits into from
Mar 25, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
removed teleporting
  • Loading branch information
kostmo committed Mar 25, 2023
commit be4b475311a4830890021d46f9cc27d5b0d1043f
89 changes: 36 additions & 53 deletions data/scenarios/Challenges/_gopher/gopher.sw
Original file line number Diff line number Diff line change
Expand Up @@ -13,81 +13,60 @@ def randSign = \x.
}
end;

def converge = \dest. \currentLoc.
def goDir = \dist. \d1. \d2.
if (dist < 0) {
turn d1;
} $ elif (dist > 0) {
turn d2;
} {};

let xDist = fst currentLoc - fst dest in
let yDist = snd currentLoc - snd dest in
doN (abs dist) move;
end;

def randSwap = \f. \g.
r <- random 2;
if (r == 0) {
f; g;
} {
g; f;
};
end;

def converge = \dest. \currentLoc.
let xDist = fst currentLoc - fst dest in
let yDist = snd currentLoc - snd dest in
randSwap (goDir xDist east west) (goDir yDist north south);
end;

if (xDist < 0) {
turn east;
} {
if (xDist > 0) {
turn west;
} {};
};
doN (abs xDist) move;

if (yDist < 0) {
turn north;
} {
if (yDist > 0) {
turn south;
} {};
};
doN (abs yDist) move;
end;

/**
TODO: Randomly alternate between horizontal and vertical
movement.
*/
def navigateTo = \destTuple.
loc <- whereami;
converge destTuple loc;
end;

def arrive = \fieldWidth. \fieldHeight.

let leadDist = 20 in

newDestinationX <- random fieldWidth;
newDestinationYtemp <- random fieldHeight;
let newDestinationY = -newDestinationYtemp in

offsetXrand <- random $ leadDist / 2;

// The manhattan-distance of the offset
// must total some preset amount.
let offsetXunsigned = (leadDist / 2) + offsetXrand in
let offsetYunsigned = leadDist - offsetXunsigned in

offsetX <- randSign offsetXunsigned;
offsetY <- randSign offsetYunsigned;

let startX = newDestinationX + offsetX in
let startY = newDestinationY + offsetY in

teleport self (startX, startY);
navigateTo (newDestinationX, newDestinationY);
turn down;
end;

def getTauntStage = \startingAmount. \newCount.
if ((newCount * 5) / startingAmount < 1) {
return (0, "Hey, maybe we can work this out?");
(0, "Hey, maybe we can work this out?")
} $ elif ((newCount * 5) / startingAmount < 2) {
return (1, "I didn't hear no bell!");
(1, "I didn't hear no bell!")
} $ elif ((newCount * 5) / startingAmount < 3) {
return (2, "Why don't you just give up?");
(2, "Why don't you just give up?")
} $ elif ((newCount * 5) / startingAmount < 4) {
return (3, "Close one!");
(3, "Close one!")
} $ elif (newCount < startingAmount - 2) {
return (4, "OK, no more Mr. Nice Gopher!");
(4, "OK, no more Mr. Nice Gopher!")
} $ elif (newCount < startingAmount - 1) {
return (5, "Bet you can't do that again!");
(5, "Bet you can't do that again!")
} $ elif (newCount < startingAmount) {
return (6, "Beginner's luck!");
} $ else {return (7, "You'll never catch me!");};
(6, "Beginner's luck!")
} $ else {(7, "You'll never catch me!")};
end;

def waitWhileHere = \e. \remainingTime.
Expand All @@ -114,7 +93,7 @@ def waitWhileHere = \e. \remainingTime.
def go = \width. \height. \lastTauntIndex. \startingAmount. \dropping.
newCount <- count dropping;
if (newCount > 0) {
tauntStage <- getTauntStage startingAmount newCount;
let tauntStage = getTauntStage startingAmount newCount in
let tauntIndex = fst tauntStage in
if (tauntIndex != lastTauntIndex) {
say $ snd tauntStage;
Expand All @@ -139,6 +118,10 @@ def go = \width. \height. \lastTauntIndex. \startingAmount. \dropping.
return ();
};

baseloc <- as base {whereami};
teleport self baseloc;
give base "serenity";

selfdestruct;
};
end;
Expand Down
49 changes: 15 additions & 34 deletions data/scenarios/Challenges/_gopher/solution.sw
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,28 @@ def elif = \t. \then. \else. {if t then else} end
def else = \t. t end

def makeSigned = \b. \x.
if b {
return (-x);
} {
return x;
}
if b {-x} {x};
end;

def getDirection = \n.
if (n == 0) {
return forward;
forward;
} $ elif (n == 1) {
return right;
right;
} $ elif (n == 2) {
return back;
back;
} $ elif (n == 3) {
return left;
left;
} $ else {
return down;
down;
};
end;

/**
Loops forever
*/
def scanDirections = \n.
d <- getDirection n;
let d = getDirection n in
out <- scan d;
shouldContinue <- case out
(\_. return true)
Expand All @@ -55,7 +51,7 @@ def scanDirections = \n.
end;

def deploySensor =
_s <- build {scanDirections 0;};
_s <- build {scanDirections 0};
return ();
end;

Expand Down Expand Up @@ -83,25 +79,14 @@ def deployGrid = \f. \width. \height.
if (height > 0) {

let nowEven = isEven height in

offsetVal <- makeSigned nowEven $ height * 2;
extraOffset <- if nowEven {
return (-1);
} {
return 0;
};
let offsetVal = makeSigned nowEven $ height * 2 in
let extraOffset = if nowEven {-1} {0} in
deployRow f (offsetVal + extraOffset) width;

d <- if nowEven {
return right;
} {
return left;
};

let d = if nowEven {right} {left} in
turn d;
move;
turn d;

deployGrid f width $ height - 1;
} {};
end;
Expand Down Expand Up @@ -133,11 +118,7 @@ def searchToolkitRows = \width. \rowCount.
if (rowCount > 1) {

let nowEven = isEven rowCount in
d <- if nowEven {
return right;
} {
return left;
};
let d = if nowEven {right} {left} in

turn d;
move;
Expand All @@ -148,9 +129,9 @@ def searchToolkitRows = \width. \rowCount.
end;

def listenForDefeat =
// Block until the gopher says something
m <- listen;
if (m == "Argh! I give up.") {} {
// Block until the gopher is defeated
m <- has "serenity";
if m {} {
listenForDefeat;
};
end;
Expand Down
12 changes: 10 additions & 2 deletions data/scenarios/Challenges/gopher.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ objectives:
} {
return true;
}
- teaser: Recover swivels
- teaser: Recover equipment
prerequisite: defeat_gopher
hidden: true
optional: true
Expand Down Expand Up @@ -84,6 +84,7 @@ robots:
inventory:
- [50, mound]
- [1, toolkit]
- [1, serenity]
program: |
run "data/scenarios/Challenges/_gopher/gopher.sw"
entities:
Expand All @@ -94,14 +95,21 @@ entities:
description:
- An unsightly pile of dirt
properties: [known]
- name: serenity
display:
char: 's'
attr: silver
description:
- Peace of mind after having driven away the gopher.
properties: [known]
- name: swivel
display:
char: 's'
attr: gold
capabilities: [turn]
description:
- Allows a robot to "turn" but not "move".
properties: [known]
properties: [known, portable]
- name: uranium
display:
char: 'U'
Expand Down