svn merge https://svn.blender.org/svnroot/bf-blender/trunk/blender -r22701:22704
[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., 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         PyObject* pythondictionary,
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                                 
162                                 pyctrl->SetDictionary(pythondictionary);
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                                 break;
191                         }
192                         default:
193                         {
194                                 
195                         }
196                 }
197
198                 if (gamecontroller)
199                 {
200                         LinkControllerToActuators(gamecontroller,bcontr,logicmgr,converter);
201                         gamecontroller->SetExecutePriority(executePriority++);
202                         gamecontroller->SetBookmark((bcontr->flag & CONT_PRIO) != 0);
203                         gamecontroller->SetState(bcontr->state_mask);
204                         STR_String uniquename = bcontr->name;
205                         uniquename += "#CONTR#";
206                         uniqueint++;
207                         CIntValue* uniqueval = new CIntValue(uniqueint);
208                         uniquename += uniqueval->GetText();
209                         uniqueval->Release();
210                         gamecontroller->SetName(uniquename);
211                         gameobj->AddController(gamecontroller);
212                         
213                         converter->RegisterGameController(gamecontroller, bcontr);
214                         
215                         if (bcontr->type==CONT_PYTHON) {
216                                 SCA_PythonController *pyctrl= static_cast<SCA_PythonController*>(gamecontroller);
217                                 /* not strictly needed but gives syntax errors early on and
218                                  * gives more pradictable performance for larger scripts */
219                                 if(pyctrl->m_mode==SCA_PythonController::SCA_PYEXEC_SCRIPT)
220                                         pyctrl->Compile();
221                                 else {
222                                         /* We cant do this because importing runs the script which could end up accessing
223                                          * internal BGE functions, this is unstable while we're converting the scene.
224                                          * This is a pitty because its useful to see errors at startup but cant help it */
225                                         
226                                         // pyctrl->Import();
227                                 }
228                         }
229                         
230                         //done with gamecontroller
231                         gamecontroller->Release();
232                 }
233                 
234                 bcontr = bcontr->next;
235         }
236
237 }