Things that hang from the ceilings of deep caves on a thread, with a little glowing lure under them. They creep along the rock toward anyone in survival and drop on them from above to bite, then climb back up. Look up when you see a light where no light should be. Beat one and it leaves its lure, which a Moon bell takes, and sometimes silk. A torch, lantern or candle keeps them from dropping. None on a Peaceful world, and fewer on a Gentle one.
| 1 | --!strict |
| 2 | -- Danglers: things that hang from the ceilings of deep caves on a thread, |
| 3 | -- with a little glowing lure under them. Down in the caves (well under the |
| 4 | -- ground, below y = -10) and away from any light, one creeps along the |
| 5 | -- rock toward anyone in survival, and when they walk under it, it drops on |
| 6 | -- them, bites and climbs back up its thread. Bop it while it's down (or |
| 7 | -- shoot it up there) and beat it, and it leaves its lure, which the Gloam's |
| 8 | -- Moon bell takes, and sometimes silk. A light near it keeps it from |
| 9 | -- dropping. How many hang about and how hard they bite go by the world's |
| 10 | -- danger (world.danger): none on Peaceful. |
| 11 | |
| 12 | local world = require("@potluck/world@0.33") |
| 13 | local tweak = require("@potluck/tweak") |
| 14 | local sound = require("@potluck/sound@0.2") |
| 15 | local ui = require("@potluck/ui@0.2") |
| 16 | |
| 17 | local knobs = tweak { |
| 18 | how_many = { 2, min = 0, max = 6, label = "How many hang near you in deep caves (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 | shy = { 4, min = 2, max = 12, label = "How far a light keeps them from dropping", order = 4 }, |
| 22 | lure = { "#80e8d8", label = "Lure color", order = 5 }, |
| 23 | } |
| 24 | |
| 25 | local 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 | } |
| 31 | local function danger() |
| 32 | return DANGER[world.danger()] or DANGER.gentle |
| 33 | end |
| 34 | |
| 35 | local 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 | |
| 43 | local 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 |
| 52 | end |
| 53 | |
| 54 | local LIGHTS: { [string]: boolean } = { Torch = true, Lantern = true, Candle = true, Brazier = true } |
| 55 | |
| 56 | local HIDE = "#3a3048" |
| 57 | local DARK = "#221c2c" |
| 58 | local EYE = "#f0fff8" |
| 59 | local PUPIL = "#120e18" |
| 60 | |
| 61 | local Dangler = world.creature { |
| 62 | name = "Dangler", |
| 63 | speed = 1, |
| 64 | count = 0, |
| 65 | home = "here", |
| 66 | flies = true, |
| 67 | size = { 0.6, 1.2, 0.6 }, |
| 68 | behaviors = { wander = false }, |
| 69 | body = { |
| 70 | { "thread", size = { 0.03, 0.4, 0.03 }, at = { 0, 1.22, 0 }, color = "#c8c0d0" }, |
| 71 | { "body", size = { 0.5, 0.46, 0.5 }, at = { 0, 0.82, 0.02 }, color = HIDE }, |
| 72 | { "spots", size = { 0.52, 0.1, 0.4 }, at = { 0, 0.9, 0.04 }, color = DARK }, |
| 73 | { "head", size = { 0.44, 0.34, 0.4 }, at = { 0, 0.44, -0.02 }, color = HIDE, head = true }, |
| 74 | { "eye_left", size = { 0.15, 0.16, 0.03 }, at = { -0.1, 0.48, -0.23 }, color = EYE, glow = true, head = true }, |
| 75 | { "pupil_left", size = { 0.08, 0.09, 0.03 }, at = { -0.09, 0.47, -0.25 }, color = PUPIL, head = true }, |
| 76 | { "eye_right", size = { 0.15, 0.16, 0.03 }, at = { 0.1, 0.48, -0.23 }, color = EYE, glow = true, head = true }, |
| 77 | { "pupil_right", size = { 0.08, 0.09, 0.03 }, at = { 0.09, 0.47, -0.25 }, color = PUPIL, head = true }, |
| 78 | { "fangs", size = { 0.2, 0.06, 0.03 }, at = { 0, 0.32, -0.23 }, color = "#e8e0d0", head = true }, |
| 79 | { "leg_1", size = { 0.06, 0.44, 0.06 }, at = { -0.32, 0.86, -0.18 }, color = DARK, leg = true }, |
| 80 | { "leg_2", size = { 0.06, 0.44, 0.06 }, at = { 0.32, 0.86, -0.18 }, color = DARK, leg = true }, |
| 81 | { "leg_3", size = { 0.06, 0.44, 0.06 }, at = { -0.32, 0.86, 0.2 }, color = DARK, leg = true }, |
| 82 | { "leg_4", size = { 0.06, 0.44, 0.06 }, at = { 0.32, 0.86, 0.2 }, color = DARK, leg = true }, |
| 83 | { "stalk", size = { 0.03, 0.2, 0.03 }, at = { 0, 0.18, -0.16 }, color = DARK, head = true }, |
| 84 | { "bulb", size = { 0.13, 0.13, 0.13 }, at = { 0, 0.06, -0.16 }, color = knobs.lure, glow = true, head = true }, |
| 85 | }, |
| 86 | } |
| 87 | |
| 88 | local MOST = 24 -- at once in the whole world |
| 89 | local DEEP = -10 -- only below this |
| 90 | local ROCK = 8 -- with this much ground over them |
| 91 | local NEAR, FAR = 6, 14 -- how far from a player they turn up |
| 92 | local CREEP = 12 -- they creep toward players this close |
| 93 | local LONELY = 40 -- and go with nobody this close |
| 94 | local UNDER = 1.6 -- how close under it sets one off |
| 95 | local UP = vector.create(0, 1, 0) |
| 96 | |
| 97 | local where: { [number]: vector } = {} |
| 98 | |
| 99 | local function flat(a: vector, b: vector): number |
| 100 | local dx, dz = a.x - b.x, a.z - b.z |
| 101 | return math.sqrt(dx * dx + dz * dz) |
| 102 | end |
| 103 | |
| 104 | local function solid(at: vector): boolean |
| 105 | local ok, kind = pcall(world.kind, world.block(at)) |
| 106 | return ok and kind ~= "air" and kind ~= "water" and kind ~= "plant" |
| 107 | end |
| 108 | |
| 109 | local function deep(at: vector): boolean |
| 110 | return at.y < DEEP and at.y < world.top(at.x, at.z) - ROCK |
| 111 | end |
| 112 | |
| 113 | local seen_at, seen = -1, {} |
| 114 | local function prey() |
| 115 | local now = world.time() |
| 116 | if now ~= seen_at then |
| 117 | seen_at, seen = now, {} |
| 118 | for _, p in world.players() do |
| 119 | if world.pockets(p) ~= nil and deep(p.position) then |
| 120 | table.insert(seen, p) |
| 121 | end |
| 122 | end |
| 123 | end |
| 124 | return seen |
| 125 | end |
| 126 | |
| 127 | local function light_near(at: vector, within: number): boolean |
| 128 | if world.find_light(at, within) then |
| 129 | return true |
| 130 | end |
| 131 | for _, p in world.players() do |
| 132 | local held = world.held(p) |
| 133 | if held and LIGHTS[held] and vector.magnitude(p.position - at) < within then |
| 134 | return true |
| 135 | end |
| 136 | end |
| 137 | return false |
| 138 | end |
| 139 | |
| 140 | local function by_id(id: number?) |
| 141 | if not id then |
| 142 | return nil |
| 143 | end |
| 144 | for _, q in world.players() do |
| 145 | if q.id == id then |
| 146 | return q |
| 147 | end |
| 148 | end |
| 149 | return nil |
| 150 | end |
| 151 | |
| 152 | local function gone(self) |
| 153 | where[self.id] = nil |
| 154 | self:remove() |
| 155 | end |
| 156 | |
| 157 | -- where it hangs at a place: its feet a body's height under the rock, or |
| 158 | -- nil if there's no rock over it to hang from |
| 159 | local function hang_at(x: number, y: number, z: number): number? |
| 160 | local fx, fz = math.floor(x) + 0.5, math.floor(z) + 0.5 |
| 161 | for dy = 0, 4 do |
| 162 | local at = vector.create(fx, math.floor(y) + dy, fz) |
| 163 | if world.block(at) ~= "Air" then |
| 164 | return nil |
| 165 | end |
| 166 | if solid(at + UP) then |
| 167 | return at.y + 1 - 1.3 |
| 168 | end |
| 169 | end |
| 170 | return nil |
| 171 | end |
| 172 | |
| 173 | function Dangler:spawned() |
| 174 | self.state = "hang" |
| 175 | self.top = self.position.y |
| 176 | self.look_in = 0 |
| 177 | self.wait = 0 |
| 178 | end |
| 179 | |
| 180 | local function drop(self, p) |
| 181 | self.state = "drop" |
| 182 | self.prey = p.id |
| 183 | self.bit = false |
| 184 | self.wait = 0 |
| 185 | sound.play("drop", { at = self, volume = 0.9 }) |
| 186 | end |
| 187 | |
| 188 | function Dangler:tick(dt: number) |
| 189 | where[self.id] = self.position |
| 190 | local d = danger() |
| 191 | if d.many == 0 or knobs.how_many < 1 then |
| 192 | gone(self) |
| 193 | return |
| 194 | end |
| 195 | self.look_in -= dt |
| 196 | if self.look_in <= 0 then |
| 197 | self.look_in = world.random(0.4, 0.6) |
| 198 | if not world.nearest_player(self.position, LONELY) then |
| 199 | gone(self) |
| 200 | return |
| 201 | end |
| 202 | self.lit = light_near(self.position, knobs.shy) |
| 203 | if world.random() < 0.05 then |
| 204 | sound.play("tick", { at = self, volume = 0.4 }) |
| 205 | end |
| 206 | end |
| 207 | if self.state == "drop" then |
| 208 | local p = by_id(self.prey) |
| 209 | self:stop() |
| 210 | if not p then |
| 211 | self.state = "climb" |
| 212 | return |
| 213 | end |
| 214 | local head = p.position.y + 1.4 |
| 215 | if not self.bit and self.position.y > head and not self:on_ground() then |
| 216 | self:climb(-4) |
| 217 | return |
| 218 | end |
| 219 | self:climb(0) |
| 220 | if not self.bit then |
| 221 | self.bit = true |
| 222 | if flat(self.position, p.position) < 1.6 and math.abs(self.position.y - head) < 2 and knobs.bite > 0 then |
| 223 | if world.hurt(p, knobs.bite * d.hurt) then |
| 224 | sound.play("nip", { at = self, volume = 0.9 }) |
| 225 | end |
| 226 | end |
| 227 | end |
| 228 | self.wait += dt |
| 229 | if self.wait > 1 then |
| 230 | self.state = "climb" |
| 231 | end |
| 232 | return |
| 233 | end |
| 234 | if self.state == "climb" then |
| 235 | if self.position.y < self.top - 0.1 then |
| 236 | self:climb(2.5) |
| 237 | return |
| 238 | end |
| 239 | self:climb(0) |
| 240 | self.state = "hang" |
| 241 | return |
| 242 | end |
| 243 | -- hanging: hold its place under the rock |
| 244 | self:climb(math.clamp((self.top - self.position.y) * 2, -1, 1)) |
| 245 | if self.lit then |
| 246 | self:stop() |
| 247 | return |
| 248 | end |
| 249 | local best, best_d = nil, CREEP |
| 250 | for _, p in prey() do |
| 251 | local dist = flat(self.position, p.position) |
| 252 | if dist < best_d and p.position.y < self.top and self.top - p.position.y < 8 then |
| 253 | best, best_d = p, dist |
| 254 | end |
| 255 | end |
| 256 | if not best then |
| 257 | self:stop() |
| 258 | return |
| 259 | end |
| 260 | if best_d < UNDER then |
| 261 | drop(self, best) |
| 262 | return |
| 263 | end |
| 264 | -- creep along the rock toward them, where there's rock to hang from |
| 265 | local to = best.position - self.position |
| 266 | local step = self.position + vector.normalize(vector.create(to.x, 0, to.z) + vector.create(0.001, 0, 0)) * 0.8 |
| 267 | local top = hang_at(step.x, self.top, step.z) |
| 268 | if top and math.abs(top - self.top) < 1.1 then |
| 269 | self.top = top |
| 270 | self:walk_to(vector.create(step.x, self.position.y, step.z), 1) |
| 271 | else |
| 272 | self:stop() |
| 273 | end |
| 274 | end |
| 275 | |
| 276 | function Dangler:hit(player, shot) |
| 277 | local bop = if shot then shot.power else bops(world.held(player)) * world.might(player) |
| 278 | self.bopped = (self.bopped or 0) + bop |
| 279 | if self.bopped < knobs.bops then |
| 280 | sound.play("tick", { at = self, volume = 0.8, pitch = 1.3 }) |
| 281 | -- poked while it hangs: it comes down at whoever did it |
| 282 | if self.state == "hang" and world.pockets(player) ~= nil then |
| 283 | drop(self, player) |
| 284 | end |
| 285 | return |
| 286 | end |
| 287 | sound.play("pop", { at = self, pitch = 0.6, caption = "A dangler is beaten" }) |
| 288 | world.give(player, "Dangler lure", 1) |
| 289 | if world.random() < 0.5 then |
| 290 | world.give(player, "Silk", 1) |
| 291 | end |
| 292 | world.reward(player, 5) |
| 293 | gone(self) |
| 294 | if world.item("Moon bell") ~= nil and world.pockets(player) ~= nil then |
| 295 | ui.show(player, "find", "A dangler lure. A Moon bell takes one.", { at = "bottom" }) |
| 296 | world.after(4, function() |
| 297 | ui.hide(player, "find") |
| 298 | end) |
| 299 | end |
| 300 | end |
| 301 | |
| 302 | -- Every few seconds: one more near each player deep in a cave in survival |
| 303 | -- and out of the light, with fewer near them than the knob and the danger |
| 304 | -- say: on the ceiling, with room under it to drop. |
| 305 | world.every(4, function() |
| 306 | local d = danger() |
| 307 | local want = math.floor(knobs.how_many * d.many + 0.5) |
| 308 | if want < 1 then |
| 309 | return |
| 310 | end |
| 311 | local live = {} |
| 312 | for _, id in world.creatures(Dangler) do |
| 313 | live[id] = true |
| 314 | end |
| 315 | local total = 0 |
| 316 | for id in where do |
| 317 | if live[id] then |
| 318 | total += 1 |
| 319 | else |
| 320 | where[id] = nil |
| 321 | end |
| 322 | end |
| 323 | for _, p in prey() do |
| 324 | -- the Long Dusk has creatures of its own |
| 325 | if world.place(p.position) == "dusk" then |
| 326 | continue |
| 327 | end |
| 328 | if total >= MOST then |
| 329 | return |
| 330 | end |
| 331 | local near = 0 |
| 332 | for _, at in where do |
| 333 | if vector.magnitude(at - p.position) < CREEP + 6 then |
| 334 | near += 1 |
| 335 | end |
| 336 | end |
| 337 | if near < want and not light_near(p.position, knobs.shy + 2) then |
| 338 | for _ = 1, 5 do |
| 339 | local a = world.random(0, 2 * math.pi) |
| 340 | local r = world.random(NEAR, FAR) |
| 341 | 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 |
| 342 | local top = hang_at(x, p.position.y + 1, z) |
| 343 | if top and top > p.position.y + 1.5 then |
| 344 | local at = vector.create(x, top, z) |
| 345 | if not light_near(at, knobs.shy + 1) then |
| 346 | where[world.spawn(Dangler, at)] = at |
| 347 | total += 1 |
| 348 | break |
| 349 | end |
| 350 | end |
| 351 | end |
| 352 | end |
| 353 | end |
| 354 | end) |