summaryrefslogtreecommitdiffstats
path: root/game/code/ai/actionbuttonmanager.cpp
blob: 3a70544f922c710133fc5eea8a6cf4eb2a689692 (plain) (blame)
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
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
//=============================================================================
// Copyright (C) 2002 Radical Entertainment Ltd.  All rights reserved.
//
// File:        
//
// Description: Implementation of class ActionButtonManager
//
// History:     18/07/2002 + Created -- TBJ
//
//=============================================================================

//========================================
// System Includes
//========================================
// Foundation Tech
#include <raddebug.hpp>
#include <memory/srrmemory.h>

//========================================
// Project Includes
//========================================
#include <worldsim/character/charactercontroller.h> 

#include <ai/actionbuttonmanager.h>
#include <ai/actionbuttonhandler.h>
#include <ai/actionlist.h>
#include <ai/actionnames.h>

#include <render/DSG/animcollisionentitydsg.h>


#include <memory/srrmemory.h>
#include <meta/actioneventlocator.h>
#include <meta/eventlocator.h>
#include <meta/spheretriggervolume.h>
#include <meta/triggervolumetracker.h>

#include <console/console.h>

#include <render/RenderManager/RenderManager.h>
#include <render/RenderManager/RenderLayer.h>
#include <render/RenderManager/WorldRenderLayer.h>
#include <render/loaders/billboardwrappedloader.h>
#include <mission/gameplaymanager.h>

#include <events/eventmanager.h>

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

ActionButtonManager* ActionButtonManager::spActionButtonManager = (ActionButtonManager*)0;
//******************************************************************************
//
// Public Member Functions
//
//******************************************************************************

//==============================================================================
// ActionButtonManager::ActionButtonManager
//==============================================================================
// Description: Constructor.
//
// Parameters: None.
//
// Return:      N/A.
//
//==============================================================================
static int s_currentID = 0;

ActionButtonManager::ActionButtonManager()
:
mpCurrentToResolve( 0 ),
mLoadingIntoInterior( false )
{
    int i;
    for ( i = 0; i < MAX_ACTIONS; i++ )
    {
        mActionButtonList[ i ].handler = NULL;
        mActionButtonList[ i ].sectionName = 0;
        mbActionButtonNeedsUpdate[ i ] = false;
    }

    // Sanity check.
    //
    rAssertMsg( ActionButton::ButtonNameListSize == CharacterController::NUM_INPUTS, "Button Name list size does not match enumeration size defined in .\\worldsim\\character\\charactercontroller.h\n" );
    rAssertMsg( ActionButton::nameIndex == ActionButton::ActionNameSize, "Action Name list size does not match enumeration size!\n" );

    Console* pConsole = GetConsole();
    if ( pConsole )
    {
        pConsole->AddFunction( "AddVehicleSelectInfo", AddVehicleSelectInfo, "", 3, 3 ); //One entry in phone selectable vehicles.
        pConsole->AddFunction( "ClearVehicleSelectInfo", ClearVehicleSelectInfo, "", 0, 0 ); //Clear all entries in phone selectable vehicles.
    }

    GetEventManager()->AddListener( this, EVENT_DUMP_DYNA_SECTION );
}

/*
==============================================================================
ActionButtonManager::FindHandler
==============================================================================
Description:    Returns the ButtonHandler that is associated with the given 
                locator. NULL if none are found

Parameters:     const ActionEventLocator* locator

Return:         ActionButton::ButtonHandler* 

=============================================================================
*/
ActionButton::ActionEventHandler* ActionButtonManager::FindHandler( const ActionEventLocator* locator )const
{
    // Iterate through the ButtonHandlers and find the Handler that contains the given locator
    for ( int i = 0 ; i < MAX_ACTIONS ; i++ )
    {
        // Expensive dynamic cast 
        ActionButton::ActionEventHandler* handler = dynamic_cast< ActionButton::ActionEventHandler* >( mActionButtonList[i].handler );
        if ( handler )
        {
            // Is this the locator we are looking for
            if ( locator == handler->GetActionEventLocator() )
            {
                return handler;
            }
        }
    }
    // No Handler found, return NULL
    return NULL;   
}


