scripting/dm_preset_spawns.sp
author David Anderson <dvander@alliedmods.net>
Wed Nov 14 07:46:34 2007 +0000 (2007-11-14)
changeset 39 009784b8e659
parent 17 c9af6f9abbd4
child 92 52cc55d67b2f
permissions -rw-r--r--
added amb874 - exit now lets you exclude gun menus and jump to the next
     1 /**
     2  * dm_preset_spawns.sp
     3  * Adds preset spawning to CS:S DM.
     4  * This file is part of CS:S DM, Copyright (C) 2005-2007 AlliedModders LLC
     5  *
     6  * This program is free software; you can redistribute it and/or
     7  * modify it under the terms of the GNU General Public License
     8  * as published by the Free Software Foundation; either version 2
     9  * of the License, or (at your option) any later version.
    10  *
    11  * This program is distributed in the hope that it will be useful,
    12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    14  * GNU General Public License for more details.
    15  * 
    16  * You should have received a copy of the GNU General Public License
    17  * along with this program; if not, write to the Free Software
    18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
    19  *
    20  * Version: $Id$
    21  */
    22 
    23 #pragma semicolon 1
    24 #pragma dynamic 16384
    25 #include <sourcemod>
    26 #include <sdktools>
    27 #include <cssdm>
    28 
    29 public Plugin:myinfo = 
    30 {
    31 	name = "CS:S DM Preset Spawns",
    32 	author = "AlliedModders LLC",
    33 	description = "Adds preset spawning to CS:S DM",
    34 	version = CSSDM_VERSION,
    35 	url = "http://www.bailopan.net/cssdm/"
    36 };
    37 
    38 #define MAX_SPAWNS			256
    39 
    40 
    41 new bool:g_AreWeSpawning = false;
    42 new g_SpawnCount = 0;
    43 new Float:g_SpawnOrigins[MAX_SPAWNS][3];
    44 new Float:g_SpawnAngles[MAX_SPAWNS][3];
    45 new Handle:g_hSpawnMenu = INVALID_HANDLE;
    46 new g_LastLocation[MAXPLAYERS+1];
    47 
    48 public OnPluginStart()
    49 {
    50 	RegAdminCmd("cssdm_spawn_menu", Command_SpawnMenu, ADMFLAG_CHANGEMAP, "Edits CS:S DM spawn points");
    51 	
    52 	g_hSpawnMenu = CreateMenu(Menu_EditSpawns);
    53 	SetMenuTitle(g_hSpawnMenu, "Spawn Point Editor");
    54 	AddMenuItem(g_hSpawnMenu, "nearest", "Teleport to nearest");
    55 	AddMenuItem(g_hSpawnMenu, "previous", "Teleport to previous");
    56 	AddMenuItem(g_hSpawnMenu, "next", "Teleport to next");
    57 	AddMenuItem(g_hSpawnMenu, "add", "Add position");
    58 	AddMenuItem(g_hSpawnMenu, "preinsert", "Insert position here");
    59 	AddMenuItem(g_hSpawnMenu, "delete", "Delete nearest");
    60 	AddMenuItem(g_hSpawnMenu, "clear", "Delete all");
    61 }
    62 
    63 public OnClientPutInServer(client)
    64 {
    65 	g_LastLocation[client] = -1;
    66 }
    67 
    68 /* :TODO: we need this in core */
    69 Float:GetDistance(const Float:vec1[3], const Float:vec2[3])
    70 {
    71 	decl Float:x, Float:y, Float:z;
    72 	
    73 	x = vec1[0] - vec2[0];
    74 	y = vec1[1] - vec2[1];
    75 	z = vec1[2] - vec2[2];
    76 	
    77 	return SquareRoot(x*x + y*y + z*z);
    78 }
    79 
    80 GetNearestSpawn(client)
    81 {
    82 	if (!g_SpawnCount)
    83 	{
    84 		return -1;
    85 	}
    86 	
    87 	new Float:clorigin[3];
    88 	GetClientAbsOrigin(client, clorigin);
    89 	
    90 	new Float:low_diff = GetDistance(g_SpawnOrigins[0], clorigin);
    91 	new low_index = 0;
    92 	for (new i=1; i<g_SpawnCount; i++)
    93 	{
    94 		new Float:diff = GetDistance(g_SpawnOrigins[i], clorigin);
    95 		if (diff < low_diff)
    96 		{
    97 			low_diff = diff;
    98 			low_index = i;
    99 		}
   100 	}
   101 	
   102 	return low_index;
   103 }
   104 
   105 bool:LoadMapConfig()
   106 {
   107 	new String:map[64];
   108 	GetCurrentMap(map, sizeof(map));
   109 	
   110 	new String:path[255];
   111 	Format(path, sizeof(path), "cfg/cssdm/spawns/%s.txt", map);
   112 	
   113 	g_SpawnCount = 0;
   114 	
   115 	new Handle:file = OpenFile(path, "rt");
   116 	if (file == INVALID_HANDLE)
   117 	{
   118 		LogError("Could not find spawn point file \"%s\"", path);
   119 		LogError("Defaulting to map-based spawns!");
   120 		return false;
   121 	}
   122 	
   123 	new String:buffer[255];
   124 	new String:parts[6][16];
   125 	new partCount;
   126 	while (!IsEndOfFile(file) && ReadFileLine(file, buffer, sizeof(buffer)))
   127 	{
   128 		TrimString(buffer);
   129 		partCount = ExplodeString(buffer, " ", parts, 6, 16);
   130 		if (partCount < 6)
   131 		{
   132 			continue;
   133 		}
   134 		g_SpawnOrigins[g_SpawnCount][0] = StringToFloat(parts[0]);
   135 		g_SpawnOrigins[g_SpawnCount][1] = StringToFloat(parts[1]);
   136 		g_SpawnOrigins[g_SpawnCount][2] = StringToFloat(parts[2]);
   137 		g_SpawnAngles[g_SpawnCount][0] = StringToFloat(parts[3]);
   138 		g_SpawnAngles[g_SpawnCount][1] = StringToFloat(parts[4]);
   139 		g_SpawnAngles[g_SpawnCount][2] = StringToFloat(parts[5]);
   140 		g_SpawnCount++;
   141 	}
   142 	
   143 	CloseHandle(file);
   144 	
   145 	LogMessage("Preset spawn points loaded (number %d) (map %s)", g_SpawnCount, map);
   146 	
   147 	return true;
   148 }
   149 
   150 bool:WriteMapConfig()
   151 {
   152 	new String:map[64];
   153 	GetCurrentMap(map, sizeof(map));
   154 	
   155 	new String:path[255];
   156 	Format(path, sizeof(path), "cfg/cssdm/spawns/%s.txt", map);
   157 	
   158 	new Handle:file = OpenFile(path, "wt");
   159 	if (file == INVALID_HANDLE)
   160 	{
   161 		LogError("Could not open spawn point file \"%s\" for writing.", path);
   162 		return false;
   163 	}
   164 	
   165 	for (new i=0; i<g_SpawnCount; i++)
   166 	{
   167 		WriteFileLine(file, "%f %f %f %f %f %f", 
   168 			g_SpawnOrigins[i][0],
   169 			g_SpawnOrigins[i][1],
   170 			g_SpawnOrigins[i][2],
   171 			g_SpawnAngles[i][0],
   172 			g_SpawnAngles[i][1],
   173 			g_SpawnAngles[i][2]);
   174 	}
   175 	
   176 	CloseHandle(file);
   177 	
   178 	return true;
   179 }
   180 
   181 AddSpawnFromClient(client)
   182 {
   183 	if (g_SpawnCount >= MAX_SPAWNS)
   184 	{
   185 		return -1;
   186 	}
   187 	
   188 	GetClientAbsOrigin(client, g_SpawnOrigins[g_SpawnCount]);
   189 	GetClientAbsAngles(client, g_SpawnAngles[g_SpawnCount]);
   190 	
   191 	new old = g_SpawnCount++;
   192 	
   193 	return old;
   194 }
   195 
   196 InsertSpawnFromClient(client, bool:pre, index)
   197 {
   198 	if (index == g_SpawnCount - 1 && !pre)
   199 	{
   200 		return AddSpawnFromClient(client);
   201 	}
   202 	
   203 	if (g_SpawnCount >= MAX_SPAWNS)
   204 	{
   205 		return -1;
   206 	}
   207 	
   208 	/* If this is a post-insertion, unmark the index for moving */
   209 	if (!pre)
   210 	{
   211 		index++;
   212 	}
   213 	
   214 	/* Move all of the slots down */
   215 	for (new i=g_SpawnCount-1; i>=index; i--)
   216 	{
   217 		g_SpawnOrigins[i+1] = g_SpawnOrigins[i];
   218 		g_SpawnAngles[i+1] = g_SpawnAngles[i];
   219 	}
   220 	
   221 	GetClientAbsOrigin(client, g_SpawnOrigins[index]);
   222 	GetClientAbsAngles(client, g_SpawnAngles[index]);
   223 	
   224 	g_SpawnCount++;
   225 	
   226 	return index;
   227 }
   228 
   229 bool:DeleteSpawn(index)
   230 {
   231 	if (index < 0 || index >= g_SpawnCount)
   232 	{
   233 		return false;
   234 	}
   235 	
   236 	for (new i=index; i<g_SpawnCount-1; i++)
   237 	{
   238 		g_SpawnAngles[i] = g_SpawnAngles[i+1];
   239 		g_SpawnOrigins[i] = g_SpawnOrigins[i+1];
   240 	}
   241 	
   242 	g_SpawnCount--;
   243 	
   244 	return true;
   245 }
   246 
   247 public Action:Command_SpawnMenu(client, args)
   248 {
   249 	if (client == 0)
   250 	{
   251 		ReplyToCommand(client, "[CSSDM] This command is not available from the server console.");
   252 		return Plugin_Handled;
   253 	}
   254 	
   255 	DisplayMenu(g_hSpawnMenu, client, MENU_TIME_FOREVER);
   256 	
   257 	return Plugin_Handled;
   258 }
   259 
   260 public Panel_VerifyDeleteSpawns(Handle:menu, MenuAction:action, param1, param2)
   261 {
   262 	if (action == MenuAction_Select)
   263 	{
   264 		if (param2 == 1)
   265 		{
   266 			g_SpawnCount = 0;
   267 			if (!WriteMapConfig())
   268 			{
   269 				PrintToChat(param1, "[CSSDM] Could not write to spawn config file.");
   270 			} else {
   271 				PrintToChat(param1, "[CSSDM] All spawn points have been deleted.");
   272 			}
   273 		}
   274 		DisplayMenu(g_hSpawnMenu, param1, MENU_TIME_FOREVER);
   275 	}
   276 }
   277 
   278 public Menu_EditSpawns(Handle:menu, MenuAction:action, param1, param2)
   279 {
   280 	if (action == MenuAction_Select)
   281 	{
   282 		if (param2 == 0)
   283 		{
   284 			new index = GetNearestSpawn(param1);
   285 			if (index == -1)
   286 			{
   287 				PrintToChat(param1, "[CSSDM] There are no spawn points.");
   288 			} else {
   289 				TeleportEntity(param1, g_SpawnOrigins[index], g_SpawnAngles[index], NULL_VECTOR);
   290 				PrintToChat(param1, "[CSSDM] Teleported to spawn #%d (%d total).", index, g_SpawnCount);
   291 				g_LastLocation[param1] = index;
   292 			}
   293 		} else if (param2 == 2) {
   294 			if (g_SpawnCount == 0)
   295 			{
   296 				PrintToChat(param1, "[CSSDM] There are no spawn points.");
   297 			} else {
   298 				new index = g_LastLocation[param1] + 1;
   299 				if (index >= g_SpawnCount)
   300 				{
   301 					index = 0;
   302 				}
   303 				TeleportEntity(param1, g_SpawnOrigins[index], g_SpawnAngles[index], NULL_VECTOR);
   304 				PrintToChat(param1, "[CSSDM] Teleported to spawn #%d (%d total).", index, g_SpawnCount);
   305 				g_LastLocation[param1] = index;
   306 			}
   307 		} else if (param2 == 1) {
   308 			if (g_SpawnCount == 0)
   309 			{
   310 				PrintToChat(param1, "[CSSDM] There are no spawn points.");
   311 			} else {
   312 				new index = g_LastLocation[param1] - 1;
   313 				if (index < 0)
   314 				{
   315 					index = g_SpawnCount - 1;
   316 				}
   317 				TeleportEntity(param1, g_SpawnOrigins[index], g_SpawnAngles[index], NULL_VECTOR);
   318 				PrintToChat(param1, "[CSSDM] Teleported to spawn #%d (%d total).", index, g_SpawnCount);
   319 				g_LastLocation[param1] = index;
   320 			}
   321 		} else if (param2 == 5) {
   322 			new index = GetNearestSpawn(param1);
   323 			if (index == -1)
   324 			{
   325 				PrintToChat(param1, "[CSSDM] There are no spawn points.");
   326 			} else {
   327 				if (!DeleteSpawn(index))
   328 				{
   329 					PrintToChat(param1, "[CSSDM] Could not delete spawn #%d.", index);
   330 				} else {
   331 					if (!WriteMapConfig())
   332 					{
   333 						PrintToChat(param1, "[CSSDM] Could not write to spawn config file!");
   334 					} else {
   335 						PrintToChat(param1, "[CSSDM] Deleted spawn #%d (%d total).", index, g_SpawnCount);
   336 					}
   337 				}
   338 			}
   339 		} else if (param2 == 3) {
   340 			new index;
   341 			if ((index = AddSpawnFromClient(param1)) == -1)
   342 			{
   343 				PrintToChat(param1, "[CSSDM] Could not add spawn (max limit reached).");
   344 			} else {
   345 				if (!WriteMapConfig())
   346 				{
   347 					PrintToChat(param1, "[CSSDM] Could not write to spawn config file!");
   348 				} else {
   349 					PrintToChat(param1, "[CSSDM] Added spawn #%d (%d total).", index, g_SpawnCount);
   350 				}
   351 			}
   352 		} else if (param2 == 4) {
   353 			new index = g_LastLocation[param1];
   354 			new bool:pre = true;
   355 			if (index == -1 || index >= g_SpawnCount)
   356 			{
   357 				index = g_SpawnCount - 1;
   358 				pre = false;
   359 			}
   360 			if ((index = InsertSpawnFromClient(param1, pre, index)) == -1)
   361 			{
   362 				PrintToChat(param1, "[CSSDM] Could not add spawn (max limit reached).");
   363 			} else {
   364 				if (!WriteMapConfig())
   365 				{
   366 					PrintToChat(param1, "[CSSDM] Could not write to spawn config file!");
   367 				} else {
   368 					PrintToChat(param1, "[CSSDM] Inserted spawn at #%d (%d total).", index, g_SpawnCount);
   369 				}
   370 			}
   371 		} else if (param2 == 6) {
   372 			/* Of course, we ask the user first. */
   373 			new Handle:panel = CreatePanel();
   374 			SetPanelTitle(panel, "Delete all spawn points?");
   375 			DrawPanelItem(panel, "Yes");
   376 			DrawPanelItem(panel, "No");
   377 			SendPanelToClient(panel, param1, Panel_VerifyDeleteSpawns, MENU_TIME_FOREVER);
   378 			CloseHandle(panel);
   379 			return;
   380 		}
   381 		/* Redraw the menu */
   382 		DisplayMenu(g_hSpawnMenu, param1, MENU_TIME_FOREVER);	
   383 	}
   384 }
   385 
   386 public DM_OnStartup()
   387 {
   388 	new String:method[32];
   389 	DM_GetSpawnMethod(method, sizeof(method));
   390 	
   391 	if (strcmp(method, "preset") != 0)
   392 	{
   393 		return;
   394 	}
   395 	
   396 	g_AreWeSpawning = LoadMapConfig();
   397 }
   398 
   399 public Action:DM_OnSetSpawnMethod(const String:method[])
   400 {
   401 	g_AreWeSpawning = (strcmp(method, "preset") == 0);
   402 	
   403 	if (!g_AreWeSpawning)
   404 	{
   405 		return Plugin_Continue;
   406 	}
   407 	
   408 	if (!LoadMapConfig())
   409 	{
   410 		g_AreWeSpawning = false;
   411 		return Plugin_Continue;
   412 	}
   413 	
   414 	return Plugin_Stop;
   415 }
   416 
   417 public DM_OnClientSpawned(client)
   418 {
   419 	if (!g_AreWeSpawning || !g_SpawnCount)
   420 	{
   421 		return;
   422 	}
   423 	
   424 	new maxClients = GetMaxClients();
   425 	new startPoint = GetRandomInt(0, g_SpawnCount-1);
   426 	
   427 	/* Prefetch player origins */
   428 	decl Float:origins[65][3];
   429 	new numToCheck = 0;
   430 	
   431 	for (new i=1; i<=maxClients; i++)
   432 	{
   433 		if (i == client || !IsClientInGame(i))
   434 		{
   435 			continue;
   436 		}
   437 		GetClientAbsOrigin(i, origins[numToCheck]);
   438 		numToCheck++;
   439 	}
   440 	
   441 	/* Cycle through until we get a spawn point */
   442 	new bool:use_this_point;
   443 	new checked = 0;
   444 	while (checked < g_SpawnCount)
   445 	{
   446 		if (startPoint >= g_SpawnCount)
   447 		{
   448 			startPoint = 0;
   449 		}
   450 		
   451 		use_this_point = true;
   452 		for (new i=0; i<numToCheck; i++)
   453 		{
   454 			if (GetDistance(g_SpawnOrigins[startPoint], origins[i]) < 600.0)
   455 			{
   456 				use_this_point = false;
   457 				break;
   458 			}
   459 		}
   460 		
   461 		if (use_this_point)
   462 		{
   463 			break;
   464 		}
   465 		
   466 		checked++;
   467 		startPoint++;
   468 	}
   469 	
   470 	if (startPoint >= g_SpawnCount)
   471 	{
   472 		startPoint = 0;
   473 	}
   474 		
   475 	TeleportEntity(client, g_SpawnOrigins[startPoint], g_SpawnAngles[startPoint], NULL_VECTOR);
   476 }