summaryrefslogblamecommitdiffstats
path: root/game/code/render/Loaders/DynaPhysLoader.cpp
blob: 018d98710462d611d02a7dee267b6abb687d2f42 (plain) (tree)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438





















































































































































































































































































































































































































































                                                                                                                                                     
//========================================================================
// Copyright (C) 2002 Radical Entertainment Ltd.  All rights reserved.
//
// File:        DynaPhysLoader.cpp
//
// Description: Implementation for DynaPhysLoader class.
//
// History:     Implemented	                         --Devin [5/27/2002]
//========================================================================

//========================================
// System Includes
//========================================
#include <simcollision/collisionobject.hpp>
#include <p3d/chunkfile.hpp>
#include <p3d/utility.hpp>

//========================================
// Project Includes
//========================================
#include <render/Loaders/DynaPhysLoader.h>
#include <render/Loaders/GeometryWrappedLoader.h>
#include <render/Loaders/AllWrappers.h>
#include <render/DSG/InstDynaPhysDSG.h>
#include <constants/srrchunks.h>
#include <constants/chunkids.hpp>
#include <constants/blobshadownames.h>
#include <memory/srrmemory.h>

#include <atc/atcmanager.h>

#ifndef RAD_RELEASE
#include <memory/propstats.h>
#endif


//************************************************************************
//
// Global Data, Local Data, Local Classes
//
//************************************************************************


//************************************************************************
//
// Public Member Functions : DynaPhysLoader Interface
//
//************************************************************************
//========================================================================
// DynaPhysLoader::
//========================================================================
//
// Description: 
//
// Parameters:  None.
//
// Return:      None.
//
// Constraints: None.
//
//========================================================================
DynaPhysLoader::DynaPhysLoader() :
tSimpleChunkHandler(SRR2::ChunkID::DYNA_PHYS_DSG)
{
    HeapMgr()->PushHeap( GMA_PERSISTENT );

    mpCollObjLoader = new sim::CollisionObjectLoader();
    mpCollObjLoader->AddRef();

    mpPhysObjLoader = new sim::PhysicsObjectLoader();
    mpPhysObjLoader->AddRef();

    mpCompDLoader = new tCompositeDrawableLoader;
    mpCompDLoader->AddRef();

    HeapMgr()->PopHeap(GMA_PERSISTENT);

    mpListenerCB  = NULL;
    mUserData     = -1;
}
//========================================================================
// DynaPhysLoader::
//========================================================================
//
// Description: 
//
// Parameters:  None.
//
// Return:      None.
//
// Constraints: None.
//
//========================================================================
DynaPhysLoader::~DynaPhysLoader()
{
    mpCollObjLoader->Release();
    mpPhysObjLoader->Release();
    mpCompDLoader->Release();
}

