1 | // This file is a part of Framsticks SDK. http://www.framsticks.com/ |
---|
2 | // Copyright (C) 1999-2015 Maciej Komosinski and Szymon Ulatowski. |
---|
3 | // See LICENSE.txt for details. |
---|
4 | |
---|
5 | #include "autoname.h" |
---|
6 | #include "common/nonstd_stl.h" |
---|
7 | #include <ctype.h> |
---|
8 | |
---|
9 | SString AutoName::makeName(Model &model) |
---|
10 | { |
---|
11 | SString t; |
---|
12 | t = firstName(model); |
---|
13 | t += ' '; |
---|
14 | t += lastName(model); |
---|
15 | return t; |
---|
16 | } |
---|
17 | |
---|
18 | /////////////////////////// |
---|
19 | |
---|
20 | static char* cat_syl(char* str, unsigned int x) |
---|
21 | { |
---|
22 | static char sp[] = "bcdfghklmnprstwz", sa[] = "aeiouy"; |
---|
23 | x %= 6 * 16; |
---|
24 | str[0] = sa[x % 6]; |
---|
25 | if (x > 5) |
---|
26 | { |
---|
27 | str[1] = sp[x / 6]; str[2] = 0; return str + 2; |
---|
28 | } |
---|
29 | else |
---|
30 | { |
---|
31 | str[1] = 0; return str + 1; |
---|
32 | } |
---|
33 | } |
---|
34 | |
---|
35 | SString AutoName::firstName(Model& model) |
---|
36 | { |
---|
37 | char buf[8]; |
---|
38 | unsigned int s1 = 0, s2 = 0, s3 = 0; |
---|
39 | SString g = model.getGeno().getGene(); |
---|
40 | const char *x = g.c_str(); |
---|
41 | for (; *x; x++) { s1 += *x; s2 = s2**x + *x; s3 = (s3^*x) + *x; } |
---|
42 | char* t = buf; |
---|
43 | t = cat_syl(t, s1); |
---|
44 | t = cat_syl(t, s2); |
---|
45 | t = cat_syl(t, s3); |
---|
46 | buf[0] = (char)toupper(buf[0]); |
---|
47 | return SString(buf); |
---|
48 | } |
---|
49 | |
---|
50 | static void przeplatanka(char *out, char *in1, char *in2) |
---|
51 | { |
---|
52 | int d1 = strlen(in1), d2 = strlen(in2); |
---|
53 | int p1 = 0, p2 = 0; |
---|
54 | int pp = d1 + d2; |
---|
55 | int i, p; |
---|
56 | if (d2 < d1) |
---|
57 | { |
---|
58 | int t = d2; d2 = d1; d1 = t; |
---|
59 | char *c = in1; in1 = in2; in2 = c; |
---|
60 | } |
---|
61 | if (pp) |
---|
62 | for (i = 0; i <= pp; i++) |
---|
63 | { |
---|
64 | p = (i*d1) / pp - 1; for (; p1 <= p; p1++) *(out++) = *(in1++); |
---|
65 | p = (i*d2) / pp - 1; for (; p2 <= p; p2++) *(out++) = *(in2++); |
---|
66 | } |
---|
67 | *out = 0; |
---|
68 | } |
---|
69 | |
---|
70 | SString AutoName::lastName(Model& model) |
---|
71 | { |
---|
72 | char Sam[] = "yeaou"; |
---|
73 | char Sp[] = "shtkdgmr"; |
---|
74 | |
---|
75 | #define NAME_MAXLENBODY 5 |
---|
76 | #define NAME_MAXLENBRAIN 5 |
---|
77 | #define NAME_BODYLEN 0.8 |
---|
78 | #define NAME_BRAINLEN 0.8 |
---|
79 | #define NAME_BODYMASS 1.0 |
---|
80 | #define NAME_BRAININP 1.0 |
---|
81 | |
---|
82 | char naz[NAME_MAXLENBODY + NAME_MAXLENBRAIN + 1]; |
---|
83 | int poz, nextpoz, i; |
---|
84 | |
---|
85 | double w; |
---|
86 | int cialo = -1; |
---|
87 | int mozg = -1; |
---|
88 | |
---|
89 | char tmpc[NAME_MAXLENBODY + 1], tmpm[NAME_MAXLENBRAIN + 1]; |
---|
90 | |
---|
91 | if (model.getPartCount() > 0) |
---|
92 | { |
---|
93 | cialo = min((int)(sqrt(double(model.getPartCount()) - 1)*NAME_BODYLEN), NAME_MAXLENBODY - 1); |
---|
94 | poz = 0; |
---|
95 | for (i = 0; i <= cialo; i++) // budowanie "opisu" ciala |
---|
96 | { |
---|
97 | nextpoz = ((model.getPartCount())*(i + 1)) / (cialo + 1) - 1; |
---|
98 | w = 1.0; |
---|
99 | for (; poz <= nextpoz; poz++) w = max(w, model.getPart(poz)->mass); |
---|
100 | tmpc[i] = Sp[min(int((w - 1.0)*NAME_BODYMASS), int(sizeof(Sp)) - 2)]; |
---|
101 | } |
---|
102 | tmpc[i] = 0; |
---|
103 | } |
---|
104 | else tmpc[0] = 0; |
---|
105 | |
---|
106 | int wint; |
---|
107 | |
---|
108 | if (model.getNeuroCount() > 0) |
---|
109 | { |
---|
110 | mozg = min((int)(sqrt((double)model.getNeuroCount())*NAME_BRAINLEN), NAME_MAXLENBRAIN - 1); |
---|
111 | poz = 0; |
---|
112 | for (i = 0; i <= mozg; i++) // budowanie "opisu" mozgu |
---|
113 | { |
---|
114 | nextpoz = (model.getNeuroCount()*(i + 1)) / (mozg + 1) - 1; |
---|
115 | wint = 0; |
---|
116 | for (; poz <= nextpoz; poz++) wint = max(wint, model.getNeuro(poz)->getInputCount()); |
---|
117 | tmpm[i] = Sam[min(int(wint*NAME_BRAININP), int(sizeof(Sam)) - 2)]; |
---|
118 | } |
---|
119 | tmpm[i] = 0; |
---|
120 | } |
---|
121 | else tmpm[0] = 0; |
---|
122 | |
---|
123 | if ((mozg + 1) < ((cialo + 2) / 2)) |
---|
124 | { |
---|
125 | for (i = mozg + 1; i <= cialo / 2; i++) tmpm[i] = 'i'; |
---|
126 | tmpm[i] = 0; |
---|
127 | } |
---|
128 | |
---|
129 | przeplatanka(naz, tmpc, tmpm); |
---|
130 | |
---|
131 | naz[0] = (char)toupper(naz[0]); |
---|
132 | return SString(naz); |
---|
133 | } |
---|