Merge of itasc branch. Project files, scons and cmake should be working. Makefile...
[blender.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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, 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_text_types.h"
43 #include "DNA_controller_types.h"
44 #include "DNA_sensor_types.h"
45 #include "DNA_actuator_types.h"
46 #include "DNA_object_types.h"
47
48 #include "BLI_blenlib.h"
49 #include "BKE_utildefines.h"
50 #include "BKE_global.h"
51 #include "BKE_main.h"
52 #include "BKE_blender.h"
53 #include "BKE_sca.h"
54
55 /* ******************* SENSORS ************************ */
56
57 void free_sensor(bSensor *sens)
58 {
59         if(sens->links) MEM_freeN(sens->links);
60         if(sens->data) MEM_freeN(sens->data);
61         MEM_freeN(sens);
62         
63 }
64
65 void free_sensors(ListBase *lb)
66 {
67         bSensor *sens;
68         
69         while((sens= lb->first)) {
70                 BLI_remlink(lb, sens);
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= 0;
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         
109         if(sens->data) MEM_freeN(sens->data);
110         sens->data= NULL;
111         sens->pulse = 0;
112         
113         switch(sens->type) {
114         case SENS_ALWAYS:
115                 sens->pulse = 0;
116                 break;
117         case SENS_TOUCH:
118                 sens->data= MEM_callocN(sizeof(bTouchSensor), "touchsens");
119                 break;
120         case SENS_NEAR:
121                 ns=sens->data= MEM_callocN(sizeof(bNearSensor), "nearsens");
122                 ns->dist= 1.0;
123                 ns->resetdist= 2.0;
124                 break;
125         case SENS_KEYBOARD:
126                 sens->data= MEM_callocN(sizeof(bKeyboardSensor), "keysens");
127                 break;
128         case SENS_PROPERTY:
129                 sens->data= MEM_callocN(sizeof(bPropertySensor), "propsens");
130                 break;
131         case SENS_ARMATURE:
132                 sens->data= MEM_callocN(sizeof(bArmatureSensor), "armsens");
133                 break;
134         case SENS_ACTUATOR:
135                 sens->data= MEM_callocN(sizeof(bActuatorSensor), "actsens");
136                 break;
137         case SENS_DELAY:
138                 sens->data= MEM_callocN(sizeof(bDelaySensor), "delaysens");
139                 break;
140         case SENS_MOUSE:
141                 ms=sens->data= MEM_callocN(sizeof(bMouseSensor), "mousesens");
142                 //XXX ms->type= LEFTMOUSE;
143                 break;
144         case SENS_COLLISION:
145                 sens->data= MEM_callocN(sizeof(bCollisionSensor), "colsens");
146                 break;
147         case SENS_RADAR:
148                 sens->data= MEM_callocN(sizeof(bRadarSensor), "radarsens");
149                 break;
150         case SENS_RANDOM:
151                 sens->data= MEM_callocN(sizeof(bRandomSensor), "randomsens");
152                 break;
153         case SENS_RAY:
154                 sens->data= MEM_callocN(sizeof(bRaySensor), "raysens");
155                 break;
156         case SENS_MESSAGE:
157                 sens->data= MEM_callocN(sizeof(bMessageSensor), "messagesens");
158                 break;
159         case SENS_JOYSTICK:
160                 sens->data= MEM_callocN(sizeof(bJoystickSensor), "joysticksens");
161                 break;
162         default:
163                 ; /* this is very severe... I cannot make any memory for this        */
164                 /* logic brick...                                                    */
165         }
166 }
167
168 bSensor *new_sensor(int type)
169 {
170         bSensor *sens;
171
172         sens= MEM_callocN(sizeof(bSensor), "Sensor");
173         sens->type= type;
174         sens->flag= SENS_SHOW;
175         
176         init_sensor(sens);
177         
178         strcpy(sens->name, "sensor");
179 // XXX  make_unique_prop_names(sens->name);
180         
181         return sens;
182 }
183
184 /* ******************* CONTROLLERS ************************ */
185
186 void unlink_controller(bController *cont)
187 {
188         bSensor *sens;
189         Object *ob;
190         int a, removed;
191         
192         /* check for controller pointers in sensors */
193         ob= G.main->object.first;
194         while(ob) {
195                 sens= ob->sensors.first;
196                 while(sens) {
197                         removed= 0;
198                         for(a=0; a<sens->totlinks; a++) {
199                                 if(removed) (sens->links)[a-1] = (sens->links)[a];
200                                 else if((sens->links)[a] == cont) removed= 1;
201                         }
202                         if(removed) {
203                                 sens->totlinks--;
204                                 
205                                 if(sens->totlinks==0) {
206                                         MEM_freeN(sens->links);
207                                         sens->links= NULL;
208                                 }
209                         }
210                         sens= sens->next;
211                 }
212                 ob= ob->id.next;
213         }
214 }
215
216 void unlink_controllers(ListBase *lb)
217 {
218         bController *cont;
219         
220         for (cont= lb->first; cont; cont= cont->next)
221                 unlink_controller(cont);        
222 }
223
224 void free_controller(bController *cont)
225 {
226         if(cont->links) MEM_freeN(cont->links);
227
228         /* the controller itself */
229         if(cont->data) MEM_freeN(cont->data);
230         MEM_freeN(cont);
231         
232 }
233
234 void free_controllers(ListBase *lb)
235 {
236         bController *cont;
237         
238         while((cont= lb->first)) {
239                 BLI_remlink(lb, cont);
240                 if(cont->slinks) MEM_freeN(cont->slinks);
241                 free_controller(cont);
242         }
243 }
244
245 bController *copy_controller(bController *cont)
246 {
247         bController *contn;
248         
249         cont->mynew=contn= MEM_dupallocN(cont);
250         contn->flag |= CONT_NEW;
251         if(cont->data) {
252                 contn->data= MEM_dupallocN(cont->data);
253         }
254
255         if(cont->links) contn->links= MEM_dupallocN(cont->links);
256         contn->slinks= NULL;
257         contn->totslinks= 0;
258         
259         return contn;
260 }
261
262 void copy_controllers(ListBase *lbn, ListBase *lbo)
263 {
264         bController *cont, *contn;
265         
266         lbn->first= lbn->last= 0;
267         cont= lbo->first;
268         while(cont) {
269                 contn= copy_controller(cont);
270                 BLI_addtail(lbn, contn);
271                 cont= cont->next;
272         }
273 }
274
275 void init_controller(bController *cont)
276 {
277         /* also use when controller changes type, leave actuators... */
278         
279         if(cont->data) MEM_freeN(cont->data);
280         cont->data= 0;
281         
282         switch(cont->type) {
283         case CONT_EXPRESSION:
284                 cont->data= MEM_callocN(sizeof(bExpressionCont), "expcont");
285                 break;
286         case CONT_PYTHON:
287                 cont->data= MEM_callocN(sizeof(bPythonCont), "pycont");
288                 break;
289         }
290 }
291
292 bController *new_controller(int type)
293 {
294         bController *cont;
295
296         cont= MEM_callocN(sizeof(bController), "Controller");
297         cont->type= type;
298         cont->flag= CONT_SHOW;
299
300         init_controller(cont);
301         
302         strcpy(cont->name, "cont");
303 // XXX  make_unique_prop_names(cont->name);
304         
305         return cont;
306 }
307
308 /* ******************* ACTUATORS ************************ */
309
310 void unlink_actuator(bActuator *act)
311 {
312         bController *cont;
313         Object *ob;
314         int a, removed;
315         
316         /* check for actuator pointers in controllers */
317         ob= G.main->object.first;
318         while(ob) {
319                 cont= ob->controllers.first;
320                 while(cont) {
321                         removed= 0;
322                         for(a=0; a<cont->totlinks; a++) {
323                                 if(removed) (cont->links)[a-1] = (cont->links)[a];
324                                 else if((cont->links)[a] == act) removed= 1;
325                         }
326                         if(removed) {
327                                 cont->totlinks--;
328                                 
329                                 if(cont->totlinks==0) {
330                                         MEM_freeN(cont->links);
331                                         cont->links= NULL;
332                                 }
333                         }
334                         cont= cont->next;
335                 }
336                 ob= ob->id.next;
337         }
338 }
339
340 void unlink_actuators(ListBase *lb)
341 {
342         bActuator *act;
343         
344         for (act= lb->first; act; act= act->next)
345                 unlink_actuator(act);
346 }
347
348 void free_actuator(bActuator *act)
349 {
350         if(act->data) MEM_freeN(act->data);
351         MEM_freeN(act);
352 }
353
354 void free_actuators(ListBase *lb)
355 {
356         bActuator *act;
357         
358         while((act= lb->first)) {
359                 BLI_remlink(lb, act);
360                 free_actuator(act);
361         }
362 }
363
364 bActuator *copy_actuator(bActuator *act)
365 {
366         bActuator *actn;
367         
368         act->mynew=actn= MEM_dupallocN(act);
369         actn->flag |= ACT_NEW;
370         if(act->data) {
371                 actn->data= MEM_dupallocN(act->data);
372         }
373         
374         return actn;
375 }
376
377 void copy_actuators(ListBase *lbn, ListBase *lbo)
378 {
379         bActuator *act, *actn;
380         
381         lbn->first= lbn->last= 0;
382         act= lbo->first;
383         while(act) {
384                 actn= copy_actuator(act);
385                 BLI_addtail(lbn, actn);
386                 act= act->next;
387         }
388 }
389
390 void init_actuator(bActuator *act)
391 {
392         /* also use when actuator changes type */
393         bObjectActuator *oa;
394         bSoundActuator *sa;
395         
396         if(act->data) MEM_freeN(act->data);
397         act->data= 0;
398         
399         switch(act->type) {
400         case ACT_ACTION:
401         case ACT_SHAPEACTION:
402                 act->data= MEM_callocN(sizeof(bActionActuator), "actionact");
403                 break;
404         case ACT_SOUND:
405                 sa = act->data= MEM_callocN(sizeof(bSoundActuator), "soundact");
406                 sa->volume = 1.0f;
407                 sa->sound3D.rolloff_factor = 1.0f;
408                 sa->sound3D.reference_distance = 1.0f;
409                 sa->sound3D.max_gain = 1.0f;
410                 sa->sound3D.cone_inner_angle = 360.0f;
411                 sa->sound3D.cone_outer_angle = 360.0f;
412                 sa->sound3D.max_distance = FLT_MAX;
413                 break;
414         case ACT_OBJECT:
415                 act->data= MEM_callocN(sizeof(bObjectActuator), "objectact");
416                 oa= act->data;
417                 oa->flag= 15;
418                 break;
419         case ACT_IPO:
420                 act->data= MEM_callocN(sizeof(bIpoActuator), "ipoact");
421                 break;
422         case ACT_PROPERTY:
423                 act->data= MEM_callocN(sizeof(bPropertyActuator), "propact");
424                 break;
425         case ACT_CAMERA:
426                 act->data= MEM_callocN(sizeof(bCameraActuator), "camact");
427                 break;
428         case ACT_EDIT_OBJECT:
429                 act->data= MEM_callocN(sizeof(bEditObjectActuator), "editobact");
430                 break;
431         case ACT_CONSTRAINT:
432                 act->data= MEM_callocN(sizeof(bConstraintActuator), "cons act");
433                 break;
434         case ACT_SCENE:
435                 act->data= MEM_callocN(sizeof(bSceneActuator), "scene act");
436                 break;
437         case ACT_GROUP:
438                 act->data= MEM_callocN(sizeof(bGroupActuator), "group act");
439                 break;
440         case ACT_RANDOM:
441                 act->data= MEM_callocN(sizeof(bRandomActuator), "random act");
442                 break;
443         case ACT_MESSAGE:
444                 act->data= MEM_callocN(sizeof(bMessageActuator), "message act");
445                 break;
446         case ACT_GAME:
447                 act->data= MEM_callocN(sizeof(bGameActuator), "game act");
448                 break;
449         case ACT_VISIBILITY:
450                 act->data= MEM_callocN(sizeof(bVisibilityActuator), "visibility act");
451                 break;
452     case ACT_2DFILTER:
453         act->data = MEM_callocN(sizeof( bTwoDFilterActuator ), "2d filter act");
454         break;
455     case ACT_PARENT:
456         act->data = MEM_callocN(sizeof( bParentActuator ), "parent act");
457         break;
458         case ACT_STATE:
459         act->data = MEM_callocN(sizeof( bStateActuator ), "state act");
460         break;
461         case ACT_ARMATURE:
462         act->data = MEM_callocN(sizeof( bArmatureActuator ), "armature act");
463         break;
464         default:
465                 ; /* this is very severe... I cannot make any memory for this        */
466                 /* logic brick...                                                    */
467         }
468 }
469
470 bActuator *new_actuator(int type)
471 {
472         bActuator *act;
473
474         act= MEM_callocN(sizeof(bActuator), "Actuator");
475         act->type= type;
476         act->flag= ACT_SHOW;
477         
478         init_actuator(act);
479         
480         strcpy(act->name, "act");
481 // XXX  make_unique_prop_names(act->name);
482         
483         return act;
484 }
485
486 /* ******************** GENERAL ******************* */
487
488 void clear_sca_new_poins_ob(Object *ob)
489 {
490         bSensor *sens;
491         bController *cont;
492         bActuator *act;
493         
494         sens= ob->sensors.first;
495         while(sens) {
496                 sens->flag &= ~SENS_NEW;
497                 sens= sens->next;
498         }
499         cont= ob->controllers.first;
500         while(cont) {
501                 cont->mynew= NULL;
502                 cont->flag &= ~CONT_NEW;
503                 cont= cont->next;
504         }
505         act= ob->actuators.first;
506         while(act) {
507                 act->mynew= NULL;
508                 act->flag &= ~ACT_NEW;
509                 act= act->next;
510         }
511 }
512
513 void clear_sca_new_poins()
514 {
515         Object *ob;
516         
517         ob= G.main->object.first;
518         while(ob) {
519                 clear_sca_new_poins_ob(ob);
520                 ob= ob->id.next;        
521         }
522 }
523
524 void set_sca_new_poins_ob(Object *ob)
525 {
526         bSensor *sens;
527         bController *cont;
528         bActuator *act;
529         int a;
530         
531         sens= ob->sensors.first;
532         while(sens) {
533                 if(sens->flag & SENS_NEW) {
534                         for(a=0; a<sens->totlinks; a++) {
535                                 if(sens->links[a] && sens->links[a]->mynew)
536                                         sens->links[a]= sens->links[a]->mynew;
537                         }
538                 }
539                 sens= sens->next;
540         }
541
542         cont= ob->controllers.first;
543         while(cont) {
544                 if(cont->flag & CONT_NEW) {
545                         for(a=0; a<cont->totlinks; a++) {
546                                 if( cont->links[a] && cont->links[a]->mynew)
547                                         cont->links[a]= cont->links[a]->mynew;
548                         }
549                 }
550                 cont= cont->next;
551         }
552         
553         
554         act= ob->actuators.first;
555         while(act) {
556                 if(act->flag & ACT_NEW) {
557                         if(act->type==ACT_EDIT_OBJECT) {
558                                 bEditObjectActuator *eoa= act->data;
559                                 ID_NEW(eoa->ob);
560                         }
561                         else if(act->type==ACT_SCENE) {
562                                 bSceneActuator *sca= act->data;
563                                 ID_NEW(sca->camera);
564                         }
565                         else if(act->type==ACT_CAMERA) {
566                                 bCameraActuator *ca= act->data;
567                                 ID_NEW(ca->ob);
568                         }
569                         else if(act->type==ACT_OBJECT) {
570                                 bObjectActuator *oa= act->data;
571                                 ID_NEW(oa->reference);
572                         }
573                         else if(act->type==ACT_SCENE) {
574                                 bSceneActuator *sca= act->data;
575                                 ID_NEW(sca->camera);
576                         }
577                 }
578                 act= act->next;
579         }
580 }
581
582
583 void set_sca_new_poins()
584 {
585         Object *ob;
586         
587         ob= G.main->object.first;
588         while(ob) {
589                 set_sca_new_poins_ob(ob);
590                 ob= ob->id.next;        
591         }
592 }
593
594 void sca_remove_ob_poin(Object *obt, Object *ob)
595 {
596         bSensor *sens;
597         bMessageSensor *ms;
598         bActuator *act;
599         bCameraActuator *ca;
600         bObjectActuator *oa;
601         bSceneActuator *sa;
602         bEditObjectActuator *eoa;
603         bPropertyActuator *pa;
604         bMessageActuator *ma;
605         bParentActuator *para;
606         bArmatureActuator *aa;
607
608         sens= obt->sensors.first;
609         while(sens) {
610                 switch(sens->type) {
611                 case SENS_MESSAGE:
612                         ms= sens->data;
613                         if(ms->fromObject==ob) ms->fromObject= NULL;
614                 }
615                 sens= sens->next;
616         }
617
618         act= obt->actuators.first;
619         while(act) {
620                 switch(act->type) {
621                 case ACT_CAMERA:
622                         ca= act->data;
623                         if(ca->ob==ob) ca->ob= NULL;
624                         break;
625                 case ACT_OBJECT:
626                         oa= act->data;
627                         if(oa->reference==ob) oa->reference= NULL;
628                         break;
629                 case ACT_PROPERTY:
630                         pa= act->data;
631                         if(pa->ob==ob) pa->ob= NULL;
632                         break;
633                 case ACT_SCENE:
634                         sa= act->data;
635                         if(sa->camera==ob) sa->camera= NULL;
636                         break;
637                 case ACT_EDIT_OBJECT:
638                         eoa= act->data;
639                         if(eoa->ob==ob) eoa->ob= NULL;
640                         break;
641                 case ACT_MESSAGE:
642                         ma= act->data;
643                         if(ma->toObject==ob) ma->toObject= NULL;
644                         break;
645                 case ACT_PARENT:
646                         para = act->data;
647                         if (para->ob==ob) para->ob = NULL;
648                         break;
649                 case ACT_ARMATURE:
650                         aa = act->data;
651                         if (aa->target == ob) aa->target = NULL;
652                         if (aa->subtarget == ob) aa->subtarget = NULL;
653                         break;
654                 }
655                 act= act->next;
656         }       
657 }