-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgunGame.cpp
1090 lines (1010 loc) · 37.9 KB
/
gunGame.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
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
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Copyright (c) 2013, Dan Ryder
All rights reserved.
This package is free software; you can redistribute it and/or
modify it under the terms of the license found in the file
named COPYING that should have accompanied this file.
THIS PACKAGE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
*/
/*
gunGame.cpp
for updates, see: https://github.com/danryder/bzGunGameStyle
*/
#include "bzfsAPI.h"
#include <stdio.h>
#include <strings.h>
#include <map>
#include <utility>
#include <functional>
#include <string>
#include <list>
using namespace std;
#define DELAYSEC 0.25
#define RETRYSEC 0.1
#define MINORWARN 3
#define MAJORWARN 1
#define DETECTCHEAT 1
#define CHEATPENALTY 3
#define SUICIDEPENALTY 1
#define REQUIRECRUSH 3
// hide SR bullets completely from others or make them PZ
// ineffective either way, but PZ can fool others
#ifdef SHOWENDSHOTS
#define ENDSHOTTYPE "PZ"
#else
#define ENDSHOTTYPE "DELETE"
#endif
// enable this if playing sounds from a plugin crashing clients is fixed
// #define PLAYSOUNDS
// ORDERED LIST OF ALL THE FLAGS WE MIGHT USE
// along with the #players required to enable them
typedef struct {
const char *flagName;
size_t playersRequired;
} FlagOption;
FlagOption possibleFlags[] = {
{"L", 2},
{"GM", 2},
{"SW", 3},
{"CL", 3},
{"F", 2},
{"IB", 3},
{"A", 2},
{"MG", 2},
{"ST", 2},
{"T", 2},
{"SB", 2},
{"V", 2},
{"BU", 3},
{"WG", 3},
{"QT", 2},
{"M", 4},
{"B", 4},
{"O", 4},
{"RT", 5},
{"LT", 5},
{"WA", 3},
{"JM", 4},
{"NJ", 4},
{"RC", 3},
{"SR", 2},
{NULL, 0}
};
class FlagManager
{
private:
struct ltstr
{
bool operator()(const char* s1, const char* s2) const
{
return strcmp(s1, s2) < 0;
}
};
typedef map<int, int> AssignedFlagsType;
typedef map<size_t, int> FlagLevelsType;
typedef map<const char *, int, ltstr> WinnersListType;
struct DelayedFlagType{
DelayedFlagType(double t=0.0, const char *f=NULL)
: givetime(t), flag(f) {}
double givetime;
const char *flag;
};
AssignedFlagsType assignedFlags; // flag# assigned by player ID
FlagLevelsType flagLevels; // flag levels of all enabled flags (by flag#)
WinnersListType winnersList; // total wins by player ID
size_t numTotalFlags; // #flags that could be enabled
size_t numEnabledFlags; // #flags actually enabled
size_t minPlayers; // min players for a game
int firstFlag; // first flag enabled
int lastFlag; // last flag enabled
// if #flags enabled changes (as players come and go), update info about
// enabled flags
void recalcFlags()
{
firstFlag = -1;
lastFlag = -1;
flagLevels.clear();
numEnabledFlags = 0;
for (size_t f = 0; f < numTotalFlags; ++f)
{
if (possibleFlags[f].playersRequired <= numPlayers)
{
if (firstFlag < 0) firstFlag = f;
lastFlag = f;
numEnabledFlags++;
flagLevels[f] = numEnabledFlags;
}
}
}
// if #flags enabled changes (as players come and go), update player scores
// handle case where a player has a flag now removed from circulation
void recalcScores()
{
for (AssignedFlagsType::iterator i = assignedFlags.begin(); i != assignedFlags.end(); ++i)
{
int playerID = i->first;
int flag = i->second;
FlagLevelsType::iterator j = flagLevels.find(flag);
if (j != flagLevels.end())
{
// current flag was still in the list
// just update the score with the new position
bz_setPlayerWins(playerID, j->second);
}
else
{
// player had a flag not in new list
// replace it with preceeding valid flag
int decr = 0;
int newFlagNo = getPrevFlag(flag, decr, 1);
j = flagLevels.find(newFlagNo);
if (j != flagLevels.end())
{
i->second = newFlagNo; // update what flag the player *should* have
bz_setPlayerWins(playerID, j->second);
const char *newFlag = possibleFlags[newFlagNo].flagName;
replaceFlagIfAlive(playerID, newFlag, "flag deactivated");
}
else
{
// this should never happen
bz_sendTextMessagef(BZ_SERVER, debuggerID, "ERROR: PREVIOUS FLAG NOT DEFINED");
}
}
}
}
// flag progression
int getNextFlag(int oldFlag, int &delta, int count=1)
{
int c = 0;
delta = 0;
if (oldFlag >= lastFlag) return lastFlag;
for (int f = oldFlag+1; f < numTotalFlags; ++f)
{
if (possibleFlags[f].playersRequired <= numPlayers)
{
delta += 1;
if ((++c==count) || (f == lastFlag))
{
return f;
}
}
}
return -1;
}
// flag regression
int getPrevFlag(int oldFlag, int &delta, int count=1)
{
int c = 0;
delta = 0;
if (oldFlag <= firstFlag) return firstFlag;
for (int f = oldFlag-1; f >= 0; --f)
{
if (possibleFlags[f].playersRequired <= numPlayers)
{
delta += 1;
if ((++c==count) || (f == firstFlag))
{
return f;
}
}
}
return -1;
}
// sometimes we don't care how many we've traversed
int getNextFlag(int oldFlag, int count=1) {
int foo = 0;
return getNextFlag(oldFlag, foo, count);
}
int getPrevFlag(int oldFlag, int count=1) {
int foo = 0;
return getPrevFlag(oldFlag, foo, count);
}
int numPlayersNeeded()
{
return (minPlayers - numPlayers);
}
bool gameOn()
{
return (numPlayersNeeded() <= 0);
}
public:
// members accessed in plugin class
typedef map<int, DelayedFlagType> DelayedFlagsType;
DelayedFlagsType delayedFlags;
size_t numPlayers;
int debuggerID;
FlagManager()
: minPlayers(0),
numPlayers(0),
numEnabledFlags(0),
debuggerID(BZ_ALLUSERS)
{
// count all flags, determine min #Players required
numTotalFlags = 0;
FlagOption *f = &possibleFlags[0];
while (f && f->flagName)
{
numTotalFlags++;
if (!minPlayers || (f->playersRequired < minPlayers))
minPlayers = f->playersRequired;
f++;
}
}
~FlagManager()
{
for (WinnersListType::iterator i = winnersList.begin(); i != winnersList.end(); ++i)
{
char *key = const_cast<char *>(i->first);
winnersList.erase(i);
free(key);
}
}
// return true if this starts a game
bool addPlayer(const bz_PlayerJoinPartEventData_V1 *joinData)
{
bool start = false;
const char *newGuy = bz_getPlayerCallsign(joinData->playerID);
if (numPlayers)
{
bz_sendTextMessagef(BZ_SERVER, BZ_ALLUSERS,
"Everyone say \"Hi %s!\", player #%d (ID %d)",
newGuy, numPlayers, joinData->playerID);
}
bz_sendTextMessagef(BZ_SERVER, joinData->playerID,
"Welcome to \"GunGame Style\", %s...",
newGuy);
bool wasGameOn = gameOn();
int oldNumFlags = numEnabledFlags;
numPlayers++;
recalcFlags();
if (gameOn())
{
int numFlags = numEnabledFlags;
if (!wasGameOn)
{
beginGG();
start = true;
bz_sendTextMessagef(BZ_SERVER, BZ_ALLUSERS,
"\"GunGame Style\" started with %d players %d flags",
numPlayers, numFlags);
listFlags();
bz_sendTextMessagef(BZ_SERVER, BZ_ALLUSERS,
"Commands: \"flags\", \"winners\", \"leaders\"");
}
else
{
// game already in progress. tell new player flag list
listFlags(joinData->playerID);
if (oldNumFlags < numFlags)
{
bz_sendTextMessagef(BZ_SERVER, BZ_ALLUSERS,
"\"GunGame Style\" added %d flags to win...",
numFlags - oldNumFlags);
// adjust scores to map to new flags
recalcScores();
}
}
assignedFlags[joinData->playerID] = firstFlag;
bz_setPlayerWins(joinData->playerID, 1);
bz_setPlayerLosses(joinData->playerID, 0);
bz_setPlayerTKs(joinData->playerID, 0);
}
else
{
// no game yet
assignedFlags[joinData->playerID] = -1;
int needed = numPlayersNeeded();
bz_sendTextMessagef(BZ_SERVER, BZ_ALLUSERS,
"\"GunGame Style\" awaiting %d more player%s...",
needed, (needed > 1) ? "s": "");
}
return start;
}
// return true if this suspends a game
bool removePlayer(const bz_PlayerJoinPartEventData_V1 *partData)
{
bool end = false;
bool wasGameOn = gameOn();
int oldNumFlags = numEnabledFlags;
if (numPlayersNeeded() == 0) announceLeaders(BZ_ALLUSERS);
if (numPlayers > 0)
{
numPlayers--;
}
else
{
// weird case seen - joined empty server and was told I was player #-2
// not sure how this could get off track
// might need to call bz_getPlayerCount() instead of maintaining
bz_sendTextMessagef(BZ_SERVER, BZ_ALLUSERS,
"\"%s\" is leaving, but numPlayers is already %d.",
bz_getPlayerCallsign(partData->playerID),
numPlayers);
}
recalcFlags();
assignedFlags.erase(partData->playerID);
if (!gameOn())
{
if (wasGameOn)
{
bz_sendTextMessagef(BZ_SERVER, BZ_ALLUSERS,
"\"GunGame Style\" suspended - thanks a lot \"%s\"!",
bz_getPlayerCallsign(partData->playerID));
endGG();
end = true;
bz_sendTextMessagef(BZ_SERVER, BZ_ALLUSERS,
"\"GunGame Style\" needs one more player to restart...");
}
else
{
bz_sendTextMessagef(BZ_SERVER, BZ_ALLUSERS,
"\"GunGame Style\" now needs %d more players to restart...",
numPlayersNeeded());
}
}
else
{
// game continues
int numFlags = numEnabledFlags;
if (oldNumFlags > numFlags)
{
bz_sendTextMessagef(BZ_SERVER, BZ_ALLUSERS,
"\"GunGame Style\" removed %d flags to win...",
oldNumFlags - numFlags);
// adjust scores to map to new flags
recalcScores();
}
}
return end;
}
void givePlayerFlagDelayed(int playerID, const char *flagName)
{
double now = bz_getCurrentTime();
delayedFlags[playerID].flag = flagName;
delayedFlags[playerID].givetime = now + DELAYSEC;
}
bool givePlayerFlagNow(int playerID, const char *flagName)
{
return bz_givePlayerFlag(playerID, flagName, true);
}
// returns True if flag give succeeded immediately
// otherwise queues it up and returns False
bool givePlayerFlag(int playerID, const char *flagName)
{
bool now = givePlayerFlagNow(playerID, flagName);
if (now) return true;
givePlayerFlagDelayed(playerID, flagName);
return false;
}
void replaceFlagIfAlive(int playerID, const char *flagName, const char *reason, bool tryFast=false)
{
bz_BasePlayerRecord *pr = bz_getPlayerByIndex(playerID);
if (pr)
{
if (pr->spawned)
{
bz_removePlayerFlag(playerID);
if (flagName)
{
if (tryFast)
givePlayerFlag(playerID, flagName);
else
givePlayerFlagDelayed(playerID, flagName);
}
}
}
else
{
bz_sendTextMessagef(BZ_SERVER, debuggerID,
"ERROR: NO PLAYER RECORD (for '%s', player ID %d) can't replace %s's flag with %s",
reason,
playerID, bz_getPlayerCallsign(playerID),
(flagName) ? flagName : "nothing");
}
bz_freePlayerRecord(pr);
}
void beginGG()
{
// new game -- pass out flags to anyone spawned
const char *firstFlagName = possibleFlags[firstFlag].flagName;
#ifdef PLAYSOUNDS
bz_sendPlayCustomLocalSound(BZ_ALLUSERS, "gungame/gungame_start");
#endif
bz_BasePlayerRecord *pr = NULL;
for (AssignedFlagsType::iterator i = assignedFlags.begin(); i != assignedFlags.end(); ++i)
{
int playerID = i->first;
i->second = firstFlag;
bz_setPlayerWins(playerID, 1);
bz_setPlayerLosses(playerID, 0);
bz_setPlayerTKs(playerID, 0);
replaceFlagIfAlive(playerID, firstFlagName, "game begin", true);
}
}
void endGG()
{
for (AssignedFlagsType::iterator i = assignedFlags.begin(); i != assignedFlags.end(); ++i)
{
// reset flag and scores
int playerID = i->first;
i->second = -1;
bz_setPlayerWins(playerID, 0);
bz_setPlayerLosses(playerID, 0);
bz_setPlayerTKs(playerID, 0);
replaceFlagIfAlive(playerID, NULL, "game end");
}
}
void addWinner(const char *callsign)
{
WinnersListType::iterator i = winnersList.find(callsign);
if (i == winnersList.end())
{
// first time winning
winnersList.insert(pair<const char *, int>(strdup(callsign),1));
}
else
{
i->second += 1;
}
}
void listFlags(int dest=BZ_ALLUSERS)
{
for (FlagLevelsType::const_iterator it = flagLevels.begin();
it != flagLevels.end();
++it)
{
bz_sendTextMessagef(BZ_SERVER, dest, "Flag %3d: %s", it->second, possibleFlags[it->first].flagName);
}
}
void announceWinners(int dest)
{
if (!winnersList.size())
{
bz_sendTextMessagef(BZ_SERVER, dest, "No wins yet...");
}
else
{
bz_sendTextMessagef(BZ_SERVER, dest, "-= S C O R E B O A R D =-");
// sort winners by win count
typedef multimap<int, const char *, greater<int> > LeaderboardType;
LeaderboardType Leaderboard;
Leaderboard.clear();
for (WinnersListType::const_iterator i = winnersList.begin(); i != winnersList.end(); ++i)
{
Leaderboard.insert(pair<int, const char *>(i->second, i->first));
}
for (LeaderboardType::const_iterator j = Leaderboard.begin(); j != Leaderboard.end(); ++j)
{
bz_sendTextMessagef(BZ_SERVER, dest, "%d win%s - %s",
j->first,
(j->first > 1) ? "s":"",
j->second);
}
}
}
void announceLeaders(int dest)
{
int maxFlag = -1;
list<int> quasiWinners;
for (AssignedFlagsType::const_iterator i = assignedFlags.begin(); i != assignedFlags.end(); ++i)
{
int playerID = i->first;
int flag = i->second;
if (flag > maxFlag)
{
quasiWinners.clear();
quasiWinners.push_back(playerID);
maxFlag = flag;
}
else if (flag == maxFlag)
{
quasiWinners.push_back(playerID);
}
}
// announce "quasi winners"
if (maxFlag > firstFlag)
{
string qwinners = "";
for(list<int>::const_iterator i = quasiWinners.begin();
i != quasiWinners.end();
++i)
{
qwinners += bz_getPlayerCallsign(*i);
qwinners += ", ";
}
qwinners = qwinners.substr(0, qwinners.length() - 2);
const char *lastFlag = possibleFlags[maxFlag].flagName;
bz_sendTextMessagef(BZ_SERVER, dest,
"Leading the pack: %s with %s",
qwinners.c_str(), lastFlag);
}
else
{
bz_sendTextMessagef(BZ_SERVER, dest,
"No leaders yet. Shoot something!");
}
}
const char *getAssignedFlag(const int playerID)
{
AssignedFlagsType::const_iterator i = assignedFlags.find(playerID);
if (i == assignedFlags.end())
{
return NULL;
}
return possibleFlags[i->second].flagName;
}
// suicide or natural causes
void handleSuicide(const bz_PlayerDieEventData_V1 *dieData)
{
const char *victimName = bz_getPlayerCallsign(dieData->playerID);
int victimFlagNo = assignedFlags[dieData->playerID];
const char *victimFlag = possibleFlags[victimFlagNo].flagName;
int decr = 0;
int newFlagNo = getPrevFlag(victimFlagNo, decr, bz_getBZDBInt("_ggSuicidePenalty"));
if (newFlagNo < 0)
{
newFlagNo = victimFlagNo;
}
const char *newFlag = possibleFlags[newFlagNo].flagName;
#ifdef PLAYSOUNDS
bz_sendPlayCustomLocalSound(BZ_ALLUSERS, "flag_lost");
#endif
bz_sendTextMessagef(BZ_SERVER, dieData->playerID, "Ha-Ha! You suicided with %s. %s %s",
victimFlag,
(newFlagNo <= firstFlag) ? "starting over with"
: "demoted to",
newFlag);
assignedFlags[dieData->playerID] = newFlagNo;
// reduce player score on suicide
if (decr)
{
bz_setPlayerWins(dieData->playerID, flagLevels[newFlagNo]);
}
}
void handleHomicide(bz_PlayerDieEventData_V1 *dieData)
{
// on noes! a homicide occurred
int killerID = dieData->killerID;
int victimID = dieData->playerID;
const char *victimName = bz_getPlayerCallsign(victimID);
const char *killerName = bz_getPlayerCallsign(killerID);
int victimFlagNo = assignedFlags[victimID];
int killerFlagNo = assignedFlags[dieData->killerID];
const char *victimFlag = possibleFlags[victimFlagNo].flagName;
const char *killerFlag = possibleFlags[killerFlagNo].flagName;
// if someone spams the drop flag key and squeezes of a shot with no flag
// ... and if we don't succeed in making the bullet PZ
// ... and this results in another player dying
// demote and publicly shame the "cheater"
//
// allow valid cases where flagKilledWith can be empty:
// 1 killer has SR (squish)
// 2 victim has BU (squish)
// 3 victim "just had" BU
bool cheat = false;
if (bz_getBZDBBool("_ggDetectCheat"))
{
// was there no flag?
if (dieData->flagKilledWith.size() == 0)
{
// exempt server and admin
// those are always "OK"
if ((killerID != 253) && (killerID != 252))
{
if ((string(killerFlag) != "SR") && (string(victimFlag) != "BU"))
{
if (bz_getBZDBBool("_ggDebug"))
{
bz_sendTextMessagef(BZ_SERVER, debuggerID, "Possible cheating? Killed without flag, not SR or BU.");
}
// if victim Z < 0, was coming out of BU -- not a cheat
if (dieData->state.pos[2] >= 0)
{
// this is as sure as we can be that "cheat" happened
cheat = true;
int decr = 0;
int newFlagNo = getPrevFlag(killerFlagNo, decr, bz_getBZDBInt("_ggCheatPenalty"));
if (newFlagNo < 0)
{
newFlagNo = firstFlag;
}
const char *newFlag = possibleFlags[newFlagNo].flagName;
bz_sendTextMessagef(BZ_SERVER, BZ_ALLUSERS, "%s killed %s ... WITHOUT holding %s! Booted to %s",
killerName, victimName, killerFlag, newFlag);
assignedFlags[killerID] = newFlagNo;
// negate cheater score increase
// and roll it back
bz_setPlayerWins(killerID, flagLevels[newFlagNo] - 1);
// if cheater died, do nothing - new flag will be given on spawn
replaceFlagIfAlive(killerID, newFlag, "suspected cheat", true);
}
}
}
}
}
if (!cheat)
{
// legit kill!
if (bz_getBZDBBool("_ggJacked"))
{
bz_sendTextMessagef(BZ_SERVER, BZ_ALLUSERS, "%s got JACKED by %s with %s",
victimName, killerName, killerFlag);
}
int killerLevel = flagLevels[killerFlagNo];
int maxLevel = numEnabledFlags;
int remainLevels = maxLevel - killerLevel;
// advance the killer... detect win case, etc
if (killerLevel < maxLevel)
{
if (remainLevels <= MAJORWARN)
{
#ifdef PLAYSOUNDS
bz_sendPlayCustomLocalSound(BZ_ALLUSERS, "flag_alert");
#endif
bz_sendTextMessagef(BZ_SERVER, BZ_ALLUSERS,
"--->>> ATTENTION: %s is ABOUT TO WIN!!! <<<---",
killerName);
}
else if (remainLevels <= MINORWARN)
{
#ifdef PLAYSOUNDS
bz_sendPlayCustomLocalSound(BZ_ALLUSERS, "lock");
#endif
bz_sendTextMessagef(BZ_SERVER, BZ_ALLUSERS,
"-> ATTENTION: %s has %d KILLS REMAINING!!! <-",
killerName, remainLevels);
}
int adv = 0;
int newFlagNo = getNextFlag(killerFlagNo, adv, 1);
if (bz_getBZDBBool("_ggDebug"))
{
bz_sendTextMessagef(BZ_SERVER, debuggerID,
"-> ATTENTION: %s made a legit kill. new flag is %d which is level %d",
killerName, newFlagNo, flagLevels[newFlagNo]);
}
assignedFlags[killerID] = newFlagNo;
// set score to new level (minus one to account for pending increment)
bz_setPlayerWins(killerID, flagLevels[newFlagNo] - 1);
const char *newFlag = possibleFlags[newFlagNo].flagName;
bz_sendTextMessagef(BZ_SERVER, killerID, "\"Upgraded\" from %s to %s (%d/%d)", killerFlag, newFlag, killerLevel+1, maxLevel);
#ifdef PLAYSOUNDS
bz_sendPlayCustomLocalSound(killerID, "gungame/gungame_boost");
#endif
replaceFlagIfAlive(killerID, newFlag, "advancing");
}
else
{
// winner!
bz_sendTextMessagef(BZ_SERVER, BZ_ALLUSERS, "---===>>> WINNER: %s <<<===---",
killerName);
addWinner(killerName);
announceWinners(BZ_ALLUSERS);
// reset game
int winnerFlag = assignedFlags[killerID];
int firstFlag = getNextFlag(-1);
const char *firstFlagName = possibleFlags[firstFlag].flagName;
for (AssignedFlagsType::iterator i = assignedFlags.begin(); i!= assignedFlags.end(); ++i)
{
int playerID = i->first;
int flag = i->second;
// reset scores
bz_setPlayerWins(i->first, 0); // will increment to 1 due to kill
bz_setPlayerLosses(i->first, 0);
bz_setPlayerTKs(i->first, 0);
i->second = firstFlag;
if (playerID != killerID)
{
if (flag == winnerFlag)
{
#ifdef PLAYSOUNDS
bz_sendPlayCustomLocalSound(playerID, "phantom");
bz_sendPlayCustomLocalSound(playerID, "flag_won");
#endif
bz_sendTextMessagef(BZ_SERVER, playerID, "So close! %s just beat you", killerName);
}
else
{
#ifdef PLAYSOUNDS
bz_sendPlayCustomLocalSound(playerID, "flag_won");
#endif
bz_sendTextMessagef(BZ_SERVER, playerID, "Bow to %s", killerName);
}
// kill all non-winners
// spawn will reset their flag
bz_killPlayer(playerID, false);
}
else
{
#ifdef PLAYSOUNDS
bz_sendPlayCustomLocalSound(playerID, "gungame/gungame_sexy");
bz_sendPlayCustomLocalSound(playerID, "flag_won");
#endif
bz_sendTextMessagef(BZ_SERVER, playerID, "Nice game %s!", killerName);
replaceFlagIfAlive(playerID, firstFlagName, "winning");
}
}
}
}
}
};
class GunGame : public bz_Plugin, public bz_CustomSlashCommandHandler
{
private:
FlagManager *flagManager;
const char *debuggerIP;
bool savedHideFlagsOnRadar;
bool savedShotMismatch;
virtual bool SlashCommand ( int playerID, bz_ApiString command, bz_ApiString message, bz_APIStringList *params )
{
if (command == "flags")
{
if (flagManager)
{
flagManager->listFlags(playerID);
}
}
else if (command == "winners")
{
if (flagManager)
{
flagManager->announceWinners(playerID);
}
}
else if (command == "leaders")
{
if (flagManager)
{
flagManager->announceLeaders(playerID);
}
}
else
{
return false;
}
return true;
}
public:
virtual const char* Name (){return "GunGame";}
virtual void Init ( const char* config)
{
savedHideFlagsOnRadar = bz_getBZDBBool("_hideFlagsOnRadar");
bz_setBZDBBool("_hideFlagsOnRadar", true, 0, false);
bz_setBZDBBool("_ggJacked", false, 0, false);
bz_setBZDBBool("_ggDebug", false, 0, false);
bz_setBZDBBool("_ggDetectCheat", true, 0, false);
bz_setBZDBInt("_ggSuicidePenalty", SUICIDEPENALTY, 0, false);
bz_setBZDBInt("_ggCheatPenalty", CHEATPENALTY, 0, false);
bz_registerCustomSlashCommand("flags", this);
bz_registerCustomSlashCommand("winners", this);
bz_registerCustomSlashCommand("leaders", this);
debuggerIP = config;
flagManager = new FlagManager();
Register(bz_ePlayerJoinEvent);
Register(bz_ePlayerPartEvent);
}
void Cleanup()
{
if (flagManager)
{
delete flagManager;
flagManager = NULL;
}
bz_removeCustomSlashCommand("flags");
bz_removeCustomSlashCommand("winners");
bz_removeCustomSlashCommand("leaders");
bz_Plugin::Cleanup();
bz_setBZDBBool("_hideFlagsOnRadar", savedHideFlagsOnRadar, 0, false);
}
virtual void Event ( bz_EventData *eventData );
};
BZ_PLUGIN(GunGame)
void GunGame::Event ( bz_EventData *eventData )
{
// sometimes a flag give fails (for example if we just took it)
// handle this with a delayed give
if (eventData->eventType == bz_eTickEvent)
{
bz_TickEventData_V1 *tickData = (bz_TickEventData_V1*)eventData;
FlagManager::DelayedFlagsType::iterator e = flagManager->delayedFlags.end();
for (FlagManager::DelayedFlagsType::iterator i = flagManager->delayedFlags.begin();
i != e; ++i)
{
if (i->second.flag)
{
if (tickData->eventTime > i->second.givetime)
{
// timer has expired - try to give the flag now
// if that still fails, reset timer
if (flagManager->givePlayerFlagNow(i->first, i->second.flag))
{
i->second.flag = NULL;
}
else
{
i->second.givetime = tickData->eventTime + RETRYSEC;
}
}
}
}
}
else if (eventData->eventType == bz_eShotFiredEvent)
{
bz_ShotFiredEventData_V1 *shotData = (bz_ShotFiredEventData_V1*)eventData;
const char *shootingPlayer = bz_getPlayerCallsign(shotData->playerID);
bz_BasePlayerRecord *pr = bz_getPlayerByIndex(shotData->playerID);
if (pr)
{
if(!pr->currentFlag.size())
{
// if no flag - change bullet to PZ
// not sure how well this actually works
// so we catch ill-gotten kills elsewhere and dispense justice there
shotData->changed = true;
shotData->type = "PZ";
if (bz_getBZDBBool("_ggDebug"))
{
bz_sendTextMessagef(BZ_SERVER, flagManager->debuggerID,
">>>>>>> %s fired a shot... but has no flag - made it PZ <<<<<",
shootingPlayer);
}
}
else
{
// check flag type
const char *shouldHave = flagManager->getAssignedFlag(shotData->playerID);
if (shotData->type != shouldHave)
{
// shooter had a flag... was it the right one?
// I've never seen this actually happen
shotData->changed = true;
shotData->type = "PZ";
if (bz_getBZDBBool("_ggDebug"))
{
bz_sendTextMessagef(BZ_SERVER,
flagManager->debuggerID,
">>>>>>> %s shot type: %s should have been: %s - made it PZ <<<<<<",
shootingPlayer, shotData->type.c_str(), shouldHave);
}
}
// also if SR - end game situation - disable gun in same way
else if((flagManager->numPlayers >= REQUIRECRUSH) && (pr->currentFlag == "SteamRoller (+SR)"))
{
shotData->changed = true;
shotData->type = ENDSHOTTYPE;
bz_sendTextMessagef(BZ_SERVER, shotData->playerID, "Shots don't work; You gotta crush someone to win");
}
}
}
else
{
// example: a world weapon
// do nothing
if (bz_getBZDBBool("_ggDebug"))
{
bz_sendTextMessagef(BZ_SERVER,
flagManager->debuggerID,
">>>>>>> No Player ID for shot - world weapon?");
}
}
bz_freePlayerRecord(pr);
}
// drops happen twice when you die - once spawned before the DieEvent and once after
else if (eventData->eventType == bz_eFlagDroppedEvent)
{
bz_FlagDroppedEventData_V1 *playerData = (bz_FlagDroppedEventData_V1*)eventData;
const char *dropPlayer = bz_getPlayerCallsign(playerData->playerID);
bz_BasePlayerRecord *pr = bz_getPlayerByIndex(playerData->playerID);
if (pr)
{
if (pr->spawned)
{
// player dropped while alive
const char *droppedFlag = bz_getFlagName(playerData->flagID).c_str();
const char *shouldHave = flagManager->getAssignedFlag(playerData->playerID);
if (shouldHave)
{
if (0 == strncmp(droppedFlag, shouldHave, strlen(droppedFlag)))
{
if (bz_getBZDBBool("_ggDebug"))
{
bz_sendTextMessagef(BZ_SERVER, flagManager->debuggerID, "%s dropped %s while alive", dropPlayer, droppedFlag);
}
// happens if a player dies (before die event)
// OR if they try to drop their flag
// either way, give them that flag back
bool res = flagManager->givePlayerFlag(playerData->playerID, droppedFlag);
if (bz_getBZDBBool("_ggDebug"))
{
bz_sendTextMessagef(BZ_SERVER, flagManager->debuggerID, "Immediate re-gift success? %s", res ? "yes" : "no");
}
}
else if (bz_getBZDBBool("_ggDebug"))
{
// happens if plugin removed their flag
// after upgrading state to a new one
// plugin will also assign next flag