cinar.gno
38.58 Kb · 1298 lines
1// Package cinar is a Sinopia engine and its first living one: a tree that
2// grows with the token's age, turns with the chain's seasons and keeps its
3// owners in its bark and roots.
4//
5// The tree's shape, species, setting and rare marks come from the seed. How
6// much of the tree exists, and what it looks like today, comes from the chain
7// inputs core passes in engine.Input (Living = true):
8//
9// - Height - MintHeight (age): growth (a square-root curve over three
10// chain years) and the Stage trait;
11// - Height alone: the chain season (a 365-day chain year of 24,000-block
12// days, anchored so that gnoland-1 height 1,480,355 = 11 Nov 2026 =
13// day 236 after the spring equinox), the sun's height, and the daily
14// sky (clouds, birds, snowfall) drawn from a per-day stream;
15// - Height - LastMoveHeight (holding time): the current owner's roots,
16// moss after 180 days and a nest after 365 days; the tree's lifetime
17// roots stay as a faint ghost below them;
18// - Moves (transfers): one notch carved in the bark per transfer, or the
19// first owner's seal while it has never moved; a Wish Tree gets more
20// ribbons with every owner.
21//
22// CollectionID and TokenID are not read. Inputs are clamped to 0..2^32-1
23// (core stores them as uint32) and MintHeight <= LastMoveHeight <= Height is
24// enforced, so any RenderWithInput call renders.
25//
26// Pure integer maths: no floats. frontend/src/engines/cinar.ts is the
27// byte-identical TypeScript twin; cinar_test.gno and cinar.test.ts assert the
28// same golden vectors.
29package cinar
30
31import (
32 "errors"
33 "strconv"
34 "strings"
35
36 "gno.land/p/g1jz6j9hj6rdf5ltl2kcrpa4jwtdh3w30z0yul06/sinopia4/engine/v2"
37)
38
39// Chain time.
40const (
41 BlocksPerDay = 24000
42 BlocksPerYear = 365 * BlocksPerDay
43 BlocksPerSeason = BlocksPerYear / 4
44 CalendarOffset = 4184000 // (Height + offset) mod year = 0 at the spring equinox
45 FullGrowthDays = 1095
46 FullRootsDays = 365
47 maxU32 = 4294967295
48)
49
50// TraitTypes in the order Traits returns them.
51var traitTypes = []string{"Species", "Setting", "Season", "Stage", "Roots", "Notches", "Mark", "Rarity"}
52
53var seasonNames = []string{"Spring", "Summer", "Autumn", "Winter"}
54
55// MaxSVGBytes: the largest output measured is ~22 KB (full-grown crown,
56// many transfers); the cap leaves headroom under the 28 KB budget.
57const MaxSVGBytes = 28672
58
59// Fingerprint is sha256(Render(engine.SampleInput())): a sapling on the
60// steppe in late summer at height 1. The integration step reserves it in
61// core (r/core config.gno reservedFingerprints).
62const Fingerprint = "95d4922c9f9cdb02ab96e88c1628deb179ebb8d327054ab64d12040b38c00e83"
63
64// ParamsSchema is the Studio form. Studio sends JSON.stringify of the values
65// in this property order; ValidateParams accepts exactly that or "".
66const ParamsSchema = `{"type":"object","properties":{` +
67 `"species":{"type":"string","title":"Species","enum":["Seeded","Oriental Plane","Olive","Judas Tree","Cypress"],` +
68 `"default":"Seeded"},` +
69 `"setting":{"type":"string","title":"Setting","enum":["Seeded","Hill","Courtyard","Shore","Steppe"],` +
70 `"default":"Seeded"}}}`
71
72// DefaultParams is the canonical form of the defaults; it renders exactly
73// like "".
74const DefaultParams = `{"species":"Seeded","setting":"Seeded"}`
75
76// Manifest describes the engine. Author and FeeBps are set by the adapter.
77func Manifest() engine.Manifest {
78 return engine.Manifest{
79 Name: "Cinar",
80 Version: "1.0.0",
81 Description: "A living tree: it grows with the token's age, turns with the chain's seasons and keeps " +
82 "its owners in its bark and roots (one notch per transfer, roots that deepen while it is held).",
83 License: "CC-BY-NC-4.0",
84 ParamsSchema: ParamsSchema,
85 TraitTypes: append([]string{}, traitTypes...),
86 Living: true,
87 MaxSVGBytes: MaxSVGBytes,
88 Kind: engine.KindSVG,
89 }
90}
91
92// Config is a parsed params string.
93type Config struct {
94 Species int // -1: drawn from the seed
95 Setting int // -1: drawn from the seed
96}
97
98// Defaults is the showcase look (params "").
99func Defaults() Config { return Config{Species: -1, Setting: -1} }
100
101var errParams = errors.New(`cinar: params must be "" or ` +
102 `{"species":<enum>,"setting":<enum>} in that order, without spaces`)
103
104// ParseParams accepts "" or the canonical JSON Studio produces, nothing else.
105func ParseParams(params string) (Config, error) {
106 cfg := Defaults()
107 if params == "" {
108 return cfg, nil
109 }
110 const p1, p2, end = `{"species":"`, `","setting":"`, `"}`
111 if len(params) < len(p1)+len(end) || !strings.HasPrefix(params, p1) || !strings.HasSuffix(params, end) {
112 return cfg, errParams
113 }
114 body := params[len(p1) : len(params)-len(end)]
115 i := strings.Index(body, p2)
116 if i < 0 {
117 return cfg, errParams
118 }
119 sp, st := body[:i], body[i+len(p2):]
120 if strings.IndexByte(sp, '"') >= 0 || strings.IndexByte(st, '"') >= 0 {
121 return cfg, errParams
122 }
123 var ok bool
124 if cfg.Species, ok = enumIndex(sp, speciesOpts); !ok {
125 return cfg, errParams
126 }
127 if cfg.Setting, ok = enumIndex(st, settingOpts); !ok {
128 return cfg, errParams
129 }
130 return cfg, nil
131}
132
133func enumIndex(v string, table []option) (int, bool) {
134 if v == "Seeded" {
135 return -1, true
136 }
137 for i, o := range table {
138 if o.name == v {
139 return i, true
140 }
141 }
142 return 0, false
143}
144
145// ValidateParams accepts "" (the showcase defaults) and the canonical JSON.
146func ValidateParams(params string) error {
147 _, err := ParseParams(params)
148 return err
149}
150
151func mustConfig(params string) Config {
152 cfg, err := ParseParams(params)
153 if err != nil {
154 panic(err)
155 }
156 return cfg
157}
158
159// ---------------------------------------------------------------------------
160// Chain time
161// ---------------------------------------------------------------------------
162
163// Clock is everything the art reads from chain time, after clamping.
164type Clock struct {
165 Height, MintHeight, LastMoveHeight, Moves int
166 AgeDays, HoldDays int
167 Season int // 0 spring .. 3 winter
168 Progress int // 0..999 within the season
169 YearAngle int // 0..255, spring equinox = 0
170 Day int // chain day number
171}
172
173func u32(v int64) int {
174 if v < 0 {
175 return 0
176 }
177 if v > maxU32 {
178 return maxU32
179 }
180 return int(v)
181}
182
183// ClockOf reads the living fields of in.
184func ClockOf(in engine.Input) Clock {
185 var c Clock
186 c.Height = u32(in.Height)
187 c.MintHeight = u32(in.MintHeight)
188 c.LastMoveHeight = u32(in.LastMoveHeight)
189 c.Moves = u32(in.Moves)
190 if c.MintHeight > c.Height {
191 c.MintHeight = c.Height
192 }
193 if c.LastMoveHeight < c.MintHeight {
194 c.LastMoveHeight = c.MintHeight
195 }
196 if c.LastMoveHeight > c.Height {
197 c.LastMoveHeight = c.Height
198 }
199 t := (c.Height + CalendarOffset) % BlocksPerYear
200 c.AgeDays = (c.Height - c.MintHeight) / BlocksPerDay
201 c.HoldDays = (c.Height - c.LastMoveHeight) / BlocksPerDay
202 c.Season = t / BlocksPerSeason
203 c.Progress = (t % BlocksPerSeason) * 1000 / BlocksPerSeason
204 c.YearAngle = t * 256 / BlocksPerYear
205 c.Day = (c.Height + CalendarOffset) / BlocksPerDay
206 return c
207}
208
209// ---------------------------------------------------------------------------
210// Deterministic randomness: FNV-1a over the stream name, then splitmix64
211// (the v1 engines' stream). Streams: seed+"|traits", "|tree", "|leaf",
212// "|land", "|roots", "|marks", "|fall|<season>", "|sky|<day>", "|snow|<day>".
213// ---------------------------------------------------------------------------
214
215type rng struct{ state uint64 }
216
217func newRNG(seed string) *rng {
218 h := uint64(14695981039346656037)
219 for i := 0; i < len(seed); i++ {
220 h ^= uint64(seed[i])
221 h *= 1099511628211
222 }
223 if h == 0 {
224 h = 1099511628211
225 }
226 return &rng{state: h}
227}
228
229func (r *rng) next() uint64 {
230 r.state += 0x9E3779B97F4A7C15
231 z := r.state
232 z = (z ^ (z >> 30)) * 0xBF58476D1CE4E5B9
233 z = (z ^ (z >> 27)) * 0x94D049BB133111EB
234 return z ^ (z >> 31)
235}
236
237// intn is in [0, n); one draw whenever n > 0.
238func (r *rng) intn(n int) int {
239 if n <= 0 {
240 return 0
241 }
242 return int(r.next() % uint64(n))
243}
244
245// between is in [a, b].
246func (r *rng) between(a, b int) int { return a + r.intn(b-a+1) }
247
248// ---------------------------------------------------------------------------
249// Traits
250// ---------------------------------------------------------------------------
251
252type option struct {
253 name string
254 weight int
255 score int
256}
257
258func pick(r *rng, table []option) int {
259 total := 0
260 for _, o := range table {
261 total += o.weight
262 }
263 x := r.intn(total)
264 for i, o := range table {
265 x -= o.weight
266 if x < 0 {
267 return i
268 }
269 }
270 return len(table) - 1
271}
272
273var speciesOpts = []option{{"Oriental Plane", 34, 0}, {"Olive", 24, 0}, {"Judas Tree", 24, 0}, {"Cypress", 18, 1}}
274var settingOpts = []option{{"Hill", 30, 0}, {"Courtyard", 24, 0}, {"Shore", 24, 1}, {"Steppe", 22, 0}}
275var markOpts = []option{{"None", 88, 0}, {"Lightning Scar", 5, 2}, {"Wish Tree", 4, 3}, {"White Tree", 3, 4}}
276
277const (
278 spPlane = iota
279 spOlive
280 spJudas
281 spCypress
282)
283
284const (
285 setHill = iota
286 setCourtyard
287 setShore
288 setSteppe
289)
290
291const (
292 mkNone = iota
293 mkLightning
294 mkWish
295 mkWhite
296)
297
298type draw struct {
299 species, setting, mark, score int
300 rarity string
301}
302
303func derive(seed string, cfg Config) draw {
304 r := newRNG(seed + "|traits")
305 // Draw order is part of the contract: changing it changes every token.
306 var d draw
307 d.species = pick(r, speciesOpts)
308 d.setting = pick(r, settingOpts)
309 d.mark = pick(r, markOpts)
310 // Params override after the draws, so the stream never shifts.
311 d.score = markOpts[d.mark].score
312 if cfg.Species >= 0 {
313 d.species = cfg.Species
314 } else {
315 d.score += speciesOpts[d.species].score
316 }
317 if cfg.Setting >= 0 {
318 d.setting = cfg.Setting
319 } else {
320 d.score += settingOpts[d.setting].score
321 }
322 switch {
323 case d.score >= 5:
324 d.rarity = "Mythic"
325 case d.score >= 4:
326 d.rarity = "Legendary"
327 case d.score >= 2:
328 d.rarity = "Rare"
329 case d.score >= 1:
330 d.rarity = "Uncommon"
331 default:
332 d.rarity = "Common"
333 }
334 return d
335}
336
337func stageOf(age int) string {
338 switch {
339 case age < 30:
340 return "Sapling"
341 case age < 180:
342 return "Young"
343 case age < 730:
344 return "Mature"
345 }
346 return "Venerable"
347}
348
349func rootsOf(hold int) string {
350 switch {
351 case hold < 30:
352 return "Fresh"
353 case hold < 180:
354 return "Settled"
355 case hold < 365:
356 return "Deep"
357 }
358 return "Ancient"
359}
360
361func notchesOf(m int) string {
362 switch {
363 case m == 0:
364 return "First Owner"
365 case m == 1:
366 return "1 Notch"
367 case m <= 4:
368 return "2-4 Notches"
369 case m <= 9:
370 return "5-9 Notches"
371 }
372 return "10+ Notches"
373}
374
375// TraitSet is the decoded properties of an input.
376type TraitSet struct {
377 Species, Setting, Season, Stage, Roots, Notches, Mark string
378 Score int
379 Rarity string
380}
381
382// TraitsOf returns the traits for in under cfg. Species, Setting, Mark and
383// Rarity come from the seed; Season, Stage, Roots and Notches from chain time.
384func TraitsOf(in engine.Input, cfg Config) TraitSet {
385 d := derive(in.Seed, cfg)
386 c := ClockOf(in)
387 return TraitSet{
388 Species: speciesOpts[d.species].name,
389 Setting: settingOpts[d.setting].name,
390 Season: seasonNames[c.Season],
391 Stage: stageOf(c.AgeDays),
392 Roots: rootsOf(c.HoldDays),
393 Notches: notchesOf(c.Moves),
394 Mark: markOpts[d.mark].name,
395 Score: d.score,
396 Rarity: d.rarity,
397 }
398}
399
400// Traits returns the eight traits of in in TraitTypes order. It never
401// renders: three table draws and the clock.
402func Traits(in engine.Input) []engine.Trait {
403 t := TraitsOf(in, mustConfig(in.Params))
404 return []engine.Trait{
405 {Type: "Species", Value: t.Species},
406 {Type: "Setting", Value: t.Setting},
407 {Type: "Season", Value: t.Season},
408 {Type: "Stage", Value: t.Stage},
409 {Type: "Roots", Value: t.Roots},
410 {Type: "Notches", Value: t.Notches},
411 {Type: "Mark", Value: t.Mark},
412 {Type: "Rarity", Value: t.Rarity},
413 }
414}
415
416// Render returns the raw SVG for in.
417func Render(in engine.Input) string { return SVG(in, mustConfig(in.Params)) }
418
419// ---------------------------------------------------------------------------
420// Integer helpers
421// ---------------------------------------------------------------------------
422
423// qsin is sin(a * 2π / 256) * 1024 over the first quadrant.
424var qsin = []int{
425 0, 25, 50, 75, 100, 125, 150, 175, 200, 224, 249, 273, 297, 321, 345, 369, 392, 415, 438, 460, 483, 505, 526, 548, 569,
426 590, 610, 630, 650, 669, 688, 706, 724, 742, 759, 775, 792, 807, 822, 837, 851, 865, 878, 891, 903, 915, 926, 936, 946,
427 955, 964, 972, 980, 987, 993, 999, 1004, 1009, 1013, 1016, 1019, 1021, 1023, 1024, 1024,
428}
429
430func sn(a int) int {
431 a = ((a % 256) + 256) % 256
432 switch {
433 case a <= 64:
434 return qsin[a]
435 case a <= 128:
436 return qsin[128-a]
437 case a <= 192:
438 return -qsin[a-128]
439 }
440 return -qsin[256-a]
441}
442
443func cs(a int) int { return sn(a + 64) }
444
445// isqrt for 0 <= n <= 1,000,000.
446func isqrt(n int) int {
447 lo, hi := 0, 1001
448 for hi-lo > 1 {
449 m := (lo + hi) / 2
450 if m*m <= n {
451 lo = m
452 } else {
453 hi = m
454 }
455 }
456 return lo
457}
458
459func imin(a, b int) int {
460 if a < b {
461 return a
462 }
463 return b
464}
465
466func imax(a, b int) int {
467 if a > b {
468 return a
469 }
470 return b
471}
472
473func clampI(v, a, b int) int {
474 if v < a {
475 return a
476 }
477 if v > b {
478 return b
479 }
480 return v
481}
482
483func itoa(n int) string { return strconv.Itoa(n) }
484
485// f10 prints tenths as a decimal: 12 -> "1.2", 30 -> "3", 7 -> "0.7".
486func f10(v int) string {
487 if v%10 == 0 {
488 return itoa(v / 10)
489 }
490 return itoa(v/10) + "." + itoa(v%10)
491}
492
493const hexd = "0123456789abcdef"
494
495func unhex(c byte) int {
496 if c >= 'a' {
497 return int(c) - 87
498 }
499 return int(c) - 48
500}
501
502// mix is a + (b - a) * w / 1000 per channel (w in permille).
503func mix(a, b string, w int) string {
504 out := []byte{'#', 0, 0, 0, 0, 0, 0}
505 for i := 1; i < 7; i += 2 {
506 x := unhex(a[i])*16 + unhex(a[i+1])
507 y := unhex(b[i])*16 + unhex(b[i+1])
508 v := clampI(x+(y-x)*w/1000, 0, 255)
509 out[i], out[i+1] = hexd[v>>4], hexd[v&15]
510 }
511 return string(out)
512}
513
514// seasonal blends a season palette smoothly into the neighbouring seasons.
515func seasonal(p []string, season, prog int) string {
516 if prog < 500 {
517 return mix(p[(season+3)%4], p[season], 500+prog)
518 }
519 return mix(p[season], p[(season+1)%4], prog-500)
520}
521
522// buf is an append-only byte buffer (amortised appends, no quadratic +=).
523type buf struct{ b []byte }
524
525func (s *buf) w(x string) { s.b = append(s.b, x...) }
526func (s *buf) n(v int) { s.b = append(s.b, strconv.Itoa(v)...) }
527func (s *buf) empty() bool { return len(s.b) == 0 }
528
529// mv appends "M<x> <y>".
530func (s *buf) mv(x, y int) {
531 s.b = append(s.b, 'M')
532 s.n(x)
533 s.b = append(s.b, ' ')
534 s.n(y)
535}
536
537// ---------------------------------------------------------------------------
538// Rendering
539// ---------------------------------------------------------------------------
540
541const (
542 cw = 600
543 ch = 760
544 gy = 584 // ground line
545 maxB = 250 // branch cap
546)
547
548// tree: spread (1/256 turn), ratio ‰, max depth, trunk length px, jitter,
549// lean, 3-split %, curve %.
550var tree = [][]int{
551 {24, 760, 8, 150, 22, 6, 30, 16},
552 {30, 700, 7, 116, 40, 12, 18, 30},
553 {22, 760, 7, 138, 20, 8, 18, 20},
554 {5, 900, 9, 64, 8, 3, 18, 5},
555}
556
557var barkCol = []string{"#857560", "#6b6252", "#4f3b2c", "#5a4434"}
558
559// taper is the branch width per depth, ‰ of the trunk.
560var taper = []int{1000, 640, 420, 280, 190, 130, 90, 62, 43, 30}
561
562var (
563 skyTopP = []string{"#cfe6e6", "#a9d0ea", "#e8cda6", "#d3dbe6"}
564 skyBotP = []string{"#f6f1de", "#f3efdc", "#f6e9d4", "#f1f3f3"}
565 sunP = []string{"#f6d36a", "#f8c94e", "#f2b25a", "#f2efe4"}
566 grassP = []string{"#8fb36a", "#7c9b4f", "#b39a5b", "#e6ebee"}
567 seaP = []string{"#7fb0c8", "#6aa6c8", "#7f9fb0", "#9fb3c2"}
568 wallP = []string{"#dccfb6", "#e0d2b6", "#d6c6aa", "#d2ccc2"}
569)
570
571// leaves[species][season]; a deciduous autumn list runs green -> rust.
572var leaves = [][][]string{
573 {{"#a8c96f", "#8fb85c"}, {"#5d8a3a", "#4c7a33", "#6f9a45"}, {"#8a9a3a", "#d6a43a", "#d0702a", "#a8461f"}, {}},
574 {{"#8a9a78", "#a3b08f"}, {"#7d8f6c", "#97a585"}, {"#7a8c68", "#94a382"}, {"#76866a", "#8c9a7e"}},
575 {{"#9cc46a", "#b4d27c"}, {"#5f8f3c", "#6f9c48", "#527f34"}, {"#a0a040", "#d9b84a", "#c99a3a", "#a0662a"}, {}},
576 {{"#3a5a3a", "#2f4a32"}, {"#2f4c34", "#24402a"}, {"#2f4a30", "#3a5536"}, {"#26402c", "#2f4a33"}},
577}
578
579var ribbonCol = []string{"#c0392b", "#f1e9d2", "#2e6da4", "#e0b03a", "#3f8a4a"}
580
581type branch struct {
582 x, y, cx, cy, ex, ey int
583 d, kids, ang, len int
584 u1, u2, jx, jy int
585}
586
587type painter struct {
588 sp, maxD, curve int
589 r, lr *rng
590 br []branch
591 gx, sc int
592}
593
594func (p *painter) add(x, y, ang, ln, d int) {
595 ex, ey := x+ln*cs(ang)/1024, y+ln*sn(ang)/1024
596 off := ln * p.r.between(-p.curve, p.curve) / 100
597 cx := (x+ex)/2 + off*cs(ang+64)/1024
598 cy := (y+ey)/2 + off*sn(ang+64)/1024
599 z := p.lr.next()
600 p.br = append(p.br, branch{
601 x: x, y: y, cx: cx, cy: cy, ex: ex, ey: ey, d: d, ang: ang, len: ln,
602 u1: int(z % 1000), u2: int((z >> 10) % 1000),
603 jx: int((z>>20)%9) - 4, jy: int((z>>24)%9) - 4,
604 })
605}
606
607func (p *painter) X(v int) int { return p.gx + v*p.sc/8000 }
608func (p *painter) Y(v int) int { return gy + v*p.sc/8000 }
609
610// SVG renders the artwork for in under cfg.
611func SVG(in engine.Input, cfg Config) string {
612 seed := in.Seed
613 t := derive(seed, cfg)
614 c := ClockOf(in)
615 sp := t.species
616 season, prog := c.Season, c.Progress
617 evergreen := sp == spOlive || sp == spCypress
618 white := t.mark == mkWhite
619
620 // growth: a square-root curve over three chain years
621 g := isqrt(imin(c.AgeDays, FullGrowthDays) * 1000000 / FullGrowthDays)
622 P := tree[sp]
623 spread, ratio, maxD, L0, jit, bend, split3 := P[0], P[1], P[2], P[3], P[4], P[5], P[6]
624
625 // tree structure from the seed alone (breadth first, so the cap trims the crown evenly)
626 p := &painter{sp: sp, maxD: maxD, curve: P[7], r: newRNG(seed + "|tree"), lr: newRNG(seed + "|leaf")}
627 R := p.r
628 p.add(0, 0, 192+R.between(-bend, bend), L0*8, 0)
629 for i := 0; i < len(p.br); i++ {
630 b := &p.br[i]
631 if b.d >= maxD-1 || b.len < 56 {
632 continue
633 }
634 n := 2
635 if b.d != 0 && R.intn(100) < split3 {
636 n = 3
637 }
638 if len(p.br)+n > maxB {
639 break
640 }
641 b.kids = n
642 bex, bey, bang, blen, bd := b.ex, b.ey, b.ang, b.len, b.d
643 for k := 0; k < n; k++ {
644 base := (k - 1) * spread
645 if n == 2 {
646 base = -spread
647 if k > 0 {
648 base = spread
649 }
650 }
651 na := bang + base + R.between(-jit, jit)/2
652 if sp == spCypress {
653 na += (192 - bang) / 2
654 }
655 p.add(bex, bey, na, blen*ratio*R.between(84, 110)/100000, bd+1)
656 }
657 }
658 br := p.br
659
660 // fit the grown tree into the frame
661 minX, maxX, minY := 0, 0, 0
662 for i := range br {
663 b := &br[i]
664 minX = imin(minX, imin(b.ex, b.cx))
665 maxX = imax(maxX, imax(b.ex, b.cx))
666 minY = imin(minY, imin(b.ey, b.cy))
667 }
668 LD := newRNG(seed + "|land")
669 gx := 300 - clampI((minX+maxX)/16, -60, 60)
670 fit := 1000
671 if minX < 0 {
672 fit = imin(fit, (gx-34)*8000/(-minX))
673 }
674 if maxX > 0 {
675 fit = imin(fit, (566-gx)*8000/maxX)
676 }
677 if minY < 0 {
678 fit = imin(fit, (gy-70)*8000/(-minY))
679 }
680 gx += LD.between(-12, 12)
681 p.gx = gx
682 p.sc = (520 + 480*g/1000) * fit / 1000
683 dvP := 1600 + (maxD*1000-1600)*g/1000
684 tw10 := (60 + 240*g/1000) * fit / 1000 // trunk width, tenths of a px
685
686 // visible geometry per branch (partial growth = de Casteljau split at f)
687 byD := make([]buf, maxD+1)
688 var tips []int // x, y, kind, branch index
689 trunkTopX, trunkTopY, fork := gx, gy, -1
690 for i := range br {
691 b := &br[i]
692 if b.d*1000 >= dvP {
693 continue
694 }
695 f := imin(1000, dvP-b.d*1000)
696 cx, cy, ex, ey := b.cx, b.cy, b.ex, b.ey
697 if f < 1000 {
698 ax, ay := b.x+(b.cx-b.x)*f/1000, b.y+(b.cy-b.y)*f/1000
699 bx, by := b.cx+(b.ex-b.cx)*f/1000, b.cy+(b.ey-b.cy)*f/1000
700 ex = ax + (bx-ax)*f/1000
701 ey = ay + (by-ay)*f/1000
702 cx, cy = ax, ay
703 }
704 bb := &byD[b.d]
705 bb.mv(p.X(b.x), p.Y(b.y))
706 bb.w("Q")
707 bb.n(p.X(cx))
708 bb.w(" ")
709 bb.n(p.Y(cy))
710 bb.w(" ")
711 bb.n(p.X(ex))
712 bb.w(" ")
713 bb.n(p.Y(ey))
714 if b.d == 0 {
715 trunkTopX, trunkTopY = p.X(ex), p.Y(ey)
716 }
717 if b.d == 2 && fork < 0 {
718 fork = i
719 }
720 terminal := (b.d+1)*1000 >= dvP || b.kids == 0
721 if b.d >= 1 && (terminal || b.d >= maxD-2 || (sp == spCypress && b.d >= 2)) {
722 kind := 1
723 if terminal {
724 kind = 0
725 }
726 tips = append(tips, p.X(ex)+b.jx, p.Y(ey)+b.jy, kind, i)
727 }
728 }
729 trunkW10 := imax(10, tw10)
730
731 // palette for this moment
732 skyTop, skyBot := seasonal(skyTopP, season, prog), seasonal(skyBotP, season, prog)
733 grass := seasonal(grassP, season, prog)
734 bark := barkCol[sp]
735 if white {
736 bark = "#dcd6ca"
737 }
738 barkLight := mix(bark, "#fff4e0", 320)
739
740 o := &buf{b: make([]byte, 0, 24576)}
741 o.w(`<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 600 760" width="600" height="760"><defs>`)
742 o.w(`<linearGradient id="sk" x1="0" y1="0" x2="0" y2="1"><stop offset="0" stop-color="` + skyTop +
743 `"/><stop offset="1" stop-color="` + skyBot + `"/></linearGradient>`)
744 o.w(`<radialGradient id="vg" cx=".5" cy=".45" r=".75"><stop offset=".6" stop-opacity="0"/><stop offset="1" stop-opacity=".22"/></radialGradient>`)
745 o.w(`<pattern id="so" width="40" height="22" patternUnits="userSpaceOnUse"><circle cx="8" cy="6" r="2.2" fill="#3a2a1e" fill-opacity=".3"/><circle cx="28" cy="16" r="1.4" fill="#e8d8c0" fill-opacity=".18"/></pattern>`)
746 o.w(`<g id="cl"><ellipse rx="46" ry="14"/><ellipse cx="-16" cy="-9" rx="22" ry="15"/><ellipse cx="14" cy="-13" rx="26" ry="19"/></g>`)
747 o.w(`<path id="lf" d="M0 0q4-3 8 0q-4 3-8 0z"/>`)
748 for d := range byD {
749 if !byD[d].empty() {
750 o.w(`<path id="b` + itoa(d) + `" d="`)
751 o.b = append(o.b, byD[d].b...)
752 o.w(`"/>`)
753 }
754 }
755
756 // leaves: colour groups as dot paths, drawn in three tones
757 pal := leaves[sp][season]
758 groups := make([]buf, len(pal))
759 var hl, blossom, flowers, fruit, balls buf
760 k := (450 + 550*g/1000) * fit / 1000
761 plane, judas := sp == spPlane, sp == spJudas
762 lwk := 11
763 switch sp {
764 case spPlane:
765 lwk = 22
766 case spJudas:
767 lwk = 20
768 case spOlive:
769 lwk = 12
770 }
771 lw := imax(5, lwk*k/1000)
772 for i := 0; i < len(tips); i += 4 {
773 x, y := tips[i], tips[i+1]
774 b := &br[tips[i+3]]
775 u1, u2 := b.u1, b.u2
776 show := len(pal) > 0
777 ci := u2 % imax(1, len(pal))
778 if !evergreen {
779 if season == 0 {
780 if judas {
781 show = u1 < (prog-300)*2
782 } else {
783 show = u1 < prog*17/10
784 }
785 } else if season == 2 {
786 show = u1 >= prog*95/100
787 ci = imin(3, (prog*3+u2)/1000)
788 }
789 }
790 if show {
791 gb := &groups[ci]
792 gb.mv(x, y)
793 switch sp {
794 case spCypress:
795 L := imax(6, 16*k/1000)
796 gb.w("v-")
797 gb.n(L)
798 gb.w("m-5 ")
799 gb.n(L - 3)
800 gb.w("v-")
801 gb.n(L - 2)
802 case spOlive:
803 gb.w("h.1m7 -5h.1m-13 9h.1")
804 default:
805 gb.w("h.1m")
806 gb.n(lw*6/10 + b.jy)
807 gb.w(" ")
808 gb.n(3 + b.jx)
809 gb.w("h.1m")
810 gb.n(-(lw * 11 / 10) - b.jx)
811 gb.w(" ")
812 gb.n(-(lw * 6 / 10))
813 gb.w("h.1")
814 }
815 if u2 < 450 {
816 hl.mv(x, y)
817 if sp == spCypress {
818 hl.w("v-6")
819 } else {
820 hl.w("h.1")
821 }
822 }
823 }
824 if judas && season == 0 {
825 bloom := 1000
826 if prog < 200 {
827 bloom = prog * 5
828 } else if prog >= 450 {
829 bloom = 1000 - (prog-450)*4
830 }
831 if u2 < bloom {
832 blossom.mv(x, y)
833 blossom.w("h.1")
834 blossom.mv(p.X(b.cx)+b.jy, p.Y(b.cy)+b.jx)
835 blossom.w("h.1")
836 }
837 }
838 if sp == spOlive && season == 0 && prog > 500 && prog < 900 && u2 < 400 {
839 flowers.mv(x+b.jy, y-3)
840 flowers.w("h.1")
841 }
842 if sp == spOlive && (season == 2 || (season == 3 && prog < 300)) && u2 < 300 {
843 fruit.mv(x-b.jy, y+5)
844 fruit.w("h.1")
845 }
846 if plane && (season == 3 || (season == 2 && prog > 300)) && u2 < 220 {
847 fruit.mv(x, y+4)
848 fruit.w("v7")
849 balls.mv(x, y+15)
850 balls.w("h.1")
851 }
852 }
853 anyLeaf := false
854 for i := range groups {
855 if !groups[i].empty() {
856 anyLeaf = true
857 o.w(`<path id="v` + itoa(i) + `" d="`)
858 o.b = append(o.b, groups[i].b...)
859 o.w(`"/>`)
860 }
861 }
862 if !hl.empty() {
863 o.w(`<path id="vh" d="`)
864 o.b = append(o.b, hl.b...)
865 o.w(`"/>`)
866 }
867 o.w(`</defs>`)
868
869 // sky, sun, clouds, birds
870 o.w(`<rect width="600" height="` + itoa(gy) + `" fill="url(#sk)"/>`)
871 sunX := LD.between(70, 530)
872 sunY := 175 - 85*sn(c.YearAngle)/1024
873 sun := seasonal(sunP, season, prog)
874 o.w(`<circle cx="` + itoa(sunX) + `" cy="` + itoa(sunY) + `" r="46" fill="` + sun + `" fill-opacity=".22"/><circle cx="` +
875 itoa(sunX) + `" cy="` + itoa(sunY) + `" r="26" fill="` + sun + `"/>`)
876 CR := newRNG(seed + "|sky|" + itoa(c.Day))
877 nClouds := CR.intn(4)
878 clouds := make([]int, 0, 9)
879 for i := 0; i < 3; i++ {
880 a := CR.between(40, 560)
881 b := CR.between(50, 240)
882 s := CR.between(7, 15)
883 clouds = append(clouds, a, b, s)
884 }
885 snowing := CR.intn(100) < 50
886 nBirds := CR.intn(3)
887 birds := make([]int, 0, 4)
888 for i := 0; i < 2; i++ {
889 a := CR.between(60, 540)
890 b := CR.between(70, 260)
891 birds = append(birds, a, b)
892 }
893 if nClouds > 0 {
894 op := ".72"
895 if season == 3 {
896 op = ".85"
897 }
898 o.w(`<g fill="#fff" opacity="` + op + `">`)
899 for i := 0; i < nClouds; i++ {
900 o.w(`<use href="#cl" transform="translate(` + itoa(clouds[i*3]) + ` ` + itoa(clouds[i*3+1]) + `) scale(` +
901 f10(clouds[i*3+2]) + `)"/>`)
902 }
903 o.w(`</g>`)
904 }
905 if (season == 0 || season == 1) && nBirds > 0 {
906 var d buf
907 for i := 0; i < nBirds; i++ {
908 d.mv(birds[i*2], birds[i*2+1])
909 d.w("q4 -5 8 0q4 -5 8 0")
910 }
911 o.w(`<path d="` + string(d.b) + `" stroke="#3a3530" stroke-width="1.5" fill="none" stroke-linecap="round"/>`)
912 }
913
914 // the setting
915 far, near := mix(grass, skyBot, 520), mix(grass, skyBot, 220)
916 switch t.setting {
917 case setHill:
918 a := LD.between(80, 260)
919 b2 := LD.between(330, 520)
920 o.w(`<path d="M0 ` + itoa(gy-70) + `Q` + itoa(a) + ` ` + itoa(gy-175) + ` ` + itoa(a+170) + ` ` + itoa(gy-92) +
921 `T600 ` + itoa(gy-120) + `V` + itoa(gy) + `H0z" fill="` + far + `"/>`)
922 o.w(`<path d="M0 ` + itoa(gy-22) + `Q` + itoa(b2-200) + ` ` + itoa(gy-96) + ` ` + itoa(b2) + ` ` + itoa(gy-34) +
923 `T600 ` + itoa(gy-40) + `V` + itoa(gy) + `H0z" fill="` + near + `"/>`)
924 case setCourtyard:
925 wall := seasonal(wallP, season, prog)
926 nx := LD.between(70, 530)
927 o.w(`<pattern id="bk" width="60" height="25" patternUnits="userSpaceOnUse"><path d="M0 .5H60M30 .5V12.5M0 12.5H60M.5 12.5V25" stroke="#000" stroke-opacity=".13" fill="none"/></pattern>`)
928 o.w(`<rect y="` + itoa(gy-150) + `" width="600" height="150" fill="` + wall + `"/><rect y="` + itoa(gy-150) +
929 `" width="600" height="150" fill="url(#bk)"/>`)
930 o.w(`<path d="M` + itoa(nx-24) + ` ` + itoa(gy) + `V` + itoa(gy-84) + `a24 24 0 0 1 48 0V` + itoa(gy) + `z" fill="` +
931 mix(wall, "#3a2a1e", 300) + `"/>`)
932 o.w(`<rect y="` + itoa(gy-158) + `" width="600" height="10" fill="` + mix(wall, "#6a5a44", 220) + `"/>`)
933 case setShore:
934 sea := seasonal(seaP, season, prog)
935 bx := LD.between(60, 500)
936 o.w(`<path d="M0 ` + itoa(gy-96) + `Q90 ` + itoa(gy-112) + ` 180 ` + itoa(gy-98) + `V` + itoa(gy-92) + `H0z" fill="` + far + `"/>`)
937 o.w(`<rect y="` + itoa(gy-94) + `" width="600" height="94" fill="` + sea + `"/>`)
938 o.w(`<path d="M0 ` + itoa(gy-70) + `h600M0 ` + itoa(gy-44) + `h600M0 ` + itoa(gy-20) +
939 `h600" stroke="#fff" stroke-opacity=".35" stroke-dasharray="18 34"/>`)
940 o.w(`<path d="M` + itoa(bx) + ` ` + itoa(gy-100) + `h30l-5 6h-21zM` + itoa(bx+14) + ` ` + itoa(gy-101) +
941 `v-24l13 22z" fill="#5a4a3a"/>`)
942 default:
943 var d buf
944 d.w("M0 ")
945 d.n(gy - 50)
946 for i := 1; i <= 8; i++ {
947 d.w("L")
948 d.n(i * 75)
949 d.w(" ")
950 d.n(gy - LD.between(30, 128))
951 }
952 o.w(`<path d="` + string(d.b) + `V` + itoa(gy) + `H0z" fill="` + mix(skyBot, "#5d6e86", 260) + `"/>`)
953 o.w(`<rect y="` + itoa(gy-26) + `" width="600" height="26" fill="` + mix(grass, "#d8b870", 360) + `"/>`)
954 }
955
956 // ground, soil, roots
957 o.w(`<rect y="` + itoa(gy-4) + `" width="600" height="12" fill="` + grass + `"/>`)
958 soilH := itoa(ch - gy - 6)
959 o.w(`<rect y="` + itoa(gy+6) + `" width="600" height="` + soilH + `" fill="#6b5240"/><rect y="` + itoa(gy+6) +
960 `" width="600" height="` + soilH + `" fill="url(#so)"/>`)
961 o.w(`<path d="M0 ` + itoa(gy+62) + `q150 -8 300 0t300 0M0 ` + itoa(gy+122) +
962 `q150 7 300 0t300 0" stroke="#4e3b2c" stroke-width="3" stroke-opacity=".45" fill="none"/>`)
963 if season != 3 {
964 var d buf
965 for i := 0; i < 18; i++ {
966 d.mv(LD.between(10, 590), gy-2)
967 d.w("l-3 -7m3 7l1 -9m-1 9l4 -6")
968 }
969 o.w(`<path d="` + string(d.b) + `" stroke="` + mix(grass, "#1e2a10", 350) +
970 `" stroke-width="1.4" fill="none" stroke-linecap="round"/>`)
971 }
972 // roots: one seeded structure; the current owner's roots (holding time)
973 // solid, the tree's lifetime roots (age) as a faint ghost below them
974 RR := newRNG(seed + "|roots")
975 var roots []int // x, y, ex, ey, d
976 rq := make([]int, 0, 5*189)
977 for i := 0; i < 3; i++ {
978 rq = append(rq, 0, 0, 64+(i-1)*30+RR.between(-6, 6), 46*8, 0)
979 }
980 for i := 0; i < len(rq); i += 5 {
981 x, y, ang, ln, d := rq[i], rq[i+1], rq[i+2], rq[i+3], rq[i+4]
982 ex, ey := x+ln*cs(ang)/1024, y+ln*sn(ang)/1024
983 roots = append(roots, x, y, ex, ey, d)
984 if d < 5 {
985 for s := -1; s <= 1; s += 2 {
986 rq = append(rq, ex, ey, clampI(ang+s*RR.between(10, 26), 10, 118), ln*72/100, d+1)
987 }
988 }
989 }
990 rs := 600 + 400*g/1000
991 rootPaths := func(depthP int) []buf {
992 ps := make([]buf, 6)
993 for i := 0; i < len(roots); i += 5 {
994 d := roots[i+4]
995 if d*1000 >= depthP {
996 continue
997 }
998 f := imin(1000, depthP-d*1000)
999 x, y := roots[i], roots[i+1]
1000 ex, ey := x+(roots[i+2]-x)*f/1000, y+(roots[i+3]-y)*f/1000
1001 ps[d].mv(gx+x*rs/8000, gy+6+y*rs/8000)
1002 ps[d].w("L")
1003 ps[d].n(gx + ex*rs/8000)
1004 ps[d].w(" ")
1005 ps[d].n(gy + 6 + ey*rs/8000)
1006 }
1007 return ps
1008 }
1009 ageRoots := 400 + imin(c.AgeDays, 730)*5600/730
1010 holdRoots := 400 + imin(c.HoldDays, FullRootsDays)*5600/FullRootsDays
1011 if ageRoots > holdRoots {
1012 o.w(`<path d="`)
1013 for _, ps := range rootPaths(ageRoots) {
1014 o.b = append(o.b, ps.b...)
1015 }
1016 o.w(`" stroke="#2a1c12" stroke-opacity=".28" stroke-width="1.5" fill="none" stroke-linecap="round"/>`)
1017 }
1018 rp := rootPaths(holdRoots)
1019 o.w(`<g stroke="#33231a" fill="none" stroke-linecap="round">`)
1020 for d := range rp {
1021 if !rp[d].empty() {
1022 o.w(`<path d="`)
1023 o.b = append(o.b, rp[d].b...)
1024 o.w(`" stroke-width="` + f10(imax(10, trunkW10*taper[d+1]/1600)) + `"/>`)
1025 }
1026 }
1027 o.w(`</g>`)
1028
1029 // trunk flare, branches, bark light
1030 tw := trunkW10/10 + 1
1031 o.w(`<path d="M` + itoa(gx-tw*16/10) + ` ` + itoa(gy+2) + `Q` + itoa(gx-tw/2) + ` ` + itoa(gy-1) + ` ` + itoa(gx-tw/2) + ` ` +
1032 itoa(gy-tw*13/10) + `H` + itoa(gx+tw/2) + `Q` + itoa(gx+tw/2) + ` ` + itoa(gy-1) + ` ` + itoa(gx+tw*16/10) + ` ` +
1033 itoa(gy+2) + `z" fill="` + bark + `"/>`)
1034 o.w(`<g stroke="` + bark + `" fill="none" stroke-linecap="round">`)
1035 for d := range byD {
1036 if !byD[d].empty() {
1037 o.w(`<use href="#b` + itoa(d) + `" stroke-width="` + f10(imax(8, trunkW10*taper[d]/1000)) + `"/>`)
1038 }
1039 }
1040 o.w(`</g><g stroke="` + barkLight + `" fill="none" stroke-linecap="round" stroke-opacity=".7">`)
1041 for d := range byD {
1042 wd := trunkW10 * taper[d] / 1000
1043 if !byD[d].empty() && wd >= 20 {
1044 o.w(`<use href="#b` + itoa(d) + `" stroke-width="` + f10(wd*35/100) + `" transform="translate(-` + f10(wd*22/100) + ` 0)"/>`)
1045 }
1046 }
1047 o.w(`</g>`)
1048 MK := newRNG(seed + "|marks")
1049 if plane && tw >= 8 && !white {
1050 o.w(`<g fill="#d9cfb2" fill-opacity=".55">`)
1051 for i := 0; i < 7; i++ {
1052 v := MK.between(80, 900)
1053 px := gx + (trunkTopX-gx)*v/1000 + MK.between(-(tw/3), tw/3)
1054 py := gy + (trunkTopY-gy)*v/1000
1055 o.w(`<ellipse cx="` + itoa(px) + `" cy="` + itoa(py) + `" rx="` + itoa(imax(2, tw/6)) + `" ry="` + itoa(imax(3, tw/3)) + `"/>`)
1056 }
1057 o.w(`</g>`)
1058 }
1059 if season == 3 {
1060 o.w(`<g stroke="#fbfcfd" fill="none" stroke-linecap="round">`)
1061 for d := 1; d < len(byD); d++ {
1062 wd := imax(8, trunkW10*taper[d]/1000)
1063 if !byD[d].empty() {
1064 o.w(`<use href="#b` + itoa(d) + `" stroke-width="` + f10(imax(6, wd*4/10)) + `" transform="translate(0 -` +
1065 f10(imax(4, wd*35/100)) + `)"/>`)
1066 }
1067 }
1068 o.w(`</g>`)
1069 }
1070 if c.AgeDays < 30 {
1071 // a sapling is staked and tied
1072 o.w(`<path d="M` + itoa(gx+9) + ` ` + itoa(gy+6) + `V` + itoa(gy-54) + `" stroke="#a0825a" stroke-width="3" stroke-linecap="round"/><path d="M` +
1073 itoa(gx) + ` ` + itoa(gy-40) + `h9M` + itoa(gx) + ` ` + itoa(gy-22) + `h9" stroke="#d8c49a" stroke-width="2"/>`)
1074 }
1075
1076 // the owners: notches, the first owner's seal, moss, ribbons, nest
1077 trunkH := gy - trunkTopY
1078 along := func(y int) int {
1079 if trunkH > 0 {
1080 return gx + (trunkTopX-gx)*(gy-y)/trunkH
1081 }
1082 return gx
1083 }
1084 if c.Moves == 0 {
1085 y := gy - trunkH*42/100
1086 r := imax(3, tw*3/10)
1087 o.w(`<circle cx="` + itoa(along(y)) + `" cy="` + itoa(y) + `" r="` + itoa(r) + `" fill="none" stroke="#2a1d14" stroke-width="1.6"/><circle cx="` +
1088 itoa(along(y)) + `" cy="` + itoa(y) + `" r="1.5" fill="#2a1d14"/>`)
1089 } else {
1090 room := imax(1, (trunkH*85/100-14)/5+1)
1091 n := imin(c.Moves, imin(25, room))
1092 nw := imax(5, tw+3)
1093 var d buf
1094 for i := 0; i < n; i++ {
1095 y := gy - 14 - i*5 - i/5*4
1096 d.mv(along(y)-nw/2, y)
1097 d.w("l")
1098 d.n(nw)
1099 d.w(" -3")
1100 }
1101 o.w(`<path d="` + string(d.b) + `" stroke="#2a1d14" stroke-width="1.6" stroke-linecap="round"/>`)
1102 }
1103 if c.HoldDays >= 180 {
1104 n := imin(24, 6+(c.HoldDays-180)/20)
1105 var d buf
1106 for i := 0; i < n; i++ {
1107 d.mv(gx-tw+(i*7)%imax(3, 2*tw), gy-(i*5)%22)
1108 d.w("h.1")
1109 }
1110 o.w(`<path d="` + string(d.b) + `" stroke="#5f8a3a" stroke-width="4" stroke-linecap="round"/>`)
1111 }
1112 if t.mark == mkLightning && trunkH > 20 {
1113 y0, h3 := gy-trunkH*88/100, trunkH/6
1114 zig := `<path d="M` + itoa(along(y0)) + ` ` + itoa(y0) + `l-4 ` + itoa(h3) + ` 6 4 -5 ` + itoa(h3) + ` 5 3 -3 ` + itoa(h3)
1115 o.w(zig + `" stroke="#2a1a10" stroke-width="4" fill="none" stroke-linejoin="round"/>`)
1116 o.w(zig + `" stroke="#f1e6c8" stroke-width="1.8" fill="none" stroke-linejoin="round"/>`)
1117 }
1118 if t.mark == mkWish {
1119 // every owner ties ribbons: more transfers, more ribbons
1120 want := imin(28, 4+c.Moves*3)
1121 ribbons := make([]buf, 5)
1122 n := 0
1123 for i := 0; i < len(br) && n < want; i++ {
1124 b := &br[i]
1125 if b.d < 1 || b.d > 3 || b.d*1000+1000 > dvP {
1126 continue
1127 }
1128 ln := 12 + b.u1%10
1129 rb := &ribbons[n%5]
1130 rb.mv(p.X(b.cx), p.Y(b.cy))
1131 rb.w("q")
1132 rb.n(b.jx)
1133 rb.w(" ")
1134 rb.n(ln / 2)
1135 rb.w(" ")
1136 rb.n(b.jy)
1137 rb.w(" ")
1138 rb.n(ln)
1139 n++
1140 }
1141 for i := range ribbons {
1142 if !ribbons[i].empty() {
1143 o.w(`<path d="` + string(ribbons[i].b) + `" stroke="` + ribbonCol[i] + `" stroke-width="3" fill="none" stroke-linecap="round"/>`)
1144 }
1145 }
1146 }
1147 if c.HoldDays >= FullRootsDays && fork >= 0 {
1148 b := &br[fork]
1149 x, y := p.X(b.x), p.Y(b.y)
1150 o.w(`<ellipse cx="` + itoa(x) + `" cy="` + itoa(y+3) + `" rx="13" ry="6" fill="#6a4a2a"/><path d="M` + itoa(x-12) + ` ` +
1151 itoa(y+1) + `q12 6 24 0" stroke="#3a2818" fill="none"/>`)
1152 if season == 0 {
1153 o.w(`<circle cx="` + itoa(x-4) + `" cy="` + itoa(y) + `" r="3" fill="#9fd0e0"/><circle cx="` + itoa(x+3) + `" cy="` +
1154 itoa(y) + `" r="3" fill="#9fd0e0"/>`)
1155 }
1156 }
1157
1158 // the crown
1159 if anyLeaf {
1160 base0 := pal[0]
1161 if white {
1162 base0 = mix(base0, "#f4f1e8", 550)
1163 }
1164 shade := mix(base0, "#1d2a14", 480)
1165 light := mix(base0, "#fff8e0", 380)
1166 if season == 3 {
1167 light = "#f6f8fa"
1168 }
1169 if sp == spCypress {
1170 // the cypress's flame: a closed column behind the sprays
1171 x0, x1, y0, y1 := tips[0], tips[0], tips[1], tips[1]
1172 for i := 0; i < len(tips); i += 4 {
1173 x0 = imin(x0, tips[i])
1174 x1 = imax(x1, tips[i])
1175 y0 = imin(y0, tips[i+1])
1176 y1 = imax(y1, tips[i+1])
1177 }
1178 mx, hw, top, bot := (x0+x1)/2, imax(8, (x1-x0)/2+6), y0-16, y1+6
1179 hh := bot - top
1180 fd := "M" + itoa(mx) + " " + itoa(top) + "C" + itoa(mx+hw) + " " + itoa(top+hh*35/100) + " " + itoa(mx+hw*9/10) + " " +
1181 itoa(bot) + " " + itoa(mx) + " " + itoa(bot+8) + "C" + itoa(mx-hw*9/10) + " " + itoa(bot) + " " + itoa(mx-hw) + " " +
1182 itoa(top+hh*35/100) + " " + itoa(mx) + " " + itoa(top) + "z"
1183 flame := pal[1]
1184 if white {
1185 flame = mix(pal[1], "#f4f1e8", 550)
1186 }
1187 o.w(`<path d="` + fd + `" fill="` + shade + `" fill-opacity=".6" transform="translate(2 3)"/><path d="` + fd + `" fill="` + flame + `"/>`)
1188 }
1189 o.w(`<g fill="none" stroke-linecap="round"><g stroke="` + shade + `" stroke-opacity=".6" stroke-width="` + itoa(lw+4) +
1190 `" transform="translate(2 3)">`)
1191 for i := range groups {
1192 if !groups[i].empty() {
1193 o.w(`<use href="#v` + itoa(i) + `"/>`)
1194 }
1195 }
1196 o.w(`</g><g stroke-width="` + itoa(lw) + `">`)
1197 for i := range groups {
1198 col := pal[i]
1199 if white {
1200 col = mix(col, "#f4f1e8", 550)
1201 }
1202 if !groups[i].empty() {
1203 o.w(`<use href="#v` + itoa(i) + `" stroke="` + col + `"/>`)
1204 }
1205 }
1206 o.w(`</g>`)
1207 if !hl.empty() {
1208 op := ".55"
1209 if season == 3 {
1210 op = ".9"
1211 }
1212 o.w(`<use href="#vh" stroke="` + light + `" stroke-opacity="` + op + `" stroke-width="` + itoa(imax(3, lw*45/100)) +
1213 `" transform="translate(-3 -4)"/>`)
1214 }
1215 o.w(`</g>`)
1216 }
1217 if !blossom.empty() {
1218 o.w(`<path d="` + string(blossom.b) + `" stroke="#c8457f" stroke-width="` + itoa(imax(5, 9*k/1000)) + `" stroke-linecap="round"/>`)
1219 }
1220 if !flowers.empty() {
1221 o.w(`<path d="` + string(flowers.b) + `" stroke="#f4f0e0" stroke-width="4" stroke-linecap="round"/>`)
1222 }
1223 if !fruit.empty() {
1224 if plane {
1225 o.w(`<path d="` + string(fruit.b) + `" stroke="#6e5236" stroke-width="1.2" stroke-linecap="round"/>`)
1226 } else {
1227 o.w(`<path d="` + string(fruit.b) + `" stroke="#3a2440" stroke-width="5" stroke-linecap="round"/>`)
1228 }
1229 }
1230 if !balls.empty() {
1231 o.w(`<path d="` + string(balls.b) + `" stroke="#7a5a3a" stroke-width="6" stroke-linecap="round"/>`)
1232 }
1233
1234 // fallen leaves, snow
1235 if !evergreen && (season == 2 || season == 3) {
1236 FR := newRNG(seed + "|fall|" + itoa(season))
1237 n := 8
1238 fall := []string{"#8a6a4a"}
1239 if season == 2 {
1240 n = 6 + prog*44/1000
1241 fall = leaves[sp][2][1:]
1242 }
1243 lg := make([]buf, len(fall))
1244 for i := 0; i < n; i++ {
1245 x := FR.between(70, 530)
1246 var y int
1247 if i%4 == 0 && season == 2 {
1248 y = FR.between(gy-260, gy-20)
1249 } else {
1250 y = gy + FR.between(-4, 5)
1251 }
1252 l := &lg[i%len(lg)]
1253 l.w(`<use href="#lf" x="`)
1254 l.n(x)
1255 l.w(`" y="`)
1256 l.n(y)
1257 l.w(`"/>`)
1258 }
1259 for i := range lg {
1260 if !lg[i].empty() {
1261 o.w(`<g fill="` + fall[i] + `">` + string(lg[i].b) + `</g>`)
1262 }
1263 }
1264 }
1265 if season == 3 {
1266 o.w(`<path d="M0 ` + itoa(gy-5) + `h600v10H0z" fill="#f7f9fb"/>`)
1267 if snowing {
1268 SR := newRNG(seed + "|snow|" + itoa(c.Day))
1269 var d buf
1270 for i := 0; i < 40; i++ {
1271 x := SR.between(5, 595)
1272 y := SR.between(10, gy-10)
1273 d.mv(x, y)
1274 d.w("h.1")
1275 }
1276 o.w(`<path d="` + string(d.b) + `" stroke="#fff" stroke-width="3.5" stroke-linecap="round" stroke-opacity=".9"/>`)
1277 }
1278 }
1279
1280 // frame, chain-time strip, year dial
1281 o.w(`<rect width="600" height="760" fill="url(#vg)"/>`)
1282 o.w(`<rect y="` + itoa(ch-30) + `" width="600" height="30" fill="#1c140e" fill-opacity=".4"/>`)
1283 tr := " transfers"
1284 if c.Moves == 1 {
1285 tr = " transfer"
1286 }
1287 o.w(`<text x="16" y="` + itoa(ch-11) + `" font-family="Menlo, monospace" font-size="11" fill="#f4ecdf">block ` + itoa(c.Height) +
1288 ` · ` + seasonNames[season] + ` · age ` + itoa(c.AgeDays) + ` d · rooted ` + itoa(c.HoldDays) + ` d · ` + itoa(c.Moves) + tr + `</text>`)
1289 dx, dy := cw-22, ch-15
1290 o.w(`<g fill="none" stroke-width="2.5"><path d="M` + itoa(dx) + ` ` + itoa(dy-9) + `a9 9 0 0 1 9 9" stroke="#9fc46a"/><path d="M` +
1291 itoa(dx+9) + ` ` + itoa(dy) + `a9 9 0 0 1 -9 9" stroke="#f2c94e"/><path d="M` + itoa(dx) + ` ` + itoa(dy+9) +
1292 `a9 9 0 0 1 -9 -9" stroke="#d0702a"/><path d="M` + itoa(dx-9) + ` ` + itoa(dy) + `a9 9 0 0 1 9 -9" stroke="#e6ebee"/></g>`)
1293 ha := 192 + c.YearAngle
1294 o.w(`<path d="M` + itoa(dx) + ` ` + itoa(dy) + `L` + itoa(dx+8*cs(ha)/1024) + ` ` + itoa(dy+8*sn(ha)/1024) +
1295 `" stroke="#f4ecdf" stroke-width="1.6" stroke-linecap="round"/>`)
1296 o.w(`</svg>`)
1297 return string(o.b)
1298}