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