Out in the meadows, far from the Green, a line of fresh molehills marks a mole run under the grass. Dig down through one and follow the run to the den at its end, where the old moles keep a hoard in a nest of hay: things they've turned up: seeds, flint, glass, old coins and, once in a while, a lost ring for the Curator. The den's moles are friendly. Offer one some berries and it'll go and fetch you something.
| 1 | --!strict |
| 2 | -- Mole Runs' own tests: `potluck-server --test=mole-runs`. |
| 3 | |
| 4 | local test = require("@potluck/test@0.8") |
| 5 | local world = require("@potluck/world@0.26") |
| 6 | local store = require("@potluck/store") |
| 7 | |
| 8 | local function v(x: number, y: number, z: number): vector |
| 9 | return vector.create(x, y, z) |
| 10 | end |
| 11 | |
| 12 | -- an open meadow far from the Green, leveled so the moles have room: grass |
| 13 | -- over earth over stone, clear sky over it, with the player in the middle |
| 14 | -- (a row at a time, so no one moment takes too long) |
| 15 | local function meadow(p): vector |
| 16 | local g = world.spot("green") :: vector |
| 17 | local x0, z0 = math.floor(g.x) - 100, math.floor(g.z) - 40 |
| 18 | local h = math.max(world.top(x0, z0), 24) |
| 19 | for dx = -32, 32 do |
| 20 | for dz = -32, 32 do |
| 21 | local x, z = x0 + dx, z0 + dz |
| 22 | for y = h + 1, math.max(world.top(x, z), h + 1) do |
| 23 | world.set_block(v(x, y, z), "Air") |
| 24 | end |
| 25 | world.set_block(v(x, h, z), "Grass") |
| 26 | for y = h - 4, h - 1 do |
| 27 | world.set_block(v(x, y, z), "Dirt") |
| 28 | end |
| 29 | for y = h - 8, h - 5 do |
| 30 | world.set_block(v(x, y, z), "Stone") |
| 31 | end |
| 32 | end |
| 33 | test.wait(0.05) |
| 34 | end |
| 35 | local at = v(x0 + 0.5, h + 1, z0 + 0.5) |
| 36 | test.move(p, at) |
| 37 | return at |
| 38 | end |
| 39 | |
| 40 | local function wait_for_run(): any |
| 41 | for _ = 1, 40 do |
| 42 | test.wait(1) |
| 43 | local list = store.get("runs") :: { any }? |
| 44 | if list and list[1] and list[1].built then |
| 45 | return list[1] |
| 46 | end |
| 47 | end |
| 48 | return nil |
| 49 | end |
| 50 | |
| 51 | test.case("molehills turn up in a meadow near a player, over a run that leads to a den with a hoard", function() |
| 52 | local p = test.player("Ada") |
| 53 | meadow(p) |
| 54 | local r = wait_for_run() |
| 55 | test.check(r ~= nil, "a mole run") |
| 56 | test.same(#r.hills, 6, "six hills") |
| 57 | for _, h in r.hills :: { vector } do |
| 58 | test.same(world.block(h + v(0, 1, 0)), "Molehill", "a hill on the grass") |
| 59 | test.same(world.block(h), "Dirt", "loose earth under it") |
| 60 | test.same(world.block(h - v(0, 1, 0)), "Air", "then down") |
| 61 | test.same(world.block(h - v(0, 3, 0)), "Air", "to the run") |
| 62 | end |
| 63 | local den = r.at :: vector |
| 64 | test.same(world.block(den), "Hay", "the hoard's nest in the den") |
| 65 | test.same(world.block(den + v(1, 1, 0)), "Air", "a room around it") |
| 66 | test.same(world.block(den + v(2, 0, 0)), "Glowcap", "lit by glowcaps") |
| 67 | test.same(world.block(den - v(0, 1, 0)), "Dirt", "on an earth floor") |
| 68 | -- the run joins the hills to the den: walk it, under the first hill to the den |
| 69 | local first = (r.hills :: { vector })[1] - v(0, 3, 0) |
| 70 | local seen, todo, found = { [tostring(first)] = true }, { first }, false |
| 71 | while #todo > 0 do |
| 72 | local c = table.remove(todo) :: vector |
| 73 | if vector.magnitude(c - den) < 2.5 then |
| 74 | found = true |
| 75 | break |
| 76 | end |
| 77 | for _, d in { v(1, 0, 0), v(-1, 0, 0), v(0, 0, 1), v(0, 0, -1), v(0, 1, 0), v(0, -1, 0) } do |
| 78 | local q = c + d |
| 79 | local b = world.block(q) |
| 80 | if not seen[tostring(q)] and (b == "Air" or b == "Hay" or b == "Glowcap") and vector.magnitude(q - den) < 40 and q.y < den.y + 5 then |
| 81 | seen[tostring(q)] = true |
| 82 | table.insert(todo, q) |
| 83 | end |
| 84 | end |
| 85 | end |
| 86 | test.check(found, "the run goes from the first hill to the den") |
| 87 | -- and the hills tell whoever comes on them |
| 88 | test.move(p, (r.hills :: { vector })[3] + v(0.5, 2, 0.5)) |
| 89 | test.wait(1.5) |
| 90 | test.same(test.shown(p, "moles"), "Fresh molehills. There's a mole run under here.") |
| 91 | test.wait(5) |
| 92 | test.same(#(store.get("runs") :: { any }), 1, "only one") |
| 93 | end) |
| 94 | |
| 95 | test.case("the hoard gives its things once, and two moles live in the den", function() |
| 96 | local p = test.player("Ada", { survival = true }) |
| 97 | meadow(p) |
| 98 | local r = wait_for_run() |
| 99 | test.check(r ~= nil, "a mole run") |
| 100 | local den = r.at :: vector |
| 101 | test.move(p, den + v(1.5, 0, 1.5)) |
| 102 | test.wait(1.5) |
| 103 | test.same(#world.creatures(test.kind("Den mole")), 2, "two moles at home") |
| 104 | local before = 0 |
| 105 | for _, n in world.pockets(p) or {} do |
| 106 | before += n |
| 107 | end |
| 108 | test.use_block(p, den) |
| 109 | local after = 0 |
| 110 | for _, n in world.pockets(p) or {} do |
| 111 | after += n |
| 112 | end |
| 113 | test.check(after > before, "something in their pockets") |
| 114 | test.same(test.shown(p, "moles"), "You've found the moles' hoard.") |
| 115 | test.answer(p, 1) |
| 116 | test.use_block(p, den) |
| 117 | test.same(test.shown(p, "moles"), "Just old hay now.") |
| 118 | end) |
| 119 | |
| 120 | test.case("a den mole given a berry fetches something, then not again for a while", function() |
| 121 | local p = test.player("Ada", { survival = true, carrying = { Berries = 3 } }) |
| 122 | meadow(p) |
| 123 | local r = wait_for_run() |
| 124 | test.check(r ~= nil, "a mole run") |
| 125 | test.move(p, r.at + v(1.5, 0, 1.5)) |
| 126 | test.wait(1.5) |
| 127 | local mole = world.creatures(test.kind("Den mole"))[1] |
| 128 | test.check(mole ~= nil, "a mole") |
| 129 | test.hold(p, "Berries") |
| 130 | test.use(p, mole) |
| 131 | test.same(world.has(p, "Berries"), 2, "it takes a berry") |
| 132 | test.wait(3.5) |
| 133 | local text = test.shown(p, "moles") or "" |
| 134 | test.check(string.find(text, "The mole brings you", 1, true) == 1, text) |
| 135 | test.use(p, mole) |
| 136 | test.same(world.has(p, "Berries"), 2, "not hungry again yet") |
| 137 | test.same(test.shown(p, "moles"), "The mole's full of berries for now.") |
| 138 | end) |
| 139 | |
| 140 | test.case("the moles leave with nobody near", function() |
| 141 | local p = test.player("Ada") |
| 142 | local at = meadow(p) |
| 143 | local r = wait_for_run() |
| 144 | test.check(r ~= nil, "a mole run") |
| 145 | test.move(p, r.at + v(1.5, 0, 1.5)) |
| 146 | test.wait(1.5) |
| 147 | test.same(#world.creatures(test.kind("Den mole")), 2, "home") |
| 148 | test.move(p, at + v(90, 0, 0)) |
| 149 | test.wait(3) |
| 150 | test.same(#world.creatures(test.kind("Den mole")), 0, "gone") |
| 151 | end) |
| 152 | |
| 153 | test.case("the lost ring is a relic, with its story", function() |
| 154 | local it = world.item("Lost ring") :: any |
| 155 | test.check(it ~= nil and it.by == "mole-runs", "an item of the runs'") |
| 156 | test.check(type(it.relic) == "string" and #it.relic > 20, "its story") |
| 157 | end) |