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