plugins/basecommands/map.sp
author David Anderson <dvander@alliedmods.net>
Fri Jul 11 06:51:21 2008 +0000 (2008-07-11)
branchrefac-jit
changeset 2192 7602d7fb60d9
parent 2168 c24a6fa957db
permissions -rw-r--r--
fixed a compat regression, forgot msvc does funky things with overloaded virtual calls
     1 /**
     2  * vim: set ts=4 :
     3  * =============================================================================
     4  * SourceMod Basecommands Plugin
     5  * Provides map functionality
     6  *
     7  * SourceMod (C)2004-2008 AlliedModders LLC.  All rights reserved.
     8  * =============================================================================
     9  *
    10  * This program is free software; you can redistribute it and/or modify it under
    11  * the terms of the GNU General Public License, version 3.0, as published by the
    12  * Free Software Foundation.
    13  * 
    14  * This program is distributed in the hope that it will be useful, but WITHOUT
    15  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    16  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
    17  * details.
    18  *
    19  * You should have received a copy of the GNU General Public License along with
    20  * this program.  If not, see <http://www.gnu.org/licenses/>.
    21  *
    22  * As a special exception, AlliedModders LLC gives you permission to link the
    23  * code of this program (as well as its derivative works) to "Half-Life 2," the
    24  * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
    25  * by the Valve Corporation.  You must obey the GNU General Public License in
    26  * all respects for all other code used.  Additionally, AlliedModders LLC grants
    27  * this exception to all derivative works.  AlliedModders LLC defines further
    28  * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
    29  * or <http://www.sourcemod.net/license.php>.
    30  *
    31  * Version: $Id$
    32  */
    33  
    34 public MenuHandler_ChangeMap(Handle:menu, MenuAction:action, param1, param2)
    35 {
    36 	if (action == MenuAction_Cancel)
    37 	{
    38 		if (param2 == MenuCancel_ExitBack && hTopMenu != INVALID_HANDLE)
    39 		{
    40 			DisplayTopMenu(hTopMenu, param1, TopMenuPosition_LastCategory);
    41 		}
    42 	}
    43 	else if (action == MenuAction_Select)
    44 	{
    45 		decl String:map[64];
    46 		
    47 		GetMenuItem(menu, param2, map, sizeof(map));
    48 	
    49 		ShowActivity2(param1, "[SM] ", "%t", "Changing map", map);
    50 
    51 		LogAction(param1, -1, "\"%L\" changed map to \"%s\"", param1, map);
    52 
    53 		new Handle:dp;
    54 		CreateDataTimer(3.0, Timer_ChangeMap, dp);
    55 		WritePackString(dp, map);
    56 
    57 	}
    58 }
    59 
    60 public AdminMenu_Map(Handle:topmenu, 
    61 							  TopMenuAction:action,
    62 							  TopMenuObject:object_id,
    63 							  param,
    64 							  String:buffer[],
    65 							  maxlength)
    66 {
    67 	if (action == TopMenuAction_DisplayOption)
    68 	{
    69 		Format(buffer, maxlength, "%T", "Choose Map", param);
    70 	}
    71 	else if (action == TopMenuAction_SelectOption)
    72 	{
    73 		DisplayMenu(g_MapList, param, MENU_TIME_FOREVER);
    74 	}
    75 }
    76 
    77 public Action:Command_Map(client, args)
    78 {
    79 	if (args < 1)
    80 	{
    81 		ReplyToCommand(client, "[SM] Usage: sm_map <map>");
    82 		return Plugin_Handled;
    83 	}
    84 
    85 	decl String:map[64];
    86 	GetCmdArg(1, map, sizeof(map));
    87 
    88 	if (!IsMapValid(map))
    89 	{
    90 		ReplyToCommand(client, "[SM] %t", "Map was not found", map);
    91 		return Plugin_Handled;
    92 	}
    93 
    94 	ShowActivity2(client, "[SM] ", "%t", "Changing map", map);
    95 
    96 	LogAction(client, -1, "\"%L\" changed map to \"%s\"", client, map);
    97 
    98 	new Handle:dp;
    99 	CreateDataTimer(3.0, Timer_ChangeMap, dp);
   100 	WritePackString(dp, map);
   101 
   102 	return Plugin_Handled;
   103 }
   104 
   105 public Action:Timer_ChangeMap(Handle:timer, Handle:dp)
   106 {
   107 	decl String:map[65];
   108 
   109 	ResetPack(dp);
   110 	ReadPackString(dp, map, sizeof(map));
   111 
   112 	ServerCommand("changelevel \"%s\"", map);
   113 
   114 	return Plugin_Stop;
   115 }
   116 
   117 new Handle:g_map_array = INVALID_HANDLE;
   118 new g_map_serial = -1;
   119 
   120 LoadMapList(Handle:menu)
   121 {
   122 	new Handle:map_array;
   123 	
   124 	if ((map_array = ReadMapList(g_map_array,
   125 			g_map_serial,
   126 			"sm_map menu",
   127 			MAPLIST_FLAG_CLEARARRAY|MAPLIST_FLAG_NO_DEFAULT|MAPLIST_FLAG_MAPSFOLDER))
   128 		!= INVALID_HANDLE)
   129 	{
   130 		g_map_array = map_array;
   131 	}
   132 	
   133 	if (g_map_array == INVALID_HANDLE)
   134 	{
   135 		return 0;
   136 	}
   137 	
   138 	RemoveAllMenuItems(menu);
   139 	
   140 	decl String:map_name[64];
   141 	new map_count = GetArraySize(g_map_array);
   142 	
   143 	for (new i = 0; i < map_count; i++)
   144 	{
   145 		GetArrayString(g_map_array, i, map_name, sizeof(map_name));
   146 		AddMenuItem(menu, map_name, map_name);
   147 	}
   148 	
   149 	return map_count;
   150 }