forked from VANCOLD/fm_drive
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FM_MAPVOTE_MAPMENU.sma
248 lines (203 loc) · 5.81 KB
/
FM_MAPVOTE_MAPMENU.sma
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
#include "feckinmad/fm_global"
#include "feckinmad/fm_mapfile_api" // fm_GetMapNameByIndex(), fm_GetMapCount()
#include "feckinmad/mapvote/fm_mapvote_nominate" // fm_NominateMap()
#include "feckinmad/fm_menu"
#define MAPS_PER_PAGE 8 // Number of maps that are displayed per page in the listmaps menu. 9) More 0)Back/Cancel
new const g_sNominateLibrary[] = "fm_mapvote_nominate"
new Array:g_PlayerListMaps[MAX_PLAYERS + 1] // Search results for listmaps
new g_iPlayerMenuPos[MAX_PLAYERS + 1] // Keep track of the players current page position in the map menu
new g_iMaxPlayers
public plugin_init()
{
fm_RegisterPlugin()
register_menucmd(register_menuid("Select Map:"), ALL_MENU_KEYS, "Command_MapsMenu")
register_clcmd("say","Handle_Say")
register_clcmd("say_team","Handle_Say")
register_concmd("fm_map_menu", "Command_MapMenu")
g_iMaxPlayers = get_maxplayers()
}
public plugin_natives()
{
register_native("fm_ShowMapMenu", "Native_ShowMapMenu")
register_library("fm_mapvote_mapmenu")
set_module_filter("Module_Filter")
set_native_filter("Native_Filter")
}
public Native_ShowMapMenu()
{
new id = get_param(1)
if (id < 1 || id > g_iMaxPlayers)
{
log_error(AMX_ERR_NATIVE, "Player out of range (%d)", id)
return 0
}
if (!is_user_connected(id))
{
log_error(AMX_ERR_NATIVE, "Invalid player (%d)", id)
return 0
}
MapsMenu(id, g_iPlayerMenuPos[id] = 0)
return 1
}
public Module_Filter(sModule[])
{
if (equal(sModule, g_sNominateLibrary))
{
return PLUGIN_HANDLED // Load the plugin even if the nominate plugin is not running
}
return PLUGIN_CONTINUE
}
public Native_Filter(sName[], iIndex, iTrap)
{
if (!iTrap)
{
return PLUGIN_HANDLED
}
return PLUGIN_CONTINUE
}
public Handle_Say(id)
{
static sArgs[192]; read_args(sArgs, charsmax(sArgs))
remove_quotes(sArgs)
trim(sArgs)
if (!sArgs[0])
{
return PLUGIN_CONTINUE
}
if (equali(sArgs, "listmaps", 8))
{
if (!sArgs[8]) // No arguments supplied, so show them the full maplist
{
// Destroy this players search results so they are not shown them when they are requesting a full maplist
// Its possible it wasn't destroyed earlier if the player did not press a menu key on the listmaps menu after doing a search
if (g_PlayerListMaps[id] != Invalid_Array)
ArrayDestroy(g_PlayerListMaps[id])
MapsMenu(id, g_iPlayerMenuPos[id] = 0)
return PLUGIN_HANDLED
}
if (sArgs[8] == ' ') // Possible search argument
{
if (!sArgs[9])
{
client_print(id, print_chat, "Usage: listmaps [string] e.g. \"listmaps fm_\"")
}
else
{
// It's possible the array was never destroyed from previous use as mentioned above
// Just clear it now as its going to be re-used else create a new one
if (g_PlayerListMaps[id] != Invalid_Array)
ArrayClear(g_PlayerListMaps[id])
else
g_PlayerListMaps[id] = ArrayCreate(MAX_MAP_LEN)
// Search through maplist for matches
new sMap[MAX_MAP_LEN]
for (new i = 0, iCount = fm_GetMapCount(); i < iCount; i++)
{
fm_GetMapNameByIndex(i, sMap, charsmax(sMap))
if (containi(sMap, sArgs[9]) != -1) // Does the mapname contain the search argument
ArrayPushString(g_PlayerListMaps[id], sMap) // Actually store the whole name rather than just an index incase the map list is reloaded
}
new iSize = ArraySize(g_PlayerListMaps[id])
if (iSize > 0)
{
MapsMenu(id, g_iPlayerMenuPos[id] = 0)
}
else
client_print(id, print_chat, "* No matches for \"%s\"", sArgs[9])
}
return PLUGIN_HANDLED
}
}
return PLUGIN_CONTINUE
}
public Command_MapMenu(id)
{
if (g_PlayerListMaps[id] != Invalid_Array)
{
ArrayDestroy(g_PlayerListMaps[id])
}
MapsMenu(id, g_iPlayerMenuPos[id] = 0)
return PLUGIN_HANDLED
}
MapsMenu(id, iPos)
{
if (iPos < 0)
{
if (g_PlayerListMaps[id] != Invalid_Array)
ArrayDestroy(g_PlayerListMaps[id])
return PLUGIN_HANDLED
}
new iMapCount = g_PlayerListMaps[id] != Invalid_Array ? ArraySize(g_PlayerListMaps[id]) : fm_GetMapCount()
if (!iMapCount)
{
client_print(id, print_chat, "* There are no maps to display")
return PLUGIN_HANDLED
}
new sMenu[512], iCurrentKey, iKeys, iLen
new iStart = iPos * MAPS_PER_PAGE
new iEnd = iStart + MAPS_PER_PAGE
if(iEnd > iMapCount)
iEnd = iMapCount
iLen = formatex(sMenu, charsmax(sMenu), "Select Map: Page %d/%d\n\n", iPos + 1, fm_GetMenuPageMax(iMapCount, MAPS_PER_PAGE))
// Loop between the start and end
new sMap[MAX_MAP_LEN]
for (new i = iStart; i < iEnd; i++)
{
iKeys |= (1 << iCurrentKey++) // Map key
if (g_PlayerListMaps[id] != Invalid_Array)
ArrayGetString(g_PlayerListMaps[id], i, sMap, charsmax(sMap))
else
fm_GetMapNameByIndex(i, sMap, charsmax(sMap))
iLen += formatex(sMenu[iLen], (charsmax(sMenu) - iLen), "%d) %s\n", iCurrentKey, sMap)
}
if(iEnd != iMapCount)
{
iLen += formatex(sMenu[iLen], charsmax(sMenu) - iLen, "\n9) More")
iKeys |= (1<<8)
}
formatex(sMenu[iLen], charsmax(sMenu) - iLen, "\n0) %s", iPos ? "Back" : "Cancel")
iKeys |= (1<<9)
show_menu(id, iKeys, sMenu)
return PLUGIN_HANDLED
}
public Command_MapsMenu(id, iKey)
{
switch(iKey)
{
case 8: MapsMenu(id, ++g_iPlayerMenuPos[id])
case 9: MapsMenu(id, --g_iPlayerMenuPos[id])
default:
{
new i = g_iPlayerMenuPos[id] * MAPS_PER_PAGE + iKey
new sMap[MAX_MAP_LEN]
if (g_PlayerListMaps[id] != Invalid_Array)
{
ArrayGetString(g_PlayerListMaps[id], i, sMap, charsmax(sMap))
ArrayDestroy(g_PlayerListMaps[id])
}
else
{
fm_GetMapNameByIndex(i, sMap, charsmax(sMap))
}
if (LibraryExists(g_sNominateLibrary, LibType_Library))
{
fm_NominateMap(id, sMap)
}
else
{
client_cmd(id, "say %s", sMap)
}
}
}
fm_PlaySound(id, FM_MENU_SELECT_SOUND)
}
public plugin_end()
{
for (new i = 1; i <= MAX_PLAYERS; i++)
{
if (g_PlayerListMaps[i] != Invalid_Array)
{
ArrayDestroy(g_PlayerListMaps[i])
}
}
}