Merge branch 'master' into blender2.8
[blender.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, const int UNUSED(flag))
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, const ListBase *lbo, const int flag)
91 {
92         bSensor *sens, *sensn;
93         
94         lbn->first= lbn->last= NULL;
95         sens= lbo->first;
96         while (sens) {
97                 sensn= copy_sensor(sens, flag);
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, const int UNUSED(flag))
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, const ListBase *lbo, const int flag)
255 {
256         bController *cont, *contn;
257         
258         lbn->first= lbn->last= NULL;
259         cont= lbo->first;
260         while (cont) {
261                 contn= copy_controller(cont, flag);
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, const int flag)
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 ((flag & LIB_ID_CREATE_NO_USER_REFCOUNT) == 0) {
378                                 id_us_plus((ID *)aa->act);
379                         }
380                         break;
381                 }
382                 case ACT_SOUND:
383                 {
384                         bSoundActuator *sa = (bSoundActuator *)act->data;
385                         if ((flag & LIB_ID_CREATE_NO_USER_REFCOUNT) == 0) {
386                                 id_us_plus((ID *)sa->sound);
387                         }
388                         break;
389                 }
390         }
391         return actn;
392 }
393
394 void copy_actuators(ListBase *lbn, const ListBase *lbo, const int flag)
395 {
396         bActuator *act, *actn;
397         
398         lbn->first= lbn->last= NULL;
399         act= lbo->first;
400         while (act) {
401                 actn= copy_actuator(act, flag);
402                 BLI_addtail(lbn, actn);
403                 act= act->next;
404         }
405 }
406
407 void init_actuator(bActuator *act)
408 {
409         /* also use when actuator changes type */
410         bCameraActuator *ca;
411         bObjectActuator *oa;
412         bRandomActuator *ra;
413         bSoundActuator *sa;
414         bSteeringActuator *sta;
415         bArmatureActuator *arma;
416         bMouseActuator *ma;
417         bEditObjectActuator *eoa;
418         
419         if (act->data) MEM_freeN(act->data);
420         act->data= NULL;
421         
422         switch (act->type) {
423         case ACT_ACTION:
424         case ACT_SHAPEACTION:
425                 act->data= MEM_callocN(sizeof(bActionActuator), "actionact");
426                 break;
427         case ACT_SOUND:
428                 sa = act->data= MEM_callocN(sizeof(bSoundActuator), "soundact");
429                 sa->volume = 1.0f;
430                 sa->sound3D.rolloff_factor = 1.0f;
431                 sa->sound3D.reference_distance = 1.0f;
432                 sa->sound3D.max_gain = 1.0f;
433                 sa->sound3D.cone_inner_angle = DEG2RADF(360.0f);
434                 sa->sound3D.cone_outer_angle = DEG2RADF(360.0f);
435                 sa->sound3D.max_distance = FLT_MAX;
436                 break;
437         case ACT_OBJECT:
438                 act->data= MEM_callocN(sizeof(bObjectActuator), "objectact");
439                 oa= act->data;
440                 oa->flag= 15;
441                 break;
442         case ACT_PROPERTY:
443                 act->data= MEM_callocN(sizeof(bPropertyActuator), "propact");
444                 break;
445         case ACT_CAMERA:
446                 act->data= MEM_callocN(sizeof(bCameraActuator), "camact");
447                 ca = act->data;
448                 ca->axis = OB_POSX;
449                 ca->damping = 1.0/32.0;
450                 break;
451         case ACT_EDIT_OBJECT:
452                 act->data= MEM_callocN(sizeof(bEditObjectActuator), "editobact");
453                 eoa = act->data;
454                 eoa->upflag= ACT_TRACK_UP_Z;
455                 eoa->trackflag= ACT_TRACK_TRAXIS_Y;
456                 break;
457         case ACT_CONSTRAINT:
458                 act->data= MEM_callocN(sizeof(bConstraintActuator), "cons act");
459                 break;
460         case ACT_SCENE:
461                 act->data= MEM_callocN(sizeof(bSceneActuator), "scene act");
462                 break;
463         case ACT_GROUP:
464                 act->data= MEM_callocN(sizeof(bGroupActuator), "group act");
465                 break;
466         case ACT_RANDOM:
467                 act->data= MEM_callocN(sizeof(bRandomActuator), "random act");
468                 ra=act->data;
469                 ra->float_arg_1 = 0.1f;
470                 break;
471         case ACT_MESSAGE:
472                 act->data= MEM_callocN(sizeof(bMessageActuator), "message act");
473                 break;
474         case ACT_GAME:
475                 act->data= MEM_callocN(sizeof(bGameActuator), "game act");
476                 break;
477         case ACT_VISIBILITY:
478                 act->data= MEM_callocN(sizeof(bVisibilityActuator), "visibility act");
479                 break;
480         case ACT_2DFILTER:
481                 act->data = MEM_callocN(sizeof( bTwoDFilterActuator ), "2d filter act");
482                 break;
483         case ACT_PARENT:
484                 act->data = MEM_callocN(sizeof( bParentActuator ), "parent act");
485                 break;
486         case ACT_STATE:
487                 act->data = MEM_callocN(sizeof( bStateActuator ), "state act");
488                 break;
489         case ACT_ARMATURE:
490                 act->data = MEM_callocN(sizeof( bArmatureActuator ), "armature act");
491                 arma = act->data;
492                 arma->influence = 1.f;
493                 break;
494         case ACT_STEERING:
495                 act->data = MEM_callocN(sizeof( bSteeringActuator), "steering act");
496                 sta = act->data;
497                 sta->acceleration = 3.f;
498                 sta->turnspeed = 120.f;
499                 sta->dist = 1.f;
500                 sta->velocity= 3.f;
501                 sta->flag = ACT_STEERING_AUTOMATICFACING | ACT_STEERING_LOCKZVEL;
502                 sta->facingaxis = 1;
503                 break;
504         case ACT_MOUSE:
505                 ma = act->data = MEM_callocN(sizeof( bMouseActuator ), "mouse act");
506                 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;
507                 ma->sensitivity[0] = ma->sensitivity[1] = 2.f;
508                 ma->object_axis[0] = ACT_MOUSE_OBJECT_AXIS_Z;
509                 ma->object_axis[1] = ACT_MOUSE_OBJECT_AXIS_X;
510                 ma->limit_y[0] = DEG2RADF(-90.0f);
511                 ma->limit_y[1] = DEG2RADF(90.0f);
512                 break;
513         default:
514                 ; /* this is very severe... I cannot make any memory for this        */
515                 /* logic brick...                                                    */
516         }
517 }
518
519 bActuator *new_actuator(int type)
520 {
521         bActuator *act;
522
523         act= MEM_callocN(sizeof(bActuator), "Actuator");
524         act->type= type;
525         act->flag= ACT_SHOW;
526         
527         init_actuator(act);
528         
529         strcpy(act->name, "act");
530 // XXX  make_unique_prop_names(act->name);
531         
532         return act;
533 }
534
535 /* ******************** GENERAL ******************* */
536 void clear_sca_new_poins_ob(Object *ob)
537 {
538         bSensor *sens;
539         bController *cont;
540         bActuator *act;
541         
542         sens= ob->sensors.first;
543         while (sens) {
544                 sens->flag &= ~SENS_NEW;
545                 sens= sens->next;
546         }
547         cont= ob->controllers.first;
548         while (cont) {
549                 cont->mynew= NULL;
550                 cont->flag &= ~CONT_NEW;
551                 cont= cont->next;
552         }
553         act= ob->actuators.first;
554         while (act) {
555                 act->mynew= NULL;
556                 act->flag &= ~ACT_NEW;
557                 act= act->next;
558         }
559 }
560
561 void clear_sca_new_poins(void)
562 {
563         Object *ob;
564         
565         ob= G.main->object.first;
566         while (ob) {
567                 clear_sca_new_poins_ob(ob);
568                 ob= ob->id.next;
569         }
570 }
571
572 void set_sca_new_poins_ob(Object *ob)
573 {
574         bSensor *sens;
575         bController *cont;
576         bActuator *act;
577         int a;
578         
579         sens= ob->sensors.first;
580         while (sens) {
581                 if (sens->flag & SENS_NEW) {
582                         for (a=0; a<sens->totlinks; a++) {
583                                 if (sens->links[a] && sens->links[a]->mynew)
584                                         sens->links[a] = sens->links[a]->mynew;
585                         }
586                 }
587                 sens= sens->next;
588         }
589
590         cont= ob->controllers.first;
591         while (cont) {
592                 if (cont->flag & CONT_NEW) {
593                         for (a=0; a<cont->totlinks; a++) {
594                                 if ( cont->links[a] && cont->links[a]->mynew)
595                                         cont->links[a] = cont->links[a]->mynew;
596                         }
597                 }
598                 cont= cont->next;
599         }
600         
601         
602         act= ob->actuators.first;
603         while (act) {
604                 if (act->flag & ACT_NEW) {
605                         if (act->type==ACT_EDIT_OBJECT) {
606                                 bEditObjectActuator *eoa= act->data;
607                                 ID_NEW_REMAP(eoa->ob);
608                         }
609                         else if (act->type==ACT_SCENE) {
610                                 bSceneActuator *sca= act->data;
611                                 ID_NEW_REMAP(sca->camera);
612                         }
613                         else if (act->type==ACT_CAMERA) {
614                                 bCameraActuator *ca= act->data;
615                                 ID_NEW_REMAP(ca->ob);
616                         }
617                         else if (act->type==ACT_OBJECT) {
618                                 bObjectActuator *oa= act->data;
619                                 ID_NEW_REMAP(oa->reference);
620                         }
621                         else if (act->type==ACT_MESSAGE) {
622                                 bMessageActuator *ma= act->data;
623                                 ID_NEW_REMAP(ma->toObject);
624                         }
625                         else if (act->type==ACT_PARENT) {
626                                 bParentActuator *para = act->data;
627                                 ID_NEW_REMAP(para->ob);
628                         }
629                         else if (act->type==ACT_ARMATURE) {
630                                 bArmatureActuator *aa = act->data;
631                                 ID_NEW_REMAP(aa->target);
632                                 ID_NEW_REMAP(aa->subtarget);
633                         }
634                         else if (act->type==ACT_PROPERTY) {
635                                 bPropertyActuator *pa= act->data;
636                                 ID_NEW_REMAP(pa->ob);
637                         }
638                         else if (act->type==ACT_STEERING) {
639                                 bSteeringActuator *sta = act->data;
640                                 ID_NEW_REMAP(sta->navmesh);
641                                 ID_NEW_REMAP(sta->target);
642                         }
643                 }
644                 act= act->next;
645         }
646 }
647
648
649 void set_sca_new_poins(void)
650 {
651         Object *ob;
652         
653         ob= G.main->object.first;
654         while (ob) {
655                 set_sca_new_poins_ob(ob);
656                 ob= ob->id.next;
657         }
658 }
659
660 /**
661  * Try to remap logic links to new object... Very, *very* weak.
662  */
663 /* XXX Logick bricks... I don't have words to say what I think about this behavior.
664  *     They have silent hidden ugly inter-objects dependencies (a sensor can link into any other
665  *     object's controllers, and same between controllers and actuators, without *any* explicit reference
666  *     to data-block involved).
667  *     This is bad, bad, bad!!!
668  *     ...and forces us to add yet another very ugly hack to get remapping with logic bricks working. */
669 void BKE_sca_logic_links_remap(Main *bmain, Object *ob_old, Object *ob_new)
670 {
671         if (ob_new == NULL || (ob_old->controllers.first == NULL && ob_old->actuators.first == NULL)) {
672                 /* Nothing to do here... */
673                 return;
674         }
675
676         GHash *controllers_map = ob_old->controllers.first ?
677                                      BLI_ghash_ptr_new_ex(__func__, BLI_listbase_count(&ob_old->controllers)) : NULL;
678         GHash *actuators_map = ob_old->actuators.first ?
679                                    BLI_ghash_ptr_new_ex(__func__, BLI_listbase_count(&ob_old->actuators)) : NULL;
680
681         /* We try to remap old controllers/actuators to new ones - in a very basic way. */
682         for (bController *cont_old = ob_old->controllers.first, *cont_new = ob_new->controllers.first;
683              cont_old;
684              cont_old = cont_old->next)
685         {
686                 bController *cont_new2 = cont_new;
687
688                 if (cont_old->mynew != NULL) {
689                         cont_new2 = cont_old->mynew;
690                         if (!(cont_new2 == cont_new || BLI_findindex(&ob_new->controllers, cont_new2) >= 0)) {
691                                 cont_new2 = NULL;
692                         }
693                 }
694                 else if (cont_new && cont_old->type != cont_new->type) {
695                         cont_new2 = NULL;
696                 }
697
698                 BLI_ghash_insert(controllers_map, cont_old, cont_new2);
699
700                 if (cont_new) {
701                         cont_new = cont_new->next;
702                 }
703         }
704
705         for (bActuator *act_old = ob_old->actuators.first, *act_new = ob_new->actuators.first;
706              act_old;
707              act_old = act_old->next)
708         {
709                 bActuator *act_new2 = act_new;
710
711                 if (act_old->mynew != NULL) {
712                         act_new2 = act_old->mynew;
713                         if (!(act_new2 == act_new || BLI_findindex(&ob_new->actuators, act_new2) >= 0)) {
714                                 act_new2 = NULL;
715                         }
716                 }
717                 else if (act_new && act_old->type != act_new->type) {
718                         act_new2 = NULL;
719                 }
720
721                 BLI_ghash_insert(actuators_map, act_old, act_new2);
722
723                 if (act_new) {
724                         act_new = act_new->next;
725                 }
726         }
727
728         for (Object *ob = bmain->object.first; ob; ob = ob->id.next) {
729                 if (controllers_map != NULL) {
730                         for (bSensor *sens = ob->sensors.first; sens; sens = sens->next) {
731                                 for (int a = 0; a < sens->totlinks; a++) {
732                                         if (sens->links[a]) {
733                                                 bController *old_link = sens->links[a];
734                                                 bController **new_link_p = (bController **)BLI_ghash_lookup_p(controllers_map, old_link);
735
736                                                 if (new_link_p == NULL) {
737                                                         /* old_link is *not* in map's keys (i.e. not to any ob_old->controllers),
738                                                          * which means we ignore it totally here. */
739                                                 }
740                                                 else if (*new_link_p == NULL) {
741                                                         unlink_logicbricks((void **)&old_link, (void ***)&(sens->links), &sens->totlinks);
742                                                         a--;
743                                                 }
744                                                 else {
745                                                         sens->links[a] = *new_link_p;
746                                                 }
747                                         }
748                                 }
749                         }
750                 }
751
752                 if (actuators_map != NULL) {
753                         for (bController *cont = ob->controllers.first; cont; cont = cont->next) {
754                                 for (int a = 0; a < cont->totlinks; a++) {
755                                         if (cont->links[a]) {
756                                                 bActuator *old_link = cont->links[a];
757                                                 bActuator **new_link_p = (bActuator **)BLI_ghash_lookup_p(actuators_map, old_link);
758
759                                                 if (new_link_p == NULL) {
760                                                         /* old_link is *not* in map's keys (i.e. not to any ob_old->actuators),
761                                                          * which means we ignore it totally here. */
762                                                 }
763                                                 else if (*new_link_p == NULL) {
764                                                         unlink_logicbricks((void **)&old_link, (void ***)&(cont->links), &cont->totlinks);
765                                                         a--;
766                                                 }
767                                                 else {
768                                                         cont->links[a] = *new_link_p;
769                                                 }
770                                         }
771                                 }
772                         }
773                 }
774         }
775
776         if (controllers_map) {
777                 BLI_ghash_free(controllers_map, NULL, NULL);
778         }
779         if (actuators_map) {
780                 BLI_ghash_free(actuators_map, NULL, NULL);
781         }
782 }
783
784 /**
785  * Handle the copying of logic data into a new object, including internal logic links update.
786  * External links (links between logic bricks of different objects) must be handled separately.
787  */
788 void BKE_sca_logic_copy(Object *ob_new, const Object *ob, const int flag)
789 {
790         copy_sensors(&ob_new->sensors, &ob->sensors, flag);
791         copy_controllers(&ob_new->controllers, &ob->controllers, flag);
792         copy_actuators(&ob_new->actuators, &ob->actuators, flag);
793
794         for (bSensor *sens = ob_new->sensors.first; sens; sens = sens->next) {
795                 if (sens->flag & SENS_NEW) {
796                         for (int a = 0; a < sens->totlinks; a++) {
797                                 if (sens->links[a] && sens->links[a]->mynew) {
798                                         sens->links[a] = sens->links[a]->mynew;
799                                 }
800                         }
801                 }
802         }
803
804         for (bController *cont = ob_new->controllers.first; cont; cont = cont->next) {
805                 if (cont->flag & CONT_NEW) {
806                         for (int a = 0; a < cont->totlinks; a++) {
807                                 if (cont->links[a] && cont->links[a]->mynew) {
808                                         cont->links[a] = cont->links[a]->mynew;
809                                 }
810                         }
811                 }
812         }
813 }
814
815 /* ******************** INTERFACE ******************* */
816 void sca_move_sensor(bSensor *sens_to_move, Object *ob, int move_up)
817 {
818         bSensor *sens, *tmp;
819
820         int val;
821         val = move_up ? 1 : 2;
822
823         /* make sure this sensor belongs to this object */
824         sens= ob->sensors.first;
825         while (sens) {
826                 if (sens == sens_to_move) break;
827                 sens= sens->next;
828         }
829         if (!sens) return;
830
831         /* move up */
832         if (val == 1 && sens->prev) {
833                 for (tmp=sens->prev; tmp; tmp=tmp->prev) {
834                         if (tmp->flag & SENS_VISIBLE)
835                                 break;
836                 }
837                 if (tmp) {
838                         BLI_remlink(&ob->sensors, sens);
839                         BLI_insertlinkbefore(&ob->sensors, tmp, sens);
840                 }
841         }
842         /* move down */
843         else if (val == 2 && sens->next) {
844                 for (tmp=sens->next; tmp; tmp=tmp->next) {
845                         if (tmp->flag & SENS_VISIBLE)
846                                 break;
847                 }
848                 if (tmp) {
849                         BLI_remlink(&ob->sensors, sens);
850                         BLI_insertlinkafter(&ob->sensors, tmp, sens);
851                 }
852         }
853 }
854
855 void sca_move_controller(bController *cont_to_move, Object *ob, int move_up)
856 {
857         bController *cont, *tmp;
858
859         int val;
860         val = move_up ? 1 : 2;
861
862         /* make sure this controller belongs to this object */
863         cont= ob->controllers.first;
864         while (cont) {
865                 if (cont == cont_to_move) break;
866                 cont= cont->next;
867         }
868         if (!cont) return;
869
870         /* move up */
871         if (val == 1 && cont->prev) {
872                 /* locate the controller that has the same state mask but is earlier in the list */
873                 tmp = cont->prev;
874                 while (tmp) {
875                         if (tmp->state_mask & cont->state_mask) 
876                                 break;
877                         tmp = tmp->prev;
878                 }
879                 if (tmp) {
880                         BLI_remlink(&ob->controllers, cont);
881                         BLI_insertlinkbefore(&ob->controllers, tmp, cont);
882                 }
883         }
884
885         /* move down */
886         else if (val == 2 && cont->next) {
887                 tmp = cont->next;
888                 while (tmp) {
889                         if (tmp->state_mask & cont->state_mask) 
890                                 break;
891                         tmp = tmp->next;
892                 }
893                 BLI_remlink(&ob->controllers, cont);
894                 BLI_insertlinkafter(&ob->controllers, tmp, cont);
895         }
896 }
897
898 void sca_move_actuator(bActuator *act_to_move, Object *ob, int move_up)
899 {
900         bActuator *act, *tmp;
901         int val;
902
903         val = move_up ? 1 : 2;
904
905         /* make sure this actuator belongs to this object */
906         act= ob->actuators.first;
907         while (act) {
908                 if (act == act_to_move) break;
909                 act= act->next;
910         }
911         if (!act) return;
912
913         /* move up */
914         if (val == 1 && act->prev) {
915                 /* locate the first visible actuators before this one */
916                 for (tmp = act->prev; tmp; tmp=tmp->prev) {
917                         if (tmp->flag & ACT_VISIBLE)
918                                 break;
919                 }
920                 if (tmp) {
921                         BLI_remlink(&ob->actuators, act);
922                         BLI_insertlinkbefore(&ob->actuators, tmp, act);
923                 }
924         }
925         /* move down */
926         else if (val == 2 && act->next) {
927                 /* locate the first visible actuators after this one */
928                 for (tmp=act->next; tmp; tmp=tmp->next) {
929                         if (tmp->flag & ACT_VISIBLE)
930                                 break;
931                 }
932                 if (tmp) {
933                         BLI_remlink(&ob->actuators, act);
934                         BLI_insertlinkafter(&ob->actuators, tmp, act);
935                 }
936         }
937 }
938
939 void link_logicbricks(void **poin, void ***ppoin, short *tot, short size)
940 {
941         void **old_links= NULL;
942         
943         int ibrick;
944
945         /* check if the bricks are already linked */
946         for (ibrick=0; ibrick < *tot; ibrick++) {
947                 if ((*ppoin)[ibrick] == *poin)
948                         return;
949         }
950
951         if (*ppoin) {
952                 old_links= *ppoin;
953
954                 (*tot) ++;
955                 *ppoin = MEM_callocN((*tot)*size, "new link");
956         
957                 for (ibrick=0; ibrick < *(tot) - 1; ibrick++) {
958                         (*ppoin)[ibrick] = old_links[ibrick];
959                 }
960                 (*ppoin)[ibrick] = *poin;
961
962                 if (old_links) MEM_freeN(old_links);
963         }
964         else {
965                 (*tot) = 1;
966                 *ppoin = MEM_callocN((*tot)*size, "new link");
967                 (*ppoin)[0] = *poin;
968         }
969 }
970
971 void unlink_logicbricks(void **poin, void ***ppoin, short *tot)
972 {
973         int ibrick, removed;
974
975         removed= 0;
976         for (ibrick=0; ibrick < *tot; ibrick++) {
977                 if (removed) (*ppoin)[ibrick - removed] = (*ppoin)[ibrick];
978                 else if ((*ppoin)[ibrick] == *poin) removed = 1;
979         }
980
981         if (removed) {
982                 (*tot) --;
983
984                 if (*tot == 0) {
985                         MEM_freeN(*ppoin);
986                         (*ppoin)= NULL;
987                 }
988                 return;
989         }
990 }
991
992 void BKE_sca_sensors_id_loop(ListBase *senslist, SCASensorIDFunc func, void *userdata)
993 {
994         bSensor *sensor;
995
996         for (sensor = senslist->first; sensor; sensor = sensor->next) {
997                 func(sensor, (ID **)&sensor->ob, userdata, IDWALK_CB_NOP);
998
999                 switch (sensor->type) {
1000                         case SENS_TOUCH:  /* DEPRECATED */
1001                         {
1002                                 bTouchSensor *ts = sensor->data;
1003                                 func(sensor, (ID **)&ts->ma, userdata, IDWALK_CB_NOP);
1004                                 break;
1005                         }
1006                         case SENS_MESSAGE:
1007                         {
1008                                 bMessageSensor *ms = sensor->data;
1009                                 func(sensor, (ID **)&ms->fromObject, userdata, IDWALK_CB_NOP);
1010                                 break;
1011                         }
1012                         case SENS_ALWAYS:
1013                         case SENS_NEAR:
1014                         case SENS_KEYBOARD:
1015                         case SENS_PROPERTY:
1016                         case SENS_MOUSE:
1017                         case SENS_COLLISION:
1018                         case SENS_RADAR:
1019                         case SENS_RANDOM:
1020                         case SENS_RAY:
1021                         case SENS_JOYSTICK:
1022                         case SENS_ACTUATOR:
1023                         case SENS_DELAY:
1024                         case SENS_ARMATURE:
1025                         default:
1026                                 break;
1027                 }
1028         }
1029 }
1030
1031 void BKE_sca_controllers_id_loop(ListBase *contlist, SCAControllerIDFunc func, void *userdata)
1032 {
1033         bController *controller;
1034
1035         for (controller = contlist->first; controller; controller = controller->next) {
1036                 switch (controller->type) {
1037                         case CONT_PYTHON:
1038                         {
1039                                 bPythonCont *pc = controller->data;
1040                                 func(controller, (ID **)&pc->text, userdata, IDWALK_CB_NOP);
1041                                 break;
1042                         }
1043                         case CONT_LOGIC_AND:
1044                         case CONT_LOGIC_OR:
1045                         case CONT_EXPRESSION:
1046                         case CONT_LOGIC_NAND:
1047                         case CONT_LOGIC_NOR:
1048                         case CONT_LOGIC_XOR:
1049                         case CONT_LOGIC_XNOR:
1050                         default:
1051                                 break;
1052                 }
1053         }
1054 }
1055
1056 void BKE_sca_actuators_id_loop(ListBase *actlist, SCAActuatorIDFunc func, void *userdata)
1057 {
1058         bActuator *actuator;
1059
1060         for (actuator = actlist->first; actuator; actuator = actuator->next) {
1061                 func(actuator, (ID **)&actuator->ob, userdata, IDWALK_CB_NOP);
1062
1063                 switch (actuator->type) {
1064                         case ACT_ADD_OBJECT:  /* DEPRECATED */
1065                         {
1066                                 bAddObjectActuator *aoa = actuator->data;
1067                                 func(actuator, (ID **)&aoa->ob, userdata, IDWALK_CB_NOP);
1068                                 break;
1069                         }
1070                         case ACT_ACTION:
1071                         {
1072                                 bActionActuator *aa = actuator->data;
1073                                 func(actuator, (ID **)&aa->act, userdata, IDWALK_CB_NOP);
1074                                 break;
1075                         }
1076                         case ACT_SOUND:
1077                         {
1078                                 bSoundActuator *sa = actuator->data;
1079                                 func(actuator, (ID **)&sa->sound, userdata, IDWALK_CB_NOP);
1080                                 break;
1081                         }
1082                         case ACT_EDIT_OBJECT:
1083                         {
1084                                 bEditObjectActuator *eoa = actuator->data;
1085                                 func(actuator, (ID **)&eoa->ob, userdata, IDWALK_CB_NOP);
1086                                 func(actuator, (ID **)&eoa->me, userdata, IDWALK_CB_NOP);
1087                                 break;
1088                         }
1089                         case ACT_SCENE:
1090                         {
1091                                 bSceneActuator *sa = actuator->data;
1092                                 func(actuator, (ID **)&sa->scene, userdata, IDWALK_CB_NOP);
1093                                 func(actuator, (ID **)&sa->camera, userdata, IDWALK_CB_NOP);
1094                                 break;
1095                         }
1096                         case ACT_PROPERTY:
1097                         {
1098                                 bPropertyActuator *pa = actuator->data;
1099                                 func(actuator, (ID **)&pa->ob, userdata, IDWALK_CB_NOP);
1100                                 break;
1101                         }
1102                         case ACT_OBJECT:
1103                         {
1104                                 bObjectActuator *oa = actuator->data;
1105                                 func(actuator, (ID **)&oa->reference, userdata, IDWALK_CB_NOP);
1106                                 break;
1107                         }
1108                         case ACT_CAMERA:
1109                         {
1110                                 bCameraActuator *ca = actuator->data;
1111                                 func(actuator, (ID **)&ca->ob, userdata, IDWALK_CB_NOP);
1112                                 break;
1113                         }
1114                         case ACT_MESSAGE:
1115                         {
1116                                 bMessageActuator *ma = actuator->data;
1117                                 func(actuator, (ID **)&ma->toObject, userdata, IDWALK_CB_NOP);
1118                                 break;
1119                         }
1120                         case ACT_2DFILTER:
1121                         {
1122                                 bTwoDFilterActuator *tdfa = actuator->data;
1123                                 func(actuator, (ID **)&tdfa->text, userdata, IDWALK_CB_NOP);
1124                                 break;
1125                         }
1126                         case ACT_PARENT:
1127                         {
1128                                 bParentActuator *pa = actuator->data;
1129                                 func(actuator, (ID **)&pa->ob, userdata, IDWALK_CB_NOP);
1130                                 break;
1131                         }
1132                         case ACT_ARMATURE:
1133                         {
1134                                 bArmatureActuator *aa = actuator->data;
1135                                 func(actuator, (ID **)&aa->target, userdata, IDWALK_CB_NOP);
1136                                 func(actuator, (ID **)&aa->subtarget, userdata, IDWALK_CB_NOP);
1137                                 break;
1138                         }
1139                         case ACT_STEERING:
1140                         {
1141                                 bSteeringActuator *sa = actuator->data;
1142                                 func(actuator, (ID **)&sa->target, userdata, IDWALK_CB_NOP);
1143                                 func(actuator, (ID **)&sa->navmesh, userdata, IDWALK_CB_NOP);
1144                                 break;
1145                         }
1146                         /* Note: some types seems to be non-implemented? ACT_LAMP, ACT_MATERIAL... */
1147                         case ACT_LAMP:
1148                         case ACT_MATERIAL:
1149                         case ACT_END_OBJECT:  /* DEPRECATED */
1150                         case ACT_CONSTRAINT:
1151                         case ACT_GROUP:
1152                         case ACT_RANDOM:
1153                         case ACT_GAME:
1154                         case ACT_VISIBILITY:
1155                         case ACT_SHAPEACTION:
1156                         case ACT_STATE:
1157                         case ACT_MOUSE:
1158                         default:
1159                                 break;
1160                 }
1161         }
1162 }
1163
1164 const char *sca_state_name_get(Object *ob, short bit)
1165 {
1166         bController *cont;
1167         unsigned int mask;
1168
1169         mask = (1<<bit);
1170         cont = ob->controllers.first;
1171         while (cont) {
1172                 if (cont->state_mask & mask) {
1173                         return cont->name;
1174                 }
1175                 cont = cont->next;
1176         }
1177         return NULL;
1178 }
1179