plugins/randomcycle.sp
author David Anderson <dvander@alliedmods.net>
Sat May 17 05:12:52 2008 +0000 (2008-05-17)
changeset 2056 f1fcde46ac7c
parent 1952 b7aa2b626807
child 2289 44045ead61cd
permissions -rw-r--r--
added amb1119 - sm_revote command
ds@1952
     1
/**
ds@1952
     2
 * vim: set ts=4 :
ds@1952
     3
 * =============================================================================
ds@1952
     4
 * SourceMod Random Map Cycle Plugin
ds@1952
     5
 * Randomly picks a map from the mapcycle.
ds@1952
     6
 *
ds@1952
     7
 * SourceMod (C)2004-2008 AlliedModders LLC.  All rights reserved.
ds@1952
     8
 * =============================================================================
ds@1952
     9
 *
ds@1952
    10
 * This program is free software; you can redistribute it and/or modify it under
ds@1952
    11
 * the terms of the GNU General Public License, version 3.0, as published by the
ds@1952
    12
 * Free Software Foundation.
ds@1952
    13
 * 
ds@1952
    14
 * This program is distributed in the hope that it will be useful, but WITHOUT
ds@1952
    15
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
ds@1952
    16
 * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
ds@1952
    17
 * details.
ds@1952
    18
 *
ds@1952
    19
 * You should have received a copy of the GNU General Public License along with
ds@1952
    20
 * this program.  If not, see <http://www.gnu.org/licenses/>.
ds@1952
    21
 *
ds@1952
    22
 * As a special exception, AlliedModders LLC gives you permission to link the
ds@1952
    23
 * code of this program (as well as its derivative works) to "Half-Life 2," the
ds@1952
    24
 * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
ds@1952
    25
 * by the Valve Corporation.  You must obey the GNU General Public License in
ds@1952
    26
 * all respects for all other code used.  Additionally, AlliedModders LLC grants
ds@1952
    27
 * this exception to all derivative works.  AlliedModders LLC defines further
ds@1952
    28
 * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
ds@1952
    29
 * or <http://www.sourcemod.net/license.php>.
ds@1952
    30
 *
ds@1952
    31
 * Version: $Id$
ds@1952
    32
 */
ds@1952
    33
ds@1952
    34
#pragma semicolon 1
ds@1952
    35
#include <sourcemod>
ds@1952
    36
ds@1952
    37
public Plugin:myinfo =
ds@1952
    38
{
ds@1952
    39
	name = "RandomCycle",
ds@1952
    40
	author = "AlliedModders LLC",
ds@1952
    41
	description = "Randomly chooses the next map.",
ds@1952
    42
	version = SOURCEMOD_VERSION,
ds@1952
    43
	url = "http://www.sourcemod.net/"
ds@1952
    44
};
ds@1952
    45
ds@1952
    46
new Handle:g_Cvar_Nextmap = INVALID_HANDLE;
ds@1952
    47
new Handle:g_Cvar_ExcludeMaps = INVALID_HANDLE;
ds@1952
    48
ds@1952
    49
new Handle:g_MapList = INVALID_HANDLE;
ds@1952
    50
new Handle:g_OldMapList = INVALID_HANDLE;
ds@1952
    51
new g_mapListSerial = -1;
ds@1952
    52
ds@1952
    53
public OnPluginStart()
ds@1952
    54
{
ds@1952
    55
	new arraySize = ByteCountToCells(33);	
ds@1952
    56
	g_MapList = CreateArray(arraySize);
ds@1952
    57
	g_OldMapList = CreateArray(arraySize);
ds@1952
    58
ds@1952
    59
	g_Cvar_ExcludeMaps = CreateConVar("sm_randomcycle_exclude", "5", "Specifies how many past maps to exclude from the vote.", _, true, 0.0);
ds@1952
    60
	
ds@1952
    61
	AutoExecConfig(true, "randomcycle");
ds@1952
    62
}
ds@1952
    63
ds@1952
    64
public OnConfigsExecuted()
ds@1952
    65
{
ds@1952
    66
	g_Cvar_Nextmap = FindConVar("sm_nextmap");
ds@1952
    67
ds@1952
    68
	if (g_Cvar_Nextmap == INVALID_HANDLE)
ds@1952
    69
	{
ds@1952
    70
		LogError("FATAL: Cannot find sm_nextmap cvar. RandomCycle not loaded.");
ds@1952
    71
		SetFailState("sm_nextmap not found");
ds@1952
    72
	}
ds@1952
    73
	
ds@1952
    74
	if (ReadMapList(g_MapList, 
ds@1952
    75
					g_mapListSerial, 
ds@1952
    76
					"randomcycle", 
ds@1952
    77
					MAPLIST_FLAG_CLEARARRAY|MAPLIST_FLAG_MAPSFOLDER)
ds@1952
    78
		== INVALID_HANDLE)
ds@1952
    79
	{
ds@1952
    80
		if (g_mapListSerial == -1)
ds@1952
    81
		{
ds@1952
    82
			LogError("Unable to create a valid map list.");
ds@1952
    83
		}
ds@1952
    84
	}
ds@1952
    85
	
ds@1952
    86
	CreateTimer(5.0, Timer_RandomizeNextmap); // Small delay to give Nextmap time to complete OnMapStart()
ds@1952
    87
}
ds@1952
    88
ds@1952
    89
public Action:Timer_RandomizeNextmap(Handle:timer)
ds@1952
    90
{
ds@1952
    91
	decl String:map[32];
ds@1952
    92
ds@1952
    93
	new bool:oldMaps = false;
ds@1952
    94
	if (GetConVarInt(g_Cvar_ExcludeMaps) && GetArraySize(g_MapList) > GetConVarInt(g_Cvar_ExcludeMaps))
ds@1952
    95
	{
ds@1952
    96
		oldMaps = true;
ds@1952
    97
	}
ds@1952
    98
	
ds@1952
    99
	new b = GetRandomInt(0, GetArraySize(g_MapList) - 1);
ds@1952
   100
	GetArrayString(g_MapList, b, map, sizeof(map));
ds@1952
   101
ds@1952
   102
	while (oldMaps && FindStringInArray(g_OldMapList, map) != -1)
ds@1952
   103
	{
ds@1952
   104
		b = GetRandomInt(0, GetArraySize(g_MapList) - 1);
ds@1952
   105
		GetArrayString(g_MapList, b, map, sizeof(map));
ds@1952
   106
	}
ds@1952
   107
	
ds@1952
   108
	SetConVarString(g_Cvar_Nextmap, map);
ds@1952
   109
	PushArrayString(g_OldMapList, map);
ds@1952
   110
ds@1952
   111
	if (GetArraySize(g_OldMapList) > GetConVarInt(g_Cvar_ExcludeMaps))
ds@1952
   112
	{
ds@1952
   113
		RemoveFromArray(g_OldMapList, 0);
ds@1952
   114
	}
ds@1952
   115
ds@1952
   116
	LogMessage("RandomCycle has chosen %s for the nextmap.", map);	
ds@1952
   117
ds@1952
   118
	return Plugin_Stop;
ds@1952
   119
}