scripting/dm_preset_spawns.sp
changeset 17 c9af6f9abbd4
child 92 52cc55d67b2f
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/scripting/dm_preset_spawns.sp	Tue Jun 26 23:13:05 2007 +0000
     1.3 @@ -0,0 +1,476 @@
     1.4 +/**
     1.5 + * dm_preset_spawns.sp
     1.6 + * Adds preset spawning to CS:S DM.
     1.7 + * This file is part of CS:S DM, Copyright (C) 2005-2007 AlliedModders LLC
     1.8 + *
     1.9 + * This program is free software; you can redistribute it and/or
    1.10 + * modify it under the terms of the GNU General Public License
    1.11 + * as published by the Free Software Foundation; either version 2
    1.12 + * of the License, or (at your option) any later version.
    1.13 + *
    1.14 + * This program is distributed in the hope that it will be useful,
    1.15 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.16 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    1.17 + * GNU General Public License for more details.
    1.18 + * 
    1.19 + * You should have received a copy of the GNU General Public License
    1.20 + * along with this program; if not, write to the Free Software
    1.21 + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
    1.22 + *
    1.23 + * Version: $Id$
    1.24 + */
    1.25 +
    1.26 +#pragma semicolon 1
    1.27 +#pragma dynamic 16384
    1.28 +#include <sourcemod>
    1.29 +#include <sdktools>
    1.30 +#include <cssdm>
    1.31 +
    1.32 +public Plugin:myinfo = 
    1.33 +{
    1.34 +	name = "CS:S DM Preset Spawns",
    1.35 +	author = "AlliedModders LLC",
    1.36 +	description = "Adds preset spawning to CS:S DM",
    1.37 +	version = CSSDM_VERSION,
    1.38 +	url = "http://www.bailopan.net/cssdm/"
    1.39 +};
    1.40 +
    1.41 +#define MAX_SPAWNS			256
    1.42 +
    1.43 +
    1.44 +new bool:g_AreWeSpawning = false;
    1.45 +new g_SpawnCount = 0;
    1.46 +new Float:g_SpawnOrigins[MAX_SPAWNS][3];
    1.47 +new Float:g_SpawnAngles[MAX_SPAWNS][3];
    1.48 +new Handle:g_hSpawnMenu = INVALID_HANDLE;
    1.49 +new g_LastLocation[MAXPLAYERS+1];
    1.50 +
    1.51 +public OnPluginStart()
    1.52 +{
    1.53 +	RegAdminCmd("cssdm_spawn_menu", Command_SpawnMenu, ADMFLAG_CHANGEMAP, "Edits CS:S DM spawn points");
    1.54 +	
    1.55 +	g_hSpawnMenu = CreateMenu(Menu_EditSpawns);
    1.56 +	SetMenuTitle(g_hSpawnMenu, "Spawn Point Editor");
    1.57 +	AddMenuItem(g_hSpawnMenu, "nearest", "Teleport to nearest");
    1.58 +	AddMenuItem(g_hSpawnMenu, "previous", "Teleport to previous");
    1.59 +	AddMenuItem(g_hSpawnMenu, "next", "Teleport to next");
    1.60 +	AddMenuItem(g_hSpawnMenu, "add", "Add position");
    1.61 +	AddMenuItem(g_hSpawnMenu, "preinsert", "Insert position here");
    1.62 +	AddMenuItem(g_hSpawnMenu, "delete", "Delete nearest");
    1.63 +	AddMenuItem(g_hSpawnMenu, "clear", "Delete all");
    1.64 +}
    1.65 +
    1.66 +public OnClientPutInServer(client)
    1.67 +{
    1.68 +	g_LastLocation[client] = -1;
    1.69 +}
    1.70 +
    1.71 +/* :TODO: we need this in core */
    1.72 +Float:GetDistance(const Float:vec1[3], const Float:vec2[3])
    1.73 +{
    1.74 +	decl Float:x, Float:y, Float:z;
    1.75 +	
    1.76 +	x = vec1[0] - vec2[0];
    1.77 +	y = vec1[1] - vec2[1];
    1.78 +	z = vec1[2] - vec2[2];
    1.79 +	
    1.80 +	return SquareRoot(x*x + y*y + z*z);
    1.81 +}
    1.82 +
    1.83 +GetNearestSpawn(client)
    1.84 +{
    1.85 +	if (!g_SpawnCount)
    1.86 +	{
    1.87 +		return -1;
    1.88 +	}
    1.89 +	
    1.90 +	new Float:clorigin[3];
    1.91 +	GetClientAbsOrigin(client, clorigin);
    1.92 +	
    1.93 +	new Float:low_diff = GetDistance(g_SpawnOrigins[0], clorigin);
    1.94 +	new low_index = 0;
    1.95 +	for (new i=1; i<g_SpawnCount; i++)
    1.96 +	{
    1.97 +		new Float:diff = GetDistance(g_SpawnOrigins[i], clorigin);
    1.98 +		if (diff < low_diff)
    1.99 +		{
   1.100 +			low_diff = diff;
   1.101 +			low_index = i;
   1.102 +		}
   1.103 +	}
   1.104 +	
   1.105 +	return low_index;
   1.106 +}
   1.107 +
   1.108 +bool:LoadMapConfig()
   1.109 +{
   1.110 +	new String:map[64];
   1.111 +	GetCurrentMap(map, sizeof(map));
   1.112 +	
   1.113 +	new String:path[255];
   1.114 +	Format(path, sizeof(path), "cfg/cssdm/spawns/%s.txt", map);
   1.115 +	
   1.116 +	g_SpawnCount = 0;
   1.117 +	
   1.118 +	new Handle:file = OpenFile(path, "rt");
   1.119 +	if (file == INVALID_HANDLE)
   1.120 +	{
   1.121 +		LogError("Could not find spawn point file \"%s\"", path);
   1.122 +		LogError("Defaulting to map-based spawns!");
   1.123 +		return false;
   1.124 +	}
   1.125 +	
   1.126 +	new String:buffer[255];
   1.127 +	new String:parts[6][16];
   1.128 +	new partCount;
   1.129 +	while (!IsEndOfFile(file) && ReadFileLine(file, buffer, sizeof(buffer)))
   1.130 +	{
   1.131 +		TrimString(buffer);
   1.132 +		partCount = ExplodeString(buffer, " ", parts, 6, 16);
   1.133 +		if (partCount < 6)
   1.134 +		{
   1.135 +			continue;
   1.136 +		}
   1.137 +		g_SpawnOrigins[g_SpawnCount][0] = StringToFloat(parts[0]);
   1.138 +		g_SpawnOrigins[g_SpawnCount][1] = StringToFloat(parts[1]);
   1.139 +		g_SpawnOrigins[g_SpawnCount][2] = StringToFloat(parts[2]);
   1.140 +		g_SpawnAngles[g_SpawnCount][0] = StringToFloat(parts[3]);
   1.141 +		g_SpawnAngles[g_SpawnCount][1] = StringToFloat(parts[4]);
   1.142 +		g_SpawnAngles[g_SpawnCount][2] = StringToFloat(parts[5]);
   1.143 +		g_SpawnCount++;
   1.144 +	}
   1.145 +	
   1.146 +	CloseHandle(file);
   1.147 +	
   1.148 +	LogMessage("Preset spawn points loaded (number %d) (map %s)", g_SpawnCount, map);
   1.149 +	
   1.150 +	return true;
   1.151 +}
   1.152 +
   1.153 +bool:WriteMapConfig()
   1.154 +{
   1.155 +	new String:map[64];
   1.156 +	GetCurrentMap(map, sizeof(map));
   1.157 +	
   1.158 +	new String:path[255];
   1.159 +	Format(path, sizeof(path), "cfg/cssdm/spawns/%s.txt", map);
   1.160 +	
   1.161 +	new Handle:file = OpenFile(path, "wt");
   1.162 +	if (file == INVALID_HANDLE)
   1.163 +	{
   1.164 +		LogError("Could not open spawn point file \"%s\" for writing.", path);
   1.165 +		return false;
   1.166 +	}
   1.167 +	
   1.168 +	for (new i=0; i<g_SpawnCount; i++)
   1.169 +	{
   1.170 +		WriteFileLine(file, "%f %f %f %f %f %f", 
   1.171 +			g_SpawnOrigins[i][0],
   1.172 +			g_SpawnOrigins[i][1],
   1.173 +			g_SpawnOrigins[i][2],
   1.174 +			g_SpawnAngles[i][0],
   1.175 +			g_SpawnAngles[i][1],
   1.176 +			g_SpawnAngles[i][2]);
   1.177 +	}
   1.178 +	
   1.179 +	CloseHandle(file);
   1.180 +	
   1.181 +	return true;
   1.182 +}
   1.183 +
   1.184 +AddSpawnFromClient(client)
   1.185 +{
   1.186 +	if (g_SpawnCount >= MAX_SPAWNS)
   1.187 +	{
   1.188 +		return -1;
   1.189 +	}
   1.190 +	
   1.191 +	GetClientAbsOrigin(client, g_SpawnOrigins[g_SpawnCount]);
   1.192 +	GetClientAbsAngles(client, g_SpawnAngles[g_SpawnCount]);
   1.193 +	
   1.194 +	new old = g_SpawnCount++;
   1.195 +	
   1.196 +	return old;
   1.197 +}
   1.198 +
   1.199 +InsertSpawnFromClient(client, bool:pre, index)
   1.200 +{
   1.201 +	if (index == g_SpawnCount - 1 && !pre)
   1.202 +	{
   1.203 +		return AddSpawnFromClient(client);
   1.204 +	}
   1.205 +	
   1.206 +	if (g_SpawnCount >= MAX_SPAWNS)
   1.207 +	{
   1.208 +		return -1;
   1.209 +	}
   1.210 +	
   1.211 +	/* If this is a post-insertion, unmark the index for moving */
   1.212 +	if (!pre)
   1.213 +	{
   1.214 +		index++;
   1.215 +	}
   1.216 +	
   1.217 +	/* Move all of the slots down */
   1.218 +	for (new i=g_SpawnCount-1; i>=index; i--)
   1.219 +	{
   1.220 +		g_SpawnOrigins[i+1] = g_SpawnOrigins[i];
   1.221 +		g_SpawnAngles[i+1] = g_SpawnAngles[i];
   1.222 +	}
   1.223 +	
   1.224 +	GetClientAbsOrigin(client, g_SpawnOrigins[index]);
   1.225 +	GetClientAbsAngles(client, g_SpawnAngles[index]);
   1.226 +	
   1.227 +	g_SpawnCount++;
   1.228 +	
   1.229 +	return index;
   1.230 +}
   1.231 +
   1.232 +bool:DeleteSpawn(index)
   1.233 +{
   1.234 +	if (index < 0 || index >= g_SpawnCount)
   1.235 +	{
   1.236 +		return false;
   1.237 +	}
   1.238 +	
   1.239 +	for (new i=index; i<g_SpawnCount-1; i++)
   1.240 +	{
   1.241 +		g_SpawnAngles[i] = g_SpawnAngles[i+1];
   1.242 +		g_SpawnOrigins[i] = g_SpawnOrigins[i+1];
   1.243 +	}
   1.244 +	
   1.245 +	g_SpawnCount--;
   1.246 +	
   1.247 +	return true;
   1.248 +}
   1.249 +
   1.250 +public Action:Command_SpawnMenu(client, args)
   1.251 +{
   1.252 +	if (client == 0)
   1.253 +	{
   1.254 +		ReplyToCommand(client, "[CSSDM] This command is not available from the server console.");
   1.255 +		return Plugin_Handled;
   1.256 +	}
   1.257 +	
   1.258 +	DisplayMenu(g_hSpawnMenu, client, MENU_TIME_FOREVER);
   1.259 +	
   1.260 +	return Plugin_Handled;
   1.261 +}
   1.262 +
   1.263 +public Panel_VerifyDeleteSpawns(Handle:menu, MenuAction:action, param1, param2)
   1.264 +{
   1.265 +	if (action == MenuAction_Select)
   1.266 +	{
   1.267 +		if (param2 == 1)
   1.268 +		{
   1.269 +			g_SpawnCount = 0;
   1.270 +			if (!WriteMapConfig())
   1.271 +			{
   1.272 +				PrintToChat(param1, "[CSSDM] Could not write to spawn config file.");
   1.273 +			} else {
   1.274 +				PrintToChat(param1, "[CSSDM] All spawn points have been deleted.");
   1.275 +			}
   1.276 +		}
   1.277 +		DisplayMenu(g_hSpawnMenu, param1, MENU_TIME_FOREVER);
   1.278 +	}
   1.279 +}
   1.280 +
   1.281 +public Menu_EditSpawns(Handle:menu, MenuAction:action, param1, param2)
   1.282 +{
   1.283 +	if (action == MenuAction_Select)
   1.284 +	{
   1.285 +		if (param2 == 0)
   1.286 +		{
   1.287 +			new index = GetNearestSpawn(param1);
   1.288 +			if (index == -1)
   1.289 +			{
   1.290 +				PrintToChat(param1, "[CSSDM] There are no spawn points.");
   1.291 +			} else {
   1.292 +				TeleportEntity(param1, g_SpawnOrigins[index], g_SpawnAngles[index], NULL_VECTOR);
   1.293 +				PrintToChat(param1, "[CSSDM] Teleported to spawn #%d (%d total).", index, g_SpawnCount);
   1.294 +				g_LastLocation[param1] = index;
   1.295 +			}
   1.296 +		} else if (param2 == 2) {
   1.297 +			if (g_SpawnCount == 0)
   1.298 +			{
   1.299 +				PrintToChat(param1, "[CSSDM] There are no spawn points.");
   1.300 +			} else {
   1.301 +				new index = g_LastLocation[param1] + 1;
   1.302 +				if (index >= g_SpawnCount)
   1.303 +				{
   1.304 +					index = 0;
   1.305 +				}
   1.306 +				TeleportEntity(param1, g_SpawnOrigins[index], g_SpawnAngles[index], NULL_VECTOR);
   1.307 +				PrintToChat(param1, "[CSSDM] Teleported to spawn #%d (%d total).", index, g_SpawnCount);
   1.308 +				g_LastLocation[param1] = index;
   1.309 +			}
   1.310 +		} else if (param2 == 1) {
   1.311 +			if (g_SpawnCount == 0)
   1.312 +			{
   1.313 +				PrintToChat(param1, "[CSSDM] There are no spawn points.");
   1.314 +			} else {
   1.315 +				new index = g_LastLocation[param1] - 1;
   1.316 +				if (index < 0)
   1.317 +				{
   1.318 +					index = g_SpawnCount - 1;
   1.319 +				}
   1.320 +				TeleportEntity(param1, g_SpawnOrigins[index], g_SpawnAngles[index], NULL_VECTOR);
   1.321 +				PrintToChat(param1, "[CSSDM] Teleported to spawn #%d (%d total).", index, g_SpawnCount);
   1.322 +				g_LastLocation[param1] = index;
   1.323 +			}
   1.324 +		} else if (param2 == 5) {
   1.325 +			new index = GetNearestSpawn(param1);
   1.326 +			if (index == -1)
   1.327 +			{
   1.328 +				PrintToChat(param1, "[CSSDM] There are no spawn points.");
   1.329 +			} else {
   1.330 +				if (!DeleteSpawn(index))
   1.331 +				{
   1.332 +					PrintToChat(param1, "[CSSDM] Could not delete spawn #%d.", index);
   1.333 +				} else {
   1.334 +					if (!WriteMapConfig())
   1.335 +					{
   1.336 +						PrintToChat(param1, "[CSSDM] Could not write to spawn config file!");
   1.337 +					} else {
   1.338 +						PrintToChat(param1, "[CSSDM] Deleted spawn #%d (%d total).", index, g_SpawnCount);
   1.339 +					}
   1.340 +				}
   1.341 +			}
   1.342 +		} else if (param2 == 3) {
   1.343 +			new index;
   1.344 +			if ((index = AddSpawnFromClient(param1)) == -1)
   1.345 +			{
   1.346 +				PrintToChat(param1, "[CSSDM] Could not add spawn (max limit reached).");
   1.347 +			} else {
   1.348 +				if (!WriteMapConfig())
   1.349 +				{
   1.350 +					PrintToChat(param1, "[CSSDM] Could not write to spawn config file!");
   1.351 +				} else {
   1.352 +					PrintToChat(param1, "[CSSDM] Added spawn #%d (%d total).", index, g_SpawnCount);
   1.353 +				}
   1.354 +			}
   1.355 +		} else if (param2 == 4) {
   1.356 +			new index = g_LastLocation[param1];
   1.357 +			new bool:pre = true;
   1.358 +			if (index == -1 || index >= g_SpawnCount)
   1.359 +			{
   1.360 +				index = g_SpawnCount - 1;
   1.361 +				pre = false;
   1.362 +			}
   1.363 +			if ((index = InsertSpawnFromClient(param1, pre, index)) == -1)
   1.364 +			{
   1.365 +				PrintToChat(param1, "[CSSDM] Could not add spawn (max limit reached).");
   1.366 +			} else {
   1.367 +				if (!WriteMapConfig())
   1.368 +				{
   1.369 +					PrintToChat(param1, "[CSSDM] Could not write to spawn config file!");
   1.370 +				} else {
   1.371 +					PrintToChat(param1, "[CSSDM] Inserted spawn at #%d (%d total).", index, g_SpawnCount);
   1.372 +				}
   1.373 +			}
   1.374 +		} else if (param2 == 6) {
   1.375 +			/* Of course, we ask the user first. */
   1.376 +			new Handle:panel = CreatePanel();
   1.377 +			SetPanelTitle(panel, "Delete all spawn points?");
   1.378 +			DrawPanelItem(panel, "Yes");
   1.379 +			DrawPanelItem(panel, "No");
   1.380 +			SendPanelToClient(panel, param1, Panel_VerifyDeleteSpawns, MENU_TIME_FOREVER);
   1.381 +			CloseHandle(panel);
   1.382 +			return;
   1.383 +		}
   1.384 +		/* Redraw the menu */
   1.385 +		DisplayMenu(g_hSpawnMenu, param1, MENU_TIME_FOREVER);	
   1.386 +	}
   1.387 +}
   1.388 +
   1.389 +public DM_OnStartup()
   1.390 +{
   1.391 +	new String:method[32];
   1.392 +	DM_GetSpawnMethod(method, sizeof(method));
   1.393 +	
   1.394 +	if (strcmp(method, "preset") != 0)
   1.395 +	{
   1.396 +		return;
   1.397 +	}
   1.398 +	
   1.399 +	g_AreWeSpawning = LoadMapConfig();
   1.400 +}
   1.401 +
   1.402 +public Action:DM_OnSetSpawnMethod(const String:method[])
   1.403 +{
   1.404 +	g_AreWeSpawning = (strcmp(method, "preset") == 0);
   1.405 +	
   1.406 +	if (!g_AreWeSpawning)
   1.407 +	{
   1.408 +		return Plugin_Continue;
   1.409 +	}
   1.410 +	
   1.411 +	if (!LoadMapConfig())
   1.412 +	{
   1.413 +		g_AreWeSpawning = false;
   1.414 +		return Plugin_Continue;
   1.415 +	}
   1.416 +	
   1.417 +	return Plugin_Stop;
   1.418 +}
   1.419 +
   1.420 +public DM_OnClientSpawned(client)
   1.421 +{
   1.422 +	if (!g_AreWeSpawning || !g_SpawnCount)
   1.423 +	{
   1.424 +		return;
   1.425 +	}
   1.426 +	
   1.427 +	new maxClients = GetMaxClients();
   1.428 +	new startPoint = GetRandomInt(0, g_SpawnCount-1);
   1.429 +	
   1.430 +	/* Prefetch player origins */
   1.431 +	decl Float:origins[65][3];
   1.432 +	new numToCheck = 0;
   1.433 +	
   1.434 +	for (new i=1; i<=maxClients; i++)
   1.435 +	{
   1.436 +		if (i == client || !IsClientInGame(i))
   1.437 +		{
   1.438 +			continue;
   1.439 +		}
   1.440 +		GetClientAbsOrigin(i, origins[numToCheck]);
   1.441 +		numToCheck++;
   1.442 +	}
   1.443 +	
   1.444 +	/* Cycle through until we get a spawn point */
   1.445 +	new bool:use_this_point;
   1.446 +	new checked = 0;
   1.447 +	while (checked < g_SpawnCount)
   1.448 +	{
   1.449 +		if (startPoint >= g_SpawnCount)
   1.450 +		{
   1.451 +			startPoint = 0;
   1.452 +		}
   1.453 +		
   1.454 +		use_this_point = true;
   1.455 +		for (new i=0; i<numToCheck; i++)
   1.456 +		{
   1.457 +			if (GetDistance(g_SpawnOrigins[startPoint], origins[i]) < 600.0)
   1.458 +			{
   1.459 +				use_this_point = false;
   1.460 +				break;
   1.461 +			}
   1.462 +		}
   1.463 +		
   1.464 +		if (use_this_point)
   1.465 +		{
   1.466 +			break;
   1.467 +		}
   1.468 +		
   1.469 +		checked++;
   1.470 +		startPoint++;
   1.471 +	}
   1.472 +	
   1.473 +	if (startPoint >= g_SpawnCount)
   1.474 +	{
   1.475 +		startPoint = 0;
   1.476 +	}
   1.477 +		
   1.478 +	TeleportEntity(client, g_SpawnOrigins[startPoint], g_SpawnAngles[startPoint], NULL_VECTOR);
   1.479 +}