-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwaypoint.cpp
12246 lines (10147 loc) · 333 KB
/
waypoint.cpp
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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//////////////////////////////////////////////////////////////////////////////////////////////
//
// -- GNU -- open source
// Please read and agree to the mb_gnu_license.txt file
// (the file is located in the marine_bot source folder)
// before editing or distributing this source code.
// This source code is free for use under the rules of the GNU General Public License.
// For more information goto:: http://www.gnu.org/licenses/
//
// credits to - valve, botman.
//
// Marine Bot - code by Frank McNeil, Kota@, Mav, Shrike.
//
// (http://marinebot.xf.cz)
//
//
// waypoint.cpp
//
////////////////////////////////////////////////////////////////////////////////////////////////
#if defined(WIN32)
#pragma warning(disable: 4005 91)
#endif
#ifndef __linux__
#include <io.h>
#endif
#include <fcntl.h>
#ifndef __linux__
#include <sys\stat.h>
#else
#include <sys/stat.h>
#include <unistd.h>
#endif
#include "defines.h"
#include "extdll.h"
#include "enginecallback.h"
#include "util.h"
#include "cbase.h"
#include "bot.h"
#include "bot_func.h"
#include "bot_manager.h"
#include "waypoint.h"
#define RANGE_20_WPT (W_FL_AMMOBOX | W_FL_DOOR | W_FL_DOORUSE | W_FL_JUMP | W_FL_DUCKJUMP | \
W_FL_LADDER | W_FL_USE)
extern int m_spriteTexture;
extern int m_spriteTexturePath1;
extern int m_spriteTexturePath2;
extern int m_spriteTexturePath3;
// waypoints with information bits (flags)
WAYPOINT waypoints[MAX_WAYPOINTS];
// number of waypoints currently in use
int num_waypoints;
// declare the array of paths
W_PATH *w_paths[MAX_W_PATHS];
// number of w_paths currently in use (must be < MAX_W_PATHS)
int num_w_paths;
// declare the array of trigger events and the array of current game state of the events
TRIGGER_EVENT trigger_events[MAX_TRIGGERS];
trigger_event_gamestate_t trigger_gamestate[MAX_TRIGGERS];
waypointsbrowser_t wptser;
// set all waypoints browser variables to defaults
waypointsbrowser_t::waypointsbrowser_t()
{
ResetShowWaypoints();
ResetShowPaths();
ResetCheckAims();
ResetCheckCross();
ResetCheckRanges();
ResetAutoWaypointing();
ResetAutoAddToPath();
ResetWaypointsDrawDistance();
ResetAutoWaypointingDistance();
ResetCompassIndex();
ResetPathToHighlight();
};
// reset all waypoints browser variables to default values on map change
// to prevent overloading game engine
void waypointsbrowser_t::ResetOnMapChange()
{
ResetShowWaypoints();
ResetShowPaths();
ResetCheckAims();
ResetCheckCross();
ResetCheckRanges();
ResetAutoWaypointing();
ResetAutoAddToPath();
ResetWaypointsDrawDistance();
ResetAutoWaypointingDistance();
ResetCompassIndex();
ResetPathToHighlight();
};
char wpt_author[32];
char wpt_modified[32];
// variables used only in this file
static FILE *fp;
Vector last_waypoint; // for autowaypointing
bool g_waypoint_paths = FALSE; // have any paths been allocated?
float wp_display_time[MAX_WAYPOINTS]; // time that this waypoint was displayed (while editing)
float f_path_time[MAX_W_PATHS]; // time that this path was diplayed (while editing)
float f_conn_time[MAX_WAYPOINTS][MAX_WAYPOINTS]; // time that the connections (cross&aim) were displayed while editing
float f_ranges_display_time[MAX_WAYPOINTS]; // time that this waypoint range was displayed (while editing)
float f_compass_time; // time that the compass was displayed
// functions prototypes used in this file
void WaypointDebug();
void FreeAllPaths();
void FreeAllTriggers();
void WaypointInit(int wpt_index);
float GetWaypointDistance(int wpt1_index = -1, int wpt2_index = -1);
bool IsMessageValid(char *msg);
int WaypointReturnTriggerWaypointPriority(int wpt_index, int team);
int TriggerNameToIndex(const char *trigger_name);
TriggerId TriggerNameToId(const char *trigger_name);
TriggerId TriggerIndexToId(int i);
void TriggerIntToName(int id, char *trigger_name);
TriggerId IntToTriggerId(int i);
int TriggerIdToInt(TriggerId triggerId);
int IsPathOkay(int path_index);
int MergePaths(int path1_index = -1, int path2_index = -1, bool reverse_order = false);
int MergePathsInverted(int path1_index = -1, int path2_index = -1, bool reverse_order = false);
int WaypointFixInvalidFlags(int wpt_index);
void UpdatePathStatus(int path_index);
void UpdateGoalPathStatus(int wpt_index, int path_index);
int GetPathStart(int path_index);
int GetPathEnd(int path_index);
int GetPathPreviousWaypoint(int wpt_index, int path_index);
bool IsPath(int path_index, PathT path_type);
bool PathMatchingFlag(int path_index1, int path_index2, PathT path_type);
bool PathCompareTeamDownwards(int checked_path, int standard_path);
bool PathCompareClassDownwards(int checked_path, int standard_path);
int ContinueCurrPath(int current_wpt_index, bool check_presence = true);
int ExcludeFromPath(int current_wpt_index, int path_index);
int ExcludeFromPath(W_PATH *p = nullptr, int path_index = -1);
bool DeleteWholePath(int path_index);
W_PATH *GetWaypointPointer(int wpt_index, int path_index);
W_PATH *GetWaypointTypePointer(WptT flag_type, int path_index);
bool WaypointTypeIsInPath(WptT flag_type, int path_index);
void WaypointSetValue(bot_t *pBot, int wpt_index, WAYPOINT_VALUE *wpt_value);
bool IsWaypointPossible(bot_t *pBot, int wpt_index);
int WaypointFindNearestType(edict_t *pEntity, float range, int type);
int WaypointFindNearestCross(const Vector &source_origin);
int WaypointFindNearestCross(const Vector &source_origin, bool see_through_doors);
int WaypointFindNearestStandard(int end_waypoint, int path_index);
int WaypointFindAimingAround(int source_waypoint);
int Wpt_CountFlags(int wpt_index);
WptT WaypointAdd(Vector position, WptT wpt_type);
int PathClassFlagCount(int path_index);
void GetPathClass(int path_index, char *the_class);
bool WaypointReachable(Vector v_srv, Vector v_dest, edict_t *pEntity);
void WptGetType(int wpt_index, char *the_type);
void SetWaypointSize(int wpt_index, Vector &start, Vector &end);
Vector SetWaypointColor(int wpt_index);
int SetPathTexture(int path_index);
Vector SetPathColor(int path_index);
/*
* returns true if the path passed by path_index has an ignore enemy flag
*/
bool bot_t::IsIgnorePath(int path_index)
{
if (path_index == -1)
path_index = curr_path_index;
// this will handle passing through a cross waypoint the bot won't remove the ignore enemy flag in this case,
// but will wait for next path to decide if the flag should or should not be removed
if (IsWaypoint(curr_wpt_index, WptT::wpt_cross) || IsWaypoint(prev_wpt_index.get(0), WptT::wpt_cross))
return TRUE;
if (IsPath(path_index, PathT::path_ignore))
return TRUE;
return FALSE;
}
/*
* returns the next waypoint index from a path the bot's using
*/
int bot_t::GetNextWaypoint() const
{
if ((curr_wpt_index == -1) || (curr_path_index == -1))
return -1;
W_PATH *cur_w_path = w_paths[curr_path_index];
while (cur_w_path)
{
if (cur_w_path->wpt_index == curr_wpt_index)
{
// the bot is using the path start to end so return next
if (!opposite_path_dir && (cur_w_path->next))
return cur_w_path->next->wpt_index;
// the bot is using the path from end to start so return previous
if (opposite_path_dir && (cur_w_path->prev))
return cur_w_path->prev->wpt_index;
}
cur_w_path = cur_w_path->next;
}
return -1;
}
void trigger_event_gamestate_t::Init()
{
SetName(TriggerId::trigger_none);
SetUsed(false);
SetTriggered(false);
SetTime(0.0);
}
void trigger_event_gamestate_t::Reset(int i)
{
if (i == -1)
SetName(TriggerId::trigger_none);
else
SetName(TriggerIndexToId(i));
SetTriggered(false);
SetTime(0.0);
}
void WaypointDebug()
{
int y = 1, x = 1;
fp=fopen(debug_fname,"a");
fprintf(fp,"WaypointDebug: LINKED LIST ERROR!!!\n");
fclose(fp);
x = x - 1; // x is zero
y = y / x; // cause an divide by zero exception
return;
}
/*
* free all paths
*/
void FreeAllPaths()
{
for (int path_index = 0; path_index < MAX_W_PATHS; path_index++)
{
int safety_stop = 0; // to stop it if encounters an error
if (w_paths[path_index])
{
W_PATH *p = w_paths[path_index]; // set the pointer to the head node
while (p)
{
W_PATH* p_next = p->next; // save the link to next
free(p); // free this node
p = p_next; // update the head node
#ifdef _DEBUG
safety_stop++;
if (safety_stop > 1000)
WaypointDebug();
#endif
}
w_paths[path_index] = nullptr;
}
}
}
/*
* free all trigger event messages
*/
void FreeAllTriggers()
{
for (int trigger_index = 0; trigger_index < MAX_TRIGGERS; trigger_index++)
{
trigger_events[trigger_index].name = TriggerIndexToId(trigger_index);
trigger_events[trigger_index].message[0] = '\0';
// we also have to initialize the current game state of the triggers
trigger_gamestate[trigger_index].Init();
}
}
/*
* add wpts author signature or sig of guy who modify them into file depending on bool author
* authors sig can't be changed
*/
bool WaypointSubscribe(char *signature, bool author, bool enforced)
{
char mapname[64];
char filename[256];
WAYPOINT_HDR header;
strcpy(mapname, STRING(gpGlobals->mapname));
strcat(mapname, ".wpt");
if (internals.IsCustomWaypoints())
UTIL_MarineBotFileName(filename, "customwpts", mapname);
else
UTIL_MarineBotFileName(filename, "defaultwpts", mapname);
FILE *bfp = fopen(filename, "rb");
if (bfp != nullptr)
{
fread(&header, sizeof(header), 1, bfp);
// is it authors sig
if (author)
{
// is wpt file NOT subcribed OR do we force it so access granted to change wpt_author (no file writing)
if ((strcmp(header.author, "") == 0) || (strcmp(header.author, "unknown") == 0) || enforced)
{
strcpy(wpt_author, signature);
wpt_author[31] = 0; // must be ended properly
fclose(bfp);
return TRUE;
}
// otherwise access denied
else
{
fclose(bfp);
return FALSE;
}
}
// sig of guy who modify them (no file writing)
else
{
if (FStrEq(signature, "clear"))
{
wpt_modified[0] = '\0'; // wipe the modified by variable
}
else
{
strcpy(wpt_modified, signature);
wpt_modified[31] = 0; // must be ended properly
}
fclose(bfp);
return TRUE;
}
}
else
{
return FALSE;
}
}
/*
* read waypoints author signature as well as waypoint modifier signature from waypoint file header
*/
void WaypointAuthors(char *author, char *modified_by)
{
char mapname[64];
char filename[256];
WAYPOINT_HDR header;
strcpy(mapname, STRING(gpGlobals->mapname));
strcat(mapname, ".wpt");
if (internals.IsCustomWaypoints())
UTIL_MarineBotFileName(filename, "customwpts", mapname);
else
UTIL_MarineBotFileName(filename, "defaultwpts", mapname);
FILE* bfp = fopen(filename, "rb");
if (bfp != nullptr)
{
fread(&header, sizeof(header), 1, bfp);
// get the author signature if there's any written in the file header
if ((strcmp(header.author, "") == 0) || (strcmp(header.author, "unknown") == 0))
strcpy(author, "noauthor"); // there's NO author tag in this waypoint file
else
strcpy(author, header.author);
// get the signature of guy who modified them if exists
if ((strcmp(header.modified_by, "") == 0) || (strcmp(header.modified_by, "unknown") == 0))
strcpy(modified_by, "nosig"); // waypoint file is NOT subscribed
else
strcpy(modified_by, header.modified_by);
fclose(bfp);
}
// waypoint file doesn't exist
else
{
strcpy(author, "nofile");
strcpy(modified_by, "nofile");
}
}
/*
* initialize just one waypoint
*/
void WaypointInit(int wpt_index)
{
waypoints[wpt_index].flags = W_FL_DELETED;
waypoints[wpt_index].red_priority = MAX_WPT_PRIOR; // lowest priority
waypoints[wpt_index].red_time = 0.0; // no "stay here" time
waypoints[wpt_index].blue_priority = MAX_WPT_PRIOR;
waypoints[wpt_index].blue_time = 0.0;
waypoints[wpt_index].trigger_red_priority = MAX_WPT_PRIOR;
waypoints[wpt_index].trigger_blue_priority = MAX_WPT_PRIOR;
waypoints[wpt_index].trigger_event_on = TriggerIdToInt(TriggerId::trigger_none); // no trigger event
waypoints[wpt_index].trigger_event_off = TriggerIdToInt(TriggerId::trigger_none);
waypoints[wpt_index].range = WPT_RANGE; // default range
waypoints[wpt_index].origin = g_vecZero;//Vector(0, 0, 0); // located at map origin
}
/*
* initialize the waypoint structures
*/
void WaypointInit()
{
// initialize the trigger event messages
FreeAllTriggers();
// have any waypoint path nodes been allocated yet?
if (g_waypoint_paths)
{
FreeAllPaths();
}
// destroy all waypoints with all their flags, priorities etc.
for (int i = 0; i < MAX_WAYPOINTS; i++)
{
WaypointInit(i);
wp_display_time[i] = 0.0;
f_ranges_display_time[i] = 0.0;
// fill also the "sub array"
for (int j = 0; j < MAX_WAYPOINTS; j++)
f_conn_time[i][j] = 0.0;
}
// prepare w_paths array
for (int path_index = 0; path_index < MAX_W_PATHS; path_index++)
{
f_path_time[path_index] = 0.0; // reset path display time
}
num_waypoints = 0;
num_w_paths = 0;
last_waypoint = g_vecZero;//Vector(0,0,0);
}
/*
* returns distance between two waypoints passed by their indexes
* if either of the indexes isn't valid it will return 9999.0
*/
float GetWaypointDistance(int wpt1_index, int wpt2_index)
{
if ((wpt1_index == -1) || (wpt2_index == -1))
return 9999.0f;
return (waypoints[wpt1_index].origin - waypoints[wpt2_index].origin).Length();
}
/*
* clear all waypoints & paths
* remove both signatures
* wipe both files from the HDD
*/
void WaypointDestroy()
{
char mapname[64];
char filename[256];
WaypointInit();
// reset author's signature
strcpy(wpt_author, "unknown");
wpt_author[31] = 0;
// reset modified_by signature
strcpy(wpt_modified, "unknown");
wpt_modified[31] = 0;
strcpy(mapname, STRING(gpGlobals->mapname));
strcat(mapname, ".wpt");
// build the filename/filepath for this file
if (internals.IsCustomWaypoints())
UTIL_MarineBotFileName(filename, "customwpts", mapname);
else
UTIL_MarineBotFileName(filename, "defaultwpts", mapname);
// and then erase it from HDD
remove(filename);
strcpy(mapname, STRING(gpGlobals->mapname));
strcat(mapname, ".pth");
if (internals.IsCustomWaypoints())
UTIL_MarineBotFileName(filename, "customwpts", mapname);
else
UTIL_MarineBotFileName(filename, "defaultwpts", mapname);
remove(filename);
return;
}
/*
* checks the message for double space or new line characters as these would invalidate the string comparison
*/
bool IsMessageValid(char *msg)
{
// just in case it's not handled before calling this method
if (msg == nullptr)
return FALSE;
const int length = strlen(msg);
for (int i = 0; i < length; i++)
{
if (msg[i] == '\n')
return FALSE;
if (msg[i] == ' ' && msg[i+1] == ' ')
return FALSE;
}
return TRUE;
}
/*
* initialize the trigger event triggered variable
*/
void WaypointResetTriggers()
{
if (botdebugger.IsDebugWaypoints())
HudNotify("All triggers have been set back to \"not triggered\"\n");
for (int i = 0; i < MAX_TRIGGERS; i++)
{
trigger_gamestate[i].Reset(i);
}
}
/*
* compare the text with trigger messages and search for matching one
*/
bool WaypointMatchingTriggerMessage(char *the_text)
{
if (the_text == nullptr)
return FALSE;
//@@@@@@@@@@@@@@@
/*
#ifdef _DEBUG
//if (botdebugger.IsDebugWaypoints())
// ALERT(at_console, "WptMatchingTriggerMSG() -> the msg before while statement is: (%s)\n", the_text);
//UTIL_DebugDev(the_text, -100, -100);
#endif
/**/
// see if the message is nice and clean ie. no additional space or new line characters
while (IsMessageValid(the_text) == false)
{
int len = strlen(the_text);
for (int ch = 0; ch < len; ch++)
{
// replace new line character with a space
if (the_text[ch] == '\n')
the_text[ch] = ' ';
// is there a space following another space then we have probably touched the string already
// so we need to remove the additional space by shifting the rest of the string
if (the_text[ch] == ' ' && the_text[ch+1] == ' ')
{
for (int s = ch; s < len; s++)
the_text[s] = the_text[s+1];
len--;
}
}
}
//@@@@@@@@@@@@@@@
/*
#ifdef _DEBUG
//if (botdebugger.IsDebugWaypoints())
// ALERT(at_console, "WptMatchingTriggerMSG() -> the final msg is: (%s)\n", the_text);
//UTIL_DebugDev(the_text, -100, -100);
#endif
/**/
// then compare it with the trigger events
for (int i = 0; i < MAX_TRIGGERS; i++)
{
// is this slot used and does the text match stored trigger message?
if (trigger_events[i].name == trigger_gamestate[i].GetName() &&
trigger_gamestate[i].GetUsed() &&
(strstr(the_text, trigger_events[i].message) != nullptr))
{
if (botdebugger.IsDebugWaypoints())
ALERT(at_console, "Trigger%d has been triggered\n", i+1);
// then mark it as event happened
trigger_gamestate[i].SetTriggered(true);
// also store the time of this event
trigger_gamestate[i].SetTime();
return TRUE;
}
}
return FALSE;
}
/*
* return correct priority for trigger waypoint based on the state of game events
*/
int WaypointReturnTriggerWaypointPriority(int wpt_index, int team)
{
const int return_no_priority = 0;
if (wpt_index == -1)
return return_no_priority; // like no priority
if (IsWaypoint(wpt_index, WptT::wpt_trigger))
{
// this is ugly conversion
//char name[32];
//TriggerIntToName(waypoints[wpt_index].trigger_event_on, name); NEW CODE 094 (prev code)
//TriggerN trigger_on = TriggerNameToId(name);
//TriggerIntToName(waypoints[wpt_index].trigger_event_off, name);
//TriggerN trigger_off = TriggerNameToId(name);
const TriggerId trigger_on = IntToTriggerId(waypoints[wpt_index].trigger_event_on);// NEW CODE 094
const TriggerId trigger_off = IntToTriggerId(waypoints[wpt_index].trigger_event_off);
float trigger_off_time;
float trigger_on_time = trigger_off_time = 0.0;
if (trigger_on != TriggerId::trigger_none)
{
for (int i = 0; i < MAX_TRIGGERS; i++)
{
// this trigger slot is used and
// the game event linked to it has already been triggered
if (trigger_gamestate[i].GetUsed() && trigger_gamestate[i].GetTriggered())
{
// then check if this trigger event is set for the waypoint and
// get the time of the trigger_on event (ie. when it happened)
if (trigger_on == trigger_gamestate[i].GetName())
trigger_on_time = trigger_gamestate[i].GetTime();
// get the time of the trigger_off event (if it is used on the waypoint)
if (trigger_off != TriggerId::trigger_none && // is there any trigger_off event set for this waypoint
(trigger_off == trigger_gamestate[i].GetName()))
trigger_off_time = trigger_gamestate[i].GetTime();
}
}
// if the trigger_on event happened after the trigger_off event
// then we have to return the trigger priorities, because this
// waypoints is triggered on
if (trigger_on_time > trigger_off_time)
{
// so we must return the trigger priorities instead of standard ones
if (team == TEAM_RED)
return waypoints[wpt_index].trigger_red_priority;
if (team == TEAM_BLUE)
return waypoints[wpt_index].trigger_blue_priority;
return return_no_priority; // just for sure if something went wrong
}
}
}
// return normal priority because of:
// 1) this waypoint is not a trigger waypoint or
// 2) the trigger_off event has been called (ie. happened) so the trigger waypoint is being
// turned off and thus we have to use the normal priorities again
if (team == TEAM_RED)
return waypoints[wpt_index].red_priority;
if (team == TEAM_BLUE)
return waypoints[wpt_index].blue_priority;
return return_no_priority; // just for sure if something went wrong
}
/*
* convert trigger name (string) to trigger array index
*/
int TriggerNameToIndex(const char *trigger_name)
{
if (FStrEq(trigger_name, "trigger1"))
return 0;
else if (FStrEq(trigger_name, "trigger2"))
return 1;
else if (FStrEq(trigger_name, "trigger3"))
return 2;
else if (FStrEq(trigger_name, "trigger4"))
return 3;
else if (FStrEq(trigger_name, "trigger5"))
return 4;
else if (FStrEq(trigger_name, "trigger6"))
return 5;
else if (FStrEq(trigger_name, "trigger7"))
return 6;
else if (FStrEq(trigger_name, "trigger8"))
return 7;
return -1;
}
/*
* convert trigger name (string) to trigger ID
*/
TriggerId TriggerNameToId(const char *trigger_name)
{
if (FStrEq(trigger_name, "trigger1"))
return TriggerId::trigger1;
else if (FStrEq(trigger_name, "trigger2"))
return TriggerId::trigger2;
else if (FStrEq(trigger_name, "trigger3"))
return TriggerId::trigger3;
else if (FStrEq(trigger_name, "trigger4"))
return TriggerId::trigger4;
else if (FStrEq(trigger_name, "trigger5"))
return TriggerId::trigger5;
else if (FStrEq(trigger_name, "trigger6"))
return TriggerId::trigger6;
else if (FStrEq(trigger_name, "trigger7"))
return TriggerId::trigger7;
else if (FStrEq(trigger_name, "trigger8"))
return TriggerId::trigger8;
return TriggerId::trigger_none;
}
/*
* convert trigger array index to trigger ID
*/
TriggerId TriggerIndexToId(int i)
{
if (i == 0)
return TriggerId::trigger1;
else if (i == 1)
return TriggerId::trigger2;
else if (i == 2)
return TriggerId::trigger3;
else if (i == 3)
return TriggerId::trigger4;
else if (i == 4)
return TriggerId::trigger5;
else if (i == 5)
return TriggerId::trigger6;
else if (i == 6)
return TriggerId::trigger7;
else if (i == 7)
return TriggerId::trigger8;
return TriggerId::trigger_none;
}
/*
* convert trigger ID to trigger name (string)
*/
void TriggerIntToName(int i, char *trigger_name)
{
if (i == 0)
strcpy(trigger_name, "no event");
else if ((i >= 1) && (i <= MAX_TRIGGERS))
{
sprintf(trigger_name, "trigger%d", i);
}
else
strcpy(trigger_name, "UKNOWN");
}
/*
* convert integer to trigger ID
* this is needed because waypoint structure works with integer value at the moment
* next update to waypoint stuct should change it so we can use trigger ID right away
*/
TriggerId IntToTriggerId(int i)
{
if (i == 1)
return TriggerId::trigger1;
else if (i == 2)
return TriggerId::trigger2;
else if (i == 3)
return TriggerId::trigger3;
else if (i == 4)
return TriggerId::trigger4;
else if (i == 5)
return TriggerId::trigger5;
else if (i == 6)
return TriggerId::trigger6;
else if (i == 7)
return TriggerId::trigger7;
else if (i == 8)
return TriggerId::trigger8;
else
return TriggerId::trigger_none;
}
/*
* convert trigger ID to integer
*/
int TriggerIdToInt(TriggerId triggerId)
{
if (triggerId == TriggerId::trigger1)
return 1;
else if (triggerId == TriggerId::trigger2)
return 2;
else if (triggerId == TriggerId::trigger3)
return 3;
else if (triggerId == TriggerId::trigger4)
return 4;
else if (triggerId == TriggerId::trigger5)
return 5;
else if (triggerId == TriggerId::trigger6)
return 6;
else if (triggerId == TriggerId::trigger7)
return 7;
else if (triggerId == TriggerId::trigger8)
return 8;
else
return 0;
}
/*
* connect the game message to certain trigger slot
*/
int WaypointAddTriggerEvent(const char *trigger_name, const char *trigger_message)
{
// incorrect input
if ((strlen(trigger_name) < 1) || (strlen(trigger_message) < 1))
return -1;
const int index = TriggerNameToIndex(trigger_name);
// invalid trigger name
if (index == -1)
return -2;
// already used
if (trigger_gamestate[index].GetUsed())
return -3;
// the max length of the trigger event message is 256
// so inform the user if he is trying to use longer one
if (strlen(trigger_message) > 255)
return -4;
// store the message
strcpy(trigger_events[index].message, trigger_message);
trigger_gamestate[index].SetUsed(true);
return 1;
}
/*
* free the trigger slot
*/
int WaypointDeleteTriggerEvent(const char *trigger_name)
{
// incorrect input
if (strlen(trigger_name) < 1)
return -1;
const int index = TriggerNameToIndex(trigger_name);
// invalid trigger name
if (index == -1)
return -2;
// already free
if (!trigger_gamestate[index].GetUsed())
return -3;
// delete the message
trigger_events[index].message[0] = '\0';
trigger_gamestate[index].SetUsed(false);
return 1;
}
/*
* connect appropriate trigger message to nearest trigger waypoint
* the state argument can be "on" or "off" and determines if this message
* will trigger the waypoint on or off
*/
int WaypointConnectTriggerEvent(edict_t *pEntity, const char *trigger_name, const char *state)
{
// incorrect input
if (strlen(trigger_name) < 1)
return -1;
bool trigger_on;
int index = TriggerNameToIndex(trigger_name);
// invalid trigger name
if (index == -1)
return -2;
// find if this message will trigger it on or off
if ((strlen(state) < 1) || (FStrEq(state, "on")))
trigger_on = TRUE;
else if (FStrEq(state, "off"))
trigger_on = FALSE;
else
return -3;
index = WaypointFindNearestType(pEntity, 50.0, W_FL_TRIGGER);
// no trigger waypoint around
if (index == -1)
return -4;
// connect the trigger message to this waypoint
if (trigger_on)
waypoints[index].trigger_event_on = TriggerIdToInt(TriggerNameToId(trigger_name));
else
waypoints[index].trigger_event_off = TriggerIdToInt(TriggerNameToId(trigger_name));
return 1;
}
/*
* remove appropriate trigger message from nearest trigger waypoint based on the state argument
*/
int WaypointRemoveTriggerEvent(edict_t *pEntity, const char *state)
{
bool trigger_on;
// find if it is a trigger on or off that's going to be removed
if (strlen(state) < 1)
return -1;
else if (FStrEq(state, "on"))
trigger_on = TRUE;
else if (FStrEq(state, "off"))
trigger_on = FALSE;
else
return -2;
const int index = WaypointFindNearestType(pEntity, 50.0, W_FL_TRIGGER);
// no trigger waypoint around
if (index == -1)
return -3;
// reset appropriate trigger event for this waypoint
if (trigger_on)
waypoints[index].trigger_event_on = TriggerIdToInt(TriggerId::trigger_none);
else
waypoints[index].trigger_event_off = TriggerIdToInt(TriggerId::trigger_none);
return 1;