Rowan the carpenter and Ivy the mason live on the Green, and villages grow up far from it as you explore, built to suit the land: a street, a well and a house for each villager. Thirteen jobs trade with you: sell them what you've gathered for beads and spend beads on what they make, and the better they know you the more they show you. A peddler and his pack yak come by now and then.
| 1 | --!strict |
| 2 | -- Villagers: Rowan the carpenter and Ivy the mason live on the Green, and |
| 3 | -- out in the world, far from it, villages grow up as players come near: |
| 4 | -- a street with a well, a house for each villager and their work by the |
| 5 | -- door, built in the style of the land (sandstone in the desert, log |
| 6 | -- cabins in the pines, snow on the roofs in the north, raised floors in |
| 7 | -- the marsh). Each villager has a job (trades.luau has thirteen), keeps |
| 8 | -- a day (out to the well in the morning, at work through the day, home at |
| 9 | -- dusk) and trades: sell them what you've gathered for beads, and spend |
| 10 | -- beads on what they make. The more you trade with one, the more they |
| 11 | -- show you. A peddler with a pack yak comes by now and then with things |
| 12 | -- from far off. |
| 13 | -- |
| 14 | -- Villages, and how well each villager knows you, are kept with |
| 15 | -- @potluck/store. TRADES are in trades.luau, the looks in looks.luau and |
| 16 | -- how a village is laid out and built in villages.luau, so a remix can |
| 17 | -- change any of them. |
| 18 | |
| 19 | local world = require("@potluck/world@0.33") |
| 20 | local tweak = require("@potluck/tweak") |
| 21 | local ui = require("@potluck/ui@0.2") |
| 22 | local sound = require("@potluck/sound@0.2") |
| 23 | local store = require("@potluck/store") |
| 24 | local trades = require("./trades") |
| 25 | local looks = require("./looks") |
| 26 | local villages = require("./villages") |
| 27 | |
| 28 | type Trade = trades.Trade |
| 29 | type Village = villages.Village |
| 30 | |
| 31 | local knobs = tweak { |
| 32 | generous = { 1, min = 1, max = 3, label = "Gives this many times as much", order = 1 }, |
| 33 | villages = { true, label = "Villages out in the world", order = 2 }, |
| 34 | apart = { 420, min = 150, max = 1500, label = "How far apart villages are", order = 3 }, |
| 35 | away = { 130, min = 60, max = 400, label = "How far from the Green the nearest is", order = 4 }, |
| 36 | stock = { 1, min = 0.5, max = 4, label = "How much villagers have each day", order = 5 }, |
| 37 | peddler = { 25, min = 5, max = 120, label = "Minutes between the peddler's visits", order = 6 }, |
| 38 | } |
| 39 | |
| 40 | local BEADS = trades.BEADS |
| 41 | local JOBS = trades.JOBS |
| 42 | local exists = villages.exists |
| 43 | |
| 44 | -- How many trades it takes for a villager to know you: a regular, then a friend. |
| 45 | local TRUST = { 6, 18 } |
| 46 | local TIER_NAMES = { "a new face", "a regular", "a friend" } |
| 47 | local FOR_TIER = { "", "regulars", "friends" } |
| 48 | local NEAR = 72 -- a village's folk come out when someone's this near |
| 49 | local GONE = 110 -- and go in when nobody's this near |
| 50 | local MOST = 40 -- villages a world keeps track of |
| 51 | local MOST_FOLK = 58 -- creatures at once, of this creation's 64 |
| 52 | |
| 53 | -- ---- the kinds ---- |
| 54 | |
| 55 | local ORDER = { "Carpenter", "Mason", "Baker", "Angler", "Weaver", "Smith", "Grower", "Chandler", "Tanner", "Herbalist", "Wayfinder", "Beekeeper", "Bowyer" } |
| 56 | local KINDS: { [string]: any } = {} |
| 57 | for _, job in ORDER do |
| 58 | KINDS[job] = world.creature { |
| 59 | name = job, |
| 60 | speed = 1.4, |
| 61 | count = 0, |
| 62 | home = "here", |
| 63 | -- under a tall hat they still fit through a doorway two blocks high |
| 64 | size = { 0.6, 1.85, 0.6 }, |
| 65 | -- and open doors as they come to them |
| 66 | doors = true, |
| 67 | body = looks.person(looks.LOOKS[job]), |
| 68 | } |
| 69 | end |
| 70 | local Peddler = world.creature { name = "Peddler", speed = 1.5, count = 0, home = "here", size = { 0.6, 1.85, 0.6 }, doors = true, body = looks.peddler() } |
| 71 | local Yak = world.creature { name = "Pack yak", speed = 1.7, count = 0, home = "here", body = looks.yak() } |
| 72 | |
| 73 | -- ---- what's kept ---- |
| 74 | |
| 75 | local list: { Village } = store.get("villages") or {} |
| 76 | -- how many trades each village's villager of each job has done with anyone |
| 77 | local trust: { [string]: number } = store.get("trust") or {} |
| 78 | |
| 79 | local function save() |
| 80 | store.set("villages", list) |
| 81 | store.set("trust", trust) |
| 82 | end |
| 83 | |
| 84 | local by_id: { [string]: Village } = {} |
| 85 | local function index() |
| 86 | by_id = {} |
| 87 | for _, v in list do |
| 88 | by_id[v.id] = v |
| 89 | end |
| 90 | end |
| 91 | index() |
| 92 | |
| 93 | -- The Green's own two, who were here before any village. |
| 94 | local GREEN_FOLK = { { job = "Carpenter", name = "Rowan" }, { job = "Mason", name = "Ivy" } } |
| 95 | |
| 96 | -- ---- words ---- |
| 97 | |
| 98 | -- "1 bead", "3 beads", "4 Planks" |
| 99 | local function amount(n: number, thing: string): string |
| 100 | if thing == BEADS then |
| 101 | return if n == 1 then "1 bead" else n .. " beads" |
| 102 | end |
| 103 | return n .. " " .. thing |
| 104 | end |
| 105 | |
| 106 | local function costs(t: Trade): string |
| 107 | local parts = {} |
| 108 | for thing, n in t.want do |
| 109 | table.insert(parts, amount(n, thing)) |
| 110 | end |
| 111 | table.sort(parts) |
| 112 | return table.concat(parts, " and ") |
| 113 | end |
| 114 | |
| 115 | local function makes(t: Trade): number |
| 116 | return math.max(1, math.floor(t.n * knobs.generous)) |
| 117 | end |
| 118 | |
| 119 | -- what a trade gets you: "3 Bread", or a service's name |
| 120 | local function gets(t: Trade): string |
| 121 | if t.does then |
| 122 | return t.label or t.does |
| 123 | end |
| 124 | return amount(makes(t), t.give) |
| 125 | end |
| 126 | |
| 127 | -- "north-east", from a step across the ground (north is -z) |
| 128 | local function way_name(dx: number, dz: number): string |
| 129 | local a = math.deg(math.atan2(dx, -dz)) % 360 |
| 130 | local names = { "north", "north-east", "east", "south-east", "south", "south-west", "west", "north-west" } |
| 131 | return names[math.floor((a + 22.5) / 45) % 8 + 1] |
| 132 | end |
| 133 | |
| 134 | local function flat(a: vector, b: vector): number |
| 135 | return vector.magnitude(vector.create(a.x - b.x, 0, a.z - b.z)) |
| 136 | end |
| 137 | |
| 138 | -- one line on a player's screen for a few seconds |
| 139 | local shown: { [number]: number } = {} |
| 140 | local function tell(player, text: string, seconds: number?) |
| 141 | ui.show(player, "villagers", text, { at = "left" }) |
| 142 | local mine = (shown[player.id] or 0) + 1 |
| 143 | shown[player.id] = mine |
| 144 | world.after(seconds or 5, function() |
| 145 | if shown[player.id] == mine then |
| 146 | shown[player.id] = nil |
| 147 | ui.hide(player, "villagers") |
| 148 | end |
| 149 | end) |
| 150 | end |
| 151 | |
| 152 | -- Whether every thing a trade names is in this world. |
| 153 | local function usable(t: Trade): boolean |
| 154 | if t.give ~= "" and not exists(t.give) then |
| 155 | return false |
| 156 | end |
| 157 | for thing in t.want do |
| 158 | if not exists(thing) then |
| 159 | return false |
| 160 | end |
| 161 | end |
| 162 | return true |
| 163 | end |
| 164 | |
| 165 | -- ---- days ---- |
| 166 | |
| 167 | -- what each villager has sold today, by village, job and trade |
| 168 | local sold: { [string]: number } = {} |
| 169 | local last_time = world.daytime() |
| 170 | world.every(5, function() |
| 171 | local now = world.daytime() |
| 172 | if now < last_time - 0.5 then |
| 173 | -- a new day: everyone's restocked |
| 174 | sold = {} |
| 175 | end |
| 176 | last_time = now |
| 177 | end) |
| 178 | |
| 179 | -- Where in their day a villager is: "inside", "well", "work" or "field". |
| 180 | local function phase(job: string, field: boolean): string |
| 181 | local d = world.daytime() |
| 182 | if d < 0.24 or d >= 0.76 then |
| 183 | return "inside" |
| 184 | elseif d < 0.31 or (d >= 0.62 and d < 0.7) then |
| 185 | return "well" |
| 186 | elseif d < 0.7 then |
| 187 | return if job == "Grower" and field then "field" else "work" |
| 188 | end |
| 189 | return "inside" |
| 190 | end |
| 191 | |
| 192 | -- ---- walking about a village ---- |
| 193 | |
| 194 | -- The way from one place in a village to another, through the house's step. |
| 195 | local TO_STEP: { [string]: { string } } = { |
| 196 | inside = { "door", "porch" }, |
| 197 | door = { "porch" }, |
| 198 | porch = {}, |
| 199 | work = { "porch" }, |
| 200 | well = { "street", "porch" }, |
| 201 | street = { "porch" }, |
| 202 | field = { "row", "inner", "gate", "street", "porch" }, |
| 203 | } |
| 204 | local FROM_STEP: { [string]: { string } } = { |
| 205 | inside = { "door", "inside" }, |
| 206 | porch = {}, |
| 207 | work = { "work" }, |
| 208 | well = { "street", "well" }, |
| 209 | field = { "street", "gate", "inner", "row", "field" }, |
| 210 | } |
| 211 | |
| 212 | local function places(v: Village, i: number): { [string]: vector } |
| 213 | local p = villages.places(v, i) |
| 214 | -- into the field by its gate, then along a row |
| 215 | local f = villages.FIELD |
| 216 | local side = if i % 2 == 0 then 1 else -1 |
| 217 | p.inner = villages.spot(v, f[1], 1, 0) + vector.create(0.5, 0.05, 0.5) |
| 218 | p.row = villages.spot(v, f[1], 1, side) + vector.create(0.5, 0.05, 0.5) |
| 219 | p.field = villages.spot(v, f[1] + 1 + (i % 5), 1, side) + vector.create(0.5, 0.05, 0.5) |
| 220 | return p |
| 221 | end |
| 222 | |
| 223 | local function route(v: Village, i: number, from: string, to: string): { { number } } |
| 224 | local p = places(v, i) |
| 225 | local points = {} |
| 226 | for _, name in TO_STEP[from] or { "porch" } do |
| 227 | table.insert(points, name) |
| 228 | end |
| 229 | for _, name in FROM_STEP[to] or {} do |
| 230 | table.insert(points, name) |
| 231 | end |
| 232 | local out = {} |
| 233 | for _, name in points do |
| 234 | local at = p[name] |
| 235 | if at then |
| 236 | table.insert(out, { at.x, at.y, at.z }) |
| 237 | end |
| 238 | end |
| 239 | return out |
| 240 | end |
| 241 | |
| 242 | -- ---- the folk ---- |
| 243 | |
| 244 | -- who's out, by creature id: "<village>#<house>" |
| 245 | local out: { [number]: string } = {} |
| 246 | -- who's been sent out but hasn't arrived yet: where, and when |
| 247 | type Arrival = { at: vector, village: string, house: number, name: string, place: string, when: number } |
| 248 | local arriving: { Arrival } = {} |
| 249 | |
| 250 | local function folk_kinds(): { any } |
| 251 | local kinds = {} |
| 252 | for _, job in ORDER do |
| 253 | table.insert(kinds, KINDS[job]) |
| 254 | end |
| 255 | return kinds |
| 256 | end |
| 257 | local ALL_KINDS = folk_kinds() |
| 258 | |
| 259 | local function send_out(kind: any, at: vector, village: string, house: number, name: string, place: string): boolean |
| 260 | table.insert(arriving, { at = at, village = village, house = house, name = name, place = place, when = world.time() }) |
| 261 | local ok = pcall(world.spawn, kind, at) |
| 262 | if not ok then |
| 263 | table.remove(arriving) |
| 264 | end |
| 265 | return ok |
| 266 | end |
| 267 | |
| 268 | local function spawned(self) |
| 269 | -- who this is: the nearest arrival sent out to about here |
| 270 | local best, bi = math.huge, 0 |
| 271 | for i, a in arriving do |
| 272 | local d = flat(self.position, a.at) |
| 273 | if d < best then |
| 274 | best, bi = d, i |
| 275 | end |
| 276 | end |
| 277 | if bi > 0 and best < 3 then |
| 278 | local a = arriving[bi] |
| 279 | table.remove(arriving, bi) |
| 280 | self.village = a.village |
| 281 | self.house = a.house |
| 282 | self.person = a.name |
| 283 | self.at_place = a.place |
| 284 | self.going = a.place |
| 285 | out[self.id] = a.village .. "#" .. a.house |
| 286 | end |
| 287 | self.chat = world.random(4, 12) |
| 288 | self.think = world.random(0, 0.5) |
| 289 | end |
| 290 | |
| 291 | -- ---- trading ---- |
| 292 | |
| 293 | local function key_of(villager): string |
| 294 | return villager.village .. "/" .. villager.kind |
| 295 | end |
| 296 | |
| 297 | local function tier_of(key: string): number |
| 298 | local t = trust[key] or 0 |
| 299 | if t >= TRUST[2] then |
| 300 | return 3 |
| 301 | elseif t >= TRUST[1] then |
| 302 | return 2 |
| 303 | end |
| 304 | return 1 |
| 305 | end |
| 306 | |
| 307 | local function stock_of(t: Trade): number |
| 308 | return math.max(1, math.floor((t.stock or 8) * knobs.stock)) |
| 309 | end |
| 310 | |
| 311 | -- "4 Planks for 1 Oak log", or just "4 Planks" when that's too long for a |
| 312 | -- choice (30 letters), with the price said in the question instead |
| 313 | local function label(t: Trade, out_of: boolean): (string, string?) |
| 314 | local full = gets(t) .. " for " .. costs(t) |
| 315 | if out_of then |
| 316 | local short = "Sold out: " .. gets(t) |
| 317 | return string.sub(short, 1, 30), nil |
| 318 | end |
| 319 | if #full <= 30 then |
| 320 | return full, nil |
| 321 | end |
| 322 | return string.sub(gets(t), 1, 30), gets(t) .. " costs " .. costs(t) .. "." |
| 323 | end |
| 324 | |
| 325 | local function say_safe(villager, text: string) |
| 326 | pcall(function() |
| 327 | villager:say(text) |
| 328 | end) |
| 329 | end |
| 330 | |
| 331 | -- The nearest village to a place but one, built or not. |
| 332 | local function nearest_village(from: vector, but: string?): (Village?, number) |
| 333 | local best, bd = nil, math.huge |
| 334 | for _, v in list do |
| 335 | local d = flat(from, v.at) |
| 336 | if v.id ~= but and d < bd and d > 40 then |
| 337 | best, bd = v, d |
| 338 | end |
| 339 | end |
| 340 | return best, bd |
| 341 | end |
| 342 | |
| 343 | local plan_far: (from: vector) -> Village? |
| 344 | |
| 345 | -- The wayfinder's service: where the nearest other village is. |
| 346 | local function the_way(villager, player): string |
| 347 | local v, d = nearest_village(player.position, villager.village) |
| 348 | if v == nil or d > knobs.apart * 2.5 then |
| 349 | v = plan_far(player.position) |
| 350 | if v then |
| 351 | d = flat(player.position, v.at) |
| 352 | end |
| 353 | end |
| 354 | if v == nil then |
| 355 | return "I can't think of one near here, I'm afraid." |
| 356 | end |
| 357 | local dx, dz = v.at.x - player.position.x, v.at.z - player.position.z |
| 358 | return v.name .. " lies " .. math.floor(d) .. " blocks " .. way_name(dx, dz) .. " of here." |
| 359 | end |
| 360 | |
| 361 | local offer: (villager: any, player: any, said: string?) -> () |
| 362 | |
| 363 | local function trade(villager, player, idx: number, t: Trade): string |
| 364 | local key = key_of(villager) |
| 365 | local stock_key = key .. "/" .. idx |
| 366 | if (sold[stock_key] or 0) >= stock_of(t) then |
| 367 | return "That's all I can spare today. Come back tomorrow!" |
| 368 | end |
| 369 | for thing, n in t.want do |
| 370 | if world.has(player, thing) < n then |
| 371 | return "You'll need " .. amount(n, thing) .. " for that." |
| 372 | end |
| 373 | end |
| 374 | local said: string |
| 375 | if t.does == "way" then |
| 376 | for thing, n in t.want do |
| 377 | world.take(player, thing, n) |
| 378 | end |
| 379 | said = the_way(villager, player) |
| 380 | else |
| 381 | for thing, n in t.want do |
| 382 | world.take(player, thing, n) |
| 383 | end |
| 384 | local want = makes(t) |
| 385 | local given = world.give(player, t.give, want) |
| 386 | if given == 0 then |
| 387 | for thing, n in t.want do |
| 388 | world.give(player, thing, n) |
| 389 | end |
| 390 | return "Your pockets are full! Make some room." |
| 391 | end |
| 392 | said = "There you go: " .. amount(given, t.give) .. "." |
| 393 | if given < want then |
| 394 | said = "That's all that would fit: " .. amount(given, t.give) .. "." |
| 395 | end |
| 396 | end |
| 397 | sold[stock_key] = (sold[stock_key] or 0) + 1 |
| 398 | local before = tier_of(key) |
| 399 | trust[key] = (trust[key] or 0) + 1 |
| 400 | save() |
| 401 | world.reward(player, 1) |
| 402 | sound.play("pop", { at = villager }) |
| 403 | local after = tier_of(key) |
| 404 | if after > before then |
| 405 | world.reward(player, 10) |
| 406 | sound.play("chime", { at = villager, caption = villager.person .. " knows you better" }) |
| 407 | said ..= if after == 2 then " You're a regular now: I've more to show you." else " We're friends now, you and I." |
| 408 | end |
| 409 | return said |
| 410 | end |
| 411 | |
| 412 | function offer(villager, player, said: string?) |
| 413 | local def = JOBS[villager.kind] |
| 414 | if def == nil then |
| 415 | return |
| 416 | end |
| 417 | local title = villager.person .. " " .. def.title |
| 418 | if world.pockets(player) == nil then |
| 419 | ui.ask(player, { |
| 420 | title = title, |
| 421 | text = "In creative you've every block already! Turn on survival in Settings, gather what you can, and come and trade.", |
| 422 | choices = { "I will" }, |
| 423 | }, function() end) |
| 424 | return |
| 425 | end |
| 426 | local key = key_of(villager) |
| 427 | local tier = tier_of(key) |
| 428 | local shown_trades: { { any } } = {} |
| 429 | local locked = 0 |
| 430 | for i, t in def.trades do |
| 431 | if usable(t) then |
| 432 | if t.tier <= tier then |
| 433 | table.insert(shown_trades, { i, t }) |
| 434 | else |
| 435 | locked += 1 |
| 436 | end |
| 437 | end |
| 438 | end |
| 439 | local choices, prices = {}, {} |
| 440 | for _, e in shown_trades do |
| 441 | local t = e[2] :: Trade |
| 442 | local short, long = label(t, (sold[key .. "/" .. e[1]] or 0) >= stock_of(t)) |
| 443 | table.insert(choices, short) |
| 444 | if long then |
| 445 | table.insert(prices, long) |
| 446 | end |
| 447 | end |
| 448 | local text = (said or "What can I do for you?") .. "\nYou're " .. TIER_NAMES[tier] .. " here." |
| 449 | if locked > 0 then |
| 450 | text ..= " I'll have more for you once I know you better." |
| 451 | end |
| 452 | if #prices > 0 then |
| 453 | text ..= "\n" .. table.concat(prices, "\n") |
| 454 | end |
| 455 | ui.ask(player, { title = string.sub(title, 1, 40), text = string.sub(text, 1, 300), choices = choices }, function(who, choice) |
| 456 | local e = choice and shown_trades[choice] |
| 457 | if e then |
| 458 | offer(villager, who, trade(villager, who, e[1] :: number, e[2] :: Trade)) |
| 459 | else |
| 460 | say_safe(villager, "Come back any time.") |
| 461 | end |
| 462 | end) |
| 463 | end |
| 464 | |
| 465 | -- ---- each villager's day ---- |
| 466 | |
| 467 | -- whether a creature's village is here still, and which house is theirs |
| 468 | local function home_of(self): (Village?, number) |
| 469 | local v = by_id[self.village or ""] |
| 470 | return v, self.house or 0 |
| 471 | end |
| 472 | |
| 473 | local function walk(self, dt: number): boolean |
| 474 | local r = self.route |
| 475 | if r == nil or #r == 0 then |
| 476 | return false |
| 477 | end |
| 478 | local p = r[1] |
| 479 | local to = vector.create(p[1], p[2], p[3]) |
| 480 | local d = flat(self.position, to) |
| 481 | if d < 0.45 then |
| 482 | table.remove(r, 1) |
| 483 | self.stuck = 0 |
| 484 | self.best = nil |
| 485 | if #r == 0 then |
| 486 | self:stop() |
| 487 | self.at_place = self.going |
| 488 | return false |
| 489 | end |
| 490 | p = r[1] |
| 491 | self:walk_to(vector.create(p[1], p[2], p[3]), 1.4) |
| 492 | return true |
| 493 | end |
| 494 | -- not getting any nearer: on to the next point |
| 495 | if self.best == nil or d < self.best - 0.2 then |
| 496 | self.best = d |
| 497 | self.stuck = 0 |
| 498 | else |
| 499 | self.stuck = (self.stuck or 0) + dt |
| 500 | end |
| 501 | if self.stuck > 5 then |
| 502 | table.remove(r, 1) |
| 503 | self.stuck = 0 |
| 504 | self.best = nil |
| 505 | if #r == 0 then |
| 506 | self.at_place = self.going |
| 507 | end |
| 508 | return true |
| 509 | end |
| 510 | self.walk_in = (self.walk_in or 0) - dt |
| 511 | if self.walk_in <= 0 then |
| 512 | self.walk_in = 1 |
| 513 | self:walk_to(to, 1.4) |
| 514 | end |
| 515 | return true |
| 516 | end |
| 517 | |
| 518 | local function tick(self, dt: number) |
| 519 | if self.village == nil then |
| 520 | -- from before there were villages, or not ours: the Green's are sent out again |
| 521 | self:remove() |
| 522 | return |
| 523 | end |
| 524 | out[self.id] = self.village .. "#" .. self.house |
| 525 | local v, i = home_of(self) |
| 526 | local near = world.nearest_player(self.position, 4) |
| 527 | -- someone's here to talk: stop and look at them |
| 528 | if near then |
| 529 | self:stop() |
| 530 | self:face(near.position) |
| 531 | self.walk_in = 0 |
| 532 | self.chat -= dt |
| 533 | if self.chat <= 0 then |
| 534 | self.chat = world.random(20, 40) |
| 535 | local def = JOBS[self.kind] |
| 536 | if def then |
| 537 | local lines = def.lines |
| 538 | if world.daytime() < 0.22 or world.daytime() > 0.8 then |
| 539 | self:say("Yawn... still up?") |
| 540 | else |
| 541 | self:say(lines[math.floor(world.random(1, #lines + 0.999))]) |
| 542 | end |
| 543 | end |
| 544 | end |
| 545 | return |
| 546 | end |
| 547 | if v == nil then |
| 548 | -- the Green's own |
| 549 | if self.village ~= "green" then |
| 550 | self:remove() |
| 551 | return |
| 552 | end |
| 553 | self:wander(1.2) |
| 554 | return |
| 555 | end |
| 556 | if walk(self, dt) then |
| 557 | return |
| 558 | end |
| 559 | self.think -= dt |
| 560 | if self.think > 0 then |
| 561 | return |
| 562 | end |
| 563 | self.think = 1 |
| 564 | -- nobody near the village: home they go, until someone comes |
| 565 | if world.nearest_player(self.position, GONE) == nil then |
| 566 | out[self.id] = nil |
| 567 | self:remove() |
| 568 | return |
| 569 | end |
| 570 | local want = phase(self.kind, v.field) |
| 571 | if want ~= self.going then |
| 572 | self.route = route(v, i, self.at_place or "porch", want) |
| 573 | self.going = want |
| 574 | self.best = nil |
| 575 | self.stuck = 0 |
| 576 | self.walk_in = 0 |
| 577 | return |
| 578 | end |
| 579 | -- there: at work, look at it and now and then say something of it |
| 580 | local p = places(v, i) |
| 581 | if want == "work" then |
| 582 | self:face(p.station) |
| 583 | self.chat -= 1 |
| 584 | if self.chat <= 0 then |
| 585 | self.chat = world.random(25, 50) |
| 586 | local def = JOBS[self.kind] |
| 587 | if def then |
| 588 | self:say(def.work[math.floor(world.random(1, #def.work + 0.999))]) |
| 589 | end |
| 590 | end |
| 591 | elseif want == "well" then |
| 592 | self:face(villages.spot(v, 0, 1, 0) + vector.create(0.5, 0, 0.5)) |
| 593 | elseif want == "field" then |
| 594 | -- along the rows, a few steps at a time |
| 595 | self.chat -= 1 |
| 596 | if self.chat <= 0 then |
| 597 | self.chat = world.random(8, 16) |
| 598 | local f = villages.FIELD |
| 599 | local side = if i % 2 == 0 then 1 else -1 |
| 600 | local to = villages.spot(v, f[1] + math.floor(world.random(1, 6.999)), 1, side) + vector.create(0.5, 0.05, 0.5) |
| 601 | self.route = { { to.x, to.y, to.z } } |
| 602 | end |
| 603 | end |
| 604 | end |
| 605 | |
| 606 | local function used(self, player) |
| 607 | if self.village == nil then |
| 608 | return |
| 609 | end |
| 610 | self:face(player.position) |
| 611 | sound.play("grumble", { at = self, pitch = 1.6, volume = 0.5, caption = (self.person or "A villager") .. " hums" }) |
| 612 | offer(self, player) |
| 613 | end |
| 614 | |
| 615 | local function hit(self, player) |
| 616 | self:hop(0.4) |
| 617 | self:say("Oi!") |
| 618 | -- that doesn't make friends |
| 619 | if self.village and player then |
| 620 | local key = key_of(self) |
| 621 | trust[key] = math.max(0, (trust[key] or 0) - 2) |
| 622 | save() |
| 623 | end |
| 624 | end |
| 625 | |
| 626 | for _, job in ORDER do |
| 627 | local kind = KINDS[job] |
| 628 | kind.spawned = spawned |
| 629 | kind.tick = tick |
| 630 | kind.used = used |
| 631 | kind.hit = hit |
| 632 | end |
| 633 | |
| 634 | -- ---- sending folk out ---- |
| 635 | |
| 636 | world.every(2, function() |
| 637 | local alive: { [number]: boolean } = {} |
| 638 | local live: { [string]: boolean } = {} |
| 639 | local count = 0 |
| 640 | for _, kind in ALL_KINDS do |
| 641 | for _, id in world.creatures(kind) do |
| 642 | alive[id] = true |
| 643 | count += 1 |
| 644 | local k = out[id] |
| 645 | if k then |
| 646 | live[k] = true |
| 647 | end |
| 648 | end |
| 649 | end |
| 650 | for id in out do |
| 651 | if not alive[id] then |
| 652 | out[id] = nil |
| 653 | end |
| 654 | end |
| 655 | -- those sent out a moment ago count as out |
| 656 | local now = world.time() |
| 657 | for i = #arriving, 1, -1 do |
| 658 | local a = arriving[i] |
| 659 | if now - a.when > 6 then |
| 660 | table.remove(arriving, i) |
| 661 | else |
| 662 | live[a.village .. "#" .. a.house] = true |
| 663 | end |
| 664 | end |
| 665 | -- the Green's two, always |
| 666 | local green = world.spot("green") |
| 667 | if green then |
| 668 | for i, f in GREEN_FOLK do |
| 669 | if not live["green#" .. i] then |
| 670 | send_out(KINDS[f.job], green + vector.create(i * 2 - 3, 0, 1), "green", i, f.name, "green") |
| 671 | end |
| 672 | end |
| 673 | end |
| 674 | -- each built village somebody's near |
| 675 | for _, v in list do |
| 676 | if v.built and count < MOST_FOLK then |
| 677 | local near = false |
| 678 | for _, p in world.players() do |
| 679 | if flat(p.position, v.at) < NEAR then |
| 680 | near = true |
| 681 | end |
| 682 | end |
| 683 | if near then |
| 684 | for i, hs in v.houses do |
| 685 | if not live[v.id .. "#" .. i] and count < MOST_FOLK then |
| 686 | local where = phase(hs.job, v.field) |
| 687 | local start = if where == "inside" then "inside" elseif where == "work" then "work" else "porch" |
| 688 | local at = places(v, i)[start] |
| 689 | if send_out(KINDS[hs.job], at, v.id, i, hs.name, start) then |
| 690 | count += 1 |
| 691 | end |
| 692 | end |
| 693 | end |
| 694 | end |
| 695 | end |
| 696 | end |
| 697 | end) |
| 698 | |
| 699 | -- ---- finding and building villages ---- |
| 700 | |
| 701 | local function next_id(): string |
| 702 | local n = 1 |
| 703 | while by_id["v" .. n] do |
| 704 | n += 1 |
| 705 | end |
| 706 | return "v" .. n |
| 707 | end |
| 708 | |
| 709 | local function far_from_villages(at: vector, apart: number): boolean |
| 710 | for _, v in list do |
| 711 | if flat(at, v.at) < apart then |
| 712 | return false |
| 713 | end |
| 714 | end |
| 715 | return true |
| 716 | end |
| 717 | |
| 718 | local function far_from_green(at: vector): boolean |
| 719 | for _, name in { "green", "start" } do |
| 720 | local s = world.spot(name) |
| 721 | if s and flat(at, s) < knobs.away then |
| 722 | return false |
| 723 | end |
| 724 | end |
| 725 | return true |
| 726 | end |
| 727 | |
| 728 | -- Whether a village could stand at x, z: lays it out and checks the ground. |
| 729 | local function try_at(x: number, z: number): Village? |
| 730 | x, z = math.floor(x), math.floor(z) |
| 731 | local along_x = villages.hash(x, z, 3) < 0.5 |
| 732 | local biome = world.biome(vector.create(x, 0, z)) |
| 733 | local style = villages.BY_BIOME[biome] |
| 734 | if style == nil then |
| 735 | return nil |
| 736 | end |
| 737 | local plan = villages.plan_village(next_id(), x, 0, z, along_x, style, true) |
| 738 | local y, real = villages.fits(x, z, along_x, plan.field) |
| 739 | if y == nil then |
| 740 | -- the other way round, or without its field |
| 741 | plan = villages.plan_village(plan.id, x, 0, z, not along_x, style, true) |
| 742 | y, real = villages.fits(x, z, not along_x, plan.field) |
| 743 | if y == nil and plan.field then |
| 744 | plan.field = false |
| 745 | y, real = villages.fits(x, z, not along_x, false) |
| 746 | end |
| 747 | end |
| 748 | if y == nil or real == nil then |
| 749 | return nil |
| 750 | end |
| 751 | if real ~= style then |
| 752 | local field = plan.field |
| 753 | plan = villages.plan_village(plan.id, x, 0, z, plan.along_x, real, true) |
| 754 | plan.field = plan.field and field |
| 755 | end |
| 756 | plan.at = vector.create(x, y, z) |
| 757 | return plan |
| 758 | end |
| 759 | |
| 760 | -- Somewhere for a village a long walk from `from`, judged from the seed |
| 761 | -- alone (the land may not be made yet), for the wayfinder to point to. It's |
| 762 | -- built when somebody gets there. |
| 763 | function plan_far(from: vector): Village? |
| 764 | if #list >= MOST then |
| 765 | return nil |
| 766 | end |
| 767 | for _ = 1, 16 do |
| 768 | local a = world.random() * math.pi * 2 |
| 769 | local d = world.random(knobs.apart * 0.8, knobs.apart * 1.4) |
| 770 | local x, z = math.floor(from.x + math.cos(a) * d), math.floor(from.z + math.sin(a) * d) |
| 771 | local at = vector.create(x, 0, z) |
| 772 | local style = villages.BY_BIOME[world.biome(at)] |
| 773 | if style and far_from_villages(at, knobs.apart * 0.7) and far_from_green(at) then |
| 774 | local lo, hi = math.huge, -math.huge |
| 775 | for _, o in { { 0, 0 }, { 16, 8 }, { -16, -8 }, { 16, -8 }, { -16, 8 } } do |
| 776 | local y = world.top(x + o[1], z + o[2]) |
| 777 | lo, hi = math.min(lo, y), math.max(hi, y) |
| 778 | end |
| 779 | if hi - lo <= 5 and lo > 20 then |
| 780 | local v = villages.plan_village(next_id(), x, lo, z, villages.hash(x, z, 3) < 0.5, style, true) |
| 781 | v.planned = true |
| 782 | table.insert(list, v) |
| 783 | index() |
| 784 | save() |
| 785 | return v |
| 786 | end |
| 787 | end |
| 788 | end |
| 789 | return nil |
| 790 | end |
| 791 | |
| 792 | -- what's left to do on the village being built |
| 793 | local work: { villages.Work } = {} |
| 794 | local done = 0 |
| 795 | local building: Village? = nil |
| 796 | local BUDGET = 260 |
| 797 | |
| 798 | local function start_building(v: Village) |
| 799 | building = v |
| 800 | work = villages.plan_build(v) |
| 801 | done = 0 |
| 802 | end |
| 803 | |
| 804 | local function finish_building() |
| 805 | local v = building |
| 806 | work, done, building = {}, 0, nil |
| 807 | if v then |
| 808 | v.built = true |
| 809 | v.planned = nil |
| 810 | save() |
| 811 | end |
| 812 | end |
| 813 | |
| 814 | local turn = 0 |
| 815 | world.every(0.1, function() |
| 816 | if #work > 0 then |
| 817 | local spent = 0 |
| 818 | while done < #work and spent < BUDGET do |
| 819 | done += 1 |
| 820 | spent += villages.step(work[done]) |
| 821 | end |
| 822 | if done >= #work then |
| 823 | finish_building() |
| 824 | end |
| 825 | return |
| 826 | end |
| 827 | if not knobs.villages then |
| 828 | return |
| 829 | end |
| 830 | -- one player a couple of times a second, to keep the looking light |
| 831 | turn += 1 |
| 832 | if turn % 20 ~= 0 then |
| 833 | return |
| 834 | end |
| 835 | local players = world.players() |
| 836 | if #players == 0 then |
| 837 | return |
| 838 | end |
| 839 | local p = players[(turn // 20) % #players + 1] |
| 840 | -- the Long Dusk has finds of its own |
| 841 | if world.place(p.position) == "dusk" then |
| 842 | return |
| 843 | end |
| 844 | -- a village the wayfinder told of, that they've come to |
| 845 | for idx, v in list do |
| 846 | if v.planned and flat(p.position, v.at) < 80 then |
| 847 | local here = world.block(vector.create(v.at.x, world.top(v.at.x, v.at.z), v.at.z)) |
| 848 | if here ~= "Air" then |
| 849 | local found: Village? = nil |
| 850 | for k = 0, 8 do |
| 851 | local a = k * math.pi / 4 |
| 852 | local r = if k == 0 then 0 else 24 |
| 853 | found = try_at(v.at.x + math.cos(a) * r, v.at.z + math.sin(a) * r) |
| 854 | if found then |
| 855 | break |
| 856 | end |
| 857 | end |
| 858 | if found then |
| 859 | -- keep its name and who lives there, at the place that fits |
| 860 | v.at = found.at |
| 861 | v.along_x = found.along_x |
| 862 | v.style = found.style |
| 863 | v.field = found.field and v.field |
| 864 | start_building(v) |
| 865 | else |
| 866 | table.remove(list, idx) |
| 867 | index() |
| 868 | save() |
| 869 | end |
| 870 | return |
| 871 | end |
| 872 | end |
| 873 | end |
| 874 | if #list >= MOST or not far_from_green(p.position) or not far_from_villages(p.position, knobs.apart) then |
| 875 | return |
| 876 | end |
| 877 | local from = world.random() * math.pi * 2 |
| 878 | for k = 1, 8 do |
| 879 | local a = from + k * math.pi / 4 |
| 880 | local d = world.random(36, 64) |
| 881 | local v = try_at(p.position.x + math.cos(a) * d, p.position.z + math.sin(a) * d) |
| 882 | if v and far_from_villages(v.at, knobs.apart) and far_from_green(v.at) then |
| 883 | table.insert(list, v) |
| 884 | index() |
| 885 | save() |
| 886 | start_building(v) |
| 887 | return |
| 888 | end |
| 889 | end |
| 890 | end) |
| 891 | |
| 892 | -- a village left half built (the world stopped mid-way) is built on |
| 893 | for _, v in list do |
| 894 | if not v.built and not v.planned then |
| 895 | start_building(v) |
| 896 | break |
| 897 | end |
| 898 | end |
| 899 | |
| 900 | -- ---- welcomes ---- |
| 901 | |
| 902 | local welcomed: { [number]: string } = {} |
| 903 | world.every(1, function() |
| 904 | for _, p in world.players() do |
| 905 | local at: string? = nil |
| 906 | for _, v in list do |
| 907 | if v.built and flat(p.position, v.at) < 24 then |
| 908 | at = v.id |
| 909 | if welcomed[p.id] ~= v.id then |
| 910 | tell(p, "Welcome to " .. v.name, 6) |
| 911 | end |
| 912 | end |
| 913 | end |
| 914 | welcomed[p.id] = at or welcomed[p.id] |
| 915 | if at == nil and welcomed[p.id] then |
| 916 | -- far enough off that coming back says it again |
| 917 | local v = by_id[welcomed[p.id]] |
| 918 | if v == nil or flat(p.position, v.at) > 60 then |
| 919 | welcomed[p.id] = nil |
| 920 | end |
| 921 | end |
| 922 | end |
| 923 | end) |
| 924 | |
| 925 | world.on("player_left", function(p) |
| 926 | welcomed[p.id] = nil |
| 927 | shown[p.id] = nil |
| 928 | end) |
| 929 | |
| 930 | -- ---- fields grow back ---- |
| 931 | |
| 932 | -- crops picked from a village's field, and when they're back |
| 933 | type Regrow = { at: vector, name: string, when: number } |
| 934 | local regrow: { [string]: Regrow } = {} |
| 935 | |
| 936 | local function key(at: vector): string |
| 937 | return string.format("%d,%d,%d", math.floor(at.x), math.floor(at.y), math.floor(at.z)) |
| 938 | end |
| 939 | |
| 940 | local crops_of: { [string]: { [string]: string } } = {} |
| 941 | local function crops(v: Village): { [string]: string } |
| 942 | local c = crops_of[v.id] |
| 943 | if c == nil then |
| 944 | c = {} |
| 945 | for _, e in villages.crops(v) do |
| 946 | c[key(e[1] :: vector)] = e[2] :: string |
| 947 | end |
| 948 | crops_of[v.id] = c |
| 949 | end |
| 950 | return c |
| 951 | end |
| 952 | |
| 953 | world.on("block_broken", function(_player, at: vector, block: string) |
| 954 | if block ~= "Wheat" and block ~= "Carrots" then |
| 955 | return |
| 956 | end |
| 957 | for _, v in list do |
| 958 | if v.field and v.built and flat(at, v.at) < 40 then |
| 959 | local name = crops(v)[key(at)] |
| 960 | if name then |
| 961 | regrow[key(at)] = { at = at, name = name, when = world.time() + 240 } |
| 962 | end |
| 963 | end |
| 964 | end |
| 965 | end) |
| 966 | |
| 967 | world.every(10, function() |
| 968 | local now = world.time() |
| 969 | for k, r in regrow do |
| 970 | if now >= r.when then |
| 971 | regrow[k] = nil |
| 972 | local below = r.at - vector.create(0, 1, 0) |
| 973 | if world.block(r.at) == "Air" and world.block(below) == "Farmland" then |
| 974 | world.set_block(r.at, r.name) |
| 975 | end |
| 976 | end |
| 977 | end |
| 978 | end) |
| 979 | |
| 980 | -- ---- the peddler ---- |
| 981 | |
| 982 | type Visit = { id: number, ends: number, offers: { Trade }, sold: { number }, buys: { Trade }, bought: { number }, leaving: boolean, at: vector, to: number? } |
| 983 | local visit: Visit? = nil |
| 984 | local visits = 0 |
| 985 | -- the first visit's a few minutes after the world starts |
| 986 | local since = math.max(0, knobs.peddler * 60 - 300) |
| 987 | local PEDDLER_NAME = "Marrowby" |
| 988 | |
| 989 | local function pick_offers(): ({ Trade }, { Trade }) |
| 990 | local pool = {} |
| 991 | for _, t in trades.PEDDLER do |
| 992 | if usable(t) then |
| 993 | table.insert(pool, t) |
| 994 | end |
| 995 | end |
| 996 | local offers = {} |
| 997 | for _ = 1, math.min(6, #pool) do |
| 998 | local i = math.floor(world.random(1, #pool + 0.999)) |
| 999 | table.insert(offers, table.remove(pool, i) :: Trade) |
| 1000 | end |
| 1001 | local buys = {} |
| 1002 | local bpool = {} |
| 1003 | for _, t in trades.PEDDLER_BUYS do |
| 1004 | if usable(t) then |
| 1005 | table.insert(bpool, t) |
| 1006 | end |
| 1007 | end |
| 1008 | if #bpool > 0 then |
| 1009 | table.insert(buys, bpool[math.floor(world.random(1, #bpool + 0.999))]) |
| 1010 | end |
| 1011 | return offers, buys |
| 1012 | end |
| 1013 | |
| 1014 | -- ground a peddler can stand on |
| 1015 | local FIRM: { [string]: boolean } = { Grass = true, Dirt = true, Sand = true, Snow = true, Stone = true, Gravel = true, Sandstone = true, Cobblestone = true, Planks = true, Slate = true } |
| 1016 | |
| 1017 | -- somewhere on dry ground a little way from a player |
| 1018 | local function ground_near(p: vector): vector? |
| 1019 | for _ = 1, 8 do |
| 1020 | local a = world.random() * math.pi * 2 |
| 1021 | local d = world.random(14, 22) |
| 1022 | local x, z = math.floor(p.x + math.cos(a) * d), math.floor(p.z + math.sin(a) * d) |
| 1023 | local y = world.top(x, z) |
| 1024 | local b = world.block(vector.create(x, y, z)) |
| 1025 | if FIRM[b] and world.block(vector.create(x, y + 1, z)) == "Air" and world.block(vector.create(x, y + 2, z)) == "Air" then |
| 1026 | return vector.create(x + 0.5, y + 1.05, z + 0.5) |
| 1027 | end |
| 1028 | end |
| 1029 | return nil |
| 1030 | end |
| 1031 | |
| 1032 | local function arrive(p): boolean |
| 1033 | local at = ground_near(p.position) |
| 1034 | if at == nil then |
| 1035 | return false |
| 1036 | end |
| 1037 | visits += 1 |
| 1038 | local offers, buys = pick_offers() |
| 1039 | visit = { id = visits, ends = world.time() + 480, offers = offers, sold = {}, buys = buys, bought = {}, leaving = false, at = at, to = p.id } |
| 1040 | local ok = pcall(world.spawn, Peddler, at) |
| 1041 | if not ok then |
| 1042 | visit = nil |
| 1043 | return false |
| 1044 | end |
| 1045 | pcall(world.spawn, Yak, at + vector.create(1.5, 0, 0)) |
| 1046 | for _, q in world.players() do |
| 1047 | if flat(q.position, at) < 80 then |
| 1048 | tell(q, "A peddler's come by, " .. way_name(at.x - q.position.x, at.z - q.position.z) .. " of you", 8) |
| 1049 | end |
| 1050 | end |
| 1051 | sound.play("chime", { at = at, pitch = 0.8, caption = "A peddler arrives" }) |
| 1052 | return true |
| 1053 | end |
| 1054 | |
| 1055 | world.every(10, function() |
| 1056 | local v = visit |
| 1057 | if v then |
| 1058 | local now = world.time() |
| 1059 | local anyone = world.nearest_player(v.at, 140) ~= nil |
| 1060 | if (now > v.ends or not anyone) and not v.leaving then |
| 1061 | v.leaving = true |
| 1062 | world.after(4, function() |
| 1063 | if visit == v then |
| 1064 | visit = nil |
| 1065 | end |
| 1066 | end) |
| 1067 | end |
| 1068 | return |
| 1069 | end |
| 1070 | since += 10 |
| 1071 | if since < knobs.peddler * 60 or world.random() > 0.2 then |
| 1072 | return |
| 1073 | end |
| 1074 | local players = world.players() |
| 1075 | if #players == 0 then |
| 1076 | return |
| 1077 | end |
| 1078 | local p = players[math.floor(world.random(1, #players + 0.999))] |
| 1079 | -- the Long Dusk has finds of its own |
| 1080 | if world.place(p.position) == "dusk" then |
| 1081 | return |
| 1082 | end |
| 1083 | if arrive(p) then |
| 1084 | since = 0 |
| 1085 | end |
| 1086 | end) |
| 1087 | |
| 1088 | function Peddler:spawned() |
| 1089 | self.visit = if visit then visit.id else -1 |
| 1090 | self.chat = 2 |
| 1091 | end |
| 1092 | |
| 1093 | function Peddler:tick(dt: number) |
| 1094 | local v = visit |
| 1095 | if v == nil or v.id ~= self.visit then |
| 1096 | self:remove() |
| 1097 | return |
| 1098 | end |
| 1099 | v.at = self.position |
| 1100 | if v.leaving then |
| 1101 | if not self.bye then |
| 1102 | self.bye = true |
| 1103 | self:stop() |
| 1104 | self:say("Time I was off. Safe roads!") |
| 1105 | sound.play("whoosh", { at = self, volume = 0.4, caption = "The peddler packs up" }) |
| 1106 | end |
| 1107 | return |
| 1108 | end |
| 1109 | local p = world.nearest_player(self.position, 4) |
| 1110 | if p then |
| 1111 | self:stop() |
| 1112 | self:face(p.position) |
| 1113 | self.chat -= dt |
| 1114 | if self.chat <= 0 then |
| 1115 | self.chat = world.random(20, 40) |
| 1116 | local lines = { "Fine things from far off!", "Snow from the north, sand from the south.", "Beads, friend? I take beads." } |
| 1117 | self:say(lines[math.floor(world.random(1, 3.999))]) |
| 1118 | end |
| 1119 | return |
| 1120 | end |
| 1121 | -- first to whoever he came to see, then about where he stands |
| 1122 | local who: any = nil |
| 1123 | if v.to then |
| 1124 | for _, q in world.players() do |
| 1125 | if q.id == v.to then |
| 1126 | who = q |
| 1127 | end |
| 1128 | end |
| 1129 | end |
| 1130 | if who and self:distance_to(who.position) > 4 and self:distance_to(who.position) < 40 then |
| 1131 | self.walk_in = (self.walk_in or 0) - dt |
| 1132 | if self.walk_in <= 0 then |
| 1133 | self.walk_in = 1 |
| 1134 | self:walk_to(who.position, 1.5) |
| 1135 | end |
| 1136 | else |
| 1137 | v.to = nil |
| 1138 | self:wander(1) |
| 1139 | end |
| 1140 | end |
| 1141 | |
| 1142 | local function peddler_offer(self, player, said: string?) |
| 1143 | local v = visit |
| 1144 | if v == nil or v.id ~= self.visit then |
| 1145 | return |
| 1146 | end |
| 1147 | local title = PEDDLER_NAME .. " the peddler" |
| 1148 | if world.pockets(player) == nil then |
| 1149 | ui.ask(player, { title = title, text = "Just looking? In creative you've everything already!", choices = { "Just looking" } }, function() end) |
| 1150 | return |
| 1151 | end |
| 1152 | local all: { Trade } = {} |
| 1153 | for _, t in v.offers do |
| 1154 | table.insert(all, t) |
| 1155 | end |
| 1156 | for _, t in v.buys do |
| 1157 | table.insert(all, t) |
| 1158 | end |
| 1159 | local choices, prices = {}, {} |
| 1160 | for i, t in all do |
| 1161 | local n = (if i <= #v.offers then v.sold[i] else v.bought[i - #v.offers]) or 0 |
| 1162 | local short, long = label(t, n >= stock_of(t)) |
| 1163 | table.insert(choices, short) |
| 1164 | if long then |
| 1165 | table.insert(prices, long) |
| 1166 | end |
| 1167 | end |
| 1168 | local text = (said or "Things from far off! Have a look.") .. "\nI'm off again soon." |
| 1169 | if #prices > 0 then |
| 1170 | text ..= "\n" .. table.concat(prices, "\n") |
| 1171 | end |
| 1172 | ui.ask(player, { title = title, text = string.sub(text, 1, 300), choices = choices }, function(who, choice) |
| 1173 | local t = choice and all[choice] |
| 1174 | if t == nil or visit ~= v then |
| 1175 | return |
| 1176 | end |
| 1177 | local counts = if choice <= #v.offers then v.sold else v.bought |
| 1178 | local i = if choice <= #v.offers then choice else choice - #v.offers |
| 1179 | local result: string |
| 1180 | if (counts[i] or 0) >= stock_of(t) then |
| 1181 | result = "Sold out of that, I'm afraid." |
| 1182 | else |
| 1183 | local short = nil |
| 1184 | for thing, n in t.want do |
| 1185 | if world.has(who, thing) < n then |
| 1186 | short = "You'll need " .. amount(n, thing) .. " for that." |
| 1187 | end |
| 1188 | end |
| 1189 | if short then |
| 1190 | result = short |
| 1191 | else |
| 1192 | for thing, n in t.want do |
| 1193 | world.take(who, thing, n) |
| 1194 | end |
| 1195 | local given = world.give(who, t.give, makes(t)) |
| 1196 | if given == 0 then |
| 1197 | for thing, n in t.want do |
| 1198 | world.give(who, thing, n) |
| 1199 | end |
| 1200 | result = "Your pockets are full, friend." |
| 1201 | else |
| 1202 | counts[i] = (counts[i] or 0) + 1 |
| 1203 | world.reward(who, 2) |
| 1204 | sound.play("pop", { at = self }) |
| 1205 | result = "A fine choice: " .. amount(given, t.give) .. "." |
| 1206 | end |
| 1207 | end |
| 1208 | end |
| 1209 | peddler_offer(self, who, result) |
| 1210 | end) |
| 1211 | end |
| 1212 | |
| 1213 | function Peddler:used(player) |
| 1214 | self:face(player.position) |
| 1215 | peddler_offer(self, player) |
| 1216 | end |
| 1217 | |
| 1218 | function Peddler:hit() |
| 1219 | self:hop(0.4) |
| 1220 | self:say("Mind the goods!") |
| 1221 | end |
| 1222 | |
| 1223 | function Yak:spawned() |
| 1224 | self.visit = if visit then visit.id else -1 |
| 1225 | end |
| 1226 | |
| 1227 | function Yak:tick(dt: number) |
| 1228 | local v = visit |
| 1229 | if v == nil or v.id ~= self.visit then |
| 1230 | self:remove() |
| 1231 | return |
| 1232 | end |
| 1233 | self.walk_in = (self.walk_in or 0) - dt |
| 1234 | if self.walk_in > 0 then |
| 1235 | return |
| 1236 | end |
| 1237 | self.walk_in = 1 |
| 1238 | if self:distance_to(v.at) > 3 then |
| 1239 | self:walk_to(v.at, 1.7) |
| 1240 | else |
| 1241 | self:stop() |
| 1242 | end |
| 1243 | end |
| 1244 | |
| 1245 | function Yak:used(player) |
| 1246 | self:face(player.position) |
| 1247 | sound.play("grumble", { at = self, pitch = 0.6, caption = "The yak grumbles" }) |
| 1248 | self:say("The yak chews, and looks at you.") |
| 1249 | end |
| 1250 | |
| 1251 | function Yak:hit() |
| 1252 | self:hop(0.3) |
| 1253 | sound.play("grumble", { at = self, pitch = 0.5, volume = 0.8, caption = "The yak snorts" }) |
| 1254 | end |
| 1255 | |
| 1256 | -- ---- the item guide, and a check on the prices ---- |
| 1257 | |
| 1258 | local function rows(list_of: { Trade }, note: string): { any } |
| 1259 | local out_rows = {} |
| 1260 | for _, t in list_of do |
| 1261 | if not t.does and usable(t) then |
| 1262 | table.insert(out_rows, { give = t.give, n = t.n, want = t.want, note = note }) |
| 1263 | end |
| 1264 | end |
| 1265 | return out_rows |
| 1266 | end |
| 1267 | |
| 1268 | local book = {} |
| 1269 | for _, job in ORDER do |
| 1270 | local def = JOBS[job] |
| 1271 | for _, t in def.trades do |
| 1272 | if not t.does and usable(t) then |
| 1273 | local who = string.upper(string.sub(def.title, 5, 5)) .. string.sub(def.title, 6) |
| 1274 | local note = if t.tier == 1 then who else who .. ", for " .. FOR_TIER[t.tier] |
| 1275 | if job == "Carpenter" or job == "Mason" then |
| 1276 | note ..= " (and on the Green)" |
| 1277 | end |
| 1278 | table.insert(book, { give = t.give, n = t.n, want = t.want, note = note }) |
| 1279 | end |
| 1280 | end |
| 1281 | end |
| 1282 | world.recipes("Villagers", book) |
| 1283 | local peddler_book = rows(trades.PEDDLER, "When the peddler has it") |
| 1284 | for _, r in rows(trades.PEDDLER_BUYS, "When the peddler wants it") do |
| 1285 | table.insert(peddler_book, r) |
| 1286 | end |
| 1287 | world.recipes("Peddler", peddler_book) |
| 1288 | |
| 1289 | -- Nothing may be bought for fewer beads than it sells for. |
| 1290 | do |
| 1291 | local sells: { [string]: number } = {} |
| 1292 | local buys: { [string]: number } = {} |
| 1293 | local function look(t: Trade) |
| 1294 | local kinds = 0 |
| 1295 | for _ in t.want do |
| 1296 | kinds += 1 |
| 1297 | end |
| 1298 | local only_beads = t.want[BEADS] ~= nil and kinds == 1 |
| 1299 | if t.give == BEADS then |
| 1300 | for thing, n in t.want do |
| 1301 | sells[thing] = math.max(sells[thing] or 0, t.n / n) |
| 1302 | end |
| 1303 | elseif only_beads and t.n > 0 then |
| 1304 | buys[t.give] = math.min(buys[t.give] or math.huge, t.want[BEADS] / t.n) |
| 1305 | end |
| 1306 | end |
| 1307 | for _, job in ORDER do |
| 1308 | for _, t in JOBS[job].trades do |
| 1309 | look(t) |
| 1310 | end |
| 1311 | end |
| 1312 | for _, t in trades.PEDDLER do |
| 1313 | look(t) |
| 1314 | end |
| 1315 | for _, t in trades.PEDDLER_BUYS do |
| 1316 | look(t) |
| 1317 | end |
| 1318 | local loops = {} |
| 1319 | for thing, worth in sells do |
| 1320 | if buys[thing] and buys[thing] <= worth then |
| 1321 | table.insert(loops, thing) |
| 1322 | end |
| 1323 | end |
| 1324 | if #loops > 0 then |
| 1325 | table.sort(loops) |
| 1326 | print("Beads grow on trees: these sell for as much as they cost: " .. table.concat(loops, ", ")) |
| 1327 | store.set("loops", loops) |
| 1328 | elseif store.get("loops") ~= nil then |
| 1329 | store.set("loops", nil) |
| 1330 | end |
| 1331 | end |