Sign in
mod

Circuits

by Potluck · version 1 · Checked for all ages

Copper wire carries power from levers, buttons, pressure plates and daylight sensors, through inverters and delays, to copper lamps and copper gates. Power is on or off. Inverters and delays face the way you put them down, take power from behind and give it out in front. Power a piston and it pushes what's in front of it; power a note block and it plays, a step higher each time. A Grid tap beside power cable is a source while its grid has enough power.

1# Building a Potluck creation
2
3This folder is a Potluck creation: plain text that the game runs in a sandbox. Edit any file and save; the game reloads it within a second, keeping the creatures where they are and what they remember. If the code has an error, the last good version keeps running and the game shows the error and its line.
4
5## Files
6
7| File | What |
8|---|---|
9| `potluck.toml` | Its name, who made it, what kind of thing it is, which file to run (`main`), the API it needs (`[needs] world = "0.3"`, `game = "0.1"` for boards, `store = "0.1"` to keep things, `input = "0.1"` for buttons of its own, `ui = "0.1"` for lines on the screen and questions, `sound = "0.2"` for sounds (and `[sounds]` of its own), `net = "0.1"` for a client half), its `client` file if it has one, its `[actions]` if it has any, its `[blocks]` if it makes any (world 0.12), and its license. A remix also says what it's a remix of (`remix_of`, and `[from]` with the market's code and version): leave those as the game wrote them, so the market can show its family tree and send suggested changes back |
10| `main.luau` | The code, in Luau (Lua 5.1 with types). `require("./other")` loads `other.luau` from this folder. It runs on the world's host, the only place the world changes |
11| `client.luau` | Only if it has a client half (`client = "client.luau"` in potluck.toml): code that runs on each player's own game, for what only they see. See `@potluck/net` |
12| `*.bbmodel` | Blockbench models, for creatures' bodies (`world.model`). Make them in Blockbench (any format; save the project, File > Save Model, with the texture kept inside it) |
13| `test.luau` | Only if it has tests of its own: cases that play in a world of their own, to check it does what it should. See `@potluck/test` |
14| `types/potluck.d.luau` | The API's types, for editors and AI helpers. Don't edit it: the game writes it |
15
16## The API
17
18Modules are versioned on their own, never by the game's version: `require("@potluck/world")` or, to be exact, `require("@potluck/world@0.34")`. A newer minor version only adds to an older one, so a creation made for `world 0.1` runs with 0.34. The game's `docs/api.md` is the promise that goes with the versions, and lists the limits.
19
20### `@potluck/world` 0.37
21
22Positions are Luau vectors: `vector.create(x, y, z)`, with `+`, `-` and `*` working on them, `.x`, `.y` and `.z` to read them. A table `{ x, y, z }` works too. y is up; one block is 1.
23
24- `world.creature(def)` defines a kind of creature and returns it, to add handlers to. `def` has:
25 - `name` (required), `speed` (blocks a second, default 2), `count` (how many the game keeps in the world, default 1; one that has `become` another kind still counts as the kind it came as), `home` (`"green"`, `"paddock"`, `"creek"`, `"start"` or a position: where they appear and stay near; or, new in 0.21, `"here"`: wherever each is spawned), `rideable`, `seat` (the height a rider sits at), `flies` (new in 0.9: no gravity; it rises and sinks as `self:climb` says), `swims` (new in 0.31: it gets about under water and never leaves it; not with `flies`), `steer` (new in 0.23: false so a rider's movement doesn't steer it, for a cart that goes where its rails go), `doors` (new in 0.32: true so doors it walks into open for it and shut again behind it, as villagers' do; monsters keep it off and stay out)
26 - `body`: a list of boxes, each `{ "name", size = { w, h, d }, at = { x, y, z }, color = "#rrggbb" }`, with `leg = true` to swing as it walks, `head = true` to bob, `glow = true` to light up, and (new in 0.27) `light = 0.5` to glow and light what's around it too, from 0 to 1 (1 as bright as a lantern; `world.find_light` finds it, so gloomlings and crawlers keep away). A game shows the real light of the few lit creatures nearest its player; the rest just glow. The creature faces negative z. Or (new in 0.11) the name of a Blockbench model in the creation's folder, `body = "hedgehog.bbmodel"`: see `world.model`.
27 - `size = { w, h, d }`: the box it bumps into things with and stands on. Left out, it's worked out from the body: as wide as its legs stand (or its body across its narrower side), so a long creature's head and tail reach past it rather than holding it up on the block beside it
28 - `behaviors` (new in 0.10): what it does by itself, with no code. See below.
29- `world.model(file, scale?)` (new in 0.11) reads a Blockbench model (a `.bbmodel` file in the creation's folder, saved from Blockbench) into the list of boxes a `body` is, so code can change it first (`part.color = knobs.fur`). Each cube is a part: 16 pixels to a block (`scale` 0.1 to 4 changes that), drawn with its faces' pictures from the model's texture, as Blockbench shows them. Its `color` is the color most of its faces show (or its marker color if it has no texture): change it and the picture takes on the new color, keeping its detail. Its `texture` says where its picture is in the model; set it to nil to paint the part in its color instead, like a part you write yourself. `head = true` if its name or a group it's in says "head", `leg = true` for "leg" and `glow = true` for "glow". The front is Blockbench's north (negative z). Bodies are upright boxes, so a turned cube is drawn upright and meshes are left out; the log says so. 64 cubes at most.
30- `world.block(pos)` is the name of the block there (`"Grass"`, `"Air"`, ...). `world.set_block(pos, name, facing?)` changes it; a block that turns (0.15) faces `facing` (`"north"`, `"east"`, `"south"` or `"west"`), or the way the block it replaces faced, or north. `world.facing(pos)` is the way a block that turns faces, or nil for one that doesn't (0.15).
31- Doors, trapdoors and beds (0.32): every wood has a door (`"Oak door"`, `"Pine door"`, ...) and a trapdoor (`"Oak trapdoor"`), and every color a bed (`"Red bed"`, `"Light blue bed"`, ...). A door is two blocks high and a bed two long, and `world.set_block` puts either down whole, the door's upper half above it and the bed's head the way it faces, and taking away either half takes both. `world.is_open(pos)` says whether the door or trapdoor there is open (nil for anything else), and `world.set_open(pos, open)` opens or shuts it, both halves of a door, saying whether it changed. Players open and shut them with Interact, creations hear `block_used` as usual, and a player who uses a bed wakes by it from then on; at night, once everyone in the world is in bed, it's morning. Wool, plaster and rugs come in the same sixteen colors (`"Light blue wool"`, `"Pink plaster"`, `"Red rug"`; white wool is `"Cream wool"`), and each color has a dye (`"Red dye"`), an item.
32- The game's own blocks come in shapes too: stairs, slabs and walls of its stones and woods (`"Stone brick stairs"`, `"Pine slab"`, `"Granite wall"`; every wood has stairs and slabs, every stone walls as well). Stairs turn with `facing` as a creation's turning blocks do (north is the way up), a slab named so lies on the bottom half of its block, and players and creatures walk up both. A wall joins to the walls and blocks beside it.
33- `world.top(x, z)` is the height of the highest solid block. `world.find_block(pos, radius, name)` is the nearest block of that name within `radius` (up to 16), or nil.
34- `world.biome(pos)` (new in 0.30) is the kind of land the world's seed makes there: `"island"` (the island itself), `"sea"`, `"beach"`, `"meadow"`, `"forest"`, `"birchwood"`, `"pinewood"`, `"snowfield"`, `"desert"`, `"savanna"`, `"marsh"`, `"mountain"`, `"maplewood"`, `"redwoods"`, `"badlands"`, `"flower_fields"`, `"heath"`, `"rainforest"`, `"swamp"`, `"blossom_grove"`, `"deep_ocean"` or `"coral_reef"`. It reads the seed, so it knows land nobody has come near yet. The Villagers build their villages to suit it.
35- The world goes on past the island in every direction and down to bedrock at y = -128 (it's 128 at the top). Land is made as players come near it, so far from everyone `world.block` says `"Air"` and `world.top` says where the ground will be (before trees); `world.set_block` there makes the land first. Keep what a creation builds near players, or near the island (0 to 384 each way), which is always there.
36- The Long Dusk (0.33) is a second place, far past the wild where nobody walks to it (from x = 20480 east), where the sun never moves: high over the salt flats and mesas of the Glare in the west, low over the copper woods of the Gold, setting in the Gloaming and gone in the frozen Long Night in the east. `world.place(pos)` says which place a spot is in, `"home"` or `"dusk"`, and `world.biome` names its lands `"glare"`, `"gold"`, `"gloaming"` and `"long_night"`. `world.daylight(pos)` there is its own standing sun's light. `world.send(player, pos, name?)` sends a player there at once, through the dark as a waypoint does, landing on the ground near `pos` (or exactly there when it's clear), and their game says "You're at" `name` (the place's, if it's left out); it says whether it sent them, and a later send to the same player replaces it. The Long Dusk creation's mirrors are the way there and back. Its own blocks: `"Dusk grass"`, `"Gloam grass"`, `"Rime"`, `"Salt crust"`, `"Duskstone"` and `"Duskstone bricks"` (with stairs, slabs and walls), `"Glarestone"`, `"Sunstone ore"`, `"Starglass"`, `"Nightglass"`, duskwood (`"Duskwood log"`, `"Duskwood leaves"`, `"Duskwood planks"`, with stairs and slabs), `"Ember pod"`, `"Lamp lily"` and `"Sunreed"`. Base creations that build or bring creatures round players leave players in the Long Dusk alone; a creation of yours should too, unless it's made for there.
37- `world.players()` is a list of players; `world.nearest_player(pos, radius?)` is the nearest one, or nil. A player is `{ id, name, position }`.
38- `world.spawn(kind, pos)` adds a creature; `world.creatures(kind?)` lists this creation's creatures by id.
39- `world.spot(name)` is a named place: `"green"`, `"paddock"`, `"creek"` (north-east of the Green), `"start"`.
40- `world.say(text)` shows a message to everyone. `world.time()` is seconds since the world started. `world.random()` is 0 to 1; `world.random(a, b)` is between a and b.
41- `print(...)` writes to the creation's log, shown in Look inside.
42
43Timers and world events (new in 0.2), for code that isn't one creature's:
44
45```luau
46local round = world.every(60, function()
47 world.say("A new round!")
48end)
49world.after(5, function() world.say("Five seconds in") end)
50world.cancel(round) -- stops a timer; true if it was waiting
51
52world.on("player_joined", function(player) world.say("Hello, " .. player.name) end)
53world.on("player_left", function(player) print(player.name .. " went home") end)
54```
55
56- `world.after(seconds, fn)` calls `fn` once, `seconds` from now; `world.every(seconds, fn)` calls it every `seconds`. Both return an id for `world.cancel(id)`. Timers run on the world's ticks, ten a second, so 0.1 is the shortest; at most 64 wait at once.
57- `world.on(event, fn)`: `"player_joined"` when someone arrives in the world and `"player_left"` when they go, each with the player. Players already here when the creation starts are in `world.players()`. Block events are below (0.5).
58- A creation's top level runs again when it reloads, and sets its timers and handlers up again. Timers started inside handlers don't carry over a reload.
59
60The blocks players carry (new in 0.3), for trading, crafting and prizes:
61
62```luau
63if world.has(player, "Oak log") >= 2 and world.take(player, "Oak log", 2) then
64 world.give(player, "Planks", 8)
65end
66local carried = world.pockets(player) -- { ["Oak log"] = 3, Sand = 2 }, or nil in creative
67```
68
69- In survival a player carries things in 36 slots: the hotbar's nine and a backpack of 27 more. A slot holds up to 99 of a block, or one tool or sword. In creative they have every block, and nothing is counted.
70- `world.pockets(player)` is what they carry, by name, or nil for a player in creative.
71- `world.has(player, thing)` is how many of that block or item they carry (`math.huge` in creative).
72- `world.take(player, thing, n?)` takes `n` (1 if left out) if they carry that many, and returns whether it did. It takes from the backpack first, so what's in hand lasts. In creative it returns true and takes nothing.
73- `world.give(player, thing, n?)` puts up to `n` in their pockets and returns how many it gave, which is fewer when they haven't room. It tops up what they carry first, then fills empty slots, the hotbar before the backpack. In creative it returns `n` and gives nothing.
74- `n` is a whole number from 1 to 64. Players can't carry air, water or bedrock.
75- A player's pockets live on their own game, so what a creation sees is what their game last said, a moment ago. A player nobody's heard from yet carries nothing.
76
77Night and day, lights, and players' health (new in 0.4), for things that come out after dark:
78
79```luau
80if world.daylight() < 0.15 and not world.find_light(self.position, 6) then
81 world.hurt(player, 1) -- true if they're in survival
82end
83world.heal(player, 4)
84print(world.daytime()) -- 0.5 is noon
85```
86
87- `world.daytime()` is the time of day, 0 to 1: 0 is midnight, 0.25 sunrise, 0.5 noon and 0.75 sunset. A day is 20 minutes.
88- `world.daylight(pos?)` is how light it is, 0 in the dead of night to 1 in full day (with a position in the Long Dusk, 0.33, how light it is there, which never changes). It's 1 through most of the day and 0 through most of the night, and moves between them in the minute or so around sunrise and sunset.
89- `world.find_light(pos, radius)` is the middle of the nearest block that gives light (a lantern, brazier, candle or torch) within `radius` (up to 16), or (new in 0.27) the lit part of a creature with `light`, or nil.
90- `world.hurt(player, amount)` takes `amount` from a player's health and `world.heal(player, amount)` gives it back. Health is 0 to 20; at 0 the player goes back to the hill. Only players in survival have health, so both return false, and do nothing, for anyone in creative. `amount` is 0 to 20.
91
92What players do to blocks (new in 0.5), for crafting tables, farms and anything a player builds:
93
94```luau
95world.on("block_used", function(player, at, block)
96 if block == "Table" then
97 print(player.name .. " wants to make something")
98 end
99end)
100world.on("block_placed", function(player, at, block) end)
101world.on("block_broken", function(player, at, block) end)
102```
103
104- `world.on("block_placed", fn)` and `world.on("block_broken", fn)` hear a player put a block down or break one; `"block_used"` hears a player Interact on a block (aiming at it, not at a creature), and from 0.17 on water too while they hold an item (a bucket, a fishing rod), with `"Water"` for its name. Each calls `fn(player, at, block)`, with the middle of the block and its name (for `block_broken`, what it was).
105- They're only for what players do: blocks creations change with `world.set_block`, and water flowing, aren't heard.
106- At most 64 are heard a tick; more wait their turn, and past a few hundred waiting the rest are dropped.
107
108Using what's in hand on nothing (new in 0.18), for armor to put on, bows, wands and anything held that does something by itself:
109
110```luau
111world.on("item_used", function(player, item, aim)
112 if item == "Iron helmet" then ... end -- nil for an empty hand
113end)
114```
115
116- `"item_used"` hears a player Interact with nothing in reach to use: no creature, no block, no water (looking at the sky, or far off). It calls `fn(player, item, aim)`, with the name of what's in their hand (nil for an empty hand) and the way they look, a vector one long.
117- It shares the 64 a tick with the block events.
118- `world.aims(item, on?)` (0.19) makes a thing to hold always aimed: Interact with it in hand is heard as `item_used` whatever it points at, a block or a creature in reach too (aiming at the middle of it), and never as `block_used` or `used`. `world.aims(item, false)` makes it as before.
119
120Shooting and throwing (new in 0.19), for bows, slings, spears and anything else that flies:
121
122```luau
123world.aims("Bow")
124world.on("item_used", function(player, item, aim)
125 if item ~= "Bow" then return end
126 world.shoot(player, { aim = aim, look = "arrow", speed = 38, drop = 0.45, power = 2 }, function(at, block, creature)
127 print(block or "", creature or "") -- what it came down on, if anything
128 end)
129end)
130function Gloomling:hit(player, shot)
131 local bop = if shot then shot.power else 1
132end
133```
134
135- `world.shoot(player, shot, landed?)` sends something flying from the player's eyes, the way `aim` says, and returns its id (or nil if the player isn't here). `look` is `"arrow"`, `"stone"` (left out) or `"spear"`; `speed` is 4 to 60 blocks a second (30); `drop` is how strongly it falls, 0 not at all to 2, 1 like a stone (1); `power` is 0 to 20 (1), for what it hits to judge how hard.
136- It flies until it comes to a creature (which hears `hit(player, shot)`, with whoever shot it) or a solid block, or for 6 seconds. Then `landed(at, block, creature, player)` is called with where it came down and the block's name, the creature's id or (new in 0.28) the player it hit (or none of them). It passes through water, and through other players unless the world's rules let players hurt each other (`world.rule("pvp")`); hurting the player it hit is the creation's to do, with `world.hurt`. Every game draws it as it flies.
137- A creation has at most 64 in the air. They go when the creation stops.
138
139Creatures that throw (new in 0.21), for imps, archers and snowball fights:
140
141```luau
142function Imp:tick(dt)
143 local p = world.nearest_player(self.position, 14)
144 if p then
145 self:throw({ at = p.position + vector.create(0, 1, 0), look = "ember", speed = 13, drop = 0.3 }, function(at, block, creature, player)
146 if player then world.hurt(player, 2) end
147 end)
148 end
149end
150```
151
152- `self:throw(shot, landed?)` sends something flying from about the creature's eyes, at `at` (a place) or the way `aim` says, with the same `look` (and `"ember"`, glowing, or, new in 0.31, `"spores"`, a puff of them), `speed`, `drop` and `power` as `world.shoot`, and returns its id.
153- It never hits the creature that threw it. Unlike a player's shot it hits players: `landed(at, block, creature, player)` gets the player it hit, and what that does is up to the creation. A creature it hits hears nothing (no player hit it), but `landed` is told which.
154
155How dangerous the world is (new in 0.21), for hostile creatures and what dying costs:
156
157```luau
158local HOW = { peaceful = 0, gentle = 0.5, normal = 1, hard = 1.5 }
159local bite = 2 * HOW[world.danger()]
160world.on("player_fell", function(player, at)
161 print(player.name .. " fell at " .. tostring(at))
162end)
163```
164
165- `world.danger()` is the world's danger: `"peaceful"`, `"gentle"` (the default), `"normal"` or `"hard"`. Players choose it for each world (Change or delete a world), and a server in its `server.toml`. What it means is up to each creation: the base game's hostile creatures don't come on Peaceful, and come fewer and gentler on Gentle.
166- `world.on("player_fell", fn)` hears a player in survival hurt too badly, with where they fell, as they go back to the hill. The Lost packs creation drops what they carry there on Normal and Hard.
167- `world.rule(name)` (new in 0.28) is whether the world allows something: `"fire_spreads"` (fire spreads to what burns beside it), `"mob_griefing"` (creatures change the world, as Ember Imps start fires) and `"pvp"` (players can hurt each other). The first two are on and pvp off unless a server's `server.toml` or the world's Change page says otherwise. Asking for any other rule is an error. Like danger, what each means is up to the creations that read it, except that players' shots pass through other players without pvp. The base game's Fire, Ember Imps, Ranged and Sparring read them, so a creation that spreads, breaks or hurts something should too.
168- `world.recipes(station, list)` (new in 0.29) tells the item guide what a station of yours makes: `list` is `{ { give = "Planks", n = 4, want = { ["Oak log"] = 1 }, note = "optional, 60 letters" }, ... }`, ideally the very table you craft from (the Workbench, anvil, furnace, cooking pot and kiln do), so the guide in everyone's inventory shows what makes and what uses each thing. Saying it again replaces the last. The station "Table" is special: someone at a table can press + on its recipes. Then every creation hears `world.on("recipe_fetch", fn(player, wants, at))` (`wants` is what the recipe takes by name; Storage brings what's short from chests, crates and barrels within 8 blocks into that player's own pockets, never anyone else's), and on the next tick the creation that said the recipe hears `world.on("recipe_craft", fn(player, station, index))`, `index` being its place in your list from 1, and makes it from the player's pockets as ever.
169- `world.on("player_hit", fn)` (new in 0.28) hears one player hit another by hand, `fn(attacker, victim)`, whatever the rules: whether it hurts is up to the creations that hear it. The Sparring creation hurts the victim by what the attacker holds where pvp is on, and tells the attacker the rules don't let them where it's off.
170- A creature kind's `home = "here"` makes each one's home wherever it's spawned, so wandering keeps it there rather than drawing it back to a spot: for creatures a creation spawns near players.
171
172XP, and what it buys (new in 0.22), for rewards, charms and anything else worth working toward:
173
174```luau
175world.on("block_broken", function(player, _at, block)
176 if block == "Iron ore" then
177 world.reward(player, 3) -- 3 XP, if they're in survival
178 end
179end)
180local xp, level = world.xp(player) -- 75 XP is level 3
181if world.spend_xp(player, 40) then -- false, and nothing spent, if they haven't 40
182 world.charm(player, "Iron pickaxe", { "Swift" })
183end
184world.haste(player, 1.5) -- digs half as fast again, until it's 1 again
185world.might(player, 1.5) -- for creations to read: world.might(player) is every creation's together
186```
187
188- `world.reward(player, xp)` gives a player in survival 0 to 100 XP and returns how much they got: each creation can give each player 120 at once, and 4 a second after that, so a lot at once comes back smaller. Players keep XP by name in each world, the game shows it as a bar over the hotbar with their level, and says when they go up a level.
189- `world.xp(player)` returns their XP and level; level n takes `world.xp_for(n)` XP in all, 5 n (n + 1): 10 for the first, 30 for the second, 550 for the tenth. `world.spend_xp(player, xp)` takes that much if they have it and says whether it did.
190- `world.haste(player, share)` makes a player dig `share` times as fast (0.25 to 4; 1 is as usual), on top of their tools. `world.might(player, share)` says how much harder they hit, which the game doesn't use itself: creatures that count bops multiply by `world.might(player)`, every creation's shares together. Called without a share, each returns every creation's together. Both last until the creation sets 1, or stops.
191- `world.charm(player, item, charms)` puts a list of names on a kind of thing a player has (up to 4, each 1 to 16 letters; an empty list takes them off), and `world.charms(player, item)` returns them, from any creation: kept with the world, by name, so a tool's charms come back when they play again. The base game's Charms creation puts `"Swift"`, `"Sturdy"`, `"Lucky"`, `"Keen"` and `"Thrifty"` on tools and weapons and `"Warm"`, `"Stout"` and `"Hearty"` on armor; the Tools creation wears a Sturdy tool slower, Ranged sometimes keeps a Thrifty bow's or sling's ammo, and Armor reads the armor charms.
192
193- `world.goals(list)` (new in 0.36) lists this creation's goals for the Goals screen, which players open from the pause menu: `{ { id = "first_ore", title = "Black rock", text = "Break coal ore with a pickaxe.", group = "Mining", after = "stone_pick", xp = 5 }, ... }`. `id` is yours alone (no slash), `title` up to 40 letters, `text` up to 120, `group` (24) is the tab it's under (default `"Goals"`), `after` is another of your goals' ids that this one comes after (until it's done the screen shows this one as locked), `xp` is 0 to 100. At most 128 a creation; saying it again replaces the list, and goals go when the creation stops.
194- `world.achieve(player, id)` marks one of your goals done for that player (kept with the world, by their name), and says whether it was new: the player's game says "Goal: title" with a chime, and a survival player gets its XP, once. `world.achieved(player, id)` says whether it's done. Goals can be done in any order, whatever `after` says. The base game's own goals are the Goals creation's.
195- `world.perk(player, name, level?)` (new in 0.34) gives a survival player a perk, level 0 to 4 (0 takes it away), from this list only: `"night_sight"` (the dark shows more), `"leap"` (jump higher), `"gills"` (breathe under water; players have a breath bar and drown slowly without it), `"feather"` (fall slowly, no fall damage), `"fireproof"`, `"luck"`, `"stride"` (walk faster) and `"warm"`. The game applies the first four and `"stride"` itself; for `"fireproof"`, `"luck"` and `"warm"` creations read `world.perk(player, name)` and do what it says (skip the burn, better odds, no cold). Each creation sets its own level and the highest counts; called without a level it returns that highest. Perks last until the creation sets 0, or stops.
196
197How fast players heal (new in 0.6), for hunger, rest and healing springs:
198
199```luau
200world.healing(player, 0) -- an empty belly: no healing by themselves
201world.healing(player, 2) -- well fed: twice as fast
202world.healing(player, 1) -- as usual again
203```
204
205- Survival players heal by themselves, one health every 4 seconds. `world.healing(player, rate)` changes how fast for that player: 1 as usual, 0 not at all, up to 4. It stays until the creation changes it again, stops, or the player leaves, and returns false for a player whose game hasn't said hello yet.
206- When several creations set it, their rates multiply, so any creation's 0 means no healing.
207
208Hurting only so far (new in 0.7), for hunger, thorns and anything that should sting but not send a player back to the hill:
209
210```luau
211world.hurt(player, 1, { down_to = 10 }) -- never below half health
212```
213
214- `down_to` is the least health the hurt leaves, 0 to 20 (0, or leaving it out, is no limit). A player already at or below it isn't hurt at all. `guarded = false` (0.18) is a hurt that `world.guard` doesn't take its share off.
215- Only the player's own game knows their health, so it works out how much to take when the hurt arrives.
216
217Keeping off part of every hurt (new in 0.18), for armor, shields and charms:
218
219```luau
220world.guard(player, 0.6) -- they take 60% of every hurt
221world.guard(player, 1) -- every hurt in full again
222```
223
224- `world.guard(player, share)` is how much of every `world.hurt` reaches that player, 0 to 1, from any creation: a Gloomling's bite, say. Falls, which the player's own game works out, aren't guarded. It stays until the creation changes it again, stops, or the player leaves.
225- When several creations set it, their shares multiply. A hurt armor shouldn't help with, like starving or burning, says `{ guarded = false }`: `world.hurt(player, 1, { down_to = 10, guarded = false })`. `down_to` holds either way.
226
227What a player is seen wearing (new in 0.24), drawn on them by every game:
228
229```luau
230world.wear(player, "head", "Iron helmet")
231world.wear(player, "arm", "Shield")
232world.wear(player, "arm", nil) -- the shield's off
233```
234
235- The slots are `"head"`, `"body"`, `"legs"`, `"feet"` and `"arm"` (a shield). `thing` is any block or item by name, or nil for nothing; it says false for a player whose game hasn't joined yet.
236- It's only how they look: what keeps hurts off is `world.guard`. The base game's Armor does both. What's worn goes when the player leaves; a creation says it again when they come back.
237- It runs where the world runs, so a creation's client half can't call it.
238
239What players hold, and tools (new in 0.8), for pickaxes, swords, wands and anything else that does something in hand:
240
241```luau
242if world.held(player) == "Stone sword" then hits = 3 end -- what's in their hand, or nil
243world.digging("Wooden pickaxe", { hard = 2.5 }) -- stone and brick break 2.5 times as fast
244world.digging("Wooden pickaxe", nil) -- as usual again
245print(world.kind("Oak log")) -- "wood"
246```
247
248- Items are things to carry and hold, not blocks: the pickaxes, axes, shovels and swords in wood, stone, copper, iron and moonsilver (the copper, iron and moonsilver ones new in 0.16), copper, iron and moonsilver hoes (0.16), Shears, a Bucket and a Water bucket, a Fishing rod, a Watering can, a Sickle and a Hammer (0.17), caps, helmets, coats, leggings and boots in wool, copper, iron and moonsilver and a Shield (0.18), and in leather (0.21), a Bow, a Sling and a Spear (0.19) and the Flint and steel (one to a slot), Arrow (0.19), Raw fish and Cooked fish (0.17), a Milk bucket (0.20), what farms give and kitchens make, Seeds, Berries, Apple, Baked potato, Fried egg, Pumpkin pie, Berry pie, Bean stew, Fish stew, Porridge, Honey and Honey cake, and a Feather (0.20), what wild creatures leave, Silk, Leather, Bristles, Raw meat, Cooked meat, Ember, Sentinel core and Gloom dust (0.21), what ores give, Coal, Raw copper, Raw iron, Raw gold, Crystal and Raw moonsilver, and what furnaces make and fire needs, Charcoal, Flint, Copper ingot, Iron ingot, Gold ingot, Moonsilver ingot, Bread and Roasted carrots (99 to a slot). They're carried, given and taken by name like blocks, but never put down, and `world.set_block` with one is an error.
249- `world.held(player)` is the name of what's in the chosen slot of their hotbar, or nil for an empty hand (an empty slot in survival).
250- `world.kind(block)` is what a block is made of: `"soft"` (earth, sand, gravel, wool), `"hard"` (stone, brick, ore), `"wood"`, `"plant"`, `"glass"`, `"water"` or `"air"`.
251- `world.digging(block, speeds)` makes a survival player holding `block` break each kind of block faster: speeds by kind (soft, hard, wood, plant or glass), from 0 to 10, 1 as usual and 0 not at all. nil puts it back. When several creations set speeds for the same thing, they multiply; they last until the creation changes them or stops. Breaking happens on the player's own game, so it's told the speeds.
252- Nothing wears out by itself: count `block_broken` while `world.held` is your tool, and `world.take` it when it's done.
253
254What breaking a block gives, and tools' levels (new in 0.13), for ores, prizes and blocks that need the right tool:
255
256```luau
257world.digging("Stone pickaxe", { hard = 4, level = 2 }) -- reaches hard blocks of level 2
258world.drops("Iron ore", { gives = "Raw iron", level = 2 }) -- needs a stone pickaxe or better
259world.drops("Coal ore", { gives = "Coal", count = { 1, 2 }, level = 1 })
260world.drops("Glass", { gives = false }) -- breaks into nothing
261world.drops("Iron ore", nil) -- gives itself again, and breaks by hand
262```
263
264- `world.drops(block, rule)` says what a survival player gets for breaking `block`: `gives` (a block or item's name, false for nothing, or left out for the block itself), `count` (a number from 0 to 64, or `{ least, most }` for somewhere between, 1 if left out) and `level`, the level of tool it needs (0 to 9, 0 if left out). nil puts it back to giving itself. When several creations say what a block gives, the one that said it last counts; a creation's rules go when it stops.
265- `level` in `world.digging` is how hard a block a thing reaches, 0 to 9, among the kinds it digs faster than a hand (speed above 1). A block whose drop has a level doesn't break at all until the player holds something of that level or more, and the game tells them which tool would. Empty hands are level 0. When several creations give the same thing a level, the highest counts.
266- The base game's levels: wooden tools 1 and stone 2. The mining creation's coal ore needs level 1 and its other ores 2, for now.
267
268A creature's handlers, written as `function Kind:handler(...)`:
269
270- `spawned()` when it first appears
271- `tick(dt)` ten times a second; `dt` is the seconds since the last
272- `used(player)` when a player interacts with it (if it isn't rideable)
273- `ridden(player)` when a player climbs on (rideable ones climb on by themselves)
274- `hit(player, shot)` when a player hits it: by hand, or (0.19) with something they shot, when `shot` is `{ id, look, power }` from `world.shoot` (nil by hand)
275
276Inside a handler, `self` is that creature. Fields you set on it (`self.mood = "happy"`) are kept across reloads. It has:
277
278- `self.position`, `self.id`, `self.kind`, `self.rider` (the player riding it, or nil)
279- `self:walk_to(pos, speed?)`, `self:wander(speed?)`, `self:stop()`, `self:hop(power?)`, `self:face(pos)`. Walking, it steps up onto anything one block high (never onto a fence) and stops at anything taller; it only ever hops when you say so
280- `self:say(text)` (a speech bubble), `self:distance_to(pos)`, `self:throw_rider(force?)`, `self:remove()`
281- `self:on_ground()`, and (new in 0.9) `self:climb(speed)` and `self:become(kind)`, and (new in 0.23) `self:follow(points, speed)` and `self:following()`, below
282
283Fliers and changing kind (new in 0.9), for balloons, birds, hatching eggs and anything with more than one look:
284
285```luau
286local Balloon = world.creature { name = "Balloon", flies = true, rideable = true, body = { ... } }
287function Balloon:tick(dt)
288 if self.rider and input.held("burner", self.rider) then self:climb(1.5) else self:climb(-0.6) end
289end
290function Egg:hit(player) self:become(Chick) end -- same creature, the Chick's body and handlers
291```
292
293- A creature with `flies = true` has no gravity. `self:climb(speed)` sets how fast it rises (or sinks, below 0), from -4 to 4 blocks a second, and it eases into that speed over a second or so, so whoever rides it is never jolted. It stays at what it was last told; 0 hovers. It returns false for a creature that neither flies nor swims. Fliers float on water, and slow to a stop 24 blocks above the world's highest blocks.
294- A creature with `swims = true` (new in 0.31) has no gravity while its middle is in water, and wanders up and down as well as about. It never swims out: a move that would take it out of the water is cut short, so it bumps along the top and the bottom. `self:climb(speed)` works as a flier's does, rising no higher than the top of the water; 0 lets it go up and down to where it walks. Out of the water (stranded, or spawned on land) it sinks and can only flop: it doesn't walk, so a creation should send it back or let it go. Ridden, it keeps to the top of the water so its rider's head is out, and the rider steers it.
295- A creature with `floats = true` (new in 0.35) swims too, but rides the top of the water, ridden or not, settling gently with its middle just under the surface and never bobbing, so a boat sits on a lake and carries its rider across. `climb` and wandering up and down are ignored. On land it only flops, as a swimmer does.
296- A rider steers a flier with their movement while it's in the air; it gathers and loses speed gently. On the ground it stays put.
297- `self:on_ground()` is whether it's standing on something.
298- `self:become(kind)` turns it into another kind of this creation's: the new kind's body, size, speed, `rideable`, `seat`, `flies` and `swims`, and its handlers from then on. Its fields and its rider stay (unless the new kind isn't rideable). `spawned` isn't called again, and it still counts toward the `count` of the kind it came as, so a sheared sheep doesn't bring another.
299- Creatures aren't kept when a server restarts (they come back by `count`); keep anything that matters, such as where a balloon is, in `@potluck/store`.
300
301Paths (new in 0.23), for carts on rails, boats on a channel and anything that should glide along a line rather than walk:
302
303```luau
304local Cart = world.creature { name = "Cart", rideable = true, steer = false, body = { ... } }
305function Cart:tick(dt)
306 local left, speed, points = self:following()
307 if points < 4 then self:follow(next_rails(self), nil, true) end -- more of the line, kept on the end
308 self:follow(nil, if self.rider and input.held("brake", self.rider) then 0 else 6)
309end
310```
311
312- `self:follow(points, speed?, more?)` sends it along `points`, a list of up to 128 positions, in straight lines from one to the next with no gravity, facing the way it goes; `more = true` adds them to the end of the path it's on instead of starting a new one. `speed` (0 to 12 blocks a second, or nil to keep the last) is how fast you'd like it to go: it gathers and loses speed gently, about 2.5 blocks a second each second, and slows by itself to stop at the path's last point, so a ride is never jolted. `self:follow({})` stops it there, and it falls and walks as usual again; `walk_to`, `wander` and `stop` end a path too.
313- `self:following()` is how far it has left to go along the path, how fast it's going, and how many points are left (the one it's heading for counts).
314- A kind with `steer = false` isn't steered by its rider, on a path or off one.
315- `world.spawn` (new in 0.23) puts a creature down where it says when that isn't inside a solid block, as on a rail or in a doorway, and only moves it up to the top when it is.
316
317Behaviors (new in 0.10): a creature can do the usual things with no code at all, and code is for what they can't do.
318
319```luau
320local Woolly = world.creature {
321 name = "Woolly",
322 home = "green",
323 behaviors = {
324 follow_food = "Wheat", -- comes to a player holding wheat
325 follow_when_used = 20, -- follows whoever pats it, for 20 s
326 flee_when_hit = 6, -- runs 6 blocks from whoever bops it
327 hop_when_hit = 0.8,
328 chatter = { "Baa.", "Baa?" }, -- says one now and then, and when patted
329 sound = "bleat", -- with a sound, and when patted or bopped
330 },
331 body = { ... },
332}
333```
334
335- `wander` (true unless it's false): wanders around its home when it has nothing else to do.
336- `follow_food`: a block's name, or a list of them. It comes to the nearest player within 10 blocks with one in their hand, stops a couple of blocks off and looks at them, and goes back to what it was doing when they put it away.
337- `follow_when_used`: seconds it follows whoever used it (true for 20, up to 300). Being hit stops it.
338- `flee_when_hit`: how far it runs from whoever hit it, straight away from them, at nearly twice its speed (true for 6, up to 16).
339- `hop_when_hit`: how high it hops when hit, as `self:hop` has it (true for 0.6, up to 3).
340- `chatter`: lines it says now and then (every 10 to 35 seconds) and when it's used, one at random, 80 letters each.
341- `sound`: one of the game's sounds or the creation's own (`sound.names()`), with its chatter, and when it's used or hit. It needn't `require` `@potluck/sound`.
342- Behaviors act first, each tick and when it's used or hit, and then its own handlers run, so its code has the last word: a `tick` that calls `self:walk_to` takes it somewhere else, and a `hit` that calls `self:say` says something else. A creature being ridden doesn't follow anyone. Kinds that `become` one another can share one table of behaviors, or have their own.
343
344#### Blocks of its own (0.12)
345
346A creation makes blocks in `potluck.toml`, one table each, drawn in letters: each letter in a picture is one pixel, colored by `colors`, and `.` is see-through. Players and code use them by name like the game's own (`world.set_block(at, "Red bricks")`, `world.give`, `follow_food`), and they stay in the world as long as it lasts, even if the creation goes. Require `@potluck/world@0.12` (or newer) so an older game says it needs updating instead of running the code without its blocks.
347
348```toml
349[blocks.red_bricks]
350name = "Red bricks" # what players see it called (default: the key, "Red bricks")
351kind = "hard" # how it sounds and which tools dig it: soft, hard, wood, plant or glass (default: by its name)
352hardness = 1.5 # seconds to break by hand, 0 to 20 (default 1)
353colors = { m = "#cdc3b3", a = "#a4432f", b = "#b24f37" }
354picture = """
355aaaaaaam
356aaaaaaam
357mmmmmmmm
358aaamaaaa
359aaamaaaa
360mmmmmmmm
361aaaaaaam
362mmmmmmmm
363"""
364```
365
366- A picture is 8, 16, 32 or 64 pixels square: a string of lines, or a list of strings. `picture` is every side; `top`, `side` and `bottom` give one side each.
367- `shape`: `"solid"` (the default), `"see-through"` (glass and leaves: `.` pixels show what's behind) or `"plant"` (two crossed pictures, like flowers, that bodies walk through).
368- `boxes` makes a block of boxes instead: up to 16 of `[x, y, z, x, y, z]` in sixteenths of a block, from 0 to 16 (a slab is `[[0, 0, 0, 16, 8, 16]]`). Each side of a box shows the part of the picture it covers, so draw the side as the block looks from the side. `solid = false` lets bodies through.
369- (0.37) a block with `light = true` can say `brightness` (0.2 to 4; a lantern is 1) and `tone` (a color like `"#9ad0ff"`) for the light it gives.
370- `light = true` lights up what's around it, and the letters in `glow` (`glow = "yo"`) shine; a light with no `glow` shines in its brightest pixels.
371- Up to 64 blocks (32 before the Power creation, 16 before 0.23), 24 colors each. A block's name in `potluck.toml` is lowercase letters, digits and `_`; what players see it called can't be one of the game's blocks. Names are shared by every creation in a world, so pick one nobody else has.
372- Change a color or a row and save, and the world shows it. Each world gives a creation's blocks their ids the first time it runs there and keeps them.
373
374#### Moving pictures (0.14)
375
376A block can flicker, flow or glow on and off: `frames` gives it more pictures, shown in turn, and `speed` how many a second (0.5 to 8, 4 if left out). Require `@potluck/world@0.14` for them.
377
378```toml
379[blocks.fire]
380shape = "plant"
381light = true
382glow = "oyw"
383speed = 4
384colors = { r = "#7a1c08", o = "#e8661c", y = "#ffaa2c", w = "#fff2c0" }
385picture = """
386...
387"""
388
389[[blocks.fire.frames]]
390picture = """
391...
392"""
393
394[[blocks.fire.frames]]
395picture = """
396...
397"""
398```
399
400- Each `[[blocks.name.frames]]` is one more picture, up to 7 (8 with the block's own), with `picture`, `top`, `side` or `bottom` as the block has them; a side a frame leaves out stays as the block has it. They share the block's `colors` and `glow`.
401- The block's own picture is the first frame, and what players with Reduce flashing on (in Settings, Picture and sound) always see, so draw it as a still picture that stands for the rest.
402- Every world shares about 32 MB for moving pictures; past that, new ones stay still. Fire, the lit furnace and the campfire in the base game are examples.
403
404#### Blocks that turn, and boxes that join (0.15)
405
406A block with `turns = true` faces the way the player looked when they put it down: north, east, south or west. Draw its pictures facing north, with the top picture's first row at the front; the game turns the top picture and the boxes for the other three ways, and keeps the side and bottom pictures as they are. A block of boxes can also reach out toward its neighbors: a box written as a table with `joins` shows only when the block on that side (`"east"`, `"west"`, `"up"`, `"down"`, `"south"` or `"north"`) is one of this creation's own blocks. Require `@potluck/world@0.15` for both.
407
408```toml
409[blocks.wire]
410solid = false
411boxes = [
412 [6, 0, 6, 10, 1, 10], # always there
413 { box = [10, 0, 7, 16, 1, 9], joins = "east" }, # only toward another of this creation's blocks
414 { box = [0, 0, 7, 6, 1, 9], joins = "west" },
415]
416
417[blocks.arrow]
418turns = true
419boxes = [[0, 0, 0, 16, 2, 16], [7, 2, 2, 9, 7, 4]] # the post at the front, which is north as drawn
420```
421
422- `world.facing(pos)` says which way one faces, and `world.set_block(pos, name, facing)` puts one down facing that way. Changing a block that turns into another that turns (an off look to an on look) keeps the way it faces.
423- `joins` names the side as the block is drawn, so on a block that turns, `"north"` is its front whichever way it faces.
424- Each of the four ways takes an id of its own in the world (which holds 8192 blocks from creations), though it's one block of the 16 in `potluck.toml`: players carry, find and break all four as the one they put down, and `world.block` names them alike. Circuits in the base game is the example: wire that joins up, and inverters and delays that face the way they're put down.
425
426#### Items of its own (0.25)
427
428A creation makes items in `potluck.toml` too: things to carry that aren't blocks, each with a picture for its icon, drawn in letters as a block's is, and a kind. Players and code use them by name like the game's own items (`world.give(player, "Plum jam", 2)`, `world.held`, `world.wear`, `world.digging`), and pockets keep them by name, so they come back with the creation. Require `@potluck/world@0.25` (or newer).
429
430```toml
431[items.plum_jam]
432name = "Plum jam" # what players see it called (default: the key, "Plum jam")
433kind = "food" # material (the default), food, tool, weapon or armor
434food = 7 # how much it fills, 1 to 20 (4 if left out)
435says = "You ate some plum jam. Sweet!"
436recipe = { Berries = 4, Honey = 1 } # what a workbench makes it from
437makes = 2 # how many the recipe makes (default 1)
438colors = { p = "#6a2050", g = "#c8c2b8", w = "#f0ece4" }
439picture = """
440..gggg..
441.wwwwww.
442.pppppp.
443.pwpppp.
444.pppppp.
445.pppppp.
446.pppppp.
447..pppp..
448"""
449```
450
451- `picture` is the icon, 8, 16, 32 or 64 pixels square, with `.` for see-through. `stack` is how many fit in a slot: 99 for materials and food unless it says, one for the rest.
452- What each kind says, read by the base game's creations: `food` with `food` and `says` (Hunger eats it when hungry, the most filling that fits); `armor` with `slot` (`"head"`, `"body"`, `"legs"`, `"feet"` or `"arm"`), `guard` (0 to 10: each point keeps off 3.5% of a hurt) and `warm = true` if it keeps the cold off as wool does (Armor puts it on); `weapon` with `power`, how hard it bops (0 to 20; a hand is 1 and an iron sword 4); and `tool` with `digs`, speeds by kind of block and a level, as `world.digging` takes them (`digs = { hard = 3, level = 2 }`), which the game gives the creation's tool as it starts. Anything with a `recipe` is on the Workbench, under its kind's page.
453- Any item can be a relic (new in 0.26): `relic = "A coin worn smooth, from before anyone remembers."` is its story, 200 letters at most. The base game's Curator, Hazel on the Green, collects relics from players, tells each one's story and gives XP and a gift for the first of each in the world's collection, so a creation that hides old things to find makes them relics.
454- A dish for the cooking pot (new in 0.27): `cook = { Carrots = 3, Honey = 1 }` is what a cooking pot over a fire cooks it from (up to 6 things, as a recipe), `makes` how many it makes, and `card = "Honey carrots recipe"` the name of something a cook must carry for the pot to offer it at all, such as a recipe card the creation makes and hides somewhere. The base game's Furnaces read them, and the Deep Kitchen is the example: a buried feast hall whose shelf holds a card for each of its dishes.
455- `world.item(name)` reads one back: for a creation's item, its table as written (without the picture and colors), with `name`, `kind`, `stack` and `by`, the creation that made it; for one of the game's own, just `{ name, stack, by = "game" }`; nil for a block or nothing. `world.items()` is the names of every item creations made in this world, so a creation can find foods, armor or recipes it didn't make.
456
457```luau
458for _, name in world.items() do
459 local it = world.item(name)
460 if it and it.kind == "weapon" then
461 print(name, "bops", it.power or 1)
462 end
463end
464```
465
466- Up to 32 items a creation, 256 in a world. A name in `potluck.toml` is lowercase letters, digits and `_`, and what players see it called can't be one of the game's blocks or items. A world gives a creation's items their ids each time it starts; save things by name, as `store` and pockets do.
467
468### `@potluck/tweak` 0.1
469
470```luau
471local tweak = require("@potluck/tweak")
472local knobs = tweak {
473 speed = { 2.2, min = 0.5, max = 8, label = "Walking speed" },
474 shell = { "#4f7a3a", label = "Shell color" },
475 friendly = { true, label = "Says hello" },
476}
477-- knobs.speed, knobs.shell and knobs.friendly hold what the player chose
478```
479
480A knob is a number (a slider from `min` to `max`), a color (`"#rrggbb"`) or true or false (a switch). `order` sorts them. Anyone can change knobs in Tweak without touching the code; the creation reloads with the new values.
481
482### `@potluck/store` 0.1
483
484What a creation keeps in a world: it lasts across reloads and, on a server, across restarts.
485
486```luau
487local store = require("@potluck/store")
488local tags = store.get("tags") or {} -- what was kept, or nil
489tags[player.name] = (tags[player.name] or 0) + 1
490store.set("tags", tags) -- keep it (a copy: change it, then set it again)
491store.set("old", nil) -- forget a key
492for _, key in store.keys() do print(key) end
493print(store.used() .. " bytes of 65536")
494```
495
496- `store.get(key)` is the value kept under `key`, or nil. `store.set(key, value)` keeps it; nil forgets it. Keys are 1 to 64 letters. Values are numbers, strings, true or false, vectors and tables of those (six deep); functions and creatures aren't kept.
497- `store.keys()` lists the keys; `store.used()` is the bytes they take as JSON. Each creation has 64 KB in each world, and `store.set` fails with an error past it.
498- Each world keeps its own: a server saves it with the world (`store.json`), and so does each world a player keeps in the game (Play alone's list). A friend's world keeps it on the friend's game.
499
500### `@potluck/input` 0.1
501
502Buttons of a creation's own. Name them in `potluck.toml`, each with a pad button and a key:
503
504```toml
505[needs]
506input = "0.1"
507
508[actions.pat]
509name = "Pat" # what players see, on the HUD and in Settings
510pad = "X" # A, X, Y, LB, RB, LT, RT, Up, Down, Left or Right
511keys = "F" # Space, or a letter but W, A, S and D
512```
513
514```luau
515local input = require("@potluck/input")
516input.on("pat", function(player) print(player.name .. " patted it") end)
517
518-- a mini game takes a player's controls while they play it
519world.on("player_joined", function(player) input.focus(player) end)
520world.every(0.1, function()
521 for _, p in world.players() do
522 if input.held("pat", p) then print(p.name .. " is holding Pat") end
523 end
524end)
525```
526
527- A creation's actions are live for a player while they ride one of its creatures, or while it has their controls. Then its buttons do its actions instead of the game's (B and Shift still get a rider off, and Menu or Esc still pauses), and the HUD shows them.
528- `input.on(action, fn)` calls `fn(player)` when a player presses it. Only actions in `potluck.toml` can have handlers; `world.on` and `input.on` handlers together are at most 32.
529- `input.held(action, player)` is true while they hold it.
530- `input.focus(player)` takes a player's controls, so its actions are live wherever they are; `input.focus(player, false)` gives them back. One creation has a player's controls at a time: the last to ask. `input.focused(player)` says whether this one has them.
531- Players can move any action to other buttons (Settings, Creations' buttons), so say what an action does by its name, never by a button. A creation has at most 8 actions. One with a pad button for every action is **pad friendly** on the market.
532
533### `@potluck/ui` 0.2
534
535Lines on a player's screen, meters beside the health bar (new in 0.2), and questions with choices.
536
537```luau
538local ui = require("@potluck/ui")
539ui.show(player, "score", "Tags: 3") -- top right, for one player
540ui.show(nil, "round", "Round 2 of 5", { at = "left" }) -- nil: everyone, and whoever comes later
541ui.hide(player, "score")
542
543ui.ask(player, {
544 title = "Rodeo",
545 text = "Ride again?",
546 choices = { "Yes", "Not now" },
547}, function(player, choice)
548 if choice == 1 then print(player.name .. " wants another go") end
549 -- choice is the number of the one they chose, or nil if they closed it
550end)
551```
552
553- `ui.show(player, id, text, { at = ... }?)` shows one line, or changes the one with that id. `at` is `"right"` (top right, the default), `"left"` (top left) or `"bottom"` (above the hotbar). `player` nil shows it to everyone in the world, now and when they arrive. A line is up to 60 letters, on one line; a creation shows at most 8 at once (different ids).
554- `ui.hide(player, id)` takes it off; `ui.hide(nil, id)` takes it off every screen, including lines shown to one player with that id.
555- `ui.effect(player, id, icon?, text?)` (new in ui 0.3) shows a chip in the bottom-left column: an item's picture (`icon` is an item name) and up to two lines of 24 letters, such as a name and the time left. A screen shows 6 at most, from every creation together; they count among the creation's 8 pieces. nil or `ui.hide` takes it away.
556- `ui.meter(player, id, value, { color = "#rrggbb" }?)` shows a bar beside the health bar, `value` full from 0 to 1, amber if no color is given (food, air, a mini game's clock). nil for `value` takes it away, as does `ui.hide`. They share the row above the hotbar with the health bar (which shows only in survival); a screen shows 3 at most, from every creation together. Meters count among a creation's 8 pieces. Give each a color of its own and say once, with a line, what it means.
557- `ui.ask(player, { title?, text?, choices? }, fn)` asks one player a question. `title` is up to 40 letters (the creation's name if left out), `text` up to 300 (`"\n"` starts a new line), and up to 8 `choices` of 30 letters (`{ "OK" }` if left out). `fn(player, choice)` is called once, on a later tick, with the number of their choice, or nil if they closed it, the creation asked them something else first, or it couldn't be shown. It returns an id.
558- `ui.close(player)` takes this creation's question back off their screen, and `fn` isn't called.
559- The panel opens when the player isn't in a menu, and the game always adds who it's from and a Close button. One question from each creation waits for a player at a time. After a player closes one, the creation's questions to them get nil at once for 10 seconds, so ask again only when they come back to it (using one of its creatures, or a block it hears `block_used` for, lets it ask at once).
560- Lines and questions go when the creation stops, and a reload takes its questions away (its lines stay until it shows them again). A player who leaves takes theirs with them, and their `fn` isn't called.
561
562### `@potluck/sound` 0.2
563
564The game's sounds by name, and sounds of the creation's own. Every one is made from numbers on each player's game, so there are no sound files.
565
566```luau
567local sound = require("@potluck/sound@0.2")
568sound.play("quack", { at = self }) -- from a creature, for anyone near
569sound.play("chime", { at = world.spot("green"), volume = 0.5 })
570sound.play("fanfare") -- nowhere in particular: everyone hears it
571sound.play_for(player, "ding", { pitch = 1.5 }) -- only this player
572for _, name in sound.names() do print(name) end
573```
574
575- The sounds: `pop`, `chime`, `ding`, `boing`, `beep`, `quack`, `croak`, `bleat`, `yip`, `chirp`, `grumble`, `splash`, `thud`, `whoosh`, `fanfare`, `buzz`, `sparkle`, `click`, and the game's own `step`, `step_hard`, `step_wood`, `step_plant`, `dig`, `crack`, `chop`, `rustle`, `shatter`, `place`, `rain`, `thunder`, `door` (a creak) and `door_shut`. `sound.names()` lists them.
576- Options: `at` (a position or a creature; left out, it's the same loudness everywhere), `volume` (0 to 1), `pitch` (0.5 to 2; 2 is an octave up) and `caption`, what subtitles say instead of the sound's own word (30 letters).
577- A sound somewhere reaches players within 40 blocks, fading with distance. `sound.play_for(player, name, opts?)` plays it to one player only.
578- A creation plays at most 10 sounds a second; past that, `play` skips the sound and returns false.
579- Players who turn on **Subtitles for sounds** read each sound's caption, and which side it came from, so pick a caption that says what made it (`"Mossback hums"`).
580
581#### Sounds of its own (0.2)
582
583A creation makes its own sounds in `potluck.toml`, one table each, and plays them by name like the game's. Its own come first, so `[sounds.quack]` plays instead of the game's quack for this creation only.
584
585```toml
586[sounds.moo]
587caption = "Moo" # what subtitles say (left out: the name, "Moo")
588wave = "saw" # sine, triangle, saw, square or noise
589pitch = [220, 160] # Hz, sliding from the first to the second (or one number)
590length = 0.9 # seconds, up to 3
591attack = 0.08 # seconds to full loudness
592decay = 0.35 # seconds for it to fade to a third (left out: a third of its length)
593wobble = [5, 12] # vibrato: times a second, and Hz up and down
594pulse = [7, 0.3] # tremolo: times a second, and how much quieter (0 to 1)
595lowpass = 1600 # takes the edge off: only what's below this many Hz
596# highpass = 200 # only what's above this many Hz
597
598[[sounds.moo.layers]] # up to 3 more layers, mixed in
599wave = "noise" # for noise, pitch is how bright it is
600pitch = 900
601length = 0.3
602at = 0.05 # seconds after the start
603gain = 0.3 # how loud against the rest (0 to 2)
604
605[sounds.tada]
606wave = "square"
607notes = ["C5", "E5", "G5", "-", "C6"] # one after another; "-" is a rest, and numbers are Hz
608each = 0.12 # seconds a note (0.15 if left out); the length is notes times each
609lowpass = 3000
610```
611
612```luau
613sound.play("moo", { at = self })
614sound.play_for(player, "tada")
615```
616
617- Names are small letters, numbers and `_`, up to 24. A creation has 16 sounds of its own at most, each up to 3 seconds, with up to 3 layers and 32 notes a layer. Numbers out of range are brought into it.
618- Every sound is made as loud as the game's own, so set how loud it plays with `volume`. A sound is made once on each game and again only when its table changes, so tweak it, save, and play it again to hear the difference.
619- A table that can't be read (`piitch`, a wave that isn't one) stops the creation loading, and Look inside says which and why. The market checks them too.
620
621### `@potluck/net` 0.2
622
623A creation can have a **client half**: a second file, named by `client` in potluck.toml, that runs on each player's own game while the main file runs on the world's host. Use it for what only that player sees and that would otherwise mean a message ten times a second: a clock ticking down, a line that follows what they do, a sound only for them. The half never changes the world. It hears what its creation sends it and answers the same way, and from net 0.2 it reads that player's game's copy of the world: the blocks, the ground and the players near enough to see. The balloon's half shows its rider how high they are.
624
625```luau
626-- main.luau, on the host
627local net = require("@potluck/net")
628net.send(player, "round", { seconds = 30 }) -- to one player's half
629net.send(nil, "over", { winner = "Ada" }) -- to every player here now
630net.on("ready", function(player, value) -- from a player's half
631 print(player.name .. " is ready")
632end)
633
634-- client.luau, on each player's game
635local net = require("@potluck/net")
636local ui = require("@potluck/ui")
637local world = require("@potluck/world")
638net.on("round", function(value)
639 local ends = world.time() + value.seconds
640 world.every(0.1, function()
641 ui.show(nil, "clock", string.format("%.1f s left", math.max(0, ends - world.time())), { at = "bottom" })
642 end)
643end)
644net.send("ready", { name = net.me().name })
645```
646
647- In the main file: `net.send(player, topic, value?)` sends a message to that player's half, and `net.send(nil, topic, value?)` to every player in the world now (not to those who come later). `net.on(topic, fn(player, value))` is called on the next tick for each message a player's half sends.
648- In the client half: `net.send(topic, value?)` sends a message to the main file, `net.on(topic, fn(value))` is called for each message the main file sends this player, and `net.me()` is this player (`id`, `name` and where they stand, and from 0.2 `look`, which way they look, and `aim`, the middle of the block they aim at, or nil).
649- A topic is 1 to 32 letters. A value is what the store keeps (numbers, strings, true or false, vectors, and tables of those), at most 4 KB as JSON.
650- The main file sends at most 30 messages a second, and each player's half 10; past that, `send` returns false and the message isn't sent. The host drops what a half sends beyond that too.
651- What a client half has: `@potluck/net`; `@potluck/ui`'s `show` and `hide`, for this player only (`player` is nil, or `net.me()`), at most 8 lines; `@potluck/sound`'s `play`, heard only by this player; and from `@potluck/world` its timers (`after`, `every`, `cancel`), `time` (seconds since this half started, so send it lengths of time rather than the host's times), `random`, `spot`, `daytime` and `daylight` (this game's clock). The rest of the API is the main file's, and says so if a half calls it.
652- From net 0.2 a client half reads too: `world.block`, `world.top`, `world.find_block` and `world.kind` from this game's copy of the world, and `world.players` and `world.nearest_player` for the players it knows of (this player first, then those near enough to see). A guest's copy is the host's as it last heard, so it can be a moment behind; anything that matters (who won, what's in a chest) stays the main file's to decide and send.
653- A half starts when its player arrives or the creation loads, and starts again, fresh, whenever any of the creation's files change. If it needs to know what's going on, have it say hello (`net.send("hello")`) and answer that from the main file.
654- Its lines share ids with the main file's lines for that player, so the last to show one wins. Its lines go when it stops.
655- A half has 16 MB and 4 ms of each frame. One that fails or runs too long stops, and Look inside shows why (its prints show there too, marked `client:`).
656
657#### Screen creations
658
659A creation with `side = "player"` in potluck.toml is a **screen creation**: only a `client` file, no main file, for its own player's screen (a compass, a clock, a meter, a helper). It runs in any world, even one where the player is a guest, without the host's say, because it can't change how anyone plays: it sees only what its player sees. `world.block` answers only for the block they aim at (nil for any other), `world.top`, `world.find_block` and `world.nearest_player` are nil, `world.players` is only them, and `net.send` errors, as there's no main file to hear it. Everything else a client half has works as usual. A server can turn screen creations off (`screens = false` in its server.toml).
660
661#### Bringing creations into someone else's world
662
663Whoever hosts a world decides what runs in it. Bringing a creation (anything but a screen creation) to a world you joined, or tweaking one of its creations, works only when its host has turned on **Collaborative mode** (the world's Change page), or for a server's ops; otherwise the game says so. Alone, anything goes.
664
665### `@potluck/sky` 0.2
666
667The weather and the season, the same on every game in the world. Every game draws them (rain, snow, cloud, fog, the wind in them, autumn's leaves and winter's snow on the land) and plays the rain and the thunder. Nothing sets them but creations: the base game's Weather and seasons counts the days and picks the weather, and Farming, Fire and Armor read it.
668
669```luau
670local sky = require("@potluck/sky@0.2")
671sky.set_weather("rain", { strength = 0.7, wind = vector.create(3, 0, -2), over = 30 })
672local now = sky.weather() -- { kind = "rain", strength = 0.7, wind = vector(3, 0, -2) }
673sky.set_season("autumn", 0.4) -- 40% of the way through autumn
674local season, progress = sky.season()
675sky.on("strike", function(at) -- lightning, from any creation
676 print("lightning at", at)
677end)
678sky.strike(vector.create(100, 70, 40))
679sky.set_rainbow(vector.create(120, 66, 40), { facing = 1.2, strength = 1 }) -- 0.2
680```
681
682- `sky.set_weather(kind, opts?)`: `kind` is `"clear"`, `"cloudy"`, `"rain"`, `"storm"`, `"snow"` or `"fog"`. `strength` is how heavy, 0 to 1 (0.6 if it isn't said, 0 for clear). `wind` is blocks a second along x and z, up to 12 (the wind stays as it was if it isn't said). `over` is how many seconds every game takes to come into it, 0 to 120 (20 if it isn't said): weather never changes all at once.
683- `sky.weather()` is the weather as it was last set: `kind`, `strength` and `wind` (a vector with y 0).
684- `sky.set_season(name, progress?)`: `"spring"`, `"summer"`, `"autumn"` or `"winter"`, and how far through it, 0 to 1 (0 if it isn't said). The land follows slowly, over a few minutes: autumn's leaves turn over its first part and fall as winter starts, and winter's snow lies until spring melts it, over its first tenth. `sky.season()` is the season and how far through it; a world nothing has set it in is in spring, after the thaw (0.3). The base Weather and seasons creation starts a new world a little way into spring (two days of seven).
685- `sky.strike(at)` is lightning at a place: a soft brightening of the sky on every game near enough to see it (none at all for players with Reduce flashing on), and thunder a moment after, later the further off it was. A creation can make one a second, three at once; past that it says false and nothing strikes.
686- `sky.on("strike", fn)` hears every strike from any creation, on the next tick, with where it was. It counts towards the 32 handlers a creation can give `world.on`, `input.on` and `net.on`.
687- `sky.set_rainbow(at, opts?)` (0.2) stands a rainbow with its foot at `at`. `facing` is the heading of its arch along the ground in radians (the arch is 60 blocks across and 30 high, in a vertical plane), `strength` how much of it shows, 0 to 1 (1 if it isn't said). Every game draws it and eases it in and out over a few seconds, so say strengths in coarse steps; trees and hills stand in front of it, and it never moves a camera. `sky.set_rainbow(nil)` takes it away. There is one rainbow in a world: setting it again moves it. `sky.rainbow()` is `{ at, facing, strength }`, or nil when there isn't one. It's out of sight in the Long Dusk.
688- Whether a place is out under the sky is `world.top(x, z) <= math.floor(y)`: rain falls through leaves but not a roof. Weather drawn on the screen never moves or shakes anyone's camera.
689- It runs where the world runs, so a creation's client half can't reach it.
690
691### `@potluck/power` 0.1
692
693Loads on the Power creation's grids. `power.consume(at, watts, class?)` says the block at `at` wants `watts` while it works (class 1 lights, 2 farm, 3 machines, 4 heavy; the higher goes without first). `power.got(at)` is how many watts it was given a moment ago. `power.release(at)` takes it away. A load is on a grid when it touches Power cable or another powered part.
694
695### `@potluck/game` 0.1
696
697For mini games: boards and other servers.
698
699```luau
700local game = require("@potluck/game")
701game.score("longest ride", player, 12.5) -- higher is better
702game.score("fastest lap", player, 31.2, { lower = true }) -- lower is better
703local top = game.board("longest ride", 5) -- { { name, value }, ... }, best first
704if table.find(game.links(), "rodeo") then
705 game.send(player, "rodeo", "Off to the rodeo!")
706end
707```
708
709- `game.score(board, player, value, { lower = true }?)` puts a player's score on one of this creation's boards. A board keeps each player's best (highest, or lowest with `lower`), up to 100 players, and at most 16 boards a creation. On a server in the market's list, boards also show on the creation's page.
710- `game.board(board, n?)` is the best `n` (10 by default) as `{ name, value }`, best first.
711- `game.links()` is the names of the other servers this one can send players to (its `[links]`, set by whoever runs it). Alone or hosting from the game it's empty.
712- `game.send(player, link, note?)` sends a player to one of those servers, showing them `note`. The player at the keyboard of a hosted world stays: they are the world.
713
714### `@potluck/test` 0.8
715
716A creation's own tests, in `test.luau` beside `main.luau`. They run only when asked for: the dev bridge's `test` tool, or `potluck-server --test=balloon` (an id or a folder, or `all` for every creation that has tests; it ends with 1 if any failed, for scripts). Each case plays in a world of its own: a copy of the world with only this creation in it, its knobs as its code sets them, and players made for the test. Nothing a test does reaches the world anyone plays in. It needn't be in `[needs]`. The base game's balloon and woolly have tests to read.
717
718```luau
719local test = require("@potluck/test@0.7")
720local world = require("@potluck/world@0.12")
721
722test.case("a basket put down fills up into a balloon", function()
723 local p = test.player("Ada") -- on the Green
724 local x, z = p.position.x + 3, p.position.z
725 local at = vector.create(x, world.top(x, z) + 1, z)
726 test.place(p, at, "Balloon basket") -- as if Ada put it down
727 test.wait(4) -- four seconds of play
728 local ids = world.creatures(test.kind("Hot air balloon"))
729 test.same(#ids, 1, "balloons")
730 local b = test.creature(ids[1])
731 test.ride(p, b)
732 test.press(p, "burner") -- one of its [actions]
733 test.wait(4)
734 test.check(b.position.y > at.y + 2, "it rose")
735end)
736```
737
738- `test.case(name, fn)` adds a case (64 at most). It passes if `fn` returns, and fails at the first error, saying the line. It fails too if the creation's own code goes wrong while it plays.
739- `test.check(ok, message?)` fails the case unless `ok`; `test.same(got, want, message?)` unless the two are equal, and says both.
740- `test.wait(seconds)` lets the world play on: creatures move, and the creation's ticks, timers and events run, as in play. A case may play 600 s at most, and take 30 s of real time.
741- `test.player(name?, { at = pos, survival = true, carrying = { ["Cream wool"] = 2 } }?)` brings a player in, on the Green unless `at` says where. In creative they have every block, as in play. `test.move(player, pos)` moves them, and the table you hold with them, and makes the land round where they go, as the game does round players (past the island, a case's world has only the land a player has been near; `test.player` makes it round where they start too).
742- What players do: `test.place(player, pos, block, facing?)` (`facing` for a block that turns, 0.5; north if left out) and `test.dig(player, pos)` (heard as `block_placed` and `block_broken`; a survival player puts down only what they carry, and keeps what they dig, as `world.drops` says; `test.dig` returns false, breaking nothing, when what they hold can't break it, 0.4), `test.use_block(player, pos)`, `test.use_item(player, aim?)` (Interact on nothing, heard as `item_used`, looking `aim` or north; 0.6), `test.use(player, creature)` and `test.hit(player, creature)` (or, 0.9, `test.hit(player, other_player)`, heard as `player_hit` after a tenth of a second), `test.ride(player, creature)` and `test.get_off(player)`, `test.press(player, action)` and `test.release(player, action)` for its `[actions]`, `test.answer(player, choice)` for the question in front of them (choices count from 1), `test.hold(player, block)` for what's in their hand (nil for nothing), and `test.from_half(player, topic, value?)`, a message from that player's client half as `net.send` sends it there (0.3). Placing, digging, using a block, pressing, answering and messages from a half pass a tenth of a second so the creation hears them; using, hitting and riding a creature are heard at once.
743- Looking: `test.kind(name)` is one of its kinds, for `world.creatures` and `world.spawn`. `test.creature(id)` is a creature's table (`position`, `kind`, `rider` and the fields its code keeps), or nil once it's gone; it's the same table from one moment to the next. `test.health(player)` is their health (20 is full), `test.shown(player, id)` the text of one of its pieces on their screen, or nil, `test.asked(player)` the question in front of them (`title`, `text`, `choices`), or nil, and `test.heard(name)` how many times the creation has played that sound (its own or the game's, by the name it plays) since the case began (0.2). `test.sent(player, topic)` is the last value the creation sent that player's client half on that topic (true for a message with no value), and how many times it has since the case began: `nil, 0` if it hasn't (0.3). The half itself doesn't run in a case.
744- `test.rule(name, on)` (0.9) sets one of the world's rules for the case (`world.rule`; fire spreads and creatures change the world, players can't hurt each other, until it's set).
745- `test.danger(level)` (0.7) sets the world's danger for the case (`"peaceful"`, `"gentle"`, `"normal"` or `"hard"`; Gentle until it's set). A survival player whose health runs out in a case is heard as `player_fell` where they stand, and is whole again.
746- `test.daytime(time)` (0.8) sets the time of day for the case, 0 to 1 (0 midnight, 0.25 sunrise, 0.5 noon), and it stays there: the sun doesn't go around in a case. Set it past 0.5 and then back near 0 to have a day go by. The sky starts clear, in spring after the thaw (0.3 of the way through), and `sky.set_weather` and `sky.set_season` set it for the case.
747- The rest of the API works in a case as in `main.luau`: `world.block`, `world.pockets`, `store.get` and so on. `main.luau`'s locals aren't reachable: use `test.kind` and `test.creature`.
748- `wait`, `player`, `move` and what players do hand over to the game, so give each a line of its own rather than putting one inside a call to `world` or a creature: `local p = test.player()` and then `world.spawn(kind, p.position)`, not `world.spawn(kind, test.player().position)`.
749
750## Rules of the sandbox
751
752- No files, no network, no other creation's memory. `os`, `io` and loading bytecode aren't there.
753- Each creation gets 32 MB and a slice of each tick (15 ms for its timers, events and creatures together, counting only while its code runs, not while the computer is busy with something else); a loop that never ends is stopped with an error. A client half gets 16 MB and 4 ms a frame on its player's game.
754- At most 64 creatures and 16 kinds per creation.
755- Creations can't shake, roll or zoom the player's camera, and can't cover the game's own menus: their lines and questions sit under them.
756
757## Testing
758
759Run the world (alone is fine), stand near your creation, save the file, and watch it change. Look inside it (L, or hold Y on a pad) for its log and any error, and for how it's doing against its budgets: how long its ticks take of their 15 ms, on average and at most lately, and its memory of 32 MB.
760
761To check it keeps doing what it should as you change it, give it tests of its own in `test.luau` (see `@potluck/test`) and run them with the dev bridge's `test` tool or `potluck-server --test=<its folder>`.
762
763### The type checker
764
765Each time you save one of your own creations, the game runs Luau's type checker over its files with the API's types (`types/potluck.d.luau`), and Look inside lists what it found under **The type checker spotted**, with the file and line. It speaks up only when it's sure something is wrong:
766
767- a name a module doesn't have (`world.spwan`, with the name it probably should be);
768- a global that isn't one (`pritn`);
769- a call given too many or too few values;
770- a plain value of the wrong kind: text where a number goes, or a word an option doesn't take (`at = "middle"`).
771
772Everything it can only guess at stays quiet, so no hints doesn't mean the code is right; tests do that. `--!strict` at the top changes nothing: it checks every file as strictly and still says only what it's sure of. `--!nocheck` on a file's first line leaves that file out, if it's ever wrong. The dev bridge's `check` tool and `potluck-server --typecheck=<its folder>` (or an id, or `all`; it ends with 1 if it found anything, for scripts) run the same checks.
773
774### With a coding helper: the dev bridge
775
776A coding helper on the same computer can work with the game while you play: turn on **Settings, Dev bridge for coding helpers on this computer** (or start the game with `--bridge`) and add `http://127.0.0.1:47130/mcp` to the helper as an MCP server over HTTP (in Claude Code, `claude mcp add --transport http potluck http://127.0.0.1:47130/mcp`). It answers only programs on this computer, and it's off until you turn it on. Its tools:
777
778- `creations`: every creation in the game, which are running, the folder each of yours is in, their errors, knobs, actions, creatures, memory and load time.
779- `guide`: this file (or, with `types`, the API's types).
780- `reload`: reads a creation's folder again and starts or restarts it, keeping its creatures; an error says which file and line, and the last good version keeps running. A new folder in your creations folder is a new creation: give it a `potluck.toml` and `main.luau` and reload it by the folder's name (it gets this file and the types).
781- `log`: what it printed, its last error, and its client half's lines.
782- `run`: a few lines of Luau inside a running creation, as if they were part of its code (`return #require("@potluck/world").creatures()`), with what they printed and returned. Its files' locals aren't reachable, and it has one tick's time.
783- `test`: runs its own tests (`test.luau`, as saved in its folder) in a copy of the world, and says which cases passed and why the others failed, with the line and what it printed. Nothing it does reaches the world.
784- `check`: runs the type checker over its files as saved, and lists what it's sure is wrong, with the file and line (see The type checker above).
785- `tweak`: turns one of its knobs, as Tweak does.
786- `look_around`: where you are and what's near you: the block you look at, creatures with their creation, kind, rider and the fields their code keeps, other players, the blocks about and the time of day.
787- `screenshot`: the screen as it is, as a picture.