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