Hazel keeps the island's collection of relics on the Green. Bring her the old things you find, in the hills, in old chests and deep in the stone, and hear each one's story; the first of each joins the collection, and she gives you something good for it.
| 1 | --!strict |
| 2 | -- The Curator: Hazel keeps the island's collection of relics, old things |
| 3 | -- found out in the world (the Gnome Diggings' brass compass, a fossil from |
| 4 | -- deep stone, a gloom pearl, a singing crystal, an old coin from an old |
| 5 | -- chest, and any other creation's: an item is a relic when its [items.*] |
| 6 | -- in potluck.toml has a `relic` line, world 0.26: its story, which she |
| 7 | -- tells you). |
| 8 | -- |
| 9 | -- Interact with her in survival and give her a relic you carry. The first |
| 10 | -- of each kind goes in the world's collection (kept with @potluck/store, |
| 11 | -- with who found it), she tells you its story, and she gives XP and |
| 12 | -- something good for it (GIFTS below, better as the collection grows); one |
| 13 | -- she has already she takes as a spare, for a little gold. When the collection holds every relic |
| 14 | -- there is in this world, whoever brought the last one gets her best |
| 15 | -- thanks. Ask to see the collection any time, in creative too. |
| 16 | -- |
| 17 | -- She makes one relic herself, the old coin, which turns up now and then |
| 18 | -- when a player digs earth (EARTH below) in survival, and in old chests. |
| 19 | |
| 20 | local world = require("@potluck/world@0.26") |
| 21 | local tweak = require("@potluck/tweak") |
| 22 | local store = require("@potluck/store") |
| 23 | local ui = require("@potluck/ui@0.2") |
| 24 | local sound = require("@potluck/sound@0.2") |
| 25 | |
| 26 | local knobs = tweak { |
| 27 | coat = { "#3f6b4f", label = "Hazel's coat", order = 1 }, |
| 28 | generous = { 1, min = 0.5, max = 3, label = "How generous she is", order = 2 }, |
| 29 | coins = { 0.004, min = 0, max = 0.1, label = "Old coins in the earth", order = 3 }, |
| 30 | } |
| 31 | |
| 32 | -- what old coins turn up in, now and then, when a player digs it |
| 33 | local EARTH = { Dirt = true, Gravel = true, Sand = true, Grass = true, Farmland = true } |
| 34 | |
| 35 | -- What she gives for the first of each relic, by how many the collection |
| 36 | -- held before it: the first relic gets GIFTS[1], the second GIFTS[2], and |
| 37 | -- from the last one on, the last again. |
| 38 | type Gift = { thing: string, n: number, xp: number } |
| 39 | local GIFTS: { Gift } = { |
| 40 | { thing = "Gold ingot", n = 2, xp = 20 }, |
| 41 | { thing = "Crystal", n = 2, xp = 25 }, |
| 42 | { thing = "Iron ingot", n = 6, xp = 30 }, |
| 43 | { thing = "Moonsilver ingot", n = 1, xp = 35 }, |
| 44 | { thing = "Crystal", n = 4, xp = 40 }, |
| 45 | { thing = "Moonsilver ingot", n = 2, xp = 45 }, |
| 46 | } |
| 47 | -- for a relic she already has |
| 48 | local SPARE: Gift = { thing = "Gold ingot", n = 1, xp = 5 } |
| 49 | -- for the one that makes the collection whole |
| 50 | local WHOLE: Gift = { thing = "Moonsilver ingot", n = 3, xp = 100 } |
| 51 | |
| 52 | local LINES = { |
| 53 | "Found anything old?", |
| 54 | "Every relic has a story.", |
| 55 | "The gnomes left such lovely things.", |
| 56 | "Mind how you go underground.", |
| 57 | } |
| 58 | |
| 59 | local Curator = world.creature { |
| 60 | name = "Curator", |
| 61 | speed = 1.2, |
| 62 | count = 1, |
| 63 | home = "green", |
| 64 | body = { |
| 65 | { "leg_left", size = { 0.2, 0.62, 0.22 }, at = { -0.12, 0.31, 0 }, color = "#3b3340", leg = true }, |
| 66 | { "leg_right", size = { 0.2, 0.62, 0.22 }, at = { 0.12, 0.31, 0 }, color = "#3b3340", leg = true }, |
| 67 | { "body", size = { 0.5, 0.66, 0.3 }, at = { 0, 0.95, 0 }, color = knobs.coat }, |
| 68 | { "collar", size = { 0.3, 0.1, 0.06 }, at = { 0, 1.24, -0.15 }, color = "#efe8d6" }, |
| 69 | { "arm_left", size = { 0.14, 0.58, 0.16 }, at = { -0.33, 0.98, 0 }, color = knobs.coat, leg = true }, |
| 70 | { "arm_right", size = { 0.14, 0.58, 0.16 }, at = { 0.33, 0.98, 0 }, color = knobs.coat, leg = true }, |
| 71 | { "head", size = { 0.42, 0.42, 0.4 }, at = { 0, 1.5, 0 }, color = "#d9a77f", head = true }, |
| 72 | { "hair", size = { 0.46, 0.14, 0.44 }, at = { 0, 1.75, 0.02 }, color = "#b8b4ad", head = true }, |
| 73 | { "bun", size = { 0.2, 0.18, 0.18 }, at = { 0, 1.82, 0.2 }, color = "#b8b4ad", head = true }, |
| 74 | { "eye_left", size = { 0.06, 0.06, 0.02 }, at = { -0.09, 1.53, -0.21 }, color = "#2b2b33", head = true }, |
| 75 | { "eye_right", size = { 0.06, 0.06, 0.02 }, at = { 0.09, 1.53, -0.21 }, color = "#2b2b33", head = true }, |
| 76 | { "lens_left", size = { 0.12, 0.1, 0.02 }, at = { -0.09, 1.53, -0.225 }, color = "#c89a3a", head = true }, |
| 77 | { "lens_right", size = { 0.12, 0.1, 0.02 }, at = { 0.09, 1.53, -0.225 }, color = "#c89a3a", head = true }, |
| 78 | }, |
| 79 | } |
| 80 | |
| 81 | -- ---- what's kept ---- |
| 82 | |
| 83 | -- the collection: each relic's name, and who found it |
| 84 | local collection: { [string]: string } = store.get("collection") or {} |
| 85 | -- how many relics there were when the collection was last made whole, so |
| 86 | -- it's thanked for once (and again if another creation adds more) |
| 87 | local whole: number = store.get("whole") or 0 |
| 88 | |
| 89 | local function save() |
| 90 | store.set("collection", collection) |
| 91 | store.set("whole", whole) |
| 92 | end |
| 93 | |
| 94 | -- ---- relics ---- |
| 95 | |
| 96 | -- The relics there are in this world, by name, with their lines: every |
| 97 | -- item a creation made with a `relic` line. |
| 98 | local function relics(): { [string]: string } |
| 99 | local out = {} |
| 100 | for _, name in world.items() do |
| 101 | local it = world.item(name) |
| 102 | if it and type(it.relic) == "string" then |
| 103 | out[name] = it.relic |
| 104 | end |
| 105 | end |
| 106 | return out |
| 107 | end |
| 108 | |
| 109 | local function count(t: { [string]: any }): number |
| 110 | local n = 0 |
| 111 | for _ in t do |
| 112 | n += 1 |
| 113 | end |
| 114 | return n |
| 115 | end |
| 116 | |
| 117 | -- The relics a player carries, in name order. |
| 118 | local function carried(player): { string } |
| 119 | local out = {} |
| 120 | for name in relics() do |
| 121 | if world.has(player, name) > 0 then |
| 122 | table.insert(out, name) |
| 123 | end |
| 124 | end |
| 125 | table.sort(out) |
| 126 | return out |
| 127 | end |
| 128 | |
| 129 | -- Gives a gift, its things and XP; says what it was, or nil when there |
| 130 | -- wasn't room for it. |
| 131 | local function give(player, g: Gift): string? |
| 132 | local n = math.max(1, math.floor(g.n * knobs.generous)) |
| 133 | local thing = g.thing |
| 134 | if not pcall(world.kind, thing) then |
| 135 | thing, n = "Gold ingot", math.max(1, n) |
| 136 | end |
| 137 | if world.give(player, thing, n) < n then |
| 138 | return nil |
| 139 | end |
| 140 | world.reward(player, math.floor(g.xp * knobs.generous)) |
| 141 | return n .. " " .. string.lower(thing) |
| 142 | end |
| 143 | |
| 144 | -- ---- talking ---- |
| 145 | |
| 146 | local offer: (any, any) -> () |
| 147 | |
| 148 | -- What's in the collection, as a question: who found each, and how many |
| 149 | -- are still to find. |
| 150 | local function show_collection(hazel, player) |
| 151 | local all = relics() |
| 152 | local names = {} |
| 153 | for name in collection do |
| 154 | if all[name] then |
| 155 | table.insert(names, name) |
| 156 | end |
| 157 | end |
| 158 | table.sort(names) |
| 159 | local total = count(all) |
| 160 | local text |
| 161 | if total == 0 then |
| 162 | text = "There are no relics in this world yet. Creations make them: when one does, bring it to me." |
| 163 | elseif #names == 0 then |
| 164 | text = "The cases are all empty. There are " .. total .. " relics out there to find: in the hills, in old chests, deep in the stone." |
| 165 | else |
| 166 | local lines = {} |
| 167 | local room = 300 - 80 |
| 168 | for i, name in names do |
| 169 | local line = name .. " (found by " .. collection[name] .. ")" |
| 170 | if #line + 1 > room then |
| 171 | table.insert(lines, "and " .. (#names - i + 1) .. " more") |
| 172 | break |
| 173 | end |
| 174 | room -= #line + 1 |
| 175 | table.insert(lines, line) |
| 176 | end |
| 177 | local still = total - #names |
| 178 | text = "The collection: " .. #names .. " of " .. total .. ".\n\n" .. table.concat(lines, "\n") |
| 179 | text ..= if still == 0 then "\n\nEvery one! Thank you all." else "\n\n" .. still .. " still to find." |
| 180 | end |
| 181 | ui.ask(player, { title = "Hazel the curator", text = string.sub(text, 1, 300), choices = { "Back" } }, function(who, choice) |
| 182 | if choice == 1 then |
| 183 | offer(hazel, who) |
| 184 | end |
| 185 | end) |
| 186 | end |
| 187 | |
| 188 | -- Takes a relic from a player into the collection, or as a spare. |
| 189 | local function take(hazel, player, name: string) |
| 190 | local all = relics() |
| 191 | local line = all[name] |
| 192 | if not line or not world.take(player, name, 1) then |
| 193 | hazel:say("Hm, I don't see it on you.") |
| 194 | return |
| 195 | end |
| 196 | local first = collection[name] == nil |
| 197 | local gift: Gift = SPARE |
| 198 | if first then |
| 199 | gift = GIFTS[math.min(count(collection) + 1, #GIFTS)] |
| 200 | end |
| 201 | local got = give(player, gift) |
| 202 | if not got then |
| 203 | world.give(player, name, 1) |
| 204 | hazel:say("Your pockets are full! Make a little room first.") |
| 205 | return |
| 206 | end |
| 207 | if not first then |
| 208 | save() |
| 209 | hazel:say("Another " .. string.lower(name) .. "! I'll keep it as a spare. Here's " .. got .. ".") |
| 210 | sound.play("pop", { at = hazel }) |
| 211 | return |
| 212 | end |
| 213 | collection[name] = player.name |
| 214 | -- is it whole now? |
| 215 | local had = 0 |
| 216 | for n in all do |
| 217 | if collection[n] then |
| 218 | had += 1 |
| 219 | end |
| 220 | end |
| 221 | local total = count(all) |
| 222 | local now_whole = had == total and total > whole |
| 223 | if now_whole then |
| 224 | whole = total |
| 225 | local more = give(player, WHOLE) |
| 226 | if more then |
| 227 | got ..= " and " .. more |
| 228 | end |
| 229 | end |
| 230 | save() |
| 231 | sound.play("sparkle", { at = hazel, caption = "Hazel is delighted" }) |
| 232 | hazel:say(if now_whole then "That's every relic there is! The collection's whole!" else "Oh, how wonderful!") |
| 233 | local text = line .. "\n\nIt joins the collection, and Hazel gives you " .. got .. "." |
| 234 | if now_whole then |
| 235 | text ..= "\n\nThat makes the collection whole: every relic in this world. Thank you!" |
| 236 | end |
| 237 | ui.ask(player, { title = string.sub(name, 1, 40), text = string.sub(text, 1, 300), choices = { "Lovely", "See the collection" } }, function(who, choice) |
| 238 | if choice == 2 then |
| 239 | show_collection(hazel, who) |
| 240 | end |
| 241 | end) |
| 242 | end |
| 243 | |
| 244 | offer = function(hazel, player) |
| 245 | local text, choices = "", {} |
| 246 | local mine: { string } = {} |
| 247 | if world.pockets(player) == nil then |
| 248 | text = "In survival, bring me relics you find out in the world, and I'll give you something good for each. You can see the collection now, though." |
| 249 | else |
| 250 | mine = carried(player) |
| 251 | if #mine == 0 then |
| 252 | text = "Relics turn up in the hills, in old chests, in geodes and deep in the stone. Bring me what you find!" |
| 253 | else |
| 254 | text = "Ooh, what have you got there? Which will you give me?" |
| 255 | for i, name in mine do |
| 256 | if i > 6 then |
| 257 | break |
| 258 | end |
| 259 | table.insert(choices, string.sub(name, 1, 30)) |
| 260 | end |
| 261 | end |
| 262 | end |
| 263 | table.insert(choices, "See the collection") |
| 264 | ui.ask(player, { title = "Hazel the curator", text = text, choices = choices }, function(who, choice) |
| 265 | if not choice then |
| 266 | return |
| 267 | end |
| 268 | local name = mine[choice] |
| 269 | if name and choice <= 6 then |
| 270 | take(hazel, who, name) |
| 271 | else |
| 272 | show_collection(hazel, who) |
| 273 | end |
| 274 | end) |
| 275 | end |
| 276 | |
| 277 | -- ---- Hazel ---- |
| 278 | |
| 279 | -- who she's already asked about the relic in their hand, by player and relic |
| 280 | local noticed: { [string]: boolean } = {} |
| 281 | |
| 282 | function Curator:spawned() |
| 283 | self.chat = world.random(4, 12) |
| 284 | end |
| 285 | |
| 286 | function Curator:tick(dt: number) |
| 287 | local p = world.nearest_player(self.position, 4) |
| 288 | if not p then |
| 289 | self:wander(1) |
| 290 | return |
| 291 | end |
| 292 | self:stop() |
| 293 | self:face(p.position) |
| 294 | local held = world.held(p) |
| 295 | if held and world.pockets(p) ~= nil and collection[held] == nil and relics()[held] then |
| 296 | local k = p.name .. "/" .. held |
| 297 | if not noticed[k] then |
| 298 | noticed[k] = true |
| 299 | self:say("Oh! Is that a " .. string.lower(held) .. "? Do let me see!") |
| 300 | self.chat = world.random(20, 40) |
| 301 | return |
| 302 | end |
| 303 | end |
| 304 | self.chat -= dt |
| 305 | if self.chat <= 0 then |
| 306 | self.chat = world.random(20, 40) |
| 307 | self:say(LINES[math.floor(world.random(1, #LINES + 1))]) |
| 308 | end |
| 309 | end |
| 310 | |
| 311 | function Curator:used(player) |
| 312 | self:face(player.position) |
| 313 | sound.play("grumble", { at = self, pitch = 1.8, volume = 0.5, caption = "Hazel hums" }) |
| 314 | offer(self, player) |
| 315 | end |
| 316 | |
| 317 | function Curator:hit() |
| 318 | self:hop(0.4) |
| 319 | self:say("Careful! I'm not one of the exhibits.") |
| 320 | end |
| 321 | |
| 322 | -- an old coin, now and then, in the earth |
| 323 | world.on("block_broken", function(player, _at: vector, block: string) |
| 324 | if EARTH[block] and world.pockets(player) ~= nil and world.random() < knobs.coins then |
| 325 | if world.give(player, "Old coin", 1) > 0 then |
| 326 | sound.play("sparkle", { at = player.position, volume = 0.5, caption = "Something glints in the earth" }) |
| 327 | end |
| 328 | end |
| 329 | end) |