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: perch and trout in shallow water (rivers, ponds, the creek and the
3-- edge of the sea) and mackerel and flounder out in deep water, swimming
4-- about near players (swims, world 0.31). Perch and trout potter about on
5-- their own; mackerel swim in shoals that follow their leader; flounder lie
6-- on the bottom and only dart off when someone comes close. They shy away
7-- from swimmers. A spear (thrown, or jabbed by hand) or an arrow catches
8-- one; by hand it just darts off. Each kind is an item too ([items.*]),
9-- which the Buckets and fishing creation's rods catch, a furnace or
10-- campfire cooks into cooked fish, and Hunger eats raw at a pinch.
11
12local world = require("@potluck/world@0.33")
13local tweak = require("@potluck/tweak")
14local sound = require("@potluck/sound@0.2")
15
16local knobs = tweak {
17 how_many = { 6, min = 0, max = 12, label = "How many swim near you (0 for none)", order = 1 },
18 shy = { 3.5, min = 0, max = 8, label = "How close a swimmer comes before they dart off", order = 2 },
19}
20
21local UP = vector.create(0, 1, 0)
22local EYE, PUPIL = "#f4f2e8", "#141414"
23
24-- a fish's body, facing -z: `long` and `tall` across, its colors, and a
25-- stripe of `mark` along it at `mark_y` (nil for none)
26local function fish(long: number, tall: number, wide: number, back: string, side: string, belly: string, fin: string, mark: string?): { any }
27 local y = tall / 2 + 0.02
28 local b: { any } = {
29 { "body", size = { wide, tall * 0.6, long * 0.8 }, at = { 0, y, 0 }, color = side },
30 { "back", size = { wide * 0.8, tall * 0.25, long * 0.7 }, at = { 0, y + tall * 0.38, 0.02 }, color = back },
31 { "belly", size = { wide * 0.8, tall * 0.2, long * 0.62 }, at = { 0, y - tall * 0.36, -0.02 }, color = belly },
32 { "head", size = { wide * 0.9, tall * 0.7, long * 0.22 }, at = { 0, y, -long * 0.48 }, color = side, head = true },
33 { "dorsal", size = { 0.02, tall * 0.3, long * 0.3 }, at = { 0, y + tall * 0.6, 0.02 }, color = fin },
34 { "tail", size = { 0.02, tall * 0.9, long * 0.24 }, at = { 0, y, long * 0.5 }, color = fin, leg = true },
35 { "fin_left", size = { 0.1, 0.02, 0.1 }, at = { -wide * 0.6, y - tall * 0.15, -long * 0.18 }, color = fin, leg = true },
36 { "fin_right", size = { 0.1, 0.02, 0.1 }, at = { wide * 0.6, y - tall * 0.15, -long * 0.18 }, color = fin, leg = true },
37 }
38 if mark then
39 table.insert(b, { "stripe", size = { wide * 1.02, tall * 0.12, long * 0.74 }, at = { 0, y + tall * 0.05, 0 }, color = mark })
40 end
41 for _, s in { -1, 1 } do
42 local n = if s < 0 then "left" else "right"
43 table.insert(b, { "eye_" .. n, size = { 0.02, 0.06, 0.06 }, at = { s * wide * 0.47, y + tall * 0.1, -long * 0.52 }, color = EYE, head = true })
44 table.insert(b, { "pupil_" .. n, size = { 0.02, 0.035, 0.035 }, at = { s * (wide * 0.47 + 0.01), y + tall * 0.1, -long * 0.53 }, color = PUPIL, head = true })
45 end
46 return b
47end
48
49-- the perch's dark bars
50local function barred(b: { any }, long: number, tall: number, wide: number, bar: string): { any }
51 for i, z in { -0.2, 0, 0.2 } do
52 table.insert(b, { "bar_" .. i, size = { wide * 1.03, tall * 0.55, 0.04 }, at = { 0, tall / 2 + 0.06, z * long }, color = bar })
53 end
54 return b
55end
56
57local Perch = world.creature {
58 name = "Perch",
59 speed = 1.6,
60 count = 0,
61 home = "here",
62 swims = true,
63 size = { 0.3, 0.24, 0.3 },
64 behaviors = { wander = false },
65 body = barred(fish(0.5, 0.24, 0.12, "#5a6a28", "#b8a848", "#e8dca8", "#e0682a", nil), 0.5, 0.24, 0.12, "#3a4a20"),
66}
67
68local Trout = world.creature {
69 name = "Trout",
70 speed = 1.9,
71 count = 0,
72 home = "here",
73 swims = true,
74 size = { 0.3, 0.22, 0.3 },
75 behaviors = { wander = false },
76 body = fish(0.62, 0.22, 0.12, "#5a7a58", "#c0c4a8", "#ece8dc", "#8a9a70", "#e07a8a"),
77}
78
79local Mackerel = world.creature {
80 name = "Mackerel",
81 speed = 2.4,
82 count = 0,
83 home = "here",
84 swims = true,
85 size = { 0.3, 0.2, 0.3 },
86 behaviors = { wander = false },
87 body = barred(fish(0.52, 0.2, 0.11, "#2a6a7a", "#c8d4d8", "#f0f4f4", "#3a5a6a", nil), 0.52, 0.12, 0.08, "#10283a"),
88}
89
90local SAND = "#b09060"
91local Flounder = world.creature {
92 name = "Flounder",
93 speed = 1.4,
94 count = 0,
95 home = "here",
96 swims = true,
97 size = { 0.4, 0.2, 0.4 },
98 behaviors = { wander = false },
99 body = {
100 { "body", size = { 0.44, 0.08, 0.6 }, at = { 0, 0.06, 0 }, color = SAND },
101 { "edge", size = { 0.54, 0.04, 0.44 }, at = { 0, 0.04, 0 }, color = "#c8a878" },
102 { "spot_1", size = { 0.08, 0.02, 0.08 }, at = { 0.1, 0.11, 0.1 }, color = "#6a5030" },
103 { "spot_2", size = { 0.07, 0.02, 0.07 }, at = { -0.12, 0.11, -0.04 }, color = "#6a5030" },
104 { "spot_3", size = { 0.06, 0.02, 0.06 }, at = { 0.05, 0.11, -0.18 }, color = "#6a5030" },
105 { "tail", size = { 0.24, 0.03, 0.14 }, at = { 0, 0.05, 0.36 }, color = "#c8a878", leg = true },
106 { "eye_left", size = { 0.06, 0.06, 0.06 }, at = { -0.06, 0.13, -0.22 }, color = EYE, head = true },
107 { "pupil_left", size = { 0.035, 0.02, 0.035 }, at = { -0.06, 0.165, -0.23 }, color = PUPIL, head = true },
108 { "eye_right", size = { 0.06, 0.06, 0.06 }, at = { 0.04, 0.13, -0.16 }, color = EYE, head = true },
109 { "pupil_right", size = { 0.035, 0.02, 0.035 }, at = { 0.04, 0.165, -0.17 }, color = PUPIL, head = true },
110 },
111}
112
113type Sort = { kind: any, item: string, deep: boolean, shoal: number }
114-- where each lives (deep water or the shallows) and how many swim together
115local SORTS: { Sort } = {
116 { kind = Perch, item = "Perch", deep = false, shoal = 1 },
117 { kind = Trout, item = "Trout", deep = false, shoal = 1 },
118 { kind = Mackerel, item = "Mackerel", deep = true, shoal = 4 },
119 { kind = Flounder, item = "Flounder", deep = true, shoal = 1 },
120}
121local ITEM: { [string]: string } = {}
122for _, s in SORTS do
123 ITEM[s.kind.name] = s.item
124end
125
126local MOST = 48 -- at once, under the 64 a creation may have
127local NEAR, FAR = 5, 18 -- how far from a player they turn up
128local LONELY = 40 -- and go with nobody this close
129local DEEP = 5 -- blocks of water that count as deep
130local CATCHES = { spear = true, arrow = true }
131
132local where: { [number]: vector } = {}
133-- a shoal's fish and the leader each is to follow, for its `spawned`
134local joining: { [number]: number } = {}
135
136local function water(at: vector): boolean
137 return world.block(at) == "Water"
138end
139
140local function gone(self)
141 where[self.id] = nil
142 self:remove()
143end
144
145-- the water's floor and how deep it is at (x, z), looking down from `y`,
146-- or nil where there's no water there
147local function column(x: number, z: number): (number?, number)
148 local top = world.top(x, z)
149 if not water(vector.create(x, top + 1, z)) then
150 return nil, 0
151 end
152 local depth = 0
153 while depth < 12 and water(vector.create(x, top + 1 + depth, z)) do
154 depth += 1
155 end
156 return top + 1, depth
157end
158
159-- somewhere in the water for one to swim to, near `at` (nil if there's none)
160local function swim_spot(at: vector, far: number): vector?
161 for _ = 1, 3 do
162 local a = world.random(0, 2 * math.pi)
163 local p = at + vector.create(math.cos(a) * far, world.random(-1, 1), math.sin(a) * far)
164 if water(p) then
165 return vector.create(p.x, math.floor(p.y) + 0.3, p.z)
166 end
167 end
168 return nil
169end
170
171local function swimmers_near(pos: vector, within: number)
172 local best, best_d = nil, within
173 for _, p in world.players() do
174 local d = vector.magnitude(p.position + UP * 0.8 - pos)
175 if d < best_d and (water(p.position) or water(p.position + UP)) then
176 best, best_d = p, d
177 end
178 end
179 return best
180end
181
182local function dart(self, from: vector)
183 local away = self.position - from
184 away = vector.create(away.x, away.y * 0.3, away.z)
185 local len = vector.magnitude(away)
186 if len < 0.1 then
187 away = vector.create(1, 0, 0)
188 else
189 away = away / len
190 end
191 local spot = self.position + away * 5
192 if water(spot) then
193 self:walk_to(spot, self.fast or 4)
194 else
195 local other = swim_spot(self.position, 3)
196 if other then
197 self:walk_to(other, self.fast or 4)
198 end
199 end
200 self.busy = 1.5
201end
202
203local function spawned(self)
204 self.think = world.random(0, 1)
205 self.dry = 0
206 self.fast = 4.5
207 local lead = joining[self.id]
208 if lead then
209 joining[self.id] = nil
210 self.leader = lead
211 self.off_x, self.off_y, self.off_z = world.random(-1, 1), world.random(-0.4, 0.4), world.random(-1, 1)
212 end
213end
214Perch.spawned = spawned
215Trout.spawned = spawned
216Mackerel.spawned = spawned
217Flounder.spawned = spawned
218
219-- out of the water: it flops about, and slips away soon after
220local function stranded(self, dt: number): boolean
221 if water(self.position + UP * 0.1) then
222 self.dry = 0
223 return false
224 end
225 self.dry += dt
226 if self:on_ground() and world.random() < dt * 1.5 then
227 self:hop(0.35)
228 end
229 if self.dry > 6 then
230 gone(self)
231 end
232 return true
233end
234
235local function tick(self, dt: number)
236 where[self.id] = self.position
237 if knobs.how_many < 1 then
238 gone(self)
239 return
240 end
241 if stranded(self, dt) then
242 return
243 end
244 self.busy = math.max(0, (self.busy or 0) - dt)
245 self.think -= dt
246 if self.think > 0 then
247 return
248 end
249 self.think = world.random(0.4, 0.8)
250 if not world.nearest_player(self.position, LONELY) then
251 gone(self)
252 return
253 end
254 local p = if knobs.shy > 0 then swimmers_near(self.position, knobs.shy) else nil
255 if p then
256 dart(self, p.position + UP * 0.8)
257 return
258 end
259 if self.busy > 0 then
260 return
261 end
262 if self.kind == "Flounder" then
263 -- lies on the bottom, shuffling now and then
264 self:stop()
265 self:climb(-0.6)
266 if world.random() < 0.08 then
267 local spot = swim_spot(self.position, 2)
268 if spot then
269 self:walk_to(vector.create(spot.x, self.position.y, spot.z), 0.8)
270 self.busy = 2
271 end
272 end
273 elseif self.leader then
274 -- a shoal follows its leader, each a little to one side
275 local at = where[self.leader]
276 if at then
277 local spot = at + vector.create(self.off_x or 0.6, self.off_y or 0, self.off_z or 0.6)
278 if water(spot) and vector.magnitude(spot - self.position) > 0.6 then
279 self:walk_to(spot, 2.6)
280 end
281 else
282 self.leader = nil
283 end
284 else
285 self:wander(if self.kind == "Mackerel" then 2.2 else 1.4)
286 end
287end
288Perch.tick = tick
289Trout.tick = tick
290Mackerel.tick = tick
291Flounder.tick = tick
292
293-- a spear or an arrow catches it; anything else and it darts off
294local function hit(self, player, shot)
295 local held = world.held(player)
296 local caught = if shot then CATCHES[shot.look] == true else held == "Spear"
297 if not caught then
298 dart(self, player.position + UP)
299 sound.play("splash", { at = self, volume = 0.4, pitch = 1.4 })
300 return
301 end
302 local item = ITEM[self.kind]
303 if item and world.give(player, item, 1) > 0 then
304 world.reward(player, 1)
305 sound.play("splash", { at = self, volume = 0.6 })
306 end
307 gone(self)
308end
309Perch.hit = hit
310Trout.hit = hit
311Mackerel.hit = hit
312Flounder.hit = hit
313
314-- Every few seconds: more near each player who's by water, with fewer near
315-- them than the knob says: the shallows' fish or the deep's, by how deep
316-- the water is where they'd come.
317world.every(2.5, function()
318 local want = math.floor(knobs.how_many)
319 if want < 1 then
320 return
321 end
322 local live = {}
323 for _, id in world.creatures() do
324 live[id] = true
325 end
326 local total = 0
327 for id in where do
328 if live[id] then
329 total += 1
330 else
331 where[id] = nil
332 end
333 end
334 for _, p in world.players() do
335 -- the Long Dusk has creatures of its own
336 if world.place(p.position) == "dusk" then
337 continue
338 end
339 local near = 0
340 for _, at in where do
341 if vector.magnitude(at - p.position) < FAR + 6 then
342 near += 1
343 end
344 end
345 if total >= MOST or near >= want then
346 continue
347 end
348 for _ = 1, 3 do
349 local a = world.random(0, 2 * math.pi)
350 local r = world.random(NEAR, FAR)
351 local x, z = math.floor(p.position.x + math.cos(a) * r) + 0.5, math.floor(p.position.z + math.sin(a) * r) + 0.5
352 local floor, depth = column(x, z)
353 if floor and depth >= 2 then
354 local deep = depth >= DEEP
355 local choices = {}
356 for _, s in SORTS do
357 if s.deep == deep then
358 table.insert(choices, s)
359 end
360 end
361 local s = choices[math.floor(world.random(1, #choices + 0.999))]
362 local n = math.min(s.shoal, MOST - total)
363 local leader: number? = nil
364 for i = 1, n do
365 local y = if s.kind == Flounder then floor + 0.05 else floor + world.random(0.3, depth - 0.8)
366 local at = vector.create(x + world.random(-0.8, 0.8), y, z + world.random(-0.8, 0.8))
367 if water(at) then
368 local id = world.spawn(s.kind, at)
369 where[id] = at
370 total += 1
371 if leader then
372 joining[id] = leader
373 else
374 leader = id
375 end
376 end
377 end
378 break
379 end
380 end
381 end
382end)