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 | -- The Villagers' own tests: `potluck-server --test=villagers`. |
| 3 | |
| 4 | local test = require("@potluck/test@0.8") |
| 5 | local world = require("@potluck/world@0.32") |
| 6 | local store = require("@potluck/store") |
| 7 | local villages = require("./villages") |
| 8 | |
| 9 | -- the first creature of a kind that's out, as its table |
| 10 | local function first(kind: string): any |
| 11 | local ids = world.creatures(test.kind(kind)) |
| 12 | if #ids == 0 then |
| 13 | return nil |
| 14 | end |
| 15 | return test.creature(ids[1]) |
| 16 | end |
| 17 | |
| 18 | local function choice(p, starts: string): number? |
| 19 | local q = test.asked(p) |
| 20 | for i, c in (q and q.choices) or {} do |
| 21 | if string.sub(c, 1, #starts) == starts then |
| 22 | return i |
| 23 | end |
| 24 | end |
| 25 | return nil |
| 26 | end |
| 27 | |
| 28 | test.case("Rowan and Ivy live on the Green and trade for beads", function() |
| 29 | local p = test.player("Ada", { survival = true, carrying = { ["Oak log"] = 20 } }) |
| 30 | test.wait(3) |
| 31 | local rowan = first("Carpenter") |
| 32 | test.check(rowan ~= nil, "Rowan's on the Green") |
| 33 | test.same(rowan.person, "Rowan") |
| 34 | test.check(first("Mason") ~= nil, "and Ivy") |
| 35 | test.move(p, rowan.position + vector.create(1.5, 0, 0)) |
| 36 | test.use(p, rowan) |
| 37 | local q = test.asked(p) |
| 38 | test.check(q ~= nil and q.title == "Rowan the carpenter", "Rowan asks what she can make") |
| 39 | local planks = choice(p, "4 Planks for 1 Oak log") |
| 40 | test.check(planks ~= nil, "planks for a log") |
| 41 | test.answer(p, planks :: number) |
| 42 | test.wait(0.3) |
| 43 | test.same(world.has(p, "Planks"), 4, "planks") |
| 44 | test.same(world.has(p, "Oak log"), 19, "a log fewer") |
| 45 | local sell = choice(p, "1 bead for 8 Oak log") |
| 46 | test.check(sell ~= nil, "beads for logs: " .. table.concat((test.asked(p) or { choices = {} }).choices, " / ")) |
| 47 | test.answer(p, sell :: number) |
| 48 | test.wait(0.3) |
| 49 | test.same(world.has(p, "Beads"), 1, "a bead") |
| 50 | test.same(world.has(p, "Oak log"), 11, "eight logs fewer") |
| 51 | end) |
| 52 | |
| 53 | test.case("the more you trade, the more a villager shows you", function() |
| 54 | local p = test.player("Bo", { survival = true, carrying = { ["Oak log"] = 40 } }) |
| 55 | test.wait(3) |
| 56 | local rowan = first("Carpenter") |
| 57 | test.move(p, rowan.position + vector.create(1.5, 0, 0)) |
| 58 | test.use(p, rowan) |
| 59 | local before = #((test.asked(p) or { choices = {} }).choices) |
| 60 | for _ = 1, 6 do |
| 61 | local i = choice(p, "4 Planks for 1 Oak log") |
| 62 | test.answer(p, i :: number) |
| 63 | test.wait(0.3) |
| 64 | end |
| 65 | local q = test.asked(p) |
| 66 | test.check(q ~= nil and string.find(q.text or "", "regular", 1, true) ~= nil, "a regular now: " .. tostring(q and q.text)) |
| 67 | test.check(#q.choices > before, "more trades: " .. #q.choices .. " after " .. before) |
| 68 | test.check(choice(p, "1 bead for 8 Birch log") ~= nil, "birch logs too") |
| 69 | end) |
| 70 | |
| 71 | -- Takes a player out past the island to level land and waits for a village |
| 72 | -- to grow up near them. |
| 73 | local function find_village(p): any |
| 74 | for x = -700, -900, -40 do |
| 75 | for z = 400, 600, 40 do |
| 76 | test.wait(0.05) |
| 77 | local lo, hi = math.huge, -math.huge |
| 78 | for dx = -60, 60, 30 do |
| 79 | for dz = -60, 60, 30 do |
| 80 | local y = world.top(x + dx, z + dz) |
| 81 | lo, hi = math.min(lo, y), math.max(hi, y) |
| 82 | end |
| 83 | end |
| 84 | if lo > 20 and hi - lo <= 5 then |
| 85 | test.move(p, vector.create(x, world.top(x, z) + 1, z)) |
| 86 | for _ = 1, 20 do |
| 87 | test.wait(1) |
| 88 | local list = store.get("villages") :: { any }? |
| 89 | if list and #list > 0 then |
| 90 | for _ = 1, 30 do |
| 91 | if (store.get("villages") :: { any })[1].built then |
| 92 | break |
| 93 | end |
| 94 | test.wait(1) |
| 95 | end |
| 96 | return (store.get("villages") :: { any })[1] |
| 97 | end |
| 98 | end |
| 99 | end |
| 100 | end |
| 101 | end |
| 102 | return nil |
| 103 | end |
| 104 | |
| 105 | test.case("villages grow up far from the Green, and their folk live and work there", function() |
| 106 | local p = test.player("Cy", { survival = true }) |
| 107 | test.daytime(0.45) |
| 108 | test.wait(3) |
| 109 | test.same(#(store.get("villages") or {}), 0, "none by the Green") |
| 110 | local v = find_village(p) |
| 111 | test.check(v ~= nil, "a village grew up out past the island") |
| 112 | test.check(v.built, "and it's built") |
| 113 | test.check(#v.houses >= 4 and #v.houses <= 7, #v.houses .. " houses") |
| 114 | test.check(string.len(v.name) > 3, "it has a name: " .. v.name) |
| 115 | -- the well in the middle, with water in it |
| 116 | test.same(world.block(villages.spot(v, 0, 0, 0)), "Water", "the well") |
| 117 | test.same(world.block(villages.spot(v, 1, 0, 1)), villages.STYLES[v.style].well, "its stones") |
| 118 | test.same(world.block(villages.spot(v, 1, 1, 1)), villages.STYLES[v.style].rim, "the wall round its top") |
| 119 | -- each house has a door, a bed and a light inside |
| 120 | local st = villages.STYLES[v.style] |
| 121 | for i, hs in v.houses do |
| 122 | local at = villages.places(v, i) |
| 123 | test.same(world.block(at.door), st.door, hs.job .. "'s door") |
| 124 | test.same(world.block(at.door + vector.create(0, 1, 0)), st.door, hs.job .. "'s door is two high") |
| 125 | test.check(world.is_open(at.door) == false, hs.job .. "'s door is shut") |
| 126 | local bed = world.block(villages.house_spot(v, hs, 1, villages.floor_h(v) + 1, 0)) |
| 127 | test.check(string.find(bed, " bed$") ~= nil, hs.job .. "'s bed: " .. bed) |
| 128 | test.same(world.block(at.inside + vector.create(0, 2, 0)), "Lantern", hs.job .. "'s lantern") |
| 129 | end |
| 130 | -- the folk come out while someone's here, and are at work by day |
| 131 | test.move(p, villages.spot(v, 0, 1, 2) + vector.create(0.5, 0, 0.5)) |
| 132 | test.wait(14) |
| 133 | local out = 0 |
| 134 | for _, hs in v.houses do |
| 135 | out += #world.creatures(test.kind(hs.job)) |
| 136 | end |
| 137 | test.check(out >= #v.houses, out .. " of " .. #v.houses .. " villagers out") |
| 138 | local i = 1 |
| 139 | local hs = v.houses[i] |
| 140 | local who: any, who_id = nil, 0 |
| 141 | for _, id in world.creatures(test.kind(hs.job)) do |
| 142 | local c = test.creature(id) |
| 143 | if c and c.village == v.id and c.house == i then |
| 144 | who, who_id = c, id |
| 145 | end |
| 146 | end |
| 147 | test.check(who ~= nil, "the first house's villager") |
| 148 | test.same(who.person, hs.name) |
| 149 | local spot = villages.places(v, i) |
| 150 | local where = if hs.job == "Grower" and v.field then spot.gate else spot.work |
| 151 | test.check(vector.magnitude(who.position - where) < 12, "at work by day: " .. math.floor(vector.magnitude(who.position - spot.work)) .. " from work") |
| 152 | -- and indoors at night |
| 153 | test.daytime(0.9) |
| 154 | test.wait(25) |
| 155 | who = test.creature(who_id) |
| 156 | local off = who.position - spot.inside |
| 157 | local d = math.sqrt(off.x * off.x + off.z * off.z) |
| 158 | test.check(d < 1.5, "home at night: " .. string.format("%.1f", d) .. " from their bed") |
| 159 | -- trading there |
| 160 | test.move(p, who.position + vector.create(0.8, 0, 0)) |
| 161 | test.use(p, who) |
| 162 | local q = test.asked(p) |
| 163 | test.check(q ~= nil and string.find(q.title or "", hs.name, 1, true) ~= nil, "they trade: " .. tostring(q and q.title)) |
| 164 | -- the folk go in when nobody's about |
| 165 | test.move(p, villages.spot(v, 0, 1, 0) + vector.create(200, 0, 0)) |
| 166 | test.wait(6) |
| 167 | test.same(#world.creatures(test.kind(hs.job)), 0, "gone in when nobody's near") |
| 168 | end) |
| 169 | |
| 170 | test.case("a villager sent out by hand, with no village, goes again", function() |
| 171 | local p = test.player("Di", { survival = true, carrying = { Beads = 4 } }) |
| 172 | test.wait(3) |
| 173 | -- one of Wayfinder's own, sent to the Green for the test |
| 174 | local id = world.spawn(test.kind("Wayfinder"), (world.spot("green") :: vector) + vector.create(4, 0, 4)) |
| 175 | test.check(id ~= nil, "a wayfinder") |
| 176 | -- one sent out by hand has no village, so it goes again |
| 177 | test.wait(1) |
| 178 | test.same(test.creature(id), nil, "a villager with no village goes home") |
| 179 | end) |
| 180 | |
| 181 | test.case("a peddler and his yak come by now and then, and trade", function() |
| 182 | local p = test.player("Ed", { survival = true, carrying = { Beads = 40 } }) |
| 183 | local came = false |
| 184 | for _ = 1, 55 do |
| 185 | test.wait(10) |
| 186 | if #world.creatures(test.kind("Peddler")) > 0 then |
| 187 | came = true |
| 188 | break |
| 189 | end |
| 190 | end |
| 191 | test.check(came, "the peddler came") |
| 192 | test.same(#world.creatures(test.kind("Pack yak")), 1, "with his yak") |
| 193 | test.check(string.find(test.shown(p, "villagers") or "", "peddler", 1, true) ~= nil, "and said so: " .. tostring(test.shown(p, "villagers"))) |
| 194 | local ped = test.creature(world.creatures(test.kind("Peddler"))[1]) |
| 195 | test.move(p, ped.position + vector.create(1, 0, 0)) |
| 196 | test.use(p, ped) |
| 197 | local q = test.asked(p) |
| 198 | test.check(q ~= nil and #q.choices >= 5, "his wares") |
| 199 | test.answer(p, 1) |
| 200 | test.wait(0.3) |
| 201 | test.check(world.has(p, "Beads") < 40, "he took some beads") |
| 202 | end) |
| 203 | |
| 204 | test.case("nothing sells for as much as it costs", function() |
| 205 | test.wait(0.5) |
| 206 | test.same(store.get("loops"), nil, "trades that make beads from nothing") |
| 207 | end) |