Sign in
creature

Hot air balloon

by Potluck · version 1 · Checked for all ages

Put a balloon basket down and it fills up. Climb in, hold Burner to rise and let go to sink slowly, and steer as you walk. It climbs and turns gently and never tips. Bop it once it's landed to pack it back into a basket. Make the basket at a workbench.

1--!strict
2-- Hot air balloon. Put a Balloon basket down and it fills up; climb in
3-- (Interact), hold Burner to rise and let go to sink slowly, Vent to come
4-- down faster, and steer as you walk. It's a flier (`flies`, world 0.9): no
5-- gravity, and `self:climb` sets how fast it rises or sinks, which it eases
6-- into, so the ride is smooth. It never tips or rolls. Bop it once it's
7-- landed and empty to pack it back into a basket. Balloons are kept with the
8-- world (store), so a server's are still there after a restart. Its client
9-- half (client.luau) shows riders how high they are and what's below.
10
11local world = require("@potluck/world@0.9")
12local tweak = require("@potluck/tweak")
13local input = require("@potluck/input@0.1")
14local ui = require("@potluck/ui@0.1")
15local sound = require("@potluck/sound@0.2")
16local store = require("@potluck/store")
17local net = require("@potluck/net@0.2")
18
19local knobs = tweak {
20 color = { "#c24b41", label = "Envelope", order = 1 },
21 band = { "#efe6d2", label = "Panels", order = 2 },
22 rise = { 1.5, min = 0.5, max = 3, label = "How fast the burner lifts it", order = 3 },
23 sink = { 0.6, min = 0.2, max = 1.5, label = "How fast it sinks", order = 4 },
24 drift = { 2, min = 0.5, max = 4, label = "How fast it drifts", order = 5 },
25}
26
27local BASKET = "Balloon basket" -- the block that becomes a balloon
28local WICKER = "#9a7038"
29local IRON = "#3a3a3e"
30
31-- the basket: a floor, four walls to stand between, and a rim
32local function basket(): { any }
33 return {
34 { "floor strap", size = { 1.3, 0.12, 1.3 }, at = { 0, 0.06, 0 }, color = WICKER },
35 { "wall strap front", size = { 1.3, 1.0, 0.1 }, at = { 0, 0.5, -0.6 }, color = WICKER },
36 { "wall strap back", size = { 1.3, 1.0, 0.1 }, at = { 0, 0.5, 0.6 }, color = WICKER },
37 { "wall strap left", size = { 0.1, 1.0, 1.1 }, at = { -0.6, 0.5, 0 }, color = WICKER },
38 { "wall strap right", size = { 0.1, 1.0, 1.1 }, at = { 0.6, 0.5, 0 }, color = WICKER },
39 }
40end
41
42-- four ropes up from the basket's corners, and the burner between them
43local function rigging(lit: boolean): { any }
44 local parts = {}
45 for _, c in { { -1, -1 }, { 1, -1 }, { -1, 1 }, { 1, 1 } } do
46 table.insert(parts, { "rope strap", size = { 0.05, 1.9, 0.05 }, at = { c[1] * 0.6, 1.95, c[2] * 0.6 }, color = "#c2a277" })
47 end
48 table.insert(parts, { "burner plate", size = { 0.5, 0.14, 0.5 }, at = { 0, 2.45, 0 }, color = IRON })
49 if lit then
50 table.insert(parts, { "flame glow", size = { 0.26, 0.5, 0.26 }, at = { 0, 2.8, 0 }, color = "#ffb347", glow = true })
51 else
52 table.insert(parts, { "pilot glow", size = { 0.1, 0.12, 0.1 }, at = { 0, 2.58, 0 }, color = "#6fa8ff", glow = true })
53 end
54 return parts
55end
56
57-- the envelope, in tiers from its mouth up, each a cross of two boxes so
58-- its corners look rounded: one box in each color, so from any side a band
59-- runs down the middle like a balloon's panels
60local function envelope(tiers: { { number } }): { any }
61 local parts = {}
62 for _, t in tiers do
63 local y, w, h = t[1], t[2], t[3]
64 table.insert(parts, { "cloth", size = { w, h, w * 0.6 }, at = { 0, y, 0 }, color = knobs.color })
65 table.insert(parts, { "cloth", size = { w * 0.6, h, w }, at = { 0, y, 0 }, color = knobs.band })
66 end
67 return parts
68end
69
70local function join(...: { any }): { any }
71 local all = {}
72 for _, list in { ... } do
73 for _, p in list do
74 table.insert(all, p)
75 end
76 end
77 return all
78end
79
80local FULL = {
81 { 3.1, 1.3, 0.5 },
82 { 3.7, 2.6, 0.7 },
83 { 4.5, 3.8, 0.9 },
84 { 5.45, 4.5, 1.0 },
85 { 6.45, 4.6, 1.0 },
86 { 7.35, 4.0, 0.8 },
87 { 7.95, 2.8, 0.4 },
88}
89local HALF = {
90 { 3.0, 1.3, 0.4 },
91 { 3.5, 3.0, 0.6 },
92 { 4.1, 3.6, 0.6 },
93 { 4.6, 2.6, 0.4 },
94}
95
96local SIZE = { 1.4, 4, 1.4 }
97
98-- just put down: the envelope heaped beside the basket
99local Empty = world.creature {
100 name = "Balloon (empty)",
101 count = 0,
102 speed = 0,
103 size = SIZE,
104 body = join(basket(), rigging(false), {
105 { "cloth heap", size = { 2.2, 0.35, 1.3 }, at = { 1.85, 0.18, 0 }, color = knobs.color },
106 { "cloth heap", size = { 1.2, 0.25, 1.0 }, at = { 1.7, 0.45, 0 }, color = knobs.band },
107 }),
108}
109
110-- the burner's filling it
111local Filling = world.creature {
112 name = "Balloon (filling)",
113 count = 0,
114 speed = 0,
115 size = SIZE,
116 body = join(basket(), rigging(true), envelope(HALF)),
117}
118
119local Balloon = world.creature {
120 name = "Hot air balloon",
121 count = 0,
122 speed = knobs.drift,
123 rideable = true,
124 seat = 0.7,
125 flies = true,
126 size = SIZE,
127 body = join(basket(), rigging(false), envelope(FULL)),
128}
129
130-- the same with the burner roaring
131local Burning = world.creature {
132 name = "Hot air balloon (burner on)",
133 count = 0,
134 speed = knobs.drift,
135 rideable = true,
136 seat = 0.7,
137 flies = true,
138 size = SIZE,
139 body = join(basket(), rigging(true), envelope(FULL)),
140}
141
142-- where every balloon is (each one says, every tick), kept with the world
143local saved: { vector } = store.get("balloons") or {}
144local where: { [number]: vector } = {}
145
146local function save()
147 local list = {}
148 for _, at in where do
149 table.insert(list, at)
150 end
151 store.set("balloons", list)
152end
153
154local function roar(self)
155 sound.play("burner", { at = self, volume = 0.6 })
156end
157
158-- ---- filling up ----
159
160function Empty:spawned()
161 self.filled = 0
162end
163
164local function filling(self, dt: number)
165 where[self.id] = self.position
166 self.filled = (self.filled or 0) + dt
167 if self.filled >= 1.2 and self.kind == Empty.name then
168 self:become(Filling)
169 roar(self)
170 elseif self.filled >= 3.2 and self.kind == Filling.name then
171 self:become(Balloon)
172 self:say("Ready! Climb in.")
173 sound.play("chime", { at = self, caption = "The balloon's full" })
174 save()
175 elseif self.kind == Filling.name and math.floor(self.filled * 2) ~= math.floor((self.filled - dt) * 2) then
176 roar(self)
177 end
178end
179Empty.tick = filling
180Filling.tick = filling
181
182local function not_yet(self, player)
183 ui.show(player, "wait", "It's still filling up.", { at = "bottom" })
184 world.after(2, function()
185 ui.hide(player, "wait")
186 end)
187end
188Empty.used = not_yet
189Filling.used = not_yet
190
191-- ---- flying ----
192
193-- each balloon's rider, as its client half was last told: it shows the
194-- height only while it's been told they're aboard
195local told: { [number]: number } = {}
196
197local function tell_rider(self)
198 local now = if self.rider then self.rider.id else nil
199 local was = told[self.id]
200 if was == now then
201 return
202 end
203 if was then
204 net.send(was, "aboard", false)
205 end
206 if now then
207 net.send(now, "aboard", true)
208 end
209 told[self.id] = now
210end
211
212-- a client half that's just started (after a change to the files) asks
213net.on("hello", function(player)
214 for _, id in told do
215 if id == player.id then
216 net.send(player, "aboard", true)
217 end
218 end
219end)
220
221local function fly(self, dt: number)
222 where[self.id] = self.position
223 tell_rider(self)
224 local r = self.rider
225 local burning = r ~= nil and input.held("burner", r)
226 if burning then
227 self:climb(knobs.rise)
228 elseif r and input.held("vent", r) then
229 self:climb(-math.min(knobs.sink * 3, 3))
230 else
231 -- let go, or nobody aboard: it settles gently back down
232 self:climb(-knobs.sink)
233 end
234 local lit = self.kind == Burning.name
235 if burning ~= lit then
236 self:become(if burning then Burning else Balloon)
237 end
238 if burning then
239 self.roar_in = (self.roar_in or 0) - dt
240 if self.roar_in <= 0 then
241 self.roar_in = 0.6
242 roar(self)
243 end
244 end
245 -- now and then, where it is now
246 self.save_in = (self.save_in or 0) - dt
247 if self.save_in <= 0 then
248 self.save_in = 5
249 save()
250 end
251end
252Balloon.tick = fly
253Burning.tick = fly
254
255local function aboard(self, player)
256 ui.show(player, "aboard", "Hold Burner to rise, let go to sink. Land to get out.", { at = "bottom" })
257 world.after(6, function()
258 ui.hide(player, "aboard")
259 end)
260end
261Balloon.ridden = aboard
262Burning.ridden = aboard
263
264-- bop it once it's down and empty to pack it up
265local function pack(self, player)
266 if self.rider then
267 return
268 end
269 if not self:on_ground() then
270 self:say("Let it land first.")
271 return
272 end
273 if world.pockets(player) ~= nil and world.give(player, BASKET, 1) == 0 then
274 ui.show(player, "full", "Make room in your hotbar to pack it up.", { at = "bottom" })
275 world.after(3, function()
276 ui.hide(player, "full")
277 end)
278 return
279 end
280 sound.play("thud", { at = self, caption = "Packed up" })
281 where[self.id] = nil
282 self:remove()
283 save()
284end
285Empty.hit = pack
286Filling.hit = pack
287Balloon.hit = pack
288Burning.hit = pack
289
290-- ---- the basket ----
291
292world.on("block_placed", function(player, at: vector, block: string)
293 if block ~= BASKET then
294 return
295 end
296 world.set_block(at, "Air")
297 if not pcall(world.spawn, Empty, at - vector.create(0, 0.5, 0)) then
298 -- too many balloons: back to the hotbar it goes
299 world.give(player, BASKET, 1)
300 end
301end)
302
303-- after a restart, the world's balloons come back full (a reload keeps them)
304world.after(0.1, function()
305 local here = 0
306 for _, kind in { Empty, Filling, Balloon, Burning } do
307 here += #world.creatures(kind)
308 end
309 if here == 0 then
310 for _, at in saved do
311 pcall(world.spawn, Balloon, at)
312 end
313 end
314end)
315