Levels
Brushwork is the level editor. It's in the macaw menu, and double-clicking a .lvl file opens it too. You build levels the way the old first-person-shooter editors did, out of brushes. A brush is a solid convex shape like a box, wedge, stair or arch, and brushes can be added together or cut out of each other. Put in lamps and a player start, press Bake (F5) to work out the light, then Play (F6) to walk around.
File > Open Example opens a copy of one of the levels that come with Macaw OS (the Sample, the Atrium, Threefold or the Keep), so you can see how they're made. Your copy goes in /home/maps, and opening the example again never overwrites your changes.
Your own programs can load these levels and draw them quickly. A level is cut into pieces with a BSP tree and portals, so only what the camera can see gets drawn.
The levels that come with it
| File | What it is |
|---|---|
/system/maps/sample.lvl | A small room with a few brushes, a good one to start from |
/system/maps/atrium.lvl | A sunlit hall of pillars and arches around a pool |
/system/maps/keep.lvl | A fortress of rooms on three floors, about as big as a level in an old shooter |
/system/maps/threefold.lvl | A castle, an underground base and a temple, joined into one long level |
Each has its baked light next to it in a .lit file. Level Player plays any of them: /games/threefold is a cart that only opens Level Player on threefold.lvl, and you can make your own the same way.
Marble Run (/games/marble) is a game of its own made from levels. Its five courses, in /games/marble/levels, float in the sky: each is closed in by a box of faces with the sky texture, so the sky shows all round and lights the course when it's baked. Its rings are crystal entities and its finish is a goal, kinds it describes for itself in /games/marble/entities.lua (see Entities for your game). The game rolls the marble with L.lvl:tracebox.
Walking round a level
WINDOW = { w = 320, h = 180, title = "Walk", realtime = true }
local L = assert(openlevel("/system/maps/sample.lvl"))
local x, y, z, yaw = levelstart(L.lv) -- the player start's feet, and which way it faces
local HALF, HH = 0.3, 0.8 -- half the player's width, and half its height
function _update()
if btn("left") then yaw = yaw - 3 end
if btn("right") then yaw = yaw + 3 end
local step = (btn("up") and 0.08 or 0) - (btn("down") and 0.08 or 0)
if step ~= 0 then
local dx, dz = math.sin(math.rad(yaw)) * step, math.cos(math.rad(yaw)) * step
-- slide the player's box along, as far as it gets before it hits something
local f = L.lvl:tracebox(x, y + HH, z, x + dx, y + HH, z + dz, HALF, HH, HALF)
x, z = x + dx * f, z + dz * f
end
end
function _draw()
clear3d(colors.black)
camera3d(x, y + 1.5, z, yaw)
L.lvl:drawbsp()
end
openlevel loads the level (.lvl), builds it, and puts on its baked light from the .lit file next to it. It gives you back a table:
| Field | What it is |
|---|---|
L.lv | The level as a Lua table, with its brushes, entities and settings (see /system/levels.lua) |
L.lvl | The built level, which draws and collides |
L.lit | Whether its baked light was put on (false if there's no .lit, or it was baked for a different version of the level) |
L.water | Its water, if it has any (see below) |
L.probes | Its reflection probes, if it has any (see below) |
L.path | The file it came from |
Drawing
| Call | What it does |
|---|---|
L.lvl:drawbsp() | Draws what the camera can see, through the BSP and portals. This is the fast way. Returns how many leaves and faces it drew |
L.lvl:draw() | Draws all of it. With a graphics card the card keeps the whole level, so this is quick too |
L.lvl:draw{ camera = "gpu" } | Draws with the graphics chip's camera, viewport and fog instead of camera3d's |
L.lvl:draw{ above = y } | Draws only what's above a height (for reflections). L.lvl:draw(y) is short for the same thing |
Levels use the same renderer as everything else in 3D, so you can draw meshes, scenes and billboards into them for people, pickups and doors.
Bumping into it
| Call | What it does |
|---|---|
L.lvl:tracebox(ax, ay, az, bx, by, bz, hx, hy, hz) | Moves a box (half-size hx, hy, hz) with its middle going from a to b. Returns how far it gets (0 to 1), the direction nx, ny, nz of whatever stopped it, and whether anything did |
L.lvl:trace(x0, y0, z0, x1, y1, z1) | How far along a line it gets before hitting something (0 to 1, where 1 means nothing) |
L.lvl:raycast(x, y, z, dx, dy, dz) | Where a ray from x, y, z going dx, dy, dz first goes into a brush. It returns t, so the spot is x + t * dx and so on, or nil if it hits nothing. It tries every brush, so it works straight after building a level |
L.lvl:boxsolid(x0, y0, z0, x1, y1, z1) | Whether a box touches anything solid |
L.lvl:boxinside(x, y, z, hx, hy, hz) | Whether a box is inside anything |
These run in C, so they're quick enough to call many times a frame. Water isn't solid, so you can walk into it.
Water
In Brushwork, Level > Water turns the selected brushes into water. Water isn't solid, and its top is a reflecting surface. L.water is a list of those surfaces, and each one can draw itself with a reflection:
function _draw()
local cam = { x = x, y = y + 1.5, z = z, yaw = yaw, pitch = 0, fov = 90 }
for _, w in ipairs(L.water) do
w:reflect(cam, function(c, above)
camera3d(c.x, c.y, c.z, c.yaw, c.pitch, c.fov)
L.lvl:draw{ above = above }
end)
end
clear3d(colors.black)
camera3d(cam.x, cam.y, cam.z, cam.yaw, cam.pitch, cam.fov)
L.lvl:drawbsp()
for _, w in ipairs(L.water) do w:draw(cam, time()) end
end
reflect draws a live reflection. The level is drawn a second time from a camera mirrored under the water, then mixed in with ripples and the water's own drifting surface. The function you give reflect gets the mirrored camera and the water's height.
That second drawing costs time. On a slow computer you can leave it out by not calling reflect (or setting w.reflections = false), and the water becomes a plain sheet.
You can also bake the reflection instead. w:bake(draw, ray) draws six views from just above the middle of the water, one each time you call it, and returns true once all six are done. After that w:draw reflects those views and nothing gets drawn twice. draw is the same function reflect takes, and ray finds the walls around the water so they show up in the right place:
if w.env == nil then
w:bake(draw, function(...) return L.lvl:raycast(...) end)
end
Baking needs a graphics card with cube maps (cubemaps() tells you). Without one, w.env becomes false and the water stays a plain sheet. Things that move don't show up in a baked reflection.
w:draw(cam, t, how) takes an optional how. Give it "plain" for just the water's surface, or "baked" to use the baked reflection even when there's a live one.
On a PC, Level Player draws live reflections, and drops them for a while if the frame rate falls too low. On PowerPC Macs it never draws them live. It bakes them instead if the graphics card has cube maps (an eMac's Radeon does), and otherwise the water is plain (an iMac G3's Rage 128). In a web browser the water is always plain.
watercamera(c) points the graphics chip the same way as camera3d, for gpu.* drawing in the reflection. It also makes clockwise triangles the front ones.
Shiny surfaces and reflection probes
Any brush can be shiny. In Brushwork, select it and type an amount into its shine field, from 0 (not shiny) to 1 (a mirror). Polished floors look good around 0.2 to 0.4.
A shiny surface reflects the nearest reflection probe. A probe is a point in the level, and the view from there is baked into a cube map. Put probes in with the Probe tool (R). One probe per room is usually enough. Put it near the middle, at about eye height, since that's where the reflections will look most right. A surface uses the closest probe in front of it, and prefers one in the same room.
Bake in Brushwork bakes the probes too. Once the lighting is done, it bakes each probe in the lit level, and they're saved next to the level as a .prb file, with its .lit file. A level that has them opens with its reflections ready, so nothing is baked while it's being played.
In your own programs, openlevel makes the probes ready, with the ones Brushwork baked already in place. bakeprobes bakes any that weren't, like those of a level that was never baked in Brushwork. Call it once a frame until it returns true. Each call draws one of a probe's six views, so it never holds things up:
function _draw()
bakeprobes(L)
clear3d(colors.black)
camera3d(x, y + 1.5, z, yaw)
L.lvl:drawbsp()
end
Only one probe is on the graphics card at a time, the one nearest the camera, so a level can have many without filling the card's memory. Surfaces that use another probe aren't shiny until you get closer to it. When you do, the old probe is taken off the card and the new one is sent a face per frame, then its reflections fade in. Baked water works the same way. Only the nearest water's reflection is kept on the card.
To keep more on the card, call L.lvl:probelimit(n): then the n probes nearest the camera are on it (from 1 to 64), and surfaces that use any of them shine. Each probe takes about 400 KB of the card's memory, so on an eMac's 32 MB card a few are plenty. L.lvl:probelimit() gives the current limit.
L = openlevel("/home/hall.lvl")
L.lvl:probelimit(3) -- the three nearest probes reflect at once
If nothing in the level is shiny, bakeprobes does nothing and returns true straight away. Probes need a graphics card with cube maps. Without one, shiny surfaces are drawn as they are. A probe sees the level as it was when it was baked, so things that move aren't in it.
For more control, newprobe(x, y, z) makes a probe of your own. Call p:bake(draw, ray) until it returns true, the same way as w:bake, then p.env is its cube map. L.lvl:setprobes({ env, ... }) tells the level which cube maps to use (up to 64), and L.lvl:setprobes() takes them away. env:pixels() gives a cube map's six faces as bytes, and gpu.envmap(bytes, where) makes it again from them. levelprobedata and applylevelprobes(L, text) do that for a level's probes and its .prb file.
Entities for your game
Besides brushes, a level holds entities: things at a place, each of a kind (its class) with fields. Macaw OS has its own (lamps, area lights, the start and probes) and a few generic kinds any game can use:
| Class | What it's for | Its fields |
|---|---|---|
marker | A named place, for a game to find | name, angle |
pickup | Something to pick up | kind, value, respawn |
enemy | Something that gets in the way | kind, health, speed, angle |
trigger | A box: something happens when the player's in it | name, w, h, d, once, target |
sound | A sound that plays near here | file, volume, radius, loop |
sign | Words to read | text, angle |
A game can describe kinds of its own in an entities.lua in its folder. Brushwork finds it for any level in that folder or below it (/home/mygame/levels/one.lvl finds /home/mygame/entities.lua), lists its kinds in the Entity tab with the rest, and gives each field a row in Properties: a number or text to type, yes or no, a choice, a colour. You can add a field of your own to any entity there too, as name = value.
-- /home/mygame/entities.lua
return {
{ name = "coin", label = "Coin", about = "Worth a point.",
colour = "#ffd700", shape = "ring", size = 0.4, float = 0.6,
fields = {
{ "value", "number", 1, min = 0 },
{ "kind", "choice", "gold", choices = { "gold", "silver" } },
{ "spins", "bool", true },
} },
{ name = "door", label = "Door", colour = "#a06030", shape = "zone",
fields = { { "w", "number", 2 }, { "h", "number", 3 }, { "d", "number", 0.5 },
{ "key", "text", "" } } },
}
shape is how Brushwork draws it: ring, flag, ball, box, diamond, star, figure (person-sized), zone (a box w by h by d), speaker or sign. size is how big that is, and float how far over a floor it goes when you put one down (0 stands on it). A field is { name, type, default }, the type one of number (with min and max if you like), text, bool, choice (with choices), colour or angle. A kind with the same name as a generic one takes its place. The file is only read for its table, never run as part of a program.
In the game, levelentities gives you each one with all its fields, the defaults filled in:
local L = assert(openlevel(APP_DIR .. "/levels/one.lvl"))
local coins = {}
for _, c in ipairs(levelentities(L.lv, "coin")) do
coins[#coins + 1] = { x = c[1], y = c[2], z = c[3], value = c.value, spins = c.spins }
end
| Call | What it does |
|---|---|
entityclasses(path) | Returns the kinds a level at path can have: { list = { class, ... }, by = { name = class }, file = its entities.lua }. openlevel puts them in L.classes |
entityclass(classes, name) | Returns one kind (one nothing describes gets a plain box) |
entitydefaults(class, e) | Fills in the fields e doesn't have with the kind's defaults |
entityvalue(field, v) | Returns v made the field's type (or its default), and whether it fitted |
Building levels from code
Levels are Lua tables, so programs can make them too. The format is described at the top of /system/levels.lua. A brush is a box { x0, y0, z0, x1, y1, z1, tex = { six names } } or a convex shape made of planes, and it can have glowing faces (glow), be water (water = true) or be shiny (shine = 0.3). Entities are lamps (light), area lights (glowing panels, arealight), the player start (start) and reflection probes (probe), plus whatever kinds a game uses: the generic ones and its own (see Entities for your game). An entity's fields are numbers, text or true/false, and the level keeps every one.
| Call | What it does |
|---|---|
newlevel() | Returns an empty level table |
loadlevel(path) / savelevel(lv, path) | Read and write .lvl files |
buildlevel(lv) | Builds a level table into something that draws (L.lvl above) |
levelstart(lv) | Returns where the player starts, as x, y, z, angle. It's 0, 2, 0, 0 if there's no start |
levellights(lv) | Returns its lamps and area lights, ready for baking |
levelwater(lv) | Returns its water surfaces as { { x0 =, z0 =, x1 =, z1 =, y = }, ... }, for newwater |
levelprobes(lv) | Returns its probes as { { x, y, z }, ... }, for newprobe |
levelentities(lv, class) | Returns its entities of one class, like "pickup", as { { x, y, z, field = value, ... }, ... }: copies, with their class's defaults for any field the level doesn't give (a level from openlevel) |
levellitpath(path) | Returns where a level's .lit file goes |
levelskies() / levelsky(name) | Returns the skies there are, and a sky's six pictures for lvl:setsky |
leveltexture(name) | Returns the picture for one of the built-in texture names |
And on a built level:
| Call | What it does |
|---|---|
lvl:bake(lights, [options]) | Starts working out the light. The options are ambient (0.08), bounce (1, how much light bounces) and softness (0.35, the size of lamps that don't give one) |
lvl:bakestep([ms]) | Bakes for up to ms milliseconds (30 if left out). Returns whether it's done, how far it's got (0 to 1), and what it's doing |
lvl:lightdata() / lvl:setlight(data) | Gets the baked light to save it, and puts it back. setlight returns false if the light was baked for a different level |
lvl:unbake() | Back to plain shading |
lvl:setsky({ px, nx, py, ny, pz, nz }) | Puts a sky around the level, from six square pictures. lvl:setsky() takes it away |
lvl:faces() | Returns every face of the built level, with its corners, texture number and which way it faces |
lvl:stats() | Returns numbers about it, such as faces, leaves, portals, luxels, baked, drawn (faces drawn last time), shiny and probes |
/games/threefold/main.lua shows how small a cart around Level Player can be, and the Atrium and Threefold levels are worth opening in Brushwork to see how they're put together.