Update code to use SourceHooks function.
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));
111 GetGameFolderName(game, sizeof(game));
113 new String:path[255];
114 Format(path, sizeof(path), "cfg/cssdm/spawns/%s/%s.txt", game, map);
118 new Handle:file = OpenFile(path, "rt");
119 if (file == INVALID_HANDLE)
121 LogError("Could not find spawn point file \"%s\"", path);
122 LogError("Defaulting to map-based spawns!");
126 new String:buffer[255];
127 new String:parts[6][16];
129 while (!IsEndOfFile(file) && ReadFileLine(file, buffer, sizeof(buffer)))
132 partCount = ExplodeString(buffer, " ", parts, 6, 16);
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]);
148 LogMessage("Preset spawn points loaded (number %d) (map %s)", g_SpawnCount, map);
153 bool:WriteMapConfig()
156 GetCurrentMap(map, sizeof(map));
159 GetGameFolderName(game, sizeof(game));
161 new String:path[255];
162 Format(path, sizeof(path), "cfg/cssdm/spawns/%s/%s.txt", game, map);
164 new Handle:file = OpenFile(path, "wt");
165 if (file == INVALID_HANDLE)
167 LogError("Could not open spawn point file \"%s\" for writing.", path);
171 for (new i=0; i<g_SpawnCount; i++)
173 WriteFileLine(file, "%f %f %f %f %f %f",
174 g_SpawnOrigins[i][0],
175 g_SpawnOrigins[i][1],
176 g_SpawnOrigins[i][2],
179 g_SpawnAngles[i][2]);
187 AddSpawnFromClient(client)
189 if (g_SpawnCount >= MAX_SPAWNS)
194 GetClientAbsOrigin(client, g_SpawnOrigins[g_SpawnCount]);
195 GetClientAbsAngles(client, g_SpawnAngles[g_SpawnCount]);
197 new old = g_SpawnCount++;
202 InsertSpawnFromClient(client, bool:pre, index)
204 if (index == g_SpawnCount - 1 && !pre)
206 return AddSpawnFromClient(client);
209 if (g_SpawnCount >= MAX_SPAWNS)
214 /* If this is a post-insertion, unmark the index for moving */
220 /* Move all of the slots down */
221 for (new i=g_SpawnCount-1; i>=index; i--)
223 g_SpawnOrigins[i+1] = g_SpawnOrigins[i];
224 g_SpawnAngles[i+1] = g_SpawnAngles[i];
227 GetClientAbsOrigin(client, g_SpawnOrigins[index]);
228 GetClientAbsAngles(client, g_SpawnAngles[index]);
235 bool:DeleteSpawn(index)
237 if (index < 0 || index >= g_SpawnCount)
242 for (new i=index; i<g_SpawnCount-1; i++)
244 g_SpawnAngles[i] = g_SpawnAngles[i+1];
245 g_SpawnOrigins[i] = g_SpawnOrigins[i+1];
253 public Action:Command_SpawnMenu(client, args)
257 ReplyToCommand(client, "[CSSDM] This command is not available from the server console.");
258 return Plugin_Handled;
261 DisplayMenu(g_hSpawnMenu, client, MENU_TIME_FOREVER);
263 return Plugin_Handled;
266 public Panel_VerifyDeleteSpawns(Handle:menu, MenuAction:action, param1, param2)
268 if (action == MenuAction_Select)
273 if (!WriteMapConfig())
275 PrintToChat(param1, "[CSSDM] Could not write to spawn config file.");
277 PrintToChat(param1, "[CSSDM] All spawn points have been deleted.");
280 DisplayMenu(g_hSpawnMenu, param1, MENU_TIME_FOREVER);
284 public Menu_EditSpawns(Handle:menu, MenuAction:action, param1, param2)
286 if (action == MenuAction_Select)
290 new index = GetNearestSpawn(param1);
293 PrintToChat(param1, "[CSSDM] There are no spawn points.");
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;
299 } else if (param2 == 2) {
300 if (g_SpawnCount == 0)
302 PrintToChat(param1, "[CSSDM] There are no spawn points.");
304 new index = g_LastLocation[param1] + 1;
305 if (index >= g_SpawnCount)
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;
313 } else if (param2 == 1) {
314 if (g_SpawnCount == 0)
316 PrintToChat(param1, "[CSSDM] There are no spawn points.");
318 new index = g_LastLocation[param1] - 1;
321 index = g_SpawnCount - 1;
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;
327 } else if (param2 == 5) {
328 new index = GetNearestSpawn(param1);
331 PrintToChat(param1, "[CSSDM] There are no spawn points.");
333 if (!DeleteSpawn(index))
335 PrintToChat(param1, "[CSSDM] Could not delete spawn #%d.", index);
337 if (!WriteMapConfig())
339 PrintToChat(param1, "[CSSDM] Could not write to spawn config file!");
341 PrintToChat(param1, "[CSSDM] Deleted spawn #%d (%d total).", index, g_SpawnCount);
345 } else if (param2 == 3) {
347 if ((index = AddSpawnFromClient(param1)) == -1)
349 PrintToChat(param1, "[CSSDM] Could not add spawn (max limit reached).");
351 if (!WriteMapConfig())
353 PrintToChat(param1, "[CSSDM] Could not write to spawn config file!");
355 PrintToChat(param1, "[CSSDM] Added spawn #%d (%d total).", index, g_SpawnCount);
358 } else if (param2 == 4) {
359 new index = g_LastLocation[param1];
361 if (index == -1 || index >= g_SpawnCount)
363 index = g_SpawnCount - 1;
366 if ((index = InsertSpawnFromClient(param1, pre, index)) == -1)
368 PrintToChat(param1, "[CSSDM] Could not add spawn (max limit reached).");
370 if (!WriteMapConfig())
372 PrintToChat(param1, "[CSSDM] Could not write to spawn config file!");
374 PrintToChat(param1, "[CSSDM] Inserted spawn at #%d (%d total).", index, g_SpawnCount);
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);
387 /* Redraw the menu */
388 DisplayMenu(g_hSpawnMenu, param1, MENU_TIME_FOREVER);
392 public DM_OnStartup()
394 new String:method[32];
395 DM_GetSpawnMethod(method, sizeof(method));
397 if (strcmp(method, "preset") != 0)
402 g_AreWeSpawning = LoadMapConfig();
405 public Action:DM_OnSetSpawnMethod(const String:method[])
407 g_AreWeSpawning = (strcmp(method, "preset") == 0);
409 if (!g_AreWeSpawning)
411 return Plugin_Continue;
414 if (!LoadMapConfig())
416 g_AreWeSpawning = false;
417 return Plugin_Continue;
423 public DM_OnClientSpawned(client)
425 if (!g_AreWeSpawning || !g_SpawnCount)
430 new maxClients = GetMaxClients();
431 new startPoint = GetRandomInt(0, g_SpawnCount-1);
433 /* Prefetch player origins */
434 decl Float:origins[65][3];
437 for (new i=1; i<=maxClients; i++)
439 if (i == client || !IsClientInGame(i))
443 GetClientAbsOrigin(i, origins[numToCheck]);
447 /* Cycle through until we get a spawn point */
448 new bool:use_this_point;
450 while (checked < g_SpawnCount)
452 if (startPoint >= g_SpawnCount)
457 use_this_point = true;
458 for (new i=0; i<numToCheck; i++)
460 if (GetDistance(g_SpawnOrigins[startPoint], origins[i]) < 600.0)
462 use_this_point = false;
476 if (startPoint >= g_SpawnCount)
481 TeleportEntity(client, g_SpawnOrigins[startPoint], g_SpawnAngles[startPoint], NULL_VECTOR);