On a Normal or Hard world, a player hurt too badly in survival drops everything they carry in a pack where they fell: go back and use the pack to take it all back (anyone can, so be quick). On Peaceful and Gentle worlds you keep what you carry. What you wear stays on.
| 1 | --!strict |
| 2 | -- Lost packs: on a Normal or Hard world (world.danger, world 0.21), a |
| 3 | -- player hurt too badly in survival drops everything they carry where they |
| 4 | -- fell (world.on("player_fell")), in a pack. Use it, or break it, to take |
| 5 | -- it all back; anyone can, so be quick. What doesn't fit stays in the pack. |
| 6 | -- On Peaceful and Gentle worlds you keep what you carry. What's in each |
| 7 | -- pack is kept with the world (store), by where it lies. |
| 8 | |
| 9 | local world = require("@potluck/world@0.21") |
| 10 | local tweak = require("@potluck/tweak") |
| 11 | local store = require("@potluck/store") |
| 12 | local ui = require("@potluck/ui@0.2") |
| 13 | local sound = require("@potluck/sound@0.2") |
| 14 | |
| 15 | local knobs = tweak { |
| 16 | on = { true, label = "Drop what you carry when you fall (Normal and Hard worlds)", order = 1 }, |
| 17 | gentle = { false, label = "On Gentle worlds too", order = 2 }, |
| 18 | } |
| 19 | |
| 20 | local PACK = "Lost pack" |
| 21 | world.drops(PACK, { gives = false }) |
| 22 | |
| 23 | -- what's in each pack, and whose it was, by "x,y,z" |
| 24 | type Thing = { name: string, n: number } |
| 25 | type Pack = { owner: string, things: { Thing } } |
| 26 | local packs: { [string]: Pack } = store.get("packs") or {} |
| 27 | |
| 28 | local function key(at: vector): string |
| 29 | return math.floor(at.x) .. "," .. math.floor(at.y) .. "," .. math.floor(at.z) |
| 30 | end |
| 31 | |
| 32 | local function save() |
| 33 | store.set("packs", packs) |
| 34 | end |
| 35 | |
| 36 | local function drops_here(): boolean |
| 37 | local d = world.danger() |
| 38 | return knobs.on and (d == "normal" or d == "hard" or (d == "gentle" and knobs.gentle)) |
| 39 | end |
| 40 | |
| 41 | -- a block a pack can lie in: air, or a plant it can sit in place of |
| 42 | local function room(at: vector): boolean |
| 43 | local b = world.block(at) |
| 44 | if b == "Air" then |
| 45 | return true |
| 46 | end |
| 47 | local ok, kind = pcall(world.kind, b) |
| 48 | return ok and kind == "plant" |
| 49 | end |
| 50 | |
| 51 | local function solid(at: vector): boolean |
| 52 | local ok, kind = pcall(world.kind, world.block(at)) |
| 53 | return ok and kind ~= "air" and kind ~= "water" and kind ~= "plant" |
| 54 | end |
| 55 | |
| 56 | -- where it lies: where they fell, or the nearest place near it with room, |
| 57 | -- on the ground if there's ground under it (not in water or mid-air) |
| 58 | local function spot(at: vector): vector? |
| 59 | local x, y, z = math.floor(at.x), math.floor(at.y), math.floor(at.z) |
| 60 | if y < -127 or y > 127 then |
| 61 | return nil |
| 62 | end |
| 63 | local best: vector? = nil |
| 64 | local best_d = math.huge |
| 65 | for dy = 0, 3 do |
| 66 | for dx = -2, 2 do |
| 67 | for dz = -2, 2 do |
| 68 | local p = vector.create(x + dx, y + dy, z + dz) |
| 69 | if room(p) and packs[key(p)] == nil and world.block(p) ~= PACK then |
| 70 | -- standing on something beats floating |
| 71 | local d = dx * dx + dz * dz + dy * dy * 2 + (if solid(p - vector.create(0, 1, 0)) then 0 else 6) |
| 72 | if d < best_d then |
| 73 | best, best_d = p, d |
| 74 | end |
| 75 | end |
| 76 | end |
| 77 | end |
| 78 | end |
| 79 | return best |
| 80 | end |
| 81 | |
| 82 | world.on("player_fell", function(player, at: vector) |
| 83 | if not drops_here() then |
| 84 | return |
| 85 | end |
| 86 | local carried = world.pockets(player) |
| 87 | if carried == nil or next(carried) == nil then |
| 88 | return |
| 89 | end |
| 90 | local p = spot(at) |
| 91 | if not p then |
| 92 | return |
| 93 | end |
| 94 | local things: { Thing } = {} |
| 95 | for name, n in carried do |
| 96 | if n > 0 and world.take(player, name, n) then |
| 97 | table.insert(things, { name = name, n = n }) |
| 98 | end |
| 99 | end |
| 100 | if #things == 0 then |
| 101 | return |
| 102 | end |
| 103 | table.sort(things, function(a, b) |
| 104 | return a.name < b.name |
| 105 | end) |
| 106 | world.set_block(p, PACK) |
| 107 | packs[key(p)] = { owner = player.name, things = things } |
| 108 | save() |
| 109 | ui.show(player, "lost_pack", "You dropped what you carried where you fell. Go back for your pack.", { at = "left" }) |
| 110 | world.after(12, function() |
| 111 | ui.hide(player, "lost_pack") |
| 112 | end) |
| 113 | end) |
| 114 | |
| 115 | -- gives what's in the pack at `at` to a player, as much as fits; true if |
| 116 | -- it's empty now |
| 117 | local function open(player, at: vector): boolean |
| 118 | local k = key(at) |
| 119 | local pack = packs[k] |
| 120 | if not pack then |
| 121 | return true |
| 122 | end |
| 123 | if world.pockets(player) == nil then |
| 124 | ui.show(player, "lost_pack", "Only someone in survival can carry what's in it.", { at = "bottom" }) |
| 125 | world.after(4, function() |
| 126 | ui.hide(player, "lost_pack") |
| 127 | end) |
| 128 | return false |
| 129 | end |
| 130 | local left: { Thing } = {} |
| 131 | local got = 0 |
| 132 | for _, t in pack.things do |
| 133 | local given = world.give(player, t.name, t.n) |
| 134 | got += given |
| 135 | if given < t.n then |
| 136 | table.insert(left, { name = t.name, n = t.n - given }) |
| 137 | end |
| 138 | end |
| 139 | local whose = if pack.owner == player.name then "your" else pack.owner .. "'s" |
| 140 | local said |
| 141 | if #left == 0 then |
| 142 | packs[k] = nil |
| 143 | said = "You took everything from " .. whose .. " pack." |
| 144 | else |
| 145 | pack.things = left |
| 146 | said = if got > 0 then "Your pockets are full: the rest stays in " .. whose .. " pack." else "Your pockets are full." |
| 147 | end |
| 148 | save() |
| 149 | if got > 0 then |
| 150 | sound.play("pop", { at = at, volume = 0.6, pitch = 0.8, caption = "A pack is opened" }) |
| 151 | end |
| 152 | ui.show(player, "lost_pack", said, { at = "bottom" }) |
| 153 | world.after(4, function() |
| 154 | ui.hide(player, "lost_pack") |
| 155 | end) |
| 156 | return #left == 0 |
| 157 | end |
| 158 | |
| 159 | world.on("block_used", function(player, at: vector, block: string) |
| 160 | if block ~= PACK then |
| 161 | return |
| 162 | end |
| 163 | if open(player, at) then |
| 164 | world.set_block(at, "Air") |
| 165 | end |
| 166 | end) |
| 167 | |
| 168 | -- broken, it's opened, and what's left lies there still |
| 169 | world.on("block_broken", function(player, at: vector, block: string) |
| 170 | if block ~= PACK then |
| 171 | return |
| 172 | end |
| 173 | if not open(player, at) and world.block(at) == "Air" then |
| 174 | world.set_block(at, PACK) |
| 175 | end |
| 176 | end) |
| 177 | |
| 178 | -- a pack someone took away by other means (a creation, a world edit) is |
| 179 | -- forgotten |
| 180 | world.every(30, function() |
| 181 | local gone = false |
| 182 | for k, _ in packs do |
| 183 | local x, y, z = string.match(k, "^(-?%d+),(-?%d+),(-?%d+)$") |
| 184 | if x and world.block(vector.create(tonumber(x) :: number, tonumber(y) :: number, tonumber(z) :: number)) ~= PACK then |
| 185 | -- far from everyone the world reads as air; only forget it nearby |
| 186 | local at = vector.create(tonumber(x) :: number + 0.5, tonumber(y) :: number, tonumber(z) :: number + 0.5) |
| 187 | if world.nearest_player(at, 24) then |
| 188 | packs[k] = nil |
| 189 | gone = true |
| 190 | end |
| 191 | end |
| 192 | end |
| 193 | if gone then |
| 194 | save() |
| 195 | end |
| 196 | end) |