///////////////////////////////////////////////////////////////////////
// tSimpleChunkHandler
///////////////////////////////////////////////////////////////////////
//========================================================================
// DynaPhysLoader::
//========================================================================
//
// Description: 
//
// Parameters:  None.
//
// Return:      None.
//
// Constraints: None.
//
//========================================================================
tEntity* DynaPhysLoader::LoadObject(tChunkFile* f, tEntityStore* store)
{
    IEntityDSG::msDeletionsSafe=true;
    char objName[128];
    char name[128];
    f->GetPString(objName);

#ifndef RAD_RELEASE
    PropStats::StartTracking( objName );
#endif

    bool test = tName::MakeUID(objName) == tName::MakeUID("l1_streetlamp_Shape");
    static bool doTestOnce = true;

	// Lets see if theis object has a shadow associated with it
	const char* pShadowName = BlobbyShadowNames::FindShadowName( objName );
	tDrawable* pShadow;
	if ( pShadowName != NULL )
	{
		pShadow = p3d::find< tDrawable > ( pShadowName );
	}
	else
	{
		pShadow = NULL;
	}

    int instanceCount = 0;

    int version = f->GetLong();
    int HasAlpha = f->GetLong();
    //pDynaPhysDSG->SetName(name);
    
    sim::CollisionObject*   pCollObj    = NULL;
    sim::PhysicsObject*     pPhysObj    = NULL;
    tDrawable*              pDrawable   = NULL;
    sim::SimState*          pSimState   = NULL;
    CollisionAttributes*    pCollAttr   = NULL;
    bool foundInstances = false;

    InstDynaPhysDSG* pCurDynaPhysDSG = static_cast<InstDynaPhysDSG*>(GetAllWrappers()->GetGlobalEntity(tName::MakeUID(objName)));

    //new InstDynaPhysDSG;

    while(f->ChunksRemaining())
    {      
        f->BeginChunk();
        switch(f->GetCurrentID())
        {
        case SRR2::ChunkID::INSTANCES:
            {
                //Instances >> Scenegraph
                f->BeginChunk();
                //Scenegraph >> ScenegraphRoot
                f->BeginChunk();
                //ScenegraphRoot >> ScenegraphBranch
                f->BeginChunk();
                //ScenegraphBranch >> ScenegraphTransform
                f->BeginChunk();

                foundInstances = true;

                //ScenegraphTransform >> real ScenegraphTransform
                //f->BeginChunk();

                for(;f->ChunksRemaining();)
                {

                    instanceCount++;
                    f->BeginChunk();
                    
                    f->GetPString(name);
                    int numChild = f->GetLong();

                    rmt::Matrix matrix;
                    f->GetData(&matrix,16,tFile::DWORD);

                    if(pCurDynaPhysDSG == 0)
                    {
                        pCurDynaPhysDSG = new InstDynaPhysDSG();
                        rAssert(pCurDynaPhysDSG);
                        pCurDynaPhysDSG->SetName(name);
                        pCurDynaPhysDSG->mTranslucent = ( HasAlpha != 0 ) || pShadow;
                        pSimState = sim::SimState::CreateSimState(pCollObj,pPhysObj);

                        // I suppose this could have just as easily gone into LoadSetUp
                        pSimState->SetControl(sim::simAICtrl);
                        pSimState->SetTransform(matrix);

                        pCurDynaPhysDSG->LoadSetUp(pSimState, pCollAttr, matrix, pDrawable, pShadow );

                        mpListenerCB->OnChunkLoaded( pCurDynaPhysDSG, mUserData, _id );
                        pCurDynaPhysDSG = 0;
                    }
                    else
                    {
                        InstDynaPhysDSG* clone = pCurDynaPhysDSG->Clone(name, matrix);
                        mpListenerCB->OnChunkLoaded(clone, mUserData, _id);
                    }
                    f->EndChunk();
                }
                //ScenegraphBranch >> ScenegraphTransform
                f->EndChunk();
                //ScenegraphRoot >> ScenegraphBranch
                f->EndChunk();
                //Scenegraph >> ScenegraphRoot
                f->EndChunk();
                //Instances >> Scenegraph
                f->EndChunk();
            }
            break;

        case Pure3D::Mesh::MESH:
            {
                if(pCurDynaPhysDSG == 0)
                {
                    tGeometry* pGeo = (tGeometry*)((GeometryWrappedLoader*)GetAllWrappers()->mpLoader(AllWrappers::msGeometry))->LoadObject(f,store);
                    if( pGeo )
                    {
                        tRefCounted::Assign(pDrawable,(tDrawable*)pGeo);
                    }
                }
            }
            break;

        case P3D_COMPOSITE_DRAWABLE:
            {
                if(pCurDynaPhysDSG == 0)
                {
                    tCompositeDrawable* pCompD = static_cast<tCompositeDrawable*>( mpCompDLoader->LoadObject( f, store ) );
                    if( store->TestCollision( pCompD->GetUID(), pCompD ) )
                    {
                        HandleCollision( pCompD );
                        pCompD = NULL;
                    }
                    else
                    {
                        store->Store( pCompD );
                        tRefCounted::Assign(pDrawable,(tDrawable*)pCompD);
                    }
                }
            }
            break;

        case Simulation::Physics::OBJECT:
            {
                if(pCurDynaPhysDSG == 0)
                {
                    tRefCounted::Assign(pPhysObj,(sim::PhysicsObject*)mpPhysObjLoader->LoadObject(f,store));
                }
            }
            break;

        case Simulation::Collision::OBJECT:
            {
                if(pCurDynaPhysDSG == 0)
                {
                    tRefCounted::Assign(pCollObj,(sim::CollisionObject*)mpCollObjLoader->LoadObject(f, store));
                    // TBJ [7/9/2002]
                    // Added this to store collision objects in the inventory.
                    // A normal loader stores the top level chunk, in this case the StaticPhysDSG.
                    // Since we want the Collision Object in the inventory, we have to store it here.
                    //
                    if ( pCollObj )
                    {
                        if( store->TestCollision( pCollObj->GetUID(), pCollObj ) )
                        {
                            HandleCollision(pCollObj);
                            // TBJ [7/9/2002]
                            // Don't know what to do with this code?  Doesn't seem right to assign to NULL if
                            // the collObj does in fact exist.
                            // I'll take it out for now.
                            //
                            //pCollObj = NULL;
                            //return LOAD_ERROR;
                        }
                        else
                        {
                            store->Store(pCollObj);
                        }                
                    }
                }
            }
            break;

        case SRR2::ChunkID::OBJECT_ATTRIBUTES:
            {
                if(pCurDynaPhysDSG == 0)
                {
                    int classType = f->GetLong();
                    int physPropID = f->GetLong();
                    char tempsound [64];
                    f->GetString(tempsound);

                    // MS10 GREG TODO: This isn't even being used and calling it is leaking memory.
                    //

                    // we need to pass in volume for this thing to be able to set the mass properly in the PhysicsProperties
                    rAssert(pPhysObj);
                    float volume = pPhysObj->GetVolume();    

                    tRefCounted::Assign(pCollAttr,GetATCManager()->CreateCollisionAttributes(classType, physPropID, volume));
                    pCollAttr->SetSound(tempsound);
                }
            }
            break;

        default:
            break;
        } // switch
        f->EndChunk();
    } // while

    if( foundInstances == false || (test && doTestOnce) )
    {
        doTestOnce = false;
        //This must be a global entity.
        rmt::Matrix matrix;
        matrix.Identity();
        pCurDynaPhysDSG = new InstDynaPhysDSG();
        rAssert(pCurDynaPhysDSG);
        pCurDynaPhysDSG->SetName(objName);
        pCurDynaPhysDSG->mTranslucent = HasAlpha != 0;
        pSimState = sim::SimState::CreateSimState(pCollObj, pPhysObj);
        pSimState->SetControl(sim::simAICtrl);
        pSimState->SetTransform(matrix);
        pCurDynaPhysDSG->LoadSetUp(pSimState, pCollAttr, matrix, pDrawable, pShadow );
        GetAllWrappers()->AddGlobalEntity(pCurDynaPhysDSG);
        instanceCount++;
    }

    //
    // Spin Pure3D async loading.
    //
    ///p3d::loadManager->SwitchTask();
    tRefCounted::Release(pCollObj);
    tRefCounted::Release(pPhysObj);
    tRefCounted::Release(pDrawable);
    tRefCounted::Release(pCollAttr);

#ifndef RAD_RELEASE
    PropStats::StopTracking( objName, instanceCount );
#endif


    IEntityDSG::msDeletionsSafe=false;
    return NULL;
}
///////////////////////////////////////////////////////////////////////
// IWrappedLoader
///////////////////////////////////////////////////////////////////////
//========================================================================
// DynaPhysLoader::SetRegdListener
//========================================================================
//
// Description: Register a new listener/caretaker, notify old listener of 
//              severed connection.
//
// Parameters:  pListenerCB: Callback to call OnChunkLoaded
//              pUserData:   Data to pass along for filtering, etc
//
// Return:      None.
//
// Constraints: None.
//
//========================================================================
void DynaPhysLoader::SetRegdListener
(
   ChunkListenerCallback* pListenerCB,
   int iUserData 
)
{
   //
   // Follow protocol; notify old Listener, that it has been 
   // "disconnected".
   //
   if( mpListenerCB != NULL )
   {
      mpListenerCB->OnChunkLoaded( NULL, iUserData, 0 );
   }
   mpListenerCB  = pListenerCB;
   mUserData     = iUserData;
}

//========================================================================
// DynaPhysLoader::ModRegdListener
//========================================================================
//
// Description: Just fuck with the current pUserData
//
// Parameters:  None.
//
// Return:      None.
//
// Constraints: None.
//
//========================================================================
void DynaPhysLoader::ModRegdListener
( 
   ChunkListenerCallback* pListenerCB,
   int iUserData 
)
{
#if 0
   char DebugBuf[255];
   sprintf( DebugBuf, "GeometryWrappedLoader::ModRegdListener: pListenerCB %X vs mpListenerCB %X\n", pListenerCB, mpListenerCB );
   rDebugString( DebugBuf );
#endif
   rAssert( pListenerCB == mpListenerCB );

   mUserData = iUserData;
}
//************************************************************************
//
// Protected Member Functions : DynaPhysLoader 
//
//************************************************************************

//************************************************************************
//
// Private Member Functions : DynaPhysLoader 
//
//************************************************************************