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