Wildlife and nature: animals, beehives, bushes, flowers, rocks, logs
Deploy web Studio / deploy (push) Successful in 1m5s

New stdlib module wildlife.rhai:
- Animals (new palette category): Deer (stag with antlers, does, fawns),
  Wild boar, Fox, Elephant (bull with tusks, cow, calf), Rabbits. Low-poly
  solids from faces (loft, tube), parts drawn back to front by depth, faces
  edged in their own shade (class beast). Herds scatter round a spot, each
  animal its own sprite facing about the object's rotation. Grazing
  animals switch between two head poses (iso-pose2 groups), elephants curl
  their trunks, rabbits hop.
- Birds and Butterflies as flocks: swim() takes a shape (bird, fly, bee);
  wings flap out of step. Fish output unchanged.
- Beehives on stands with supers, lids, entrances and bees flying round;
  Bush (round, flowering, berries); Flowers (bed in rows or meadow); Rocks;
  Log or stump. g-meadow and g-soil ground materials.
- polyline() takes 3D points as documented (antlers).

Theme colours for dark and light, viewer CSS for poses, flapping, hopping.
examples/country: elephants in a paddock with a cut corner, deer at a
forest edge, boar, fox, rabbits, an apiary by a meadow, a pond, a lane,
fence and hedgerow at angles; Country demo in the web Studio and deploy.
MCP guidance, README, SCRIPTING, ROADMAP.

Tests: Rust 78, Studio 122; existing examples draw the same (only the
embedded theme CSS grew).
This commit is contained in:
grabowski committed 2026-10-25 13:57:23 +07:00
1 parent 96d472625f
commit e840d36000
22 files changed
+1211 -16

No files matched your search

