plugins/mapmanager.sp
author Matt Woodrow <pred@alliedmods.net>
Mon Mar 31 22:44:18 2008 +0000 (2008-03-31)
changeset 1953 e0ca4605d828
parent 1952 b7aa2b626807
child 2027 887d7271f947
permissions -rw-r--r--
Big changes to dynamic menu. Removed constant kv tracking. Tested everything.
Import of config files.
ds@1952
     1
/**
ds@1952
     2
 * vim: set ts=4 :
ds@1952
     3
 * =============================================================================
ds@1952
     4
 * SourceMod Map Management Plugin
ds@1952
     5
 * Provides all map related functionality, including map changing, map voting,
ds@1952
     6
 * and nextmap.
ds@1952
     7
 *
ds@1952
     8
 * SourceMod (C)2004-2008 AlliedModders LLC.  All rights reserved.
ds@1952
     9
 * =============================================================================
ds@1952
    10
 *
ds@1952
    11
 * This program is free software; you can redistribute it and/or modify it under
ds@1952
    12
 * the terms of the GNU General Public License, version 3.0, as published by the
ds@1952
    13
 * Free Software Foundation.
ds@1952
    14
 * 
ds@1952
    15
 * This program is distributed in the hope that it will be useful, but WITHOUT
ds@1952
    16
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
ds@1952
    17
 * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
ds@1952
    18
 * details.
ds@1952
    19
 *
ds@1952
    20
 * You should have received a copy of the GNU General Public License along with
ds@1952
    21
 * this program.  If not, see <http://www.gnu.org/licenses/>.
ds@1952
    22
 *
ds@1952
    23
 * As a special exception, AlliedModders LLC gives you permission to link the
ds@1952
    24
 * code of this program (as well as its derivative works) to "Half-Life 2," the
ds@1952
    25
 * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
ds@1952
    26
 * by the Valve Corporation.  You must obey the GNU General Public License in
ds@1952
    27
 * all respects for all other code used.  Additionally, AlliedModders LLC grants
ds@1952
    28
 * this exception to all derivative works.  AlliedModders LLC defines further
ds@1952
    29
 * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
ds@1952
    30
 * or <http://www.sourcemod.net/license.php>.
ds@1952
    31
 *
ds@1952
    32
 * Version: $Id$
ds@1952
    33
 */
ds@1952
    34
ds@1952
    35
#pragma semicolon 1
ds@1952
    36
#include <sourcemod>
ds@1952
    37
ds@1952
    38
public Plugin:myinfo =
ds@1952
    39
{
ds@1952
    40
	name = "Map Manager",
ds@1952
    41
	author = "AlliedModders LLC",
ds@1952
    42
	description = "Map Management",
ds@1952
    43
	version = SOURCEMOD_VERSION,
ds@1952
    44
	url = "http://www.sourcemod.net/"
ds@1952
    45
};
ds@1952
    46
ds@1952
    47
#include "mapmanagement/globals.sp"
ds@1952
    48
#include "mapmanagement/commands.sp"
ds@1952
    49
#include "mapmanagement/events.sp"
ds@1952
    50
#include "mapmanagement/functions.sp"
ds@1952
    51
#include "mapmanagement/menus.sp"
ds@1952
    52
#include "mapmanagement/timers.sp"
ds@1952
    53
#include "mapmanagement/votes.sp"
ds@1952
    54
ds@1952
    55
