[GameEngine] Commit all Kester's changes made to the gameengine to restore 2.25 like...
[blender-staging.git] / source / gameengine / Converter / KX_BlenderSceneConverter.cpp
1 /**
2  * $Id$
3  * ***** BEGIN GPL/BL DUAL 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. The Blender
9  * Foundation also sells licenses for use in proprietary software under
10  * the Blender License.  See http://www.blender.org/BL/ for information
11  * about this.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software Foundation,
20  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
21  *
22  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
23  * All rights reserved.
24  *
25  * The Original Code is: all of this file.
26  *
27  * Contributor(s): none yet.
28  *
29  * ***** END GPL/BL DUAL LICENSE BLOCK *****
30  */
31
32 #ifdef HAVE_CONFIG_H
33 #include <config.h>
34 #endif
35
36 #ifdef WIN32
37         #pragma warning (disable:4786) // suppress stl-MSVC debug info warning
38 #endif
39
40
41 #include "KX_Scene.h"
42 #include "KX_GameObject.h"
43 #include "KX_IpoConvert.h"
44 #include "RAS_MeshObject.h"
45 #include "KX_PhysicsEngineEnums.h"
46
47 #include "DummyPhysicsEnvironment.h"
48
49 //to decide to use sumo/ode or dummy physics - defines USE_ODE
50 #include "KX_ConvertPhysicsObject.h"
51
52 #ifdef USE_ODE
53 #include "OdePhysicsEnvironment.h"
54 #endif //USE_ODE
55
56 #ifdef USE_SUMO_SOLID
57 #include "SumoPhysicsEnvironment.h"
58 #endif
59
60 #include "KX_BlenderSceneConverter.h"
61 #include "KX_BlenderScalarInterpolator.h"
62 #include "BL_BlenderDataConversion.h"
63 #include "BlenderWorldInfo.h"
64 #include "KX_Scene.h"
65
66 /* This little block needed for linking to Blender... */
67 #ifdef WIN32
68 #include "BLI_winstuff.h"
69 #endif
70
71 /* This list includes only data type definitions */
72 #include "DNA_scene_types.h"
73 #include "DNA_world_types.h"
74 #include "BKE_main.h"
75
76
77 KX_BlenderSceneConverter::KX_BlenderSceneConverter(
78                                                         struct Main* maggie,
79                                                         class KX_KetsjiEngine* engine
80                                                         )
81                                                         : m_maggie(maggie),
82                                                         m_ketsjiEngine(engine),
83                                                         m_alwaysUseExpandFraming(false)
84 {
85         m_newfilename = "";
86 }
87
88
89 KX_BlenderSceneConverter::~KX_BlenderSceneConverter()
90 {
91         // clears meshes, and hashmaps from blender to gameengine data
92         int i;
93         // delete sumoshapes
94         
95
96         int numipolists = m_map_blender_to_gameipolist.size();
97         for (i=0; i<numipolists; i++) {
98                 BL_InterpolatorList *ipoList= *m_map_blender_to_gameipolist.at(i);
99
100                 delete (ipoList);
101         }
102
103         vector<KX_WorldInfo*>::iterator itw = m_worldinfos.begin();
104         while (itw != m_worldinfos.end()) {
105                 delete (*itw);
106                 itw++;
107         }
108
109         vector<RAS_IPolyMaterial*>::iterator itp = m_polymaterials.begin();
110         while (itp != m_polymaterials.end()) {
111                 delete (*itp);
112                 itp++;
113         }
114         
115         vector<RAS_MeshObject*>::iterator itm = m_meshobjects.begin();
116         while (itm != m_meshobjects.end()) {
117                 delete (*itm);
118                 itm++;
119         }
120 }
121
122
123
124 void KX_BlenderSceneConverter::SetNewFileName(const STR_String& filename)
125 {
126         m_newfilename = filename;
127 }
128
129
130
131 bool KX_BlenderSceneConverter::TryAndLoadNewFile()
132 {
133         bool result = false;
134
135         // find the file
136 /*      if ()
137         {
138                 result = true;
139         }
140         // if not, clear the newfilename
141         else
142         {
143                 m_newfilename = "";     
144         }
145 */
146         return result;
147 }
148
149
150
151         /**
152          * Find the specified scene by name, or the first
153          * scene if nothing matches (shouldn't happen).
154          */
155 static struct Scene *GetSceneForName2(struct Main *maggie, const STR_String& scenename) {
156         Scene *sce;
157
158         for (sce= (Scene*) maggie->scene.first; sce; sce= (Scene*) sce->id.next)
159                 if (scenename == (sce->id.name+2))
160                         return sce;
161
162         return (Scene*) maggie->scene.first;
163 }
164
165
166 void KX_BlenderSceneConverter::ConvertScene(const STR_String& scenename,
167                                                                                         class KX_Scene* destinationscene,
168                                                                                         PyObject* dictobj,
169                                                                                         class SCA_IInputDevice* keyinputdev,
170                                                                                         class RAS_IRenderTools* rendertools,
171                                                                                         class RAS_ICanvas* canvas)
172 {
173 //find out which physics engine
174   Scene *blenderscene = GetSceneForName2(m_maggie, scenename);
175
176   e_PhysicsEngine physics_engine = UseSumo;
177
178   /* FIXME: Force physics engine = sumo.
179      This isn't really a problem - no other physics engines are available. 
180   if (blenderscene)
181         {
182           int i=0;
183                 
184           if (blenderscene->world)
185                 {
186                   switch (blenderscene->world->physicsEngine)
187                         {
188                                 
189                         case 4:
190                           {
191                                 physics_engine = UseODE;
192                                 break;
193                           }
194                         case 3:
195                           {
196                                 physics_engine = UseDynamo;
197                                 break;
198                           }
199                         case 2:
200                           {
201                                 physics_engine = UseSumo;
202                                 break;
203                           };
204                         case 0:
205                           {
206                                 physics_engine = UseNone;
207                           }
208                         }
209                   
210                 }
211         }
212 */
213   switch (physics_engine)
214         {
215
216 #ifdef USE_SUMO_SOLID
217         case UseSumo:
218                 destinationscene ->SetPhysicsEnvironment(new SumoPhysicsEnvironment());
219                 break;
220 #endif
221 #ifdef USE_ODE
222         case UseODE:
223                 destinationscene ->SetPhysicsEnvironment(new ODEPhysicsEnvironment());
224                 break;
225 #endif //USE_ODE
226
227         case UseDynamo:
228           {
229           }
230
231         case UseNone:
232           {
233           }
234         default:
235                 physics_engine = UseNone;
236                 destinationscene ->SetPhysicsEnvironment(new DummyPhysicsEnvironment());
237                 break;
238         }
239
240   BL_ConvertBlenderObjects(m_maggie,
241                                                    scenename,
242                                                    destinationscene,
243                                                    m_ketsjiEngine,
244                                                    physics_engine,
245                                                    dictobj,
246                                                    keyinputdev,
247                                                    rendertools,
248                                                    canvas,
249                                                    this,
250                                                    m_alwaysUseExpandFraming
251                                                    );
252
253   m_map_blender_to_gameactuator.clear();
254   m_map_blender_to_gamecontroller.clear();
255
256   m_map_blender_to_gameobject.clear();
257   m_map_mesh_to_gamemesh.clear();
258   m_map_gameobject_to_blender.clear();
259 }
260
261
262
263 void KX_BlenderSceneConverter::SetAlwaysUseExpandFraming(
264         bool to_what)
265 {
266         m_alwaysUseExpandFraming= to_what;
267 }
268
269         
270
271 void KX_BlenderSceneConverter::RegisterGameObject(
272                                                                         KX_GameObject *gameobject, 
273                                                                         struct Object *for_blenderobject) 
274 {
275         m_map_gameobject_to_blender.insert(CHashedPtr(gameobject),for_blenderobject);
276         m_map_blender_to_gameobject.insert(CHashedPtr(for_blenderobject),gameobject);
277 }
278
279
280
281 KX_GameObject *KX_BlenderSceneConverter::FindGameObject(
282                                                                         struct Object *for_blenderobject) 
283 {
284         KX_GameObject **obp= m_map_blender_to_gameobject[CHashedPtr(for_blenderobject)];
285         
286         return obp?*obp:NULL;
287 }
288
289
290
291 struct Object *KX_BlenderSceneConverter::FindBlenderObject(
292                                                                         KX_GameObject *for_gameobject) 
293 {
294         struct Object **obp= m_map_gameobject_to_blender[CHashedPtr(for_gameobject)];
295         
296         return obp?*obp:NULL;
297 }
298
299         
300
301 void KX_BlenderSceneConverter::RegisterGameMesh(
302                                                                         RAS_MeshObject *gamemesh,
303                                                                         struct Mesh *for_blendermesh)
304 {
305         m_map_mesh_to_gamemesh.insert(CHashedPtr(for_blendermesh),gamemesh);
306         m_meshobjects.push_back(gamemesh);
307 }
308
309
310
311 RAS_MeshObject *KX_BlenderSceneConverter::FindGameMesh(
312                                                                         struct Mesh *for_blendermesh,
313                                                                         unsigned int onlayer)
314 {
315         RAS_MeshObject** meshp = m_map_mesh_to_gamemesh[CHashedPtr(for_blendermesh)];
316         
317         if (meshp && onlayer==(*meshp)->GetLightLayer()) {
318                 return *meshp;
319         } else {
320                 return NULL;
321         }
322 }
323
324         
325
326
327         
328
329 void KX_BlenderSceneConverter::RegisterPolyMaterial(RAS_IPolyMaterial *polymat)
330 {
331         m_polymaterials.push_back(polymat);
332 }
333
334
335
336 void KX_BlenderSceneConverter::RegisterInterpolatorList(
337                                                                         BL_InterpolatorList *ipoList,
338                                                                         struct Ipo *for_ipo)
339 {
340         m_map_blender_to_gameipolist.insert(CHashedPtr(for_ipo), ipoList);
341 }
342
343
344
345 BL_InterpolatorList *KX_BlenderSceneConverter::FindInterpolatorList(
346                                                                         struct Ipo *for_ipo)
347 {
348         BL_InterpolatorList **listp = m_map_blender_to_gameipolist[CHashedPtr(for_ipo)];
349                 
350         return listp?*listp:NULL;
351 }
352
353
354
355 void KX_BlenderSceneConverter::RegisterGameActuator(
356                                                                         SCA_IActuator *act,
357                                                                         struct bActuator *for_actuator)
358 {
359         m_map_blender_to_gameactuator.insert(CHashedPtr(for_actuator), act);
360 }
361
362
363
364 SCA_IActuator *KX_BlenderSceneConverter::FindGameActuator(
365                                                                         struct bActuator *for_actuator)
366 {
367         SCA_IActuator **actp = m_map_blender_to_gameactuator[CHashedPtr(for_actuator)];
368         
369         return actp?*actp:NULL;
370 }
371
372
373
374 void KX_BlenderSceneConverter::RegisterGameController(
375                                                                         SCA_IController *cont,
376                                                                         struct bController *for_controller)
377 {
378         m_map_blender_to_gamecontroller.insert(CHashedPtr(for_controller), cont);
379 }
380
381
382
383 SCA_IController *KX_BlenderSceneConverter::FindGameController(
384                                                                         struct bController *for_controller)
385 {
386         SCA_IController **contp = m_map_blender_to_gamecontroller[CHashedPtr(for_controller)];
387         
388         return contp?*contp:NULL;
389 }
390
391
392
393 void KX_BlenderSceneConverter::RegisterWorldInfo(
394                                                                         KX_WorldInfo *worldinfo)
395 {
396         m_worldinfos.push_back(worldinfo);
397 }