BGE LibLoad: Allow the user to disable loading text datablocks if they want to attemp...
[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                 {
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 #ifdef WITH_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                                         if (pycont->flag & CONT_PY_DEBUG) {
185                                                 printf("\nDebuging \"%s\", module for object %s\n\texpect worse performance.\n", pycont->module, blenderobject->id.name+2);
186                                                 pyctrl->SetDebug(true);
187                                         }
188                                 }
189                                 
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                         if (bcontr->type==CONT_PYTHON) {
223                                 SCA_PythonController *pyctrl= static_cast<SCA_PythonController*>(gamecontroller);
224                                 /* not strictly needed but gives syntax errors early on and
225                                  * gives more predictable performance for larger scripts */
226                                 if (pyctrl->m_mode==SCA_PythonController::SCA_PYEXEC_SCRIPT)
227                                         pyctrl->Compile();
228                                 else {
229                                         /* We cant do this because importing runs the script which could end up accessing
230                                          * internal BGE functions, this is unstable while we're converting the scene.
231                                          * This is a pity because its useful to see errors at startup but cant help it */
232                                         
233                                         // pyctrl->Import();
234                                 }
235                         }
236 #endif // WITH_PYTHON
237
238                         //done with gamecontroller
239                         gamecontroller->Release();
240                 }
241                 
242                 bcontr = bcontr->next;
243         }
244
245 }