public OnPluginStart()
ds@1952
    56
{	
ds@1952
    57
	LoadTranslations("mapmanager.phrases");
ds@1952
    58
ds@1952
    59
	// Prepare nextmap functionality.	
ds@1952
    60
	g_VGUIMenu = GetUserMessageId("VGUIMenu");
ds@1952
    61
	if (g_VGUIMenu == INVALID_MESSAGE_ID)
ds@1952
    62
	{
ds@1952
    63
		LogError("FATAL: Cannot find VGUIMenu user message id. MapManager crippled.");
ds@1952
    64
		g_NextMapEnabled = false;
ds@1952
    65
	}
ds@1952
    66
	HookUserMessage(g_VGUIMenu, UserMsg_VGUIMenu);
ds@1952
    67
ds@1952
    68
	// Create all of the arrays, sized for a 64 character string.
ds@1952
    69
	new arraySize = ByteCountToCells(64);
ds@1952
    70
	g_MapCycle = CreateArray(arraySize);
ds@1952
    71
	g_MapList = CreateArray(arraySize);
ds@1952
    72
	g_MapHistory = CreateArray(arraySize);
ds@1952
    73
	g_NextVoteMaps = CreateArray(arraySize);
ds@1952
    74
	g_SelectedMaps = CreateArray(arraySize);
ds@1952
    75
	g_NominatedMaps = CreateArray(arraySize);
ds@1952
    76
	
ds@1952
    77
	g_TeamScores = CreateArray(2);
ds@1952
    78
ds@1952
    79
	// Hook say
ds@1952
    80
	RegConsoleCmd("say", Command_Say);
ds@1952
    81
	RegConsoleCmd("say_team", Command_Say);	
ds@1952
    82
ds@1952
    83
	// Register all commands.
ds@1952
    84
	RegAdminCmd("sm_map", Command_Map, ADMFLAG_CHANGEMAP, "sm_map <map> [r/e]");
ds@1952
    85
	RegAdminCmd("sm_setnextmap", Command_SetNextmap, ADMFLAG_CHANGEMAP, "sm_setnextmap <map>");
ds@1952
    86
	RegAdminCmd("sm_votemap", Command_Votemap, ADMFLAG_VOTE|ADMFLAG_CHANGEMAP, "sm_votemap <mapname> [mapname2] ... [mapname5] ");
ds@1952
    87
	RegAdminCmd("sm_maplist", Command_List, ADMFLAG_GENERIC, "sm_maplist");
ds@1952
    88
	RegAdminCmd("sm_nominate", Command_Addmap, ADMFLAG_CHANGEMAP, "sm_nominate <mapname> - Nominates a map for RockTheVote and MapChooser. Overrides existing nominations.");
ds@1952
    89
	RegAdminCmd("sm_mapvote", Command_Mapvote, ADMFLAG_CHANGEMAP, "sm_mapvote - Forces the MapChooser vote to occur.");
ds@1952
    90
	
ds@1952
    91
	if (GetCommandFlags("nextmap") == INVALID_FCVAR_FLAGS)
ds@1952
    92
	{
ds@1952
    93
		RegServerCmd("nextmap", Command_Nextmap);
ds@1952
    94
	}	
ds@1952
    95
	
ds@1952
    96
	// Register all convars
ds@1952
    97
	g_Cvar_Nextmap = CreateConVar("sm_nextmap", "", "Sets the Next Map", FCVAR_NOTIFY);
ds@1952
    98
	
ds@1952
    99
	g_Cvar_MapCount = CreateConVar("sm_mm_maps", "4", "Number of maps to be voted on at end of map or RTV. 2 to 6. (Def 4)", 0, true, 2.0, true, 6.0);	
ds@1952
   100
	g_Cvar_Excludes = CreateConVar("sm_mm_exclude", "5", "Specifies how many past maps to exclude from end of map vote and RTV.", _, true, 0.0);
ds@1952
   101
	
ds@1952
   102
	g_Cvar_MapChooser = CreateConVar("sm_mm_mapchooser", "0", "Enables MapChooser (End of Map voting)", 0, true, 0.0, true, 1.0);
ds@1952
   103
	g_Cvar_RockTheVote = CreateConVar("sm_mm_rockthevote", "0", "Enables RockTheVote (Player initiated map votes)", 0, true, 0.0, true, 1.0);
ds@1952
   104
	g_Cvar_Randomize = CreateConVar("sm_mm_randomize", "0", "Enabled Randomizer (Randomly picks the next map)", 0, true, 0.0, true, 1.0);
ds@1952
   105
	g_Cvar_Nominate = CreateConVar("sm_mm_nominate", "1", "Enables nomination system.", 0, true, 0.0, true, 1.0);
ds@1952
   106
	
ds@1952
   107
	g_Cvar_VoteMap = CreateConVar("sm_mm_votemap", "0.60", "Percentage required for successful sm_votemap.", 0, true, 0.05, true, 1.0);
ds@1952
   108
	g_Cvar_RTVLimit = CreateConVar("sm_mm_rtvlimit", "0.60", "Required percentage of players needed to rockthevote", 0, true, 0.05, true, 1.0);
ds@1952
   109
	g_Cvar_MinPlayers = CreateConVar("sm_mm_minplayers", "0", "Number of players required before RTV will be enabled.", 0, true, 0.0, true, float(MAXPLAYERS));
ds@1952
   110
	
ds@1952
   111
	g_Cvar_StartTime = CreateConVar("sm_mapvote_start", "3.0", "Specifies when to start the vote based on time remaining.", _, true, 1.0);
ds@1952
   112
	g_Cvar_StartRounds = CreateConVar("sm_mapvote_startround", "2.0", "Specifies when to start the vote based on rounds remaining.", _, true, 1.0);
ds@1952
   113
	g_Cvar_StartFrags = CreateConVar("sm_mapvote_startfrags", "5.0", "Specifies when to start the vote base on frags remaining.", _, true, 1.0);
ds@1952
   114
	g_Cvar_ExtendTimeMax = CreateConVar("sm_extendmap_maxtime", "90", "Specifies the maximum amount of time a map can be extended", _, true, 0.0);
ds@1952
   115
	g_Cvar_ExtendTimeStep = CreateConVar("sm_extendmap_timestep", "15", "Specifies how much many more minutes each extension makes", _, true, 5.0);
ds@1952
   116
	g_Cvar_ExtendRoundMax = CreateConVar("sm_extendmap_maxrounds", "30", "Specfies the maximum amount of rounds a map can be extended", _, true, 0.0);
ds@1952
   117
	g_Cvar_ExtendRoundStep = CreateConVar("sm_extendmap_roundstep", "5", "Specifies how many more rounds each extension makes", _, true, 5.0);
ds@1952
   118
	g_Cvar_ExtendFragMax = CreateConVar("sm_extendmap_maxfrags", "150", "Specfies the maximum frags allowed that a map can be extended.", _, true, 0.0);
ds@1952
   119
	g_Cvar_ExtendFragStep = CreateConVar("sm_extendmap_fragstep", "10", "Specifies how many more frags are allowed when map is extended.", _, true, 5.0);	
ds@1952
   120
	g_Cvar_Mapfile = CreateConVar("sm_mapvote_file", "configs/maps.ini", "Map file to use. (Def sourcemod/configs/maps.ini)");
ds@1952
   121
	g_Cvar_ExcludeMaps = CreateConVar("sm_mapvote_exclude", "5", "Specifies how many past maps to exclude from the vote.", _, true, 0.0);
ds@1952
   122
	g_Cvar_IncludeMaps = CreateConVar("sm_mapvote_include", "5", "Specifies how many maps to include in the vote.", _, true, 2.0, true, 6.0);
ds@1952
   123
	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);