/*
==============================================================================
ActionButtonManager::AddVehicleSelectInfo
==============================================================================
Description:    Scripter hook

Parameters:     ( int argc, char** argv )

Return:         void 

=============================================================================
*/
void ActionButtonManager::AddVehicleSelectInfo( int argc, char** argv )
{
    rWarningMsg( false, "ActionButtonManager::AddVehicleSelectInfo() function is deprecated!" );
/*
    int index = ActionButton::SummonVehiclePhone::CarSelectionInfo::GetNumUsedSlots();
    ActionButton::SummonVehiclePhone::CarSelectionInfo* pInfo = ActionButton::SummonVehiclePhone::GetCarSelectInfo( index );
    if ( pInfo )
    {
        pInfo->AddVehicleSelectionInfo( argv[1], argv[2], argv[3] );
    }
    else
    {
        rReleasePrintf( "Too many vehicle select info lines.  Call ClearVehicleSelectInfo() first!." );
    }
*/
}

/*
==============================================================================
ActionButtonManager::ClearVehicleSelectInfo
==============================================================================
Description:    Scripter hook

Parameters:     ( int argc, char** argv )

Return:         void 

=============================================================================
*/
void ActionButtonManager::ClearVehicleSelectInfo( int argc, char** argv )
{
    rWarningMsg( false, "ActionButtonManager::ClearVehicleSelectInfo() function is deprecated!" );
/*
    ActionButton::SummonVehiclePhone::ClearCarSelectInfo( );
*/
}

//=============================================================================
// ActionButtonManager::OnProcessRequestsComplete
//=============================================================================
// Description: Comment
//
// Parameters:  ( void* pUserData )
//
// Return:      void 
//
//=============================================================================
void ActionButtonManager::OnProcessRequestsComplete( void* pUserData )
{
    BillboardWrappedLoader::OverrideLoader( false );
    GetRenderManager()->pWorldRenderLayer()->DoPostDynaLoad();
}


//==============================================================================
// ActionButtonManager::~ActionButtonManager
//==============================================================================
// Description: Destructor.
//
// Parameters: None.
//
// Return:      N/A.
//
//==============================================================================
ActionButtonManager::~ActionButtonManager()
{
    Destroy( );

    GetEventManager()->RemoveAll( this );
}
/*
==============================================================================
ActionButtonManager::Destroy
==============================================================================
Description:    Comment

Parameters:     ( void )

Return:         void 

=============================================================================
*/
void ActionButtonManager::Destroy( void )
{
    int i;
    for ( i = 0; i < MAX_ACTIONS; i++ )
    {
        // Delete the actionbuttonhandler.
        //
        RemoveActionByArrayPos( i );
    }

    s_currentID = 0;
}
/*
==============================================================================
ActionButtonManager::CreateInstance
==============================================================================
Description:    Comment

Parameters:     ( void )

Return:         ActionButtonManager

=============================================================================
*/
ActionButtonManager*  ActionButtonManager::CreateInstance( void )
{
	rAssertMsg( spActionButtonManager == 0, "ActionButtonManager already created.\n" );

    #ifdef RAD_GAMECUBE
        HeapMgr()->PushHeap( GMA_GC_VMM );
    #else
        HeapMgr()->PushHeap( GMA_PERSISTENT );
    #endif

	spActionButtonManager = new ActionButtonManager;

    #ifdef RAD_GAMECUBE
        HeapMgr()->PopHeap( GMA_GC_VMM );
    #else
        HeapMgr()->PopHeap( GMA_PERSISTENT );
    #endif

    return( spActionButtonManager );
}
/*
==============================================================================
ActionButtonManager::GetInstance
==============================================================================
Description:    Comment

Parameters:     ( void )

Return:         ActionButtonManager

=============================================================================
*/
ActionButtonManager* ActionButtonManager::GetInstance( void )
{
	rAssertMsg( spActionButtonManager != 0, "ActionButtonManager has not been created yet.\n" );
	return spActionButtonManager;
}

