summaryrefslogblamecommitdiffstats
path: root/game/code/ai/actor/actordsg.cpp
blob: af93d74f903fde617f77a7d26f9feb3c7db4e5dc (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
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585








































































































































































































































































































































































































































































































































































































                                                                                                                                         
//===========================================================================
// Copyright (C) 2002 Radical Entertainment Ltd.  All rights reserved.
//
// Component:   ActorDSG
//
// Description: ActorDSG is a statepropdsg that has an id that will indicate the
//              relationship between an object and the objects that it can shoot
//
// Authors:     Michael Riegger
//
//===========================================================================

//---------------------------------------------------------------------------
// Includes
//===========================================================================

#include <ai/actor/actordsg.h>
#include <ai/actor/actormanager.h>
#include <render/breakables/breakablesmanager.h>
#include <stateprop/statepropdata.hpp>
#include <p3d/matrixstack.hpp>
#include <simcommon/simstatearticulated.hpp>
#include <simphysics/articulatedphysicsobject.hpp>
#include <debug/profiler.h>
#include <worldsim/coins/coinmanager.h>
#include <render/intersectmanager/intersectmanager.h>
#include <ai/actor/intersectionlist.h>
#include <constants/actorenum.h>
#include <p3d/view.hpp>
#include <p3d/light.hpp>
#include <raddebug.hpp>


//===========================================================================
// Local Constants, Typedefs, and Macros
//===========================================================================

unsigned int ActorDSG::s_IDCounter = 0;

static const int REMOVE_FROM_WORLD = 0;
static const int SPAWN_5_COINS = 1;
static const int REMOVE_COLLISION_VOLUME = 2;
static const int FIRE_ENERGY_BOLT = 3;
static const int KILL_SPEED = 4;
static const int SPAWN_10_COINS = 5;
static const int SPAWN_15_COINS = 6;
static const int SPAWN_20_COINS = 7;

// States for the shield
static const unsigned int SHIELD_STATE_POWER_UP = 0;
static const unsigned int SHIELD_STATE_IDLE = 1;
static const unsigned int SHIELD_STATE_HIT = 2;
static const unsigned int SHIELD_STATE_DESTROYED = 3;

// States for the tractor beam
static const unsigned int TRACTOR_BEAM_ACTIVATE = 0;
static const unsigned int TRACTOR_BEAM_ON = 1;
static const unsigned int TRACTOR_BEAM_DEACTIVATE = 2;
static const unsigned int TRACTOR_BEAM_OFF = 3;

//===========================================================================
// Global Data, Local Data, Local Classes
//===========================================================================

class CStateProp;
static tUID s_UfoName = tName::MakeUID("spaceship");

//===========================================================================
// Member Functions
//===========================================================================



//===========================================================================
// Debugging / Watching
//===========================================================================

static float AMB_RED_SCALE = 2.0f;
static float AMB_GREEN_SCALE = 2.0f;
static float AMB_BLUE_SCALE = 2.0f;
static float DIR_RED_SCALE = 1.0f;
static float DIR_GREEN_SCALE = 1.0f;
static float DIR_BLUE_SCALE = 1.0f;



ActorDSG::ActorDSG():
m_ID( ++s_IDCounter ),
m_ShieldProp( NULL ),
m_TractorBeamProp( NULL ),
m_ShieldEnabled( false ),
mPhysicsProperties( NULL )
{
}

ActorDSG::~ActorDSG()
{
    if ( m_ShieldProp != NULL )
    {
        m_ShieldProp->Release();
        m_ShieldProp = NULL;
    }
    if ( m_TractorBeamProp != NULL )
    {
        m_TractorBeamProp->ReleaseVerified();
        m_TractorBeamProp = NULL;
    }
    if ( mPhysicsProperties != NULL )
    {
        mPhysicsProperties->Release();
    }
}

sim::Solving_Answer 
ActorDSG::PreReactToCollision( sim::SimState* pCollidedObj, sim::Collision& inCollision )
{  
    // Check to see that we aren't going to collide with an object that has the same ID
    // that we do
    if ( pCollidedObj->mAIRefIndex == PhysicsAIRef::ActorStateProp )
    {
        rAssert( dynamic_cast< ActorDSG* >( static_cast< tRefCounted* >( pCollidedObj->mAIRefPointer) ) );
        ActorDSG* pStatePropDSG = static_cast< ActorDSG* >( pCollidedObj->mAIRefPointer );
        if ( pStatePropDSG->GetID() == GetID() )
        {
            return sim::Solving_Aborted;
        }
    }
    HandleEvent( StatePropDSG::FEATHER_TOUCH );
   
    return sim::Solving_Continue;
}
        
void 
ActorDSG::SetRank(rmt::Vector& irRefPosn, rmt::Vector& mViewVector)
{
    if ( mpStateProp->GetUID() != s_UfoName )
    {
        IEntityDSG::SetRank( irRefPosn, mViewVector );
    }
    else
    {
        // Final hack to make the UFO always draw last
        // The UFO has a *huge* bounding sphere that makes it
        // always drawn after the vehicle explosion, cutting it
        // off nastily, lets fix it and force the thing to be drawn first
        mRank = FLT_MAX;
    }
}


sim::Solving_Answer 
ActorDSG::PostReactToCollision(rmt::Vector& impulse, sim::Collision& inCollision)
{
    // Calculate impulse
    float impulseMagSqr = impulse.MagnitudeSqr();
    // If the shield is on, impulse has to be above an very high number to cause it be
    // be destroyed

    const float FORCE_REQUIRED_TO_BREAK_SHIELD = 1600000;
    const float FORCE_REQUIRE_TO_DESTROY = 800000;

    if ( m_ShieldEnabled )
    {
        if ( impulseMagSqr > FORCE_REQUIRED_TO_BREAK_SHIELD )
        {   
            DestroyShield();
        }
        else
        {
            PlayShieldGettingHit();
        }
    }
    else
    {
        if ( impulseMagSqr > FORCE_REQUIRE_TO_DESTROY )
        {        
            HandleEvent( DESTROYED );
        }
    }

    AddToSimulation();

    return CollisionEntityDSG::PostReactToCollision(impulse, inCollision);
}

        
void ActorDSG::ApplyForce( const rmt::Vector& direction, float force )
{
    // Force over a certain     
    const float SHIELD_BREAKING_FORCE = 400.0f;
    const float FORCE_REQUIRED_TO_DESTROY = 400.0f;
    if ( m_ShieldEnabled )
    {
        if ( force < SHIELD_BREAKING_FORCE )
        {
            PlayShieldGettingHit();    
        }
        else
        {
            DestroyShield();
        }
    }
    else
    {
        if ( force >= FORCE_REQUIRED_TO_DESTROY )
        {
            DestroyShield();
            HandleEvent( StatePropDSG::DESTROYED );
            DynaPhysDSG::ApplyForce( direction, force );
        }
    }
}


void 
ActorDSG::RecieveEvent( int callback , CStateProp* stateProp )
{
    switch ( callback )
    {
    case FIRE_ENERGY_BOLT:
        {
            tName name = "waspray";
            GetActorManager()->FireProjectile( name.GetUID(), mPosn, -mTransform.Row(2), m_ID );
        }
        break;
    case REMOVE_FROM_WORLD:
        GetActorManager()->RemoveActorByDSGPointer( this, true );
        break;
    default:
        StatePropDSG::RecieveEvent( callback, stateProp );
        break;
    };
}

void 
ActorDSG::HandleEvent( StatePropDSG::Event event )
{
    // If a shield is enabled, then only destroyed events should get 
    StatePropDSG::HandleEvent( event );
}


bool 
ActorDSG::LoadShield( const char* statePropName )
{
    bool loadedOk = false;
    CStatePropData* statePropData = p3d::find< CStatePropData > ( statePropName );
    if ( statePropData != NULL )
    {
        CStateProp* stateprop = CStateProp::CreateStateProp( statePropData, 0 );
        if ( stateprop != NULL )
        {
            m_ShieldEnabled = true;
            tRefCounted::Assign( m_ShieldProp, stateprop);
            loadedOk = true;
            SetPhysicsProperties( 100.0f, 0.8f, 1.15f, 0.0f );
        }
    }
    return loadedOk;
}

bool 
ActorDSG::LoadTractorBeam( const char* statePropName )
{
    bool loadedOk = false;
    CStatePropData* statePropData = p3d::find< CStatePropData > ( statePropName );
    if ( statePropData != NULL )
    {
        CStateProp* stateprop = CStateProp::CreateStateProp( statePropData, TRACTOR_BEAM_OFF );
        if ( stateprop != NULL )
        {
            tRefCounted::Assign( m_TractorBeamProp, stateprop);
            loadedOk = true;
        }
    }
    return loadedOk;
}

void 
ActorDSG::ActivateTractorBeam()
{
    if ( m_TractorBeamProp != NULL )
    {
        if ( m_TractorBeamProp->GetState() == TRACTOR_BEAM_OFF )
        {
            m_TractorBeamProp->SetState( TRACTOR_BEAM_ACTIVATE );
        }
    }
}
 
void 
ActorDSG::DeactivateTractorBeam()
{
    if ( m_TractorBeamProp != NULL )
    {
        if ( m_TractorBeamProp->GetState() == TRACTOR_BEAM_ON ||
             m_TractorBeamProp->GetState() == TRACTOR_BEAM_ACTIVATE )
        {
            m_TractorBeamProp->SetState( TRACTOR_BEAM_DEACTIVATE );
        }
    }
}

bool 
ActorDSG::IsTractorBeamOn()const
{
    if ( m_TractorBeamProp == NULL )
        return false;

    return m_TractorBeamProp->GetState() != TRACTOR_BEAM_OFF;
}


void 
ActorDSG::Display()
{
    // Have the rad debugging functions been attached yet?
    // Check, and if not, add them once
#ifndef RAD_RELEASE

    static bool radattached = false;

    if ( radattached == false )
    {
        radDbgWatchAddFloat( &AMB_RED_SCALE, "Amb Red Scale", "Wasp lighting", NULL, NULL, 0, 4.0f );
        radDbgWatchAddFloat( &AMB_GREEN_SCALE, "Amb Green Scale", "Wasp lighting", NULL, NULL, 0, 4.0f );
        radDbgWatchAddFloat( &AMB_BLUE_SCALE, "Amb Blue Scale", "Wasp lighting", NULL, NULL, 0, 4.0f );
        radDbgWatchAddFloat( &DIR_RED_SCALE, "Dir Red Scale", "Wasp lighting", NULL, NULL, 0, 4.0f );
        radDbgWatchAddFloat( &DIR_GREEN_SCALE, "Dir Green Scale", "Wasp lighting", NULL, NULL, 0, 4.0f );
        radDbgWatchAddFloat( &DIR_BLUE_SCALE, "Dir Blue Scale", "Wasp lighting", NULL, NULL, 0, 4.0f );
        radattached = true;
    }


#endif

    // Aryan and Yayoi want the beecamera brightened artificially
    // There doesnt appear to be a nice + quick solution 
    // Do a quick UID compare then brighten the level lights
    // We will reset them after drawing is finished
    static tUID beecameraUID = tName::MakeUID( "beecamera" );
    tColour origColours[MAX_VIEW_LIGHTS];
    tColour origAmbient;
    if ( mpStateProp->GetUID() == beecameraUID )
    {
        tView* view = p3d::context->GetView();
        for ( unsigned int i = 0 ; i < MAX_VIEW_LIGHTS ; i ++ )
        {
            tLight* light = view->GetLight( i );
            if ( light )
            {
                origColours[i] = light->GetColour();
 
                int r = (int)( (float) origColours[i].Red() * DIR_RED_SCALE );
                int g = (int)( (float) origColours[i].Green() * DIR_GREEN_SCALE );
                int b = (int)( (float) origColours[i].Blue() * DIR_BLUE_SCALE );

                if ( r > 255 ) r = 255;
                if ( g > 255 ) g = 255;
                if ( b > 255 ) b = 255;

                tColour newColour( r, g, b );
                light->SetColour( newColour );
            }
        }
        origAmbient = p3d::pddi->GetAmbientLight();

        int r = (int)( (float) origAmbient.Red() * AMB_RED_SCALE );
        int g = (int)( (float) origAmbient.Green() * AMB_GREEN_SCALE );
        int b = (int)( (float) origAmbient.Blue() * AMB_BLUE_SCALE );

        if ( r > 255 ) r = 255;
        if ( g > 255 ) g = 255;
        if ( b > 255 ) b = 255;

        tColour scaledAmbient( r, g, b );
        p3d::pddi->SetAmbientLight( scaledAmbient );
    }


#ifdef PROFILER_ENABLED
    char profileName[] = "  ActorDSG Display";
#endif
    DSG_BEGIN_PROFILE(profileName)
    StatePropDSG::Display();
    if ( m_ShieldProp != NULL )
    {
        // Draw the shield
        p3d::stack->PushMultiply( mTransform );
        m_ShieldProp->UpdateFrameControllersForRender();
        m_ShieldProp->GetDrawable()->Display();
        p3d::stack->Pop();
    }
    if ( m_TractorBeamProp != NULL )
    {
        // Draw the tractor beam
        p3d::stack->PushMultiply( mTransform );
        m_TractorBeamProp->Display();
        p3d::stack->Pop();
    }

    // Reset the level lights for the beecamera
    if ( mpStateProp->GetUID() == beecameraUID )
    {
        tView* view = p3d::context->GetView();
        for ( unsigned int i = 0 ; i < MAX_VIEW_LIGHTS ; i ++ )
        {
            tLight* light = view->GetLight( i );
            if ( light )
            {
                light->SetColour(  origColours[i] );
            }
        }
        // Reset the ambient    
        p3d::pddi->SetAmbientLight( origAmbient );
    }


    DSG_END_PROFILE(profileName)

}

void 
ActorDSG::AdvanceAnimation( float timeInMS )
{
    if ( m_ShieldProp )
    {
        m_ShieldProp->Update( timeInMS );
    }
    if ( m_TractorBeamProp )
    {
        m_TractorBeamProp->Update( timeInMS );
    }


    StatePropDSG::AdvanceAnimation( timeInMS );




}

void 
ActorDSG::DestroyShield()
{
    if ( m_ShieldProp != NULL )
    {
        m_ShieldEnabled = false;
        m_ShieldProp->SetState( SHIELD_STATE_DESTROYED );
    }
}
        
void 
ActorDSG::PlayShieldGettingHit()
{
    if ( m_ShieldProp != NULL )
    {
        if ( m_ShieldProp->GetState() != SHIELD_STATE_HIT )
        {
            m_ShieldProp->SetState( SHIELD_STATE_HIT );
        }
    }    
}
        
void 
ActorDSG::RestoreShield()
{
    if ( m_ShieldProp )
    {
        m_ShieldEnabled = true;
        m_ShieldProp->SetState( SHIELD_STATE_POWER_UP );
    }
}

void 
ActorDSG::SetPhysicsProperties( float mass, float friction, float rest, float tang )
{

    if ( mPhysicsProperties == NULL )
        return;

    // We want to grab the volume so we can compute the density via the given mass
    // and the simstate's volume
    sim::SimStateArticulated* pSimStateArt = ( sim::SimStateArticulated* )( mpSimStateObj );
    sim::ArticulatedPhysicsObject* physicsObject = static_cast< sim::ArticulatedPhysicsObject*>( pSimStateArt->GetSimulatedObject( -1 ));
    float volume = physicsObject->GetVolume();

    rAssert( volume != 0 );

    float density = mass / volume;

    mPhysicsProperties->SetRestCoeffCGS( rest );
    mPhysicsProperties->SetFrictCoeffCGS( friction );
    mPhysicsProperties->SetTangRestCoeffCGS( tang );
    mPhysicsProperties->SetDensityCGS( density );

    mpSimStateObj->SetPhysicsProperties( mPhysicsProperties );
}

void 
ActorDSG::LoadSetup( CStatePropData* statePropData, 
                     int startState, 
                     const rmt::Matrix& transform,
                     CollisionAttributes* ipCollAttr,
                     bool isMoveable,
                     bool isDynaLoaded,
                     tEntityStore* ipSearchStore  )
{

    HeapMgr()->PushHeap( GMA_LEVEL_OTHER );
    sim::PhysicsProperties* physicsProperties = new sim::PhysicsProperties;

    StatePropDSG::LoadSetup( statePropData, startState, transform, ipCollAttr, isDynaLoaded, ipSearchStore );

    tRefCounted::Assign( mPhysicsProperties, physicsProperties );
    HeapMgr()->PopHeap( GMA_LEVEL_OTHER );

}

void 
ActorDSG::GenerateCoins( int numCoins )
{

    // Generate coins, but take into account the fact that this object might be hovering 
    // over the ground.
    // So generate coins at the top of the object below the stateprop
    // First find the ground plane
    bool foundPlane;
    rmt::Vector deepestPosn, deepestNormal, groundPlaneNormal, groundPlanePosn;
    rmt::Vector searchPosn = rPosition();
    searchPosn.y += 100.0f;
    GetIntersectManager()->FindIntersection( searchPosn, foundPlane, groundPlaneNormal, groundPlanePosn );
    if ( foundPlane == false )
    {
        groundPlanePosn = rPosition();
        groundPlanePosn.y -= 30.0f;
    }
    // Check to see if there any static objects directly underneath us
    // find the midpoint between the current position and the ground position
    rmt::Vector midpoint = ( groundPlanePosn + rPosition() ) * 0.5f;
    float radius = (rPosition().y - groundPlanePosn.y );
    ReserveArray< StaticPhysDSG* > spList(200);
    GetIntersectManager()->FindStaticPhysElems( midpoint, radius , spList );
    // Iterate through the static phys list and add them to an intersection list
    IntersectionList staticList;
    for ( int i = 0 ; i < spList.mUseSize; i++ )
    {
        staticList.AddStatic( spList[i]->GetSimState() );
    }
    // Now query our list using subcollision volume accuracy to pinpoint the precise location
    // of intersection
    rmt::Vector staticIntersection;
    rmt::Vector start = rPosition();
    rmt::Vector end = groundPlanePosn;
    if ( staticList.TestIntersectionStatics( start, end, &staticIntersection ))
    {
        // There is a static in the way. spawn teh coins on top of this object
        GetCoinManager()->SpawnCoins( numCoins, rPosition(), staticIntersection.y );           
    }
    else
    {
        // The wasp is hovering above solid ground. Spawn the coins on the ground
        GetCoinManager()->SpawnCoins( numCoins, rPosition(), groundPlanePosn.y );
    }

}

int
ActorDSG::CastsShadow()
{
    // Dont cast a shadow in the first (fade in) and last(exploded) states
    int castsShadow;

    unsigned int statepropState = GetState();
    if ( statepropState == ActorEnum::eFadeIn || statepropState == ActorEnum::eDestroyed )
        castsShadow = 0;
    else
        castsShadow = StaticPhysDSG::CastsShadow();  

    return castsShadow;
}