-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
ReDM_round_modes.inc
276 lines (214 loc) 路 6.38 KB
/
ReDM_round_modes.inc
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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
#include "ReDeathmatch/Modes/Menus.inc"
#include "ReDeathmatch/Modes/Vote.inc"
enum {
RoundMode_NotSet = -1, /**< Round game-mode is not set */
}
static JSON: g_arrModes = Invalid_JSON
static g_currentRoundModeIdx = RoundMode_NotSet
static g_forcedModeIdx = RoundMode_NotSet
static redm_modes_switch[32]
RoundModes_Init() {
register_dictionary("redm/modes.txt")
bind_pcvar_string(
create_cvar(
"redm_modes_switch", "sequentially",
.description = "Game mode change mode. \
`0`, `disable` - disable modes, \
`random` - random change, \
`sequentially` - sequential mode change."
),
redm_modes_switch,
charsmax(redm_modes_switch)
)
ModesMenus_Init()
ModeVote_Init()
RoundModes_ReadModes()
}
RoundModes_ReadModes() {
if (g_arrModes != Invalid_JSON)
json_free(g_arrModes)
if (!json_object_has_value(Config_GetCurrent(), "modes"))
return
g_arrModes = json_object_get_value(Config_GetCurrent(), "modes")
}
/**
* Handles mode voting and mode switching.
*/
RoundModes_RoundEnd() {
if (strcmp(redm_modes_switch, "disable") == 0)
return
new count
RoundModes_GetModesArr(count)
if (!count) {
LogMessageEx(Warning, "RoundModes_RoundEnd(): Can't load modes list from config!")
return
}
new bool: voteStarted = ModeVote_RoundEnd_Post()
if (voteStarted) {
client_print(0, print_center, "%l %l.", "Selecting", "NextGameMode")
return
}
new nextModeIdx = GetNextMode()
if (nextModeIdx == RoundMode_NotSet)
return
SetCurrentMode(nextModeIdx)
new modeName[32]
RoundModes_GetModeInfo(g_currentRoundModeIdx, modeName, charsmax(modeName))
client_print_color(0, print_team_red,
HasLangKey(modeName) ? "[Re:DM] %l ^3%l^1" : "[Re:DM] %l ^3%s^1",
"NextMode",
modeName
)
// TODO: better will be hook EndRoundMessage()
client_print(0, print_center, "%l", "ModeChanges")
}
RoundModes_RestartRound() {
if (!IsActive())
return
new votedModeIdx = ModeVote_RestartRound()
if (votedModeIdx >= 0)
SetCurrentMode(votedModeIdx)
if (g_currentRoundModeIdx == RoundMode_NotSet)
return
if (!RoundModes_GetModeInfo(g_currentRoundModeIdx))
return
ApplyMode(g_currentRoundModeIdx)
for (new p = 1; p <= MaxClients; p++) {
EquipManager_Reset(p)
}
}
RoundModes_OnSpawnEquip(const player) {
if (g_currentRoundModeIdx == RoundMode_NotSet)
return
set_task(0.3, "RoundInfoMessage", player)
}
public RoundInfoMessage(const player) {
new modeName[32]
RoundModes_GetModeInfo(g_currentRoundModeIdx, modeName, charsmax(modeName))
set_dhudmessage(
.red = 200,
.green = 200,
.blue = 200,
.y = 0.85,
.holdtime = 4.0
)
SetGlobalTransTarget(player)
show_dhudmessage(
player,
HasLangKey(modeName) ? "%l %l" : "%l %s",
"CurrentMode",
modeName
)
}
/**
* Retrieves the JSON array containing game modes from the current configuration.
*
* This function retrieves the JSON array named "modes" from the current configuration.
* It is often used to access and iterate over the game modes defined in the configuration.
*
* @param count The number of game modes in the JSON array.
*
* @return A reference to the JSON array containing game modes, or `Invalid_JSON` if not found.
*/
JSON: RoundModes_GetModesArr(& count = 0) {
if (g_arrModes == Invalid_JSON)
return Invalid_JSON
count = json_array_get_count(g_arrModes)
if (!count)
return Invalid_JSON
return g_arrModes
}
/**
* Retrieves information about a specific game mode.
*
* @param modeIdx The index of the game mode.
* @param name The name of the game mode (output).
* @param len The length of the output buffer.
*
* @return Whether the operation was successful.
*/
bool: RoundModes_GetModeInfo(const modeIdx, name[] = "", len = 0) {
new count
new JSON: arrModes = RoundModes_GetModesArr(count)
if (!count)
return false
if (modeIdx < 0 || modeIdx >= count)
return false
new JSON: objMode = json_array_get_value(arrModes, modeIdx)
json_object_get_string(objMode, "name", name, len)
json_free(objMode)
return true
}
static GetNextMode() {
new count
RoundModes_GetModesArr(count)
if (!count)
return RoundMode_NotSet
if (RoundModes_IsModeForced())
return g_forcedModeIdx
new currentIdx = g_currentRoundModeIdx
if (strcmp(redm_modes_switch, "random") == 0) {
while (currentIdx == g_currentRoundModeIdx) {
currentIdx = random_num(0, count - 1)
}
} else if (strcmp(redm_modes_switch, "sequentially") == 0) {
++currentIdx
currentIdx %= count
}
return currentIdx
}
static bool: ApplyMode(const modeIdx) {
if (!IsActive())
return false
if (modeIdx < 0)
return false
Config_Reload()
new JSON: arrModes = RoundModes_GetModesArr()
if (arrModes == Invalid_JSON)
return false
new JSON: objMode = json_array_get_value(arrModes, modeIdx)
if (json_object_has_value(objMode, "cvars")) {
new JSON: objCvars = json_object_get_value(objMode, "cvars")
CvarsHandler_ApplyCvars(objCvars)
json_free(objCvars)
}
json_free(objMode)
return true
}
/**
* Retrieves the current round mode index.
*/
RoundModes_GetCurrentMode() {
if (g_forcedModeIdx > RoundMode_NotSet)
return g_forcedModeIdx
return g_currentRoundModeIdx
}
/**
* Sets a forced round mode index.
*
* @param modeIdx The index of the forced round mode.
*/
RoundModes_SetForcedMode(const modeIdx = RoundMode_NotSet) {
return (g_forcedModeIdx = modeIdx)
}
static SetCurrentMode(const modeIdx = RoundMode_NotSet) {
return (g_currentRoundModeIdx = modeIdx)
}
/**
* Resets the current round mode to the default state.
*
* @return Always returns `RoundMode_NotSet` to indicate that the current round mode has been reset.
*/
RoundModes_ResetCurrentMode() {
return (g_currentRoundModeIdx = RoundMode_NotSet)
}
/**
* Checks if a round mode is forced.
*
* @return If a mode is
* forced, it returns `true`; otherwise, it returns `false`.
*/
bool: RoundModes_IsModeForced() {
// TODO: find better
return g_forcedModeIdx > RoundMode_NotSet
}