-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathengine.ts
More file actions
222 lines (193 loc) · 8.71 KB
/
engine.ts
File metadata and controls
222 lines (193 loc) · 8.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
import { TITLES, PERSONALITY_PHRASES } from './config.js';
// --- 1. TYPES ---
export type PetBody = 'slime' | 'cube' | 'wisp' | 'mecha-spider' | 'orb' | 'crystal' | 'pyramid' | 'cloud' | 'ghost' | 'dragon-egg';
export type PetAura = 'none' | 'fire' | 'digital-glitch' | 'shadow' | 'rainbow' | 'stars' | 'rain' | 'plasma' | 'leaves' | 'void' | 'nebula';
export type PetMutation =
'horns' | 'halo' | 'bat-wings' | 'spikes' | 'tail' | 'fins' | 'antenna' |
'shield' | 'sword' | 'magic-wand' | 'wiggle' | 'angel-wings' | 'demon-wings' |
'third-eye' | 'hover-bots' | 'leafy-tail' | 'gem-core' | 'beard' | 'mustache';
export type PetAccessory =
'none' | 'hat' | 'scarf' | 'glasses' | 'tie' | 'cape' | 'crown' |
'monocle' | 'headphones' | 'backpack' | 'wizard-hat' | 'viking-helmet' |
'detective-pipe' | 'flower' | 'cyber-mask';
export type PetPattern = 'solid' | 'stripes' | 'dots' | 'gradient-shift' | 'circuit' | 'honeycomb' | 'star-field' | 'waves' | 'lava' | 'glitch-static';
export type PetPersonality = keyof typeof PERSONALITY_PHRASES;
export type PetEye = 'normal' | 'large' | 'narrow' | 'angry' | 'cute' | 'cyclops' | 'googly' | 'sparkle' | 'robot';
export interface PetState {
body: PetBody;
color: string;
aura: PetAura;
mutations: PetMutation[];
accessory: PetAccessory;
pattern: PetPattern;
complexity: number;
personality: PetPersonality;
title: string;
evolutionTier: number;
growthLabel: string;
colorShift: number; // For hue rotation evolution
dnaLength: number;
totalCommits: number;
observedDays: number;
efficiencyRank?: string; // e.g. "Record Holder", "Worst Month", "#2 in 2026"
eyeType: PetEye;
}
export interface CollectionPet {
signature: string;
username: string;
year: string;
month: string;
enabled: boolean;
addedAt: number;
totalCommits?: number;
dnaLength?: number;
observedDays?: number;
}
export interface PetParts {
bodies: PetBody[];
colors: string[];
premiumColors: string[];
auras: PetAura[];
mutations: PetMutation[];
accessories: PetAccessory[];
patterns: PetPattern[];
}
// --- 2. SEEDED PRNG ---
export function seededRandom(seed: number): () => number {
return function() {
var t = seed += 0x6D2B79F5;
t = Math.imul(t ^ t >>> 15, t | 1);
t ^= t + Math.imul(t ^ t >>> 7, t | 61);
return ((t ^ t >>> 14) >>> 0) / 4294967296;
}
}
export function pickRandom<T>(array: T[], randomFunc: () => number): T {
return array[Math.floor(randomFunc() * array.length)];
}
function hashString(str: string): number {
let hash = 0;
for (let i = 0; i < str.length; i++) {
hash = ((hash << 5) - hash) + str.charCodeAt(i);
hash |= 0;
}
return Math.abs(hash);
}
// --- 3. PROCEDURAL ENGINE ---
export const PET_PARTS: PetParts = {
bodies: ['slime', 'cube', 'wisp', 'mecha-spider', 'orb', 'crystal', 'pyramid', 'cloud', 'ghost'],
colors: [
'#FF3388', '#33FFCC', '#FFDD00', '#CC66FF',
'#FF8833', '#88FF33', '#33CCFF', '#FF33FF',
'#E74C3C', '#2ECC71', '#3498DB', '#F1C40F'
],
premiumColors: [
'#FFFFFF', '#FFCC66', '#66FF66', '#66CCFF',
'#FF99AA', '#99FFCC', '#FFEE99', '#CC99FF',
'#00FF00', '#00FFFF', '#FF00FF', '#FFFF00',
'#C0C0C0', '#F39C12', '#D35400', '#8E44AD'
],
auras: ['none', 'fire', 'digital-glitch', 'shadow', 'rainbow', 'stars', 'rain', 'plasma', 'leaves', 'void', 'nebula'],
mutations: ['horns', 'halo', 'bat-wings', 'spikes', 'tail', 'fins', 'antenna', 'shield', 'sword', 'magic-wand', 'wiggle', 'angel-wings', 'demon-wings', 'third-eye', 'hover-bots', 'leafy-tail', 'gem-core', 'beard', 'mustache'],
accessories: ['none', 'hat', 'scarf', 'glasses', 'tie', 'cape', 'crown', 'monocle', 'headphones', 'backpack', 'wizard-hat', 'viking-helmet', 'detective-pipe', 'flower', 'cyber-mask'],
patterns: ['solid', 'stripes', 'dots', 'gradient-shift', 'circuit', 'honeycomb', 'star-field', 'waves', 'lava', 'glitch-static'],
eyes: ['normal', 'large', 'narrow', 'angry', 'cute', 'cyclops', 'googly', 'sparkle', 'robot']
};
const PERSONALITIES = Object.keys(PERSONALITY_PHRASES) as PetPersonality[];
export function generateProceduralPet(hexString: string, salt: string = "", totalCommitsOverride?: number, dnaLengthOverride?: number, observedDaysOverride?: number): PetState {
const dna = hexString;
// Identity should be stable for the month, using only salt
const identitySeed = hashString(salt);
const idRng = seededRandom(identitySeed);
// Evolution seed depends on the growth (dna)
const evolutionSeed = hashString(dna + salt);
const evRng = seededRandom(evolutionSeed);
let totalCommits = 0;
if (totalCommitsOverride !== undefined) {
totalCommits = totalCommitsOverride;
} else {
for (const char of dna) {
const val = parseInt(char, 16);
if (!isNaN(val)) totalCommits += val;
}
}
const complexity = Math.min(5, Math.floor(totalCommits / 15));
// New Evolution Logic: Grow from Hatchling to Legendary within the month
const consistency = dnaLengthOverride !== undefined ? dnaLengthOverride : dna.length;
let evolutionTier = 0;
let growthLabel = "Egg Hatchling";
if (consistency >= 21 || totalCommits >= 100) {
evolutionTier = 3;
growthLabel = "Legendary Growth";
} else if (consistency >= 14 || totalCommits >= 50) {
evolutionTier = 2;
growthLabel = "Elite Resident";
} else if (consistency >= 7 || totalCommits >= 20) {
evolutionTier = 1;
growthLabel = "Growing Fledgling";
}
// Use idRng for stable traits
const hatchedBody = pickRandom(PET_PARTS.bodies, idRng);
const body = evolutionTier === 0 ? 'dragon-egg' : hatchedBody;
const title = `${pickRandom(TITLES.prefixes, idRng)} ${pickRandom(TITLES.suffixes, idRng)}`;
const personality = pickRandom(PERSONALITIES, idRng);
// Color evolution: use premium colors at higher complexity
const colorPool = complexity >= 3 ? PET_PARTS.premiumColors : PET_PARTS.colors;
// We use idRng for color so it's stable, or maybe it should evolve?
// Let's use idRng for base color so it doesn't flip wildly,
// but the pool choice still depends on complexity.
const baseColor = pickRandom(colorPool, idRng);
// Color shift based on DNA length (progression through the month)
const colorShift = (dna.length * 10) % 360;
let petVisuals: PetState = {
body,
color: baseColor,
aura: 'none',
mutations: [],
accessory: 'none',
pattern: pickRandom(PET_PARTS.patterns, idRng),
complexity,
personality,
title,
evolutionTier,
growthLabel,
colorShift,
dnaLength: consistency,
totalCommits,
observedDays: observedDaysOverride || (dnaLengthOverride !== undefined ? dnaLengthOverride : (dna.length > 0 ? dna.length : 28)),
eyeType: pickRandom(PET_PARTS.eyes, idRng)
};
// Evolution chain additions (only if hatched)
if (evolutionTier > 0) {
const modRng = seededRandom(evolutionSeed + 123);
// Tier 1+: Guaranteed 1st mutation
if (evolutionTier >= 1) petVisuals.mutations.push(pickRandom(PET_PARTS.mutations, modRng));
// Tier 2+: Guaranteed accessory + 2nd mutation
if (evolutionTier >= 2) {
petVisuals.accessory = pickRandom(PET_PARTS.accessories, modRng);
const mut2 = pickRandom(PET_PARTS.mutations, seededRandom(evolutionSeed + 456));
if (!petVisuals.mutations.includes(mut2)) petVisuals.mutations.push(mut2);
}
// Tier 3: Guaranteed aura + 3rd mutation
if (evolutionTier >= 3) {
petVisuals.aura = pickRandom(PET_PARTS.auras, modRng);
const mut3 = pickRandom(PET_PARTS.mutations, seededRandom(evolutionSeed + 789));
if (!petVisuals.mutations.includes(mut3)) petVisuals.mutations.push(mut3);
}
for (let i = 0; i < dna.length; i++) {
const commitLevel = parseInt(dna[i], 16);
if (isNaN(commitLevel)) continue;
if (commitLevel >= 12) {
const mRng = seededRandom(evolutionSeed + i + 1);
const newMutation = pickRandom(PET_PARTS.mutations, mRng);
if (!petVisuals.mutations.includes(newMutation)) {
petVisuals.mutations.push(newMutation);
}
}
if (commitLevel === 11 && petVisuals.accessory === 'none') {
const aRng = seededRandom(evolutionSeed + i + 2);
petVisuals.accessory = pickRandom(PET_PARTS.accessories, aRng);
}
}
}
return petVisuals;
}