Elin Decompiled Documentation EA 23.251 Nightly
Loading...
Searching...
No Matches
DNA.cs
Go to the documentation of this file.
1using System;
2using System.Collections.Generic;
3using System.Runtime.Serialization;
4using Newtonsoft.Json;
5using UnityEngine;
6
7public class DNA : EClass
8{
9 public enum Type
10 {
11 Inferior = 0,
12 Default = 3,
13 Superior = 5,
14 Brain = 8
15 }
16
17 [JsonProperty]
18 public string id;
19
20 [JsonProperty]
21 public int[] ints = new int[6];
22
23 [JsonProperty]
24 public List<int> vals = new List<int>();
25
27
28 public Type type
29 {
30 get
31 {
32 return ints[0].ToEnum<Type>();
33 }
34 set
35 {
36 ints[0] = (int)value;
37 }
38 }
39
40 public int cost
41 {
42 get
43 {
44 return ints[1];
45 }
46 set
47 {
48 ints[1] = value;
49 }
50 }
51
52 public int lv
53 {
54 get
55 {
56 return ints[2];
57 }
58 set
59 {
60 ints[2] = value;
61 }
62 }
63
64 public int seed
65 {
66 get
67 {
68 return ints[3];
69 }
70 set
71 {
72 ints[3] = value;
73 }
74 }
75
76 public int slot
77 {
78 get
79 {
80 return ints[5];
81 }
82 set
83 {
84 ints[5] = value;
85 }
86 }
87
88 public bool isManiGene
89 {
90 get
91 {
92 return bits[0];
93 }
94 set
95 {
96 bits[0] = value;
97 }
98 }
99
100 [OnSerializing]
101 private void _OnSerializing(StreamingContext context)
102 {
103 ints[4] = bits.ToInt();
104 }
105
106 [OnDeserialized]
107 private void _OnDeserialized(StreamingContext context)
108 {
109 if (ints.Length < 6)
110 {
111 Array.Resize(ref ints, 6);
112 slot = 1;
113 }
114 }
115
116 public static Thing GenerateGene(Chara c, Type? type = null)
117 {
118 Thing thing = ThingGen.Create((type == Type.Brain) ? "gene_brain" : "gene");
119 DNA dNA = new DNA();
120 thing.MakeRefFrom(c);
121 dNA.id = c.id;
122 dNA.lv = c.LV;
123 dNA.seed = EClass.rnd(20000);
124 thing.c_DNA = dNA;
125 dNA.GenerateWithGene(type ?? dNA.GetRandomType(), thing, c);
126 return thing;
127 }
128
129 public static Thing GenerateGene(CardRow r, Type? type = null, int lv = -1, int seed = -1)
130 {
131 Thing thing = ThingGen.Create((type == Type.Brain) ? "gene_brain" : "gene");
132 DNA dNA = new DNA();
133 thing.MakeRefFrom(r.id);
134 dNA.id = r.id;
135 dNA.lv = ((lv == -1) ? r.LV : lv);
136 dNA.seed = ((seed == -1) ? EClass.rnd(20000) : seed);
137 thing.c_DNA = dNA;
138 dNA.GenerateWithGene(type ?? dNA.GetRandomType(), thing);
139 return thing;
140 }
141
142 public static Thing CopyDNA(Thing from, Thing to)
143 {
144 to.c_DNA = from.c_DNA;
145 to.c_idRefCard = from.c_idRefCard;
146 to.ChangeMaterial(from.material);
147 return to;
148 }
149
150 public static Thing GenerateRandomGene(int lv = -1, int seed = -1)
151 {
152 if (lv == -1)
153 {
155 }
157 CardRow r = SpawnList.Get("chara").Select(100);
158 Rand.SetSeed();
159 return GenerateGene(r, Type.Superior, lv, seed);
160 }
161
162 public static Thing GenerateManiGene(Card owner)
163 {
164 owner.things.DestroyAll();
165 Debug.Log("Mani:" + owner.c_seed);
166 Rand.SetSeed(owner.c_seed);
167 CardRow r = SpawnList.Get("chara").Select(100);
168 Rand.SetSeed(owner.c_seed);
169 Thing thing = GenerateGene(r, Type.Superior, owner.LV, owner.c_seed);
170 thing.c_DNA.cost = thing.c_DNA.cost / 2;
171 thing.c_DNA.isManiGene = true;
172 thing.MakeRefFrom("mani");
173 Rand.SetSeed();
174 owner.c_seed++;
175 return thing;
176 }
177
178 public void Apply(Chara c)
179 {
180 if (c.c_genes == null)
181 {
182 c.c_genes = new CharaGenes();
183 }
184 CharaGenes c_genes = c.c_genes;
185 if (type == Type.Inferior)
186 {
187 c.c_genes.inferior++;
188 return;
189 }
190 c.feat -= cost;
191 Apply(c, reverse: false);
192 c_genes.items.Add(this);
193 }
194
195 public void Apply(Chara c, bool reverse)
196 {
197 if (type == Type.Brain)
198 {
199 c._tactics = null;
200 return;
201 }
202 for (int i = 0; i < vals.Count; i += 2)
203 {
204 int num = vals[i];
205 int num2 = vals[i + 1];
207 switch (row.category)
208 {
209 case "slot":
210 if (reverse)
211 {
212 c.body.RemoveBodyPart(row.id);
213 }
214 else
215 {
216 c.body.AddBodyPart(row.id);
217 }
219 break;
220 case "feat":
221 c.SetFeat(num, c.elements.ValueWithoutLink(num) + ((!reverse) ? 1 : (-1)), !reverse);
222 break;
223 case "ability":
224 if (reverse)
225 {
226 c.ability.Remove(num * ((num2 >= 0) ? 1 : (-1)));
227 }
228 else
229 {
230 c.ability.Add(num, Mathf.Abs(num2), num2 < 0);
231 }
232 break;
233 default:
234 if (!reverse && c.elements.ValueWithoutLink(row.id) == 0)
235 {
236 c.elements.Learn(row.id, num2);
237 }
238 else
239 {
240 c.elements.ModBase(num, reverse ? (-num2) : num2);
241 }
242 break;
243 }
244 }
245 }
246
247 public bool CanRemove()
248 {
249 for (int i = 0; i < vals.Count; i += 2)
250 {
251 int num = vals[i];
252 if (num == 1237 || num == 1415)
253 {
254 return false;
255 }
256 }
257 return true;
258 }
259
260 public void GenerateWithGene(Type _type, Card gene, Chara model = null)
261 {
262 Generate(_type, model);
264 gene.elements.SetTo(10, 0);
265 }
266
267 public void Generate(Type _type, Chara model = null)
268 {
269 type = _type;
270 cost = 0;
271 slot = 0;
272 vals.Clear();
274 if (model == null)
275 {
276 model = CharaGen.Create(id);
277 if (lv > model.LV)
278 {
279 model.SetLv(lv);
280 }
281 }
282 if (type == Type.Inferior || model == null)
283 {
284 type = Type.Inferior;
285 return;
286 }
287 if (type == Type.Brain)
288 {
289 cost = 4;
290 return;
291 }
292 int body = 0;
293 int action = 0;
294 int feat = 0;
295 int maxSlot = 1;
296 List<Element> listAttb = model.elements.ListBestAttributes();
297 List<Element> listSkill = model.elements.ListBestSkills();
298 List<Element> listFeat = model.elements.ListGeneFeats();
300 if (listFeat.Count == 0)
301 {
302 listFeat = model.ListAvailabeFeats(pet: true);
303 }
305 switch (type)
306 {
307 case Type.Default:
308 AddRandom(EClass.rnd(EClass.rnd(4)) + 1);
309 if (EClass.rnd(3) == 0 || vals.Count == 0)
310 {
311 AddSpecial();
312 }
313 break;
314 case Type.Superior:
315 AddRandom(EClass.rnd(EClass.rnd(4)) + 3);
316 if (EClass.rnd(3) == 0)
317 {
318 AddSpecial();
319 }
320 AddSpecial();
321 break;
322 }
323 if (vals.Count == 0)
324 {
325 for (int i = 0; i < 10; i++)
326 {
327 if (EClass.rnd(4) == 0)
328 {
329 AddSpecial();
330 }
331 else
332 {
333 AddRandom(1);
334 }
335 if (vals.Count > 0)
336 {
337 break;
338 }
339 }
340 }
341 Rand.SetSeed();
342 CalcCost();
343 CalcSlot();
344 void AddAction()
345 {
346 if (model.ability.list.items.Count != 0)
347 {
348 ActList.Item a = model.ability.list.items.RandomItem();
349 if (!(a.act.source.category != "ability") && a.act.source.geneSlot >= 0)
350 {
351 AddVal(a.act.source.id, a.chance * ((!a.pt) ? 1 : (-1)), allowStack: false, (int v) => 8 + a.act.source.cost[0] / 10 * 2);
352 action++;
353 }
354 }
355 }
356 void AddAttribute()
357 {
358 Element element = listAttb[EClass.rnd(3)];
359 AddVal(element.id, EClass.rndHalf(element.ValueWithoutLink / 2), allowStack: true, (int v) => v / 5 + 1);
360 }
361 void AddBody()
362 {
363 if (body == 0)
364 {
365 BodySlot bodySlot = null;
366 for (int j = 0; j < 100; j++)
367 {
368 BodySlot bodySlot2 = model.body.slots.RandomItem();
369 if (bodySlot2 != null && bodySlot2.elementId != 40 && bodySlot2.elementId != 44)
370 {
371 bodySlot = bodySlot2;
372 break;
373 }
374 }
375 if (bodySlot != null)
376 {
377 AddVal(bodySlot.elementId, 1, allowStack: false, (int v) => 20);
378 body++;
379 }
380 }
381 }
382 void AddFeat()
383 {
384 if (listFeat.Count != 0)
385 {
386 feat++;
387 Element e = listFeat.RandomItem();
388 if (maxSlot <= 1 || e.source.geneSlot <= 1)
389 {
390 if (e.source.geneSlot > maxSlot)
391 {
392 maxSlot = e.source.geneSlot;
393 }
394 AddVal(e.id, 1, allowStack: false, (int v) => e.source.cost[0] * 5);
395 }
396 }
397 }
398 void AddRandom(int n)
399 {
400 for (int l = 0; l < n; l++)
401 {
402 if (EClass.debug.enable && EClass.rnd(2) == 0)
403 {
404 AddSpecial();
405 }
406 else if (EClass.rnd(2) == 0)
407 {
408 AddSkill();
409 }
410 else
411 {
412 AddAttribute();
413 }
414 }
415 }
416 void AddSkill()
417 {
418 Element element2 = listSkill[Mathf.Clamp(EClass.rnd(6), 0, listSkill.Count - 1)];
419 AddVal(element2.id, EClass.rndHalf(element2.ValueWithoutLink / 2), allowStack: true, (int v) => v / 5 + 1);
420 }
421 void AddSpecial()
422 {
423 if (EClass.rnd(3) == 0)
424 {
425 AddAction();
426 }
427 else if (EClass.rnd(5) == 0)
428 {
429 AddBody();
430 }
431 else if (listFeat.Count > 0)
432 {
433 AddFeat();
434 }
435 else if (EClass.rnd(2) == 0)
436 {
437 AddSkill();
438 }
439 else
440 {
441 AddAttribute();
442 }
443 }
444 void AddVal(int id, int v, bool allowStack, Func<int, int> funcCost)
445 {
446 bool flag = false;
447 int num = EClass.curve(v, 20, 10, 90);
448 if (v < -100)
449 {
450 num = EClass.curve(Mathf.Abs(v + 100), 20, 10, 90);
451 }
452 v = EClass.curve(v, 20, 10, 80);
453 for (int k = 0; k < vals.Count; k += 2)
454 {
455 if (vals[k] == id)
456 {
457 if (allowStack)
458 {
459 v /= 2;
460 num /= 2;
461 vals[k + 1] += v;
462 Debug.Log(vals[k + 1] + ": " + v + "/" + num);
463 flag = true;
464 break;
465 }
466 return;
467 }
468 }
469 if (v != 0)
470 {
471 if (!flag)
472 {
473 vals.Add(id);
474 vals.Add(v);
475 }
476 cost += Mathf.Max(0, funcCost(num));
477 }
478 }
479 }
480
481 public void CalcCost()
482 {
483 if (cost < 0)
484 {
485 cost = 0;
486 }
487 }
488
489 public void CalcSlot()
490 {
491 slot = 0;
492 for (int i = 0; i < vals.Count; i += 2)
493 {
494 Element element = Element.Create(vals[i], vals[i + 1]);
495 if (element.source.geneSlot < 0)
496 {
497 slot = 99;
498 }
499 if (element.source.geneSlot > slot)
500 {
501 slot = element.source.geneSlot;
502 }
503 }
504 if (slot < 0)
505 {
506 slot = 0;
507 }
508 }
509
510 public static Type GetType(string idMat)
511 {
512 return idMat switch
513 {
514 "jelly" => Type.Default,
515 "gold" => Type.Superior,
516 "amethyst" => Type.Brain,
517 _ => Type.Inferior,
518 };
519 }
520
521 public string GetMaterialId(Type type)
522 {
523 return type switch
524 {
525 Type.Default => "jelly",
526 Type.Superior => "gold",
527 Type.Brain => "amethyst",
528 _ => "process",
529 };
530 }
531
532 public int GetDurationHour()
533 {
534 return Mathf.Min(cost, 5000) * Mathf.Min(cost, 5000) / 2;
535 }
536
538 {
539 if (EClass.rnd(5) == 0)
540 {
541 return Type.Superior;
542 }
543 if (EClass.rnd(10) == 0)
544 {
545 return Type.Inferior;
546 }
547 return Type.Default;
548 }
549
550 public string GetText()
551 {
552 string text = EClass.sources.cards.map.TryGetValue(id)?.GetName() ?? "???";
553 if (text == "*r")
554 {
555 text = "???";
556 }
557 return "gene".lang(text.ToTitleCase(), cost.ToString() ?? "");
558 }
559
560 public void WriteNote(UINote n)
561 {
562 if (slot >= 1)
563 {
564 n.AddText("isGeneReqSlots".lang(slot.ToString() ?? ""), FontColor.Warning);
565 }
566 if (!CanRemove())
567 {
568 n.AddText("isPermaGene".lang(), FontColor.Warning);
569 }
570 n.Space(4);
571 if (type == Type.Brain)
572 {
573 SourceChara.Row row = EClass.sources.charas.map.TryGetValue(id);
574 if (row != null)
575 {
576 string key = row.tactics.IsEmpty(EClass.sources.tactics.map.TryGetValue(row.id)?.id ?? EClass.sources.tactics.map.TryGetValue(row.job)?.id ?? "predator");
577 n.AddText("gene_info".lang(EClass.sources.tactics.map[key].GetName().ToTitleCase(), ""), FontColor.ButtonGeneral);
578 }
579 for (int i = 0; i < vals.Count; i += 2)
580 {
581 int num = vals[i];
582 int num2 = vals[i + 1];
583 FontColor color = ((num2 >= 0) ? FontColor.Good : FontColor.Bad);
584 string @ref = (num + 1).ToString() ?? "";
585 string text = "";
586 num2 = Mathf.Abs(num2 / 20) + 1;
587 text = text + "[" + "*".Repeat(Mathf.Clamp(num2, 1, 5)) + ((num2 > 5) ? "+" : "") + "]";
588 n.AddText("gene_info_brain".lang(@ref, text), color);
589 }
590 return;
591 }
592 for (int j = 0; j < vals.Count; j += 2)
593 {
594 Element element = Element.Create(vals[j], vals[j + 1]);
595 string text2 = "";
596 int num3 = element.Value / 10;
597 FontColor color2 = FontColor.Good;
598 bool flag = false;
599 switch (element.source.category)
600 {
601 case "slot":
602 color2 = FontColor.Myth;
603 num3 = -1;
604 break;
605 case "feat":
606 color2 = FontColor.FoodMisc;
607 num3 = -1;
608 break;
609 case "ability":
610 color2 = FontColor.Topic2;
611 num3 = -1;
612 break;
613 default:
614 flag = true;
615 break;
616 }
617 if (num3 >= 0)
618 {
619 text2 = text2 + "[" + "*".Repeat(Mathf.Clamp(num3, 1, 5)) + ((num3 > 5) ? "+" : "") + "]";
620 }
621 if (flag)
622 {
623 text2 = text2 + " (" + element.Value + ")";
624 }
625 n.AddText("gene_info".lang(element.Name.ToTitleCase(wholeText: true), text2), color2);
626 }
627 }
628
630 {
631 for (int i = 0; i < vals.Count; i += 2)
632 {
633 Element element = Element.Create(vals[i], vals[i + 1]);
634 if (!(element.source.category == "feat"))
635 {
636 continue;
637 }
638 if (c.Evalue(element.id) < element.source.max)
639 {
640 Element orCreateElement = c.elements.GetOrCreateElement(element.id);
641 if (orCreateElement != null && orCreateElement.vLink == 0)
642 {
643 continue;
644 }
645 }
646 return element;
647 }
648 return null;
649 }
650
652 {
653 for (int i = 0; i < vals.Count; i += 2)
654 {
655 Element element = Element.Create(vals[i], vals[i + 1]);
656 if (!(element.source.category == "ability"))
657 {
658 continue;
659 }
660 foreach (ActList.Item item in c.ability.list.items)
661 {
662 if (item.act.source.id == element.id)
663 {
664 return element;
665 }
666 }
667 }
668 return null;
669 }
670}
FontColor
Definition: FontColor.cs:2
int chance
Definition: ActList.cs:9
bool pt
Definition: ActList.cs:11
Act act
Definition: ActList.cs:7
List< Item > items
Definition: ActList.cs:14
int elementId
Definition: BodySlot.cs:6
string id
Definition: CardRow.cs:7
Definition: Card.cs:11
ElementContainerCard elements
Definition: Card.cs:41
string id
Definition: Card.cs:35
SourceMaterial.Row material
Definition: Card.cs:2051
Card ChangeMaterial(int idNew, bool ignoreFixedMaterial=false)
Definition: Card.cs:3041
Card MakeRefFrom(string id)
Definition: Card.cs:5709
Card SetLv(int a)
Definition: Card.cs:2905
int c_seed
Definition: Card.cs:1305
CharaGenes c_genes
Definition: Card.cs:1845
ThingContainer things
Definition: Card.cs:38
int Evalue(int ele)
Definition: Card.cs:2571
DNA c_DNA
Definition: Card.cs:1881
int LV
Definition: Card.cs:386
string c_idRefCard
Definition: Card.cs:1737
ActList list
Definition: CharaAbility.cs:12
void Add(int id, int chance, bool pt)
void Remove(int id)
void AddBodyPart(int ele, Thing thing=null)
Definition: CharaBody.cs:268
void RefreshBodyParts()
Definition: CharaBody.cs:294
void RemoveBodyPart(int ele)
Definition: CharaBody.cs:323
static Chara Create(string id, int lv=-1)
Definition: CharaGen.cs:17
int inferior
Definition: CharaGenes.cs:10
List< DNA > items
Definition: CharaGenes.cs:7
Definition: Chara.cs:10
CharaBody body
Definition: Chara.cs:94
void SetFeat(int id, int value=1, bool msg=false)
Definition: Chara.cs:9879
CharaAbility ability
Definition: Chara.cs:424
bool enable
Definition: CoreDebug.cs:286
Definition: DNA.cs:8
void Apply(Chara c, bool reverse)
Definition: DNA.cs:195
string id
Definition: DNA.cs:18
Type type
Definition: DNA.cs:29
int cost
Definition: DNA.cs:41
void GenerateWithGene(Type _type, Card gene, Chara model=null)
Definition: DNA.cs:260
void _OnDeserialized(StreamingContext context)
Definition: DNA.cs:107
Element GetInvalidFeat(Chara c)
Definition: DNA.cs:629
int seed
Definition: DNA.cs:65
static Thing GenerateGene(CardRow r, Type? type=null, int lv=-1, int seed=-1)
Definition: DNA.cs:129
void _OnSerializing(StreamingContext context)
Definition: DNA.cs:101
Type
Definition: DNA.cs:10
Type GetRandomType()
Definition: DNA.cs:537
static Thing CopyDNA(Thing from, Thing to)
Definition: DNA.cs:142
static Thing GenerateRandomGene(int lv=-1, int seed=-1)
Definition: DNA.cs:150
int slot
Definition: DNA.cs:77
static Type GetType(string idMat)
Definition: DNA.cs:510
int[] ints
Definition: DNA.cs:21
bool isManiGene
Definition: DNA.cs:89
BitArray32 bits
Definition: DNA.cs:26
static Thing GenerateManiGene(Card owner)
Definition: DNA.cs:162
void CalcSlot()
Definition: DNA.cs:489
string GetMaterialId(Type type)
Definition: DNA.cs:521
void Generate(Type _type, Chara model=null)
Definition: DNA.cs:267
void Apply(Chara c)
Definition: DNA.cs:178
int GetDurationHour()
Definition: DNA.cs:532
void CalcCost()
Definition: DNA.cs:481
bool CanRemove()
Definition: DNA.cs:247
int lv
Definition: DNA.cs:53
string GetText()
Definition: DNA.cs:550
static Thing GenerateGene(Chara c, Type? type=null)
Definition: DNA.cs:116
List< int > vals
Definition: DNA.cs:24
Element GetInvalidAction(Chara c)
Definition: DNA.cs:651
void WriteNote(UINote n)
Definition: DNA.cs:560
Definition: EClass.cs:6
static int curve(int _a, int start, int step, int rate=75)
Definition: EClass.cs:69
static Zone _zone
Definition: EClass.cs:21
static int rnd(long a)
Definition: EClass.cs:59
static SourceManager sources
Definition: EClass.cs:43
static int rndHalf(int a)
Definition: EClass.cs:97
static CoreDebug debug
Definition: EClass.cs:49
void Learn(int ele, int v=1)
int ValueWithoutLink(int ele)
Element ModBase(int ele, int v)
void SetTo(int id, int v)
Element GetOrCreateElement(Element ele)
int ValueWithoutLink
Definition: ELEMENT.cs:294
int id
Definition: ELEMENT.cs:250
SourceElement.Row source
Definition: ELEMENT.cs:273
virtual string Name
Definition: ELEMENT.cs:304
static Element Create(int id, int v=0)
Definition: ELEMENT.cs:1102
int vLink
Definition: ELEMENT.cs:260
Definition: Rand.cs:4
static void SetSeed(int a=-1)
Definition: Rand.cs:37
int LV
Definition: RenderRow.cs:22
Dictionary< string, CardRow > map
Definition: SourceCard.cs:8
SourceCard cards
SourceElement elements
SourceChara charas
SourceTactics tactics
static SpawnList Get(string id, string parent=null, CardFilter filter=null)
Definition: SpawnList.cs:18
CardRow Select(int lv=-1, int levelRange=-1)
Definition: SpawnList.cs:139
void DestroyAll(Func< Thing, bool > funcExclude=null)
static Thing Create(string id, int idMat=-1, int lv=-1)
Definition: ThingGen.cs:53
Definition: Thing.cs:8
Definition: UINote.cs:6
UIItem AddText(string text, FontColor color=FontColor.DontChange)
Definition: UINote.cs:113
void Space(int sizeY=0, int sizeX=1)
Definition: UINote.cs:62
override int ContentLv
Definition: Zone.cs:98
int ToInt()
Definition: BitArray32.cs:84