-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMvMRandomDifficulty.sp
More file actions
327 lines (272 loc) · 9.68 KB
/
MvMRandomDifficulty.sp
File metadata and controls
327 lines (272 loc) · 9.68 KB
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
#include <sourcemod>
#include <tf2>
#include <tf2_stocks>
#include <sdktools>
#pragma semicolon 1
#pragma newdecls required
bool g_bMissionSelected = false;
// Array stores the "Group Key" (e.g., "Advanced")
ArrayList g_hDifficultyList;
public Plugin myinfo = {
name = "MvM Random Difficulty",
author = "gloom",
description = "Randomly selects a supported MvM mission difficulty on first spawn.",
version = "4.3",
url = ""
};
public void OnPluginStart()
{
g_hDifficultyList = new ArrayList(ByteCountToCells(64));
RegAdminCmd("sm_randommvm", Command_RandomMvM, ADMFLAG_GENERIC, "Randomly selects a mission for the current map.");
HookEvent("player_spawn", Event_PlayerSpawn);
AutoExecConfig(true, "mvm_random_difficulties");
}
public void OnMapStart()
{
g_bMissionSelected = false;
LoadDifficulties();
}
void LoadDifficulties()
{
g_hDifficultyList.Clear();
char sPath[PLATFORM_MAX_PATH];
// Direct path construction to avoid compiler errors
Format(sPath, sizeof(sPath), "addons/sourcemod/configs/mvm_random_difficulties.cfg");
KeyValues kv = new KeyValues("MvMDifficulties");
if (!kv.ImportFromFile(sPath))
{
// Create default config with Grouped Keys
// Normal
kv.JumpToKey("Normal", true);
kv.SetString("color", "FFFFFF");
kv.SetString("abbrev", "nor");
kv.GoBack();
// Intermediate (covers intermediate, intermediate2)
kv.JumpToKey("Intermediate", true);
kv.SetString("color", "FFA500");
kv.SetString("abbrev", "int");
kv.GoBack();
// Advanced (covers advanced, advanced1, advanced2)
kv.JumpToKey("Advanced", true);
kv.SetString("color", "FF4500");
kv.SetString("abbrev", "adv");
kv.GoBack();
// Expert (covers expert, expert1, expert2)
kv.JumpToKey("Expert", true);
kv.SetString("color", "FF0000");
kv.SetString("abbrev", "exp");
kv.GoBack();
// Nightmare
kv.JumpToKey("Nightmare", true);
kv.SetString("color", "8B0000");
kv.SetString("abbrev", "nig");
kv.GoBack();
// Ironman
kv.JumpToKey("Ironman", true);
kv.SetString("color", "4169E1");
kv.SetString("abbrev", "iro");
kv.GoBack();
// 666
kv.JumpToKey("666", true);
kv.SetString("color", "800080");
kv.SetString("abbrev", "666");
kv.GoBack();
kv.Rewind();
kv.ExportToFile(sPath);
delete kv;
// Reload the newly created file
kv = new KeyValues("MvMDifficulties");
kv.ImportFromFile(sPath);
}
// Add keys to the list
char sKey[64];
if (kv.GotoFirstSubKey(false))
{
do
{
kv.GetSectionName(sKey, sizeof(sKey));
g_hDifficultyList.PushString(sKey);
}
while (kv.GotoNextKey(false));
}
delete kv;
}
public Action Event_PlayerSpawn(Event event, const char[] name, bool dontBroadcast)
{
if (g_bMissionSelected) return Plugin_Continue;
int userid = event.GetInt("userid");
int client = GetClientOfUserId(userid);
if (client > 0 && IsClientInGame(client) && !IsFakeClient(client))
{
SelectRandomMission();
}
return Plugin_Continue;
}
void SelectRandomMission()
{
if (!g_bMissionSelected)
{
g_bMissionSelected = true;
}
char mapName[128];
GetCurrentMap(mapName, sizeof(mapName));
int dotPos = FindCharInString(mapName, '.', true);
if (dotPos != -1)
{
mapName[dotPos] = '\0';
}
if (StrContains(mapName, "mvm_", false) == -1)
{
return;
}
// Structure: Array of strings. Format: "DisplayGroup|PopfileSuffix"
ArrayList availableMissions = new ArrayList(ByteCountToCells(128));
char checkPath[PLATFORM_MAX_PATH];
char sGroup[64];
char sAbbrev[16];
char sEntry[128];
char sTestName[128];
// Iterate through all defined groups in config
for (int i = 0; i < g_hDifficultyList.Length; i++)
{
g_hDifficultyList.GetString(i, sGroup, sizeof(sGroup));
// Get the abbreviation for this group
GetAbbrevForDifficulty(sGroup, sAbbrev, sizeof(sAbbrev));
// --- NORMAL HANDLING ---
if (StrEqual(sGroup, "Normal", false))
{
Format(checkPath, sizeof(checkPath), "scripts/population/%s.pop", mapName);
if (FileExists(checkPath, true))
{
Format(sEntry, sizeof(sEntry), "Normal|normal");
availableMissions.PushString(sEntry);
}
continue;
}
// --- STANDARD DIFFICULTIES ---
// We need to check multiple suffixes for a single group.
// e.g., Group "Advanced" checks "advanced", "advanced1", "advanced2", and "adv"
// 1. Check Base Name (e.g., "advanced")
Format(sTestName, sizeof(sTestName), "%s", sGroup);
StringToLower(sTestName); // Ensure lowercase for file checking
Format(checkPath, sizeof(checkPath), "scripts/population/%s_%s.pop", mapName, sTestName);
if (FileExists(checkPath, true))
{
Format(sEntry, sizeof(sEntry), "%s|%s", sGroup, sTestName);
availableMissions.PushString(sEntry);
}
// 2. Check Variants (e.g., "advanced1", "advanced2")
// We only do this if the group isn't "666" or other special ones that don't usually have numbers
if (!StrEqual(sGroup, "666") && !StrEqual(sGroup, "Ironman") && !StrEqual(sGroup, "Nightmare"))
{
for (int n = 1; n <= 2; n++)
{
Format(sTestName, sizeof(sTestName), "%s%d", sGroup, n);
StringToLower(sTestName);
Format(checkPath, sizeof(checkPath), "scripts/population/%s_%s.pop", mapName, sTestName);
if (FileExists(checkPath, true))
{
Format(sEntry, sizeof(sEntry), "%s|%s", sGroup, sTestName);
availableMissions.PushString(sEntry);
}
}
}
// 3. Check Abbreviation (e.g., "adv")
if (!StrEqual(sGroup, sAbbrev, false))
{
Format(checkPath, sizeof(checkPath), "scripts/population/%s_%s.pop", mapName, sAbbrev);
if (FileExists(checkPath, true))
{
Format(sEntry, sizeof(sEntry), "%s|%s", sGroup, sAbbrev);
availableMissions.PushString(sEntry);
}
}
}
if (availableMissions.Length == 0)
{
PrintToServer("[MvM Random] No valid mission files found for map %s", mapName);
delete availableMissions;
return;
}
int randomIndex = GetRandomInt(0, availableMissions.Length - 1);
char sSelectedEntry[128];
availableMissions.GetString(randomIndex, sSelectedEntry, sizeof(sSelectedEntry));
delete availableMissions;
// Parse the entry "DisplayGroup|PopfileSuffix"
char sDisplayGroup[64];
char sPopSuffix[64];
int splitPos = SplitString(sSelectedEntry, "|", sDisplayGroup, sizeof(sDisplayGroup));
strcopy(sPopSuffix, sizeof(sPopSuffix), sSelectedEntry[splitPos]);
char fullPopName[128];
if (StrEqual(sPopSuffix, "normal"))
{
Format(fullPopName, sizeof(fullPopName), "%s", mapName);
}
else
{
Format(fullPopName, sizeof(fullPopName), "%s_%s", mapName, sPopSuffix);
}
ServerCommand("tf_mvm_popfile %s", fullPopName);
PrintToServer("[MvM Random] Selected Difficulty: %s", sDisplayGroup);
PrintToServer("[MvM Random] Loading Popfile: %s", fullPopName);
char displayName[64];
strcopy(displayName, sizeof(displayName), sDisplayGroup);
StringToUpper(displayName);
char colorCode[16];
GetColorForDifficulty(sDisplayGroup, colorCode, sizeof(colorCode));
// Construct the final chat message with the correct color format
char sChatMsg[128];
Format(sChatMsg, sizeof(sChatMsg), "\x07%s%s", colorCode, displayName);
PrintToChatAll("%s", sChatMsg);
}
void GetAbbrevForDifficulty(const char[] sDifficulty, char[] sAbbrev, int iMaxLen)
{
char sPath[PLATFORM_MAX_PATH];
Format(sPath, sizeof(sPath), "addons/sourcemod/configs/mvm_random_difficulties.cfg");
KeyValues kv = new KeyValues("MvMDifficulties");
if (kv.ImportFromFile(sPath))
{
if (KvJumpToKey(kv, sDifficulty))
{
kv.GetString("abbrev", sAbbrev, iMaxLen, "");
kv.GoBack();
}
}
delete kv;
}
void GetColorForDifficulty(const char[] sDifficulty, char[] sColor, int iMaxLen)
{
char sPath[PLATFORM_MAX_PATH];
Format(sPath, sizeof(sPath), "addons/sourcemod/configs/mvm_random_difficulties.cfg");
KeyValues kv = new KeyValues("MvMDifficulties");
if (kv.ImportFromFile(sPath))
{
if (KvJumpToKey(kv, sDifficulty))
{
kv.GetString("color", sColor, iMaxLen, "FFFFFF");
kv.GoBack();
}
}
delete kv;
}
void StringToUpper(char[] str)
{
int len = strlen(str);
for (int i = 0; i < len; i++)
{
str[i] = CharToUpper(str[i]);
}
}
void StringToLower(char[] str)
{
int len = strlen(str);
for (int i = 0; i < len; i++)
{
str[i] = CharToLower(str[i]);
}
}
public Action Command_RandomMvM(int client, int args)
{
SelectRandomMission();
return Plugin_Handled;
}