Advertisement
CheezPuff

[Free] Maps manager - 90% Work

Jul 14th, 2024 (edited)
437
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pawn 6.82 KB | None | 0 0
  1. #include <amxmodx>
  2. #include <amxmisc>
  3.  
  4. #define PLUGIN "VoteMap"
  5. #define VERSION "1.2"
  6. #define AUTHOR "CheezPuff"
  7.  
  8. #define VOTE_TIME 30 // Voting duration in seconds
  9. #define MAP_CHANGE_DELAY 5.0 // Delay after round end to change map
  10. #define MAPS_TO_SHOW 4 // Number of random maps to show in vote
  11. #define AUTO_VOTE_TIME 1500.0 // 25 minutes in seconds
  12.  
  13. new g_mapList[64][32]
  14. new g_mapCount
  15. new g_voteTime
  16. new g_mapVotes[MAPS_TO_SHOW]
  17. new g_voteMaps[MAPS_TO_SHOW][32]
  18. new g_votedPlayers[33]
  19. new Float:g_nextVoteTime
  20.  
  21. new bool:g_debugMode
  22. new g_voteMenuId
  23.  
  24. public plugin_init() {
  25.     register_plugin(PLUGIN, VERSION, AUTHOR)
  26.    
  27.     register_clcmd("say rtv", "CmdRockTheVote")
  28.     register_clcmd("say /nextmap", "CmdNextMap")
  29.     register_concmd("amx_startvote", "CmdStartVote", ADMIN_MAP, "- Starts a map vote")
  30.    
  31.     g_voteMenuId = register_menuid("VoteMap")
  32.     register_menucmd(g_voteMenuId, MENU_KEY_1|MENU_KEY_2|MENU_KEY_3|MENU_KEY_4, "HandleVote")
  33.    
  34.     register_cvar("amx_votemap_debug", "0")
  35.    
  36.     LoadMapList()
  37.     g_nextVoteTime = get_gametime() + AUTO_VOTE_TIME
  38.     set_task(AUTO_VOTE_TIME, "StartVoteMap", .flags = "b")
  39. }
  40.  
  41. LoadMapList() {
  42.     new mapFile[64]
  43.     get_configsdir(mapFile, charsmax(mapFile))
  44.     add(mapFile, charsmax(mapFile), "/maps.ini")
  45.    
  46.     new file = fopen(mapFile, "rt")
  47.     if (!file) {
  48.         log_amx("Error: Cannot open maps file %s", mapFile)
  49.         return
  50.     }
  51.    
  52.     while (!feof(file) && g_mapCount < sizeof(g_mapList)) {
  53.         fgets(file, g_mapList[g_mapCount], charsmax(g_mapList[]))
  54.         trim(g_mapList[g_mapCount])
  55.         if (g_mapList[g_mapCount][0]) g_mapCount++
  56.     }
  57.    
  58.     fclose(file)
  59.     DebugPrint("Loaded %d maps", g_mapCount)
  60. }
  61.  
  62. public StartVoteMap() {
  63.     UpdateDebugMode()
  64.     DebugPrint("StartVoteMap called")
  65.    
  66.     if (g_voteTime > 0) {
  67.         DebugPrint("Vote already in progress, aborting")
  68.         return
  69.     }
  70.    
  71.     g_voteTime = VOTE_TIME
  72.     arrayset(g_mapVotes, 0, sizeof(g_mapVotes))
  73.     arrayset(g_votedPlayers, 0, sizeof(g_votedPlayers))
  74.    
  75.     // Select random maps
  76.     for (new i = 0; i < MAPS_TO_SHOW; i++) {
  77.         new mapIndex
  78.         do {
  79.             mapIndex = random(g_mapCount)
  80.         } while (IsMapInVoteList(g_mapList[mapIndex]))
  81.         copy(g_voteMaps[i], charsmax(g_voteMaps[]), g_mapList[mapIndex])
  82.     }
  83.    
  84.     ShowVoteMenu()
  85.     set_task(1.0, "VoteTimer", .flags = "b")
  86.    
  87.     // Set next vote time
  88.     g_nextVoteTime = get_gametime() + AUTO_VOTE_TIME
  89.     DebugPrint("Next vote time set to: %.2f", g_nextVoteTime)
  90. }
  91.  
  92. ShowVoteMenu() {
  93.     new menu[512], len
  94.     len = formatex(menu, charsmax(menu), "\rVoteMap - Choose a map:^n")
  95.    
  96.     for (new i = 0; i < MAPS_TO_SHOW; i++) {
  97.         len += formatex(menu[len], charsmax(menu) - len, "^n\w%d. %s", i + 1, g_voteMaps[i])
  98.     }
  99.    
  100.     show_menu(0, MENU_KEY_1|MENU_KEY_2|MENU_KEY_3|MENU_KEY_4, menu, g_voteTime, "VoteMap")
  101.     DebugPrint("Vote menu shown to all players")
  102. }
  103.  
  104. public VoteTimer() {
  105.     if (--g_voteTime <= 0) {
  106.         EndVote()
  107.         remove_task()
  108.     }
  109. }
  110.  
  111. public client_putinserver(id) {
  112.     g_votedPlayers[id] = 0
  113. }
  114.  
  115. public HandleVote(id, key) {
  116.     UpdateDebugMode()
  117.    
  118.     if (g_voteTime <= 0) {
  119.         DebugPrint("Vote time expired, rejecting vote from player %d", id)
  120.         return PLUGIN_HANDLED
  121.     }
  122.    
  123.     if (g_votedPlayers[id]) {
  124.         DebugPrint("Player %d already voted, rejecting vote", id)
  125.         client_print(id, print_chat, "You have already voted.")
  126.         return PLUGIN_HANDLED
  127.     }
  128.    
  129.     g_votedPlayers[id] = 1
  130.     g_mapVotes[key]++
  131.    
  132.     new name[32]
  133.     get_user_name(id, name, charsmax(name))
  134.     DebugPrint("Player %s voted for map %s", name, g_voteMaps[key])
  135.     client_print(id, print_chat, "You voted for %s", g_voteMaps[key])
  136.    
  137.     return PLUGIN_HANDLED
  138. }
  139.  
  140. EndVote() {
  141.     UpdateDebugMode()
  142.     DebugPrint("EndVote called")
  143.    
  144.     new winner = 0
  145.     new maxVotes = g_mapVotes[0]
  146.    
  147.     for (new i = 1; i < MAPS_TO_SHOW; i++) {
  148.         if (g_mapVotes[i] > maxVotes) {
  149.             winner = i
  150.             maxVotes = g_mapVotes[i]
  151.         }
  152.     }
  153.    
  154.     DebugPrint("Winning map: %s with %d votes", g_voteMaps[winner], maxVotes)
  155.     client_print(0, print_chat, "Vote ended. Changing to %s after this round.", g_voteMaps[winner])
  156.     set_task(MAP_CHANGE_DELAY, "ChangeMap", winner)
  157. }
  158.  
  159. public ChangeMap(winner) {
  160.     UpdateDebugMode()
  161.    
  162.     new winningMap[32]
  163.     copy(winningMap, charsmax(winningMap), g_voteMaps[winner])
  164.    
  165.     DebugPrint("ChangeMap called. Winning map: %s", winningMap)
  166.    
  167.     // Schedule map change for next round
  168.     set_cvar_string("amx_nextmap", winningMap)
  169.     set_task(1.0, "CheckRoundEnd", _, _, _, "b")
  170. }
  171.  
  172. public CheckRoundEnd() {
  173.     UpdateDebugMode()
  174.    
  175.     if (get_timeleft() < 1) {
  176.         new nextMap[32]
  177.         get_cvar_string("amx_nextmap", nextMap, charsmax(nextMap))
  178.         DebugPrint("Round ended. Changing map to: %s", nextMap)
  179.         server_cmd("changelevel %s", nextMap)
  180.         remove_task()
  181.     }
  182. }
  183.  
  184. IsMapInVoteList(const map[]) {
  185.     for (new i = 0; i < MAPS_TO_SHOW; i++) {
  186.         if (equal(map, g_voteMaps[i])) return true
  187.     }
  188.     return false
  189. }
  190.  
  191. public CmdRockTheVote(id) {
  192.     UpdateDebugMode()
  193.     DebugPrint("CmdRockTheVote called by player %d", id)
  194.    
  195.     if (g_voteTime > 0) {
  196.         client_print(id, print_chat, "A vote is already in progress.")
  197.         return PLUGIN_HANDLED
  198.     }
  199.    
  200.     StartVoteMap()
  201.     return PLUGIN_HANDLED
  202. }
  203.  
  204. public CmdStartVote(id, level, cid) {
  205.     UpdateDebugMode()
  206.     DebugPrint("CmdStartVote called by admin %d", id)
  207.    
  208.     if (!cmd_access(id, level, cid, 1)) {
  209.         DebugPrint("Admin %d does not have access to start vote", id)
  210.         return PLUGIN_HANDLED
  211.     }
  212.    
  213.     if (g_voteTime > 0) {
  214.         console_print(id, "A vote is already in progress.")
  215.         return PLUGIN_HANDLED
  216.     }
  217.    
  218.     StartVoteMap()
  219.     console_print(id, "Map vote started manually.")
  220.     return PLUGIN_HANDLED
  221. }
  222.  
  223. public CmdNextMap(id) {
  224.     UpdateDebugMode()
  225.    
  226.     new Float:timeLeft = g_nextVoteTime - get_gametime()
  227.     new minutes = floatround(timeLeft / 60.0, floatround_floor)
  228.     new seconds = floatround(timeLeft - minutes * 60.0)
  229.    
  230.     DebugPrint("CmdNextMap called by player %d. Next vote in %d:%02d", id, minutes, seconds)
  231.     client_print(id, print_chat, "Next map vote will start in %d minutes and %d seconds.", minutes, seconds)
  232.     return PLUGIN_HANDLED
  233. }
  234.  
  235. UpdateDebugMode() {
  236.     g_debugMode = get_cvar_num("amx_votemap_debug") ? true : false
  237. }
  238.  
  239. DebugPrint(const format[], any:...) {
  240.     if (!g_debugMode) return
  241.    
  242.     static message[192]
  243.     vformat(message, charsmax(message), format, 2)
  244.    
  245.     log_amx("VoteMap Debug: %s", message)
  246.     server_print("VoteMap Debug: %s", message)
  247. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement