1cb15c932ac45b3d18733605d308a0885110235e
[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 #ifndef DISABLE_PYTHON
96         PyObject* pythondictionary,
97 #endif
98         int activeLayerBitInfo,
99         bool isInActiveLayer,
100         KX_BlenderSceneConverter* converter
101 ) {
102         int uniqueint=0;
103         int count = 0;
104         int executePriority=0;
105         bController* bcontr = (bController*)blenderobject->controllers.first;
106         while (bcontr)
107         {
108                 bcontr = bcontr->next;
109                 count++;
110         }
111         gameobj->ReserveController(count);
112         bcontr = (bController*)blenderobject->controllers.first;
113         while (bcontr)
114         {
115                 SCA_IController* gamecontroller = NULL;
116                 switch(bcontr->type)
117                 {
118                         case CONT_LOGIC_AND:
119                         {
120                                 gamecontroller = new SCA_ANDController(gameobj);
121                                 break;
122                         }
123                         case CONT_LOGIC_OR:
124                         {
125                                 gamecontroller = new SCA_ORController(gameobj);
126                                 break;
127                         }
128                         case CONT_LOGIC_NAND:
129                         {
130                                 gamecontroller = new SCA_NANDController(gameobj);
131                                 break;
132                         }
133                         case CONT_LOGIC_NOR:
134                         {
135                                 gamecontroller = new SCA_NORController(gameobj);
136                                 break;
137                         }
138                         case CONT_LOGIC_XOR:
139                         {
140                                 gamecontroller = new SCA_XORController(gameobj);
141                                 break;
142                         }
143                         case CONT_LOGIC_XNOR:
144                         {
145                                 gamecontroller = new SCA_XNORController(gameobj);
146                                 break;
147                         }
148                         case CONT_EXPRESSION:
149                         {
150                                 bExpressionCont* bexpcont = (bExpressionCont*) bcontr->data;
151                                 STR_String expressiontext = STR_String(bexpcont->str);
152                                 if (expressiontext.Length() > 0)
153                                 {
154                                         gamecontroller = new SCA_ExpressionController(gameobj,expressiontext);
155                                 }
156                                 break;
157                         }
158                         case CONT_PYTHON:
159                         {
160                                 bPythonCont* pycont = (bPythonCont*) bcontr->data;
161                                 SCA_PythonController* pyctrl = new SCA_PythonController(gameobj, pycont->mode);
162                                 gamecontroller = pyctrl;
163                                 
164 #ifndef DISABLE_PYTHON
165
166                                 pyctrl->SetDictionary(pythondictionary);
167                                 
168                                 if(pycont->mode==SCA_PythonController::SCA_PYEXEC_SCRIPT) {
169                                         if (pycont->text)
170                                         {
171                                                 char *buf;
172                                                 // this is some blender specific code
173                                                 buf= txt_to_buf(pycont->text);
174                                                 if (buf)
175                                                 {
176                                                         pyctrl->SetScriptText(STR_String(buf));
177                                                         pyctrl->SetScriptName(pycont->text->id.name+2);
178                                                         MEM_freeN(buf);
179                                                 }
180                                                 
181                                         }
182                                 }
183                                 else {
184                                         /* let the controller print any warnings here when importing */
185                                         pyctrl->SetScriptText(STR_String(pycont->module)); 
186                                         pyctrl->SetScriptName(pycont->module); /* will be something like module.func so using it as the name is OK */
187                                 }
188
189                                 if(pycont->flag & CONT_PY_DEBUG) {
190                                         printf("\nDebuging \"%s\", module for object %s\n\texpect worse performance.\n", pycont->module, blenderobject->id.name+2);
191                                         pyctrl->SetDebug(true);
192                                 }
193                                 
194 #endif // DISABLE_PYTHON
195
196                                 break;
197                         }
198                         default:
199                         {
200                                 
201                         }
202                 }
203
204                 if (gamecontroller)
205                 {
206                         LinkControllerToActuators(gamecontroller,bcontr,logicmgr,converter);
207                         gamecontroller->SetExecutePriority(executePriority++);
208                         gamecontroller->SetBookmark((bcontr->flag & CONT_PRIO) != 0);
209                         gamecontroller->SetState(bcontr->state_mask);
210                         STR_String uniquename = bcontr->name;
211                         uniquename += "#CONTR#";
212                         uniqueint++;
213                         CIntValue* uniqueval = new CIntValue(uniqueint);
214                         uniquename += uniqueval->GetText();
215                         uniqueval->Release();
216                         gamecontroller->SetName(uniquename);
217                         gameobj->AddController(gamecontroller);
218                         
219                         converter->RegisterGameController(gamecontroller, bcontr);
220
221 #ifndef DISABLE_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 pradictable 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 pitty because its useful to see errors at startup but cant help it */
232                                         
233                                         // pyctrl->Import();
234                                 }
235                         }
236 #endif // DISABLE_PYTHON
237
238                         //done with gamecontroller
239                         gamecontroller->Release();
240                 }
241                 
242                 bcontr = bcontr->next;
243         }
244
245 }