-
Notifications
You must be signed in to change notification settings - Fork 5
/
waypoint.cpp
3412 lines (2969 loc) · 98.7 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
// ####################################
// # #
// # Ping of Death - Bot #
// # by #
// # Markus Klinge aka Count Floyd #
// # #
// ####################################
//
// Started from the HPB-Bot Alpha Source
// by Botman so Credits for a lot of the basic
// HL Server/Client Stuff goes to him
//
// bot_navigate.cpp
//
// Features the Waypoint Code (Editor)
#include "bot_globals.h"
#include <vector>
#ifdef __linux__
#define _snprintf_s snprintf
#endif
// adds a path between 2 waypoints
void WaypointAddPath(short int add_index, short int path_index, float fDistance)
{
PATH* p;
int i;
g_bWaypointsChanged = TRUE;
p = paths[add_index];
// Don't allow Paths get connected to the same Waypoint
if (add_index == path_index)
{
UTIL_ServerPrint("Denied path creation from %d to %d (same waypoint)\n", add_index, path_index);
return;
}
// Don't allow Paths get connected twice
for (i = 0; i < MAX_PATH_INDEX; i++)
if (p->index[i] == path_index)
{
UTIL_ServerPrint("Denied path creation from %d to %d (path already exists)\n", add_index, path_index);
return;
}
// Check for free space in the Connection indices
for (i = 0; i < MAX_PATH_INDEX; i++)
{
if (p->index[i] == -1)
{
p->index[i] = path_index;
p->distance[i] = (int)fabs(fDistance); // KWo - to remove warning
UTIL_ServerPrint("Path added from %d to %d\n", add_index, path_index);
return;
}
}
// There wasn't any free space. Try exchanging it with a long-distance path
for (i = 0; i < MAX_PATH_INDEX; i++)
{
if (p->distance[i] > fabs(fDistance))
{
UTIL_ServerPrint("Path added from %d to %d\n", add_index, path_index);
p->index[i] = path_index;
p->distance[i] = (int)fabs(fDistance); // KWo - to remove warning
return;
}
}
return;
}
// find the nearest waypoint to the player and return the index (-1 if not found)
int WaypointFindNearest(void)
{
int i;
int index = -1; // KWo - to remove warning uninitialised
float distance;
float min_distance;
// find the nearest waypoint...
min_distance = 9999.0f;
for (i = 0; i < g_iNumWaypoints; i++)
{
distance = (paths[i]->origin - pHostEdict->v.origin).Length();
if (distance < min_distance)
{
index = i;
min_distance = distance;
}
}
// if not close enough to a waypoint then just return
if (min_distance > 50.0f)
return -1;
return index;
}
// Shows waypoints under WP editor
void WaypointDrawBeam(Vector start, Vector end, int width, int red, int green, int blue)
{
if (FNullEnt(pHostEdict))
return;
MESSAGE_BEGIN(MSG_ONE_UNRELIABLE, SVC_TEMPENTITY, NULL, pHostEdict);
WRITE_BYTE(TE_BEAMPOINTS);
WRITE_COORD(start.x);
WRITE_COORD(start.y);
WRITE_COORD(start.z);
WRITE_COORD(end.x);
WRITE_COORD(end.y);
WRITE_COORD(end.z);
WRITE_SHORT(g_pSpriteTexture);
WRITE_BYTE(1); // framestart
WRITE_BYTE(10); // framerate
WRITE_BYTE(3); // life in 0.1's KWo - 28.02.2008
WRITE_BYTE(width); // width
WRITE_BYTE(0); // noise
WRITE_BYTE(red); // r, g, b
WRITE_BYTE(green); // r, g, b
WRITE_BYTE(blue); // r, g, b
WRITE_BYTE(255); // brightness
WRITE_BYTE(0); // speed
MESSAGE_END();
return;
}
// Checks if the node is reachable (for "waypoint add" function)
bool WaypointNodeReachable(int i_src, int i_dest) // KWo - 06.01.2008
{
TraceResult tr;
float height, last_height;
Vector v_dest, v_src;
IGNORE_MONSTERS igm; // KWo - 13.01.2008
if (i_src < 0 || i_src > g_iNumWaypoints || i_dest < 0 || i_dest > g_iNumWaypoints)
return false;
v_dest = paths[i_dest]->origin; // KWo - 06.01.2008
v_src = paths[i_src]->origin; // KWo - 06.01.2008
if (paths[i_src]->flags & W_FL_LADDER) // KWo - 06.01.2008
v_src = v_src + Vector(0.0f, 0.0f, 16.0f);
float distance = (v_dest - v_src).Length();
// is the destination NOT close enough?
if (g_bWaypointOn) // 13.01.2008
{
if (distance > g_fAutoPathMaxDistance || g_bAutoWaypoint && distance > 160.0f) // KWo - 09.11.2007
return false;
}
igm = g_bWaypointOn ? ignore_monsters : dont_ignore_monsters; // KWo - 13.01.2008
// check if we go through a func_illusionary, in which case return FALSE
TRACE_HULL(v_src, v_dest, igm, head_hull, pHostEdict, &tr); // KWo - 13.01.2008
if (!FNullEnt(tr.pHit))
{
if (strcmp("func_illusionary", STRING(tr.pHit->v.classname)) == 0)
return false; // don't add pathwaypoints through func_illusionaries
}
// check if this waypoint is "visible"...
TRACE_LINE(v_src, v_dest, igm, pHostEdict, &tr); // KWo - 13.01.2008
// if waypoint is visible from current position (even behind head)...
if (tr.flFraction >= 1.0f || strncmp("func_door", STRING(tr.pHit->v.classname), 9) == 0)
{
// If it's a door check if nothing blocks behind
if (strncmp("func_door", STRING(tr.pHit->v.classname), 9) == 0)
{
Vector vDoorEnd = tr.vecEndPos;
TRACE_LINE(vDoorEnd, v_dest, igm, tr.pHit, &tr); // KWo - 13.01.2008
if (tr.flFraction < 1.0f)
return false;
}
// check for special case of both waypoints being in water...
if (POINT_CONTENTS(v_src) == CONTENTS_WATER
&& POINT_CONTENTS(v_dest) == CONTENTS_WATER)
return true; // then they're reachable each other
// check for special case of waypoint being suspended in mid-air...
// is dest waypoint higher than src? (45 is max jump height)
if (v_dest.z > v_src.z + 45.0f)
{
Vector v_new_src = v_dest;
Vector v_new_dest = v_dest;
v_new_dest.z = v_new_dest.z - 50.0f; // straight down 50 units
TRACE_LINE(v_new_src, v_new_dest, igm, pHostEdict, &tr); // KWo - 13.01.2008
// check if we didn't hit anything, if not then it's in mid-air
if (tr.flFraction >= 1.0f)
return false; // can't reach this one
}
// check if distance to ground drops more than step height at points
// between source and destination...
Vector v_direction = (v_dest - v_src).Normalize(); // 1 unit long
Vector v_check = v_src;
Vector v_down = v_src;
distance = (v_dest - v_check).Length(); // distance from goal
if (paths[i_src]->flags & W_FL_LADDER && distance > 32.0f) // KWo - 06.01.2008
{
v_check = v_check + v_direction * 32.0f;
v_down = v_check;
}
v_down.z = v_down.z - 1000.0f; // straight down 1000 units
TRACE_LINE(v_check, v_down, igm, pHostEdict, &tr); // KWo - 13.01.2008
last_height = tr.flFraction * 1000.0f; // height from ground
while (distance > 10.0f)
{
// move 10 units closer to the goal...
v_check = v_check + v_direction * 10.0f;
v_down = v_check;
v_down.z = v_down.z - 1000.0f; // straight down 1000 units
TRACE_LINE(v_check, v_down, igm, pHostEdict, &tr); // KWo - 13.01.2008
height = tr.flFraction * 1000.0f; // height from ground
// is the current height greater than the step height?
if (height < last_height - 18.0f)
return false; // can't get there without jumping...
last_height = height;
distance = (v_dest - v_check).Length(); // distance from goal
}
return true;
}
return false;
}
// Calculates the Waypoint wayzone
void CalculateWaypointWayzone(void)
{
PATH* p;
Vector start;
Vector vRadiusEnd;
Vector v_direction;
TraceResult tr;
int iScanDistance;
float fRadCircle;
bool bWayBlocked;
int index;
int x;
index = WaypointFindNearest();
p = paths[index];
if (p->flags & W_FL_LADDER
|| p->flags & W_FL_GOAL
|| p->flags & W_FL_CAMP
|| p->flags & W_FL_RESCUE
|| p->flags & W_FL_CROUCH
|| g_bLearnJumpWaypoint)
{
p->Radius = 0;
return;
}
for (x = 0; x < MAX_PATH_INDEX; x++)
{
if (p->index[x] != -1 && paths[p->index[x]]->flags & W_FL_LADDER)
{
p->Radius = 0;
return;
}
}
bWayBlocked = FALSE;
for (iScanDistance = 32; iScanDistance < 128; iScanDistance += 16)
{
start = p->origin;
MAKE_VECTORS(g_vecZero);
vRadiusEnd = start + gpGlobals->v_forward * iScanDistance;
v_direction = vRadiusEnd - start;
v_direction = UTIL_VecToAngles(v_direction);
p->Radius = iScanDistance;
for (fRadCircle = 0.0f; fRadCircle < 360.0f; fRadCircle += 20)
{
MAKE_VECTORS(v_direction);
vRadiusEnd = start + gpGlobals->v_forward * iScanDistance;
TRACE_LINE(start, vRadiusEnd, ignore_monsters, pHostEdict, &tr);
if (tr.flFraction < 1.0f)
{
if (strncmp("func_door", STRING(tr.pHit->v.classname), 9) == 0)
{
p->Radius = 0;
bWayBlocked = TRUE;
break;
}
bWayBlocked = TRUE;
p->Radius -= 16;
break;
}
vRadiusEnd.z += 34;
TRACE_LINE(start, vRadiusEnd, ignore_monsters, pHostEdict, &tr);
if (tr.flFraction < 1.0f)
{
bWayBlocked = TRUE;
p->Radius -= 16;
break;
}
v_direction.y += fRadCircle;
UTIL_ClampAngle(&v_direction.y);
}
if (bWayBlocked)
break;
}
p->Radius -= 16;
if (p->Radius < 0)
p->Radius = 0;
return;
}
// adds a new waypoint
void WaypointAdd(int wpt_type)
{
int index, i, j, k;
float distance;
float min_distance;
float fLongestDist = 0.0f;
PATH* pPath, * pPrev; // KWo - 06.01.2008 changed names...
bool bPlaceNew = TRUE;
Vector vecNewWaypoint;
TraceResult tr;
int iDestIndex;
int flags;
int TempInd[MAX_PATH_INDEX];
int TempDistance[MAX_PATH_INDEX];
float TempAngleY[MAX_PATH_INDEX];
int Temp_ind;
int Temp_dist;
float Temp_ang;
Vector Temp_vector_origin; // KWo - 06.01.2008
Vector vec_dir_ang;
int iPathIndexOfLongest = -1;
bool bLongestCalculated = FALSE;
bool bSorting;
g_bWaypointsChanged = TRUE;
vecNewWaypoint = pHostEdict->v.origin;
index = WaypointFindNearest();
pPath = NULL; // KWo - 11.04.2010
if (wpt_type == WAYPOINT_ADD_CAMP_START)
{
if (index != -1)
{
pPath = paths[index];
if (pPath->flags & W_FL_CAMP)
{
pPath->fcampstartx = pHostEdict->v.v_angle.x;
pPath->fcampstarty = pHostEdict->v.v_angle.y;
EMIT_SOUND_DYN2(pHostEdict, CHAN_WEAPON, "common/wpn_hudon.wav", VOL_NORM, ATTN_NORM, 0, PITCH_NORM);
return;
}
}
}
else if (wpt_type == WAYPOINT_ADD_CAMP_END)
{
if (index != -1)
{
pPath = paths[index];
if (!(pPath->flags & W_FL_CAMP))
{
UTIL_ServerPrint("This is no Camping Waypoint !\n");
return;
}
pPath->fcampendx = pHostEdict->v.v_angle.x;
pPath->fcampendy = pHostEdict->v.v_angle.y;
}
EMIT_SOUND_DYN2(pHostEdict, CHAN_WEAPON, "common/wpn_hudon.wav", VOL_NORM, ATTN_NORM, 0, PITCH_NORM);
return;
}
else if (wpt_type == WAYPOINT_ADD_JUMP_START)
{
if (index != -1)
{
distance = (paths[index]->origin - pHostEdict->v.origin).Length();
if (distance < 50.0f)
{
bPlaceNew = FALSE;
pPath = paths[index];
pPath->origin = (pPath->origin + g_vecLearnPos) / 2;
}
}
else
vecNewWaypoint = g_vecLearnPos;
}
else if (wpt_type == WAYPOINT_ADD_JUMP_END)
{
if (index != -1)
{
distance = (paths[index]->origin - pHostEdict->v.origin).Length();
if (distance < 50.0f)
{
bPlaceNew = FALSE;
pPath = paths[index];
flags = 0;
for (i = 0; i < MAX_PATH_INDEX; i++)
flags += pPath->connectflag[i];
if (flags == 0)
pPath->origin = (pPath->origin + pHostEdict->v.origin) / 2;
}
}
}
if (bPlaceNew)
{
if (g_iNumWaypoints + 1 >= MAX_WAYPOINTS)
{
UTIL_HostPrint("Max. Waypoint reached! Can't add Waypoint!\n");
EMIT_SOUND_DYN2(pHostEdict, CHAN_WEAPON, "debris/bustglass1.wav", VOL_NORM, ATTN_NORM, 0, PITCH_NORM);
return;
}
index = 0;
// find the next available slot for the new waypoint...
pPath = paths[0];
pPrev = NULL;
// find an empty slot for new path_index...
while (pPath != NULL)
{
pPrev = pPath;
pPath = pPath->next;
index++;
}
paths[index] = new PATH;
if (paths[index] == NULL)
return; // ERROR ALLOCATING MEMORY!!!
pPath = paths[index];
if (pPrev)
pPrev->next = pPath;
// increment total number of waypoints
g_iNumWaypoints++;
pPath->iPathNumber = index;
pPath->flags = 0;
// store the origin (location) of this waypoint
pPath->origin = vecNewWaypoint;
pPath->fcampstartx = 0.0f;
pPath->fcampstarty = 0.0f;
pPath->fcampendx = 0.0f;
pPath->fcampendy = 0.0f;
for (i = 0; i < MAX_PATH_INDEX; i++)
{
pPath->index[i] = -1;
pPath->distance[i] = 0;
pPath->connectflag[i] = 0;
pPath->vecConnectVel[i] = g_vecZero;
}
pPath->next = NULL;
// store the last used waypoint for the auto waypoint code...
g_vecLastWaypoint = pHostEdict->v.origin;
}
if (wpt_type == WAYPOINT_ADD_JUMP_START)
g_iLastJumpWaypoint = index;
else if (wpt_type == WAYPOINT_ADD_JUMP_END)
{
distance = (paths[g_iLastJumpWaypoint]->origin - pHostEdict->v.origin).Length();
WaypointAddPath(g_iLastJumpWaypoint, index, distance);
for (i = 0; i < MAX_PATH_INDEX; i++)
{
if (paths[g_iLastJumpWaypoint]->index[i] == index)
{
paths[g_iLastJumpWaypoint]->connectflag[i] |= C_FL_JUMP;
paths[g_iLastJumpWaypoint]->vecConnectVel[i] = g_vecLearnVelocity;
break;
}
}
CalculateWaypointWayzone();
EMIT_SOUND_DYN2(pHostEdict, CHAN_WEAPON, "weapons/xbow_hit1.wav", VOL_NORM, ATTN_NORM, 0, PITCH_NORM);
return;
}
if (pPath == NULL)
return;
if (pHostEdict->v.flags & FL_DUCKING)
pPath->flags |= W_FL_CROUCH; // set a crouch waypoint
// *******************************************************
// look for buttons, lift, ammo, flag, health, armor, etc.
// *******************************************************
if (wpt_type == WAYPOINT_ADD_TERRORIST)
pPath->flags |= W_FL_TERRORIST;
else if (wpt_type == WAYPOINT_ADD_COUNTER)
pPath->flags |= W_FL_COUNTER;
else if (wpt_type == WAYPOINT_ADD_LADDER)
pPath->flags |= W_FL_LADDER;
else if (wpt_type == WAYPOINT_ADD_RESCUE)
pPath->flags |= W_FL_RESCUE;
else if (wpt_type == WAYPOINT_ADD_CAMP_START)
{
pPath->flags |= W_FL_CAMP;
pPath->fcampstartx = pHostEdict->v.v_angle.x;
pPath->fcampstarty = pHostEdict->v.v_angle.y;
}
else if (wpt_type == WAYPOINT_ADD_GOAL)
pPath->flags |= W_FL_GOAL;
// Ladder waypoints need careful connections
if (wpt_type == WAYPOINT_ADD_LADDER)
{
min_distance = 9999.0f;
iDestIndex = -1;
// calculate all the paths to this new waypoint
for (i = 0; i < g_iNumWaypoints; i++)
{
if (i == index)
continue; // skip the waypoint that was just added
// Other ladder waypoints should connect to this
if (paths[i]->flags & W_FL_LADDER)
{
// check if the waypoint is reachable from the new one
TRACE_LINE(vecNewWaypoint, paths[i]->origin, ignore_monsters, pHostEdict, &tr);
if (tr.flFraction == 1.0f
&& fabs(vecNewWaypoint.x - paths[i]->origin.x) < 32.0f
&& fabs(vecNewWaypoint.y - paths[i]->origin.y) < 32.0f
&& fabs(vecNewWaypoint.z - paths[i]->origin.z) < g_fAutoPathMaxDistance)
{
distance = (paths[i]->origin - vecNewWaypoint).Length();
WaypointAddPath(index, i, distance);
WaypointAddPath(i, index, distance);
}
}
else
{
// check if the waypoint is reachable from the new one
if (WaypointNodeReachable(index, i)
|| WaypointNodeReachable(i, index) && fabs(vecNewWaypoint.z + 16.0f - paths[i]->origin.z) < 64.0f) // KWo - 16.12.2007 - I don't like stupid connections...
{
distance = (paths[i]->origin - vecNewWaypoint).Length();
if (distance < min_distance)
{
iDestIndex = i;
min_distance = distance;
}
}
}
}
if (iDestIndex > -1 && iDestIndex < g_iNumWaypoints)
{
// check if the waypoint is reachable from the new one (one-way)
if (WaypointNodeReachable(index, iDestIndex))
{
distance = (paths[iDestIndex]->origin - vecNewWaypoint).Length();
WaypointAddPath(index, iDestIndex, distance);
}
// check if the new one is reachable from the waypoint (other way)
if (WaypointNodeReachable(iDestIndex, index))
{
distance = (paths[iDestIndex]->origin - vecNewWaypoint).Length();
WaypointAddPath(iDestIndex, index, distance);
}
}
}
else // KWo - 07.05.2013 - rewritten again the code to prevent making stupid connections
{
// calculate all the paths to this new waypoint
j = 0;
for (k = 0; k < MAX_PATH_INDEX; k++)
{
TempInd[k] = -1;
TempDistance[k] = 9999.0f;
TempAngleY[k] = 360.0f;
}
for (i = 0; i < g_iNumWaypoints; i++) // this loop is to find 8 closests waypoints to just added WP...
{
if (i == index)
continue; // skip the waypoint that was just added
// check if the waypoint is reachable from the new one (one-way)
if (WaypointNodeReachable(index, i))
{
distance = (paths[i]->origin - vecNewWaypoint).Length();
if (j < MAX_PATH_INDEX)
{
TempInd[j] = i;
TempDistance[j] = (int)distance;
if (fLongestDist < distance)
{
fLongestDist = distance;
iPathIndexOfLongest = j;
bLongestCalculated = TRUE;
}
j++;
}
else
{
if (bLongestCalculated && iPathIndexOfLongest > -1) // we are trying to find 8 closest waypoints to connect
{
if (distance < fLongestDist)
{
fLongestDist = 0.0f;
TempInd[iPathIndexOfLongest] = i; // here we are replacing the longest path by the closer one...
TempDistance[iPathIndexOfLongest] = (int)distance;
bLongestCalculated = FALSE;
fLongestDist = 0.0f;
for (k = 0; k < MAX_PATH_INDEX; k++)
{
if (TempDistance[k] > (int)fLongestDist)
{
fLongestDist = (float)TempDistance[k];
iPathIndexOfLongest = k;
bLongestCalculated = TRUE;
}
}
}
}
}
}
}
do // this code sorts paths from the closest WP to the farest away one...
{
bSorting = FALSE;
for (k = 0; k < MAX_PATH_INDEX - 1; k++)
{
if (TempDistance[k] > TempDistance[k + 1])
{
Temp_ind = TempInd[k + 1];
Temp_dist = TempDistance[k + 1];
TempInd[k + 1] = TempInd[k];
TempDistance[k + 1] = TempDistance[k];
TempInd[k] = Temp_ind;
TempDistance[k] = Temp_dist;
bSorting = TRUE;
}
}
} while (bSorting);
if (g_b_DebugWpEdit)
{
for (k = 0; k < MAX_PATH_INDEX; k++)
{
if (j > 0)
{
ALERT(at_logged, "[DEBUG] WaypointAdd - %d closest WPs to added %d index are:\n",
j - 1, index);
if (TempInd[k] > -1)
ALERT(at_logged, "[DEBUG] WaypointAdd - WP %d (distance = %d).\n",
TempInd[k], TempDistance[k]);
}
}
}
for (k = 0; k < MAX_PATH_INDEX; k++)
{
if (TempInd[k] > -1)
{
// now calculate angles related to the angle created by the added WP and the closest connected one
vec_dir_ang = UTIL_VecToAngles(paths[TempInd[k]]->origin - vecNewWaypoint)
- UTIL_VecToAngles(paths[TempInd[0]]->origin - vecNewWaypoint);
if (vec_dir_ang.y < 0.0f)
vec_dir_ang.y += 360.0f;
TempAngleY[k] = vec_dir_ang.y;
}
}
do // this code sorts the paths from the lowest to the highest angle...
{
bSorting = FALSE;
for (k = 0; k < MAX_PATH_INDEX - 1; k++)
{
if (TempAngleY[k] > TempAngleY[k + 1])
{
Temp_ind = TempInd[k + 1];
Temp_dist = TempDistance[k + 1];
Temp_ang = TempAngleY[k + 1];
TempInd[k + 1] = TempInd[k];
TempDistance[k + 1] = TempDistance[k];
TempAngleY[k + 1] = TempAngleY[k];
TempInd[k] = Temp_ind;
TempDistance[k] = Temp_dist;
TempAngleY[k] = Temp_ang;
bSorting = TRUE;
}
}
} while (bSorting);
if (g_b_DebugWpEdit)
{
for (k = 0; k < MAX_PATH_INDEX; k++)
{
ALERT(at_logged, "[DEBUG] WaypointAdd - index %d - checked to connect with WP nr %d (angle = %.1f).\n",
index, TempInd[k], TempAngleY[k]);
}
}
Temp_ind = -1;
Temp_dist = 0;
Temp_ang = 0.0f;
for (k = 2; k < MAX_PATH_INDEX; k++)
{
testagain:
if (TempInd[k] == -1 || TempInd[k - 1] == -1 || TempInd[k - 2] == -1)
continue;
Temp_ind = TempInd[k]; // store the highest index which should be tested later...
Temp_dist = TempDistance[k];
Temp_ang = TempAngleY[k];
if (TempAngleY[k] - TempAngleY[k - 2] < 80.0f)
{
if (1.1 * (TempDistance[k] + TempDistance[k - 2]) / 2.0f < (float)TempDistance[k - 1]
&& !(paths[TempInd[k - 1]]->flags & W_FL_LADDER))
{
if (g_b_DebugWpEdit)
ALERT(at_logged, "[DEBUG] WaypointAdd - Prevent making a stupid connection (1) from index = %d to %d.\n",
index, TempInd[k - 1]);
TempInd[k - 1] = -1; // prevent making an unnecessary connection... :)
for (j = k - 1; j < MAX_PATH_INDEX - 1; j++)
{
TempInd[j] = TempInd[j + 1];
TempDistance[j] = TempDistance[j + 1];
TempAngleY[j] = TempAngleY[j + 1];
}
TempInd[MAX_PATH_INDEX - 1] = -1;
goto testagain;
}
}
}
if (Temp_ind > -1 && TempInd[0] > -1 && TempInd[1] > -1)
{
if (TempAngleY[1] - Temp_ang < 80.0f || 360.0f - (TempAngleY[1] - Temp_ang) < 80.0f)
{
if (1.1 * (TempDistance[1] + Temp_dist) / 2.0f < (float)TempDistance[0]
&& !(paths[TempInd[0]]->flags & W_FL_LADDER))
{
if (g_b_DebugWpEdit)
ALERT(at_logged, "[DEBUG] WaypointAdd - Prevent making a stupid connection (2) from index = %d to %d.\n",
index, TempInd[0]);
TempInd[0] = -1; // prevent making an unnecessary connection... :)
}
}
}
for (k = 0; k < MAX_PATH_INDEX; k++)
{
i = TempInd[k];
if (i > -1)
{
distance = (float)TempDistance[k];
WaypointAddPath(index, i, distance);
if (g_b_DebugWpEdit)
ALERT(at_logged, "[DEBUG] WaypointAdd - Adding path from index = %i to %i, distance = %i, angle = %.1f.\n",
index, i, TempDistance[k], TempAngleY[k]);
// check if the new one is reachable from the waypoint (other way)
if (WaypointNodeReachable(i, index))
{
distance = (paths[i]->origin - vecNewWaypoint).Length();
WaypointAddPath(i, index, distance);
}
}
}
WaypointCleanUnnessPaths(index);
}
CalculateWaypointWayzone();
EMIT_SOUND_DYN2(pHostEdict, CHAN_WEAPON, "weapons/xbow_hit1.wav", VOL_NORM, ATTN_NORM, 0, PITCH_NORM);
return;
}
// deletes an existing waypoint
void WaypointDelete(void)
{
PATH* pPrev = NULL; // KWo - 06.01.2008 changed name
PATH* pPath; // KWo - 06.01.2008 changed name
int wpt_index;
int i, ix;
if (g_iNumWaypoints < 1)
return;
wpt_index = WaypointFindNearest();
if (wpt_index < 0 || wpt_index >= g_iNumWaypoints)
{
UTIL_HostPrint("No Waypoint nearby!\n");
return;
}
if (paths[wpt_index] != NULL && wpt_index > 0)
pPrev = paths[wpt_index - 1];
// delete all references to Node
for (i = 0; i < g_iNumWaypoints; i++)
{
pPath = paths[i];
for (ix = 0; ix < MAX_PATH_INDEX; ix++)
{
if (pPath->index[ix] == wpt_index)
{
pPath->index[ix] = -1; // unassign this path
pPath->connectflag[ix] = 0;
pPath->distance[ix] = 0;
pPath->vecConnectVel[ix] = g_vecZero;
}
}
}
// delete all connections to Node
for (i = 0; i < g_iNumWaypoints; i++)
{
pPath = paths[i];
// if Pathnumber bigger than deleted Node Number=Number -1
if (pPath->iPathNumber > wpt_index)
pPath->iPathNumber--;
for (ix = 0; ix < MAX_PATH_INDEX; ix++)
if (pPath->index[ix] > wpt_index)
pPath->index[ix]--;
}
// free deleted node
delete paths[wpt_index];
paths[wpt_index] = NULL;
// Rotate Path Array down
for (i = wpt_index; i < g_iNumWaypoints - 1; i++)
paths[i] = paths[i + 1];
if (pPrev)
pPrev->next = paths[wpt_index];
g_iNumWaypoints--;
EMIT_SOUND_DYN2(pHostEdict, CHAN_WEAPON, "weapons/mine_activate.wav", VOL_NORM, ATTN_NORM, 0, PITCH_NORM);
g_bWaypointsChanged = TRUE;
return;
}
// Remember a closest Waypoint
void WaypointCache(void)
{
int iNode;
iNode = WaypointFindNearest();
if (iNode == -1)
{
g_iCachedWaypoint = -1;
UTIL_HostPrint("Cache cleared (no Waypoint nearby!)\n");
return;
}
g_iCachedWaypoint = iNode;
UTIL_HostPrint("Waypoint #%d has been put into memory\n", g_iCachedWaypoint);
return;
}
// Move a remembered Waypoint
void WaypointMoveToPosition(void) // KWo - 09.11.2007
{
if (g_iCachedWaypoint == -1)
{
UTIL_HostPrint("Nothing to Move (no Waypoint nearby!)\n");
return;
}
if (fabs((paths[g_iCachedWaypoint]->origin - pHostEdict->v.origin).Length()) > 256.0f)
{
UTIL_HostPrint("Waypoint #%d couldn't be moved to this postion (too far away!)\n");
return;
}
paths[g_iCachedWaypoint]->origin = pHostEdict->v.origin;
g_bWaypointsChanged = TRUE;
UTIL_HostPrint("Waypoint #%d has been moved to the new position\n", g_iCachedWaypoint);
g_iCachedWaypoint = -1;
return;
}
// change a waypoint's radius
void WaypointChangeRadius(float radius)
{
int wpt_index;
wpt_index = WaypointFindNearest();
if (wpt_index < 0 || wpt_index >= g_iNumWaypoints)
{
UTIL_HostPrint("No Waypoint nearby!\n");
return;
}
paths[wpt_index]->Radius = radius;
EMIT_SOUND_DYN2(pHostEdict, CHAN_WEAPON, "common/wpn_hudon.wav", VOL_NORM, ATTN_NORM, 0, PITCH_NORM);
g_bWaypointsChanged = TRUE;
return;
}
// switch a waypoint's flag on/off
void WaypointChangeFlag(int flag, char status)
{
int wpt_index;
wpt_index = WaypointFindNearest();
if (wpt_index < 0 || wpt_index >= g_iNumWaypoints)
{
UTIL_HostPrint("No Waypoint nearby!\n");
return;
}
if (status == FLAG_SET)
paths[wpt_index]->flags |= flag; // set flag
else if (status == FLAG_CLEAR)
paths[wpt_index]->flags &= ~flag; // reset flag
else if (status == FLAG_TOGGLE)
paths[wpt_index]->flags ^= flag; // Switch flag on/off (XOR it)
EMIT_SOUND_DYN2(pHostEdict, CHAN_WEAPON, "common/wpn_hudon.wav", VOL_NORM, ATTN_NORM, 0, PITCH_NORM);
g_bWaypointsChanged = TRUE;
return;
}
int WaypointLookAt(void) // KWo - 04.10.2006
{
// find the waypoint the user is pointing at
int j;
float distance;
Vector vToWaypoint;
Vector vWaypointBound;
float fCone1, fCone2, fCone3, fCone4, fCone5; // KWo - 11.02.2008
float fConeMax = 0.0f; // KWo - 11.02.2008
g_iPointedWpIndex = -1;
for (j = 0; j < g_iNumWaypoints; j++)
{