-
Notifications
You must be signed in to change notification settings - Fork 1
/
respawndead_keepweapons.as
194 lines (156 loc) · 7.17 KB
/
respawndead_keepweapons.as
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
/* respawndead_keepweapons
Adds a feature for trigger_respawn to let dead players respawn with the weapons they collected when they died, instead of losing them
Option to keep ammo as well
Install:-
Put the script file into "scripts/maps/beast" then either
- put "map_script respawndead_keepweapons" to your map cfg
OR
-Add this as a trigger_script
"classname" "trigger_script"
"m_iszScriptFile" "respawndead_keepweapons"
OR
-Include it in your main map script header
#include "beast/respawndead_keepweapons"
Usage:-
- make your trigger_respawn, check the flag "Respawn dead" then
- check flag box 4 (value 8)- this is the new flag to allow the trigger_respawn to let dead respawning players keep their weapons they had when they died
- To keep ammo, check box 5 (value 16)
*/
namespace RESPAWNDEAD_KEEPWEAPONS
{
enum respawndead_flags
{
KEEP_WEAPONS = 8,
KEEP_AMMO = 16,
RESET_LOADOUT = 32
};
CScheduledFunction@ fnPatchTriggerRespawn = g_Scheduler.SetTimeout( "PatchTriggerRespawn", 0.0f );
bool blPlayerKilled = g_Hooks.RegisterHook( Hooks::Player::PlayerKilled, PlayerKilled );
array<dictionary> DICT_PLAYER_LOADOUT( g_Engine.maxClients + 1 );
void PatchTriggerRespawn()
{
dictionary dictDeadRespawner =
{
{ "m_iszScriptFunctionName", "RESPAWNDEAD_KEEPWEAPONS::RespawnDead" },
{ "m_iMode", "1" },
{ "targetname", "ts_dead_respawner" }
};
CBaseEntity@ pEntity;
while( ( ( @pEntity = g_EntityFuncs.FindEntityByClassname( pEntity, "trigger_respawn" ) ) !is null ) )
{
if( pEntity is null )
continue;
if( pEntity.pev.SpawnFlagBitSet( 1 << 1 ) && pEntity.pev.SpawnFlagBitSet( KEEP_WEAPONS ) )
{
if( pEntity.pev.SpawnFlagBitSet( 1 << 0 ) && pEntity.pev.target != "" )
dictDeadRespawner["netname"] = string( pEntity.pev.target );
pEntity.pev.spawnflags &= ~( 1 << 0 | 1 << 1 );// disable respawn dead setting, trigger_script will do this now
pEntity.pev.target = string( dictDeadRespawner["targetname"] );
}
}
g_EntityFuncs.CreateEntity( "trigger_script", dictDeadRespawner );
}
// Replace trigger_respawn's dead respawner with our own
void RespawnDead(CBaseEntity@ pActivator, CBaseEntity@ pCaller, USE_TYPE useType, float flValue)
{
const bool
blKeepAmmo = pCaller !is null ? pCaller.pev.SpawnFlagBitSet( KEEP_AMMO ) : false,
blResetLoadout = pCaller !is null ? pCaller.pev.SpawnFlagBitSet( RESET_LOADOUT ) : false;
if( pCaller.pev.netname != "!activator" )
{
for( int iPlayer = 1; iPlayer <= g_PlayerFuncs.GetNumPlayers(); iPlayer++ )
{
CBasePlayer@ pPlayer = g_PlayerFuncs.FindPlayerByIndex( iPlayer );
if( pPlayer is null || !pPlayer.IsConnected() || pPlayer.IsAlive() )
continue;
if( pCaller.pev.netname != "" && pPlayer.GetTargetname() != pCaller.pev.netname )
continue;
g_PlayerFuncs.RespawnPlayer( pPlayer, false, true );
ReEquipCollected( pPlayer, blKeepAmmo, blResetLoadout );
}
}
else if( pActivator !is null )
{
if( !pActivator.IsPlayer() || pActivator.IsAlive() )
return;
g_PlayerFuncs.RespawnPlayer( cast<CBasePlayer@>( pActivator ), false, true );
ReEquipCollected( pActivator, pCaller !is null ? pCaller.pev.SpawnFlagBitSet( KEEP_AMMO ) : false );
}
}
// Players get their old loadout when they died
void ReEquipCollected(EHandle hPlayer, bool blKeepAmmo = false, bool blResetLoadout = false)
{
if( !hPlayer )
return;
CBasePlayer@ pPlayer = cast<CBasePlayer@>( hPlayer.GetEntity() );
array<string> STR_LOADOUT_WEAPONS = DICT_PLAYER_LOADOUT[pPlayer.entindex()].getKeys();
if( blResetLoadout )
pPlayer.RemoveAllItems( false );
for( uint i = 0; i < STR_LOADOUT_WEAPONS.length(); i++ )
{
if( STR_LOADOUT_WEAPONS[i] == "" )
continue;
if( STR_LOADOUT_WEAPONS[i] == "item_longjump" )
{
if( bool( DICT_PLAYER_LOADOUT[pPlayer.entindex()]["item_longjump"] ) && !pPlayer.m_fLongJump )
{
pPlayer.m_fLongJump = true;
g_EngineFuncs.GetPhysicsKeyBuffer( pPlayer.edict() ).SetValue( "slj", "1" );
}
continue;
}
if( STR_LOADOUT_WEAPONS[i] == "item_suit" )
{
if( bool( DICT_PLAYER_LOADOUT[pPlayer.entindex()]["item_suit"] ) && !pPlayer.HasSuit() )
pPlayer.SetHasSuit( true );
continue;
}
if( pPlayer.HasNamedPlayerItem( STR_LOADOUT_WEAPONS[i] ) is null )
pPlayer.GiveNamedItem( STR_LOADOUT_WEAPONS[i] ); // Would be nice if this returned the actual item ptr...
CBasePlayerWeapon@ pEquippedWeapon = cast<CBasePlayerWeapon@>( pPlayer.HasNamedPlayerItem( STR_LOADOUT_WEAPONS[i] ) );
const Vector2D vec2DAmmoValues = Vector2D( DICT_PLAYER_LOADOUT[pPlayer.entindex()][STR_LOADOUT_WEAPONS[i]] );
if( pEquippedWeapon is null )
continue;
if( blKeepAmmo && pEquippedWeapon.m_iPrimaryAmmoType > 0 )
{
pPlayer.m_rgAmmo( pEquippedWeapon.m_iPrimaryAmmoType, int( vec2DAmmoValues.x ) );
if( int( vec2DAmmoValues.y ) > 0 )
pPlayer.m_rgAmmo( pEquippedWeapon.m_iSecondaryAmmoType, int( vec2DAmmoValues.y ) );
}
}
DICT_PLAYER_LOADOUT[pPlayer.entindex()] = dictionary();
}
dictionary GetPlayerLoadout(EHandle hPlayer)
{
if( !hPlayer )
return dictionary();
CBasePlayer@ pPlayer = cast<CBasePlayer@>( hPlayer.GetEntity() );
dictionary dictLoadout =
{
{ "item_suit", pPlayer.HasSuit() },
{ "item_longjump", pPlayer.m_fLongJump }
};
for( uint i = 0; i < MAX_ITEM_TYPES; i++ )
{
CBasePlayerWeapon@ pWeapon = cast<CBasePlayerWeapon@>( pPlayer.m_rgpPlayerItems( i ) );
while( pWeapon !is null )
{
const string strWeapon = pWeapon.GetClassname() == "weapon_uzi" && pWeapon.m_fIsAkimbo ? "weapon_uziakimbo" : pWeapon.GetClassname();
dictLoadout[strWeapon] = pWeapon.m_iPrimaryAmmoType < 0 ? Vector2D() :
Vector2D( pPlayer.m_rgAmmo( pWeapon.m_iPrimaryAmmoType ), pWeapon.m_iSecondaryAmmoType > 0 ?
pPlayer.m_rgAmmo( pWeapon.m_iSecondaryAmmoType ) :
0 );
@pWeapon = cast<CBasePlayerWeapon@>( pWeapon.m_hNextItem.GetEntity() );
}
}
return dictLoadout;
}
// Save player loadout upon death
HookReturnCode PlayerKilled(CBasePlayer@ pPlayer, CBaseEntity@ pAttacker, int iGib)
{
if( pPlayer is null )
return HOOK_CONTINUE;
DICT_PLAYER_LOADOUT[pPlayer.entindex()] = GetPlayerLoadout( pPlayer );
return HOOK_CONTINUE;
}
}