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

spiders demo #2275

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
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
55 changes: 55 additions & 0 deletions data/scenarios/Fun/_spider/monkey.sw
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
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 placeBoulder = \border.
create border;

isEmptyHere <- isempty;
if isEmptyHere {
place border
} {
swap border;
return ();
};

return ();
end

def spinWeb = \border.
move;
turn left;

try {
intersperse 2 move $ placeBoulder border;
doN 3 (
turn left;
doN 2 (placeBoulder border; move);
);
placeBoulder border;
} {};
end

def goDir = \f. \result.
let d = fst result in
if (d == down) {
spinWeb "mountain";
} {
turn d; move;
};
f;
end;

def followRoute = \item.
nextDir <- path (inL ()) (inR item);
case nextDir return $ goDir $ followRoute item;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be simpler to just say case nextDir return goDir; followRoute item and remove the f argument from goDir?

end;

followRoute "tree";
55 changes: 55 additions & 0 deletions data/scenarios/Fun/_spider/spider.sw
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
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 placeBoulder = \border.
create border;

isEmptyHere <- isempty;
if isEmptyHere {
place border
} {
swap border;
return ();
};

return ();
end

def spinWeb = \border.
move;
turn left;

try {
intersperse 2 move $ placeBoulder border;
doN 3 (
turn left;
doN 2 (placeBoulder border; move);
);
placeBoulder border;
} {};
end

def goDir = \f. \result.
let d = fst result in
if (d == down) {
spinWeb "mountain";
} {
turn d; move;
};
f;
end;

def followRoute = \item.
nextDir <- path (inL ()) (inR item);
case nextDir return $ goDir $ followRoute item;
end;

followRoute "flower";
46 changes: 46 additions & 0 deletions data/scenarios/Fun/spider.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
version: 1
name: Spiders
description: |
Spiders catching prey by spinning webs
creative: true
robots:
- name: base
dir: north
loc: [5, 0]
- name: spider
display:
invisible: false
attr: blue
dir: north
system: true
loc: [0, 0]
program: |
run "scenarios/Fun/_spider/spider.sw"
- name: monkey
display:
invisible: false
attr: red
dir: north
system: true
loc: [0, 0]
program: |
run "scenarios/Fun/_spider/monkey.sw"
structures:
- name: cocoon
recognize: [north]
structure:
mask: '.'
palette:
'b': [grass, mountain]
map: |
bbb
b.b
bbb
known: [tree]
world:
dsl: |-
overlay
[ {grass}
, mask ((hash % 200) == 0) {flower}
, mask (((hash+1) % 200) == 0) {tree}
]
4 changes: 2 additions & 2 deletions scripts/gen/preview-world-vscode.sh
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ IMG_WIDTH=200
IMG_HEIGHT=150

IMG_OUTPUT_PATH=output.png
RENDER_IMG_COMMAND="stack exec swarm-scene -- $SCENARIO_PATH --fail-blank --dest $IMG_OUTPUT_PATH --png --width $IMG_WIDTH --height $IMG_HEIGHT"
RENDER_IMG_COMMAND="cabal run swarm-scene -- $SCENARIO_PATH --fail-blank --dest $IMG_OUTPUT_PATH --png --width $IMG_WIDTH --height $IMG_HEIGHT"

stack build --fast swarm:swarm-scene
cabal build -j -O0 swarm:swarm-scene

$RENDER_IMG_COMMAND
code --reuse-window $SCENARIO_PATH && code --reuse-window $IMG_OUTPUT_PATH
Expand Down
Loading