Skip to content Search Apps Documentation Source Content Run Reset Eval Playground File Folder Download Copy Actions State String Boolean Number Struct Map Slice Pointer Function Closure Reference Nil Package Type Interface Unknown

Community realm. Read the code first. Community realm, deployed by its author. Read the code before you interact or send coins.

genesis.gno

12.35 Kb · 445 lines
  1// Package genesis is the platform's first engine: a pure p/ port of
  2// the prototype's gen1 engine. The RNG stream, trait tables,
  3// draw order and SVG drawing are byte-for-byte nftgen1's, so a seed renders
  4// the same artwork here as there; genesis_test.gno pins trait vectors
  5// computed independently from the same algorithm.
  6//
  7// What changed is only the packaging (RA L-7): it is a stateless p/
  8// package implementing engine/v2, the seed comes from core's commit–reveal
  9// (so the salt/seed helpers and the gnoweb Render of nftgen1 are gone), and
 10// the engine takes no params.
 11package genesis
 12
 13import (
 14	"errors"
 15	"strconv"
 16	"strings"
 17
 18	"gno.land/p/nt/ufmt/v0"
 19
 20	"gno.land/p/g1jz6j9hj6rdf5ltl2kcrpa4jwtdh3w30z0yul06/sinopia4/engine/v2"
 21)
 22
 23// TraitTypes in the order Traits returns them.
 24var traitTypes = []string{"Palette", "Shape", "Ground", "Frame", "Finish", "Aura", "Elements", "Symmetry", "Score", "Rarity"}
 25
 26// MaxSVGBytes: 8 symmetry x 14 elements of the widest primitive plus the
 27// densest ground and frame stays well under this (gen1 tested < 32,000).
 28const MaxSVGBytes = 32768
 29
 30// Manifest describes the engine. Author is left empty: the adapter realm
 31// that registers this engine sets the address that will ClaimEngine.
 32func Manifest() engine.Manifest {
 33	return engine.Manifest{
 34		Name:    "Genesis",
 35		Version: "1.0.0",
 36		Description: "Fully on-chain generative engine, nftgen1 lineage: FNV-1a + splitmix64 stream, " +
 37			"10 traits, 1,254,400 combinations, score-based rarity over every layer.",
 38		License:      "CC-BY-NC-4.0",
 39		ParamsSchema: `{"const":""}`,
 40		TraitTypes:   append([]string{}, traitTypes...),
 41		Living:       false,
 42		MaxSVGBytes:  MaxSVGBytes,
 43		Kind:         engine.KindSVG,
 44	}
 45}
 46
 47// ValidateParams: genesis has no parameters.
 48func ValidateParams(params string) error {
 49	if params != "" {
 50		return errors.New("genesis takes no params")
 51	}
 52	return nil
 53}
 54
 55// Traits returns the ten traits of in.Seed in TraitTypes order.
 56func Traits(in engine.Input) []engine.Trait {
 57	t := TraitsOf(in.Seed)
 58	return []engine.Trait{
 59		{Type: "Palette", Value: t.Palette},
 60		{Type: "Shape", Value: t.Shape},
 61		{Type: "Ground", Value: t.Ground},
 62		{Type: "Frame", Value: t.Frame},
 63		{Type: "Finish", Value: t.Finish},
 64		{Type: "Aura", Value: t.Aura},
 65		{Type: "Elements", Value: strconv.Itoa(t.Elements)},
 66		{Type: "Symmetry", Value: strconv.Itoa(t.Symmetry)},
 67		{Type: "Score", Value: strconv.Itoa(t.Score)},
 68		{Type: "Rarity", Value: t.Rarity},
 69	}
 70}
 71
 72// Render returns the raw SVG for in.Seed.
 73func Render(in engine.Input) string { return SVG(in.Seed) }
 74
 75// ---------------------------------------------------------------------------
 76// Deterministic randomness — FNV-1a over the seed, then a splitmix64 mixer.
 77// Pure integer math: no floats, no time, no block data.
 78// ---------------------------------------------------------------------------
 79
 80const (
 81	fnvOffset uint64 = 14695981039346656037
 82	fnvPrime  uint64 = 1099511628211
 83)
 84
 85type rng struct {
 86	state uint64
 87}
 88
 89func newRNG(seed string) *rng {
 90	h := fnvOffset
 91	for i := 0; i < len(seed); i++ {
 92		h ^= uint64(seed[i])
 93		h *= fnvPrime
 94	}
 95	if h == 0 {
 96		h = fnvPrime
 97	}
 98	return &rng{state: h}
 99}
