Added Joystick sensor (from snailrose)
[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 "nla.h"        /* For __NLA: Important, do not remove */
44 #include "DNA_text_types.h"
45 #include "DNA_controller_types.h"
46 #include "DNA_sensor_types.h"
47 #include "DNA_actuator_types.h"
48 #include "DNA_object_types.h"
49
50 #include "BLI_blenlib.h"
51 #include "BKE_bad_level_calls.h"
52 #include "BKE_utildefines.h"
53 #include "BKE_global.h"
54 #include "BKE_main.h"
55 #include "BKE_blender.h"
56 #include "BKE_sca.h"
57
58 void free_text_controllers(Text *txt)
59 {
60         Object *ob;
61         bController *cont;
62         
63         ob= G.main->object.first;
64         while(ob) {
65                 cont= ob->controllers.first;
66                 while(cont) {
67                         if(cont->type==CONT_PYTHON) {
68                                 bPythonCont *pc;
69                                 
70                                 pc= cont->data;
71                                 if(pc->text==txt) pc->text= NULL;
72                         }
73                         cont= cont->next;
74                 }
75                 ob= ob->id.next;
76         }
77 }
78
79
80 /* ******************* SENSORS ************************ */
81
82 void free_sensor(bSensor *sens)
83 {
84         if(sens->links) MEM_freeN(sens->links);
85         if(sens->data) MEM_freeN(sens->data);
86         MEM_freeN(sens);
87         
88 }
89
90 void free_sensors(ListBase *lb)
91 {
92         bSensor *sens;
93         
94         while((sens= lb->first)) {
95                 BLI_remlink(lb, sens);
96                 free_sensor(sens);
97         }
98 }
99
100 bSensor *copy_sensor(bSensor *sens)
101 {
102         bSensor *sensn;
103         
104         sensn= MEM_dupallocN(sens);
105         sensn->flag |= SENS_NEW;
106         if(sens->data) {
107                 sensn->data= MEM_dupallocN(sens->data);
108         }
109
110         if(sens->links) sensn->links= MEM_dupallocN(sens->links);
111         
112         return sensn;
113 }
114
115 void copy_sensors(ListBase *lbn, ListBase *lbo)
116 {
117         bSensor *sens, *sensn;
118         
119         lbn->first= lbn->last= 0;
120         sens= lbo->first;
121         while(sens) {
122                 sensn= copy_sensor(sens);
123                 BLI_addtail(lbn, sensn);
124                 sens= sens->next;
125         }
126 }
127
128 void init_sensor(bSensor *sens)
129 {
130         /* also use when sensor changes type */
131         bNearSensor *ns;
132         bMouseSensor *ms;
133         
134         if(sens->data) MEM_freeN(sens->data);
135         sens->data= NULL;
136         sens->pulse = 0;
137         
138         switch(sens->type) {
139         case SENS_ALWAYS:
140                 sens->pulse = 1;
141                 break;
142         case SENS_TOUCH:
143                 sens->data= MEM_callocN(sizeof(bTouchSensor), "touchsens");
144                 break;
145         case SENS_NEAR:
146                 ns=sens->data= MEM_callocN(sizeof(bNearSensor), "nearsens");
147                 ns->dist= 1.0;
148                 ns->resetdist= 2.0;
149                 break;
150         case SENS_KEYBOARD:
151                 sens->data= MEM_callocN(sizeof(bKeyboardSensor), "keysens");
152                 break;
153         case SENS_PROPERTY:
154                 sens->data= MEM_callocN(sizeof(bPropertySensor), "propsens");
155                 break;
156         case SENS_MOUSE:
157                 ms=sens->data= MEM_callocN(sizeof(bMouseSensor), "mousesens");
158                 ms->type= LEFTMOUSE;
159                 break;
160         case SENS_COLLISION:
161                 sens->data= MEM_callocN(sizeof(bCollisionSensor), "colsens");
162                 break;
163         case SENS_RADAR:
164                 sens->data= MEM_callocN(sizeof(bRadarSensor), "radarsens");
165                 break;
166         case SENS_RANDOM:
167                 sens->data= MEM_callocN(sizeof(bRandomSensor), "randomsens");
168                 break;
169         case SENS_RAY:
170                 sens->data= MEM_callocN(sizeof(bRaySensor), "raysens");
171                 break;
172         case SENS_MESSAGE:
173                 sens->data= MEM_callocN(sizeof(bMessageSensor), "messagesens");
174                 break;
175         case SENS_JOYSTICK:
176                 sens->data= MEM_callocN(sizeof(bJoystickSensor), "joysticksens");
177                 break;
178         default:
179                 ; /* this is very severe... I cannot make any memory for this        */
180                 /* logic brick...                                                    */
181         }
182 }
183
184 bSensor *new_sensor(int type)
185 {
186         bSensor *sens;
187
188         sens= MEM_callocN(sizeof(bSensor), "Sensor");
189         sens->type= type;
190         sens->flag= SENS_SHOW;
191         
192         init_sensor(sens);
193         
194         strcpy(sens->name, "sensor");
195         make_unique_prop_names(sens->name);
196         
197         return sens;
198 }
199
200 /* ******************* CONTROLLERS ************************ */
201
202 void unlink_controller(bController *cont)
203 {
204         bSensor *sens;
205         Object *ob;
206         int a, removed;
207         
208         /* check for controller pointers in sensors */
209         ob= G.main->object.first;
210         while(ob) {
211                 sens= ob->sensors.first;
212                 while(sens) {
213                         removed= 0;
214                         for(a=0; a<sens->totlinks; a++) {
215                                 if(removed) (sens->links)[a-1] = (sens->links)[a];
216                                 else if((sens->links)[a] == cont) removed= 1;
217                         }
218                         if(removed) {
219                                 sens->totlinks--;
220                                 
221                                 if(sens->totlinks==0) {
222                                         MEM_freeN(sens->links);
223                                         sens->links= NULL;
224                                 }
225                         }
226                         sens= sens->next;
227                 }
228                 ob= ob->id.next;
229         }
230 }
231
232 void unlink_controllers(ListBase *lb)
233 {
234         bController *cont;
235         
236         for (cont= lb->first; cont; cont= cont->next)
237                 unlink_controller(cont);        
238 }
239
240 void free_controller(bController *cont)
241 {
242         if(cont->links) MEM_freeN(cont->links);
243
244         /* the controller itself */
245         if(cont->data) MEM_freeN(cont->data);
246         MEM_freeN(cont);
247         
248 }
249
250 void free_controllers(ListBase *lb)
251 {
252         bController *cont;
253         
254         while((cont= lb->first)) {
255                 BLI_remlink(lb, cont);
256                 if(cont->slinks) MEM_freeN(cont->slinks);
257                 free_controller(cont);
258         }
259 }
260
261 bController *copy_controller(bController *cont)
262 {
263         bController *contn;
264         
265         cont->mynew=contn= MEM_dupallocN(cont);
266         contn->flag |= CONT_NEW;
267         if(cont->data) {
268                 contn->data= MEM_dupallocN(cont->data);
269         }
270
271         if(cont->links) contn->links= MEM_dupallocN(cont->links);
272         contn->slinks= NULL;
273         contn->totslinks= 0;
274         
275         return contn;
276 }
277
278 void copy_controllers(ListBase *lbn, ListBase *lbo)
279 {
280         bController *cont, *contn;
281         
282         lbn->first= lbn->last= 0;
283         cont= lbo->first;
284         while(cont) {
285                 contn= copy_controller(cont);
286                 BLI_addtail(lbn, contn);
287                 cont= cont->next;
288         }
289 }
290
291 void init_controller(bController *cont)
292 {
293         /* also use when controller changes type, leave actuators... */
294         
295         if(cont->data) MEM_freeN(cont->data);
296         cont->data= 0;
297         
298         switch(cont->type) {
299         case CONT_EXPRESSION:
300                 cont->data= MEM_callocN(sizeof(bExpressionCont), "expcont");
301                 break;
302         case CONT_PYTHON:
303                 cont->data= MEM_callocN(sizeof(bPythonCont), "pycont");
304                 break;
305         }
306 }
307
308 bController *new_controller(int type)
309 {
310         bController *cont;
311
312         cont= MEM_callocN(sizeof(bController), "Controller");
313         cont->type= type;
314         cont->flag= CONT_SHOW;
315
316         init_controller(cont);
317         
318         strcpy(cont->name, "cont");
319         make_unique_prop_names(cont->name);
320         
321         return cont;
322 }
323
324 /* ******************* ACTUATORS ************************ */
325
326 void unlink_actuator(bActuator *act)
327 {
328         bController *cont;
329         Object *ob;
330         int a, removed;
331         
332         /* check for actuator pointers in controllers */
333         ob= G.main->object.first;
334         while(ob) {
335                 cont= ob->controllers.first;
336                 while(cont) {
337                         removed= 0;
338                         for(a=0; a<cont->totlinks; a++) {
339                                 if(removed) (cont->links)[a-1] = (cont->links)[a];
340                                 else if((cont->links)[a] == act) removed= 1;
341                         }
342                         if(removed) {
343                                 cont->totlinks--;
344                                 
345                                 if(cont->totlinks==0) {
346                                         MEM_freeN(cont->links);
347                                         cont->links= NULL;
348                                 }
349                         }
350                         cont= cont->next;
351                 }
352                 ob= ob->id.next;
353         }
354 }
355
356 void unlink_actuators(ListBase *lb)
357 {
358         bActuator *act;
359         
360         for (act= lb->first; act; act= act->next)
361                 unlink_actuator(act);
362 }
363
364 void free_actuator(bActuator *act)
365 {
366         if(act->data) MEM_freeN(act->data);
367         MEM_freeN(act);
368 }
369
370 void free_actuators(ListBase *lb)
371 {
372         bActuator *act;
373         
374         while((act= lb->first)) {
375                 BLI_remlink(lb, act);
376                 free_actuator(act);
377         }
378 }
379
380 bActuator *copy_actuator(bActuator *act)
381 {
382         bActuator *actn;
383         
384         act->mynew=actn= MEM_dupallocN(act);
385         actn->flag |= ACT_NEW;
386         if(act->data) {
387                 actn->data= MEM_dupallocN(act->data);
388         }
389         
390         return actn;
391 }
392
393 void copy_actuators(ListBase *lbn, ListBase *lbo)
394 {
395         bActuator *act, *actn;
396         
397         lbn->first= lbn->last= 0;
398         act= lbo->first;
399         while(act) {
400                 actn= copy_actuator(act);
401                 BLI_addtail(lbn, actn);
402                 act= act->next;
403         }
404 }
405
406 void init_actuator(bActuator *act)
407 {
408         /* also use when actuator changes type */
409         bObjectActuator *oa;
410         
411         if(act->data) MEM_freeN(act->data);
412         act->data= 0;
413         
414         switch(act->type) {
415 #ifdef __NLA
416         case ACT_ACTION:
417                 act->data= MEM_callocN(sizeof(bActionActuator), "actionact");
418                 break;
419 #endif
420         case ACT_SOUND:
421                 act->data= MEM_callocN(sizeof(bSoundActuator), "soundact");
422                 break;
423         case ACT_CD:
424                 act->data= MEM_callocN(sizeof(bCDActuator), "cdact");
425                 break;
426         case ACT_OBJECT:
427                 act->data= MEM_callocN(sizeof(bObjectActuator), "objectact");
428                 oa= act->data;
429                 oa->flag= 15;
430                 break;
431         case ACT_IPO:
432                 act->data= MEM_callocN(sizeof(bIpoActuator), "ipoact");
433                 break;
434         case ACT_PROPERTY:
435                 act->data= MEM_callocN(sizeof(bPropertyActuator), "propact");
436                 break;
437         case ACT_CAMERA:
438                 act->data= MEM_callocN(sizeof(bCameraActuator), "camact");
439                 break;
440         case ACT_EDIT_OBJECT:
441                 act->data= MEM_callocN(sizeof(bEditObjectActuator), "editobact");
442                 break;
443         case ACT_CONSTRAINT:
444                 act->data= MEM_callocN(sizeof(bConstraintActuator), "cons act");
445                 break;
446         case ACT_SCENE:
447                 act->data= MEM_callocN(sizeof(bSceneActuator), "scene act");
448                 break;
449         case ACT_GROUP:
450                 act->data= MEM_callocN(sizeof(bGroupActuator), "group act");
451                 break;
452         case ACT_RANDOM:
453                 act->data= MEM_callocN(sizeof(bRandomActuator), "random act");
454                 break;
455         case ACT_MESSAGE:
456                 act->data= MEM_callocN(sizeof(bMessageActuator), "message act");
457                 break;
458         case ACT_GAME:
459                 act->data= MEM_callocN(sizeof(bGameActuator), "game act");
460                 break;
461         case ACT_VISIBILITY:
462                 act->data= MEM_callocN(sizeof(bVisibilityActuator), "visibility 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         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_SCENE) {
570                                 bSceneActuator *sca= act->data;
571                                 ID_NEW(sca->camera);
572                         }
573                 }
574                 act= act->next;
575         }
576 }
577
578
579 void set_sca_new_poins()
580 {
581         Object *ob;
582         
583         ob= G.main->object.first;
584         while(ob) {
585                 set_sca_new_poins_ob(ob);
586                 ob= ob->id.next;        
587         }
588 }
589
590 void sca_remove_ob_poin(Object *obt, Object *ob)
591 {
592         bSensor *sens;
593         bMessageSensor *ms;
594         bActuator *act;
595         bCameraActuator *ca;
596         bSceneActuator *sa;
597         bEditObjectActuator *eoa;
598         bPropertyActuator *pa;
599         bMessageActuator *ma;
600
601         sens= obt->sensors.first;
602         while(sens) {
603                 switch(sens->type) {
604                 case SENS_MESSAGE:
605                         ms= sens->data;
606                         if(ms->fromObject==ob) ms->fromObject= NULL;
607                 }
608                 sens= sens->next;
609         }
610
611         act= obt->actuators.first;
612         while(act) {
613                 switch(act->type) {
614                 case ACT_CAMERA:
615                         ca= act->data;
616                         if(ca->ob==ob) ca->ob= NULL;
617                         break;
618                 case ACT_PROPERTY:
619                         pa= act->data;
620                         if(pa->ob==ob) pa->ob= NULL;
621                         break;
622                 case ACT_SCENE:
623                         sa= act->data;
624                         if(sa->camera==ob) sa->camera= NULL;
625                         break;
626                 case ACT_EDIT_OBJECT:
627                         eoa= act->data;
628                         if(eoa->ob==ob) eoa->ob= NULL;
629                         break;
630                 case ACT_MESSAGE:
631                         ma= act->data;
632                         if(ma->toObject==ob) ma->toObject= NULL;
633                         break;
634
635                 }
636                 act= act->next;
637         }       
638 }