- rather then passing the python namespace dictionary to the controller function...
[blender-staging.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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, 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 #include "MEM_guardedalloc.h"
30
31 #include "KX_BlenderSceneConverter.h"
32 #include "KX_ConvertControllers.h"
33 #include "KX_Python.h"
34
35 // Controller
36 #include "SCA_ANDController.h"
37 #include "SCA_ORController.h"
38 #include "SCA_NANDController.h"
39 #include "SCA_NORController.h"
40 #include "SCA_XORController.h"
41 #include "SCA_XNORController.h"
42 #include "SCA_PythonController.h"
43 #include "SCA_ExpressionController.h"
44
45 #include "SCA_LogicManager.h"
46 #include "KX_GameObject.h"
47 #include "IntValue.h"
48
49 #ifdef HAVE_CONFIG_H
50 #include <config.h>
51 #endif
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                 {
115                         case CONT_LOGIC_AND:
116                         {
117                                 gamecontroller = new SCA_ANDController(gameobj);
118                                 break;
119                         }
120                         case CONT_LOGIC_OR:
121                         {
122                                 gamecontroller = new SCA_ORController(gameobj);
123                                 break;
124                         }
125                         case CONT_LOGIC_NAND:
126                         {
127                                 gamecontroller = new SCA_NANDController(gameobj);
128                                 break;
129                         }
130                         case CONT_LOGIC_NOR:
131                         {
132                                 gamecontroller = new SCA_NORController(gameobj);
133                                 break;
134                         }
135                         case CONT_LOGIC_XOR:
136                         {
137                                 gamecontroller = new SCA_XORController(gameobj);
138                                 break;
139                         }
140                         case CONT_LOGIC_XNOR:
141                         {
142                                 gamecontroller = new SCA_XNORController(gameobj);
143                                 break;
144                         }
145                         case CONT_EXPRESSION:
146                         {
147                                 bExpressionCont* bexpcont = (bExpressionCont*) bcontr->data;
148                                 STR_String expressiontext = STR_String(bexpcont->str);
149                                 if (expressiontext.Length() > 0)
150                                 {
151                                         gamecontroller = new SCA_ExpressionController(gameobj,expressiontext);
152                                 }
153                                 break;
154                         }
155                         case CONT_PYTHON:
156                         {
157                                 bPythonCont* pycont = (bPythonCont*) bcontr->data;
158                                 SCA_PythonController* pyctrl = new SCA_PythonController(gameobj, pycont->mode);
159                                 gamecontroller = pyctrl;
160 #ifndef DISABLE_PYTHON
161
162                                 pyctrl->SetNamespace(converter->GetPyNamespace());
163                                 
164                                 if(pycont->mode==SCA_PythonController::SCA_PYEXEC_SCRIPT) {
165                                         if (pycont->text)
166                                         {
167                                                 char *buf;
168                                                 // this is some blender specific code
169                                                 buf= txt_to_buf(pycont->text);
170                                                 if (buf)
171                                                 {
172                                                         pyctrl->SetScriptText(STR_String(buf));
173                                                         pyctrl->SetScriptName(pycont->text->id.name+2);
174                                                         MEM_freeN(buf);
175                                                 }
176                                                 
177                                         }
178                                 }
179                                 else {
180                                         /* let the controller print any warnings here when importing */
181                                         pyctrl->SetScriptText(STR_String(pycont->module)); 
182                                         pyctrl->SetScriptName(pycont->module); /* will be something like module.func so using it as the name is OK */
183                                 }
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 #endif // DISABLE_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                         gamecontroller->SetName(uniquename);
213                         gameobj->AddController(gamecontroller);
214                         
215                         converter->RegisterGameController(gamecontroller, bcontr);
216
217 #ifndef DISABLE_PYTHON
218                         if (bcontr->type==CONT_PYTHON) {
219                                 SCA_PythonController *pyctrl= static_cast<SCA_PythonController*>(gamecontroller);
220                                 /* not strictly needed but gives syntax errors early on and
221                                  * gives more pradictable performance for larger scripts */
222                                 if(pyctrl->m_mode==SCA_PythonController::SCA_PYEXEC_SCRIPT)
223                                         pyctrl->Compile();
224                                 else {
225                                         /* We cant do this because importing runs the script which could end up accessing
226                                          * internal BGE functions, this is unstable while we're converting the scene.
227                                          * This is a pitty because its useful to see errors at startup but cant help it */
228                                         
229                                         // pyctrl->Import();
230                                 }
231                         }
232 #endif // DISABLE_PYTHON
233
234                         //done with gamecontroller
235                         gamecontroller->Release();
236                 }
237                 
238                 bcontr = bcontr->next;
239         }
240
241 }