Lay rails and ride a cart along them. Rails join the rails beside them by themselves, bending around corners and sloping up a block. Powered rails beside live wire, a pulled lever or a lit inverter speed a cart up, and without power they slow it to a stop. Make rails, powered rails and carts at a workbench.
| 1 | --!strict |
| 2 | -- Rails and carts' own tests: `potluck-server --test=rails`. |
| 3 | |
| 4 | local test = require("@potluck/test@0.7") |
| 5 | local world = require("@potluck/world@0.23") |
| 6 | |
| 7 | local UP = vector.create(0, 1, 0) |
| 8 | local LIFT = 0 |
| 9 | |
| 10 | -- a flat stone floor high in the sky: its north-west corner, where rails |
| 11 | -- go on it |
| 12 | local function floor(): vector |
| 13 | local g = world.spot("green") |
| 14 | local x0, z0, y = math.floor(g.x) + 60, math.floor(g.z) - 12, 90 |
| 15 | for dz = 0, 23 do |
| 16 | for dx = 0, 11 do |
| 17 | world.set_block(vector.create(x0 + dx, y, z0 + dz), "Stone") |
| 18 | end |
| 19 | test.wait(0) |
| 20 | end |
| 21 | return vector.create(x0, y + 1, z0) |
| 22 | end |
| 23 | |
| 24 | local function at(o: vector, x: number, y: number, z: number): vector |
| 25 | return o + vector.create(x, y, z) |
| 26 | end |
| 27 | |
| 28 | local function carts(): { number } |
| 29 | return world.creatures(test.kind("Cart")) |
| 30 | end |
| 31 | |
| 32 | -- a cart put down on the rail at c, by a player standing at `from` |
| 33 | local function cart_on(c: vector, from: vector): (any, number) |
| 34 | local p = test.player("Ada", { survival = true, carrying = { Cart = 1 }, at = from }) |
| 35 | test.place(p, c + UP, "Cart") |
| 36 | test.wait(0.2) |
| 37 | local list = carts() |
| 38 | test.same(#list, 1, "a cart") |
| 39 | return p, list[1] |
| 40 | end |
| 41 | |
| 42 | local function where(id: number): vector |
| 43 | local c = test.creature(id) |
| 44 | test.check(c ~= nil, "the cart's there") |
| 45 | return (c :: any).position |
| 46 | end |
| 47 | |
| 48 | -- rides a cart a while, a tenth of a second at a time: where it went |
| 49 | local function ride(id: number, seconds: number): { vector } |
| 50 | local went = { where(id) } |
| 51 | for _ = 1, math.floor(seconds * 10) do |
| 52 | test.wait(0.1) |
| 53 | table.insert(went, where(id)) |
| 54 | end |
| 55 | return went |
| 56 | end |
| 57 | |
| 58 | -- the most its speed changed from one tenth of a second to the next, where |
| 59 | -- it kept on the same way (a turn or the foot of a slope shortens the |
| 60 | -- straight line between two places it was) |
| 61 | local function jolt(went: { vector }): number |
| 62 | local most = 0 |
| 63 | for i = 3, #went do |
| 64 | local a, b = went[i - 1] - went[i - 2], went[i] - went[i - 1] |
| 65 | local la, lb = vector.magnitude(a), vector.magnitude(b) |
| 66 | if la < 1e-4 or lb < 1e-4 or vector.dot(a, b) / (la * lb) > 0.995 then |
| 67 | most = math.max(most, math.abs(lb - la) * 10) |
| 68 | end |
| 69 | end |
| 70 | return most |
| 71 | end |
| 72 | |
| 73 | test.case("rails join around a corner and up a slope by themselves", function() |
| 74 | local o = floor() |
| 75 | local p = test.player("Ada", { at = at(o, 0, 0, 0) }) |
| 76 | local a, b, c, d = at(o, 4, 0, 4), at(o, 4, 0, 5), at(o, 4, 0, 6), at(o, 5, 0, 6) |
| 77 | for _, r in { a, b, c, d } do |
| 78 | test.place(p, r, "Rail", "east") |
| 79 | end |
| 80 | test.wait(0.2) |
| 81 | test.same(world.block(b), "Rail", "a straight rail") |
| 82 | test.check(world.facing(b) == "north" or world.facing(b) == "south", "running on north and south: " .. tostring(world.facing(b))) |
| 83 | test.same(world.block(c), "Rail bend", "the corner bends") |
| 84 | test.same(world.facing(c), "west", "joining north and east") |
| 85 | test.same(world.block(d), "Rail", "and the rail past it") |
| 86 | -- a rail on a step up north of the first: the first climbs to it |
| 87 | world.set_block(at(o, 4, 0, 3), "Stone") |
| 88 | test.place(p, at(o, 4, 1, 3), "Rail", "east") |
| 89 | test.wait(0.2) |
| 90 | test.same(world.block(a), "Rail slope", "a slope up to it") |
| 91 | test.same(world.facing(a), "north", "climbing north") |
| 92 | test.check(world.facing(at(o, 4, 1, 3)) == "north" or world.facing(at(o, 4, 1, 3)) == "south", "the one on the step turns to meet it") |
| 93 | -- a powered rail never bends |
| 94 | test.place(p, at(o, 8, 0, 4), "Powered rail", "east") |
| 95 | test.place(p, at(o, 8, 0, 5), "Rail", "north") |
| 96 | test.place(p, at(o, 9, 0, 4), "Rail", "north") |
| 97 | test.wait(0.2) |
| 98 | test.same(world.block(at(o, 8, 0, 4)), "Powered rail", "still a powered rail") |
| 99 | end) |
| 100 | |
| 101 | test.case("a cart rolls along smoothly, around a corner, and stops at the end", function() |
| 102 | local o = floor() |
| 103 | for z = 2, 11 do |
| 104 | world.set_block(at(o, 4, 0, z), "Rail", "north") |
| 105 | end |
| 106 | world.set_block(at(o, 4, 0, 12), "Rail bend", "west") |
| 107 | for x = 5, 9 do |
| 108 | world.set_block(at(o, x, 0, 12), "Rail", "east") |
| 109 | end |
| 110 | local p, id = cart_on(at(o, 4, 0, 3), at(o, 4, 0, 0)) |
| 111 | test.same(world.has(p, "Cart"), 0, "the cart put down") |
| 112 | local rest = where(id) |
| 113 | test.check(math.abs(rest.y - (o.y + LIFT)) < 0.01, "it sits on the rail: " .. rest.y) |
| 114 | test.ride(p, test.creature(id)) |
| 115 | test.press(p, "push") |
| 116 | test.wait(0.2) |
| 117 | test.release(p, "push") |
| 118 | local went = ride(id, 12) |
| 119 | for _, w in went do |
| 120 | test.check(math.abs(w.y - (o.y + LIFT)) < 0.01, "level all the way: " .. w.y) |
| 121 | end |
| 122 | local last = went[#went] |
| 123 | test.check(vector.magnitude(last - at(o, 9.5, LIFT, 12.5)) < 0.3, "it came to rest at the end of the line: " .. tostring(last)) |
| 124 | test.check(jolt(went) < 0.4, "it gathers and loses speed gently: " .. jolt(went)) |
| 125 | local turned = false |
| 126 | for _, w in went do |
| 127 | turned = turned or (w.x > o.x + 6 and math.abs(w.z - (o.z + 12.5)) < 0.05) |
| 128 | end |
| 129 | test.check(turned, "it went around the corner and on east") |
| 130 | test.check(test.heard("rumble") > 0, "it rumbles along") |
| 131 | end) |
| 132 | |
| 133 | test.case("up a slope it slows and rolls back, and down one it speeds up", function() |
| 134 | local o = floor() |
| 135 | for z = 2, 8 do |
| 136 | world.set_block(at(o, 4, 0, z), "Rail", "north") |
| 137 | end |
| 138 | -- three steps up to the south, and a rail at the top |
| 139 | for i = 0, 2 do |
| 140 | for h = 0, i do |
| 141 | world.set_block(at(o, 4, h, 10 + i), "Stone") |
| 142 | end |
| 143 | world.set_block(at(o, 4, i, 9 + i), "Rail slope", "south") |
| 144 | end |
| 145 | world.set_block(at(o, 4, 3, 12), "Rail", "north") |
| 146 | local p, id = cart_on(at(o, 4, 0, 4), at(o, 4, 0, 2)) |
| 147 | test.ride(p, test.creature(id)) |
| 148 | test.press(p, "push") |
| 149 | test.wait(0.2) |
| 150 | test.release(p, "push") |
| 151 | local went = ride(id, 14) |
| 152 | local high, back = 0, false |
| 153 | for i, w in went do |
| 154 | high = math.max(high, w.y - o.y) |
| 155 | back = back or (i > 1 and w.z < went[i - 1].z - 0.01 and high > 0.5) |
| 156 | end |
| 157 | test.check(high > 0.5 and high < 3, "it climbed some way, not to the top: " .. high) |
| 158 | test.check(back, "and rolled back down") |
| 159 | test.check(jolt(went) < 0.4, "gently: " .. jolt(went)) |
| 160 | -- back on the flat, it went on north faster than it had gone into the slope |
| 161 | local fastest = 0 |
| 162 | for i = 2, #went do |
| 163 | local v = went[i] - went[i - 1] |
| 164 | if went[i].z < o.z + 8 and v.z < 0 then |
| 165 | fastest = math.max(fastest, -v.z * 10) |
| 166 | end |
| 167 | end |
| 168 | test.check(fastest > 1, "it came back down with some speed: " .. fastest) |
| 169 | local rest = went[#went] |
| 170 | test.check(math.abs(rest.y - (o.y + LIFT)) < 0.01, "and rests on the flat: " .. rest.y) |
| 171 | end) |
| 172 | |
| 173 | test.case("a powered rail with no power brings a cart to a stop", function() |
| 174 | local o = floor() |
| 175 | for z = 2, 20 do |
| 176 | world.set_block(at(o, 4, 0, z), if z == 6 then "Powered rail" else "Rail", "north") |
| 177 | end |
| 178 | local p, id = cart_on(at(o, 4, 0, 3), at(o, 4, 0, 1)) |
| 179 | test.ride(p, test.creature(id)) |
| 180 | test.press(p, "push") |
| 181 | test.wait(0.2) |
| 182 | test.release(p, "push") |
| 183 | local went = ride(id, 8) |
| 184 | local last = went[#went] |
| 185 | test.check(last.z > o.z + 6 and last.z < o.z + 10, "it stopped soon after the powered rail: " .. (last.z - o.z)) |
| 186 | test.check(vector.magnitude(last - went[#went - 5]) < 0.01, "and stays stopped") |
| 187 | test.check(jolt(went) < 0.4, "gently: " .. jolt(went)) |
| 188 | end) |
| 189 | |
| 190 | test.case("Brake stops a cart, and an empty one is bopped back into a cart", function() |
| 191 | local o = floor() |
| 192 | for z = 2, 20 do |
| 193 | world.set_block(at(o, 4, 0, z), "Rail", "north") |
| 194 | end |
| 195 | local p, id = cart_on(at(o, 4, 0, 3), at(o, 4, 0, 1)) |
| 196 | test.ride(p, test.creature(id)) |
| 197 | test.press(p, "push") |
| 198 | test.wait(1.5) |
| 199 | test.release(p, "push") |
| 200 | test.press(p, "brake") |
| 201 | local went = ride(id, 3) |
| 202 | test.check(vector.magnitude(went[#went] - went[#went - 3]) < 0.01, "stopped") |
| 203 | test.check(went[#went].z < o.z + 12, "well before the end: " .. (went[#went].z - o.z)) |
| 204 | test.release(p, "brake") |
| 205 | test.hit(p, test.creature(id)) |
| 206 | test.same(#carts(), 1, "not while someone's in it") |
| 207 | test.get_off(p) |
| 208 | test.hit(p, test.creature(id)) |
| 209 | test.same(#carts(), 0, "bopped empty, it's gone") |
| 210 | test.same(world.has(p, "Cart"), 1, "back into a cart") |
| 211 | end) |
| 212 | |
| 213 | test.case("a cart goes on a rail, and a bend or a slope breaks into a rail", function() |
| 214 | local o = floor() |
| 215 | local p = test.player("Ada", { survival = true, carrying = { Cart = 1 }, at = at(o, 2, 0, 2) }) |
| 216 | test.place(p, at(o, 4, 0, 4), "Cart") |
| 217 | test.wait(0.2) |
| 218 | test.same(#carts(), 0, "no cart off the rails") |
| 219 | test.same(world.block(at(o, 4, 0, 4)), "Air", "nor the block") |
| 220 | test.same(world.has(p, "Cart"), 1, "it's given back") |
| 221 | world.set_block(at(o, 6, 0, 4), "Rail bend", "north") |
| 222 | test.dig(p, at(o, 6, 0, 4)) |
| 223 | test.same(world.has(p, "Rail"), 1, "a bend breaks into a rail") |
| 224 | world.set_block(at(o, 7, 0, 4), "Rail slope", "east") |
| 225 | test.dig(p, at(o, 7, 0, 4)) |
| 226 | test.same(world.has(p, "Rail"), 2, "and so does a slope") |
| 227 | end) |