ds@1952
   124
	g_Cvar_Extend = CreateConVar("sm_mapvote_extend", "1", "Specifies whether or not MapChooser will allow the map to be extended.", _, true, 0.0, true, 1.0);
ds@1952
   125
	
ds@1952
   126
	// Find game convars
ds@1952
   127
	g_Cvar_Chattime = FindConVar("mp_chattime");
ds@1952
   128
	g_Cvar_Winlimit = FindConVar("mp_winlimit");
ds@1952
   129
	g_Cvar_Maxrounds = FindConVar("mp_maxrounds");
ds@1952
   130
	g_Cvar_Fraglimit = FindConVar("mp_fraglimit");	
ds@1952
   131
ds@1952
   132
	if (GetCommandFlags("nextmap") == INVALID_FCVAR_FLAGS)
ds@1952
   133
	{
ds@1952
   134
		RegServerCmd("nextmap", Command_Nextmap);
ds@1952
   135
	}
ds@1952
   136
	
ds@1952
   137
	// Hook events
ds@1952
   138
	HookEvent("round_end", Event_RoundEnd);	// We always require round_end
ds@1952
   139
	if (g_Cvar_Fraglimit != INVALID_HANDLE)
ds@1952
   140
	{
ds@1952
   141
		HookEvent("player_death", Event_PlayerDeath);		
ds@1952
   142
	}	
