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