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