ds@1952
   143
	
ds@1952
   144
	// Set to the current map so OnMapStart() will know what to do
ds@1952
   145
	decl String:currentMap[64];
ds@1952
   146
	GetCurrentMap(currentMap, 64);
ds@1952
   147
	SetNextmap(currentMap);	
ds@1952
   148
ds@1952
   149
	// Create necessary menus for TopMenu
ds@1952
   150
	g_Menu_Map = CreateMenu(MenuHandler_Map);
ds@1952
   151
	SetMenuTitle(g_Menu_Map, "Please select a map");
ds@1952
   152
	SetMenuExitBackButton(g_Menu_Map, true);
ds@1952
   153
	
ds@1952
   154
	g_Menu_Votemap = CreateMenu(MenuHandler_VoteMap, MenuAction_DrawItem);
ds@1952
   155
	SetMenuTitle(g_Menu_Votemap, "Please select a map");
ds@1952
   156
	SetMenuExitBackButton(g_Menu_Votemap, true);	
ds@1952
   157
	
ds@1952
   158
	// Bind TopMenu commands to adminmenu_maplist.ini, in cases it doesn't exist in maplists.cfg
ds@1952
   159
	decl String:mapListPath[PLATFORM_MAX_PATH];
ds@1952
   160
	BuildPath(Path_SM, mapListPath, sizeof(mapListPath), "configs/adminmenu_maplist.ini");
ds@1952
   161
	SetMapListCompatBind("sm_map menu", mapListPath);
ds@1952
   162
	SetMapListCompatBind("sm_votemap menu", mapListPath);
ds@1952
   163
ds@1952
   164
	// Account for late loading
ds@1952
   165
	new Handle:topmenu;
ds@1952
   166
	if (LibraryExists("adminmenu") && ((topmenu = GetAdminTopMenu()) != INVALID_HANDLE))
ds@1952
   167
	{
ds@1952
   168
		OnAdminMenuReady(topmenu);
ds@1952
   169
	}	
ds@1952
   170
	
ds@1952
   171
	AutoExecConfig(true, "mapmanager");
ds@1952
   172
}
ds@1952
   173
ds@1952
   174
public OnAdminMenuReady(Handle:topmenu)
ds@1952
   175
{
ds@1952
   176
	/* Block us from being called twice */
ds@1952
   177
	if (topmenu == hTopMenu)
ds@1952
   178
	{
ds@1952
   179
		return;
ds@1952
   180
	}
ds@1952
   181
	
ds@1952
   182
	/* Save the Handle */
ds@1952
   183
	hTopMenu = topmenu;
ds@1952
   184
	
ds@1952
   185
	new TopMenuObject:server_commands = FindTopMenuCategory(hTopMenu, ADMINMENU_SERVERCOMMANDS);
ds@1952
   186
ds@1952
   187
	if (server_commands != INVALID_TOPMENUOBJECT)
ds@1952
   188
	{
ds@1952
   189
		AddToTopMenu(hTopMenu,
ds@1952
   190
			"sm_map",
ds@1952
   191
			TopMenuObject_Item,
ds@1952
   192
			AdminMenu_Map,
ds@1952
   193
			server_commands,
ds@1952
   194
			"sm_map",
ds@1952
   195
			ADMFLAG_CHANGEMAP);
ds@1952
   196
	}
ds@1952
   197
ds@1952
   198
	new TopMenuObject:voting_commands = FindTopMenuCategory(hTopMenu, ADMINMENU_VOTINGCOMMANDS);
ds@1952
   199
ds@1952
   200
	if (voting_commands != INVALID_TOPMENUOBJECT)
ds@1952
   201
	{
ds@1952
   202
		AddToTopMenu(hTopMenu,
ds@1952
   203
			"sm_votemap",
ds@1952
   204
			TopMenuObject_Item,
ds@1952
   205
			AdminMenu_VoteMap,
ds@1952
   206
			voting_commands,
ds@1952
   207
			"sm_votemap",
ds@1952
   208
			ADMFLAG_VOTE|ADMFLAG_CHANGEMAP);
ds@1952
   209
	}
ds@1952
   210
}
ds@1952
   211
