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