[286] | 1 | // This file is a part of Framsticks SDK. http://www.framsticks.com/ |
---|
[671] | 2 | // Copyright (C) 1999-2017 Maciej Komosinski and Szymon Ulatowski. |
---|
[286] | 3 | // See LICENSE.txt for details. |
---|
[109] | 4 | |
---|
| 5 | #include "conv_f1.h" |
---|
| 6 | #include <common/nonstd_stl.h> |
---|
[375] | 7 | #include <common/log.h> |
---|
[109] | 8 | #include <frams/util/multirange.h> |
---|
| 9 | #include <frams/util/multimap.h> |
---|
| 10 | #include <ctype.h> |
---|
| 11 | |
---|
[671] | 12 | //#define v1f1COMPATIBLE //as in ancient Framsticks 1.x |
---|
[109] | 13 | |
---|
[671] | 14 | F1Props stdprops = { 1, 0, 1, 0.4, 0.25, 0.25, 0.25, 0.25, 0.0, 1.0, 1.0, 1, |
---|
| 15 | 0.2, 0.5, 0.5, 0.5 }; |
---|
[109] | 16 | |
---|
| 17 | class Builder |
---|
| 18 | { |
---|
| 19 | public: |
---|
[671] | 20 | Builder(const char*g, int mapping = 0) :invalid(0), genbegin(g), usemapping(mapping), first_part_mapping(NULL), model_energy(0), model_energy_max(0) {} |
---|
| 21 | ~Builder() { SAFEDELETE(first_part_mapping); } |
---|
| 22 | char tmp[222]; |
---|
| 23 | bool invalid; |
---|
| 24 | Model model; |
---|
| 25 | const char *genbegin; |
---|
| 26 | SList neuro_f1_to_f0; // neuro_f1_to_f0(f1_refno) = actual neuro pointer |
---|
| 27 | Neuro *last_f1_neuro; |
---|
| 28 | SyntParam *neuro_cls_param; |
---|
[109] | 29 | |
---|
[671] | 30 | struct Connection |
---|
| 31 | { |
---|
| 32 | int n1, n2; double w; |
---|
| 33 | Connection(int _n1, int _n2, double _w) :n1(_n1), n2(_n2), w(_w) {} |
---|
| 34 | }; |
---|
[109] | 35 | |
---|
[671] | 36 | SListTempl<Connection> connections; |
---|
| 37 | int usemapping; |
---|
| 38 | MultiRange range; |
---|
| 39 | MultiRange *first_part_mapping; |
---|
| 40 | double lastjoint_muscle_power; |
---|
| 41 | double model_energy, model_energy_max; |
---|
| 42 | void grow(int part1, const char*g, Pt3D k, F1Props c); |
---|
| 43 | void setPartMapping(int p, const char* g); |
---|
| 44 | int growJoint(int part1, int part2, Pt3D &angle, F1Props &c, const char *g); |
---|
| 45 | int growPart(F1Props &c, const char *g); |
---|
| 46 | const char *skipNeuro(const char *z); |
---|
| 47 | const char* growNeuro(const char* t, F1Props &c, int&); |
---|
| 48 | void growConnection(const char* begin, const char* colon, const char* end, F1Props& props); |
---|
| 49 | int countBranches(const char*g, SList &out); |
---|
| 50 | SyntParam* lastNeuroClassParam(); |
---|
| 51 | void addClassParam(const char* name, double value); |
---|
| 52 | void addClassParam(const char* name, const char* value); |
---|
[109] | 53 | |
---|
[671] | 54 | const MultiRange* makeRange(const char*g) { return makeRange(g, g); } |
---|
| 55 | const MultiRange* makeRange(const char*g, const char*g2); |
---|
| 56 | Part *getLastPart() { return getLastJoint()->part2; } |
---|
| 57 | Neuro *getLastNeuro() { return model.getNeuro(model.getNeuroCount() - 1); } |
---|
| 58 | Joint *getLastJoint() { return model.getJoint(model.getJointCount() - 1); } |
---|
| 59 | void addOrRememberInput(int n1, int n2, double w) |
---|
| 60 | { |
---|
[109] | 61 | //if (!addInput(n1,n2,w,false)) |
---|
[671] | 62 | connections += Connection(n1, n2, w); |
---|
| 63 | } |
---|
| 64 | bool addInput(int n1, int n2, double w, bool final) |
---|
| 65 | { |
---|
| 66 | if ((n1 < 0) || (n2 < 0) || (n1 >= neuro_f1_to_f0.size()) || (n2 >= neuro_f1_to_f0.size())) |
---|
[109] | 67 | { |
---|
[671] | 68 | if (final) logPrintf("GenoConvF1", "addInput", LOG_WARN, |
---|
| 69 | "illegal neuron connection %d <- %d (ignored)", n1, n2); |
---|
[109] | 70 | return 0; |
---|
[671] | 71 | } |
---|
| 72 | Neuro *neuro = (Neuro*)neuro_f1_to_f0(n1); |
---|
| 73 | Neuro *input = (Neuro*)neuro_f1_to_f0(n2); |
---|
| 74 | neuro->addInput(input, w); |
---|
[109] | 75 | return 1; |
---|
[671] | 76 | } |
---|
| 77 | void addPendingInputs() |
---|
| 78 | { |
---|
| 79 | for (int i = 0; i < connections.size(); i++) |
---|
[109] | 80 | { |
---|
[671] | 81 | Connection *c = &connections(i); |
---|
| 82 | addInput(c->n1, c->n2, c->w, true); |
---|
[109] | 83 | } |
---|
[671] | 84 | } |
---|
[109] | 85 | }; |
---|
| 86 | |
---|
[671] | 87 | const MultiRange* Builder::makeRange(const char*g, const char*g2) |
---|
[109] | 88 | { |
---|
[671] | 89 | if (!usemapping) return 0; |
---|
| 90 | range.clear(); |
---|
| 91 | range.add(g - genbegin, g2 - genbegin); |
---|
| 92 | return ⦥ |
---|
[109] | 93 | } |
---|
| 94 | |
---|
[671] | 95 | void F1Props::normalizeBiol4() |
---|
[109] | 96 | { |
---|
[671] | 97 | double sum = muscle_power + assimilation + stamina + ingestion; |
---|
| 98 | muscle_power /= sum; |
---|
| 99 | assimilation /= sum; |
---|
| 100 | stamina /= sum; |
---|
| 101 | ingestion /= sum; |
---|
[109] | 102 | } |
---|
| 103 | |
---|
| 104 | /** main conversion function - with conversion map support */ |
---|
[671] | 105 | SString GenoConv_f1::convert(SString &i, MultiMap *map) |
---|
[109] | 106 | { |
---|
[671] | 107 | const char* g = i.c_str(); |
---|
| 108 | Builder builder(g, map ? 1 : 0); |
---|
| 109 | builder.model.open(); |
---|
| 110 | builder.grow(-1, g, Pt3D_0, stdprops); // uses Model::singleStepBuild to create model elements |
---|
| 111 | if (builder.invalid) return SString(); |
---|
| 112 | builder.addPendingInputs(); |
---|
| 113 | builder.model.startenergy = (builder.model_energy_max > 0) ? (builder.model_energy / builder.model_energy_max) : 1.0; |
---|
| 114 | builder.model.close(); // model is ready to use now |
---|
| 115 | if (map) builder.model.getCurrentToF0Map(*map); // generate f1-to-f0 conversion map |
---|
| 116 | return builder.model.getF0Geno().getGenes(); |
---|
[109] | 117 | } |
---|
| 118 | |
---|
[671] | 119 | void Builder::setPartMapping(int p, const char* g) |
---|
[109] | 120 | { |
---|
[671] | 121 | if (!usemapping) return; |
---|
| 122 | const MultiRange *r = makeRange(g); |
---|
| 123 | if (p < 0) |
---|
[109] | 124 | { //special case: mapping the part which is not yet created |
---|
[671] | 125 | if (first_part_mapping) first_part_mapping->add(*r); |
---|
| 126 | else first_part_mapping = new MultiRange(*r); |
---|
[109] | 127 | } |
---|
[671] | 128 | else |
---|
| 129 | model.getPart(p)->addMapping(*r); |
---|
[109] | 130 | } |
---|
| 131 | |
---|
[671] | 132 | void Builder::grow(int part1, const char*g, Pt3D k, F1Props c) |
---|
[109] | 133 | { |
---|
[671] | 134 | int hasmuscles = 0; |
---|
| 135 | k += Pt3D(c.twist, 0, c.curvedness); |
---|
| 136 | while (1) |
---|
[109] | 137 | { |
---|
[671] | 138 | switch (*g) |
---|
| 139 | { |
---|
| 140 | case 0: case ',': case ')': return; |
---|
| 141 | case 'R': k.x += 0.7853; setPartMapping(part1, g); break; |
---|
| 142 | case 'r': k.x -= 0.7853; setPartMapping(part1, g); break; |
---|
| 143 | case 'Q': c.twist += (1.58 - c.twist)*0.3; setPartMapping(part1, g); break; |
---|
| 144 | case 'q': c.twist += (-1.58 - c.twist)*0.3; setPartMapping(part1, g); break; |
---|
[109] | 145 | #ifdef v1f1COMPATIBLE |
---|
[671] | 146 | case 'L': c.length += (3.0 - c.length)*0.3; setPartMapping(part1, g); break; |
---|
[109] | 147 | #else |
---|
[671] | 148 | case 'L': c.length += (2.0 - c.length)*0.3; setPartMapping(part1, g); break; |
---|
[109] | 149 | #endif |
---|
[671] | 150 | case 'l': c.length += (0.33 - c.length)*0.3; setPartMapping(part1, g); break; |
---|
| 151 | case 'A': c.assimilation += (1 - c.assimilation)*0.8; c.normalizeBiol4(); setPartMapping(part1, g); break; |
---|
| 152 | case 'a': c.assimilation -= c.assimilation*0.4; c.normalizeBiol4(); setPartMapping(part1, g); break; |
---|
| 153 | case 'I': c.ingestion += (1 - c.ingestion)*0.8; c.normalizeBiol4(); setPartMapping(part1, g); break; |
---|
| 154 | case 'i': c.ingestion -= c.ingestion*0.4; c.normalizeBiol4(); setPartMapping(part1, g); break; |
---|
| 155 | case 'S': c.stamina += (1 - c.stamina)*0.8; c.normalizeBiol4(); setPartMapping(part1, g); break; |
---|
| 156 | case 's': c.stamina -= c.stamina*0.4; c.normalizeBiol4(); setPartMapping(part1, g); break; |
---|
| 157 | case 'M': c.muscle_power += (1 - c.muscle_power)*0.8; c.normalizeBiol4(); setPartMapping(part1, g); break; |
---|
| 158 | case 'm': c.muscle_power -= c.muscle_power*0.4; c.normalizeBiol4(); setPartMapping(part1, g); break; |
---|
| 159 | case 'C': c.curvedness += (2.0 - c.curvedness)*0.25; setPartMapping(part1, g); break; |
---|
| 160 | case 'c': c.curvedness += (-2.0 - c.curvedness)*0.25; setPartMapping(part1, g); break; |
---|
| 161 | case 'F': c.friction += (4 - c.friction)*0.2; setPartMapping(part1, g); break; |
---|
| 162 | case 'f': c.friction -= c.friction*0.2; setPartMapping(part1, g); break; |
---|
| 163 | case 'W': c.weight += (2.0 - c.weight)*0.3; setPartMapping(part1, g); break; |
---|
| 164 | case 'w': c.weight += (0.5 - c.weight)*0.3; setPartMapping(part1, g); break; |
---|
| 165 | case 'E': c.energy += (10.0 - c.energy)*0.1; setPartMapping(part1, g); break; |
---|
| 166 | case 'e': c.energy -= c.energy*0.1; setPartMapping(part1, g); break; |
---|
[109] | 167 | |
---|
[671] | 168 | case 'D': c.cred += (1.0 - c.cred)*0.25; setPartMapping(part1, g); break; |
---|
| 169 | case 'd': c.cred += (0.0 - c.cred)*0.25; setPartMapping(part1, g); break; |
---|
| 170 | case 'G': c.cgreen += (1.0 - c.cgreen)*0.25; setPartMapping(part1, g); break; |
---|
| 171 | case 'g': c.cgreen += (0.0 - c.cgreen)*0.25; setPartMapping(part1, g); break; |
---|
| 172 | case 'B': c.cblue += (1.0 - c.cblue)*0.25; setPartMapping(part1, g); break; |
---|
| 173 | case 'b': c.cblue += (0.0 - c.cblue)*0.25; setPartMapping(part1, g); break; |
---|
| 174 | case 'H': c.visual_size += (0.7 - c.visual_size)*0.25; setPartMapping(part1, g); break; |
---|
| 175 | case 'h': c.visual_size += (0.05 - c.visual_size)*0.25; setPartMapping(part1, g); break; |
---|
[109] | 176 | |
---|
[671] | 177 | case '[': //neuron |
---|
| 178 | // setdebug(g-(char*)geny,DEBUGNEURO | !l_neu); |
---|
| 179 | if (model.getJointCount()) |
---|
| 180 | g = growNeuro(g + 1, c, hasmuscles); |
---|
| 181 | else |
---|
[109] | 182 | { |
---|
[671] | 183 | logMessage("GenoConv_F1", "grow", 1, "Illegal neuron position (ignored)"); |
---|
| 184 | g = skipNeuro(g + 1); |
---|
[109] | 185 | } |
---|
[671] | 186 | break; |
---|
| 187 | case 'X': |
---|
[109] | 188 | { |
---|
[671] | 189 | int freshpart = 0; |
---|
| 190 | //setdebug(g-(char*)geny,DEBUGEST | !l_est); |
---|
| 191 | if (part1 < 0) //initial grow |
---|
[109] | 192 | { |
---|
[671] | 193 | if (model.getPartCount() > 0) |
---|
| 194 | part1 = 0; |
---|
| 195 | else |
---|
| 196 | { |
---|
| 197 | part1 = growPart(c, g); |
---|
| 198 | freshpart = 1; |
---|
| 199 | if (first_part_mapping) |
---|
| 200 | model.getPart(part1)->setMapping(*first_part_mapping); |
---|
| 201 | } |
---|
| 202 | } |
---|
| 203 | if (!freshpart) |
---|
[109] | 204 | { |
---|
[671] | 205 | Part *part = model.getPart(part1); |
---|
| 206 | part->density = ((part->mass*part->density) + 1.0 / c.weight) / (part->mass + 1.0); // v=m*d |
---|
| 207 | // part->volume+=1.0/c.weight; |
---|
| 208 | part->mass += 1.0; |
---|
[109] | 209 | } |
---|
[671] | 210 | model_energy += 0.9*c.energy + 0.1; |
---|
| 211 | model_energy_max += 1.0; |
---|
[109] | 212 | |
---|
[671] | 213 | int part2 = growPart(c, g); |
---|
| 214 | growJoint(part1, part2, k, c, g); |
---|
| 215 | // est* e = new est(*s,*s2,k,c,zz,this); |
---|
[109] | 216 | |
---|
[671] | 217 | // attenuate properties as they are propagated along the structure |
---|
| 218 | c.length = 0.5*c.length + 0.5*stdprops.length; |
---|
| 219 | c.visual_size = 0.5*c.visual_size + 0.5*stdprops.visual_size; |
---|
| 220 | c.curvedness = 0.66*c.curvedness; |
---|
| 221 | c.twist = 0.66*c.twist; |
---|
| 222 | c.friction = 0.8*c.friction + 0.2*stdprops.friction; |
---|
[109] | 223 | |
---|
[671] | 224 | c.assimilation = 0.8*c.assimilation + 0.2*stdprops.assimilation; |
---|
| 225 | c.stamina = 0.8*c.stamina + 0.2*stdprops.stamina; |
---|
| 226 | c.muscle_power = 0.8*c.muscle_power + 0.2*stdprops.muscle_power; |
---|
| 227 | c.ingestion = 0.8*c.ingestion + 0.2*stdprops.ingestion; |
---|
| 228 | c.weight += (stdprops.weight - c.weight)*0.5; |
---|
| 229 | c.normalizeBiol4(); |
---|
| 230 | |
---|
| 231 | if (c.muscle_reset_range) c.muscle_bend_range = 1.0; else c.muscle_reset_range = true; |
---|
| 232 | grow(part2, g + 1, Pt3D_0, c); |
---|
| 233 | return; |
---|
[109] | 234 | } |
---|
[671] | 235 | case '(': |
---|
[109] | 236 | { |
---|
[671] | 237 | setPartMapping(part1, g); |
---|
| 238 | SList ga; |
---|
| 239 | int i, count; |
---|
| 240 | count = countBranches(g + 1, ga); |
---|
| 241 | c.muscle_reset_range = false; |
---|
| 242 | c.muscle_bend_range = 1.0 / count; |
---|
| 243 | for (i = 0; i < count; i++) |
---|
| 244 | grow(part1, (char*)ga(i), k + Pt3D(0, 0, -M_PI + (i + 1)*(2*M_PI / (count + 1))), c); |
---|
| 245 | return; |
---|
[109] | 246 | } |
---|
[671] | 247 | case ' ': case '\t': case '\n': case '\r': break; |
---|
| 248 | default: invalid = 1; return; |
---|
| 249 | } |
---|
| 250 | g++; |
---|
[109] | 251 | } |
---|
| 252 | } |
---|
| 253 | |
---|
| 254 | SyntParam* Builder::lastNeuroClassParam() |
---|
| 255 | { |
---|
[671] | 256 | if (!neuro_cls_param) |
---|
[109] | 257 | { |
---|
[671] | 258 | NeuroClass *cls = last_f1_neuro->getClass(); |
---|
| 259 | if (cls) |
---|
[109] | 260 | { |
---|
[671] | 261 | neuro_cls_param = new SyntParam(last_f1_neuro->classProperties()); |
---|
| 262 | // this is equivalent to: |
---|
| 263 | // SyntParam tmp=last_f1_neuro->classProperties(); |
---|
| 264 | // neuro_cls_param=new SyntParam(tmp); |
---|
| 265 | // interestingly, some compilers eliminate the call to new SyntParam, |
---|
| 266 | // realizing that a copy constructor is redundant when the original object is |
---|
| 267 | // temporary. there are no side effect of such optimization, as long as the |
---|
| 268 | // copy-constructed object is exact equivalent of the original. |
---|
[109] | 269 | } |
---|
| 270 | } |
---|
[671] | 271 | return neuro_cls_param; |
---|
[109] | 272 | } |
---|
| 273 | |
---|
[671] | 274 | void Builder::addClassParam(const char* name, double value) |
---|
[109] | 275 | { |
---|
[671] | 276 | lastNeuroClassParam(); |
---|
| 277 | if (neuro_cls_param) |
---|
| 278 | neuro_cls_param->setDoubleById(name, value); |
---|
[109] | 279 | } |
---|
| 280 | |
---|
[671] | 281 | void Builder::addClassParam(const char* name, const char* value) |
---|
[109] | 282 | { |
---|
[671] | 283 | lastNeuroClassParam(); |
---|
| 284 | if (neuro_cls_param) |
---|
[109] | 285 | { |
---|
[671] | 286 | ExtValue e(value); |
---|
| 287 | const ExtValue &re(e); |
---|
| 288 | neuro_cls_param->setById(name, re); |
---|
[109] | 289 | } |
---|
| 290 | } |
---|
| 291 | |
---|
[671] | 292 | int Builder::countBranches(const char*g, SList &out) |
---|
[109] | 293 | { |
---|
[671] | 294 | int gl = 0; |
---|
| 295 | out += (void*)g; |
---|
| 296 | while (gl >= 0) |
---|
[109] | 297 | { |
---|
[671] | 298 | switch (*g) |
---|
[109] | 299 | { |
---|
[671] | 300 | case 0: gl = -1; break; |
---|
[109] | 301 | case '(': case '[': ++gl; break; |
---|
| 302 | case ')': case ']': --gl; break; |
---|
[671] | 303 | case ',': if (!gl) out += (void*)(g + 1); |
---|
[109] | 304 | } |
---|
[671] | 305 | g++; |
---|
[109] | 306 | } |
---|
[671] | 307 | return out.size(); |
---|
[109] | 308 | } |
---|
| 309 | |
---|
[671] | 310 | int Builder::growJoint(int part1, int part2, Pt3D &angle, F1Props &c, const char *g) |
---|
[109] | 311 | { |
---|
[671] | 312 | double len = min(2.0, c.length); |
---|
| 313 | sprintf(tmp, "j:p1=%ld,p2=%ld,dx=%lg,rx=%lg,ry=%lg,rz=%lg,stam=%lg,vr=%g,vg=%g,vb=%g", |
---|
| 314 | part1, part2, len, angle.x, angle.y, angle.z, c.stamina, c.cred, c.cgreen, c.cblue); |
---|
| 315 | lastjoint_muscle_power = c.muscle_power; |
---|
| 316 | return model.singleStepBuild(tmp, makeRange(g)); |
---|
[109] | 317 | } |
---|
| 318 | |
---|
[671] | 319 | int Builder::growPart(F1Props &c, const char *g) |
---|
[109] | 320 | { |
---|
[671] | 321 | sprintf(tmp, "p:dn=%lg,fr=%lg,ing=%lg,as=%lg,vs=%g,vr=%g,vg=%g,vb=%g", |
---|
| 322 | 1.0 / c.weight, c.friction, c.ingestion, c.assimilation, c.visual_size, c.cred, c.cgreen, c.cblue); |
---|
| 323 | return model.singleStepBuild(tmp, makeRange(g)); |
---|
[109] | 324 | } |
---|
| 325 | |
---|
| 326 | const char *Builder::skipNeuro(const char *z) |
---|
| 327 | { |
---|
[671] | 328 | for (; *z; z++) if ((*z == ']') || (*z == ')')) break; |
---|
| 329 | return z - 1; |
---|
[109] | 330 | } |
---|
| 331 | |
---|
[671] | 332 | const char* Builder::growNeuro(const char* t, F1Props& props, int &hasmuscles) |
---|
[109] | 333 | { |
---|
[671] | 334 | const char*neuroend = skipNeuro(t); |
---|
| 335 | last_f1_neuro = model.addNewNeuro(); |
---|
| 336 | neuro_cls_param = NULL; |
---|
| 337 | last_f1_neuro->attachToPart(getLastPart()); |
---|
| 338 | const MultiRange *mr = makeRange(t - 1, neuroend + 1); |
---|
| 339 | if (mr) last_f1_neuro->addMapping(*mr); |
---|
| 340 | neuro_f1_to_f0 += last_f1_neuro; |
---|
[109] | 341 | |
---|
[671] | 342 | SString clsname; |
---|
| 343 | bool haveclass = 0; |
---|
| 344 | while (*t && *t <= ' ') t++; |
---|
| 345 | const char* next = (*t) ? (t + 1) : t; |
---|
| 346 | while (*next && *next <= ' ') next++; |
---|
| 347 | if (*t && *next != ',' && *next != ']') // old style muscles [|rest] or [@rest] |
---|
| 348 | switch (*t) |
---|
[109] | 349 | { |
---|
[671] | 350 | case '@': if (t[1] == ':') break; |
---|
| 351 | haveclass = 1; |
---|
| 352 | // if (!(hasmuscles&1)) |
---|
| 353 | { |
---|
| 354 | hasmuscles |= 1; |
---|
| 355 | Neuro *muscle = model.addNewNeuro(); |
---|
| 356 | sprintf(tmp, "@:p=%lg", lastjoint_muscle_power); |
---|
| 357 | muscle->addInput(last_f1_neuro); |
---|
| 358 | muscle->setDetails(tmp); |
---|
| 359 | muscle->attachToJoint(getLastJoint()); |
---|
| 360 | if (usemapping) muscle->addMapping(*makeRange(t)); |
---|
| 361 | } |
---|
| 362 | t++; |
---|
| 363 | break; |
---|
| 364 | case '|': if (t[1] == ':') break; |
---|
| 365 | haveclass = 1; |
---|
| 366 | // if (!(hasmuscles&2)) |
---|
| 367 | { |
---|
| 368 | hasmuscles |= 2; |
---|
| 369 | Neuro *muscle = model.addNewNeuro(); |
---|
| 370 | sprintf(tmp, "|:p=%lg,r=%lg", lastjoint_muscle_power, props.muscle_bend_range); |
---|
| 371 | muscle->addInput(last_f1_neuro); |
---|
| 372 | muscle->setDetails(tmp); |
---|
| 373 | muscle->attachToJoint(getLastJoint()); |
---|
| 374 | if (usemapping) muscle->addMapping(*makeRange(t)); |
---|
| 375 | } |
---|
| 376 | t++; |
---|
| 377 | break; |
---|
[109] | 378 | } |
---|
[671] | 379 | while (*t && *t <= ' ') t++; |
---|
| 380 | bool finished = 0; |
---|
| 381 | const char *begin = t; |
---|
| 382 | const char* colon = 0; |
---|
| 383 | SString classparams; |
---|
| 384 | while (!finished) |
---|
[109] | 385 | { |
---|
[671] | 386 | switch (*t) |
---|
[109] | 387 | { |
---|
[671] | 388 | case ':': colon = t; break; |
---|
| 389 | case 0: case ']': case ')': finished = 1; |
---|
[109] | 390 | // NO break! |
---|
| 391 | case ',': |
---|
[671] | 392 | if (!haveclass && !colon && t > begin) |
---|
| 393 | { |
---|
| 394 | haveclass = 1; |
---|
| 395 | SString clsname(begin, t - begin); |
---|
| 396 | clsname = trim(clsname); |
---|
[109] | 397 | last_f1_neuro->setClassName(clsname); |
---|
[671] | 398 | NeuroClass *cls = last_f1_neuro->getClass(); |
---|
[109] | 399 | if (cls) |
---|
[671] | 400 | { |
---|
| 401 | if (cls->getPreferredLocation() == 2) |
---|
[109] | 402 | last_f1_neuro->attachToJoint(getLastJoint()); |
---|
[671] | 403 | else if (cls->getPreferredLocation() == 1) |
---|
[109] | 404 | last_f1_neuro->attachToPart(getLastPart()); |
---|
| 405 | |
---|
| 406 | lastNeuroClassParam(); |
---|
| 407 | //special handling: muscle properties (can be overwritten by subsequent property assignments) |
---|
[671] | 408 | if (!strcmp(cls->getName().c_str(), "|")) |
---|
| 409 | { |
---|
| 410 | neuro_cls_param->setDoubleById("p", lastjoint_muscle_power); |
---|
| 411 | neuro_cls_param->setDoubleById("r", props.muscle_bend_range); |
---|
[109] | 412 | } |
---|
[671] | 413 | else if (!strcmp(cls->getName().c_str(), "@")) |
---|
| 414 | { |
---|
| 415 | neuro_cls_param->setDoubleById("p", lastjoint_muscle_power); |
---|
| 416 | } |
---|
[109] | 417 | } |
---|
[671] | 418 | } |
---|
| 419 | else if (colon && (colon > begin) && (t > colon)) |
---|
| 420 | growConnection(begin, colon, t, props); |
---|
| 421 | if (t[0] != ',') t--; |
---|
| 422 | begin = t + 1; colon = 0; |
---|
[109] | 423 | break; |
---|
| 424 | } |
---|
[671] | 425 | t++; |
---|
[109] | 426 | } |
---|
[671] | 427 | SAFEDELETE(neuro_cls_param); |
---|
| 428 | return t; |
---|
[109] | 429 | } |
---|
[671] | 430 | void Builder::growConnection(const char* begin, const char* colon, const char* end, F1Props& props) |
---|
[109] | 431 | { |
---|
[671] | 432 | while (*begin && *begin <= ' ') begin++; |
---|
| 433 | int i; |
---|
| 434 | if (isdigit(begin[0]) || (begin[0] == '-')) |
---|
[109] | 435 | { |
---|
[671] | 436 | double conn_weight = ExtValue::getDouble(trim(SString(colon + 1, end - (colon + 1))).c_str()); |
---|
| 437 | paInt relative = ExtValue::getInt(trim(SString(begin, colon - begin)).c_str(), false); |
---|
| 438 | int this_refno = neuro_f1_to_f0.size() - 1; |
---|
| 439 | addOrRememberInput(this_refno, this_refno + relative, conn_weight); |
---|
[109] | 440 | } |
---|
[671] | 441 | else if ((i = last_f1_neuro->extraProperties().findIdn(begin, colon - begin)) >= 0) |
---|
[109] | 442 | { |
---|
[671] | 443 | last_f1_neuro->extraProperties().set(i, colon + 1); |
---|
[109] | 444 | } |
---|
[671] | 445 | else if (isupper(begin[0]) || strchr("*|@", begin[0])) |
---|
[109] | 446 | { |
---|
[671] | 447 | SString clsname(begin, colon - begin); |
---|
| 448 | trim(clsname); |
---|
| 449 | Neuro *receptor = model.addNewNeuro(); |
---|
| 450 | receptor->setClassName(clsname); |
---|
| 451 | NeuroClass *cls = receptor->getClass(); |
---|
| 452 | if (cls) |
---|
[109] | 453 | { |
---|
[671] | 454 | if (cls->getPreferredLocation() == 2) receptor->attachToJoint(getLastJoint()); |
---|
| 455 | else if (cls->getPreferredLocation() == 1) receptor->attachToPart(getLastPart()); |
---|
[109] | 456 | } |
---|
[671] | 457 | last_f1_neuro->addInput(receptor, ExtValue::getDouble(trim(SString(colon + 1, end - (colon + 1))).c_str())); |
---|
| 458 | if (usemapping) receptor->addMapping(*makeRange(begin, end - 1)); |
---|
[109] | 459 | } |
---|
[671] | 460 | else if ((begin[0] == '>') && (begin[1])) |
---|
[109] | 461 | { |
---|
[671] | 462 | Neuro *out = model.addNewNeuro(); |
---|
| 463 | out->addInput(last_f1_neuro, ExtValue::getDouble(trim(SString(colon + 1, end - (colon + 1))).c_str())); |
---|
| 464 | out->setClassName(SString(begin + 1, end - colon - 1)); |
---|
| 465 | if (begin[1] == '@') |
---|
[109] | 466 | { |
---|
[671] | 467 | sprintf(tmp, "p=%lg", lastjoint_muscle_power); |
---|
| 468 | out->setClassParams(tmp); |
---|
[109] | 469 | } |
---|
[671] | 470 | else if (begin[1] == '|') |
---|
[109] | 471 | { |
---|
[671] | 472 | sprintf(tmp, "p=%lg,r=%lg", lastjoint_muscle_power, props.muscle_bend_range); |
---|
| 473 | out->setClassParams(tmp); |
---|
[109] | 474 | } |
---|
[671] | 475 | NeuroClass *cls = out->getClass(); |
---|
| 476 | if (cls) |
---|
[109] | 477 | { |
---|
[671] | 478 | if (cls->getPreferredLocation() == 2) out->attachToJoint(getLastJoint()); |
---|
| 479 | else if (cls->getPreferredLocation() == 1) out->attachToPart(getLastPart()); |
---|
[109] | 480 | } |
---|
[671] | 481 | if (usemapping) out->addMapping(*makeRange(begin, end - 1)); |
---|
[109] | 482 | } |
---|
[671] | 483 | else if (*begin == '!') addClassParam("fo", ExtValue::getDouble(trim(SString(colon + 1, end - (colon + 1))).c_str())); |
---|
| 484 | else if (*begin == '=') addClassParam("in", ExtValue::getDouble(trim(SString(colon + 1, end - (colon + 1))).c_str())); |
---|
| 485 | else if (*begin == '/') addClassParam("si", ExtValue::getDouble(trim(SString(colon + 1, end - (colon + 1))).c_str())); |
---|
| 486 | else if (islower(begin[0])) |
---|
[109] | 487 | { |
---|
[671] | 488 | SString name(begin, colon - begin); |
---|
| 489 | SString value(colon + 1, end - (colon + 1)); |
---|
| 490 | addClassParam(name.c_str(), value.c_str()); |
---|
[109] | 491 | } |
---|
| 492 | } |
---|