Shelled snappers that swim under the sand of the deserts, the badlands and the far beaches at night. All you see is a hump and two eyes on stalks coming your way, then one bursts out, snaps its claws at you and digs back down. Swords glance off one that's under the sand, but a shovel digs it right out. Beat one and it leaves its shell, which a Moon bell takes. A torch, lantern or campfire keeps them off. None on a Peaceful world, and fewer on a Gentle one.
| 1 | --!strict |
| 2 | -- Sandsnappers: shelled snappers that swim under the sand of the deserts, |
| 3 | -- the badlands and the far beaches at night. One comes at anyone in |
| 4 | -- survival standing on the sand, as a hump with two eyes on stalks; close |
| 5 | -- by it bursts out (self:become), snaps its claws for a few seconds and |
| 6 | -- digs back down to come again. Under the sand, swords and fists glance |
| 7 | -- off it, but a shovel digs it out, dazed for a moment. Beat one and it |
| 8 | -- leaves its shell, which the Gloam's Moon bell takes. A light keeps them |
| 9 | -- off, and they go at sunrise. How many come and how hard they snap go by |
| 10 | -- the world's 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 come at you on the sand at night (0 for none)", order = 1 }, |
| 19 | snap = { 2.5, min = 0, max = 8, label = "How hard they snap", order = 2 }, |
| 20 | bops = { 6, min = 2, max = 16, label = "Bops to beat one by hand", order = 3 }, |
| 21 | shy = { 5, min = 2, max = 12, label = "How far they keep from a light", order = 4 }, |
| 22 | color = { "#d8b070", label = "Shell color", order = 5 }, |
| 23 | } |
| 24 | |
| 25 | local DANGER: { [string]: { many: number, hurt: number, fast: number } } = { |
| 26 | peaceful = { many = 0, hurt = 0, fast = 0 }, |
| 27 | gentle = { many = 0.5, hurt = 0.5, fast = 2.2 }, |
| 28 | normal = { many = 1, hurt = 1, fast = 2.8 }, |
| 29 | hard = { many = 1.5, hurt = 1.5, fast = 3.4 }, |
| 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 function shovel(held: string?): boolean |
| 55 | return held ~= nil and string.find(string.lower(held), "shovel") ~= nil |
| 56 | end |
| 57 | |
| 58 | local LANDS: { [string]: boolean } = { desert = true, badlands = true, beach = true } |
| 59 | local SANDY: { [string]: boolean } = { Sand = true, ["Red sand"] = true } |
| 60 | local LIGHTS: { [string]: boolean } = { Torch = true, Lantern = true, Candle = true, Brazier = true } |
| 61 | |
| 62 | local SAND = "#e0c890" |
| 63 | local DARK = "#6a4824" |
| 64 | local EYE = "#fff8e0" |
| 65 | local PUPIL = "#1a1008" |
| 66 | |
| 67 | local Hump = world.creature { |
| 68 | name = "Sandsnapper (buried)", |
| 69 | speed = 2.8, |
| 70 | count = 0, |
| 71 | home = "here", |
| 72 | size = { 0.8, 0.4, 0.8 }, |
| 73 | behaviors = { wander = false }, |
| 74 | body = { |
| 75 | { "hump", size = { 0.9, 0.22, 0.9 }, at = { 0, 0.11, 0 }, color = SAND }, |
| 76 | { "ridge", size = { 0.5, 0.12, 0.6 }, at = { 0, 0.26, 0.05 }, color = SAND }, |
| 77 | { "stalk_left", size = { 0.05, 0.3, 0.05 }, at = { -0.12, 0.38, -0.3 }, color = knobs.color, head = true }, |
| 78 | { "stalk_right", size = { 0.05, 0.3, 0.05 }, at = { 0.12, 0.38, -0.3 }, color = knobs.color, head = true }, |
| 79 | { "eye_left", size = { 0.15, 0.15, 0.15 }, at = { -0.12, 0.58, -0.3 }, color = EYE, glow = true, head = true }, |
| 80 | { "pupil_left", size = { 0.08, 0.09, 0.03 }, at = { -0.12, 0.58, -0.385 }, color = PUPIL, head = true }, |
| 81 | { "eye_right", size = { 0.15, 0.15, 0.15 }, at = { 0.12, 0.58, -0.3 }, color = EYE, glow = true, head = true }, |
| 82 | { "pupil_right", size = { 0.08, 0.09, 0.03 }, at = { 0.12, 0.58, -0.385 }, color = PUPIL, head = true }, |
| 83 | }, |
| 84 | } |
| 85 | |
| 86 | local Snapper = world.creature { |
| 87 | name = "Sandsnapper", |
| 88 | speed = 2.4, |
| 89 | count = 0, |
| 90 | home = "here", |
| 91 | size = { 1.0, 0.7, 1.0 }, |
| 92 | behaviors = { wander = false }, |
| 93 | body = { |
| 94 | { "shell", size = { 0.8, 0.34, 0.9 }, at = { 0, 0.42, 0.05 }, color = knobs.color }, |
| 95 | { "shell_top", size = { 0.56, 0.14, 0.64 }, at = { 0, 0.64, 0.08 }, color = "#e8c888" }, |
| 96 | { "belly", size = { 0.66, 0.12, 0.76 }, at = { 0, 0.22, 0.05 }, color = DARK }, |
| 97 | { "face", size = { 0.5, 0.22, 0.12 }, at = { 0, 0.4, -0.44 }, color = DARK, head = true }, |
| 98 | { "stalk_left", size = { 0.05, 0.24, 0.05 }, at = { -0.13, 0.66, -0.4 }, color = knobs.color, head = true }, |
| 99 | { "stalk_right", size = { 0.05, 0.24, 0.05 }, at = { 0.13, 0.66, -0.4 }, color = knobs.color, head = true }, |
| 100 | { "eye_left", size = { 0.16, 0.16, 0.16 }, at = { -0.13, 0.84, -0.4 }, color = EYE, glow = true, head = true }, |
| 101 | { "pupil_left", size = { 0.08, 0.09, 0.03 }, at = { -0.13, 0.84, -0.49 }, color = PUPIL, head = true }, |
| 102 | { "eye_right", size = { 0.16, 0.16, 0.16 }, at = { 0.13, 0.84, -0.4 }, color = EYE, glow = true, head = true }, |
| 103 | { "pupil_right", size = { 0.08, 0.09, 0.03 }, at = { 0.13, 0.84, -0.49 }, color = PUPIL, head = true }, |
| 104 | { "claw_left", size = { 0.26, 0.2, 0.34 }, at = { -0.46, 0.34, -0.56 }, color = knobs.color, leg = true }, |
| 105 | { "claw_right", size = { 0.26, 0.2, 0.34 }, at = { 0.46, 0.34, -0.56 }, color = knobs.color, leg = true }, |
| 106 | { "pincer_left", size = { 0.08, 0.1, 0.18 }, at = { -0.38, 0.44, -0.78 }, color = DARK, leg = true }, |
| 107 | { "pincer_right", size = { 0.08, 0.1, 0.18 }, at = { 0.38, 0.44, -0.78 }, color = DARK, leg = true }, |
| 108 | { "leg_1", size = { 0.3, 0.06, 0.06 }, at = { -0.5, 0.18, -0.12 }, color = DARK, leg = true }, |
| 109 | { "leg_2", size = { 0.3, 0.06, 0.06 }, at = { 0.5, 0.18, -0.12 }, color = DARK, leg = true }, |
| 110 | { "leg_3", size = { 0.32, 0.06, 0.06 }, at = { -0.52, 0.18, 0.14 }, color = DARK, leg = true }, |
| 111 | { "leg_4", size = { 0.32, 0.06, 0.06 }, at = { 0.52, 0.18, 0.14 }, color = DARK, leg = true }, |
| 112 | { "leg_5", size = { 0.28, 0.06, 0.06 }, at = { -0.48, 0.18, 0.38 }, color = DARK, leg = true }, |
| 113 | { "leg_6", size = { 0.28, 0.06, 0.06 }, at = { 0.48, 0.18, 0.38 }, color = DARK, leg = true }, |
| 114 | { "tail_1", size = { 0.2, 0.16, 0.3 }, at = { 0, 0.4, 0.6 }, color = knobs.color }, |
| 115 | { "tail_2", size = { 0.14, 0.26, 0.14 }, at = { 0, 0.6, 0.76 }, color = knobs.color }, |
| 116 | { "sting", size = { 0.08, 0.12, 0.16 }, at = { 0, 0.78, 0.68 }, color = DARK }, |
| 117 | }, |
| 118 | } |
| 119 | |
| 120 | local MOST = 24 -- at once in the whole world |
| 121 | local NEAR, FAR = 9, 15 -- how far from a player they turn up |
| 122 | local CHASE = 20 -- they go after players this close |
| 123 | local LONELY = 40 -- and go with nobody this close |
| 124 | local BURST = 2.2 -- how close they come before bursting out |
| 125 | local OUT = 6 -- seconds out of the sand before digging down again |
| 126 | local SNAP_EVERY = 1.3 |
| 127 | local DAZED = 2 -- seconds a shovel leaves one dazed |
| 128 | local UP = vector.create(0, 1, 0) |
| 129 | |
| 130 | local where: { [number]: vector } = {} |
| 131 | |
| 132 | local function flat(a: vector, b: vector): number |
| 133 | local dx, dz = a.x - b.x, a.z - b.z |
| 134 | return math.sqrt(dx * dx + dz * dz) |
| 135 | end |
| 136 | |
| 137 | local function dark(): boolean |
| 138 | return world.daylight() < 0.15 |
| 139 | end |
| 140 | |
| 141 | local function on_sand(at: vector): boolean |
| 142 | return SANDY[world.block(vector.create(at.x, at.y - 0.5, at.z))] == true |
| 143 | end |
| 144 | |
| 145 | -- players in survival on the sand, out under the sky |
| 146 | local seen_at, seen = -1, {} |
| 147 | local function prey() |
| 148 | local now = world.time() |
| 149 | if now ~= seen_at then |
| 150 | seen_at, seen = now, {} |
| 151 | for _, p in world.players() do |
| 152 | if world.pockets(p) ~= nil and on_sand(p.position) and p.position.y >= world.top(p.position.x, p.position.z) - 0.5 then |
| 153 | table.insert(seen, p) |
| 154 | end |
| 155 | end |
| 156 | end |
| 157 | return seen |
| 158 | end |
| 159 | |
| 160 | local function light_near(at: vector, within: number): vector? |
| 161 | local lit = world.find_light(at, within) |
| 162 | if lit then |
| 163 | return lit |
| 164 | end |
| 165 | for _, p in world.players() do |
| 166 | local held = world.held(p) |
| 167 | if held and LIGHTS[held] and vector.magnitude(p.position - at) < within then |
| 168 | return p.position |
| 169 | end |
| 170 | end |
| 171 | return nil |
| 172 | end |
| 173 | |
| 174 | local function gone(self) |
| 175 | where[self.id] = nil |
| 176 | self:remove() |
| 177 | end |
| 178 | |
| 179 | local function away_from(self, from: vector, far: number, speed: number) |
| 180 | local d = math.max(flat(self.position, from), 0.1) |
| 181 | local away = vector.create((self.position.x - from.x) / d, 0, (self.position.z - from.z) / d) |
| 182 | self:walk_to(self.position + away * far, speed) |
| 183 | end |
| 184 | |
| 185 | local function find_prey(self) |
| 186 | local best, best_d = nil, CHASE |
| 187 | for _, p in prey() do |
| 188 | local dist = vector.magnitude(p.position - self.position) |
| 189 | if dist < best_d then |
| 190 | best, best_d = p, dist |
| 191 | end |
| 192 | end |
| 193 | return best |
| 194 | end |
| 195 | |
| 196 | local function by_id(id: number?) |
| 197 | if not id then |
| 198 | return nil |
| 199 | end |
| 200 | for _, q in world.players() do |
| 201 | if q.id == id then |
| 202 | return q |
| 203 | end |
| 204 | end |
| 205 | return nil |
| 206 | end |
| 207 | |
| 208 | -- out of the sand, `wait` seconds before its first snap |
| 209 | local function burst(self, wait: number) |
| 210 | self:stop() |
| 211 | self:become(Snapper) |
| 212 | self.out = OUT |
| 213 | self.snap_in = wait |
| 214 | sound.play("burst", { at = self, volume = 0.9 }) |
| 215 | end |
| 216 | |
| 217 | -- ---- under the sand ---- |
| 218 | |
| 219 | function Hump:spawned() |
| 220 | self.look_in = 0 |
| 221 | self.rest = 0 |
| 222 | sound.play("hiss", { at = self, volume = 0.5 }) |
| 223 | end |
| 224 | |
| 225 | function Hump:tick(dt: number) |
| 226 | where[self.id] = self.position |
| 227 | local d = danger() |
| 228 | if d.many == 0 or knobs.how_many < 1 or world.daylight() > 0.3 then |
| 229 | gone(self) |
| 230 | return |
| 231 | end |
| 232 | self.look_in -= dt |
| 233 | if self.look_in <= 0 then |
| 234 | self.look_in = world.random(0.5, 0.8) |
| 235 | if not world.nearest_player(self.position, LONELY) then |
| 236 | gone(self) |
| 237 | return |
| 238 | end |
| 239 | local p = find_prey(self) |
| 240 | self.prey = if p then p.id else nil |
| 241 | self.light = light_near(self.position, knobs.shy) |
| 242 | if world.random() < 0.12 then |
| 243 | sound.play("hiss", { at = self, volume = 0.4 }) |
| 244 | end |
| 245 | end |
| 246 | if self.light then |
| 247 | away_from(self, self.light, 4, d.fast) |
| 248 | return |
| 249 | end |
| 250 | -- just dug down: off a little way first |
| 251 | if (self.rest or 0) > 0 then |
| 252 | self.rest -= dt |
| 253 | return |
| 254 | end |
| 255 | local p = by_id(self.prey) |
| 256 | if not p or not on_sand(self.position) then |
| 257 | self:wander(1) |
| 258 | return |
| 259 | end |
| 260 | if flat(self.position, p.position) < BURST and math.abs(self.position.y - p.position.y) < 1.5 then |
| 261 | self:face(p.position) |
| 262 | burst(self, 0.5) |
| 263 | return |
| 264 | end |
| 265 | self:walk_to(p.position, d.fast) |
| 266 | end |
| 267 | |
| 268 | function Hump:hit(player, shot) |
| 269 | local held = if shot then nil else world.held(player) |
| 270 | if shovel(held) then |
| 271 | -- dug out, dazed |
| 272 | burst(self, DAZED) |
| 273 | self.bopped = (self.bopped or 0) + 2 * world.might(player) |
| 274 | self:say("!") |
| 275 | return |
| 276 | end |
| 277 | if shot then |
| 278 | self.bopped = (self.bopped or 0) + shot.power * 0.5 |
| 279 | end |
| 280 | sound.play("hiss", { at = self, volume = 0.6, pitch = 1.3, caption = "A blow glances off the sand" }) |
| 281 | if world.pockets(player) ~= nil and not shot then |
| 282 | ui.show(player, "glance", "It's under the sand. A shovel would dig it out.", { at = "bottom" }) |
| 283 | world.after(3, function() |
| 284 | ui.hide(player, "glance") |
| 285 | end) |
| 286 | end |
| 287 | end |
| 288 | |
| 289 | -- ---- out of the sand ---- |
| 290 | |
| 291 | function Snapper:spawned() |
| 292 | self.out = OUT |
| 293 | self.snap_in = 0.5 |
| 294 | self.look_in = 0 |
| 295 | end |
| 296 | |
| 297 | function Snapper:tick(dt: number) |
| 298 | where[self.id] = self.position |
| 299 | local d = danger() |
| 300 | if d.many == 0 or knobs.how_many < 1 then |
| 301 | gone(self) |
| 302 | return |
| 303 | end |
| 304 | self.out -= dt |
| 305 | if self.out <= 0 or world.daylight() > 0.3 then |
| 306 | -- back down, and off a little way before coming again |
| 307 | self:become(Hump) |
| 308 | self.rest = 2.5 |
| 309 | self.look_in = 0 |
| 310 | sound.play("burst", { at = self, volume = 0.5, pitch = 1.3, caption = "A sandsnapper digs down" }) |
| 311 | local p = by_id(self.prey) |
| 312 | if p then |
| 313 | away_from(self, p.position, 6, d.fast) |
| 314 | end |
| 315 | return |
| 316 | end |
| 317 | self.look_in -= dt |
| 318 | if self.look_in <= 0 then |
| 319 | self.look_in = 0.5 |
| 320 | local p = find_prey(self) |
| 321 | self.prey = if p then p.id else nil |
| 322 | end |
| 323 | local p = by_id(self.prey) |
| 324 | if not p then |
| 325 | self:stop() |
| 326 | return |
| 327 | end |
| 328 | self.snap_in -= dt |
| 329 | if flat(self.position, p.position) < 1.7 and math.abs(self.position.y - p.position.y) < 1.5 then |
| 330 | self:stop() |
| 331 | self:face(p.position) |
| 332 | if self.snap_in <= 0 then |
| 333 | self.snap_in = SNAP_EVERY |
| 334 | if knobs.snap > 0 and world.hurt(p, knobs.snap * d.hurt) then |
| 335 | sound.play("snap", { at = self, volume = 0.9 }) |
| 336 | end |
| 337 | end |
| 338 | elseif self.snap_in <= 0 then |
| 339 | self:walk_to(p.position, d.fast * 0.8) |
| 340 | end |
| 341 | end |
| 342 | |
| 343 | function Snapper:hit(player, shot) |
| 344 | local bop = if shot then shot.power else bops(world.held(player)) * world.might(player) |
| 345 | self.bopped = (self.bopped or 0) + bop |
| 346 | if self.bopped < knobs.bops then |
| 347 | sound.play("snap", { at = self, volume = 0.6, pitch = 0.8 }) |
| 348 | self:hop(0.4) |
| 349 | self.snap_in = math.max(self.snap_in or 0, 0.8) |
| 350 | return |
| 351 | end |
| 352 | sound.play("crack", { at = self, pitch = 1.3, caption = "A sandsnapper is beaten" }) |
| 353 | world.give(player, "Snapper shell", 1) |
| 354 | world.reward(player, 6) |
| 355 | gone(self) |
| 356 | if world.item("Moon bell") ~= nil and world.pockets(player) ~= nil then |
| 357 | ui.show(player, "find", "A snapper shell. A Moon bell takes one.", { at = "bottom" }) |
| 358 | world.after(4, function() |
| 359 | ui.hide(player, "find") |
| 360 | end) |
| 361 | end |
| 362 | end |
| 363 | |
| 364 | -- on top of the sand, out of the light |
| 365 | local function sand_at(x: number, z: number): vector? |
| 366 | local top = world.top(x, z) |
| 367 | local at = vector.create(math.floor(x) + 0.5, top, math.floor(z) + 0.5) |
| 368 | if not SANDY[world.block(at)] or world.block(at + UP) ~= "Air" then |
| 369 | return nil |
| 370 | end |
| 371 | local over = vector.create(at.x, top + 1.02, at.z) |
| 372 | if light_near(over, knobs.shy + 2) then |
| 373 | return nil |
| 374 | end |
| 375 | return over |
| 376 | end |
| 377 | |
| 378 | -- Every few seconds at night: one more near each player on the sand of the |
| 379 | -- deserts, the badlands and the beaches past the island. |
| 380 | world.every(4, function() |
| 381 | if not dark() then |
| 382 | return |
| 383 | end |
| 384 | local d = danger() |
| 385 | local want = math.floor(knobs.how_many * d.many + 0.5) |
| 386 | if want < 1 then |
| 387 | return |
| 388 | end |
| 389 | local live = {} |
| 390 | for _, id in world.creatures(Hump) do |
| 391 | live[id] = true |
| 392 | end |
| 393 | local total = 0 |
| 394 | for id in where do |
| 395 | if live[id] then |
| 396 | total += 1 |
| 397 | else |
| 398 | where[id] = nil |
| 399 | end |
| 400 | end |
| 401 | for _, p in prey() do |
| 402 | -- the Long Dusk has creatures of its own |
| 403 | if world.place(p.position) == "dusk" then |
| 404 | continue |
| 405 | end |
| 406 | if total >= MOST then |
| 407 | return |
| 408 | end |
| 409 | if LANDS[world.biome(p.position)] and not light_near(p.position, knobs.shy) then |
| 410 | local near = 0 |
| 411 | for _, at in where do |
| 412 | if vector.magnitude(at - p.position) < CHASE + 4 then |
| 413 | near += 1 |
| 414 | end |
| 415 | end |
| 416 | if near < want then |
| 417 | for _ = 1, 4 do |
| 418 | local a = world.random(0, 2 * math.pi) |
| 419 | local r = world.random(NEAR, FAR) |
| 420 | local at = sand_at(p.position.x + math.cos(a) * r, p.position.z + math.sin(a) * r) |
| 421 | if at then |
| 422 | where[world.spawn(Hump, at)] = at |
| 423 | total += 1 |
| 424 | break |
| 425 | end |
| 426 | end |
| 427 | end |
| 428 | end |
| 429 | end |
| 430 | end) |