Sign in
creature

Villagers

by Potluck · version 1 · Checked for all ages

Rowan the carpenter and Ivy the mason live on the Green, and villages grow up far from it as you explore, built to suit the land: a street, a well and a house for each villager. Thirteen jobs trade with you: sell them what you've gathered for beads and spend beads on what they make, and the better they know you the more they show you. A peddler and his pack yak come by now and then.

1--!strict
2-- How a village is laid out and built: a street with a well in the middle,
3-- houses either side facing it (one for each villager, with their work by
4-- the door), lamps along the way and, where a grower lives, a field at one
5-- end. Everything's in the style of the land it stands on (STYLES), and
6-- only natural ground is dug or built on, so nothing a player made is
7-- touched.
8--
9-- Places in a village are by how far along the street (u) and across it
10-- (w), and how far up (h) from the street's level.
11
12local world = require("@potluck/world@0.32")
13local trades = require("./trades")
14
15export type House = { job: string, name: string, u: number, side: number }
16export type Village = {
17 id: string,
18 name: string,
19 at: vector,
20 along_x: boolean,
21 style: string,
22 houses: { House },
23 field: boolean,
24 built: boolean,
25 planned: boolean?,
26}
27
28export type Style = {
29 -- the walls, their corners, the floor and the stones under the house
30 wall: string,
31 frame: string,
32 floor: string,
33 base: string,
34 -- the roof: its blocks, the stairs down its slopes, the beam along its
35 -- ridge and the slabs along the ridge and the eaves; a flat roof has a
36 -- low wall round it (parapet)
37 roof: string,
38 stairs: string,
39 slab: string,
40 ridge: string,
41 flat: boolean?,
42 parapet: string?,
43 -- the floor's a block up off the ground, on posts
44 raised: boolean?,
45 door: string,
46 window: string,
47 street: string,
48 square: string,
49 -- the well's stones, and the low wall round its top
50 well: string,
51 rim: string,
52 post: string,
53 -- what the ground is, and under it, where it's built up
54 ground: string,
55 fill: string,
56 -- jobs that live here, likeliest first
57 jobs: { string },
58 -- now and then by the street
59 plants: { string },
60}
61
62local STYLES: { [string]: Style } = {
63 meadow = {
64 wall = "Planks", frame = "Oak log", floor = "Planks", base = "Cobblestone",
65 roof = "Hay", stairs = "Hay stairs", slab = "Hay slab", ridge = "Oak log", door = "Oak door",
66 window = "Glass", street = "Gravel", square = "Cobblestone", well = "Cobblestone", rim = "Cobblestone wall", post = "Fence",
67 ground = "Grass", fill = "Dirt",
68 jobs = { "Grower", "Baker", "Weaver", "Beekeeper", "Carpenter", "Smith", "Chandler", "Herbalist", "Wayfinder", "Tanner", "Bowyer" },
69 plants = { "Red flower", "Blue flower", "White flower", "Tall grass", "Clover", "Yellow flower" },
70 },
71 forest = {
72 wall = "Planks", frame = "Oak log", floor = "Pine planks", base = "Mossy brick",
73 roof = "Pine planks", stairs = "Pine stairs", slab = "Pine slab", ridge = "Oak log", door = "Oak door",
74 window = "Glass", street = "Gravel", square = "Mossy brick", well = "Mossy brick", rim = "Mossy brick wall", post = "Fence",
75 ground = "Grass", fill = "Dirt",
76 jobs = { "Carpenter", "Herbalist", "Bowyer", "Grower", "Beekeeper", "Tanner", "Wayfinder", "Chandler", "Baker" },
77 plants = { "Fern", "Tall grass", "Violet flower", "Bush", "Brown mushroom" },
78 },
79 birch = {
80 wall = "Birch planks", frame = "Birch log", floor = "Birch planks", base = "Cobblestone",
81 roof = "Terracotta", stairs = "Terracotta stairs", slab = "Terracotta slab", ridge = "Birch log", door = "Birch door",
82 window = "Glass", street = "Gravel", square = "Cobblestone", well = "Cobblestone", rim = "Cobblestone wall", post = "Fence",
83 ground = "Grass", fill = "Dirt",
84 jobs = { "Herbalist", "Beekeeper", "Carpenter", "Weaver", "Bowyer", "Wayfinder", "Chandler", "Grower" },
85 plants = { "White flower", "Fern", "Tall grass", "Blue flower" },
86 },
87 pine = {
88 wall = "Pine planks", frame = "Pine log", floor = "Pine planks", base = "Cobblestone",
89 roof = "Slate", stairs = "Slate stairs", slab = "Slate slab", ridge = "Pine log", door = "Pine door",
90 window = "Glass", street = "Gravel", square = "Cobblestone", well = "Cobblestone", rim = "Cobblestone wall", post = "Fence",
91 ground = "Grass", fill = "Dirt",
92 jobs = { "Carpenter", "Tanner", "Bowyer", "Smith", "Angler", "Wayfinder", "Chandler", "Herbalist" },
93 plants = { "Fern", "Bush", "Tall grass", "Red mushroom" },
94 },
95 snow = {
96 wall = "Pine log", frame = "Stone bricks", floor = "Pine planks", base = "Cobblestone",
97 roof = "Pine planks", stairs = "Pine stairs", slab = "Pine slab", ridge = "Pine log", door = "Pine door",
98 window = "Glass", street = "Gravel", square = "Stone bricks", well = "Stone bricks", rim = "Stone brick wall", post = "Fence",
99 ground = "Snow", fill = "Dirt",
100 jobs = { "Tanner", "Smith", "Angler", "Weaver", "Mason", "Wayfinder", "Chandler", "Bowyer" },
101 plants = {},
102 },
103 mountain = {
104 wall = "Cobblestone", frame = "Pine log", floor = "Pine planks", base = "Stone",
105 roof = "Slate", stairs = "Slate stairs", slab = "Slate slab", ridge = "Slate", door = "Pine door",
106 window = "Glass", street = "Gravel", square = "Stone bricks", well = "Stone bricks", rim = "Stone brick wall", post = "Fence",
107 ground = "Grass", fill = "Stone",
108 jobs = { "Smith", "Mason", "Tanner", "Wayfinder", "Chandler", "Weaver", "Herbalist" },
109 plants = { "Tall grass", "Heather" },
110 },
111 desert = {
112 wall = "Sandstone", frame = "Sandstone bricks", floor = "Sandstone bricks", base = "Sandstone bricks",
113 roof = "Sandstone bricks", stairs = "Sandstone brick stairs", slab = "Sandstone brick slab", ridge = "Sandstone bricks", flat = true, parapet = "Sandstone brick wall", door = "Palm door",
114 window = "Air", street = "Sandstone", square = "Sandstone bricks", well = "Sandstone bricks", rim = "Sandstone brick wall", post = "Sandstone bricks",
115 ground = "Sand", fill = "Sand",
116 jobs = { "Mason", "Weaver", "Herbalist", "Smith", "Wayfinder", "Baker", "Chandler", "Tanner" },
117 plants = { "Cactus", "Dry bush" },
118 },
119 badlands = {
120 wall = "Red sandstone", frame = "Terracotta", floor = "Redwood planks", base = "Terracotta",
121 roof = "Terracotta", stairs = "Terracotta stairs", slab = "Terracotta slab", ridge = "Terracotta", flat = true, parapet = "Red sandstone wall", door = "Redwood door",
122 window = "Air", street = "Red sand", square = "Red sandstone", well = "Red sandstone", rim = "Red sandstone wall", post = "Fence",
123 ground = "Red sand", fill = "Red sand",
124 jobs = { "Mason", "Smith", "Tanner", "Wayfinder", "Chandler", "Herbalist", "Weaver" },
125 plants = { "Dry bush", "Cactus" },
126 },
127 savanna = {
128 wall = "Terracotta", frame = "Palm log", floor = "Palm planks", base = "Red sandstone",
129 roof = "Hay", stairs = "Hay stairs", slab = "Hay slab", ridge = "Hay", door = "Palm door",
130 window = "Air", street = "Gravel", square = "Red sandstone", well = "Red sandstone", rim = "Red sandstone wall", post = "Fence",
131 ground = "Grass", fill = "Dirt",
132 jobs = { "Grower", "Tanner", "Bowyer", "Weaver", "Baker", "Beekeeper", "Wayfinder", "Smith" },
133 plants = { "Dry bush", "Tall grass" },
134 },
135 marsh = {
136 wall = "Willow planks", frame = "Willow log", floor = "Willow planks", base = "Willow log", raised = true,
137 roof = "Hay", stairs = "Hay stairs", slab = "Hay slab", ridge = "Willow log", door = "Willow door",
138 window = "Glass", street = "Willow planks", square = "Willow planks", well = "Mossy brick", rim = "Mossy brick wall", post = "Fence",
139 ground = "Grass", fill = "Dirt",
140 jobs = { "Angler", "Herbalist", "Grower", "Carpenter", "Chandler", "Wayfinder", "Weaver" },
141 plants = { "Reeds", "Fern", "Tall grass" },
142 },
143 beach = {
144 wall = "Sandstone", frame = "Palm log", floor = "Palm planks", base = "Sandstone",
145 roof = "Palm planks", stairs = "Palm stairs", slab = "Palm slab", ridge = "Palm log", door = "Palm door",
146 window = "Glass", street = "Sandstone", square = "Sandstone bricks", well = "Sandstone bricks", rim = "Sandstone brick wall", post = "Fence",
147 ground = "Sand", fill = "Sand",
148 jobs = { "Angler", "Mason", "Weaver", "Baker", "Wayfinder", "Chandler", "Bowyer" },
149 plants = { "Reeds", "Dry bush" },
150 },
151 maple = {
152 wall = "Maple planks", frame = "Maple log", floor = "Planks", base = "Cobblestone",
153 roof = "Slate", stairs = "Slate stairs", slab = "Slate slab", ridge = "Maple log", door = "Maple door",
154 window = "Glass", street = "Gravel", square = "Cobblestone", well = "Cobblestone", rim = "Cobblestone wall", post = "Fence",
155 ground = "Grass", fill = "Dirt",
156 jobs = { "Carpenter", "Beekeeper", "Baker", "Herbalist", "Grower", "Chandler", "Wayfinder", "Bowyer", "Weaver" },
157 plants = { "Fern", "Orange flower", "Tall grass", "Brown mushroom" },
158 },
159 redwood = {
160 wall = "Redwood planks", frame = "Redwood log", floor = "Redwood planks", base = "Mossy brick",
161 roof = "Planks", stairs = "Oak stairs", slab = "Oak slab", ridge = "Redwood log", door = "Redwood door",
162 window = "Glass", street = "Gravel", square = "Mossy brick", well = "Mossy brick", rim = "Mossy brick wall", post = "Fence",
163 ground = "Grass", fill = "Dirt",
164 jobs = { "Carpenter", "Bowyer", "Tanner", "Herbalist", "Wayfinder", "Angler", "Chandler", "Smith" },
165 plants = { "Fern", "Red mushroom", "Brown mushroom", "Bush" },
166 },
167 rainforest = {
168 wall = "Mahogany planks", frame = "Mahogany log", floor = "Mahogany planks", base = "Mossy cobblestone", raised = true,
169 roof = "Hay", stairs = "Hay stairs", slab = "Hay slab", ridge = "Mahogany log", door = "Mahogany door",
170 window = "Air", street = "Mahogany planks", square = "Mossy cobblestone", well = "Mossy cobblestone", rim = "Mossy cobblestone wall", post = "Fence",
171 ground = "Grass", fill = "Dirt",
172 jobs = { "Herbalist", "Grower", "Beekeeper", "Bowyer", "Weaver", "Wayfinder", "Chandler", "Angler" },
173 plants = { "Orchid", "Fern", "Bush", "Tall grass" },
174 },
175 swamp = {
176 wall = "Cypress planks", frame = "Cypress log", floor = "Cypress planks", base = "Cypress log", raised = true,
177 roof = "Mud bricks", stairs = "Mud brick stairs", slab = "Mud brick slab", ridge = "Cypress log", door = "Cypress door",
178 window = "Glass", street = "Cypress planks", square = "Cypress planks", well = "Mud bricks", rim = "Mud brick wall", post = "Fence",
179 ground = "Grass", fill = "Dirt",
180 jobs = { "Angler", "Herbalist", "Chandler", "Tanner", "Grower", "Wayfinder", "Carpenter" },
181 plants = { "Reeds", "Fern", "Tall grass" },
182 },
183 blossom = {
184 wall = "Plum planks", frame = "Plum log", floor = "Plum planks", base = "Smooth stone",
185 roof = "Polished slate", stairs = "Polished slate stairs", slab = "Polished slate slab", ridge = "Plum log", door = "Plum door",
186 window = "Glass", street = "Gravel", square = "Smooth stone", well = "Stone bricks", rim = "Stone brick wall", post = "Fence",
187 ground = "Grass", fill = "Dirt",
188 jobs = { "Beekeeper", "Grower", "Weaver", "Herbalist", "Baker", "Chandler", "Wayfinder", "Carpenter" },
189 plants = { "Bluebells", "Pink flower", "Fallen petals", "Tall grass" },
190 },
191}
192
193-- Which style for each kind of land (world.biome).
194local BY_BIOME: { [string]: string } = {
195 island = "meadow", meadow = "meadow", forest = "forest", birchwood = "birch", pinewood = "pine",
196 snowfield = "snow", mountain = "mountain", desert = "desert", savanna = "savanna", marsh = "marsh", beach = "beach",
197 maplewood = "maple", redwoods = "redwood", badlands = "badlands", flower_fields = "meadow", heath = "meadow",
198 rainforest = "rainforest", swamp = "swamp", blossom_grove = "blossom",
199}
200
201-- Where the houses stand along the street, and the field beyond its end.
202local SLOTS: { { number } } = {
203 { -6, -1 }, { 6, 1 }, { 6, -1 }, { -6, 1 }, { -14, -1 }, { 14, 1 }, { 14, -1 }, { -14, 1 },
204}
205local STREET = 19 -- the street runs from -STREET to STREET
206local FIELD = { 21, 27 } -- and the field from here to here, beyond its +u end
207local DEPTH = 7 -- from the street's middle to a house's middle
208
209-- what may be dug, built over or leveled
210local NATURAL: { [string]: boolean } = {
211 Grass = true, Dirt = true, Stone = true, Sand = true, Gravel = true, Snow = true, Sandstone = true, Slate = true, Ice = true, Water = true,
212 ["Red sand"] = true, ["Red sandstone"] = true, Terracotta = true, Granite = true, Marble = true, Basalt = true,
213 ["Tall grass"] = true, Fern = true, Bush = true, ["Dry bush"] = true, Cactus = true, Reeds = true, Glowcap = true,
214 ["Red flower"] = true, ["Blue flower"] = true, ["White flower"] = true, ["Violet flower"] = true,
215 ["Orange flower"] = true, ["Yellow flower"] = true, ["Pink flower"] = true, Lavender = true, Heather = true, Clover = true,
216 ["Brown mushroom"] = true, ["Red mushroom"] = true,
217 Mud = true, Vines = true, Orchid = true, ["Hanging moss"] = true, ["Lily pad"] = true, ["Fallen petals"] = true, Bluebells = true,
218 Kelp = true, ["Sea grass"] = true,
219 ["Coal ore"] = true, ["Copper ore"] = true, ["Iron ore"] = true, ["Gold ore"] = true,
220}
221-- what stands on the ground or hangs over it, rather than being it
222local STANDING: { [string]: boolean } = {
223 ["Tall grass"] = true, Fern = true, Bush = true, ["Dry bush"] = true, Cactus = true, Reeds = true,
224 ["Red flower"] = true, ["Blue flower"] = true, ["White flower"] = true, ["Violet flower"] = true,
225 ["Orange flower"] = true, ["Yellow flower"] = true, ["Pink flower"] = true, Lavender = true, Heather = true, Clover = true,
226 ["Brown mushroom"] = true, ["Red mushroom"] = true,
227 Vines = true, Orchid = true, ["Hanging moss"] = true, ["Lily pad"] = true, ["Fallen petals"] = true, Bluebells = true,
228 Kelp = true, ["Sea grass"] = true,
229}
230-- every wood's trees are natural, and stand on the ground
231for _, wood in { "Oak", "Birch", "Pine", "Maple", "Willow", "Palm", "Redwood", "Mahogany", "Cypress", "Plum" } do
232 NATURAL[wood .. " log"] = true
233 STANDING[wood .. " log"] = true
234end
235for _, leaves in {
236 "Oak leaves", "Birch leaves", "Pine needles", "Maple leaves", "Willow leaves", "Palm fronds", "Redwood needles",
237 "Mahogany leaves", "Cypress leaves", "Plum blossom",
238} do
239 NATURAL[leaves] = true
240 STANDING[leaves] = true
241end
242
243-- Whether a block or item is in this world: roof tiles, furnaces and
244-- anvils come from other creations.
245local here: { [string]: boolean } = {}
246local function exists(name: string): boolean
247 if here[name] == nil then
248 here[name] = (pcall(world.kind, name))
249 end
250 return here[name] == true
251end
252
253-- the first of these that's in the world
254local function first(names: { string }): string?
255 for _, n in names do
256 if exists(n) then
257 return n
258 end
259 end
260 return nil
261end
262
263-- the same number from 0 to 1 every time, for a place and a purpose
264local function hash(x: number, z: number, n: number): number
265 local h = (math.floor(x) * 73856093 + math.floor(z) * 19349663 + n * 83492791) % 1000003
266 return (h * 7919 % 1000003) / 1000003
267end
268
269-- A place in a village, by along (u), up from the street (h) and across (w).
270local function spot(v: Village, u: number, h: number, w: number): vector
271 if v.along_x then
272 return vector.create(v.at.x + u, v.at.y + h, v.at.z + w)
273 end
274 return vector.create(v.at.x + w, v.at.y + h, v.at.z + u)
275end
276
277-- The middle of a block, a little above its floor, for walking to.
278local function stand(v: Village, u: number, h: number, w: number): vector
279 return spot(v, u, h, w) + vector.create(0.5, 0.05, 0.5)
280end
281
282-- A place in a house, by across it (a, the same way as u), up (h) and in
283-- toward the street (b: 2 is the front wall, 3 the step outside it).
284local function house_spot(v: Village, hs: House, a: number, h: number, b: number): vector
285 return spot(v, hs.u + a, h, hs.side * (DEPTH - b))
286end
287
288-- the way a block that turns faces, toward the street from a house
289local function facing(v: Village, hs: House): string
290 if v.along_x then
291 return if hs.side < 0 then "south" else "north"
292 end
293 return if hs.side < 0 then "east" else "west"
294end
295
296-- and the way away from the street, toward a house's back wall
297local function away(v: Village, hs: House): string
298 if v.along_x then
299 return if hs.side < 0 then "north" else "south"
300 end
301 return if hs.side < 0 then "west" else "east"
302end
303
304-- the colors a house's bed may be
305local BEDS = { "Red", "Blue", "Green", "Yellow", "Light blue", "Brown", "Orange", "Purple", "Cyan", "Pink", "Lime", "Gray" }
306
307-- The height a house's floor is at: a block up on raised floors.
308local function floor_h(v: Village): number
309 return if STYLES[v.style].raised then 1 else 0
310end
311
312-- Where a villager goes in their day, for walking: inside their house,
313-- in its doorway, on the step outside, at their work by the door, out on
314-- the street in front, and by the well.
315local function places(v: Village, i: number): { [string]: vector }
316 local hs = v.houses[i]
317 local f = floor_h(v)
318 local well = (i % 4) - 1.5
319 return {
320 inside = house_spot(v, hs, 0, f + 1, 0) + vector.create(0.5, 0.05, 0.5),
321 door = house_spot(v, hs, 0, f + 1, 2) + vector.create(0.5, 0.05, 0.5),
322 porch = house_spot(v, hs, 0, 1, 3) + vector.create(0.5, 0.05, 0.5),
323 work = house_spot(v, hs, -1, 1, 3) + vector.create(0.5, 0.05, 0.5),
324 station = house_spot(v, hs, -2, 1, 3) + vector.create(0.5, 0.5, 0.5),
325 street = stand(v, hs.u, 1, 0),
326 well = stand(v, well * 1.4, 1, if i % 2 == 0 then 2 else -2),
327 gate = stand(v, FIELD[1] - 1, 1, 0),
328 field = stand(v, FIELD[1] + 1 + (i % 4), 1, if i % 2 == 0 then 1 else -1),
329 }
330end
331
332-- ---- finding a place ----
333
334-- The ground at a column, under any plant or tree on it; and what it is.
335local function ground(x: number, z: number): (number, string)
336 local y = world.top(x, z)
337 for _ = 1, 24 do
338 local b = world.block(vector.create(x, y, z))
339 if not STANDING[b] then
340 return y, b
341 end
342 y -= 1
343 end
344 return y, world.block(vector.create(x, y, z))
345end
346
347local SEA = 19
348
349-- Whether a village fits with its middle at x, z, running along x or z, and
350-- if so the street's level and the style. Only natural, dry ground that's
351-- already there will do, nearly level.
352local function fits(x: number, z: number, along_x: boolean, field: boolean): (number?, string?)
353 local biome = world.biome(vector.create(x, 0, z))
354 local style = BY_BIOME[biome]
355 if style == nil then
356 return nil, nil
357 end
358 local heights = {}
359 local v: Village = { id = "", name = "", at = vector.create(x, 0, z), along_x = along_x, style = style, houses = {}, field = field, built = false }
360 local reach = if field then FIELD[2] else STREET
361 for _, u in { -STREET, -10, 0, 10, STREET, reach } do
362 for _, w in { -10, -4, 0, 4, 10 } do
363 local p = spot(v, u, 0, w)
364 local y, b = ground(p.x, p.z)
365 if b == "Water" or b == "Ice" or b == "Air" or not NATURAL[b] or y <= SEA then
366 return nil, nil
367 end
368 table.insert(heights, y)
369 end
370 end
371 table.sort(heights)
372 if heights[#heights] - heights[1] > 5 then
373 return nil, nil
374 end
375 -- on the island its land says what it is
376 if biome == "island" then
377 local _, b = ground(x, z)
378 if b == "Sand" then
379 style = "beach"
380 elseif b == "Snow" then
381 style = "snow"
382 end
383 end
384 return heights[(#heights + 1) // 2], style
385end
386
387-- Names for villages: a first half and a second, the same for a place.
388local FIRST = {
389 "Ash", "Bram", "Clover", "Dun", "Elder", "Fern", "Gold", "Hollow", "Juniper", "Kettle", "Lark", "Marrow", "Nettle", "Oat",
390 "Pebble", "Quill", "Rush", "Sorrel", "Thistle", "Willow", "Yarrow", "Briar", "Copper", "Dapple", "Ember", "Fallow", "Honey", "Moss",
391}
392local SECOND = {
393 "brook", "by", "combe", "dale", "field", "ford", "gate", "ham", "hollow", "hurst", "ley", "mere", "stead", "ton", "wick", "well", "bury", "thorpe",
394}
395-- and for the folk who live in them
396local FOLK = {
397 "Alder", "Bramble", "Briony", "Clem", "Dell", "Edda", "Fen", "Garnet", "Hollis", "Isla", "Jory", "Kit", "Linden", "Marlo", "Nell", "Odo",
398 "Perrin", "Quince", "Rue", "Sorrel", "Tamsin", "Ula", "Vesper", "Wren", "Yara", "Zeb", "Ansel", "Bea", "Cass", "Dov", "Elin", "Faye",
399 "Gil", "Hob", "Ines", "Jem", "Kerra", "Lio", "Maude", "Nico", "Orla", "Pip", "Rosalind", "Sim", "Tobin", "Una", "Vale", "Wynn",
400}
401
402local function pick(list: { string }, x: number, z: number, n: number): string
403 return list[math.floor(hash(x, z, n) * #list) + 1]
404end
405
406-- Lays out a new village at x, z: its houses, who lives in each and its name.
407local function plan_village(id: string, x: number, y: number, z: number, along_x: boolean, style: string, field: boolean): Village
408 local st = STYLES[style]
409 local count = 4 + math.floor(hash(x, z, 5) * 4) -- 4 to 7 houses
410 local jobs: { string } = {}
411 -- the likeliest jobs first, shuffled a little
412 local pool = table.clone(st.jobs)
413 for i = #pool, 2, -1 do
414 local j = math.floor(hash(x, z, 40 + i) * i) + 1
415 if j < i - 2 then
416 j = i - 2
417 end
418 pool[i], pool[j] = pool[j], pool[i]
419 end
420 for i = 1, math.min(count, #pool) do
421 table.insert(jobs, pool[i])
422 end
423 local houses: { House } = {}
424 local used: { [string]: boolean } = {}
425 for i, job in jobs do
426 local name = pick(FOLK, x, z, 60 + i)
427 local k = 0
428 while used[name] and k < 10 do
429 k += 1
430 name = pick(FOLK, x + k, z, 60 + i)
431 end
432 used[name] = true
433 table.insert(houses, { job = job, name = name, u = SLOTS[i][1], side = SLOTS[i][2] })
434 end
435 local has_grower = table.find(jobs, "Grower") ~= nil
436 return {
437 id = id,
438 name = pick(FIRST, x, z, 1) .. pick(SECOND, x, z, 2),
439 at = vector.create(x, y, z),
440 along_x = along_x,
441 style = style,
442 houses = houses,
443 field = field and has_grower,
444 built = false,
445 }
446end
447
448-- ---- building ----
449
450-- A piece of work: level a column, or put a block down.
451export type Work = { kind: string, at: vector, name: string?, facing: string?, top: string?, fill: string? }
452
453-- Lays out everything a village is, to be done a little at a time.
454local function plan_build(v: Village): { Work }
455 local st = STYLES[v.style]
456 local work: { Work } = {}
457 local function put(at: vector, name: string?, face: string?)
458 if name then
459 table.insert(work, { kind = "block", at = at, name = name, facing = face })
460 end
461 end
462 local function block(u: number, h: number, w: number, name: string?, face: string?)
463 put(spot(v, u, h, w), name, face)
464 end
465 -- the ground, level, and clear of trees
466 local reach = if v.field then FIELD[2] + 1 else STREET + 1
467 for u = -STREET - 1, reach do
468 for w = -11, 11 do
469 table.insert(work, { kind = "level", at = spot(v, u, 0, w), top = st.ground, fill = st.fill })
470 end
471 end
472 local roof = first({ st.roof, "Planks" }) or "Planks"
473 local ridge = first({ st.ridge, roof }) or roof
474 -- the street, the square and the well
475 -- (what's put down first stays, so the well's cells are left to it)
476 local function by_well(u: number, w: number): boolean
477 return math.abs(u) <= 1 and math.abs(w) <= 1
478 end
479 for u = -STREET, STREET do
480 for w = -1, 1 do
481 if not by_well(u, w) then
482 block(u, 0, w, if math.abs(u) <= 3 then st.square else st.street)
483 end
484 end
485 end
486 for u = -3, 3 do
487 for w = -3, 3 do
488 if not by_well(u, w) and math.abs(w) > 1 then
489 block(u, 0, w, st.square)
490 end
491 end
492 end
493 local rim = first({ st.rim, st.well }) or st.well
494 local slab = first({ st.slab, "Oak slab" }) or roof
495 for u = -1, 1 do
496 for w = -1, 1 do
497 if u == 0 and w == 0 then
498 for h = -3, 0 do
499 block(u, h, w, "Water")
500 end
501 else
502 for h = -3, 0 do
503 block(u, h, w, st.well)
504 end
505 -- a low wall round its top
506 block(u, 1, w, rim)
507 end
508 end
509 end
510 -- posts at the corners, under a cover
511 for _, c in { { -1, -1 }, { 1, -1 }, { -1, 1 }, { 1, 1 } } do
512 block(c[1], 2, c[2], rim)
513 block(c[1], 3, c[2], rim)
514 end
515 for u = -1, 1 do
516 for w = -1, 1 do
517 block(u, 4, w, if st.flat then roof else slab)
518 end
519 end
520 block(0, 3, 0, "Lantern")
521 -- lamps along the street, and a plant here and there
522 for _, l in { { -STREET, -2 }, { -STREET, 2 }, { STREET, -2 }, { STREET, 2 }, { -10, -2 }, { 10, 2 } } do
523 block(l[1], 1, l[2], st.post)
524 block(l[1], 2, l[2], st.post)
525 block(l[1], 3, l[2], "Lantern")
526 end
527 if #st.plants > 0 then
528 for u = -STREET, STREET, 3 do
529 for _, w in { -3, 3 } do
530 local p = spot(v, u, 1, w)
531 if math.abs(u) > 4 and hash(p.x, p.z, 7) < 0.3 then
532 local plant = st.plants[math.floor(hash(p.x, p.z, 8) * #st.plants) + 1]
533 block(u, 1, w, plant)
534 end
535 end
536 end
537 end
538 -- the houses
539 local f = floor_h(v)
540 local stairs = first({ st.stairs, "Oak stairs" }) or roof
541 local door = first({ st.door, "Oak door" }) or "Air"
542 local parapet = first({ st.parapet or st.frame, st.frame }) or st.frame
543 for _, hs in v.houses do
544 local function at(a: number, h: number, b: number, name: string?, face: string?)
545 put(house_spot(v, hs, a, h, b), name, face)
546 end
547 local front, back = facing(v, hs), away(v, hs)
548 -- the floor, on stones (or on posts, raised)
549 for a = -2, 2 do
550 for b = -2, 2 do
551 local edge = math.abs(a) == 2 or math.abs(b) == 2
552 if f > 0 then
553 -- posts at the corners, on the ground
554 at(a, 0, b, if math.abs(a) == 2 and math.abs(b) == 2 then st.base else st.fill)
555 at(a, 1, b, if edge then st.frame else st.floor)
556 else
557 at(a, 0, b, if edge then st.base else st.floor)
558 end
559 end
560 end
561 -- the walls, with corners, a door and windows
562 for h = f + 1, f + 3 do
563 for a = -2, 2 do
564 for b = -2, 2 do
565 local corner = math.abs(a) == 2 and math.abs(b) == 2
566 local edge = math.abs(a) == 2 or math.abs(b) == 2
567 if corner then
568 at(a, h, b, st.frame)
569 elseif edge then
570 local doorway = a == 0 and b == 2 and h < f + 3
571 local window = h == f + 2 and ((b == 0 and math.abs(a) == 2) or (a == 0 and b == -2) or (math.abs(a) == 1 and b == 2))
572 if doorway then
573 -- the door brings its upper half, so that's left clear for it
574 if h == f + 1 then
575 at(a, h + 1, b, "Air")
576 at(a, h, b, door, front)
577 end
578 elseif window then
579 at(a, h, b, st.window)
580 else
581 at(a, h, b, if h == f + 3 then st.frame else st.wall)
582 end
583 else
584 at(a, h, b, "Air")
585 end
586 end
587 end
588 end
589 -- a step up to a raised floor
590 if f > 0 then
591 at(0, 1, 3, "Air")
592 end
593 -- the roof
594 local r = f + 4
595 if st.flat then
596 for a = -2, 2 do
597 for b = -2, 2 do
598 at(a, r, b, roof)
599 if math.abs(a) == 2 or math.abs(b) == 2 then
600 at(a, r + 1, b, parapet)
601 end
602 end
603 end
604 else
605 -- stairs up both slopes to a beam along the ridge, capped with
606 -- slabs, and slabs out along the eaves
607 for step = 0, 2 do
608 local b = 3 - step
609 for a = -3, 3 do
610 at(a, r + step, b, stairs, back)
611 at(a, r + step, -b, stairs, front)
612 end
613 -- the gable ends, and the attic's air
614 if b > 1 then
615 for bb = -(b - 1), b - 1 do
616 for a = -2, 2 do
617 at(a, r + step, bb, if math.abs(a) == 2 then st.wall else "Air")
618 end
619 end
620 end
621 end
622 for a = -3, 3 do
623 at(a, r + 2, 0, ridge)
624 at(a, r + 3, 0, slab)
625 at(a, r, 4, slab)
626 at(a, r, -4, slab)
627 end
628 end
629 -- inside: a light, a bed by the back wall and what they keep
630 at(0, f + 3, 0, "Lantern")
631 local job = hs.job
632 local inside = { "Crate", "Barrel" }
633 local station = { "Crate" }
634 local on_top: string? = nil
635 local def = trades.JOBS[job]
636 if def then
637 inside = def.inside
638 station = def.station
639 on_top = def.on_top
640 end
641 local spot_at = house_spot(v, hs, 0, 0, 0)
642 local color = BEDS[math.floor(hash(spot_at.x, spot_at.z, 11) * #BEDS) + 1]
643 at(1, f + 1, 0, first({ color .. " bed", "Red bed" }), back)
644 at(0, f + 1, 0, first({ color .. " rug" }))
645 at(-1, f + 1, -1, first({ inside[1] or "Crate", "Crate" }))
646 at(0, f + 1, -1, first({ inside[2] or "Barrel", "Barrel" }))
647 at(-1, f + 1, 0, first({ inside[3] or "Table", "Crate" }))
648 if (inside[3] or "Table") == "Table" then
649 at(-1, f + 2, 0, "Candle")
650 end
651 at(1, f + 1, 1, if inside[4] then first({ inside[4], "Stool" }) else nil)
652 -- their work, by the door
653 at(-2, 1, 3, first(station) or "Crate", front)
654 at(-2, 2, 3, on_top)
655 -- the path to the street
656 for b = 4, 6 do
657 at(0, 0, b, st.street)
658 at(0, 1, b, "Air")
659 end
660 at(0, 0, 3, st.street)
661 end
662 -- the field: crops either side of a ditch, fenced
663 if v.field then
664 for u = FIELD[1] - 1, FIELD[2] + 1 do
665 for w = -4, 4 do
666 local edge = u == FIELD[1] - 1 or u == FIELD[2] + 1 or math.abs(w) == 4
667 if edge then
668 local gate = u == FIELD[1] - 1 and w == 0
669 block(u, 1, w, if gate then "Air" else "Fence")
670 elseif w == 0 and u > FIELD[1] and u < FIELD[2] then
671 block(u, 0, w, "Water")
672 elseif w ~= 0 then
673 block(u, 0, w, "Farmland")
674 block(u, 1, w, if w < 0 then "Wheat" else "Carrots")
675 end
676 end
677 end
678 for u = STREET + 1, FIELD[1] - 1 do
679 block(u, 0, 0, st.street)
680 end
681 end
682 return work
683end
684
685-- The crops in a village's field, for growing back once they're picked.
686local function crops(v: Village): { { any } }
687 local list = {}
688 if not v.field then
689 return list
690 end
691 for u = FIELD[1], FIELD[2] do
692 for w = -3, 3 do
693 if w ~= 0 then
694 table.insert(list, { spot(v, u, 1, w), if w < 0 then "Wheat" else "Carrots" })
695 end
696 end
697 end
698 return list
699end
700
701-- Does one piece of work, and says how much it took.
702local function step(w: Work): number
703 if w.kind == "level" then
704 local at = w.at
705 local top = world.top(at.x, at.z)
706 local cost = 1
707 -- clear what stands over the level, up through any tree
708 for y = math.max(top, at.y + 1), at.y + 1, -1 do
709 local p = vector.create(at.x, y, at.z)
710 local b = world.block(p)
711 cost += 1
712 if NATURAL[b] then
713 world.set_block(p, "Air")
714 end
715 end
716 -- and build up under it, or cut it down to it
717 local y, b = ground(at.x, at.z)
718 if y > at.y then
719 y = at.y
720 end
721 for h = y + 1, at.y - 1 do
722 world.set_block(vector.create(at.x, h, at.z), w.fill or "Dirt")
723 cost += 1
724 end
725 local here_b = world.block(at)
726 if NATURAL[here_b] or here_b == "Air" or here_b == "Water" then
727 world.set_block(at, w.top or "Grass")
728 end
729 return cost + 2
730 end
731 local name = w.name or "Air"
732 local b = world.block(w.at)
733 if b == name then
734 return 1
735 end
736 if NATURAL[b] or b == "Air" then
737 if exists(name) then
738 world.set_block(w.at, name, w.facing)
739 end
740 end
741 return 2
742end
743
744return {
745 STYLES = STYLES,
746 BY_BIOME = BY_BIOME,
747 STREET = STREET,
748 FIELD = FIELD,
749 exists = exists,
750 hash = hash,
751 spot = spot,
752 house_spot = house_spot,
753 floor_h = floor_h,
754 places = places,
755 fits = fits,
756 plan_village = plan_village,
757 plan_build = plan_build,
758 crops = crops,
759 step = step,
760}