[286] | 1 | // This file is a part of Framsticks SDK. http://www.framsticks.com/ |
---|
[932] | 2 | // Copyright (C) 1999-2020 Maciej Komosinski and Szymon Ulatowski. |
---|
[286] | 3 | // See LICENSE.txt for details. |
---|
[109] | 4 | |
---|
| 5 | #include <stdlib.h> |
---|
| 6 | #include <math.h> |
---|
| 7 | #include <stdio.h> |
---|
| 8 | #include <string.h> |
---|
| 9 | #include <ctype.h> |
---|
| 10 | #include <time.h> |
---|
| 11 | #include <errno.h> |
---|
| 12 | |
---|
| 13 | /////////////////////////////// MODELPARTS.CPP /////////////// |
---|
| 14 | |
---|
| 15 | #include "modelparts.h" |
---|
| 16 | #include "model.h" |
---|
| 17 | |
---|
| 18 | #include <common/nonstd.h> |
---|
| 19 | #include <frams/param/param.h> |
---|
| 20 | #include <frams/neuro/neurolibrary.h> |
---|
| 21 | #include <frams/util/multirange.h> |
---|
| 22 | #include <frams/util/extvalue.h> |
---|
| 23 | #include <frams/param/paramobj.h> |
---|
[952] | 24 | #include <frams/neuro/neuroimpl.h> |
---|
[109] | 25 | |
---|
| 26 | #include F0_DEFASSIGN_FILE |
---|
| 27 | |
---|
[288] | 28 | #ifndef SDK_WITHOUT_FRAMS |
---|
[109] | 29 | #include <frams/neuro/neuroclsobject.h> |
---|
| 30 | #endif |
---|
| 31 | |
---|
| 32 | ///////////////////////// |
---|
| 33 | |
---|
| 34 | PartBase::~PartBase() |
---|
[714] | 35 | { |
---|
| 36 | if (mapped) delete mapped; |
---|
| 37 | } |
---|
[109] | 38 | |
---|
| 39 | void PartBase::notifyMappingChange() |
---|
| 40 | { |
---|
[714] | 41 | if (owner) owner->partmappingchanged = 1; |
---|
[109] | 42 | } |
---|
| 43 | |
---|
| 44 | void PartBase::setMapping(const IRange &r) |
---|
| 45 | { |
---|
[714] | 46 | if (mapped) (*mapped) = r; |
---|
| 47 | else mapped = new MultiRange(r); |
---|
| 48 | notifyMappingChange(); |
---|
[109] | 49 | } |
---|
| 50 | |
---|
| 51 | void PartBase::clearMapping() |
---|
| 52 | { |
---|
[714] | 53 | if (mapped) { delete mapped; mapped = 0; } |
---|
[109] | 54 | } |
---|
| 55 | |
---|
| 56 | void PartBase::addMapping(const IRange &r) |
---|
| 57 | { |
---|
[714] | 58 | if (mapped) mapped->add(r); |
---|
| 59 | else mapped = new MultiRange(r); |
---|
| 60 | notifyMappingChange(); |
---|
[109] | 61 | } |
---|
| 62 | |
---|
| 63 | void PartBase::setMapping(const MultiRange &mr) |
---|
| 64 | { |
---|
[714] | 65 | if (mapped) (*mapped) = mr; |
---|
| 66 | else mapped = new MultiRange(mr); |
---|
| 67 | notifyMappingChange(); |
---|
[109] | 68 | } |
---|
| 69 | |
---|
| 70 | void PartBase::addMapping(const MultiRange &mr) |
---|
| 71 | { |
---|
[714] | 72 | if (mapped) mapped->add(mr); |
---|
| 73 | else mapped = new MultiRange(mr); |
---|
| 74 | notifyMappingChange(); |
---|
[109] | 75 | } |
---|
| 76 | |
---|
[899] | 77 | void PartBase::setInfo(const SString &name, const SString &value) |
---|
[109] | 78 | { |
---|
[714] | 79 | strSetField(info, name, value); |
---|
[109] | 80 | } |
---|
| 81 | |
---|
[899] | 82 | void PartBase::setInfo(const SString &name, int value) |
---|
[109] | 83 | { |
---|
[714] | 84 | setInfo(name, SString::valueOf(value)); |
---|
[109] | 85 | } |
---|
| 86 | |
---|
[899] | 87 | void PartBase::setInfo(const SString &name, double value) |
---|
[109] | 88 | { |
---|
[714] | 89 | setInfo(name, SString::valueOf(value)); |
---|
[109] | 90 | } |
---|
| 91 | |
---|
[899] | 92 | SString PartBase::getInfo(const SString &name) |
---|
[109] | 93 | { |
---|
[714] | 94 | return strGetField(info, name); |
---|
[109] | 95 | } |
---|
| 96 | |
---|
| 97 | ///////////////////////// |
---|
| 98 | |
---|
[714] | 99 | NeuroClass::NeuroClass(ParamEntry *_props, SString _description, |
---|
| 100 | int _prefinputs, int _prefoutput, int _preflocation, |
---|
[932] | 101 | int *_vectordata, bool own_vd, int vhints, int sup_shapes) |
---|
[109] | 102 | :ownedvectordata(own_vd), |
---|
[714] | 103 | name(_props->name), longname(_props->id), description(_description), |
---|
| 104 | props(_props), ownedprops(false), |
---|
| 105 | prefinputs(_prefinputs), |
---|
| 106 | prefoutput(_prefoutput), |
---|
| 107 | preflocation(_preflocation), |
---|
[932] | 108 | supported_shape_types(sup_shapes), |
---|
[714] | 109 | vectordata(_vectordata), |
---|
| 110 | visualhints(vhints), impl_count(0),/*impl(0),*/active(1), genactive(0) |
---|
[109] | 111 | {} |
---|
| 112 | |
---|
| 113 | NeuroClass::~NeuroClass() |
---|
| 114 | { |
---|
[714] | 115 | setSymbolGlyph(0, 0); |
---|
| 116 | if (props && ownedprops) |
---|
| 117 | ParamObject::freeParamTab(props); |
---|
[109] | 118 | } |
---|
| 119 | |
---|
| 120 | NeuroClass::NeuroClass() |
---|
| 121 | :ownedvectordata(0), |
---|
[714] | 122 | name("Invalid"), |
---|
| 123 | props(empty_paramtab), ownedprops(false), |
---|
| 124 | prefinputs(0), prefoutput(0), |
---|
| 125 | preflocation(0), vectordata(0), |
---|
| 126 | visualhints(0), impl_count(0), /*impl(0),*/ active(1), genactive(0) |
---|
[109] | 127 | {} |
---|
| 128 | |
---|
[714] | 129 | void NeuroClass::resetActive() |
---|
| 130 | { |
---|
| 131 | for (int i = 0; i < Neuro::getClassCount(); i++) |
---|
| 132 | { |
---|
| 133 | Neuro::getClass(i)->genactive = 0; |
---|
| 134 | Neuro::getClass(i)->active = 1; |
---|
| 135 | } |
---|
| 136 | } |
---|
| 137 | |
---|
[899] | 138 | void NeuroClass::setGenActive(const char *genactive_classes[]) |
---|
[714] | 139 | { |
---|
[899] | 140 | for (const char **n = genactive_classes; *n; n++) |
---|
[714] | 141 | { |
---|
[899] | 142 | NeuroClass *cls = Neuro::getClass(*n); |
---|
[714] | 143 | if (cls) cls->genactive = 1; |
---|
| 144 | } |
---|
| 145 | } |
---|
| 146 | |
---|
[109] | 147 | SString NeuroClass::getSummary() |
---|
| 148 | { |
---|
[714] | 149 | SString t; |
---|
| 150 | t = getDescription(); |
---|
| 151 | if (t.len()) t += "\n\n"; |
---|
| 152 | t += "Characteristics:\n"; |
---|
| 153 | if (getPreferredInputs()) |
---|
[109] | 154 | { |
---|
[714] | 155 | if (getPreferredInputs() < 0) t += " supports any number of inputs\n"; |
---|
| 156 | else if (getPreferredInputs() == 1) t += " uses single input\n"; |
---|
| 157 | else t += SString::sprintf(" uses %d inputs\n", getPreferredInputs()); |
---|
[109] | 158 | } |
---|
[714] | 159 | else t += " does not use inputs\n"; |
---|
| 160 | if (getPreferredOutput()) |
---|
| 161 | t += " provides output value\n"; |
---|
| 162 | else |
---|
| 163 | t += " does not provide output value\n"; |
---|
| 164 | switch (getPreferredLocation()) |
---|
[109] | 165 | { |
---|
[714] | 166 | case 0: t += " does not require location in body\n"; break; |
---|
| 167 | case 1: t += " should be located on a Part\n"; break; |
---|
| 168 | case 2: t += " should be located on a Joint\n"; break; |
---|
[109] | 169 | } |
---|
[714] | 170 | Param p = getProperties(); |
---|
| 171 | if (p.getPropCount()) |
---|
[109] | 172 | { |
---|
[714] | 173 | if (t.len()) t += "\n\n"; |
---|
| 174 | t += "Properties:\n"; |
---|
| 175 | const char *h; |
---|
| 176 | int i; |
---|
| 177 | for (i = 0; i < p.getPropCount(); i++) |
---|
[109] | 178 | { |
---|
[714] | 179 | if (i) t += "\n"; |
---|
| 180 | t += " "; t += p.name(i); t += " ("; t += p.id(i); t += ") "; |
---|
[743] | 181 | t += p.friendlyTypeDescr(i); |
---|
[714] | 182 | if (h = p.help(i)) if (*h) { t += " - "; t += h; } |
---|
[109] | 183 | } |
---|
| 184 | } |
---|
[714] | 185 | return t; |
---|
[109] | 186 | } |
---|
| 187 | |
---|
| 188 | ///////////////////////// |
---|
| 189 | |
---|
| 190 | ///////////////////////////////////// |
---|
| 191 | |
---|
[714] | 192 | Neuro::Neuro(double _state, double _inertia, double _force, double _sigmo) |
---|
| 193 | :PartBase(getDefaultStyle()), state(_state) |
---|
[109] | 194 | { |
---|
[714] | 195 | flags = 0; |
---|
| 196 | myclass = 0; |
---|
| 197 | knownclass = 1; |
---|
| 198 | part_refno = -1; joint_refno = -1; |
---|
[109] | 199 | } |
---|
| 200 | |
---|
[714] | 201 | Neuro::Neuro(void) :PartBase(getDefaultStyle()) |
---|
[109] | 202 | { |
---|
[714] | 203 | defassign(); |
---|
| 204 | state = 0.0; |
---|
| 205 | myclass = NULL; |
---|
| 206 | myclassname = "N";//default d="N" but f0.def is unable to set this (d is GETSET, not a regular FIELD) |
---|
| 207 | knownclass = 0; |
---|
| 208 | refno = 0; |
---|
| 209 | pos = Pt3D_0; rot = Pt3D_0; |
---|
| 210 | parent = 0; part = 0; joint = 0; |
---|
| 211 | parentcount = 0; |
---|
| 212 | flags = 0; |
---|
| 213 | part_refno = -1; joint_refno = -1; |
---|
[109] | 214 | } |
---|
| 215 | |
---|
| 216 | |
---|
| 217 | Neuro::~Neuro() |
---|
| 218 | { |
---|
[714] | 219 | int i; |
---|
| 220 | for (i = 0; i < inputs.size(); i++) |
---|
[109] | 221 | { |
---|
[714] | 222 | NInput &ni = inputs(i); |
---|
| 223 | if (ni.info) delete ni.info; |
---|
[109] | 224 | } |
---|
| 225 | } |
---|
| 226 | |
---|
[899] | 227 | SString **Neuro::inputInfo(int i) |
---|
[109] | 228 | { |
---|
[714] | 229 | if (i >= getInputCount()) return 0; |
---|
| 230 | return &inputs(i).info; |
---|
[109] | 231 | } |
---|
| 232 | |
---|
[899] | 233 | void Neuro::setInputInfo(int i, const SString &name, const SString &value) |
---|
[109] | 234 | { |
---|
[714] | 235 | SString **s = inputInfo(i); |
---|
| 236 | if (!s) return; |
---|
| 237 | if (!*s) *s = new SString(); |
---|
| 238 | strSetField(**s, name, value); |
---|
[109] | 239 | } |
---|
| 240 | |
---|
[899] | 241 | void Neuro::setInputInfo(int i, const SString &name, int value) |
---|
[109] | 242 | { |
---|
[714] | 243 | setInputInfo(i, name, SString::valueOf(value)); |
---|
[109] | 244 | } |
---|
| 245 | |
---|
[899] | 246 | void Neuro::setInputInfo(int i, const SString &name, double value) |
---|
[109] | 247 | { |
---|
[714] | 248 | setInputInfo(i, name, SString::valueOf(value)); |
---|
[109] | 249 | } |
---|
| 250 | |
---|
| 251 | SString Neuro::getInputInfo(int i) |
---|
| 252 | { |
---|
[714] | 253 | SString **s = inputInfo(i); |
---|
| 254 | if (!s) return SString(); |
---|
| 255 | if (!*s) return SString(); |
---|
| 256 | return **s; |
---|
[109] | 257 | } |
---|
| 258 | |
---|
[899] | 259 | SString Neuro::getInputInfo(int i, const SString &name) |
---|
[109] | 260 | { |
---|
[714] | 261 | SString **s = inputInfo(i); |
---|
| 262 | if (!s) return SString(); |
---|
| 263 | if (!*s) return SString(); |
---|
| 264 | return strGetField(**s, name); |
---|
[109] | 265 | } |
---|
| 266 | |
---|
[899] | 267 | void Neuro::operator=(const Neuro &src) |
---|
[109] | 268 | { |
---|
[714] | 269 | refno = src.refno; |
---|
| 270 | state = src.state; |
---|
| 271 | part_refno = -1; |
---|
| 272 | joint_refno = -1; |
---|
| 273 | pos = src.pos; rot = src.rot; |
---|
| 274 | parent = 0; part = 0; joint = 0; |
---|
| 275 | parentcount = 0; |
---|
| 276 | flags = 0; |
---|
| 277 | myclass = src.myclass; |
---|
| 278 | knownclass = src.knownclass; |
---|
| 279 | myclassname = src.myclassname; |
---|
| 280 | myclassparams = src.myclassparams; |
---|
[109] | 281 | } |
---|
| 282 | |
---|
| 283 | void Neuro::attachToPart(int i) |
---|
[714] | 284 | { |
---|
| 285 | attachToPart((i >= 0) ? owner->getPart(i) : 0); |
---|
| 286 | } |
---|
[109] | 287 | |
---|
| 288 | void Neuro::attachToJoint(int i) |
---|
[714] | 289 | { |
---|
| 290 | attachToJoint((i >= 0) ? owner->getJoint(i) : 0); |
---|
| 291 | } |
---|
[109] | 292 | |
---|
| 293 | int Neuro::getClassCount() |
---|
[714] | 294 | { |
---|
| 295 | return NeuroLibrary::staticlibrary.getClassCount(); |
---|
| 296 | } |
---|
[109] | 297 | |
---|
[899] | 298 | NeuroClass *Neuro::getClass(int classindex) |
---|
[714] | 299 | { |
---|
| 300 | return NeuroLibrary::staticlibrary.getClass(classindex); |
---|
| 301 | } |
---|
[109] | 302 | |
---|
[899] | 303 | NeuroClass *Neuro::getClass(const SString &classname) |
---|
[714] | 304 | { |
---|
| 305 | return NeuroLibrary::staticlibrary.findClass(classname); |
---|
| 306 | } |
---|
[109] | 307 | |
---|
[899] | 308 | int Neuro::getClassIndex(const NeuroClass *nc) |
---|
[714] | 309 | { |
---|
[899] | 310 | return NeuroLibrary::staticlibrary.classes.find((void *)nc); |
---|
[714] | 311 | } |
---|
[109] | 312 | |
---|
[899] | 313 | NeuroClass *Neuro::getClass() |
---|
[109] | 314 | { |
---|
[714] | 315 | checkClass(); |
---|
| 316 | return myclass; |
---|
[109] | 317 | } |
---|
| 318 | |
---|
[899] | 319 | void Neuro::setClass(NeuroClass *cl) |
---|
[109] | 320 | { |
---|
[714] | 321 | myclass = cl; |
---|
| 322 | myclassname = cl->getName(); |
---|
| 323 | knownclass = 1; |
---|
[109] | 324 | } |
---|
| 325 | |
---|
| 326 | SString Neuro::getClassName(int classindex) |
---|
| 327 | { |
---|
[714] | 328 | NeuroClass *cl = NeuroLibrary::staticlibrary.getClass(classindex); |
---|
| 329 | return cl ? cl->getName() : SString(); |
---|
[109] | 330 | } |
---|
| 331 | |
---|
[899] | 332 | void Neuro::setDetails(const SString &details) |
---|
[109] | 333 | { |
---|
[714] | 334 | int colon = details.indexOf(':'); |
---|
| 335 | if (colon >= 0) { myclassname = details.substr(0, colon); myclassparams = details.substr(colon + 1); } |
---|
| 336 | else { myclassname = details; myclassparams = 0; } |
---|
| 337 | knownclass = 0; |
---|
[109] | 338 | } |
---|
| 339 | |
---|
| 340 | SString Neuro::getDetails() |
---|
| 341 | { |
---|
[714] | 342 | SString ret = getClassName(); |
---|
| 343 | if (myclassparams.len()) { if (!ret.len()) ret = "N"; ret += ":"; ret += myclassparams; } |
---|
| 344 | return ret; |
---|
[109] | 345 | } |
---|
| 346 | |
---|
| 347 | void Neuro::checkClass() |
---|
| 348 | { |
---|
[714] | 349 | if (knownclass) return; |
---|
| 350 | myclass = getClass(myclassname); |
---|
| 351 | knownclass = 1; |
---|
[109] | 352 | } |
---|
| 353 | |
---|
| 354 | SyntParam Neuro::classProperties(bool handle_defaults_when_saving) |
---|
| 355 | { |
---|
[714] | 356 | NeuroClass *cl = getClass(); |
---|
| 357 | ParamEntry *pe = cl ? cl->getParamTab() : emptyParamTab; |
---|
| 358 | return SyntParam(pe, &myclassparams, handle_defaults_when_saving); |
---|
[109] | 359 | } |
---|
| 360 | |
---|
| 361 | SString Neuro::getClassName() |
---|
| 362 | { |
---|
[714] | 363 | return myclassname; |
---|
[109] | 364 | } |
---|
| 365 | |
---|
[899] | 366 | void Neuro::setClassName(const SString &clazz) |
---|
[109] | 367 | { |
---|
[714] | 368 | myclassname = clazz; |
---|
| 369 | knownclass = 0; |
---|
[109] | 370 | } |
---|
| 371 | |
---|
[899] | 372 | int Neuro::addInput(Neuro *child, double weight, const SString *info) |
---|
[109] | 373 | { |
---|
[714] | 374 | inputs += NInput(child, weight, (info && (info->len())) ? new SString(*info) : 0); |
---|
| 375 | child->parentcount++; |
---|
| 376 | if (child->parentcount == 1) { child->parent = this; } |
---|
| 377 | return inputs.size() - 1; |
---|
[109] | 378 | } |
---|
| 379 | |
---|
[899] | 380 | int Neuro::findInput(Neuro *child) const |
---|
[109] | 381 | { |
---|
[714] | 382 | for (int i = 0; i < inputs.size(); i++) |
---|
| 383 | if (inputs(i).n == child) return i; |
---|
| 384 | return -1; |
---|
[109] | 385 | } |
---|
| 386 | |
---|
[899] | 387 | Neuro *Neuro::getInput(int i, double &weight) const |
---|
[109] | 388 | { |
---|
[714] | 389 | if (i >= getInputCount()) return 0; |
---|
| 390 | NInput &inp = inputs(i); |
---|
| 391 | weight = inp.weight; |
---|
| 392 | return inp.n; |
---|
[109] | 393 | } |
---|
| 394 | |
---|
| 395 | double Neuro::getInputWeight(int i) const |
---|
| 396 | { |
---|
[714] | 397 | return inputs(i).weight; |
---|
[109] | 398 | } |
---|
| 399 | |
---|
[714] | 400 | void Neuro::setInputWeight(int i, double w) |
---|
[109] | 401 | { |
---|
[714] | 402 | inputs(i).weight = w; |
---|
[109] | 403 | } |
---|
| 404 | |
---|
[899] | 405 | void Neuro::setInput(int i, Neuro *n) |
---|
[109] | 406 | { |
---|
[714] | 407 | NInput &inp = inputs(i); |
---|
| 408 | inp.n = n; |
---|
[109] | 409 | } |
---|
| 410 | |
---|
[899] | 411 | void Neuro::setInput(int i, Neuro *n, double w) |
---|
[109] | 412 | { |
---|
[714] | 413 | NInput &inp = inputs(i); |
---|
| 414 | inp.n = n; |
---|
| 415 | inp.weight = w; |
---|
[109] | 416 | } |
---|
| 417 | |
---|
| 418 | void Neuro::removeInput(int refno) |
---|
| 419 | { |
---|
[714] | 420 | Neuro *child = getInput(refno); |
---|
| 421 | child->parentcount--; |
---|
| 422 | if (child->parent == this) child->parent = 0; |
---|
| 423 | SString *s = inputs(refno).info; |
---|
| 424 | if (s) delete s; |
---|
| 425 | inputs.remove(refno); |
---|
[109] | 426 | } |
---|
| 427 | |
---|
[899] | 428 | int Neuro::removeInput(Neuro *child) |
---|
[109] | 429 | { |
---|
[714] | 430 | int i = findInput(child); |
---|
| 431 | if (i >= 0) removeInput(i); |
---|
| 432 | return i; |
---|
[109] | 433 | } |
---|
| 434 | |
---|
| 435 | int Neuro::getOutputsCount() const |
---|
| 436 | { |
---|
[714] | 437 | int c = 0; |
---|
| 438 | for (int i = 0; i < owner->getNeuroCount(); i++) |
---|
| 439 | for (int j = 0; j < owner->getNeuro(i)->getInputCount(); j++) c += owner->getNeuro(i)->getInput(j) == this; |
---|
| 440 | return c; |
---|
[109] | 441 | } |
---|
| 442 | |
---|
| 443 | int Neuro::isOldEffector() |
---|
| 444 | { |
---|
[714] | 445 | static SString bend("|"), rot("@"); |
---|
| 446 | return ((getClassName() == bend) || (getClassName() == rot)); |
---|
[109] | 447 | } |
---|
| 448 | |
---|
| 449 | int Neuro::isOldReceptor() |
---|
| 450 | { |
---|
[714] | 451 | static SString g("G"), t("T"), s("S"); |
---|
| 452 | return ((getClassName() == g) || (getClassName() == t) || (getClassName() == s)); |
---|
[109] | 453 | } |
---|
| 454 | |
---|
| 455 | int Neuro::isOldNeuron() |
---|
| 456 | { |
---|
[714] | 457 | static SString n("N"); |
---|
| 458 | return (getClassName() == n); |
---|
[109] | 459 | } |
---|
| 460 | |
---|
| 461 | int Neuro::isNNConnection() |
---|
| 462 | { |
---|
[714] | 463 | static SString conn("-"); |
---|
| 464 | return (getClassName() == conn); |
---|
[109] | 465 | } |
---|
| 466 | |
---|
[899] | 467 | int Neuro::findInputs(SList &result, const char *classname, const Part *part, const Joint *joint) const |
---|
[109] | 468 | { |
---|
[714] | 469 | Neuro *nu; |
---|
| 470 | SString cn(classname); |
---|
| 471 | int n0 = result.size(); |
---|
| 472 | for (int i = 0; nu = getInput(i); i++) |
---|
[109] | 473 | { |
---|
[714] | 474 | if (part) |
---|
| 475 | if (nu->part != part) continue; |
---|
| 476 | if (joint) |
---|
| 477 | if (nu->joint != joint) continue; |
---|
| 478 | if (classname) |
---|
| 479 | if (nu->getClassName() != cn) continue; |
---|
[899] | 480 | result += (void *)nu; |
---|
[109] | 481 | } |
---|
[714] | 482 | return result.size() - n0; |
---|
[109] | 483 | } |
---|
| 484 | |
---|
[899] | 485 | int Neuro::findOutputs(SList &result, const char *classname, const Part *part, const Joint *joint) const |
---|
[109] | 486 | { // not very efficient... |
---|
[714] | 487 | Neuro *nu, *inp; |
---|
| 488 | SString cn(classname); |
---|
| 489 | SList found; |
---|
| 490 | int n0 = result.size(); |
---|
| 491 | for (int i = 0; nu = getModel().getNeuro(i); i++) |
---|
[109] | 492 | { |
---|
[714] | 493 | if (part) |
---|
| 494 | if (nu->part != part) continue; |
---|
| 495 | if (joint) |
---|
| 496 | if (nu->joint != joint) continue; |
---|
| 497 | if (classname) |
---|
| 498 | if (inp->getClassName() != cn) continue; |
---|
| 499 | for (int j = 0; inp = nu->getInput(j); j++) |
---|
| 500 | if (inp == this) |
---|
[109] | 501 | { |
---|
[899] | 502 | result += (void *)nu; |
---|
| 503 | break; |
---|
[109] | 504 | } |
---|
| 505 | } |
---|
[714] | 506 | return result.size() - n0; |
---|
[109] | 507 | } |
---|
| 508 | |
---|
| 509 | void Neuro::get_inputCount(PARAMGETARGS) |
---|
[714] | 510 | { |
---|
| 511 | ret->setInt(inputs.size()); |
---|
| 512 | } |
---|
[109] | 513 | |
---|
[714] | 514 | void Neuro::p_getInputNeuroDef(ExtValue *args, ExtValue *ret) |
---|
[109] | 515 | { |
---|
[714] | 516 | int i = args->getInt(); |
---|
| 517 | if ((i < 0) || (i >= inputs.size())) |
---|
| 518 | ret->setEmpty(); |
---|
| 519 | else |
---|
| 520 | ret->setObject(ExtObject(&Neuro::getStaticParam(), inputs(i).n)); |
---|
[109] | 521 | } |
---|
| 522 | |
---|
[714] | 523 | void Neuro::p_getInputWeight(ExtValue *args, ExtValue *ret) |
---|
[109] | 524 | { |
---|
[714] | 525 | int i = args->getInt(); |
---|
| 526 | if ((i < 0) || (i >= inputs.size())) |
---|
| 527 | ret->setEmpty(); |
---|
| 528 | else |
---|
| 529 | ret->setDouble(inputs(i).weight); |
---|
[109] | 530 | } |
---|
| 531 | |
---|
[714] | 532 | void Neuro::p_getInputNeuroIndex(ExtValue *args, ExtValue *ret) |
---|
[109] | 533 | { |
---|
[714] | 534 | int i = args->getInt(); |
---|
| 535 | if ((i < 0) || (i >= inputs.size())) |
---|
| 536 | ret->setInt(-1); |
---|
| 537 | else |
---|
| 538 | ret->setInt(inputs(i).n->refno); |
---|
[109] | 539 | } |
---|
| 540 | |
---|
| 541 | void Neuro::get_classObject(PARAMGETARGS) |
---|
| 542 | { |
---|
[288] | 543 | #ifndef SDK_WITHOUT_FRAMS |
---|
[714] | 544 | NeuroClassExt::makeStaticObject(ret, getClass()); |
---|
[109] | 545 | #endif |
---|
| 546 | } |
---|
| 547 | |
---|
| 548 | /////////////////////////////////////// |
---|
| 549 | |
---|
| 550 | SString Part::getDefaultStyle() |
---|
[714] | 551 | { |
---|
| 552 | return SString("part"); |
---|
| 553 | } |
---|
[109] | 554 | SString Joint::getDefaultStyle() |
---|
[714] | 555 | { |
---|
| 556 | return SString("joint"); |
---|
| 557 | } |
---|
[109] | 558 | /* |
---|
| 559 | const SString& Neuro::getDefaultStyle() |
---|
| 560 | {static SString s("neuro"); return s;} |
---|
| 561 | const SString& NeuroItem::getDefaultStyle() |
---|
| 562 | {static SString s("neuroitem"); return s;} |
---|
| 563 | */ |
---|
| 564 | SString Neuro::getDefaultStyle() |
---|
[714] | 565 | { |
---|
| 566 | return SString("neuro"); |
---|
| 567 | } |
---|
[109] | 568 | |
---|
[714] | 569 | Part::Part(enum Shape s) :PartBase(getDefaultStyle()) |
---|
[109] | 570 | { |
---|
[714] | 571 | o = Orient_1; |
---|
| 572 | p = Pt3D_0; |
---|
| 573 | rot = Pt3D_0; |
---|
| 574 | flags = 0; |
---|
| 575 | defassign(); |
---|
| 576 | shape = s; |
---|
| 577 | mass = 1; |
---|
[109] | 578 | } |
---|
| 579 | |
---|
[899] | 580 | void Part::operator=(const Part &src) |
---|
[109] | 581 | { |
---|
[714] | 582 | p = src.p; o = src.o; |
---|
| 583 | flags = src.flags; |
---|
| 584 | mass = src.mass; density = src.density; |
---|
| 585 | friction = src.friction; |
---|
| 586 | ingest = src.ingest; |
---|
| 587 | assim = src.assim; |
---|
| 588 | size = src.size; |
---|
| 589 | rot = src.rot; |
---|
| 590 | refno = src.refno; |
---|
| 591 | vcolor = src.vcolor; |
---|
| 592 | vsize = src.vsize; |
---|
| 593 | vis_style = src.vis_style; |
---|
| 594 | shape = src.shape; |
---|
| 595 | scale = src.scale; |
---|
| 596 | hollow = src.hollow; |
---|
[109] | 597 | } |
---|
| 598 | |
---|
| 599 | void Part::setOrient(const Orient &_o) |
---|
| 600 | { |
---|
[714] | 601 | o = _o; |
---|
| 602 | rot.getAngles(o.x, o.z); |
---|
[109] | 603 | } |
---|
| 604 | |
---|
| 605 | void Part::setRot(const Pt3D &r) |
---|
| 606 | { |
---|
[714] | 607 | rot = r; |
---|
| 608 | o = Orient_1; |
---|
| 609 | o.rotate(rot); |
---|
[109] | 610 | } |
---|
| 611 | |
---|
[714] | 612 | void Part::setPositionAndRotationFromAxis(const Pt3D &p1, const Pt3D &p2) |
---|
[109] | 613 | { |
---|
[714] | 614 | Pt3D x = p2 - p1; |
---|
| 615 | Pt3D dir(x.y, x.z, x.x); |
---|
[899] | 616 | p = p1 + x * 0.5; |
---|
[714] | 617 | rot.getAngles(x, dir); |
---|
[109] | 618 | } |
---|
| 619 | |
---|
[899] | 620 | Param &Part::getStaticParam() |
---|
[109] | 621 | { |
---|
[714] | 622 | static Param p(f0_part_paramtab, 0, "Part"); |
---|
| 623 | return p; |
---|
[109] | 624 | } |
---|
| 625 | |
---|
| 626 | |
---|
| 627 | /////////////////////////// |
---|
| 628 | |
---|
[714] | 629 | Joint::Joint() :PartBase(getDefaultStyle()) |
---|
[109] | 630 | { |
---|
[714] | 631 | rot = Pt3D_0; |
---|
| 632 | defassign(); |
---|
| 633 | d.x = JOINT_DELTA_MARKER; |
---|
| 634 | d.y = JOINT_DELTA_MARKER; |
---|
| 635 | d.z = JOINT_DELTA_MARKER; |
---|
| 636 | part1 = 0; part2 = 0; |
---|
| 637 | flags = 0; |
---|
| 638 | usedelta = 0; |
---|
[109] | 639 | } |
---|
| 640 | |
---|
[899] | 641 | void Joint::operator=(const Joint &src) |
---|
[109] | 642 | { |
---|
[714] | 643 | rot = src.rot; |
---|
| 644 | d = src.d; |
---|
| 645 | shape = src.shape; |
---|
[915] | 646 | hinge_pos = src.hinge_pos; |
---|
| 647 | hinge_rot = src.hinge_rot; |
---|
[926] | 648 | hinge_limit_x[0] = src.hinge_limit_x[0]; |
---|
| 649 | hinge_limit_x[1] = src.hinge_limit_x[1]; |
---|
| 650 | hinge_limit_y[0] = src.hinge_limit_y[0]; |
---|
| 651 | hinge_limit_y[1] = src.hinge_limit_y[1]; |
---|
[714] | 652 | stamina = src.stamina; |
---|
| 653 | stif = src.stif; rotstif = src.rotstif; |
---|
| 654 | vis_style = src.vis_style; |
---|
| 655 | vcolor = src.vcolor; |
---|
| 656 | part1 = 0; part2 = 0; |
---|
| 657 | flags = src.flags; |
---|
| 658 | usedelta = src.usedelta; |
---|
| 659 | refno = src.refno; |
---|
[109] | 660 | } |
---|
| 661 | |
---|
[899] | 662 | void Joint::attachToParts(Part *p1, Part *p2) |
---|
[109] | 663 | { |
---|
[714] | 664 | part1 = p1; |
---|
| 665 | part2 = p2; |
---|
| 666 | if (p1 && p2) |
---|
[109] | 667 | { |
---|
[714] | 668 | o = rot; |
---|
| 669 | if (usedelta) |
---|
[109] | 670 | { |
---|
[714] | 671 | p1->o.transform(p2->o, o); |
---|
| 672 | // p2->o.x=p1->o/o.x; p2->o.y=p1->o/o.y; p2->o.z=p1->o/o.z; |
---|
| 673 | p2->p = p2->o.transform(d) + p1->p; |
---|
[109] | 674 | } |
---|
| 675 | } |
---|
| 676 | } |
---|
| 677 | |
---|
[714] | 678 | void Joint::attachToParts(int p1, int p2) |
---|
[109] | 679 | { |
---|
[714] | 680 | attachToParts((p1 >= 0) ? owner->getPart(p1) : 0, (p2 >= 0) ? owner->getPart(p2) : 0); |
---|
[109] | 681 | } |
---|
| 682 | |
---|
| 683 | void Joint::resetDelta() |
---|
| 684 | { |
---|
[714] | 685 | d = Pt3D(JOINT_DELTA_MARKER, JOINT_DELTA_MARKER, JOINT_DELTA_MARKER); |
---|
[109] | 686 | } |
---|
| 687 | |
---|
[114] | 688 | void Joint::resetDeltaMarkers() |
---|
[109] | 689 | { |
---|
[714] | 690 | if (d.x == JOINT_DELTA_MARKER) d.x = 0; |
---|
| 691 | if (d.y == JOINT_DELTA_MARKER) d.y = 0; |
---|
| 692 | if (d.z == JOINT_DELTA_MARKER) d.z = 0; |
---|
[109] | 693 | } |
---|
| 694 | |
---|
[114] | 695 | void Joint::useDelta(bool use) |
---|
[109] | 696 | { |
---|
[714] | 697 | usedelta = use; |
---|
[114] | 698 | } |
---|
| 699 | |
---|
| 700 | bool Joint::isDelta() |
---|
| 701 | { |
---|
[714] | 702 | return usedelta; |
---|
[109] | 703 | } |
---|
| 704 | |
---|
[899] | 705 | Param &Joint::getStaticParam() |
---|
[109] | 706 | { |
---|
[714] | 707 | static Param p(f0_joint_paramtab, 0, "Joint"); |
---|
| 708 | return p; |
---|
[109] | 709 | } |
---|
| 710 | |
---|
| 711 | ///////////////////////////////////////////////////////////////// |
---|
| 712 | |
---|
| 713 | #include F0_CLASSES_FILE |
---|
| 714 | |
---|
| 715 | //////////////////////////////////////// |
---|
| 716 | |
---|
[714] | 717 | ParamEntry Neuro::emptyParamTab[] = |
---|
[109] | 718 | { |
---|
[714] | 719 | { "Undefined Neuro", 1, 0, "?", }, |
---|
| 720 | { 0, 0, 0, }, |
---|
[109] | 721 | }; |
---|
| 722 | |
---|
| 723 | Param Part::extraProperties() |
---|
| 724 | { |
---|
[714] | 725 | return Param(f0_part_xtra_paramtab, this); |
---|
[109] | 726 | } |
---|
| 727 | |
---|
| 728 | Param Joint::extraProperties() |
---|
| 729 | { |
---|
[714] | 730 | return Param(f0_joint_xtra_paramtab, this); |
---|
[109] | 731 | } |
---|
| 732 | |
---|
| 733 | Param Neuro::extraProperties() |
---|
| 734 | { |
---|
[714] | 735 | return Param(f0_neuro_xtra_paramtab, this); |
---|
[109] | 736 | } |
---|
| 737 | |
---|
| 738 | Param Part::properties() |
---|
| 739 | { |
---|
[714] | 740 | return Param(f0_part_paramtab, this); |
---|
[109] | 741 | } |
---|
| 742 | |
---|
| 743 | Param Joint::properties() |
---|
| 744 | { |
---|
[714] | 745 | return Param(usedelta ? f0_joint_paramtab : f0_nodeltajoint_paramtab, this); |
---|
[109] | 746 | } |
---|
| 747 | |
---|
| 748 | Param Neuro::properties() |
---|
| 749 | { |
---|
[714] | 750 | return Param(f0_neuro_paramtab, this); |
---|
[109] | 751 | } |
---|
| 752 | |
---|
[714] | 753 | class NeuroExtParamTab : public ParamTab |
---|
[109] | 754 | { |
---|
[714] | 755 | public: |
---|
| 756 | NeuroExtParamTab() :ParamTab(f0_neuro_paramtab) |
---|
[109] | 757 | { |
---|
| 758 | #define FIELDSTRUCT NeuroExt |
---|
[952] | 759 | ParamEntry entries[] = { |
---|
| 760 | { "class", 2, 0, "neuro class", "s", GETSET(neuroclass) }, |
---|
| 761 | { "liveNeuro", 2, 1, "live Neuro object", "oNeuro", GETONLY(liveNeuro) }}; |
---|
[109] | 762 | #undef FIELDSTRUCT |
---|
[952] | 763 | for(auto& e : entries) add(&e); |
---|
[109] | 764 | |
---|
| 765 | #define FIELDSTRUCT Neuro |
---|
[714] | 766 | ParamEntry entry2 = { "state", 2, 0, "state", "f", FIELD(state) }; |
---|
[109] | 767 | #undef FIELDSTRUCT |
---|
[714] | 768 | add(&entry2); |
---|
[109] | 769 | } |
---|
| 770 | }; |
---|
| 771 | |
---|
[899] | 772 | Param &Neuro::getStaticParam() |
---|
[109] | 773 | { |
---|
[714] | 774 | static Param p(f0_neuro_paramtab, 0, "NeuroDef"); |
---|
| 775 | return p; |
---|
[109] | 776 | } |
---|
| 777 | |
---|
| 778 | //////////////////////// |
---|
| 779 | |
---|
| 780 | NeuroConn::NeuroConn() |
---|
| 781 | { |
---|
[714] | 782 | defassign(); |
---|
[109] | 783 | } |
---|
| 784 | |
---|
| 785 | ////////////////////////////////////// |
---|
| 786 | |
---|
| 787 | ParamEntry *NeuroExt::getParamTab() |
---|
| 788 | { |
---|
[714] | 789 | static NeuroExtParamTab tab; |
---|
| 790 | return tab.getParamTab(); |
---|
[109] | 791 | } |
---|
| 792 | |
---|
| 793 | void NeuroExt::get_neuroclass(PARAMGETARGS) |
---|
[714] | 794 | { |
---|
| 795 | ret->setString(getClassName()); |
---|
| 796 | } |
---|
[109] | 797 | |
---|
| 798 | int NeuroExt::set_neuroclass(PARAMSETARGS) |
---|
[714] | 799 | { |
---|
| 800 | setClassName(arg->getString()); return PSET_CHANGED; |
---|
| 801 | } |
---|
[952] | 802 | |
---|
| 803 | void NeuroExt::get_liveNeuro(PARAMGETARGS) |
---|
| 804 | { |
---|
| 805 | #ifndef SDK_WITHOUT_FRAMS |
---|
| 806 | NeuroNetImpl::getLiveNeuroObject(this,ret); |
---|
| 807 | #endif |
---|
| 808 | } |
---|