Merged 15170:15635 from trunk (no conflicts or even merges)
[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         for (int i=0;i<bcontr->totlinks;i++)
80         {
81                 bActuator* bact = (bActuator*) bcontr->links[i];
82                 SCA_IActuator *game_actuator = converter->FindGameActuator(bact);
83                 if (game_actuator) {
84                         logicmgr->RegisterToActuator(game_controller, game_actuator);
85                 }
86         }
87 }
88
89
90 void BL_ConvertControllers(
91         struct Object* blenderobject,
92         class KX_GameObject* gameobj,
93         SCA_LogicManager* logicmgr, 
94         PyObject* pythondictionary,
95         int &executePriority,
96         int activeLayerBitInfo,
97         bool isInActiveLayer,
98         KX_BlenderSceneConverter* converter
99 ) {
100         int uniqueint=0;
101         bController* bcontr = (bController*)blenderobject->controllers.first;
102         while (bcontr)
103         {
104                 SCA_IController* gamecontroller = NULL;
105                 switch(bcontr->type)
106                 {
107                         case CONT_LOGIC_AND:
108                         {
109                                 gamecontroller = new SCA_ANDController(gameobj);
110                                 LinkControllerToActuators(gamecontroller,bcontr,logicmgr,converter);
111                                 break;
112                         }
113                         case CONT_LOGIC_OR:
114                         {
115                                 gamecontroller = new SCA_ORController(gameobj);
116                                 LinkControllerToActuators(gamecontroller,bcontr,logicmgr,converter);
117                                 break;
118                         }
119                         case CONT_LOGIC_NAND:
120                         {
121                                 gamecontroller = new SCA_NANDController(gameobj);
122                                 LinkControllerToActuators(gamecontroller,bcontr,logicmgr,converter);
123                                 break;
124                         }
125                         case CONT_LOGIC_NOR:
126                         {
127                                 gamecontroller = new SCA_NORController(gameobj);
128                                 LinkControllerToActuators(gamecontroller,bcontr,logicmgr,converter);
129                                 break;
130                         }
131                         case CONT_LOGIC_XOR:
132                         {
133                                 gamecontroller = new SCA_XORController(gameobj);
134                                 LinkControllerToActuators(gamecontroller,bcontr,logicmgr,converter);
135                                 break;
136                         }
137                         case CONT_LOGIC_XNOR:
138                         {
139                                 gamecontroller = new SCA_XNORController(gameobj);
140                                 LinkControllerToActuators(gamecontroller,bcontr,logicmgr,converter);
141                                 break;
142                         }
143                         case CONT_EXPRESSION:
144                         {
145                                 bExpressionCont* bexpcont = (bExpressionCont*) bcontr->data;
146                                 STR_String expressiontext = STR_String(bexpcont->str);
147                                 if (expressiontext.Length() > 0)
148                                 {
149                                         gamecontroller = new SCA_ExpressionController(gameobj,expressiontext);
150                                         LinkControllerToActuators(gamecontroller,bcontr,logicmgr,converter);
151
152                                 }
153                                 break;
154                         }
155                         case CONT_PYTHON:
156                         {
157                                         
158                                 // we should create a Python controller here
159                                                         
160                                 SCA_PythonController* pyctrl = new SCA_PythonController(gameobj);
161                                 gamecontroller = pyctrl;
162                                         
163                                 bPythonCont* pycont = (bPythonCont*) bcontr->data;
164                                 pyctrl->SetDictionary(pythondictionary);
165                                         
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                                 LinkControllerToActuators(gamecontroller,bcontr,logicmgr,converter);
181                                 break;
182                         }
183                         default:
184                         {
185                                 
186                         }
187                 }
188
189                 if (gamecontroller)
190                 {
191                         gamecontroller->SetExecutePriority(executePriority++);
192                         gamecontroller->SetState(bcontr->state_mask);
193                         STR_String uniquename = bcontr->name;
194                         uniquename += "#CONTR#";
195                         uniqueint++;
196                         CIntValue* uniqueval = new CIntValue(uniqueint);
197                         uniquename += uniqueval->GetText();
198                         uniqueval->Release();
199                         gamecontroller->SetName(uniquename);
200                         gameobj->AddController(gamecontroller);
201                         
202                         converter->RegisterGameController(gamecontroller, bcontr);
203                         //done with gamecontroller
204                         gamecontroller->Release();
205                 }
206                 
207                 bcontr = bcontr->next;
208         }
209
210 }