-
Notifications
You must be signed in to change notification settings - Fork 1
/
pvp_mode.as
430 lines (351 loc) · 15.4 KB
/
pvp_mode.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
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
/* Sven Co-op PVP Deathmatch Mode Script
- by Outerbeast
Script for enabling friendly fire between players for pvp deathmatch maps
Useful for Half-Life Deathmatch map ports to SC
Usage:-
- Put this script in scripts/maps folder
- In your map cfg file put this code in to enable
map_script pvp_mode
- Add your optional cvars
Map cfg settings:-
"map_script pvp_mode" - install the script to the map
"as_command pvp_spawnprotecttime" - set the time duration in seconds for how long spawn invulnerbility lasts, by default this is 5 if not set
"as_command pvp_viewmode" - set the force viewmode, 0 for first person or 1 for third person, by default this is first person if not set
"as_command pvp_killinfo" - enable or disable hud kill info, enabled by default
Chat commands:-
!pvp_stats/info - show your stats
!pvp_leave/spectate - enter Spectator mode
!pvp_join/play/afk - exit Spectator mode
Issues:-
- Only supports 17 player slots maximum because of limitations imposed by the game + API. 18th player and following will automatically
be moved to Spectator mode until a slot becomes free.
- Some players will have colored usernames in the scoreboard, this is due to the classification system assigning them to
TEAM classifications which apply these colors.
*/
namespace PVP_MODE
{
enum SpectatorType
{
SPECTYPE_NONE = 0,
SPECTYPE_WAITING,
SPECTYPE_VOLUNTARY
};
array<bool> BL_PLAYER_SLOT( g_Engine.maxClients + 1 );
array<CLASS> PLAYER_TEAMS =
{
CLASS_MACHINE,
CLASS_PLAYER,
CLASS_HUMAN_MILITARY,
CLASS_ALIEN_MILITARY,
CLASS_ALIEN_PASSIVE,
CLASS_ALIEN_MONSTER,
CLASS_ALIEN_PREY,
CLASS_ALIEN_PREDATOR,
CLASS_INSECT,
CLASS_PLAYER_BIOWEAPON,
CLASS_XRACE_PITDRONE,
CLASS_XRACE_SHOCK,
CLASS_TEAM1,
CLASS_TEAM2,
CLASS_TEAM3,
CLASS_TEAM1,
CLASS_BARNACLE
};
HUDTextParams txtWinner, txtLoser, txtStats;
int8 iTotalTeams = PLAYER_TEAMS.length();
const int iForceRespawnDefault = int( g_EngineFuncs.CVarGetFloat( "mp_forcerespawn" ) );
CCVar
cvarProtectDuration( "pvp_spawnprotecttime", 10.0f, "Duration of spawn invulnerability", ConCommandFlag::AdminOnly ),
cvarViewModeSetting( "pvp_viewmode", 0.0f, "View mode setting", ConCommandFlag::AdminOnly ),
cvarKillInfoSetting( "pvp_killinfo", 1.0f, "Kill hud info", ConCommandFlag::AdminOnly );
CScheduledFunction@ fnThink, fnInit = g_Scheduler.SetTimeout( "Initialise", 0.1f );
void Initialise()
{
if( PLAYER_TEAMS.length() < uint( g_Engine.maxClients + 1 ) )
PLAYER_TEAMS.resize( g_Engine.maxClients + 1 );
if( iForceRespawnDefault < 1 )
g_EngineFuncs.CVarSetFloat( "mp_forcerespawn", 1 );
txtWinner.y = txtLoser.y = 0.6;
txtLoser.r1 = txtLoser.r2 = 128;
txtWinner.r1 = 0;
txtLoser.g1 = 0;
txtWinner.g1 = txtWinner.g2 = 128;
txtWinner.b1 = txtLoser.b1 = 0;
txtWinner.effect = txtLoser.effect = 0;
txtWinner.fadeinTime = txtLoser.fadeinTime = 0;
txtWinner.fadeoutTime = txtLoser.fadeoutTime = 0;
txtWinner.holdTime = txtLoser.holdTime = 3;
txtWinner.channel = 5;
txtLoser.channel = 7;
txtStats.x = 0.7;
txtStats.y = 0.7;
txtStats.a1 = 0;
txtStats.r2 = 250;
txtStats.g2 = 250;
txtStats.b2 = 250;
txtStats.a2 = 1;
txtStats.fadeinTime = 0.0;
txtStats.fadeoutTime = 0.0;
txtStats.holdTime = 10.0;
txtStats.fxTime = 0.0;
g_Hooks.RegisterHook( Hooks::Player::PlayerSpawn, PlayerSpawn );
g_Hooks.RegisterHook( Hooks::Player::PlayerUse, PlayerUse );
g_Hooks.RegisterHook( Hooks::Player::PlayerTakeDamage, PlayerTakeDamage );
g_Hooks.RegisterHook( Hooks::Player::PlayerKilled, PlayerKilled );
g_Hooks.RegisterHook( Hooks::Player::ClientSay, PlayerChatCommand );
g_Hooks.RegisterHook( Hooks::Player::ClientDisconnect, PlayerLeave );
@fnThink = g_Scheduler.SetInterval( "Think", 0.01f, g_Scheduler.REPEAT_INFINITE_TIMES );
}
CLASS AssignTeam(EHandle hPlayer, const bool blSetTeam)
{
if( !hPlayer )
return CLASS_NONE;
CBasePlayer@ pPlayer = cast<CBasePlayer@>( hPlayer.GetEntity() );
if( blSetTeam && BL_PLAYER_SLOT.find( false ) >= 0 )
{
pPlayer.SetClassification( PLAYER_TEAMS[BL_PLAYER_SLOT.find( false )] );
BL_PLAYER_SLOT[PLAYER_TEAMS.find( CLASS( pPlayer.m_iClassSelection ) )] = true;
}
else if( !blSetTeam ) // For when we want to remove classification from player
{
BL_PLAYER_SLOT[PLAYER_TEAMS.find( CLASS( pPlayer.m_iClassSelection ) )] = false;
pPlayer.SetClassification( CLASS_FORCE_NONE );
}
return CLASS( pPlayer.m_iClassSelection );
}
void SpawnProtection(EHandle hPlayer, const int iTakeDamageIn)
{
if( !hPlayer )
return;
CBasePlayer@ pPlayer = cast<CBasePlayer@>( hPlayer.GetEntity() );
if( pPlayer is null || pPlayer.m_iClassSelection < CLASS_MACHINE )
return;
pPlayer.pev.takedamage = float( iTakeDamageIn );
switch( int( pPlayer.pev.takedamage ) )
{
case DAMAGE_NO:
{
pPlayer.SetMaxSpeedOverride( 0 );
pPlayer.pev.rendermode = kRenderTransTexture;
pPlayer.pev.renderamt = 100.0f;
g_Scheduler.SetTimeout( "SpawnProtection", cvarProtectDuration.GetFloat(), EHandle( pPlayer ), int( DAMAGE_YES ) );
break;
}
case DAMAGE_YES:
{
pPlayer.SetMaxSpeedOverride( -1 );
pPlayer.pev.rendermode = pPlayer.m_iOriginalRenderMode;
pPlayer.pev.renderamt = pPlayer.m_flOriginalRenderAmount;
break;
}
}
}
void EnterSpectator(EHandle hPlayer, const bool blSpectatorOverride)
{
if( !hPlayer )
return;
CBasePlayer@ pPlayer = cast<CBasePlayer@>( hPlayer.GetEntity() );
if( pPlayer is null || !pPlayer.IsConnected() || pPlayer.GetObserver().IsObserver() )
return;
if( !blSpectatorOverride )
{
pPlayer.GetUserData( "i_spectating" ) = SPECTYPE_WAITING;
pPlayer.SetMaxSpeedOverride( -1 );
pPlayer.GetObserver().StartObserver( pPlayer.GetOrigin(), pPlayer.pev.angles, false );
pPlayer.GetObserver().SetObserverModeControlEnabled( true );
pPlayer.RemoveAllItems( true, true );
g_PlayerFuncs.ShowMessage( pPlayer, "SPECTATING\nNo player slots available. Please wait..." );
} // !-BUG-! - Index out of bounds in 2 player slot server - why???? Hence this check.
else if( blSpectatorOverride && g_Engine.maxClients > 2 )
{
pPlayer.GetUserData( "i_spectating" ) = SPECTYPE_VOLUNTARY;
AssignTeam( EHandle( pPlayer ), false );
pPlayer.SetMaxSpeedOverride( -1 );
pPlayer.pev.frags = 0;
pPlayer.GetObserver().StartObserver( pPlayer.GetOrigin(), pPlayer.pev.angles, false );
pPlayer.GetObserver().SetObserverModeControlEnabled( true );
pPlayer.RemoveAllItems( true, true );
g_PlayerFuncs.ShowMessage( pPlayer, "You are in Spectator mode.\n\nType '!pvp_join' to exit." );
}
}
void Think()
{
for( int iPlayer = 1; iPlayer <= g_Engine.maxClients; iPlayer++ )
{
CBasePlayer@ pPlayer = g_PlayerFuncs.FindPlayerByIndex( iPlayer );
if( pPlayer is null )
continue;
if( pPlayer.IsAlive() )
pPlayer.SetViewMode( PlayerViewMode( cvarViewModeSetting.GetInt() ) );// Utterly retarded. Forced to cast to enum and not the actual value.
else if( pPlayer.GetObserver().IsObserver() )
pPlayer.pev.nextthink = g_Engine.time + 1.0f;
if( pPlayer.pev.FlagBitSet( FL_GODMODE ) )
pPlayer.pev.flags &= ~FL_GODMODE;
}
}
// ========================= Hook Funcs - Runs the entire bloody thing ========================= //
HookReturnCode PlayerSpawn(CBasePlayer@ pPlayer)
{
if( pPlayer is null || !pPlayer.IsConnected() || !pPlayer.IsAlive() )
return HOOK_CONTINUE;
pPlayer.GetUserData()["i_spectating"] = SPECTYPE_NONE;
if( AssignTeam( pPlayer, true ) < CLASS_MACHINE )
{
EnterSpectator( pPlayer, false );
return HOOK_CONTINUE;
}
g_Scheduler.SetTimeout( "SpawnProtection", 0.01f, EHandle( pPlayer ), int( DAMAGE_NO ) ); // Why delay? Because rendering won't apply on spawn - but WHY.
return HOOK_CONTINUE;
}
HookReturnCode PlayerUse(CBasePlayer@ pPlayer, uint& out uiFlags)
{
if( pPlayer is null || !pPlayer.IsConnected() || !pPlayer.IsAlive() )
return HOOK_CONTINUE;
if( pPlayer.GetMaxSpeedOverride() == 0 &&
pPlayer.m_afButtonPressed & (
IN_DUCK |
IN_JUMP |
IN_USE |
IN_ATTACK |
IN_ATTACK2 |
IN_ALT1 |
IN_FORWARD |
IN_BACK |
IN_MOVELEFT |
IN_MOVERIGHT ) != 0 )
SpawnProtection( pPlayer, int( DAMAGE_YES ) );
return HOOK_CONTINUE;
}
HookReturnCode PlayerChatCommand(SayParameters@ pParams)
{
if( pParams is null )
return HOOK_CONTINUE;
CBasePlayer@ pPlayer = pParams.GetPlayer();
const CCommand@ cmdArgs = pParams.GetArguments();
if( pPlayer is null || !pPlayer.IsConnected() )
return HOOK_CONTINUE;
// Remove the coloration of player chat messages if they are in TEAM1-4
if( pPlayer.m_iClassSelection >= CLASS_TEAM1 && pPlayer.m_iClassSelection <= CLASS_TEAM4 )
{
pParams.set_ShouldHide( true ); // "Remove" the original chat message
g_PlayerFuncs.SayTextAll( pPlayer, "" + pPlayer.pev.netname + ": " + cmdArgs.GetCommandString() ); // Replace with decoy
}
if( cmdArgs.ArgC() < 1 || cmdArgs[0][0] != "!pvp_" )
return HOOK_CONTINUE;
pParams.set_ShouldHide( true );
if( cmdArgs[0] == "!pvp_leave" || cmdArgs[0] == "!pvp_spectate" || cmdArgs[0] == "!pvp_afk" )
{
if( !pPlayer.GetObserver().IsObserver() )
{
EnterSpectator( EHandle( pPlayer ), true );
return HOOK_HANDLED;
}
else
{
g_PlayerFuncs.SayText( pPlayer, "You are already spectating. Type '!pvp_join' to start playing." );
return HOOK_HANDLED;
}
}
if( cmdArgs[0] == "!pvp_join" || cmdArgs[0] == "!pvp_play" )
{
if( pPlayer.GetObserver().IsObserver() )
{
if( BL_PLAYER_SLOT.find( false ) <= iTotalTeams )
pPlayer.GetObserver().StopObserver( true );
else
g_PlayerFuncs.SayText( pPlayer, "There are no free slots available yet. Please try again later." );
return HOOK_HANDLED;
}
else
{
g_PlayerFuncs.SayText( pPlayer, "You are already playing. Type '!pvp_spectate' to enter Spectator mode." );
return HOOK_HANDLED;
}
}
if( ( cmdArgs[0] == "!pvp_stats" || cmdArgs[0] == "!pvp_info" ) && !pPlayer.GetObserver().IsObserver() )
{
string
strMyWeapon = pPlayer.m_hActiveItem ? pPlayer.m_hActiveItem.GetEntity().GetClassname().Replace( "weapon_", "" ) : "No weapon selected",
strStats = " -Team: " + pPlayer.m_iClassSelection + " - " + pPlayer.GetClassificationName() + "\n -Points: " + pPlayer.pev.frags + "\n -Deaths: " + pPlayer.m_iDeaths + "\n -Weapon: " + strMyWeapon + "\n";
g_PlayerFuncs.HudMessage( pPlayer, txtStats, "" + pPlayer.pev.netname + "'s Stats:-\n" + strStats );
return HOOK_HANDLED;
}
return HOOK_CONTINUE;
}
HookReturnCode PlayerTakeDamage(DamageInfo@ pDamageInfo)
{
if( pDamageInfo is null || pDamageInfo.pVictim is null || pDamageInfo.pAttacker is null || pDamageInfo.pInflictor is null )
return HOOK_CONTINUE;
// Prevents goomba-stomp killage
if( pDamageInfo.bitsDamageType & DMG_CRUSH != 0 && ( pDamageInfo.pAttacker.IsPlayer() || pDamageInfo.pInflictor.IsPlayer() ) )
pDamageInfo.flDamage = 0.0f;
return HOOK_CONTINUE;
}
HookReturnCode PlayerKilled(CBasePlayer@ pPlayer, CBaseEntity@ pAttacker, int iGib)
{
if( pPlayer is null || pAttacker is null )
return HOOK_CONTINUE;
AssignTeam( EHandle( pPlayer ), false );
CBasePlayer@ pAttackingPlayer = cast<CBasePlayer@>( pAttacker );
string strAttackerWeapon = pAttackingPlayer.m_hActiveItem ? " with: " + pAttackingPlayer.m_hActiveItem.GetEntity().GetClassname().Replace( "weapon_", "" ) : "";
int iDamageDone = pPlayer.m_lastPlayerDamageAmount >= int( pPlayer.pev.dmg_take ) ? pPlayer.m_lastPlayerDamageAmount : int( pPlayer.pev.dmg_take );
if( pAttackingPlayer !is pPlayer && cvarKillInfoSetting.GetInt() > 0 )
{
g_PlayerFuncs.HudMessage( pAttackingPlayer, txtWinner, "You killed\n\n" + string( pPlayer.pev.netname ).ToUppercase() + "\n" + strAttackerWeapon + "\n Damage done: " + iDamageDone + "\n" );
g_PlayerFuncs.HudMessage( pPlayer, txtLoser, "" + string( pAttackingPlayer.pev.netname ).ToUppercase() + "\nkilled you" + strAttackerWeapon + "\n Damage taken: " + iDamageDone + "\n" );
}
else if( pAttackingPlayer is pPlayer ) // Case player suicided
{ // True suicide, and not done via weapon self-damage
if( pAttackingPlayer.pev.dmg_inflictor is pAttackingPlayer.edict() ) // !-BUG-!: Suicides via "kill" in console can sometimes cause a weapon be used as the dmg_inflictor and this check is false.
g_Scheduler.SetTimeout( "EnterSpectator", 0.1f, EHandle( pPlayer ), true );
}
return HOOK_CONTINUE;
}
HookReturnCode PlayerLeave(CBasePlayer@ pDisconnectedPlayer)
{
if( pDisconnectedPlayer is null )
return HOOK_CONTINUE;
AssignTeam( pDisconnectedPlayer, false );
pDisconnectedPlayer.ClearClassification();
pDisconnectedPlayer.pev.frags = 0;
for( int iPlayer = 1; iPlayer <= g_Engine.maxClients; iPlayer++ )
{
if( BL_PLAYER_SLOT.find( false ) < 0 )
break;
CBasePlayer@ pObserverPlayer = g_PlayerFuncs.FindPlayerByIndex( iPlayer );
if( pObserverPlayer is null || !pObserverPlayer.GetObserver().IsObserver() )
continue;
// Skip players who are spectating voluntarily
if( SpectatorType( pObserverPlayer.GetUserData( "i_spectating" ) ) == SPECTYPE_VOLUNTARY )
continue;
pObserverPlayer.GetObserver().StopObserver( true );
pObserverPlayer.GetUserData( "i_spectating" ) = SPECTYPE_NONE;
}
return HOOK_CONTINUE;
}
void Disable(CBaseEntity@ pActivator, CBaseEntity@ pCaller, USE_TYPE useType, float flValue)
{
g_EngineFuncs.CVarSetFloat( "mp_forcerespawn", iForceRespawnDefault );
g_Hooks.RemoveHook( Hooks::Player::PlayerSpawn, PlayerSpawn );
g_Hooks.RemoveHook( Hooks::Player::PlayerUse, PlayerUse );
g_Hooks.RemoveHook( Hooks::Player::PlayerTakeDamage, PlayerTakeDamage );
g_Hooks.RemoveHook( Hooks::Player::PlayerKilled, PlayerKilled );
g_Hooks.RemoveHook( Hooks::Player::ClientSay, PlayerChatCommand );
g_Hooks.RemoveHook( Hooks::Player::ClientDisconnect, PlayerLeave );
if( fnThink !is null )
g_Scheduler.RemoveTimer( fnThink );
BL_PLAYER_SLOT = array<bool>( g_Engine.maxClients + 1, false );
for( int iPlayer = 1; iPlayer <= g_Engine.maxClients; iPlayer++ )
{
if( g_PlayerFuncs.FindPlayerByIndex( iPlayer ) is null )
continue;
g_PlayerFuncs.FindPlayerByIndex( iPlayer ).pev.frags = 0.0f;
g_PlayerFuncs.FindPlayerByIndex( iPlayer ).SetClassification( CLASS_PLAYER );
g_PlayerFuncs.FindPlayerByIndex( iPlayer ).GetUserData( "i_spectating" ) = SPECTYPE_NONE;
}
}
}
/* Special thanks to
- Zode, H2 and Neo for scripting help
AlexCorruptor for testing and building a test map
Gauna, SV BOY, Jumpy for helping test */