Real fire: light it with a flint and steel or an ember, and it burns for a while and goes out. Beside wood, leaves, plants, hay or wool it spreads, slowly, and may burn them away. Water and rain put it out, lightning can start one, and standing in it hurts. Knobs turn spreading off.
| 1 | --!strict |
| 2 | -- Fire's own tests: `potluck-server --test=fire`. |
| 3 | |
| 4 | local test = require("@potluck/test@0.9") |
| 5 | local world = require("@potluck/world@0.34") |
| 6 | local sky = require("@potluck/sky@0.1") |
| 7 | |
| 8 | -- a spot on the ground a few blocks from a player |
| 9 | local function ground(p, dx: number): vector |
| 10 | local x, z = p.position.x + dx, p.position.z |
| 11 | return vector.create(x, world.top(x, z) + 1, z) |
| 12 | end |
| 13 | |
| 14 | local SIDES = { |
| 15 | vector.create(1, 0, 0), |
| 16 | vector.create(-1, 0, 0), |
| 17 | vector.create(0, 1, 0), |
| 18 | vector.create(0, 0, 1), |
| 19 | vector.create(0, 0, -1), |
| 20 | } |
| 21 | |
| 22 | -- how many fires there are around `at`, `r` blocks each way |
| 23 | local function fires_near(at: vector, r: number): number |
| 24 | local n = 0 |
| 25 | for x = -r, r do |
| 26 | for y = -1, r do |
| 27 | for z = -r, r do |
| 28 | if world.block(at + vector.create(x, y, z)) == "Fire" then |
| 29 | n += 1 |
| 30 | end |
| 31 | end |
| 32 | end |
| 33 | end |
| 34 | return n |
| 35 | end |
| 36 | |
| 37 | test.case("the fire block is in the world", function() |
| 38 | test.same(world.kind("Fire"), "plant") |
| 39 | local p = test.player("Bo") |
| 40 | local at = ground(p, 3) |
| 41 | world.set_block(at, "Fire") |
| 42 | test.same(world.block(at), "Fire") |
| 43 | end) |
| 44 | |
| 45 | test.case("a flint and steel lights a fire beside the block it's used on", function() |
| 46 | local p = test.player("Ada", { survival = true, carrying = { ["Flint and steel"] = 1, Planks = 1 } }) |
| 47 | test.hold(p, "Flint and steel") |
| 48 | local at = ground(p, 2) |
| 49 | test.place(p, at, "Planks") |
| 50 | test.use_block(p, at) |
| 51 | local lit = false |
| 52 | for _, d in SIDES do |
| 53 | if world.block(at + d) == "Fire" then |
| 54 | lit = true |
| 55 | end |
| 56 | end |
| 57 | test.check(lit, "a fire beside the planks") |
| 58 | test.same(world.has(p, "Flint and steel"), 1, "one use doesn't wear it out") |
| 59 | end) |
| 60 | |
| 61 | test.case("nothing happens with something else in hand", function() |
| 62 | local p = test.player("Ada", { survival = true, carrying = { Planks = 2 } }) |
| 63 | test.hold(p, "Planks") |
| 64 | local at = ground(p, 2) |
| 65 | test.place(p, at, "Planks") |
| 66 | test.use_block(p, at) |
| 67 | test.same(fires_near(at, 1), 0) |
| 68 | end) |
| 69 | |
| 70 | test.case("a fire with nothing to eat goes out by itself", function() |
| 71 | local p = test.player("Cy") |
| 72 | local at = ground(p, 3) |
| 73 | world.set_block(at - vector.create(0, 1, 0), "Stone") |
| 74 | test.place(p, at, "Fire") |
| 75 | test.same(world.block(at), "Fire") |
| 76 | -- 30 seconds a fire, a third of that with nothing to burn, give or take |
| 77 | test.wait(16) |
| 78 | test.same(world.block(at), "Air", "gone out") |
| 79 | end) |
| 80 | |
| 81 | test.case("water puts a fire out", function() |
| 82 | local p = test.player("Cy") |
| 83 | local at = ground(p, 3) |
| 84 | test.place(p, at, "Fire") |
| 85 | world.set_block(at + vector.create(1, 0, 0), "Water") |
| 86 | test.wait(1) |
| 87 | test.same(world.block(at), "Air", "put out") |
| 88 | end) |
| 89 | |
| 90 | test.case("fire spreads along wood, and burns it away", function() |
| 91 | local p = test.player("Di") |
| 92 | local start = ground(p, 4) |
| 93 | local planks: { vector } = {} |
| 94 | for i = 0, 5 do |
| 95 | local at = start + vector.create(i, 0, 0) |
| 96 | world.set_block(at, "Planks") |
| 97 | table.insert(planks, at) |
| 98 | end |
| 99 | test.place(p, start + vector.create(0, 1, 0), "Fire") |
| 100 | local most = 0 |
| 101 | for _ = 1, 30 do |
| 102 | test.wait(2) |
| 103 | most = math.max(most, fires_near(start + vector.create(3, 0, 0), 4)) |
| 104 | end |
| 105 | test.check(most >= 2, "it spread: " .. most .. " fires at most") |
| 106 | local left = 0 |
| 107 | for _, at in planks do |
| 108 | if world.block(at) == "Planks" then |
| 109 | left += 1 |
| 110 | end |
| 111 | end |
| 112 | test.check(left < 6, "some planks burnt away") |
| 113 | end) |
| 114 | |
| 115 | test.case("where the world's rules say fire doesn't spread, it stays where it's lit", function() |
| 116 | test.rule("fire_spreads", false) |
| 117 | local p = test.player("Di") |
| 118 | local start = ground(p, 4) |
| 119 | for i = 0, 5 do |
| 120 | world.set_block(start + vector.create(i, 0, 0), "Planks") |
| 121 | end |
| 122 | test.place(p, start + vector.create(0, 1, 0), "Fire") |
| 123 | local most = 0 |
| 124 | for _ = 1, 15 do |
| 125 | test.wait(2) |
| 126 | most = math.max(most, fires_near(start + vector.create(3, 0, 0), 4)) |
| 127 | end |
| 128 | test.check(most <= 1, "it didn't spread: " .. most .. " fires at most") |
| 129 | end) |
| 130 | |
| 131 | test.case("standing in fire hurts, in survival", function() |
| 132 | local p = test.player("Ed", { survival = true }) |
| 133 | local feet = vector.create(math.floor(p.position.x) + 0.5, math.floor(p.position.y) + 0.5, math.floor(p.position.z) + 0.5) |
| 134 | world.set_block(feet, "Fire") |
| 135 | test.wait(3) |
| 136 | test.check(test.health(p) < 20, "hurt: " .. test.health(p)) |
| 137 | end) |
| 138 | |
| 139 | test.case("breaking a fire puts it out, and gives nothing", function() |
| 140 | local p = test.player("Fay", { survival = true }) |
| 141 | local at = ground(p, 2) |
| 142 | world.set_block(at, "Fire") |
| 143 | test.check(test.dig(p, at), "it breaks") |
| 144 | test.same(world.block(at), "Air") |
| 145 | test.same(world.has(p, "Fire"), 0, "nothing to carry") |
| 146 | end) |
| 147 | |
| 148 | test.case("rain puts out a fire under the sky, not one under a roof", function() |
| 149 | local p = test.player("Gus") |
| 150 | local open, roofed = ground(p, 3), ground(p, -3) |
| 151 | for _, at in { open, roofed } do |
| 152 | -- each beside a plank, so it would burn on a while by itself |
| 153 | world.set_block(at + vector.create(0, 0, 1), "Planks") |
| 154 | test.place(p, at, "Fire") |
| 155 | end |
| 156 | for dx = -1, 1 do |
| 157 | for dz = -1, 2 do |
| 158 | world.set_block(roofed + vector.create(dx, 3, dz), "Stone") |
| 159 | end |
| 160 | end |
| 161 | sky.set_weather("rain", { strength = 1, over = 0 }) |
| 162 | test.wait(6) |
| 163 | test.same(world.block(open), "Air", "the one in the rain's out") |
| 164 | test.same(world.block(roofed), "Fire", "the one under the roof burns on") |
| 165 | -- snow doesn't put fires out |
| 166 | test.place(p, open, "Fire") |
| 167 | sky.set_weather("snow", { strength = 1, over = 0 }) |
| 168 | test.wait(4) |
| 169 | test.same(world.block(open), "Fire", "snow leaves it") |
| 170 | end) |
| 171 | |
| 172 | test.case("lightning lights a fire where it strikes", function() |
| 173 | local p = test.player("Hal") |
| 174 | local at = ground(p, 5) |
| 175 | for y = 0, 3 do |
| 176 | world.set_block(at + vector.create(0, y, 0), "Oak log") |
| 177 | end |
| 178 | local top = at + vector.create(0, 4, 0) |
| 179 | test.check(sky.strike(top), "it struck") |
| 180 | test.wait(0.5) |
| 181 | test.same(world.block(top), "Fire", "on top of the trunk") |
| 182 | end) |
| 183 | |
| 184 | test.case("a fireproof player isn't hurt by standing in fire", function() |
| 185 | local p = test.player("Gil", { survival = true }) |
| 186 | test.same(world.perk(p, "fireproof"), 0, "not fireproof") |
| 187 | local feet = vector.create(math.floor(p.position.x) + 0.5, math.floor(p.position.y) + 0.5, math.floor(p.position.z) + 0.5) |
| 188 | world.set_block(feet, "Fire") |
| 189 | -- a creation's perk, as a Cinderward tonic gives it (this case plays as the fire's creation) |
| 190 | world.perk(p, "fireproof", 1) |
| 191 | test.wait(3) |
| 192 | test.same(test.health(p), 20, "not a scratch") |
| 193 | world.perk(p, "fireproof", 0) |
| 194 | test.wait(3) |
| 195 | test.check(test.health(p) < 20, "hurt again without it: " .. test.health(p)) |
| 196 | end) |