Heaps of peat and reeds that heave up out of the ground on dark nights, wherever you're out under the sky in survival with no light about. They're slow but they hit hard, and they sink back into the ground at sunrise. A lantern, torch or campfire keeps them from rising near you. They're earth, so a shovel breaks them up twice as fast, and a broken bogle leaves peat, which burns long in a furnace. None on a Peaceful world, and fewer on a Gentle one.
| 1 | --!strict |
| 2 | -- Bogles: heaps of peat and reeds that heave up out of soft ground on dark |
| 3 | -- nights, wherever someone in survival is out under the sky away from any |
| 4 | -- light (a lantern, torch or campfire keeps them from rising near you, and |
| 5 | -- they won't come close to one). They lumber after you, slow but heavy |
| 6 | -- handed, and sink back into the ground when the sun comes up. Bop one |
| 7 | -- enough to break it up: they're earth, so a shovel bops them twice as |
| 8 | -- hard. A broken bogle leaves peat, which burns long in a furnace, and |
| 9 | -- sometimes reeds. How many rise and how hard they hit 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 | |
| 16 | local knobs = tweak { |
| 17 | how_many = { 2, min = 0, max = 6, label = "How many rise near you on a dark night (0 for none)", order = 1 }, |
| 18 | hits = { 3, min = 0, max = 8, label = "How hard they hit", order = 2 }, |
| 19 | bops = { 6, min = 2, max = 16, label = "Bops to break one by hand", order = 3 }, |
| 20 | shy = { 5, min = 2, max = 12, label = "How far they keep from a light", order = 4 }, |
| 21 | color = { "#4a3a28", label = "Peat color", order = 5 }, |
| 22 | } |
| 23 | |
| 24 | local DANGER: { [string]: { many: number, hurt: number, fast: number } } = { |
| 25 | peaceful = { many = 0, hurt = 0, fast = 0 }, |
| 26 | gentle = { many = 0.5, hurt = 0.5, fast = 1.3 }, |
| 27 | normal = { many = 1, hurt = 1, fast = 1.6 }, |
| 28 | hard = { many = 1.5, hurt = 1.5, fast = 1.9 }, |
| 29 | } |
| 30 | local function danger() |
| 31 | return DANGER[world.danger()] or DANGER.gentle |
| 32 | end |
| 33 | |
| 34 | local SWORDS: { [string]: number } = { |
| 35 | ["Wooden sword"] = 2, |
| 36 | ["Stone sword"] = 3, |
| 37 | ["Copper sword"] = 3, |
| 38 | ["Iron sword"] = 4, |
| 39 | ["Moonsilver sword"] = 5, |
| 40 | } |
| 41 | |
| 42 | -- what's in hand bops this hard: a sword, a weapon another creation made |
| 43 | -- by its power (world.item), or a hand; any shovel bops twice as hard as a |
| 44 | -- hand or its kind of sword would |
| 45 | local function bops(held: string?): number |
| 46 | if not held then |
| 47 | return 1 |
| 48 | end |
| 49 | if string.find(string.lower(held), "shovel") then |
| 50 | return 2.5 |
| 51 | end |
| 52 | if SWORDS[held] then |
| 53 | return SWORDS[held] |
| 54 | end |
| 55 | local it = world.item(held) |
| 56 | return if it and it.kind == "weapon" then it.power or 1 else 1 |
| 57 | end |
| 58 | |
| 59 | local REED = "#8a9a48" |
| 60 | local MUD = "#2e2418" |
| 61 | local EYE = "#d8e070" |
| 62 | |
| 63 | local Mound = world.creature { |
| 64 | name = "Bogle (rising)", |
| 65 | speed = 0, |
| 66 | count = 0, |
| 67 | home = "here", |
| 68 | size = { 0.9, 0.5, 0.9 }, |
| 69 | behaviors = { wander = false }, |
| 70 | body = { |
| 71 | { "heap", size = { 1.0, 0.4, 1.0 }, at = { 0, 0.2, 0 }, color = knobs.color }, |
| 72 | { "top", size = { 0.6, 0.2, 0.6 }, at = { 0, 0.5, 0 }, color = MUD }, |
| 73 | { "reed_1", size = { 0.05, 0.4, 0.05 }, at = { -0.2, 0.6, 0.1 }, color = REED }, |
| 74 | { "reed_2", size = { 0.05, 0.3, 0.05 }, at = { 0.25, 0.55, -0.15 }, color = REED }, |
| 75 | }, |
| 76 | } |
| 77 | |
| 78 | local Bogle = world.creature { |
| 79 | name = "Bogle", |
| 80 | speed = 1.6, |
| 81 | count = 0, |
| 82 | home = "here", |
| 83 | size = { 0.9, 1.9, 0.9 }, |
| 84 | behaviors = { wander = false }, |
| 85 | body = { |
| 86 | { "base", size = { 1.0, 0.5, 0.9 }, at = { 0, 0.25, 0 }, color = knobs.color, leg = true }, |
| 87 | { "body", size = { 0.9, 0.8, 0.75 }, at = { 0, 0.9, 0 }, color = knobs.color }, |
| 88 | { "head", size = { 0.7, 0.5, 0.6 }, at = { 0, 1.55, -0.05 }, color = MUD, head = true }, |
| 89 | { "eye_left", size = { 0.12, 0.08, 0.04 }, at = { -0.15, 1.6, -0.36 }, color = EYE, glow = true, head = true }, |
| 90 | { "eye_right", size = { 0.12, 0.08, 0.04 }, at = { 0.15, 1.6, -0.36 }, color = EYE, glow = true, head = true }, |
| 91 | { "mouth", size = { 0.3, 0.06, 0.04 }, at = { 0, 1.4, -0.36 }, color = "#140e08", head = true }, |
| 92 | { "arm_left", size = { 0.28, 0.8, 0.3 }, at = { -0.6, 0.85, -0.1 }, color = knobs.color, leg = true }, |
| 93 | { "arm_right", size = { 0.28, 0.8, 0.3 }, at = { 0.6, 0.85, -0.1 }, color = knobs.color, leg = true }, |
| 94 | { "fist_left", size = { 0.32, 0.28, 0.34 }, at = { -0.62, 0.38, -0.14 }, color = MUD, leg = true }, |
| 95 | { "fist_right", size = { 0.32, 0.28, 0.34 }, at = { 0.62, 0.38, -0.14 }, color = MUD, leg = true }, |
| 96 | { "reed_1", size = { 0.05, 0.5, 0.05 }, at = { -0.2, 1.95, 0.1 }, color = REED, head = true }, |
| 97 | { "reed_2", size = { 0.05, 0.4, 0.05 }, at = { 0.18, 1.9, -0.05 }, color = REED, head = true }, |
| 98 | { "reed_3", size = { 0.05, 0.36, 0.05 }, at = { 0.3, 1.2, 0.3 }, color = REED }, |
| 99 | { "moss", size = { 0.5, 0.1, 0.4 }, at = { -0.1, 1.33, 0.2 }, color = "#4a6a30" }, |
| 100 | }, |
| 101 | } |
| 102 | |
| 103 | local MOST = 30 |
| 104 | local NEAR, FAR = 10, 20 |
| 105 | local CHASE = 24 |
| 106 | local LONELY = 44 |
| 107 | local HIT_EVERY = 1.8 |
| 108 | local RISE = 2 -- seconds to heave up out of the ground |
| 109 | local UP = vector.create(0, 1, 0) |
| 110 | |
| 111 | -- ground they rise from |
| 112 | local SOFT: { [string]: boolean } = { Grass = true, Dirt = true, Sand = true, Gravel = true, Snow = true, Farmland = true } |
| 113 | |
| 114 | local where: { [number]: vector } = {} |
| 115 | |
| 116 | local function flat(a: vector, b: vector): number |
| 117 | local dx, dz = a.x - b.x, a.z - b.z |
| 118 | return math.sqrt(dx * dx + dz * dz) |
| 119 | end |
| 120 | |
| 121 | local function dark(): boolean |
| 122 | return world.daylight() < 0.15 |
| 123 | end |
| 124 | |
| 125 | -- out under the sky: not under rock or a roof |
| 126 | local function outside(at: vector): boolean |
| 127 | return at.y >= world.top(at.x, at.z) - 0.5 |
| 128 | end |
| 129 | |
| 130 | local seen_at, seen = -1, {} |
| 131 | local function prey() |
| 132 | local now = world.time() |
| 133 | if now ~= seen_at then |
| 134 | seen_at, seen = now, {} |
| 135 | for _, p in world.players() do |
| 136 | if world.pockets(p) ~= nil and outside(p.position) then |
| 137 | table.insert(seen, p) |
| 138 | end |
| 139 | end |
| 140 | end |
| 141 | return seen |
| 142 | end |
| 143 | |
| 144 | local function gone(self) |
| 145 | where[self.id] = nil |
| 146 | self:remove() |
| 147 | end |
| 148 | |
| 149 | function Mound:spawned() |
| 150 | self.rise = RISE |
| 151 | sound.play("heave", { at = self, volume = 0.7, caption = "The ground heaves" }) |
| 152 | end |
| 153 | |
| 154 | function Mound:tick(dt: number) |
| 155 | where[self.id] = self.position |
| 156 | self.rise -= dt |
| 157 | if self.rise > 0 then |
| 158 | return |
| 159 | end |
| 160 | if self.sinking or not dark() then |
| 161 | gone(self) |
| 162 | return |
| 163 | end |
| 164 | self:become(Bogle) |
| 165 | self.hit_in = HIT_EVERY |
| 166 | self.think = 0 |
| 167 | sound.play("groan", { at = self, volume = 0.8 }) |
| 168 | end |
| 169 | |
| 170 | function Bogle:spawned() |
| 171 | self.hit_in = HIT_EVERY |
| 172 | self.think = 0 |
| 173 | end |
| 174 | |
| 175 | local function away_from(self, from: vector, far: number, speed: number) |
| 176 | local d = math.max(flat(self.position, from), 0.1) |
| 177 | local away = vector.create((self.position.x - from.x) / d, 0, (self.position.z - from.z) / d) |
| 178 | self:walk_to(self.position + away * far, speed) |
| 179 | end |
| 180 | |
| 181 | function Bogle:tick(dt: number) |
| 182 | where[self.id] = self.position |
| 183 | local d = danger() |
| 184 | if d.many == 0 or knobs.how_many < 1 then |
| 185 | gone(self) |
| 186 | return |
| 187 | end |
| 188 | -- the sun's up: back into the ground |
| 189 | if not dark() and world.daylight() > 0.3 then |
| 190 | self.sinking = true |
| 191 | self.rise = RISE |
| 192 | self:stop() |
| 193 | self:become(Mound) |
| 194 | sound.play("heave", { at = self, volume = 0.5, pitch = 0.8, caption = "A bogle sinks into the ground" }) |
| 195 | return |
| 196 | end |
| 197 | if (self.back_off or 0) > 0 then |
| 198 | self.back_off -= dt |
| 199 | return |
| 200 | end |
| 201 | self.think -= dt |
| 202 | if self.think <= 0 then |
| 203 | self.think = world.random(0.6, 1) |
| 204 | if not world.nearest_player(self.position, LONELY) then |
| 205 | gone(self) |
| 206 | return |
| 207 | end |
| 208 | self.light = world.find_light(self.position, knobs.shy) |
| 209 | local best, best_d = nil, CHASE |
| 210 | for _, p in prey() do |
| 211 | local dist = vector.magnitude(p.position - self.position) |
| 212 | if dist < best_d then |
| 213 | best, best_d = p, dist |
| 214 | end |
| 215 | end |
| 216 | self.prey = if best then best.id else nil |
| 217 | if world.random() < 0.1 then |
| 218 | sound.play("groan", { at = self, volume = 0.5 }) |
| 219 | end |
| 220 | -- stuck against something: heave itself up it |
| 221 | if self.prey and self.was and vector.magnitude(self.position - self.was) < 0.2 and self:on_ground() then |
| 222 | self:hop(1.2) |
| 223 | end |
| 224 | self.was = self.position |
| 225 | end |
| 226 | if self.light then |
| 227 | away_from(self, self.light, 3, 1.8) |
| 228 | return |
| 229 | end |
| 230 | local p = nil |
| 231 | for _, q in prey() do |
| 232 | if q.id == self.prey then |
| 233 | p = q |
| 234 | end |
| 235 | end |
| 236 | if not p then |
| 237 | self:wander(0.8) |
| 238 | return |
| 239 | end |
| 240 | local safe = world.find_light(p.position, knobs.shy) |
| 241 | if safe and flat(self.position, safe) < knobs.shy + 1 then |
| 242 | away_from(self, safe, 2, 1.2) |
| 243 | return |
| 244 | end |
| 245 | self.hit_in -= dt |
| 246 | if flat(self.position, p.position) < 1.6 and math.abs(self.position.y - p.position.y) < 1.8 then |
| 247 | self:stop() |
| 248 | self:face(p.position) |
| 249 | if self.hit_in <= 0 then |
| 250 | self.hit_in = HIT_EVERY |
| 251 | if knobs.hits > 0 and world.hurt(p, knobs.hits * d.hurt) then |
| 252 | sound.play("thud", { at = self, volume = 0.8, pitch = 0.6, caption = "A bogle swings" }) |
| 253 | end |
| 254 | end |
| 255 | else |
| 256 | self:walk_to(p.position, d.fast) |
| 257 | end |
| 258 | end |
| 259 | |
| 260 | function Bogle:hit(player, shot) |
| 261 | local bop = if shot then shot.power else bops(world.held(player)) * world.might(player) |
| 262 | self.bopped = (self.bopped or 0) + bop |
| 263 | if self.bopped < knobs.bops then |
| 264 | away_from(self, player.position, 1, 2) |
| 265 | self.back_off = 0.6 |
| 266 | self.hit_in = math.max(self.hit_in or 0, 1) |
| 267 | sound.play("dig", { at = self, volume = 0.8 }) |
| 268 | return |
| 269 | end |
| 270 | sound.play("crack", { at = self, pitch = 0.5, caption = "A bogle breaks up" }) |
| 271 | world.give(player, "Peat", math.floor(world.random(1, 3.999))) |
| 272 | if world.random() < 0.3 and pcall(world.kind, "Reeds") then |
| 273 | world.give(player, "Reeds", math.floor(world.random(1, 2.999))) |
| 274 | end |
| 275 | world.reward(player, 5) |
| 276 | gone(self) |
| 277 | end |
| 278 | |
| 279 | -- somewhere one can rise: the top of soft ground, open to the sky, dry and |
| 280 | -- out of the light |
| 281 | local function ground(x: number, z: number): vector? |
| 282 | local top = world.top(x, z) |
| 283 | local at = vector.create(math.floor(x) + 0.5, top, math.floor(z) + 0.5) |
| 284 | if not SOFT[world.block(at)] then |
| 285 | return nil |
| 286 | end |
| 287 | local over = at + UP |
| 288 | if world.block(over) ~= "Air" or world.block(over + UP) ~= "Air" then |
| 289 | return nil |
| 290 | end |
| 291 | if world.find_light(over, knobs.shy + 2) then |
| 292 | return nil |
| 293 | end |
| 294 | return vector.create(at.x, top + 1.02, at.z) |
| 295 | end |
| 296 | |
| 297 | -- Every few seconds on a dark night: one more near each player out under |
| 298 | -- the sky in survival, with fewer near them than the knob and the danger |
| 299 | -- say, and none near a light. |
| 300 | world.every(4, function() |
| 301 | if not dark() then |
| 302 | return |
| 303 | end |
| 304 | local d = danger() |
| 305 | local want = math.floor(knobs.how_many * d.many + 0.5) |
| 306 | if want < 1 then |
| 307 | return |
| 308 | end |
| 309 | local live = {} |
| 310 | for _, id in world.creatures() do |
| 311 | live[id] = true |
| 312 | end |
| 313 | local total = 0 |
| 314 | for id in where do |
| 315 | if live[id] then |
| 316 | total += 1 |
| 317 | else |
| 318 | where[id] = nil |
| 319 | end |
| 320 | end |
| 321 | for _, p in prey() do |
| 322 | -- the Long Dusk has creatures of its own |
| 323 | if world.place(p.position) == "dusk" then |
| 324 | continue |
| 325 | end |
| 326 | local near = 0 |
| 327 | for _, at in where do |
| 328 | if vector.magnitude(at - p.position) < CHASE + 4 then |
| 329 | near += 1 |
| 330 | end |
| 331 | end |
| 332 | if total < MOST and near < want and not world.find_light(p.position, knobs.shy) then |
| 333 | for _ = 1, 4 do |
| 334 | local a = world.random(0, 2 * math.pi) |
| 335 | local r = world.random(NEAR, FAR) |
| 336 | local at = ground(p.position.x + math.cos(a) * r, p.position.z + math.sin(a) * r) |
| 337 | if at then |
| 338 | where[world.spawn(Mound, at)] = at |
| 339 | total += 1 |
| 340 | break |
| 341 | end |
| 342 | end |
| 343 | end |
| 344 | end |
| 345 | end) |