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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
|
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose:
//
// $NoKeywords: $
//=============================================================================//
#include "cbase.h"
#include "portal_util_shared.h"
#include "prop_portal_shared.h"
#include "portal_shareddefs.h"
#include "portal_collideable_enumerator.h"
#include "beam_shared.h"
#include "collisionutils.h"
#include "util_shared.h"
#ifndef CLIENT_DLL
#include "util.h"
#include "ndebugoverlay.h"
#include "env_debughistory.h"
#else
#include "c_portal_player.h"
#endif
#include "PortalSimulation.h"
bool g_bAllowForcePortalTrace = false;
bool g_bForcePortalTrace = false;
bool g_bBulletPortalTrace = false;
ConVar sv_portal_trace_vs_world ("sv_portal_trace_vs_world", "1", FCVAR_REPLICATED | FCVAR_CHEAT, "Use traces against portal environment world geometry" );
ConVar sv_portal_trace_vs_displacements ("sv_portal_trace_vs_displacements", "1", FCVAR_REPLICATED | FCVAR_CHEAT, "Use traces against portal environment displacement geometry" );
ConVar sv_portal_trace_vs_holywall ("sv_portal_trace_vs_holywall", "1", FCVAR_REPLICATED | FCVAR_CHEAT, "Use traces against portal environment carved wall" );
ConVar sv_portal_trace_vs_staticprops ("sv_portal_trace_vs_staticprops", "1", FCVAR_REPLICATED | FCVAR_CHEAT, "Use traces against portal environment static prop geometry" );
ConVar sv_use_find_closest_passable_space ("sv_use_find_closest_passable_space", "1", FCVAR_REPLICATED | FCVAR_CHEAT, "Enables heavy-handed player teleporting stuck fix code." );
ConVar sv_use_transformed_collideables("sv_use_transformed_collideables", "1", FCVAR_REPLICATED | FCVAR_CHEAT, "Disables traces against remote portal moving entities using transforms to bring them into local space." );
class CTransformedCollideable : public ICollideable //wraps an existing collideable, but transforms everything that pertains to world space by another transform
{
public:
VMatrix m_matTransform; //the transformation we apply to the wrapped collideable
VMatrix m_matInvTransform; //cached inverse of m_matTransform
ICollideable *m_pWrappedCollideable; //the collideable we're transforming without it knowing
struct CTC_ReferenceVars_t
{
Vector m_vCollisionOrigin;
QAngle m_qCollisionAngles;
matrix3x4_t m_matCollisionToWorldTransform;
matrix3x4_t m_matRootParentToWorldTransform;
};
mutable CTC_ReferenceVars_t m_ReferencedVars; //when returning a const reference, it needs to point to something, so here we go
//abstract functions which require no transforms, just pass them along to the wrapped collideable
virtual IHandleEntity *GetEntityHandle() { return m_pWrappedCollideable->GetEntityHandle(); }
virtual const Vector& OBBMinsPreScaled() const { return m_pWrappedCollideable->OBBMinsPreScaled(); }
virtual const Vector& OBBMaxsPreScaled() const { return m_pWrappedCollideable->OBBMaxsPreScaled(); }
virtual const Vector& OBBMins() const { return m_pWrappedCollideable->OBBMins(); }
virtual const Vector& OBBMaxs() const { return m_pWrappedCollideable->OBBMaxs(); }
virtual int GetCollisionModelIndex() { return m_pWrappedCollideable->GetCollisionModelIndex(); }
virtual const model_t* GetCollisionModel() { return m_pWrappedCollideable->GetCollisionModel(); }
virtual SolidType_t GetSolid() const { return m_pWrappedCollideable->GetSolid(); }
virtual int GetSolidFlags() const { return m_pWrappedCollideable->GetSolidFlags(); }
virtual IClientUnknown* GetIClientUnknown() { return m_pWrappedCollideable->GetIClientUnknown(); }
virtual int GetCollisionGroup() const { return m_pWrappedCollideable->GetCollisionGroup(); }
virtual bool ShouldTouchTrigger( int triggerSolidFlags ) const { return m_pWrappedCollideable->ShouldTouchTrigger(triggerSolidFlags); }
//slightly trickier functions
virtual void WorldSpaceTriggerBounds( Vector *pVecWorldMins, Vector *pVecWorldMaxs ) const;
virtual bool TestCollision( const Ray_t &ray, unsigned int fContentsMask, trace_t& tr );
virtual bool TestHitboxes( const Ray_t &ray, unsigned int fContentsMask, trace_t& tr );
virtual const Vector& GetCollisionOrigin() const;
virtual const QAngle& GetCollisionAngles() const;
virtual const matrix3x4_t& CollisionToWorldTransform() const;
virtual void WorldSpaceSurroundingBounds( Vector *pVecMins, Vector *pVecMaxs );
virtual const matrix3x4_t *GetRootParentToWorldTransform() const;
};
void CTransformedCollideable::WorldSpaceTriggerBounds( Vector *pVecWorldMins, Vector *pVecWorldMaxs ) const
{
m_pWrappedCollideable->WorldSpaceTriggerBounds( pVecWorldMins, pVecWorldMaxs );
if( pVecWorldMins )
*pVecWorldMins = m_matTransform * (*pVecWorldMins);
if( pVecWorldMaxs )
*pVecWorldMaxs = m_matTransform * (*pVecWorldMaxs);
}
bool CTransformedCollideable::TestCollision( const Ray_t &ray, unsigned int fContentsMask, trace_t& tr )
{
//TODO: Transform the ray by inverse matTransform and transform the trace results by matTransform? AABB Errors arise by transforming the ray.
return m_pWrappedCollideable->TestCollision( ray, fContentsMask, tr );
}
bool CTransformedCollideable::TestHitboxes( const Ray_t &ray, unsigned int fContentsMask, trace_t& tr )
{
//TODO: Transform the ray by inverse matTransform and transform the trace results by matTransform? AABB Errors arise by transforming the ray.
return m_pWrappedCollideable->TestHitboxes( ray, fContentsMask, tr );
}
const Vector& CTransformedCollideable::GetCollisionOrigin() const
{
m_ReferencedVars.m_vCollisionOrigin = m_matTransform * m_pWrappedCollideable->GetCollisionOrigin();
return m_ReferencedVars.m_vCollisionOrigin;
}
const QAngle& CTransformedCollideable::GetCollisionAngles() const
{
m_ReferencedVars.m_qCollisionAngles = TransformAnglesToWorldSpace( m_pWrappedCollideable->GetCollisionAngles(), m_matTransform.As3x4() );
return m_ReferencedVars.m_qCollisionAngles;
}
const matrix3x4_t& CTransformedCollideable::CollisionToWorldTransform() const
{
//1-2 order correct?
ConcatTransforms( m_matTransform.As3x4(), m_pWrappedCollideable->CollisionToWorldTransform(), m_ReferencedVars.m_matCollisionToWorldTransform );
return m_ReferencedVars.m_matCollisionToWorldTransform;
}
void CTransformedCollideable::WorldSpaceSurroundingBounds( Vector *pVecMins, Vector *pVecMaxs )
{
if( (pVecMins == NULL) && (pVecMaxs == NULL) )
return;
Vector vMins, vMaxs;
m_pWrappedCollideable->WorldSpaceSurroundingBounds( &vMins, &vMaxs );
TransformAABB( m_matTransform.As3x4(), vMins, vMaxs, vMins, vMaxs );
if( pVecMins )
*pVecMins = vMins;
if( pVecMaxs )
*pVecMaxs = vMaxs;
}
const matrix3x4_t* CTransformedCollideable::GetRootParentToWorldTransform() const
{
const matrix3x4_t *pWrappedVersion = m_pWrappedCollideable->GetRootParentToWorldTransform();
if( pWrappedVersion == NULL )
return NULL;
ConcatTransforms( m_matTransform.As3x4(), *pWrappedVersion, m_ReferencedVars.m_matRootParentToWorldTransform );
return &m_ReferencedVars.m_matRootParentToWorldTransform;
}
Color UTIL_Portal_Color( int iPortal )
{
switch ( iPortal )
{
case 0:
// GRAVITY BEAM
return Color( 242, 202, 167, 255 );
case 1:
// PORTAL 1
return Color( 64, 160, 255, 255 );
case 2:
// PORTAL 2
return Color( 255, 160, 32, 255 );
}
return Color( 255, 255, 255, 255 );
}
void UTIL_Portal_Trace_Filter( CTraceFilterSimpleClassnameList *traceFilterPortalShot )
{
traceFilterPortalShot->AddClassnameToIgnore( "prop_physics" );
traceFilterPortalShot->AddClassnameToIgnore( "func_physbox" );
traceFilterPortalShot->AddClassnameToIgnore( "npc_portal_turret_floor" );
traceFilterPortalShot->AddClassnameToIgnore( "prop_energy_ball" );
traceFilterPortalShot->AddClassnameToIgnore( "npc_security_camera" );
traceFilterPortalShot->AddClassnameToIgnore( "player" );
traceFilterPortalShot->AddClassnameToIgnore( "simple_physics_prop" );
traceFilterPortalShot->AddClassnameToIgnore( "simple_physics_brush" );
traceFilterPortalShot->AddClassnameToIgnore( "prop_ragdoll" );
traceFilterPortalShot->AddClassnameToIgnore( "prop_glados_core" );
traceFilterPortalShot->AddClassnameToIgnore( "updateitem2" );
}
CProp_Portal* UTIL_Portal_FirstAlongRay( const Ray_t &ray, float &fMustBeCloserThan )
{
CProp_Portal *pIntersectedPortal = NULL;
int iPortalCount = CProp_Portal_Shared::AllPortals.Count();
if( iPortalCount != 0 )
{
CProp_Portal **pPortals = CProp_Portal_Shared::AllPortals.Base();
for( int i = 0; i != iPortalCount; ++i )
{
CProp_Portal *pTempPortal = pPortals[i];
if( pTempPortal->IsActivedAndLinked() )
{
float fIntersection = UTIL_IntersectRayWithPortal( ray, pTempPortal );
if( fIntersection >= 0.0f && fIntersection < fMustBeCloserThan )
{
//within range, now check directionality
if( pTempPortal->m_plane_Origin.normal.Dot( ray.m_Delta ) < 0.0f )
{
//qualifies for consideration, now it just has to compete for closest
pIntersectedPortal = pTempPortal;
fMustBeCloserThan = fIntersection;
}
}
}
}
}
return pIntersectedPortal;
}
bool UTIL_Portal_TraceRay_Bullets( const CProp_Portal *pPortal, const Ray_t &ray, unsigned int fMask, ITraceFilter *pTraceFilter, trace_t *pTrace, bool bTraceHolyWall )
{
if( !pPortal || !pPortal->IsActivedAndLinked() )
{
//not in a portal environment, use regular traces
enginetrace->TraceRay( ray, fMask, pTraceFilter, pTrace );
return false;
}
trace_t trReal;
enginetrace->TraceRay( ray, fMask, pTraceFilter, &trReal );
Vector vRayNormal = ray.m_Delta;
VectorNormalize( vRayNormal );
Vector vPortalForward;
pPortal->GetVectors( &vPortalForward, 0, 0 );
// If the ray isn't going into the front of the portal, just use the real trace
if ( vPortalForward.Dot( vRayNormal ) > 0.0f )
{
*pTrace = trReal;
return false;
}
// If the real trace collides before the portal plane, just use the real trace
float fPortalFraction = UTIL_IntersectRayWithPortal( ray, pPortal );
if ( fPortalFraction == -1.0f || trReal.fraction + 0.0001f < fPortalFraction )
{
// Didn't intersect or the real trace intersected closer
*pTrace = trReal;
return false;
}
Ray_t rayPostPortal;
rayPostPortal = ray;
rayPostPortal.m_Start = ray.m_Start + ray.m_Delta * fPortalFraction;
rayPostPortal.m_Delta = ray.m_Delta * ( 1.0f - fPortalFraction );
VMatrix matThisToLinked = pPortal->MatrixThisToLinked();
Ray_t rayTransformed;
UTIL_Portal_RayTransform( matThisToLinked, rayPostPortal, rayTransformed );
// After a bullet traces through a portal it can hit the player that fired it
CTraceFilterSimple *pSimpleFilter = dynamic_cast<CTraceFilterSimple*>(pTraceFilter);
const IHandleEntity *pPassEntity = NULL;
if ( pSimpleFilter )
{
pPassEntity = pSimpleFilter->GetPassEntity();
pSimpleFilter->SetPassEntity( 0 );
}
trace_t trPostPortal;
enginetrace->TraceRay( rayTransformed, fMask, pTraceFilter, &trPostPortal );
if ( pSimpleFilter )
{
pSimpleFilter->SetPassEntity( pPassEntity );
}
//trPostPortal.startpos = ray.m_Start;
UTIL_Portal_PointTransform( matThisToLinked, ray.m_Start, trPostPortal.startpos );
trPostPortal.fraction = trPostPortal.fraction * ( 1.0f - fPortalFraction ) + fPortalFraction;
*pTrace = trPostPortal;
return true;
}
CProp_Portal* UTIL_Portal_TraceRay_Beam( const Ray_t &ray, unsigned int fMask, ITraceFilter *pTraceFilter, float *pfFraction )
{
// Do a regular trace
trace_t tr;
UTIL_TraceLine( ray.m_Start, ray.m_Start + ray.m_Delta, fMask, pTraceFilter, &tr );
float fMustBeCloserThan = tr.fraction + 0.0001f;
CProp_Portal *pIntersectedPortal = UTIL_Portal_FirstAlongRay( ray, fMustBeCloserThan );
*pfFraction = fMustBeCloserThan; //will be real trace distance if it didn't hit a portal
return pIntersectedPortal;
}
bool UTIL_Portal_Trace_Beam( const CBeam *pBeam, Vector &vecStart, Vector &vecEnd, Vector &vecIntersectionStart, Vector &vecIntersectionEnd, ITraceFilter *pTraceFilter )
{
vecStart = pBeam->GetAbsStartPos();
vecEnd = pBeam->GetAbsEndPos();
// Trace to see if we've intersected a portal
float fEndFraction;
Ray_t rayBeam;
bool bIsReversed = ( pBeam->GetBeamFlags() & FBEAM_REVERSED ) != 0x0;
if ( !bIsReversed )
rayBeam.Init( vecStart, vecEnd );
else
rayBeam.Init( vecEnd, vecStart );
CProp_Portal *pPortal = UTIL_Portal_TraceRay_Beam( rayBeam, MASK_SHOT, pTraceFilter, &fEndFraction );
// If we intersected a portal we need to modify the start and end points to match the actual trace through portal drawing extents
if ( !pPortal )
return false;
// Modify the start and end points to match the actual trace through portal drawing extents
vecStart = rayBeam.m_Start;
Vector vecIntersection = rayBeam.m_Start + rayBeam.m_Delta * fEndFraction;
int iNumLoops = 0;
// Loop through the portals (at most 16 times)
while ( pPortal && iNumLoops < 16 )
{
// Get the point that we hit a portal or wall
vecIntersectionStart = vecIntersection;
VMatrix matThisToLinked = pPortal->MatrixThisToLinked();
// Get the transformed positions of the sub beam in the other portal's space
UTIL_Portal_PointTransform( matThisToLinked, vecIntersectionStart, vecIntersectionEnd );
UTIL_Portal_PointTransform( matThisToLinked, rayBeam.m_Start + rayBeam.m_Delta, vecEnd );
CTraceFilterSkipClassname traceFilter( pPortal->m_hLinkedPortal, "prop_energy_ball", COLLISION_GROUP_NONE );
rayBeam.Init( vecIntersectionEnd, vecEnd );
pPortal = UTIL_Portal_TraceRay_Beam( rayBeam, MASK_SHOT, &traceFilter, &fEndFraction );
vecIntersection = rayBeam.m_Start + rayBeam.m_Delta * fEndFraction;
++iNumLoops;
}
vecEnd = vecIntersection;
return true;
}
void UTIL_Portal_TraceRay_With( const CProp_Portal *pPortal, const Ray_t &ray, unsigned int fMask, ITraceFilter *pTraceFilter, trace_t *pTrace, bool bTraceHolyWall )
{
//check to see if the player is theoretically in a portal environment
if( !pPortal || !pPortal->m_PortalSimulator.IsReadyToSimulate() )
{
//not in a portal environment, use regular traces
enginetrace->TraceRay( ray, fMask, pTraceFilter, pTrace );
}
else
{
trace_t RealTrace;
enginetrace->TraceRay( ray, fMask, pTraceFilter, &RealTrace );
trace_t PortalTrace;
UTIL_Portal_TraceRay( pPortal, ray, fMask, pTraceFilter, &PortalTrace, bTraceHolyWall );
if( !g_bForcePortalTrace && !RealTrace.startsolid && PortalTrace.fraction <= RealTrace.fraction )
{
*pTrace = RealTrace;
return;
}
if ( g_bAllowForcePortalTrace )
{
g_bForcePortalTrace = true;
}
*pTrace = PortalTrace;
// If this ray has a delta, make sure its towards the portal before we try to trace across portals
Vector vDirection = ray.m_Delta;
VectorNormalize( vDirection );
Vector vPortalForward;
pPortal->GetVectors( &vPortalForward, 0, 0 );
float flDot = -1.0f;
if ( ray.m_IsSwept )
{
flDot = vDirection.Dot( vPortalForward );
}
// TODO: Translate extents of rays properly, tracing extruded box rays across portals causes collision bugs
// Until this is fixed, we'll only test true rays across portals
if ( flDot < 0.0f && /*PortalTrace.fraction == 1.0f &&*/ ray.m_IsRay)
{
// Check if we're hitting stuff on the other side of the portal
trace_t PortalLinkedTrace;
UTIL_PortalLinked_TraceRay( pPortal, ray, fMask, pTraceFilter, &PortalLinkedTrace, bTraceHolyWall );
if ( PortalLinkedTrace.fraction < pTrace->fraction )
{
// Only collide with the cross-portal objects if this trace crossed a portal
if ( UTIL_DidTraceTouchPortals( ray, PortalLinkedTrace ) )
{
*pTrace = PortalLinkedTrace;
}
}
}
if( pTrace->fraction < 1.0f )
{
pTrace->contents = RealTrace.contents;
pTrace->surface = RealTrace.surface;
}
}
}
//-----------------------------------------------------------------------------
// Purpose: Tests if a ray touches the surface of any portals
// Input : ray - the ray to be tested against portal surfaces
// trace - a filled-in trace corresponding to the parameter ray
// Output : bool - false if the 'ray' parameter failed to hit any portal surface
// pOutLocal - the portal touched (if any)
// pOutRemote - the portal linked to the portal touched
//-----------------------------------------------------------------------------
bool UTIL_DidTraceTouchPortals( const Ray_t& ray, const trace_t& trace, CProp_Portal** pOutLocal, CProp_Portal** pOutRemote )
{
int iPortalCount = CProp_Portal_Shared::AllPortals.Count();
if( iPortalCount == 0 )
{
if( pOutLocal )
*pOutLocal = NULL;
if( pOutRemote )
*pOutRemote = NULL;
return false;
}
CProp_Portal **pPortals = CProp_Portal_Shared::AllPortals.Base();
CProp_Portal *pIntersectedPortal = NULL;
if( ray.m_IsSwept )
{
float fMustBeCloserThan = trace.fraction + 0.0001f;
pIntersectedPortal = UTIL_Portal_FirstAlongRay( ray, fMustBeCloserThan );
}
if( (pIntersectedPortal == NULL) && !ray.m_IsRay )
{
//haven't hit anything yet, try again with box tests
Vector ptRayEndPoint = trace.endpos - ray.m_StartOffset; // The trace added the start offset to the end position, so remove it for the box test
CProp_Portal **pBoxIntersectsPortals = (CProp_Portal **)stackalloc( sizeof(CProp_Portal *) * iPortalCount );
int iBoxIntersectsPortalsCount = 0;
for( int i = 0; i != iPortalCount; ++i )
{
CProp_Portal *pTempPortal = pPortals[i];
if( (pTempPortal->m_bActivated) &&
(pTempPortal->m_hLinkedPortal.Get() != NULL) )
{
if( UTIL_IsBoxIntersectingPortal( ptRayEndPoint, ray.m_Extents, pTempPortal, 0.00f ) )
{
pBoxIntersectsPortals[iBoxIntersectsPortalsCount] = pTempPortal;
++iBoxIntersectsPortalsCount;
}
}
}
if( iBoxIntersectsPortalsCount > 0 )
{
pIntersectedPortal = pBoxIntersectsPortals[0];
if( iBoxIntersectsPortalsCount > 1 )
{
//hit more than one, use the closest
float fDistToBeat = (ptRayEndPoint - pIntersectedPortal->GetAbsOrigin()).LengthSqr();
for( int i = 1; i != iBoxIntersectsPortalsCount; ++i )
{
float fDist = (ptRayEndPoint - pBoxIntersectsPortals[i]->GetAbsOrigin()).LengthSqr();
if( fDist < fDistToBeat )
{
pIntersectedPortal = pBoxIntersectsPortals[i];
fDistToBeat = fDist;
}
}
}
}
}
if( pIntersectedPortal == NULL )
{
if( pOutLocal )
*pOutLocal = NULL;
if( pOutRemote )
*pOutRemote = NULL;
return false;
}
else
{
// Record the touched portals and return
if( pOutLocal )
*pOutLocal = pIntersectedPortal;
if( pOutRemote )
*pOutRemote = pIntersectedPortal->m_hLinkedPortal.Get();
return true;
}
}
//-----------------------------------------------------------------------------
// Purpose: Redirects the trace to either a trace that uses portal environments, or if a
// global boolean is set, trace with a special bullets trace.
// NOTE: UTIL_Portal_TraceRay_With will use the default world trace if it gets a NULL portal pointer
// Input : &ray - the ray to use to trace
// fMask - collision mask
// *pTraceFilter - customizable filter on the trace
// *pTrace - trace struct to fill with output info
//-----------------------------------------------------------------------------
CProp_Portal* UTIL_Portal_TraceRay( const Ray_t &ray, unsigned int fMask, ITraceFilter *pTraceFilter, trace_t *pTrace, bool bTraceHolyWall )
{
float fMustBeCloserThan = 2.0f;
CProp_Portal *pIntersectedPortal = UTIL_Portal_FirstAlongRay( ray, fMustBeCloserThan );
if ( g_bBulletPortalTrace )
{
if ( UTIL_Portal_TraceRay_Bullets( pIntersectedPortal, ray, fMask, pTraceFilter, pTrace, bTraceHolyWall ) )
return pIntersectedPortal;
// Bullet didn't actually go through portal
return NULL;
}
else
{
UTIL_Portal_TraceRay_With( pIntersectedPortal, ray, fMask, pTraceFilter, pTrace, bTraceHolyWall );
return pIntersectedPortal;
}
}
CProp_Portal* UTIL_Portal_TraceRay( const Ray_t &ray, unsigned int fMask, const IHandleEntity *ignore, int collisionGroup, trace_t *pTrace, bool bTraceHolyWall )
{
CTraceFilterSimple traceFilter( ignore, collisionGroup );
return UTIL_Portal_TraceRay( ray, fMask, &traceFilter, pTrace, bTraceHolyWall );
}
//-----------------------------------------------------------------------------
// Purpose: This version of traceray only traces against the portal environment of the specified portal.
// Input : *pPortal - the portal whose physics we will trace against
// &ray - the ray to trace with
// fMask - collision mask
// *pTraceFilter - customizable filter to determine what it hits
// *pTrace - the trace struct to fill in with results
// bTraceHolyWall - if this trace is to test against the 'holy wall' geometry
//-----------------------------------------------------------------------------
void UTIL_Portal_TraceRay( const CProp_Portal *pPortal, const Ray_t &ray, unsigned int fMask, ITraceFilter *pTraceFilter, trace_t *pTrace, bool bTraceHolyWall )
{
#ifdef CLIENT_DLL
Assert( (GameRules() == NULL) || GameRules()->IsMultiplayer() );
#endif
Assert( pPortal->m_PortalSimulator.IsReadyToSimulate() ); //a trace shouldn't make it down this far if the portal is incapable of changing the results of the trace
CTraceFilterHitAll traceFilterHitAll;
if ( !pTraceFilter )
{
pTraceFilter = &traceFilterHitAll;
}
pTrace->fraction = 2.0f;
pTrace->startsolid = true;
pTrace->allsolid = true;
trace_t TempTrace;
int counter;
const CPortalSimulator &portalSimulator = pPortal->m_PortalSimulator;
CPortalSimulator *pLinkedPortalSimulator = portalSimulator.GetLinkedPortalSimulator();
//bool bTraceDisplacements = sv_portal_trace_vs_displacements.GetBool();
bool bTraceStaticProps = sv_portal_trace_vs_staticprops.GetBool();
if( sv_portal_trace_vs_holywall.GetBool() == false )
bTraceHolyWall = false;
bool bTraceTransformedGeometry = ( (pLinkedPortalSimulator != NULL) && bTraceHolyWall && portalSimulator.RayIsInPortalHole( ray ) );
bool bCopyBackBrushTraceData = false;
// Traces vs world
if( pTraceFilter->GetTraceType() != TRACE_ENTITIES_ONLY )
{
//trace_t RealTrace;
//enginetrace->TraceRay( ray, fMask, pTraceFilter, &RealTrace );
if( portalSimulator.m_DataAccess.Simulation.Static.World.Brushes.pCollideable && sv_portal_trace_vs_world.GetBool() )
{
physcollision->TraceBox( ray, portalSimulator.m_DataAccess.Simulation.Static.World.Brushes.pCollideable, vec3_origin, vec3_angle, pTrace );
bCopyBackBrushTraceData = true;
}
if( bTraceHolyWall )
{
if( portalSimulator.m_DataAccess.Simulation.Static.Wall.Local.Tube.pCollideable )
{
physcollision->TraceBox( ray, portalSimulator.m_DataAccess.Simulation.Static.Wall.Local.Tube.pCollideable, vec3_origin, vec3_angle, &TempTrace );
if( (TempTrace.startsolid == false) && (TempTrace.fraction < pTrace->fraction) ) //never allow something to be stuck in the tube, it's more of a last-resort guide than a real collideable
{
*pTrace = TempTrace;
bCopyBackBrushTraceData = true;
}
}
if( portalSimulator.m_DataAccess.Simulation.Static.Wall.Local.Brushes.pCollideable )
{
physcollision->TraceBox( ray, portalSimulator.m_DataAccess.Simulation.Static.Wall.Local.Brushes.pCollideable, vec3_origin, vec3_angle, &TempTrace );
if( (TempTrace.fraction < pTrace->fraction) )
{
*pTrace = TempTrace;
bCopyBackBrushTraceData = true;
}
}
//if( portalSimulator.m_DataAccess.Simulation.Static.Wall.RemoteTransformedToLocal.Brushes.pCollideable && sv_portal_trace_vs_world.GetBool() )
if( bTraceTransformedGeometry && pLinkedPortalSimulator->m_DataAccess.Simulation.Static.World.Brushes.pCollideable )
{
physcollision->TraceBox( ray, pLinkedPortalSimulator->m_DataAccess.Simulation.Static.World.Brushes.pCollideable, portalSimulator.m_DataAccess.Placement.ptaap_LinkedToThis.ptOriginTransform, portalSimulator.m_DataAccess.Placement.ptaap_LinkedToThis.qAngleTransform, &TempTrace );
if( (TempTrace.fraction < pTrace->fraction) )
{
*pTrace = TempTrace;
bCopyBackBrushTraceData = true;
}
}
}
if( bCopyBackBrushTraceData )
{
pTrace->surface = portalSimulator.m_DataAccess.Simulation.Static.SurfaceProperties.surface;
pTrace->contents = portalSimulator.m_DataAccess.Simulation.Static.SurfaceProperties.contents;
pTrace->m_pEnt = portalSimulator.m_DataAccess.Simulation.Static.SurfaceProperties.pEntity;
bCopyBackBrushTraceData = false;
}
}
// Traces vs entities
if( pTraceFilter->GetTraceType() != TRACE_WORLD_ONLY )
{
bool bFilterStaticProps = (pTraceFilter->GetTraceType() == TRACE_EVERYTHING_FILTER_PROPS);
//solid entities
CPortalCollideableEnumerator enumerator( pPortal );
partition->EnumerateElementsAlongRay( PARTITION_ENGINE_SOLID_EDICTS | PARTITION_ENGINE_STATIC_PROPS, ray, false, &enumerator );
for( counter = 0; counter != enumerator.m_iHandleCount; ++counter )
{
if( staticpropmgr->IsStaticProp( enumerator.m_pHandles[counter] ) )
{
//if( bFilterStaticProps && !pTraceFilter->ShouldHitEntity( enumerator.m_pHandles[counter], fMask ) )
continue; //static props are handled separately, with clipped versions
}
else if ( !pTraceFilter->ShouldHitEntity( enumerator.m_pHandles[counter], fMask ) )
{
continue;
}
enginetrace->ClipRayToEntity( ray, fMask, enumerator.m_pHandles[counter], &TempTrace );
if( (TempTrace.fraction < pTrace->fraction) )
*pTrace = TempTrace;
}
if( bTraceStaticProps )
{
//local clipped static props
{
int iLocalStaticCount = portalSimulator.m_DataAccess.Simulation.Static.World.StaticProps.ClippedRepresentations.Count();
if( iLocalStaticCount != 0 && portalSimulator.m_DataAccess.Simulation.Static.World.StaticProps.bCollisionExists )
{
const PS_SD_Static_World_StaticProps_ClippedProp_t *pCurrentProp = portalSimulator.m_DataAccess.Simulation.Static.World.StaticProps.ClippedRepresentations.Base();
const PS_SD_Static_World_StaticProps_ClippedProp_t *pStop = pCurrentProp + iLocalStaticCount;
Vector vTransform = vec3_origin;
QAngle qTransform = vec3_angle;
do
{
if( (!bFilterStaticProps) || pTraceFilter->ShouldHitEntity( pCurrentProp->pSourceProp, fMask ) )
{
physcollision->TraceBox( ray, pCurrentProp->pCollide, vTransform, qTransform, &TempTrace );
if( (TempTrace.fraction < pTrace->fraction) )
{
*pTrace = TempTrace;
pTrace->surface.flags = pCurrentProp->iTraceSurfaceFlags;
pTrace->surface.surfaceProps = pCurrentProp->iTraceSurfaceProps;
pTrace->surface.name = pCurrentProp->szTraceSurfaceName;
pTrace->contents = pCurrentProp->iTraceContents;
pTrace->m_pEnt = pCurrentProp->pTraceEntity;
}
}
++pCurrentProp;
}
while( pCurrentProp != pStop );
}
}
if( bTraceHolyWall )
{
//remote clipped static props transformed into our wall space
if( bTraceTransformedGeometry && (pTraceFilter->GetTraceType() != TRACE_WORLD_ONLY) && sv_portal_trace_vs_staticprops.GetBool() )
{
int iLocalStaticCount = pLinkedPortalSimulator->m_DataAccess.Simulation.Static.World.StaticProps.ClippedRepresentations.Count();
if( iLocalStaticCount != 0 )
{
const PS_SD_Static_World_StaticProps_ClippedProp_t *pCurrentProp = pLinkedPortalSimulator->m_DataAccess.Simulation.Static.World.StaticProps.ClippedRepresentations.Base();
const PS_SD_Static_World_StaticProps_ClippedProp_t *pStop = pCurrentProp + iLocalStaticCount;
Vector vTransform = portalSimulator.m_DataAccess.Placement.ptaap_LinkedToThis.ptOriginTransform;
QAngle qTransform = portalSimulator.m_DataAccess.Placement.ptaap_LinkedToThis.qAngleTransform;
do
{
if( (!bFilterStaticProps) || pTraceFilter->ShouldHitEntity( pCurrentProp->pSourceProp, fMask ) )
{
physcollision->TraceBox( ray, pCurrentProp->pCollide, vTransform, qTransform, &TempTrace );
if( (TempTrace.fraction < pTrace->fraction) )
{
*pTrace = TempTrace;
pTrace->surface.flags = pCurrentProp->iTraceSurfaceFlags;
pTrace->surface.surfaceProps = pCurrentProp->iTraceSurfaceProps;
pTrace->surface.name = pCurrentProp->szTraceSurfaceName;
pTrace->contents = pCurrentProp->iTraceContents;
pTrace->m_pEnt = pCurrentProp->pTraceEntity;
}
}
++pCurrentProp;
}
while( pCurrentProp != pStop );
}
}
}
}
}
if( pTrace->fraction > 1.0f ) //this should only happen if there was absolutely nothing to trace against
{
//AssertMsg( 0, "Nothing to trace against" );
memset( pTrace, 0, sizeof( trace_t ) );
pTrace->fraction = 1.0f;
pTrace->startpos = ray.m_Start - ray.m_StartOffset;
pTrace->endpos = pTrace->startpos + ray.m_Delta;
}
else if ( pTrace->fraction < 0 )
{
// For all brush traces, use the 'portal backbrush' surface surface contents
// BUGBUG: Doing this is a great solution because brushes near a portal
// will have their contents and surface properties homogenized to the brush the portal ray hit.
pTrace->contents = portalSimulator.m_DataAccess.Simulation.Static.SurfaceProperties.contents;
pTrace->surface = portalSimulator.m_DataAccess.Simulation.Static.SurfaceProperties.surface;
pTrace->m_pEnt = portalSimulator.m_DataAccess.Simulation.Static.SurfaceProperties.pEntity;
}
}
void UTIL_Portal_TraceRay( const CProp_Portal *pPortal, const Ray_t &ray, unsigned int fMask, const IHandleEntity *ignore, int collisionGroup, trace_t *pTrace, bool bTraceHolyWall )
{
CTraceFilterSimple traceFilter( ignore, collisionGroup );
UTIL_Portal_TraceRay( pPortal, ray, fMask, &traceFilter, pTrace, bTraceHolyWall );
}
//-----------------------------------------------------------------------------
// Purpose: Trace a ray 'past' a portal's surface, hitting objects in the linked portal's collision environment
// Input : *pPortal - The portal being traced 'through'
// &ray - The ray being traced
// fMask - trace mask to cull results
// *pTraceFilter - trace filter to cull results
// *pTrace - Empty trace to return the result (value will be overwritten)
//-----------------------------------------------------------------------------
void UTIL_PortalLinked_TraceRay( const CProp_Portal *pPortal, const Ray_t &ray, unsigned int fMask, ITraceFilter *pTraceFilter, trace_t *pTrace, bool bTraceHolyWall )
{
#ifdef CLIENT_DLL
Assert( (GameRules() == NULL) || GameRules()->IsMultiplayer() );
#endif
// Transform the specified ray to the remote portal's space
Ray_t rayTransformed;
UTIL_Portal_RayTransform( pPortal->MatrixThisToLinked(), ray, rayTransformed );
AssertMsg ( ray.m_IsRay, "Ray with extents across portal tracing not implemented!" );
const CPortalSimulator &portalSimulator = pPortal->m_PortalSimulator;
CProp_Portal *pLinkedPortal = (CProp_Portal*)(pPortal->m_hLinkedPortal.Get());
if( (pLinkedPortal == NULL) || (portalSimulator.RayIsInPortalHole( ray ) == false) )
{
memset( pTrace, 0, sizeof(trace_t));
pTrace->fraction = 1.0f;
pTrace->fractionleftsolid = 0;
pTrace->contents = pPortal->m_PortalSimulator.m_DataAccess.Simulation.Static.SurfaceProperties.contents;
pTrace->surface = pPortal->m_PortalSimulator.m_DataAccess.Simulation.Static.SurfaceProperties.surface;
pTrace->m_pEnt = pPortal->m_PortalSimulator.m_DataAccess.Simulation.Static.SurfaceProperties.pEntity;
return;
}
UTIL_Portal_TraceRay( pLinkedPortal, rayTransformed, fMask, pTraceFilter, pTrace, bTraceHolyWall );
// Transform the ray's start, end and plane back into this portal's space,
// because we react to the collision as it is displayed, and the image is displayed with this local portal's orientation.
VMatrix matLinkedToThis = pLinkedPortal->MatrixThisToLinked();
UTIL_Portal_PointTransform( matLinkedToThis, pTrace->startpos, pTrace->startpos );
UTIL_Portal_PointTransform( matLinkedToThis, pTrace->endpos, pTrace->endpos );
UTIL_Portal_PlaneTransform( matLinkedToThis, pTrace->plane, pTrace->plane );
}
void UTIL_PortalLinked_TraceRay( const CProp_Portal *pPortal, const Ray_t &ray, unsigned int fMask, const IHandleEntity *ignore, int collisionGroup, trace_t *pTrace, bool bTraceHolyWall )
{
CTraceFilterSimple traceFilter( ignore, collisionGroup );
UTIL_PortalLinked_TraceRay( pPortal, ray, fMask, &traceFilter, pTrace, bTraceHolyWall );
}
//-----------------------------------------------------------------------------
// Purpose: A version of trace entity which detects portals and translates the trace through portals
//-----------------------------------------------------------------------------
void UTIL_Portal_TraceEntity( CBaseEntity *pEntity, const Vector &vecAbsStart, const Vector &vecAbsEnd,
unsigned int mask, ITraceFilter *pFilter, trace_t *pTrace )
{
#ifdef CLIENT_DLL
Assert( (GameRules() == NULL) || GameRules()->IsMultiplayer() );
Assert( pEntity->IsPlayer() );
CPortalSimulator *pPortalSimulator = NULL;
if( pEntity->IsPlayer() )
{
C_Prop_Portal *pPortal = ((C_Portal_Player *)pEntity)->m_hPortalEnvironment.Get();
if( pPortal )
pPortalSimulator = &pPortal->m_PortalSimulator;
}
#else
CPortalSimulator *pPortalSimulator = CPortalSimulator::GetSimulatorThatOwnsEntity( pEntity );
#endif
memset( pTrace, 0, sizeof(trace_t));
pTrace->fraction = 1.0f;
pTrace->fractionleftsolid = 0;
ICollideable* pCollision = enginetrace->GetCollideable( pEntity );
// If main is simulating this object, trace as UTIL_TraceEntity would
trace_t realTrace;
QAngle qCollisionAngles = pCollision->GetCollisionAngles();
enginetrace->SweepCollideable( pCollision, vecAbsStart, vecAbsEnd, qCollisionAngles, mask, pFilter, &realTrace );
// For the below box test, we need to add the tolerance onto the extents, because the underlying
// box on plane side test doesn't use the parameter tolerance.
float flTolerance = 0.1f;
Vector vEntExtents = pEntity->WorldAlignSize() * 0.5 + Vector ( flTolerance, flTolerance, flTolerance );
Vector vColCenter = realTrace.endpos + ( pEntity->WorldAlignMaxs() + pEntity->WorldAlignMins() ) * 0.5f;
// If this entity is not simulated in a portal environment, trace as normal
if( pPortalSimulator == NULL )
{
// If main is simulating this object, trace as UTIL_TraceEntity would
*pTrace = realTrace;
}
else
{
CPortalSimulator *pLinkedPortalSimulator = pPortalSimulator->GetLinkedPortalSimulator();
Ray_t entRay;
entRay.Init( vecAbsStart, vecAbsEnd, pCollision->OBBMins(), pCollision->OBBMaxs() );
#if 0 // this trace for brush ents made sense at one time, but it's 'overcolliding' during portal transitions (bugzilla#25)
if( realTrace.m_pEnt && (realTrace.m_pEnt->GetMoveType() != MOVETYPE_NONE) ) //started by hitting something moving which wouldn't be detected in the following traces
{
float fFirstPortalFraction = 2.0f;
CProp_Portal *pFirstPortal = UTIL_Portal_FirstAlongRay( entRay, fFirstPortalFraction );
if ( !pFirstPortal )
*pTrace = realTrace;
else
{
Vector vFirstPortalForward;
pFirstPortal->GetVectors( &vFirstPortalForward, NULL, NULL );
if ( vFirstPortalForward.Dot( realTrace.endpos - pFirstPortal->GetAbsOrigin() ) > 0.0f )
*pTrace = realTrace;
}
}
#endif
// We require both environments to be active in order to trace against them
Assert ( pCollision );
if ( !pCollision )
{
return;
}
// World, displacements and holy wall are stored in separate collideables
// Traces against each and keep the closest intersection (if any)
trace_t tempTrace;
// Hit the world
if ( pFilter->GetTraceType() != TRACE_ENTITIES_ONLY )
{
if( pPortalSimulator->m_DataAccess.Simulation.Static.World.Brushes.pCollideable &&
sv_portal_trace_vs_world.GetBool() )
{
//physcollision->TraceCollide( vecAbsStart, vecAbsEnd, pCollision, qCollisionAngles,
// pPortalSimulator->m_DataAccess.Simulation.Static.World.Brushes.pCollideable, vec3_origin, vec3_angle, &tempTrace );
physcollision->TraceBox( entRay, MASK_ALL, NULL, pPortalSimulator->m_DataAccess.Simulation.Static.World.Brushes.pCollideable, vec3_origin, vec3_angle, &tempTrace );
if ( tempTrace.startsolid || (tempTrace.fraction < pTrace->fraction) )
{
*pTrace = tempTrace;
}
}
//if( pPortalSimulator->m_DataAccess.Simulation.Static.Wall.RemoteTransformedToLocal.Brushes.pCollideable &&
if( pLinkedPortalSimulator &&
pLinkedPortalSimulator->m_DataAccess.Simulation.Static.World.Brushes.pCollideable &&
sv_portal_trace_vs_world.GetBool() &&
sv_portal_trace_vs_holywall.GetBool() )
{
//physcollision->TraceCollide( vecAbsStart, vecAbsEnd, pCollision, qCollisionAngles,
// pLinkedPortalSimulator->m_DataAccess.Simulation.Static.World.Brushes.pCollideable, pPortalSimulator->m_DataAccess.Placement.ptaap_LinkedToThis.ptOriginTransform, pPortalSimulator->m_DataAccess.Placement.ptaap_LinkedToThis.qAngleTransform, &tempTrace );
physcollision->TraceBox( entRay, MASK_ALL, NULL, pLinkedPortalSimulator->m_DataAccess.Simulation.Static.World.Brushes.pCollideable, pPortalSimulator->m_DataAccess.Placement.ptaap_LinkedToThis.ptOriginTransform, pPortalSimulator->m_DataAccess.Placement.ptaap_LinkedToThis.qAngleTransform, &tempTrace );
if ( tempTrace.startsolid || (tempTrace.fraction < pTrace->fraction) )
{
*pTrace = tempTrace;
}
}
if ( pPortalSimulator->m_DataAccess.Simulation.Static.Wall.Local.Brushes.pCollideable &&
sv_portal_trace_vs_holywall.GetBool() )
{
//physcollision->TraceCollide( vecAbsStart, vecAbsEnd, pCollision, qCollisionAngles,
// pPortalSimulator->m_DataAccess.Simulation.Static.Wall.Local.Brushes.pCollideable, vec3_origin, vec3_angle, &tempTrace );
physcollision->TraceBox( entRay, MASK_ALL, NULL, pPortalSimulator->m_DataAccess.Simulation.Static.Wall.Local.Brushes.pCollideable, vec3_origin, vec3_angle, &tempTrace );
if ( tempTrace.startsolid || (tempTrace.fraction < pTrace->fraction) )
{
if( tempTrace.fraction == 0.0f )
tempTrace.startsolid = true;
if( tempTrace.fractionleftsolid == 1.0f )
tempTrace.allsolid = true;
*pTrace = tempTrace;
}
}
if ( pPortalSimulator->m_DataAccess.Simulation.Static.Wall.Local.Tube.pCollideable &&
sv_portal_trace_vs_holywall.GetBool() )
{
//physcollision->TraceCollide( vecAbsStart, vecAbsEnd, pCollision, qCollisionAngles,
// pPortalSimulator->m_DataAccess.Simulation.Static.Wall.Local.Tube.pCollideable, vec3_origin, vec3_angle, &tempTrace );
physcollision->TraceBox( entRay, MASK_ALL, NULL, pPortalSimulator->m_DataAccess.Simulation.Static.Wall.Local.Tube.pCollideable, vec3_origin, vec3_angle, &tempTrace );
if( (tempTrace.startsolid == false) && (tempTrace.fraction < pTrace->fraction) ) //never allow something to be stuck in the tube, it's more of a last-resort guide than a real collideable
{
*pTrace = tempTrace;
}
}
// For all brush traces, use the 'portal backbrush' surface surface contents
// BUGBUG: Doing this is a great solution because brushes near a portal
// will have their contents and surface properties homogenized to the brush the portal ray hit.
if ( pTrace->startsolid || (pTrace->fraction < 1.0f) )
{
pTrace->surface = pPortalSimulator->m_DataAccess.Simulation.Static.SurfaceProperties.surface;
pTrace->contents = pPortalSimulator->m_DataAccess.Simulation.Static.SurfaceProperties.contents;
pTrace->m_pEnt = pPortalSimulator->m_DataAccess.Simulation.Static.SurfaceProperties.pEntity;
}
}
// Trace vs entities
if ( pFilter->GetTraceType() != TRACE_WORLD_ONLY )
{
if( sv_portal_trace_vs_staticprops.GetBool() && (pFilter->GetTraceType() != TRACE_ENTITIES_ONLY) )
{
bool bFilterStaticProps = (pFilter->GetTraceType() == TRACE_EVERYTHING_FILTER_PROPS);
//local clipped static props
{
int iLocalStaticCount = pPortalSimulator->m_DataAccess.Simulation.Static.World.StaticProps.ClippedRepresentations.Count();
if( iLocalStaticCount != 0 && pPortalSimulator->m_DataAccess.Simulation.Static.World.StaticProps.bCollisionExists )
{
const PS_SD_Static_World_StaticProps_ClippedProp_t *pCurrentProp = pPortalSimulator->m_DataAccess.Simulation.Static.World.StaticProps.ClippedRepresentations.Base();
const PS_SD_Static_World_StaticProps_ClippedProp_t *pStop = pCurrentProp + iLocalStaticCount;
Vector vTransform = vec3_origin;
QAngle qTransform = vec3_angle;
do
{
if( (!bFilterStaticProps) || pFilter->ShouldHitEntity( pCurrentProp->pSourceProp, mask ) )
{
//physcollision->TraceCollide( vecAbsStart, vecAbsEnd, pCollision, qCollisionAngles,
// pCurrentProp->pCollide, vTransform, qTransform, &tempTrace );
physcollision->TraceBox( entRay, MASK_ALL, NULL, pCurrentProp->pCollide, vTransform, qTransform, &tempTrace );
if( tempTrace.startsolid || (tempTrace.fraction < pTrace->fraction) )
{
*pTrace = tempTrace;
pTrace->surface.flags = pCurrentProp->iTraceSurfaceFlags;
pTrace->surface.surfaceProps = pCurrentProp->iTraceSurfaceProps;
pTrace->surface.name = pCurrentProp->szTraceSurfaceName;
pTrace->contents = pCurrentProp->iTraceContents;
pTrace->m_pEnt = pCurrentProp->pTraceEntity;
}
}
++pCurrentProp;
}
while( pCurrentProp != pStop );
}
}
if( pLinkedPortalSimulator && pPortalSimulator->EntityIsInPortalHole( pEntity ) )
{
#ifndef CLIENT_DLL
if( sv_use_transformed_collideables.GetBool() ) //if this never gets turned off, it should be removed before release
{
//moving entities near the remote portal
CBaseEntity *pEnts[1024];
int iEntCount = pLinkedPortalSimulator->GetMoveableOwnedEntities( pEnts, 1024 );
CTransformedCollideable transformedCollideable;
transformedCollideable.m_matTransform = pLinkedPortalSimulator->m_DataAccess.Placement.matThisToLinked;
transformedCollideable.m_matInvTransform = pLinkedPortalSimulator->m_DataAccess.Placement.matLinkedToThis;
for( int i = 0; i != iEntCount; ++i )
{
CBaseEntity *pRemoteEntity = pEnts[i];
if( pRemoteEntity->GetSolid() == SOLID_NONE )
continue;
transformedCollideable.m_pWrappedCollideable = pRemoteEntity->GetCollideable();
Assert( transformedCollideable.m_pWrappedCollideable != NULL );
//enginetrace->ClipRayToCollideable( entRay, mask, &transformedCollideable, pTrace );
enginetrace->ClipRayToCollideable( entRay, mask, &transformedCollideable, &tempTrace );
if( tempTrace.startsolid || (tempTrace.fraction < pTrace->fraction) )
{
*pTrace = tempTrace;
}
}
}
#endif //#ifndef CLIENT_DLL
}
}
}
if( pTrace->fraction == 1.0f )
{
memset( pTrace, 0, sizeof( trace_t ) );
pTrace->fraction = 1.0f;
pTrace->startpos = vecAbsStart;
pTrace->endpos = vecAbsEnd;
}
//#endif
}
}
void UTIL_Portal_PointTransform( const VMatrix matThisToLinked, const Vector &ptSource, Vector &ptTransformed )
{
ptTransformed = matThisToLinked * ptSource;
}
void UTIL_Portal_VectorTransform( const VMatrix matThisToLinked, const Vector &vSource, Vector &vTransformed )
{
vTransformed = matThisToLinked.ApplyRotation( vSource );
}
void UTIL_Portal_AngleTransform( const VMatrix matThisToLinked, const QAngle &qSource, QAngle &qTransformed )
{
qTransformed = TransformAnglesToWorldSpace( qSource, matThisToLinked.As3x4() );
}
void UTIL_Portal_RayTransform( const VMatrix matThisToLinked, const Ray_t &raySource, Ray_t &rayTransformed )
{
rayTransformed = raySource;
UTIL_Portal_PointTransform( matThisToLinked, raySource.m_Start, rayTransformed.m_Start );
UTIL_Portal_VectorTransform( matThisToLinked, raySource.m_StartOffset, rayTransformed.m_StartOffset );
UTIL_Portal_VectorTransform( matThisToLinked, raySource.m_Delta, rayTransformed.m_Delta );
//BUGBUG: Extents are axis aligned, so rotating it won't necessarily give us what we're expecting
UTIL_Portal_VectorTransform( matThisToLinked, raySource.m_Extents, rayTransformed.m_Extents );
//HACKHACK: Negative extents hang in traces, make each positive because we rotated it above
if ( rayTransformed.m_Extents.x < 0.0f )
{
rayTransformed.m_Extents.x = -rayTransformed.m_Extents.x;
}
if ( rayTransformed.m_Extents.y < 0.0f )
{
rayTransformed.m_Extents.y = -rayTransformed.m_Extents.y;
}
if ( rayTransformed.m_Extents.z < 0.0f )
{
rayTransformed.m_Extents.z = -rayTransformed.m_Extents.z;
}
}
void UTIL_Portal_PlaneTransform( const VMatrix matThisToLinked, const cplane_t &planeSource, cplane_t &planeTransformed )
{
planeTransformed = planeSource;
Vector vTrans;
UTIL_Portal_VectorTransform( matThisToLinked, planeSource.normal, planeTransformed.normal );
planeTransformed.dist = planeSource.dist * DotProduct( planeTransformed.normal, planeTransformed.normal );
planeTransformed.dist += DotProduct( planeTransformed.normal, matThisToLinked.GetTranslation( vTrans ) );
}
void UTIL_Portal_PlaneTransform( const VMatrix matThisToLinked, const VPlane &planeSource, VPlane &planeTransformed )
{
Vector vTranformedNormal;
float fTransformedDist;
Vector vTrans;
UTIL_Portal_VectorTransform( matThisToLinked, planeSource.m_Normal, vTranformedNormal );
fTransformedDist = planeSource.m_Dist * DotProduct( vTranformedNormal, vTranformedNormal );
fTransformedDist += DotProduct( vTranformedNormal, matThisToLinked.GetTranslation( vTrans ) );
planeTransformed.Init( vTranformedNormal, fTransformedDist );
}
void UTIL_Portal_Triangles( const Vector &ptPortalCenter, const QAngle &qPortalAngles, Vector pvTri1[ 3 ], Vector pvTri2[ 3 ] )
{
// Get points to make triangles
Vector vRight, vUp;
AngleVectors( qPortalAngles, NULL, &vRight, &vUp );
Vector vTopEdge = vUp * PORTAL_HALF_HEIGHT;
Vector vBottomEdge = -vTopEdge;
Vector vRightEdge = vRight * PORTAL_HALF_WIDTH;
Vector vLeftEdge = -vRightEdge;
Vector vTopLeft = ptPortalCenter + vTopEdge + vLeftEdge;
Vector vTopRight = ptPortalCenter + vTopEdge + vRightEdge;
Vector vBottomLeft = ptPortalCenter + vBottomEdge + vLeftEdge;
Vector vBottomRight = ptPortalCenter + vBottomEdge + vRightEdge;
// Make triangles
pvTri1[ 0 ] = vTopRight;
pvTri1[ 1 ] = vTopLeft;
pvTri1[ 2 ] = vBottomLeft;
pvTri2[ 0 ] = vTopRight;
pvTri2[ 1 ] = vBottomLeft;
pvTri2[ 2 ] = vBottomRight;
}
void UTIL_Portal_Triangles( const CProp_Portal *pPortal, Vector pvTri1[ 3 ], Vector pvTri2[ 3 ] )
{
UTIL_Portal_Triangles( pPortal->GetAbsOrigin(), pPortal->GetAbsAngles(), pvTri1, pvTri2 );
}
float UTIL_Portal_DistanceThroughPortal( const CProp_Portal *pPortal, const Vector &vPoint1, const Vector &vPoint2 )
{
return FastSqrt( UTIL_Portal_DistanceThroughPortalSqr( pPortal, vPoint1, vPoint2 ) );
}
float UTIL_Portal_DistanceThroughPortalSqr( const CProp_Portal *pPortal, const Vector &vPoint1, const Vector &vPoint2 )
{
if ( !pPortal || !pPortal->m_bActivated )
return -1.0f;
CProp_Portal *pPortalLinked = pPortal->m_hLinkedPortal;
if ( !pPortalLinked || !pPortalLinked->m_bActivated )
return -1.0f;
return vPoint1.DistToSqr( pPortal->GetAbsOrigin() ) + pPortalLinked->GetAbsOrigin().DistToSqr( vPoint2 );
}
float UTIL_Portal_ShortestDistance( const Vector &vPoint1, const Vector &vPoint2, CProp_Portal **pShortestDistPortal_Out /*= NULL*/, bool bRequireStraightLine /*= false*/ )
{
return FastSqrt( UTIL_Portal_ShortestDistanceSqr( vPoint1, vPoint2, pShortestDistPortal_Out, bRequireStraightLine ) );
}
float UTIL_Portal_ShortestDistanceSqr( const Vector &vPoint1, const Vector &vPoint2, CProp_Portal **pShortestDistPortal_Out /*= NULL*/, bool bRequireStraightLine /*= false*/ )
{
float fMinDist = vPoint1.DistToSqr( vPoint2 );
int iPortalCount = CProp_Portal_Shared::AllPortals.Count();
if( iPortalCount == 0 )
{
if( pShortestDistPortal_Out )
*pShortestDistPortal_Out = NULL;
return fMinDist;
}
CProp_Portal **pPortals = CProp_Portal_Shared::AllPortals.Base();
CProp_Portal *pShortestDistPortal = NULL;
for( int i = 0; i != iPortalCount; ++i )
{
CProp_Portal *pTempPortal = pPortals[i];
if( pTempPortal->m_bActivated )
{
CProp_Portal *pLinkedPortal = pTempPortal->m_hLinkedPortal.Get();
if( pLinkedPortal != NULL )
{
Vector vPoint1Transformed = pTempPortal->MatrixThisToLinked() * vPoint1;
float fDirectDist = vPoint1Transformed.DistToSqr( vPoint2 );
if( fDirectDist < fMinDist )
{
//worth investigating further
//find out if it's a straight line through the portal, or if we have to wrap around a corner
float fPoint1TransformedDist = pLinkedPortal->m_plane_Origin.normal.Dot( vPoint1Transformed ) - pLinkedPortal->m_plane_Origin.dist;
float fPoint2Dist = pLinkedPortal->m_plane_Origin.normal.Dot( vPoint2 ) - pLinkedPortal->m_plane_Origin.dist;
bool bStraightLine = true;
if( (fPoint1TransformedDist > 0.0f) || (fPoint2Dist < 0.0f) ) //straight line through portal impossible, part of the line has to backtrack to get to the portal surface
bStraightLine = false;
if( bStraightLine ) //if we're not already doing some crazy wrapping, find an intersection point
{
float fTotalDist = fPoint2Dist - fPoint1TransformedDist; //fPoint1TransformedDist is known to be negative
Vector ptPlaneIntersection;
if( fTotalDist != 0.0f )
{
float fInvTotalDist = 1.0f / fTotalDist;
ptPlaneIntersection = (vPoint1Transformed * (fPoint2Dist * fInvTotalDist)) + (vPoint2 * ((-fPoint1TransformedDist) * fInvTotalDist));
}
else
{
ptPlaneIntersection = vPoint1Transformed;
}
Vector vRight, vUp;
pLinkedPortal->GetVectors( NULL, &vRight, &vUp );
Vector ptLinkedCenter = pLinkedPortal->GetAbsOrigin();
Vector vCenterToIntersection = ptPlaneIntersection - ptLinkedCenter;
float fRight = vRight.Dot( vCenterToIntersection );
float fUp = vUp.Dot( vCenterToIntersection );
float fAbsRight = fabs( fRight );
float fAbsUp = fabs( fUp );
if( (fAbsRight > PORTAL_HALF_WIDTH) ||
(fAbsUp > PORTAL_HALF_HEIGHT) )
bStraightLine = false;
if( bStraightLine == false )
{
if( bRequireStraightLine )
continue;
//find the offending extent and shorten both extents to bring it into the portal quad
float fNormalizer;
if( fAbsRight > PORTAL_HALF_WIDTH )
{
fNormalizer = fAbsRight/PORTAL_HALF_WIDTH;
if( fAbsUp > PORTAL_HALF_HEIGHT )
{
float fUpNormalizer = fAbsUp/PORTAL_HALF_HEIGHT;
if( fUpNormalizer > fNormalizer )
fNormalizer = fUpNormalizer;
}
}
else
{
fNormalizer = fAbsUp/PORTAL_HALF_HEIGHT;
}
vCenterToIntersection *= (1.0f/fNormalizer);
ptPlaneIntersection = ptLinkedCenter + vCenterToIntersection;
float fWrapDist = vPoint1Transformed.DistToSqr( ptPlaneIntersection ) + vPoint2.DistToSqr( ptPlaneIntersection );
if( fWrapDist < fMinDist )
{
fMinDist = fWrapDist;
pShortestDistPortal = pTempPortal;
}
}
else
{
//it's a straight shot from point 1 to 2 through the portal
fMinDist = fDirectDist;
pShortestDistPortal = pTempPortal;
}
}
else
{
if( bRequireStraightLine )
continue;
//do some crazy wrapped line intersection algorithm
//for now, just do the cheap and easy solution
float fWrapDist = vPoint1.DistToSqr( pTempPortal->GetAbsOrigin() ) + pLinkedPortal->GetAbsOrigin().DistToSqr( vPoint2 );
if( fWrapDist < fMinDist )
{
fMinDist = fWrapDist;
pShortestDistPortal = pTempPortal;
}
}
}
}
}
}
return fMinDist;
}
void UTIL_Portal_AABB( const CProp_Portal *pPortal, Vector &vMin, Vector &vMax )
{
Vector vOrigin = pPortal->GetAbsOrigin();
QAngle qAngles = pPortal->GetAbsAngles();
Vector vOBBForward;
Vector vOBBRight;
Vector vOBBUp;
AngleVectors( qAngles, &vOBBForward, &vOBBRight, &vOBBUp );
//scale the extents to usable sizes
vOBBForward *= PORTAL_HALF_DEPTH;
vOBBRight *= PORTAL_HALF_WIDTH;
vOBBUp *= PORTAL_HALF_HEIGHT;
vOrigin -= vOBBForward + vOBBRight + vOBBUp;
vOBBForward *= 2.0f;
vOBBRight *= 2.0f;
vOBBUp *= 2.0f;
vMin = vMax = vOrigin;
for( int i = 1; i != 8; ++i )
{
Vector ptTest = vOrigin;
if( i & (1 << 0) ) ptTest += vOBBForward;
if( i & (1 << 1) ) ptTest += vOBBRight;
if( i & (1 << 2) ) ptTest += vOBBUp;
if( ptTest.x < vMin.x ) vMin.x = ptTest.x;
if( ptTest.y < vMin.y ) vMin.y = ptTest.y;
if( ptTest.z < vMin.z ) vMin.z = ptTest.z;
if( ptTest.x > vMax.x ) vMax.x = ptTest.x;
if( ptTest.y > vMax.y ) vMax.y = ptTest.y;
if( ptTest.z > vMax.z ) vMax.z = ptTest.z;
}
}
float UTIL_IntersectRayWithPortal( const Ray_t &ray, const CProp_Portal *pPortal )
{
if ( !pPortal || !pPortal->m_bActivated )
{
return -1.0f;
}
Vector vForward;
pPortal->GetVectors( &vForward, NULL, NULL );
// Discount rays not coming from the front of the portal
float fDot = DotProduct( vForward, ray.m_Delta );
if ( fDot > 0.0f )
{
return -1.0f;
}
Vector pvTri1[ 3 ], pvTri2[ 3 ];
UTIL_Portal_Triangles( pPortal, pvTri1, pvTri2 );
float fT;
// Test triangle 1
fT = IntersectRayWithTriangle( ray, pvTri1[ 0 ], pvTri1[ 1 ], pvTri1[ 2 ], false );
// If there was an intersection return the T
if ( fT >= 0.0f )
return fT;
// Return the result of collision with the other face triangle
return IntersectRayWithTriangle( ray, pvTri2[ 0 ], pvTri2[ 1 ], pvTri2[ 2 ], false );
}
bool UTIL_IntersectRayWithPortalOBB( const CProp_Portal *pPortal, const Ray_t &ray, trace_t *pTrace )
{
return IntersectRayWithOBB( ray, pPortal->GetAbsOrigin(), pPortal->GetAbsAngles(), CProp_Portal_Shared::vLocalMins, CProp_Portal_Shared::vLocalMaxs, 0.0f, pTrace );
}
bool UTIL_IntersectRayWithPortalOBBAsAABB( const CProp_Portal *pPortal, const Ray_t &ray, trace_t *pTrace )
{
Vector vAABBMins, vAABBMaxs;
UTIL_Portal_AABB( pPortal, vAABBMins, vAABBMaxs );
return IntersectRayWithBox( ray, vAABBMins, vAABBMaxs, 0.0f, pTrace );
}
bool UTIL_IsBoxIntersectingPortal( const Vector &vecBoxCenter, const Vector &vecBoxExtents, const Vector &ptPortalCenter, const QAngle &qPortalAngles, float flTolerance )
{
Vector pvTri1[ 3 ], pvTri2[ 3 ];
UTIL_Portal_Triangles( ptPortalCenter, qPortalAngles, pvTri1, pvTri2 );
cplane_t plane;
ComputeTrianglePlane( pvTri1[ 0 ], pvTri1[ 1 ], pvTri1[ 2 ], plane.normal, plane.dist );
plane.type = PLANE_ANYZ;
plane.signbits = SignbitsForPlane( &plane );
if ( IsBoxIntersectingTriangle( vecBoxCenter, vecBoxExtents, pvTri1[ 0 ], pvTri1[ 1 ], pvTri1[ 2 ], plane, flTolerance ) )
{
return true;
}
ComputeTrianglePlane( pvTri2[ 0 ], pvTri2[ 1 ], pvTri2[ 2 ], plane.normal, plane.dist );
plane.type = PLANE_ANYZ;
plane.signbits = SignbitsForPlane( &plane );
return IsBoxIntersectingTriangle( vecBoxCenter, vecBoxExtents, pvTri2[ 0 ], pvTri2[ 1 ], pvTri2[ 2 ], plane, flTolerance );
}
bool UTIL_IsBoxIntersectingPortal( const Vector &vecBoxCenter, const Vector &vecBoxExtents, const CProp_Portal *pPortal, float flTolerance )
{
if( pPortal == NULL )
return false;
return UTIL_IsBoxIntersectingPortal( vecBoxCenter, vecBoxExtents, pPortal->GetAbsOrigin(), pPortal->GetAbsAngles(), flTolerance );
}
CProp_Portal *UTIL_IntersectEntityExtentsWithPortal( const CBaseEntity *pEntity )
{
int iPortalCount = CProp_Portal_Shared::AllPortals.Count();
if( iPortalCount == 0 )
return NULL;
Vector vMin, vMax;
pEntity->CollisionProp()->WorldSpaceAABB( &vMin, &vMax );
Vector ptCenter = ( vMin + vMax ) * 0.5f;
Vector vExtents = ( vMax - vMin ) * 0.5f;
CProp_Portal **pPortals = CProp_Portal_Shared::AllPortals.Base();
for( int i = 0; i != iPortalCount; ++i )
{
CProp_Portal *pTempPortal = pPortals[i];
if( pTempPortal->m_bActivated &&
(pTempPortal->m_hLinkedPortal.Get() != NULL) &&
UTIL_IsBoxIntersectingPortal( ptCenter, vExtents, pTempPortal ) )
{
return pPortals[i];
}
}
return NULL;
}
void UTIL_Portal_NDebugOverlay( const Vector &ptPortalCenter, const QAngle &qPortalAngles, int r, int g, int b, int a, bool noDepthTest, float duration )
{
#ifndef CLIENT_DLL
Vector pvTri1[ 3 ], pvTri2[ 3 ];
UTIL_Portal_Triangles( ptPortalCenter, qPortalAngles, pvTri1, pvTri2 );
NDebugOverlay::Triangle( pvTri1[ 0 ], pvTri1[ 1 ], pvTri1[ 2 ], r, g, b, a, noDepthTest, duration );
NDebugOverlay::Triangle( pvTri2[ 0 ], pvTri2[ 1 ], pvTri2[ 2 ], r, g, b, a, noDepthTest, duration );
#endif //#ifndef CLIENT_DLL
}
void UTIL_Portal_NDebugOverlay( const CProp_Portal *pPortal, int r, int g, int b, int a, bool noDepthTest, float duration )
{
#ifndef CLIENT_DLL
UTIL_Portal_NDebugOverlay( pPortal->GetAbsOrigin(), pPortal->GetAbsAngles(), r, g, b, a, noDepthTest, duration );
#endif //#ifndef CLIENT_DLL
}
bool FindClosestPassableSpace( CBaseEntity *pEntity, const Vector &vIndecisivePush, unsigned int fMask ) //assumes the object is already in a mostly passable space
{
if ( sv_use_find_closest_passable_space.GetBool() == false )
return true;
// Don't ever do this to entities with a move parent
if ( pEntity->GetMoveParent() )
return true;
#ifndef CLIENT_DLL
ADD_DEBUG_HISTORY( HISTORY_PLAYER_DAMAGE, UTIL_VarArgs( "RUNNING FIND CLOSEST PASSABLE SPACE on %s..\n", pEntity->GetDebugName() ) );
#endif
Vector ptExtents[8]; //ordering is going to be like 3 bits, where 0 is a min on the related axis, and 1 is a max on the same axis, axis order x y z
float fExtentsValidation[8]; //some points are more valid than others, and this is our measure
Vector vEntityMaxs;// = pEntity->WorldAlignMaxs();
Vector vEntityMins;// = pEntity->WorldAlignMins();
CCollisionProperty *pEntityCollision = pEntity->CollisionProp();
pEntityCollision->WorldSpaceAABB( &vEntityMins, &vEntityMaxs );
Vector ptEntityCenter = ((vEntityMins + vEntityMaxs) / 2.0f);
vEntityMins -= ptEntityCenter;
vEntityMaxs -= ptEntityCenter;
Vector ptEntityOriginalCenter = ptEntityCenter;
ptEntityCenter.z += 0.001f; //to satisfy m_IsSwept on first pass
int iEntityCollisionGroup = pEntity->GetCollisionGroup();
trace_t traces[2];
Ray_t entRay;
//entRay.Init( ptEntityCenter, ptEntityCenter, vEntityMins, vEntityMaxs );
entRay.m_Extents = vEntityMaxs;
entRay.m_IsRay = false;
entRay.m_IsSwept = true;
entRay.m_StartOffset = vec3_origin;
Vector vOriginalExtents = vEntityMaxs;
Vector vGrowSize = vEntityMaxs / 101.0f;
vEntityMaxs -= vGrowSize;
vEntityMins += vGrowSize;
Ray_t testRay;
testRay.m_Extents = vGrowSize;
testRay.m_IsRay = false;
testRay.m_IsSwept = true;
testRay.m_StartOffset = vec3_origin;
unsigned int iFailCount;
for( iFailCount = 0; iFailCount != 100; ++iFailCount )
{
entRay.m_Start = ptEntityCenter;
entRay.m_Delta = ptEntityOriginalCenter - ptEntityCenter;
UTIL_TraceRay( entRay, fMask, pEntity, iEntityCollisionGroup, &traces[0] );
if( traces[0].startsolid == false )
{
Vector vNewPos = traces[0].endpos + (pEntity->GetAbsOrigin() - ptEntityOriginalCenter);
#ifdef CLIENT_DLL
pEntity->SetAbsOrigin( vNewPos );
#else
pEntity->Teleport( &vNewPos, NULL, NULL );
#endif
return true; //current placement worked
}
bool bExtentInvalid[8];
for( int i = 0; i != 8; ++i )
{
fExtentsValidation[i] = 0.0f;
ptExtents[i] = ptEntityCenter;
ptExtents[i].x += ((i & (1<<0)) ? vEntityMaxs.x : vEntityMins.x);
ptExtents[i].y += ((i & (1<<1)) ? vEntityMaxs.y : vEntityMins.y);
ptExtents[i].z += ((i & (1<<2)) ? vEntityMaxs.z : vEntityMins.z);
bExtentInvalid[i] = enginetrace->PointOutsideWorld( ptExtents[i] );
}
unsigned int counter, counter2;
for( counter = 0; counter != 7; ++counter )
{
for( counter2 = counter + 1; counter2 != 8; ++counter2 )
{
testRay.m_Delta = ptExtents[counter2] - ptExtents[counter];
if( bExtentInvalid[counter] )
traces[0].startsolid = true;
else
{
testRay.m_Start = ptExtents[counter];
UTIL_TraceRay( testRay, fMask, pEntity, iEntityCollisionGroup, &traces[0] );
}
if( bExtentInvalid[counter2] )
traces[1].startsolid = true;
else
{
testRay.m_Start = ptExtents[counter2];
testRay.m_Delta = -testRay.m_Delta;
UTIL_TraceRay( testRay, fMask, pEntity, iEntityCollisionGroup, &traces[1] );
}
float fDistance = testRay.m_Delta.Length();
for( int i = 0; i != 2; ++i )
{
int iExtent = (i==0)?(counter):(counter2);
if( traces[i].startsolid )
{
fExtentsValidation[iExtent] -= 100.0f;
}
else
{
fExtentsValidation[iExtent] += traces[i].fraction * fDistance;
}
}
}
}
Vector vNewOriginDirection( 0.0f, 0.0f, 0.0f );
float fTotalValidation = 0.0f;
for( counter = 0; counter != 8; ++counter )
{
if( fExtentsValidation[counter] > 0.0f )
{
vNewOriginDirection += (ptExtents[counter] - ptEntityCenter) * fExtentsValidation[counter];
fTotalValidation += fExtentsValidation[counter];
}
}
if( fTotalValidation != 0.0f )
{
ptEntityCenter += (vNewOriginDirection / fTotalValidation);
//increase sizing
testRay.m_Extents += vGrowSize;
vEntityMaxs -= vGrowSize;
vEntityMins = -vEntityMaxs;
}
else
{
//no point was valid, apply the indecisive vector
ptEntityCenter += vIndecisivePush;
//reset sizing
testRay.m_Extents = vGrowSize;
vEntityMaxs = vOriginalExtents;
vEntityMins = -vEntityMaxs;
}
}
// X360TBD: Hits in portal devtest
AssertMsg( IsX360() || iFailCount != 100, "FindClosestPassableSpace() failure." );
return false;
}
bool UTIL_Portal_EntityIsInPortalHole( const CProp_Portal *pPortal, CBaseEntity *pEntity )
{
CCollisionProperty *pCollisionProp = pEntity->CollisionProp();
Vector vMins = pCollisionProp->OBBMins();
Vector vMaxs = pCollisionProp->OBBMaxs();
Vector vForward, vUp, vRight;
AngleVectors( pCollisionProp->GetCollisionAngles(), &vForward, &vRight, &vUp );
Vector ptOrigin = pEntity->GetAbsOrigin();
Vector ptOBBCenter = pEntity->GetAbsOrigin() + (vMins + vMaxs * 0.5f);
Vector vExtents = (vMaxs - vMins) * 0.5f;
vForward *= vExtents.x;
vRight *= vExtents.y;
vUp *= vExtents.z;
Vector vPortalForward, vPortalRight, vPortalUp;
pPortal->GetVectors( &vPortalForward, &vPortalRight, &vPortalUp );
Vector ptPortalCenter = pPortal->GetAbsOrigin();
return OBBHasFullyContainedIntersectionWithQuad( vForward, vRight, vUp, ptOBBCenter,
vPortalForward, vPortalForward.Dot( ptPortalCenter ), ptPortalCenter,
vPortalRight, PORTAL_HALF_WIDTH + 1.0f, vPortalUp, PORTAL_HALF_HEIGHT + 1.0f );
}
#ifdef CLIENT_DLL
void UTIL_TransformInterpolatedAngle( CInterpolatedVar< QAngle > &qInterped, matrix3x4_t matTransform, bool bSkipNewest )
{
int iHead = qInterped.GetHead();
if( !qInterped.IsValidIndex( iHead ) )
return;
#ifdef DBGFLAG_ASSERT
float fHeadTime;
qInterped.GetHistoryValue( iHead, fHeadTime );
#endif
float fTime;
QAngle *pCurrent;
int iCurrent;
if( bSkipNewest )
iCurrent = qInterped.GetNext( iHead );
else
iCurrent = iHead;
while( (pCurrent = qInterped.GetHistoryValue( iCurrent, fTime )) != NULL )
{
Assert( (fTime <= fHeadTime) || (iCurrent == iHead) ); //asserting that head is always newest
if( fTime < gpGlobals->curtime )
*pCurrent = TransformAnglesToWorldSpace( *pCurrent, matTransform );
iCurrent = qInterped.GetNext( iCurrent );
if( iCurrent == iHead )
break;
}
qInterped.Interpolate( gpGlobals->curtime );
}
void UTIL_TransformInterpolatedPosition( CInterpolatedVar< Vector > &vInterped, VMatrix matTransform, bool bSkipNewest )
{
int iHead = vInterped.GetHead();
if( !vInterped.IsValidIndex( iHead ) )
return;
#ifdef DBGFLAG_ASSERT
float fHeadTime;
vInterped.GetHistoryValue( iHead, fHeadTime );
#endif
float fTime;
Vector *pCurrent;
int iCurrent;
if( bSkipNewest )
iCurrent = vInterped.GetNext( iHead );
else
iCurrent = iHead;
while( (pCurrent = vInterped.GetHistoryValue( iCurrent, fTime )) != NULL )
{
Assert( (fTime <= fHeadTime) || (iCurrent == iHead) );
if( fTime < gpGlobals->curtime )
*pCurrent = matTransform * (*pCurrent);
iCurrent = vInterped.GetNext( iCurrent );
if( iCurrent == iHead )
break;
}
vInterped.Interpolate( gpGlobals->curtime );
}
#endif
#ifndef CLIENT_DLL
void CC_Debug_FixMyPosition( void )
{
CBaseEntity *pPlayer = UTIL_GetCommandClient();
FindClosestPassableSpace( pPlayer, vec3_origin );
}
static ConCommand debug_fixmyposition("debug_fixmyposition", CC_Debug_FixMyPosition, "Runs FindsClosestPassableSpace() on player.", FCVAR_CHEAT );
#endif
|