BGE: Some various changes to make moving the character physics type easier:
[blender.git] / source / gameengine / Converter / KX_ConvertControllers.cpp
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
19  * All rights reserved.
20  *
21  * The Original Code is: all of this file.
22  *
23  * Contributor(s): none yet.
24  *
25  * ***** END GPL LICENSE BLOCK *****
26  */
27
28 /** \file gameengine/Converter/KX_ConvertControllers.cpp
29  *  \ingroup bgeconv
30  */
31
32
33 #include "MEM_guardedalloc.h"
34
35 #include "KX_BlenderSceneConverter.h"
36 #include "KX_ConvertControllers.h"
37 #include "KX_Python.h"
38
39 // Controller
40 #include "SCA_ANDController.h"
41 #include "SCA_ORController.h"
42 #include "SCA_NANDController.h"
43 #include "SCA_NORController.h"
44 #include "SCA_XORController.h"
45 #include "SCA_XNORController.h"
46 #include "SCA_PythonController.h"
47 #include "SCA_ExpressionController.h"
48
49 #include "SCA_LogicManager.h"
50 #include "KX_GameObject.h"
51 #include "IntValue.h"
52
53 /* This little block needed for linking to Blender... */
54 #ifdef WIN32
55 #include "BLI_winstuff.h"
56 #endif
57
58 #include "DNA_object_types.h"
59 #include "DNA_controller_types.h"
60 #include "DNA_text_types.h"
61
62 #include "BKE_text.h"
63
64 #include "BLI_blenlib.h"
65
66 /* end of blender include block */
67
68
69         static void
70 LinkControllerToActuators(
71         SCA_IController *game_controller,
72         bController* bcontr,
73         SCA_LogicManager* logicmgr,
74         KX_BlenderSceneConverter* converter
75 ) {
76         // Iterate through the actuators of the game blender
77         // controller and find the corresponding ketsji actuator.
78
79         game_controller->ReserveActuator(bcontr->totlinks);
80         for (int i=0;i<bcontr->totlinks;i++)
81         {
82                 bActuator* bact = (bActuator*) bcontr->links[i];
83                 SCA_IActuator *game_actuator = converter->FindGameActuator(bact);
84                 if (game_actuator) {
85                         logicmgr->RegisterToActuator(game_controller, game_actuator);
86                 }
87         }
88 }
89
90
91 void BL_ConvertControllers(
92         struct Object* blenderobject,
93         class KX_GameObject* gameobj,
94         SCA_LogicManager* logicmgr,
95         int activeLayerBitInfo,
96         bool isInActiveLayer,
97         KX_BlenderSceneConverter* converter
98 ) {
99         int uniqueint=0;
100         int count = 0;
101         int executePriority=0;
102         bController* bcontr = (bController*)blenderobject->controllers.first;
103         while (bcontr)
104         {
105                 bcontr = bcontr->next;
106                 count++;
107         }
108         gameobj->ReserveController(count);
109         bcontr = (bController*)blenderobject->controllers.first;
110         while (bcontr)
111         {
112                 SCA_IController* gamecontroller = NULL;
113                 switch (bcontr->type) {
114                         case CONT_LOGIC_AND:
115                         {
116                                 gamecontroller = new SCA_ANDController(gameobj);
117                                 break;
118                         }
119                         case CONT_LOGIC_OR:
120                         {
121                                 gamecontroller = new SCA_ORController(gameobj);
122                                 break;
123                         }
124                         case CONT_LOGIC_NAND:
125                         {
126                                 gamecontroller = new SCA_NANDController(gameobj);
127                                 break;
128                         }
129                         case CONT_LOGIC_NOR:
130                         {
131                                 gamecontroller = new SCA_NORController(gameobj);
132                                 break;
133                         }
134                         case CONT_LOGIC_XOR:
135                         {
136                                 gamecontroller = new SCA_XORController(gameobj);
137                                 break;
138                         }
139                         case CONT_LOGIC_XNOR:
140                         {
141                                 gamecontroller = new SCA_XNORController(gameobj);
142                                 break;
143                         }
144                         case CONT_EXPRESSION:
145                         {
146                                 bExpressionCont* bexpcont = (bExpressionCont*) bcontr->data;
147                                 STR_String expressiontext = STR_String(bexpcont->str);
148                                 if (expressiontext.Length() > 0)
149                                 {
150                                         gamecontroller = new SCA_ExpressionController(gameobj,expressiontext);
151                                 }
152                                 break;
153                         }
154                         case CONT_PYTHON:
155                         {
156                                 bPythonCont* pycont = (bPythonCont*) bcontr->data;
157                                 SCA_PythonController* pyctrl = new SCA_PythonController(gameobj, pycont->mode);
158                                 gamecontroller = pyctrl;
159 #ifdef WITH_PYTHON
160                                 PyGILState_STATE gstate = PyGILState_Ensure();
161                                 pyctrl->SetNamespace(converter->GetPyNamespace());
162                                 
163                                 if (pycont->mode==SCA_PythonController::SCA_PYEXEC_SCRIPT) {
164                                         if (pycont->text)
165                                         {
166                                                 char *buf;
167                                                 // this is some blender specific code
168                                                 buf= txt_to_buf(pycont->text);
169                                                 if (buf)
170                                                 {
171                                                         pyctrl->SetScriptText(STR_String(buf));
172                                                         pyctrl->SetScriptName(pycont->text->id.name+2);
173                                                         MEM_freeN(buf);
174                                                 }
175                                                 
176                                         }
177                                 }
178                                 else {
179                                         /* let the controller print any warnings here when importing */
180                                         pyctrl->SetScriptText(STR_String(pycont->module)); 
181                                         pyctrl->SetScriptName(pycont->module); /* will be something like module.func so using it as the name is OK */
182
183                                         if (pycont->flag & CONT_PY_DEBUG) {
184                                                 printf("\nDebuging \"%s\", module for object %s\n\texpect worse performance.\n", pycont->module, blenderobject->id.name+2);
185                                                 pyctrl->SetDebug(true);
186                                         }
187                                 }
188                                 
189                                 PyGILState_Release(gstate);
190 #endif // WITH_PYTHON
191
192                                 break;
193                         }
194                         default:
195                         {
196                                 
197                         }
198                 }
199
200                 if (gamecontroller)
201                 {
202                         LinkControllerToActuators(gamecontroller,bcontr,logicmgr,converter);
203                         gamecontroller->SetExecutePriority(executePriority++);
204                         gamecontroller->SetBookmark((bcontr->flag & CONT_PRIO) != 0);
205                         gamecontroller->SetState(bcontr->state_mask);
206                         STR_String uniquename = bcontr->name;
207                         uniquename += "#CONTR#";
208                         uniqueint++;
209                         CIntValue* uniqueval = new CIntValue(uniqueint);
210                         uniquename += uniqueval->GetText();
211                         uniqueval->Release();
212                         //unique name was never implemented for sensors and actuators, only for controllers
213                         //and it's producing difference in the keys for the lists: obj.controllers/sensors/actuators
214                         //at some point it should either be implemented globally (and saved as a separate var) or removed.
215                         //gamecontroller->SetName(uniquename);
216                         gamecontroller->SetName(bcontr->name);
217                         gameobj->AddController(gamecontroller);
218                         
219                         converter->RegisterGameController(gamecontroller, bcontr);
220
221 #ifdef WITH_PYTHON
222                         PyGILState_STATE gstate = PyGILState_Ensure();
223                         if (bcontr->type==CONT_PYTHON) {
224                                 SCA_PythonController *pyctrl= static_cast<SCA_PythonController*>(gamecontroller);
225                                 /* not strictly needed but gives syntax errors early on and
226                                  * gives more predictable performance for larger scripts */
227                                 if (pyctrl->m_mode==SCA_PythonController::SCA_PYEXEC_SCRIPT)
228                                         pyctrl->Compile();
229                                 else {
230                                         /* We cant do this because importing runs the script which could end up accessing
231                                          * internal BGE functions, this is unstable while we're converting the scene.
232                                          * This is a pity because its useful to see errors at startup but cant help it */
233                                         
234                                         // pyctrl->Import();
235                                 }
236                         }
237
238                         PyGILState_Release(gstate);
239 #endif // WITH_PYTHON
240
241                         //done with gamecontroller
242                         gamecontroller->Release();
243                 }
244                 
245                 bcontr = bcontr->next;
246         }
247
248 }