Sign in
creature

Fish

by Potluck · version 1 · Checked for all ages

Perch and trout in rivers, ponds and the shallows, mackerel in shoals and flounder on the bottom out in deep water, swimming about wherever you go. They dart off from swimmers. Catch one with a spear or an arrow, or with a fishing rod. A furnace or a campfire cooks any of them into cooked fish.

1--!strict
2-- Fish's own tests: `potluck-server --test=fish`.
3local test = require("@potluck/test@0.9")
4local world = require("@potluck/world@0.31")
5
6local UP = vector.create(0, 1, 0)
7
8-- a stone basin of water high in the sky, away from the Green, `depth`
9-- deep: the middle of its floor
10local function pool(depth: number): vector
11 local g = world.spot("green")
12 local cx, cz, y = math.floor(g.x) + 80, math.floor(g.z), 90
13 for dx = -12, 12 do
14 for dz = -12, 12 do
15 local edge = math.abs(dx) == 12 or math.abs(dz) == 12
16 world.set_block(vector.create(cx + dx, y, cz + dz), "Stone")
17 for dy = 1, depth do
18 world.set_block(vector.create(cx + dx, y + dy, cz + dz), if edge then "Stone" else "Water")
19 end
20 end
21 test.wait(0)
22 end
23 return vector.create(cx + 0.5, y + 1, cz + 0.5)
24end
25
26local function all(): { number }
27 local out = {}
28 for _, kind in { "Perch", "Trout", "Mackerel", "Flounder" } do
29 for _, id in world.creatures(test.kind(kind)) do
30 table.insert(out, id)
31 end
32 end
33 return out
34end
35
36local function count(kind: string): number
37 return #world.creatures(test.kind(kind))
38end
39
40local function wet(id: number): boolean
41 local c = test.creature(id)
42 return c ~= nil and world.block(c.position + UP * 0.1) == "Water"
43end
44
45test.case("deep water brings mackerel and flounder, and they stay in it", function()
46 local p = test.player("Ada")
47 local floor = pool(7)
48 test.move(p, floor + vector.create(0, 8, 0))
49 test.wait(12)
50 test.check(count("Mackerel") + count("Flounder") >= 1, "deep water fish came")
51 test.same(count("Perch") + count("Trout"), 0, "and no shallows fish")
52 test.wait(20)
53 local fish = all()
54 test.check(#fish >= 1, "they're still here")
55 for _, id in fish do
56 test.check(wet(id), "every one is still in the water")
57 end
58end)
59
60test.case("shallow water brings perch and trout", function()
61 local p = test.player("Ada")
62 local floor = pool(2)
63 test.move(p, floor + vector.create(0, 3, 0))
64 test.wait(12)
65 test.check(count("Perch") + count("Trout") >= 1, "shallows fish came")
66 test.same(count("Mackerel") + count("Flounder"), 0, "and no deep water fish")
67end)
68
69test.case("a hand only scares one off; a spear catches it", function()
70 local p = test.player("Ada", { survival = true, carrying = { Spear = 1 } })
71 local floor = pool(3)
72 test.move(p, floor + vector.create(2, 3, 0))
73 local id = world.spawn(test.kind("Trout"), floor + vector.create(0, 1, 0))
74 test.wait(0.2)
75 test.hold(p, nil)
76 test.hit(p, test.creature(id))
77 test.check(test.creature(id) ~= nil, "still swimming")
78 test.hold(p, "Spear")
79 test.hit(p, test.creature(id))
80 test.same(test.creature(id), nil, "caught")
81 test.same(world.has(p, "Trout"), 1, "a trout")
82end)
83
84test.case("swimming close sends them darting off", function()
85 local p = test.player("Ada")
86 local floor = pool(4)
87 local id = world.spawn(test.kind("Perch"), floor + vector.create(0, 1.5, 0))
88 test.wait(0.5)
89 local c = test.creature(id)
90 test.move(p, c.position - UP * 0.8 + vector.create(1, 0, 0))
91 test.wait(2)
92 test.check(vector.magnitude(c.position - p.position) > 2.5, "it darted away")
93 test.check(wet(id), "and is still in the water")
94end)