ds@1952
   212
public OnLibraryRemoved(const String:name[])
ds@1952
   213
{
ds@1952
   214
	if (strcmp(name, "adminmenu") == 0)
ds@1952
   215
	{
ds@1952
   216
		hTopMenu = INVALID_HANDLE;
ds@1952
   217
	}
ds@1952
   218
}
ds@1952
   219
ds@1952
   220
public OnConfigsExecuted()
ds@1952
   221
{
ds@1952
   222
	// Add map logic here
ds@1952
   223
	
ds@1952
   224
	// Get the current and last maps.
ds@1952
   225
	decl String:lastMap[64], String:currentMap[64];
ds@1952
   226
	GetConVarString(g_Cvar_Nextmap, lastMap, 64);
ds@1952
   227
	GetCurrentMap(currentMap, 64);
ds@1952
   228
	
ds@1952
   229
	// Why am I doing this? If we switched to a new map, but it wasn't what we expected (Due to sm_map, sm_votemap, or
ds@1952
   230
	// some other plugin/command), we don't want to scramble the map cycle. Or for example, admin switches to a custom map
ds@1952
   231
	// not in mapcyclefile. So we keep it set to the last expected nextmap. - ferret
ds@1952
   232
	if (strcmp(lastMap, currentMap) == 0)
ds@1952
   233
	{
ds@1952
   234
		FindAndSetNextMap();
ds@1952
   235
	}
ds@1952
   236
	
ds@1952
   237
	// Build map menus for sm_map, sm_votemap, and RTV.
ds@1952
   238
	BuildMapMenu(g_Menu_Map, list);
ds@1952
   239
	BuildMapMenu(g_Menu_VoteMap, list);
ds@1952
   240
	
ds@1952
   241
	// If the Randomize option is on, randomize!
ds@1952
   242
	if (GetConVarBool(g_Cvar_Randomize))
ds@1952
   243
	{
ds@1952
   244
		CreateTimer(5.0, Timer_RandomizeNextmap);
ds@1952
   245
	}
ds@1952
   246
	
ds@1952
   247
	// If MapChooser is active, start it up!
ds@1952
   248
	if (GetConVarBool(g_Cvar_MapChooser))
ds@1952
   249
	{
ds@1952
   250
		SetupTimeleftTimer();
ds@1952
   251
		SetConVarString(g_Cvar_Nextmap, "Pending Vote");
ds@1952
   252
	}
ds@1952
   253
	
ds@1952
   254
	// If RockTheVote is active, start it up!
ds@1952
   255
	if (GetConVarBool(g_Cvar_RockTheVote))
ds@1952
   256
	{
ds@1952
   257
		BuildMapMenu(g_Menu_RTV, list);
ds@1952
   258
		CreateTimer(30.0, Timer_DelayRTV);
ds@1952
   259
	}	
ds@1952
   260
}
ds@1952
   261
ds@1952
   262
// Reinitialize all our various globals
ds@1952
   263
public OnMapStart()
ds@1952
   264
{
ds@1952
   265
	if (g_Nominate != INVALID_HANDLE)
ds@1952
   266
	{
ds@1952
   267
		ClearArray(g_Nominate);
ds@1952
   268
	}
ds@1952
   269
	
ds@1952
   270
	if (g_TeamScores != INVALID_HANDLE)
ds@1952
   271
	{
ds@1952
   272
		ClearArray(g_TeamScores);
ds@1952
   273
	}
ds@1952
   274
ds@1952
   275
	g_TotalRounds = 0;	
ds@1952
   276
	
ds@1952
   277
	g_RTV_Voters = 0;
ds@1952
   278
	g_RTV_Votes = 0;
ds@1952
   279
	g_RTV_VotesNeeded = 0;
ds@1952
   280
	g_RTV_Started = false;
ds@1952
   281
	g_RTV_Ended = false;
ds@1952
   282
}
ds@1952
   283
