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