-
Notifications
You must be signed in to change notification settings - Fork 0
/
AntiStall.as
507 lines (406 loc) · 14 KB
/
AntiStall.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
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
// TODO:
// - trains ans stuff
// - fake survival
// - disable hud
// - op4 boss damage not counted
// - trigger camera pause
void print(string text) { g_Game.AlertMessage( at_console, text); }
void println(string text) { print(text + "\n"); }
dictionary g_player_states;
dictionary g_monster_blacklist; // boring monsters that can't possibly be exciting
dictionary g_toggle_ents; // entities that trigger things (buttons, doors, etc.)
array<array<array<uint32>>> g_visited_zones;
array<ExcitementEnt> g_excitement_ents;
const int ZONES_PER_AXIS = 128;
const Vector MIN_ZONE_COORDS = Vector(-32768, -32768, -32768);
const float ZONE_SIZE = abs(MIN_ZONE_COORDS.x*2) / ZONES_PER_AXIS;
CCVar@ g_maxExcitement;
float g_excitement; // how interesting the game is to watch (0-100)
bool g_is_boring; // is excitement at 0?
bool g_is_kinda_boring; // excitement levels low but not dangerously low
float g_last_kinda_boring;
int g_keepHudOn = 0; // keep hud on for another tick so it's obvious why it's about to be hidden
bool g_was_enforcing = false;
class ExcitementEnt {
EHandle h_ent;
float lastHealth;
bool isKillable = false;
bool isToggleEnt = false;
bool everUsed = false;
bool wasAlive = true;
int oldToggleState = 0;
string name;
ExcitementEnt() {}
ExcitementEnt(EHandle h_ent) {
this.h_ent = h_ent;
name = h_ent.GetEntity().pev.classname;
if (g_toggle_ents.exists(name)) {
isToggleEnt = true;
oldToggleState = h_ent.GetEntity().GetToggleState();
} else {
isKillable = true;
wasAlive = true;
}
}
}
class PlayerState {
bool debug = false;
bool wasAlive = true;
PlayerState() {}
float checkExcitement(CBasePlayer@ plr) {
float newExcitement = 0;
Vector zoneCoords = plr.pev.origin - MIN_ZONE_COORDS;
int zoneX = int(zoneCoords.x / ZONE_SIZE);
int zoneY = int(zoneCoords.y / ZONE_SIZE);
int zoneZ = int(zoneCoords.z / ZONE_SIZE);
if (zoneX < 0 or zoneX >= ZONES_PER_AXIS or zoneY < 0 or zoneY >= ZONES_PER_AXIS or zoneZ < 0 or zoneZ >= ZONES_PER_AXIS) {
println("ZONE OUT OF BOUNDS");
} else if (plr.IsAlive()) {
if (g_visited_zones[zoneX][zoneY][zoneZ] & getPlayerBit(plr) == 0) {
if (g_visited_zones[zoneX][zoneY][zoneZ] == 0) {
newExcitement += 10;
debugMessage("[AntiStall] New area explored by " + plr.pev.netname + "! +10 excitement\n");
} else {
newExcitement += 5;
debugMessage("[AntiStall] " + plr.pev.netname + " hasn't been here yet! +5 excitement\n");
}
g_visited_zones[zoneX][zoneY][zoneZ] |= getPlayerBit(plr);
} else {
//println("ZONE " + zoneX + " " + zoneY + " " + zoneZ);
}
}
if (plr.IsAlive() && !wasAlive) {
newExcitement += 5;
debugMessage("[AntiStall] " + plr.pev.netname + " was revived! +5 excitement\n");
}
wasAlive = plr.IsAlive();
return newExcitement;
}
}
void add_excitement_ent(CBaseEntity@ ent) {
string cname = ent.pev.classname;
if (isMonster(ent)) {
if (g_monster_blacklist.exists(ent.pev.classname)) {
return;
}
g_excitement_ents.insertLast(ExcitementEnt(EHandle(ent)));
} else if (ent.IsBreakable()) {
g_excitement_ents.insertLast(ExcitementEnt(EHandle(ent)));
} else if (g_toggle_ents.exists(ent.pev.classname)) {
g_excitement_ents.insertLast(ExcitementEnt(EHandle(ent)));
}
}
bool isMonster(CBaseEntity@ ent) {
string cname = ent.pev.classname;
return (ent.IsMonster() && cname.Find("monster_") == 0) || cname.Find("geneworm_") == 0;
}
void reload_ents() {
g_excitement_ents.resize(0);
CBaseEntity@ent = null;
do {
@ent = g_EntityFuncs.FindEntityByClassname(ent, "*");
if (ent !is null) {
add_excitement_ent(ent);
}
} while(ent !is null);
}
HookReturnCode EntityCreated(CBaseEntity@ ent) {
add_excitement_ent(ent);
return HOOK_CONTINUE;
}
uint32 getPlayerBit(CBaseEntity@ plr) {
return (1 << (plr.entindex() & 31));
}
void PluginInit() {
g_Module.ScriptInfo.SetAuthor( "w00tguy" );
g_Module.ScriptInfo.SetContactInfo( "https://github.com/wootguy" );
g_Hooks.RegisterHook( Hooks::Game::EntityCreated, @EntityCreated );
g_Hooks.RegisterHook( Hooks::Player::ClientSay, @ClientSay );
g_Scheduler.SetInterval("update_excitement", 0.5f, -1);
@g_maxExcitement = CCVar("maxExcitement", 120, "Time before players are killed for making no progress", ConCommandFlag::AdminOnly);
g_monster_blacklist["monster_barney_dead"] = true;
g_monster_blacklist["monster_cockroach"] = true;
g_monster_blacklist["monster_furniture"] = true;
g_monster_blacklist["monster_handgrenade"] = true;
g_monster_blacklist["monster_hevsuit_dead"] = true;
g_monster_blacklist["monster_hgrunt_dead"] = true;
g_monster_blacklist["monster_human_grunt_ally_dead"] = true;
g_monster_blacklist["monster_leech"] = true;
g_monster_blacklist["monster_otis_dead"] = true;
g_monster_blacklist["monster_satchel"] = true;
g_monster_blacklist["monster_scientist_dead"] = true;
g_monster_blacklist["monster_sitting_scientist"] = true;
g_monster_blacklist["monster_tripmine"] = true;
g_monster_blacklist["monster_mortar"] = true;
g_monster_blacklist["monster_snark"] = true;
g_monster_blacklist["monster_babycrab"] = true;
g_toggle_ents["func_button"] = true;
g_toggle_ents["func_door"] = true;
g_toggle_ents["func_door_rotating"] = true;
g_toggle_ents["func_rot_button"] = true;
g_toggle_ents["func_rotating"] = true;
g_toggle_ents["func_train"] = true;
g_toggle_ents["func_tracktrain"] = true;
g_toggle_ents["func_wall_toggle"] = true;
g_toggle_ents["momentary_rot_button"] = true;
MapActivate();
println("ZONE SIZE: " + ZONE_SIZE);
}
void MapActivate() {
g_visited_zones.resize(0);
g_visited_zones.resize(ZONES_PER_AXIS);
for (uint x = 0; x < ZONES_PER_AXIS; x++) {
g_visited_zones[x].resize(0);
g_visited_zones[x].resize(ZONES_PER_AXIS);
for (uint y = 0; y < ZONES_PER_AXIS; y++) {
g_visited_zones[x][y].resize(0);
g_visited_zones[x][y].resize(ZONES_PER_AXIS);
}
}
g_excitement = g_maxExcitement.GetInt();
g_is_boring = false;
g_last_kinda_boring = -999;
g_is_kinda_boring = false;
reload_ents();
}
PlayerState@ getPlayerState(CBasePlayer@ plr) {
string steamId = g_EngineFuncs.GetPlayerAuthId( plr.edict() );
if (steamId == 'STEAM_ID_LAN') {
steamId = plr.pev.netname;
}
if ( !g_player_states.exists(steamId) ) {
PlayerState state;
g_player_states[steamId] = state;
}
return cast<PlayerState@>( g_player_states[steamId] );
}
void update_excitement() {
bool should_enforce = shouldEnforceExcitement();
if (should_enforce) {
if (!g_was_enforcing) {
g_excitement = g_maxExcitement.GetInt();
debugMessage("[AntiStall] Excitement is now enforced and limited to " + g_maxExcitement.GetInt() + ".\n");
}
g_was_enforcing = true;
} else {
if (g_was_enforcing) {
debugMessage("[AntiStall] Excitement is no longer enforced nor limited.\n");
}
g_was_enforcing = false;
}
g_excitement -= 0.5f;
//g_excitement -= 5.0f;
if (g_excitement < 0) {
g_excitement = 0;
}
for (int i = 1; i <= g_Engine.maxClients; i++) {
CBasePlayer@ plr = g_PlayerFuncs.FindPlayerByIndex(i);
if (plr is null or !plr.IsConnected()) {
continue;
}
PlayerState@ state = getPlayerState(plr);
g_excitement += state.checkExcitement(plr);
}
array<ExcitementEnt> new_ents;
for (uint i = 0; i < g_excitement_ents.size(); i++) {
ExcitementEnt@ exciteEnt = @g_excitement_ents[i];
CBaseEntity@ ent = exciteEnt.h_ent;
CBaseToggle@ toggle = cast<CBaseToggle@>(ent);
if (ent is null) {
if (exciteEnt.isKillable && exciteEnt.wasAlive) {
int exciteAmount = int(exciteEnt.name.Find("monster_")) != -1 ? 30 : 10;
g_excitement += exciteAmount;
debugMessage("[AntiStall] " + exciteEnt.name + " was killed! +" + exciteAmount + " excitement\n");
}
continue;
}
if (isMonster(ent) and exciteEnt.wasAlive) {
if (ent.IRelationshipByClass(CLASS_PLAYER) <= 0) {
continue; // ignore allies
}
float damage = exciteEnt.lastHealth - ent.pev.health;
if (!ent.IsAlive()) {
g_excitement += 30;
debugMessage("[AntiStall] " + exciteEnt.name + " was killed! +30 excitement\n");
}
else if (damage >= 40) {
g_excitement += 10;
debugMessage("[AntiStall] " + ent.pev.classname + " took heavy damage! +10 excitement\n");
}
else if (damage >= 1) {
g_excitement += 5;
debugMessage("[AntiStall] " + ent.pev.classname + " took minor damage! +5 excitement\n");
}
exciteEnt.lastHealth = ent.pev.health;
exciteEnt.wasAlive = ent.IsAlive();
}
else if (exciteEnt.isToggleEnt and !exciteEnt.everUsed) {
if (ent.GetToggleState() != exciteEnt.oldToggleState) {
exciteEnt.everUsed = true;
bool isReallyCool = int(string(ent.pev.classname).Find("button")) != -1;
int neatnessLevel = isReallyCool ? 30 : 10;
g_excitement += neatnessLevel;
debugMessage("[AntiStall] " + ent.pev.classname + " was used for the first time! +" + neatnessLevel + " excitement\n");
}
}
new_ents.insertLast(g_excitement_ents[i]);
}
g_excitement_ents = new_ents;
if (g_excitement > g_maxExcitement.GetInt() && should_enforce) {
g_excitement = g_maxExcitement.GetInt();
}
debug_excitement();
if (g_excitement < 20 && should_enforce) {
if (!g_is_kinda_boring) {
bool isSpammy = g_Engine.time - g_last_kinda_boring < 60;
//g_PlayerFuncs.ClientPrintAll(isSpammy ? HUD_PRINTNOTIFY : HUD_PRINTTALK, "[AntiStall] Warning: Excitement level is below 20.\n");
g_PlayerFuncs.ClientPrintAll(HUD_PRINTNOTIFY, "[AntiStall] Warning: Excitement level is below 20.\n");
g_last_kinda_boring = g_Engine.time;
}
g_is_kinda_boring = true;
} else {
g_is_kinda_boring = false;
}
if (g_excitement <= 0 && should_enforce) {
if (!g_is_boring) {
g_is_boring = true;
g_PlayerFuncs.ClientPrintAll(HUD_PRINTTALK, "[AntiStall] Excitement level is 0. Make progress or die.\n");
g_Scheduler.SetTimeout("checkBoringRestart", 10.0f);
}
}
}
void doBoringRestart() {
g_PlayerFuncs.ClientPrintAll(HUD_PRINTNOTIFY, "[AntiStall] Say \".boring\" to see how the plugin calculates excitement.\n");
g_is_boring = false;
}
void checkBoringRestart() {
if (g_excitement < 10) {
g_PlayerFuncs.ClientPrintAll(HUD_PRINTTALK, "[AntiStall] Excitement level is still below 10. Time to die.\n");
for (int i = 1; i <= g_Engine.maxClients; i++) {
CBasePlayer@ plr = g_PlayerFuncs.FindPlayerByIndex(i);
if (plr is null or !plr.IsConnected()) {
continue;
}
if (plr.IsAlive()) {
g_EntityFuncs.Remove(plr);
}
}
g_Scheduler.SetTimeout("doBoringRestart", 5.0f);
} else {
g_is_boring = false;
g_PlayerFuncs.ClientPrintAll(HUD_PRINTTALK, "[AntiStall] Excitement level is at " + int(g_excitement) + ". Punishment cancelled.\n");
}
}
void debugMessage(string msg) {
for (int i = 1; i <= g_Engine.maxClients; i++) {
CBasePlayer@ plr = g_PlayerFuncs.FindPlayerByIndex(i);
if (plr is null or !plr.IsConnected()) {
continue;
}
PlayerState@ state = getPlayerState(plr);
if (state.debug) {
g_PlayerFuncs.ClientPrint(plr, HUD_PRINTNOTIFY, msg);
}
}
}
bool areSomeDead() {
int totalPlayers = 0;
int totalDead = 0;
for (int i = 1; i <= g_Engine.maxClients; i++) {
CBasePlayer@ plr = g_PlayerFuncs.FindPlayerByIndex(i);
if (plr is null or !plr.IsConnected()) {
continue;
}
totalPlayers += 1;
if (!plr.IsAlive()) {
totalDead += 1;
}
}
return totalPlayers > 0 and totalDead > 0 and totalDead < totalPlayers;
}
bool shouldEnforceExcitement() {
return g_SurvivalMode.IsActive() && areSomeDead();
}
string formatInteger(int ival) {
string val = "" + ival;
string formatted;
for (int i = int(val.Length())-1, k = 0; i >= 0; i--, k++) {
if (k % 3 == 0 && k != 0) {
formatted = "," + formatted;
}
formatted = "" + val[i] + formatted;
}
return formatted;
}
void debug_excitement() {
bool shouldHideHud = !shouldEnforceExcitement() or g_excitement > 60;
if (shouldHideHud) {
if (g_keepHudOn > 0) {
g_keepHudOn -= 1;
shouldHideHud = false;
}
} else {
g_keepHudOn = 2;
}
for (int i = 1; i <= g_Engine.maxClients; i++) {
CBasePlayer@ plr = g_PlayerFuncs.FindPlayerByIndex(i);
if (plr is null or !plr.IsConnected()) {
continue;
}
PlayerState@ state = getPlayerState(plr);
if (!state.debug and shouldHideHud) {
continue;
}
HUDTextParams params;
params.effect = 0;
params.fadeinTime = 0;
params.fadeoutTime = 0.1;
params.holdTime = 1.0f;
if (g_excitement < 10) {
params.r1 = 255;
params.g1 = 16;
params.b1 = 16;
} else if (g_excitement < 20) {
params.r1 = 255;
params.g1 = 128;
params.b1 = 16;
}
params.x = -1;
params.y = 0.92;
params.channel = 4;
string info = "Excitement: " + formatInteger(int(g_excitement));
if (!shouldEnforceExcitement()) {
info += "\n(not enforced)";
params.r1 = 96;
params.g1 = 16;
params.b1 = 255;
}
g_PlayerFuncs.HudMessage(plr, params, info);
}
}
bool doCommand(CBasePlayer@ plr, const CCommand@ args) {
PlayerState@ state = getPlayerState(plr);
if ( args.ArgC() > 0 ) {
if ( args[0] == ".boring" ) {
state.debug = !state.debug;
g_PlayerFuncs.ClientPrint(plr, HUD_PRINTTALK, "[AntiStall] Debug mode " + (state.debug ? "ENABLED" : "DISABLED") + ".\n");
return true;
}
}
return false;
}
HookReturnCode ClientSay( SayParameters@ pParams ) {
CBasePlayer@ plr = pParams.GetPlayer();
const CCommand@ args = pParams.GetArguments();
if (doCommand(plr, args)) {
pParams.ShouldHide = true;
return HOOK_HANDLED;
}
return HOOK_CONTINUE;
}
CClientCommand _boring("boring", "AntiStall info", @consoleCmd );
void consoleCmd( const CCommand@ args )
{
CBasePlayer@ plr = g_ConCommandSystem.GetCurrentPlayer();
doCommand(plr, args);
}