Merge of SimpleDeform modifier from soc-2008-jaguarandi branch
[blender.git] / source / gameengine / Converter / KX_ConvertActuators.cpp
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 * Convert Blender actuators for use in the GameEngine
29 */
30
31 #ifdef WIN32
32 #pragma warning (disable : 4786) 
33 #endif //WIN32
34
35 #define BLENDER_HACK_DTIME 0.02
36
37 #include "MEM_guardedalloc.h"
38
39 #include "KX_BlenderSceneConverter.h"
40 #include "KX_ConvertActuators.h"
41
42 // Actuators
43 //SCA logiclibrary native logicbricks
44 #include "SCA_PropertyActuator.h"
45 #include "SCA_LogicManager.h"
46 #include "SCA_RandomActuator.h"
47 #include "SCA_2DFilterActuator.h"
48
49 // Ketsji specific logicbricks
50 #include "KX_SceneActuator.h"
51 #include "KX_IpoActuator.h"
52 #include "KX_SoundActuator.h"
53 #include "KX_CDActuator.h"
54 #include "KX_ObjectActuator.h"
55 #include "KX_TrackToActuator.h"
56 #include "KX_ConstraintActuator.h"
57 #include "KX_CameraActuator.h"
58 #include "KX_GameActuator.h"
59 #include "KX_StateActuator.h"
60 #include "KX_VisibilityActuator.h"
61 #include "KX_SCA_AddObjectActuator.h"
62 #include "KX_SCA_EndObjectActuator.h"
63 #include "KX_SCA_ReplaceMeshActuator.h"
64 #include "KX_ParentActuator.h"
65 #include "KX_SCA_DynamicActuator.h"
66
67 #include "KX_Scene.h"
68 #include "KX_KetsjiEngine.h"
69
70 #include "IntValue.h"
71 #include "KX_GameObject.h"
72
73 /* This little block needed for linking to Blender... */
74 #include "BKE_text.h"
75 #include "BLI_blenlib.h"
76
77 #define FILE_MAX 240 // repeated here to avoid dependency from BKE_utildefines.h
78
79 #include "KX_NetworkMessageActuator.h"
80
81 #ifdef WIN32
82 #include "BLI_winstuff.h"
83 #endif
84
85 #include "DNA_object_types.h"
86 #include "DNA_sound_types.h"
87 #include "DNA_scene_types.h"
88 #include "DNA_actuator_types.h"
89 #include "DNA_packedFile_types.h"
90 #include "BL_ActionActuator.h"
91 #include "BL_ShapeActionActuator.h"
92 /* end of blender include block */
93
94 #include "BL_BlenderDataConversion.h"
95
96 /** 
97 KX_BLENDERTRUNC needed to round 'almost' zero values to zero, else velocities etc. are incorrectly set 
98 */
99
100 #define KX_BLENDERTRUNC(x)  (( x < 0.0001 && x > -0.0001 )  ? 0.0 : x)
101
102 void BL_ConvertActuators(char* maggiename,
103                                                  struct Object* blenderobject,
104                                                  KX_GameObject* gameobj,
105                                                  SCA_LogicManager* logicmgr,
106                                                  KX_Scene* scene,
107                                                  KX_KetsjiEngine* ketsjiEngine,
108                                                  int & executePriority, 
109                                                  int activeLayerBitInfo,
110                                                  bool isInActiveLayer,
111                                                  RAS_IRenderTools* rendertools,
112                                                  KX_BlenderSceneConverter* converter
113                                                  )
114 {
115         
116         int uniqueint = 0;
117         bActuator* bact = (bActuator*) blenderobject->actuators.first;
118         while(bact)
119         {
120                 STR_String uniquename = bact->name;
121                 STR_String objectname = gameobj->GetName();
122                 
123                 SCA_IActuator* baseact = NULL;
124                 switch (bact->type)
125                 {
126                 case ACT_OBJECT:
127                         {
128                                 bObjectActuator* obact = (bObjectActuator*) bact->data;
129                                 MT_Vector3 forcevec(KX_BLENDERTRUNC(obact->forceloc[0]),
130                                         KX_BLENDERTRUNC(obact->forceloc[1]),
131                                         KX_BLENDERTRUNC(obact->forceloc[2]));
132                                 MT_Vector3 torquevec(obact->forcerot[0],obact->forcerot[1],obact->forcerot[2]);
133                                 MT_Vector3 dlocvec ( KX_BLENDERTRUNC(obact->dloc[0]),
134                                         KX_BLENDERTRUNC(obact->dloc[1]),
135                                         KX_BLENDERTRUNC(obact->dloc[2]));
136                                 MT_Vector3 drotvec ( KX_BLENDERTRUNC(obact->drot[0]),obact->drot[1],obact->drot[2]);
137                                 MT_Vector3 linvelvec ( KX_BLENDERTRUNC(obact->linearvelocity[0]),
138                                         KX_BLENDERTRUNC(obact->linearvelocity[1]),
139                                         KX_BLENDERTRUNC(obact->linearvelocity[2]));
140                                 MT_Vector3 angvelvec ( KX_BLENDERTRUNC(obact->angularvelocity[0]),
141                                         KX_BLENDERTRUNC(obact->angularvelocity[1]),
142                                         KX_BLENDERTRUNC(obact->angularvelocity[2]));
143                                 short damping = obact->damping;
144                                 
145                                 drotvec /=              BLENDER_HACK_DTIME;
146                                 //drotvec /=            BLENDER_HACK_DTIME;
147                                 drotvec *= MT_2_PI/360.0;
148                                 //dlocvec /= BLENDER_HACK_DTIME;
149                                 //linvelvec /=  BLENDER_HACK_DTIME;
150                                 //angvelvec /=  BLENDER_HACK_DTIME;
151                                 
152                                 /* Blender uses a bit vector internally for the local-flags. In */
153                                 /* KX, we have four bools. The compiler should be smart enough  */
154                                 /* to do the right thing. We need to explicitly convert here!   */
155                                 
156                                 KX_LocalFlags bitLocalFlag;
157                                 
158                                 bitLocalFlag.Force = bool((obact->flag & ACT_FORCE_LOCAL)!=0);
159                                 bitLocalFlag.Torque = bool((obact->flag & ACT_TORQUE_LOCAL) !=0);//rlocal;
160                                 bitLocalFlag.DLoc = bool((obact->flag & ACT_DLOC_LOCAL)!=0);
161                                 bitLocalFlag.DRot = bool((obact->flag & ACT_DROT_LOCAL)!=0);
162                                 bitLocalFlag.LinearVelocity = bool((obact->flag & ACT_LIN_VEL_LOCAL)!=0);
163                                 bitLocalFlag.AngularVelocity = bool((obact->flag & ACT_ANG_VEL_LOCAL)!=0);
164                                 bitLocalFlag.ServoControl = bool(obact->type == ACT_OBJECT_SERVO);
165                                 bitLocalFlag.AddOrSetLinV = bool((obact->flag & ACT_ADD_LIN_VEL)!=0);
166                                 
167                                 
168                                 KX_ObjectActuator* tmpbaseact = new KX_ObjectActuator(gameobj,
169                                         forcevec.getValue(),
170                                         torquevec.getValue(),
171                                         dlocvec.getValue(),
172                                         drotvec.getValue(),
173                                         linvelvec.getValue(),
174                                         angvelvec.getValue(),
175                                         damping,
176                                         bitLocalFlag
177                                         );
178                                 baseact = tmpbaseact;
179                                 break;
180                         }
181                 case ACT_ACTION:
182                         {
183                                 if (blenderobject->type==OB_ARMATURE){
184                                         bActionActuator* actact = (bActionActuator*) bact->data;
185                                         STR_String propname = (actact->name ? actact->name : "");
186                                         STR_String propframe = (actact->frameProp ? actact->frameProp : "");
187                                         
188                                         BL_ActionActuator* tmpbaseact = new BL_ActionActuator(
189                                                 gameobj,
190                                                 propname,
191                                                 propframe,
192                                                 actact->sta,
193                                                 actact->end,
194                                                 actact->act,
195                                                 actact->type, // + 1, because Blender starts to count at zero,
196                                                 actact->blendin,
197                                                 actact->priority,
198                                                 actact->end_reset,
199                                                 actact->stridelength
200                                                 // Ketsji at 1, because zero is reserved for "NoDef"
201                                                 );
202                                         baseact= tmpbaseact;
203                                         break;
204                                 }
205                                 else
206                                         printf ("Discarded action actuator from non-armature object [%s]\n", blenderobject->id.name+2);
207                         }
208                 case ACT_SHAPEACTION:
209                         {
210                                 if (blenderobject->type==OB_MESH){
211                                         bActionActuator* actact = (bActionActuator*) bact->data;
212                                         STR_String propname = (actact->name ? actact->name : "");
213                                         STR_String propframe = (actact->frameProp ? actact->frameProp : "");
214                                         
215                                         BL_ShapeActionActuator* tmpbaseact = new BL_ShapeActionActuator(
216                                                 gameobj,
217                                                 propname,
218                                                 propframe,
219                                                 actact->sta,
220                                                 actact->end,
221                                                 actact->act,
222                                                 actact->type, // + 1, because Blender starts to count at zero,
223                                                 actact->blendin,
224                                                 actact->priority,
225                                                 actact->stridelength
226                                                 // Ketsji at 1, because zero is reserved for "NoDef"
227                                                 );
228                                         baseact= tmpbaseact;
229                                         break;
230                                 }
231                                 else
232                                         printf ("Discarded shape action actuator from non-mesh object [%s]\n", blenderobject->id.name+2);
233                         }
234                 case ACT_IPO:
235                         {
236                                 bIpoActuator* ipoact = (bIpoActuator*) bact->data;
237                                 bool ipochild = (ipoact->flag & ACT_IPOCHILD) !=0;
238                                 STR_String propname = ( ipoact->name ? ipoact->name : "");
239                                 // first bit?
240                                 bool ipo_as_force = (ipoact->flag & ACT_IPOFORCE);
241                                 bool local = (ipoact->flag & ACT_IPOLOCAL);
242                                 bool ipo_add = (ipoact->flag & ACT_IPOADD);
243                                 
244                                 KX_IpoActuator* tmpbaseact = new KX_IpoActuator(
245                                         gameobj,
246                                         propname ,
247                                         ipoact->sta,
248                                         ipoact->end,
249                                         ipochild,
250                                         ipoact->type + 1, // + 1, because Blender starts to count at zero,
251                                         // Ketsji at 1, because zero is reserved for "NoDef"
252                                         ipo_as_force,
253                                         ipo_add,
254                                         local);
255                                 baseact = tmpbaseact;
256                                 break;
257                         }
258                 case ACT_LAMP:
259                         {
260                                 break;
261                         }
262                 case ACT_CAMERA:
263                         {
264                                 bCameraActuator *camact = (bCameraActuator *) bact->data;
265                                 if (camact->ob) {
266                                         KX_GameObject *tmpgob = converter->FindGameObject(camact->ob);
267                                         
268                                         /* visifac, fac and axis are not copied from the struct...   */ 
269                                         /* that's some internal state...                             */
270                                         KX_CameraActuator *tmpcamact
271                                                 = new KX_CameraActuator(gameobj,
272                                                 tmpgob,
273                                                 camact->height,
274                                                 camact->min,
275                                                 camact->max,
276                                                 camact->axis=='x');
277                                         baseact = tmpcamact;
278                                 }
279                                 break;
280                         }
281                 case ACT_MESSAGE:
282                         {
283                                 bMessageActuator *msgAct = (bMessageActuator *) bact->data;
284                                 
285                                 /**
286                                 * Get the name of the properties that objects must own that
287                                 * we're sending to, if present
288                                 */
289                                 STR_String toPropName = (msgAct->toPropName
290                                         ? (char*) msgAct->toPropName
291                                         : "");
292                                         /**
293                                         * Get the Message Subject to send.
294                                 */
295                                 STR_String subject = (msgAct->subject
296                                         ? (char*) msgAct->subject
297                                         : "");
298                                 
299                                         /**
300                                         * Get the bodyType
301                                 */
302                                 int bodyType = msgAct->bodyType;
303                                 
304                                 /**
305                                 * Get the body (text message or property name whose value
306                                 * we'll be sending, might be empty
307                                 */
308                                 STR_String body = (msgAct->body
309                                         ? (char*) msgAct->body
310                                         : "");
311                                 
312                                 KX_NetworkMessageActuator *tmpmsgact = 
313                                         new KX_NetworkMessageActuator(
314                                         gameobj,                                        // actuator controlling object
315                                         scene->GetNetworkScene(),       // needed for replication
316                                         toPropName,
317                                         subject,
318                                         bodyType,
319                                         body);
320                                 baseact = tmpmsgact;
321                                 break;
322                         }
323                 case ACT_MATERIAL:
324                         {
325                                 break;
326                         }
327                 case ACT_SOUND:
328                         {
329                                 bSoundActuator* soundact = (bSoundActuator*) bact->data;
330                                 /* get type, and possibly a start and end frame */
331                                 short startFrame = soundact->sta, stopFrame = soundact->end;
332                                 KX_SoundActuator::KX_SOUNDACT_TYPE 
333                                         soundActuatorType = KX_SoundActuator::KX_SOUNDACT_NODEF;
334                                 
335                                 switch(soundact->type) {
336                                 case ACT_SND_PLAY_STOP_SOUND:
337                                         soundActuatorType = KX_SoundActuator::KX_SOUNDACT_PLAYSTOP;
338                                         break;
339                                 case ACT_SND_PLAY_END_SOUND:
340                                         soundActuatorType = KX_SoundActuator::KX_SOUNDACT_PLAYEND;
341                                         break;
342                                 case ACT_SND_LOOP_STOP_SOUND:
343                                         soundActuatorType = KX_SoundActuator::KX_SOUNDACT_LOOPSTOP;
344                                         break;
345                                 case ACT_SND_LOOP_END_SOUND:
346                                         soundActuatorType = KX_SoundActuator::KX_SOUNDACT_LOOPEND;
347                                         break;
348                                 case ACT_SND_LOOP_BIDIRECTIONAL_SOUND:
349                                         soundActuatorType = KX_SoundActuator::KX_SOUNDACT_LOOPBIDIRECTIONAL;
350                                         break;
351                                 case ACT_SND_LOOP_BIDIRECTIONAL_STOP_SOUND:
352                                         soundActuatorType = KX_SoundActuator::KX_SOUNDACT_LOOPBIDIRECTIONAL_STOP;
353                                         break;
354                                         
355                                 default:
356                                         /* This is an error!!! */
357                                         soundActuatorType = KX_SoundActuator::KX_SOUNDACT_NODEF;
358                                 }
359                                 
360                                 if (soundActuatorType != KX_SoundActuator::KX_SOUNDACT_NODEF) 
361                                 {
362                                         SND_Scene* soundscene = scene->GetSoundScene();
363                                         STR_String samplename = "";
364                                         bool sampleisloaded = false;
365                                         
366                                         if (soundact->sound) {
367                                                 /* Need to convert the samplename into absolute path
368                                                  * before checking if its loaded */
369                                                 char fullpath[FILE_MAX];
370                                                 
371                                                 /* dont modify soundact->sound->name, only change a copy */
372                                                 BLI_strncpy(fullpath, soundact->sound->name, sizeof(fullpath));
373                                                 BLI_convertstringcode(fullpath, maggiename);
374                                                 samplename = fullpath;
375                                                 
376                                                 /* let's see if the sample was already loaded */
377                                                 if (soundscene->IsSampleLoaded(samplename))
378                                                 {
379                                                         sampleisloaded = true;
380                                                 }
381                                                 else {
382                                                         /* if not, make it so */
383                                                         PackedFile* pf = soundact->sound->newpackedfile;
384                                                         
385                                                         /* but we need a packed file then */
386                                                         if (pf)
387                                                         {
388                                                                 if (soundscene->LoadSample(samplename, pf->data, pf->size) > -1)
389                                                                         sampleisloaded = true;
390                                                         }
391                                                         /* or else load it from disk */
392                                                         else
393                                                         {
394                                                                 if (soundscene->LoadSample(samplename, NULL, 0) > -1) {
395                                                                         sampleisloaded = true;
396                                                                 }
397                                                                 else {
398                                                                         std::cout <<    "WARNING: Sound actuator \"" << bact->name <<
399                                                                                                         "\" from object \"" <<  blenderobject->id.name+2 <<
400                                                                                                         "\" failed to load sample." << std::endl;
401                                                                 }
402                                                         }
403                                                 }
404                                         } else {
405                                                 std::cout <<    "WARNING: Sound actuator \"" << bact->name <<
406                                                                                 "\" from object \"" <<  blenderobject->id.name+2 <<
407                                                                                 "\" has no sound datablock." << std::endl;
408                                         }
409                                         
410                                         /* Note, allowing actuators for sounds that are not there was added since 2.47
411                                          * This is because python may expect the actuator and raise an exception if it dosnt find it
412                                          * better just to add a dummy sound actuator. */
413                                         SND_SoundObject* sndobj = NULL;
414                                         if (sampleisloaded)
415                                         {
416                                                 /* setup the SND_SoundObject */
417                                                 sndobj = new SND_SoundObject();
418                                                 sndobj->SetSampleName(samplename.Ptr());
419                                                 sndobj->SetObjectName(bact->name);
420                                                 if (soundact->sound) {
421                                                         sndobj->SetRollOffFactor(soundact->sound->attenuation);
422                                                         sndobj->SetGain(soundact->sound->volume);
423                                                         sndobj->SetPitch(exp((soundact->sound->pitch / 12.0) * log(2.0)));
424                                                         //                                                      sndobj->SetLoopStart(soundact->sound->loopstart);
425                                                         //                                                      sndobj->SetLoopStart(soundact->sound->loopend);
426                                                         if (soundact->sound->flags & SOUND_FLAGS_LOOP)
427                                                         {
428                                                                 if (soundact->sound->flags & SOUND_FLAGS_BIDIRECTIONAL_LOOP)
429                                                                         sndobj->SetLoopMode(SND_LOOP_BIDIRECTIONAL);
430                                                                 else
431                                                                         sndobj->SetLoopMode(SND_LOOP_NORMAL);
432                                                         }
433                                                         else {
434                                                                 sndobj->SetLoopMode(SND_LOOP_OFF);
435                                                         }
436                                                         
437                                                         if (soundact->sound->flags & SOUND_FLAGS_PRIORITY)
438                                                                 sndobj->SetHighPriority(true);
439                                                         else
440                                                                 sndobj->SetHighPriority(false);
441                                                         
442                                                         if (soundact->sound->flags & SOUND_FLAGS_3D)
443                                                                 sndobj->Set3D(true);
444                                                         else
445                                                                 sndobj->Set3D(false);
446                                                 }
447                                                 else {
448                                                         /* dummy values for a NULL sound
449                                                         * see editsound.c - defaults are unlikely to change soon */
450                                                         sndobj->SetRollOffFactor(1.0);
451                                                         sndobj->SetGain(1.0);
452                                                         sndobj->SetPitch(1.0);
453                                                         sndobj->SetLoopMode(SND_LOOP_OFF);
454                                                         sndobj->SetHighPriority(false);
455                                                         sndobj->Set3D(false);
456                                                 }
457                                         }
458                                         KX_SoundActuator* tmpsoundact = 
459                                                 new KX_SoundActuator(gameobj, 
460                                                 sndobj,
461                                                 scene->GetSoundScene(), // needed for replication!
462                                                 soundActuatorType,
463                                                 startFrame,
464                                                 stopFrame);
465                                         
466                                         tmpsoundact->SetName(bact->name);
467                                         baseact = tmpsoundact;
468                                         if (sndobj)
469                                                 soundscene->AddObject(sndobj);
470                                 }
471                                 break;
472                         }
473                 case ACT_CD:
474                         {
475                                 bCDActuator* cdact = (bCDActuator*) bact->data;
476                                 /* get type, and possibly a start and end frame */
477                                 short startFrame = cdact->sta, stopFrame = cdact->end;
478                                 KX_CDActuator::KX_CDACT_TYPE 
479                                         cdActuatorType = KX_CDActuator::KX_CDACT_NODEF;
480                                 
481                                 switch(cdact->type)
482                                 {
483                                 case ACT_CD_PLAY_ALL:
484                                         cdActuatorType = KX_CDActuator::KX_CDACT_PLAY_ALL;
485                                         break;
486                                 case ACT_CD_PLAY_TRACK:
487                                         cdActuatorType = KX_CDActuator::KX_CDACT_PLAY_TRACK;
488                                         break;
489                                 case ACT_CD_LOOP_TRACK:
490                                         cdActuatorType = KX_CDActuator::KX_CDACT_LOOP_TRACK;
491                                         break;
492                                 case ACT_CD_VOLUME:
493                                         cdActuatorType = KX_CDActuator::KX_CDACT_VOLUME;
494                                         break;
495                                 case ACT_CD_STOP:
496                                         cdActuatorType = KX_CDActuator::KX_CDACT_STOP;
497                                         break;
498                                 case ACT_CD_PAUSE:
499                                         cdActuatorType = KX_CDActuator::KX_CDACT_PAUSE;
500                                         break;
501                                 case ACT_CD_RESUME:
502                                         cdActuatorType = KX_CDActuator::KX_CDACT_RESUME;
503                                         break;
504                                         
505                                 default:
506                                         /* This is an error!!! */
507                                         cdActuatorType = KX_CDActuator::KX_CDACT_NODEF;
508                                 }
509                                 
510                                 if (cdActuatorType != KX_CDActuator::KX_CDACT_NODEF) 
511                                 {
512                                         SND_CDObject* pCD = SND_CDObject::Instance();
513                                         
514                                         if (pCD)
515                                         {
516                                                 pCD->SetGain(cdact->volume);
517                                                 
518                                                 KX_CDActuator* tmpcdact = 
519                                                         new KX_CDActuator(gameobj, 
520                                                         scene->GetSoundScene(), // needed for replication!
521                                                         cdActuatorType,
522                                                         cdact->track,
523                                                         startFrame,
524                                                         stopFrame);
525                                                 
526                                                 tmpcdact->SetName(bact->name);
527                                                 baseact = tmpcdact;
528                                         }
529                                 }
530                                 break;
531                         }
532                 case ACT_PROPERTY:
533                         {
534                                 bPropertyActuator* propact = (bPropertyActuator*) bact->data;
535                                 SCA_IObject* destinationObj = NULL;
536                                 
537                                 /*
538                                 here the destinationobject is searched. problem with multiple scenes: other scenes
539                                 have not been converted yet, so the destobj will not be found, so the prop will
540                                 not be copied.
541                                 possible solutions:
542                                 - convert everything when possible and not realtime only when needed.
543                                 - let the object-with-property report itself to the act when converted
544                                 */
545                                 if (propact->ob)
546                                         destinationObj = converter->FindGameObject(propact->ob);
547                                 
548                                 SCA_PropertyActuator* tmppropact = new SCA_PropertyActuator(
549                                         gameobj,
550                                         destinationObj,
551                                         propact->name,
552                                         propact->value,
553                                         propact->type+1); // + 1 because Ketsji Logic starts
554                                 // with 0 for KX_ACT_PROP_NODEF
555                                 baseact = tmppropact;
556                                 break;
557                         }
558                 case ACT_EDIT_OBJECT:
559                         {
560                                 bEditObjectActuator *editobact 
561                                         = (bEditObjectActuator *) bact->data;
562                                 /* There are four different kinds of 'edit object' thingies  */
563                                 /* The alternative to this lengthy conversion is packing     */
564                                 /* several actuators in one, which is not very nice design.. */
565                                 switch (editobact->type) {
566                                 case ACT_EDOB_ADD_OBJECT: 
567                                         {
568                                                 
569                                                 // does the 'original' for replication exists, and 
570                                                 // is it in a non-active layer ?
571                                                 SCA_IObject* originalval = NULL;
572                                                 if (editobact->ob)
573                                                 {
574                                                         if (editobact->ob->lay & activeLayerBitInfo)
575                                                         {
576                                                                 fprintf(stderr, "Warning, object \"%s\" from AddObject actuator \"%s\" is not in a hidden layer.\n", objectname.Ptr(), uniquename.Ptr());
577                                                         }
578                                                         else {
579                                                                 originalval = converter->FindGameObject(editobact->ob);
580                                                         }
581                                                 }
582                                                 MT_Vector3 linvelvec (
583                                                         KX_BLENDERTRUNC(editobact->linVelocity[0]),
584                                                         KX_BLENDERTRUNC(editobact->linVelocity[1]),
585                                                         KX_BLENDERTRUNC(editobact->linVelocity[2]));
586                                                 
587                                                 MT_Vector3 angvelvec (
588                                                         KX_BLENDERTRUNC(editobact->angVelocity[0]),
589                                                         KX_BLENDERTRUNC(editobact->angVelocity[1]),
590                                                         KX_BLENDERTRUNC(editobact->angVelocity[2]));
591                                                 
592                                                 KX_SCA_AddObjectActuator* tmpaddact = 
593                                                         new KX_SCA_AddObjectActuator(
594                                                                 gameobj, 
595                                                                 originalval,
596                                                                 editobact->time,
597                                                                 scene,
598                                                                 linvelvec.getValue(),
599                                                                 (editobact->localflag & ACT_EDOB_LOCAL_LINV)!=0,
600                                                                 angvelvec.getValue(),
601                                                                 (editobact->localflag & ACT_EDOB_LOCAL_ANGV)!=0
602                                                                 );
603                                                                 
604                                                                 //editobact->ob to gameobj
605                                                                 baseact = tmpaddact;
606                                         }
607                                         break;
608                                 case ACT_EDOB_END_OBJECT:
609                                         {
610                                                 KX_SCA_EndObjectActuator* tmpendact 
611                                                         = new KX_SCA_EndObjectActuator(gameobj,scene);
612                                                 baseact = tmpendact;
613                                         }
614                                         break;
615                                 case ACT_EDOB_REPLACE_MESH:
616                                         {
617                                                 RAS_MeshObject *tmpmesh = NULL;
618                                                 if (editobact->me)
619                                                         tmpmesh = BL_ConvertMesh(
620                                                                 editobact->me,
621                                                                 blenderobject,
622                                                                 rendertools,
623                                                                 scene,
624                                                                 converter
625                                                                 );
626
627                                                 KX_SCA_ReplaceMeshActuator* tmpreplaceact
628                                                         = new KX_SCA_ReplaceMeshActuator(
629                                                                 gameobj,
630                                                                 tmpmesh,
631                                                                 scene
632                                                                 );
633                                                         
634                                                         baseact = tmpreplaceact;
635                                         }
636                                         break;
637                                 case ACT_EDOB_TRACK_TO:
638                                         {
639                                                 SCA_IObject* originalval = NULL;
640                                                 if (editobact->ob)
641                                                         originalval = converter->FindGameObject(editobact->ob);
642                                                         
643                                                 KX_TrackToActuator* tmptrackact 
644                                                         = new KX_TrackToActuator(gameobj, 
645                                                                 originalval,
646                                                                 editobact->time,
647                                                                 editobact->flag,
648                                                                 blenderobject->trackflag,
649                                                                 blenderobject->upflag
650                                                                 );
651                                                         baseact = tmptrackact;
652                                                 break;
653                                         }
654                                 case ACT_EDOB_DYNAMICS:
655                                         {
656                                                 KX_SCA_DynamicActuator* tmpdynact 
657                                                         = new KX_SCA_DynamicActuator(gameobj, 
658                                                                 editobact->dyn_operation
659                                                                 );
660                                                         baseact = tmpdynact;
661                                         }
662                                 }
663                                 break;
664                         }
665                 case ACT_CONSTRAINT:
666                         {
667                                 float min = 0.0, max = 0.0;
668                                 char *prop = NULL;
669                                 KX_ConstraintActuator::KX_CONSTRAINTTYPE locrot = KX_ConstraintActuator::KX_ACT_CONSTRAINT_NODEF;
670                                 bConstraintActuator *conact 
671                                         = (bConstraintActuator*) bact->data;
672                                 /* convert settings... degrees in the ui become radians  */ 
673                                 /* internally                                            */ 
674                                 if (conact->type == ACT_CONST_TYPE_ORI) {
675                                         min = (MT_2_PI * conact->minloc[0])/360.0;
676                                         max = (MT_2_PI * conact->maxloc[0])/360.0;
677                                         switch (conact->mode) {
678                                         case ACT_CONST_DIRPX:
679                                                 locrot = KX_ConstraintActuator::KX_ACT_CONSTRAINT_ORIX;
680                                                 break;
681                                         case ACT_CONST_DIRPY:
682                                                 locrot = KX_ConstraintActuator::KX_ACT_CONSTRAINT_ORIY;
683                                                 break;
684                                         case ACT_CONST_DIRPZ:
685                                                 locrot = KX_ConstraintActuator::KX_ACT_CONSTRAINT_ORIZ;
686                                                 break;
687                                         }
688                                 } else if (conact->type == ACT_CONST_TYPE_DIST) {
689                                         switch (conact->mode) {
690                                         case ACT_CONST_DIRPX:
691                                                 locrot = KX_ConstraintActuator::KX_ACT_CONSTRAINT_DIRPX;
692                                                 min = conact->minloc[0];
693                                                 max = conact->maxloc[0];
694                                                 break;
695                                         case ACT_CONST_DIRPY:
696                                                 locrot = KX_ConstraintActuator::KX_ACT_CONSTRAINT_DIRPY;
697                                                 min = conact->minloc[1];
698                                                 max = conact->maxloc[1];
699                                                 break;
700                                         case ACT_CONST_DIRPZ:
701                                                 locrot = KX_ConstraintActuator::KX_ACT_CONSTRAINT_DIRPZ;
702                                                 min = conact->minloc[2];
703                                                 max = conact->maxloc[2];
704                                                 break;
705                                         case ACT_CONST_DIRNX:
706                                                 locrot = KX_ConstraintActuator::KX_ACT_CONSTRAINT_DIRNX;
707                                                 min = conact->minloc[0];
708                                                 max = conact->maxloc[0];
709                                                 break;
710                                         case ACT_CONST_DIRNY:
711                                                 locrot = KX_ConstraintActuator::KX_ACT_CONSTRAINT_DIRNY;
712                                                 min = conact->minloc[1];
713                                                 max = conact->maxloc[1];
714                                                 break;
715                                         case ACT_CONST_DIRNZ:
716                                                 locrot = KX_ConstraintActuator::KX_ACT_CONSTRAINT_DIRNZ;
717                                                 min = conact->minloc[2];
718                                                 max = conact->maxloc[2];
719                                                 break;
720                                         }
721                                         prop = conact->matprop;
722                                 } else {
723                                         switch (conact->flag) {
724                                         case ACT_CONST_LOCX:
725                                                 locrot = KX_ConstraintActuator::KX_ACT_CONSTRAINT_LOCX; 
726                                                 min = conact->minloc[0];
727                                                 max = conact->maxloc[0];
728                                                 break;
729                                         case ACT_CONST_LOCY:
730                                                 locrot = KX_ConstraintActuator::KX_ACT_CONSTRAINT_LOCY; 
731                                                 min = conact->minloc[1];
732                                                 max = conact->maxloc[1];
733                                                 break;
734                                         case ACT_CONST_LOCZ:
735                                                 locrot = KX_ConstraintActuator::KX_ACT_CONSTRAINT_LOCZ;
736                                                 min = conact->minloc[2];
737                                                 max = conact->maxloc[2];
738                                                 break;
739                                         case ACT_CONST_ROTX:
740                                                 locrot = KX_ConstraintActuator::KX_ACT_CONSTRAINT_ROTX;
741                                                 min = MT_2_PI * conact->minrot[0] / 360.0;
742                                                 max = MT_2_PI * conact->maxrot[0] / 360.0;
743                                                 break;
744                                         case ACT_CONST_ROTY:
745                                                 locrot = KX_ConstraintActuator::KX_ACT_CONSTRAINT_ROTY;
746                                                 min = MT_2_PI * conact->minrot[1] / 360.0;
747                                                 max = MT_2_PI * conact->maxrot[1] / 360.0;
748                                                 break;
749                                         case ACT_CONST_ROTZ:
750                                                 locrot = KX_ConstraintActuator::KX_ACT_CONSTRAINT_ROTZ;
751                                                 min = MT_2_PI * conact->minrot[2] / 360.0;
752                                                 max = MT_2_PI * conact->maxrot[2] / 360.0;
753                                                 break;
754                                         default:
755                                                 ; /* error */ 
756                                         }
757                                 }
758                                 KX_ConstraintActuator *tmpconact 
759                                         = new KX_ConstraintActuator(gameobj,
760                                                 conact->damp,
761                                                 conact->rotdamp,
762                                                 min,
763                                                 max,
764                                                 conact->maxrot,
765                                                 locrot,
766                                                 conact->time,
767                                                 conact->flag,
768                                                 prop);
769                                 baseact = tmpconact;
770                                 break;
771                         }
772                 case ACT_GROUP:
773                         {
774                                 // deprecated
775                         }
776                         break;
777                 case ACT_SCENE:
778                         {
779                                 bSceneActuator *sceneact = (bSceneActuator *) bact->data;
780                                 STR_String nextSceneName("");
781                                 
782                                 KX_SceneActuator* tmpsceneact;
783                                 int mode = KX_SceneActuator::KX_SCENE_NODEF;
784                                 KX_Camera *cam = NULL;
785                                 //KX_Scene* scene = NULL;
786                                 switch (sceneact->type)
787                                 {
788                                 case ACT_SCENE_RESUME:
789                                 case ACT_SCENE_SUSPEND:
790                                 case ACT_SCENE_ADD_FRONT:
791                                 case ACT_SCENE_ADD_BACK:
792                                 case ACT_SCENE_REMOVE:
793                                 case ACT_SCENE_SET:
794                                         {
795                                                 switch (sceneact->type)
796                                                 {
797                                                 case ACT_SCENE_RESUME:
798                                                         mode = KX_SceneActuator::KX_SCENE_RESUME;
799                                                         break;
800                                                 case ACT_SCENE_SUSPEND:
801                                                         mode = KX_SceneActuator::KX_SCENE_SUSPEND;
802                                                         break;
803                                                 case ACT_SCENE_ADD_FRONT:
804                                                         mode = KX_SceneActuator::KX_SCENE_ADD_FRONT_SCENE;
805                                                         break;
806                                                 case ACT_SCENE_ADD_BACK:
807                                                         mode = KX_SceneActuator::KX_SCENE_ADD_BACK_SCENE;
808                                                         break;
809                                                 case ACT_SCENE_REMOVE:
810                                                         mode = KX_SceneActuator::KX_SCENE_REMOVE_SCENE;
811                                                         break;
812                                                 case ACT_SCENE_SET:
813                                                 default:
814                                                         mode = KX_SceneActuator::KX_SCENE_SET_SCENE;
815                                                         break;
816                                                 };
817                                                 
818                                                 if (sceneact->scene)
819                                                 {
820                                                         nextSceneName = sceneact->scene->id.name + 2; // this '2' is necessary to remove prefix 'SC'
821                                                 }
822                                                 
823                                                 break;
824                                         }
825                                 case ACT_SCENE_CAMERA:
826                                         mode = KX_SceneActuator::KX_SCENE_SET_CAMERA;
827                                         if (sceneact->camera)
828                                         {
829                                                 cam = (KX_Camera*) converter->FindGameObject(sceneact->camera);
830                                         }
831                                         break;
832                                 case ACT_SCENE_RESTART:
833                                         {
834                                                 
835                                                 mode =  KX_SceneActuator::KX_SCENE_RESTART;
836                                                 break;
837                                         }
838                                 default:
839                                         ; /* flag error */
840                                 }
841                                 tmpsceneact = new KX_SceneActuator(gameobj,
842                                                 mode,
843                                                 scene,
844                                                 ketsjiEngine,
845                                                 nextSceneName,
846                                                 cam);
847                                         baseact = tmpsceneact;
848                                 break;
849                         }
850                 case ACT_GAME:
851                         {
852                                 bGameActuator *gameact = (bGameActuator *) bact->data;
853                                 KX_GameActuator* tmpgameact;
854                                 STR_String filename = maggiename;
855                                 STR_String loadinganimationname = "";
856                                 int mode = KX_GameActuator::KX_GAME_NODEF;
857                                 switch (gameact->type)
858                                 {
859                                 case ACT_GAME_LOAD:
860                                         {
861                                                 mode = KX_GameActuator::KX_GAME_LOAD;
862                                                 filename = gameact->filename;
863                                                 loadinganimationname = gameact->loadaniname;
864                                                 break;
865                                         }
866                                 case ACT_GAME_START:
867                                         {
868                                                 mode = KX_GameActuator::KX_GAME_START;
869                                                 filename = gameact->filename;
870                                                 loadinganimationname = gameact->loadaniname;
871                                                 break;
872                                         }
873                                 case ACT_GAME_RESTART:
874                                         {
875                                                 mode = KX_GameActuator::KX_GAME_RESTART;
876                                                 break;
877                                         }
878                                 case ACT_GAME_QUIT:
879                                         {
880                                                 mode = KX_GameActuator::KX_GAME_QUIT;
881                                                 break;
882                                         }
883                                 case ACT_GAME_SAVECFG:
884                                         {
885                                                 mode = KX_GameActuator::KX_GAME_SAVECFG;
886                                                 break;
887                                         }
888                                 case ACT_GAME_LOADCFG:
889                                         {
890                                                 mode = KX_GameActuator::KX_GAME_LOADCFG;
891                                                 break;
892                                         }
893                                 default:
894                                         ; /* flag error */
895                                 }
896                                         tmpgameact = new KX_GameActuator(gameobj,
897                                                 mode,
898                                                 filename,
899                                                 loadinganimationname,
900                                                 scene,
901                                                 ketsjiEngine);
902                                         baseact = tmpgameact;
903
904                                 break;
905                         }
906                 case ACT_RANDOM:
907                         {
908                                 bRandomActuator *randAct 
909                                         = (bRandomActuator *) bact->data;
910                                 
911                                 unsigned long seedArg = randAct->seed;
912                                 SCA_RandomActuator::KX_RANDOMACT_MODE modeArg 
913                                         = SCA_RandomActuator::KX_RANDOMACT_NODEF;
914                                 SCA_RandomActuator *tmprandomact;
915                                 float paraArg1 = 0.0;
916                                 float paraArg2 = 0.0;
917                                 
918                                 switch  (randAct->distribution) {
919                                 case ACT_RANDOM_BOOL_CONST:
920                                         modeArg = SCA_RandomActuator::KX_RANDOMACT_BOOL_CONST;
921                                         paraArg1 = (float) randAct->int_arg_1;
922                                         break;
923                                 case ACT_RANDOM_BOOL_UNIFORM:
924                                         modeArg = SCA_RandomActuator::KX_RANDOMACT_BOOL_UNIFORM;
925                                         break;
926                                 case ACT_RANDOM_BOOL_BERNOUILLI:
927                                         paraArg1 = randAct->float_arg_1;
928                                         modeArg = SCA_RandomActuator::KX_RANDOMACT_BOOL_BERNOUILLI;
929                                         break;
930                                 case ACT_RANDOM_INT_CONST:
931                                         modeArg = SCA_RandomActuator::KX_RANDOMACT_INT_CONST;
932                                         paraArg1 = (float) randAct->int_arg_1;
933                                         break;
934                                 case ACT_RANDOM_INT_UNIFORM:
935                                         paraArg1 = (float) randAct->int_arg_1;
936                                         paraArg2 = (float) randAct->int_arg_2;
937                                         modeArg = SCA_RandomActuator::KX_RANDOMACT_INT_UNIFORM;
938                                         break;
939                                 case ACT_RANDOM_INT_POISSON:
940                                         paraArg1 = randAct->float_arg_1;
941                                         modeArg = SCA_RandomActuator::KX_RANDOMACT_INT_POISSON;
942                                         break;
943                                 case ACT_RANDOM_FLOAT_CONST:
944                                         paraArg1 = randAct->float_arg_1;
945                                         modeArg = SCA_RandomActuator::KX_RANDOMACT_FLOAT_CONST;
946                                         break;
947                                 case ACT_RANDOM_FLOAT_UNIFORM:
948                                         paraArg1 = randAct->float_arg_1;
949                                         paraArg2 = randAct->float_arg_2;
950                                         modeArg = SCA_RandomActuator::KX_RANDOMACT_FLOAT_UNIFORM;
951                                         break;
952                                 case ACT_RANDOM_FLOAT_NORMAL:
953                                         paraArg1 = randAct->float_arg_1;
954                                         paraArg2 = randAct->float_arg_2;
955                                         modeArg = SCA_RandomActuator::KX_RANDOMACT_FLOAT_NORMAL;
956                                         break;
957                                 case ACT_RANDOM_FLOAT_NEGATIVE_EXPONENTIAL:
958                                         paraArg1 = randAct->float_arg_1;
959                                         modeArg = SCA_RandomActuator::KX_RANDOMACT_FLOAT_NEGATIVE_EXPONENTIAL;
960                                         break;
961                                 default:
962                                         ; /* error */                           
963                                 }
964                                 tmprandomact = new SCA_RandomActuator(gameobj,
965                                         seedArg,
966                                         modeArg, 
967                                         paraArg1,
968                                         paraArg2,
969                                         randAct->propname);
970                                 baseact = tmprandomact;
971                         }
972                         break;
973
974                 case ACT_VISIBILITY:
975                 {
976                         bVisibilityActuator *vis_act = (bVisibilityActuator *) bact->data;
977                         KX_VisibilityActuator * tmp_vis_act = NULL;
978                         bool v = ((vis_act->flag & ACT_VISIBILITY_INVISIBLE) != 0);
979                         bool recursive = ((vis_act->flag & ACT_VISIBILITY_RECURSIVE) != 0);
980
981                         tmp_vis_act = new KX_VisibilityActuator(gameobj, !v, recursive);
982                         
983                         baseact = tmp_vis_act;
984                 }
985                 break;
986
987                 case ACT_STATE:
988                 {
989                         bStateActuator *sta_act = (bStateActuator *) bact->data;
990                         KX_StateActuator * tmp_sta_act = NULL;
991
992                         tmp_sta_act = 
993                                 new KX_StateActuator(gameobj, sta_act->type, sta_act->mask);
994                         
995                         baseact = tmp_sta_act;
996                 }
997                 break;
998
999                 case ACT_2DFILTER:
1000                 {
1001                         bTwoDFilterActuator *_2dfilter = (bTwoDFilterActuator*) bact->data;
1002             SCA_2DFilterActuator *tmp = NULL;
1003
1004                         RAS_2DFilterManager::RAS_2DFILTER_MODE filtermode;
1005                         switch(_2dfilter->type)
1006                         {
1007                                 case ACT_2DFILTER_MOTIONBLUR:
1008                                         filtermode = RAS_2DFilterManager::RAS_2DFILTER_MOTIONBLUR;
1009                                         break;
1010                                 case ACT_2DFILTER_BLUR:
1011                                         filtermode = RAS_2DFilterManager::RAS_2DFILTER_BLUR;
1012                                         break;
1013                                 case ACT_2DFILTER_SHARPEN:
1014                                         filtermode = RAS_2DFilterManager::RAS_2DFILTER_SHARPEN;
1015                                         break;
1016                                 case ACT_2DFILTER_DILATION:
1017                                         filtermode = RAS_2DFilterManager::RAS_2DFILTER_DILATION;
1018                                         break;
1019                                 case ACT_2DFILTER_EROSION:
1020                                         filtermode = RAS_2DFilterManager::RAS_2DFILTER_EROSION;
1021                                         break;
1022                                 case ACT_2DFILTER_LAPLACIAN:
1023                                         filtermode = RAS_2DFilterManager::RAS_2DFILTER_LAPLACIAN;
1024                                         break;
1025                                 case ACT_2DFILTER_SOBEL:
1026                                         filtermode = RAS_2DFilterManager::RAS_2DFILTER_SOBEL;
1027                                         break;
1028                                 case ACT_2DFILTER_PREWITT:
1029                                         filtermode = RAS_2DFilterManager::RAS_2DFILTER_PREWITT;
1030                                         break;
1031                                 case ACT_2DFILTER_GRAYSCALE:
1032                                         filtermode = RAS_2DFilterManager::RAS_2DFILTER_GRAYSCALE;
1033                                         break;
1034                                 case ACT_2DFILTER_SEPIA:
1035                                         filtermode = RAS_2DFilterManager::RAS_2DFILTER_SEPIA;
1036                                         break;
1037                                 case ACT_2DFILTER_INVERT:
1038                                         filtermode = RAS_2DFilterManager::RAS_2DFILTER_INVERT;
1039                                         break;
1040                                 case ACT_2DFILTER_CUSTOMFILTER:
1041                                         filtermode = RAS_2DFilterManager::RAS_2DFILTER_CUSTOMFILTER;
1042                                         break;
1043                                 case ACT_2DFILTER_NOFILTER:
1044                                         filtermode = RAS_2DFilterManager::RAS_2DFILTER_NOFILTER;
1045                                         break;
1046                                 case ACT_2DFILTER_DISABLED:
1047                                         filtermode = RAS_2DFilterManager::RAS_2DFILTER_DISABLED;
1048                                         break;
1049                                 case ACT_2DFILTER_ENABLED:
1050                                         filtermode = RAS_2DFilterManager::RAS_2DFILTER_ENABLED;
1051                                         break;
1052                                 default:
1053                                         filtermode = RAS_2DFilterManager::RAS_2DFILTER_NOFILTER;
1054                                         break;
1055                         }
1056             
1057                         tmp = new SCA_2DFilterActuator(gameobj, filtermode, _2dfilter->flag,
1058                                 _2dfilter->float_arg,_2dfilter->int_arg,ketsjiEngine->GetRasterizer(),rendertools);
1059
1060                         if (_2dfilter->text)
1061                         {
1062                                 char *buf;
1063                                 // this is some blender specific code
1064                                 buf = txt_to_buf(_2dfilter->text);
1065                                 if (buf)
1066                                 {
1067                                         tmp->SetShaderText(STR_String(buf));
1068                                         MEM_freeN(buf);
1069                                 }
1070                         }
1071
1072             baseact = tmp;
1073                         
1074                 }
1075                 break;
1076                 case ACT_PARENT:
1077                         {
1078                                 bParentActuator *parAct = (bParentActuator *) bact->data;
1079                                 int mode = KX_ParentActuator::KX_PARENT_NODEF;
1080                                 KX_GameObject *tmpgob = NULL;
1081
1082                                 switch(parAct->type)
1083                                 {
1084                                         case ACT_PARENT_SET:
1085                                                 mode = KX_ParentActuator::KX_PARENT_SET;
1086                                                 tmpgob = converter->FindGameObject(parAct->ob);
1087                                                 break;
1088                                         case ACT_PARENT_REMOVE:
1089                                                 mode = KX_ParentActuator::KX_PARENT_REMOVE;
1090                                                 tmpgob = NULL;
1091                                                 break;
1092                                 }
1093         
1094                                 KX_ParentActuator *tmpparact
1095                                         = new KX_ParentActuator(gameobj,
1096                                         mode,
1097                                         tmpgob);
1098                                 baseact = tmpparact;
1099                                 break;
1100                         }
1101                 
1102                 default:
1103                         ; /* generate some error */
1104                 }
1105                 
1106                 if (baseact)
1107                 {
1108                         baseact->SetExecutePriority(executePriority++);
1109                         uniquename += "#ACT#";
1110                         uniqueint++;
1111                         CIntValue* uniqueval = new CIntValue(uniqueint);
1112                         uniquename += uniqueval->GetText();
1113                         uniqueval->Release();
1114                         baseact->SetName(STR_String(bact->name));
1115                         //gameobj->SetProperty(uniquename,baseact);
1116                         gameobj->AddActuator(baseact);
1117                         
1118                         converter->RegisterGameActuator(baseact, bact);
1119                         // done with baseact, release it
1120                         baseact->Release();
1121                 }
1122                 
1123                 bact = bact->next;
1124         }
1125 }
1126
1127