plugins/randomcycle.sp
author David Anderson <dvander@alliedmods.net>
Sun Oct 14 21:51:30 2007 +0000 (2007-10-14)
changeset 1559 bb3fd890699d
parent 1539 74121104e67d
child 1606 4fb3959c2c96
permissions -rw-r--r--
fixed bug in build tool
     1 /**
     2  * vim: set ts=4 :
     3  * =============================================================================
     4  * SourceMod Random Map Cycle Plugin
     5  * Randomly picks a map from the mapcycle.
     6  *
     7  * SourceMod (C)2004-2007 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 #pragma semicolon 1
    35 #include <sourcemod>
    36 
    37 public Plugin:myinfo =
    38 {
    39 	name = "RandomCycle",
    40 	author = "AlliedModders LLC",
    41 	description = "Randomly chooses the next map.",
    42 	version = SOURCEMOD_VERSION,
    43 	url = "http://www.sourcemod.net/"
    44 };
    45 
    46 new Handle:g_Cvar_Nextmap = INVALID_HANDLE;
    47 new Handle:g_Cvar_Mapfile = INVALID_HANDLE;
    48 new Handle:g_Cvar_ExcludeMaps = INVALID_HANDLE;
    49 
    50 new Handle:g_MapList = INVALID_HANDLE;
    51 new Handle:g_OldMapList = INVALID_HANDLE;
    52 new g_mapFileTime;
    53 
    54 public OnPluginStart()
    55 {
    56 	g_MapList = CreateArray(33);
    57 	g_OldMapList = CreateArray(33);
    58 
    59 	g_Cvar_Mapfile = CreateConVar("sm_randomcycle_file", "configs/maps.ini", "Map file to use. (Def sourcemod/configs/maps.ini)");
    60 	g_Cvar_ExcludeMaps = CreateConVar("sm_randomcycle_exclude", "5", "Specifies how many past maps to exclude from the vote.", _, true, 0.0);
    61 	
    62 	AutoExecConfig(true, "randomcycle");
    63 }
    64 
    65 public OnMapStart()
    66 {
    67 	g_Cvar_Nextmap = FindConVar("sm_nextmap");
    68 
    69 	if (g_Cvar_Nextmap == INVALID_HANDLE)
    70 	{
    71 		LogError("FATAL: Cannot find sm_nextmap cvar. RandomCycle not loaded.");
    72 		SetFailState("sm_nextmap not found");
    73 	}
    74 	
    75 	if (LoadMaps())
    76 	{
    77 		CreateTimer(5.0, Timer_RandomizeNextmap); // Small delay to give Nextmap time to complete OnMapStart()
    78 	}
    79 }
    80 
    81 public Action:Timer_RandomizeNextmap(Handle:timer)
    82 {
    83 	decl String:map[32];
    84 
    85 	new bool:oldMaps = false;
    86 	if (GetArraySize(g_MapList) > GetConVarInt(g_Cvar_ExcludeMaps))
    87 	{
    88 		oldMaps = true;
    89 	}
    90 	
    91 	new b = GetRandomInt(0, GetArraySize(g_MapList) - 1);
    92 	GetArrayString(g_MapList, b, map, sizeof(map));
    93 
    94 	while (oldMaps && IsStringInArray(g_OldMapList, map))
    95 	{
    96 		b = GetRandomInt(0, GetArraySize(g_MapList) - 1);
    97 		GetArrayString(g_MapList, b, map, sizeof(map));
    98 	}
    99 	
   100 	SetConVarString(g_Cvar_Nextmap, map);
   101 	PushArrayString(g_OldMapList, map);
   102 
   103 	if (GetArraySize(g_OldMapList) > GetConVarInt(g_Cvar_ExcludeMaps))
   104 	{
   105 		RemoveFromArray(g_OldMapList, 0);
   106 	}
   107 
   108 	LogMessage("RandomCycle has chosen %s for the nextmap.", map);	
   109 
   110 	return Plugin_Stop;
   111 }
   112 
   113 LoadMaps()
   114 {
   115 	new bool:fileFound;
   116 
   117 	decl String:mapPath[256], String:mapFile[64];
   118 	GetConVarString(g_Cvar_Mapfile, mapFile, 64);
   119 	BuildPath(Path_SM, mapPath, sizeof(mapPath), mapFile);
   120 	fileFound = FileExists(mapPath);
   121 	if (!fileFound)
   122 	{
   123 		new Handle:mapCycleFile = FindConVar("mapcyclefile");
   124 		GetConVarString(mapCycleFile, mapPath, sizeof(mapPath));
   125 		fileFound = FileExists(mapPath);
   126 	}
   127 	
   128 	if (!fileFound)
   129 	{
   130 		LogError("Unable to locate sm_randomcycle_file or mapcyclefile, no maps loaded.");
   131 		
   132 		if (g_MapList != INVALID_HANDLE)
   133 		{
   134 			ClearArray(g_MapList);
   135 		}
   136 		
   137 		return 0;		
   138 	}
   139 
   140 	// If the file hasn't changed, there's no reason to reload
   141 	// all of the maps.
   142 	new fileTime =  GetFileTime(mapPath, FileTime_LastChange);
   143 	if (g_mapFileTime == fileTime)
   144 	{
   145 		return GetArraySize(g_MapList);
   146 	}
   147 	
   148 	g_mapFileTime = fileTime;
   149 	
   150 	// Reset the array
   151 	if (g_MapList != INVALID_HANDLE)
   152 	{
   153 		ClearArray(g_MapList);
   154 	}
   155 
   156 	LogMessage("[SM] Loading Random Cycle map file [%s]", mapPath);
   157 
   158 	new Handle:file = OpenFile(mapPath, "rt");
   159 	if (file == INVALID_HANDLE)
   160 	{
   161 		LogError("[SM] Could not open file: %s", mapPath);
   162 		return 0;
   163 	}
   164 
   165 	decl String:currentMap[32];
   166 	GetCurrentMap(currentMap, sizeof(currentMap));
   167 	
   168 	decl String:buffer[64], len;
   169 	while (!IsEndOfFile(file) && ReadFileLine(file, buffer, sizeof(buffer)))
   170 	{
   171 		TrimString(buffer);
   172 
   173 		if ((len = StrContains(buffer, ".bsp", false)) != -1)
   174 		{
   175 			buffer[len] = '\0';
   176 		}
   177 
   178 		if (buffer[0] == '\0' || !IsValidConVarChar(buffer[0]) || !IsMapValid(buffer)
   179 			|| strcmp(currentMap, buffer, false) == 0)
   180 		{
   181 			continue;
   182 		}
   183 
   184 		PushArrayString(g_MapList, buffer);
   185 	}
   186 
   187 	CloseHandle(file);
   188 	return GetArraySize(g_MapList);
   189 }