Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <amxmodx>
- #include <amxmisc>
- #define PLUGIN "VoteMap"
- #define VERSION "1.2"
- #define AUTHOR "CheezPuff"
- #define VOTE_TIME 30 // Voting duration in seconds
- #define MAP_CHANGE_DELAY 5.0 // Delay after round end to change map
- #define MAPS_TO_SHOW 4 // Number of random maps to show in vote
- #define AUTO_VOTE_TIME 1500.0 // 25 minutes in seconds
- new g_mapList[64][32]
- new g_mapCount
- new g_voteTime
- new g_mapVotes[MAPS_TO_SHOW]
- new g_voteMaps[MAPS_TO_SHOW][32]
- new g_votedPlayers[33]
- new Float:g_nextVoteTime
- new bool:g_debugMode
- new g_voteMenuId
- public plugin_init() {
- register_plugin(PLUGIN, VERSION, AUTHOR)
- register_clcmd("say rtv", "CmdRockTheVote")
- register_clcmd("say /nextmap", "CmdNextMap")
- register_concmd("amx_startvote", "CmdStartVote", ADMIN_MAP, "- Starts a map vote")
- g_voteMenuId = register_menuid("VoteMap")
- register_menucmd(g_voteMenuId, MENU_KEY_1|MENU_KEY_2|MENU_KEY_3|MENU_KEY_4, "HandleVote")
- register_cvar("amx_votemap_debug", "0")
- LoadMapList()
- g_nextVoteTime = get_gametime() + AUTO_VOTE_TIME
- set_task(AUTO_VOTE_TIME, "StartVoteMap", .flags = "b")
- }
- LoadMapList() {
- new mapFile[64]
- get_configsdir(mapFile, charsmax(mapFile))
- add(mapFile, charsmax(mapFile), "/maps.ini")
- new file = fopen(mapFile, "rt")
- if (!file) {
- log_amx("Error: Cannot open maps file %s", mapFile)
- return
- }
- while (!feof(file) && g_mapCount < sizeof(g_mapList)) {
- fgets(file, g_mapList[g_mapCount], charsmax(g_mapList[]))
- trim(g_mapList[g_mapCount])
- if (g_mapList[g_mapCount][0]) g_mapCount++
- }
- fclose(file)
- DebugPrint("Loaded %d maps", g_mapCount)
- }
- public StartVoteMap() {
- UpdateDebugMode()
- DebugPrint("StartVoteMap called")
- if (g_voteTime > 0) {
- DebugPrint("Vote already in progress, aborting")
- return
- }
- g_voteTime = VOTE_TIME
- arrayset(g_mapVotes, 0, sizeof(g_mapVotes))
- arrayset(g_votedPlayers, 0, sizeof(g_votedPlayers))
- // Select random maps
- for (new i = 0; i < MAPS_TO_SHOW; i++) {
- new mapIndex
- do {
- mapIndex = random(g_mapCount)
- } while (IsMapInVoteList(g_mapList[mapIndex]))
- copy(g_voteMaps[i], charsmax(g_voteMaps[]), g_mapList[mapIndex])
- }
- ShowVoteMenu()
- set_task(1.0, "VoteTimer", .flags = "b")
- // Set next vote time
- g_nextVoteTime = get_gametime() + AUTO_VOTE_TIME
- DebugPrint("Next vote time set to: %.2f", g_nextVoteTime)
- }
- ShowVoteMenu() {
- new menu[512], len
- len = formatex(menu, charsmax(menu), "\rVoteMap - Choose a map:^n")
- for (new i = 0; i < MAPS_TO_SHOW; i++) {
- len += formatex(menu[len], charsmax(menu) - len, "^n\w%d. %s", i + 1, g_voteMaps[i])
- }
- show_menu(0, MENU_KEY_1|MENU_KEY_2|MENU_KEY_3|MENU_KEY_4, menu, g_voteTime, "VoteMap")
- DebugPrint("Vote menu shown to all players")
- }
- public VoteTimer() {
- if (--g_voteTime <= 0) {
- EndVote()
- remove_task()
- }
- }
- public client_putinserver(id) {
- g_votedPlayers[id] = 0
- }
- public HandleVote(id, key) {
- UpdateDebugMode()
- if (g_voteTime <= 0) {
- DebugPrint("Vote time expired, rejecting vote from player %d", id)
- return PLUGIN_HANDLED
- }
- if (g_votedPlayers[id]) {
- DebugPrint("Player %d already voted, rejecting vote", id)
- client_print(id, print_chat, "You have already voted.")
- return PLUGIN_HANDLED
- }
- g_votedPlayers[id] = 1
- g_mapVotes[key]++
- new name[32]
- get_user_name(id, name, charsmax(name))
- DebugPrint("Player %s voted for map %s", name, g_voteMaps[key])
- client_print(id, print_chat, "You voted for %s", g_voteMaps[key])
- return PLUGIN_HANDLED
- }
- EndVote() {
- UpdateDebugMode()
- DebugPrint("EndVote called")
- new winner = 0
- new maxVotes = g_mapVotes[0]
- for (new i = 1; i < MAPS_TO_SHOW; i++) {
- if (g_mapVotes[i] > maxVotes) {
- winner = i
- maxVotes = g_mapVotes[i]
- }
- }
- DebugPrint("Winning map: %s with %d votes", g_voteMaps[winner], maxVotes)
- client_print(0, print_chat, "Vote ended. Changing to %s after this round.", g_voteMaps[winner])
- set_task(MAP_CHANGE_DELAY, "ChangeMap", winner)
- }
- public ChangeMap(winner) {
- UpdateDebugMode()
- new winningMap[32]
- copy(winningMap, charsmax(winningMap), g_voteMaps[winner])
- DebugPrint("ChangeMap called. Winning map: %s", winningMap)
- // Schedule map change for next round
- set_cvar_string("amx_nextmap", winningMap)
- set_task(1.0, "CheckRoundEnd", _, _, _, "b")
- }
- public CheckRoundEnd() {
- UpdateDebugMode()
- if (get_timeleft() < 1) {
- new nextMap[32]
- get_cvar_string("amx_nextmap", nextMap, charsmax(nextMap))
- DebugPrint("Round ended. Changing map to: %s", nextMap)
- server_cmd("changelevel %s", nextMap)
- remove_task()
- }
- }
- IsMapInVoteList(const map[]) {
- for (new i = 0; i < MAPS_TO_SHOW; i++) {
- if (equal(map, g_voteMaps[i])) return true
- }
- return false
- }
- public CmdRockTheVote(id) {
- UpdateDebugMode()
- DebugPrint("CmdRockTheVote called by player %d", id)
- if (g_voteTime > 0) {
- client_print(id, print_chat, "A vote is already in progress.")
- return PLUGIN_HANDLED
- }
- StartVoteMap()
- return PLUGIN_HANDLED
- }
- public CmdStartVote(id, level, cid) {
- UpdateDebugMode()
- DebugPrint("CmdStartVote called by admin %d", id)
- if (!cmd_access(id, level, cid, 1)) {
- DebugPrint("Admin %d does not have access to start vote", id)
- return PLUGIN_HANDLED
- }
- if (g_voteTime > 0) {
- console_print(id, "A vote is already in progress.")
- return PLUGIN_HANDLED
- }
- StartVoteMap()
- console_print(id, "Map vote started manually.")
- return PLUGIN_HANDLED
- }
- public CmdNextMap(id) {
- UpdateDebugMode()
- new Float:timeLeft = g_nextVoteTime - get_gametime()
- new minutes = floatround(timeLeft / 60.0, floatround_floor)
- new seconds = floatround(timeLeft - minutes * 60.0)
- DebugPrint("CmdNextMap called by player %d. Next vote in %d:%02d", id, minutes, seconds)
- client_print(id, print_chat, "Next map vote will start in %d minutes and %d seconds.", minutes, seconds)
- return PLUGIN_HANDLED
- }
- UpdateDebugMode() {
- g_debugMode = get_cvar_num("amx_votemap_debug") ? true : false
- }
- DebugPrint(const format[], any:...) {
- if (!g_debugMode) return
- static message[192]
- vformat(message, charsmax(message), format, 2)
- log_amx("VoteMap Debug: %s", message)
- server_print("VoteMap Debug: %s", message)
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement