-
Notifications
You must be signed in to change notification settings - Fork 6
/
nextmap_poplimits.sp
361 lines (296 loc) · 9.18 KB
/
nextmap_poplimits.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
/**
* vim: set ts=4 :
* =============================================================================
* SourceMod Nextmap Plugin
* Adds sm_nextmap cvar for changing map and nextmap chat trigger.
*
* 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$
*/
#include <sourcemod>
#include "include/nextmap.inc"
#pragma semicolon 1
#pragma newdecls required
public Plugin myinfo =
{
name = "Nextmap w/ Population Limits",
author = "AlliedModders LLC/StevoTVR",
description = "Provides nextmap and sm_nextmap, with population limits",
version = SOURCEMOD_VERSION,
url = "http://www.sourcemod.net/"
};
int g_MapPos = -1;
int g_NextMapPos = -1;
ArrayList g_MapList = null;
ArrayList g_MapLimits = null;
int g_CurrentMapStartTime;
bool g_NextMapLock;
public APLRes AskPluginLoad2(Handle myself, bool late, char[] error, int err_max)
{
char game[128];
GetGameFolderName(game, sizeof(game));
if (StrEqual(game, "left4dead", false)
|| StrEqual(game, "dystopia", false)
|| StrEqual(game, "synergy", false)
|| StrEqual(game, "left4dead2", false)
|| StrEqual(game, "garrysmod", false)
|| StrEqual(game, "swarm", false)
|| StrEqual(game, "dota", false)
|| StrEqual(game, "bms", false)
|| GetEngineVersion() == Engine_Insurgency)
{
strcopy(error, err_max, "Nextmap is incompatible with this game");
return APLRes_SilentFailure;
}
DisablePlugin("nextmap");
DisablePlugin("mapchooser");
return APLRes_Success;
}
public void OnPluginStart()
{
LoadTranslations("common.phrases");
LoadTranslations("nextmap.phrases");
LoadTranslations("mapchooser.phrases");
int size = ByteCountToCells(PLATFORM_MAX_PATH);
g_MapList = new ArrayList(size);
g_MapLimits = new ArrayList(2);
RegAdminCmd("sm_maphistory", Command_MapHistory, ADMFLAG_CHANGEMAP, "Shows the most recent maps played");
RegAdminCmd("sm_setnextmap", Command_SetNextmap, ADMFLAG_CHANGEMAP, "sm_setnextmap <map>");
RegConsoleCmd("listmaps", Command_List);
CreateTimer(60.0, Timer_Update, _, TIMER_REPEAT);
}
public void OnMapStart()
{
g_CurrentMapStartTime = GetTime();
g_NextMapLock = false;
}
public void OnConfigsExecuted()
{
ReadMapCycle();
g_MapPos = -1;
}
public Action Command_List(int client, int args)
{
PrintToConsole(client, "Map Cycle:");
int mapCount = g_MapList.Length;
char mapName[PLATFORM_MAX_PATH];
int limits[2];
for (int i = 0; i < mapCount; i++)
{
g_MapList.GetString(i, mapName, sizeof(mapName));
g_MapLimits.GetArray(i, limits);
int min = limits[0];
int max = limits[1] > 0 ? limits[1] : MaxClients;
PrintToConsole(client, "%s (%i - %i pl.)", mapName, min, max);
}
return Plugin_Handled;
}
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];
GetCmdArg(1, map, sizeof(map));
if (!IsMapValid(map))
{
ReplyToCommand(client, "[SM] %t", "Map was not found", map);
return Plugin_Handled;
}
ShowActivity(client, "%t", "Changed Next Map", map);
LogAction(client, -1, "\"%L\" changed nextmap to \"%s\"", client, map);
SetNextMap(map);
g_NextMapLock = true;
return Plugin_Handled;
}
public Action Timer_Update(Handle timer)
{
if (!g_NextMapLock)
FindAndSetNextMap();
}
void FindAndSetNextMap()
{
int mapCount = g_MapList.Length;
char mapName[PLATFORM_MAX_PATH];
if (g_MapPos == -1)
{
char current[PLATFORM_MAX_PATH];
GetCurrentMap(current, sizeof(current));
for (int i = 0; i < mapCount; i++)
{
g_MapList.GetString(i, mapName, sizeof(mapName));
if (strcmp(current, mapName, false) == 0)
{
g_MapPos = i;
break;
}
}
if (g_MapPos == -1)
g_MapPos = 0;
}
g_NextMapPos = g_MapPos;
if (mapCount > 1)
{
int pop = GetClientCount();
int limits[2];
int nextMap = g_NextMapPos + 1;
if (nextMap >= mapCount)
nextMap = 0;
while (nextMap != g_MapPos)
{
g_MapLimits.GetArray(nextMap, limits);
if (limits[0] <= pop && (limits[1] == 0 || limits[1] >= pop))
{
break;
}
nextMap++;
if (nextMap >= mapCount)
nextMap = 0;
}
g_NextMapPos = nextMap;
}
g_MapList.GetString(g_NextMapPos, mapName, sizeof(mapName));
SetNextMap(mapName);
}
void ReadMapCycle()
{
char fileName[PLATFORM_MAX_PATH];
if (!FindMapCycle(fileName, sizeof(fileName)))
{
LogError("FATAL: Cannot load map cycle. Nextmap not loaded.");
SetFailState("Mapcycle Not Found");
}
File mapCycle = OpenFile(fileName, "r", true);
if (mapCycle != null)
{
g_MapList.Clear();
char line[PLATFORM_MAX_PATH];
char entry[3][PLATFORM_MAX_PATH];
int limits[2];
while (mapCycle.ReadLine(line, sizeof(line)))
{
// format: map_name [min_players] [max_players]
int num = ExplodeString(line, " ", entry, sizeof(entry), sizeof(entry[]));
// add map name
TrimString(entry[0]);
if (!IsMapValid(entry[0]))
continue;
g_MapList.PushString(entry[0]);
// add population limits for map entry
limits[0] = num > 1 ? StringToInt(entry[1]) : 0;
limits[1] = num > 2 ? StringToInt(entry[2]) : 0;
g_MapLimits.PushArray(limits);
}
mapCycle.Close();
}
}
bool FindMapCycle(char[] buffer, int maxlength)
{
char path[PLATFORM_MAX_PATH];
char fileName[PLATFORM_MAX_PATH];
ConVar mapCycleFile = FindConVar("mapcyclefile");
mapCycleFile.GetString(fileName, sizeof(fileName));
Format(path, sizeof(path), "cfg/%s", fileName);
if (!FileExists(path, true))
{
Format(path, sizeof(path), "%s", fileName);
if (!FileExists(path, true))
{
Format(path, sizeof(path), "cfg/mapcycle_default.txt");
if (!FileExists(path, true))
return false;
}
}
strcopy(buffer, maxlength, path);
return true;
}
public Action Command_MapHistory(int client, int args)
{
int mapCount = GetMapHistorySize();
char mapName[PLATFORM_MAX_PATH];
char changeReason[100];
char timeString[100];
char playedTime[100];
int startTime;
int lastMapStartTime = g_CurrentMapStartTime;
PrintToConsole(client, "%t:\n", "Map History");
PrintToConsole(client, "%t : %t : %t : %t", "Map", "Started", "Played Time", "Reason");
GetCurrentMap(mapName, sizeof(mapName));
PrintToConsole(client, "%02i. %s (%t)", 0, mapName, "Current Map");
for (int i=0; i<mapCount; i++)
{
GetMapHistory(i, mapName, sizeof(mapName), changeReason, sizeof(changeReason), startTime);
FormatTimeDuration(timeString, sizeof(timeString), GetTime() - startTime);
FormatTimeDuration(playedTime, sizeof(playedTime), lastMapStartTime - startTime);
PrintToConsole(client, "%02i. %s : %s %t : %s : %s", i+1, mapName, timeString, "ago", playedTime, changeReason);
lastMapStartTime = startTime;
}
return Plugin_Handled;
}
int FormatTimeDuration(char[] buffer, int maxlen, int time)
{
int days = time / 86400;
int hours = (time / 3600) % 24;
int minutes = (time / 60) % 60;
int seconds = time % 60;
if (days > 0)
{
return Format(buffer, maxlen, "%id %ih %im", days, hours, (seconds >= 30) ? minutes+1 : minutes);
}
else if (hours > 0)
{
return Format(buffer, maxlen, "%ih %im", hours, (seconds >= 30) ? minutes+1 : minutes);
}
else if (minutes > 0)
{
return Format(buffer, maxlen, "%im", (seconds >= 30) ? minutes+1 : minutes);
}
else
{
return Format(buffer, maxlen, "%is", seconds);
}
}
// Taken from SourceBans
bool DisablePlugin(const char[] file)
{
char sNewPath[PLATFORM_MAX_PATH + 1];
char sOldPath[PLATFORM_MAX_PATH + 1];
BuildPath(Path_SM, sNewPath, sizeof(sNewPath), "plugins/disabled/%s.smx", file);
BuildPath(Path_SM, sOldPath, sizeof(sOldPath), "plugins/%s.smx", file);
// If plugins/<file>.smx does not exist, ignore
if (!FileExists(sOldPath))
return false;
// If plugins/disabled/<file>.smx exists, delete it
if (FileExists(sNewPath))
DeleteFile(sNewPath);
// Unload plugins/<file>.smx and move it to plugins/disabled/<file>.smx
ServerCommand("sm plugins unload %s", file);
RenameFile(sNewPath, sOldPath);
LogMessage("plugins/%s.smx was unloaded and moved to plugins/disabled/%s.smx", file, file);
return true;
}