-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathwaypoint.cpp
More file actions
1634 lines (1342 loc) · 65.3 KB
/
waypoint.cpp
File metadata and controls
1634 lines (1342 loc) · 65.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
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
#include "clients.h"
#include "item.h"
#include "server_plugin.h"
#include "type2string.h"
#include "waypoint.h"
#include <good/string_buffer.h>
// memdbgon must be the last include file in a .cpp file!!!
#include "tier0/memdbgon.h"
//----------------------------------------------------------------------------------------------------------------
extern char* szMainBuffer;
extern int iMainBufferSize;
//----------------------------------------------------------------------------------------------------------------
// Waypoints file header.
//----------------------------------------------------------------------------------------------------------------
#pragma pack(push)
#pragma pack(1)
struct waypoint_header
{
int szFileType;
char szMapName[64];
int iVersion;
int iNumWaypoints;
int iFlags;
};
#pragma pack(pop)
static const char* WAYPOINT_FILE_HEADER_ID = "BtxW"; // Botrix's Waypoints.
static const int WAYPOINT_VERSION = 1; // Waypoints file version.
static const int WAYPOINT_FILE_FLAG_VISIBILITY = 1<<0; // Flag for waypoint visibility table.
static const int WAYPOINT_FILE_FLAG_AREAS = 1<<1; // Flag for area names.
static const int WAYPOINT_VERSION_FLAGS_SHORT = 1; // Flags was short (16bits) instead of int (32bits).
//----------------------------------------------------------------------------------------------------------------
// CWaypoint static members.
//----------------------------------------------------------------------------------------------------------------
int CWaypoint::iWaypointTexture = -1;
int CWaypoint::iDefaultDistance = 144;
int CWaypoint::iUnreachablePathFailuresToDelete = 4;
int CWaypoint::iAnalyzeDistance = 96;
int CWaypoint::iWaypointsMaxCountToAnalyzeMap = 64;
#if defined(DEBUG) || defined(_DEBUG)
float CWaypoint::fAnalyzeWaypointsPerFrame = 1;
#else
float CWaypoint::fAnalyzeWaypointsPerFrame = 0.25f;
#endif
bool CWaypoint::bSaveOnMapChange = false;
bool CWaypoint::bShowAnalyzePotencialWaypoints = false;
bool CWaypoint::bAnalyzeTraceAll = false;
const TWaypointFlags CWaypoint::m_aFlagsForEntityType[ EItemTypeCanPickTotal ] =
{
FWaypointHealth | FWaypointHealthMachine,
FWaypointArmor | FWaypointArmorMachine,
FWaypointWeapon,
FWaypointAmmo,
};
StringVector CWaypoints::m_aAreas;
CWaypoints::WaypointGraph CWaypoints::m_cGraph;
float CWaypoints::fNextDrawWaypointsTime = 0.0f;
CWaypoints::Bucket CWaypoints::m_cBuckets[CWaypoints::BUCKETS_SIZE_X][CWaypoints::BUCKETS_SIZE_Y][CWaypoints::BUCKETS_SIZE_Z];
good::vector< good::bitset > CWaypoints::m_aVisTable;
bool CWaypoints::bValidVisibilityTable = false;
good::vector<CWaypoints::unreachable_path_t> CWaypoints::m_aUnreachablePaths;
// Sometimes the analyze doesn't add waypoints in small passages, so we try to add waypoints at inter-position between waypoint
// analyzed neighbours. Waypoint analyzed neighbours are x's. W = analyzed waypoint.
//
// x - x - x
// | | |
// x - W - x
// | | |
// x - x - x
//
// '-' and '|' on the picture will be evaluated in 'inters' step, when the adjacent waypoints are not set for some reason (like hit wall).
good::vector<CWaypoints::CNeighbour> CWaypoints::m_aWaypointsNeighbours;
good::vector<Vector> CWaypoints::m_aWaypointsToAddOmitInAnalyze[ CWaypoints::EAnalyzeWaypointsTotal ];
CWaypoints::TAnalyzeStep CWaypoints::m_iAnalyzeStep = EAnalyzeStepTotal;
float CWaypoints::m_fAnalyzeWaypointsForNextFrame = 0;
edict_t* CWaypoints::m_pAnalyzer = NULL;
bool CWaypoints::m_bIsAnalyzeStepAddedWaypoints = false;
TWaypointId CWaypoints::m_iCurrentAnalyzeWaypoint = 0;
static const float ANALIZE_HELP_IF_LESS_WAYPOINTS_PER_FRAME = 0.0011;
//----------------------------------------------------------------------------------------------------------------
void CWaypoint::GetColor(unsigned char& r, unsigned char& g, unsigned char& b) const
{
if ( FLAG_SOME_SET(FWaypointStop, iFlags) )
{
r = 0x00; g = 0x00; b = 0xFF; // Blue effect, stop.
}
else if ( FLAG_SOME_SET(FWaypointCamper | FWaypointSniper, iFlags) )
{
r = 0x66; g = 0x00; b = 0x00; // Dark red effect, camper / sniper.
}
else if ( FLAG_SOME_SET(FWaypointLadder, iFlags) )
{
r = 0xFF; g = 0x00; b = 0x00; // Red effect, ladder.
}
else if ( FLAG_SOME_SET(FWaypointWeapon, iFlags) )
{
r = 0xFF; g = 0xFF; b = 0x00; // Light yellow effect, weapon.
}
else if ( FLAG_SOME_SET(FWaypointAmmo, iFlags) )
{
r = 0x66; g = 0x66; b = 0x00; // Dark yellow effect, ammo.
}
else if ( FLAG_SOME_SET(FWaypointHealth, iFlags) )
{
r = 0xFF; g = 0xFF; b = 0xFF; // Light white effect, health.
}
else if ( FLAG_SOME_SET(FWaypointHealthMachine, iFlags) )
{
r = 0x66; g = 0x66; b = 0x66; // Gray effect, health machine.
}
else if ( FLAG_SOME_SET(FWaypointArmor, iFlags) )
{
r = 0x00; g = 0xFF; b = 0x00; // Light green effect, armor.
}
else if ( FLAG_SOME_SET(FWaypointArmorMachine, iFlags) )
{
r = 0x00; g = 0x66; b = 0x00; // Dark green effect, armor machine.
}
else if ( FLAG_SOME_SET(FWaypointButton | FWaypointSeeButton, iFlags) )
{
r = 0x8A; g = 0x2B; b = 0xE2; // Violet effect, button.
}
else
{
r = 0x00; g = 0xFF; b = 0xFF; // Cyan effect, other flags.
}
}
//----------------------------------------------------------------------------------------------------------------
void CWaypoint::Draw( TWaypointId iWaypointId, TWaypointDrawFlags iDrawType, float fDrawTime ) const
{
unsigned char r, g, b; // Red, green, blue.
GetColor(r, g, b);
Vector vEnd = Vector(vOrigin.x, vOrigin.y, vOrigin.z - CMod::GetVar( EModVarPlayerEye ));
if ( FLAG_ALL_SET_OR_0(FWaypointDrawBeam, iDrawType) )
CUtil::DrawBeam(vOrigin, vEnd, WIDTH, fDrawTime, r, g, b);
if ( FLAG_ALL_SET_OR_0(FWaypointDrawLine, iDrawType) )
CUtil::DrawLine(vOrigin, vEnd, fDrawTime, r, g, b);
if ( FLAG_ALL_SET_OR_0(FWaypointDrawBox, iDrawType) )
{
Vector vBoxOrigin(vOrigin.x - CMod::GetVar( EModVarPlayerWidth )/2, vOrigin.y - CMod::GetVar( EModVarPlayerWidth )/2, vOrigin.z - CMod::GetVar( EModVarPlayerEye ));
CUtil::DrawBox(vBoxOrigin, CUtil::vZero, CMod::vPlayerCollisionHull, fDrawTime, r, g, b);
}
if ( FLAG_ALL_SET_OR_0(FWaypointDrawText, iDrawType) )
{
sprintf(szMainBuffer, "%d", iWaypointId);
Vector v = vOrigin;
v.z -= 20.0f;
int i = 0;
CUtil::DrawText( v, i++, fDrawTime, 0xFF, 0xFF, 0xFF, szMainBuffer );
if ( iAreaId != 0 )
CUtil::DrawText( v, i++, fDrawTime, 0xFF, 0xFF, 0xFF, CWaypoints::GetAreas()[ iAreaId ].c_str() );
if ( FLAG_SOME_SET( FWaypointButton | FWaypointSeeButton, iFlags ) )
{
sprintf( szMainBuffer, FLAG_SOME_SET( FWaypointSeeButton, iFlags ) ? "see button %d" : "button %d",
CWaypoint::GetButton( iArgument ) );
CUtil::DrawText( v, i++, fDrawTime, 0xFF, 0xFF, 0xFF, szMainBuffer );
sprintf( szMainBuffer, FLAG_SOME_SET( FWaypointElevator, iFlags ) ? "for elevator %d" : "for door %d",
CWaypoint::GetDoor( iArgument ) );
CUtil::DrawText( v, i++, fDrawTime, 0xFF, 0xFF, 0xFF, szMainBuffer );
}
else
CUtil::DrawText( v, i++, fDrawTime, 0xFF, 0xFF, 0xFF, CTypeToString::WaypointFlagsToString( iFlags, false ).c_str() );
}
}
//********************************************************************************************************************
void CWaypoints::Clear()
{
CPlayers::InvalidatePlayersWaypoints();
ClearLocations();
m_cGraph.clear();
m_aAreas.clear();
m_aAreas.push_back( "default" ); // New waypoints without area id will be put under this empty area id.
m_aUnreachablePaths.clear();
m_aVisTable.clear();
bValidVisibilityTable = false;
}
//********************************************************************************************************************
bool CWaypoints::Save()
{
const good::string& sFileName = CUtil::BuildFileName("waypoints", CBotrixPlugin::instance->sMapName, "way");
FILE *f = CUtil::OpenFile(sFileName.c_str(), "wb");
if (f == NULL)
return false;
waypoint_header header;
header.iFlags = 0;
FLAG_SET(WAYPOINT_FILE_FLAG_AREAS, header.iFlags);
FLAG_SET(WAYPOINT_FILE_FLAG_VISIBILITY, header.iFlags);
header.iNumWaypoints = m_cGraph.size();
header.iVersion = WAYPOINT_VERSION;
header.szFileType = *((int*)&WAYPOINT_FILE_HEADER_ID[0]);
strncpy(header.szMapName, CBotrixPlugin::instance->sMapName.c_str(), sizeof(header.szMapName));
// Write header.
fwrite(&header, sizeof(waypoint_header), 1, f);
// Write waypoints data.
for (WaypointNodeIt it = m_cGraph.begin(); it != m_cGraph.end(); ++it)
{
fwrite(&it->vertex.vOrigin, sizeof(Vector), 1, f);
fwrite(&it->vertex.iFlags, sizeof(TWaypointFlags), 1, f);
fwrite(&it->vertex.iAreaId, sizeof(TAreaId), 1, f);
fwrite(&it->vertex.iArgument, sizeof(TWaypointArgument), 1, f);
}
// Write waypoints neighbours.
for (WaypointNodeIt it = m_cGraph.begin(); it != m_cGraph.end(); ++it)
{
int iNumPaths = it->neighbours.size();
fwrite(&iNumPaths, sizeof(int), 1, f);
for ( WaypointArcIt arcIt = it->neighbours.begin(); arcIt != it->neighbours.end(); ++arcIt)
{
fwrite(&arcIt->target, sizeof(TWaypointId), 1, f); // Save waypoint id.
fwrite(&arcIt->edge.iFlags, sizeof(TPathFlags), 1, f); // Save path flags.
fwrite(&arcIt->edge.iArgument, sizeof(TPathArgument), 1, f); // Save path arguments.
}
}
// Save area names.
int iAreaNamesSize = m_aAreas.size();
BASSERT( iAreaNamesSize >= 0, iAreaNamesSize=0 );
fwrite(&iAreaNamesSize, sizeof(int), 1, f); // Save area names size.
for ( int i=1; i < iAreaNamesSize; i++ ) // First area name is always empty, for new waypoints.
{
int iSize = m_aAreas[i].size();
fwrite(&iSize, 1, sizeof(int), f); // Save string size.
fwrite(m_aAreas[i].c_str(), 1, iSize+1, f); // Write string & trailing 0.
}
// Save waypoint visibility table.
BLOG_I( "Saving waypoint visibility table, this may take a while." );
m_aVisTable.resize( Size() );
for ( TWaypointId i = 0; i < Size(); ++i )
{
good::bitset& cVisibles = m_aVisTable[i];
cVisibles.resize( Size() );
Vector vFrom = Get(i).vOrigin;
CUtil::SetPVSForVector( vFrom );
for ( TWaypointId j = 0; j < Size(); ++j )
{
if ( i < j )
{
Vector vTo = Get( j ).vOrigin;
cVisibles.set( j, CUtil::IsVisiblePVS( vTo ) || CUtil::IsVisible( vFrom, vTo, EVisibilityWorld, false ) );
}
else
cVisibles.set( j, (i == j) || m_aVisTable[j].test(i) );
}
fwrite( cVisibles.data(), 1, cVisibles.byte_size(), f );
}
// Save items marks.
BLOG_I( "Saving items marks." );
GoodAssert( sizeof( TItemIndex ) == sizeof( TItemFlags ) );
const good::vector<TItemId>& aItems = CItems::GetObjectsFlags();
int iSize = aItems.size() / 2;
fwrite( &iSize, 1, sizeof( int ), f );
for ( int i = 0; i < iSize; ++i )
{
TItemIndex iIndex = aItems[ i * 2 ] - CPlayers::Size(); // Items indexes are relatives to player's count.
fwrite( &iIndex, 1, sizeof( TItemIndex ), f );
fwrite( &aItems[ i * 2 + 1 ], 1, sizeof( TItemFlags ), f );
}
fclose(f);
bValidVisibilityTable = true;
return true;
}
//----------------------------------------------------------------------------------------------------------------
bool CWaypoints::Load()
{
Clear();
const good::string& sFileName = CUtil::BuildFileName("waypoints", CBotrixPlugin::instance->sMapName, "way");
FILE *f = CUtil::OpenFile(sFileName, "rb");
if ( f == NULL )
{
BLOG_W( "No waypoints for map %s:", CBotrixPlugin::instance->sMapName.c_str() );
BLOG_W( " File '%s' doesn't exists.", sFileName.c_str() );
return false;
}
struct waypoint_header header;
size_t iRead = fread(&header, 1, sizeof(struct waypoint_header), f);
BASSERT(iRead == sizeof(struct waypoint_header), Clear();fclose(f);return false);
if (*((int*)&WAYPOINT_FILE_HEADER_ID[0]) != header.szFileType)
{
BLOG_E("Error loading waypoints: invalid file header.");
fclose(f);
return false;
}
if ( (header.iVersion <= 0) || (header.iVersion > WAYPOINT_VERSION) )
{
BLOG_E( "Error loading waypoints, version mismatch:" );
BLOG_E( " File version %d, current waypoint version %d.", header.iVersion, WAYPOINT_VERSION );
fclose(f);
return false;
}
if ( CBotrixPlugin::instance->sMapName != header.szMapName )
{
BLOG_W( "Warning loading waypoints, map name mismatch:" );
BLOG_W( " File map %s, current map %s.", header.szMapName, CBotrixPlugin::instance->sMapName.c_str() );
}
Vector vOrigin;
TWaypointFlags iFlags = 0;
int iNumPaths = 0, iArgument = 0;
TAreaId iAreaId = 0;
int sizeOfFlags = header.iVersion <= WAYPOINT_VERSION_FLAGS_SHORT ? sizeof( short ) : sizeof( int );
// Read waypoints information.
for ( TWaypointId i = 0; i < header.iNumWaypoints; ++i )
{
iRead = fread(&vOrigin, 1, sizeof(Vector), f);
BASSERT(iRead == sizeof(Vector), Clear();fclose(f);return false);
iRead = fread(&iFlags, 1, sizeOfFlags, f);
BASSERT(iRead == sizeOfFlags, Clear();fclose(f);return false);
iRead = fread(&iAreaId, 1, sizeof(TAreaId), f);
BASSERT(iRead == sizeof(TAreaId), Clear();fclose(f);return false);
if ( FLAG_CLEARED(WAYPOINT_FILE_FLAG_AREAS, header.iFlags) )
iAreaId = 0;
iRead = fread( &iArgument, 1, sizeof( TWaypointArgument ), f );
BASSERT( iRead == sizeof( TWaypointArgument ), Clear(); fclose( f ); return false );
Add( vOrigin, iFlags, iArgument, iAreaId );
}
// Read waypoints paths.
TWaypointId iPathTo;
TPathFlags iPathFlags = 0;
TPathArgument iPathArgument = 0;
for ( TWaypointId i = 0; i < header.iNumWaypoints; ++i )
{
WaypointGraph::node_it from = m_cGraph.begin() + i;
iRead = fread(&iNumPaths, 1, sizeof(int), f);
BASSERT( (iRead == sizeof(int)) && (0 <= iNumPaths) && (iNumPaths < header.iNumWaypoints), Clear();fclose(f);return false );
m_cGraph[i].neighbours.reserve(iNumPaths);
for ( int n = 0; n < iNumPaths; n ++ )
{
iRead = fread(&iPathTo, 1, sizeof(int), f);
BASSERT( (iRead == sizeof(int)) && (0 <= iPathTo) && (iPathTo < header.iNumWaypoints), Clear();fclose(f);return false );
iRead = fread(&iPathFlags, 1, sizeOfFlags, f);
BASSERT( iRead == sizeOfFlags, Clear();fclose(f);return false );
iRead = fread(&iPathArgument, 1, sizeOfFlags, f);
BASSERT( iRead == sizeOfFlags, Clear();fclose(f);return false );
WaypointGraph::node_it to = m_cGraph.begin() + iPathTo;
m_cGraph.add_arc( from, to, CWaypointPath(from->vertex.vOrigin.DistTo(to->vertex.vOrigin), iPathFlags, iPathArgument) );
}
}
int iAreaNamesSize = 0;
if ( FLAG_SOME_SET(WAYPOINT_FILE_FLAG_AREAS, header.iFlags) )
{
// Read area names.
iRead = fread(&iAreaNamesSize, 1, sizeof(int), f);
BASSERT( iRead == sizeof(int), Clear();fclose(f);return false);
//BASSERT( (0 <= iAreaNamesSize) && (iAreaNamesSize <= header.iNumWaypoints), Clear();fclose(f);return false );
m_aAreas.reserve(iAreaNamesSize);
m_aAreas.push_back("default"); // New waypoints without area id will be put under this empty area id.
for ( int i=1; i < iAreaNamesSize; i++ )
{
int iStrSize;
iRead = fread(&iStrSize, 1, sizeof(int), f);
BASSERT(iRead == sizeof(int), Clear();fclose(f);return false);
BASSERT(0 < iStrSize && iStrSize < iMainBufferSize, Clear(); return false);
if ( iStrSize > 0 )
{
iRead = fread(szMainBuffer, 1, iStrSize+1, f); // Read also trailing 0.
BASSERT(iRead == iStrSize+1, Clear();fclose(f);return false);
good::string sArea(szMainBuffer, true, true, iStrSize);
m_aAreas.push_back(sArea);
}
}
}
else
m_aAreas.push_back("default"); // New waypoints without area id will be put under this empty area id.
// Check for areas names.
iAreaNamesSize = m_aAreas.size();
for ( TWaypointId i = 0; i < header.iNumWaypoints; ++i )
{
if ( m_cGraph[i].vertex.iAreaId >= iAreaNamesSize )
{
BreakDebugger();
m_cGraph[i].vertex.iAreaId = 0;
}
}
bValidVisibilityTable = header.iFlags & WAYPOINT_FILE_FLAG_VISIBILITY;
if ( bValidVisibilityTable )
{
m_aVisTable.resize( header.iNumWaypoints );
for ( TWaypointId i = 0; i < Size(); ++i )
{
m_aVisTable[i].resize( header.iNumWaypoints );
int iByteSize = m_aVisTable[i].byte_size();
iRead = fread( m_aVisTable[i].data(), 1, iByteSize, f );
if ( iRead != iByteSize )
{
BLOG_E( "Invalid waypoints visibility table." );
Clear();
fclose(f);
return false;
}
}
BLOG_I( "Waypoints visibility table loaded." );
}
else
BLOG_W( "No waypoints visibility table in file." );
// Load items marks.
int iItemsCount = 0;
if ( fread( &iItemsCount, 1, sizeof( int ), f ) == sizeof( int ) )
{
good::vector<TItemId> aItems;
aItems.resize( iItemsCount * 2 );
iRead = fread( &aItems[0], 1, iItemsCount * 2 * sizeof( TItemIndex ), f );
BASSERT( iRead == iItemsCount * 2 * (int)sizeof( TItemIndex ), fclose( f ); return true; );
for ( int i = 0; i < iItemsCount; ++i )
{
aItems[ i * 2 ] += CPlayers::Size();
BLOG_D( "Object id %d: %s.", aItems[ i * 2 ], CTypeToString::EntityClassFlagsToString( aItems[ i * 2 + 1 ] ).c_str() );
}
CItems::SetObjectsFlags( aItems );
BLOG_I( "Loaded %d object marks.", iItemsCount );
}
fclose(f);
return true;
}
//----------------------------------------------------------------------------------------------------------------
TWaypointId CWaypoints::GetRandomNeighbour( TWaypointId iWaypoint, TWaypointId iTo, bool bVisible )
{
const WaypointNode::arcs_t& aNeighbours = GetNode(iWaypoint).neighbours;
if ( !aNeighbours.size() )
return EWaypointIdInvalid;
TWaypointId iResult = rand() % aNeighbours.size();
if ( bValidVisibilityTable && CWaypoint::IsValid(iTo) )
{
for ( int i = 0; i < aNeighbours.size(); ++i )
{
TWaypointId iNeighbour = aNeighbours[iResult].target;
if ( m_aVisTable[iNeighbour].test(iTo) == bVisible )
return iNeighbour;
if ( ++iResult == aNeighbours.size() )
iResult = 0;
}
}
return aNeighbours[iResult].target;
}
//----------------------------------------------------------------------------------------------------------------
TWaypointId CWaypoints::GetNearestNeighbour( TWaypointId iWaypoint, TWaypointId iTo, bool bVisible )
{
GoodAssert( bValidVisibilityTable );
const WaypointNode::arcs_t& aNeighbours = GetNode(iWaypoint).neighbours;
if ( !aNeighbours.size() )
return EWaypointIdInvalid;
Vector vTo = Get(iTo).vOrigin;
TWaypointId iResult = aNeighbours[0].target;
float fMinDist = Get(iResult).vOrigin.DistToSqr(vTo);
bool bResultVisibleOk = (m_aVisTable[iResult].test(iTo) == bVisible);
for ( int i = 1; i < aNeighbours.size(); ++i )
{
TWaypointId iNeighbour = aNeighbours[i].target;
bool bVisibleNeighbourOk = (m_aVisTable[iNeighbour].test(iTo) == bVisible);
if ( bResultVisibleOk && !bVisibleNeighbourOk )
continue;
Vector& vOrigin = Get(iNeighbour).vOrigin;
float fDist = vOrigin.DistToSqr(vTo);
if ( fDist < fMinDist )
{
bVisible = bVisibleNeighbourOk;
fMinDist = fDist;
iResult = iNeighbour;
}
}
return iResult;
}
//----------------------------------------------------------------------------------------------------------------
TWaypointId CWaypoints::GetFarestNeighbour( TWaypointId iWaypoint, TWaypointId iTo, bool bVisible )
{
GoodAssert( bValidVisibilityTable );
const WaypointNode::arcs_t& aNeighbours = GetNode(iWaypoint).neighbours;
if ( !aNeighbours.size() )
return EWaypointIdInvalid;
Vector vTo = Get(iTo).vOrigin;
TWaypointId iResult = aNeighbours[0].target;
float fMaxDist = Get(iResult).vOrigin.DistToSqr(vTo);
bool bResultVisibleOk = (m_aVisTable[iResult].test(iTo) == bVisible);
for ( int i = 1; i < aNeighbours.size(); ++i )
{
TWaypointId iNeighbour = aNeighbours[i].target;
bool bVisibleNeighbourOk = (m_aVisTable[iNeighbour].test(iTo) == bVisible);
if ( bResultVisibleOk && !bVisibleNeighbourOk )
continue;
Vector& vOrigin = Get(iNeighbour).vOrigin;
float fDist = vOrigin.DistToSqr(vTo);
if ( fDist > fMaxDist )
{
bVisible = bVisibleNeighbourOk;
fMaxDist = fDist;
iResult = iNeighbour;
}
}
return iResult;
}
//----------------------------------------------------------------------------------------------------------------
CWaypointPath* CWaypoints::GetPath(TWaypointId iFrom, TWaypointId iTo)
{
WaypointGraph::node_t& from = m_cGraph[iFrom];
for (WaypointGraph::arc_it it = from.neighbours.begin(); it != from.neighbours.end(); ++it)
if (it->target == iTo)
return &(it->edge);
return NULL;
}
//----------------------------------------------------------------------------------------------------------------
TWaypointId CWaypoints::Add( const Vector& vOrigin, TWaypointFlags iFlags, int iArgument, int iAreaId )
{
CWaypoint w(vOrigin, iFlags, iArgument, iAreaId);
// lol, this is not working because m_cGraph.begin() is called first. wtf?
// TWaypointId id = m_cGraph.add_node(w) - m_cGraph.begin();
CWaypoints::WaypointNodeIt it( m_cGraph.add_node(w) );
TWaypointId id = it - m_cGraph.begin();
it->neighbours.reserve( 8 );
AddLocation(id, vOrigin);
bValidVisibilityTable = false;
return id;
}
//----------------------------------------------------------------------------------------------------------------
void CWaypoints::Remove( TWaypointId id, bool bResetPlayers )
{
if ( bResetPlayers )
CPlayers::InvalidatePlayersWaypoints();
DecrementLocationIds(id);
m_cGraph.delete_node( m_cGraph.begin() + id );
bValidVisibilityTable = false;
}
//----------------------------------------------------------------------------------------------------------------
bool CWaypoints::AddPath( TWaypointId iFrom, TWaypointId iTo, float fDistance, TPathFlags iFlags )
{
if ( !CWaypoint::IsValid( iFrom ) || !CWaypoint::IsValid( iTo ) || ( iFrom == iTo ) || HasPath( iFrom, iTo ) )
return false; // Can happen with commands.
WaypointGraph::node_it from = m_cGraph.begin() + iFrom;
WaypointGraph::node_it to = m_cGraph.begin() + iTo;
if ( fDistance <= 0.0f)
fDistance = from->vertex.vOrigin.DistTo(to->vertex.vOrigin);
CWaypointPath cPath( fDistance, iFlags );
if ( FLAG_SOME_SET( FPathJump | FPathBreak, iFlags ) )
{
// Jump / break after half seconds, and maintain (duck/shoot) during 1 second.
cPath.SetActionTime( 5 );
cPath.SetActionDuration( 10 );
}
m_cGraph.add_arc( from, to, cPath );
return true;
}
//----------------------------------------------------------------------------------------------------------------
bool CWaypoints::RemovePath( TWaypointId iFrom, TWaypointId iTo )
{
if ( !CWaypoint::IsValid(iFrom) || !CWaypoint::IsValid(iTo) || (iFrom == iTo) || !HasPath(iFrom, iTo) )
return false;
m_cGraph.delete_arc( m_cGraph.begin() + iFrom, m_cGraph.begin() + iTo );
return true;
}
//----------------------------------------------------------------------------------------------------------------
void CWaypoints::CreatePathsWithAutoFlags( TWaypointId iWaypoint1, TWaypointId iWaypoint2, bool bIsCrouched, int iMaxDistance, bool bShowHelp )
{
BASSERT( CWaypoints::IsValid(iWaypoint1) && CWaypoints::IsValid(iWaypoint2), return );
WaypointNode& w1 = m_cGraph[iWaypoint1];
WaypointNode& w2 = m_cGraph[iWaypoint2];
if ( FLAG_SOME_SET( FWaypointLadder, w1.vertex.iFlags ) || FLAG_SOME_SET( FWaypointLadder, w2.vertex.iFlags ) )
return;
float fDist = w1.vertex.vOrigin.DistTo( w2.vertex.vOrigin );
Vector v1 = w1.vertex.vOrigin, v2 = w2.vertex.vOrigin;
bool bCrouch = bIsCrouched;
TReach iReach = CUtil::GetReachableInfoFromTo( v1, v2, bCrouch, 0, Sqr( iMaxDistance ), bShowHelp );
if (iReach != EReachNotReachable)
{
TPathFlags iFlags = (iReach == EReachNeedJump) ? FPathJump :
(iReach == EReachFallDamage) ? FPathDamage : FPathNone;
if ( bIsCrouched )
FLAG_SET(FPathCrouch, iFlags);
AddPath(iWaypoint1, iWaypoint2, fDist, iFlags);
}
iReach = CUtil::GetReachableInfoFromTo( v2, v1, bCrouch, 0, Sqr( iMaxDistance ), bShowHelp );
if (iReach != EReachNotReachable)
{
TPathFlags iFlags = (iReach == EReachNeedJump) ? FPathJump :
(iReach == EReachFallDamage) ? FPathDamage :
FPathNone;
if ( bIsCrouched )
FLAG_SET(FPathCrouch, iFlags);
AddPath(iWaypoint2, iWaypoint1, fDist, iFlags);
}
}
//----------------------------------------------------------------------------------------------------------------
void CWaypoints::CreateAutoPaths( TWaypointId id, bool bIsCrouched, float fMaxDistance, bool bShowHelp )
{
WaypointNode& w = m_cGraph[id];
Vector vOrigin = w.vertex.vOrigin;
int minX, minY, minZ, maxX, maxY, maxZ;
int x = GetBucketX(vOrigin.x);
int y = GetBucketY(vOrigin.y);
int z = GetBucketZ(vOrigin.z);
GetBuckets(x, y, z, minX, minY, minZ, maxX, maxY, maxZ);
for (x = minX; x <= maxX; ++x)
for (y = minY; y <= maxY; ++y)
for (z = minZ; z <= maxZ; ++z)
{
Bucket& bucket = m_cBuckets[x][y][z];
for ( Bucket::iterator it = bucket.begin(); it != bucket.end(); ++it )
{
if ( *it == id || FLAG_SOME_SET( FPathLadder, Get( *it ).iFlags ) || HasPath( *it, id ) || HasPath( id, *it ) )
continue;
CreatePathsWithAutoFlags( id, *it, bIsCrouched, fMaxDistance, bShowHelp );
}
}
}
//----------------------------------------------------------------------------------------------------------------
void CWaypoints::DecrementLocationIds( TWaypointId id )
{
BASSERT( CWaypoint::IsValid(id), return );
// Shift waypoints indexes, all waypoints with index > id.
for (int x=0; x<BUCKETS_SIZE_X; ++x)
for (int y=0; y<BUCKETS_SIZE_Y; ++y)
for (int z=0; z<BUCKETS_SIZE_Z; ++z)
{
Bucket& bucket = m_cBuckets[x][y][z];
for (Bucket::iterator it=bucket.begin(); it != bucket.end(); ++it)
if (*it > id)
--(*it);
}
// Remove waypoint id from bucket.
Vector vOrigin = m_cGraph[id].vertex.vOrigin;
Bucket& bucket = m_cBuckets[GetBucketX(vOrigin.x)][GetBucketY(vOrigin.y)][GetBucketZ(vOrigin.z)];
bucket.erase(find(bucket, id));
}
//----------------------------------------------------------------------------------------------------------------
TWaypointId CWaypoints::GetNearestWaypoint(const Vector& vOrigin, const good::bitset* aOmit,
bool bNeedVisible, float fMaxDistance, TWaypointFlags iFlags)
{
TWaypointId result = EWaypointIdInvalid;
float sqDist = SQR(fMaxDistance);
float sqMinDistance = sqDist;
int minX, minY, minZ, maxX, maxY, maxZ;
int x = GetBucketX(vOrigin.x);
int y = GetBucketY(vOrigin.y);
int z = GetBucketZ(vOrigin.z);
GetBuckets(x, y, z, minX, minY, minZ, maxX, maxY, maxZ);
if ( bNeedVisible )
CUtil::SetPVSForVector( vOrigin );
for (x = minX; x <= maxX; ++x)
for (y = minY; y <= maxY; ++y)
for (z = minZ; z <= maxZ; ++z)
{
Bucket& bucket = m_cBuckets[x][y][z];
for (Bucket::iterator it=bucket.begin(); it != bucket.end(); ++it)
{
TWaypointId iWaypoint = *it;
if ( aOmit && aOmit->test(iWaypoint) )
continue;
WaypointNode& node = m_cGraph[iWaypoint];
if ( FLAG_SOME_SET_OR_0(iFlags, node.vertex.iFlags) )
{
float distTo = vOrigin.DistToSqr(node.vertex.vOrigin);
if ( (distTo <= sqDist) && (distTo < sqMinDistance) )
{
if ( !bNeedVisible || ( CUtil::IsVisiblePVS( node.vertex.vOrigin ) &&
CUtil::IsVisible( vOrigin, node.vertex.vOrigin, EVisibilityWorld, false ) ) )
{
result = iWaypoint;
sqMinDistance = distTo;
}
}
}
}
}
return result;
}
//----------------------------------------------------------------------------------------------------------------
void CWaypoints::GetNearestWaypoints( good::vector<TWaypointId>& aResult, const Vector& vOrigin, bool bNeedVisible, float fMaxDistance )
{
float fMaxDistSqr = SQR( fMaxDistance );
int minX, minY, minZ, maxX, maxY, maxZ;
int x = GetBucketX( vOrigin.x );
int y = GetBucketY( vOrigin.y );
int z = GetBucketZ( vOrigin.z );
GetBuckets( x, y, z, minX, minY, minZ, maxX, maxY, maxZ );
if ( bNeedVisible )
CUtil::SetPVSForVector( vOrigin );
for ( x = minX; x <= maxX; ++x )
for ( y = minY; y <= maxY; ++y )
for ( z = minZ; z <= maxZ; ++z )
{
Bucket& bucket = m_cBuckets[ x ][ y ][ z ];
for ( Bucket::iterator it = bucket.begin(); it != bucket.end(); ++it )
{
TWaypointId iWaypoint = *it;
const WaypointNode& node = m_cGraph[ iWaypoint ];
if ( fabs( vOrigin.z - node.vertex.vOrigin.z ) <= fMaxDistance )
{
float fDistToSqr = vOrigin.AsVector2D().DistToSqr( node.vertex.vOrigin.AsVector2D() );
if ( fDistToSqr <= fMaxDistSqr &&
( !bNeedVisible || ( CUtil::IsVisiblePVS( node.vertex.vOrigin ) &&
CUtil::IsVisible( vOrigin, node.vertex.vOrigin, EVisibilityWorld, false ) ) ) )
aResult.push_back( iWaypoint );
}
}
}
}
//----------------------------------------------------------------------------------------------------------------
TWaypointId CWaypoints::GetAnyWaypoint(TWaypointFlags iFlags)
{
if ( CWaypoints::Size() == 0 )
return EWaypointIdInvalid;
TWaypointId id = rand() % CWaypoints::Size();
for ( TWaypointId i = id; i >= 0; --i )
if ( FLAG_SOME_SET_OR_0(iFlags, CWaypoints::Get(i).iFlags) )
return i;
for ( TWaypointId i = id+1; i < CWaypoints::Size(); ++i )
if ( FLAG_SOME_SET_OR_0(iFlags, CWaypoints::Get(i).iFlags) )
return i;
return EWaypointIdInvalid;
}
//----------------------------------------------------------------------------------------------------------------
TWaypointId CWaypoints::GetAimedWaypoint( const Vector& vOrigin, const QAngle& ang )
{
int x = GetBucketX(vOrigin.x);
int y = GetBucketY(vOrigin.y);
int z = GetBucketZ(vOrigin.z);
// Draw only waypoints from nearest buckets.
int minX, minY, minZ, maxX, maxY, maxZ;
GetBuckets(x, y, z, minX, minY, minZ, maxX, maxY, maxZ);
// Get visible clusters from player's position.
CUtil::SetPVSForVector(vOrigin);
TWaypointId iResult = EWaypointIdInvalid;
float fLowestAngDiff = 180 + 90; // Set to max angle difference.
for (x = minX; x <= maxX; ++x)
for (y = minY; y <= maxY; ++y)
for (z = minZ; z <= maxZ; ++z)
{
Bucket& bucket = m_cBuckets[x][y][z];
for (Bucket::iterator it=bucket.begin(); it != bucket.end(); ++it)
{
WaypointNode& node = m_cGraph[*it];
// Check if waypoint is in pvs from player's position.
if ( CUtil::IsVisiblePVS(node.vertex.vOrigin) && CUtil::IsVisible(vOrigin, node.vertex.vOrigin, EVisibilityWorld, false ) )
{
Vector vRelative(node.vertex.vOrigin);
vRelative.z -= CMod::GetVar( EModVarPlayerEye ) / 2; // Consider to look at center of waypoint.
vRelative -= vOrigin;
QAngle angDiff;
VectorAngles( vRelative, angDiff );
CUtil::DeNormalizeAngle(angDiff.y);
CUtil::GetAngleDifference(ang, angDiff, angDiff);
float fAngDiff = fabs(angDiff.x) + fabs(angDiff.y);
if ( fAngDiff < fLowestAngDiff )
{
fLowestAngDiff = fAngDiff;
iResult = *it;
}
}
}
}
return iResult;
}
//----------------------------------------------------------------------------------------------------------------
void CWaypoints::Draw( CClient* pClient )
{
if ( CBotrixPlugin::fTime < fNextDrawWaypointsTime )
return;
float fDrawTime = CWaypoint::DRAW_INTERVAL + (2.0f / CBotrixPlugin::iFPS); // Add two frames to not flick.
fNextDrawWaypointsTime = CBotrixPlugin::fTime + CWaypoint::DRAW_INTERVAL;
if ( pClient->iWaypointDrawFlags != FWaypointDrawNone )
{
float fPlayerEye = CMod::GetVar( EModVarPlayerEye );
Vector vOrigin;
vOrigin = pClient->GetHead();
int x = GetBucketX(vOrigin.x);
int y = GetBucketY(vOrigin.y);
int z = GetBucketZ(vOrigin.z);
// Draw only waypoints from nearest buckets.
int minX, minY, minZ, maxX, maxY, maxZ;
GetBuckets(x, y, z, minX, minY, minZ, maxX, maxY, maxZ);
// Get visible clusters from player's position.
CUtil::SetPVSForVector(vOrigin);
for (x = minX; x <= maxX; ++x)
for (y = minY; y <= maxY; ++y)
for (z = minZ; z <= maxZ; ++z)
{
Bucket& bucket = m_cBuckets[x][y][z];
for (Bucket::iterator it=bucket.begin(); it != bucket.end(); ++it)
{
WaypointNode& node = m_cGraph[*it];
// Check if waypoint is in pvs from player's position.
if ( CUtil::IsVisiblePVS( node.vertex.vOrigin ) && CUtil::IsVisible( vOrigin, node.vertex.vOrigin, EVisibilityWorld ) )
node.vertex.Draw( *it, pClient->iWaypointDrawFlags, fDrawTime );
}
}
for ( TAnalyzeWaypoints j = 0; j < EAnalyzeWaypointsTotal; ++j )
{
good::vector<Vector>& aPositions = m_aWaypointsToAddOmitInAnalyze[j];
unsigned char r[ 3 ] = { 0x00, 0xFF, 0x00 }, g[ 3 ] = { 0xFF, 0x00, 0x00 }, b[ 3 ] = { 0x00, 0x00, 0xFF };
for ( int i = 0; i < aPositions.size(); ++i )
{
Vector vOrigin = aPositions[ i ]; vOrigin.x += 0.3f; vOrigin.y += 0.3f;
Vector vEnd = vOrigin; vEnd.x += 0.3f; vEnd.y += 0.3f; vEnd.z -= fPlayerEye;
if ( CUtil::IsVisiblePVS( aPositions[ i ] ) && CUtil::IsVisible( vOrigin, aPositions[ i ], EVisibilityWorld ) )
CUtil::DrawLine( vOrigin, vEnd, fDrawTime, r[ j ], g[ j ], b[ j ] );
}
}
}
if ( pClient->iPathDrawFlags != FPathDrawNone )
{
// Draw nearest waypoint paths.
if ( CWaypoint::IsValid( pClient->iCurrentWaypoint ) )
{
if ( pClient->iPathDrawFlags != FPathDrawNone )
DrawWaypointPaths( pClient->iCurrentWaypoint, pClient->iPathDrawFlags );
CWaypoint& w = CWaypoints::Get( pClient->iCurrentWaypoint );
CUtil::DrawText(w.vOrigin, 0, fDrawTime, 0xFF, 0xFF, 0xFF, "Current");
}
if ( CWaypoint::IsValid(pClient->iDestinationWaypoint) )
{
CWaypoint& w = CWaypoints::Get( pClient->iDestinationWaypoint );
Vector v(w.vOrigin);
v.z -= 10.0f;
CUtil::DrawText(v, 0, fDrawTime, 0xFF, 0xFF, 0xFF, "Destination");
}
}
if ( bValidVisibilityTable && (pClient->iVisiblesDrawFlags != FPathDrawNone) &&
CWaypoint::IsValid(pClient->iCurrentWaypoint) )
DrawVisiblePaths( pClient->iCurrentWaypoint, pClient->iVisiblesDrawFlags );
}
//----------------------------------------------------------------------------------------------------------------
void CWaypoints::MarkUnreachablePath( TWaypointId iWaypointFrom, TWaypointId iWaypointTo )
{
if ( CWaypoint::iUnreachablePathFailuresToDelete <= 0 )
return;
GoodAssert( IsValid( iWaypointFrom ) && IsValid( iWaypointTo ) && HasPath( iWaypointFrom, iWaypointTo ) );
Vector vFrom = Get( iWaypointFrom ).vOrigin;