-
Notifications
You must be signed in to change notification settings - Fork 4
/
Configs.sp
412 lines (327 loc) · 12.3 KB
/
Configs.sp
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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
#pragma semicolon 1
Handle g_AutoCoord_Timer = INVALID_HANDLE;
public void ParseChaosEffects(){
ChaosEffects.Clear();
MetaEffects.Clear();
char function_name[64];
int count = 0;
for(int i = 0; i < sizeof(EffectNames); i++)
{
EffectData effect; // Ensures a new object every time
Function effect_function = GetFunctionByName(GetMyHandle(), EffectNames[i]);
if(effect_function == INVALID_FUNCTION){
Log("Couldnt find setup function for %s", EffectNames[i]);
continue;
}
count++;
Call_StartFunction(GetMyHandle(), effect_function);
Call_PushArrayEx(effect, sizeof(effect), SM_PARAM_COPYBACK);
Call_Finish();
Format(function_name, sizeof(function_name), "%s_START", EffectNames[i]);
effect.START = GetFunctionByName(GetMyHandle(), function_name);
Format(function_name, sizeof(function_name), "%s_RESET", EffectNames[i]);
effect.RESET = GetFunctionByName(GetMyHandle(), function_name);
Format(function_name, sizeof(function_name), "%s_OnGameFrame", EffectNames[i]);
effect.OnGameFrame = GetFunctionByName(GetMyHandle(), function_name);
Format(function_name, sizeof(function_name), "%s_OnPlayerRunCmd", EffectNames[i]);
effect.OnPlayerRunCmd = GetFunctionByName(GetMyHandle(), function_name);
Format(function_name, sizeof(function_name), "%s_OnEntityCreated", EffectNames[i]);
effect.OnEntityCreated = GetFunctionByName(GetMyHandle(), function_name);
Format(function_name, sizeof(function_name), "%s_OnEntityDestroyed", EffectNames[i]);
effect.OnEntityDestroyed = GetFunctionByName(GetMyHandle(), function_name);
Format(function_name, sizeof(function_name), "%s_Conditions", EffectNames[i]);
effect.Conditions = GetFunctionByName(GetMyHandle(), function_name);
Format(function_name, sizeof(function_name), "%s_OnPlayerSpawn", EffectNames[i]);
effect.OnPlayerSpawn = GetFunctionByName(GetMyHandle(), function_name);
if(
effect.START == INVALID_FUNCTION &&
effect.RESET == INVALID_FUNCTION &&
effect.OnPlayerRunCmd == INVALID_FUNCTION &&
effect.OnEntityCreated == INVALID_FUNCTION &&
effect.OnEntityDestroyed == INVALID_FUNCTION &&
effect.OnGameFrame == INVALID_FUNCTION
){
Log("Could not find any start/reset functions or any forwards for %s. This effect will be ignored [DEBUG]", EffectNames[i]);
continue;
}
// if(start_func == INVALID_FUNCTION || reset_func == INVALID_FUNCTION) continue;
if(effect.Duration > 0 && effect.HasNoDuration == true){
effect.Duration = 0;
}
Format(function_name, sizeof(function_name), EffectNames[i]);
Format(effect.FunctionName, sizeof(effect.FunctionName), "%s", function_name);
effect.Enabled = true;
char Chaos_Function_Title[64];
// If valid translation exists, use translation, else use plugin default
if(TranslationPhraseExists(effect.FunctionName) && IsTranslatedForLanguage(effect.FunctionName, LANG_SERVER)){
Format(Chaos_Function_Title, sizeof(Chaos_Function_Title), "%t", effect.FunctionName, LANG_SERVER);
effect.Title = Chaos_Function_Title;
}
ChaosEffects.PushArray(effect);
if(effect.IsMetaEffect){
MetaEffects.PushString(effect.FunctionName);
}
}
MetaEffects.Sort(Sort_Random, Sort_String);
ChaosEffects.Sort(Sort_Ascending, Sort_String); // sort the effects alphabetically
EffectData effect;
LoopAllEffects(effect, index){
effect.ID = index;
ChaosEffects.SetArray(index, effect);
}
}
public void OnConfigsExecuted(){
ParseMapCoordinates("Chaos_Locations");
// ParseChaosConfigEffects();
ParseOverrideEffects();
int warnings = 0;
Log("---------------------------------CONFIGS EXECUTED---------------------------------");
if(!ValidMapPoints()){
warnings++;
Log("ERROR: No valid spawn points were found for %s. Certain effects will not run.", g_sCurrentMapName);
}else{
Log("MAP SPAWNS FOUND.");
}
if(IsHostageMap()){
warnings++;
Log("WARNING: %s has been flagged as a hostage map, certain C4 effects will be disabled.", g_sCurrentMapName);
}
if(!ValidBombSpawns()){
warnings++;
Log("No valid bomb spawns were found for %s. Certain C4 effects will not run.", g_sCurrentMapName);
}
StopTimer(g_AutoCoord_Timer);
if(!ValidMapPoints()){
Log("No valid map points. Locations will be automatically saved");
ParseMapCoordinates("Chaos_TempLocations2");
CreateTimer(2.5, Timer_SaveCoordinates, _, TIMER_REPEAT);
}
Run_OnMapStart_Functions();
}
void Run_OnMapStart_Functions(){
EffectData effect;
char init_function[64];
LoopAllEffects(effect, index){
Format(init_function, sizeof(init_function), "%s_OnMapStart", effect.FunctionName);
Function func = GetFunctionByName(GetMyHandle(), init_function);
if(func != INVALID_FUNCTION){
Call_StartFunction(GetMyHandle(), func);
Call_Finish();
}
}
}
void SaveBombPosition(){
float c4_location[3];
bool found = false;
char classname[64];
LoopAllEntities(ent, GetMaxEntities(), classname){
if(StrEqual(classname, "planted_c4")){ //&& GetEntPropEnt(ent, Prop_Send, "m_hOwnerEntity") == -1){
found = true;
GetEntPropVector(ent, Prop_Send, "m_vecOrigin", c4_location);
}
}
if(!found) return;
int site = GetNearestBombsite(c4_location);
if(site == BOMBSITE_INVALID) return;
char c4_loc_string[64];
FormatEx(c4_loc_string, sizeof(c4_loc_string), "%f %f %f", c4_location[0], c4_location[1], c4_location[2]);
if(site == BOMBSITE_A){
UpdateConfig(-1, "Chaos_TempLocations2", "Maps", g_sCurrentMapName, "bombA", c4_loc_string, .folderPath="data");
PushArrayArray(bombSiteA, c4_location);
}else if(site == BOMBSITE_B){
UpdateConfig(-1, "Chaos_TempLocations2", "Maps", g_sCurrentMapName, "bombB", c4_loc_string, .folderPath="data");
PushArrayArray(bombSiteB, c4_location);
}
PushArrayArray(g_MapCoordinates, c4_location);
}
Action Timer_SaveCoordinates(Handle timer){
if (GameRules_GetProp("m_bWarmupPeriod") == 1) return Plugin_Continue;
if(g_bPortalGuns || TinyPlayers || _1v1Active) return Plugin_Continue;
if(IsCoopStrike()) return Plugin_Continue;
float client_vec[3];
float client_vel[3];
float compare_vec[3];
char client_vec_string[64];
LoopAlivePlayers(i){
GetClientAbsOrigin(i, client_vec);
GetEntPropVector(i, Prop_Data, "m_vecVelocity", client_vel);
if(!(GetClientButtons(i) & IN_DUCK) && client_vel[2] == 0.0 && GetEntityMoveType(i) == MOVETYPE_WALK && GetEntPropFloat(i, Prop_Send, "m_flLaggedMovementValue") == 1.0) { //ensure player isnt mid jump or falling down
FormatEx(client_vec_string, sizeof(client_vec_string), "%f %f %f", client_vec[0], client_vec[1], client_vec[2]);
if(GetArraySize(g_MapCoordinates) == 0){
UpdateConfig(-1, "Chaos_TempLocations2", "Maps", g_sCurrentMapName, client_vec_string, client_vec_string, .folderPath="data");
PushArrayArray(g_MapCoordinates, client_vec);
}else{
float distanceToBeat = 99999.0;
for(int g = 0; g < GetArraySize(g_MapCoordinates); g++){
GetArrayArray(g_MapCoordinates, g, compare_vec, sizeof(compare_vec));
float dist = GetVectorDistance(client_vec, compare_vec);
if(dist < distanceToBeat){
distanceToBeat = dist;
}
}
if(distanceToBeat > 250){
UpdateConfig(-1, "Chaos_TempLocations2", "Maps", g_sCurrentMapName, client_vec_string, client_vec_string, .folderPath="data");
PushArrayArray(g_MapCoordinates, client_vec);
}
}
}
}
return Plugin_Continue;
}
void ParseOverrideEffects(){
char filePath[PLATFORM_MAX_PATH];
BuildPath(Path_SM, filePath, sizeof(filePath), "configs/Chaos/Chaos_Override.cfg");
if(!FileExists(filePath)){
return;
}
KeyValues kvConfig = new KeyValues("Effects");
if(!kvConfig.ImportFromFile(filePath)){
return;
}
if(!kvConfig.GotoFirstSubKey()){
return;
}
do{
char Chaos_Function_Name[64];
if (kvConfig.GetSectionName(Chaos_Function_Name, sizeof(Chaos_Function_Name))){
int enabled = kvConfig.GetNum("enabled", 1);
int expires = kvConfig.GetNum("duration", -1);
if(enabled != 0 && enabled != 1) enabled = 1;
EffectData effect;
LoopAllEffects(effect, index){
if(StrEqual(effect.FunctionName, Chaos_Function_Name, false)){
effect.Enabled = view_as<bool>(enabled);
if(!effect.OverrideDuration && expires > -1){
effect.Duration = expires;
}else {
if(expires > -1){
Log("Can not set duration for effect '%s' as it is controlled by the plugin.", Chaos_Function_Name);
}
}
ChaosEffects.SetArray(index, effect);
// PrintToChatAll("Found %s override and setting enabled to %i and duration to %i", Chaos_Function_Name, enabled, expires);
}
}
}
} while(kvConfig.GotoNextKey());
ChaosEffects.Sort(Sort_Ascending, Sort_String); // sort the effects alphabetically
Log("Parsed Chaos_Override.cfg Effects succesfully!");
}
void InitSpawns() {
g_UnusedCoordinates = CreateArray(3);
//TODO: put all map spawn data into structs
if(g_MapCoordinates != INVALID_HANDLE) ClearArray(g_MapCoordinates);
if(bombSiteA != INVALID_HANDLE) ClearArray(bombSiteA);
if(bombSiteB != INVALID_HANDLE) ClearArray(bombSiteB);
g_MapCoordinates = INVALID_HANDLE;
bombSiteA = INVALID_HANDLE;
bombSiteB = INVALID_HANDLE;
}
void ParseMapCoordinates(char[] config) {
char path[PLATFORM_MAX_PATH];
BuildPath(Path_SM, path, sizeof(path), "data/Chaos/%s.cfg", config);
if(g_MapCoordinates == INVALID_HANDLE){
g_MapCoordinates = CreateArray(3);
bombSiteA = CreateArray(3);
bombSiteB = CreateArray(3);
}else{
if(StrEqual(config, "Chaos_Locations", false)){
ClearArray(g_MapCoordinates);
ClearArray(bombSiteA);
ClearArray(bombSiteB);
}
}
if(!FileExists(path)){
Log("Could not find %s. Effects that rely on map data will not run.", path);
return;
}
GetCurrentMap(g_sCurrentMapName, sizeof(g_sCurrentMapName));
KeyValues kv = new KeyValues("Maps");
if(!kv.ImportFromFile(path)){
Log("Unable to parse Key Values from %s", path);
return;
}
//jump to key of map name
if(!kv.JumpToKey(g_sCurrentMapName)){
Log("Unable to find %s Key from %s", g_sCurrentMapName, path);
return;
}
if(!kv.GotoFirstSubKey(false)){
Log("Unable to find sub keys %s", path);
return;
}
//all keys are fine to go in the all map coordinates, but we ALSO want to add bombA and bombB to respective handles;
do{
float vec[3];
kv.GetVector(NULL_STRING, vec);
char key[25];
kv.GetSectionName(key, sizeof(key));
if(StrContains(key, "bombA", false) != -1) PushArrayArray(bombSiteA, vec);
if(StrContains(key, "bombB", false) != -1) PushArrayArray(bombSiteB, vec);
PushArrayArray(g_MapCoordinates, vec);
} while(kv.GotoNextKey(false));
delete kv;
}
void UpdateConfig(int client = -1, char[] config, char[] KeyValues_name, char[] function_name, char[] key, char[] newValue, char[] folderPath="configs"){
char path[PLATFORM_MAX_PATH];
//TODO: for /data and /configs
BuildPath(Path_SM, path, sizeof(path), "%s/Chaos/%s.cfg", folderPath, config);
KeyValues kvConfig = new KeyValues(KeyValues_name);
if(!FileExists(path)){
Handle FileHandle = OpenFile(path, "w");
if(!FileHandle){
CloseHandle(FileHandle);
return;
}
CloseHandle(FileHandle);
}else{
if(!kvConfig.ImportFromFile(path)){
Log("Unable to parse Key Values file %s.", path);
}
}
kvConfig.JumpToKey(function_name, true);
kvConfig.SetString(key, newValue);
kvConfig.Rewind();
kvConfig.ExportToFile(path);
if(kvConfig.ExportToFile(path)){
if(IsValidClient(client)){
PrintToChatAll("Effect '%s' modified in config. Key '%s' has been set to '%s'", function_name, key, newValue);
Log("Effect '%s' modified in config. Key '%s' has been set to '%s'", function_name, key, newValue);
}
if(StrEqual(KeyValues_name, "Effects", false)){
// ParseChaosConfigEffects();
ParseOverrideEffects();
}
}else{
if(IsValidClient(client)){
PrintToChat(client, "[Chaos] Failed to update config.");
}
}
delete kvConfig;
}
void LogEffect(char[] function_name){
char path[PLATFORM_MAX_PATH];
BuildPath(Path_SM, path, sizeof(path), "data/Chaos/Chaos_Statistics.cfg");
KeyValues kvConfig = new KeyValues("Stats");
if(!FileExists(path)){
Handle FileHandle = OpenFile(path, "w");
if(!FileHandle){
CloseHandle(FileHandle);
return;
}
CloseHandle(FileHandle);
}else{
if(!kvConfig.ImportFromFile(path)){
Log("Unable to parse Key Values file %s.", path);
}
}
kvConfig.JumpToKey(function_name, true);
int currentValue = kvConfig.GetNum("Times Ran", 0);
kvConfig.SetNum("Times Ran", currentValue + 1);
kvConfig.Rewind();
kvConfig.ExportToFile(path);
if(!kvConfig.ExportToFile(path)){
Log("Error saving to %s", path);
}
delete kvConfig;
}