fa2213489326693f9fa40ea30feaf992ddc9c936
[blender-staging.git] / source / blender / blenkernel / intern / sca.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
19  * All rights reserved.
20  *
21  * The Original Code is: all of this file.
22  *
23  * Contributor(s): none yet.
24  *
25  * ***** END GPL LICENSE BLOCK *****
26  * these all are linked to objects (listbase)
27  * all data is 'direct data', not Blender lib data.
28  */
29
30 /** \file blender/blenkernel/intern/sca.c
31  *  \ingroup bke
32  */
33
34
35 #include <stdio.h>
36 #include <string.h>
37 #include <float.h>
38
39 #include "MEM_guardedalloc.h"
40
41 #include "DNA_controller_types.h"
42 #include "DNA_sensor_types.h"
43 #include "DNA_actuator_types.h"
44 #include "DNA_object_types.h"
45
46 #include "BLI_blenlib.h"
47 #include "BLI_ghash.h"
48 #include "BLI_math.h"
49
50 #include "BKE_global.h"
51 #include "BKE_main.h"
52 #include "BKE_library.h"
53 #include "BKE_library_query.h"
54 #include "BKE_sca.h"
55
56 /* ******************* SENSORS ************************ */
57
58 void free_sensor(bSensor *sens)
59 {
60         if (sens->links) MEM_freeN(sens->links);
61         if (sens->data) MEM_freeN(sens->data);
62         MEM_freeN(sens);
63         
64 }
65
66 void free_sensors(ListBase *lb)
67 {
68         bSensor *sens;
69         
70         while ((sens = BLI_pophead(lb))) {
71                 free_sensor(sens);
72         }
73 }
74
75 bSensor *copy_sensor(bSensor *sens)
76 {
77         bSensor *sensn;
78         
79         sensn= MEM_dupallocN(sens);
80         sensn->flag |= SENS_NEW;
81         if (sens->data) {
82                 sensn->data= MEM_dupallocN(sens->data);
83         }
84
85         if (sens->links) sensn->links= MEM_dupallocN(sens->links);
86         
87         return sensn;
88 }
89
90 void copy_sensors(ListBase *lbn, ListBase *lbo)
91 {
92         bSensor *sens, *sensn;
93         
94         lbn->first= lbn->last= NULL;
95         sens= lbo->first;
96         while (sens) {
97                 sensn= copy_sensor(sens);
98                 BLI_addtail(lbn, sensn);
99                 sens= sens->next;
100         }
101 }
102
103 void init_sensor(bSensor *sens)
104 {
105         /* also use when sensor changes type */
106         bNearSensor *ns;
107         bMouseSensor *ms;
108         bJoystickSensor *js;
109         bRaySensor *rs;
110         
111         if (sens->data) MEM_freeN(sens->data);
112         sens->data= NULL;
113         sens->pulse = 0;
114         
115         switch (sens->type) {
116         case SENS_ALWAYS:
117                 sens->pulse = 0;
118                 break;
119         case SENS_NEAR:
120                 ns=sens->data= MEM_callocN(sizeof(bNearSensor), "nearsens");
121                 ns->dist= 1.0;
122                 ns->resetdist= 2.0;
123                 break;
124         case SENS_KEYBOARD:
125                 sens->data= MEM_callocN(sizeof(bKeyboardSensor), "keysens");
126                 break;
127         case SENS_PROPERTY:
128                 sens->data= MEM_callocN(sizeof(bPropertySensor), "propsens");
129                 break;
130         case SENS_ARMATURE:
131                 sens->data= MEM_callocN(sizeof(bArmatureSensor), "armsens");
132                 break;
133         case SENS_ACTUATOR:
134                 sens->data= MEM_callocN(sizeof(bActuatorSensor), "actsens");
135                 break;
136         case SENS_DELAY:
137                 sens->data= MEM_callocN(sizeof(bDelaySensor), "delaysens");
138                 break;
139         case SENS_MOUSE:
140                 ms=sens->data= MEM_callocN(sizeof(bMouseSensor), "mousesens");
141                 ms->type= 1; // LEFTMOUSE workaround because Mouse Sensor types enum starts in 1
142                 break;
143         case SENS_COLLISION:
144                 sens->data= MEM_callocN(sizeof(bCollisionSensor), "colsens");
145                 break;
146         case SENS_RADAR:
147                 sens->data= MEM_callocN(sizeof(bRadarSensor), "radarsens");
148                 break;
149         case SENS_RANDOM:
150                 sens->data= MEM_callocN(sizeof(bRandomSensor), "randomsens");
151                 break;
152         case SENS_RAY:
153                 sens->data= MEM_callocN(sizeof(bRaySensor), "raysens");
154                 rs = sens->data;
155                 rs->range = 0.01f;
156                 break;
157         case SENS_MESSAGE:
158                 sens->data= MEM_callocN(sizeof(bMessageSensor), "messagesens");
159                 break;
160         case SENS_JOYSTICK:
161                 sens->data= MEM_callocN(sizeof(bJoystickSensor), "joysticksens");
162                 js= sens->data;
163                 js->hatf = SENS_JOY_HAT_UP;
164                 js->axis = 1;
165                 js->hat = 1;
166                 break;
167         default:
168                 ; /* this is very severe... I cannot make any memory for this        */
169                 /* logic brick...                                                    */
170         }
171 }
172
173 bSensor *new_sensor(int type)
174 {
175         bSensor *sens;
176
177         sens= MEM_callocN(sizeof(bSensor), "Sensor");
178         sens->type= type;
179         sens->flag= SENS_SHOW;
180         
181         init_sensor(sens);
182         
183         strcpy(sens->name, "sensor");
184 // XXX  make_unique_prop_names(sens->name);
185         
186         return sens;
187 }
188
189 /* ******************* CONTROLLERS ************************ */
190
191 void unlink_controller(bController *cont)
192 {
193         bSensor *sens;
194         Object *ob;
195         
196         /* check for controller pointers in sensors */
197         ob= G.main->object.first;
198         while (ob) {
199                 sens= ob->sensors.first;
200                 while (sens) {
201                         unlink_logicbricks((void **)&cont, (void ***)&(sens->links), &sens->totlinks);
202                         sens= sens->next;
203                 }
204                 ob= ob->id.next;
205         }
206 }
207
208 void unlink_controllers(ListBase *lb)
209 {
210         bController *cont;
211         
212         for (cont= lb->first; cont; cont= cont->next)
213                 unlink_controller(cont);
214 }
215
216 void free_controller(bController *cont)
217 {
218         if (cont->links) MEM_freeN(cont->links);
219
220         /* the controller itself */
221         if (cont->data) MEM_freeN(cont->data);
222         MEM_freeN(cont);
223         
224 }
225
226 void free_controllers(ListBase *lb)
227 {
228         bController *cont;
229         
230         while ((cont = BLI_pophead(lb))) {
231                 if (cont->slinks)
232                         MEM_freeN(cont->slinks);
233                 free_controller(cont);
234         }
235 }
236
237 bController *copy_controller(bController *cont)
238 {
239         bController *contn;
240         
241         cont->mynew=contn= MEM_dupallocN(cont);
242         contn->flag |= CONT_NEW;
243         if (cont->data) {
244                 contn->data= MEM_dupallocN(cont->data);
245         }
246
247         if (cont->links) contn->links= MEM_dupallocN(cont->links);
248         contn->slinks= NULL;
249         contn->totslinks= 0;
250         
251         return contn;
252 }
253
254 void copy_controllers(ListBase *lbn, ListBase *lbo)
255 {
256         bController *cont, *contn;
257         
258         lbn->first= lbn->last= NULL;
259         cont= lbo->first;
260         while (cont) {
261                 contn= copy_controller(cont);
262                 BLI_addtail(lbn, contn);
263                 cont= cont->next;
264         }
265 }
266
267 void init_controller(bController *cont)
268 {
269         /* also use when controller changes type, leave actuators... */
270         
271         if (cont->data) MEM_freeN(cont->data);
272         cont->data= NULL;
273         
274         switch (cont->type) {
275         case CONT_EXPRESSION:
276                 cont->data= MEM_callocN(sizeof(bExpressionCont), "expcont");
277                 break;
278         case CONT_PYTHON:
279                 cont->data= MEM_callocN(sizeof(bPythonCont), "pycont");
280                 break;
281         }
282 }
283
284 bController *new_controller(int type)
285 {
286         bController *cont;
287
288         cont= MEM_callocN(sizeof(bController), "Controller");
289         cont->type= type;
290         cont->flag= CONT_SHOW;
291
292         init_controller(cont);
293         
294         strcpy(cont->name, "cont");
295 // XXX  make_unique_prop_names(cont->name);
296         
297         return cont;
298 }
299
300 /* ******************* ACTUATORS ************************ */
301
302 void unlink_actuator(bActuator *act)
303 {
304         bController *cont;
305         Object *ob;
306         
307         /* check for actuator pointers in controllers */
308         ob= G.main->object.first;
309         while (ob) {
310                 cont= ob->controllers.first;
311                 while (cont) {
312                         unlink_logicbricks((void **)&act, (void ***)&(cont->links), &cont->totlinks);
313                         cont= cont->next;
314                 }
315                 ob= ob->id.next;
316         }
317 }
318
319 void unlink_actuators(ListBase *lb)
320 {
321         bActuator *act;
322         
323         for (act= lb->first; act; act= act->next)
324                 unlink_actuator(act);
325 }
326
327 void free_actuator(bActuator *act)
328 {
329         if (act->data) {
330                 switch (act->type) {
331                         case ACT_ACTION:
332                         case ACT_SHAPEACTION:
333                         {
334                                 bActionActuator *aa = (bActionActuator *)act->data;
335                                 if (aa->act)
336                                         id_us_min((ID *)aa->act);
337                                 break;
338                         }
339                         case ACT_SOUND:
340                         {
341                                 bSoundActuator *sa = (bSoundActuator *) act->data;
342                                 if (sa->sound)
343                                         id_us_min((ID *)sa->sound);
344                                 break;
345                         }
346                 }
347
348                 MEM_freeN(act->data);
349         }
350         MEM_freeN(act);
351 }
352
353 void free_actuators(ListBase *lb)
354 {
355         bActuator *act;
356         
357         while ((act = BLI_pophead(lb))) {
358                 free_actuator(act);
359         }
360 }
361
362 bActuator *copy_actuator(bActuator *act)
363 {
364         bActuator *actn;
365         
366         act->mynew=actn= MEM_dupallocN(act);
367         actn->flag |= ACT_NEW;
368         if (act->data) {
369                 actn->data= MEM_dupallocN(act->data);
370         }
371         
372         switch (act->type) {
373                 case ACT_ACTION:
374                 case ACT_SHAPEACTION:
375                 {
376                         bActionActuator *aa = (bActionActuator *)act->data;
377                         if (aa->act)
378                                 id_us_plus((ID *)aa->act);
379                         break;
380                 }
381                 case ACT_SOUND:
382                 {
383                         bSoundActuator *sa = (bSoundActuator *)act->data;
384                         if (sa->sound)
385                                 id_us_plus((ID *)sa->sound);
386                         break;
387                 }
388         }
389         return actn;
390 }
391
392 void copy_actuators(ListBase *lbn, ListBase *lbo)
393 {
394         bActuator *act, *actn;
395         
396         lbn->first= lbn->last= NULL;
397         act= lbo->first;
398         while (act) {
399                 actn= copy_actuator(act);
400                 BLI_addtail(lbn, actn);
401                 act= act->next;
402         }
403 }
404
405 void init_actuator(bActuator *act)
406 {
407         /* also use when actuator changes type */
408         bCameraActuator *ca;
409         bObjectActuator *oa;
410         bRandomActuator *ra;
411         bSoundActuator *sa;
412         bSteeringActuator *sta;
413         bArmatureActuator *arma;
414         bMouseActuator *ma;
415         bEditObjectActuator *eoa;
416         
417         if (act->data) MEM_freeN(act->data);
418         act->data= NULL;
419         
420         switch (act->type) {
421         case ACT_ACTION:
422         case ACT_SHAPEACTION:
423                 act->data= MEM_callocN(sizeof(bActionActuator), "actionact");
424                 break;
425         case ACT_SOUND:
426                 sa = act->data= MEM_callocN(sizeof(bSoundActuator), "soundact");
427                 sa->volume = 1.0f;
428                 sa->sound3D.rolloff_factor = 1.0f;
429                 sa->sound3D.reference_distance = 1.0f;
430                 sa->sound3D.max_gain = 1.0f;
431                 sa->sound3D.cone_inner_angle = DEG2RADF(360.0f);
432                 sa->sound3D.cone_outer_angle = DEG2RADF(360.0f);
433                 sa->sound3D.max_distance = FLT_MAX;
434                 break;
435         case ACT_OBJECT:
436                 act->data= MEM_callocN(sizeof(bObjectActuator), "objectact");
437                 oa= act->data;
438                 oa->flag= 15;
439                 break;
440         case ACT_PROPERTY:
441                 act->data= MEM_callocN(sizeof(bPropertyActuator), "propact");
442                 break;
443         case ACT_CAMERA:
444                 act->data= MEM_callocN(sizeof(bCameraActuator), "camact");
445                 ca = act->data;
446                 ca->axis = OB_POSX;
447                 ca->damping = 1.0/32.0;
448                 break;
449         case ACT_EDIT_OBJECT:
450                 act->data= MEM_callocN(sizeof(bEditObjectActuator), "editobact");
451                 eoa = act->data;
452                 eoa->upflag= ACT_TRACK_UP_Z;
453                 eoa->trackflag= ACT_TRACK_TRAXIS_Y;
454                 break;
455         case ACT_CONSTRAINT:
456                 act->data= MEM_callocN(sizeof(bConstraintActuator), "cons act");
457                 break;
458         case ACT_SCENE:
459                 act->data= MEM_callocN(sizeof(bSceneActuator), "scene act");
460                 break;
461         case ACT_GROUP:
462                 act->data= MEM_callocN(sizeof(bGroupActuator), "group act");
463                 break;
464         case ACT_RANDOM:
465                 act->data= MEM_callocN(sizeof(bRandomActuator), "random act");
466                 ra=act->data;
467                 ra->float_arg_1 = 0.1f;
468                 break;
469         case ACT_MESSAGE:
470                 act->data= MEM_callocN(sizeof(bMessageActuator), "message act");
471                 break;
472         case ACT_GAME:
473                 act->data= MEM_callocN(sizeof(bGameActuator), "game act");
474                 break;
475         case ACT_VISIBILITY:
476                 act->data= MEM_callocN(sizeof(bVisibilityActuator), "visibility act");
477                 break;
478         case ACT_2DFILTER:
479                 act->data = MEM_callocN(sizeof( bTwoDFilterActuator ), "2d filter act");
480                 break;
481         case ACT_PARENT:
482                 act->data = MEM_callocN(sizeof( bParentActuator ), "parent act");
483                 break;
484         case ACT_STATE:
485                 act->data = MEM_callocN(sizeof( bStateActuator ), "state act");
486                 break;
487         case ACT_ARMATURE:
488                 act->data = MEM_callocN(sizeof( bArmatureActuator ), "armature act");
489                 arma = act->data;
490                 arma->influence = 1.f;
491                 break;
492         case ACT_STEERING:
493                 act->data = MEM_callocN(sizeof( bSteeringActuator), "steering act");
494                 sta = act->data;
495                 sta->acceleration = 3.f;
496                 sta->turnspeed = 120.f;
497                 sta->dist = 1.f;
498                 sta->velocity= 3.f;
499                 sta->flag = ACT_STEERING_AUTOMATICFACING | ACT_STEERING_LOCKZVEL;
500                 sta->facingaxis = 1;
501                 break;
502         case ACT_MOUSE:
503                 ma = act->data = MEM_callocN(sizeof( bMouseActuator ), "mouse act");
504                 ma->flag = ACT_MOUSE_VISIBLE|ACT_MOUSE_USE_AXIS_X|ACT_MOUSE_USE_AXIS_Y|ACT_MOUSE_RESET_X|ACT_MOUSE_RESET_Y|ACT_MOUSE_LOCAL_Y;
505                 ma->sensitivity[0] = ma->sensitivity[1] = 2.f;
506                 ma->object_axis[0] = ACT_MOUSE_OBJECT_AXIS_Z;
507                 ma->object_axis[1] = ACT_MOUSE_OBJECT_AXIS_X;
508                 ma->limit_y[0] = DEG2RADF(-90.0f);
509                 ma->limit_y[1] = DEG2RADF(90.0f);
510                 break;
511         default:
512                 ; /* this is very severe... I cannot make any memory for this        */
513                 /* logic brick...                                                    */
514         }
515 }
516
517 bActuator *new_actuator(int type)
518 {
519         bActuator *act;
520
521         act= MEM_callocN(sizeof(bActuator), "Actuator");
522         act->type= type;
523         act->flag= ACT_SHOW;
524         
525         init_actuator(act);
526         
527         strcpy(act->name, "act");
528 // XXX  make_unique_prop_names(act->name);
529         
530         return act;
531 }
532
533 /* ******************** GENERAL ******************* */
534 void clear_sca_new_poins_ob(Object *ob)
535 {
536         bSensor *sens;
537         bController *cont;
538         bActuator *act;
539         
540         sens= ob->sensors.first;
541         while (sens) {
542                 sens->flag &= ~SENS_NEW;
543                 sens= sens->next;
544         }
545         cont= ob->controllers.first;
546         while (cont) {
547                 cont->mynew= NULL;
548                 cont->flag &= ~CONT_NEW;
549                 cont= cont->next;
550         }
551         act= ob->actuators.first;
552         while (act) {
553                 act->mynew= NULL;
554                 act->flag &= ~ACT_NEW;
555                 act= act->next;
556         }
557 }
558
559 void clear_sca_new_poins(void)
560 {
561         Object *ob;
562         
563         ob= G.main->object.first;
564         while (ob) {
565                 clear_sca_new_poins_ob(ob);
566                 ob= ob->id.next;
567         }
568 }
569
570 void set_sca_new_poins_ob(Object *ob)
571 {
572         bSensor *sens;
573         bController *cont;
574         bActuator *act;
575         int a;
576         
577         sens= ob->sensors.first;
578         while (sens) {
579                 if (sens->flag & SENS_NEW) {
580                         for (a=0; a<sens->totlinks; a++) {
581                                 if (sens->links[a] && sens->links[a]->mynew)
582                                         sens->links[a] = sens->links[a]->mynew;
583                         }
584                 }
585                 sens= sens->next;
586         }
587
588         cont= ob->controllers.first;
589         while (cont) {
590                 if (cont->flag & CONT_NEW) {
591                         for (a=0; a<cont->totlinks; a++) {
592                                 if ( cont->links[a] && cont->links[a]->mynew)
593                                         cont->links[a] = cont->links[a]->mynew;
594                         }
595                 }
596                 cont= cont->next;
597         }
598         
599         
600         act= ob->actuators.first;
601         while (act) {
602                 if (act->flag & ACT_NEW) {
603                         if (act->type==ACT_EDIT_OBJECT) {
604                                 bEditObjectActuator *eoa= act->data;
605                                 ID_NEW_REMAP(eoa->ob);
606                         }
607                         else if (act->type==ACT_SCENE) {
608                                 bSceneActuator *sca= act->data;
609                                 ID_NEW_REMAP(sca->camera);
610                         }
611                         else if (act->type==ACT_CAMERA) {
612                                 bCameraActuator *ca= act->data;
613                                 ID_NEW_REMAP(ca->ob);
614                         }
615                         else if (act->type==ACT_OBJECT) {
616                                 bObjectActuator *oa= act->data;
617                                 ID_NEW_REMAP(oa->reference);
618                         }
619                         else if (act->type==ACT_MESSAGE) {
620                                 bMessageActuator *ma= act->data;
621                                 ID_NEW_REMAP(ma->toObject);
622                         }
623                         else if (act->type==ACT_PARENT) {
624                                 bParentActuator *para = act->data;
625                                 ID_NEW_REMAP(para->ob);
626                         }
627                         else if (act->type==ACT_ARMATURE) {
628                                 bArmatureActuator *aa = act->data;
629                                 ID_NEW_REMAP(aa->target);
630                                 ID_NEW_REMAP(aa->subtarget);
631                         }
632                         else if (act->type==ACT_PROPERTY) {
633                                 bPropertyActuator *pa= act->data;
634                                 ID_NEW_REMAP(pa->ob);
635                         }
636                         else if (act->type==ACT_STEERING) {
637                                 bSteeringActuator *sta = act->data;
638                                 ID_NEW_REMAP(sta->navmesh);
639                                 ID_NEW_REMAP(sta->target);
640                         }
641                 }
642                 act= act->next;
643         }
644 }
645
646
647 void set_sca_new_poins(void)
648 {
649         Object *ob;
650         
651         ob= G.main->object.first;
652         while (ob) {
653                 set_sca_new_poins_ob(ob);
654                 ob= ob->id.next;
655         }
656 }
657
658 /**
659  * Try to remap logic links to new object... Very, *very* weak.
660  */
661 /* XXX Logick bricks... I don't have words to say what I think about this behavior.
662  *     They have silent hidden ugly inter-objects dependencies (a sensor can link into any other
663  *     object's controllers, and same between controllers and actuators, without *any* explicit reference
664  *     to data-block involved).
665  *     This is bad, bad, bad!!!
666  *     ...and forces us to add yet another very ugly hack to get remapping with logic bricks working. */
667 void BKE_sca_logic_links_remap(Main *bmain, Object *ob_old, Object *ob_new)
668 {
669         if (ob_new == NULL || (ob_old->controllers.first == NULL && ob_old->actuators.first == NULL)) {
670                 /* Nothing to do here... */
671                 return;
672         }
673
674         GHash *controllers_map = ob_old->controllers.first ?
675                                      BLI_ghash_ptr_new_ex(__func__, BLI_listbase_count(&ob_old->controllers)) : NULL;
676         GHash *actuators_map = ob_old->actuators.first ?
677                                    BLI_ghash_ptr_new_ex(__func__, BLI_listbase_count(&ob_old->actuators)) : NULL;
678
679         /* We try to remap old controllers/actuators to new ones - in a very basic way. */
680         for (bController *cont_old = ob_old->controllers.first, *cont_new = ob_new->controllers.first;
681              cont_old;
682              cont_old = cont_old->next)
683         {
684                 bController *cont_new2 = cont_new;
685
686                 if (cont_old->mynew != NULL) {
687                         cont_new2 = cont_old->mynew;
688                         if (!(cont_new2 == cont_new || BLI_findindex(&ob_new->controllers, cont_new2) >= 0)) {
689                                 cont_new2 = NULL;
690                         }
691                 }
692                 else if (cont_new && cont_old->type != cont_new->type) {
693                         cont_new2 = NULL;
694                 }
695
696                 BLI_ghash_insert(controllers_map, cont_old, cont_new2);
697
698                 if (cont_new) {
699                         cont_new = cont_new->next;
700                 }
701         }
702
703         for (bActuator *act_old = ob_old->actuators.first, *act_new = ob_new->actuators.first;
704              act_old;
705              act_old = act_old->next)
706         {
707                 bActuator *act_new2 = act_new;
708
709                 if (act_old->mynew != NULL) {
710                         act_new2 = act_old->mynew;
711                         if (!(act_new2 == act_new || BLI_findindex(&ob_new->actuators, act_new2) >= 0)) {
712                                 act_new2 = NULL;
713                         }
714                 }
715                 else if (act_new && act_old->type != act_new->type) {
716                         act_new2 = NULL;
717                 }
718
719                 BLI_ghash_insert(actuators_map, act_old, act_new2);
720
721                 if (act_new) {
722                         act_new = act_new->next;
723                 }
724         }
725
726         for (Object *ob = bmain->object.first; ob; ob = ob->id.next) {
727                 if (controllers_map != NULL) {
728                         for (bSensor *sens = ob->sensors.first; sens; sens = sens->next) {
729                                 for (int a = 0; a < sens->totlinks; a++) {
730                                         if (sens->links[a]) {
731                                                 bController *old_link = sens->links[a];
732                                                 bController **new_link_p = (bController **)BLI_ghash_lookup_p(controllers_map, old_link);
733
734                                                 if (new_link_p == NULL) {
735                                                         /* old_link is *not* in map's keys (i.e. not to any ob_old->controllers),
736                                                          * which means we ignore it totally here. */
737                                                 }
738                                                 else if (*new_link_p == NULL) {
739                                                         unlink_logicbricks((void **)&old_link, (void ***)&(sens->links), &sens->totlinks);
740                                                         a--;
741                                                 }
742                                                 else {
743                                                         sens->links[a] = *new_link_p;
744                                                 }
745                                         }
746                                 }
747                         }
748                 }
749
750                 if (actuators_map != NULL) {
751                         for (bController *cont = ob->controllers.first; cont; cont = cont->next) {
752                                 for (int a = 0; a < cont->totlinks; a++) {
753                                         if (cont->links[a]) {
754                                                 bActuator *old_link = cont->links[a];
755                                                 bActuator **new_link_p = (bActuator **)BLI_ghash_lookup_p(actuators_map, old_link);
756
757                                                 if (new_link_p == NULL) {
758                                                         /* old_link is *not* in map's keys (i.e. not to any ob_old->actuators),
759                                                          * which means we ignore it totally here. */
760                                                 }
761                                                 else if (*new_link_p == NULL) {
762                                                         unlink_logicbricks((void **)&old_link, (void ***)&(cont->links), &cont->totlinks);
763                                                         a--;
764                                                 }
765                                                 else {
766                                                         cont->links[a] = *new_link_p;
767                                                 }
768                                         }
769                                 }
770                         }
771                 }
772         }
773
774         if (controllers_map) {
775                 BLI_ghash_free(controllers_map, NULL, NULL);
776         }
777         if (actuators_map) {
778                 BLI_ghash_free(actuators_map, NULL, NULL);
779         }
780 }
781
782 /**
783  * Handle the copying of logic data into a new object, including internal logic links update.
784  * External links (links between logic bricks of different objects) must be handled separately.
785  */
786 void BKE_sca_logic_copy(Object *ob_new, Object *ob)
787 {
788         copy_sensors(&ob_new->sensors, &ob->sensors);
789         copy_controllers(&ob_new->controllers, &ob->controllers);
790         copy_actuators(&ob_new->actuators, &ob->actuators);
791
792         for (bSensor *sens = ob_new->sensors.first; sens; sens = sens->next) {
793                 if (sens->flag & SENS_NEW) {
794                         for (int a = 0; a < sens->totlinks; a++) {
795                                 if (sens->links[a] && sens->links[a]->mynew) {
796                                         sens->links[a] = sens->links[a]->mynew;
797                                 }
798                         }
799                 }
800         }
801
802         for (bController *cont = ob_new->controllers.first; cont; cont = cont->next) {
803                 if (cont->flag & CONT_NEW) {
804                         for (int a = 0; a < cont->totlinks; a++) {
805                                 if (cont->links[a] && cont->links[a]->mynew) {
806                                         cont->links[a] = cont->links[a]->mynew;
807                                 }
808                         }
809                 }
810         }
811 }
812
813 /* ******************** INTERFACE ******************* */
814 void sca_move_sensor(bSensor *sens_to_move, Object *ob, int move_up)
815 {
816         bSensor *sens, *tmp;
817
818         int val;
819         val = move_up ? 1 : 2;
820
821         /* make sure this sensor belongs to this object */
822         sens= ob->sensors.first;
823         while (sens) {
824                 if (sens == sens_to_move) break;
825                 sens= sens->next;
826         }
827         if (!sens) return;
828
829         /* move up */
830         if (val == 1 && sens->prev) {
831                 for (tmp=sens->prev; tmp; tmp=tmp->prev) {
832                         if (tmp->flag & SENS_VISIBLE)
833                                 break;
834                 }
835                 if (tmp) {
836                         BLI_remlink(&ob->sensors, sens);
837                         BLI_insertlinkbefore(&ob->sensors, tmp, sens);
838                 }
839         }
840         /* move down */
841         else if (val == 2 && sens->next) {
842                 for (tmp=sens->next; tmp; tmp=tmp->next) {
843                         if (tmp->flag & SENS_VISIBLE)
844                                 break;
845                 }
846                 if (tmp) {
847                         BLI_remlink(&ob->sensors, sens);
848                         BLI_insertlinkafter(&ob->sensors, tmp, sens);
849                 }
850         }
851 }
852
853 void sca_move_controller(bController *cont_to_move, Object *ob, int move_up)
854 {
855         bController *cont, *tmp;
856
857         int val;
858         val = move_up ? 1 : 2;
859
860         /* make sure this controller belongs to this object */
861         cont= ob->controllers.first;
862         while (cont) {
863                 if (cont == cont_to_move) break;
864                 cont= cont->next;
865         }
866         if (!cont) return;
867
868         /* move up */
869         if (val == 1 && cont->prev) {
870                 /* locate the controller that has the same state mask but is earlier in the list */
871                 tmp = cont->prev;
872                 while (tmp) {
873                         if (tmp->state_mask & cont->state_mask) 
874                                 break;
875                         tmp = tmp->prev;
876                 }
877                 if (tmp) {
878                         BLI_remlink(&ob->controllers, cont);
879                         BLI_insertlinkbefore(&ob->controllers, tmp, cont);
880                 }
881         }
882
883         /* move down */
884         else if (val == 2 && cont->next) {
885                 tmp = cont->next;
886                 while (tmp) {
887                         if (tmp->state_mask & cont->state_mask) 
888                                 break;
889                         tmp = tmp->next;
890                 }
891                 BLI_remlink(&ob->controllers, cont);
892                 BLI_insertlinkafter(&ob->controllers, tmp, cont);
893         }
894 }
895
896 void sca_move_actuator(bActuator *act_to_move, Object *ob, int move_up)
897 {
898         bActuator *act, *tmp;
899         int val;
900
901         val = move_up ? 1 : 2;
902
903         /* make sure this actuator belongs to this object */
904         act= ob->actuators.first;
905         while (act) {
906                 if (act == act_to_move) break;
907                 act= act->next;
908         }
909         if (!act) return;
910
911         /* move up */
912         if (val == 1 && act->prev) {
913                 /* locate the first visible actuators before this one */
914                 for (tmp = act->prev; tmp; tmp=tmp->prev) {
915                         if (tmp->flag & ACT_VISIBLE)
916                                 break;
917                 }
918                 if (tmp) {
919                         BLI_remlink(&ob->actuators, act);
920                         BLI_insertlinkbefore(&ob->actuators, tmp, act);
921                 }
922         }
923         /* move down */
924         else if (val == 2 && act->next) {
925                 /* locate the first visible actuators after this one */
926                 for (tmp=act->next; tmp; tmp=tmp->next) {
927                         if (tmp->flag & ACT_VISIBLE)
928                                 break;
929                 }
930                 if (tmp) {
931                         BLI_remlink(&ob->actuators, act);
932                         BLI_insertlinkafter(&ob->actuators, tmp, act);
933                 }
934         }
935 }
936
937 void link_logicbricks(void **poin, void ***ppoin, short *tot, short size)
938 {
939         void **old_links= NULL;
940         
941         int ibrick;
942
943         /* check if the bricks are already linked */
944         for (ibrick=0; ibrick < *tot; ibrick++) {
945                 if ((*ppoin)[ibrick] == *poin)
946                         return;
947         }
948
949         if (*ppoin) {
950                 old_links= *ppoin;
951
952                 (*tot) ++;
953                 *ppoin = MEM_callocN((*tot)*size, "new link");
954         
955                 for (ibrick=0; ibrick < *(tot) - 1; ibrick++) {
956                         (*ppoin)[ibrick] = old_links[ibrick];
957                 }
958                 (*ppoin)[ibrick] = *poin;
959
960                 if (old_links) MEM_freeN(old_links);
961         }
962         else {
963                 (*tot) = 1;
964                 *ppoin = MEM_callocN((*tot)*size, "new link");
965                 (*ppoin)[0] = *poin;
966         }
967 }
968
969 void unlink_logicbricks(void **poin, void ***ppoin, short *tot)
970 {
971         int ibrick, removed;
972
973         removed= 0;
974         for (ibrick=0; ibrick < *tot; ibrick++) {
975                 if (removed) (*ppoin)[ibrick - removed] = (*ppoin)[ibrick];
976                 else if ((*ppoin)[ibrick] == *poin) removed = 1;
977         }
978
979         if (removed) {
980                 (*tot) --;
981
982                 if (*tot == 0) {
983                         MEM_freeN(*ppoin);
984                         (*ppoin)= NULL;
985                 }
986                 return;
987         }
988 }
989
990 void BKE_sca_sensors_id_loop(ListBase *senslist, SCASensorIDFunc func, void *userdata)
991 {
992         bSensor *sensor;
993
994         for (sensor = senslist->first; sensor; sensor = sensor->next) {
995                 func(sensor, (ID **)&sensor->ob, userdata, IDWALK_NOP);
996
997                 switch (sensor->type) {
998                         case SENS_TOUCH:  /* DEPRECATED */
999                         {
1000                                 bTouchSensor *ts = sensor->data;
1001                                 func(sensor, (ID **)&ts->ma, userdata, IDWALK_NOP);
1002                                 break;
1003                         }
1004                         case SENS_MESSAGE:
1005                         {
1006                                 bMessageSensor *ms = sensor->data;
1007                                 func(sensor, (ID **)&ms->fromObject, userdata, IDWALK_NOP);
1008                                 break;
1009                         }
1010                         case SENS_ALWAYS:
1011                         case SENS_NEAR:
1012                         case SENS_KEYBOARD:
1013                         case SENS_PROPERTY:
1014                         case SENS_MOUSE:
1015                         case SENS_COLLISION:
1016                         case SENS_RADAR:
1017                         case SENS_RANDOM:
1018                         case SENS_RAY:
1019                         case SENS_JOYSTICK:
1020                         case SENS_ACTUATOR:
1021                         case SENS_DELAY:
1022                         case SENS_ARMATURE:
1023                         default:
1024                                 break;
1025                 }
1026         }
1027 }
1028
1029 void BKE_sca_controllers_id_loop(ListBase *contlist, SCAControllerIDFunc func, void *userdata)
1030 {
1031         bController *controller;
1032
1033         for (controller = contlist->first; controller; controller = controller->next) {
1034                 switch (controller->type) {
1035                         case CONT_PYTHON:
1036                         {
1037                                 bPythonCont *pc = controller->data;
1038                                 func(controller, (ID **)&pc->text, userdata, IDWALK_NOP);
1039                                 break;
1040                         }
1041                         case CONT_LOGIC_AND:
1042                         case CONT_LOGIC_OR:
1043                         case CONT_EXPRESSION:
1044                         case CONT_LOGIC_NAND:
1045                         case CONT_LOGIC_NOR:
1046                         case CONT_LOGIC_XOR:
1047                         case CONT_LOGIC_XNOR:
1048                         default:
1049                                 break;
1050                 }
1051         }
1052 }
1053
1054 void BKE_sca_actuators_id_loop(ListBase *actlist, SCAActuatorIDFunc func, void *userdata)
1055 {
1056         bActuator *actuator;
1057
1058         for (actuator = actlist->first; actuator; actuator = actuator->next) {
1059                 func(actuator, (ID **)&actuator->ob, userdata, IDWALK_NOP);
1060
1061                 switch (actuator->type) {
1062                         case ACT_ADD_OBJECT:  /* DEPRECATED */
1063                         {
1064                                 bAddObjectActuator *aoa = actuator->data;
1065                                 func(actuator, (ID **)&aoa->ob, userdata, IDWALK_NOP);
1066                                 break;
1067                         }
1068                         case ACT_ACTION:
1069                         {
1070                                 bActionActuator *aa = actuator->data;
1071                                 func(actuator, (ID **)&aa->act, userdata, IDWALK_NOP);
1072                                 break;
1073                         }
1074                         case ACT_SOUND:
1075                         {
1076                                 bSoundActuator *sa = actuator->data;
1077                                 func(actuator, (ID **)&sa->sound, userdata, IDWALK_NOP);
1078                                 break;
1079                         }
1080                         case ACT_EDIT_OBJECT:
1081                         {
1082                                 bEditObjectActuator *eoa = actuator->data;
1083                                 func(actuator, (ID **)&eoa->ob, userdata, IDWALK_NOP);
1084                                 func(actuator, (ID **)&eoa->me, userdata, IDWALK_NOP);
1085                                 break;
1086                         }
1087                         case ACT_SCENE:
1088                         {
1089                                 bSceneActuator *sa = actuator->data;
1090                                 func(actuator, (ID **)&sa->scene, userdata, IDWALK_NOP);
1091                                 func(actuator, (ID **)&sa->camera, userdata, IDWALK_NOP);
1092                                 break;
1093                         }
1094                         case ACT_PROPERTY:
1095                         {
1096                                 bPropertyActuator *pa = actuator->data;
1097                                 func(actuator, (ID **)&pa->ob, userdata, IDWALK_NOP);
1098                                 break;
1099                         }
1100                         case ACT_OBJECT:
1101                         {
1102                                 bObjectActuator *oa = actuator->data;
1103                                 func(actuator, (ID **)&oa->reference, userdata, IDWALK_NOP);
1104                                 break;
1105                         }
1106                         case ACT_CAMERA:
1107                         {
1108                                 bCameraActuator *ca = actuator->data;
1109                                 func(actuator, (ID **)&ca->ob, userdata, IDWALK_NOP);
1110                                 break;
1111                         }
1112                         case ACT_MESSAGE:
1113                         {
1114                                 bMessageActuator *ma = actuator->data;
1115                                 func(actuator, (ID **)&ma->toObject, userdata, IDWALK_NOP);
1116                                 break;
1117                         }
1118                         case ACT_2DFILTER:
1119                         {
1120                                 bTwoDFilterActuator *tdfa = actuator->data;
1121                                 func(actuator, (ID **)&tdfa->text, userdata, IDWALK_NOP);
1122                                 break;
1123                         }
1124                         case ACT_PARENT:
1125                         {
1126                                 bParentActuator *pa = actuator->data;
1127                                 func(actuator, (ID **)&pa->ob, userdata, IDWALK_NOP);
1128                                 break;
1129                         }
1130                         case ACT_ARMATURE:
1131                         {
1132                                 bArmatureActuator *aa = actuator->data;
1133                                 func(actuator, (ID **)&aa->target, userdata, IDWALK_NOP);
1134                                 func(actuator, (ID **)&aa->subtarget, userdata, IDWALK_NOP);
1135                                 break;
1136                         }
1137                         case ACT_STEERING:
1138                         {
1139                                 bSteeringActuator *sa = actuator->data;
1140                                 func(actuator, (ID **)&sa->target, userdata, IDWALK_NOP);
1141                                 func(actuator, (ID **)&sa->navmesh, userdata, IDWALK_NOP);
1142                                 break;
1143                         }
1144                         /* Note: some types seems to be non-implemented? ACT_LAMP, ACT_MATERIAL... */
1145                         case ACT_LAMP:
1146                         case ACT_MATERIAL:
1147                         case ACT_END_OBJECT:  /* DEPRECATED */
1148                         case ACT_CONSTRAINT:
1149                         case ACT_GROUP:
1150                         case ACT_RANDOM:
1151                         case ACT_GAME:
1152                         case ACT_VISIBILITY:
1153                         case ACT_SHAPEACTION:
1154                         case ACT_STATE:
1155                         case ACT_MOUSE:
1156                         default:
1157                                 break;
1158                 }
1159         }
1160 }
1161
1162 const char *sca_state_name_get(Object *ob, short bit)
1163 {
1164         bController *cont;
1165         unsigned int mask;
1166
1167         mask = (1<<bit);
1168         cont = ob->controllers.first;
1169         while (cont) {
1170                 if (cont->state_mask & mask) {
1171                         return cont->name;
1172                 }
1173                 cont = cont->next;
1174         }
1175         return NULL;
1176 }
1177