forked from bklol/Misc-Plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
NMC.sp
1305 lines (1093 loc) · 35.7 KB
/
NMC.sp
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
/**
* vim: set ts=4 :
* =============================================================================
* SourceMod Mapchooser Plugin
* Creates a map vote at appropriate times, setting sm_nextmap to the winning
* vote
*
* SourceMod (C)2004-2014 AlliedModders LLC. All rights reserved.
* =============================================================================
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, version 3.0, as published by the
* Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>.
*
* As a special exception, AlliedModders LLC gives you permission to link the
* code of this program (as well as its derivative works) to "Half-Life 2," the
* "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
* by the Valve Corporation. You must obey the GNU General Public License in
* all respects for all other code used. Additionally, AlliedModders LLC grants
* this exception to all derivative works. AlliedModders LLC defines further
* exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
* or <http://www.sourcemod.net/license.php>.
*
* Version: $Id$
*/
#pragma semicolon 1
#include <sourcemod>
#include <mapchooser>
#include <nextmap>
#include <shavit>
#pragma newdecls required
public Plugin myinfo =
{
name = "NEKOMapChooser",
author = "AlliedModders LLC,neko",
description = "Automated Map Voting",
version = SOURCEMOD_VERSION,
url = "http://www.sourcemod.net/"
};
/* Valve ConVars */
ConVar g_Cvar_Winlimit;
ConVar g_Cvar_Maxrounds;
ConVar g_Cvar_Fraglimit;
ConVar g_Cvar_Bonusroundtime;
/* Plugin ConVars */
ConVar g_Cvar_StartTime;
ConVar g_Cvar_StartRounds;
ConVar g_Cvar_StartFrags;
ConVar g_Cvar_ExtendTimeStep;
ConVar g_Cvar_ExtendRoundStep;
ConVar g_Cvar_ExtendFragStep;
ConVar g_Cvar_ExcludeMaps;
ConVar g_Cvar_IncludeMaps;
ConVar g_Cvar_NoVoteMode;
ConVar g_Cvar_Extend;
ConVar g_Cvar_DontChange;
ConVar g_Cvar_EndOfMapVote;
ConVar g_Cvar_VoteDuration;
ConVar g_Cvar_RunOff;
ConVar g_Cvar_RunOffPercent;
Handle g_VoteTimer = null;
Handle g_RetryTimer = null;
// g_MapList stores unresolved names so we can resolve them after every map change in the workshop updates.
// g_OldMapList and g_NextMapList are resolved. g_NominateList depends on the nominations implementation.
/* Data Handles */
ArrayList g_MapList;
ArrayList g_NominateList;
ArrayList g_NominateOwners;
ArrayList g_OldMapList;
ArrayList g_NextMapList;
Menu g_VoteMenu;
int g_Extends;
int g_TotalRounds;
bool g_HasVoteStarted;
bool g_WaitingForVote;
bool g_MapVoteCompleted;
bool g_ChangeMapAtRoundEnd;
bool g_ChangeMapInProgress;
int g_mapFileSerial = -1;
char g_HostName[256];
MapChange g_ChangeTime;
ConVar g_HostnameCvar;
Handle g_NominationsResetForward = null;
Handle g_MapVoteStartedForward = null;
ArrayList g_aMapList;
ArrayList g_aMapTiers;
Database g_hDatabase;
/* Upper bound of how many team there could be */
#define MAXTEAMS 10
int g_winCount[MAXTEAMS];
#define VOTE_EXTEND "##extend##"
#define VOTE_DONTCHANGE "##dontchange##"
public void OnPluginStart()
{
LoadTranslations("mapchooser.phrases");
LoadTranslations("common.phrases");
int arraySize = ByteCountToCells(PLATFORM_MAX_PATH);
g_MapList = new ArrayList(arraySize);
g_NominateList = new ArrayList(arraySize);
g_NominateOwners = new ArrayList();
g_OldMapList = new ArrayList(arraySize);
g_NextMapList = new ArrayList(arraySize);
g_Cvar_EndOfMapVote = CreateConVar("sm_mapvote_endvote", "1", "Specifies if MapChooser should run an end of map vote", _, true, 0.0, true, 1.0);
g_Cvar_StartTime = CreateConVar("sm_mapvote_start", "3.0", "Specifies when to start the vote based on time remaining.", _, true, 1.0);
g_Cvar_StartRounds = CreateConVar("sm_mapvote_startround", "2.0", "Specifies when to start the vote based on rounds remaining. Use 0 on TF2 to start vote during bonus round time", _, true, 0.0);
g_Cvar_StartFrags = CreateConVar("sm_mapvote_startfrags", "5.0", "Specifies when to start the vote base on frags remaining.", _, true, 1.0);
g_Cvar_ExtendTimeStep = CreateConVar("sm_extendmap_timestep", "30", "Specifies how much many more minutes each extension makes", _, true, 5.0);
g_Cvar_ExtendRoundStep = CreateConVar("sm_extendmap_roundstep", "5", "Specifies how many more rounds each extension makes", _, true, 1.0);
g_Cvar_ExtendFragStep = CreateConVar("sm_extendmap_fragstep", "10", "Specifies how many more frags are allowed when map is extended.", _, true, 5.0);
g_Cvar_ExcludeMaps = CreateConVar("sm_mapvote_exclude", "5", "Specifies how many past maps to exclude from the vote.", _, true, 0.0);
g_Cvar_IncludeMaps = CreateConVar("sm_mapvote_include", "5", "Specifies how many maps to include in the vote.", _, true, 2.0, true, 6.0);
g_Cvar_NoVoteMode = CreateConVar("sm_mapvote_novote", "1", "Specifies whether or not MapChooser should pick a map if no votes are received.", _, true, 0.0, true, 1.0);
g_Cvar_Extend = CreateConVar("sm_mapvote_extend", "3", "Number of extensions allowed each map.", _, true, 0.0);
g_Cvar_DontChange = CreateConVar("sm_mapvote_dontchange", "0", "Specifies if a 'Don't Change' option should be added to early votes", _, true, 0.0);
g_Cvar_VoteDuration = CreateConVar("sm_mapvote_voteduration", "20", "Specifies how long the mapvote should be available for.", _, true, 5.0);
g_Cvar_RunOff = CreateConVar("sm_mapvote_runoff", "0", "Hold run of votes if winning choice is less than a certain margin", _, true, 0.0, true, 1.0);
g_Cvar_RunOffPercent = CreateConVar("sm_mapvote_runoffpercent", "50", "If winning choice has less than this percent of votes, hold a runoff", _, true, 0.0, true, 100.0);
RegAdminCmd("sm_mapvote", Command_Mapvote, ADMFLAG_CHANGEMAP, "sm_mapvote - Forces MapChooser to attempt to run a map vote now.");
RegAdminCmd("sm_setnextmap", Command_SetNextmap, ADMFLAG_CHANGEMAP, "sm_setnextmap <map>");
g_Cvar_Winlimit = FindConVar("mp_winlimit");
g_Cvar_Maxrounds = FindConVar("mp_maxrounds");
g_Cvar_Fraglimit = FindConVar("mp_fraglimit");
g_Cvar_Bonusroundtime = FindConVar("mp_bonusroundtime");
g_HostnameCvar = FindConVar("hostname");
if (g_Cvar_Winlimit || g_Cvar_Maxrounds)
{
char folder[64];
GetGameFolderName(folder, sizeof(folder));
if (strcmp(folder, "tf") == 0)
{
HookEvent("teamplay_win_panel", Event_TeamPlayWinPanel);
HookEvent("teamplay_restart_round", Event_TFRestartRound);
HookEvent("arena_win_panel", Event_TeamPlayWinPanel);
}
else if (strcmp(folder, "nucleardawn") == 0)
{
HookEvent("round_win", Event_RoundEnd);
}
else if (strcmp(folder, "empires") == 0)
{
HookEvent("game_end", Event_RoundEnd);
}
else
{
HookEvent("round_end", Event_RoundEnd);
}
}
if (g_Cvar_Fraglimit)
{
HookEvent("player_death", Event_PlayerDeath);
}
AutoExecConfig(true, "mapchooser");
//Change the mp_bonusroundtime max so that we have time to display the vote
//If you display a vote during bonus time good defaults are 17 vote duration and 19 mp_bonustime
if (g_Cvar_Bonusroundtime)
{
g_Cvar_Bonusroundtime.SetBounds(ConVarBound_Upper, true, 30.0);
}
g_NominationsResetForward = CreateGlobalForward("OnNominationRemoved", ET_Ignore, Param_String, Param_Cell);
g_MapVoteStartedForward = CreateGlobalForward("OnMapVoteStarted", ET_Ignore);
g_aMapList = new ArrayList( ByteCountToCells(PLATFORM_MAX_PATH) );
g_aMapTiers = new ArrayList();
LoadMapList();
}
public APLRes AskPluginLoad2(Handle myself, bool late, char[] error, int err_max)
{
RegPluginLibrary("mapchooser");
CreateNative("NominateMap", Native_NominateMap);
CreateNative("RemoveNominationByMap", Native_RemoveNominationByMap);
CreateNative("RemoveNominationByOwner", Native_RemoveNominationByOwner);
CreateNative("InitiateMapChooserVote", Native_InitiateVote);
CreateNative("CanMapChooserStartVote", Native_CanVoteStart);
CreateNative("HasEndOfMapVoteFinished", Native_CheckVoteDone);
CreateNative("GetExcludeMapList", Native_GetExcludeMapList);
CreateNative("GetNominatedMapList", Native_GetNominatedMapList);
CreateNative("EndOfMapVoteEnabled", Native_EndOfMapVoteEnabled);
return APLRes_Success;
}
public void OnConfigsExecuted()
{
if (ReadMapList(g_MapList,
g_mapFileSerial,
"mapchooser",
MAPLIST_FLAG_CLEARARRAY|MAPLIST_FLAG_MAPSFOLDER)
!= null)
{
if (g_mapFileSerial == -1)
{
LogError("Unable to create a valid map list.");
}
}
CreateNextVote();
SetupTimeleftTimer();
g_TotalRounds = 0;
g_Extends = 0;
g_MapVoteCompleted = false;
g_NominateList.Clear();
g_NominateOwners.Clear();
for (int i=0; i<MAXTEAMS; i++)
{
g_winCount[i] = 0;
}
/* Check if mapchooser will attempt to start mapvote during bonus round time - TF2 Only */
if (g_Cvar_Bonusroundtime && !g_Cvar_StartRounds.IntValue)
{
if (g_Cvar_Bonusroundtime.FloatValue <= g_Cvar_VoteDuration.FloatValue)
{
LogError("Warning - Bonus Round Time shorter than Vote Time. Votes during bonus round may not have time to complete");
}
}
}
public void OnMapStart()
{
LoadMapList();
char g_szMapName[128],hostname[256];
GetCurrentMap(g_szMapName, 128);
g_HostnameCvar.GetString(g_HostName, sizeof(g_HostName));
int tier = Shavit_GetMapTier(g_szMapName);
Format(hostname, sizeof(hostname), "%s [地图难度 %i]", g_HostName, tier);
g_HostnameCvar.SetString(hostname);
}
void LoadMapList()
{
g_aMapList.Clear();
g_aMapTiers.Clear();
char buffer[128];
g_hDatabase = SQL_Connect( "shavit", true, buffer, sizeof(buffer) );
if (g_hDatabase == null)
{
SetFailState("Cannot connect to datbase error");
}
Format( buffer, sizeof(buffer), "SELECT * FROM `maptiers` ORDER BY `map`");
g_hDatabase.Query( LoadZonedMapsCallback, buffer, _, DBPrio_High );
}
public void LoadZonedMapsCallback( Database db, DBResultSet results, const char[] error, any data )
{
if( results == null )
{
LogError( "[NMC] - (LoadMapZonesCallback) - %s", error );
return;
}
char map[PLATFORM_MAX_PATH];
while( results.FetchRow() )
{
results.FetchString( 0, map, sizeof(map) );
//这个也是垃圾玩意
if( FindMap( map, map, sizeof(map) ) != FindMap_NotFound )
{
GetMapDisplayName( map, map, sizeof(map) );
g_aMapList.PushString( map );
g_aMapTiers.Push( results.FetchInt( 1 ) );
}
}
}
public void OnMapEnd()
{
g_HasVoteStarted = false;
g_WaitingForVote = false;
g_ChangeMapAtRoundEnd = false;
g_ChangeMapInProgress = false;
g_VoteTimer = null;
g_RetryTimer = null;
char map[PLATFORM_MAX_PATH];
GetCurrentMap(map, sizeof(map));
g_OldMapList.PushString(map);
if (g_OldMapList.Length > g_Cvar_ExcludeMaps.IntValue)
{
g_OldMapList.Erase(0);
}
}
public void OnClientDisconnect(int client)
{
int index = g_NominateOwners.FindValue(client);
if (index == -1)
{
return;
}
char oldmap[PLATFORM_MAX_PATH];
g_NominateList.GetString(index, oldmap, sizeof(oldmap));
Call_StartForward(g_NominationsResetForward);
Call_PushString(oldmap);
Call_PushCell(g_NominateOwners.Get(index));
Call_Finish();
g_NominateOwners.Erase(index);
g_NominateList.Erase(index);
}
public Action Command_SetNextmap(int client, int args)
{
if (args < 1)
{
ReplyToCommand(client, "[SM] Usage: sm_setnextmap <map>");
return Plugin_Handled;
}
char map[PLATFORM_MAX_PATH];
char displayName[PLATFORM_MAX_PATH];
GetCmdArg(1, map, sizeof(map));
if (FindMap(map, displayName, sizeof(displayName)) == FindMap_NotFound)
{
ReplyToCommand(client, "[SM] %t", "Map was not found", map);
return Plugin_Handled;
}
GetMapDisplayName(displayName, displayName, sizeof(displayName));
ShowActivity2(client, "[SM] ", "%t", "Changed Next Map", displayName);
LogAction(client, -1, "\"%L\" changed nextmap to \"%s\"", client, map);
SetNextMap(map);
g_MapVoteCompleted = true;
return Plugin_Handled;
}
public void OnMapTimeLeftChanged()
{
if (g_MapList.Length)
{
SetupTimeleftTimer();
}
}
void SetupTimeleftTimer()
{
int time;
if (GetMapTimeLeft(time) && time > 0)
{
int startTime = g_Cvar_StartTime.IntValue * 60;
if (time - startTime < 0 && g_Cvar_EndOfMapVote.BoolValue && !g_MapVoteCompleted && !g_HasVoteStarted)
{
InitiateVote(MapChange_MapEnd, null);
}
else
{
if (g_VoteTimer != null)
{
KillTimer(g_VoteTimer);
g_VoteTimer = null;
}
//g_VoteTimer = CreateTimer(float(time - startTime), Timer_StartMapVote, _, TIMER_FLAG_NO_MAPCHANGE);
DataPack data;
g_VoteTimer = CreateDataTimer(float(time - startTime), Timer_StartMapVote, data, TIMER_FLAG_NO_MAPCHANGE);
data.WriteCell(MapChange_MapEnd);
data.WriteCell(INVALID_HANDLE);
data.Reset();
}
}
}
public Action Timer_StartMapVote(Handle timer, DataPack data)
{
if (timer == g_RetryTimer)
{
g_WaitingForVote = false;
g_RetryTimer = null;
}
else
{
g_VoteTimer = null;
}
if (!g_MapList.Length || !g_Cvar_EndOfMapVote.BoolValue || g_MapVoteCompleted || g_HasVoteStarted)
{
return Plugin_Stop;
}
MapChange mapChange = view_as<MapChange>(data.ReadCell());
ArrayList hndl = view_as<ArrayList>(data.ReadCell());
InitiateVote(mapChange, hndl);
return Plugin_Stop;
}
public void Event_TFRestartRound(Event event, const char[] name, bool dontBroadcast)
{
/* Game got restarted - reset our round count tracking */
g_TotalRounds = 0;
}
public void Event_TeamPlayWinPanel(Event event, const char[] name, bool dontBroadcast)
{
if (g_ChangeMapAtRoundEnd)
{
g_ChangeMapAtRoundEnd = false;
CreateTimer(2.0, Timer_ChangeMap, INVALID_HANDLE, TIMER_FLAG_NO_MAPCHANGE);
g_ChangeMapInProgress = true;
}
int bluescore = event.GetInt("blue_score");
int redscore = event.GetInt("red_score");
if (event.GetInt("round_complete") == 1 || StrEqual(name, "arena_win_panel"))
{
g_TotalRounds++;
if (!g_MapList.Length || g_HasVoteStarted || g_MapVoteCompleted || !g_Cvar_EndOfMapVote.BoolValue)
{
return;
}
CheckMaxRounds(g_TotalRounds);
switch(event.GetInt("winning_team"))
{
case 3:
{
CheckWinLimit(bluescore);
}
case 2:
{
CheckWinLimit(redscore);
}
//We need to do nothing on winning_team == 0 this indicates stalemate.
default:
{
return;
}
}
}
}
/* You ask, why don't you just use team_score event? And I answer... Because CSS doesn't. */
public void Event_RoundEnd(Event event, const char[] name, bool dontBroadcast)
{
if (g_ChangeMapAtRoundEnd)
{
g_ChangeMapAtRoundEnd = false;
CreateTimer(2.0, Timer_ChangeMap, INVALID_HANDLE, TIMER_FLAG_NO_MAPCHANGE);
g_ChangeMapInProgress = true;
}
int winner;
if (strcmp(name, "round_win") == 0)
{
// Nuclear Dawn
winner = event.GetInt("team");
}
else
{
winner = event.GetInt("winner");
}
if (winner == 0 || winner == 1 || !g_Cvar_EndOfMapVote.BoolValue)
{
return;
}
if (winner >= MAXTEAMS)
{
SetFailState("Mod exceed maximum team count - Please file a bug report.");
}
g_TotalRounds++;
g_winCount[winner]++;
if (!g_MapList.Length || g_HasVoteStarted || g_MapVoteCompleted)
{
return;
}
CheckWinLimit(g_winCount[winner]);
CheckMaxRounds(g_TotalRounds);
}
public void CheckWinLimit(int winner_score)
{
if (g_Cvar_Winlimit)
{
int winlimit = g_Cvar_Winlimit.IntValue;
if (winlimit)
{
if (winner_score >= (winlimit - g_Cvar_StartRounds.IntValue))
{
InitiateVote(MapChange_MapEnd, null);
}
}
}
}
public void CheckMaxRounds(int roundcount)
{
if (g_Cvar_Maxrounds)
{
int maxrounds = g_Cvar_Maxrounds.IntValue;
if (maxrounds)
{
if (roundcount >= (maxrounds - g_Cvar_StartRounds.IntValue))
{
InitiateVote(MapChange_MapEnd, null);
}
}
}
}
public void Event_PlayerDeath(Event event, const char[] name, bool dontBroadcast)
{
if (!g_MapList.Length || !g_Cvar_Fraglimit || g_HasVoteStarted)
{
return;
}
if (!g_Cvar_Fraglimit.IntValue || !g_Cvar_EndOfMapVote.BoolValue)
{
return;
}
if (g_MapVoteCompleted)
{
return;
}
int fragger = GetClientOfUserId(event.GetInt("attacker"));
if (!fragger)
{
return;
}
if (GetClientFrags(fragger) >= (g_Cvar_Fraglimit.IntValue - g_Cvar_StartFrags.IntValue))
{
InitiateVote(MapChange_MapEnd, null);
}
}
public Action Command_Mapvote(int client, int args)
{
InitiateVote(MapChange_MapEnd, null);
return Plugin_Handled;
}
/**
* Starts a new map vote
*
* @param when When the resulting map change should occur.
* @param inputlist Optional list of maps to use for the vote, otherwise an internal list of nominations + random maps will be used.
* @param noSpecials Block special vote options like extend/nochange (upgrade this to bitflags instead?)
*/
void InitiateVote(MapChange when, ArrayList inputlist=null)
{
g_WaitingForVote = true;
if (IsVoteInProgress())
{
// Can't start a vote, try again in 5 seconds.
//g_RetryTimer = CreateTimer(5.0, Timer_StartMapVote, _, TIMER_FLAG_NO_MAPCHANGE);
DataPack data;
g_RetryTimer = CreateDataTimer(5.0, Timer_StartMapVote, data, TIMER_FLAG_NO_MAPCHANGE);
data.WriteCell(when);
data.WriteCell(inputlist);
data.Reset();
return;
}
/* If the main map vote has completed (and chosen result) and its currently changing (not a delayed change) we block further attempts */
if (g_MapVoteCompleted && g_ChangeMapInProgress)
{
return;
}
g_ChangeTime = when;
g_WaitingForVote = false;
g_HasVoteStarted = true;
g_VoteMenu = new Menu(Handler_MapVoteMenu);
g_VoteMenu.SetTitle("[NEKO]地图投票");
g_VoteMenu.VoteResultCallback = Handler_MapVoteFinished;
/* Call OnMapVoteStarted() Forward */
Call_StartForward(g_MapVoteStartedForward);
Call_Finish();
/**
* TODO: Make a proper decision on when to clear the nominations list.
* Currently it clears when used, and stays if an external list is provided.
* Is this the right thing to do? External lists will probably come from places
* like sm_mapvote from the adminmenu in the future.
*/
char map[PLATFORM_MAX_PATH];
/* No input given - User our internal nominations and maplist */
if (inputlist == null)
{
int nominateCount = g_NominateList.Length;
int voteSize = g_Cvar_IncludeMaps.IntValue;
/* Smaller of the two - It should be impossible for nominations to exceed the size though (cvar changed mid-map?) */
int nominationsToAdd = nominateCount >= voteSize ? voteSize : nominateCount;
g_VoteMenu.AddItem("dis","地图投票开始了!",ITEMDRAW_DISABLED);
g_VoteMenu.AddItem("dis","地图投票开始了!",ITEMDRAW_DISABLED);
for (int i=0; i<nominationsToAdd; i++)
{
char displayName[PLATFORM_MAX_PATH];
g_NominateList.GetString(i, map, sizeof(map));
GetMapDisplayName(map, displayName, sizeof(displayName));
int tier = 1;
int idx = g_aMapList.FindString( map );
if( idx != -1 )
{
tier = g_aMapTiers.Get( idx );
}
Format( displayName, sizeof(displayName), "[难度T%i] %s", tier, map );
g_VoteMenu.AddItem(map, displayName);
RemoveStringFromArray(g_NextMapList, map);
/* Notify Nominations that this map is now free */
Call_StartForward(g_NominationsResetForward);
Call_PushString(map);
Call_PushCell(g_NominateOwners.Get(i));
Call_Finish();
}
/* Clear out the rest of the nominations array */
for (int i=nominationsToAdd; i<nominateCount; i++)
{
g_NominateList.GetString(i, map, sizeof(map));
/* These maps shouldn't be excluded from the vote as they weren't really nominated at all */
/* Notify Nominations that this map is now free */
Call_StartForward(g_NominationsResetForward);
Call_PushString(map);
Call_PushCell(g_NominateOwners.Get(i));
Call_Finish();
}
/* There should currently be 'nominationsToAdd' unique maps in the vote */
int i = nominationsToAdd;
int count = 0;
int availableMaps = g_NextMapList.Length;
while (i < voteSize)
{
if (count >= availableMaps)
{
//Run out of maps, this will have to do.
break;
}
g_NextMapList.GetString(count, map, sizeof(map));
count++;
/* Insert the map and increment our count */
char displayName[PLATFORM_MAX_PATH];
GetMapDisplayName(map, displayName, sizeof(displayName));
int tier = 1;
int idx = g_aMapList.FindString( map );
if( idx != -1 )
{
tier = g_aMapTiers.Get( idx );
}
Format( displayName, sizeof(displayName), "[难度T%i] %s", tier, map );
g_VoteMenu.AddItem(map, displayName);
i++;
}
/* Wipe out our nominations list - Nominations have already been informed of this */
g_NominateOwners.Clear();
g_NominateList.Clear();
}
else //We were given a list of maps to start the vote with
{
int size = inputlist.Length;
for (int i=0; i<size; i++)
{
inputlist.GetString(i, map, sizeof(map));
if (IsMapValid(map))
{
char displayName[PLATFORM_MAX_PATH];
GetMapDisplayName(map, displayName, sizeof(displayName));
int tier = 1;
int idx = g_aMapList.FindString( map );
if( idx != -1 )
{
tier = g_aMapTiers.Get( idx );
}
Format( displayName, sizeof(displayName), "[难度T%i] %s", tier, map );
g_VoteMenu.AddItem(map, displayName);
}
}
}
/* Do we add any special items? */
if ((when == MapChange_Instant || when == MapChange_RoundEnd) && g_Cvar_DontChange.BoolValue)
{
g_VoteMenu.AddItem(VOTE_DONTCHANGE, "Don't Change");
}
else if (g_Cvar_Extend.BoolValue && g_Extends < g_Cvar_Extend.IntValue)
{
g_VoteMenu.AddItem(VOTE_EXTEND, "Extend Map");
}
/* There are no maps we could vote for. Don't show anything. */
if (g_VoteMenu.ItemCount == 0)
{
g_HasVoteStarted = false;
delete g_VoteMenu;
return;
}
int voteDuration = g_Cvar_VoteDuration.IntValue;
g_VoteMenu.ExitButton = false;
g_VoteMenu.DisplayVoteToAll(voteDuration);
LogAction(-1, -1, "Voting for next map has started.");
PrintToChatAll("[SM] %t", "Nextmap Voting Started");
}
public void Handler_VoteFinishedGeneric(Menu menu,
int num_votes,
int num_clients,
const int[][] client_info,
int num_items,
const int[][] item_info)
{
char map[PLATFORM_MAX_PATH];
char displayName[PLATFORM_MAX_PATH];
menu.GetItem(item_info[0][VOTEINFO_ITEM_INDEX], map, sizeof(map), _, displayName, sizeof(displayName));
if (strcmp(map, VOTE_EXTEND, false) == 0)
{
g_Extends++;
int time;
if (GetMapTimeLimit(time))
{
if (time > 0)
{
ExtendMapTimeLimit(g_Cvar_ExtendTimeStep.IntValue * 60);
}
}
if (g_Cvar_Winlimit)
{
int winlimit = g_Cvar_Winlimit.IntValue;
if (winlimit)
{
g_Cvar_Winlimit.IntValue = winlimit + g_Cvar_ExtendRoundStep.IntValue;
}
}
if (g_Cvar_Maxrounds)
{
int maxrounds = g_Cvar_Maxrounds.IntValue;
if (maxrounds)
{
g_Cvar_Maxrounds.IntValue = maxrounds + g_Cvar_ExtendRoundStep.IntValue;
}
}
if (g_Cvar_Fraglimit)
{
int fraglimit = g_Cvar_Fraglimit.IntValue;
if (fraglimit)
{
g_Cvar_Fraglimit.IntValue = fraglimit + g_Cvar_ExtendFragStep.IntValue;
}
}
PrintToChatAll("[SM] %t", "Current Map Extended", RoundToFloor(float(item_info[0][VOTEINFO_ITEM_VOTES])/float(num_votes)*100), num_votes);
LogAction(-1, -1, "Voting for next map has finished. The current map has been extended.");
// We extended, so we'll have to vote again.
g_HasVoteStarted = false;
CreateNextVote();
SetupTimeleftTimer();
}
else if (strcmp(map, VOTE_DONTCHANGE, false) == 0)
{
PrintToChatAll("[SM] %t", "Current Map Stays", RoundToFloor(float(item_info[0][VOTEINFO_ITEM_VOTES])/float(num_votes)*100), num_votes);
LogAction(-1, -1, "Voting for next map has finished. 'No Change' was the winner");
g_HasVoteStarted = false;
CreateNextVote();
SetupTimeleftTimer();
}
else
{
if (g_ChangeTime == MapChange_MapEnd)
{
SetNextMap(map);
}
else if (g_ChangeTime == MapChange_Instant)
{
DataPack data;
CreateDataTimer(2.0, Timer_ChangeMap, data);
data.WriteString(map);
g_ChangeMapInProgress = false;
}
else // MapChange_RoundEnd
{
SetNextMap(map);
g_ChangeMapAtRoundEnd = true;
}
g_HasVoteStarted = false;
g_MapVoteCompleted = true;
PrintToChatAll("[SM] %t", "Nextmap Voting Finished", displayName, RoundToFloor(float(item_info[0][VOTEINFO_ITEM_VOTES])/float(num_votes)*100), num_votes);
LogAction(-1, -1, "Voting for next map has finished. Nextmap: %s.", map);
}
}
public void Handler_MapVoteFinished(Menu menu,
int num_votes,
int num_clients,
const int[][] client_info,
int num_items,
const int[][] item_info)
{
if (g_Cvar_RunOff.BoolValue && num_items > 1)
{
float winningvotes = float(item_info[0][VOTEINFO_ITEM_VOTES]);
float required = num_votes * (g_Cvar_RunOffPercent.FloatValue / 100.0);
if (winningvotes < required)
{
/* Insufficient Winning margin - Lets do a runoff */
g_VoteMenu = new Menu(Handler_MapVoteMenu, MENU_ACTIONS_ALL);
g_VoteMenu.SetTitle("Runoff Vote Nextmap");
g_VoteMenu.VoteResultCallback = Handler_VoteFinishedGeneric;
char map[PLATFORM_MAX_PATH];
char info1[PLATFORM_MAX_PATH];
char info2[PLATFORM_MAX_PATH];
menu.GetItem(item_info[0][VOTEINFO_ITEM_INDEX], map, sizeof(map), _, info1, sizeof(info1));
g_VoteMenu.AddItem(map, info1);
menu.GetItem(item_info[1][VOTEINFO_ITEM_INDEX], map, sizeof(map), _, info2, sizeof(info2));
g_VoteMenu.AddItem(map, info2);
int voteDuration = g_Cvar_VoteDuration.IntValue;
g_VoteMenu.ExitButton = false;
g_VoteMenu.DisplayVoteToAll(voteDuration);
/* Notify */
float map1percent = float(item_info[0][VOTEINFO_ITEM_VOTES])/ float(num_votes) * 100;
float map2percent = float(item_info[1][VOTEINFO_ITEM_VOTES])/ float(num_votes) * 100;
PrintToChatAll("[SM] %t", "Starting Runoff", g_Cvar_RunOffPercent.FloatValue, info1, map1percent, info2, map2percent);
LogMessage("Voting for next map was indecisive, beginning runoff vote");
return;
}
}
Handler_VoteFinishedGeneric(menu, num_votes, num_clients, client_info, num_items, item_info);
}
public int Handler_MapVoteMenu(Menu menu, MenuAction action, int param1, int param2)
{
switch (action)
{
case MenuAction_End:
{
g_VoteMenu = null;
delete menu;
}
case MenuAction_Display:
{
char buffer[255];
Format(buffer, sizeof(buffer), "%T", "Vote Nextmap", param1);
Panel panel = view_as<Panel>(param2);
panel.SetTitle(buffer);
}
case MenuAction_DisplayItem:
{
if (menu.ItemCount - 1 == param2)
{
char map[PLATFORM_MAX_PATH], buffer[255];
menu.GetItem(param2, map, sizeof(map));
if (strcmp(map, VOTE_EXTEND, false) == 0)
{
Format(buffer, sizeof(buffer), "%T", "Extend Map", param1);
return RedrawMenuItem(buffer);
}
else if (strcmp(map, VOTE_DONTCHANGE, false) == 0)
{
Format(buffer, sizeof(buffer), "%T", "Dont Change", param1);
return RedrawMenuItem(buffer);
}
}
}
case MenuAction_VoteCancel:
{
// If we receive 0 votes, pick at random.
if (param1 == VoteCancel_NoVotes && g_Cvar_NoVoteMode.BoolValue)
{
int count = menu.ItemCount;
char map[PLATFORM_MAX_PATH];
menu.GetItem(0, map, sizeof(map));
// Make sure the first map in the menu isn't one of the special items.
// This would mean there are no real maps in the menu, because the special items are added after all maps. Don't do anything if that's the case.
if (strcmp(map, VOTE_EXTEND, false) != 0 && strcmp(map, VOTE_DONTCHANGE, false) != 0)
{
// Get a random map from the list.
int item = GetRandomInt(0, count - 1);
menu.GetItem(item, map, sizeof(map));
// Make sure it's not one of the special items.
while (strcmp(map, VOTE_EXTEND, false) == 0 || strcmp(map, VOTE_DONTCHANGE, false) == 0)
{
item = GetRandomInt(0, count - 1);
menu.GetItem(item, map, sizeof(map));
}
SetNextMap(map);
g_MapVoteCompleted = true;
}
}
else
{
// We were actually cancelled. I guess we do nothing.
}
g_HasVoteStarted = false;
}
}
return 0;
}
public Action Timer_ChangeMap(Handle hTimer, DataPack dp)
{