Blender V2.61 - r43446

KX_ConvertControllers.cpp

Go to the documentation of this file.
00001 /*
00002  * ***** BEGIN GPL LICENSE BLOCK *****
00003  *
00004  * This program is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU General Public License
00006  * as published by the Free Software Foundation; either version 2
00007  * of the License, or (at your option) any later version.
00008  *
00009  * This program is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  * GNU General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software Foundation,
00016  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00017  *
00018  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
00019  * All rights reserved.
00020  *
00021  * The Original Code is: all of this file.
00022  *
00023  * Contributor(s): none yet.
00024  *
00025  * ***** END GPL LICENSE BLOCK *****
00026  */
00027 
00033 #include "MEM_guardedalloc.h"
00034 
00035 #include "KX_BlenderSceneConverter.h"
00036 #include "KX_ConvertControllers.h"
00037 #include "KX_Python.h"
00038 
00039 // Controller
00040 #include "SCA_ANDController.h"
00041 #include "SCA_ORController.h"
00042 #include "SCA_NANDController.h"
00043 #include "SCA_NORController.h"
00044 #include "SCA_XORController.h"
00045 #include "SCA_XNORController.h"
00046 #include "SCA_PythonController.h"
00047 #include "SCA_ExpressionController.h"
00048 
00049 #include "SCA_LogicManager.h"
00050 #include "KX_GameObject.h"
00051 #include "IntValue.h"
00052 
00053 /* This little block needed for linking to Blender... */
00054 #ifdef WIN32
00055 #include "BLI_winstuff.h"
00056 #endif
00057 
00058 #include "DNA_object_types.h"
00059 #include "DNA_controller_types.h"
00060 #include "DNA_text_types.h"
00061 
00062 #include "BKE_text.h"
00063 
00064 #include "BLI_blenlib.h"
00065 
00066 /* end of blender include block */
00067 
00068 
00069     static void
00070 LinkControllerToActuators(
00071     SCA_IController *game_controller,
00072     bController* bcontr,    
00073     SCA_LogicManager* logicmgr,
00074     KX_BlenderSceneConverter* converter
00075 ) {
00076     // Iterate through the actuators of the game blender
00077     // controller and find the corresponding ketsji actuator.
00078 
00079     game_controller->ReserveActuator(bcontr->totlinks);
00080     for (int i=0;i<bcontr->totlinks;i++)
00081     {
00082         bActuator* bact = (bActuator*) bcontr->links[i];
00083         SCA_IActuator *game_actuator = converter->FindGameActuator(bact);
00084         if (game_actuator) {
00085             logicmgr->RegisterToActuator(game_controller, game_actuator);
00086         }
00087     }
00088 }
00089 
00090 
00091 void BL_ConvertControllers(
00092     struct Object* blenderobject,
00093     class KX_GameObject* gameobj,
00094     SCA_LogicManager* logicmgr,
00095     int activeLayerBitInfo,
00096     bool isInActiveLayer,
00097     KX_BlenderSceneConverter* converter
00098 ) {
00099     int uniqueint=0;
00100     int count = 0;
00101     int executePriority=0;
00102     bController* bcontr = (bController*)blenderobject->controllers.first;
00103     while (bcontr)
00104     {
00105         bcontr = bcontr->next;
00106         count++;
00107     }
00108     gameobj->ReserveController(count);
00109     bcontr = (bController*)blenderobject->controllers.first;
00110     while (bcontr)
00111     {
00112         SCA_IController* gamecontroller = NULL;
00113         switch(bcontr->type)
00114         {
00115             case CONT_LOGIC_AND:
00116             {
00117                 gamecontroller = new SCA_ANDController(gameobj);
00118                 break;
00119             }
00120             case CONT_LOGIC_OR:
00121             {
00122                 gamecontroller = new SCA_ORController(gameobj);
00123                 break;
00124             }
00125             case CONT_LOGIC_NAND:
00126             {
00127                 gamecontroller = new SCA_NANDController(gameobj);
00128                 break;
00129             }
00130             case CONT_LOGIC_NOR:
00131             {
00132                 gamecontroller = new SCA_NORController(gameobj);
00133                 break;
00134             }
00135             case CONT_LOGIC_XOR:
00136             {
00137                 gamecontroller = new SCA_XORController(gameobj);
00138                 break;
00139             }
00140             case CONT_LOGIC_XNOR:
00141             {
00142                 gamecontroller = new SCA_XNORController(gameobj);
00143                 break;
00144             }
00145             case CONT_EXPRESSION:
00146             {
00147                 bExpressionCont* bexpcont = (bExpressionCont*) bcontr->data;
00148                 STR_String expressiontext = STR_String(bexpcont->str);
00149                 if (expressiontext.Length() > 0)
00150                 {
00151                     gamecontroller = new SCA_ExpressionController(gameobj,expressiontext);
00152                 }
00153                 break;
00154             }
00155             case CONT_PYTHON:
00156             {
00157                 bPythonCont* pycont = (bPythonCont*) bcontr->data;
00158                 SCA_PythonController* pyctrl = new SCA_PythonController(gameobj, pycont->mode);
00159                 gamecontroller = pyctrl;
00160 #ifdef WITH_PYTHON
00161 
00162                 pyctrl->SetNamespace(converter->GetPyNamespace());
00163                 
00164                 if(pycont->mode==SCA_PythonController::SCA_PYEXEC_SCRIPT) {
00165                     if (pycont->text)
00166                     {
00167                         char *buf;
00168                         // this is some blender specific code
00169                         buf= txt_to_buf(pycont->text);
00170                         if (buf)
00171                         {
00172                             pyctrl->SetScriptText(STR_String(buf));
00173                             pyctrl->SetScriptName(pycont->text->id.name+2);
00174                             MEM_freeN(buf);
00175                         }
00176                         
00177                     }
00178                 }
00179                 else {
00180                     /* let the controller print any warnings here when importing */
00181                     pyctrl->SetScriptText(STR_String(pycont->module)); 
00182                     pyctrl->SetScriptName(pycont->module); /* will be something like module.func so using it as the name is OK */
00183 
00184                     if(pycont->flag & CONT_PY_DEBUG) {
00185                         printf("\nDebuging \"%s\", module for object %s\n\texpect worse performance.\n", pycont->module, blenderobject->id.name+2);
00186                         pyctrl->SetDebug(true);
00187                     }
00188                 }
00189                 
00190 #endif // WITH_PYTHON
00191 
00192                 break;
00193             }
00194             default:
00195             {
00196                 
00197             }
00198         }
00199 
00200         if (gamecontroller)
00201         {
00202             LinkControllerToActuators(gamecontroller,bcontr,logicmgr,converter);
00203             gamecontroller->SetExecutePriority(executePriority++);
00204             gamecontroller->SetBookmark((bcontr->flag & CONT_PRIO) != 0);
00205             gamecontroller->SetState(bcontr->state_mask);
00206             STR_String uniquename = bcontr->name;
00207             uniquename += "#CONTR#";
00208             uniqueint++;
00209             CIntValue* uniqueval = new CIntValue(uniqueint);
00210             uniquename += uniqueval->GetText();
00211             uniqueval->Release();
00212             //unique name was never implemented for sensors and actuators, only for controllers
00213             //and it's producing difference in the keys for the lists: obj.controllers/sensors/actuators
00214             //at some point it should either be implemented globally (and saved as a separate var) or removed.
00215             //gamecontroller->SetName(uniquename);
00216             gamecontroller->SetName(bcontr->name);
00217             gameobj->AddController(gamecontroller);
00218             
00219             converter->RegisterGameController(gamecontroller, bcontr);
00220 
00221 #ifdef WITH_PYTHON
00222             if (bcontr->type==CONT_PYTHON) {
00223                 SCA_PythonController *pyctrl= static_cast<SCA_PythonController*>(gamecontroller);
00224                 /* not strictly needed but gives syntax errors early on and
00225                  * gives more predictable performance for larger scripts */
00226                 if(pyctrl->m_mode==SCA_PythonController::SCA_PYEXEC_SCRIPT)
00227                     pyctrl->Compile();
00228                 else {
00229                     /* We cant do this because importing runs the script which could end up accessing
00230                      * internal BGE functions, this is unstable while we're converting the scene.
00231                      * This is a pitty because its useful to see errors at startup but cant help it */
00232                     
00233                     // pyctrl->Import();
00234                 }
00235             }
00236 #endif // WITH_PYTHON
00237 
00238             //done with gamecontroller
00239             gamecontroller->Release();
00240         }
00241         
00242         bcontr = bcontr->next;
00243     }
00244 
00245 }