Sign in
creature

Frostwisps

by Potluck · version 1 · Checked for all ages

Little drifting spirits of frost that come out on cold nights, over the snowfields, the pine woods and the mountains, wherever you're out under the sky in survival. They circle you, then dart in for a chilly touch: it stings, and for a while you heal slowly. A torch, lantern or campfire keeps them off, and they melt at sunrise. Two bops pop one, and it leaves a frost shard, which a Moon bell takes. None on a Peaceful world, and fewer on a Gentle one.

1--!strict
2-- Frostwisps: little drifting spirits of frost that come out on cold
3-- nights (the snowfields, the pine woods and the mountains, or anywhere
4-- there's snow or ice underfoot), wherever someone in survival is out under
5-- the sky away from any light. They circle a player, then dart in for a
6-- chilly touch: it stings, and for a while that player heals slowly
7-- (world.healing). A torch, lantern or campfire, put down or in hand,
8-- keeps them off, and they melt at sunrise. They're brittle: two bops pop
9-- one, and it leaves a frost shard, which the Gloam's Moon bell takes. How
10-- many come and how hard they chill go by the world's danger
11-- (world.danger): none on Peaceful.
12
13local world = require("@potluck/world@0.33")
14local tweak = require("@potluck/tweak")
15local sound = require("@potluck/sound@0.2")
16local ui = require("@potluck/ui@0.2")
17
18local knobs = tweak {
19 how_many = { 3, min = 0, max = 8, label = "How many come near you on a cold night (0 for none)", order = 1 },
20 touch = { 1.5, min = 0, max = 6, label = "How hard a touch stings", order = 2 },
21 chill = { 8, min = 0, max = 30, label = "Seconds a touch keeps you from healing well", order = 3 },
22 bops = { 2, min = 1, max = 8, label = "Bops to pop one by hand", order = 4 },
23 shy = { 5, min = 2, max = 12, label = "How far they keep from a light", order = 5 },
24 color = { "#bfe8ff", label = "Frost color", order = 6 },
25}
26
27local DANGER: { [string]: { many: number, hurt: number, pace: number } } = {
28 peaceful = { many = 0, hurt = 0, pace = 0 },
29 gentle = { many = 0.5, hurt = 0.5, pace = 1.4 },
30 normal = { many = 1, hurt = 1, pace = 1 },
31 hard = { many = 1.5, hurt = 1.5, pace = 0.75 },
32}
33local function danger()
34 return DANGER[world.danger()] or DANGER.gentle
35end
36
37local SWORDS: { [string]: number } = {
38 ["Wooden sword"] = 2,
39 ["Stone sword"] = 3,
40 ["Copper sword"] = 3,
41 ["Iron sword"] = 4,
42 ["Moonsilver sword"] = 5,
43}
44
45-- what's in hand bops this hard: a sword, a weapon another creation made
46-- by its power (world.item), or a hand
47local function bops(held: string?): number
48 if not held then
49 return 1
50 end
51 if SWORDS[held] then
52 return SWORDS[held]
53 end
54 local it = world.item(held)
55 return if it and it.kind == "weapon" then it.power or 1 else 1
56end
57
58-- where they come: these lands, or anywhere with snow or ice underfoot
59local COLD: { [string]: boolean } = { snowfield = true, pinewood = true, mountain = true }
60local ICY: { [string]: boolean } = { Snow = true, Ice = true }
61-- lights in hand that keep them off
62local LIGHTS: { [string]: boolean } = { Torch = true, Lantern = true, Candle = true, Brazier = true }
63
64local ICE = "#e8f8ff"
65local EYE = "#f4fbff"
66local PUPIL = "#16304a"
67
68local Wisp = world.creature {
69 name = "Frostwisp",
70 speed = 3,
71 count = 0,
72 home = "here",
73 flies = true,
74 size = { 0.6, 0.8, 0.6 },
75 behaviors = { wander = false },
76 body = {
77 { "core", size = { 0.46, 0.46, 0.4 }, at = { 0, 0.42, 0 }, color = knobs.color, glow = true },
78 { "crown_1", size = { 0.08, 0.24, 0.08 }, at = { 0, 0.76, 0.02 }, color = ICE, glow = true, head = true },
79 { "crown_2", size = { 0.07, 0.18, 0.07 }, at = { -0.16, 0.7, 0.04 }, color = ICE, head = true },
80 { "crown_3", size = { 0.07, 0.18, 0.07 }, at = { 0.16, 0.7, 0.04 }, color = ICE, head = true },
81 { "eye_left", size = { 0.15, 0.17, 0.03 }, at = { -0.1, 0.48, -0.21 }, color = EYE, glow = true, head = true },
82 { "pupil_left", size = { 0.08, 0.1, 0.03 }, at = { -0.09, 0.46, -0.235 }, color = PUPIL, head = true },
83 { "eye_right", size = { 0.15, 0.17, 0.03 }, at = { 0.1, 0.48, -0.21 }, color = EYE, glow = true, head = true },
84 { "pupil_right", size = { 0.08, 0.1, 0.03 }, at = { 0.09, 0.46, -0.235 }, color = PUPIL, head = true },
85 { "mouth", size = { 0.1, 0.03, 0.03 }, at = { 0, 0.32, -0.21 }, color = PUPIL, head = true },
86 { "shard_left", size = { 0.22, 0.06, 0.06 }, at = { -0.3, 0.42, 0.02 }, color = ICE, leg = true },
87 { "shard_right", size = { 0.22, 0.06, 0.06 }, at = { 0.3, 0.42, 0.02 }, color = ICE, leg = true },
88 { "tail_1", size = { 0.24, 0.12, 0.24 }, at = { 0, 0.16, 0.06 }, color = knobs.color },
89 { "tail_2", size = { 0.12, 0.1, 0.12 }, at = { 0, 0.04, 0.12 }, color = ICE },
90 },
91}
92
93local MOST = 24 -- at once in the whole world
94local NEAR, FAR = 8, 14 -- how far from a player they turn up
95local CHASE = 20 -- they go after players this close
96local LONELY = 36 -- and melt away with nobody this close
97local RING = 2.6 -- how far off they circle
98local UP = vector.create(0, 1, 0)
99
100local where: { [number]: vector } = {}
101
102local function flat(a: vector, b: vector): number
103 local dx, dz = a.x - b.x, a.z - b.z
104 return math.sqrt(dx * dx + dz * dz)
105end
106
107local function dark(): boolean
108 return world.daylight() < 0.15
109end
110
111-- out under the sky: not under rock or a roof
112local function outside(at: vector): boolean
113 return at.y >= world.top(at.x, at.z) - 0.5
114end
115
116local function cold(at: vector): boolean
117 if COLD[world.biome(at)] then
118 return true
119 end
120 return ICY[world.block(vector.create(at.x, at.y - 0.5, at.z))] == true
121end
122
123local seen_at, seen = -1, {}
124local function prey()
125 local now = world.time()
126 if now ~= seen_at then
127 seen_at, seen = now, {}
128 for _, p in world.players() do
129 if world.pockets(p) ~= nil and outside(p.position) then
130 table.insert(seen, p)
131 end
132 end
133 end
134 return seen
135end
136
137-- a light near a place: put down, or in the hand of a player near it
138local function light_near(at: vector, within: number): vector?
139 local lit = world.find_light(at, within)
140 if lit then
141 return lit
142 end
143 for _, p in world.players() do
144 local held = world.held(p)
145 if held and LIGHTS[held] and vector.magnitude(p.position - at) < within then
146 return p.position
147 end
148 end
149 return nil
150end
151
152-- ---- the chill ----
153
154-- when each chilled player warms up again, by id
155local chilled: { [number]: number } = {}
156local told: { [number]: boolean } = {}
157
158local function chill(p, seconds: number)
159 if seconds <= 0 then
160 return
161 end
162 local was = chilled[p.id]
163 chilled[p.id] = math.max(was or 0, world.time() + seconds)
164 world.healing(p, 0.25)
165 if not was and not told[p.id] then
166 told[p.id] = true
167 ui.show(p, "chill", "You're chilled: you heal slowly for a while.", { at = "bottom" })
168 world.after(4, function()
169 ui.hide(p, "chill")
170 end)
171 end
172end
173
174world.every(0.5, function()
175 local now = world.time()
176 for _, p in world.players() do
177 local until_ = chilled[p.id]
178 if until_ and now >= until_ then
179 chilled[p.id] = nil
180 world.healing(p, 1)
181 end
182 end
183end)
184
185world.on("player_left", function(p)
186 chilled[p.id] = nil
187 told[p.id] = nil
188end)
189
190-- ---- frostwisps ----
191
192local function gone(self)
193 where[self.id] = nil
194 self:remove()
195end
196
197function Wisp:spawned()
198 self.side = world.random(0, 2 * math.pi)
199 self.dart_in = world.random(2, 4)
200 self.look_in = 0
201 sound.play("tinkle", { at = self, volume = 0.5 })
202end
203
204function Wisp:tick(dt: number)
205 where[self.id] = self.position
206 local d = danger()
207 if d.many == 0 or knobs.how_many < 1 then
208 gone(self)
209 return
210 end
211 -- melts at sunrise
212 if world.daylight() > 0.3 then
213 sound.play("tinkle", { at = self, volume = 0.4, pitch = 0.7, caption = "A frostwisp melts" })
214 gone(self)
215 return
216 end
217 if (self.back_off or 0) > 0 then
218 self.back_off -= dt
219 return
220 end
221 self.look_in -= dt
222 if self.look_in <= 0 then
223 self.look_in = world.random(0.5, 0.8)
224 if not world.nearest_player(self.position, LONELY) then
225 gone(self)
226 return
227 end
228 local best, best_d = nil, CHASE
229 for _, p in prey() do
230 local dist = vector.magnitude(p.position - self.position)
231 if dist < best_d then
232 best, best_d = p, dist
233 end
234 end
235 self.prey = if best then best.id else nil
236 self.warm = light_near(self.position, knobs.shy)
237 if world.random() < 0.08 then
238 sound.play("tinkle", { at = self, volume = 0.35 })
239 end
240 end
241 -- keeps off a light
242 if self.warm then
243 local away = self.position - self.warm
244 local len = math.max(vector.magnitude(away), 0.1)
245 self:climb(0.5)
246 self:walk_to(self.position + vector.create(away.x / len, 0, away.z / len) * 4, 3)
247 return
248 end
249 local p = nil
250 for _, q in prey() do
251 if q.id == self.prey then
252 p = q
253 end
254 end
255 if not p then
256 self:climb(0)
257 self:wander(1.2)
258 return
259 end
260 local head = p.position.y + 1.4
261 self.dart_in -= dt
262 if self.dart_in > 0 then
263 -- circle at a distance, a little over their head
264 self:climb(math.clamp((head + 0.6 - self.position.y) * 1.2, -2.5, 2.5))
265 self.side += dt * 0.7
266 local ring = p.position + vector.create(math.cos(self.side), 0, math.sin(self.side)) * RING
267 self:walk_to(vector.create(ring.x, self.position.y, ring.z), if flat(self.position, p.position) > RING + 3 then 3.6 else 2.2)
268 self:face(p.position)
269 return
270 end
271 -- darting in
272 self:climb(math.clamp((head - self.position.y) * 2, -3, 3))
273 self:walk_to(p.position, 4.5)
274 if flat(self.position, p.position) < 1.1 and math.abs(self.position.y - head) < 1.4 then
275 self.dart_in = 3.2 * d.pace * world.random(0.8, 1.3)
276 if knobs.touch > 0 and world.hurt(p, knobs.touch * d.hurt) then
277 sound.play("chill", { at = self, volume = 0.8 })
278 chill(p, knobs.chill * d.hurt)
279 end
280 self.back_off = 0.6
281 elseif self.dart_in < -2.5 then
282 -- couldn't reach them: circle again
283 self.dart_in = 1.5
284 end
285end
286
287function Wisp:hit(player, shot)
288 local bop = if shot then shot.power else bops(world.held(player)) * world.might(player)
289 self.bopped = (self.bopped or 0) + bop
290 if self.bopped < knobs.bops then
291 local away = self.position - player.position
292 self:walk_to(self.position + vector.normalize(vector.create(away.x, 0, away.z) + vector.create(0.01, 0, 0)) * 3, 4)
293 self:climb(1.2)
294 self.back_off = 0.8
295 self.dart_in = math.max(self.dart_in or 0, 1.5)
296 sound.play("tinkle", { at = self, volume = 0.7, pitch = 1.4 })
297 return
298 end
299 sound.play("shatter", { at = self, pitch = 1.5, caption = "A frostwisp pops" })
300 world.give(player, "Frost shard", math.floor(world.random(1, 2.999)))
301 world.reward(player, 4)
302 gone(self)
303 if world.item("Moon bell") ~= nil and world.pockets(player) ~= nil then
304 ui.show(player, "find", "A frost shard. A Moon bell takes one.", { at = "bottom" })
305 world.after(4, function()
306 ui.hide(player, "find")
307 end)
308 end
309end
310
311-- open air a few blocks over the ground, out of the light
312local function air(x: number, z: number): vector?
313 local top = world.top(x, z)
314 local at = vector.create(math.floor(x) + 0.5, top + world.random(2.5, 4), math.floor(z) + 0.5)
315 if world.block(at) ~= "Air" or world.block(at + UP) ~= "Air" then
316 return nil
317 end
318 if light_near(at, knobs.shy + 2) then
319 return nil
320 end
321 return at
322end
323
324-- Every few seconds on a dark night: one more near each player out in the
325-- cold in survival, with fewer near them than the knob and the danger say.
326world.every(4, function()
327 if not dark() then
328 return
329 end
330 local d = danger()
331 local want = math.floor(knobs.how_many * d.many + 0.5)
332 if want < 1 then
333 return
334 end
335 local live = {}
336 for _, id in world.creatures(Wisp) do
337 live[id] = true
338 end
339 local total = 0
340 for id in where do
341 if live[id] then
342 total += 1
343 else
344 where[id] = nil
345 end
346 end
347 for _, p in prey() do
348 -- the Long Dusk has creatures of its own
349 if world.place(p.position) == "dusk" then
350 continue
351 end
352 if total >= MOST then
353 return
354 end
355 if cold(p.position) and not light_near(p.position, knobs.shy) then
356 local near = 0
357 for _, at in where do
358 if vector.magnitude(at - p.position) < CHASE + 4 then
359 near += 1
360 end
361 end
362 if near < want then
363 for _ = 1, 4 do
364 local a = world.random(0, 2 * math.pi)
365 local r = world.random(NEAR, FAR)
366 local at = air(p.position.x + math.cos(a) * r, p.position.z + math.sin(a) * r)
367 if at then
368 where[world.spawn(Wisp, at)] = at
369 total += 1
370 break
371 end
372 end
373 end
374 end
375 end
376end)