Sign in
creature

Duskwasps

by Potluck · version 1 · Checked for all ages

Papery nests that hang under the leaves in the woods at night, each with a few duskwasps buzzing round it. Wander under one in survival and they swarm you and sting, but they won't follow far from home. Pop the wasps with a bop each, or knock the nest down to send them off, and it leaves dusk wax, which a Moon bell takes. A light under the nest calms them, and they're gone by sunrise. None on a Peaceful world, and fewer on a Gentle one.

1--!strict
2-- Duskwasps: papery nests that hang under the leaves in the woods at night,
3-- each with a few wasps buzzing round it. Come under one in survival and
4-- they swarm you and sting, but they never follow far from home. A wasp
5-- pops with one bop; knock the nest down (a few bops, harder with a sword
6-- or a shot) and the rest give up and go, and it leaves dusk wax, which the
7-- Gloam's Moon bell takes. A light under a nest calms its wasps, and nests
8-- are gone by sunrise. How many wasps a nest has and how hard they sting go
9-- by the world's danger (world.danger): none on Peaceful.
10
11local world = require("@potluck/world@0.33")
12local tweak = require("@potluck/tweak")
13local sound = require("@potluck/sound@0.2")
14local ui = require("@potluck/ui@0.2")
15
16local knobs = tweak {
17 on = { true, label = "Duskwasps nest in the woods at night", order = 1 },
18 wasps = { 3, min = 1, max = 6, label = "Wasps to a nest", order = 2 },
19 sting = { 1, min = 0, max = 5, label = "How hard a wasp stings", order = 3 },
20 bops = { 4, min = 1, max = 12, label = "Bops to knock a nest down by hand", order = 4 },
21 guard = { 6, min = 3, max = 12, label = "How close to its nest sets them off", order = 5 },
22 stripe = { "#e0a030", label = "Wasp stripe color", order = 6 },
23}
24
25local DANGER: { [string]: { many: number, hurt: number, pace: number } } = {
26 peaceful = { many = 0, hurt = 0, pace = 0 },
27 gentle = { many = 0.67, hurt = 0.5, pace = 1.4 },
28 normal = { many = 1, hurt = 1, pace = 1 },
29 hard = { many = 1.67, hurt = 1.5, pace = 0.75 },
30}
31local function danger()
32 return DANGER[world.danger()] or DANGER.gentle
33end
34
35local SWORDS: { [string]: number } = {
36 ["Wooden sword"] = 2,
37 ["Stone sword"] = 3,
38 ["Copper sword"] = 3,
39 ["Iron sword"] = 4,
40 ["Moonsilver sword"] = 5,
41}
42
43local function bops(held: string?): number
44 if not held then
45 return 1
46 end
47 if SWORDS[held] then
48 return SWORDS[held]
49 end
50 local it = world.item(held)
51 return if it and it.kind == "weapon" then it.power or 1 else 1
52end
53
54local LEAVES: { [string]: boolean } = {
55 ["Oak leaves"] = true, ["Birch leaves"] = true, ["Maple leaves"] = true, ["Willow leaves"] = true,
56 ["Pine needles"] = true, ["Redwood needles"] = true,
57}
58
59local PAPER = "#8a8078"
60local DARK = "#2a2018"
61local EYE = "#fff6e8"
62local PUPIL = "#140c06"
63
64local Nest = world.creature {
65 name = "Duskwasp nest",
66 speed = 0,
67 count = 0,
68 home = "here",
69 flies = true,
70 size = { 0.6, 0.8, 0.6 },
71 behaviors = { wander = false },
72 body = {
73 { "stalk", size = { 0.08, 0.2, 0.08 }, at = { 0, 0.78, 0 }, color = DARK },
74 { "top", size = { 0.42, 0.16, 0.42 }, at = { 0, 0.64, 0 }, color = PAPER },
75 { "middle", size = { 0.6, 0.34, 0.6 }, at = { 0, 0.4, 0 }, color = "#9a9088" },
76 { "band", size = { 0.62, 0.05, 0.62 }, at = { 0, 0.46, 0 }, color = "#6a625a" },
77 { "bottom", size = { 0.4, 0.16, 0.4 }, at = { 0, 0.16, 0 }, color = PAPER },
78 { "hole", size = { 0.16, 0.14, 0.03 }, at = { 0, 0.3, -0.31 }, color = "#ffb040", glow = true },
79 },
80}
81
82local Wasp = world.creature {
83 name = "Duskwasp",
84 speed = 4.5,
85 count = 0,
86 home = "here",
87 flies = true,
88 size = { 0.4, 0.4, 0.4 },
89 behaviors = { wander = false },
90 body = {
91 { "thorax", size = { 0.18, 0.16, 0.18 }, at = { 0, 0.2, -0.02 }, color = DARK },
92 { "head", size = { 0.2, 0.18, 0.16 }, at = { 0, 0.22, -0.18 }, color = DARK, head = true },
93 { "eye_left", size = { 0.08, 0.1, 0.03 }, at = { -0.055, 0.24, -0.27 }, color = EYE, glow = true, head = true },
94 { "pupil_left", size = { 0.045, 0.055, 0.03 }, at = { -0.05, 0.235, -0.285 }, color = PUPIL, head = true },
95 { "eye_right", size = { 0.08, 0.1, 0.03 }, at = { 0.055, 0.24, -0.27 }, color = EYE, glow = true, head = true },
96 { "pupil_right", size = { 0.045, 0.055, 0.03 }, at = { 0.05, 0.235, -0.285 }, color = PUPIL, head = true },
97 { "belly_1", size = { 0.2, 0.18, 0.12 }, at = { 0, 0.18, 0.12 }, color = knobs.stripe },
98 { "belly_2", size = { 0.18, 0.16, 0.1 }, at = { 0, 0.16, 0.23 }, color = DARK },
99 { "belly_3", size = { 0.14, 0.13, 0.08 }, at = { 0, 0.14, 0.32 }, color = knobs.stripe },
100 { "sting", size = { 0.03, 0.03, 0.08 }, at = { 0, 0.12, 0.4 }, color = DARK },
101 { "wing_left", size = { 0.22, 0.02, 0.12 }, at = { -0.14, 0.3, 0.02 }, color = "#c8c0d8", leg = true },
102 { "wing_right", size = { 0.22, 0.02, 0.12 }, at = { 0.14, 0.3, 0.02 }, color = "#c8c0d8", leg = true },
103 },
104}
105
106local MOST = 8 -- nests at once in the whole world
107local NEAR, FAR = 5, 14 -- how far from a player a nest is found
108local LEASH = 14 -- wasps never go farther than this from their nest
109local LONELY = 48 -- nests go with nobody this close
110local SPACE = 24 -- one nest near a player at a time
111local CALM = 4 -- a light this close to a nest calms it
112local STING_EVERY = 1.4
113local UP = vector.create(0, 1, 0)
114
115-- each nest's place, and each wasp's nest
116local nests: { [number]: vector } = {}
117local home_of: { [number]: number } = {}
118-- whoever each nest is angry with, whom its wasps go after
119local angry: { [number]: number } = {}
120
121local function dark(): boolean
122 return world.daylight() < 0.15
123end
124
125local function by_id(id: number?)
126 if not id then
127 return nil
128 end
129 for _, q in world.players() do
130 if q.id == id then
131 return q
132 end
133 end
134 return nil
135end
136
137-- the nearest player in survival within `within` of a place
138local function intruder(at: vector, within: number)
139 local best, best_d = nil, within
140 for _, p in world.players() do
141 if world.pockets(p) ~= nil then
142 local d = vector.magnitude(p.position + UP - at)
143 if d < best_d then
144 best, best_d = p, d
145 end
146 end
147 end
148 return best
149end
150
151-- ---- nests ----
152
153function Nest:spawned()
154 self.look_in = 0
155 nests[self.id] = self.position
156end
157
158local function break_up(nest: number)
159 nests[nest] = nil
160 angry[nest] = nil
161 for w, n in home_of do
162 if n == nest then
163 home_of[w] = nil
164 end
165 end
166end
167
168function Nest:tick(dt: number)
169 nests[self.id] = self.position
170 self:climb(0)
171 local d = danger()
172 if not knobs.on or d.many == 0 or world.daylight() > 0.3 then
173 break_up(self.id)
174 self:remove()
175 return
176 end
177 self.look_in -= dt
178 if self.look_in <= 0 then
179 self.look_in = 1
180 if not world.nearest_player(self.position, LONELY) then
181 break_up(self.id)
182 self:remove()
183 return
184 end
185 local calm = world.find_light(self.position, CALM) ~= nil
186 local p = if calm then nil else intruder(self.position, knobs.guard)
187 local was = by_id(angry[self.id])
188 if p then
189 if not was then
190 sound.play("drone", { at = self, volume = 0.9 })
191 end
192 angry[self.id] = p.id
193 elseif calm or not was or vector.magnitude(was.position - self.position) > LEASH then
194 -- a light, or they've gone far enough: calm down
195 angry[self.id] = nil
196 end
197 if world.random() < 0.15 then
198 sound.play("drone", { at = self, volume = 0.35 })
199 end
200 end
201end
202
203function Nest:hit(player, shot)
204 local bop = if shot then shot.power else bops(world.held(player)) * world.might(player)
205 self.bopped = (self.bopped or 0) + bop
206 if world.pockets(player) ~= nil then
207 angry[self.id] = player.id
208 end
209 if self.bopped < knobs.bops then
210 sound.play("drone", { at = self, volume = 1, pitch = 1.3, caption = "The nest's wasps are angry" })
211 return
212 end
213 sound.play("rustle", { at = self, pitch = 0.7, caption = "A duskwasp nest falls" })
214 world.give(player, "Dusk wax", math.floor(world.random(1, 2.999)))
215 world.reward(player, 6)
216 -- its wasps give up and go
217 break_up(self.id)
218 self:remove()
219 if world.item("Moon bell") ~= nil and world.pockets(player) ~= nil then
220 ui.show(player, "find", "Dusk wax. A Moon bell takes some.", { at = "bottom" })
221 world.after(4, function()
222 ui.hide(player, "find")
223 end)
224 end
225end
226
227-- ---- wasps ----
228
229local function nest_of(self): (number?, vector?)
230 local n = home_of[self.id]
231 if n and nests[n] then
232 return n, nests[n]
233 end
234 return nil, nil
235end
236
237function Wasp:spawned()
238 self.side = world.random(0, 2 * math.pi)
239 self.sting_in = world.random(0.5, 1.5)
240 self.lost = 0
241end
242
243function Wasp:tick(dt: number)
244 local d = danger()
245 if not knobs.on or d.many == 0 or world.daylight() > 0.3 then
246 home_of[self.id] = nil
247 self:remove()
248 return
249 end
250 local n, at = nest_of(self)
251 if not n or not at then
252 -- no nest: off and away
253 self.lost += dt
254 self:climb(1.5)
255 self:wander(3)
256 if self.lost > 3 then
257 self:remove()
258 end
259 return
260 end
261 local target = by_id(angry[n])
262 if target and vector.magnitude(target.position - at) > LEASH then
263 target = nil
264 end
265 if not target then
266 -- round the nest
267 self.side += dt * 2.2
268 local ring = at + vector.create(math.cos(self.side), 0, math.sin(self.side)) * 1.1
269 self:climb(math.clamp((at.y + 0.3 + math.sin(self.side * 1.7) * 0.3 - self.position.y) * 2, -2, 2))
270 self:walk_to(vector.create(ring.x, self.position.y, ring.z), 2.5)
271 return
272 end
273 -- buzz round their head and sting
274 local head = target.position + UP * 1.5
275 self.side += dt * 3
276 local near = head + vector.create(math.cos(self.side), 0, math.sin(self.side)) * 0.8
277 self:climb(math.clamp((head.y - self.position.y) * 2.5, -3, 3))
278 self:walk_to(vector.create(near.x, self.position.y, near.z), 4.5)
279 self.sting_in -= dt
280 if self.sting_in <= 0 and vector.magnitude(self.position - head) < 1.6 then
281 self.sting_in = STING_EVERY * d.pace * world.random(0.8, 1.3)
282 if knobs.sting > 0 and world.hurt(target, knobs.sting * d.hurt) then
283 sound.play("sting", { at = self, volume = 0.7 })
284 end
285 end
286end
287
288function Wasp:hit(player, shot)
289 local n = home_of[self.id]
290 if n and world.pockets(player) ~= nil then
291 angry[n] = player.id
292 end
293 home_of[self.id] = nil
294 sound.play("pop", { at = self, pitch = 1.8, caption = "A duskwasp pops" })
295 world.reward(player, 1)
296 self:remove()
297end
298
299-- ---- where nests go ----
300
301-- under the leaves near a place: the air under a leaf with room to hang
302local function hanging(x: number, y0: number, z: number): vector?
303 local fx, fz = math.floor(x) + 0.5, math.floor(z) + 0.5
304 for y = math.floor(y0) + 3, math.floor(y0) + 16 do
305 local at = vector.create(fx, y, fz)
306 if LEAVES[world.block(at)] then
307 local under = at - UP
308 if world.block(under) == "Air" and world.block(under - UP) == "Air" then
309 return vector.create(fx, y - 0.85, fz)
310 end
311 return nil
312 end
313 end
314 return nil
315end
316
317-- Every few seconds at night: a nest near each player in survival in the
318-- woods, if there's none near them yet.
319world.every(5, function()
320 if not knobs.on or not dark() then
321 return
322 end
323 local d = danger()
324 if d.many == 0 then
325 return
326 end
327 local live = {}
328 for _, id in world.creatures(Nest) do
329 live[id] = true
330 end
331 local total = 0
332 for id in nests do
333 if live[id] then
334 total += 1
335 else
336 break_up(id)
337 end
338 end
339 for _, p in world.players() do
340 -- the Long Dusk has creatures of its own
341 if world.place(p.position) == "dusk" then
342 continue
343 end
344 if total >= MOST then
345 return
346 end
347 if world.pockets(p) ~= nil then
348 local close = false
349 for _, at in nests do
350 if vector.magnitude(at - p.position) < SPACE then
351 close = true
352 end
353 end
354 if not close then
355 for _ = 1, 6 do
356 local a = world.random(0, 2 * math.pi)
357 local r = world.random(NEAR, FAR)
358 local at = hanging(p.position.x + math.cos(a) * r, p.position.y, p.position.z + math.sin(a) * r)
359 if at and not world.find_light(at, CALM + 2) then
360 local n = world.spawn(Nest, at)
361 nests[n] = at
362 total += 1
363 for i = 1, math.max(1, math.floor(knobs.wasps * d.many + 0.5)) do
364 local t = i / 6 * 2 * math.pi
365 home_of[world.spawn(Wasp, at + vector.create(math.cos(t), 0.2, math.sin(t)))] = n
366 end
367 break
368 end
369 end
370 end
371 end
372 end
373end)