-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathNodeMachine.cpp
4810 lines (4021 loc) · 176 KB
/
NodeMachine.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
/**
* RealBot : Artificial Intelligence
* Version : Work In Progress
* Author : Stefan Hendriks
* Url : http://realbot.bots-united.com
**
* DISCLAIMER
*
* History, Information & Credits:
* RealBot is based partially upon the HPB-Bot Template #3 by Botman
* Thanks to Ditlew (NNBot), Pierre Marie Baty (RACCBOT), Tub (RB AI PR1/2/3)
* Greg Slocum & Shivan (RB V1.0), Botman (HPB-Bot) and Aspirin (JOEBOT). And
* everybody else who helped me with this project.
* Storage of Visibility Table using BITS by Cheesemonster.
*
* Some portions of code are from other bots, special thanks (and credits) go
* to (in no specific order):
*
* Pierre Marie Baty
* Count-Floyd
*
* !! BOTS-UNITED FOREVER !!
*
* This project is open-source, it is protected under the GPL license;
* By using this source-code you agree that you will ALWAYS release the
* source-code with your project.
*
**/
/**
* NODE MACHINE
* COPYRIGHTED BY STEFAN HENDRIKS (C) 2003-2004
**/
#include <string.h>
#include <extdll.h>
#include <dllapi.h>
#include <meta_api.h>
#include <entity_state.h>
// malloc stuff?
#include "stdlib.h"
// ---
#include "bot.h"
#include "IniParser.h"
#include "bot_weapons.h"
#include "game.h"
#include "bot_func.h"
#include "NodeMachine.h"
tNodestar astar_list[MAX_NODES];
const Vector &INVALID_VECTOR = Vector(9999, 9999, 9999);
extern edict_t *pHostEdict;
extern cGame Game;
extern cBot bots[32];
extern int draw_nodepath;
//---------------------------------------------------------
//CODE: CHEESEMONSTER
int // BERKED
cNodeMachine::GetVisibilityFromTo(int iFrom, int iTo) {
// prevent negative indexes on iVisChecked below -- BERKED
if (iFrom < 0 || iFrom > MAX_NODES || iTo < 0 || iTo > MAX_NODES) {
return VIS_INVALID;
}
// -- BY STEFAN --
if (iVisChecked[iFrom] == 0)
return VIS_UNKNOWN; // we have no clue
// -- END --
// was int
// work out the position
long iPosition = (iFrom * MAX_NODES) + iTo;
long iByte = (int) (iPosition / 8);
unsigned int iBit = iPosition % 8;
if (iByte < g_iMaxVisibilityByte) {
// Get the Byte that this is in
unsigned char *ToReturn = (cVisTable + iByte);
// get the bit in the byte
return ((*ToReturn & (1 << iBit)) > 0) ? VIS_VISIBLE : VIS_BLOCKED; // BERKED
}
return VIS_BLOCKED; // BERKED
}
void
cNodeMachine::SetVisibilityFromTo(int iFrom, int iTo, bool bVisible) {
// prevent negative indexes on iVisChecked below, fixing SEGV -- BERKED
if (iFrom < 0 || iFrom > MAX_NODES || iTo < 0 || iTo > MAX_NODES) {
return;
}
// -- STEFAN --
iVisChecked[iFrom] = 1; // WE HAVE CHECKED THIS ONE
// -- END --
// was int
long iPosition = (iFrom * MAX_NODES) + iTo;
long iByte = (int) (iPosition / 8);
unsigned int iBit = iPosition % 8;
if (iByte < g_iMaxVisibilityByte) {
unsigned char *ToChange = (cVisTable + iByte);
if (bVisible)
*ToChange |= (1 << iBit);
else
*ToChange &= ~(1 << iBit);
}
}
void cNodeMachine::ClearVisibilityTable(void) {
if (cVisTable) {
memset(cVisTable, 0, g_iMaxVisibilityByte);
}
}
void cNodeMachine::FreeVisibilityTable(void) {
if (cVisTable) {
free(cVisTable);
}
}
//---------------------------------------------------------
// Initialization of node machine
void cNodeMachine::init() {
rblog("cNodeMachine::init() - START\n");
iMaxUsedNodes = 0;
for (int i = 0; i < MAX_NODES; i++) {
// --- nodes
Nodes[i].origin = Vector(9999, 9999, 9999);
for (int n = 0; n < MAX_NEIGHBOURS; n++)
Nodes[i].iNeighbour[n] = -1;
// No bits set
Nodes[i].iNodeBits = 0;
// --- info nodes
for (int d = 0; d < 2; d++) {
InfoNodes[i].fDanger[d] = 0.0;
}
}
// Init trouble
for (int t = 0; t < MAX_TROUBLE; t++) {
Troubles[t].iFrom = -1;
Troubles[t].iTo = -1;
Troubles[t].iTries = -1;
}
initGoals();
// Init paths
for (int p = 0; p < MAX_BOTS; p++)
path_clear(p);
// Init VisTable
for (int iVx = 0; iVx < MAX_NODES; iVx++) {
iVisChecked[iVx] = 0; // not checked yet
}
// CODE: From cheesemonster
unsigned long iSize = g_iMaxVisibilityByte;
//create a heap type thing...
FreeVisibilityTable(); // 16/07/04 - free it first
cVisTable = (unsigned char *) malloc(iSize);
memset(cVisTable, 0, iSize);
ClearVisibilityTable();
// END:
// Init Meredians
for (int iMx = 0; iMx < MAX_MEREDIANS; iMx++)
for (int iMy = 0; iMy < MAX_MEREDIANS; iMy++)
for (int iNode = 0; iNode < MAX_NODES_IN_MEREDIANS; iNode++)
Meredians[iMx][iMy].iNodes[iNode] = -1;
rblog("cNodeMachine::init() - END\n");
}
void cNodeMachine::initGoals() {
// Init goals
for (int g = 0; g < MAX_GOALS; g++) {
initGoal(g);
}
}
void cNodeMachine::initGoal(int g) {
Goals[g].iNode = -1;
Goals[g].pGoalEdict = NULL;
Goals[g].iType = GOAL_NONE;
Goals[g].index = g;
Goals[g].iChecked = 0;
Goals[g].iBadScore = 0; // no bad score at init
memset(Goals[g].name, 0, sizeof(Goals[g].name));
}
int cNodeMachine::GetTroubleIndexForConnection(int iFrom, int iTo) {
char msg[255];
// sprintf(msg, "GetTroubleIndexForConnection | from %d to %d\n", iFrom, iTo);
// rblog(msg);
// in case of invalid values, return -1 - no need to loop
if (iFrom < -1 || iFrom >= MAX_NODES) {
rblog("GetTroubleIndexForConnection | invalid iFrom\n");
return -1;
}
if (iTo < -1 || iTo >= MAX_NODES) {
rblog("GetTroubleIndexForConnection | invalid iTo\n");
return -1;
}
// seems to be valid, look for troubled connections
int index;
for (index = 0; index < MAX_TROUBLE; index++) {
if (Troubles[index].iFrom == iFrom &&
Troubles[index].iTo == iTo) {
memset(msg, 0, sizeof(msg));
sprintf(msg, "GetTroubleIndexForConnection | Found index [%d] for from %d to %d\n", index, iFrom, iTo);
rblog(msg);
// found troubled connection, return its index
return index;
}
}
// rblog("GetTroubleIndexForConnection | found no index matching from/to. Returning -1\n");
return -1;
}
/**
* Adds a 'troubled connection' to the list of troubled connections.
*
* @param iFrom
* @param iTo
* @return index of newly created index
*/
int cNodeMachine::AddTroubledConnection(int iFrom, int iTo) {
int existingIndex = GetTroubleIndexForConnection(iFrom, iTo);
if (existingIndex > -1)
return existingIndex; // already exists
int iNew = -1;
int t;
for (t = 0; t < MAX_TROUBLE; t++)
if (Troubles[t].iFrom < 0 ||
Troubles[t].iTo < 0) {
iNew = t;
break;
}
if (iNew < 0) {
return -1;
}
Troubles[iNew].iFrom = iFrom;
Troubles[iNew].iTo = iTo;
Troubles[iNew].iTries = 0;
return iNew;
}
bool cNodeMachine::hasAttemptedConnectionTooManyTimes(int index) {
if (index < 0) {
rblog("(trouble) hasAttemptedConnectionTooManyTimes | invalid index for hasAttemptedConnectionTooManyTimes()\n");
// deal with invalid connection
return false;
}
tTrouble &trouble = Troubles[index];
char msg[255];
sprintf(msg, "(trouble) hasAttemptedConnectionTooManyTimes | Connection %d (%d->%d) has %d tries.\n", index, trouble.iFrom, trouble.iTo, trouble.iTries);
rblog(msg);
if (trouble.iTries > 2) {
rblog("(trouble) hasAttemptedConnectionTooManyTimes | Connection has been tried too many times!\n");
// after 3 times we quit
return true;
}
return false;
}
/**
* Remembers that this is a bad connection, increases a counter. When counter exceeds max attempts, it will
* remove the connection. When it removes the connection, this method will return "false". If a bot
* may have another go with this connection, this method will return "true".
* @param iFrom
* @param iTo
* @return
*/
bool cNodeMachine::IncreaseAttemptsForTroubledConnectionOrRemoveIfExceeded(int iFrom, int iTo) {
int index = AddTroubledConnection(iFrom, iTo);
IncreaseAttemptsForTroubledConnection(index);
if (hasAttemptedConnectionTooManyTimes(index)) {
rblog("(trouble) IncreaseAttemptsForTroubledConnectionOrRemoveIfExceeded | a troubled connection - tried too many times!\n");
// remove connection
if (!removeConnection(iFrom, iTo)) {
rblog("(trouble) IncreaseAttemptsForTroubledConnectionOrRemoveIfExceeded | for some reason the connection was not removed!?\n");
} else {
rblog("(trouble) IncreaseAttemptsForTroubledConnectionOrRemoveIfExceeded | the connection is removed!\n");
}
return false;
} else {
rblog("(trouble) IncreaseAttemptsForTroubledConnectionOrRemoveIfExceeded | may attempt another time\n");
return true;
}
}
void cNodeMachine::IncreaseAttemptsForTroubledConnection(int index) {
if (index < 0 || index >= MAX_TROUBLE) return;
char msg[255];
memset(msg, 0, sizeof(msg));
sprintf(msg, "(trouble) IncreaseAttemptsForTroubledConnection | Increasing trouble for connection [%d]\n", index);
rblog(msg);
Troubles[index].iTries++;
tTrouble &trouble = Troubles[index];
memset(msg, 0, sizeof(msg));
sprintf(msg, "(trouble) IncreaseAttemptsForTroubledConnection | Connection %d (%d->%d) has %d tries.\n", index, trouble.iFrom, trouble.iTo, trouble.iTries);
rblog(msg);
}
bool cNodeMachine::ClearTroubledConnection(int iFrom, int iTo) {
char msg[255];
sprintf(msg, "(trouble) NodeMachine::ClearTroubledConnection | %d -> %d - START\n", iFrom, iTo);
rblog(msg);
int index = GetTroubleIndexForConnection(iFrom, iTo);
memset(msg, 0, sizeof(msg));
sprintf(msg, "(trouble) NodeMachine::ClearTroubledConnection | %d -> %d has index %d\n", iFrom, iTo, index);
rblog(msg);
if (index < 0) {
// deal with scenario that this is a non-existing connection
return false;
}
// clear values
Troubles[index].iFrom = -1;
Troubles[index].iTo = -1;
Troubles[index].iTries = -1;
return true;
}
void cNodeMachine::path_clear(int botIndex) {
for (int nodeIndex = 0; nodeIndex < MAX_NODES; nodeIndex++) {
iPath[botIndex][nodeIndex] = -1;
}
}
// Return
Vector cNodeMachine::node_vector(int iNode) {
if (iNode > -1) {
return Nodes[iNode].origin;
}
return Vector(9999, 9999, 9999);
}
// Input: Vector, Output X and Y Meredians
void cNodeMachine::VectorToMeredian(Vector vOrigin, int *iX, int *iY) {
// Called for lookupt and for storing
float iCoordX = vOrigin.x + 8192.0; // map height (converts from - to +)
float iCoordY = vOrigin.y + 8192.0; // map width (converts from - to +)
// Meredian:
iCoordX = iCoordX / SIZE_MEREDIAN;
iCoordY = iCoordY / SIZE_MEREDIAN;
*iX = (int) iCoordX;
*iY = (int) iCoordY;
}
void cNodeMachine::AddToMeredian(int iX, int iY, int iNode) {
int index = -1;
for (int i = 0; i < MAX_NODES_IN_MEREDIANS; i++)
if (Meredians[iX][iY].iNodes[i] < 0) {
index = i;
break;
}
if (index < 0)
return;
Meredians[iX][iY].iNodes[index] = iNode;
}
// Does the node float?
bool cNodeMachine::node_float(Vector vOrigin, edict_t *pEdict) {
TraceResult tr;
Vector tr_end = vOrigin - Vector(0, 0, (ORIGIN_HEIGHT * 1.2));
//Using TraceHull to detect de_aztec bridge and other entities. (skill self)
//UTIL_TraceHull(vOrigin, tr_end, ignore_monsters, point_hull, pEdict->v.pContainingEntity, &tr);
if (pEdict)
UTIL_TraceHull(vOrigin, tr_end, ignore_monsters, human_hull,
pEdict->v.pContainingEntity, &tr);
else
UTIL_TraceHull(vOrigin, tr_end, ignore_monsters, human_hull, NULL,
&tr);
// if nothing hit: floating too high, return false
if (tr.flFraction >= 1.0)
return true; // floating
// *NOTE*: Actually this check should not be nescesary!
if (tr.flFraction < 1.0)
if (tr.pHit == pEdict)
return true;
// if inside wall: return false
if (tr.fStartSolid == 1) {
// todo: make sure the node does not start within this wall
return false; // not floating
}
return false; // not floating
}
// Does the node stand on a crate? or a steep slope?
bool cNodeMachine::node_on_crate(Vector vOrigin, edict_t *pEdict) {
TraceResult tr;
Vector tr_end = vOrigin - Vector(0, 0, (ORIGIN_HEIGHT * 1.2));
//Using TraceHull to detect de_aztec bridge and other entities. (skill self)
if (pEdict)
UTIL_TraceHull(vOrigin, tr_end, ignore_monsters, human_hull,
pEdict->v.pContainingEntity, &tr);
else
UTIL_TraceHull(vOrigin, tr_end, ignore_monsters, human_hull, NULL,
&tr);
// if nothing hit: floating too high, return false
if (tr.flFraction >= 1.0)
return false;
// hit something
if (tr.flFraction < 1.0) {
// thanks a million to PMB , so i know what the difference
// is between something straight (crate) and steep... although i have
// no clue yet how to compute these values myself.
if ( /*tr.vecPlaneNormal.z >= 0.7 && */ tr.vecPlaneNormal.z == 1.0) {
return true;
}
}
// impossible to reach
return false;
}
/**
* Find a node close to vOrigin within distance fDist. Ignoring any pEdict it hits.
* @param vOrigin
* @param fDist
* @param pEdict
* @return
*/
int cNodeMachine::getClosestNode(Vector vOrigin, float fDist, edict_t *pEdict) {
// REDO: Need faster method to find a node
// TOADD: For secure results all nodes should be checked to figure out the real
// 'closest' node.
// Use Meredians to search for nodes
// TODO: we should take care in the situation where we're at the 'edge' of such a meridian (subspace). So we should
// basicly take edging meridians as well when too close to the edge.
int iX, iY;
VectorToMeredian(vOrigin, &iX, &iY);
// no Meridian coordinates found, bail
if (iX < 0 || iY < 0) {
return -1;
}
float dist = fDist;
int iCloseNode = -1;
// Search in this meredian
for (int i = 0; i < MAX_NODES_IN_MEREDIANS; i++) {
if (Meredians[iX][iY].iNodes[i] < 0) continue; // skip invalid node indexes
int iNode = Meredians[iX][iY].iNodes[i];
// if (Nodes[iNode].origin.z > (vOrigin.z + 32)) continue; // do not pick nodes higher than us
float distanceFromTo = func_distance(vOrigin, Nodes[iNode].origin);
if (distanceFromTo < dist) {
dist = distanceFromTo;
TraceResult tr;
Vector nodeVector = Nodes[iNode].origin;
//Using TraceHull to detect de_aztec bridge and other entities.
//DONT_IGNORE_MONSTERS, we reached it only when there are no other bots standing in our way!
//UTIL_TraceHull(vOrigin, nodeVector, dont_ignore_monsters, point_hull, pEdict, &tr);
//UTIL_TraceHull(vOrigin, nodeVector, dont_ignore_monsters, human_hull, pEdict, &tr);
if (pEdict) {
UTIL_TraceHull(vOrigin, nodeVector, dont_ignore_monsters,
head_hull, pEdict->v.pContainingEntity,
&tr);
} else {
UTIL_TraceHull(vOrigin, nodeVector, dont_ignore_monsters,
head_hull, NULL, &tr);
}
// if nothing hit:
if (tr.flFraction >= 1.0) {
if (pEdict != NULL) {
if (FInViewCone(&nodeVector, pEdict) // in FOV
&& FVisible(nodeVector, pEdict)) {
iCloseNode = iNode;
} else {
iCloseNode = iNode;
}
}
}
}
}
return iCloseNode;
}
/**
* Find a node as far away from vOrigin but still within distance fDist. Ignoring any pEdict it hits.
* @param vOrigin
* @param fDist
* @param pEdict
* @return
*/
int cNodeMachine::getFurthestNode(Vector vOrigin, float fDist, edict_t *pEdict) {
// Use Meredians to search for nodes
// TODO: we should take care in the situation where we're at the 'edge' of such a meridian (subspace). So we should
// basicly take edging meridians as well when too close to the edge.
int iX, iY;
VectorToMeredian(vOrigin, &iX, &iY);
// no Meridian coordinates found, bail
if (iX < 0 || iY < 0) {
return -1;
}
float dist = 0;
int iFarNode = -1;
// Search in this meredian
for (int i = 0; i < MAX_NODES_IN_MEREDIANS; i++) {
if (Meredians[iX][iY].iNodes[i] < 0) continue; // skip invalid node indexes
int iNode = Meredians[iX][iY].iNodes[i];
// if (Nodes[iNode].origin.z > (vOrigin.z + 32)) continue; // do not pick nodes higher than us
float distanceFromTo = func_distance(vOrigin, Nodes[iNode].origin);
if (distanceFromTo < fDist && // within range
distanceFromTo > dist) { // but furthest so far
dist = distanceFromTo;
TraceResult tr;
Vector nodeVector = Nodes[iNode].origin;
//Using TraceHull to detect de_aztec bridge and other entities.
//DONT_IGNORE_MONSTERS, we reached it only when there are no other bots standing in our way!
//UTIL_TraceHull(vOrigin, nodeVector, dont_ignore_monsters, point_hull, pEdict, &tr);
//UTIL_TraceHull(vOrigin, nodeVector, dont_ignore_monsters, human_hull, pEdict, &tr);
if (pEdict) {
UTIL_TraceHull(vOrigin, nodeVector, dont_ignore_monsters,
head_hull, pEdict->v.pContainingEntity,
&tr);
} else {
UTIL_TraceHull(vOrigin, nodeVector, dont_ignore_monsters,
head_hull, NULL, &tr);
}
// if nothing hit:
if (tr.flFraction >= 1.0) {
if (pEdict != NULL) {
if (FInViewCone(&nodeVector, pEdict) // in FOV
&& FVisible(nodeVector, pEdict)) {
iFarNode = iNode;
} else {
iFarNode = iNode;
}
}
}
}
}
return iFarNode;
}
// Adds a neighbour connection to a node ID
bool cNodeMachine::add_neighbour_node(int iNode, int iToNode) {
if (iNode < 0)
return false;
tNode *node = getNode(iNode);
int iNeighbourId = freeNeighbourNodeIndex(node);
if (iNeighbourId > -1) {
node->iNeighbour[iNeighbourId] = iToNode;
return true;
}
return false;
}
/**
* Removes a neighbour connection from a node ID. Do note that we pass in node id's, ie not the actual indexes
* of the iNeighbour[] array.
* @param iFrom
* @param iTo
* @return
*/
bool cNodeMachine::removeConnection(int iFrom, int iTo) {
if (iFrom < 0 || iTo < 0) {
return false;
}
char msg[255];
memset(msg, 0, sizeof(msg));
tNode *node = getNode(iFrom);
if (!node) {
sprintf(msg, "(trouble) cNodeMachine::removeConnection | From %d, to %d has no node! (error)\n", iFrom, iTo);
rblog(msg);
return false;
}
bool removedOneOrMoreNeighbours = false;
// Find the connection and remove it
int i = 0;
for (i = 0; i < MAX_NEIGHBOURS; i++) {
int neighbourNode = node->iNeighbour[i];
sprintf(msg,
"(trouble) removeConnection(from->%d, to->%d), evaluating neighbour [%d] = node %d\n",
iFrom,
iTo,
i,
neighbourNode);
rblog(msg);
if (neighbourNode == iTo) {
rblog("(trouble) this is the connection to remove\n");
node->iNeighbour[i] = -1;
removedOneOrMoreNeighbours = true;
}
}
if (removedOneOrMoreNeighbours) {
ClearTroubledConnection(iFrom, iTo);
}
return removedOneOrMoreNeighbours;
}
// Removes ALL neighbour connections on iNode
bool cNodeMachine::remove_neighbour_nodes(int iNode) {
if (iNode < 0)
return false;
int i = 0;
for (i = 0; i < MAX_NEIGHBOURS; i++)
Nodes[iNode].iNeighbour[i] = -1;
return true;
}
// returns the next free 'neighbour id' for that node
int cNodeMachine::freeNeighbourNodeIndex(tNode *Node) {
for (int i = 0; i < MAX_NEIGHBOURS; i++) {
if (Node->iNeighbour[i] < 0) {
return i;
}
}
return -1;
}
// Return the node id from bot path on Index NR
int cNodeMachine::getNodeIndexFromBotForPath(int botIndex, int pathNodeIndex) {
if (botIndex > -1 && botIndex < MAX_BOTS &&
pathNodeIndex > -1 && pathNodeIndex < MAX_PATH_NODES) {
return iPath[botIndex][pathNodeIndex];
}
return -1; // nothing found
}
// Compute the horizontal distance between A and B (ignoring z coordinate)
static float horizontal_distance(Vector a, Vector b) {
return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y));
}
#define STEP 20 //Incremental move
// Return the floor below V
// TO BE IMPROVED use pEntityCOntaining
static Vector FloorBelow(Vector V) {
static TraceResult tr; // Keep it available even outside of the call
Vector ReallyDown, UpALittle;
int HullNumber, HullHeight;
// First use this hull
HullNumber = human_hull;
HullHeight = 36;
// Bump V a little higher (to allow for a steep climb)
UpALittle = V + Vector(0, 0, HullHeight);
ReallyDown = V + Vector(0, 0, -500);
UTIL_TraceHull(UpALittle, ReallyDown, ignore_monsters, HullNumber, NULL, &tr);
//printf(" Floor %.0f -> %.0f, TraceHull fraction = %.2f, vecEndPos.z=%.0f %s %s\n",
//UpALittle.z,ReallyDown.z,tr.flFraction,tr.vecEndPos.z,
//(tr.fAllSolid) ? "AllSolid" : "",
//(tr.fStartSolid) ? "StartSolid" : "") ;
if (tr.fStartSolid) { // Perhaps we where too high and hit the ceiling
UpALittle = V + Vector(0, 0, 0);
ReallyDown = V + Vector(0, 0, -500);
UTIL_TraceHull(UpALittle, ReallyDown, ignore_monsters, HullNumber, NULL, &tr);
//printf(" Floor without raising %.0f -> %.0f, TraceHull fraction = %.2f, vecEndPos.z=%.0f %s %s\n",
//UpALittle.z,ReallyDown.z,tr.flFraction,tr.vecEndPos.z,
//(tr.fAllSolid) ? "AllSolid" : "",
//(tr.fStartSolid) ? "StartSolid" : "") ;
if (tr.fStartSolid) { // Fall back to a point_hull
HullNumber = point_hull;
HullHeight = 0;
UpALittle = V + Vector(0, 0, STEP);
ReallyDown = V + Vector(0, 0, -500);
UTIL_TraceHull(UpALittle, ReallyDown, ignore_monsters, HullNumber, NULL, &tr);
//printf(" Floor with point hull %.0f -> %.0f, TraceHull fraction = %.2f, vecEndPos.z=%.0f %s %s\n",
//UpALittle.z,ReallyDown.z,tr.flFraction,tr.vecEndPos.z,
//(tr.fAllSolid) ? "AllSolid" : "",
//(tr.fStartSolid) ? "StartSolid" : "") ;
if (tr.fStartSolid) {
tr.vecEndPos = V;
tr.vecEndPos.z = -4000;
}
}
}
tr.vecEndPos.z -= HullHeight; //Look at feet position (depends on used hull)
return tr.vecEndPos;
}
// Check whether we can reach End from Start being a normal player
// It uses (or should use) different heuristics:
// - plain walking on a floor with upward jumps or downward falls
// - downward falls either from solid ground or from a point in the air
// to a point in water or a point in the air
// - ducking and walking (assuming flat ground)
// - swimming
int cNodeMachine::Reachable(const int iStart, const int iEnd) {
Vector IncMove, Check, Floor, Start, End;
float Dist, Height, PreviousHeight;
TraceResult tr;
Start = Nodes[iStart].origin;
End = Nodes[iEnd].origin;
#ifdef DEBUG_REACHABLE
printf("Reachable %d(%.0f,%.0f,%.0f)%s", iStart, Start.x, Start.y,
Start.z,
(Nodes[iStart].iNodeBits & BIT_LADDER) ? "OnLadder" : "");
printf(" >> %d(%.0f,%.0f,%.0f)%s\n", iEnd, End.x, End.y, End.z,
(Nodes[iEnd].iNodeBits & BIT_LADDER) ? "OnLadder" : "");
#endif
// Just in case
if (Start.x == 9999 || End.x == 9999)
return false;
// Quick & dirty check whether we can go through...
// This is simply to quickly decide whether the move is impossible
UTIL_TraceHull(Start, End, ignore_monsters, point_hull, NULL, &tr);
#ifdef DEBUG_REACHABLE
printf("TraceHull --> tr.flFraction = %.2f\n", tr.flFraction);
#endif
if (tr.flFraction < 1.0)
return false;
// If either start/end is on ladder, assume we can fly without falling
// but still check whether a human hull can go through
if ((Nodes[iStart].iNodeBits & BIT_LADDER) ||
(Nodes[iEnd].iNodeBits & BIT_LADDER))
{
UTIL_TraceHull(Start, End, ignore_monsters, human_hull, NULL, &tr);
return tr.flFraction >= 1.0;
}
// Heurestic for falling
// TODO
// Heurestic for walking/jumping/falling on the ground
// Now check whether we have ground while going from Start to End...
// Initialize IncMove to a STEP units long vector from Start to End
IncMove = End - Start;
IncMove = IncMove.Normalize();
IncMove = IncMove * STEP;
// Move Check towards End by small increment and check Z variation
// Assuming that the move is a pure line TO BE IMPROVED !!
Check = Start;
Dist = (End - Check).Length();
Floor = FloorBelow(Check);
PreviousHeight = Floor.z;
while (Dist > STEP) {
Floor = FloorBelow(Check);
Height = Floor.z;
#ifdef DEBUG_REACHABLE
printf
(" in loop Check=(%.0f,%.0f,%.0f), Dist = %.0f, Height = %.0f\n",
Check.x, Check.y, Check.z, Dist, Height);
#endif
if (Height > PreviousHeight) { // Going upwards
if (Height - PreviousHeight > MAX_JUMPHEIGHT) {
//printf(" too high for upward jump\n") ;
return false;
}
} else { // Going downwards
if (PreviousHeight - Height > MAX_FALLHEIGHT)
if (UTIL_PointContents(Floor + Vector(0, 0, 5))
!= CONTENTS_WATER)
//{printf(" too high for a downward fall not in water\n") ;
return false; // Falling from too high not in water
//}
//else printf(" ouf we are in water\n") ;
}
// TO BE IMPROVED should check whether the surface is walkable & is not LAVA
PreviousHeight = Height;
Check = Check + IncMove;
Dist = (End - Check).Length();
}
Height = End.z - 36.0f;
#ifdef DEBUG_REACHABLE
printf(" after loop End=(%.0f,%.0f,%.0f), Height = %.0f\n",
End.x, End.y, End.z, Height);
#endif
if (Height > PreviousHeight) { // Going upwards
if (Height - PreviousHeight > MAX_JUMPHEIGHT) {
//printf(" too high for upward jump\n") ;
return false;
}
} else { // Going downwards
if (PreviousHeight - Height > MAX_FALLHEIGHT)
if (UTIL_PointContents(End) != CONTENTS_WATER) {
//printf(" too high for a downward fall not in water\n") ;
return false; // Falling from too high not in water
}
//else printf(" ouf we are in water\n") ;
}
// TODO TO BE IMPROVED should check whether the surface is walkable & is not LAVA
// Heurestic for ducking
// TODO
// Heurestic for swimming
// TODO
// Success !
return true;
}
// Adding a node: another way...
int cNodeMachine::add2(Vector vOrigin, int iType, edict_t *pEntity) {
// Do not add a node when there is already one close
if (getClosestNode(vOrigin, NODE_ZONE, pEntity) > -1)
return -1;
int newNodeIndex = getFreeNodeIndex();
if (newNodeIndex >= MAX_NODES || newNodeIndex < 0) {
return -1;
}
Nodes[newNodeIndex].origin = vOrigin;
if (newNodeIndex > iMaxUsedNodes)
iMaxUsedNodes = newNodeIndex;
// Set different flags about the node
Nodes[newNodeIndex].iNodeBits = iType; // EVY's extension
if (pEntity) {
// ladder
if (FUNC_IsOnLadder(pEntity))
Nodes[newNodeIndex].iNodeBits |= BIT_LADDER;
// water at origin (=waist) or at the feet (-30)
if (UTIL_PointContents(pEntity->v.origin) == CONTENTS_WATER ||
UTIL_PointContents(pEntity->v.origin - Vector(0, 0, 30)) ==
CONTENTS_WATER)
Nodes[newNodeIndex].iNodeBits |= BIT_WATER;
// record jumping
if (pEntity->v.button & IN_JUMP)
Nodes[newNodeIndex].iNodeBits |= BIT_JUMP;
} else {
// water at origin (=waist) or at the feet (-30)
if (UTIL_PointContents(vOrigin) == CONTENTS_WATER ||
UTIL_PointContents(vOrigin - Vector(0, 0, 30)) == CONTENTS_WATER)
Nodes[newNodeIndex].iNodeBits |= BIT_WATER;
}
// add to meredians
int iX, iY;
VectorToMeredian(Nodes[newNodeIndex].origin, &iX, &iY);
if (iX > -1 && iY > -1) {
AddToMeredian(iX, iY, newNodeIndex);
} else {
rblog("ERROR: Could not add node, no meredian found to add to.\n");
return -1;
}
// Connect this node to other nodes (and vice versa)
// TODO should use the Meredian structure to only check for nodes in adjacents meredians... (faster algo)
int MyNeighbourCount, j;
for (j = 0, MyNeighbourCount = 0;
j < iMaxUsedNodes && MyNeighbourCount < MAX_NEIGHBOURS; j++) {
if (j == newNodeIndex)
continue; // Exclude self
if (Nodes[j].origin == Vector(9999, 9999, 9999))
continue; // Skip non existing nodes
// When walking the human player can't pass a certain speed and distance
// however, when a human is falling, the distance will be bigger.
int maxDistance = 3 * NODE_ZONE;
if (horizontal_distance(Nodes[newNodeIndex].origin, Nodes[j].origin) > maxDistance)
continue;
// j is a potential candidate for a neighbour
// Let's do further tests
if (Reachable(newNodeIndex, j)) { // Can reach j from newNodeIndex ?
add_neighbour_node(newNodeIndex, j); // Add j as a neighbour of mine
MyNeighbourCount++;
}
if (Reachable(j, newNodeIndex)) { // Can reach newNodeIndex from j ?
add_neighbour_node(j, newNodeIndex); // Add i as a neighbour of j
}
}
return newNodeIndex;
}
/**
* Returns a free node index, this is not bound to a meredian (subcluster)!
* @return
*/
int cNodeMachine::getFreeNodeIndex() {
int i = 0;
for (i = 0; i < MAX_NODES; i++) {
if (Nodes[i].origin == INVALID_VECTOR) {
return i;
break;
}
}
return -1; // no free node found
}
// Adding a node
int cNodeMachine::addNode(Vector vOrigin, edict_t *pEntity) {
// Do not add a node when there is already one close
if (getClosestNode(vOrigin, NODE_ZONE, pEntity) > -1)
return -1;
int currentIndex = getFreeNodeIndex();
// failed to find free node, bail
if (currentIndex < 0) {
return -1;
}
Nodes[currentIndex].origin = vOrigin;
// SET BITS:
bool bIsInWater = false;