/*
==============================================================================
ActionButtonManager::DestroyInstance
==============================================================================
Description:    Destroy the singleton.

Parameters:     ( void )

Return:         n/a

=============================================================================
*/
void ActionButtonManager::DestroyInstance( void )
{
    rAssert( spActionButtonManager != NULL );

    #ifdef RAD_GAMECUBE
        delete( GMA_GC_VMM, spActionButtonManager );
    #else
        delete( GMA_PERSISTENT, spActionButtonManager );
    #endif

    spActionButtonManager = NULL;
}

/*
==============================================================================
ActionButtonManager::EnterGame
==============================================================================
Description:    Comment

Parameters:     ( void )

Return:         void 

=============================================================================
*/
void ActionButtonManager::EnterGame( void )
{
}

/*
==============================================================================
ActionButtonManager::Update
==============================================================================
Description:    Comment

Parameters:     ( float timeins )

Return:         void 

=============================================================================
*/
void ActionButtonManager::Update( float timeins )
{
    int i;
    for ( i = 0; i < MAX_ACTIONS; i++ )
    {
        if ( mActionButtonList[ i ].handler != 0 && mActionButtonList[ i ].handler->NeedsUpdate( ) )
        {
            mActionButtonList[ i ].handler->Update( timeins );
        }
    }

     unsigned int time = rmt::FtoL( timeins * 1000.0f );

     //These are the collection effects.  They are one shot and remove
     //themselves from the world when they're done.
    ActionButton::CollectibleCard::UpdateThing( time  );
    ActionButton::WrenchIcon::UpdateThing( time );
    ActionButton::NitroIcon::UpdateThing( time );
}

/*
==============================================================================
ActionButtonManager::AddActionEventLocator
==============================================================================
Description:    Comment

Parameters:     ( ActionEventLocator* pActionEventLocator )

Return:         bool 

=============================================================================
*/
bool ActionButtonManager::AddActionEventLocator( ActionEventLocator* pActionEventLocator, tEntityStore* inStore )
{
    rAssert( pActionEventLocator );
    return LinkActionToLocator( pActionEventLocator, inStore );
}

/*
==============================================================================
ActionButtonManager::GetActionAt
==============================================================================
Description:    Comment

Parameters:     ( int i )

Return:         ActionButtonHandler

=============================================================================
*/
ActionButton::ButtonHandler* ActionButtonManager::GetActionByIndex( int i ) const
{
    i = Find(i);

    if ( i < MAX_ACTIONS && i >= 0 )
    {
        return mActionButtonList[ i ].handler;
    }
    else
    {
        return 0;
    }
}


/*
==============================================================================
ActionButtonManager::AddAction
==============================================================================
Description:    Comment

Parameters:     ( ActionButtonHandler* pAction )

Return:         bool 

=============================================================================
*/
bool ActionButtonManager::AddAction( ActionButton::ButtonHandler* pAction, int& id, tUID section)
{
    rAssert( pAction );
    int i;
    for ( i = 0; i < MAX_ACTIONS; i++ )
    {
        if ( mActionButtonList[ i ].handler == (ActionButton::ButtonHandler*)0 )
        {
            break;
        }
    }
    if ( i < MAX_ACTIONS )
    {
        mActionButtonList[ i ].handler = pAction;
        mActionButtonList[ i ].handler->AddRef();
        mActionButtonList[ i ].id = s_currentID++;
        mActionButtonList[ i ].sectionName = section;
        id = mActionButtonList[ i ].id;
        return true;
    }
    id = -1;
    return false;
}

