Sign in
creature

Cave Crawlers

by Potluck · version 1 · Checked for all ages

Many-legged things that live in the dark of caves. Underground, away from any light, they come skittering at anyone in survival, climb up after you and nip. A torch or lantern keeps them off, so light your way. Bop one three times to pop it (a sword bops harder), and it leaves silk, which a workbench spins into wool or a bowstring. None come on a Peaceful world, and fewer on a Gentle one.

1--!strict
2-- Cave Crawlers: many-legged things in the dark of caves. Underground (well
3-- below the ground over them) and away from any light, they come at anyone
4-- in survival, climb after them (a hop up whatever's in the way) and nip.
5-- A torch, lantern or candle keeps them off (world.find_light). Bop one
6-- three times to pop it, harder with a sword or a shot, and it leaves silk
7-- in the pockets of whoever popped it. How many come, and how hard they
8-- nip, go by the world's danger (world.danger, world 0.21): none on
9-- Peaceful, half on Gentle, more on Hard. Set how many to 0 for none at all.
10
11local world = require("@potluck/world@0.33")
12local tweak = require("@potluck/tweak")
13local sound = require("@potluck/sound@0.2")
14
15local knobs = tweak {
16 how_many = { 3, min = 0, max = 8, label = "How many come at you in the dark (0 for none)", order = 1 },
17 bite = { 1.5, min = 0, max = 6, label = "How hard they nip", order = 2 },
18 bops = { 3, min = 1, max = 8, label = "Bops to pop one by hand", order = 3 },
19 shy = { 5, min = 2, max = 12, label = "How far they keep from a light", order = 4 },
20 color = { "#3a3430", label = "Crawler color", order = 5 },
21 eyes = { "#c8f070", label = "Eye color", order = 6 },
22}
23
24-- How many come and how hard they nip, by the world's danger.
25local DANGER: { [string]: { many: number, hurt: number } } = {
26 peaceful = { many = 0, hurt = 0 },
27 gentle = { many = 0.5, hurt = 0.5 },
28 normal = { many = 1, hurt = 1 },
29 hard = { many = 1.5, hurt = 1.5 },
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
57-- What one leaves, from least to most.
58local SILK = { thing = "Silk", least = 1, most = 2 }
59
60local LEG = "#2a2420"
61local Crawler = world.creature {
62 name = "Cave Crawler",
63 speed = 2.8,
64 count = 0,
65 home = "here",
66 size = { 0.8, 0.45, 0.8 },
67 behaviors = { wander = false },
68 body = {
69 { "body", size = { 0.5, 0.3, 0.62 }, at = { 0, 0.34, 0.14 }, color = knobs.color },
70 { "back", size = { 0.34, 0.12, 0.42 }, at = { 0, 0.52, 0.18 }, color = knobs.color },
71 { "head", size = { 0.36, 0.24, 0.28 }, at = { 0, 0.32, -0.3 }, color = knobs.color, head = true },
72 { "eye_1", size = { 0.07, 0.07, 0.03 }, at = { -0.1, 0.38, -0.45 }, color = knobs.eyes, glow = true, head = true },
73 { "eye_2", size = { 0.07, 0.07, 0.03 }, at = { 0.1, 0.38, -0.45 }, color = knobs.eyes, glow = true, head = true },
74 { "eye_3", size = { 0.05, 0.05, 0.03 }, at = { -0.06, 0.29, -0.45 }, color = knobs.eyes, glow = true, head = true },
75 { "eye_4", size = { 0.05, 0.05, 0.03 }, at = { 0.06, 0.29, -0.45 }, color = knobs.eyes, glow = true, head = true },
76 { "fang_left", size = { 0.04, 0.1, 0.04 }, at = { -0.07, 0.17, -0.43 }, color = "#d8d0b8", head = true },
77 { "fang_right", size = { 0.04, 0.1, 0.04 }, at = { 0.07, 0.17, -0.43 }, color = "#d8d0b8", head = true },
78 { "leg_front_left", size = { 0.34, 0.06, 0.06 }, at = { -0.4, 0.22, -0.16 }, color = LEG, leg = true },
79 { "leg_front_right", size = { 0.34, 0.06, 0.06 }, at = { 0.4, 0.22, -0.16 }, color = LEG, leg = true },
80 { "leg_mid_left", size = { 0.38, 0.06, 0.06 }, at = { -0.42, 0.22, 0.08 }, color = LEG, leg = true },
81 { "leg_mid_right", size = { 0.38, 0.06, 0.06 }, at = { 0.42, 0.22, 0.08 }, color = LEG, leg = true },
82 { "leg_back_left", size = { 0.34, 0.06, 0.06 }, at = { -0.4, 0.22, 0.32 }, color = LEG, leg = true },
83 { "leg_back_right", size = { 0.34, 0.06, 0.06 }, at = { 0.4, 0.22, 0.32 }, color = LEG, leg = true },
84 { "foot_1", size = { 0.06, 0.2, 0.06 }, at = { -0.58, 0.1, -0.16 }, color = LEG, leg = true },
85 { "foot_2", size = { 0.06, 0.2, 0.06 }, at = { 0.58, 0.1, -0.16 }, color = LEG, leg = true },
86 { "foot_3", size = { 0.06, 0.2, 0.06 }, at = { -0.62, 0.1, 0.08 }, color = LEG, leg = true },
87 { "foot_4", size = { 0.06, 0.2, 0.06 }, at = { 0.62, 0.1, 0.08 }, color = LEG, leg = true },
88 { "foot_5", size = { 0.06, 0.2, 0.06 }, at = { -0.58, 0.1, 0.32 }, color = LEG, leg = true },
89 { "foot_6", size = { 0.06, 0.2, 0.06 }, at = { 0.58, 0.1, 0.32 }, color = LEG, leg = true },
90 },
91}
92
93local MOST = 40 -- at once, however many players are down there
94local NEAR, FAR = 7, 14 -- how far from a player they turn up
95local CHASE = 18 -- they follow players this close
96local LONELY = 40 -- and go back into the dark with nobody this close
97local NIP_EVERY = 1.2 -- seconds
98local DEEP = 4 -- blocks under the ground over them that counts as a cave
99
100local UP = vector.create(0, 1, 0)
101
102-- where each one is, for counting how many are near a player
103local where: { [number]: vector } = {}
104
105local function flat(a: vector, b: vector): number
106 local dx, dz = a.x - b.x, a.z - b.z
107 return math.sqrt(dx * dx + dz * dz)
108end
109
110local function solid(at: vector): boolean
111 local ok, kind = pcall(world.kind, world.block(at))
112 return ok and kind ~= "air" and kind ~= "water" and kind ~= "plant"
113end
114
115-- underground: well below the highest ground over it
116local function below_ground(at: vector): boolean
117 return at.y < world.top(at.x, at.z) - DEEP
118end
119
120-- the players in survival down in a cave this tick
121local seen_at, seen = -1, {}
122local function prey()
123 local now = world.time()
124 if now ~= seen_at then
125 seen_at, seen = now, {}
126 for _, p in world.players() do
127 if world.pockets(p) ~= nil and below_ground(p.position) then
128 table.insert(seen, p)
129 end
130 end
131 end
132 return seen
133end
134
135local function nearest(pos: vector, within: number)
136 local best, best_d = nil, within
137 for _, p in prey() do
138 local d = pos - p.position
139 local dist = vector.magnitude(d)
140 if dist < best_d then
141 best, best_d = p, dist
142 end
143 end
144 return best
145end
146
147local function away_from(self, from: vector, far: number, speed: number)
148 local d = math.max(flat(self.position, from), 0.1)
149 local away = vector.create((self.position.x - from.x) / d, 0, (self.position.z - from.z) / d)
150 self:walk_to(self.position + away * far, speed)
151end
152
153function Crawler:spawned()
154 self.nip_in = NIP_EVERY
155 self.look_in = 0
156 self.stuck = 0
157 self.was = self.position
158 sound.play("skitter", { at = self, volume = 0.5, caption = "Something skitters in the dark" })
159end
160
161local function gone(self)
162 where[self.id] = nil
163 self:remove()
164end
165
166function Crawler:tick(dt: number)
167 where[self.id] = self.position
168 local d = danger()
169 if d.many == 0 or knobs.how_many < 1 then
170 gone(self)
171 return
172 end
173 -- just bopped: keep running off a moment
174 if (self.back_off or 0) > 0 then
175 self.back_off -= dt
176 return
177 end
178 self.look_in -= dt
179 if self.look_in <= 0 then
180 self.look_in = world.random(0.7, 1.1)
181 if not world.nearest_player(self.position, LONELY) then
182 gone(self)
183 return
184 end
185 self.light = world.find_light(self.position, knobs.shy)
186 local p = nearest(self.position, CHASE)
187 self.prey = if p then p.id else nil
188 self.safe = if p then world.find_light(p.position, knobs.shy) else nil
189 -- stuck against something: climb it
190 if self.chasing and vector.magnitude(self.position - self.was) < 0.3 and self:on_ground() then
191 self:hop(1.25)
192 end
193 self.was = self.position
194 if world.random() < 0.15 then
195 sound.play("skitter", { at = self, volume = 0.35 })
196 end
197 end
198 if self.light then
199 self.chasing = false
200 away_from(self, self.light, 4, 3.4)
201 return
202 end
203 local p = nil
204 for _, q in prey() do
205 if q.id == self.prey then
206 p = q
207 end
208 end
209 if not p then
210 self.chasing = false
211 self:wander(1.4)
212 return
213 end
214 -- they're in the light: wait just out of it
215 if self.safe then
216 self.chasing = false
217 if flat(self.position, self.safe) < knobs.shy + 1.5 then
218 away_from(self, self.safe, 2, 2)
219 else
220 self:stop()
221 self:face(p.position)
222 end
223 return
224 end
225 self.nip_in -= dt
226 local close = flat(self.position, p.position)
227 if close < 1.4 and math.abs(self.position.y - p.position.y) < 1.6 then
228 self.chasing = false
229 self:face(p.position)
230 if self.nip_in <= 0 then
231 self.nip_in = NIP_EVERY
232 if knobs.bite > 0 and world.hurt(p, knobs.bite * d.hurt) then
233 sound.play("hiss", { at = self, volume = 0.7 })
234 end
235 end
236 else
237 self.chasing = true
238 self:walk_to(p.position, 2.8 + (if d.hurt > 1 then 0.6 else 0))
239 -- they're above: up after them
240 if p.position.y > self.position.y + 0.8 and close < 2.5 and self:on_ground() then
241 self:hop(1.3)
242 end
243 end
244end
245
246function Crawler:hit(player, shot)
247 local held = world.held(player)
248 -- a Keen sword hits harder (world.might); a shot's power says already
249 local bop = if shot then shot.power else bops(held) * world.might(player)
250 self.bopped = (self.bopped or 0) + bop
251 if self.bopped < knobs.bops then
252 away_from(self, player.position, 3, 4)
253 self:hop(0.5)
254 self.back_off = 1.2
255 self.nip_in = 1.5
256 sound.play("hiss", { at = self, volume = 0.6, pitch = 1.3 })
257 return
258 end
259 sound.play("pop", { at = self, pitch = 0.6, caption = "A cave crawler pops" })
260 local n = math.floor(world.random(SILK.least, SILK.most + 0.999))
261 world.give(player, SILK.thing, n)
262 world.reward(player, 3)
263 gone(self)
264end
265
266-- somewhere a crawler can come out: two blocks of air on the ground, in a
267-- cave, out of the light
268local function hole(x: number, y: number, z: number): vector?
269 local at = vector.create(math.floor(x) + 0.5, y, math.floor(z) + 0.5)
270 for dy = 3, -3, -1 do
271 local p = at + UP * dy
272 if world.block(p) == "Air" and world.block(p + UP) == "Air" and solid(p - UP) and below_ground(p) then
273 if not world.find_light(p, knobs.shy + 1) then
274 return vector.create(p.x, math.floor(p.y) + 0.05, p.z)
275 end
276 return nil
277 end
278 end
279 return nil
280end
281
282-- Every few seconds: one more near each player in survival down a cave and
283-- out of the light, with fewer near them than the knob and the danger say.
284world.every(3, function()
285 local d = danger()
286 local want = math.floor(knobs.how_many * d.many + 0.5)
287 if want < 1 then
288 return
289 end
290 -- where each one is (gone ones forgotten)
291 local live = {}
292 for _, id in world.creatures(Crawler) do
293 live[id] = true
294 end
295 local total = 0
296 for id in where do
297 if live[id] then
298 total += 1
299 else
300 where[id] = nil
301 end
302 end
303 for _, p in prey() do
304 -- the Long Dusk has creatures of its own
305 if world.place(p.position) == "dusk" then
306 continue
307 end
308 local near = 0
309 for _, at in where do
310 if vector.magnitude(at - p.position) < CHASE + 4 then
311 near += 1
312 end
313 end
314 if total < MOST and near < want and not world.find_light(p.position, knobs.shy) then
315 for _ = 1, 4 do
316 local a = world.random(0, 2 * math.pi)
317 local r = world.random(NEAR, FAR)
318 local at = hole(p.position.x + math.cos(a) * r, math.floor(p.position.y), p.position.z + math.sin(a) * r)
319 if at then
320 where[world.spawn(Crawler, at)] = at
321 total += 1
322 break
323 end
324 end
325 end
326 end
327end)