Sign in
creature

Ember Imps

by Potluck · version 1 · Checked for all ages

Little fiery things that flit about the deep caverns, far below the ground. They keep their distance and throw embers at anyone in survival: an ember stings, and where it lands it may start a small fire. Pop one with two bops, or better an arrow, and it leaves an ember, which burns in a furnace and lights a fire like flint and steel. None on a Peaceful world, and fewer on a Gentle one.

1--!strict
2-- Ember Imps: little fiery things that flit about the deep caverns (more
3-- than 40 blocks down, and well under the ground over them). They fly
4-- (flies), keep a few blocks off anyone in survival, and throw embers at
5-- them (self:throw, world 0.21): an ember stings, and where it comes down
6-- it may start a small fire that soon goes out. Bop one twice to pop it,
7-- harder with a sword or a shot, and it leaves an ember in the pockets of
8-- whoever popped it. How many come, how often they throw and how hard
9-- embers sting go by the world's danger (world.danger): none on Peaceful.
10
11local world = require("@potluck/world@0.34")
12local tweak = require("@potluck/tweak")
13local store = require("@potluck/store")
14local sound = require("@potluck/sound@0.2")
15
16local knobs = tweak {
17 how_many = { 2, min = 0, max = 6, label = "How many flit about you deep down (0 for none)", order = 1 },
18 burn = { 2, min = 0, max = 8, label = "How hard an ember stings", order = 2 },
19 every = { 3.5, min = 1, max = 12, label = "Seconds between embers", order = 3 },
20 fires = { true, label = "Embers start small fires", order = 4 },
21 bops = { 2, min = 1, max = 6, label = "Bops to pop one by hand", order = 5 },
22}
23
24-- How many come, how often they throw and how hard, by the world's danger.
25local DANGER: { [string]: { many: number, hurt: number, pace: number } } = {
26 peaceful = { many = 0, hurt = 0, pace = 1 },
27 gentle = { many = 0.5, hurt = 0.5, pace = 1.4 },
28 normal = { many = 1, hurt = 1, pace = 1 },
29 hard = { many = 1.5, hurt = 1.5, pace = 0.7 },
30}
31local function danger()
32 return DANGER[world.danger()] or DANGER.gentle
33end
34
35-- How hard each sword bops (a hand bops 1): a remix can add its own.
36local SWORDS: { [string]: number } = {
37 ["Wooden sword"] = 2,
38 ["Stone sword"] = 3,
39 ["Copper sword"] = 3,
40 ["Iron sword"] = 4,
41 ["Moonsilver sword"] = 5,
42}
43
44-- What's in hand bops this hard: a sword, a weapon another creation made
45-- ([items.*] with kind = "weapon", world.item) by its power, or a hand.
46local function bops(held: string?): number
47 if not held then
48 return 1
49 end
50 if SWORDS[held] then
51 return SWORDS[held]
52 end
53 local it = world.item(held)
54 return if it and it.kind == "weapon" then it.power or 1 else 1
55end
56
57local SKIN = "#c8401c"
58local HORN = "#3a1408"
59local Imp = world.creature {
60 name = "Ember Imp",
61 speed = 3,
62 count = 0,
63 home = "here",
64 flies = true,
65 size = { 0.5, 0.7, 0.5 },
66 behaviors = { wander = false },
67 body = {
68 { "body", size = { 0.34, 0.36, 0.26 }, at = { 0, 0.32, 0 }, color = SKIN },
69 { "head", size = { 0.36, 0.32, 0.32 }, at = { 0, 0.66, -0.02 }, color = SKIN, head = true },
70 { "horn_left", size = { 0.06, 0.14, 0.06 }, at = { -0.13, 0.88, 0 }, color = HORN, head = true },
71 { "horn_right", size = { 0.06, 0.14, 0.06 }, at = { 0.13, 0.88, 0 }, color = HORN, head = true },
72 { "eye_left", size = { 0.08, 0.06, 0.03 }, at = { -0.08, 0.7, -0.19 }, color = "#ffe060", glow = true, head = true },
73 { "eye_right", size = { 0.08, 0.06, 0.03 }, at = { 0.08, 0.7, -0.19 }, color = "#ffe060", glow = true, head = true },
74 { "grin", size = { 0.16, 0.03, 0.03 }, at = { 0, 0.58, -0.19 }, color = "#ffb030", glow = true, head = true },
75 { "flame", size = { 0.16, 0.18, 0.16 }, at = { 0, 0.96, 0.06 }, color = "#ffa020", glow = true, head = true },
76 { "wing_left", size = { 0.32, 0.22, 0.03 }, at = { -0.3, 0.44, 0.15 }, color = "#7a2010", leg = true },
77 { "wing_right", size = { 0.32, 0.22, 0.03 }, at = { 0.3, 0.44, 0.15 }, color = "#7a2010", leg = true },
78 { "tail", size = { 0.05, 0.05, 0.3 }, at = { 0, 0.2, 0.26 }, color = SKIN },
79 { "tail_tip", size = { 0.1, 0.1, 0.08 }, at = { 0, 0.2, 0.42 }, color = "#ffa020", glow = true },
80 { "leg_left", size = { 0.08, 0.16, 0.08 }, at = { -0.09, 0.08, 0 }, color = SKIN },
81 { "leg_right", size = { 0.08, 0.16, 0.08 }, at = { 0.09, 0.08, 0 }, color = SKIN },
82 },
83}
84
85local MOST = 24 -- at once, however many players are down there
86local DEEP = -40 -- only below this
87local NEAR, FAR = 8, 16 -- how far from a player they turn up
88local CHASE = 22 -- they go after players this close
89local LONELY = 40 -- and go with nobody this close
90local KEEP = 7 -- how far off they like to stay
91local REACH = 14 -- the farthest they throw
92local FIRE = "Fire"
93local BURNS = 8 -- seconds a fire an ember started burns
94
95local UP = vector.create(0, 1, 0)
96local SIDES = { UP, vector.create(1, 0, 0), vector.create(-1, 0, 0), vector.create(0, 0, 1), vector.create(0, 0, -1), vector.create(0, -1, 0) }
97
98local where: { [number]: vector } = {}
99
100local function flat(a: vector, b: vector): number
101 local dx, dz = a.x - b.x, a.z - b.z
102 return math.sqrt(dx * dx + dz * dz)
103end
104
105local function deep(at: vector): boolean
106 return at.y < DEEP and at.y < world.top(at.x, at.z) - 6
107end
108
109local function prey()
110 local list = {}
111 for _, p in world.players() do
112 if world.pockets(p) ~= nil and deep(p.position) then
113 table.insert(list, p)
114 end
115 end
116 return list
117end
118
119local function gone(self)
120 where[self.id] = nil
121 self:remove()
122end
123
124-- ---- the little fires embers start, which go out by themselves ----
125
126type Lit = { at: vector, out: number }
127local lit: { Lit } = {}
128do
129 local now = world.time()
130 for _, f in (store.get("fires") or {}) :: { { number } } do
131 table.insert(lit, { at = vector.create(f[1], f[2], f[3]), out = now + BURNS })
132 end
133end
134
135local function keep()
136 local list = {}
137 for _, f in lit do
138 table.insert(list, { f.at.x, f.at.y, f.at.z })
139 end
140 store.set("fires", list)
141end
142
143local function burnable(at: vector): boolean
144 if world.block(at) ~= "Air" then
145 return false
146 end
147 for _, d in SIDES do
148 if world.block(at + d) == "Water" then
149 return false
150 end
151 end
152 local ok, kind = pcall(world.kind, world.block(at - UP))
153 return ok and kind ~= "air" and kind ~= "water"
154end
155
156-- a small fire at or beside where an ember came down, if fire is in this
157-- world (the Fire creation's block), its rules let creatures start fires
158-- (world.rule, world 0.28) and there's room
159local function kindle(at: vector)
160 if not knobs.fires or not world.rule("mob_griefing") or #lit >= 32 or not pcall(world.kind, FIRE) then
161 return
162 end
163 local here = vector.create(math.floor(at.x), math.floor(at.y), math.floor(at.z))
164 for _, d in SIDES do
165 local p = here + d
166 if burnable(p) then
167 world.set_block(p, FIRE)
168 table.insert(lit, { at = p, out = world.time() + BURNS * world.random(0.8, 1.3) })
169 keep()
170 return
171 end
172 end
173end
174
175world.every(1, function()
176 if #lit == 0 then
177 return
178 end
179 local now, left = world.time(), {}
180 for _, f in lit do
181 if now >= f.out then
182 if world.block(f.at) == FIRE then
183 world.set_block(f.at, "Air")
184 end
185 elseif world.block(f.at) == FIRE then
186 table.insert(left, f)
187 end
188 end
189 if #left ~= #lit then
190 lit = left
191 keep()
192 end
193end)
194
195-- ---- imps ----
196
197function Imp:spawned()
198 self.throw_in = world.random(1.5, 3)
199 self.side = world.random(0, 2 * math.pi)
200 self.look_in = 0
201 sound.play("cackle", { at = self, volume = 0.6 })
202end
203
204local function sting(at: vector, block: string?, creature: number?, player)
205 local d = danger()
206 if player then
207 -- fireproof (a Cinderward tonic or an Ashen piece) shrugs the sting off
208 if knobs.burn > 0 and world.perk(player, "fireproof") == 0 and world.hurt(player, knobs.burn * d.hurt) then
209 sound.play_for(player, "fling", { caption = "An ember stings you", volume = 0.8 })
210 end
211 if world.random() < 0.3 then
212 kindle(player.position)
213 end
214 elseif block then
215 kindle(at)
216 end
217end
218
219function Imp:tick(dt: number)
220 where[self.id] = self.position
221 local d = danger()
222 if d.many == 0 or knobs.how_many < 1 then
223 gone(self)
224 return
225 end
226 if (self.back_off or 0) > 0 then
227 self.back_off -= dt
228 return
229 end
230 self.look_in -= dt
231 if self.look_in <= 0 then
232 self.look_in = world.random(0.5, 0.8)
233 if not world.nearest_player(self.position, LONELY) then
234 gone(self)
235 return
236 end
237 local best, best_d = nil, CHASE
238 for _, p in prey() do
239 local dist = vector.magnitude(p.position - self.position)
240 if dist < best_d then
241 best, best_d = p, dist
242 end
243 end
244 self.prey = if best then best.id else nil
245 end
246 local p = nil
247 for _, q in prey() do
248 if q.id == self.prey then
249 p = q
250 end
251 end
252 if not p then
253 self:climb(0)
254 self:wander(1.5)
255 return
256 end
257 -- hover a little above them, and circle at a distance
258 local want_y = p.position.y + 2.2
259 self:climb(math.clamp((want_y - self.position.y) * 1.2, -2.5, 2.5))
260 self.side += dt * 0.4
261 local ring = p.position + vector.create(math.cos(self.side), 0, math.sin(self.side)) * KEEP
262 self:walk_to(vector.create(ring.x, self.position.y, ring.z), if flat(self.position, p.position) > KEEP + 4 then 3.4 else 2.2)
263 self:face(p.position)
264 self.throw_in -= dt
265 if self.throw_in <= 0 then
266 self.throw_in = knobs.every * d.pace * world.random(0.8, 1.25)
267 if vector.magnitude(p.position - self.position) < REACH then
268 local ok = pcall(function()
269 self:throw({ at = p.position + UP * 1.1, look = "ember", speed = 13, drop = 0.3, power = 1 }, sting)
270 end)
271 if ok then
272 sound.play("fling", { at = self, volume = 0.7 })
273 end
274 end
275 end
276end
277
278function Imp:hit(player, shot)
279 local held = world.held(player)
280 -- a Keen sword hits harder (world.might); a shot's power says already
281 local bop = if shot then shot.power else bops(held) * world.might(player)
282 self.bopped = (self.bopped or 0) + bop
283 if self.bopped < knobs.bops then
284 local away = self.position - player.position
285 self:walk_to(self.position + vector.normalize(vector.create(away.x, 0, away.z) + vector.create(0.01, 0, 0)) * 4, 4)
286 self:climb(1.5)
287 self.back_off = 1
288 sound.play("cackle", { at = self, volume = 0.7, pitch = 1.4 })
289 return
290 end
291 sound.play("pop", { at = self, pitch = 1.3, caption = "An ember imp pops" })
292 world.give(player, "Ember", math.floor(world.random(1, 2.999)))
293 world.reward(player, 4)
294 gone(self)
295end
296
297-- somewhere an imp can come out: a cavern's open air, three blocks high
298local function open_air(x: number, y: number, z: number): vector?
299 local at = vector.create(math.floor(x) + 0.5, y, math.floor(z) + 0.5)
300 for dy = 4, -2, -1 do
301 local p = at + UP * dy
302 if world.block(p) == "Air" and world.block(p + UP) == "Air" and world.block(p - UP) == "Air" and deep(p) then
303 return vector.create(p.x, math.floor(p.y) + 0.2, p.z)
304 end
305 end
306 return nil
307end
308
309-- Every few seconds: one more near each player in survival deep down,
310-- with fewer near them than the knob and the danger say.
311world.every(4, function()
312 local d = danger()
313 local want = math.floor(knobs.how_many * d.many + 0.5)
314 if want < 1 then
315 return
316 end
317 local live = {}
318 for _, id in world.creatures(Imp) do
319 live[id] = true
320 end
321 local total = 0
322 for id in where do
323 if live[id] then
324 total += 1
325 else
326 where[id] = nil
327 end
328 end
329 for _, p in prey() do
330 -- the Long Dusk has creatures of its own
331 if world.place(p.position) == "dusk" then
332 continue
333 end
334 local near = 0
335 for _, at in where do
336 if vector.magnitude(at - p.position) < CHASE + 4 then
337 near += 1
338 end
339 end
340 if total < MOST and near < want then
341 for _ = 1, 4 do
342 local a = world.random(0, 2 * math.pi)
343 local r = world.random(NEAR, FAR)
344 local at = open_air(p.position.x + math.cos(a) * r, math.floor(p.position.y), p.position.z + math.sin(a) * r)
345 if at then
346 where[world.spawn(Imp, at)] = at
347 total += 1
348 break
349 end
350 end
351 end
352 end
353end)