/*
==============================================================================
ActionButtonManager::RemoveActionByIndex
==============================================================================
Description:    Comment

Parameters:     ( int id )

Return:         bool 

=============================================================================
*/
bool ActionButtonManager::RemoveActionByIndex( int id )
{
    return RemoveActionByArrayPos(Find(id));
}
/*
==============================================================================
ActionButtonManager::RemoveAction
==============================================================================
Description:    Comment

Parameters:     ( ActionButton::ButtonHandler* pAction )

Return:         int 

=============================================================================
*/
int ActionButtonManager::RemoveAction( ActionButton::ButtonHandler* pAction )
{
    rAssert( pAction );
    int id = -1;
    int i;
    for ( i = 0; i < MAX_ACTIONS; i++ )
    {
        if ( mActionButtonList[ i ].handler == pAction )
        {
            break;
        }
    }
    if ( i < MAX_ACTIONS )
    {
        RemoveActionByArrayPos( i );
        id = i;
    }    
    return id;    
}
/*
==============================================================================
ActionButtonManager::CreateActionEventTrigger
==============================================================================
Description:    Comment

Parameters:     ( const char* triggerName, rmt::Vector& pos, float r )

Return:         bool 

=============================================================================
*/
bool ActionButtonManager::CreateActionEventTrigger( const char* triggerName, rmt::Vector& pos, float r )
{
    return true;
}
/*
==============================================================================
ActionButtonManager::LinkActionToObjectJoint
==============================================================================
Description:    Comment

Parameters:     ( const char* objectName, const char* jointName, const char* triggerName, const char* typeName, const char* buttonName )

Return:         bool 

=============================================================================
*/
bool ActionButtonManager::LinkActionToObjectJoint( const char* objectName, const char* jointName, const char* triggerName, const char* typeName, const char* buttonName )
{   
    return LinkActionToObject( objectName, jointName, triggerName, typeName, buttonName, true );
}
/*
==============================================================================
ActionButtonManager::LinkActionToObject
==============================================================================
Description:    Comment

Parameters:     ( const char* objectName, const char* triggerName, const char* typeName, const char* buttonName )

Return:         bool 

=============================================================================
*/
bool ActionButtonManager::LinkActionToObject( const char* objectName, const char* jointName, const char* triggerName, const char* typeName, const char* buttonName, bool attachToJoint )
{
    return true;
} 

/*
==============================================================================
ActionButtonManager::LinkActionToObject
==============================================================================
Description:    Comment

Parameters:     ( ActionEventLocator* pActionEventLocator )

Return:         bool 

=============================================================================
*/
bool ActionButtonManager::LinkActionToLocator( ActionEventLocator* pActionEventLocator, tEntityStore* inStore )
{
    ActionButton::ButtonHandler* pABHandler = this->NewActionButtonHandler( pActionEventLocator->GetActionName(), pActionEventLocator );
    bool bAdded = false;
    
    if ( pABHandler != 0 )
    {
        rAssert( dynamic_cast< ActionButton::ActionEventHandler*>( pABHandler ) != NULL );
        ActionButton::ActionEventHandler* pActionHandler = static_cast<ActionButton::ActionEventHandler*>( pABHandler );    
        rAssert( pActionHandler );
        pActionHandler->AddRef();
    
        
        int id = -1;
        bool bCreated = pActionHandler->Create( inStore );
        if ( bCreated )
        {
            tUID section = 0;
            if ( GetRenderManager()->pWorldRenderLayer()->GetCurrentState() == WorldRenderLayer::msLoad )
            {
                section = GetRenderManager()->pWorldRenderLayer()->GetCurSectionName().GetUID();
            }

            bAdded = this->AddAction( pActionHandler, id, section);
            rAssert( bAdded );
            pActionEventLocator->SetData( id );
        }
        else
        {
            rReleasePrintf( "****************** Failed to create %s, action type %s\n", pActionEventLocator->GetObjName( ), pActionEventLocator->GetActionName( ) );
        } 

        //HACK
        mLoadingIntoInterior = false;

        // We are done with it at this level.
        // If it was not added, then this will delete the object.
        //
        pActionHandler->Release( );
    }    
    return bAdded;
}

