plugins/basecommands/map.sp
changeset 1536 fe2146e4975d
child 1580 e5198a9a458b
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/plugins/basecommands/map.sp	Sat Oct 13 04:43:42 2007 +0000
     1.3 @@ -0,0 +1,147 @@
     1.4 +
     1.5 +public MenuHandler_ChangeMap(Handle:menu, MenuAction:action, param1, param2)
     1.6 +{
     1.7 +	if (action == MenuAction_Cancel)
     1.8 +	{
     1.9 +		if (param2 == MenuCancel_ExitBack && hTopMenu != INVALID_HANDLE)
    1.10 +		{
    1.11 +			DisplayTopMenu(hTopMenu, param1, TopMenuPosition_LastCategory);
    1.12 +		}
    1.13 +	}
    1.14 +	else if (action == MenuAction_Select)
    1.15 +	{
    1.16 +		decl String:map[64];
    1.17 +		
    1.18 +		GetMenuItem(menu, param2, map, sizeof(map));
    1.19 +	
    1.20 +		ShowActivity(param1, "%t", "Changing map", map);
    1.21 +
    1.22 +		LogAction(param1, -1, "\"%L\" changed map to \"%s\"", param1, map);
    1.23 +
    1.24 +		new Handle:dp;
    1.25 +		CreateDataTimer(3.0, Timer_ChangeMap, dp);
    1.26 +		WritePackString(dp, map);
    1.27 +
    1.28 +	}
    1.29 +}
    1.30 +
    1.31 +public AdminMenu_Map(Handle:topmenu, 
    1.32 +							  TopMenuAction:action,
    1.33 +							  TopMenuObject:object_id,
    1.34 +							  param,
    1.35 +							  String:buffer[],
    1.36 +							  maxlength)
    1.37 +{
    1.38 +	if (action == TopMenuAction_DisplayOption)
    1.39 +	{
    1.40 +		Format(buffer, maxlength, "%T", "Choose Map", param);
    1.41 +	}
    1.42 +	else if (action == TopMenuAction_SelectOption)
    1.43 +	{
    1.44 +		DisplayMenu(g_MapList, param, MENU_TIME_FOREVER);
    1.45 +	}
    1.46 +}
    1.47 +
    1.48 +public Action:Command_Map(client, args)
    1.49 +{
    1.50 +	if (args < 1)
    1.51 +	{
    1.52 +		ReplyToCommand(client, "[SM] Usage: sm_map <map>");
    1.53 +		return Plugin_Handled;
    1.54 +	}
    1.55 +
    1.56 +	decl String:map[64];
    1.57 +	GetCmdArg(1, map, sizeof(map));
    1.58 +
    1.59 +	if (!IsMapValid(map))
    1.60 +	{
    1.61 +		ReplyToCommand(client, "[SM] %t", "Map was not found", map);
    1.62 +		return Plugin_Handled;
    1.63 +	}
    1.64 +
    1.65 +	ShowActivity(client, "%t", "Changing map", map);
    1.66 +
    1.67 +	LogAction(client, -1, "\"%L\" changed map to \"%s\"", client, map);
    1.68 +
    1.69 +	new Handle:dp;
    1.70 +	CreateDataTimer(3.0, Timer_ChangeMap, dp);
    1.71 +	WritePackString(dp, map);
    1.72 +
    1.73 +	return Plugin_Handled;
    1.74 +}
    1.75 +
    1.76 +public Action:Timer_ChangeMap(Handle:timer, Handle:dp)
    1.77 +{
    1.78 +	decl String:map[65];
    1.79 +
    1.80 +	ResetPack(dp);
    1.81 +	ReadPackString(dp, map, sizeof(map));
    1.82 +
    1.83 +	ServerCommand("changelevel \"%s\"", map);
    1.84 +
    1.85 +	return Plugin_Stop;
    1.86 +}
    1.87 +
    1.88 +LoadMaps(Handle:menu)
    1.89 +{
    1.90 +	decl String:mapPath[256];
    1.91 +	BuildPath(Path_SM, mapPath, sizeof(mapPath), "configs/menu_maplist.ini");
    1.92 +	
    1.93 +	if (!FileExists(mapPath))
    1.94 +	{
    1.95 +		LogError("Unable to locate menu_maplist, no maps loaded.");
    1.96 +		
    1.97 +		if (g_MapList != INVALID_HANDLE)
    1.98 +		{
    1.99 +			RemoveAllMenuItems(menu);
   1.100 +		}
   1.101 +		
   1.102 +		return;		
   1.103 +	}
   1.104 +
   1.105 +	// If the file hasn't changed, there's no reason to reload
   1.106 +	// all of the maps.
   1.107 +	new fileTime =  GetFileTime(mapPath, FileTime_LastChange);
   1.108 +	if (g_mapFileTime == fileTime)
   1.109 +	{
   1.110 +		return;
   1.111 +	}
   1.112 +	
   1.113 +	g_mapFileTime = fileTime;
   1.114 +	
   1.115 +	// Reset the array
   1.116 +	if (g_MapList != INVALID_HANDLE)
   1.117 +	{
   1.118 +		RemoveAllMenuItems(menu);
   1.119 +	}
   1.120 +
   1.121 +	LogMessage("[SM] Loading menu map list file [%s]", mapPath);
   1.122 +
   1.123 +	new Handle:file = OpenFile(mapPath, "rt");
   1.124 +	if (file == INVALID_HANDLE)
   1.125 +	{
   1.126 +		LogError("[SM] Could not open file: %s", mapPath);
   1.127 +		return;
   1.128 +	}
   1.129 +	
   1.130 +	decl String:buffer[64], len;
   1.131 +	while (!IsEndOfFile(file) && ReadFileLine(file, buffer, sizeof(buffer)))
   1.132 +	{
   1.133 +		TrimString(buffer);
   1.134 +
   1.135 +		if ((len = StrContains(buffer, ".bsp", false)) != -1)
   1.136 +		{
   1.137 +			buffer[len] = '\0';
   1.138 +		}
   1.139 +
   1.140 +		if (buffer[0] == '\0' || !IsValidConVarChar(buffer[0]) || !IsMapValid(buffer))
   1.141 +		{
   1.142 +			continue;
   1.143 +		}
   1.144 +
   1.145 +		AddMenuItem(menu, buffer, buffer);
   1.146 +	}
   1.147 +
   1.148 +	CloseHandle(file);
   1.149 +	return;
   1.150 +}
   1.151 \ No newline at end of file