Sneaky little night thieves with big round eyes and a sack on their backs. Out on the grass at night in survival, one creeps up, snatches a few of whatever you're carrying (never your tools, gear or treasures) and runs off giggling. Catch it and bop it twice and you get it all back, sometimes with beads it stole from someone else, and now and then a silver thimble the Curator would love. If it gets away, it's gone. A light keeps them off. None on a Peaceful world.
| 1 | --!strict |
| 2 | -- Pilferlings: sneaky little night thieves with big round eyes and a sack |
| 3 | -- on their backs. Out on the grass at night, away from any light, one |
| 4 | -- creeps up on someone in survival, snatches a few of something they carry |
| 5 | -- (a block, a material or food: never tools, weapons, armor, relics or a |
| 6 | -- Moon bell) and runs off giggling, stopping now and then to look back. |
| 7 | -- Catch it and bop it twice and whoever bopped it gets back everything in |
| 8 | -- its sack, sometimes with beads it stole from someone else (when the |
| 9 | -- Villagers are about) and now and then a silver thimble, a relic for the |
| 10 | -- Curator. If it gets far enough away, or the sun comes up, it's gone with |
| 11 | -- what it took. How much it takes goes by the world's danger |
| 12 | -- (world.danger): none come on Peaceful. |
| 13 | |
| 14 | local world = require("@potluck/world@0.33") |
| 15 | local tweak = require("@potluck/tweak") |
| 16 | local sound = require("@potluck/sound@0.2") |
| 17 | local ui = require("@potluck/ui@0.2") |
| 18 | |
| 19 | local knobs = tweak { |
| 20 | how_many = { 1, min = 0, max = 3, label = "How many come after you at night (0 for none)", order = 1 }, |
| 21 | bops = { 2, min = 1, max = 6, label = "Bops to catch one", order = 2 }, |
| 22 | shy = { 5, min = 2, max = 12, label = "How far they keep from a light", order = 3 }, |
| 23 | fur = { "#7a9a6a", label = "Fur color", order = 4 }, |
| 24 | } |
| 25 | |
| 26 | -- how many come, and how much one snatches at once |
| 27 | local DANGER: { [string]: { many: number, takes: number } } = { |
| 28 | peaceful = { many = 0, takes = 0 }, |
| 29 | gentle = { many = 1, takes = 1 }, |
| 30 | normal = { many = 1, takes = 3 }, |
| 31 | hard = { many = 2, takes = 6 }, |
| 32 | } |
| 33 | local function danger() |
| 34 | return DANGER[world.danger()] or DANGER.gentle |
| 35 | end |
| 36 | |
| 37 | local GROUND: { [string]: boolean } = { Grass = true, Dirt = true, Clover = true, Heather = true } |
| 38 | local LIGHTS: { [string]: boolean } = { Torch = true, Lantern = true, Candle = true, Brazier = true } |
| 39 | local THIMBLE = "Silver thimble" |
| 40 | |
| 41 | local DARK = "#3a4a32" |
| 42 | local EYE = "#fffdf0" |
| 43 | local PUPIL = "#120e08" |
| 44 | local SACK = "#8a6a40" |
| 45 | |
| 46 | local Pilferling = world.creature { |
| 47 | name = "Pilferling", |
| 48 | speed = 2.6, |
| 49 | count = 0, |
| 50 | home = "here", |
| 51 | size = { 0.6, 0.9, 0.6 }, |
| 52 | behaviors = { wander = false }, |
| 53 | body = { |
| 54 | { "body", size = { 0.36, 0.34, 0.3 }, at = { 0, 0.34, 0.02 }, color = knobs.fur }, |
| 55 | { "head", size = { 0.52, 0.44, 0.44 }, at = { 0, 0.72, -0.04 }, color = knobs.fur, head = true }, |
| 56 | { "ear_left", size = { 0.18, 0.1, 0.06 }, at = { -0.33, 0.84, 0 }, color = knobs.fur, head = true }, |
| 57 | { "ear_right", size = { 0.18, 0.1, 0.06 }, at = { 0.33, 0.84, 0 }, color = knobs.fur, head = true }, |
| 58 | { "eye_left", size = { 0.18, 0.2, 0.03 }, at = { -0.12, 0.76, -0.27 }, color = EYE, glow = true, head = true }, |
| 59 | { "pupil_left", size = { 0.1, 0.12, 0.03 }, at = { -0.11, 0.75, -0.29 }, color = PUPIL, head = true }, |
| 60 | { "eye_right", size = { 0.18, 0.2, 0.03 }, at = { 0.12, 0.76, -0.27 }, color = EYE, glow = true, head = true }, |
| 61 | { "pupil_right", size = { 0.1, 0.12, 0.03 }, at = { 0.11, 0.75, -0.29 }, color = PUPIL, head = true }, |
| 62 | { "grin", size = { 0.2, 0.04, 0.03 }, at = { 0, 0.6, -0.27 }, color = DARK, head = true }, |
| 63 | { "nose", size = { 0.06, 0.05, 0.04 }, at = { 0, 0.67, -0.28 }, color = "#c87a6a", head = true }, |
| 64 | { "sack", size = { 0.34, 0.36, 0.22 }, at = { 0, 0.46, 0.26 }, color = SACK }, |
| 65 | { "sack_tie", size = { 0.12, 0.08, 0.1 }, at = { 0, 0.66, 0.3 }, color = "#5a4428" }, |
| 66 | { "arm_left", size = { 0.08, 0.3, 0.08 }, at = { -0.23, 0.32, -0.04 }, color = DARK, leg = true }, |
| 67 | { "arm_right", size = { 0.08, 0.3, 0.08 }, at = { 0.23, 0.32, -0.04 }, color = DARK, leg = true }, |
| 68 | { "leg_left", size = { 0.1, 0.18, 0.12 }, at = { -0.1, 0.09, 0 }, color = DARK, leg = true }, |
| 69 | { "leg_right", size = { 0.1, 0.18, 0.12 }, at = { 0.1, 0.09, 0 }, color = DARK, leg = true }, |
| 70 | { "tail", size = { 0.06, 0.06, 0.3 }, at = { 0, 0.22, 0.28 }, color = knobs.fur }, |
| 71 | }, |
| 72 | } |
| 73 | |
| 74 | local MOST = 10 -- at once in the whole world |
| 75 | local NEAR, FAR = 12, 18 -- how far from a player they turn up |
| 76 | local CHASE = 24 -- they creep after players this close |
| 77 | local AWAY = 40 -- one this far from everyone has got away |
| 78 | local RUN, LOOK = 3, 1.2 -- seconds it runs, then looks back |
| 79 | local UP = vector.create(0, 1, 0) |
| 80 | |
| 81 | local where: { [number]: vector } = {} |
| 82 | |
| 83 | local function flat(a: vector, b: vector): number |
| 84 | local dx, dz = a.x - b.x, a.z - b.z |
| 85 | return math.sqrt(dx * dx + dz * dz) |
| 86 | end |
| 87 | |
| 88 | local function dark(): boolean |
| 89 | return world.daylight() < 0.15 |
| 90 | end |
| 91 | |
| 92 | local function outside(at: vector): boolean |
| 93 | return at.y >= world.top(at.x, at.z) - 0.5 |
| 94 | end |
| 95 | |
| 96 | local function light_near(at: vector, within: number): vector? |
| 97 | local lit = world.find_light(at, within) |
| 98 | if lit then |
| 99 | return lit |
| 100 | end |
| 101 | for _, p in world.players() do |
| 102 | local held = world.held(p) |
| 103 | if held and LIGHTS[held] and vector.magnitude(p.position - at) < within then |
| 104 | return p.position |
| 105 | end |
| 106 | end |
| 107 | return nil |
| 108 | end |
| 109 | |
| 110 | local function by_id(id: number?) |
| 111 | if not id then |
| 112 | return nil |
| 113 | end |
| 114 | for _, q in world.players() do |
| 115 | if q.id == id then |
| 116 | return q |
| 117 | end |
| 118 | end |
| 119 | return nil |
| 120 | end |
| 121 | |
| 122 | local function tell(player, text: string) |
| 123 | ui.show(player, "pilfer", text, { at = "bottom" }) |
| 124 | world.after(4, function() |
| 125 | ui.hide(player, "pilfer") |
| 126 | end) |
| 127 | end |
| 128 | |
| 129 | -- what it may take: blocks, and materials and food that stack |
| 130 | local function stealable(name: string): boolean |
| 131 | if name == "Moon bell" then |
| 132 | return false |
| 133 | end |
| 134 | local it = world.item(name) |
| 135 | if it == nil then |
| 136 | return true |
| 137 | end |
| 138 | if it.relic ~= nil then |
| 139 | return false |
| 140 | end |
| 141 | if it.kind ~= nil and it.kind ~= "material" and it.kind ~= "food" then |
| 142 | return false |
| 143 | end |
| 144 | return (it.stack or 1) > 1 |
| 145 | end |
| 146 | |
| 147 | local function gone(self) |
| 148 | where[self.id] = nil |
| 149 | self:remove() |
| 150 | end |
| 151 | |
| 152 | function Pilferling:spawned() |
| 153 | self.state = "creep" |
| 154 | self.look_in = 0 |
| 155 | self.timer = 0 |
| 156 | end |
| 157 | |
| 158 | local function snatch(self, p): boolean |
| 159 | local carried = world.pockets(p) |
| 160 | if not carried then |
| 161 | return false |
| 162 | end |
| 163 | local can = {} |
| 164 | for name, n in carried do |
| 165 | if n > 0 and stealable(name) then |
| 166 | table.insert(can, name) |
| 167 | end |
| 168 | end |
| 169 | if #can == 0 then |
| 170 | return false |
| 171 | end |
| 172 | table.sort(can) |
| 173 | local name = can[math.floor(world.random(1, #can + 0.999))] |
| 174 | local n = math.min(carried[name], danger().takes) |
| 175 | if n < 1 or not world.take(p, name, n) then |
| 176 | return false |
| 177 | end |
| 178 | self.loot = { thing = name, n = n } |
| 179 | self.victim = p.name |
| 180 | sound.play("snatch", { at = self, volume = 0.9 }) |
| 181 | tell(p, string.format("A pilferling took %d %s! Catch it.", n, string.lower(name))) |
| 182 | return true |
| 183 | end |
| 184 | |
| 185 | function Pilferling:tick(dt: number) |
| 186 | where[self.id] = self.position |
| 187 | local d = danger() |
| 188 | if d.many == 0 or knobs.how_many < 1 then |
| 189 | gone(self) |
| 190 | return |
| 191 | end |
| 192 | -- got away, or the sun's up: gone, with whatever it took |
| 193 | local nearest = world.nearest_player(self.position, AWAY) |
| 194 | if not nearest or world.daylight() > 0.3 then |
| 195 | local v = self.victim |
| 196 | if self.loot and v then |
| 197 | for _, p in world.players() do |
| 198 | if p.name == v then |
| 199 | tell(p, "A pilferling got away with your " .. string.lower(self.loot.thing) .. ".") |
| 200 | end |
| 201 | end |
| 202 | end |
| 203 | gone(self) |
| 204 | return |
| 205 | end |
| 206 | self.look_in -= dt |
| 207 | if self.look_in <= 0 then |
| 208 | self.look_in = world.random(0.4, 0.7) |
| 209 | self.light = light_near(self.position, knobs.shy) |
| 210 | if world.random() < 0.08 then |
| 211 | sound.play("giggle", { at = self, volume = 0.4 }) |
| 212 | end |
| 213 | end |
| 214 | if self.state == "creep" then |
| 215 | local best, best_d = nil, CHASE |
| 216 | for _, p in world.players() do |
| 217 | if world.pockets(p) ~= nil and outside(p.position) then |
| 218 | local dist = vector.magnitude(p.position - self.position) |
| 219 | if dist < best_d then |
| 220 | best, best_d = p, dist |
| 221 | end |
| 222 | end |
| 223 | end |
| 224 | if self.light or not best then |
| 225 | if self.light then |
| 226 | local away = self.position - self.light |
| 227 | self:walk_to(self.position + vector.normalize(vector.create(away.x, 0, away.z) + vector.create(0.01, 0, 0)) * 4, 3) |
| 228 | else |
| 229 | self:wander(1) |
| 230 | end |
| 231 | return |
| 232 | end |
| 233 | if flat(self.position, best.position) < 1.4 and math.abs(self.position.y - best.position.y) < 1.5 then |
| 234 | self:face(best.position) |
| 235 | if snatch(self, best) then |
| 236 | self.state = "run" |
| 237 | self.from = best.id |
| 238 | self.timer = RUN |
| 239 | self:say("Mine!") |
| 240 | else |
| 241 | -- nothing to take: off to try someone else later |
| 242 | self:say("Hmph.") |
| 243 | self.state = "run" |
| 244 | self.from = best.id |
| 245 | self.timer = RUN |
| 246 | end |
| 247 | return |
| 248 | end |
| 249 | self:walk_to(best.position, 2.6) |
| 250 | return |
| 251 | end |
| 252 | -- running off, looking back now and then |
| 253 | self.timer -= dt |
| 254 | local p = by_id(self.from) |
| 255 | if self.state == "run" then |
| 256 | if p then |
| 257 | local away = vector.create(self.position.x - p.position.x, 0, self.position.z - p.position.z) |
| 258 | local len = math.max(vector.magnitude(away), 0.1) |
| 259 | self:walk_to(self.position + away / len * 6, 4.4) |
| 260 | else |
| 261 | self:wander(3) |
| 262 | end |
| 263 | if self.timer <= 0 then |
| 264 | self.state = "look" |
| 265 | self.timer = LOOK |
| 266 | self:stop() |
| 267 | if p then |
| 268 | self:face(p.position) |
| 269 | end |
| 270 | sound.play("giggle", { at = self, volume = 0.7 }) |
| 271 | end |
| 272 | elseif self.state == "look" then |
| 273 | self:stop() |
| 274 | if self.timer <= 0 then |
| 275 | self.state = "run" |
| 276 | self.timer = RUN |
| 277 | end |
| 278 | end |
| 279 | end |
| 280 | |
| 281 | function Pilferling:hit(player, shot) |
| 282 | -- quick, but small: any bop counts the same |
| 283 | self.bopped = (self.bopped or 0) + 1 |
| 284 | if self.bopped < knobs.bops then |
| 285 | sound.play("giggle", { at = self, volume = 0.8, pitch = 1.4 }) |
| 286 | self:hop(0.5) |
| 287 | if self.state == "creep" then |
| 288 | self.state = "run" |
| 289 | self.from = player.id |
| 290 | self.timer = RUN |
| 291 | end |
| 292 | return |
| 293 | end |
| 294 | sound.play("pop", { at = self, pitch = 1.4, caption = "A pilferling is caught" }) |
| 295 | local got = {} |
| 296 | local loot = self.loot |
| 297 | if loot then |
| 298 | local n = world.give(player, loot.thing, loot.n) |
| 299 | if n > 0 then |
| 300 | table.insert(got, n .. " " .. string.lower(loot.thing)) |
| 301 | end |
| 302 | end |
| 303 | if world.random() < 0.4 and pcall(world.kind, "Beads") then |
| 304 | local n = world.give(player, "Beads", math.floor(world.random(1, 3.999))) |
| 305 | if n > 0 then |
| 306 | table.insert(got, n .. " beads") |
| 307 | end |
| 308 | end |
| 309 | if world.random() < 0.15 and world.give(player, THIMBLE, 1) > 0 then |
| 310 | table.insert(got, "a silver thimble") |
| 311 | end |
| 312 | world.reward(player, 4) |
| 313 | gone(self) |
| 314 | if world.pockets(player) ~= nil then |
| 315 | tell(player, if #got > 0 then "Caught! Its sack had " .. table.concat(got, ", ") .. "." else "Caught! Its sack was empty.") |
| 316 | end |
| 317 | end |
| 318 | |
| 319 | -- somewhere one can turn up: grass in the open, out of the light |
| 320 | local function ground(x: number, z: number): vector? |
| 321 | local top = world.top(x, z) |
| 322 | local at = vector.create(math.floor(x) + 0.5, top, math.floor(z) + 0.5) |
| 323 | if not GROUND[world.block(at)] then |
| 324 | return nil |
| 325 | end |
| 326 | if world.block(at + UP) ~= "Air" and world.block(at + UP) ~= "Tall grass" then |
| 327 | return nil |
| 328 | end |
| 329 | local over = vector.create(at.x, top + 1.02, at.z) |
| 330 | if light_near(over, knobs.shy + 2) then |
| 331 | return nil |
| 332 | end |
| 333 | return over |
| 334 | end |
| 335 | |
| 336 | -- Every few seconds at night, now and then: one comes after a player out on |
| 337 | -- the grass in survival with something to take, away from any light. |
| 338 | world.every(6, function() |
| 339 | if not dark() then |
| 340 | return |
| 341 | end |
| 342 | local d = danger() |
| 343 | local want = math.floor(knobs.how_many * d.many + 0.5) |
| 344 | if want < 1 then |
| 345 | return |
| 346 | end |
| 347 | local live = {} |
| 348 | for _, id in world.creatures(Pilferling) do |
| 349 | live[id] = true |
| 350 | end |
| 351 | local total = 0 |
| 352 | for id in where do |
| 353 | if live[id] then |
| 354 | total += 1 |
| 355 | else |
| 356 | where[id] = nil |
| 357 | end |
| 358 | end |
| 359 | for _, p in world.players() do |
| 360 | -- the Long Dusk has creatures of its own |
| 361 | if world.place(p.position) == "dusk" then |
| 362 | continue |
| 363 | end |
| 364 | if total >= MOST then |
| 365 | return |
| 366 | end |
| 367 | local carried = world.pockets(p) |
| 368 | if carried and outside(p.position) and world.random() < 0.5 and not light_near(p.position, knobs.shy) then |
| 369 | local near = 0 |
| 370 | for _, at in where do |
| 371 | if vector.magnitude(at - p.position) < CHASE + 4 then |
| 372 | near += 1 |
| 373 | end |
| 374 | end |
| 375 | if near < want then |
| 376 | for _ = 1, 4 do |
| 377 | local a = world.random(0, 2 * math.pi) |
| 378 | local r = world.random(NEAR, FAR) |
| 379 | local at = ground(p.position.x + math.cos(a) * r, p.position.z + math.sin(a) * r) |
| 380 | if at then |
| 381 | where[world.spawn(Pilferling, at)] = at |
| 382 | total += 1 |
| 383 | break |
| 384 | end |
| 385 | end |
| 386 | end |
| 387 | end |
| 388 | end |
| 389 | end) |