/*
==============================================================================
ActionButtonManager::EnterActionTrigger
==============================================================================
Description:    Comment

Parameters:     ( Character* pCharacter, int index )

Return:         void 

=============================================================================
*/
void ActionButtonManager::EnterActionTrigger( Character* pCharacter, int index )
{
    index = Find(index);

    rAssert( index < MAX_ACTIONS && index >= 0 );
    
    if ( index < MAX_ACTIONS && index >= 0 )
    {
        rAssert( mActionButtonList[ index ].handler != 0 );
        if ( mActionButtonList[ index ].handler != 0 )
        {
            mActionButtonList[ index ].handler->Enter( pCharacter );
        }
    }
}
/*
==============================================================================
ActionButtonManager::ExitActionTrigger
==============================================================================
Description:    Comment

Parameters:     ( Character* pCharacter, int index )

Return:         void 

=============================================================================
*/
void ActionButtonManager::ExitActionTrigger( Character* pCharacter, int index )
{
    index = Find(index);

    rAssert( index < MAX_ACTIONS && index >= 0 );
    
    if ( index < MAX_ACTIONS && index >= 0 )
    {
        rAssert( mActionButtonList[ index ].handler != 0 );
        if ( mActionButtonList[ index ].handler != 0 )
        {
            mActionButtonList[ index ].handler->Exit( pCharacter );
        }
    }
}
/*
==============================================================================
ActionButtonManager::NewActionButtonHandler
==============================================================================
Description:    Comment

Parameters:     ( const char* typeName, ActionEventLocator* pActionEventLocator )

Return:         ActionButtonHandler

=============================================================================
*/
ActionButton::ButtonHandler* ActionButtonManager::NewActionButtonHandler( const char* typeName, ActionEventLocator* pActionEventLocator )
{
    ActionButton::ButtonHandler* pActionButtonHandler = 0;  
    int i;
    for ( i = 0; i < ActionButton::ActionListSize; i++ )
    {
        if ( ActionButton::CompareActionType( typeName, ActionButton::theListOfActions[ i ].mActionKey ) )
        {
            pActionButtonHandler = ActionButton::theListOfActions[ i ].actionPtr( pActionEventLocator );
            break;
        }
    }
    
    if ( !pActionButtonHandler )
    {
        rDebugPrintf("Could not create action button handler type %s.  Update your worldbuilder and reexport.", typeName );
    }

    return pActionButtonHandler;
}
/*
==============================================================================
ActionButtonManager::ResolveActionTrigger
==============================================================================
Description:    Comment

Parameters:     ( AnimCollisionEntityDSG* pAnimObject )

Return:         bool, true if successfully resolved. 

=============================================================================
*/
bool ActionButtonManager::ResolveActionTrigger( AnimCollisionEntityDSG* pAnimObject, tEntityStore* inStore )
{
    p3d::inventory->PushSection(); 
    p3d::inventory->SelectSection("Default");
    mpCurrentToResolve = pAnimObject;

    ActionEventLocator* pLocator = p3d::find<ActionEventLocator>( pAnimObject->GetUID() );
    bool bAdded = false;
    if ( pLocator )
    {
        bAdded = LinkActionToLocator( pLocator, inStore );
        rAssert( bAdded );
    }
    mpCurrentToResolve = 0;
    p3d::inventory->PopSection(); 
    return bAdded;
}

//=============================================================================
// ActionButtonManager::HandleEvent
//=============================================================================
// Description: Comment
//
// Parameters:  ( EventEnum id, void* pEventData )
//
// Return:      void 
//
//=============================================================================
void ActionButtonManager::HandleEvent( EventEnum id, void* pEventData )
{
    //When the event EVENT_DUMP_DYNA_SECTION is sent, the dyna load section UID is 
    //compared to the stored section of the action butons.  Any buttons created during the
    //specified load section are released.
    unsigned int i;
    for ( i = 0; i < MAX_ACTIONS; ++i )
    {
        if ( mActionButtonList[ i ].handler != NULL &&
             mActionButtonList[ i ].sectionName == static_cast<tName*>(pEventData)->GetUID() )
        {
            //Remove this action button.
            RemoveActionByArrayPos( i );
        }
    }
}

//******************************************************************************
//
// Private Member Functions
//
//******************************************************************************

int ActionButtonManager::Find(int id) const
{
    unsigned int i;
    for ( i = 0; i < MAX_ACTIONS; ++i )
    {
        if ( mActionButtonList[ i ].handler != NULL &&
             mActionButtonList[ i ].id == id )
        {
            return i;
        }
    }
    return -1;
}

bool  ActionButtonManager::RemoveActionByArrayPos(int id)
{
    if( id < MAX_ACTIONS && id >= 0 )
    {
        if ( mActionButtonList[ id ].handler != 0 )
        {
            mActionButtonList[ id ].handler->Release( );
            mActionButtonList[ id ].handler = 0;
            return true;
        }
    }
    return false;
}