100
101func (r *rng) next() uint64 {
102	r.state += 0x9E3779B97F4A7C15
103	z := r.state
104	z = (z ^ (z >> 30)) * 0xBF58476D1CE4E5B9
105	z = (z ^ (z >> 27)) * 0x94D049BB133111EB
106	return z ^ (z >> 31)
107}
108
109// intn returns a value in [0, n).
110func (r *rng) intn(n int) int {
111	if n <= 0 {
112		return 0
113	}
114	return int(r.next() % uint64(n))
115}
116
117// ---------------------------------------------------------------------------
118// Weighted trait tables (weights are explicit; one draw walks the sum).
119// ---------------------------------------------------------------------------
120
121type option struct {
122	name   string
123	weight int
124	score  int // contribution to the rarity score
125}
126
127func pick(r *rng, table []option) option {
128	total := 0
129	for _, o := range table {
130		total += o.weight
131	}
132	roll := r.intn(total)
133	acc := 0
134	for _, o := range table {
135		acc += o.weight
136		if roll < acc {
137			return o
138		}
139	}
140	return table[len(table)-1]
141}
142
143type palette struct {
144	option
145	bg  string
146	ink string
147	c1  string
148	c2  string
149	c3  string
150}
151
152// Palette weights sum to 100, so a weight reads directly as a percent.
153var palettes = []palette{
154	{option{"Emerald", 18, 0}, "#04140f", "#7cf2c4", "#10b981", "#34d399", "#065f46"},
155	{option{"Amethyst", 18, 0}, "#0b0713", "#e9d5ff", "#8b5cf6", "#c084fc", "#4c1d95"},
156	{option{"Abyss", 16, 0}, "#02101c", "#bae6fd", "#0ea5e9", "#38bdf8", "#0c4a6e"},
157	{option{"Ember", 14, 0}, "#160806", "#fecaca", "#f97316", "#fb923c", "#7c2d12"},
158	{option{"Bone", 12, 0}, "#f5f5f4", "#1c1917", "#78716c", "#a8a29e", "#292524"},
159	{option{"Solar", 10, 0}, "#1a1403", "#fef08a", "#eab308", "#facc15", "#713f12"},
160	{option{"Obsidian", 8, 2}, "#050505", "#e5e5e5", "#404040", "#737373", "#171717"},
161	{option{"Prism", 4, 4}, "#0a0a12", "#fdf4ff", "#22d3ee", "#f472b6", "#a78bfa"},
162}
163
164var shapes = []option{
165	{"Orbit", 20, 0},
166	{"Lattice", 18, 0},
167	{"Bloom", 18, 0},
168	{"Shard", 16, 0},
169	{"Spiral", 14, 0},
170	{"Weave", 9, 2},
171	{"Rift", 5, 4},
172}
173
174var grounds = []option{
175	{"Flat", 30, 0},
176	{"Halo", 25, 0},
177	{"Grid", 22, 0},
178	{"Rings", 14, 1},
179	{"Scatter", 9, 2},
180}
181
182var frames = []option{
183	{"None", 55, 0},
184	{"Thin", 25, 1},
185	{"Double", 14, 2},
186	{"Ornate", 6, 4},
187}
188
189var finishes = []option{
190	{"Matte", 50, 0},
191	{"Gloss", 28, 1},
192	{"Glow", 16, 2},
193	{"Foil", 6, 4},
194}
195
196var auras = []option{
197	{"None", 78, 0},
198	{"Soft", 14, 2},
199	{"Bright", 6, 4},
200	{"Nova", 2, 7},
201}
202
203// TraitSet is the decoded properties of a seed. The SVG builder and the
204// trait list both read this one struct, so picture and metadata cannot
205// describe different artwork.
206type TraitSet struct {
207	Palette  string
208	Shape    string
209	Ground   string
210	Frame    string
211	Finish   string
212	Aura     string
213	Elements int
214	Symmetry int
215	Score    int
216	Rarity   string
217}
218
219// Tier cut-offs, chosen on nftgen1's 1000-seed simulation: Common 21.4% /
220// Uncommon 44.4% / Rare 25.2% / Legendary 7.6% / Mythic 1.4%.
221const (
222	scoreMythic    = 12
223	scoreLegendary = 8
224	scoreRare      = 5
225	scoreUncommon  = 2
226)
227
228func derive(seed string) (TraitSet, palette) {
229	r := newRNG(seed)
230
231	// Draw order is part of the contract: changing it changes every token.
232	p := palettes[0]
233	{
234		total := 0
235		for _, q := range palettes {
236			total += q.weight
237		}
238		roll := r.intn(total)
239		acc := 0
240		for _, q := range palettes {
241			acc += q.weight
242			if roll < acc {
243				p = q
244				break
245			}
246		}
247	}
248	shape := pick(r, shapes)
249	ground := pick(r, grounds)
250	frame := pick(r, frames)
251	finish := pick(r, finishes)
252	aura := pick(r, auras)
253
254	t := TraitSet{
255		Palette:  p.name,
256		Shape:    shape.name,
257		Ground:   ground.name,
258		Frame:    frame.name,
259		Finish:   finish.name,
260		Aura:     aura.name,
261		Elements: 5 + r.intn(10), // 5..14
262		Symmetry: 2 + r.intn(7),  // 2..8
263	}
264
265	score := p.score + shape.score + ground.score + frame.score + finish.score + aura.score
266	if t.Elements >= 13 {
267		score++
268	}
269	if t.Symmetry >= 7 {
270		score++
271	}
272	t.Score = score
273
274	switch {
275	case score >= scoreMythic:
276		t.Rarity = "Mythic"
277	case score >= scoreLegendary:
278		t.Rarity = "Legendary"
279	case score >= scoreRare:
280		t.Rarity = "Rare"
281	case score >= scoreUncommon:
282		t.Rarity = "Uncommon"
283	default:
284		t.Rarity = "Common"
285	}
286	return t, p
287}
288
289// TraitsOf returns the decoded traits for a seed.
290func TraitsOf(seed string) TraitSet {
291	t, _ := derive(seed)
292	return t
293}
294
295func finishStroke(finish string) int {
296	switch finish {
297	case "Gloss":
298		return 4
299	case "Glow":
300		return 5
301	case "Foil":
302		return 6
303	default: // Matte
304		return 3
305	}
306}
307
308func auraRing(aura string) (int, int) { // width, opacity percent
309	switch aura {
310	case "Soft":
311		return 4, 22
312	case "Bright":
313		return 8, 38
314	case "Nova":
315		return 14, 60
316	default:
317		return 0, 0
318	}
319}
320
321// SVG renders the artwork for a seed as a standalone SVG document.
322func SVG(seed string) string {
323	t, p := derive(seed)
324	r := newRNG(seed + "|art") // separate stream: trait draws never shift the art
325
326	var b strings.Builder
327	b.WriteString(`<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512" width="512" height="512">`)
328	b.WriteString(ufmt.Sprintf(`<rect width="512" height="512" fill="%s"/>`, p.bg))
329
330	// --- ground layer ---
331	switch t.Ground {
332	case "Halo":
333		b.WriteString(ufmt.Sprintf(
334			`<circle cx="256" cy="256" r="200" fill="none" stroke="%s" stroke-width="1" opacity="0.35"/>`, p.c3))
335	case "Grid":
336		for i := 64; i < 512; i += 64 {
337			b.WriteString(ufmt.Sprintf(
338				`<line x1="%d" y1="0" x2="%d" y2="512" stroke="%s" stroke-width="1" opacity="0.18"/>`, i, i, p.c3))
339			b.WriteString(ufmt.Sprintf(
340				`<line x1="0" y1="%d" x2="512" y2="%d" stroke="%s" stroke-width="1" opacity="0.18"/>`, i, i, p.c3))
341		}
342	case "Rings":
343		for rad := 60; rad <= 240; rad += 45 {
344			b.WriteString(ufmt.Sprintf(
345				`<circle cx="256" cy="256" r="%d" fill="none" stroke="%s" stroke-width="1" opacity="0.22"/>`, rad, p.c3))
346		}
347	case "Scatter":
348		for i := 0; i < 40; i++ {
349			b.WriteString(ufmt.Sprintf(
350				`<circle cx="%d" cy="%d" r="2" fill="%s" opacity="0.30"/>`,
351				16+r.intn(480), 16+r.intn(480), p.c3))
352		}
353	}
354
355	// --- aura layer ---
356	if w, op := auraRing(t.Aura); w > 0 {
357		b.WriteString(ufmt.Sprintf(
358			`<circle cx="256" cy="256" r="230" fill="none" stroke="%s" stroke-width="%d" opacity="0.%d"/>`,
359			p.ink, w, op))
360	}
361
362	// --- composition: one pass, mirrored Symmetry times ---
363	stroke := finishStroke(t.Finish)
364	step := 360 / t.Symmetry
365	for s := 0; s < t.Symmetry; s++ {
366		b.WriteString(ufmt.Sprintf(`<g transform="rotate(%d 256 256)">`, s*step))
367		for e := 0; e < t.Elements; e++ {
368			color := p.c1
369			switch r.intn(3) {
370			case 1:
371				color = p.c2
372			case 2:
373				color = p.ink
374			}
375			x := 96 + r.intn(320)
376			y := 96 + r.intn(320)
377			size := 12 + r.intn(70)
378			opacity := 40 + r.intn(55) // percent, integer only
379			if t.Finish == "Glow" || t.Finish == "Foil" {
380				opacity = 55 + r.intn(40)
381			}
382
383			switch t.Shape {
384			case "Orbit":
385				b.WriteString(ufmt.Sprintf(
386					`<circle cx="%d" cy="%d" r="%d" fill="none" stroke="%s" stroke-width="%d" opacity="0.%d"/>`,
387					x, y, size, color, stroke, opacity))
388			case "Lattice":
389				b.WriteString(ufmt.Sprintf(
390					`<rect x="%d" y="%d" width="%d" height="%d" fill="none" stroke="%s" stroke-width="%d" opacity="0.%d"/>`,
391					x, y, size, size, color, stroke-1, opacity))
392			case "Bloom":
393				b.WriteString(ufmt.Sprintf(
394					`<circle cx="%d" cy="%d" r="%d" fill="%s" opacity="0.%d"/>`,
395					x, y, size/2, color, opacity))
396			case "Shard":
397				b.WriteString(ufmt.Sprintf(
398					`<polygon points="%d,%d %d,%d %d,%d" fill="%s" opacity="0.%d"/>`,
399					x, y, x+size, y+size/2, x+size/3, y+size, color, opacity))
400			case "Weave":
401				b.WriteString(ufmt.Sprintf(
402					`<line x1="%d" y1="%d" x2="%d" y2="%d" stroke="%s" stroke-width="%d" opacity="0.%d"/>`,
403					x, y, x+size, y+size/3, color, stroke, opacity))
404				b.WriteString(ufmt.Sprintf(
405					`<line x1="%d" y1="%d" x2="%d" y2="%d" stroke="%s" stroke-width="%d" opacity="0.%d"/>`,
406					x+size, y, x, y+size/3, color, stroke, opacity))
407			case "Rift":
408				b.WriteString(ufmt.Sprintf(
409					`<path d="M%d %d l %d %d l %d %d l %d %d" fill="none" stroke="%s" stroke-width="%d" opacity="0.%d"/>`,
410					x, y, size/2, -size/3, size/3, size, size/2, -size/2,
411					color, stroke, opacity))
412			default: // Spiral
413				b.WriteString(ufmt.Sprintf(
414					`<path d="M%d %d q %d %d %d %d" fill="none" stroke="%s" stroke-width="%d" opacity="0.%d"/>`,
415					x, y, size, -size, size*2, 0, color, stroke, opacity))
416			}
417		}
418		b.WriteString(`</g>`)
419	}
420
421	// --- frame layer, drawn last so it always sits on top ---
422	switch t.Frame {
423	case "Thin":
424		b.WriteString(ufmt.Sprintf(
425			`<rect x="12" y="12" width="488" height="488" fill="none" stroke="%s" stroke-width="2" opacity="0.55"/>`, p.ink))
426	case "Double":
427		b.WriteString(ufmt.Sprintf(
428			`<rect x="10" y="10" width="492" height="492" fill="none" stroke="%s" stroke-width="3" opacity="0.60"/>`, p.ink))
429		b.WriteString(ufmt.Sprintf(
430			`<rect x="24" y="24" width="464" height="464" fill="none" stroke="%s" stroke-width="1" opacity="0.45"/>`, p.c2))
431	case "Ornate":
432		b.WriteString(ufmt.Sprintf(
433			`<rect x="8" y="8" width="496" height="496" fill="none" stroke="%s" stroke-width="4" opacity="0.70"/>`, p.ink))
434		b.WriteString(ufmt.Sprintf(
435			`<rect x="22" y="22" width="468" height="468" fill="none" stroke="%s" stroke-width="2" opacity="0.55"/>`, p.c2))
436		for _, c := range [][2]int{{8, 8}, {504, 8}, {8, 504}, {504, 504}} {
437			b.WriteString(ufmt.Sprintf(
438				`<circle cx="%d" cy="%d" r="10" fill="none" stroke="%s" stroke-width="3" opacity="0.80"/>`,
439				c[0], c[1], p.c1))
440		}
441	}
442
443	b.WriteString(`</svg>`)
444	return b.String()
445}