Bumped version.
3 * Adds preset spawning to CS:S DM.
4 * This file is part of CS:S DM, Copyright (C) 2005-2007 AlliedModders LLC
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.
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.
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.
29 public Plugin:myinfo =
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/"
38 #define MAX_SPAWNS 256
41 new bool:g_AreWeSpawning = false;
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];
48 public OnPluginStart()
50 RegAdminCmd("cssdm_spawn_menu", Command_SpawnMenu, ADMFLAG_CHANGEMAP, "Edits CS:S DM spawn points");
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");
63 public OnClientPutInServer(client)
65 g_LastLocation[client] = -1;
68 /* :TODO: we need this in core */
69 Float:GetDistance(const Float:vec1[3], const Float:vec2[3])
71 decl Float:x, Float:y, Float:z;
73 x = vec1[0] - vec2[0];
74 y = vec1[1] - vec2[1];
75 z = vec1[2] - vec2[2];
77 return SquareRoot(x*x + y*y + z*z);
80 GetNearestSpawn(client)
87 new Float:clorigin[3];
88 GetClientAbsOrigin(client, clorigin);
90 new Float:low_diff = GetDistance(g_SpawnOrigins[0], clorigin);
92 for (new i=1; i<g_SpawnCount; i++)
94 new Float:diff = GetDistance(g_SpawnOrigins[i], clorigin);
108 GetCurrentMap(map, sizeof(map));
110 new String:path[255];
111 Format(path, sizeof(path), "cfg/cssdm/spawns/%s.txt", map);
115 new Handle:file = OpenFile(path, "rt");
116 if (file == INVALID_HANDLE)
118 LogError("Could not find spawn point file \"%s\"", path);
119 LogError("Defaulting to map-based spawns!");
123 new String:buffer[255];
124 new String:parts[6][16];
126 while (!IsEndOfFile(file) && ReadFileLine(file, buffer, sizeof(buffer)))
129 partCount = ExplodeString(buffer, " ", parts, 6, 16);
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]);
145 LogMessage("Preset spawn points loaded (number %d) (map %s)", g_SpawnCount, map);
150 bool:WriteMapConfig()
153 GetCurrentMap(map, sizeof(map));
155 new String:path[255];
156 Format(path, sizeof(path), "cfg/cssdm/spawns/%s.txt", map);
158 new Handle:file = OpenFile(path, "wt");
159 if (file == INVALID_HANDLE)
161 LogError("Could not open spawn point file \"%s\" for writing.", path);
165 for (new i=0; i<g_SpawnCount; i++)
167 WriteFileLine(file, "%f %f %f %f %f %f",
168 g_SpawnOrigins[i][0],
169 g_SpawnOrigins[i][1],
170 g_SpawnOrigins[i][2],
173 g_SpawnAngles[i][2]);
181 AddSpawnFromClient(client)
183 if (g_SpawnCount >= MAX_SPAWNS)
188 GetClientAbsOrigin(client, g_SpawnOrigins[g_SpawnCount]);
189 GetClientAbsAngles(client, g_SpawnAngles[g_SpawnCount]);
191 new old = g_SpawnCount++;
196 InsertSpawnFromClient(client, bool:pre, index)
198 if (index == g_SpawnCount - 1 && !pre)
200 return AddSpawnFromClient(client);
203 if (g_SpawnCount >= MAX_SPAWNS)
208 /* If this is a post-insertion, unmark the index for moving */
214 /* Move all of the slots down */
215 for (new i=g_SpawnCount-1; i>=index; i--)
217 g_SpawnOrigins[i+1] = g_SpawnOrigins[i];
218 g_SpawnAngles[i+1] = g_SpawnAngles[i];
221 GetClientAbsOrigin(client, g_SpawnOrigins[index]);
222 GetClientAbsAngles(client, g_SpawnAngles[index]);
229 bool:DeleteSpawn(index)
231 if (index < 0 || index >= g_SpawnCount)
236 for (new i=index; i<g_SpawnCount-1; i++)
238 g_SpawnAngles[i] = g_SpawnAngles[i+1];
239 g_SpawnOrigins[i] = g_SpawnOrigins[i+1];
247 public Action:Command_SpawnMenu(client, args)
251 ReplyToCommand(client, "[CSSDM] This command is not available from the server console.");
252 return Plugin_Handled;
255 DisplayMenu(g_hSpawnMenu, client, MENU_TIME_FOREVER);
257 return Plugin_Handled;
260 public Panel_VerifyDeleteSpawns(Handle:menu, MenuAction:action, param1, param2)
262 if (action == MenuAction_Select)
267 if (!WriteMapConfig())
269 PrintToChat(param1, "[CSSDM] Could not write to spawn config file.");
271 PrintToChat(param1, "[CSSDM] All spawn points have been deleted.");
274 DisplayMenu(g_hSpawnMenu, param1, MENU_TIME_FOREVER);
278 public Menu_EditSpawns(Handle:menu, MenuAction:action, param1, param2)
280 if (action == MenuAction_Select)
284 new index = GetNearestSpawn(param1);
287 PrintToChat(param1, "[CSSDM] There are no spawn points.");
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;
293 } else if (param2 == 2) {
294 if (g_SpawnCount == 0)
296 PrintToChat(param1, "[CSSDM] There are no spawn points.");
298 new index = g_LastLocation[param1] + 1;
299 if (index >= g_SpawnCount)
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;
307 } else if (param2 == 1) {
308 if (g_SpawnCount == 0)
310 PrintToChat(param1, "[CSSDM] There are no spawn points.");
312 new index = g_LastLocation[param1] - 1;
315 index = g_SpawnCount - 1;
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;
321 } else if (param2 == 5) {
322 new index = GetNearestSpawn(param1);
325 PrintToChat(param1, "[CSSDM] There are no spawn points.");
327 if (!DeleteSpawn(index))
329 PrintToChat(param1, "[CSSDM] Could not delete spawn #%d.", index);
331 if (!WriteMapConfig())
333 PrintToChat(param1, "[CSSDM] Could not write to spawn config file!");
335 PrintToChat(param1, "[CSSDM] Deleted spawn #%d (%d total).", index, g_SpawnCount);
339 } else if (param2 == 3) {
341 if ((index = AddSpawnFromClient(param1)) == -1)
343 PrintToChat(param1, "[CSSDM] Could not add spawn (max limit reached).");
345 if (!WriteMapConfig())
347 PrintToChat(param1, "[CSSDM] Could not write to spawn config file!");
349 PrintToChat(param1, "[CSSDM] Added spawn #%d (%d total).", index, g_SpawnCount);
352 } else if (param2 == 4) {
353 new index = g_LastLocation[param1];
355 if (index == -1 || index >= g_SpawnCount)
357 index = g_SpawnCount - 1;
360 if ((index = InsertSpawnFromClient(param1, pre, index)) == -1)
362 PrintToChat(param1, "[CSSDM] Could not add spawn (max limit reached).");
364 if (!WriteMapConfig())
366 PrintToChat(param1, "[CSSDM] Could not write to spawn config file!");
368 PrintToChat(param1, "[CSSDM] Inserted spawn at #%d (%d total).", index, g_SpawnCount);
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);
381 /* Redraw the menu */
382 DisplayMenu(g_hSpawnMenu, param1, MENU_TIME_FOREVER);
386 public DM_OnStartup()
388 new String:method[32];
389 DM_GetSpawnMethod(method, sizeof(method));
391 if (strcmp(method, "preset") != 0)
396 g_AreWeSpawning = LoadMapConfig();
399 public Action:DM_OnSetSpawnMethod(const String:method[])
401 g_AreWeSpawning = (strcmp(method, "preset") == 0);
403 if (!g_AreWeSpawning)
405 return Plugin_Continue;
408 if (!LoadMapConfig())
410 g_AreWeSpawning = false;
411 return Plugin_Continue;
417 public DM_OnClientSpawned(client)
419 if (!g_AreWeSpawning || !g_SpawnCount)
424 new maxClients = GetMaxClients();
425 new startPoint = GetRandomInt(0, g_SpawnCount-1);
427 /* Prefetch player origins */
428 decl Float:origins[65][3];
431 for (new i=1; i<=maxClients; i++)
433 if (i == client || !IsClientInGame(i))
437 GetClientAbsOrigin(i, origins[numToCheck]);
441 /* Cycle through until we get a spawn point */
442 new bool:use_this_point;
444 while (checked < g_SpawnCount)
446 if (startPoint >= g_SpawnCount)
451 use_this_point = true;
452 for (new i=0; i<numToCheck; i++)
454 if (GetDistance(g_SpawnOrigins[startPoint], origins[i]) < 600.0)
456 use_this_point = false;
470 if (startPoint >= g_SpawnCount)
475 TeleportEntity(client, g_SpawnOrigins[startPoint], g_SpawnAngles[startPoint], NULL_VECTOR);