Sign in
creature

Fenlures

by Potluck · version 1 · Checked for all ages

Soft lights that bob over the marshes at night, just out of reach. Follow one and it drifts away, and when you catch up a mossy head with a mouthful of teeth rises under it and bites. Bop or shoot the light and it opens up in surprise; beat it and it leaves its fen glow, which a Moon bell takes. Being lights themselves, they keep other night creatures away. None on a Peaceful world, and fewer on a Gentle one.

1--!strict
2-- Fenlures: soft lights that bob over the marshes at night. One drifts
3-- just ahead of anyone in survival who comes after it (a little slower
4-- than they walk, so they catch up), and when they're close a mossy head
5-- with a mouthful of teeth rises under the light (self:become) and bites,
6-- then it closes and slips off to lure again. Bop or shoot the light and
7-- it opens up in surprise; beat it and it leaves its fen glow, which the
8-- Gloam's Moon bell takes. Its light is a real light (a part's `light`), so
9-- other night creatures keep away from it. How many come and how hard they
10-- bite go by the world's danger (world.danger): none on Peaceful.
11
12local world = require("@potluck/world@0.33")
13local tweak = require("@potluck/tweak")
14local sound = require("@potluck/sound@0.2")
15local ui = require("@potluck/ui@0.2")
16
17local knobs = tweak {
18 how_many = { 2, min = 0, max = 5, label = "How many bob near you in the marsh at night (0 for none)", order = 1 },
19 bite = { 3, min = 0, max = 8, label = "How hard they bite", order = 2 },
20 bops = { 4, min = 1, max = 12, label = "Bops to beat one by hand", order = 3 },
21 glow = { "#d8f070", label = "Light color", order = 4 },
22}
23
24local DANGER: { [string]: { many: number, hurt: number } } = {
25 peaceful = { many = 0, hurt = 0 },
26 gentle = { many = 0.5, hurt = 0.5 },
27 normal = { many = 1, hurt = 1 },
28 hard = { many = 1.5, hurt = 1.5 },
29}
30local function danger()
31 return DANGER[world.danger()] or DANGER.gentle
32end
33
34local SWORDS: { [string]: number } = {
35 ["Wooden sword"] = 2,
36 ["Stone sword"] = 3,
37 ["Copper sword"] = 3,
38 ["Iron sword"] = 4,
39 ["Moonsilver sword"] = 5,
40}
41
42local function bops(held: string?): number
43 if not held then
44 return 1
45 end
46 if SWORDS[held] then
47 return SWORDS[held]
48 end
49 local it = world.item(held)
50 return if it and it.kind == "weapon" then it.power or 1 else 1
51end
52
53local MOSS = "#3a5a28"
54local DARK = "#1e2a14"
55local EYE = "#f4ffd8"
56local PUPIL = "#101808"
57local TOOTH = "#ece4c8"
58
59local Lure = world.creature {
60 name = "Fenlure",
61 speed = 1.4,
62 count = 0,
63 home = "here",
64 flies = true,
65 size = { 0.5, 0.6, 0.5 },
66 behaviors = { wander = false },
67 body = {
68 { "light", size = { 0.34, 0.34, 0.34 }, at = { 0, 0.3, 0 }, color = knobs.glow, glow = true, light = 0.5 },
69 { "halo_1", size = { 0.12, 0.12, 0.12 }, at = { 0.24, 0.42, 0.06 }, color = knobs.glow, glow = true },
70 { "halo_2", size = { 0.1, 0.1, 0.1 }, at = { -0.22, 0.2, -0.04 }, color = knobs.glow, glow = true },
71 { "stalk", size = { 0.04, 0.2, 0.04 }, at = { 0, 0.04, 0 }, color = DARK },
72 },
73}
74
75local Open = world.creature {
76 name = "Fenlure (open)",
77 speed = 1.6,
78 count = 0,
79 home = "here",
80 flies = true,
81 size = { 1.0, 1.4, 1.0 },
82 behaviors = { wander = false },
83 body = {
84 { "light", size = { 0.34, 0.34, 0.34 }, at = { 0, 1.9, -0.3 }, color = knobs.glow, glow = true, light = 0.5, head = true },
85 { "stalk", size = { 0.05, 0.5, 0.05 }, at = { 0, 1.5, -0.18 }, color = DARK, head = true },
86 { "head", size = { 1.0, 0.9, 0.9 }, at = { 0, 0.75, 0 }, color = MOSS, head = true },
87 { "brow", size = { 0.9, 0.14, 0.2 }, at = { 0, 1.14, -0.4 }, color = DARK, head = true },
88 { "eye_left", size = { 0.24, 0.24, 0.04 }, at = { -0.22, 0.96, -0.46 }, color = EYE, glow = true, head = true },
89 { "pupil_left", size = { 0.12, 0.14, 0.03 }, at = { -0.2, 0.94, -0.49 }, color = PUPIL, head = true },
90 { "eye_right", size = { 0.24, 0.24, 0.04 }, at = { 0.22, 0.96, -0.46 }, color = EYE, glow = true, head = true },
91 { "pupil_right", size = { 0.12, 0.14, 0.03 }, at = { 0.2, 0.94, -0.49 }, color = PUPIL, head = true },
92 { "mouth", size = { 0.8, 0.3, 0.04 }, at = { 0, 0.5, -0.46 }, color = "#2a0e0a", head = true },
93 { "tooth_1", size = { 0.07, 0.12, 0.03 }, at = { -0.3, 0.6, -0.48 }, color = TOOTH, head = true },
94 { "tooth_2", size = { 0.07, 0.12, 0.03 }, at = { -0.1, 0.6, -0.48 }, color = TOOTH, head = true },
95 { "tooth_3", size = { 0.07, 0.12, 0.03 }, at = { 0.1, 0.6, -0.48 }, color = TOOTH, head = true },
96 { "tooth_4", size = { 0.07, 0.12, 0.03 }, at = { 0.3, 0.6, -0.48 }, color = TOOTH, head = true },
97 { "tooth_5", size = { 0.07, 0.1, 0.03 }, at = { -0.2, 0.4, -0.48 }, color = TOOTH, head = true },
98 { "tooth_6", size = { 0.07, 0.1, 0.03 }, at = { 0.2, 0.4, -0.48 }, color = TOOTH, head = true },
99 { "weed_1", size = { 0.08, 0.4, 0.08 }, at = { -0.4, 0.3, 0.3 }, color = "#5a7a30", leg = true },
100 { "weed_2", size = { 0.08, 0.36, 0.08 }, at = { 0.42, 0.28, 0.2 }, color = "#5a7a30", leg = true },
101 { "weed_3", size = { 0.08, 0.3, 0.08 }, at = { 0.1, 0.24, 0.42 }, color = "#5a7a30", leg = true },
102 },
103}
104
105local MOST = 12 -- at once in the whole world
106local NEAR, FAR = 10, 18 -- how far from a player they turn up
107local LURE = 18 -- they lure players this close
108local LONELY = 40 -- and go with nobody this close
109local KEEP = 6 -- how far ahead they drift
110local CLOSE = 2.6 -- and how close before they open
111local OPEN = 4 -- seconds it stays open
112local BITE_EVERY = 1.4
113local HOVER = 1.2 -- over the ground or the water
114local UP = vector.create(0, 1, 0)
115
116local where: { [number]: vector } = {}
117
118local function flat(a: vector, b: vector): number
119 local dx, dz = a.x - b.x, a.z - b.z
120 return math.sqrt(dx * dx + dz * dz)
121end
122
123local function dark(): boolean
124 return world.daylight() < 0.15
125end
126
127-- the top of the ground or the water at a place
128local function surface(x: number, z: number): number
129 local y = world.top(x, z) + 1
130 for _ = 1, 8 do
131 if world.block(vector.create(x, y, z)) ~= "Water" then
132 break
133 end
134 y += 1
135 end
136 return y
137end
138
139local seen_at, seen = -1, {}
140local function prey()
141 local now = world.time()
142 if now ~= seen_at then
143 seen_at, seen = now, {}
144 for _, p in world.players() do
145 if world.pockets(p) ~= nil and p.position.y >= world.top(p.position.x, p.position.z) - 0.5 then
146 table.insert(seen, p)
147 end
148 end
149 end
150 return seen
151end
152
153local function nearest(at: vector, within: number)
154 local best, best_d = nil, within
155 for _, p in prey() do
156 local d = vector.magnitude(p.position - at)
157 if d < best_d then
158 best, best_d = p, d
159 end
160 end
161 return best
162end
163
164local function gone(self)
165 where[self.id] = nil
166 self:remove()
167end
168
169local function hover(self, over: number)
170 local want = surface(self.position.x, self.position.z) + over + math.sin(world.time() * 1.6 + self.id) * 0.15
171 self:climb(math.clamp((want - self.position.y) * 1.5, -2, 2))
172end
173
174local function opens(self, wait: number)
175 self:stop()
176 self:become(Open)
177 self.open = OPEN
178 self.bite_in = wait
179 sound.play("rise", { at = self, volume = 0.9 })
180end
181
182-- ---- the light ----
183
184function Lure:spawned()
185 self.look_in = 0
186 sound.play("hum", { at = self, volume = 0.4 })
187end
188
189function Lure:tick(dt: number)
190 where[self.id] = self.position
191 local d = danger()
192 if d.many == 0 or knobs.how_many < 1 or world.daylight() > 0.3 then
193 gone(self)
194 return
195 end
196 hover(self, HOVER)
197 if (self.rest or 0) > 0 then
198 self.rest -= dt
199 return
200 end
201 self.look_in -= dt
202 if self.look_in <= 0 then
203 self.look_in = world.random(0.4, 0.7)
204 if not world.nearest_player(self.position, LONELY) then
205 gone(self)
206 return
207 end
208 local p = nearest(self.position, LURE)
209 self.prey = if p then p.id else nil
210 if world.random() < 0.06 then
211 sound.play("hum", { at = self, volume = 0.3 })
212 end
213 end
214 local p = nil
215 for _, q in prey() do
216 if q.id == self.prey then
217 p = q
218 end
219 end
220 if not p then
221 self:wander(0.8)
222 return
223 end
224 local dist = flat(self.position, p.position)
225 if dist < CLOSE then
226 self:face(p.position)
227 opens(self, 0.6)
228 elseif dist < KEEP then
229 -- drift away, a little slower than they walk
230 local away = vector.create(self.position.x - p.position.x, 0, self.position.z - p.position.z) / math.max(dist, 0.1)
231 local side = vector.create(-away.z, 0, away.x) * math.sin(world.time() * 0.5 + self.id)
232 self:walk_to(self.position + (away + side * 0.5) * 3, 1.4)
233 else
234 self:stop()
235 self:face(p.position)
236 end
237end
238
239-- beaten: it goes out, and leaves its glow
240local function beaten(self, player)
241 sound.play("pop", { at = self, pitch = 0.7, caption = "A fenlure goes out" })
242 world.give(player, "Fen glow", 1)
243 world.reward(player, 5)
244 gone(self)
245 if world.item("Moon bell") ~= nil and world.pockets(player) ~= nil then
246 ui.show(player, "find", "Fen glow. A Moon bell takes one.", { at = "bottom" })
247 world.after(4, function()
248 ui.hide(player, "find")
249 end)
250 end
251end
252
253local function bop_of(player, shot): number
254 return if shot then shot.power * 1.5 else bops(world.held(player)) * world.might(player)
255end
256
257function Lure:hit(player, shot)
258 self.bopped = (self.bopped or 0) + bop_of(player, shot)
259 if self.bopped >= knobs.bops then
260 beaten(self, player)
261 return
262 end
263 -- surprised: it opens up
264 opens(self, 0.8)
265end
266
267-- ---- open ----
268
269function Open:tick(dt: number)
270 where[self.id] = self.position
271 local d = danger()
272 if d.many == 0 or knobs.how_many < 1 then
273 gone(self)
274 return
275 end
276 hover(self, 0)
277 self.open -= dt
278 if self.open <= 0 or world.daylight() > 0.3 then
279 -- closes and slips off to lure again
280 self:become(Lure)
281 self.rest = 2
282 self.look_in = 0
283 local p = nearest(self.position, LURE)
284 if p then
285 local away = vector.create(self.position.x - p.position.x, 0, self.position.z - p.position.z)
286 self:walk_to(self.position + vector.normalize(away + vector.create(0.01, 0, 0)) * 6, 3)
287 end
288 return
289 end
290 local p = nearest(self.position, LURE)
291 if not p then
292 self:stop()
293 return
294 end
295 self:face(p.position)
296 self.bite_in -= dt
297 if flat(self.position, p.position) < 2 then
298 self:stop()
299 if self.bite_in <= 0 then
300 self.bite_in = BITE_EVERY
301 if knobs.bite > 0 and world.hurt(p, knobs.bite * d.hurt) then
302 sound.play("chomp", { at = self, volume = 0.9 })
303 end
304 end
305 else
306 self:walk_to(p.position, 1.6)
307 end
308end
309
310function Open:hit(player, shot)
311 self.bopped = (self.bopped or 0) + bop_of(player, shot)
312 if self.bopped >= knobs.bops then
313 beaten(self, player)
314 return
315 end
316 sound.play("chomp", { at = self, volume = 0.6, pitch = 0.7 })
317 self.bite_in = math.max(self.bite_in or 0, 0.6)
318end
319
320-- Every few seconds at night: one more near each player out in the marsh in
321-- survival, with fewer near them than the knob and the danger say.
322world.every(4, function()
323 if not dark() then
324 return
325 end
326 local d = danger()
327 local want = math.floor(knobs.how_many * d.many + 0.5)
328 if want < 1 then
329 return
330 end
331 local live = {}
332 for _, id in world.creatures(Lure) do
333 live[id] = true
334 end
335 local total = 0
336 for id in where do
337 if live[id] then
338 total += 1
339 else
340 where[id] = nil
341 end
342 end
343 for _, p in prey() do
344 -- the Long Dusk has creatures of its own
345 if world.place(p.position) == "dusk" then
346 continue
347 end
348 if total >= MOST then
349 return
350 end
351 local land = world.biome(p.position)
352 if land == "marsh" or land == "swamp" then
353 local near = 0
354 for _, at in where do
355 if vector.magnitude(at - p.position) < LURE + 4 then
356 near += 1
357 end
358 end
359 if near < want then
360 for _ = 1, 4 do
361 local a = world.random(0, 2 * math.pi)
362 local r = world.random(NEAR, FAR)
363 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
364 local at = vector.create(x, surface(x, z) + HOVER, z)
365 if world.block(at) == "Air" and world.block(at + UP) == "Air" then
366 where[world.spawn(Lure, at)] = at
367 total += 1
368 break
369 end
370 end
371 end
372 end
373 end
374end)