Little shadows that come out after dark and nip at anyone out in survival. They can't bear light, so a lantern by the door keeps them off, and they're gone by morning. Bop one twice to pop it, or once with a sword, and better swords bop harder. Arrows, stones from a sling and spears bop them from afar. A popped one leaves gloom dust. None on a Peaceful world, and fewer on a Gentle one.
| 1 | --!strict |
| 2 | -- Gloomlings: little shadows that come out after dark and follow anyone |
| 3 | -- out in survival, nipping at their heels. They can't bear light: they |
| 4 | -- keep away from lanterns, braziers and candles (world.find_light), lurk |
| 5 | -- at the edge of a lit place, and fade away in the morning. Bop one twice |
| 6 | -- and it pops, or once with a sword (world.held); an arrow, a stone or a |
| 7 | -- spear bops it as hard as the shot's power says (world.shoot). A popped |
| 8 | -- one leaves gloom dust. How many come and how hard they nip go by the |
| 9 | -- world's danger (world.danger, world 0.21): none on Peaceful, fewer on |
| 10 | -- Gentle. Players in creative have nothing to fear, and nothing comes near |
| 11 | -- the Green. |
| 12 | |
| 13 | local world = require("@potluck/world@0.33") |
| 14 | local tweak = require("@potluck/tweak") |
| 15 | local ui = require("@potluck/ui@0.1") |
| 16 | local sound = require("@potluck/sound@0.1") |
| 17 | |
| 18 | local knobs = tweak { |
| 19 | color = { "#221d36", label = "Gloomling color", order = 1 }, |
| 20 | eyes = { "#ffe27a", label = "Eye color", order = 2 }, |
| 21 | how_many = { 4, min = 0, max = 8, label = "How many follow you at night (0 for none)", order = 3 }, |
| 22 | bite = { 1, min = 0, max = 4, label = "How hard they nip", order = 4 }, |
| 23 | shy = { 6, min = 2, max = 12, label = "How far they keep from a light", order = 5 }, |
| 24 | bops = { 2, min = 1, max = 5, label = "Bops to pop one by hand", order = 6 }, |
| 25 | } |
| 26 | |
| 27 | -- How many come and how hard they nip, by the world's danger. |
| 28 | local DANGER: { [string]: { many: number, hurt: number } } = { |
| 29 | peaceful = { many = 0, hurt = 0 }, |
| 30 | gentle = { many = 0.5, hurt = 0.5 }, |
| 31 | normal = { many = 1, hurt = 1 }, |
| 32 | hard = { many = 1.5, hurt = 1.5 }, |
| 33 | } |
| 34 | local function danger() |
| 35 | return DANGER[world.danger()] or DANGER.gentle |
| 36 | end |
| 37 | |
| 38 | -- How hard each sword bops (a hand bops 1): a remix can add its own. |
| 39 | local SWORDS: { [string]: number } = { |
| 40 | ["Wooden sword"] = 2, |
| 41 | ["Stone sword"] = 3, |
| 42 | ["Copper sword"] = 3, |
| 43 | ["Iron sword"] = 4, |
| 44 | ["Moonsilver sword"] = 5, |
| 45 | } |
| 46 | |
| 47 | -- What's in hand bops this hard: a sword, a weapon another creation made |
| 48 | -- ([items.*] with kind = "weapon", world.item) by its power, or a hand. |
| 49 | local function bops(held: string?): number |
| 50 | if not held then |
| 51 | return 1 |
| 52 | end |
| 53 | if SWORDS[held] then |
| 54 | return SWORDS[held] |
| 55 | end |
| 56 | local it = world.item(held) |
| 57 | return if it and it.kind == "weapon" then it.power or 1 else 1 |
| 58 | end |
| 59 | |
| 60 | local Gloomling = world.creature { |
| 61 | name = "Gloomling", |
| 62 | speed = 2.6, |
| 63 | count = 0, |
| 64 | home = "here", |
| 65 | body = { |
| 66 | { "body", size = { 0.62, 0.5, 0.62 }, at = { 0, 0.32, 0 }, color = knobs.color }, |
| 67 | { "tuft", size = { 0.3, 0.14, 0.3 }, at = { 0, 0.63, 0.04 }, color = knobs.color, head = true }, |
| 68 | { "eye_left", size = { 0.13, 0.1, 0.04 }, at = { -0.14, 0.42, -0.32 }, color = knobs.eyes, glow = true, head = true }, |
| 69 | { "eye_right", size = { 0.13, 0.1, 0.04 }, at = { 0.14, 0.42, -0.32 }, color = knobs.eyes, glow = true, head = true }, |
| 70 | { "foot_left", size = { 0.16, 0.1, 0.22 }, at = { -0.16, 0.05, 0 }, color = knobs.color, leg = true }, |
| 71 | { "foot_right", size = { 0.16, 0.1, 0.22 }, at = { 0.16, 0.05, 0 }, color = knobs.color, leg = true }, |
| 72 | }, |
| 73 | } |
| 74 | |
| 75 | local MOST = 40 -- at once, however many players are out |
| 76 | local DUSK, DAWN = 0.15, 0.3 -- daylight they come out under, and go in |
| 77 | local NEAR, FAR = 12, 18 -- how far from a player they turn up |
| 78 | local CHASE = 20 -- they follow players this close |
| 79 | local LONELY = 40 -- and fade away with nobody this close |
| 80 | local NIP_EVERY = 1.5 -- seconds |
| 81 | |
| 82 | -- where each one is, for counting how many follow a player |
| 83 | local where: { [number]: vector } = {} |
| 84 | |
| 85 | local function flat(a: vector, b: vector): number |
| 86 | local dx, dz = a.x - b.x, a.z - b.z |
| 87 | return math.sqrt(dx * dx + dz * dz) |
| 88 | end |
| 89 | |
| 90 | -- the players out in survival this tick (creative ones have no pockets) |
| 91 | local seen_at, seen = -1, {} |
| 92 | local function prey() |
| 93 | local now = world.time() |
| 94 | if now ~= seen_at then |
| 95 | seen_at, seen = now, {} |
| 96 | for _, p in world.players() do |
| 97 | if world.pockets(p) ~= nil then |
| 98 | table.insert(seen, p) |
| 99 | end |
| 100 | end |
| 101 | end |
| 102 | return seen |
| 103 | end |
| 104 | |
| 105 | local function nearest(pos: vector, within: number) |
| 106 | local best, best_d = nil, within |
| 107 | for _, p in prey() do |
| 108 | local d = flat(p.position, pos) |
| 109 | if d < best_d then |
| 110 | best, best_d = p, d |
| 111 | end |
| 112 | end |
| 113 | return best |
| 114 | end |
| 115 | |
| 116 | local function fade(g, why: string?) |
| 117 | where[g.id] = nil |
| 118 | if why then |
| 119 | sound.play(why, { at = g, volume = 0.3, pitch = 0.6, caption = "A gloomling fades" }) |
| 120 | end |
| 121 | g:remove() |
| 122 | end |
| 123 | |
| 124 | function Gloomling:spawned() |
| 125 | where[self.id] = self.position |
| 126 | self.nip_in = NIP_EVERY |
| 127 | self.look_in = 0 |
| 128 | self.side = world.random(0, 2 * math.pi) -- which side of you it comes at |
| 129 | sound.play("grumble", { at = self, volume = 0.35, pitch = 1.7, caption = "A gloomling stirs" }) |
| 130 | end |
| 131 | |
| 132 | function Gloomling:tick(dt: number) |
| 133 | where[self.id] = self.position |
| 134 | -- none on a Peaceful world |
| 135 | if danger().many == 0 then |
| 136 | fade(self, "whoosh") |
| 137 | return |
| 138 | end |
| 139 | -- gone by morning, a few at a time |
| 140 | if world.daylight() > DAWN then |
| 141 | if world.random() < dt then |
| 142 | fade(self, "whoosh") |
| 143 | end |
| 144 | return |
| 145 | end |
| 146 | -- just bopped: keep running off a moment |
| 147 | if (self.back_off or 0) > 0 then |
| 148 | self.back_off -= dt |
| 149 | return |
| 150 | end |
| 151 | -- once a second: who to follow, and whether a light is near |
| 152 | self.look_in -= dt |
| 153 | if self.look_in <= 0 then |
| 154 | self.look_in = world.random(0.8, 1.2) |
| 155 | local p = nearest(self.position, LONELY) |
| 156 | if not p then |
| 157 | fade(self) |
| 158 | return |
| 159 | end |
| 160 | self.light = world.find_light(self.position, knobs.shy) |
| 161 | self.prey = if flat(p.position, self.position) < CHASE then p.id else nil |
| 162 | self.safe = if self.prey then world.find_light(p.position, knobs.shy) else nil |
| 163 | end |
| 164 | -- a light near: back off, quick |
| 165 | if self.light then |
| 166 | local d = math.max(flat(self.position, self.light), 0.1) |
| 167 | local away = vector.create((self.position.x - self.light.x) / d, 0, (self.position.z - self.light.z) / d) |
| 168 | self:walk_to(self.position + away * 4, 3.4) |
| 169 | return |
| 170 | end |
| 171 | local p = nil |
| 172 | for _, q in prey() do |
| 173 | if q.id == self.prey then |
| 174 | p = q |
| 175 | end |
| 176 | end |
| 177 | if not p then |
| 178 | self:wander(1.2) |
| 179 | return |
| 180 | end |
| 181 | -- they're in the light: wait at its edge |
| 182 | if self.safe then |
| 183 | local d = math.max(flat(self.position, self.safe), 0.1) |
| 184 | if d < knobs.shy + 2 then |
| 185 | local away = vector.create((self.position.x - self.safe.x) / d, 0, (self.position.z - self.safe.z) / d) |
| 186 | self:walk_to(self.position + away * 2, 2) |
| 187 | else |
| 188 | self:stop() |
| 189 | self:face(p.position) |
| 190 | end |
| 191 | return |
| 192 | end |
| 193 | self.nip_in -= dt |
| 194 | if self:distance_to(p.position) < 1.7 then |
| 195 | self:face(p.position) |
| 196 | if self.nip_in <= 0 then |
| 197 | self.nip_in = NIP_EVERY |
| 198 | self:hop(0.5) |
| 199 | if knobs.bite > 0 and world.hurt(p, knobs.bite * danger().hurt) then |
| 200 | sound.play("grumble", { at = self, volume = 0.7, pitch = 1.9, caption = "A gloomling nips" }) |
| 201 | end |
| 202 | end |
| 203 | else |
| 204 | -- each comes at you from its own side, so they gather around you |
| 205 | local side = self.side or 0 |
| 206 | self:walk_to(p.position + vector.create(math.cos(side), 0, math.sin(side)) * 0.9, 2.6) |
| 207 | end |
| 208 | end |
| 209 | |
| 210 | function Gloomling:hit(player, shot) |
| 211 | local held = world.held(player) |
| 212 | -- a Keen sword hits harder (world.might); a shot's power says already |
| 213 | local bop = if shot then shot.power else bops(held) * world.might(player) |
| 214 | self.bopped = (self.bopped or 0) + bop |
| 215 | if self.bopped < knobs.bops then |
| 216 | -- knocked back: it hops off and keeps away a moment |
| 217 | -- (a few blocks, however far off the one who bopped it stands) |
| 218 | local d = self.position - player.position |
| 219 | local away = self.position + d * math.min(2, 4 / math.max(vector.magnitude(d), 0.5)) |
| 220 | self:walk_to(away, 4) |
| 221 | self:hop(0.6) |
| 222 | self.back_off = 1.5 |
| 223 | self.nip_in = 2 |
| 224 | sound.play("grumble", { at = self, volume = 0.6, pitch = 2.3, caption = "A gloomling squeaks" }) |
| 225 | return |
| 226 | end |
| 227 | sound.play("pop", { at = self, pitch = 0.7, caption = "A gloomling pops" }) |
| 228 | -- what's left of it, for charms |
| 229 | world.give(player, "Gloom dust", 1) |
| 230 | world.reward(player, 2) |
| 231 | where[self.id] = nil |
| 232 | self:remove() |
| 233 | end |
| 234 | |
| 235 | -- Every two seconds after dark: one more near each survival player who has |
| 236 | -- fewer following than the knob says, somewhere dark and dry. |
| 237 | local night = false |
| 238 | world.every(2, function() |
| 239 | local dark = world.daylight() < DUSK |
| 240 | if dark and not night then |
| 241 | for _, p in prey() do |
| 242 | -- the Long Dusk has creatures of its own |
| 243 | if world.place(p.position) == "dusk" then |
| 244 | continue |
| 245 | end |
| 246 | ui.show(p, "night", "Night falls. Gloomlings keep away from light.", { at = "left" }) |
| 247 | world.after(10, function() |
| 248 | ui.hide(p, "night") |
| 249 | end) |
| 250 | end |
| 251 | end |
| 252 | night = if dark then true elseif world.daylight() > DAWN then false else night |
| 253 | if not dark then |
| 254 | return |
| 255 | end |
| 256 | -- forget the ones that went |
| 257 | local live = {} |
| 258 | for _, id in world.creatures(Gloomling) do |
| 259 | live[id] = true |
| 260 | end |
| 261 | local total = 0 |
| 262 | for id in where do |
| 263 | if live[id] then |
| 264 | total += 1 |
| 265 | else |
| 266 | where[id] = nil |
| 267 | end |
| 268 | end |
| 269 | local green = world.spot("green") |
| 270 | for _, p in prey() do |
| 271 | -- the Long Dusk has creatures of its own |
| 272 | if world.place(p.position) == "dusk" then |
| 273 | continue |
| 274 | end |
| 275 | local following = 0 |
| 276 | for _, at in where do |
| 277 | if flat(at, p.position) < CHASE + 4 then |
| 278 | following += 1 |
| 279 | end |
| 280 | end |
| 281 | if total < MOST and following < math.floor(knobs.how_many * danger().many + 0.5) and flat(p.position, green) > 30 then |
| 282 | local a = world.random(0, 2 * math.pi) |
| 283 | local r = world.random(NEAR, FAR) |
| 284 | local x, z = p.position.x + math.cos(a) * r, p.position.z + math.sin(a) * r |
| 285 | local y = world.top(x, z) |
| 286 | local at = vector.create(x, y + 1.1, z) |
| 287 | -- on the ground (grass and flowers are fine), not under water |
| 288 | local dry = world.block(vector.create(x, y + 1, z)) ~= "Water" |
| 289 | if dry and math.abs(y - p.position.y) < 10 and flat(at, green) > 30 and not world.find_light(at, knobs.shy + 2) then |
| 290 | where[world.spawn(Gloomling, at)] = at |
| 291 | total += 1 |
| 292 | end |
| 293 | end |
| 294 | end |
| 295 | end) |