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