/addons/sourcemod/scripting/nextmap.sp

https://bitbucket.org/kimoto/sushi · Unknown · 228 lines · 188 code · 40 blank · 0 comment · 0 complexity · c688644a574573a1667434a632aa1e1e MD5 · raw file

  1. /**
  2. * vim: set ts=4 :
  3. * =============================================================================
  4. * SourceMod Nextmap Plugin
  5. * Adds sm_nextmap cvar for changing map and nextmap chat trigger.
  6. *
  7. * SourceMod (C)2004-2008 AlliedModders LLC. All rights reserved.
  8. * =============================================================================
  9. *
  10. * This program is free software; you can redistribute it and/or modify it under
  11. * the terms of the GNU General Public License, version 3.0, as published by the
  12. * Free Software Foundation.
  13. *
  14. * This program is distributed in the hope that it will be useful, but WITHOUT
  15. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  16. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  17. * details.
  18. *
  19. * You should have received a copy of the GNU General Public License along with
  20. * this program. If not, see <http://www.gnu.org/licenses/>.
  21. *
  22. * As a special exception, AlliedModders LLC gives you permission to link the
  23. * code of this program (as well as its derivative works) to "Half-Life 2," the
  24. * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
  25. * by the Valve Corporation. You must obey the GNU General Public License in
  26. * all respects for all other code used. Additionally, AlliedModders LLC grants
  27. * this exception to all derivative works. AlliedModders LLC defines further
  28. * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
  29. * or <http://www.sourcemod.net/license.php>.
  30. *
  31. * Version: $Id$
  32. */
  33. #pragma semicolon 1
  34. #include <sourcemod>
  35. #include "include/nextmap.inc"
  36. public Plugin:myinfo =
  37. {
  38. name = "Nextmap",
  39. author = "AlliedModders LLC",
  40. description = "Provides nextmap and sm_nextmap",
  41. version = SOURCEMOD_VERSION,
  42. url = "http://www.sourcemod.net/"
  43. };
  44. new g_MapPos = -1;
  45. new Handle:g_MapList = INVALID_HANDLE;
  46. new g_MapListSerial = -1;
  47. new g_CurrentMapStartTime;
  48. public APLRes:AskPluginLoad2(Handle:myself, bool:late, String:error[], err_max)
  49. {
  50. decl String:game[128];
  51. GetGameFolderName(game, sizeof(game));
  52. if (StrEqual(game, "left4dead", false)
  53. || StrEqual(game, "dystopia", false)
  54. || StrEqual(game, "synergy", false)
  55. || StrEqual(game, "left4dead2", false)
  56. || StrEqual(game, "garrysmod", false)
  57. || StrEqual(game, "swarm", false))
  58. {
  59. strcopy(error, err_max, "Nextmap is incompatible with this game");
  60. return APLRes_SilentFailure;
  61. }
  62. return APLRes_Success;
  63. }
  64. public OnPluginStart()
  65. {
  66. LoadTranslations("common.phrases");
  67. LoadTranslations("nextmap.phrases");
  68. g_MapList = CreateArray(32);
  69. RegAdminCmd("sm_maphistory", Command_MapHistory, ADMFLAG_CHANGEMAP, "Shows the most recent maps played");
  70. RegConsoleCmd("listmaps", Command_List);
  71. // Set to the current map so OnMapStart() will know what to do
  72. decl String:currentMap[64];
  73. GetCurrentMap(currentMap, 64);
  74. SetNextMap(currentMap);
  75. }
  76. public OnMapStart()
  77. {
  78. g_CurrentMapStartTime = GetTime();
  79. }
  80. public OnConfigsExecuted()
  81. {
  82. decl String:lastMap[64], String:currentMap[64];
  83. GetNextMap(lastMap, sizeof(lastMap));
  84. GetCurrentMap(currentMap, 64);
  85. // Why am I doing this? If we switched to a new map, but it wasn't what we expected (Due to sm_map, sm_votemap, or
  86. // some other plugin/command), we don't want to scramble the map cycle. Or for example, admin switches to a custom map
  87. // not in mapcyclefile. So we keep it set to the last expected nextmap. - ferret
  88. if (strcmp(lastMap, currentMap) == 0)
  89. {
  90. FindAndSetNextMap();
  91. }
  92. }
  93. public Action:Command_List(client, args)
  94. {
  95. PrintToConsole(client, "Map Cycle:");
  96. new mapCount = GetArraySize(g_MapList);
  97. decl String:mapName[32];
  98. for (new i = 0; i < mapCount; i++)
  99. {
  100. GetArrayString(g_MapList, i, mapName, sizeof(mapName));
  101. PrintToConsole(client, "%s", mapName);
  102. }
  103. return Plugin_Handled;
  104. }
  105. FindAndSetNextMap()
  106. {
  107. if (ReadMapList(g_MapList,
  108. g_MapListSerial,
  109. "mapcyclefile",
  110. MAPLIST_FLAG_CLEARARRAY|MAPLIST_FLAG_NO_DEFAULT)
  111. == INVALID_HANDLE)
  112. {
  113. if (g_MapListSerial == -1)
  114. {
  115. LogError("FATAL: Cannot load map cycle. Nextmap not loaded.");
  116. SetFailState("Mapcycle Not Found");
  117. }
  118. }
  119. new mapCount = GetArraySize(g_MapList);
  120. decl String:mapName[32];
  121. if (g_MapPos == -1)
  122. {
  123. decl String:current[64];
  124. GetCurrentMap(current, 64);
  125. for (new i = 0; i < mapCount; i++)
  126. {
  127. GetArrayString(g_MapList, i, mapName, sizeof(mapName));
  128. if (strcmp(current, mapName, false) == 0)
  129. {
  130. g_MapPos = i;
  131. break;
  132. }
  133. }
  134. if (g_MapPos == -1)
  135. g_MapPos = 0;
  136. }
  137. g_MapPos++;
  138. if (g_MapPos >= mapCount)
  139. g_MapPos = 0;
  140. GetArrayString(g_MapList, g_MapPos, mapName, sizeof(mapName));
  141. SetNextMap(mapName);
  142. }
  143. public Action:Command_MapHistory(client, args)
  144. {
  145. new mapCount = GetMapHistorySize();
  146. decl String:mapName[32];
  147. decl String:changeReason[100];
  148. decl String:timeString[100];
  149. decl String:playedTime[100];
  150. new startTime;
  151. new lastMapStartTime = g_CurrentMapStartTime;
  152. PrintToConsole(client, "Map History:\n");
  153. PrintToConsole(client, "Map : Started : Played Time : Reason for ending");
  154. GetCurrentMap(mapName, sizeof(mapName));
  155. PrintToConsole(client, "%02i. %s (Current Map)", 0, mapName);
  156. for (new i=0; i<mapCount; i++)
  157. {
  158. GetMapHistory(i, mapName, sizeof(mapName), changeReason, sizeof(changeReason), startTime);
  159. FormatTimeDuration(timeString, sizeof(timeString), GetTime() - startTime);
  160. FormatTimeDuration(playedTime, sizeof(playedTime), lastMapStartTime - startTime);
  161. PrintToConsole(client, "%02i. %s : %s ago : %s : %s", i+1, mapName, timeString, playedTime, changeReason);
  162. lastMapStartTime = startTime;
  163. }
  164. return Plugin_Handled;
  165. }
  166. FormatTimeDuration(String:buffer[], maxlen, time)
  167. {
  168. new days = time / 86400;
  169. new hours = (time / 3600) % 24;
  170. new minutes = (time / 60) % 60;
  171. new seconds = time % 60;
  172. if (days > 0)
  173. {
  174. return Format(buffer, maxlen, "%id %ih %im", days, hours, (seconds >= 30) ? minutes+1 : minutes);
  175. }
  176. else if (hours > 0)
  177. {
  178. return Format(buffer, maxlen, "%ih %im", hours, (seconds >= 30) ? minutes+1 : minutes);
  179. }
  180. else if (minutes > 0)
  181. {
  182. return Format(buffer, maxlen, "%im", (seconds >= 30) ? minutes+1 : minutes);
  183. }
  184. else
  185. {
  186. return Format(buffer, maxlen, "%is", seconds);
  187. }
  188. }