[286] | 1 | // This file is a part of Framsticks SDK. http://www.framsticks.com/ |
---|
[732] | 2 | // Copyright (C) 1999-2018 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, |
---|
[732] | 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; |
---|
[719] | 45 | void grow(int part1, const char*g, Pt3D k, F1Props c, int branching_part); |
---|
[671] | 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 */ |
---|
[732] | 108 | SString GenoConv_f1::convert(SString &i, MultiMap *map, bool using_checkpoints) |
---|
[109] | 109 | { |
---|
[671] | 110 | const char* g = i.c_str(); |
---|
| 111 | Builder builder(g, map ? 1 : 0); |
---|
[732] | 112 | builder.model.open(using_checkpoints); |
---|
[726] | 113 | builder.grow(-1, g, Pt3D_0, stdprops, -1); // uses Model::addFromString() to create model elements |
---|
[671] | 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 | |
---|
[719] | 135 | void Builder::grow(int part1, const char*g, Pt3D k, F1Props c, int branching_part) |
---|
[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 | { |
---|
[719] | 143 | case 0: return; |
---|
| 144 | case ',': case ')': setPartMapping(branching_part, g); return; |
---|
[671] | 145 | case 'R': k.x += 0.7853; setPartMapping(part1, g); break; |
---|
| 146 | case 'r': k.x -= 0.7853; setPartMapping(part1, g); break; |
---|
| 147 | case 'Q': c.twist += (1.58 - c.twist)*0.3; setPartMapping(part1, g); break; |
---|
| 148 | case 'q': c.twist += (-1.58 - c.twist)*0.3; setPartMapping(part1, g); break; |
---|
[109] | 149 | #ifdef v1f1COMPATIBLE |
---|
[671] | 150 | case 'L': c.length += (3.0 - c.length)*0.3; setPartMapping(part1, g); break; |
---|
[109] | 151 | #else |
---|
[671] | 152 | case 'L': c.length += (2.0 - c.length)*0.3; setPartMapping(part1, g); break; |
---|
[109] | 153 | #endif |
---|
[671] | 154 | case 'l': c.length += (0.33 - c.length)*0.3; setPartMapping(part1, g); break; |
---|
| 155 | case 'A': c.assimilation += (1 - c.assimilation)*0.8; c.normalizeBiol4(); setPartMapping(part1, g); break; |
---|
| 156 | case 'a': c.assimilation -= c.assimilation*0.4; c.normalizeBiol4(); setPartMapping(part1, g); break; |
---|
| 157 | case 'I': c.ingestion += (1 - c.ingestion)*0.8; c.normalizeBiol4(); setPartMapping(part1, g); break; |
---|
| 158 | case 'i': c.ingestion -= c.ingestion*0.4; c.normalizeBiol4(); setPartMapping(part1, g); break; |
---|
| 159 | case 'S': c.stamina += (1 - c.stamina)*0.8; c.normalizeBiol4(); setPartMapping(part1, g); break; |
---|
| 160 | case 's': c.stamina -= c.stamina*0.4; c.normalizeBiol4(); setPartMapping(part1, g); break; |
---|
| 161 | case 'M': c.muscle_power += (1 - c.muscle_power)*0.8; c.normalizeBiol4(); setPartMapping(part1, g); break; |
---|
| 162 | case 'm': c.muscle_power -= c.muscle_power*0.4; c.normalizeBiol4(); setPartMapping(part1, g); break; |
---|
| 163 | case 'C': c.curvedness += (2.0 - c.curvedness)*0.25; setPartMapping(part1, g); break; |
---|
| 164 | case 'c': c.curvedness += (-2.0 - c.curvedness)*0.25; setPartMapping(part1, g); break; |
---|
| 165 | case 'F': c.friction += (4 - c.friction)*0.2; setPartMapping(part1, g); break; |
---|
| 166 | case 'f': c.friction -= c.friction*0.2; setPartMapping(part1, g); break; |
---|
| 167 | case 'W': c.weight += (2.0 - c.weight)*0.3; setPartMapping(part1, g); break; |
---|
| 168 | case 'w': c.weight += (0.5 - c.weight)*0.3; setPartMapping(part1, g); break; |
---|
| 169 | case 'E': c.energy += (10.0 - c.energy)*0.1; setPartMapping(part1, g); break; |
---|
| 170 | case 'e': c.energy -= c.energy*0.1; setPartMapping(part1, g); break; |
---|
[109] | 171 | |
---|
[671] | 172 | case 'D': c.cred += (1.0 - c.cred)*0.25; setPartMapping(part1, g); break; |
---|
| 173 | case 'd': c.cred += (0.0 - c.cred)*0.25; setPartMapping(part1, g); break; |
---|
| 174 | case 'G': c.cgreen += (1.0 - c.cgreen)*0.25; setPartMapping(part1, g); break; |
---|
| 175 | case 'g': c.cgreen += (0.0 - c.cgreen)*0.25; setPartMapping(part1, g); break; |
---|
| 176 | case 'B': c.cblue += (1.0 - c.cblue)*0.25; setPartMapping(part1, g); break; |
---|
| 177 | case 'b': c.cblue += (0.0 - c.cblue)*0.25; setPartMapping(part1, g); break; |
---|
| 178 | case 'H': c.visual_size += (0.7 - c.visual_size)*0.25; setPartMapping(part1, g); break; |
---|
| 179 | case 'h': c.visual_size += (0.05 - c.visual_size)*0.25; setPartMapping(part1, g); break; |
---|
[109] | 180 | |
---|
[671] | 181 | case '[': //neuron |
---|
| 182 | // setdebug(g-(char*)geny,DEBUGNEURO | !l_neu); |
---|
| 183 | if (model.getJointCount()) |
---|
| 184 | g = growNeuro(g + 1, c, hasmuscles); |
---|
| 185 | else |
---|
[109] | 186 | { |
---|
[671] | 187 | logMessage("GenoConv_F1", "grow", 1, "Illegal neuron position (ignored)"); |
---|
| 188 | g = skipNeuro(g + 1); |
---|
[109] | 189 | } |
---|
[671] | 190 | break; |
---|
| 191 | case 'X': |
---|
[109] | 192 | { |
---|
[671] | 193 | int freshpart = 0; |
---|
| 194 | //setdebug(g-(char*)geny,DEBUGEST | !l_est); |
---|
| 195 | if (part1 < 0) //initial grow |
---|
[109] | 196 | { |
---|
[671] | 197 | if (model.getPartCount() > 0) |
---|
| 198 | part1 = 0; |
---|
| 199 | else |
---|
| 200 | { |
---|
| 201 | part1 = growPart(c, g); |
---|
| 202 | freshpart = 1; |
---|
| 203 | if (first_part_mapping) |
---|
[716] | 204 | { |
---|
| 205 | //mapping was defined before creating this initial Part -> put it into the Part |
---|
| 206 | assert(own_first_part_mapping); |
---|
[671] | 207 | model.getPart(part1)->setMapping(*first_part_mapping); |
---|
[716] | 208 | delete first_part_mapping; |
---|
| 209 | //first_part_mapping can be still used later but from now on it references the internal Part mapping |
---|
| 210 | first_part_mapping = model.getPart(part1)->getMapping(); |
---|
| 211 | own_first_part_mapping = false; |
---|
| 212 | } |
---|
[671] | 213 | } |
---|
| 214 | } |
---|
| 215 | if (!freshpart) |
---|
[109] | 216 | { |
---|
[671] | 217 | Part *part = model.getPart(part1); |
---|
| 218 | part->density = ((part->mass*part->density) + 1.0 / c.weight) / (part->mass + 1.0); // v=m*d |
---|
| 219 | // part->volume+=1.0/c.weight; |
---|
| 220 | part->mass += 1.0; |
---|
[109] | 221 | } |
---|
[671] | 222 | model_energy += 0.9*c.energy + 0.1; |
---|
[672] | 223 | model_energy_count++; |
---|
[109] | 224 | |
---|
[671] | 225 | int part2 = growPart(c, g); |
---|
| 226 | growJoint(part1, part2, k, c, g); |
---|
| 227 | // est* e = new est(*s,*s2,k,c,zz,this); |
---|
[109] | 228 | |
---|
[671] | 229 | // attenuate properties as they are propagated along the structure |
---|
| 230 | c.length = 0.5*c.length + 0.5*stdprops.length; |
---|
| 231 | c.visual_size = 0.5*c.visual_size + 0.5*stdprops.visual_size; |
---|
| 232 | c.curvedness = 0.66*c.curvedness; |
---|
| 233 | c.twist = 0.66*c.twist; |
---|
| 234 | c.friction = 0.8*c.friction + 0.2*stdprops.friction; |
---|
[109] | 235 | |
---|
[671] | 236 | c.assimilation = 0.8*c.assimilation + 0.2*stdprops.assimilation; |
---|
| 237 | c.stamina = 0.8*c.stamina + 0.2*stdprops.stamina; |
---|
| 238 | c.muscle_power = 0.8*c.muscle_power + 0.2*stdprops.muscle_power; |
---|
| 239 | c.ingestion = 0.8*c.ingestion + 0.2*stdprops.ingestion; |
---|
| 240 | c.weight += (stdprops.weight - c.weight)*0.5; |
---|
| 241 | c.normalizeBiol4(); |
---|
| 242 | |
---|
| 243 | if (c.muscle_reset_range) c.muscle_bend_range = 1.0; else c.muscle_reset_range = true; |
---|
[732] | 244 | model.checkpoint(); |
---|
[719] | 245 | grow(part2, g + 1, Pt3D_0, c, branching_part); |
---|
[671] | 246 | return; |
---|
[109] | 247 | } |
---|
[671] | 248 | case '(': |
---|
[109] | 249 | { |
---|
[671] | 250 | setPartMapping(part1, g); |
---|
| 251 | SList ga; |
---|
| 252 | int i, count; |
---|
| 253 | count = countBranches(g + 1, ga); |
---|
| 254 | c.muscle_reset_range = false; |
---|
| 255 | c.muscle_bend_range = 1.0 / count; |
---|
| 256 | for (i = 0; i < count; i++) |
---|
[719] | 257 | grow(part1, (char*)ga(i), k + Pt3D(0, 0, -M_PI + (i + 1)*(2 * M_PI / (count + 1))), c, part1); |
---|
[671] | 258 | return; |
---|
[109] | 259 | } |
---|
[671] | 260 | case ' ': case '\t': case '\n': case '\r': break; |
---|
| 261 | default: invalid = 1; return; |
---|
| 262 | } |
---|
| 263 | g++; |
---|
[109] | 264 | } |
---|
| 265 | } |
---|
| 266 | |
---|
| 267 | SyntParam* Builder::lastNeuroClassParam() |
---|
| 268 | { |
---|
[671] | 269 | if (!neuro_cls_param) |
---|
[109] | 270 | { |
---|
[671] | 271 | NeuroClass *cls = last_f1_neuro->getClass(); |
---|
| 272 | if (cls) |
---|
[109] | 273 | { |
---|
[671] | 274 | neuro_cls_param = new SyntParam(last_f1_neuro->classProperties()); |
---|
| 275 | // this is equivalent to: |
---|
| 276 | // SyntParam tmp=last_f1_neuro->classProperties(); |
---|
| 277 | // neuro_cls_param=new SyntParam(tmp); |
---|
| 278 | // interestingly, some compilers eliminate the call to new SyntParam, |
---|
| 279 | // realizing that a copy constructor is redundant when the original object is |
---|
| 280 | // temporary. there are no side effect of such optimization, as long as the |
---|
| 281 | // copy-constructed object is exact equivalent of the original. |
---|
[109] | 282 | } |
---|
| 283 | } |
---|
[671] | 284 | return neuro_cls_param; |
---|
[109] | 285 | } |
---|
| 286 | |
---|
[671] | 287 | void Builder::addClassParam(const char* name, double value) |
---|
[109] | 288 | { |
---|
[671] | 289 | lastNeuroClassParam(); |
---|
| 290 | if (neuro_cls_param) |
---|
| 291 | neuro_cls_param->setDoubleById(name, value); |
---|
[109] | 292 | } |
---|
| 293 | |
---|
[671] | 294 | void Builder::addClassParam(const char* name, const char* value) |
---|
[109] | 295 | { |
---|
[671] | 296 | lastNeuroClassParam(); |
---|
| 297 | if (neuro_cls_param) |
---|
[109] | 298 | { |
---|
[671] | 299 | ExtValue e(value); |
---|
| 300 | const ExtValue &re(e); |
---|
| 301 | neuro_cls_param->setById(name, re); |
---|
[109] | 302 | } |
---|
| 303 | } |
---|
| 304 | |
---|
[671] | 305 | int Builder::countBranches(const char*g, SList &out) |
---|
[109] | 306 | { |
---|
[671] | 307 | int gl = 0; |
---|
| 308 | out += (void*)g; |
---|
| 309 | while (gl >= 0) |
---|
[109] | 310 | { |
---|
[671] | 311 | switch (*g) |
---|
[109] | 312 | { |
---|
[671] | 313 | case 0: gl = -1; break; |
---|
[109] | 314 | case '(': case '[': ++gl; break; |
---|
| 315 | case ')': case ']': --gl; break; |
---|
[671] | 316 | case ',': if (!gl) out += (void*)(g + 1); |
---|
[109] | 317 | } |
---|
[671] | 318 | g++; |
---|
[109] | 319 | } |
---|
[671] | 320 | return out.size(); |
---|
[109] | 321 | } |
---|
| 322 | |
---|
[671] | 323 | int Builder::growJoint(int part1, int part2, Pt3D &angle, F1Props &c, const char *g) |
---|
[109] | 324 | { |
---|
[671] | 325 | double len = min(2.0, c.length); |
---|
[726] | 326 | sprintf(tmp, "p1=%ld,p2=%ld,dx=%lg,rx=%lg,ry=%lg,rz=%lg,stam=%lg,vr=%g,vg=%g,vb=%g", |
---|
[671] | 327 | part1, part2, len, angle.x, angle.y, angle.z, c.stamina, c.cred, c.cgreen, c.cblue); |
---|
| 328 | lastjoint_muscle_power = c.muscle_power; |
---|
[726] | 329 | return model.addFromString(Model::JointType, tmp, makeRange(g)); |
---|
[109] | 330 | } |
---|
| 331 | |
---|
[671] | 332 | int Builder::growPart(F1Props &c, const char *g) |
---|
[109] | 333 | { |
---|
[726] | 334 | sprintf(tmp, "dn=%lg,fr=%lg,ing=%lg,as=%lg,vs=%g,vr=%g,vg=%g,vb=%g", |
---|
[671] | 335 | 1.0 / c.weight, c.friction, c.ingestion, c.assimilation, c.visual_size, c.cred, c.cgreen, c.cblue); |
---|
[726] | 336 | return model.addFromString(Model::PartType, tmp, makeRange(g)); |
---|
[109] | 337 | } |
---|
| 338 | |
---|
| 339 | const char *Builder::skipNeuro(const char *z) |
---|
| 340 | { |
---|
[671] | 341 | for (; *z; z++) if ((*z == ']') || (*z == ')')) break; |
---|
| 342 | return z - 1; |
---|
[109] | 343 | } |
---|
| 344 | |
---|
[671] | 345 | const char* Builder::growNeuro(const char* t, F1Props& props, int &hasmuscles) |
---|
[109] | 346 | { |
---|
[671] | 347 | const char*neuroend = skipNeuro(t); |
---|
| 348 | last_f1_neuro = model.addNewNeuro(); |
---|
| 349 | neuro_cls_param = NULL; |
---|
| 350 | last_f1_neuro->attachToPart(getLastPart()); |
---|
| 351 | const MultiRange *mr = makeRange(t - 1, neuroend + 1); |
---|
| 352 | if (mr) last_f1_neuro->addMapping(*mr); |
---|
| 353 | neuro_f1_to_f0 += last_f1_neuro; |
---|
[109] | 354 | |
---|
[671] | 355 | SString clsname; |
---|
| 356 | bool haveclass = 0; |
---|
| 357 | while (*t && *t <= ' ') t++; |
---|
| 358 | const char* next = (*t) ? (t + 1) : t; |
---|
| 359 | while (*next && *next <= ' ') next++; |
---|
| 360 | if (*t && *next != ',' && *next != ']') // old style muscles [|rest] or [@rest] |
---|
| 361 | switch (*t) |
---|
[109] | 362 | { |
---|
[671] | 363 | case '@': if (t[1] == ':') break; |
---|
| 364 | haveclass = 1; |
---|
| 365 | // if (!(hasmuscles&1)) |
---|
| 366 | { |
---|
| 367 | hasmuscles |= 1; |
---|
| 368 | Neuro *muscle = model.addNewNeuro(); |
---|
| 369 | sprintf(tmp, "@:p=%lg", lastjoint_muscle_power); |
---|
| 370 | muscle->addInput(last_f1_neuro); |
---|
| 371 | muscle->setDetails(tmp); |
---|
| 372 | muscle->attachToJoint(getLastJoint()); |
---|
| 373 | if (usemapping) muscle->addMapping(*makeRange(t)); |
---|
| 374 | } |
---|
| 375 | t++; |
---|
| 376 | break; |
---|
| 377 | case '|': if (t[1] == ':') break; |
---|
| 378 | haveclass = 1; |
---|
| 379 | // if (!(hasmuscles&2)) |
---|
| 380 | { |
---|
| 381 | hasmuscles |= 2; |
---|
| 382 | Neuro *muscle = model.addNewNeuro(); |
---|
| 383 | sprintf(tmp, "|:p=%lg,r=%lg", lastjoint_muscle_power, props.muscle_bend_range); |
---|
| 384 | muscle->addInput(last_f1_neuro); |
---|
| 385 | muscle->setDetails(tmp); |
---|
| 386 | muscle->attachToJoint(getLastJoint()); |
---|
| 387 | if (usemapping) muscle->addMapping(*makeRange(t)); |
---|
| 388 | } |
---|
| 389 | t++; |
---|
| 390 | break; |
---|
[109] | 391 | } |
---|
[671] | 392 | while (*t && *t <= ' ') t++; |
---|
| 393 | bool finished = 0; |
---|
| 394 | const char *begin = t; |
---|
| 395 | const char* colon = 0; |
---|
| 396 | SString classparams; |
---|
| 397 | while (!finished) |
---|
[109] | 398 | { |
---|
[671] | 399 | switch (*t) |
---|
[109] | 400 | { |
---|
[671] | 401 | case ':': colon = t; break; |
---|
| 402 | case 0: case ']': case ')': finished = 1; |
---|
[109] | 403 | // NO break! |
---|
| 404 | case ',': |
---|
[671] | 405 | if (!haveclass && !colon && t > begin) |
---|
| 406 | { |
---|
| 407 | haveclass = 1; |
---|
| 408 | SString clsname(begin, t - begin); |
---|
| 409 | clsname = trim(clsname); |
---|
[109] | 410 | last_f1_neuro->setClassName(clsname); |
---|
[671] | 411 | NeuroClass *cls = last_f1_neuro->getClass(); |
---|
[109] | 412 | if (cls) |
---|
[671] | 413 | { |
---|
| 414 | if (cls->getPreferredLocation() == 2) |
---|
[109] | 415 | last_f1_neuro->attachToJoint(getLastJoint()); |
---|
[671] | 416 | else if (cls->getPreferredLocation() == 1) |
---|
[109] | 417 | last_f1_neuro->attachToPart(getLastPart()); |
---|
| 418 | |
---|
| 419 | lastNeuroClassParam(); |
---|
| 420 | //special handling: muscle properties (can be overwritten by subsequent property assignments) |
---|
[671] | 421 | if (!strcmp(cls->getName().c_str(), "|")) |
---|
| 422 | { |
---|
| 423 | neuro_cls_param->setDoubleById("p", lastjoint_muscle_power); |
---|
| 424 | neuro_cls_param->setDoubleById("r", props.muscle_bend_range); |
---|
[109] | 425 | } |
---|
[671] | 426 | else if (!strcmp(cls->getName().c_str(), "@")) |
---|
| 427 | { |
---|
| 428 | neuro_cls_param->setDoubleById("p", lastjoint_muscle_power); |
---|
| 429 | } |
---|
[109] | 430 | } |
---|
[671] | 431 | } |
---|
| 432 | else if (colon && (colon > begin) && (t > colon)) |
---|
| 433 | growConnection(begin, colon, t, props); |
---|
| 434 | if (t[0] != ',') t--; |
---|
| 435 | begin = t + 1; colon = 0; |
---|
[109] | 436 | break; |
---|
| 437 | } |
---|
[671] | 438 | t++; |
---|
[109] | 439 | } |
---|
[671] | 440 | SAFEDELETE(neuro_cls_param); |
---|
| 441 | return t; |
---|
[109] | 442 | } |
---|
[671] | 443 | void Builder::growConnection(const char* begin, const char* colon, const char* end, F1Props& props) |
---|
[109] | 444 | { |
---|
[671] | 445 | while (*begin && *begin <= ' ') begin++; |
---|
| 446 | int i; |
---|
| 447 | if (isdigit(begin[0]) || (begin[0] == '-')) |
---|
[109] | 448 | { |
---|
[671] | 449 | double conn_weight = ExtValue::getDouble(trim(SString(colon + 1, end - (colon + 1))).c_str()); |
---|
| 450 | paInt relative = ExtValue::getInt(trim(SString(begin, colon - begin)).c_str(), false); |
---|
| 451 | int this_refno = neuro_f1_to_f0.size() - 1; |
---|
| 452 | addOrRememberInput(this_refno, this_refno + relative, conn_weight); |
---|
[109] | 453 | } |
---|
[671] | 454 | else if ((i = last_f1_neuro->extraProperties().findIdn(begin, colon - begin)) >= 0) |
---|
[109] | 455 | { |
---|
[671] | 456 | last_f1_neuro->extraProperties().set(i, colon + 1); |
---|
[109] | 457 | } |
---|
[671] | 458 | else if (isupper(begin[0]) || strchr("*|@", begin[0])) |
---|
[109] | 459 | { |
---|
[671] | 460 | SString clsname(begin, colon - begin); |
---|
| 461 | trim(clsname); |
---|
| 462 | Neuro *receptor = model.addNewNeuro(); |
---|
| 463 | receptor->setClassName(clsname); |
---|
| 464 | NeuroClass *cls = receptor->getClass(); |
---|
| 465 | if (cls) |
---|
[109] | 466 | { |
---|
[671] | 467 | if (cls->getPreferredLocation() == 2) receptor->attachToJoint(getLastJoint()); |
---|
| 468 | else if (cls->getPreferredLocation() == 1) receptor->attachToPart(getLastPart()); |
---|
[109] | 469 | } |
---|
[671] | 470 | last_f1_neuro->addInput(receptor, ExtValue::getDouble(trim(SString(colon + 1, end - (colon + 1))).c_str())); |
---|
| 471 | if (usemapping) receptor->addMapping(*makeRange(begin, end - 1)); |
---|
[109] | 472 | } |
---|
[671] | 473 | else if ((begin[0] == '>') && (begin[1])) |
---|
[109] | 474 | { |
---|
[671] | 475 | Neuro *out = model.addNewNeuro(); |
---|
| 476 | out->addInput(last_f1_neuro, ExtValue::getDouble(trim(SString(colon + 1, end - (colon + 1))).c_str())); |
---|
| 477 | out->setClassName(SString(begin + 1, end - colon - 1)); |
---|
| 478 | if (begin[1] == '@') |
---|
[109] | 479 | { |
---|
[671] | 480 | sprintf(tmp, "p=%lg", lastjoint_muscle_power); |
---|
| 481 | out->setClassParams(tmp); |
---|
[109] | 482 | } |
---|
[671] | 483 | else if (begin[1] == '|') |
---|
[109] | 484 | { |
---|
[671] | 485 | sprintf(tmp, "p=%lg,r=%lg", lastjoint_muscle_power, props.muscle_bend_range); |
---|
| 486 | out->setClassParams(tmp); |
---|
[109] | 487 | } |
---|
[671] | 488 | NeuroClass *cls = out->getClass(); |
---|
| 489 | if (cls) |
---|
[109] | 490 | { |
---|
[671] | 491 | if (cls->getPreferredLocation() == 2) out->attachToJoint(getLastJoint()); |
---|
| 492 | else if (cls->getPreferredLocation() == 1) out->attachToPart(getLastPart()); |
---|
[109] | 493 | } |
---|
[671] | 494 | if (usemapping) out->addMapping(*makeRange(begin, end - 1)); |
---|
[109] | 495 | } |
---|
[671] | 496 | else if (*begin == '!') addClassParam("fo", ExtValue::getDouble(trim(SString(colon + 1, end - (colon + 1))).c_str())); |
---|
| 497 | else if (*begin == '=') addClassParam("in", ExtValue::getDouble(trim(SString(colon + 1, end - (colon + 1))).c_str())); |
---|
| 498 | else if (*begin == '/') addClassParam("si", ExtValue::getDouble(trim(SString(colon + 1, end - (colon + 1))).c_str())); |
---|
| 499 | else if (islower(begin[0])) |
---|
[109] | 500 | { |
---|
[671] | 501 | SString name(begin, colon - begin); |
---|
| 502 | SString value(colon + 1, end - (colon + 1)); |
---|
| 503 | addClassParam(name.c_str(), value.c_str()); |
---|
[109] | 504 | } |
---|
| 505 | } |
---|