plugins/randomcycle.sp
author Nicholas Hastings <skamonkey@gmail.com>
Tue May 14 11:27:06 2013 -0400 (2013-05-14)
changeset 3918 312fa627bf21
parent 3217 b50010ccb6d0
permissions -rw-r--r--
Fixed SDKTools dota build.
     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-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 #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_ExcludeMaps = INVALID_HANDLE;
    47 
    48 new Handle:g_MapList = INVALID_HANDLE;
    49 new Handle:g_OldMapList = INVALID_HANDLE;
    50 new g_mapListSerial = -1;
    51 
    52 public OnPluginStart()
    53 {
    54 	new arraySize = ByteCountToCells(33);	
    55 	g_MapList = CreateArray(arraySize);
    56 	g_OldMapList = CreateArray(arraySize);
    57 
    58 	g_Cvar_ExcludeMaps = CreateConVar("sm_randomcycle_exclude", "5", "Specifies how many past maps to exclude from the vote.", _, true, 0.0);
    59 	
    60 	AutoExecConfig(true, "randomcycle");
    61 }
    62 
    63 public OnConfigsExecuted()
    64 {
    65 	if (ReadMapList(g_MapList, 
    66 					g_mapListSerial, 
    67 					"randomcycle", 
    68 					MAPLIST_FLAG_CLEARARRAY|MAPLIST_FLAG_MAPSFOLDER)
    69 		== INVALID_HANDLE)
    70 	{
    71 		if (g_mapListSerial == -1)
    72 		{
    73 			LogError("Unable to create a valid map list.");
    74 		}
    75 	}
    76 	
    77 	CreateTimer(5.0, Timer_RandomizeNextmap); // Small delay to give Nextmap time to complete OnMapStart()
    78 }
    79 
    80 public Action:Timer_RandomizeNextmap(Handle:timer)
    81 {
    82 	decl String:map[32];
    83 
    84 	new bool:oldMaps = false;
    85 	if (GetConVarInt(g_Cvar_ExcludeMaps) && GetArraySize(g_MapList) > GetConVarInt(g_Cvar_ExcludeMaps))
    86 	{
    87 		oldMaps = true;
    88 	}
    89 	
    90 	new b = GetRandomInt(0, GetArraySize(g_MapList) - 1);
    91 	GetArrayString(g_MapList, b, map, sizeof(map));
    92 
    93 	while (oldMaps && FindStringInArray(g_OldMapList, map) != -1)
    94 	{
    95 		b = GetRandomInt(0, GetArraySize(g_MapList) - 1);
    96 		GetArrayString(g_MapList, b, map, sizeof(map));
    97 	}
    98 	
    99 	PushArrayString(g_OldMapList, map);
   100 	SetNextMap(map);
   101 
   102 	if (GetArraySize(g_OldMapList) > GetConVarInt(g_Cvar_ExcludeMaps))
   103 	{
   104 		RemoveFromArray(g_OldMapList, 0);
   105 	}
   106 
   107 	LogAction(-1, -1, "RandomCycle has chosen %s for the nextmap.", map);	
   108 
   109 	return Plugin_Stop;
   110 }