removed unused includes, except for physics and particle related files
[blender-staging.git] / source / blender / blenkernel / intern / sca.c
1 /**
2  * $Id$
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19  *
20  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
21  * All rights reserved.
22  *
23  * The Original Code is: all of this file.
24  *
25  * Contributor(s): none yet.
26  *
27  * ***** END GPL LICENSE BLOCK *****
28  * these all are linked to objects (listbase)
29  * all data is 'direct data', not Blender lib data.
30  */
31
32 #ifdef HAVE_CONFIG_H
33 #include <config.h>
34 #endif
35
36 #include <stdio.h>
37 #include <string.h>
38 #include <float.h>
39
40 #include "MEM_guardedalloc.h"
41
42 #include "DNA_controller_types.h"
43 #include "DNA_sensor_types.h"
44 #include "DNA_actuator_types.h"
45 #include "DNA_object_types.h"
46
47 #include "BLI_blenlib.h"
48 #include "BKE_utildefines.h"
49 #include "BKE_global.h"
50 #include "BKE_main.h"
51
52 /* ******************* SENSORS ************************ */
53
54 void free_sensor(bSensor *sens)
55 {
56         if(sens->links) MEM_freeN(sens->links);
57         if(sens->data) MEM_freeN(sens->data);
58         MEM_freeN(sens);
59         
60 }
61
62 void free_sensors(ListBase *lb)
63 {
64         bSensor *sens;
65         
66         while((sens= lb->first)) {
67                 BLI_remlink(lb, sens);
68                 free_sensor(sens);
69         }
70 }
71
72 bSensor *copy_sensor(bSensor *sens)
73 {
74         bSensor *sensn;
75         
76         sensn= MEM_dupallocN(sens);
77         sensn->flag |= SENS_NEW;
78         if(sens->data) {
79                 sensn->data= MEM_dupallocN(sens->data);
80         }
81
82         if(sens->links) sensn->links= MEM_dupallocN(sens->links);
83         
84         return sensn;
85 }
86
87 void copy_sensors(ListBase *lbn, ListBase *lbo)
88 {
89         bSensor *sens, *sensn;
90         
91         lbn->first= lbn->last= 0;
92         sens= lbo->first;
93         while(sens) {
94                 sensn= copy_sensor(sens);
95                 BLI_addtail(lbn, sensn);
96                 sens= sens->next;
97         }
98 }
99
100 void init_sensor(bSensor *sens)
101 {
102         /* also use when sensor changes type */
103         bNearSensor *ns;
104         bMouseSensor *ms;
105         
106         if(sens->data) MEM_freeN(sens->data);
107         sens->data= NULL;
108         sens->pulse = 0;
109         
110         switch(sens->type) {
111         case SENS_ALWAYS:
112                 sens->pulse = 0;
113                 break;
114         case SENS_TOUCH:
115                 sens->data= MEM_callocN(sizeof(bTouchSensor), "touchsens");
116                 break;
117         case SENS_NEAR:
118                 ns=sens->data= MEM_callocN(sizeof(bNearSensor), "nearsens");
119                 ns->dist= 1.0;
120                 ns->resetdist= 2.0;
121                 break;
122         case SENS_KEYBOARD:
123                 sens->data= MEM_callocN(sizeof(bKeyboardSensor), "keysens");
124                 break;
125         case SENS_PROPERTY:
126                 sens->data= MEM_callocN(sizeof(bPropertySensor), "propsens");
127                 break;
128         case SENS_ARMATURE:
129                 sens->data= MEM_callocN(sizeof(bArmatureSensor), "armsens");
130                 break;
131         case SENS_ACTUATOR:
132                 sens->data= MEM_callocN(sizeof(bActuatorSensor), "actsens");
133                 break;
134         case SENS_DELAY:
135                 sens->data= MEM_callocN(sizeof(bDelaySensor), "delaysens");
136                 break;
137         case SENS_MOUSE:
138                 ms=sens->data= MEM_callocN(sizeof(bMouseSensor), "mousesens");
139                 ms->type= 1; // LEFTMOUSE workaround because Mouse Sensor types enum starts in 1
140                 break;
141         case SENS_COLLISION:
142                 sens->data= MEM_callocN(sizeof(bCollisionSensor), "colsens");
143                 break;
144         case SENS_RADAR:
145                 sens->data= MEM_callocN(sizeof(bRadarSensor), "radarsens");
146                 break;
147         case SENS_RANDOM:
148                 sens->data= MEM_callocN(sizeof(bRandomSensor), "randomsens");
149                 break;
150         case SENS_RAY:
151                 sens->data= MEM_callocN(sizeof(bRaySensor), "raysens");
152                 break;
153         case SENS_MESSAGE:
154                 sens->data= MEM_callocN(sizeof(bMessageSensor), "messagesens");
155                 break;
156         case SENS_JOYSTICK:
157                 sens->data= MEM_callocN(sizeof(bJoystickSensor), "joysticksens");
158                 break;
159         default:
160                 ; /* this is very severe... I cannot make any memory for this        */
161                 /* logic brick...                                                    */
162         }
163 }
164
165 bSensor *new_sensor(int type)
166 {
167         bSensor *sens;
168
169         sens= MEM_callocN(sizeof(bSensor), "Sensor");
170         sens->type= type;
171         sens->flag= SENS_SHOW;
172         
173         init_sensor(sens);
174         
175         strcpy(sens->name, "sensor");
176 // XXX  make_unique_prop_names(sens->name);
177         
178         return sens;
179 }
180
181 /* ******************* CONTROLLERS ************************ */
182
183 void unlink_controller(bController *cont)
184 {
185         bSensor *sens;
186         Object *ob;
187         int a, removed;
188         
189         /* check for controller pointers in sensors */
190         ob= G.main->object.first;
191         while(ob) {
192                 sens= ob->sensors.first;
193                 while(sens) {
194                         removed= 0;
195                         for(a=0; a<sens->totlinks; a++) {
196                                 if(removed) (sens->links)[a-1] = (sens->links)[a];
197                                 else if((sens->links)[a] == cont) removed= 1;
198                         }
199                         if(removed) {
200                                 sens->totlinks--;
201                                 
202                                 if(sens->totlinks==0) {
203                                         MEM_freeN(sens->links);
204                                         sens->links= NULL;
205                                 }
206                         }
207                         sens= sens->next;
208                 }
209                 ob= ob->id.next;
210         }
211 }
212
213 void unlink_controllers(ListBase *lb)
214 {
215         bController *cont;
216         
217         for (cont= lb->first; cont; cont= cont->next)
218                 unlink_controller(cont);        
219 }
220
221 void free_controller(bController *cont)
222 {
223         if(cont->links) MEM_freeN(cont->links);
224
225         /* the controller itself */
226         if(cont->data) MEM_freeN(cont->data);
227         MEM_freeN(cont);
228         
229 }
230
231 void free_controllers(ListBase *lb)
232 {
233         bController *cont;
234         
235         while((cont= lb->first)) {
236                 BLI_remlink(lb, cont);
237                 if(cont->slinks) MEM_freeN(cont->slinks);
238                 free_controller(cont);
239         }
240 }
241
242 bController *copy_controller(bController *cont)
243 {
244         bController *contn;
245         
246         cont->mynew=contn= MEM_dupallocN(cont);
247         contn->flag |= CONT_NEW;
248         if(cont->data) {
249                 contn->data= MEM_dupallocN(cont->data);
250         }
251
252         if(cont->links) contn->links= MEM_dupallocN(cont->links);
253         contn->slinks= NULL;
254         contn->totslinks= 0;
255         
256         return contn;
257 }
258
259 void copy_controllers(ListBase *lbn, ListBase *lbo)
260 {
261         bController *cont, *contn;
262         
263         lbn->first= lbn->last= 0;
264         cont= lbo->first;
265         while(cont) {
266                 contn= copy_controller(cont);
267                 BLI_addtail(lbn, contn);
268                 cont= cont->next;
269         }
270 }
271
272 void init_controller(bController *cont)
273 {
274         /* also use when controller changes type, leave actuators... */
275         
276         if(cont->data) MEM_freeN(cont->data);
277         cont->data= 0;
278         
279         switch(cont->type) {
280         case CONT_EXPRESSION:
281                 cont->data= MEM_callocN(sizeof(bExpressionCont), "expcont");
282                 break;
283         case CONT_PYTHON:
284                 cont->data= MEM_callocN(sizeof(bPythonCont), "pycont");
285                 break;
286         }
287 }
288
289 bController *new_controller(int type)
290 {
291         bController *cont;
292
293         cont= MEM_callocN(sizeof(bController), "Controller");
294         cont->type= type;
295         cont->flag= CONT_SHOW;
296
297         init_controller(cont);
298         
299         strcpy(cont->name, "cont");
300 // XXX  make_unique_prop_names(cont->name);
301         
302         return cont;
303 }
304
305 /* ******************* ACTUATORS ************************ */
306
307 void unlink_actuator(bActuator *act)
308 {
309         bController *cont;
310         Object *ob;
311         int a, removed;
312         
313         /* check for actuator pointers in controllers */
314         ob= G.main->object.first;
315         while(ob) {
316                 cont= ob->controllers.first;
317                 while(cont) {
318                         removed= 0;
319                         for(a=0; a<cont->totlinks; a++) {
320                                 if(removed) (cont->links)[a-1] = (cont->links)[a];
321                                 else if((cont->links)[a] == act) removed= 1;
322                         }
323                         if(removed) {
324                                 cont->totlinks--;
325                                 
326                                 if(cont->totlinks==0) {
327                                         MEM_freeN(cont->links);
328                                         cont->links= NULL;
329                                 }
330                         }
331                         cont= cont->next;
332                 }
333                 ob= ob->id.next;
334         }
335 }
336
337 void unlink_actuators(ListBase *lb)
338 {
339         bActuator *act;
340         
341         for (act= lb->first; act; act= act->next)
342                 unlink_actuator(act);
343 }
344
345 void free_actuator(bActuator *act)
346 {
347         if(act->data) MEM_freeN(act->data);
348         MEM_freeN(act);
349 }
350
351 void free_actuators(ListBase *lb)
352 {
353         bActuator *act;
354         
355         while((act= lb->first)) {
356                 BLI_remlink(lb, act);
357                 free_actuator(act);
358         }
359 }
360
361 bActuator *copy_actuator(bActuator *act)
362 {
363         bActuator *actn;
364         
365         act->mynew=actn= MEM_dupallocN(act);
366         actn->flag |= ACT_NEW;
367         if(act->data) {
368                 actn->data= MEM_dupallocN(act->data);
369         }
370         
371         return actn;
372 }
373
374 void copy_actuators(ListBase *lbn, ListBase *lbo)
375 {
376         bActuator *act, *actn;
377         
378         lbn->first= lbn->last= 0;
379         act= lbo->first;
380         while(act) {
381                 actn= copy_actuator(act);
382                 BLI_addtail(lbn, actn);
383                 act= act->next;
384         }
385 }
386
387 void init_actuator(bActuator *act)
388 {
389         /* also use when actuator changes type */
390         bObjectActuator *oa;
391         bSoundActuator *sa;
392         
393         if(act->data) MEM_freeN(act->data);
394         act->data= 0;
395         
396         switch(act->type) {
397         case ACT_ACTION:
398         case ACT_SHAPEACTION:
399                 act->data= MEM_callocN(sizeof(bActionActuator), "actionact");
400                 break;
401         case ACT_SOUND:
402                 sa = act->data= MEM_callocN(sizeof(bSoundActuator), "soundact");
403                 sa->volume = 1.0f;
404                 sa->sound3D.rolloff_factor = 1.0f;
405                 sa->sound3D.reference_distance = 1.0f;
406                 sa->sound3D.max_gain = 1.0f;
407                 sa->sound3D.cone_inner_angle = 360.0f;
408                 sa->sound3D.cone_outer_angle = 360.0f;
409                 sa->sound3D.max_distance = FLT_MAX;
410                 break;
411         case ACT_OBJECT:
412                 act->data= MEM_callocN(sizeof(bObjectActuator), "objectact");
413                 oa= act->data;
414                 oa->flag= 15;
415                 break;
416         case ACT_IPO:
417                 act->data= MEM_callocN(sizeof(bIpoActuator), "ipoact");
418                 break;
419         case ACT_PROPERTY:
420                 act->data= MEM_callocN(sizeof(bPropertyActuator), "propact");
421                 break;
422         case ACT_CAMERA:
423                 act->data= MEM_callocN(sizeof(bCameraActuator), "camact");
424                 break;
425         case ACT_EDIT_OBJECT:
426                 act->data= MEM_callocN(sizeof(bEditObjectActuator), "editobact");
427                 break;
428         case ACT_CONSTRAINT:
429                 act->data= MEM_callocN(sizeof(bConstraintActuator), "cons act");
430                 break;
431         case ACT_SCENE:
432                 act->data= MEM_callocN(sizeof(bSceneActuator), "scene act");
433                 break;
434         case ACT_GROUP:
435                 act->data= MEM_callocN(sizeof(bGroupActuator), "group act");
436                 break;
437         case ACT_RANDOM:
438                 act->data= MEM_callocN(sizeof(bRandomActuator), "random act");
439                 break;
440         case ACT_MESSAGE:
441                 act->data= MEM_callocN(sizeof(bMessageActuator), "message act");
442                 break;
443         case ACT_GAME:
444                 act->data= MEM_callocN(sizeof(bGameActuator), "game act");
445                 break;
446         case ACT_VISIBILITY:
447                 act->data= MEM_callocN(sizeof(bVisibilityActuator), "visibility act");
448                 break;
449     case ACT_2DFILTER:
450         act->data = MEM_callocN(sizeof( bTwoDFilterActuator ), "2d filter act");
451         break;
452     case ACT_PARENT:
453         act->data = MEM_callocN(sizeof( bParentActuator ), "parent act");
454         break;
455         case ACT_STATE:
456         act->data = MEM_callocN(sizeof( bStateActuator ), "state act");
457         break;
458         case ACT_ARMATURE:
459         act->data = MEM_callocN(sizeof( bArmatureActuator ), "armature act");
460         break;
461         default:
462                 ; /* this is very severe... I cannot make any memory for this        */
463                 /* logic brick...                                                    */
464         }
465 }
466
467 bActuator *new_actuator(int type)
468 {
469         bActuator *act;
470
471         act= MEM_callocN(sizeof(bActuator), "Actuator");
472         act->type= type;
473         act->flag= ACT_SHOW;
474         
475         init_actuator(act);
476         
477         strcpy(act->name, "act");
478 // XXX  make_unique_prop_names(act->name);
479         
480         return act;
481 }
482
483 /* ******************** GENERAL ******************* */
484
485 void clear_sca_new_poins_ob(Object *ob)
486 {
487         bSensor *sens;
488         bController *cont;
489         bActuator *act;
490         
491         sens= ob->sensors.first;
492         while(sens) {
493                 sens->flag &= ~SENS_NEW;
494                 sens= sens->next;
495         }
496         cont= ob->controllers.first;
497         while(cont) {
498                 cont->mynew= NULL;
499                 cont->flag &= ~CONT_NEW;
500                 cont= cont->next;
501         }
502         act= ob->actuators.first;
503         while(act) {
504                 act->mynew= NULL;
505                 act->flag &= ~ACT_NEW;
506                 act= act->next;
507         }
508 }
509
510 void clear_sca_new_poins()
511 {
512         Object *ob;
513         
514         ob= G.main->object.first;
515         while(ob) {
516                 clear_sca_new_poins_ob(ob);
517                 ob= ob->id.next;        
518         }
519 }
520
521 void set_sca_new_poins_ob(Object *ob)
522 {
523         bSensor *sens;
524         bController *cont;
525         bActuator *act;
526         int a;
527         
528         sens= ob->sensors.first;
529         while(sens) {
530                 if(sens->flag & SENS_NEW) {
531                         for(a=0; a<sens->totlinks; a++) {
532                                 if(sens->links[a] && sens->links[a]->mynew)
533                                         sens->links[a]= sens->links[a]->mynew;
534                         }
535                 }
536                 sens= sens->next;
537         }
538
539         cont= ob->controllers.first;
540         while(cont) {
541                 if(cont->flag & CONT_NEW) {
542                         for(a=0; a<cont->totlinks; a++) {
543                                 if( cont->links[a] && cont->links[a]->mynew)
544                                         cont->links[a]= cont->links[a]->mynew;
545                         }
546                 }
547                 cont= cont->next;
548         }
549         
550         
551         act= ob->actuators.first;
552         while(act) {
553                 if(act->flag & ACT_NEW) {
554                         if(act->type==ACT_EDIT_OBJECT) {
555                                 bEditObjectActuator *eoa= act->data;
556                                 ID_NEW(eoa->ob);
557                         }
558                         else if(act->type==ACT_SCENE) {
559                                 bSceneActuator *sca= act->data;
560                                 ID_NEW(sca->camera);
561                         }
562                         else if(act->type==ACT_CAMERA) {
563                                 bCameraActuator *ca= act->data;
564                                 ID_NEW(ca->ob);
565                         }
566                         else if(act->type==ACT_OBJECT) {
567                                 bObjectActuator *oa= act->data;
568                                 ID_NEW(oa->reference);
569                         }
570                         else if(act->type==ACT_SCENE) {
571                                 bSceneActuator *sca= act->data;
572                                 ID_NEW(sca->camera);
573                         }
574                 }
575                 act= act->next;
576         }
577 }
578
579
580 void set_sca_new_poins()
581 {
582         Object *ob;
583         
584         ob= G.main->object.first;
585         while(ob) {
586                 set_sca_new_poins_ob(ob);
587                 ob= ob->id.next;        
588         }
589 }
590
591 void sca_remove_ob_poin(Object *obt, Object *ob)
592 {
593         bSensor *sens;
594         bMessageSensor *ms;
595         bActuator *act;
596         bCameraActuator *ca;
597         bObjectActuator *oa;
598         bSceneActuator *sa;
599         bEditObjectActuator *eoa;
600         bPropertyActuator *pa;
601         bMessageActuator *ma;
602         bParentActuator *para;
603         bArmatureActuator *aa;
604
605         sens= obt->sensors.first;
606         while(sens) {
607                 switch(sens->type) {
608                 case SENS_MESSAGE:
609                         ms= sens->data;
610                         if(ms->fromObject==ob) ms->fromObject= NULL;
611                 }
612                 sens= sens->next;
613         }
614
615         act= obt->actuators.first;
616         while(act) {
617                 switch(act->type) {
618                 case ACT_CAMERA:
619                         ca= act->data;
620                         if(ca->ob==ob) ca->ob= NULL;
621                         break;
622                 case ACT_OBJECT:
623                         oa= act->data;
624                         if(oa->reference==ob) oa->reference= NULL;
625                         break;
626                 case ACT_PROPERTY:
627                         pa= act->data;
628                         if(pa->ob==ob) pa->ob= NULL;
629                         break;
630                 case ACT_SCENE:
631                         sa= act->data;
632                         if(sa->camera==ob) sa->camera= NULL;
633                         break;
634                 case ACT_EDIT_OBJECT:
635                         eoa= act->data;
636                         if(eoa->ob==ob) eoa->ob= NULL;
637                         break;
638                 case ACT_MESSAGE:
639                         ma= act->data;
640                         if(ma->toObject==ob) ma->toObject= NULL;
641                         break;
642                 case ACT_PARENT:
643                         para = act->data;
644                         if (para->ob==ob) para->ob = NULL;
645                         break;
646                 case ACT_ARMATURE:
647                         aa = act->data;
648                         if (aa->target == ob) aa->target = NULL;
649                         if (aa->subtarget == ob) aa->subtarget = NULL;
650                         break;
651                 }
652                 act= act->next;
653         }       
654 }