cantiere.gno
41.44 Kb · 1372 lines
1// Package cantiere (v2, "Kasaba Dili") is a Sinopia engine: an isometric
2// maquette of an imaginary town on a drafting sheet, built from a vocabulary
3// of architectural elements — gabled houses, crenellated towers, domes, a
4// bell tower, arcades, gardens with cypresses, a square with a fountain and
5// building sites in red scaffolding — on six plans (Hill Town, Terraces,
6// Courtyard, Coast, Citadel, Ruin), in six materials and four lights. The
7// title block of the sheet carries the token number ("TAV. 42").
8//
9// It is the successor of Cantiere v1 (p/.../art/cantiere/v1, which stays
10// untouched): the same FNV-1a + splitmix64 streams, a new town. Three streams:
11// seed+"|traits" (the trait draws), seed+"|town" (the 6 x 6 parcel plan, which
12// Traits reads too, so Landmark and Tallest come without rendering) and
13// seed+"|art" (colours and details). frontend/src/engines/cantierev2.ts is a
14// byte-identical TypeScript twin; cantiere_test.gno pins golden vectors that
15// the twin asserts too.
16//
17// The art reads Input.TokenID (the title block). It never reads heights or
18// moves, so the engine is not living. Pure integer maths: every division is a
19// floor division (fdiv), as in the twin.
20package cantiere
21
22import (
23 "errors"
24 "strconv"
25 "strings"
26
27 "gno.land/p/g1jz6j9hj6rdf5ltl2kcrpa4jwtdh3w30z0yul06/sinopia4/engine/v2"
28)
29
30// TraitTypes in the order Traits returns them.
31var traitTypes = []string{"Plan", "Material", "Light", "Scaffolding", "Landmark", "Event", "Tallest", "Rarity"}
32
33// MaxSVGBytes: the largest output over 10,000 default seeds is about 22 KB
34// (docs/engines/cantiere-v2.md); the 28 KB budget.
35const MaxSVGBytes = 28672
36
37// ParamsSchema is the Studio form. Studio sends JSON.stringify of the values
38// in this property order; ValidateParams accepts exactly that or "".
39const ParamsSchema = `{"type":"object","properties":{` +
40 `"plan":{"type":"string","title":"Plan","enum":["Seeded","Hill Town","Terraces","Courtyard","Coast","Citadel","Ruin"],"default":"Seeded"},` +
41 `"material":{"type":"string","title":"Material","enum":["Seeded","Tufa","Brick","Travertine","Painted Plaster","Whitewash","Basalt"],"default":"Seeded"},` +
42 `"light":{"type":"string","title":"Light","enum":["Seeded","Morning","Noon","Evening","Night"],"default":"Seeded"}}}`
43
44// DefaultParams is the canonical form of the defaults; it renders exactly
45// like "".
46const DefaultParams = `{"plan":"Seeded","material":"Seeded","light":"Seeded"}`
47
48// Manifest describes the engine. Author and FeeBps are set by the adapter.
49func Manifest() engine.Manifest {
50 return engine.Manifest{
51 Name: "Cantiere · Kasaba Dili",
52 Version: "2.0.0",
53 Description: "Cantiere v2: an isometric maquette of an imaginary town on a drafting sheet, built from a " +
54 "vocabulary of architecture — gabled houses, towers, domes, a bell tower, arcades, gardens, a fountain " +
55 "square and building sites in red scaffolding — on hill, terrace, courtyard, coast, citadel or ruin plans. " +
56 "The title block carries the token number.",
57 License: "CC-BY-NC-4.0",
58 ParamsSchema: ParamsSchema,
59 TraitTypes: append([]string{}, traitTypes...),
60 Living: false,
61 MaxSVGBytes: MaxSVGBytes,
62 Kind: engine.KindSVG,
63 }
64}
65
66// Config is a parsed params string; -1 means drawn from the seed.
67type Config struct {
68 Plan, Material, Light int
69}
70
71// Defaults is the showcase look (params "").
72func Defaults() Config { return Config{Plan: -1, Material: -1, Light: -1} }
73
74var errParams = errors.New(`cantierev2: params must be "" or ` +
75 `{"plan":<enum>,"material":<enum>,"light":<enum>} in that order, without spaces`)
76
77// ParseParams accepts "" or the canonical JSON Studio produces, nothing else.
78func ParseParams(params string) (Config, error) {
79 cfg := Defaults()
80 if params == "" {
81 return cfg, nil
82 }
83 v, rest, ok := strValue(params, `{"plan":"`)
84 if !ok {
85 return cfg, errParams
86 }
87 if cfg.Plan, ok = enumIndex(v, plans); !ok {
88 return cfg, errParams
89 }
90 if v, rest, ok = strValue(rest, `","material":"`); !ok {
91 return cfg, errParams
92 }
93 if cfg.Material, ok = enumIndex(v, materials); !ok {
94 return cfg, errParams
95 }
96 if v, rest, ok = strValue(rest, `","light":"`); !ok {
97 return cfg, errParams
98 }
99 if cfg.Light, ok = enumIndex(v, lights); !ok {
100 return cfg, errParams
101 }
102 if rest != `"}` {
103 return cfg, errParams
104 }
105 return cfg, nil
106}
107
108// strValue expects s to start with prefix and returns the text up to the
109// next '"' and the remainder starting at that quote.
110func strValue(s, prefix string) (string, string, bool) {
111 if !strings.HasPrefix(s, prefix) {
112 return "", "", false
113 }
114 s = s[len(prefix):]
115 end := strings.IndexByte(s, '"')
116 if end < 0 {
117 return "", "", false
118 }
119 return s[:end], s[end:], true
120}
121
122func enumIndex(v string, table []option) (int, bool) {
123 if v == "Seeded" {
124 return -1, true
125 }
126 for i, o := range table {
127 if o.name == v {
128 return i, true
129 }
130 }
131 return 0, false
132}
133
134// ValidateParams accepts "" (the showcase defaults) and the canonical JSON.
135func ValidateParams(params string) error {
136 _, err := ParseParams(params)
137 return err
138}
139
140func mustConfig(params string) Config {
141 cfg, err := ParseParams(params)
142 if err != nil {
143 panic(err)
144 }
145 return cfg
146}
147
148// Traits returns the eight traits of in.Seed in TraitTypes order. It never
149// renders: six draws and the 36-parcel town plan.
150func Traits(in engine.Input) []engine.Trait {
151 t := TraitsOf(in.Seed, mustConfig(in.Params))
152 return []engine.Trait{
153 {Type: "Plan", Value: t.Plan},
154 {Type: "Material", Value: t.Material},
155 {Type: "Light", Value: t.Light},
156 {Type: "Scaffolding", Value: t.Scaffolding},
157 {Type: "Landmark", Value: t.Landmark},
158 {Type: "Event", Value: t.Event},
159 {Type: "Tallest", Value: t.Tallest},
160 {Type: "Rarity", Value: t.Rarity},
161 }
162}
163
164// Render returns the raw SVG for in.Seed, in.Params and in.TokenID.
165func Render(in engine.Input) string { return SVG(in.Seed, mustConfig(in.Params), in.TokenID) }
166
167// ---------------------------------------------------------------------------
168// Deterministic randomness: FNV-1a over the seed, then splitmix64.
169// ---------------------------------------------------------------------------
170
171type rng struct{ state uint64 }
172
173func newRNG(seed string) *rng {
174 h := uint64(14695981039346656037)
175 for i := 0; i < len(seed); i++ {
176 h ^= uint64(seed[i])
177 h *= 1099511628211
178 }
179 if h == 0 {
180 h = 1099511628211
181 }
182 return &rng{state: h}
183}
184
185func (r *rng) next() uint64 {
186 r.state += 0x9E3779B97F4A7C15
187 z := r.state
188 z = (z ^ (z >> 30)) * 0xBF58476D1CE4E5B9
189 z = (z ^ (z >> 27)) * 0x94D049BB133111EB
190 return z ^ (z >> 31)
191}
192
193// intn is in [0, n); one draw whenever n > 0.
194func (r *rng) intn(n int) int {
195 if n <= 0 {
196 return 0
197 }
198 return int(r.next() % uint64(n))
199}
200
201// between is in [a, b].
202func (r *rng) between(a, b int) int { return a + r.intn(b-a+1) }
203
204// fdiv is floor division (b > 0), as Math.floor(a / b) in the twin.
205func fdiv(a, b int) int {
206 q := a / b
207 if a%b != 0 && a < 0 {
208 q--
209 }
210 return q
211}
212
213func absInt(x int) int {
214 if x < 0 {
215 return -x
216 }
217 return x
218}
219
220func maxInt(a, b int) int {
221 if a > b {
222 return a
223 }
224 return b
225}
226
227func minInt(a, b int) int {
228 if a < b {
229 return a
230 }
231 return b
232}
233
234// ---------------------------------------------------------------------------
235// Trait tables. A weight is a percent; score feeds the rarity tier.
236// ---------------------------------------------------------------------------
237
238type option struct {
239 name string
240 weight int
241 score int
242}
243
244func pick(r *rng, table []option) int {
245 total := 0
246 for _, o := range table {
247 total += o.weight
248 }
249 x := r.intn(total)
250 for i, o := range table {
251 x -= o.weight
252 if x < 0 {
253 return i
254 }
255 }
256 return len(table) - 1
257}
258
259var (
260 plans = []option{{"Hill Town", 28, 0}, {"Terraces", 22, 0}, {"Courtyard", 18, 0}, {"Coast", 16, 1}, {"Citadel", 9, 3}, {"Ruin", 7, 3}}
261 materials = []option{{"Tufa", 22, 0}, {"Brick", 20, 0}, {"Travertine", 16, 0}, {"Painted Plaster", 20, 0}, {"Whitewash", 14, 0}, {"Basalt", 8, 2}}
262 lights = []option{{"Morning", 28, 0}, {"Noon", 24, 0}, {"Evening", 30, 0}, {"Night", 18, 2}}
263 scaffolds = []option{{"None", 24, 0}, {"Some", 46, 0}, {"Heavy", 30, 1}}
264 landmarks = []option{{"Bell Tower", 32, 0}, {"Dome", 30, 0}, {"Tower", 22, 0}, {"Dome and Bell Tower", 10, 1}, {"Twin Towers", 6, 2}}
265 events = []option{{"None", 58, 0}, {"Festival", 12, 2}, {"Fog", 12, 2}, {"Birds", 12, 2}, {"Crane", 6, 3}}
266)
267
268const (
269 pHill = iota
270 pTerraces
271 pCourtyard
272 pCoast
273 pCitadel
274 pRuin
275)
276
277const (
278 mPlaster = 3
279 mWhitewash = 4
280 mBasalt = 5
281)
282
283const (
284 lMorning = iota
285 lNoon
286 lEvening
287 lNight
288)
289
290const (
291 sSome = 1
292 sHeavy = 2
293)
294
295const (
296 eFestival = 1
297 eFog = 2
298 eBirds = 3
299 eCrane = 4
300)
301
302// Rarity tiers on the summed score.
303var tierCuts = []int{8, 6, 4, 2}
304var tierNames = []string{"Mythic", "Legendary", "Rare", "Uncommon"}
305
306// ---------------------------------------------------------------------------
307// The town plan (stream seed+"|town"; Traits reads it, never renders).
308// ---------------------------------------------------------------------------
309
310const n6 = 6 // cells per side
311
312const (
313 kHouse = iota
314 kTower
315 kDome
316 kBell
317 kArcade
318 kGarden
319 kSquare
320 kSite
321 kWall
322 kWater
323)
324
325// Weighted kinds of the free parcels: house, tower, arcade, garden, square;
326// then site (weight from the Scaffolding trait).
327var (
328 freeKinds = []int{kHouse, kTower, kArcade, kGarden, kSquare}
329 freeWeights = []int{50, 5, 9, 15, 6}
330 siteWeight = []int{0, 9, 20}
331 landmarkKinds = [][]int{{kBell}, {kDome}, {kTower}, {kDome, kBell}, {kTower, kTower}}
332)
333
334type draw struct {
335 plan, material, light, scaffold, landmark, event, score int
336}
337
338// deriveDraw: the draw order is part of the contract: changing it changes
339// every token.
340func deriveDraw(seed string, cfg Config) draw {
341 r := newRNG(seed + "|traits")
342 plan := pick(r, plans)
343 material := pick(r, materials)
344 light := pick(r, lights)
345 scaffold := pick(r, scaffolds)
346 landmark := pick(r, landmarks)
347 event := pick(r, events)
348 // params override after the draws, so the stream never shifts; a pinned trait scores 0
349 score := scaffolds[scaffold].score + landmarks[landmark].score + events[event].score
350 if cfg.Plan >= 0 {
351 plan = cfg.Plan
352 } else {
353 score += plans[plan].score
354 }
355 if cfg.Material >= 0 {
356 material = cfg.Material
357 } else {
358 score += materials[material].score
359 }
360 if cfg.Light >= 0 {
361 light = cfg.Light
362 } else {
363 score += lights[light].score
364 }
365 return draw{plan, material, light, scaffold, landmark, event, score}
366}
367
368type town struct {
369 t, kind, st []int // per cell i*6+j: terrain steps 0..3, kind, storeys
370 tallest int
371}
372
373func clampInt(v, lo, hi int) int {
374 if v < lo {
375 return lo
376 }
377 if v > hi {
378 return hi
379 }
380 return v
381}
382
383func terrain(plan, i, j int) int {
384 d := absInt(2*i-5) + absInt(2*j-5)
385 switch plan {
386 case pHill, pRuin:
387 return clampInt(fdiv(2*(10-d), 5), 0, 3)
388 case pTerraces:
389 return clampInt(fdiv(10-i-j, 3), 0, 3)
390 case pCoast:
391 if i+j >= 8 {
392 return 0
393 }
394 return clampInt(fdiv(9-i-j, 3), 1, 3)
395 case pCitadel:
396 if d <= 4 {
397 return 2
398 }
399 return 1
400 }
401 return 1
402}
403
404func storeys(r *rng, k int) int {
405 switch k {
406 case kHouse:
407 return r.between(1, 4)
408 case kTower:
409 return r.between(4, 7)
410 case kDome:
411 return r.between(2, 3)
412 case kBell:
413 return r.between(5, 7)
414 case kArcade:
415 return r.between(2, 3)
416 case kSite:
417 return r.between(1, 2)
418 }
419 return 0
420}
421
422// highest returns one of the free cells (kind -1) of among on the highest
423// ground, drawn from r; -1 if none is free.
424func highest(r *rng, t, kind, among []int) int {
425 best := -1
426 var list []int
427 for _, c := range among {
428 if kind[c] != -1 {
429 continue
430 }
431 if t[c] > best {
432 best = t[c]
433 list = list[:0]
434 }
435 if t[c] == best {
436 list = append(list, c)
437 }
438 }
439 if len(list) == 0 {
440 return -1
441 }
442 return list[r.intn(len(list))]
443}
444
445func buildTown(seed string, d draw) town {
446 r := newRNG(seed + "|town")
447 t := make([]int, 0, n6*n6)
448 kind := make([]int, 0, n6*n6)
449 st := make([]int, 0, n6*n6)
450 all := make([]int, 0, n6*n6)
451 // fixed parcels: the sea, the courtyard, the citadel's walls and corner towers
452 for i := 0; i < n6; i++ {
453 for j := 0; j < n6; j++ {
454 t = append(t, terrain(d.plan, i, j))
455 all = append(all, i*n6+j)
456 edgeI, edgeJ := i == 0 || i == n6-1, j == 0 || j == n6-1
457 k, s := -1, 0
458 if d.plan == pCoast && i+j >= 8 {
459 k = kWater
460 } else if d.plan == pCourtyard && i >= 2 && i <= 3 && j >= 2 && j <= 3 {
461 k = kGarden
462 if i == j {
463 k = kSquare
464 }
465 } else if d.plan == pCitadel && (edgeI || edgeJ) {
466 k, s = kWall, 2
467 if edgeI && edgeJ {
468 k, s = kTower, 4
469 }
470 }
471 kind = append(kind, k)
472 st = append(st, s)
473 }
474 }
475 // the landmark on the highest free ground; a second one beside it
476 lk := landmarkKinds[d.landmark]
477 first := highest(r, t, kind, all)
478 kind[first] = lk[0]
479 st[first] = storeys(r, lk[0])
480 if len(lk) > 1 {
481 i, j := fdiv(first, n6), first%n6
482 var nb []int
483 if i > 0 {
484 nb = append(nb, first-n6)
485 }
486 if j > 0 {
487 nb = append(nb, first-1)
488 }
489 if i < n6-1 {
490 nb = append(nb, first+n6)
491 }
492 if j < n6-1 {
493 nb = append(nb, first+1)
494 }
495 second := highest(r, t, kind, nb)
496 if second < 0 {
497 second = highest(r, t, kind, all)
498 }
499 kind[second] = lk[1]
500 if lk[1] == kTower {
501 st[second] = st[first] // twin towers match
502 } else {
503 st[second] = storeys(r, lk[1])
504 }
505 }
506 // the free parcels
507 sw := siteWeight[d.scaffold]
508 towers, sites := 0, 0
509 for c := 0; c < n6*n6; c++ {
510 if kind[c] != -1 {
511 continue
512 }
513 x := r.intn(85 + sw)
514 k := kSite
515 for q := 0; q < len(freeKinds); q++ {
516 x -= freeWeights[q]
517 if x < 0 {
518 k = freeKinds[q]
519 break
520 }
521 }
522 if k == kTower {
523 if towers >= 2 {
524 k = kHouse
525 }
526 towers++
527 }
528 if k == kSite {
529 sites++
530 }
531 kind[c] = k
532 st[c] = storeys(r, k)
533 }
534 // the Scaffolding trait is always visible: Some has a site, Heavy three
535 // (front-most houses become sites first)
536 need := 0
537 if d.scaffold == sHeavy {
538 need = 3
539 } else if d.scaffold == sSome {
540 need = 1
541 }
542 for s := 2*n6 - 2; s >= 0 && sites < need; s-- {
543 for i := minInt(s, n6-1); i >= 0 && s-i < n6 && sites < need; i-- {
544 c := i*n6 + s - i
545 if kind[c] == kHouse {
546 kind[c] = kSite
547 st[c] = r.between(1, 2)
548 sites++
549 }
550 }
551 }
552 // a ruin keeps a part of its storeys
553 tallest := 0
554 for c := 0; c < n6*n6; c++ {
555 if d.plan == pRuin && st[c] > 1 {
556 st[c] = r.between(1, st[c])
557 }
558 if st[c] > tallest {
559 tallest = st[c]
560 }
561 }
562 return town{t: t, kind: kind, st: st, tallest: tallest}
563}
564
565// TraitSet is the decoded traits of a seed; the SVG and the trait list both
566// read it, so picture and metadata cannot disagree.
567type TraitSet struct {
568 Plan, Material, Light, Scaffolding, Landmark, Event, Tallest, Rarity string
569 Score int
570}
571
572func traitsOf(d draw, tw town) TraitSet {
573 rarity := "Common"
574 for i, cut := range tierCuts {
575 if d.score >= cut {
576 rarity = tierNames[i]
577 break
578 }
579 }
580 tallest := strconv.Itoa(tw.tallest) + " storeys"
581 if tw.tallest == 1 {
582 tallest = "1 storey"
583 }
584 return TraitSet{
585 Plan: plans[d.plan].name, Material: materials[d.material].name, Light: lights[d.light].name,
586 Scaffolding: scaffolds[d.scaffold].name, Landmark: landmarks[d.landmark].name, Event: events[d.event].name,
587 Tallest: tallest, Rarity: rarity, Score: d.score,
588 }
589}
590
591// TraitsOf returns the decoded traits of a seed under cfg.
592func TraitsOf(seed string, cfg Config) TraitSet {
593 d := deriveDraw(seed, cfg)
594 return traitsOf(d, buildTown(seed, d))
595}
596
597// TavNumber is the sheet number in the title block: TokenID read as a
598// decimal, modulo 100,000 (core's supply cap is 10,000). Anything but ASCII
599// digits is 0, which prints "PROVA" (a proof; Studio renders use "0").
600func TavNumber(tokenID string) int {
601 v := 0
602 for i := 0; i < len(tokenID); i++ {
603 c := tokenID[i]
604 if c < '0' || c > '9' {
605 return 0
606 }
607 v = (v*10 + int(c-'0')) % 100000
608 }
609 return v
610}
611
612// ---------------------------------------------------------------------------
613// Colour
614// ---------------------------------------------------------------------------
615
616const hexDigits = "0123456789abcdef"
617
618func hx(c string, k int) int {
619 return strings.IndexByte(hexDigits, c[k])*16 + strings.IndexByte(hexDigits, c[k+1])
620}
621
622func hex2(b *strings.Builder, v int) {
623 b.WriteByte(hexDigits[v>>4])
624 b.WriteByte(hexDigits[v&15])
625}
626
627// shade scales each channel by f/100, rounded, capped at 255.
628func shade(c string, f int) string {
629 var b strings.Builder
630 b.WriteByte('#')
631 for k := 1; k < 7; k += 2 {
632 hex2(&b, minInt(255, fdiv(hx(c, k)*f+50, 100)))
633 }
634 return b.String()
635}
636
637// mix goes t % of the way from a to b, rounded.
638func mix(a, c string, t int) string {
639 var b strings.Builder
640 b.WriteByte('#')
641 for k := 1; k < 7; k += 2 {
642 hex2(&b, fdiv(hx(a, k)*(100-t)+hx(c, k)*t+50, 100))
643 }
644 return b.String()
645}
646
647// Light: left / right / top face factors.
648var lightFactors = [][]int{{100, 70, 114}, {92, 80, 120}, {72, 100, 108}, {46, 36, 58}}
649
650var (
651 skies = []string{"#f6e3c8", "#e7eef0", "#f2c49a", "#141a28"}
652 pastels = []string{"#e8b48a", "#e6cf8f", "#d98b74", "#c9d1b0", "#efd9b8", "#d4a5a0", "#bfc9cf"}
653 wallCols = []string{"#cdbf9a", "#b5623f", "#e6dcc4", "", "#f2efe8", "#6e6b66"}
654 roofCols = []string{"#b5523a", "#a9492f", "#c0603f"}
655 lanterns = []string{"#e8473a", "#f2c14e", "#3c8dbc", "#7bc043"}
656)
657
658type palette struct {
659 light int
660 memo map[string]string
661}
662
663func (p *palette) tint(c string) string {
664 switch p.light {
665 case lEvening:
666 return mix(c, "#e88a4a", 14)
667 case lNight:
668 return mix(c, "#1e2a48", 35)
669 }
670 return c
671}
672
673// face: 0 left, 1 right, 2 top.
674func (p *palette) face(c string, face int) string {
675 key := c + string(rune('0'+face))
676 if v, ok := p.memo[key]; ok {
677 return v
678 }
679 v := p.tint(shade(c, lightFactors[p.light][face]))
680 p.memo[key] = v
681 return v
682}
683
684// ---------------------------------------------------------------------------
685// Geometry: isometric cells of 52 x 26 px, storeys of 26 px; positions in
686// 1/20 cell.
687// ---------------------------------------------------------------------------
688
689const (
690 canvas = 704
691 ox = 364
692 oy = 338
693 storey = 26
694 cell = 20
695)
696
697func px(u, v int) int { return ox + fdiv((u-v)*13, 5) }
698func py(u, v, z int) int { return oy + fdiv((u+v)*13, 10) - z }
699
700func itoa(n int) string { return strconv.Itoa(n) }
701
702// sp writes n with a leading space unless it is negative.
703func sp(n int) string {
704 if n < 0 {
705 return strconv.Itoa(n)
706 }
707 return " " + strconv.Itoa(n)
708}
709
710// poly is "Mx y" then relative "l" moves over the flat point list; zero
711// moves are dropped.
712func poly(p []int, close bool) string {
713 var b strings.Builder
714 x, y := p[0], p[1]
715 b.WriteString("M" + itoa(x) + sp(y))
716 first := true
717 for k := 2; k < len(p); k += 2 {
718 dx, dy := p[k]-x, p[k+1]-y
719 if dx == 0 && dy == 0 {
720 continue
721 }
722 if first {
723 b.WriteString("l" + itoa(dx) + sp(dy))
724 } else {
725 b.WriteString(sp(dx) + sp(dy))
726 }
727 first = false
728 x, y = p[k], p[k+1]
729 }
730 if close {
731 b.WriteString("z")
732 }
733 return b.String()
734}
735
736// pts projects (u, v, z) triples to a flat point list.
737func pts(uvz ...int) []int {
738 out := make([]int, 0, len(uvz)/3*2)
739 for k := 0; k < len(uvz); k += 3 {
740 out = append(out, px(uvz[k], uvz[k+1]), py(uvz[k], uvz[k+1], uvz[k+2]))
741 }
742 return out
743}
744
745// Layers of one diagonal. Cells on a diagonal never overlap, so each layer
746// merges the paths of all its cells by attributes; a cell's own elements keep
747// their order through the layer numbers.
748const (
749 lyTL = iota
750 lyTR
751 lyTT
752 lyGround
753 lyWL
754 lyWR
755 lyArc
756 lyUL
757 lyUR
758 lyTop
759 lyDetail
760 lyR1
761 lyR2
762 lyR3
763 lyEdge
764 lyAir
765 nLayers
766)
767
768type entry struct {
769 key string
770 val strings.Builder
771 raw bool
772}
773
774type diagonal struct {
775 layers [nLayers][]*entry
776}
777
778func (g *diagonal) path(l int, attrs, d string) {
779 for _, e := range g.layers[l] {
780 if !e.raw && e.key == attrs {
781 e.val.WriteString(d)
782 return
783 }
784 }
785 e := &entry{key: attrs}
786 e.val.WriteString(d)
787 g.layers[l] = append(g.layers[l], e)
788}
789
790func (g *diagonal) element(l int, s string) {
791 e := &entry{raw: true}
792 e.val.WriteString(s)
793 g.layers[l] = append(g.layers[l], e)
794}
795
796func (g *diagonal) flush(o *strings.Builder) {
797 for l := 0; l < nLayers; l++ {
798 for _, e := range g.layers[l] {
799 if e.raw {
800 o.WriteString(e.val.String())
801 } else {
802 o.WriteString(`<path d="` + e.val.String() + `" ` + e.key + `/>`)
803 }
804 }
805 }
806}
807
808func fill(c string) string { return `fill="` + c + `"` }
809
810// box writes the left (v = v1) and right (u = u1) faces of a block whose
811// faces start at zl / zr.
812func box(g *diagonal, ll, lr, u0, v0, u1, v1, z1, zl, zr int, fl, fr string) {
813 if z1 > zl {
814 g.path(ll, fl, poly(pts(u0, v1, z1, u1, v1, z1, u1, v1, zl, u0, v1, zl), true))
815 }
816 if z1 > zr {
817 g.path(lr, fr, poly(pts(u1, v0, z1, u1, v1, z1, u1, v1, zr, u1, v0, zr), true))
818 }
819}
820
821func top(g *diagonal, l, u0, v0, u1, v1, z int, f string) {
822 g.path(l, f, poly(pts(u0, v0, z, u1, v0, z, u1, v1, z, u0, v1, z), true))
823}
824
825// opening is an arched opening on the left (side 0, along u from u0) or right
826// (side 1, along -v from v1) face, from a to b, h px high above z.
827func opening(side, u0, v1, a, b, z, h int) string {
828 var p []int
829 if side == 0 {
830 p = pts(u0+a, v1, z, u0+b, v1, z)
831 } else {
832 p = pts(u0, v1-a, z, u0, v1-b, z)
833 }
834 half := fdiv(absInt(p[2]-p[0]), 2)
835 return "M" + itoa(p[0]) + sp(p[1]) + "v" + itoa(-h) + "a" + itoa(half) + " " + itoa(half) + " 0 0 1 " +
836 itoa(p[2]-p[0]) + sp(p[3]-p[1]) + "v" + itoa(h) + "z"
837}
838
839// ---------------------------------------------------------------------------
840// Render
841// ---------------------------------------------------------------------------
842
843// SVG renders the artwork for a seed, a config and a token id.
844func SVG(seed string, cfg Config, tokenID string) string {
845 d := deriveDraw(seed, cfg)
846 tw := buildTown(seed, d)
847 tr := traitsOf(d, tw)
848 r := newRNG(seed + "|art") // separate stream: trait draws never shift the art
849 pal := &palette{light: d.light, memo: map[string]string{}}
850 night := d.light == lNight
851 ruin := d.plan == pRuin
852 plaster := d.material == mPlaster
853 stone := "#b9ab8e"
854 if d.material == mBasalt {
855 stone = "#7a756c"
856 }
857 // wall colours: the material's, or under painted plaster the seven pastels and tufa for civic buildings
858 walls := []string{wallCols[d.material]}
859 if plaster {
860 walls = append(append([]string{}, pastels...), wallCols[0])
861 }
862 arched := d.material <= 2 // tufa, brick, travertine: arched windows
863 tid := TavNumber(tokenID)
864
865 // night: which of the 16 windows of a 2 x 4 bay tile are lit (a ruin stays dark)
866 var lit []bool
867 nlit := 0
868 if night && !ruin {
869 for k := 0; k < 16; k++ {
870 lit = append(lit, r.intn(100) < 40)
871 if lit[k] {
872 nlit++
873 }
874 }
875 for k := 1; nlit < 3; k += 5 {
876 if !lit[k] {
877 lit[k] = true
878 nlit++
879 }
880 }
881 }
882 var stars strings.Builder
883 if night {
884 for k := 0; k < 28; k++ {
885 x := r.between(16, 688)
886 stars.WriteString("M" + itoa(x) + " " + itoa(r.between(12, 300)) + "h0")
887 }
888 }
889 // the crane stands at the front corner of a site, else a garden or square
890 crane := -1
891 if d.event == eCrane {
892 for pass := 0; pass < 3 && crane < 0; pass++ {
893 for c := n6*n6 - 1; c >= 0; c-- {
894 k := tw.kind[c]
895 if (pass == 0 && k == kSite) || (pass == 1 && (k == kGarden || k == kSquare)) || (pass == 2 && k != kWater) {
896 crane = c
897 break
898 }
899 }
900 }
901 }
902
903 usedWin := make([]bool, len(walls))
904 usedArc := false
905 win := func(ci, side int) string {
906 usedWin[ci] = true
907 if side == 0 {
908 return `fill="url(#w` + itoa(ci) + `l)"`
909 }
910 return `fill="url(#w` + itoa(ci) + `r)"`
911 }
912 var tops []int // x, y of roof tops in drawing order (festival)
913 var body strings.Builder
914 h := storey
915
916 for s := 0; s <= 2*n6-2; s++ {
917 g := &diagonal{}
918 for i := maxInt(0, s-n6+1); i <= minInt(s, n6-1); i++ {
919 j := s - i
920 c := i*n6 + j
921 k := tw.kind[c]
922 u0, v0 := i*cell, j*cell
923 u1, v1 := u0+cell, v0+cell
924 zt := tw.t[c] * h
925 nl, nr := 0, 0
926 if j+1 < n6 {
927 nl = tw.t[c+1] * h
928 }
929 if i+1 < n6 {
930 nr = tw.t[c+n6] * h
931 }
932 cu, cv := u0+10, v0+10
933 if k == kWater {
934 wc := "#6fa2bf"
935 if night {
936 wc = "#1d3550"
937 }
938 top(g, lyTT, u0, v0, u1, v1, 0, fill(pal.tint(wc)))
939 x, y := px(cu, cv), py(cu, cv, 0)
940 g.path(lyGround, `fill="none" stroke="#fff" stroke-opacity=".5"`,
941 "M"+itoa(x-16)+" "+itoa(y-3)+"q4-3 8 0t8 0M"+itoa(x-4)+" "+itoa(y+5)+"q4-3 8 0t8 0")
942 if r.intn(100) < 30 {
943 g.path(lyDetail, fill(pal.tint("#7a4a2a")),
944 "M"+itoa(x-10)+" "+itoa(y-2)+"h20l-4 5h-12zM"+itoa(x)+" "+itoa(y-3)+"v-16l9 14z")
945 }
946 continue
947 }
948 topc := pal.face(stone, 2)
949 if k == kGarden {
950 gc := "#8aa05a"
951 if night {
952 gc = "#3c4a32"
953 }
954 topc = pal.tint(gc)
955 } else if k == kSquare {
956 topc = pal.face("#d9cfba", 2)
957 }
958 box(g, lyTL, lyTR, u0, v0, u1, v1, zt, nl, nr, fill(pal.face(stone, 0)), fill(pal.face(stone, 1)))
959 top(g, lyTT, u0, v0, u1, v1, zt, fill(topc))
960 if crane == c {
961 q := pts(u1-3, v1-3, zt)
962 cx, cy := q[0], q[1]
963 tp := maxInt(40, cy-260)
964 dir := 1
965 if cx > 500 {
966 dir = -1
967 }
968 var p strings.Builder
969 p.WriteString("M" + itoa(cx-4) + " " + itoa(cy) + "V" + itoa(tp) + "M" + itoa(cx+4) + " " + itoa(cy) + "V" + itoa(tp))
970 for y := cy; y > tp+12; y -= 12 {
971 p.WriteString("M" + itoa(cx-4) + " " + itoa(y) + "l8-12")
972 }
973 p.WriteString("M" + itoa(cx-40*dir) + " " + itoa(tp) + "h" + itoa(170*dir) +
974 "M" + itoa(cx) + " " + itoa(tp-18) + "L" + itoa(cx-40*dir) + " " + itoa(tp) +
975 "M" + itoa(cx) + " " + itoa(tp-18) + "L" + itoa(cx+130*dir) + " " + itoa(tp) +
976 "M" + itoa(cx+110*dir) + " " + itoa(tp) + "v60")
977 g.element(lyAir, `<path d="`+p.String()+`" fill="none" stroke="`+pal.tint("#b8402a")+`" stroke-width="2.5"/>`+
978 `<path d="M`+itoa(cx+110*dir-6)+" "+itoa(tp+60)+"h12v8h-12zM"+itoa(cx-40*dir-6*dir)+" "+itoa(tp)+
979 "h"+itoa(12*dir)+"v10h"+itoa(-12*dir)+`z" `+fill(pal.tint("#5a4a3a"))+`/>`)
980 }
981
982 if k == kGarden {
983 n := r.between(1, 3)
984 var tree []int
985 for q := 0; q < n; q++ {
986 tu := u0 + 4 + r.intn(12)
987 tv := v0 + 4 + r.intn(12)
988 cyp := 0
989 if r.intn(100) < 60 {
990 cyp = 1
991 }
992 tree = append(tree, tu, tv, cyp)
993 }
994 // back to front
995 for a := 0; a < n; a++ {
996 for b := a + 1; b < n; b++ {
997 if tree[3*b]+tree[3*b+1] < tree[3*a]+tree[3*a+1] {
998 for q := 0; q < 3; q++ {
999 tree[3*a+q], tree[3*b+q] = tree[3*b+q], tree[3*a+q]
1000 }
1001 }
1002 }
1003 }
1004 for q := 0; q < n; q++ {
1005 x, y := px(tree[3*q], tree[3*q+1]), py(tree[3*q], tree[3*q+1], zt)
1006 if tree[3*q+2] == 1 {
1007 g.element(lyGround, `<path d="M`+itoa(x)+" "+itoa(y-34)+`c4 6 7 18 5 31q-5 4-10 0c-2-13 1-25 5-31z" `+fill(pal.tint("#3b5234"))+`/>`+
1008 `<path d="M`+itoa(x)+" "+itoa(y-34)+`c4 6 7 18 5 31q-2 2-5 2z" `+fill(pal.tint("#28391f"))+`/>`)
1009 } else {
1010 g.element(lyGround, `<path d="M`+itoa(x)+" "+itoa(y)+`v-8" stroke="`+pal.tint("#5a3e28")+`" stroke-width="2"/>`+
1011 `<path d="M`+itoa(x-9)+" "+itoa(y-13)+`a9 9 0 1 0 18 0a9 9 0 1 0-18 0z" `+fill(pal.tint("#5d7a3c"))+`/>`)
1012 }
1013 }
1014 continue
1015 }
1016 if k == kSquare {
1017 x, y := px(cu, cv), py(cu, cv, zt)
1018 g.element(lyGround, `<ellipse cx="`+itoa(x)+`" cy="`+itoa(y)+`" rx="17" ry="8" `+fill(pal.face("#b7ab94", 2))+`/>`+
1019 `<ellipse cx="`+itoa(x)+`" cy="`+itoa(y-1)+`" rx="13" ry="6" `+fill(pal.tint("#6fa2bf"))+`/>`+
1020 `<path d="M`+itoa(x)+" "+itoa(y-2)+`v-12" stroke="`+pal.tint("#cfe3ee")+`" stroke-width="2"/>`)
1021 continue
1022 }
1023
1024 // painted plaster: each house its own colour; towers and walls stay in tufa
1025 civic := k == kTower || k == kWall || k == kBell
1026 ci := 0
1027 if plaster {
1028 if civic {
1029 ci = len(pastels)
1030 } else {
1031 ci = r.intn(len(pastels))
1032 }
1033 }
1034 col := walls[ci]
1035 fl, fr, ft := fill(pal.face(col, 0)), fill(pal.face(col, 1)), fill(pal.face(col, 2))
1036 z1 := zt + tw.st[c]*h
1037
1038 if k == kBell {
1039 box(g, lyWL, lyWR, u0+4, v0+4, u1-4, v1-4, z1, zt, zt, fl, fr)
1040 top(g, lyTop, u0+4, v0+4, u1-4, v1-4, z1, ft)
1041 a, b := pts(u0+7, v1-4, z1-6), pts(u1-4, v1-7, z1-6)
1042 g.path(lyDetail, fill(pal.tint("#2c2620")),
1043 "M"+itoa(a[0])+" "+itoa(a[1])+"v-10l6 3v10zM"+itoa(b[0])+" "+itoa(b[1])+"v-10l-6 3v10z")
1044 x, y := px(cu, cv), py(cu, cv, z1)-34
1045 rc := "#b5523a"
1046 if d.material == mBasalt {
1047 rc = "#5a5650"
1048 }
1049 g.path(lyR1, fill(pal.face(rc, 0)), poly(append(pts(u0+4, v1-4, z1, u1-4, v1-4, z1), x, y), true))
1050 g.path(lyR2, fill(pal.face(rc, 1)), poly(append(pts(u1-4, v1-4, z1, u1-4, v0+4, z1), x, y), true))
1051 g.path(lyAir, `stroke="`+pal.tint("#3a332c")+`" stroke-width="1.5"`,
1052 "M"+itoa(x)+" "+itoa(y)+"v-9M"+itoa(x-3)+" "+itoa(y-6)+"h6")
1053 tops = append(tops, x, y)
1054 continue
1055 }
1056 if k == kSite {
1057 box(g, lyWL, lyWR, u0, v0, u1, v1, z1, zt, zt, win(ci, 0), win(ci, 1))
1058 top(g, lyTop, u0, v0, u1, v1, z1, fill(pal.face("#c9bfae", 2)))
1059 sh := r.between(2, 4) * h
1060 var p strings.Builder
1061 corners := []int{u0, v0, u1, v0, u1, v1, u0, v1}
1062 for q := 0; q < 8; q += 2 {
1063 pp := pts(corners[q], corners[q+1], z1)
1064 p.WriteString("M" + itoa(pp[0]) + " " + itoa(pp[1]) + "v" + itoa(-sh))
1065 }
1066 for z := z1 + h; z <= z1+sh; z += h {
1067 p.WriteString(poly(pts(u0, v1, z, u1, v1, z, u1, v0, z, u0, v0, z), true))
1068 }
1069 p.WriteString(poly(pts(u0, v1, z1, u1, v1, z1+sh), false) + poly(pts(u1, v0, z1, u1, v1, z1+sh), false))
1070 g.path(lyAir, `fill="none" stroke="`+pal.tint("#b5452f")+`" stroke-width="1.3"`, p.String())
1071 // a plank on the first lift
1072 g.path(lyAir, `stroke="`+pal.tint("#8a6a3a")+`" stroke-width="3"`, poly(pts(u0+2, v1, z1+h, u1, v1, z1+h), false))
1073 tops = append(tops, px(cu, cv), py(cu, cv, z1+sh))
1074 continue
1075 }
1076 if k == kArcade {
1077 box(g, lyWL, lyWR, u0, v0, u1, v1, zt+h, zt, zt, fl, fr)
1078 g.path(lyArc, `fill="url(#al)"`, poly(pts(u0, v1, zt+h, u1, v1, zt+h, u1, v1, zt, u0, v1, zt), true))
1079 g.path(lyArc, `fill="url(#ar)"`, poly(pts(u1, v0, zt+h, u1, v1, zt+h, u1, v1, zt, u1, v0, zt), true))
1080 usedArc = true
1081 box(g, lyUL, lyUR, u0, v0, u1, v1, z1, zt+h, zt+h, win(ci, 0), win(ci, 1))
1082 top(g, lyTop, u0, v0, u1, v1, z1, ft)
1083 g.path(lyEdge, `fill="none" stroke="`+shade(pal.face(col, 2), 60)+`" stroke-width="1.2"`,
1084 poly(pts(u0, v1, z1+5, u1, v1, z1+5, u1, v0, z1+5), false))
1085 tops = append(tops, px(cu, cv), py(cu, cv, z1))
1086 continue
1087 }
1088 if k == kHouse && ruin {
1089 // a roofless shell: broken wall tops, the floor inside in shadow
1090 h1 := z1 - fdiv(r.intn(3)*h, 2)
1091 h2 := z1 - fdiv(r.intn(3)*h, 2)
1092 lo := maxInt(zt+fdiv(h, 2), minInt(h1, h2)-fdiv(h, 2))
1093 top(g, lyGround, u0, v0, u1, v1, lo, fill(pal.face("#5b4c3e", 2)))
1094 wl, wr := maxInt(zt+6, h1), maxInt(zt+6, h2)
1095 g.path(lyWL, win(ci, 0), poly(pts(u0, v1, zt, u0, v1, wl, u0+7, v1, wl, u0+7, v1, z1, u0+13, v1, z1, u0+13, v1, wr, u1, v1, wr, u1, v1, zt), true))
1096 g.path(lyWR, win(ci, 1), poly(pts(u1, v1, zt, u1, v1, wr, u1, v1-8, wr, u1, v1-8, wl, u1, v0, wl, u1, v0, zt), true))
1097 } else {
1098 if k == kWall {
1099 box(g, lyWL, lyWR, u0, v0, u1, v1, z1, zt, zt, fl, fr)
1100 } else {
1101 box(g, lyWL, lyWR, u0, v0, u1, v1, z1, zt, zt, win(ci, 0), win(ci, 1))
1102 }
1103 if k != kHouse {
1104 top(g, lyTop, u0, v0, u1, v1, z1, ft)
1105 }
1106 }
1107 switch k {
1108 case kHouse:
1109 // a door on the front face that is not hidden by higher ground
1110 side := r.intn(2)
1111 if (side == 0 && zt >= nl) || (side == 1 && zt >= nr) {
1112 du := u0
1113 if side == 1 {
1114 du = u1
1115 }
1116 g.path(lyDetail, fill(pal.tint("#4a3426")), opening(side, du, v1, 8, 12, zt, 9))
1117 }
1118 if !ruin {
1119 rc := "#5a5650"
1120 if d.material != mBasalt {
1121 rc = roofCols[r.intn(3)]
1122 }
1123 zr := z1 + 19
1124 if r.intn(2) == 0 {
1125 ridge := pts(u0, cv, zr, u1, cv, zr)
1126 g.path(lyR1, fill(pal.face(rc, 2)), poly(append(pts(u0, v0, z1, u1, v0, z1), ridge[2], ridge[3], ridge[0], ridge[1]), true))
1127 g.path(lyR2, fr, poly(append(pts(u1, v0, z1, u1, v1, z1), ridge[2], ridge[3]), true))
1128 g.path(lyR3, fill(pal.face(rc, 0)), poly(append(pts(u0, v1, z1, u1, v1, z1), ridge[2], ridge[3], ridge[0], ridge[1]), true))
1129 } else {
1130 ridge := pts(cu, v0, zr, cu, v1, zr)
1131 g.path(lyR1, fill(pal.face(rc, 2)), poly(append(pts(u0, v0, z1, u0, v1, z1), ridge[2], ridge[3], ridge[0], ridge[1]), true))
1132 g.path(lyR2, fl, poly(append(pts(u0, v1, z1, u1, v1, z1), ridge[2], ridge[3]), true))
1133 g.path(lyR3, fill(pal.face(rc, 1)), poly(append(pts(u1, v0, z1, u1, v1, z1), ridge[2], ridge[3], ridge[0], ridge[1]), true))
1134 }
1135 tops = append(tops, px(cu, cv), py(cu, cv, zr))
1136 } else {
1137 tops = append(tops, px(cu, cv), py(cu, cv, z1))
1138 }
1139 case kTower, kWall:
1140 zc := z1 + 4
1141 cl := `fill="none" stroke="` + pal.face(col, 0) + `" stroke-width="8" stroke-dasharray="6 5"`
1142 cr := `fill="none" stroke="` + pal.face(col, 1) + `" stroke-width="8" stroke-dasharray="6 5"`
1143 g.path(lyDetail, cl, poly(pts(u0, v0, zc, u1, v0, zc), false))
1144 g.path(lyDetail, cr, poly(pts(u0, v0, zc, u0, v1, zc), false))
1145 g.path(lyEdge, cl, poly(pts(u0, v1, zc, u1, v1, zc), false))
1146 g.path(lyEdge, cr, poly(pts(u1, v1, zc, u1, v0, zc), false))
1147 // the citadel gate, in the middle of the two front walls
1148 if k == kWall && i == n6-1 && j == 2 {
1149 g.path(lyDetail, fill(pal.tint("#2c2620")), opening(1, u1, v1, 2, 18, zt, 22))
1150 }
1151 if k == kWall && j == n6-1 && i == 2 {
1152 g.path(lyDetail, fill(pal.tint("#2c2620")), opening(0, u0, v1, 2, 18, zt, 22))
1153 }
1154 tops = append(tops, px(cu, cv), py(cu, cv, z1))
1155 case kDome:
1156 x, y := px(cu, cv), py(cu, cv, z1)
1157 dc := "#c8a04a"
1158 if gd := r.intn(8); gd < 4 {
1159 dc = "#8d9aa0"
1160 } else if gd < 7 {
1161 dc = "#6f9c8a"
1162 }
1163 dome := "M" + itoa(x-34) + " " + itoa(y) + "a34 34 0 0 1 68 0a34 15 0 0 1-68 0z"
1164 g.path(lyR1, fill(pal.face(dc, 2)), dome)
1165 g.path(lyR2, `fill="url(#dm)"`, dome)
1166 g.path(lyR3, fill(pal.face(col, 0))+` stroke="`+pal.face(dc, 0)+`"`,
1167 "M"+itoa(x-3)+" "+itoa(y-30)+"h6v-8h-6zM"+itoa(x)+" "+itoa(y-46)+"v8")
1168 tops = append(tops, x, y-30)
1169 }
1170 }
1171 g.flush(&body)
1172 }
1173
1174 // --- assemble ---
1175 winD := "#3a332c"
1176 if night {
1177 winD = "#2a2c38"
1178 } else if d.material == mWhitewash {
1179 winD = "#3c6fa8"
1180 }
1181 var wd, wlit, sills strings.Builder
1182 for k := 0; k < 16; k++ {
1183 x, y := 8+26*(k%4), 8+26*(k>>2)
1184 w := "M" + itoa(x) + " " + itoa(y) + "h9v12h-9z"
1185 if arched {
1186 w = "M" + itoa(x) + " " + itoa(y+12) + "v-7a4.5 4.5 0 0 1 9 0v7z"
1187 }
1188 if len(lit) > 0 && lit[k] {
1189 wlit.WriteString(w)
1190 } else {
1191 wd.WriteString(w)
1192 }
1193 sills.WriteString("M" + itoa(x-1) + " " + itoa(y+13) + "h11")
1194 }
1195 var o strings.Builder
1196 W := itoa(canvas)
1197 o.WriteString(`<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 ` + W + " " + W + `" width="` + W + `" height="` + W + `">`)
1198 sheet := "Prova"
1199 if tid > 0 {
1200 sheet = "Tav. " + itoa(tid)
1201 }
1202 o.WriteString(`<title>Cantiere · Kasaba Dili</title><desc>` + tr.Plan + " in " + strings.ToLower(tr.Material) + ", " +
1203 strings.ToLower(tr.Light) + " light. Landmark: " + strings.ToLower(tr.Landmark) + ". Event: " +
1204 strings.ToLower(tr.Event) + ". " + sheet + ".</desc>")
1205 grid, skyLow, paper := "#d9cfbb", "#f1ead9", "#f1ead9"
1206 if night {
1207 grid, skyLow, paper = "#3b4766", "#1f2738", "#1b2232"
1208 }
1209 o.WriteString(`<defs><pattern id="gr" width="52" height="26" patternUnits="userSpaceOnUse"><path d="M0 0l52 26M0 26l52-26" stroke="` + grid + `" stroke-width=".7"/></pattern>`)
1210 o.WriteString(`<linearGradient id="sky" x1="0" y1="0" x2="0" y2="1"><stop offset="0" stop-color="` + skies[d.light] + `"/><stop offset="1" stop-color="` + skyLow + `" stop-opacity="0"/></linearGradient>`)
1211 o.WriteString(`<radialGradient id="dm" cx=".35" cy=".3" r=".8"><stop offset="0" stop-color="#fff" stop-opacity=".55"/><stop offset="1" stop-color="#000" stop-opacity=".25"/></radialGradient>`)
1212 o.WriteString(`<filter id="sh" x="-.2" y="-.3" width="1.4" height="1.6"><feGaussianBlur stdDeviation="9"/></filter>`)
1213 o.WriteString(`<path id="wd" d="` + wd.String() + `" fill="` + winD + `"/>`)
1214 if wlit.Len() > 0 {
1215 o.WriteString(`<path id="wl" d="` + wlit.String() + `" fill="#ffd36a"/>`)
1216 } else if !night {
1217 o.WriteString(`<path id="wl" d="` + sills.String() + `" stroke="#fff" stroke-opacity=".45"/>`)
1218 }
1219 mtx := []string{"1 .5", "1 -.5"}
1220 lr := []string{"l", "r"}
1221 for ci := 0; ci < len(walls); ci++ {
1222 if !usedWin[ci] {
1223 continue
1224 }
1225 for side := 0; side < 2; side++ {
1226 o.WriteString(`<pattern id="w` + itoa(ci) + lr[side] + `" width="104" height="104" patternUnits="userSpaceOnUse" patternTransform="matrix(` + mtx[side] + ` 0 1 0 0)">` +
1227 `<rect width="104" height="104" fill="` + pal.face(walls[ci], side) + `"/><use href="#wd"/>`)
1228 if !night || wlit.Len() > 0 {
1229 o.WriteString(`<use href="#wl"/>`)
1230 }
1231 o.WriteString(`</pattern>`)
1232 }
1233 }
1234 if usedArc {
1235 for side := 0; side < 2; side++ {
1236 o.WriteString(`<pattern id="a` + lr[side] + `" width="26" height="26" patternUnits="userSpaceOnUse" patternTransform="matrix(` + mtx[side] + ` 0 1 0 0)">` +
1237 `<path d="M6 26v-11a7 7 0 0 1 14 0v11z" fill="#2c2620" fill-opacity=".75"/></pattern>`)
1238 }
1239 }
1240 if d.event == eFog {
1241 fogc := "#fff"
1242 if night {
1243 fogc = "#aab4ca"
1244 }
1245 o.WriteString(`<linearGradient id="fg" x1="0" y1="0" x2="0" y2="1"><stop offset="0" stop-color="#fff" stop-opacity="0"/>` +
1246 `<stop offset=".7" stop-color="` + fogc + `" stop-opacity=".75"/><stop offset="1" stop-color="#fff" stop-opacity=".2"/></linearGradient>`)
1247 }
1248 o.WriteString(`</defs><rect width="` + W + `" height="` + W + `" fill="` + paper + `"/><rect width="` + W + `" height="` + W + `" fill="url(#gr)"/><rect width="` + W + `" height="360" fill="url(#sky)"/>`)
1249 if night {
1250 o.WriteString(`<path d="` + stars.String() + `" stroke="#e9e4cf" stroke-width="2" stroke-linecap="round" opacity=".7"/><path d="M588 74a22 22 0 1 0 20 34a20 20 0 0 1-20-34z" fill="#e9e4cf"/>`)
1251 } else {
1252 sx, sy, sc := 110, 120, "#f0c35a"
1253 if d.light == lEvening {
1254 sx, sc = 600, "#ef9a52"
1255 }
1256 if d.light == lNoon {
1257 sy = 70
1258 }
1259 o.WriteString(`<circle cx="` + itoa(sx) + `" cy="` + itoa(sy) + `" r="54" fill="` + sc + `" opacity=".18"/><circle cx="` + itoa(sx) + `" cy="` + itoa(sy) + `" r="34" fill="` + sc + `" opacity=".85"/>`)
1260 }
1261 // the maquette's shadow on the sheet, away from the sun
1262 sdx := 0
1263 if d.light == lMorning {
1264 sdx = 26
1265 } else if d.light == lEvening {
1266 sdx = -26
1267 }
1268 e := n6 * cell
1269 shadowOp := ".3"
1270 if night {
1271 shadowOp = ".45"
1272 }
1273 o.WriteString(`<path d="` + poly([]int{px(0, 0) + sdx, py(0, 0, -18) + 14, px(e, 0) + sdx, py(e, 0, -18) + 14,
1274 px(e, e) + sdx, py(e, e, -18) + 14, px(0, e) + sdx, py(0, e, -18) + 14}, true) +
1275 `" fill="#2a2018" opacity="` + shadowOp + `" filter="url(#sh)"/>`)
1276 o.WriteString(`<g stroke="#2a2420" stroke-opacity=".25" stroke-width=".6" stroke-linejoin="round">`)
1277 base := "#a58a64"
1278 o.WriteString(`<path d="` + poly(pts(0, e, 0, e, e, 0, e, e, -18, 0, e, -18), true) + `" ` + fill(pal.face(base, 0)) + `/>`)
1279 o.WriteString(`<path d="` + poly(pts(e, 0, 0, e, e, 0, e, e, -18, e, 0, -18), true) + `" ` + fill(pal.face(base, 1)) + `/>`)
1280 o.WriteString(body.String())
1281 o.WriteString(`</g>`)
1282
1283 // --- events ---
1284 switch d.event {
1285 case eFestival:
1286 // a string of lanterns from the highest top to the top that spans furthest across
1287 if len(tops) < 4 {
1288 tops = append(tops, 200, 220, 504, 220)
1289 }
1290 a := 0
1291 for q := 2; q < len(tops); q += 2 {
1292 if tops[q+1] < tops[a+1] {
1293 a = q
1294 }
1295 }
1296 b, best := -1, 0
1297 for q := 0; q < len(tops); q += 2 {
1298 if q == a {
1299 continue
1300 }
1301 v := 2*absInt(tops[q]-tops[a]) - (tops[q+1] - tops[a+1])
1302 if b < 0 || v > best {
1303 b, best = q, v
1304 }
1305 }
1306 ax, ay, bx, by := tops[a], tops[a+1], tops[b], tops[b+1]
1307 my := maxInt(ay, by) + 40
1308 var lx []int
1309 for q := 1; q < 12; q++ {
1310 x := fdiv((12-q)*(12-q)*2*ax+2*q*(12-q)*(ax+bx)+q*q*2*bx, 288)
1311 y := fdiv((12-q)*(12-q)*ay+2*q*(12-q)*my+q*q*by, 144)
1312 lx = append(lx, x, y+3)
1313 }
1314 o.WriteString(`<path d="M` + itoa(ax) + " " + itoa(ay) + "Q" + itoa(fdiv(ax+bx, 2)) + " " + itoa(my) + " " + itoa(bx) + " " + itoa(by) +
1315 `" fill="none" stroke="#2a2420" stroke-width=".8"/>`)
1316 for col := 0; col < 4; col++ {
1317 var p strings.Builder
1318 for q := 1; q < 12; q++ {
1319 if q%4 == col {
1320 p.WriteString("M" + itoa(lx[2*q-2]-4) + " " + itoa(lx[2*q-1]) + "a4 4 0 1 0 8 0a4 4 0 1 0-8 0z")
1321 }
1322 }
1323 o.WriteString(`<path d="` + p.String() + `" fill="` + lanterns[col] + `"/>`)
1324 }
1325 case eFog:
1326 o.WriteString(`<rect y="380" width="` + W + `" height="260" fill="url(#fg)"/>`)
1327 case eBirds:
1328 var p strings.Builder
1329 for q := 0; q < 7; q++ {
1330 x := r.between(120, 600)
1331 y := r.between(40, 160)
1332 s := 2 * r.between(2, 4)
1333 hh := s / 2
1334 p.WriteString("M" + itoa(x-s) + " " + itoa(y-hh) + "q" + itoa(hh) + " 0 " + itoa(s) + " " + itoa(hh) +
1335 "q" + itoa(hh) + "-" + itoa(hh) + " " + itoa(s) + "-" + itoa(hh))
1336 }
1337 bc := "#3a332c"
1338 if night {
1339 bc = "#cfd6e6"
1340 }
1341 o.WriteString(`<path d="` + p.String() + `" fill="none" stroke="` + bc + `" stroke-width="1.4"/>`)
1342 }
1343
1344 // --- title block, north arrow and scale bar ---
1345 ink := "#3a332c"
1346 if night {
1347 ink = "#c9d2e6"
1348 }
1349 head := "PROVA"
1350 if tid > 0 {
1351 head = "TAV. " + itoa(tid)
1352 }
1353 o.WriteString(`<g font-family="Georgia, serif" fill="` + ink + `"><rect x="520" y="626" width="164" height="54" fill="none" stroke="` + ink + `"/>` +
1354 `<path d="M520 646h164M610 646v34" stroke="` + ink + `" stroke-width=".7"/>` +
1355 `<text x="528" y="641" font-size="11" letter-spacing="1">CANTIERE · ` + head + `</text>` +
1356 `<text x="528" y="662" font-size="9">` + tr.Plan + `</text><text x="528" y="674" font-size="9">` + tr.Material + `</text>` +
1357 `<text x="618" y="662" font-size="9">1:500</text><text x="618" y="674" font-size="9">` + tr.Light + `</text></g>`)
1358 o.WriteString(`<g stroke="` + ink + `" fill="none"><circle cx="46" cy="650" r="16"/><path d="M46 628v44M42 640l4-12 4 12z" fill="` + ink + `"/>` +
1359 `<path d="M80 670h60M80 666v8M110 667v6M140 666v8" stroke-width="1.2"/><path d="M80 668h30v4h-30z" fill="` + ink + `"/></g>`)
1360 o.WriteString(`</svg>`)
1361 return o.String()
1362}
1363
1364// TraitString is "Type=Value;…" in TraitTypes order (the golden-vector form
1365// shared with the TypeScript twin).
1366func TraitString(seed, params string) string {
1367 parts := []string{}
1368 for _, t := range Traits(engine.Input{Seed: seed, Params: params}) {
1369 parts = append(parts, t.Type+"="+t.Value)
1370 }
1371 return strings.Join(parts, ";")
1372}