A boat for every wood, made at a workbench (under Travel) from five planks. Use one on water to set it afloat, then Interact to climb in and steer it with your movement: it rides the top of the water, calm and level, with nothing to make you seasick. Bop an empty boat to pick it back up. Boats stay where you leave them.
| 1 | --!strict |
| 2 | -- Boats: one for every wood, made at a workbench from five planks. Use a |
| 3 | -- boat on water to set it afloat (floats, world 0.35: it rides the top of |
| 4 | -- the water, calm and level, never bobbing), Interact to climb in and steer |
| 5 | -- with your movement. Bop an empty boat to pick it back up (a boat with |
| 6 | -- someone in it stays put). Every boat is kept with the world, so the |
| 7 | -- boats you leave moored are still there after a restart. |
| 8 | |
| 9 | local world = require("@potluck/world@0.35") |
| 10 | local tweak = require("@potluck/tweak") |
| 11 | local store = require("@potluck/store") |
| 12 | local sound = require("@potluck/sound@0.2") |
| 13 | |
| 14 | local knobs = tweak { |
| 15 | speed = { 3.4, min = 1.5, max = 7, label = "How fast a boat goes with a rider", order = 1 }, |
| 16 | most = { 24, min = 1, max = 64, label = "Most boats in the world", order = 2 }, |
| 17 | } |
| 18 | |
| 19 | -- each wood: its boat's planks and trim |
| 20 | local WOODS = { |
| 21 | { "Oak", "#b8905a", "#7a5a30" }, |
| 22 | { "Birch", "#d8caa0", "#9a8a62" }, |
| 23 | { "Pine", "#a07848", "#6a4c28" }, |
| 24 | { "Maple", "#c8844a", "#8a5430" }, |
| 25 | { "Willow", "#b8b078", "#7a7448" }, |
| 26 | { "Palm", "#d4b27a", "#9a7c4a" }, |
| 27 | { "Redwood", "#a8503a", "#6e2e20" }, |
| 28 | { "Mahogany", "#8a3e2a", "#541e14" }, |
| 29 | { "Cypress", "#9a8a6a", "#5e5238" }, |
| 30 | { "Plum", "#9a5a6e", "#5e3040" }, |
| 31 | { "Duskwood", "#6a5a8a", "#3a2e54" }, |
| 32 | } |
| 33 | |
| 34 | -- a boat faces -z: floor, two sides, a pointed bow, a stern and a bench |
| 35 | local function hull(plank: string, trim: string): { any } |
| 36 | return { |
| 37 | { "floor", size = { 0.8, 0.08, 1.5 }, at = { 0, 0.14, 0 }, color = trim }, |
| 38 | { "side_left", size = { 0.1, 0.45, 1.7 }, at = { -0.45, 0.4, 0 }, color = plank }, |
| 39 | { "side_right", size = { 0.1, 0.45, 1.7 }, at = { 0.45, 0.4, 0 }, color = plank }, |
| 40 | { "bow", size = { 0.6, 0.42, 0.1 }, at = { 0, 0.4, -0.88 }, color = plank }, |
| 41 | { "bow_tip", size = { 0.3, 0.34, 0.14 }, at = { 0, 0.45, -1.0 }, color = trim }, |
| 42 | { "stern", size = { 0.8, 0.4, 0.1 }, at = { 0, 0.38, 0.85 }, color = plank }, |
| 43 | { "bench", size = { 0.8, 0.06, 0.2 }, at = { 0, 0.46, 0.35 }, color = trim }, |
| 44 | { "rim_left", size = { 0.14, 0.05, 1.7 }, at = { -0.45, 0.65, 0 }, color = trim }, |
| 45 | { "rim_right", size = { 0.14, 0.05, 1.7 }, at = { 0.45, 0.65, 0 }, color = trim }, |
| 46 | } |
| 47 | end |
| 48 | |
| 49 | local kinds: { [string]: any } = {} -- by the wood's name |
| 50 | local wood_of: { [string]: string } = {} -- a kind's name to its wood |
| 51 | for _, w in WOODS do |
| 52 | local name = w[1] .. " boat" |
| 53 | kinds[w[1]] = world.creature { |
| 54 | name = name, |
| 55 | speed = knobs.speed, |
| 56 | count = 0, |
| 57 | home = "here", |
| 58 | floats = true, |
| 59 | rideable = true, |
| 60 | seat = 0.3, |
| 61 | size = { 1.0, 0.6, 1.0 }, |
| 62 | behaviors = { wander = false }, |
| 63 | body = hull(w[2], w[3]), |
| 64 | } |
| 65 | wood_of[name] = w[1] |
| 66 | end |
| 67 | |
| 68 | -- ---- boats are kept with the world ---- |
| 69 | |
| 70 | type Kept = { wood: string, x: number, y: number, z: number } |
| 71 | local saved: { Kept } = store.get("boats") or {} |
| 72 | local where: { [number]: Kept } = {} |
| 73 | local dirty = false |
| 74 | |
| 75 | local function save() |
| 76 | local list = {} |
| 77 | for _, k in where do |
| 78 | table.insert(list, k) |
| 79 | end |
| 80 | store.set("boats", list) |
| 81 | dirty = false |
| 82 | end |
| 83 | |
| 84 | local function note(self, wood: string) |
| 85 | local at = self.position |
| 86 | local k = where[self.id] |
| 87 | if not k then |
| 88 | where[self.id] = { wood = wood, x = at.x, y = at.y, z = at.z } |
| 89 | dirty = true |
| 90 | elseif math.abs(k.x - at.x) + math.abs(k.z - at.z) > 0.4 then |
| 91 | k.x, k.y, k.z = at.x, at.y, at.z |
| 92 | dirty = true |
| 93 | end |
| 94 | end |
| 95 | |
| 96 | local function total(): number |
| 97 | local n = 0 |
| 98 | for _, k in kinds do |
| 99 | n += #world.creatures(k) |
| 100 | end |
| 101 | return n |
| 102 | end |
| 103 | |
| 104 | for wood, kind in kinds do |
| 105 | function kind:tick(dt: number) |
| 106 | note(self, wood) |
| 107 | end |
| 108 | |
| 109 | -- bop an empty boat to take it home with you |
| 110 | function kind:hit(player) |
| 111 | if self.rider then |
| 112 | return |
| 113 | end |
| 114 | if world.give(player, wood .. " boat", 1) == 0 then |
| 115 | self:say("Your pockets are full.") |
| 116 | return |
| 117 | end |
| 118 | where[self.id] = nil |
| 119 | dirty = true |
| 120 | sound.play("thud", { at = self, volume = 0.5, caption = "A boat's lifted out" }) |
| 121 | self:remove() |
| 122 | end |
| 123 | |
| 124 | function kind:ridden(player) |
| 125 | sound.play("splash", { at = self, volume = 0.4, pitch = 0.8 }) |
| 126 | end |
| 127 | end |
| 128 | |
| 129 | -- the top water block above a spot, or nil |
| 130 | local function surface(at: vector): vector? |
| 131 | local x, z = math.floor(at.x), math.floor(at.z) |
| 132 | local y = math.floor(at.y) |
| 133 | if world.block(vector.create(x, y, z)) ~= "Water" then |
| 134 | return nil |
| 135 | end |
| 136 | for _ = 1, 40 do |
| 137 | if world.block(vector.create(x, y + 1, z)) ~= "Water" then |
| 138 | break |
| 139 | end |
| 140 | y += 1 |
| 141 | end |
| 142 | -- room above for the boat |
| 143 | if world.block(vector.create(x, y + 1, z)) ~= "Air" then |
| 144 | return nil |
| 145 | end |
| 146 | return vector.create(x + 0.5, y + 1 - 0.36, z + 0.5) |
| 147 | end |
| 148 | |
| 149 | world.on("block_used", function(player, at: vector, block: string) |
| 150 | if block ~= "Water" then |
| 151 | return |
| 152 | end |
| 153 | local held = world.held(player) |
| 154 | local wood = if held then string.match(held, "^(%a+) boat$") else nil |
| 155 | if not held or not wood or not kinds[wood] then |
| 156 | return |
| 157 | end |
| 158 | local spot = surface(at) |
| 159 | if not spot or total() >= math.floor(knobs.most) then |
| 160 | return |
| 161 | end |
| 162 | if not world.take(player, held, 1) then |
| 163 | return |
| 164 | end |
| 165 | local ok = pcall(world.spawn, kinds[wood], spot) |
| 166 | if not ok then |
| 167 | world.give(player, held, 1) |
| 168 | return |
| 169 | end |
| 170 | sound.play("splash", { at = spot, volume = 0.6, caption = "A boat's set afloat" }) |
| 171 | end) |
| 172 | |
| 173 | -- a reload or a restart: the world's boats are put back where they were |
| 174 | world.after(0.1, function() |
| 175 | if total() == 0 then |
| 176 | for _, k in saved do |
| 177 | if kinds[k.wood] then |
| 178 | pcall(world.spawn, kinds[k.wood], vector.create(k.x, k.y, k.z)) |
| 179 | end |
| 180 | end |
| 181 | end |
| 182 | end) |
| 183 | |
| 184 | world.every(3, function() |
| 185 | if dirty then |
| 186 | save() |
| 187 | end |
| 188 | end) |