source: cpp/frams/model/autoname.cpp @ 732

Last change on this file since 732 was 534, checked in by Maciej Komosinski, 8 years ago

Renamed: get/setGene -> get/setGenes, setGeneOnly -> setGenesAssumingSameFormat

  • Property svn:eol-style set to native
File size: 3.1 KB
Line 
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
9SString AutoName::makeName(Model &model)
10{
11        SString t;
12        t = firstName(model);
13        SString last=lastName(model);
14        if (last.len()>0)
15                {
16                t += ' ';
17                t += last;
18                }
19        return t;
20}
21
22///////////////////////////
23
24static char* cat_syl(char* str, unsigned int x)
25{
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        }
37}
38
39SString AutoName::firstName(const SString& g)
40{
41        char buf[8];
42        unsigned int s1 = 0, s2 = 0, s3 = 0;
43        const char *x = g.c_str();
44        if (*x==0) return SString();
45        for (; *x; x++) { s1 += *x; s2 = s2**x + *x; s3 = (s3^*x) + *x; }
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);
52}
53
54SString AutoName::firstName(Model& model)
55{
56        return firstName(model.getGeno().getGenes());
57}
58
59static void przeplatanka(char *out, char *in1, char *in2)
60{
61        int d1 = strlen(in1), d2 = strlen(in2);
62        int p1 = 0, p2 = 0;
63        int pp = d1 + d2;
64        int i, p;
65        if (d2 < d1)
66        {
67                int t = d2; d2 = d1; d1 = t;
68                char *c = in1; in1 = in2; in2 = c;
69        }
70        if (pp)
71                for (i = 0; i <= pp; i++)
72                {
73                p = (i*d1) / pp - 1; for (; p1 <= p; p1++) *(out++) = *(in1++);
74                p = (i*d2) / pp - 1; for (; p2 <= p; p2++) *(out++) = *(in2++);
75                }
76        *out = 0;
77}
78
79SString AutoName::lastName(Model& model)
80{
81        char Sam[] = "yeaou";
82        char Sp[] = "shtkdgmr";
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
91        char naz[NAME_MAXLENBODY + NAME_MAXLENBRAIN + 1];
92        int poz, nextpoz, i;
93
94        double w;
95        int cialo = -1;
96        int mozg = -1;
97
98        char tmpc[NAME_MAXLENBODY + 1], tmpm[NAME_MAXLENBRAIN + 1];
99
100        if (model.getPartCount() > 0)
101        {
102                cialo = min((int)(sqrt(double(model.getPartCount()) - 1)*NAME_BODYLEN), NAME_MAXLENBODY - 1);
103                poz = 0;
104                for (i = 0; i <= cialo; i++) // budowanie "opisu" ciala
105                {
106                        nextpoz = ((model.getPartCount())*(i + 1)) / (cialo + 1) - 1;
107                        w = 1.0;
108                        for (; poz <= nextpoz; poz++) w = max(w, model.getPart(poz)->mass);
109                        tmpc[i] = Sp[min(int((w - 1.0)*NAME_BODYMASS), int(sizeof(Sp)) - 2)];
110                }
111                tmpc[i] = 0;
112        }
113        else tmpc[0] = 0;
114
115        int wint;
116
117        if (model.getNeuroCount() > 0)
118        {
119                mozg = min((int)(sqrt((double)model.getNeuroCount())*NAME_BRAINLEN), NAME_MAXLENBRAIN - 1);
120                poz = 0;
121                for (i = 0; i <= mozg; i++) // budowanie "opisu" mozgu
122                {
123                        nextpoz = (model.getNeuroCount()*(i + 1)) / (mozg + 1) - 1;
124                        wint = 0;
125                        for (; poz <= nextpoz; poz++) wint = max(wint, model.getNeuro(poz)->getInputCount());
126                        tmpm[i] = Sam[min(int(wint*NAME_BRAININP), int(sizeof(Sam)) - 2)];
127                }
128                tmpm[i] = 0;
129        }
130        else tmpm[0] = 0;
131
132        if ((mozg + 1) < ((cialo + 2) / 2))
133        {
134                for (i = mozg + 1; i <= cialo / 2; i++) tmpm[i] = 'i';
135                tmpm[i] = 0;
136        }
137
138        przeplatanka(naz, tmpc, tmpm);
139
140        naz[0] = (char)toupper(naz[0]);
141        return SString(naz);
142}
Note: See TracBrowser for help on using the repository browser.