+1
View File
@@ -470,6 +470,7 @@ pub fn register_sprite(e: &mut Engine) {
.get("cls")
.map(|v| v.to_string())
.unwrap_or_else(|| "koi".into()),
shape: m.get("shape").map(|v| v.to_string()).unwrap_or_default(),
});
}
s.0.borrow_mut().swim(n(&x)?, n(&y)?, n(&z)?, fish);
+24 -6
View File
@@ -90,13 +90,31 @@ pub fn polys3(d: &Dynamic) -> RR<Vec<Vec<P3>>> {
}
}
/// One line or several, of plan points `[x, y]` (put at height z) or 3D points.
/// One line or several, of plan points `[x, y]` (put at height z) or 3D
/// points `[x, y, z]` (antlers, branches).
pub fn polys3_or_flat(d: &Dynamic, z: f64) -> RR<Vec<Vec<P3>>> {
polys(d).map(|ls| {
ls.into_iter()
.map(|l| l.into_iter().map(|p| [p[0], p[1], z]).collect())
.collect()
})
let a = arr(d, "polyline")?;
let lines: Vec<&Dynamic> = match a.first() {
None => return Ok(vec![]),
Some(f) if is_point(f) => vec![d],
_ => a.iter().collect(),
};
lines
.into_iter()
.map(|l| {
arr(l, "polyline")?
.iter()
.map(|q| {
let n = q.read_lock::<Array>().map(|v| v.len()).unwrap_or(0);
if n >= 3 {
p3(q)
} else {
p2(q).map(|p| [p[0], p[1], z])
}
})
.collect()
})
.collect()
}
/// `[[a, b, c, d], ...]` (segments, opening lists)
+1
View File
@@ -9,6 +9,7 @@ pub const MODULES: &[(&str, &str)] = &[
("isokit/util", include_str!("../stdlib/util.rhai")),
("isokit/ground", include_str!("../stdlib/ground.rhai")),
("isokit/streets", include_str!("../stdlib/streets.rhai")),
("isokit/wildlife", include_str!("../stdlib/wildlife.rhai")),
("isokit/nature", include_str!("../stdlib/nature.rhai")),
("isokit/building", include_str!("../stdlib/building.rhai")),
("isokit/vehicles", include_str!("../stdlib/vehicles.rhai")),
+95
View File
@@ -858,6 +858,101 @@ fn piece(id: &str, kind: &str, a: [f64; 2], b: [f64; 2], props: Value) -> Object
o
}
#[test]
fn wildlife_and_garden_prefabs_draw_from_every_side() {
let r = Runtime::new(&Bundle::default()).unwrap();
let doc = Doc {
objects: vec![
obj("deer", "deer", -18.0, 8.0, json!({"n": 5, "r": 4})),
obj(
"stag",
"deer",
-8.0,
12.0,
json!({"n": 1, "mix": "stag", "graze": false}),
),
obj("boar", "boar", -18.0, -6.0, json!({"n": 3})),
obj("fox", "fox", -8.0, -2.0, json!({})),
obj(
"el",
"elephant",
6.0,
10.0,
json!({"n": 3, "mix": "herd", "r": 5}),
),
obj("rab", "rabbits", -4.0, -12.0, json!({"n": 4})),
obj("birds", "birds", 0.0, 0.0, json!({"n": 7})),
obj("bf", "butterflies", 10.0, -12.0, json!({"n": 5})),
obj("hives", "beehive", 16.0, -6.0, json!({"n": 4})),
obj("bed", "flowers", 10.0, -12.0, json!({"w": 5, "d": 3})),
obj(
"mead",
"flowers",
20.0,
-14.0,
json!({"w": 8, "d": 5, "kind": "meadow"}),
),
obj("b1", "bush", 3.0, -4.0, json!({})),
obj("b2", "bush", 5.0, -6.0, json!({"kind": "berries"})),
obj("rk", "rocks", 22.0, 6.0, json!({"n": 4})),
obj("lg", "log", 14.0, 4.0, json!({})),
obj("st", "log", 18.0, 2.0, json!({"kind": "stump"})),
],
..Default::default()
};
for view in 0..4u8 {
let b = r.build("site", view, &cfg(), &doc).unwrap();
assert!(b.scene.warnings.is_empty(), "{:?}", b.scene.warnings);
let out = render(&b.scene, view, &RenderOpts::default());
assert!(!out.html.contains("NaN"));
// each animal its own sprite: five deer, three boar, one fox, three elephants, four rabbits
for (id, n) in [
("deer", 5),
("stag", 1),
("boar", 3),
("fox", 1),
("el", 3),
("rab", 4),
] {
assert_eq!(sprites_of(&b, id), n, "{id}");
}
// grazing animals carry two head poses taking turns, the first shown unanimated;
// the standing stag and the rabbits have none
assert_eq!(
out.html.matches("iso-a iso-pose2 iso-pose0").count(),
5 + 3 + 1 + 3
);
assert_eq!(out.html.matches("iso-a iso-pose2\"").count(), 5 + 3 + 1 + 3);
assert_eq!(out.html.matches("iso-a iso-hop").count(), 4);
// birds flap, butterflies flutter, bees fly round the hives
assert_eq!(out.html.matches("iso-a iso-flap").count(), 7);
assert_eq!(out.html.matches("iso-a iso-flutter").count(), 5);
assert_eq!(out.html.matches("class=\"iso-bee iso-bee\"").count(), 4 * 8);
// animal faces are edged in their own shade; antlers on the one stag
assert!(out.html.contains("iso-deer iso-beast"));
assert!(out.html.contains("iso-antler"));
}
let b = r.build("site", 0, &cfg(), &doc).unwrap();
// a stag stands about 1.5 m at the head, antlers up to 2.1 m; it fits its sort box
let st = b.boxes["stag"];
assert!(st[5] > 1.9 && st[5] < 2.3, "{st:?}");
// an elephant bull: about 3 m tall at the head
let el = b.boxes["el"];
assert!(el[5] > 2.6 && el[5] < 3.3, "{el:?}");
let out = render(&b.scene, 0, &RenderOpts::default());
let row = |id: &str, k: &str| {
out.info[id]
.rows
.iter()
.find(|r| r.0 == k)
.map(|r| r.1.clone())
.unwrap_or_default()
};
assert_eq!(row("deer", "Herd"), "1 stag, 3 does, 1 fawn");
assert_eq!(row("el", "Herd"), "1 bull, 1 cow, 1 calf");
assert_eq!(row("hives", "Apiary"), "4 hives, 2 supers each");
}
#[test]
fn diagonal_walls_fences_and_hedges_mitre_and_sort() {
let r = Runtime::new(&Bundle::default()).unwrap();
+1 -1
View File
@@ -3,7 +3,7 @@
// document order (later objects lie on top).
fn ground_materials() {
["g-grass", "g-grass2", "g-green", "g-garden", "g-asph", "g-conc", "g-pad", "g-path", "g-sand", "g-court", "dirt"]
["g-grass", "g-grass2", "g-green", "g-garden", "g-asph", "g-conc", "g-pad", "g-path", "g-sand", "g-court", "g-meadow", "g-soil", "dirt"]
}
fn meta_site() {
+698
View File
@@ -0,0 +1,698 @@
// isokit/wildlife: animals (deer, wild boar, fox, elephant, rabbits, a flock
// of birds, butterflies) and beehives, bushes, flowers, rocks and logs.
//
// Animals are low-poly solids drawn face by face (`face`: the engine culls
// the faces turned away and shades the others), their parts back to front by
// depth, so they read right from every side and at any rotation. Herds stand
// at scattered spots with their own headings, each animal its own sprite.
// Grazing animals take turns between two head poses (a CSS flipbook, paused
// like every animation); birds, butterflies and bees fly round on orbits.
fn animal_kinds() { ["deer", "boar", "fox", "elephant", "rabbits"] }
// ---------------------------------------------------------------- vectors
fn vadd(a, b) { [a[0] + b[0], a[1] + b[1], a[2] + b[2]] }
fn vsub(a, b) { [a[0] - b[0], a[1] - b[1], a[2] - b[2]] }
fn vmul(a, k) { [a[0] * k, a[1] * k, a[2] * k] }
fn vdot(a, b) { a[0] * b[0] + a[1] * b[1] + a[2] * b[2] }
fn vcross(a, b) { [a[1] * b[2] - a[2] * b[1], a[2] * b[0] - a[0] * b[2], a[0] * b[1] - a[1] * b[0]] }
fn vunit(a) {
let l = fmax((a[0] * a[0] + a[1] * a[1] + a[2] * a[2]).sqrt(), 1e-12);
[a[0] / l, a[1] / l, a[2] / l]
}
// ---------------------------------------------------------------- solids
/// A loft along `spine` (3D points of this frame) with n sides: rings of
/// radii rs (each a number or [across, up]: an oval), side faces between
/// them and a cap at each end; mats: [sides, first cap, last cap]. Bodies,
/// limbs, necks, heads, trunks, logs. The rings keep their turn along a bent
/// spine (parallel transport), so trunks and tails do not twist.
fn loft(spine, rs, n, mats) {
let m = spine.len();
let rings = [];
let v1 = ();
for i in 0..m {
let a = spine[if i == 0 { 0 } else { i - 1 }];
let b = spine[if i == m - 1 { m - 1 } else { i + 1 }];
let u = vunit(vsub(b, a));
if type_of(v1) == "()" {
v1 = if u[2].abs() > 0.95 { vunit(vcross([0.0, 1.0, 0.0], u)) } else { vunit(vcross([0.0, 0.0, 1.0], u)) };
} else {
v1 = vunit(vsub(v1, vmul(u, vdot(v1, u))));
}
let v2 = vcross(u, v1);
let r = rs[i];
let rw = if type_of(r) == "array" { r[0] } else { r };
let rh = if type_of(r) == "array" { r[1] } else { r };
let ring = [];
for k in 0..n {
let t = 2.0 * PI() * (k + 0.5) / n;
ring.push(vadd(spine[i], vadd(vmul(v1, rw * t.cos()), vmul(v2, rh * t.sin()))));
}
rings.push(ring);
}
for i in 1..m {
let p = rings[i - 1];
let q = rings[i];
for k in 0..n {
let j = (k + 1) % n;
this.face([p[k], p[j], q[j], q[k]], mats[0]);
}
}
let c0 = rings[0];
c0.reverse();
this.face(c0, mats[1]);
this.face(rings[m - 1], mats[2]);
this
}
/// A tapered tube from a to b, radii ra and rb, n sides.
fn tube(a, b, ra, rb, n, mat) { this.loft([a, b], [ra, rb], n, [mat, mat, mat]) }
// Parts of a creature, drawn back to front: #{ at, t, ... } with t "loft"
// (spine, rs, n, mats), "face" (pts, mat), "line" (pts, cls) or "alt" (two
// part lists shown in turn: a, b, css).
fn p_loft(spine, rs, n, mats) { #{ at: spine[spine.len() / 2], t: "loft", spine: spine, rs: rs, n: n, mats: mats } }
fn p_tube(a, b, ra, rb, n, mat) { #{ at: vmul(vadd(a, b), 0.5), t: "loft", spine: [a, b], rs: [ra, rb], n: n, mats: [mat, mat, mat] } }
fn p_face(pts, mat) { #{ at: pts[0], t: "face", pts: pts, mat: mat } }
fn p_line(pts, cls) { #{ at: pts[pts.len() / 2], t: "line", pts: pts, cls: cls } }
/// The depth of a part's anchor: nearer to the viewer is larger (plan depth
/// plus height: the view looks down along (1, 1, 1) in view space).
fn part_key(p) { depth(p.at[0], p.at[1]) + p.at[2] }
/// Draws parts back to front.
fn draw_parts(parts) {
let ps = parts.map(|p| #{ k: part_key(p), p: p });
ps.sort(|a, b| if a.k < b.k { -1 } else if a.k > b.k { 1 } else { 0 });
for e in ps {
let p = e.p;
if p.t == "loft" {
this.loft(p.spine, p.rs, p.n, p.mats.map(|m| m + " beast"));
} else if p.t == "face" {
this.face(p.pts, p.mat + " beast", #{ two_sided: true });
} else if p.t == "line" {
this.polyline(p.pts, 0.0, p.cls);
} else if p.t == "alt" {
this.begin("a pose2 pose0", p.css);
this.draw_parts(p.a);
this.end();
this.begin("a pose2", p.css);
this.draw_parts(p.b);
this.end();
}
}
this
}
/// A point of a head: `a` along its way f, `b` to its left, `c` up from it,
/// from its base (f points from the neck to the snout, in the x-z plane).
fn hp(base, f, a, b, c) {
let w = [-f[2], 0.0, f[0]];
vadd(base, vadd(vmul(f, a), vadd([0.0, b, 0.0], vmul(w, c))))
}
/// Scale every point of a part list by s (fawns, calves).
fn scale_parts(parts, s) {
parts.map(|p| {
let q = p;
q.at = vmul(p.at, s);
if p.t == "loft" {
q.spine = p.spine.map(|v| vmul(v, s));
q.rs = p.rs.map(|r| if type_of(r) == "array" { [r[0] * s, r[1] * s] } else { r * s });
} else if p.t == "face" || p.t == "line" {
q.pts = p.pts.map(|v| vmul(v, s));
} else if p.t == "alt" {
q.a = scale_parts(p.a, s);
q.b = scale_parts(p.b, s);
}
q
})
}
/// Four legs from the underside of a body to the ground: at x fx (front)
/// and bx (back), y = +-y, from height z0, radii r0 at the top, r1 at the hoof.
fn legs(fx, bx, y, z0, r0, r1, mat) {
let out = [];
for x in [fx, bx] {
for s in [1.0, -1.0] {
out.push(p_tube([x, s * y, z0], [x + 0.02 * (if x > 0.0 { 1.0 } else { -1.0 }), s * y, 0.0], r0, r1, 4, mat));
}
}
out
}
/// The parts of one animal of `kind` facing +x at (0, 0), and the head
/// poses that take turns when it grazes: #{ body: [...], up: [...], down: [...] }.
fn animal_parts(kind, sub) {
if kind == "deer" { return deer_parts(sub); }
if kind == "boar" { return boar_parts(); }
if kind == "fox" { return fox_parts(); }
if kind == "elephant" { return elephant_parts(sub); }
rabbit_parts()
}
/// A red or roe deer: sub "stag" (antlers), "doe" or "fawn" (drawn smaller).
fn deer_parts(sub) {
let body = [
p_loft([[-0.62, 0.0, 0.99], [-0.55, 0.0, 0.97], [-0.3, 0.0, 0.95], [0.15, 0.0, 0.96], [0.5, 0.0, 1.0], [0.57, 0.0, 1.02]],
[[0.08, 0.1], [0.15, 0.18], [0.19, 0.23], [0.2, 0.24], [0.17, 0.22], [0.1, 0.14]], 8, ["deer", "deerl", "deer"]),
p_tube([-0.55, 0.0, 1.05], [-0.64, 0.0, 0.94], 0.04, 0.03, 4, "deerl"),
];
body += legs(0.38, -0.4, 0.1, 0.8, 0.045, 0.022, "deer");
let head = |base, f| {
let out = [
p_tube(base, hp(base, f, 0.27, 0.0, -0.02), [0.07, 0.08], [0.033, 0.04], 6, "deer"),
p_tube(hp(base, f, 0.25, 0.0, -0.02), hp(base, f, 0.29, 0.0, -0.025), 0.03, 0.022, 4, "nose"),
];
for s in [1.0, -1.0] {
out.push(p_face([hp(base, f, 0.03, s * 0.05, 0.05), hp(base, f, -0.03, s * 0.05, 0.04), hp(base, f, -0.04, s * 0.18, 0.15)], "deer"));
if sub == "stag" {
let a = hp(base, f, -0.01, s * 0.04, 0.07);
let b = hp(base, f, -0.08, s * 0.16, 0.3);
let c = hp(base, f, -0.14, s * 0.22, 0.5);
out.push(p_line([a, b, c, hp(base, f, -0.1, s * 0.3, 0.64)], "antler"));
out.push(p_line([b, hp(base, f, 0.06, s * 0.2, 0.4)], "antler"));
out.push(p_line([c, hp(base, f, -0.02, s * 0.32, 0.58)], "antler"));
}
}
out
};
let up = [p_tube([0.45, 0.0, 1.08], [0.72, 0.0, 1.45], 0.085, 0.06, 6, "deer")] + head.call([0.72, 0.0, 1.47], vunit([0.95, 0.0, -0.3]));
let down = [p_tube([0.47, 0.0, 1.02], [0.8, 0.0, 0.48], 0.085, 0.06, 6, "deer")] + head.call([0.8, 0.0, 0.47], vunit([0.35, 0.0, -0.94]));
let r = #{ body: body, up: up, down: down };
if sub == "fawn" {
r.body = scale_parts(r.body, 0.6);
r.up = scale_parts(r.up, 0.6);
r.down = scale_parts(r.down, 0.6);
}
r
}
/// A wild boar: a heavy front, a long snout, small tusks; it roots with its snout.
fn boar_parts() {
let body = [
p_loft([[-0.66, 0.0, 0.58], [-0.58, 0.0, 0.58], [-0.3, 0.0, 0.6], [0.15, 0.0, 0.64], [0.5, 0.0, 0.66], [0.58, 0.0, 0.67]],
[[0.09, 0.11], [0.17, 0.2], [0.23, 0.27], [0.26, 0.33], [0.24, 0.3], [0.16, 0.2]], 8, ["boar", "boar", "boar"]),
p_tube([-0.58, 0.0, 0.66], [-0.68, 0.0, 0.5], 0.025, 0.015, 4, "boar"),
p_line([[-0.45, 0.0, 0.86], [0.0, 0.0, 0.96], [0.45, 0.0, 0.98]], "bristle"),
];
body += legs(0.36, -0.4, 0.12, 0.42, 0.06, 0.035, "boar");
let head = |base, f| {
let out = [
p_tube(base, hp(base, f, 0.42, 0.0, -0.04), [0.17, 0.19], [0.06, 0.065], 6, "boar"),
p_tube(hp(base, f, 0.41, 0.0, -0.04), hp(base, f, 0.45, 0.0, -0.045), 0.06, 0.055, 6, "nose"),
];
for s in [1.0, -1.0] {
out.push(p_face([hp(base, f, 0.04, s * 0.09, 0.12), hp(base, f, -0.04, s * 0.08, 0.1), hp(base, f, -0.02, s * 0.15, 0.24)], "boar"));
out.push(p_tube(hp(base, f, 0.32, s * 0.06, -0.08), hp(base, f, 0.36, s * 0.09, 0.0), 0.014, 0.008, 4, "tusk"));
}
out
};
let up = head.call([0.6, 0.0, 0.72], vunit([0.9, 0.0, -0.45]));
let down = head.call([0.62, 0.0, 0.62], vunit([0.55, 0.0, -0.84]));
#{ body: body, up: up, down: down }
}
/// A red fox: slim, dark stockings, big ears, a bushy tail with a white tip.
fn fox_parts() {
let body = [
p_loft([[-0.32, 0.0, 0.36], [0.0, 0.0, 0.36], [0.3, 0.0, 0.39], [0.36, 0.0, 0.41]], [[0.08, 0.09], [0.1, 0.11], [0.085, 0.1], [0.05, 0.06]], 8, ["fox", "fox", "foxw"]),
p_loft([[-0.3, 0.0, 0.38], [-0.5, 0.0, 0.33], [-0.68, 0.0, 0.26]], [0.045, 0.085, 0.07], 6, ["fox", "fox", "fox"]),
p_tube([-0.68, 0.0, 0.26], [-0.8, 0.0, 0.22], 0.07, 0.025, 6, "foxw"),
];
body += legs(0.24, -0.24, 0.05, 0.3, 0.025, 0.018, "foxd");
let head = |base, f| {
let out = [
p_tube([0.27, 0.0, 0.42], base, 0.06, 0.055, 6, "fox"),
p_tube(base, hp(base, f, 0.2, 0.0, -0.03), [0.07, 0.06], [0.014, 0.014], 6, "fox"),
p_tube(hp(base, f, 0.18, 0.0, -0.03), hp(base, f, 0.21, 0.0, -0.035), 0.015, 0.01, 4, "nose"),
];
for s in [1.0, -1.0] {
out.push(p_face([hp(base, f, 0.04, s * 0.04, 0.05), hp(base, f, -0.04, s * 0.04, 0.04), hp(base, f, -0.01, s * 0.08, 0.16)], "fox"));
}
out
};
let up = head.call([0.4, 0.0, 0.5], vunit([0.95, 0.0, -0.25]));
let down = head.call([0.42, 0.0, 0.38], vunit([0.5, 0.0, -0.86]));
#{ body: body, up: up, down: down }
}
/// An Asian elephant: sub "bull" has tusks. Its trunk hangs or curls up.
fn elephant_parts(sub) {
let body = [
p_loft([[-1.48, 0.0, 1.85], [-1.3, 0.0, 1.87], [-0.6, 0.0, 1.95], [0.4, 0.0, 2.05], [1.15, 0.0, 2.0], [1.3, 0.0, 2.02]],
[[0.3, 0.38], [0.58, 0.68], [0.75, 0.85], [0.78, 0.9], [0.66, 0.78], [0.42, 0.52]], 8, ["eleph", "eleph", "eleph"]),
p_tube([-1.3, 0.0, 2.1], [-1.4, 0.0, 1.3], 0.04, 0.025, 4, "eleph"),
];
body += legs(0.8, -0.85, 0.34, 1.3, 0.24, 0.21, "eleph");
let base = [1.2, 0.0, 2.25];
let f = vunit([0.6, 0.0, -0.8]);
let head = [p_tube(base, hp(base, f, 0.62, 0.0, 0.0), [0.46, 0.52], [0.3, 0.3], 8, "eleph")];
for s in [1.0, -1.0] {
head.push(p_face([[1.3, s * 0.5, 2.6], [1.02, s * 0.86, 2.52], [0.86, s * 0.98, 2.0], [0.98, s * 0.88, 1.45], [1.26, s * 0.56, 1.62]], "elephe"));
if sub == "bull" {
head.push(p_loft([[1.62, s * 0.2, 1.62], [1.85, s * 0.24, 1.42], [2.08, s * 0.22, 1.42]], [0.055, 0.045, 0.025], 5, ["tusk", "tusk", "tusk"]));
}
}
let t0 = hp(base, f, 0.6, 0.0, 0.0);
let hang = [p_loft([t0, [1.92, 0.0, 1.15], [1.98, 0.0, 0.7], [1.93, 0.0, 0.25]], [0.17, 0.13, 0.1, 0.07], 6, ["eleph", "eleph", "eleph"])];
let curl = [p_loft([t0, [2.0, 0.0, 1.3], [2.2, 0.0, 1.25], [2.28, 0.0, 1.5]], [0.17, 0.13, 0.1, 0.07], 6, ["eleph", "eleph", "eleph"])];
let r = #{ body: body + head, up: hang, down: curl };
if sub == "calf" {
r.body = scale_parts(r.body, 0.55);
r.up = scale_parts(r.up, 0.55);
r.down = scale_parts(r.down, 0.55);
}
r
}
/// A rabbit sitting up, long ears, a white scut.
fn rabbit_parts() {
let body = [
p_loft([[-0.13, 0.0, 0.11], [0.0, 0.0, 0.12], [0.1, 0.0, 0.15]], [[0.075, 0.08], [0.095, 0.1], [0.07, 0.08]], 6, ["rabbit", "rabbit", "rabbit"]),
p_tube([-0.13, 0.0, 0.13], [-0.17, 0.0, 0.12], 0.035, 0.02, 4, "foxw"),
p_tube([0.1, 0.0, 0.2], [0.2, 0.0, 0.17], [0.05, 0.055], [0.025, 0.03], 6, "rabbit"),
];
for s in [1.0, -1.0] {
body.push(p_tube([0.11, s * 0.025, 0.24], [0.05, s * 0.05, 0.37], [0.012, 0.026], [0.008, 0.016], 4, "rabbit"));
}
#{ body: body, up: [], down: [] }
}
/// Draws one animal of `kind` (sub-kind sub) in this sprite's frame;
/// `graze`: its head poses take turns every `cycle` seconds, `delay` in.
fn animal(kind, sub, graze, cycle, delay) {
let a = animal_parts(kind, sub);
let parts = a.body;
if a.up.len() > 0 {
if graze {
let at = a.up[0].at;
parts.push(#{ at: at, t: "alt", a: a.down, b: a.up, css: `animation-duration:${cycle}s;animation-delay:-${delay}s` });
} else {
parts += a.up;
}
}
this.draw_parts(parts)
}
/// The sub-kinds of a herd of `kind`, `mix` as picked in its props.
fn herd_subs(kind, mix, n) {
let out = [];
for i in 0..n {
out.push(if kind == "deer" {
if mix == "herd" { if i == 0 { "stag" } else if i % 3 == 2 { "fawn" } else { "doe" } } else { mix }
} else if kind == "elephant" {
if mix == "herd" { if i == 0 { "bull" } else if i % 3 == 2 { "calf" } else { "cow" } } else { mix }
} else { "" });
}
out
}
/// A herd (or one animal) of `kind`: n animals at scattered spots within r
/// of the object's position, facing its rotation give or take 70 degrees.
fn herd(o, kind, mix, graze) {
let p = o.p;
let n = p.n;
let subs = herd_subs(kind, mix, n);
let seed = o.x * 0.731 + o.y * 1.379;
let cycle = if kind == "elephant" { 7.0 } else { 9.0 };
for i in 0..n {
// a sunflower spread: even, not in rows
let rr = if n == 1 { 0.0 } else { p.r * ((i + 0.5) / n).sqrt() };
let ang = i * 2.39996 + hash(seed + i) * 0.6;
let lx = rr * ang.cos();
let ly = rr * ang.sin();
let w = world_of(o, lx, ly);
let turn = if n == 1 { 0.0 } else { (hash(seed + i * 3.7) - 0.5) * 140.0 };
frame(w[0], w[1], o.z, o.rot + turn);
let s = sprite();
if kind == "rabbits" && p.hop {
s.begin("a hop", `animation-delay:-${round1(hash(seed + i * 5.1) * 3.0)}s`);
s.animal(kind, subs[i], false, cycle, 0.0);
s.end();
} else {
s.animal(kind, subs[i], graze, cycle, round1(hash(seed + i * 2.3) * cycle));
}
}
frame(o);
}
fn herd_props(n, r, rmax) {
[
#{ key: "n", type: "int", def: n, min: 1, max: 12, step: 1, label: "How many" },
#{ key: "r", type: "float", def: r, min: 0.5, max: rmax, step: 0.5, label: "Spread (m)" },
]
}
fn meta_deer() {
#{ label: "Deer", category: "Animals", icon: "🦌",
help: "A herd of deer grazing: a stag with antlers, does and fawns (or all of one kind), at scattered spots facing about the object's rotation. Grazing deer lower and raise their heads in turn.",
props: herd_props(4, 4.0, 30.0) + [
#{ key: "mix", type: "choice", def: "herd", choices: ["herd", "stag", "doe", "fawn"], label: "Kind" },
#{ key: "graze", type: "bool", def: true, label: "Grazing" },
] }
}
fn prefab_deer(o) {
herd(o, "deer", o.p.mix, o.p.graze);
let subs = herd_subs("deer", o.p.mix, o.p.n);
let c = |k| subs.filter(|s| s == k).len();
info_row("Herd", herd_text(c, [["stag", "stags"], ["doe", "does"], ["fawn", "fawns"]]));
}
/// "1 stag, 2 does, 1 fawn" from counts per sub-kind.
fn herd_text(c, names) {
let parts = [];
for nm in names {
let k = c.call(nm[0]);
if k > 0 { parts.push(`${k} ${if k == 1 { nm[0] } else { nm[1] }}`); }
}
parts.reduce(|s, x| if s == "" { x } else { s + ", " + x }, "")
}
fn meta_boar() {
#{ label: "Wild boar", category: "Animals", icon: "🐗",
help: "Wild boar rooting about: they push their snouts into the ground and look up in turn.",
props: herd_props(3, 3.0, 20.0) + [ #{ key: "graze", type: "bool", def: true, label: "Rooting" } ] }
}
fn prefab_boar(o) {
herd(o, "boar", "", o.p.graze);
info_row("Animals", `${o.p.n} wild boar`);
}
fn meta_fox() {
#{ label: "Fox", category: "Animals", icon: "🦊",
help: "A red fox (or a few), nosing about now and then.",
props: herd_props(1, 3.0, 20.0) + [ #{ key: "graze", type: "bool", def: true, label: "Sniffing" } ] }
}
fn prefab_fox(o) {
herd(o, "fox", "", o.p.graze);
info_row("Animals", `${o.p.n} fox${if o.p.n == 1 { "" } else { "es" }}`);
}
fn meta_elephant() {
#{ label: "Elephant", category: "Animals", icon: "🐘",
help: "Asian elephants: a bull with tusks, cows and calves (or all of one kind); their trunks hang and curl up in turn.",
props: herd_props(1, 6.0, 40.0) + [
#{ key: "mix", type: "choice", def: "bull", choices: ["herd", "bull", "cow", "calf"], label: "Kind" },
#{ key: "graze", type: "bool", def: true, label: "Trunk moves" },
] }
}
fn prefab_elephant(o) {
herd(o, "elephant", o.p.mix, o.p.graze);
let subs = herd_subs("elephant", o.p.mix, o.p.n);
let c = |k| subs.filter(|s| s == k).len();
info_row("Herd", herd_text(c, [["bull", "bulls"], ["cow", "cows"], ["calf", "calves"]]));
}
fn meta_rabbits() {
#{ label: "Rabbits", category: "Animals", icon: "🐇",
help: "Rabbits sitting about a meadow, hopping now and then.",
props: herd_props(4, 2.0, 15.0) + [ #{ key: "hop", type: "bool", def: true, label: "Hopping" } ] }
}
fn prefab_rabbits(o) {
herd(o, "rabbits", "", false);
info_row("Animals", `${o.p.n} rabbit${if o.p.n == 1 { "" } else { "s" }}`);
}
// ---------------------------------------------------------------- flocks
fn meta_birds() {
#{ label: "Birds", category: "Animals", icon: "🕊",
help: "A flock of birds circling at a height over the spot, wings flapping.",
props: [
#{ key: "n", type: "int", def: 9, min: 1, max: 40, step: 1, label: "How many" },
#{ key: "h", type: "float", def: 14.0, min: 3.0, max: 80.0, step: 0.5, label: "Height (m)" },
#{ key: "r", type: "float", def: 8.0, min: 1.0, max: 60.0, step: 0.5, label: "Circle (m)" },
#{ key: "kind", type: "choice", def: "dark", choices: ["dark", "white"], label: "Birds" },
] }
}
fn prefab_birds(o) {
let p = o.p;
let seed = o.x * 0.53 + o.y * 0.97;
let fl = [];
for i in 0..p.n {
let rr = p.r * (0.55 + 0.45 * hash(seed + i));
fl.push(#{ rx: rr, ry: rr * (0.8 + 0.2 * hash(seed + i * 1.7)), dur: 14.0 + 8.0 * hash(seed + i * 2.9), delay: 20.0 * hash(seed + i * 4.1),
cw: false, len: 0.9, cls: if p.kind == "white" { "birdw" } else { "birdd" }, shape: "bird" });
}
sprite().swim(0.0, 0.0, p.h, fl);
info_row("Flock", `${p.n} birds circling ${num1(p.h)} m up`);
}
fn meta_butterflies() {
#{ label: "Butterflies", category: "Animals", icon: "🦋",
help: "Butterflies fluttering round over flowers.",
props: [
#{ key: "n", type: "int", def: 6, min: 1, max: 30, step: 1, label: "How many" },
#{ key: "r", type: "float", def: 2.0, min: 0.3, max: 15.0, step: 0.1, label: "Circle (m)" },
#{ key: "h", type: "float", def: 0.8, min: 0.2, max: 4.0, step: 0.1, label: "Height (m)" },
] }
}
fn prefab_butterflies(o) {
let p = o.p;
let seed = o.x * 0.41 + o.y * 0.77;
let cls = ["flyo", "flyb", "flyw", "flyy"];
let low = [];
let high = [];
for i in 0..p.n {
let rr = p.r * (0.3 + 0.7 * hash(seed + i));
let b = #{ rx: rr, ry: rr * (0.6 + 0.4 * hash(seed + i * 1.3)), dur: 5.0 + 6.0 * hash(seed + i * 2.1), delay: 10.0 * hash(seed + i * 3.3),
cw: hash(seed + i * 5.9) > 0.5, len: 0.22, cls: cls[i % 4], shape: "fly" };
if i % 2 == 0 { low.push(b); } else { high.push(b); }
}
let s = sprite();
if low.len() > 0 { s.swim(0.0, 0.0, p.h, low); }
if high.len() > 0 { s.swim(0.0, 0.0, p.h + 0.4, high); }
}
// ---------------------------------------------------------------- beehives
fn hive_colours() { ["mixed", "white", "yellow", "blue", "green"] }
fn meta_beehive() {
#{ label: "Beehives", category: "Nature", icon: "🐝",
help: "A row of beehives on stands along local x, entrances to local -y, bees flying round in front.",
props: [
#{ key: "n", type: "int", def: 3, min: 1, max: 12, step: 1, label: "Hives" },
#{ key: "gap", type: "float", def: 0.9, min: 0.6, max: 4.0, step: 0.1, label: "Spacing (m)" },
#{ key: "colour", type: "choice", def: "mixed", choices: hive_colours(), label: "Paint" },
#{ key: "supers", type: "int", def: 2, min: 0, max: 4, step: 1, label: "Supers" },
#{ key: "stand", type: "bool", def: true, label: "Stands" },
#{ key: "bees", type: "bool", def: true, label: "Bees" },
] }
}
fn prefab_beehive(o) {
let p = o.p;
let x0 = -(p.n - 1) * p.gap / 2.0;
let seed = o.x * 0.29 + o.y * 0.61;
for i in 0..p.n {
let x = x0 + i * p.gap;
let col = if p.colour == "mixed" { ["hive1", "hive2", "hive3", "hive4"][i % 4] } else { switch p.colour { "yellow" => "hive2", "blue" => "hive3", "green" => "hive4", _ => "hive1" } };
let s = sprite();
let z = 0.0;
if p.stand {
for lx in [-0.2, 0.2] { for ly in [-0.17, 0.17] { s.box(x + lx - 0.025, ly - 0.025, x + lx + 0.025, ly + 0.025, 0.0, 0.33, "woodd"); } }
s.box(x - 0.27, -0.24, x + 0.27, 0.24, 0.33, 0.37, "wood");
z = 0.37;
}
// brood box with its entrance and landing board, the supers, the lid
s.box(x - 0.25, -0.21, x + 0.25, 0.21, z, z + 0.25, col);
s.box(x - 0.18, -0.29, x + 0.18, -0.21, z, z + 0.02, "wood");
s.open([x - 0.25, -0.21], [x + 0.25, -0.21], [[0.1, 0.4, 0.02, 0.045]], "hivedoor");
let zt = z + 0.25;
for k in 0..p.supers {
s.box(x - 0.25, -0.21, x + 0.25, 0.21, zt + 0.004, zt + 0.17, col);
zt += 0.17;
}
s.box(x - 0.29, -0.25, x + 0.29, 0.25, zt + 0.004, zt + 0.07, "hivelid");
if p.bees {
let bs = [];
for k in 0..8 {
let r = 0.25 + 0.6 * hash(seed + i * 11.0 + k);
bs.push(#{ rx: r, ry: r * 0.7, dur: 1.6 + 2.0 * hash(seed + i * 13.0 + k * 1.9), delay: 4.0 * hash(seed + i * 7.0 + k * 3.1),
cw: k % 2 == 0, len: 0.12, cls: "bee", shape: "bee" });
}
s.swim(x, -0.55, z + 0.35, bs);
}
}
info_row("Apiary", `${p.n} hive${if p.n == 1 { "" } else { "s" }}, ${p.supers} super${if p.supers == 1 { "" } else { "s" }} each`);
}
// ---------------------------------------------------------------- plants
fn flower_colours() { ["mixed", "red", "yellow", "white", "purple", "pink"] }
/// The flower classes for a colour choice.
fn flower_cls(c) {
if c == "mixed" { ["flr", "fly", "flw", "flp", "flk"] } else { [switch c { "red" => "flr", "yellow" => "fly", "white" => "flw", "purple" => "flp", _ => "flk" }] }
}
/// Small diamonds at plan points pts, height z, one path per class.
fn blossoms(pts, z, size, classes) {
let by = classes.map(|c| []);
for i in 0..pts.len() {
let q = pts[i];
let k = i % classes.len();
let d = by[k];
d.push([[q[0] - size, q[1], z], [q[0], q[1] - size, z], [q[0] + size, q[1], z], [q[0], q[1] + size, z]]);
by[k] = d;
}
for k in 0..classes.len() {
if by[k].len() > 0 { this.poly(by[k], classes[k]); }
}
this
}
fn meta_bush() {
#{ label: "Bush", category: "Nature", icon: "🌿",
help: "A shrub: round and green, in flower, or with berries.",
props: [
#{ key: "r", type: "float", def: 0.9, min: 0.3, max: 4.0, step: 0.1, label: "Size (m)" },
#{ key: "kind", type: "choice", def: "round", choices: ["round", "flowering", "berries"], label: "Kind" },
#{ key: "colour", type: "choice", def: "mixed", choices: flower_colours(), label: "Flowers" },
] }
}
fn prefab_bush(o) {
let p = o.p;
let r = p.r;
let s = sprite();
s.ext_box(-r, -r, r, r, 0.0, r * 1.5);
let R = r * px() * 0.95;
let seed = o.x * 0.37 + o.y * 0.83;
// three overlapping clumps, the back ones first, a light top
let clumps = [[-0.35 * r, 0.25 * r, 0.75], [0.3 * r, 0.3 * r, 0.8], [0.0, -0.25 * r, 0.9]];
clumps.sort(|a, b| { let da = depth(a[0], a[1]); let db = depth(b[0], b[1]); if da < db { -1 } else if da > db { 1 } else { 0 } });
for c in clumps {
s.circle(c[0], c[1], r * 0.62, R * c[2], "bush l");
}
s.circle(0.0, 0.0, r * 0.9, R * 0.55, "bush t", -R * 0.2, -R * 0.25);
if p.kind != "round" {
// blossoms or berries dotted over the crown: small, spread up and down its face
let cls = if p.kind == "berries" { ["berry"] } else { flower_cls(p.colour) };
let dot = R * (if p.kind == "berries" { 0.07 } else { 0.09 });
for i in 0..22 {
let a = i * 2.39996 + hash(seed + i);
let d = r * (0.15 + 0.5 * hash(seed + i * 1.9));
let z = r * (0.5 + 0.7 * hash(seed + i * 3.7));
s.circle(d * a.cos(), d * a.sin(), z, dot, cls[i % cls.len()]);
}
}
}
fn meta_flowers() {
#{ label: "Flowers", category: "Nature", icon: "✿", shape: "rect",
help: "A flower bed (rows in dark soil) or a wildflower meadow. Drag a rectangle on the map.",
props: [
#{ key: "w", type: "float", def: 4.0, min: 0.5, max: 200.0, step: 0.5, label: "Width (m)" },
#{ key: "d", type: "float", def: 2.0, min: 0.5, max: 200.0, step: 0.5, label: "Length (m)" },
#{ key: "kind", type: "choice", def: "bed", choices: ["bed", "meadow"], label: "Kind" },
#{ key: "colour", type: "choice", def: "mixed", choices: flower_colours(), label: "Flowers" },
#{ key: "density", type: "float", def: 3.0, min: 0.5, max: 12.0, step: 0.5, label: "Per m²" },
] }
}
fn prefab_flowers(o) {
let p = o.p;
let w = p.w / 2.0;
let d = p.d / 2.0;
layer("ground");
let s = sprite();
let bed = p.kind == "bed";
s.flat(rect(-w, -d, w, d), 0.02, if bed { "g-soil t" } else { "g-meadow t" });
let n = fmin(600.0, (p.w * p.d * p.density).round()).to_int();
let seed = o.x * 0.19 + o.y * 0.47;
let pts = [];
if bed {
// rows across the bed's length, a little uneven
let rows = fmax(1.0, (p.d / 0.45).floor()).to_int();
let per = fmax(1.0, (n.to_float() / rows.to_float()).round()).to_int();
for j in 0..rows {
let y = -d + (j + 0.5) * p.d / rows;
for i in 0..per {
let x = -w + (i + 0.5) * p.w / per;
pts.push([x + (hash(seed + i * 3.1 + j) - 0.5) * 0.15, y + (hash(seed + i + j * 5.3) - 0.5) * 0.12]);
}
}
} else {
for i in 0..n {
pts.push([-w + p.w * hash(seed + i * 1.37), -d + p.d * hash(seed + i * 2.71)]);
}
}
s.blossoms(pts.map(|q| [q[0] + 0.04, q[1] - 0.04]), 0.05, 0.07, ["leafy"]);
s.blossoms(pts, if bed { 0.32 } else { 0.22 }, 0.06, flower_cls(p.colour));
info_row("Flowers", `${pts.len()} on ${num1(p.w * p.d)} m²`);
}
// ---------------------------------------------------------------- rocks and logs
fn meta_rocks() {
#{ label: "Rocks", category: "Nature", icon: "🪨",
help: "Boulders lying about: low-poly stones of varied size.",
props: [
#{ key: "n", type: "int", def: 3, min: 1, max: 20, step: 1, label: "How many" },
#{ key: "r", type: "float", def: 0.6, min: 0.1, max: 5.0, step: 0.1, label: "Size (m)" },
#{ key: "spread", type: "float", def: 1.5, min: 0.0, max: 20.0, step: 0.1, label: "Spread (m)" },
] }
}
fn prefab_rocks(o) {
let p = o.p;
let seed = o.x * 0.67 + o.y * 0.23;
for i in 0..p.n {
let rr = if p.n == 1 { 0.0 } else { p.spread * ((i + 0.5) / p.n).sqrt() };
let a = i * 2.39996 + hash(seed + i);
let size = p.r * (0.55 + 0.6 * hash(seed + i * 4.3));
sprite().rock(rr * a.cos(), rr * a.sin(), size, seed + i * 9.1);
}
}
/// A low-poly boulder of radius r at (x, y): jittered rings from the ground
/// up, widest a little above it, and a flat-ish top.
fn rock(x, y, r, seed) {
let n = 7;
let h = r * (0.75 + 0.35 * hash(seed));
let rings = [[0.0, 0.9], [0.35, 1.0], [0.75, 0.78], [1.0, 0.4]];
let pts = [];
for ring in rings {
let q = [];
for k in 0..n {
let a = 2.0 * PI() * (k + 0.3 * hash(seed + k)) / n;
let rk = r * ring[1] * (0.85 + 0.25 * hash(seed + k * 1.7 + ring[0]));
q.push([x + rk * a.cos(), y + rk * a.sin() * 0.85, h * ring[0]]);
}
pts.push(q);
}
for i in 1..pts.len() {
let lo = pts[i - 1];
let hi = pts[i];
for k in 0..n {
let j = (k + 1) % n;
this.face([lo[k], lo[j], hi[j], hi[k]], "rock beast");
}
}
this.face(pts[pts.len() - 1], "rock beast");
this
}
fn meta_log() {
#{ label: "Log", category: "Nature", icon: "🪵",
help: "A fallen log along local x, or a tree stump.",
props: [
#{ key: "kind", type: "choice", def: "log", choices: ["log", "stump"], label: "Kind" },
#{ key: "w", type: "float", def: 3.0, min: 0.3, max: 20.0, step: 0.1, label: "Length (m)" },
#{ key: "r", type: "float", def: 0.25, min: 0.05, max: 1.5, step: 0.05, label: "Radius (m)" },
] }
}
fn prefab_log(o) {
let p = o.p;
let s = sprite();
if p.kind == "stump" {
let h = fmax(0.25, p.r * 1.6);
s.loft([[0.0, 0.0, 0.0], [0.0, 0.0, h]], [p.r * 1.15, p.r], 9, ["bark", "bark", "woodend"]);
} else {
let w = p.w / 2.0;
s.loft([[-w, 0.0, p.r * 0.92], [w, 0.0, p.r * 0.92]], [p.r, p.r * 0.9], 9, ["bark", "woodend", "woodend"]);
s.tube([w * 0.3, 0.0, p.r * 1.5], [w * 0.3 + 0.2, 0.22, p.r * 2.1], p.r * 0.25, p.r * 0.15, 5, "bark");
}
}