1 | // This file is a part of Framsticks SDK. http://www.framsticks.com/ |
---|
2 | // Copyright (C) 1999-2015 Maciej Komosinski and Szymon Ulatowski. |
---|
3 | // See LICENSE.txt for details. |
---|
4 | |
---|
5 | #include "neuroimpl.h" |
---|
6 | #include "neurofactory.h" |
---|
7 | #include <frams/util/rndutil.h> |
---|
8 | #include <common/nonstd_math.h> |
---|
9 | #ifndef SDK_WITHOUT_FRAMS |
---|
10 | #include <frams/simul/creature.h> |
---|
11 | #include <frams/mech/creatmechobj.h> |
---|
12 | #include <frams/simul/livegroups.h> |
---|
13 | #include <frams/simul/simul.h> |
---|
14 | #endif |
---|
15 | |
---|
16 | const int NeuroImpl::ENDDRAWING=-9999; |
---|
17 | const int NeuroImpl::MAXDRAWINGXY=0xffff; |
---|
18 | |
---|
19 | int NeuroNetImpl::mytags_id=0; |
---|
20 | |
---|
21 | ///////////////////////////////////////////////////////// |
---|
22 | |
---|
23 | #define FIELDSTRUCT NeuroNetConfig |
---|
24 | static ParamEntry nncfg_paramtab[]= |
---|
25 | { |
---|
26 | {"Creature: Neurons",1,3,"nnsim",}, |
---|
27 | {"randinit",1,0,"Random initialization","f 0 10 0.01",FIELD(randominit),"Allowed range for initializing all neuron states with uniform distribution random numbers and zero mean. Set to 0 for deterministic initialization."}, |
---|
28 | {"nnoise",1,0,"Noise","f 0 1 0",FIELD(nnoise),"Gaussian neural noise: a random value is added to each neural output in each simulation step. Set standard deviation here to add random noise, or 0 for deterministic simulation."}, |
---|
29 | {"touchrange",1,0,"T receptor range","f 0 100 1",FIELD(touchrange),}, |
---|
30 | {0,0,0,}, |
---|
31 | }; |
---|
32 | #undef FIELDSTRUCT |
---|
33 | |
---|
34 | NeuroNetConfig::NeuroNetConfig(NeuroFactory *fac) |
---|
35 | :par(nncfg_paramtab,this), |
---|
36 | randominit(0.01), |
---|
37 | nnoise(0), |
---|
38 | touchrange(1), |
---|
39 | factory(fac) |
---|
40 | {} |
---|
41 | |
---|
42 | ///////////////////////////////////////////////////////////////// |
---|
43 | |
---|
44 | NeuroNetImpl::NeuroNetImpl(Model& model, NeuroNetConfig& conf |
---|
45 | #ifdef NEURO_SIGNALS |
---|
46 | , ChannelSpace *ch |
---|
47 | #endif |
---|
48 | ) |
---|
49 | :mod(model),config(conf), |
---|
50 | isbuilt(1),errorcount(0) |
---|
51 | #ifdef NEURO_SIGNALS |
---|
52 | ,channels(ch) |
---|
53 | #endif |
---|
54 | { |
---|
55 | if (!mytags_id) mytags_id=mod.userdata.newID(); |
---|
56 | |
---|
57 | Neuro *n; |
---|
58 | NeuroImpl *ni; |
---|
59 | Joint *j; |
---|
60 | int i; |
---|
61 | DB(printf("makeNeuroNet(%p)\n",&mod)); |
---|
62 | |
---|
63 | minorder=3; maxorder=0; |
---|
64 | errorcount=0; |
---|
65 | |
---|
66 | for (i=0;j=mod.getJoint(i);i++) |
---|
67 | j->flags&=~(4+8); // todo: !!!neuroitems shouldn't use model fields!!! |
---|
68 | |
---|
69 | for (i=0;n=mod.getNeuro(i);i++) |
---|
70 | { |
---|
71 | ni=conf.factory->createNeuroImpl(n); |
---|
72 | n->userdata[mytags_id]=ni; |
---|
73 | if (!ni) { errorcount++; |
---|
74 | FMprintf("NeuroNetImpl","create",FMLV_WARN,"neuron #%d (%s) implementation not available", |
---|
75 | i,(const char*)n->getClassName()); |
---|
76 | continue; } // implementation not available?! |
---|
77 | ni->owner=this; |
---|
78 | ni->neuro=n; |
---|
79 | ni->readParam(); |
---|
80 | } |
---|
81 | |
---|
82 | for (i=0;n=mod.getNeuro(i);i++) |
---|
83 | { |
---|
84 | n->state+=(rnd01-0.5)*config.randominit; |
---|
85 | ni=(NeuroImpl*)n->userdata[mytags_id]; |
---|
86 | if (!ni) continue; |
---|
87 | if (!ni->lateinit()) |
---|
88 | { ni->status=NeuroImpl::InitError; errorcount++; |
---|
89 | FMprintf("NeuroNetImpl","create",FMLV_WARN,"neuron #%d (%s) initialization failed", |
---|
90 | i,(const char*)n->getClassName()); |
---|
91 | continue; } |
---|
92 | ni->status=NeuroImpl::InitOk; |
---|
93 | int order=ni->getSimOrder(); |
---|
94 | if (order<0) order=0; else if (order>2) order=2; |
---|
95 | if (order<minorder) minorder=order; |
---|
96 | if (order>maxorder) maxorder=order; |
---|
97 | neurons[order]+=ni; |
---|
98 | if (ni->getNeedPhysics()) |
---|
99 | neurons[3]+=ni; |
---|
100 | } |
---|
101 | cnode=mod.delmodel_list.add(STATRICKCALLBACK(this,&NeuroNetImpl::destroyNN,0)); |
---|
102 | } |
---|
103 | |
---|
104 | void NeuroNetImpl::destroyNN(CALLBACKARGS) |
---|
105 | { |
---|
106 | if (!isbuilt) return; |
---|
107 | DB(printf("destroyNeuroNet(%p)\n",&mod)); |
---|
108 | NeuroImpl *ni; |
---|
109 | Neuro *n; |
---|
110 | for (int i=0;n=mod.getNeuro(i);i++) |
---|
111 | { |
---|
112 | ni=(NeuroImpl*)n->userdata[mytags_id]; |
---|
113 | delete ni; |
---|
114 | n->userdata[mytags_id]=0; |
---|
115 | } |
---|
116 | mod.delmodel_list.remove(cnode); |
---|
117 | isbuilt=0; errorcount=0; |
---|
118 | delete this; |
---|
119 | } |
---|
120 | |
---|
121 | NeuroNetImpl::~NeuroNetImpl() |
---|
122 | { |
---|
123 | destroyNN(0,0); |
---|
124 | } |
---|
125 | |
---|
126 | void NeuroNetImpl::simulateNeuroNet() |
---|
127 | { |
---|
128 | NeuroImpl *ni; |
---|
129 | for (int order=minorder;order<=maxorder;order++) |
---|
130 | { |
---|
131 | int i; |
---|
132 | SList &nlist=neurons[order]; |
---|
133 | for (i=0;ni=(NeuroImpl*)nlist(i);i++) |
---|
134 | ni->go(); |
---|
135 | for (i=0;ni=(NeuroImpl*)nlist(i);i++) |
---|
136 | ni->commit(); |
---|
137 | } |
---|
138 | } |
---|
139 | |
---|
140 | void NeuroNetImpl::simulateNeuroPhysics() |
---|
141 | { |
---|
142 | NeuroImpl *ni; |
---|
143 | int i; |
---|
144 | SList &nlist=neurons[3]; |
---|
145 | for (i=0;ni=(NeuroImpl*)nlist(i);i++) |
---|
146 | ni->goPhysics(); |
---|
147 | } |
---|
148 | |
---|
149 | /////////////////////////////////////////////// |
---|
150 | |
---|
151 | void NeuroImpl::setChannelCount(int c) |
---|
152 | { |
---|
153 | if (c<1) c=1; |
---|
154 | if (c==channels) return; |
---|
155 | if (c<channels) {channels=c; chstate.trim(c-1); chnewstate.trim(c-1); return;} |
---|
156 | double s=getState(channels-1); |
---|
157 | chnewstate.setSize(c-1); |
---|
158 | chstate.setSize(c-1); |
---|
159 | for(int i=channels;i<c;i++) |
---|
160 | {chstate(i-1)=s; chnewstate(i-1)=s;} |
---|
161 | channels=c; |
---|
162 | } |
---|
163 | |
---|
164 | void NeuroImpl::setState(double st,int channel) |
---|
165 | { |
---|
166 | validateNeuroState(st); |
---|
167 | if (channel>=channels) channel=channels-1; |
---|
168 | if (channel<=0) {newstate=st;return;} |
---|
169 | chnewstate(channel-1)=st; |
---|
170 | } |
---|
171 | |
---|
172 | void NeuroImpl::setCurrentState(double st,int channel) |
---|
173 | { |
---|
174 | validateNeuroState(st); |
---|
175 | if (channel>=channels) channel=channels-1; |
---|
176 | if (channel<=0) {neuro->state=st; return;} |
---|
177 | chstate(channel-1)=st; |
---|
178 | } |
---|
179 | |
---|
180 | double NeuroImpl::getNewState(int channel) |
---|
181 | { |
---|
182 | if (neuro->flags&Neuro::HoldState) return getState(channel); |
---|
183 | if (channel>=channels) channel=channels-1; |
---|
184 | if (channel<=0) {return newstate;} |
---|
185 | return chnewstate(channel-1); |
---|
186 | } |
---|
187 | |
---|
188 | double NeuroImpl::getState(int channel) |
---|
189 | { |
---|
190 | if (channel>=channels) channel=channels-1; |
---|
191 | if (channel<=0) return neuro->state; |
---|
192 | return chstate(channel-1); |
---|
193 | } |
---|
194 | |
---|
195 | void NeuroImpl::commit() |
---|
196 | { |
---|
197 | if (!(neuro->flags&Neuro::HoldState)) |
---|
198 | { |
---|
199 | if (channels>1) |
---|
200 | chstate=chnewstate; |
---|
201 | neuro->state=newstate; |
---|
202 | if (owner->getConfig().nnoise>0.0) |
---|
203 | { |
---|
204 | neuro->state+=RndGen.GaussStd()*owner->getConfig().nnoise; |
---|
205 | if (channels>1) |
---|
206 | for(int i=0;i<chstate.size();i++) |
---|
207 | chstate(0)+=RndGen.GaussStd()*owner->getConfig().nnoise; |
---|
208 | } |
---|
209 | } |
---|
210 | } |
---|
211 | |
---|
212 | int NeuroImpl::getInputChannelCount(int i) |
---|
213 | { |
---|
214 | if ((i<0)||(i >= neuro->getInputCount())) return 1; |
---|
215 | Neuro *nu=neuro->getInput(i); |
---|
216 | NeuroImpl *ni=NeuroNetImpl::getImpl(nu); |
---|
217 | if (!ni) return 1; |
---|
218 | return ni->channels; |
---|
219 | } |
---|
220 | |
---|
221 | double NeuroImpl::getInputState(int i,int channel) |
---|
222 | { |
---|
223 | if ((i<0)||(i >= neuro->getInputCount())) return 0; |
---|
224 | Neuro *nu=neuro->getInput(i); |
---|
225 | if (channel<=0) return nu->state; |
---|
226 | NeuroImpl *ni=NeuroNetImpl::getImpl(nu); |
---|
227 | if (!ni) return nu->state; |
---|
228 | if (channel>=ni->channels) channel=ni->channels-1; |
---|
229 | if (!channel) return nu->state; |
---|
230 | return ni->chstate(channel-1); |
---|
231 | } |
---|
232 | |
---|
233 | double NeuroImpl::getWeightedInputState(int i, int channel) |
---|
234 | { |
---|
235 | if ((i<0)||(i >= neuro->getInputCount())) return 0; |
---|
236 | double w; |
---|
237 | Neuro *nu=neuro->getInput(i,w); |
---|
238 | if (channel<=0) return nu->state * w; |
---|
239 | NeuroImpl *ni=NeuroNetImpl::getImpl(nu); |
---|
240 | if (!ni) return nu->state * w; |
---|
241 | if (channel>=ni->channels) channel=ni->channels-1; |
---|
242 | if (!channel) return w * nu->state; |
---|
243 | return w * ni->chstate(channel-1); |
---|
244 | } |
---|
245 | |
---|
246 | double NeuroImpl::getInputSum(int startwith) |
---|
247 | { |
---|
248 | if (startwith<0) return 0; |
---|
249 | Neuro *inp; |
---|
250 | double sum=0.0; |
---|
251 | while(inp=neuro->getInput(startwith++)) |
---|
252 | sum+=inp->state; |
---|
253 | return sum; |
---|
254 | } |
---|
255 | |
---|
256 | double NeuroImpl::getWeightedInputSum(int startwith) |
---|
257 | { |
---|
258 | if (startwith<0) return 0; |
---|
259 | Neuro *inp; |
---|
260 | double sum=0.0; |
---|
261 | double w; |
---|
262 | while(inp=neuro->getInput(startwith++,w)) |
---|
263 | sum+=inp->state*w; |
---|
264 | return sum; |
---|
265 | } |
---|
266 | |
---|
267 | void NeuroImpl::readParam() |
---|
268 | { |
---|
269 | Param par; |
---|
270 | if (!paramentries) return; |
---|
271 | par.setParamTab(paramentries); |
---|
272 | par.select(this); |
---|
273 | par.setDefault(); |
---|
274 | int zero=0; |
---|
275 | par.load2(neuro->getClassParams(),zero); |
---|
276 | } |
---|
277 | |
---|
278 | Param& NeuroImpl::getStaticParam() |
---|
279 | { |
---|
280 | static Param p(neuroimpl_tab,0,"Neuro"); |
---|
281 | return p; |
---|
282 | } |
---|
283 | |
---|
284 | ///////////////////////////// |
---|
285 | |
---|
286 | #ifdef NEURO_SIGNALS |
---|
287 | #define NEUROIMPL_SIGNAL_PROPS 1 |
---|
288 | #else |
---|
289 | #define NEUROIMPL_SIGNAL_PROPS 0 |
---|
290 | #endif |
---|
291 | |
---|
292 | #define FIELDSTRUCT NeuroImpl |
---|
293 | ParamEntry neuroimpl_tab[]= |
---|
294 | { |
---|
295 | {"Neuro",1,27+NEUROIMPL_SIGNAL_PROPS,"Neuro","Live Neuron object."}, |
---|
296 | |
---|
297 | {"getInputState",0,0,"Get input signal","p f(d input)",PROCEDURE(p_get),}, |
---|
298 | {"getInputWeight",0,0,"Get input weight","p f(d input)",PROCEDURE(p_getweight),}, |
---|
299 | {"getWeightedInputState",0,0,"Get weighted input signal","p f(d input)",PROCEDURE(p_getw),}, |
---|
300 | {"getInputSum",0,0,"Get signal sum","p f(d input)",PROCEDURE(p_getsum),}, |
---|
301 | {"getWeightedInputSum",0,0,"Get weighted signal sum","p f(d input)",PROCEDURE(p_getwsum),"Uses any number of inputs starting with the specified input. getWeightedInputSum(0)=weightedInputSum"}, |
---|
302 | {"getInputCount",0,PARAM_READONLY,"Get input count","d",GETONLY(count),}, |
---|
303 | {"inputSum",0,PARAM_READONLY,"Full signal sum","f",GETONLY(sum),}, |
---|
304 | {"weightedInputSum",0,PARAM_READONLY,"Full weighted signal sum","f",GETONLY(wsum),}, |
---|
305 | {"getInputChannelCount",0,0,"Get channel count for input","p d(d input)",PROCEDURE(p_getchancount),}, |
---|
306 | {"getInputStateChannel",0,0,"Get input signal from channel","p f(d input,d channel)",PROCEDURE(p_getchan),}, |
---|
307 | {"getWeightedInputStateChannel",0,0,"Get weighted input signal from channel","p f(d input,d channel)",PROCEDURE(p_getwchan),}, |
---|
308 | {"state",0,0,"Neuron state (channel 0)","f",GETSET(state),"When read, returns the current neuron state.\nWhen written, sets the 'internal' neuron state that will become current in the next step.\nTypically you should use this field, and not currState."}, |
---|
309 | {"channelCount",0,0,"Number of output channels","d",GETSET(channels),}, |
---|
310 | {"getStateChannel",0,0,"Get state for channel","p f(d channel)",PROCEDURE(p_getstate),}, |
---|
311 | {"setStateChannel",0,0,"Set state for channel","p(d channel,f value)",PROCEDURE(p_setstate),}, |
---|
312 | {"hold",0,0,"Hold state","d 0 1",GETSET(hold),"\"Holding\" means keeping the neuron state as is, blocking the regular neuron operation. This is useful when your script needs to inject some control signals into the NN. Without \"holding\", live neurons would be constantly overwriting your changes, and the rest of the NN could see inconsistent states, depending on the connections. Setting hold=1 ensures the neuron state will be only set by you, and not by the neuron. The enforced signal value can be set using Neuro.currState before or after setting hold=1. Set hold=0 to resume normal operation.",}, |
---|
313 | {"currState",0,0,"Current neuron state (channel 0)","f",GETSET(cstate),"When read, it behaves just like the 'state' field.\nWhen written, changes the current neuron state immediately, which disturbs the regular synchronous NN operation.\nThis feature should only be used while controlling the neuron 'from outside' (like a neuro probe) and not in the neuron definition. See also: Neuro.hold",}, |
---|
314 | {"setCurrStateChannel",0,0,"Set current neuron state for channel","p(d channel,f value)",PROCEDURE(p_setcstate),"Analogous to \"currState\"."}, |
---|
315 | {"position_x",0,PARAM_READONLY,"Position x","f",GETONLY(position_x),}, |
---|
316 | {"position_y",0,PARAM_READONLY,"Position y","f",GETONLY(position_y),}, |
---|
317 | {"position_z",0,PARAM_READONLY,"Position z","f",GETONLY(position_z),}, |
---|
318 | {"creature",0,PARAM_READONLY,"Gets owner creature","o Creature",GETONLY(creature),}, |
---|
319 | {"part",0,PARAM_READONLY,"The Part object where this neuron is located","o MechPart",GETONLY(part),}, |
---|
320 | {"joint",0,PARAM_READONLY,"The Joint object where this neuron is located","o MechJoint",GETONLY(joint),}, |
---|
321 | {"fields",0,PARAM_READONLY,"Custom neuron fields","o Fields",GETONLY(fields), |
---|
322 | "Neurons can have different fields depending on their class. Script neurons have their fields defined using the \"prop:\" syntax. If you develop a custom neuron script you should use the Fields object for accessing your own neuron fields. The Neuro.fields property is meant for accessing the neuron fields from the outside script.\n" |
---|
323 | "Examples:\n" |
---|
324 | "var c=Populations.createFromString(\"X[N]\");\n" |
---|
325 | "Simulator.print(\"standard neuron inertia=\"+c.getNeuro(0).fields.in);\n" |
---|
326 | "c=Populations.createFromString(\"X[Nn,e:0.1]\");\n" |
---|
327 | "Simulator.print(\"noisy neuron error rate=\"+c.getNeuro(0).fields.e);\n" |
---|
328 | "\n" |
---|
329 | "The Interface object can be used to discover which fields are available for a certain neuron object:\n" |
---|
330 | "c=Populations.createFromString(\"X[N]\");\n" |
---|
331 | "var iobj=Interface.makeFrom(c.getNeuro(0).fields);\n" |
---|
332 | "var i;\n" |
---|
333 | "for(i=0;i<iobj.properties;i++)\n" |
---|
334 | " Simulator.print(iobj.getId(i)+\" (\"+iobj.getName(i)+\")\");",}, |
---|
335 | {"def",0,PARAM_READONLY,"Neuron definition from which this live neuron was built","o NeuroDef",GETONLY(neurodef),}, |
---|
336 | {"classObject",0,PARAM_READONLY,"Neuron class for this neuron","o NeuroClass",GETONLY(classObject),}, |
---|
337 | #ifdef NEURO_SIGNALS |
---|
338 | {"signals",0,PARAM_READONLY,"Signals","o NeuroSignals",FIELD(sigs_obj),}, |
---|
339 | #endif |
---|
340 | |
---|
341 | {0,0,0,}, |
---|
342 | }; |
---|
343 | #undef FIELDSTRUCT |
---|
344 | |
---|
345 | #ifdef NEURO_SIGNALS |
---|
346 | ParamEntry neurosignals_paramtab[]= |
---|
347 | { |
---|
348 | {"NeuroSignals",1,8,"NeuroSignals","Signals attached to a neuron.\nSee also: Signal, WorldSignals, CreatureSignals.\nscripts/light.neuro and scripts/seelight.neuro are simple custom neuron examples demonstrating how to send/receive signals between creatures.",}, |
---|
349 | |
---|
350 | #define FIELDSTRUCT NeuroSignals |
---|
351 | SIGNPAR_ADD(""), |
---|
352 | SIGNPAR_RECEIVE(""), |
---|
353 | SIGNPAR_RECEIVESET(""), |
---|
354 | SIGNPAR_RECEIVEFILTER(""), |
---|
355 | SIGNPAR_RECEIVESINGLE(""), |
---|
356 | #undef FIELDSTRUCT |
---|
357 | |
---|
358 | #define FIELDSTRUCT SignalSet |
---|
359 | SIGNSETPAR_GET, |
---|
360 | SIGNSETPAR_SIZE, |
---|
361 | SIGNSETPAR_CLEAR, |
---|
362 | #undef FIELDSTRUCT |
---|
363 | {0,0,0,}, |
---|
364 | }; |
---|
365 | |
---|
366 | Param& NeuroSignals::getStaticParam() |
---|
367 | { |
---|
368 | static Param p(neurosignals_paramtab,0); |
---|
369 | return p; |
---|
370 | } |
---|
371 | #endif |
---|
372 | |
---|
373 | #ifdef NEURO_SIGNALS |
---|
374 | class NeuroSigSource: public SigSource |
---|
375 | { |
---|
376 | protected: |
---|
377 | NeuroImpl* owner; |
---|
378 | public: |
---|
379 | NeuroSigSource(NeuroImpl *n,Creature *c):SigSource(0,c),owner(n) {} |
---|
380 | bool update(); |
---|
381 | }; |
---|
382 | |
---|
383 | bool NeuroSigSource::update() |
---|
384 | { |
---|
385 | Pt3D p; |
---|
386 | if (owner->getPosition(p)) |
---|
387 | { |
---|
388 | setLocation(p); |
---|
389 | return true; |
---|
390 | } |
---|
391 | return false; |
---|
392 | } |
---|
393 | |
---|
394 | Creature *NeuroSignals::getCreature() |
---|
395 | { |
---|
396 | if (!cr) |
---|
397 | { |
---|
398 | cr=owner->getCreature(); |
---|
399 | } |
---|
400 | return cr; |
---|
401 | } |
---|
402 | |
---|
403 | void NeuroSignals::p_add(PARAMPROCARGS) |
---|
404 | { |
---|
405 | SigSource *s=new NeuroSigSource(owner,getCreature()); |
---|
406 | if (owner->owner->channels) |
---|
407 | { |
---|
408 | SigChannel *ch=owner->owner->channels->getChannel(args->getString(),true); |
---|
409 | ch->addSource(s); |
---|
410 | } |
---|
411 | else |
---|
412 | SigChannel::dummy_channel.addSource(s); |
---|
413 | sigs+=s; |
---|
414 | s->setupObject(ret); |
---|
415 | } |
---|
416 | |
---|
417 | void NeuroSignals::p_receive(PARAMPROCARGS) |
---|
418 | { |
---|
419 | SigChannel *ch; Pt3D p; |
---|
420 | if (owner->owner->channels && (ch=owner->owner->channels->getChannel(args->getString(),false)) && owner->getPosition(p)) |
---|
421 | ret->setDouble(ch->receive(&p,getCreature())); |
---|
422 | else |
---|
423 | ret->setDouble(0); |
---|
424 | } |
---|
425 | |
---|
426 | void NeuroSignals::p_receiveFilter(PARAMPROCARGS) |
---|
427 | { |
---|
428 | SigChannel *ch; Pt3D p; |
---|
429 | if (owner->owner->channels && (ch=owner->owner->channels->getChannel(args[3].getString(),false)) && owner->getPosition(p)) |
---|
430 | ret->setDouble(ch->receive(&p,getCreature(),args[2].getDouble(),args[1].getDouble(),args[0].getDouble())); |
---|
431 | else |
---|
432 | ret->setDouble(0); |
---|
433 | } |
---|
434 | |
---|
435 | void NeuroSignals::p_receiveSet(PARAMPROCARGS) |
---|
436 | { |
---|
437 | SigChannel *ch; Pt3D p; |
---|
438 | SigVector *vec=new SigVector(); |
---|
439 | if (owner->owner->channels && (ch=owner->owner->channels->getChannel(args[1].getString(),false)) && owner->getPosition(p)) |
---|
440 | ch->receiveSet(vec,&p,getCreature(),args[0].getDouble()); |
---|
441 | ret->setObject(vec->makeObject()); |
---|
442 | } |
---|
443 | |
---|
444 | void NeuroSignals::p_receiveSingle(PARAMPROCARGS) |
---|
445 | { |
---|
446 | SigChannel *ch; Pt3D p; |
---|
447 | if (owner->owner->channels && (ch=owner->owner->channels->getChannel(args[1].getString(),false)) && owner->getPosition(p)) |
---|
448 | { |
---|
449 | SigSource *src=ch->receiveSingle(&p,getCreature(),args[0].getDouble(),0,1e99); |
---|
450 | if (src) |
---|
451 | { |
---|
452 | src->setupObject(ret); |
---|
453 | return; |
---|
454 | } |
---|
455 | } |
---|
456 | ret->setEmpty(); |
---|
457 | } |
---|
458 | #endif |
---|
459 | |
---|
460 | extern ParamEntry creature_paramtab[]; |
---|
461 | static Param creature_param(creature_paramtab,0); |
---|
462 | |
---|
463 | Creature* NeuroImpl::getCreature() |
---|
464 | { |
---|
465 | #ifndef SDK_WITHOUT_FRAMS |
---|
466 | CreatMechObject *cmo=(CreatMechObject *)neuro->owner->userdata[CreatMechObject::modeltags_id]; |
---|
467 | return cmo->creature; |
---|
468 | #else |
---|
469 | return 0; |
---|
470 | #endif |
---|
471 | } |
---|
472 | |
---|
473 | void NeuroImpl::get_creature(ExtValue *ret) |
---|
474 | { |
---|
475 | #ifndef SDK_WITHOUT_FRAMS |
---|
476 | ret->setObject(ExtObject(&creature_param,getCreature())); |
---|
477 | #endif |
---|
478 | } |
---|
479 | |
---|
480 | void NeuroImpl::get_part(ExtValue *ret) |
---|
481 | { |
---|
482 | #ifndef SDK_WITHOUT_FRAMS |
---|
483 | Part *pa; |
---|
484 | if (pa=neuro->getPart()) |
---|
485 | ret->setObject(ExtObject(&MechPart::getStaticParam(),((MechPart *)pa->userdata[CreatMechObject::modeltags_id]))); |
---|
486 | else |
---|
487 | ret->setEmpty(); |
---|
488 | #endif |
---|
489 | } |
---|
490 | |
---|
491 | void NeuroImpl::get_joint(ExtValue *ret) |
---|
492 | { |
---|
493 | #ifndef SDK_WITHOUT_FRAMS |
---|
494 | Joint *jo; |
---|
495 | if (jo=neuro->getJoint()) |
---|
496 | ret->setObject(ExtObject(&MechJoint::getStaticParam(),((MechJoint*)jo->userdata[CreatMechObject::modeltags_id]))); |
---|
497 | else |
---|
498 | ret->setEmpty(); |
---|
499 | #endif |
---|
500 | } |
---|
501 | |
---|
502 | bool NeuroImpl::getPosition(Pt3D &pos) |
---|
503 | { |
---|
504 | #ifndef SDK_WITHOUT_FRAMS |
---|
505 | Part *pa; Joint *jo; |
---|
506 | if (pa=neuro->getPart()) |
---|
507 | {pos=((MechPart *)pa->userdata[CreatMechObject::modeltags_id])->p; return true;} |
---|
508 | if (jo=neuro->getJoint()) |
---|
509 | { |
---|
510 | if (neuro->getClass()->getVisualHints() & NeuroClass::AtFirstPart) |
---|
511 | pos=((MechPart*)jo->part1->userdata[CreatMechObject::modeltags_id])->p; |
---|
512 | else if (neuro->getClass()->getVisualHints() & NeuroClass::AtSecondPart) |
---|
513 | pos=((MechPart*)jo->part2->userdata[CreatMechObject::modeltags_id])->p; |
---|
514 | else pos=(((MechPart*)jo->part1->userdata[CreatMechObject::modeltags_id])->p |
---|
515 | +((MechPart*)jo->part2->userdata[CreatMechObject::modeltags_id])->p)/2; |
---|
516 | return true; |
---|
517 | } |
---|
518 | #endif |
---|
519 | return false; |
---|
520 | } |
---|
521 | |
---|
522 | void NeuroImpl::get_position_x(ExtValue *ret) |
---|
523 | {Pt3D pos; if (getPosition(pos)) ret->setDouble(pos.x); else ret->setEmpty();} |
---|
524 | void NeuroImpl::get_position_y(ExtValue *ret) |
---|
525 | {Pt3D pos; if (getPosition(pos)) ret->setDouble(pos.y); else ret->setEmpty();} |
---|
526 | void NeuroImpl::get_position_z(ExtValue *ret) |
---|
527 | {Pt3D pos; if (getPosition(pos)) ret->setDouble(pos.z); else ret->setEmpty();} |
---|
528 | |
---|
529 | |
---|
530 | void NeuroImpl::createFieldsObject() |
---|
531 | { |
---|
532 | fields_param=new Param(paramentries?paramentries:(ParamEntry*)&empty_paramtab,this,"Fields"); |
---|
533 | fields_object=new ExtObject(fields_param); |
---|
534 | } |
---|
535 | |
---|
536 | void NeuroImpl::get_fields(ExtValue *ret) |
---|
537 | { |
---|
538 | if (!fields_object) |
---|
539 | createFieldsObject(); |
---|
540 | ret->setObject(*fields_object); |
---|
541 | } |
---|
542 | |
---|
543 | void NeuroImpl::get_neurodef(ExtValue *ret) |
---|
544 | { |
---|
545 | ret->setObject(ExtObject(&Neuro::getStaticParam(),neuro)); |
---|
546 | } |
---|
547 | |
---|
548 | void NeuroImpl::get_classObject(ExtValue *ret) |
---|
549 | { |
---|
550 | #ifndef SDK_WITHOUT_FRAMS |
---|
551 | NeuroClassExt::makeStaticObject(ret,neuroclass); |
---|
552 | #endif |
---|
553 | } |
---|
554 | |
---|
555 | NeuroImpl::~NeuroImpl() |
---|
556 | { |
---|
557 | if (fields_param) |
---|
558 | { |
---|
559 | delete fields_param; |
---|
560 | delete fields_object; |
---|
561 | } |
---|
562 | } |
---|