ds@1952
   284
// Reset globals as necessary and kill timers
ds@1952
   285
public OnMapEnd()
ds@1952
   286
{
ds@1952
   287
	g_IntermissionCalled = false;
ds@1952
   288
	g_HasVoteStarted = false;
ds@1952
   289
		
ds@1952
   290
	g_RTV_Allowed = false;	
ds@1952
   291
	
ds@1952
   292
	if (g_VoteTimer != INVALID_HANDLE)
ds@1952
   293
	{
ds@1952
   294
		KillTimer(g_VoteTimer);
ds@1952
   295
		g_VoteTimer = INVALID_HANDLE;
ds@1952
   296
	}
ds@1952
   297
	
ds@1952
   298
	if (g_RetryTimer != INVALID_HANDLE)
ds@1952
   299
	{
ds@1952
   300
		KillTimer(g_RetryTimer);
ds@1952
   301
		g_RetryTimer = INVALID_HANDLE;
ds@1952
   302
	}
ds@1952
   303
}
ds@1952
   304
ds@1952
   305
public bool:OnClientConnect(client, String:rejectmsg[], maxlen)
ds@1952
   306
{
ds@1952
   307
	if (IsFakeClient(client))
ds@1952
   308
	{
ds@1952
   309
		return true;
ds@1952
   310
	}
ds@1952
   311
	
ds@1952
   312
	// If RTV is active, deal with vote counting.
ds@1952
   313
	if (GetConVarBool(g_Cvar_RockTheVote))
ds@1952
   314
	{
ds@1952
   315
		g_RTV_Voted[client] = false;
ds@1952
   316
		g_RTV_Voters++;
ds@1952
   317
		g_RTV_VotesNeeded = RoundToFloor(float(g_Voters) * GetConVarFloat(g_Cvar_Needed));
ds@1952
   318
	}
ds@1952
   319
ds@1952
   320
	// If Nominate is active, let the new client nominate
ds@1952
   321
	if (GetConVarBool(g_Cvar_Nominate))
ds@1952
   322
	{
ds@1952
   323
		g_Nominated[client] = false;
ds@1952
   324
	}
ds@1952
   325
	
ds@1952
   326
	return true;
ds@1952
   327
}
ds@1952
   328
ds@1952
   329
public OnClientDisconnect(client)
ds@1952
   330
{
ds@1952
   331
	if (IsFakeClient(client))
ds@1952
   332
	{
ds@1952
   333
		return;
ds@1952
   334
	}
ds@1952
   335
	
ds@1952
   336
	// If RTV is active, deal with vote counting.
ds@1952
   337
	if (GetConVarBool(g_Cvar_RockTheVote))
ds@1952
   338
	{
ds@1952
   339
		if(g_RTV_Voted[client])
ds@1952
   340
		{
ds@1952
   341
			g_RTV_Votes--;
ds@1952
   342
		}
ds@1952
   343
ds@1952
   344
		g_RTV_Voters--;
ds@1952
   345
		g_RTV_VotesNeeded = RoundToFloor(float(g_RTV_Voters) * GetConVarFloat(g_Cvar_RTVLimit));
ds@1952
   346
		
ds@1952
   347
		// If this client caused us to fall below the RTV threshold and its allowed be started, start it.	
ds@1952
   348
		if (g_RTV_Votes && g_RTV_Voters && g_RTV_Votes >= g_RTV_VotesNeeded && g_RTV_Allowed && !g_RTV_Started) 
ds@1952
   349
		{
ds@1952
   350
			g_RTV_Started = true;
ds@1952
   351
			CreateTimer(2.0, Timer_StartRTV, TIMER_FLAG_NO_MAPCHANGE);
ds@1952
   352
		}	
ds@1952
   353
	}
ds@1952
   354
}
ds@1952
   355
ds@1952
   356
public OnMapTimeLeftChanged()
ds@1952
   357
{
ds@1952
   358
	if (GetConVarBool(g_Cvar_MapChooser))
ds@1952
   359
	{
ds@1952
   360
		SetupTimeleftTimer();
ds@1952
   361
	}
ds@1952
   362
}