/
CPlayerPedSA.cpp
610 lines (526 loc) · 18.2 KB
/
CPlayerPedSA.cpp
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
/*****************************************************************************
*
* PROJECT: Multi Theft Auto v1.0
* LICENSE: See LICENSE in the top level directory
* FILE: game_sa/CPlayerPedSA.cpp
* PURPOSE: Player ped entity
*
* Multi Theft Auto is available from http://www.multitheftauto.com/
*
*****************************************************************************/
#include "StdInc.h"
/**
* Constructor for CPlayerPedSA
*/
static CPedClothesDesc* pLocalClothes = 0;
static CWantedSAInterface* pLocalWanted = 0;
static std::set<SString> ms_DoneAnimBlockRefMap;
CPlayerPedSA::CPlayerPedSA(ePedModel pedType)
{
DEBUG_TRACE("CPlayerPedSA::CPlayerPedSA( ePedModel pedType )");
// based on CPlayerPed::SetupPlayerPed (R*)
DWORD CPedOperatorNew = FUNC_CPedOperatorNew;
DWORD CPlayerPedConstructor = FUNC_CPlayerPedConstructor;
DWORD dwPedPointer = 0;
_asm
{
push SIZEOF_CPLAYERPED
call CPedOperatorNew
add esp, 4
mov dwPedPointer, eax
mov ecx, eax
push 0 // set to 0 and they'll behave like AI peds
push 1
call CPlayerPedConstructor
}
this->SetInterface((CEntitySAInterface*)dwPedPointer);
this->Init(); // init our interfaces
CPoolsSA* pools = (CPoolsSA*)pGame->GetPools();
this->internalID = pools->GetPedRef((DWORD*)this->GetInterface());
CWorldSA* world = (CWorldSA*)pGame->GetWorld();
this->SetModelIndex(pedType);
this->BeingDeleted = FALSE;
this->DoNotRemoveFromGame = FALSE;
this->SetType(PLAYER_PED);
// Allocate a player data struct and set it as the players
m_bIsLocal = false;
m_pData = new CPlayerPedDataSAInterface;
// Copy the local player data so we're defaulted to something good
CPlayerPedSA* pLocalPlayerSA = dynamic_cast<CPlayerPedSA*>(pools->GetPedFromRef((DWORD)1));
if (pLocalPlayerSA)
MemCpyFast(m_pData, ((CPlayerPedSAInterface*)pLocalPlayerSA->GetInterface())->pPlayerData, sizeof(CPlayerPedDataSAInterface));
// Replace the player ped data in our ped interface with the one we just created
GetPlayerPedInterface()->pPlayerData = m_pData;
// Set default stuff
m_pData->m_bRenderWeapon = true;
m_pData->m_Wanted = pLocalWanted;
m_pData->m_fSprintEnergy = 1000.0f;
// Clothes pointers or we'll crash later (TODO: Wrap up with some cloth classes and make it unique per player)
m_pData->m_pClothes = pLocalClothes;
// Not sure why was this here (svn blame reports that this line came from the old SVN),
// but it's causing a bug in what the just streamed-in players that are in the air are
// processed as if they would be standing on some surface, screwing velocity calculations
// for players floating in air (using superman script, for example) because GTA:SA will
// try to apply the floor friction to their velocity.
// SetIsStanding ( true );
GetPlayerPedInterface()->pedFlags.bCanBeShotInVehicle = true;
GetPlayerPedInterface()->pedFlags.bTestForShotInVehicle = true;
// Stop remote players targeting eachother, this also stops the local player targeting them (needs to be fixed)
GetPlayerPedInterface()->pedFlags.bNeverEverTargetThisPed = true;
GetPlayerPedInterface()->pedFlags.bIsLanding = false;
GetPlayerPedInterface()->fRotationSpeed = 7.5;
m_pInterface->bStreamingDontDelete = true;
m_pInterface->bDontStream = true;
world->Add(m_pInterface, CPlayerPed_Constructor);
}
CPlayerPedSA::CPlayerPedSA(CPlayerPedSAInterface* pPlayer)
{
DEBUG_TRACE("CPlayerPedSA::CPlayerPedSA( CPedSAInterface * ped )");
// based on CPlayerPed::SetupPlayerPed (R*)
this->SetInterface((CEntitySAInterface*)pPlayer);
this->Init();
CPoolsSA* pools = (CPoolsSA*)pGame->GetPools();
this->internalID = pools->GetPedRef((DWORD*)this->GetInterface());
this->SetType(PLAYER_PED);
this->BeingDeleted = FALSE;
m_bIsLocal = true;
DoNotRemoveFromGame = true;
m_pData = GetPlayerPedInterface()->pPlayerData;
m_pWanted = NULL;
GetPlayerPedInterface()->pedFlags.bCanBeShotInVehicle = true;
GetPlayerPedInterface()->pedFlags.bTestForShotInVehicle = true;
GetPlayerPedInterface()->pedFlags.bIsLanding = false;
GetPlayerPedInterface()->fRotationSpeed = 7.5;
pLocalClothes = m_pData->m_pClothes;
pLocalWanted = m_pData->m_Wanted;
GetPlayerPedInterface()->pedFlags.bCanBeShotInVehicle = true;
// Something resets this, constantly
GetPlayerPedInterface()->pedFlags.bTestForShotInVehicle = true;
// Stop remote players targeting the local (need to stop them targeting eachother too)
GetPlayerPedInterface()->pedFlags.bNeverEverTargetThisPed = true;
}
CPlayerPedSA::~CPlayerPedSA(void)
{
DEBUG_TRACE("CPlayerPedSA::~CPlayerPedSA( )");
if (!this->BeingDeleted && DoNotRemoveFromGame == false)
{
DWORD dwInterface = (DWORD)m_pInterface;
if ((DWORD)this->GetInterface()->vtbl != VTBL_CPlaceable)
{
CWorldSA* world = (CWorldSA*)pGame->GetWorld();
world->Remove(m_pInterface, CPlayerPed_Destructor);
DWORD dwThis = (DWORD)m_pInterface;
DWORD dwFunc = m_pInterface->vtbl->SCALAR_DELETING_DESTRUCTOR; // we use the vtbl so we can be type independent
_asm
{
mov ecx, dwThis
push 1 // delete too
call dwFunc
}
}
this->BeingDeleted = true;
((CPoolsSA*)pGame->GetPools())->RemovePed((CPed*)(CPedSA*)this, false);
}
// Delete the player data
if (!m_bIsLocal)
{
delete m_pData;
}
}
CWanted* CPlayerPedSA::GetWanted(void)
{
return m_pWanted;
}
float CPlayerPedSA::GetSprintEnergy(void)
{
/*
OutputDebugString("GetSprintEnergy HACK\n");
m_pData->bCanBeDamaged = true;
m_pData->m_bRenderWeapon = true;
m_pData->m_bDontAllowWeaponChange = true;
((CPedSAInterface*)GetInterface())->pedFlags.bUpdateAnimHeading = true;
((CPedSAInterface*)GetInterface())->pedFlags.bHeadStuckInCollision = true;
((CPedSAInterface*)GetInterface())->pedFlags.bDonePositionOutOfCollision = true;
((CPedSAInterface*)GetInterface())->pedFlags.bIsRestoringGun = true;
RebuildPlayer ();
*/
return m_pData->m_fSprintEnergy;
}
void CPlayerPedSA::SetSprintEnergy(float fSprintEnergy)
{
m_pData->m_fSprintEnergy = fSprintEnergy;
}
void CPlayerPedSA::SetInitialState(void)
{
DWORD dwUnknown = 1;
DWORD dwFunction = FUNC_SetInitialState;
DWORD dwThis = (DWORD)m_pInterface;
_asm
{
push dwUnknown
mov ecx, dwThis
call dwFunction
}
// Avoid direction locks for respawning after a jump
GetPlayerPedInterface()->pedFlags.bIsLanding = false;
// Avoid direction locks for respawning after a sprint stop
GetPlayerPedInterface()->fRotationSpeed = 7.5;
// This seems to also be causing some movement / direction locks
GetPlayerPedInterface()->pedFlags.bStayInSamePlace = false;
}
eMoveAnim CPlayerPedSA::GetMoveAnim(void)
{
CPedSAInterface* pedInterface = (CPedSAInterface*)this->GetInterface();
return (eMoveAnim)pedInterface->iMoveAnimGroup;
}
// Helper for SetMoveAnim
bool IsBlendAssocGroupLoaded(int iGroup)
{
CAnimBlendAssocGroupSAInterface* pBlendAssocGroup = *(CAnimBlendAssocGroupSAInterface**)0xB4EA34;
pBlendAssocGroup += iGroup;
return pBlendAssocGroup->pAnimBlock != NULL;
}
// Helper for SetMoveAnim
bool IsBlendAssocGroupValid(int iGroup)
{
CAnimBlendAssocGroupSAInterface* pBlendAssocGroup = *(CAnimBlendAssocGroupSAInterface**)0xB4EA34;
pBlendAssocGroup += iGroup;
if (pBlendAssocGroup->pAnimBlock == NULL)
return false;
if (pBlendAssocGroup->pAssociationsArray == NULL)
return false;
int moveIds[] = {0, 1, 2, 3, 5};
for (uint i = 0; i < NUMELMS(moveIds); i++)
{
int iUseAnimId = moveIds[i] - pBlendAssocGroup->iIDOffset;
CAnimBlendStaticAssociationSAInterface* pAssociation = pBlendAssocGroup->pAssociationsArray + iUseAnimId;
if (pAssociation == NULL)
return false;
if (pAssociation->pAnimHeirarchy == NULL)
return false;
}
return true;
}
void CPlayerPedSA::SetMoveAnim(eMoveAnim iAnimGroup)
{
// Range check
if (!IsValidMoveAnim(iAnimGroup))
return;
if (iAnimGroup == MOVE_DEFAULT)
{
m_iCustomMoveAnim = 0;
return;
}
// Find which anim block to load
SString strBlockName;
switch (iAnimGroup)
{
case 55:
case 58:
case 61:
case 64:
case 67:
strBlockName = "fat";
break;
case 56:
case 59:
case 62:
case 65:
case 68:
strBlockName = "muscular";
break;
case 138:
strBlockName = "skate";
break;
default:
strBlockName = "ped";
break;
}
// Need to load ?
if (!IsBlendAssocGroupLoaded(iAnimGroup))
{
CAnimBlock* pAnimBlock = pGame->GetAnimManager()->GetAnimationBlock(strBlockName);
// Try load
if (pAnimBlock && !pAnimBlock->IsLoaded())
{
pAnimBlock->Request(BLOCKING, true);
MapInsert(ms_DoneAnimBlockRefMap, strBlockName); // Request() adds a ref for us
}
// Load fail?
if (!IsBlendAssocGroupLoaded(iAnimGroup))
return;
}
// Check it all looks good
if (!IsBlendAssocGroupValid(iAnimGroup))
return;
// Ensure we add a ref to this block, even if it wasn't loaded by us
CAnimBlock* pAnimBlock = pGame->GetAnimManager()->GetAnimationBlock(strBlockName);
if (!pAnimBlock)
return;
if (!MapContains(ms_DoneAnimBlockRefMap, strBlockName))
{
MapInsert(ms_DoneAnimBlockRefMap, strBlockName);
pAnimBlock->AddRef();
}
m_iCustomMoveAnim = iAnimGroup;
// Set the the new move animation group now, although it does get updated through the hooks as well
CPedSAInterface* pedInterface = (CPedSAInterface*)this->GetInterface();
pedInterface->iMoveAnimGroup = (int)iAnimGroup;
DWORD dwThis = (DWORD)pedInterface;
DWORD dwFunc = FUNC_CPlayerPed_ReApplyMoveAnims;
_asm
{
mov ecx, dwThis
call dwFunc
}
}
/**
* Gets information on the player's wanted status
* @return Pointer to a CWanted class containing the wanted information for the PlayerPed.
*/
// CWanted * CPlayerPedSA::GetWanted ( )
//{
// return internalInterface->Wanted;
//}
/**
* Gets the current weapon type that the playerped is using
* @return DWORD containing the current weapon type
* \todo Check this is the weapon type, not the actual weapon ID (or whatever)
*/
/*DWORD CPlayerPedSA::GetCurrentWeaponType ( )
{
return internalInterface->CurrentWeapon;
}*/
/**
* Gets the time the last shot was fired by the playerped
* @return DWORD containing a system time value
*/
/*DWORD CPlayerPedSA::GetLastShotTime ( )
{
return internalInterface->LastShotTime;
}
*/
/**
* Checks if the player is stationary on foot
* \todo Does this duplicate?
* @return BOOL TRUE if the player is stationary on foot, FALSE otherwise
*/
/*
BOOL CPlayerPedSA::IsStationaryOnFoot ( )
{
return internalInterface->StationaryOnFoot;
}*/
/**
* Resets the played ped to their initial state
* \note This also resets some global values, such as game speed
*/
/*
VOID CPlayerPedSA::ResetToInitialState ( )
{
DWORD dwFunction = FUNC_SetInitialState;
DWORD dwThis = (DWORD)internalInterface;
_asm
{
mov ecx, dwThis
call dwFunction
}
}*/
/**
* Clears information related to the player targeting
*/
/*
VOID CPlayerPedSA::ClearWeaponTarget ( )
{
DWORD dwFunction = FUNC_ClearWeaponTarget;
DWORD dwThis = (DWORD)internalInterface;
_asm
{
mov ecx, dwThis
call dwFunction
}
}*/
////////////////////////////////////////////////////////////////
//
// Hooks for CPlayerPedSA
//
////////////////////////////////////////////////////////////////
/*
Summary of move anim types
--------------------------
Physique based anims with plain pose:
MOVE_PLAYER = 54,
MOVE_PLAYER_F,
MOVE_PLAYER_M,
Physique based anims with special pose:
MOVE_ROCKET,
MOVE_ROCKET_F,
MOVE_ROCKET_M,
MOVE_ARMED,
MOVE_ARMED_F,
MOVE_ARMED_M,
MOVE_BBBAT,
MOVE_BBBAT_F,
MOVE_BBBAT_M,
MOVE_CSAW,
MOVE_CSAW_F,
MOVE_CSAW_M,
Non physique based pose:
MOVE_JETPACK,
Non physique based anims:
MOVE_SNEAK,
MOVE_MAN = 118,
MOVE_SHUFFLE,
MOVE_OLDMAN,
MOVE_GANG1,
MOVE_GANG2,
MOVE_OLDFATMAN,
MOVE_FATMAN,
MOVE_JOGGER,
MOVE_DRUNKMAN,
MOVE_BLINDMAN,
MOVE_SWAT,
MOVE_WOMAN,
MOVE_SHOPPING,
MOVE_BUSYWOMAN,
MOVE_SEXYWOMAN,
MOVE_PRO,
MOVE_OLDWOMAN,
MOVE_FATWOMAN,
MOVE_JOGWOMAN,
MOVE_OLDFATWOMAN,
MOVE_SKATE,
*/
bool IsAnimJetPack(int iAnim)
{
return iAnim == MOVE_JETPACK;
}
// True if anim has Std/Fat/Muscular variants
bool IsAnimPhysiqueBased(int iAnim)
{
return (iAnim >= MOVE_PLAYER) && (iAnim <= MOVE_CSAW_M);
}
// Special pose is MOVE_ROCKET, MOVE_ARMED, MOVE_BBBAT or MOVE_CSAW
bool IsAnimSpecialPose(int iAnim)
{
return (iAnim >= MOVE_ROCKET) && (iAnim <= MOVE_CSAW_M);
}
// 0-Std 1-Fat 2-Muscular
int GetAnimPhysiqueIndex(int iAnim)
{
return (iAnim - MOVE_PLAYER) % 3;
}
// Pose is MOVE_PLAYER, MOVE_ROCKET, MOVE_ARMED, MOVE_BBBAT or MOVE_CSAW
int GetAnimPose(int iAnim)
{
return iAnim - GetAnimPhysiqueIndex(iAnim);
}
////////////////////////////////////////////////////////////////
//
// CPlayerPed_ProcessAnimGroups_Mid
//
// Stop GTA overriding our custom move anim
// Returns anim to use
//
////////////////////////////////////////////////////////////////
__declspec(noinline) int _cdecl OnCPlayerPed_ProcessAnimGroups_Mid(CPlayerPedSAInterface* pPlayerPedSAInterface, int iReqMoveAnim)
{
SClientEntity<CPedSA>* pPedClientEntity = pGame->GetPools()->GetPed((DWORD*)pPlayerPedSAInterface);
CPed* pPed = pPedClientEntity ? pPedClientEntity->pEntity : nullptr;
if (pPed)
{
int iCustomAnim = pPed->GetCustomMoveAnim();
if (iCustomAnim)
{
// If iReqMoveAnim is jetpack, use iReqMoveAnim
if (IsAnimJetPack(iReqMoveAnim))
return iReqMoveAnim;
// If iCustomAnim is physique based without special pose, and iReqMoveAnim is physique based, then use pose from iReqMoveAnim and physique from
// iCustomAnim
if (IsAnimPhysiqueBased(iCustomAnim) && !IsAnimSpecialPose(iCustomAnim) && IsAnimPhysiqueBased(iReqMoveAnim))
{
int iPhysique = GetAnimPhysiqueIndex(iCustomAnim);
int iPose = GetAnimPose(iReqMoveAnim);
return iPose + iPhysique;
}
// Everything else, use iCustomAnim
return iCustomAnim;
}
}
return iReqMoveAnim;
}
// Hook info
#define HOOKPOS_CPlayerPed_ProcessAnimGroups_Mid 0x0609A44
#define HOOKSIZE_CPlayerPed_ProcessAnimGroups_Mid 6
DWORD RETURN_CPlayerPed_ProcessAnimGroups_Mid = 0x0609A4A;
void _declspec(naked) HOOK_CPlayerPed_ProcessAnimGroups_Mid()
{
_asm
{
pushad
push eax
push esi
call OnCPlayerPed_ProcessAnimGroups_Mid
mov [esp+0],eax // Put temp
add esp, 4*2
popad
mov eax,[esp-32-4*2] // Get temp
cmp [esi+4D4h], eax // pPed->iMoveAnim
jmp RETURN_CPlayerPed_ProcessAnimGroups_Mid
}
}
////////////////////////////////////////////////////////////////
//
// CClothes_GetDefaultPlayerMotionGroup
//
// Stop GTA overriding the physique of our custom move anim
// Returns MOVE_PLAYER, MOVE_PLAYER_F or MOVE_PLAYER_M
//
////////////////////////////////////////////////////////////////
__declspec(noinline) int _cdecl OnCClothes_GetDefaultPlayerMotionGroup(int iReqMoveAnim)
{
CPed* pPed = g_pCore->GetMultiplayer()->GetContextSwitchedPed();
if (pPed)
{
int iCustomAnim = pPed->GetCustomMoveAnim();
if (iCustomAnim)
{
if (IsAnimPhysiqueBased(iCustomAnim))
{
int iPhysique = GetAnimPhysiqueIndex(iCustomAnim);
int iBaseAnim = MOVE_PLAYER + iPhysique;
return iBaseAnim;
}
}
}
return iReqMoveAnim;
}
// Hook info
#define HOOKPOS_CClothes_GetDefaultPlayerMotionGroup 0x05A81B0
#define HOOKSIZE_CClothes_GetDefaultPlayerMotionGroup 5
DWORD RETURN_CClothes_GetDefaultPlayerMotionGroup = 0x05A81B5;
void _declspec(naked) HOOK_CClothes_GetDefaultPlayerMotionGroup()
{
_asm
{
mov eax, 0x05A7FB0 // CClothes::GetPlayerMotionGroupToLoad
call eax
pushad
push eax
call OnCClothes_GetDefaultPlayerMotionGroup
mov [esp+0],eax // Put temp
add esp, 4*1
popad
mov eax,[esp-32-4*1] // Get temp
jmp RETURN_CClothes_GetDefaultPlayerMotionGroup
}
}
//////////////////////////////////////////////////////////////////////////////////////////
//
// Setup hooks
//
//////////////////////////////////////////////////////////////////////////////////////////
void CPlayerPedSA::StaticSetHooks(void)
{
EZHookInstall(CPlayerPed_ProcessAnimGroups_Mid);
EZHookInstall(CClothes_GetDefaultPlayerMotionGroup);
}