scripting/dm_preset_spawns.sp
author Ruben Gonzalez <drifter01620@gmail.com>
Thu Jun 12 10:18:02 2014 -0400 (2014-06-12)
changeset 183 d6abe75f1896
parent 92 52cc55d67b2f
permissions -rw-r--r--
Update CS:GO gamedata
     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:game[64];
   111 	GetGameFolderName(game, sizeof(game));
   112 	
   113 	new String:path[255];
   114 	Format(path, sizeof(path), "cfg/cssdm/spawns/%s/%s.txt", game, map);
   115 	
   116 	g_SpawnCount = 0;
   117 	
   118 	new Handle:file = OpenFile(path, "rt");
   119 	if (file == INVALID_HANDLE)
   120 	{
   121 		LogError("Could not find spawn point file \"%s\"", path);
   122 		LogError("Defaulting to map-based spawns!");
   123 		return false;
   124 	}
   125 	
   126 	new String:buffer[255];
   127 	new String:parts[6][16];
   128 	new partCount;
   129 	while (!IsEndOfFile(file) && ReadFileLine(file, buffer, sizeof(buffer)))
   130 	{
   131 		TrimString(buffer);
   132 		partCount = ExplodeString(buffer, " ", parts, 6, 16);
   133 		if (partCount < 6)
   134 		{
   135 			continue;
   136 		}
   137 		g_SpawnOrigins[g_SpawnCount][0] = StringToFloat(parts[0]);
   138 		g_SpawnOrigins[g_SpawnCount][1] = StringToFloat(parts[1]);
   139 		g_SpawnOrigins[g_SpawnCount][2] = StringToFloat(parts[2]);
   140 		g_SpawnAngles[g_SpawnCount][0] = StringToFloat(parts[3]);
   141 		g_SpawnAngles[g_SpawnCount][1] = StringToFloat(parts[4]);
   142 		g_SpawnAngles[g_SpawnCount][2] = StringToFloat(parts[5]);
   143 		g_SpawnCount++;
   144 	}
   145 	
   146 	CloseHandle(file);
   147 	
   148 	LogMessage("Preset spawn points loaded (number %d) (map %s)", g_SpawnCount, map);
   149 	
   150 	return true;
   151 }
   152 
   153 bool:WriteMapConfig()
   154 {
   155 	new String:map[64];
   156 	GetCurrentMap(map, sizeof(map));
   157 	
   158 	new String:game[64];
   159 	GetGameFolderName(game, sizeof(game));
   160 	
   161 	new String:path[255];
   162 	Format(path, sizeof(path), "cfg/cssdm/spawns/%s/%s.txt", game, map);
   163 	
   164 	new Handle:file = OpenFile(path, "wt");
   165 	if (file == INVALID_HANDLE)
   166 	{
   167 		LogError("Could not open spawn point file \"%s\" for writing.", path);
   168 		return false;
   169 	}
   170 	
   171 	for (new i=0; i<g_SpawnCount; i++)
   172 	{
   173 		WriteFileLine(file, "%f %f %f %f %f %f", 
   174 			g_SpawnOrigins[i][0],
   175 			g_SpawnOrigins[i][1],
   176 			g_SpawnOrigins[i][2],
   177 			g_SpawnAngles[i][0],
   178 			g_SpawnAngles[i][1],
   179 			g_SpawnAngles[i][2]);
   180 	}
   181 	
   182 	CloseHandle(file);
   183 	
   184 	return true;
   185 }
   186 
   187 AddSpawnFromClient(client)
   188 {
   189 	if (g_SpawnCount >= MAX_SPAWNS)
   190 	{
   191 		return -1;
   192 	}
   193 	
   194 	GetClientAbsOrigin(client, g_SpawnOrigins[g_SpawnCount]);
   195 	GetClientAbsAngles(client, g_SpawnAngles[g_SpawnCount]);
   196 	
   197 	new old = g_SpawnCount++;
   198 	
   199 	return old;
   200 }
   201 
   202 InsertSpawnFromClient(client, bool:pre, index)
   203 {
   204 	if (index == g_SpawnCount - 1 && !pre)
   205 	{
   206 		return AddSpawnFromClient(client);
   207 	}
   208 	
   209 	if (g_SpawnCount >= MAX_SPAWNS)
   210 	{
   211 		return -1;
   212 	}
   213 	
   214 	/* If this is a post-insertion, unmark the index for moving */
   215 	if (!pre)
   216 	{
   217 		index++;
   218 	}
   219 	
   220 	/* Move all of the slots down */
   221 	for (new i=g_SpawnCount-1; i>=index; i--)
   222 	{
   223 		g_SpawnOrigins[i+1] = g_SpawnOrigins[i];
   224 		g_SpawnAngles[i+1] = g_SpawnAngles[i];
   225 	}
   226 	
   227 	GetClientAbsOrigin(client, g_SpawnOrigins[index]);
   228 	GetClientAbsAngles(client, g_SpawnAngles[index]);
   229 	
   230 	g_SpawnCount++;
   231 	
   232 	return index;
   233 }
   234 
   235 bool:DeleteSpawn(index)
   236 {
   237 	if (index < 0 || index >= g_SpawnCount)
   238 	{
   239 		return false;
   240 	}
   241 	
   242 	for (new i=index; i<g_SpawnCount-1; i++)
   243 	{
   244 		g_SpawnAngles[i] = g_SpawnAngles[i+1];
   245 		g_SpawnOrigins[i] = g_SpawnOrigins[i+1];
   246 	}
   247 	
   248 	g_SpawnCount--;
   249 	
   250 	return true;
   251 }
   252 
   253 public Action:Command_SpawnMenu(client, args)
   254 {
   255 	if (client == 0)
   256 	{
   257 		ReplyToCommand(client, "[CSSDM] This command is not available from the server console.");
   258 		return Plugin_Handled;
   259 	}
   260 	
   261 	DisplayMenu(g_hSpawnMenu, client, MENU_TIME_FOREVER);
   262 	
   263 	return Plugin_Handled;
   264 }
   265 
   266 public Panel_VerifyDeleteSpawns(Handle:menu, MenuAction:action, param1, param2)
   267 {
   268 	if (action == MenuAction_Select)
   269 	{
   270 		if (param2 == 1)
   271 		{
   272 			g_SpawnCount = 0;
   273 			if (!WriteMapConfig())
   274 			{
   275 				PrintToChat(param1, "[CSSDM] Could not write to spawn config file.");
   276 			} else {
   277 				PrintToChat(param1, "[CSSDM] All spawn points have been deleted.");
   278 			}
   279 		}
   280 		DisplayMenu(g_hSpawnMenu, param1, MENU_TIME_FOREVER);
   281 	}
   282 }
   283 
   284 public Menu_EditSpawns(Handle:menu, MenuAction:action, param1, param2)
   285 {
   286 	if (action == MenuAction_Select)
   287 	{
   288 		if (param2 == 0)
   289 		{
   290 			new index = GetNearestSpawn(param1);
   291 			if (index == -1)
   292 			{
   293 				PrintToChat(param1, "[CSSDM] There are no spawn points.");
   294 			} else {
   295 				TeleportEntity(param1, g_SpawnOrigins[index], g_SpawnAngles[index], NULL_VECTOR);
   296 				PrintToChat(param1, "[CSSDM] Teleported to spawn #%d (%d total).", index, g_SpawnCount);
   297 				g_LastLocation[param1] = index;
   298 			}
   299 		} else if (param2 == 2) {
   300 			if (g_SpawnCount == 0)
   301 			{
   302 				PrintToChat(param1, "[CSSDM] There are no spawn points.");
   303 			} else {
   304 				new index = g_LastLocation[param1] + 1;
   305 				if (index >= g_SpawnCount)
   306 				{
   307 					index = 0;
   308 				}
   309 				TeleportEntity(param1, g_SpawnOrigins[index], g_SpawnAngles[index], NULL_VECTOR);
   310 				PrintToChat(param1, "[CSSDM] Teleported to spawn #%d (%d total).", index, g_SpawnCount);
   311 				g_LastLocation[param1] = index;
   312 			}
   313 		} else if (param2 == 1) {
   314 			if (g_SpawnCount == 0)
   315 			{
   316 				PrintToChat(param1, "[CSSDM] There are no spawn points.");
   317 			} else {
   318 				new index = g_LastLocation[param1] - 1;
   319 				if (index < 0)
   320 				{
   321 					index = g_SpawnCount - 1;
   322 				}
   323 				TeleportEntity(param1, g_SpawnOrigins[index], g_SpawnAngles[index], NULL_VECTOR);
   324 				PrintToChat(param1, "[CSSDM] Teleported to spawn #%d (%d total).", index, g_SpawnCount);
   325 				g_LastLocation[param1] = index;
   326 			}
   327 		} else if (param2 == 5) {
   328 			new index = GetNearestSpawn(param1);
   329 			if (index == -1)
   330 			{
   331 				PrintToChat(param1, "[CSSDM] There are no spawn points.");
   332 			} else {
   333 				if (!DeleteSpawn(index))
   334 				{
   335 					PrintToChat(param1, "[CSSDM] Could not delete spawn #%d.", index);
   336 				} else {
   337 					if (!WriteMapConfig())
   338 					{
   339 						PrintToChat(param1, "[CSSDM] Could not write to spawn config file!");
   340 					} else {
   341 						PrintToChat(param1, "[CSSDM] Deleted spawn #%d (%d total).", index, g_SpawnCount);
   342 					}
   343 				}
   344 			}
   345 		} else if (param2 == 3) {
   346 			new index;
   347 			if ((index = AddSpawnFromClient(param1)) == -1)
   348 			{
   349 				PrintToChat(param1, "[CSSDM] Could not add spawn (max limit reached).");
   350 			} else {
   351 				if (!WriteMapConfig())
   352 				{
   353 					PrintToChat(param1, "[CSSDM] Could not write to spawn config file!");
   354 				} else {
   355 					PrintToChat(param1, "[CSSDM] Added spawn #%d (%d total).", index, g_SpawnCount);
   356 				}
   357 			}
   358 		} else if (param2 == 4) {
   359 			new index = g_LastLocation[param1];
   360 			new bool:pre = true;
   361 			if (index == -1 || index >= g_SpawnCount)
   362 			{
   363 				index = g_SpawnCount - 1;
   364 				pre = false;
   365 			}
   366 			if ((index = InsertSpawnFromClient(param1, pre, index)) == -1)
   367 			{
   368 				PrintToChat(param1, "[CSSDM] Could not add spawn (max limit reached).");
   369 			} else {
   370 				if (!WriteMapConfig())
   371 				{
   372 					PrintToChat(param1, "[CSSDM] Could not write to spawn config file!");
   373 				} else {
   374 					PrintToChat(param1, "[CSSDM] Inserted spawn at #%d (%d total).", index, g_SpawnCount);
   375 				}
   376 			}
   377 		} else if (param2 == 6) {
   378 			/* Of course, we ask the user first. */
   379 			new Handle:panel = CreatePanel();
   380 			SetPanelTitle(panel, "Delete all spawn points?");
   381 			DrawPanelItem(panel, "Yes");
   382 			DrawPanelItem(panel, "No");
   383 			SendPanelToClient(panel, param1, Panel_VerifyDeleteSpawns, MENU_TIME_FOREVER);
   384 			CloseHandle(panel);
   385 			return;
   386 		}
   387 		/* Redraw the menu */
   388 		DisplayMenu(g_hSpawnMenu, param1, MENU_TIME_FOREVER);	
   389 	}
   390 }
   391 
   392 public DM_OnStartup()
   393 {
   394 	new String:method[32];
   395 	DM_GetSpawnMethod(method, sizeof(method));
   396 	
   397 	if (strcmp(method, "preset") != 0)
   398 	{
   399 		return;
   400 	}
   401 	
   402 	g_AreWeSpawning = LoadMapConfig();
   403 }
   404 
   405 public Action:DM_OnSetSpawnMethod(const String:method[])
   406 {
   407 	g_AreWeSpawning = (strcmp(method, "preset") == 0);
   408 	
   409 	if (!g_AreWeSpawning)
   410 	{
   411 		return Plugin_Continue;
   412 	}
   413 	
   414 	if (!LoadMapConfig())
   415 	{
   416 		g_AreWeSpawning = false;
   417 		return Plugin_Continue;
   418 	}
   419 	
   420 	return Plugin_Stop;
   421 }
   422 
   423 public DM_OnClientSpawned(client)
   424 {
   425 	if (!g_AreWeSpawning || !g_SpawnCount)
   426 	{
   427 		return;
   428 	}
   429 	
   430 	new maxClients = GetMaxClients();
   431 	new startPoint = GetRandomInt(0, g_SpawnCount-1);
   432 	
   433 	/* Prefetch player origins */
   434 	decl Float:origins[65][3];
   435 	new numToCheck = 0;
   436 	
   437 	for (new i=1; i<=maxClients; i++)
   438 	{
   439 		if (i == client || !IsClientInGame(i))
   440 		{
   441 			continue;
   442 		}
   443 		GetClientAbsOrigin(i, origins[numToCheck]);
   444 		numToCheck++;
   445 	}
   446 	
   447 	/* Cycle through until we get a spawn point */
   448 	new bool:use_this_point;
   449 	new checked = 0;
   450 	while (checked < g_SpawnCount)
   451 	{
   452 		if (startPoint >= g_SpawnCount)
   453 		{
   454 			startPoint = 0;
   455 		}
   456 		
   457 		use_this_point = true;
   458 		for (new i=0; i<numToCheck; i++)
   459 		{
   460 			if (GetDistance(g_SpawnOrigins[startPoint], origins[i]) < 600.0)
   461 			{
   462 				use_this_point = false;
   463 				break;
   464 			}
   465 		}
   466 		
   467 		if (use_this_point)
   468 		{
   469 			break;
   470 		}
   471 		
   472 		checked++;
   473 		startPoint++;
   474 	}
   475 	
   476 	if (startPoint >= g_SpawnCount)
   477 	{
   478 		startPoint = 0;
   479 	}
   480 		
   481 	TeleportEntity(client, g_SpawnOrigins[startPoint], g_SpawnAngles[startPoint], NULL_VECTOR);
   482 }