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
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
|
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose:
//
//=============================================================================//
#include "cbase.h"
#include "movevars_shared.h"
#include "ai_blended_movement.h"
#include "ai_route.h"
#include "ai_navigator.h"
#include "ai_moveprobe.h"
#include "KeyValues.h"
// memdbgon must be the last include file in a .cpp file!!!
#include "tier0/memdbgon.h"
//-----------------------------------------------------------------------------
//
// class CAI_BlendedMotor
//
BEGIN_SIMPLE_DATADESC( CAI_BlendedMotor )
// DEFINE_FIELD( m_bDeceleratingToGoal, FIELD_BOOLEAN ),
// DEFINE_FIELD( m_iPrimaryLayer, FIELD_INTEGER ),
// DEFINE_FIELD( m_iSecondaryLayer, FIELD_INTEGER ),
// DEFINE_FIELD( m_nPrimarySequence, FIELD_INTEGER ),
// DEFINE_FIELD( m_nSecondarySequence, FIELD_INTEGER ),
// DEFINE_FIELD( m_flSecondaryWeight, FIELD_FLOAT ),
// DEFINE_CUSTOM_FIELD( m_nSavedGoalActivity, ActivityDataOps() ),
// DEFINE_CUSTOM_FIELD( m_nSavedTranslatedGoalActivity, ActivityDataOps() ),
// DEFINE_FIELD( m_nGoalSequence, FIELD_INTEGER ),
// DEFINE_FIELD( m_nPrevMovementSequence, FIELD_INTEGER ),
// DEFINE_FIELD( m_nInteriorSequence, FIELD_INTEGER ),
// DEFINE_FIELD( m_flCurrRate, FIELD_FLOAT ),
// DEFINE_FIELD( m_flStartCycle, FIELD_FLOAT ),
// m_scriptMove
// m_scriptTurn
// DEFINE_FIELD( m_flNextTurnGesture, FIELD_TIME ),
// DEFINE_FIELD( m_prevYaw, FIELD_FLOAT ),
// DEFINE_FIELD( m_doTurn, FIELD_FLOAT ),
// DEFINE_FIELD( m_doLeft, FIELD_FLOAT ),
// DEFINE_FIELD( m_doRight, FIELD_FLOAT ),
// DEFINE_FIELD( m_flNextTurnAct, FIELD_TIME ),
// DEFINE_FIELD( m_flPredictiveSpeedAdjust, FIELD_FLOAT ),
// DEFINE_FIELD( m_flReactiveSpeedAdjust, FIELD_FLOAT ),
// DEFINE_FIELD( m_vecPrevOrigin1, FIELD_POSITION ),
// DEFINE_FIELD( m_vecPrevOrigin2, FIELD_POSITION ),
END_DATADESC()
//-------------------------------------
void CAI_BlendedMotor::ResetMoveCalculations()
{
BaseClass::ResetMoveCalculations();
m_scriptMove.RemoveAll();
m_scriptTurn.RemoveAll();
}
//-------------------------------------
void CAI_BlendedMotor::MoveStart()
{
AI_PROFILE_SCOPE(CAI_BlendedMotor_MoveStart);
if (m_nPrimarySequence == -1)
{
m_nPrimarySequence = GetSequence();
m_flStartCycle = GetCycle();
m_flCurrRate = 0.4;
// Assert( !GetOuter()->HasMovement( m_nStartSequence ) );
m_nSecondarySequence = -1;
m_iPrimaryLayer = AddLayeredSequence( m_nPrimarySequence, 0 );
SetLayerWeight( m_iPrimaryLayer, 0.0 );
SetLayerPlaybackRate( m_iPrimaryLayer, 0.0 );
SetLayerNoRestore( m_iPrimaryLayer, true );
SetLayerCycle( m_iPrimaryLayer, m_flStartCycle, m_flStartCycle );
m_flSecondaryWeight = 0.0;
}
else
{
// suspect that MoveStop() wasn't called when the previous route finished
// Assert( 0 );
}
if (m_nGoalSequence == ACT_INVALID)
{
ResetGoalSequence();
}
m_vecPrevOrigin2 = GetAbsOrigin();
m_vecPrevOrigin1 = GetAbsOrigin();
m_bDeceleratingToGoal = false;
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CAI_BlendedMotor::ResetGoalSequence( void )
{
m_nSavedGoalActivity = GetNavigator()->GetArrivalActivity( );
if (m_nSavedGoalActivity == ACT_INVALID)
{
m_nSavedGoalActivity = GetOuter()->GetStoppedActivity();
}
m_nSavedTranslatedGoalActivity = GetOuter()->NPC_TranslateActivity( m_nSavedGoalActivity );
m_nGoalSequence = GetNavigator()->GetArrivalSequence( m_nPrimarySequence );
// Msg("Start %s end %s\n", GetOuter()->GetSequenceName( m_nPrimarySequence ), GetOuter()->GetSequenceName( m_nGoalSequence ) );
m_nGoalSequence = GetInteriorSequence( m_nPrimarySequence );
Assert( m_nGoalSequence != ACT_INVALID );
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CAI_BlendedMotor::MoveStop()
{
AI_PROFILE_SCOPE(CAI_BlendedMotor_MoveStop);
CAI_Motor::MoveStop();
if (m_iPrimaryLayer != -1)
{
RemoveLayer( m_iPrimaryLayer, 0.2, 0.1 );
m_iPrimaryLayer = -1;
}
if (m_iSecondaryLayer != -1)
{
RemoveLayer( m_iSecondaryLayer, 0.2, 0.1 );
m_iSecondaryLayer = -1;
}
m_nPrimarySequence = ACT_INVALID;
m_nSecondarySequence = ACT_INVALID;
m_nPrevMovementSequence = ACT_INVALID;
m_nInteriorSequence = ACT_INVALID;
// int nNextSequence = FindTransitionSequence(GetSequence(), m_nIdealSequence, NULL);
}
void CAI_BlendedMotor::MovePaused()
{
CAI_Motor::MovePaused();
SetMoveScriptAnim( 0.0 );
}
void CAI_BlendedMotor::MoveContinue()
{
AI_PROFILE_SCOPE(CAI_BlendedMotor_MoveContinue);
m_nPrimarySequence = GetInteriorSequence( ACT_INVALID );
m_nGoalSequence = m_nPrimarySequence;
Assert( m_nPrimarySequence != ACT_INVALID );
if (m_nPrimarySequence == ACT_INVALID)
return;
m_flStartCycle = 0.0;
m_iPrimaryLayer = AddLayeredSequence( m_nPrimarySequence, 0 );
SetLayerWeight( m_iPrimaryLayer, 0.0 );
SetLayerPlaybackRate( m_iPrimaryLayer, 0.0 );
SetLayerNoRestore( m_iPrimaryLayer, true );
SetLayerCycle( m_iPrimaryLayer, m_flStartCycle, m_flStartCycle );
m_bDeceleratingToGoal = false;
}
//-----------------------------------------------------------------------------
// Purpose: for the MoveInterval, interpolate desired speed, calc actual distance traveled
//-----------------------------------------------------------------------------
float CAI_BlendedMotor::GetMoveScriptDist( float &flNewSpeed )
{
AI_PROFILE_SCOPE(CAI_BlendedMotor_GetMoveScriptDist);
int i;
float flTotalDist = 0;
float t = GetMoveInterval();
Assert( m_scriptMove.Count() > 1);
flNewSpeed = 0;
for (i = 0; i < m_scriptMove.Count()-1; i++)
{
if (t < m_scriptMove[i].flTime)
{
// get new velocity
float a = t / m_scriptMove[i].flTime;
flNewSpeed = m_scriptMove[i].flMaxVelocity * (1 - a) + m_scriptMove[i+1].flMaxVelocity * a;
// get distance traveled over this entry
flTotalDist += (m_scriptMove[i].flMaxVelocity + flNewSpeed) * 0.5 * t;
break;
}
else
{
// used all of entries time, get entries total movement
flNewSpeed = m_scriptMove[i+1].flMaxVelocity;
flTotalDist += m_scriptMove[i].flDist;
t -= m_scriptMove[i].flTime;
}
}
return flTotalDist;
}
//-----------------------------------------------------------------------------
// Purpose: return the total time that the move script covers
//-----------------------------------------------------------------------------
float CAI_BlendedMotor::GetMoveScriptTotalTime()
{
float flDist = GetNavigator()->GetArrivalDistance();
int i = m_scriptMove.Count() - 1;
if (i < 0)
return -1;
while (i > 0 && flDist > 1)
{
flDist -= m_scriptMove[i].flDist;
i--;
}
return m_scriptMove[i].flElapsedTime;
}
//-----------------------------------------------------------------------------
// Purpose: for the MoveInterval, interpolate desired angle
//-----------------------------------------------------------------------------
float CAI_BlendedMotor::GetMoveScriptYaw( void )
{
int i;
// interpolate desired angle
float flNewYaw = GetAbsAngles().y;
float t = GetMoveInterval();
for (i = 0; i < m_scriptTurn.Count()-1; i++)
{
if (t < m_scriptTurn[i].flTime)
{
// get new direction
float a = t / m_scriptTurn[i].flTime;
float deltaYaw = UTIL_AngleDiff( m_scriptTurn[i+1].flYaw, m_scriptTurn[i].flYaw );
flNewYaw = UTIL_AngleMod( m_scriptTurn[i].flYaw + a * deltaYaw );
break;
}
else
{
t -= m_scriptTurn[i].flTime;
}
}
return flNewYaw;
}
//-----------------------------------------------------------------------------
// Purpose: blend in the "idle" or "arrival" animation depending on speed
//-----------------------------------------------------------------------------
void CAI_BlendedMotor::SetMoveScriptAnim( float flNewSpeed )
{
AI_PROFILE_SCOPE(CAI_BlendedMotor_SetMoveScriptAnim);
// don't bother if the npc is dead
if (!GetOuter()->IsAlive())
return;
// insert ideal layers
// FIXME: needs full transitions, as well as starting vs stopping sequences, leaning, etc.
CAI_Navigator *pNavigator = GetNavigator();
SetPlaybackRate( m_flCurrRate );
// calc weight of idle animation layer that suppresses the run animation
float flWeight = 0.0f;
if (GetIdealSpeed() > 0.0f)
{
flWeight = 1.0f - (flNewSpeed / (GetIdealSpeed() * GetPlaybackRate()));
}
if (flWeight < 0.0f)
{
m_flCurrRate = flNewSpeed / GetIdealSpeed();
m_flCurrRate = clamp( m_flCurrRate, 0.0f, 1.0f );
SetPlaybackRate( m_flCurrRate );
flWeight = 0.0;
}
// Msg("weight %.3f rate %.3f\n", flWeight, m_flCurrRate );
m_flCurrRate = MIN( m_flCurrRate + (1.0 - m_flCurrRate) * 0.8f, 1.0f );
if (m_nSavedGoalActivity == ACT_INVALID)
{
ResetGoalSequence();
}
// detect state change
Activity activity = GetOuter()->NPC_TranslateActivity( m_nSavedGoalActivity );
if ( activity != m_nSavedTranslatedGoalActivity )
{
m_nSavedTranslatedGoalActivity = activity;
m_nInteriorSequence = ACT_INVALID;
m_nGoalSequence = pNavigator->GetArrivalSequence( m_nPrimarySequence );
}
if (m_bDeceleratingToGoal)
{
// find that sequence to play when at goal
m_nGoalSequence = pNavigator->GetArrivalSequence( m_nPrimarySequence );
if (m_nGoalSequence == ACT_INVALID)
{
m_nGoalSequence = GetInteriorSequence( m_nPrimarySequence );
}
Assert( m_nGoalSequence != ACT_INVALID );
}
if (m_flSecondaryWeight == 1.0 || (m_iSecondaryLayer != -1 && m_nPrimarySequence == m_nSecondarySequence))
{
// secondary layer at full strength last time, delete the primary and shift down
RemoveLayer( m_iPrimaryLayer, 0.0, 0.0 );
m_iPrimaryLayer = m_iSecondaryLayer;
m_nPrimarySequence = m_nSecondarySequence;
m_iSecondaryLayer = -1;
m_nSecondarySequence = ACT_INVALID;
m_flSecondaryWeight = 0.0;
}
// look for transition sequence if needed
if (m_nSecondarySequence == ACT_INVALID)
{
if (!m_bDeceleratingToGoal && m_nGoalSequence != GetInteriorSequence( m_nPrimarySequence ))
{
// strob interior sequence in case it changed
m_nGoalSequence = GetInteriorSequence( m_nPrimarySequence );
}
if (m_nGoalSequence != ACT_INVALID && m_nPrimarySequence != m_nGoalSequence)
{
// Msg("From %s to %s\n", GetOuter()->GetSequenceName( m_nPrimarySequence ), GetOuter()->GetSequenceName( m_nGoalSequence ) );
m_nSecondarySequence = GetOuter()->FindTransitionSequence(m_nPrimarySequence, m_nGoalSequence, NULL);
if (m_nSecondarySequence == ACT_INVALID)
m_nSecondarySequence = m_nGoalSequence;
}
}
// set blending for
if (m_nSecondarySequence != ACT_INVALID)
{
if (m_iSecondaryLayer == -1)
{
m_iSecondaryLayer = AddLayeredSequence( m_nSecondarySequence, 0 );
SetLayerWeight( m_iSecondaryLayer, 0.0 );
if (m_nSecondarySequence == m_nGoalSequence)
{
SetLayerPlaybackRate( m_iSecondaryLayer, 0.0 );
}
else
{
SetLayerPlaybackRate( m_iSecondaryLayer, 1.0 );
}
SetLayerNoRestore( m_iSecondaryLayer, true );
m_flSecondaryWeight = 0.0;
}
m_flSecondaryWeight = MIN( m_flSecondaryWeight + 0.3, 1.0 );
if (m_flSecondaryWeight < 1.0)
{
SetLayerWeight( m_iPrimaryLayer, (flWeight - m_flSecondaryWeight * flWeight) / (1.0f - m_flSecondaryWeight * flWeight) );
SetLayerWeight( m_iSecondaryLayer, flWeight * m_flSecondaryWeight );
}
else
{
SetLayerWeight( m_iPrimaryLayer, 0.0f );
SetLayerWeight( m_iSecondaryLayer, flWeight );
}
}
else
{
// recreate layer if missing
if (m_iPrimaryLayer == -1)
{
MoveContinue();
}
// try to catch a stale layer
if (m_iSecondaryLayer != -1)
{
// secondary layer at full strength last time, delete the primary and shift down
RemoveLayer( m_iSecondaryLayer, 0.0, 0.0 );
m_iSecondaryLayer = -1;
m_nSecondarySequence = ACT_INVALID;
m_flSecondaryWeight = 0.0;
}
// debounce
// flWeight = flWeight * 0.5 + 0.5 * GetOuter()->GetLayerWeight( m_iPrimaryLayer );
SetLayerWeight( m_iPrimaryLayer, flWeight );
}
}
//-----------------------------------------------------------------------------
// Purpose: get the "idle" animation to play as the compliment to the movement animation
//-----------------------------------------------------------------------------
int CAI_BlendedMotor::GetInteriorSequence( int fromSequence )
{
AI_PROFILE_SCOPE(CAI_BlendedMotor_GetInteriorSequence);
// FIXME: add interior activity to path, just like arrival activity.
int sequence = GetNavigator()->GetMovementSequence();
if (m_nInteriorSequence != ACT_INVALID && sequence == m_nPrevMovementSequence)
{
return m_nInteriorSequence;
}
m_nPrevMovementSequence = sequence;
KeyValues *seqKeyValues = GetOuter()->GetSequenceKeyValues( sequence );
// Msg("sequence %d : %s (%d)\n", sequence, GetOuter()->GetSequenceName( sequence ), seqKeyValues != NULL );
if (seqKeyValues)
{
KeyValues *pkvInterior = seqKeyValues->FindKey("interior");
if (pkvInterior)
{
const char *szActivity = pkvInterior->GetString();
Activity activity = ( Activity )GetOuter()->LookupActivity( szActivity );
if ( activity != ACT_INVALID )
{
m_nInteriorSequence = GetOuter()->SelectWeightedSequence( GetOuter()->TranslateActivity( activity ), fromSequence );
}
else
{
activity = (Activity)GetOuter()->GetActivityID( szActivity );
if ( activity != ACT_INVALID )
{
m_nInteriorSequence = GetOuter()->SelectWeightedSequence( GetOuter()->TranslateActivity( activity ), fromSequence );
}
}
if (activity == ACT_INVALID || m_nInteriorSequence == ACT_INVALID)
{
m_nInteriorSequence = GetOuter()->LookupSequence( szActivity );
}
}
}
if (m_nInteriorSequence == ACT_INVALID)
{
Activity activity = GetNavigator()->GetMovementActivity();
if (activity == ACT_WALK_AIM || activity == ACT_RUN_AIM)
{
activity = ACT_IDLE_ANGRY;
}
else
{
activity = ACT_IDLE;
}
m_nInteriorSequence = GetOuter()->SelectWeightedSequence( GetOuter()->TranslateActivity( activity ), fromSequence );
Assert( m_nInteriorSequence != ACT_INVALID );
}
return m_nInteriorSequence;
}
//-----------------------------------------------------------------------------
// Purpose: Move the npc to the next location on its route.
//-----------------------------------------------------------------------------
AIMotorMoveResult_t CAI_BlendedMotor::MoveGroundExecute( const AILocalMoveGoal_t &move, AIMoveTrace_t *pTraceResult )
{
AI_PROFILE_SCOPE(CAI_BlendedMotor_MoveGroundExecute);
if ( move.curExpectedDist < 0.001 )
{
AIMotorMoveResult_t result = BaseClass::MoveGroundExecute( move, pTraceResult );
// Msg(" BaseClass::MoveGroundExecute() - remaining %.2f\n", GetMoveInterval() );
SetMoveScriptAnim( 0.0 );
return result;
}
BuildMoveScript( move, pTraceResult );
float flNewSpeed = GetCurSpeed();
float flTotalDist = GetMoveScriptDist( flNewSpeed );
Assert( move.maxDist < 0.01 || flTotalDist > 0.0 );
// --------------------------------------------
// turn in the direction of movement
// --------------------------------------------
float flNewYaw = GetMoveScriptYaw( );
// get facing based on movement yaw
AILocalMoveGoal_t move2 = move;
move2.facing = UTIL_YawToVector( flNewYaw );
// turn in the direction needed
MoveFacing( move2 );
// reset actual "sequence" ground speed based current movement sequence, orientation
// FIXME: this should be based on
GetOuter()->m_flGroundSpeed = GetSequenceGroundSpeed( GetSequence());
/*
if (1 || flNewSpeed > GetIdealSpeed())
{
// DevMsg( "%6.2f : Speed %.1f : %.1f (%.1f) : %d\n", gpGlobals->curtime, flNewSpeed, move.maxDist, move.transitionDist, GetOuter()->m_pHintNode != NULL );
// DevMsg( "%6.2f : Speed %.1f : %.1f\n", gpGlobals->curtime, flNewSpeed, GetIdealSpeed() );
}
*/
SetMoveScriptAnim( flNewSpeed );
/*
if ((GetOuter()->m_debugOverlays & OVERLAY_NPC_SELECTED_BIT))
{
DevMsg( "%6.2f : Speed %.1f : %.1f : %.2f\n", gpGlobals->curtime, flNewSpeed, GetIdealSpeed(), flNewSpeed / GetIdealSpeed() );
}
*/
AIMotorMoveResult_t result = MoveGroundExecuteWalk( move, flNewSpeed, flTotalDist, pTraceResult );
return result;
}
AIMotorMoveResult_t CAI_BlendedMotor::MoveFlyExecute( const AILocalMoveGoal_t &move, AIMoveTrace_t *pTraceResult )
{
AI_PROFILE_SCOPE(CAI_BlendedMotor_MoveFlyExecute);
if ( move.curExpectedDist < 0.001 )
return BaseClass::MoveFlyExecute( move, pTraceResult );
BuildMoveScript( move, pTraceResult );
float flNewSpeed = GetCurSpeed();
float flTotalDist = GetMoveScriptDist( flNewSpeed );
Assert( move.maxDist < 0.01 || flTotalDist > 0.0 );
// --------------------------------------------
// turn in the direction of movement
// --------------------------------------------
float flNewYaw = GetMoveScriptYaw( );
// get facing based on movement yaw
AILocalMoveGoal_t move2 = move;
move2.facing = UTIL_YawToVector( flNewYaw );
// turn in the direction needed
MoveFacing( move2 );
GetOuter()->m_flGroundSpeed = GetSequenceGroundSpeed( GetSequence());
SetMoveScriptAnim( flNewSpeed );
// DevMsg( "%6.2f : Speed %.1f : %.1f\n", gpGlobals->curtime, flNewSpeed, GetIdealSpeed() );
// reset actual "sequence" ground speed based current movement sequence, orientation
// FIXME: the above is redundant with MoveGroundExecute, and the below is a mix of MoveGroundExecuteWalk and MoveFlyExecute
bool bReachingLocalGoal = ( flTotalDist > move.maxDist );
// can I move farther in this interval than I'm supposed to?
if ( bReachingLocalGoal )
{
if ( !(move.flags & AILMG_CONSUME_INTERVAL) )
{
// only use a portion of the time interval
SetMoveInterval( GetMoveInterval() * (1 - move.maxDist / flTotalDist) );
}
else
SetMoveInterval( 0 );
flTotalDist = move.maxDist;
}
else
{
// use all the time
SetMoveInterval( 0 );
}
SetMoveVel( move.dir * flNewSpeed );
// orig
Vector vecStart, vecEnd;
vecStart = GetLocalOrigin();
VectorMA( vecStart, flTotalDist, move.dir, vecEnd );
AIMoveTrace_t moveTrace;
GetMoveProbe()->MoveLimit( NAV_FLY, vecStart, vecEnd, MASK_NPCSOLID, NULL, &moveTrace );
if ( pTraceResult )
*pTraceResult = moveTrace;
// Check for total blockage
if (fabs(moveTrace.flDistObstructed - flTotalDist) <= 1e-1)
{
// But if we bumped into our target, then we succeeded!
if ( move.pMoveTarget && (moveTrace.pObstruction == move.pMoveTarget) )
return AIM_PARTIAL_HIT_TARGET;
return AIM_FAILED;
}
// The true argument here causes it to touch all triggers
// in the volume swept from the previous position to the current position
UTIL_SetOrigin(GetOuter(), moveTrace.vEndPosition, true);
return (IsMoveBlocked(moveTrace.fStatus)) ? AIM_PARTIAL_HIT_WORLD : AIM_SUCCESS;
}
float CAI_BlendedMotor::OverrideMaxYawSpeed( Activity activity )
{
// Don't do this is we're locked
if ( IsYawLocked() )
return 0.0f;
switch( activity )
{
case ACT_TURN_LEFT:
case ACT_TURN_RIGHT:
return 45;
break;
default:
if (GetOuter()->IsMoving())
{
return 15;
}
return 45; // too fast?
break;
}
return -1;
}
void CAI_BlendedMotor::UpdateYaw( int speed )
{
// Don't do this is we're locked
if ( IsYawLocked() )
return;
GetOuter()->UpdateTurnGesture( );
BaseClass::UpdateYaw( speed );
}
void CAI_BlendedMotor::RecalculateYawSpeed()
{
// Don't do this is we're locked
if ( IsYawLocked() )
{
SetYawSpeed( 0.0f );
return;
}
if (GetOuter()->HasMemory( bits_MEMORY_TURNING ))
return;
SetYawSpeed( CalcYawSpeed() );
}
//-------------------------------------
void CAI_BlendedMotor::MoveClimbStart( const Vector &climbDest, const Vector &climbDir, float climbDist, float yaw )
{
// TODO: merge transitions with movement script
if (m_iPrimaryLayer != -1)
{
SetLayerWeight( m_iPrimaryLayer, 0 );
}
if (m_iSecondaryLayer != -1)
{
SetLayerWeight( m_iSecondaryLayer, 0 );
}
BaseClass::MoveClimbStart( climbDest, climbDir, climbDist, yaw );
}
//-------------------------------------
void CAI_BlendedMotor::MoveJumpStart( const Vector &velocity )
{
// TODO: merge transitions with movement script
if (m_iPrimaryLayer != -1)
{
SetLayerWeight( m_iPrimaryLayer, 0 );
}
if (m_iSecondaryLayer != -1)
{
SetLayerWeight( m_iSecondaryLayer, 0 );
}
BaseClass::MoveJumpStart( velocity );
}
//-------------------------------------
void CAI_BlendedMotor::BuildMoveScript( const AILocalMoveGoal_t &move, AIMoveTrace_t *pTraceResult )
{
m_scriptMove.RemoveAll();
m_scriptTurn.RemoveAll();
BuildVelocityScript( move );
BuildTurnScript( move );
/*
if (GetOuter()->m_debugOverlays & OVERLAY_NPC_SELECTED_BIT)
{
int i;
#if 1
for (i = 1; i < m_scriptMove.Count(); i++)
{
NDebugOverlay::Line( m_scriptMove[i-1].vecLocation, m_scriptMove[i].vecLocation, 255,255,255, true, 0.1 );
NDebugOverlay::Box( m_scriptMove[i].vecLocation, Vector( -2, -2, -2 ), Vector( 2, 2, 2 ), 0,255,255, 0, 0.1 );
//NDebugOverlay::Line( m_scriptMove[i].vecLocation, m_scriptMove[i].vecLocation + Vector( 0,0,m_scriptMove[i].flMaxVelocity), 0,255,255, true, 0.1 );
Vector vecMidway = m_scriptMove[i].vecLocation + ((m_scriptMove[i-1].vecLocation - m_scriptMove[i].vecLocation) * 0.5);
NDebugOverlay::Text( vecMidway, UTIL_VarArgs( "%d", i ), false, 0.1 );
}
#endif
#if 0
for (i = 1; i < m_scriptTurn.Count(); i++)
{
NDebugOverlay::Line( m_scriptTurn[i-1].vecLocation, m_scriptTurn[i].vecLocation, 255,255,255, true, 0.1 );
NDebugOverlay::Box( m_scriptTurn[i].vecLocation, Vector( -2, -2, -2 ), Vector( 2, 2, 2 ), 255,0,0, 0, 0.1 );
NDebugOverlay::Line( m_scriptTurn[i].vecLocation + Vector( 0,0,1), m_scriptTurn[i].vecLocation + Vector( 0,0,1) + UTIL_YawToVector( m_scriptTurn[i].flYaw ) * 32, 255,0,0, true, 0.1 );
}
#endif
}
*/
}
#define YAWSPEED 150
void CAI_BlendedMotor::BuildTurnScript( const AILocalMoveGoal_t &move )
{
AI_PROFILE_SCOPE(CAI_BlendedMotor_BuildTurnScript);
int i;
AI_Movementscript_t script;
script.Init();
// current location
script.vecLocation = GetAbsOrigin();
script.flYaw = GetAbsAngles().y;
m_scriptTurn.AddToTail( script );
//-------------------------
// insert default turn parameters, try to turn 80% to goal at all corners before getting there
int prev = 0;
for (i = 0; i < m_scriptMove.Count(); i++)
{
AI_Waypoint_t *pCurWaypoint = m_scriptMove[i].pWaypoint;
if (pCurWaypoint)
{
script.Init();
script.vecLocation = pCurWaypoint->vecLocation;
script.pWaypoint = pCurWaypoint;
script.flElapsedTime = m_scriptMove[i].flElapsedTime;
m_scriptTurn[prev].flTime = script.flElapsedTime - m_scriptTurn[prev].flElapsedTime;
if (pCurWaypoint->GetNext())
{
Vector d1 = pCurWaypoint->GetNext()->vecLocation - script.vecLocation;
Vector d2 = script.vecLocation - m_scriptTurn[prev].vecLocation;
d1.z = 0;
VectorNormalize( d1 );
d2.z = 0;
VectorNormalize( d2 );
float y1 = UTIL_VecToYaw( d1 );
float y2 = UTIL_VecToYaw( d2 );
float deltaYaw = fabs( UTIL_AngleDiff( y1, y2 ) );
if (deltaYaw > 0.1)
{
// turn to 80% of goal
script.flYaw = UTIL_ApproachAngle( y1, y2, deltaYaw * 0.8 );
m_scriptTurn.AddToTail( script );
// DevMsg("turn waypoint %.1f %.1f %.1f\n", script.vecLocation.x, script.vecLocation.y, script.vecLocation.z );
prev++;
}
}
else
{
Vector vecDir = GetNavigator()->GetArrivalDirection();
script.flYaw = UTIL_VecToYaw( vecDir );
m_scriptTurn.AddToTail( script );
// DevMsg("turn waypoint %.1f %.1f %.1f\n", script.vecLocation.x, script.vecLocation.y, script.vecLocation.z );
prev++;
}
}
}
// propagate ending facing back over any nearby nodes
// FIXME: this needs to minimize total turning, not just local/end turning.
// depending on waypoint spacing, complexity, it may turn the wrong way!
for (i = m_scriptTurn.Count()-1; i > 1; i--)
{
float deltaYaw = UTIL_AngleDiff( m_scriptTurn[i-1].flYaw, m_scriptTurn[i].flYaw );
float maxYaw = YAWSPEED * m_scriptTurn[i-1].flTime;
if (fabs(deltaYaw) > maxYaw)
{
m_scriptTurn[i-1].flYaw = UTIL_ApproachAngle( m_scriptTurn[i-1].flYaw, m_scriptTurn[i].flYaw, maxYaw );
}
}
for (i = 0; i < m_scriptTurn.Count() - 1; )
{
i = i + BuildTurnScript( i, i + 1 ) + 1;
}
//-------------------------
}
int CAI_BlendedMotor::BuildTurnScript( int i, int j )
{
AI_PROFILE_SCOPE(CAI_BlendedMotor_BuildTurnScript2);
int k;
Vector vecDir = m_scriptTurn[j].vecLocation - m_scriptTurn[i].vecLocation;
float interiorYaw = UTIL_VecToYaw( vecDir );
float deltaYaw;
deltaYaw = fabs( UTIL_AngleDiff( interiorYaw, m_scriptTurn[i].flYaw ) );
float t1 = deltaYaw / YAWSPEED;
deltaYaw = fabs( UTIL_AngleDiff( m_scriptTurn[j].flYaw, interiorYaw ) );
float t2 = deltaYaw / YAWSPEED;
float totalTime = m_scriptTurn[j].flElapsedTime - m_scriptTurn[i].flElapsedTime;
Assert( totalTime > 0 );
if (t1 < 0.01)
{
if (t2 > totalTime * 0.8)
{
// too close, nothing to do
return 0;
}
// go ahead and force yaw
m_scriptTurn[i].flYaw = interiorYaw;
// we're already aiming close enough to the interior yaw, set the point where we need to blend out
k = BuildInsertNode( i, totalTime - t2 );
m_scriptTurn[k].flYaw = interiorYaw;
return 1;
}
else if (t2 < 0.01)
{
if (t1 > totalTime * 0.8)
{
// too close, nothing to do
return 0;
}
// we'll finish up aiming close enough to the interior yaw, set the point where we need to blend in
k = BuildInsertNode( i, t1 );
m_scriptTurn[k].flYaw = interiorYaw;
return 1;
}
else if (t1 + t2 > totalTime)
{
// don't bother with interior node
return 0;
// waypoints need to much turning, ignore interior yaw
float a = (t1 / (t1 + t2));
t1 = a * totalTime;
k = BuildInsertNode( i, t1 );
deltaYaw = UTIL_AngleDiff( m_scriptTurn[j].flYaw, m_scriptTurn[i].flYaw );
m_scriptTurn[k].flYaw = UTIL_ApproachAngle( m_scriptTurn[j].flYaw, m_scriptTurn[i].flYaw, deltaYaw * (1 - a) );
return 1;
}
else if (t1 + t2 < totalTime * 0.8)
{
// turn to face interior, run a ways, then turn away
k = BuildInsertNode( i, t1 );
m_scriptTurn[k].flYaw = interiorYaw;
k = BuildInsertNode( i, t2 );
m_scriptTurn[k].flYaw = interiorYaw;
return 2;
}
return 0;
}
int CAI_BlendedMotor::BuildInsertNode( int i, float flTime )
{
AI_Movementscript_t script;
script.Init();
Assert( flTime > 0.0 );
for (i; i < m_scriptTurn.Count() - 1; i++)
{
if (m_scriptTurn[i].flTime < flTime)
{
flTime -= m_scriptTurn[i].flTime;
}
else
{
float a = flTime / m_scriptTurn[i].flTime;
script.flTime = (m_scriptTurn[i].flTime - flTime);
m_scriptTurn[i].flTime = flTime;
script.flElapsedTime = m_scriptTurn[i].flElapsedTime * (1 - a) + m_scriptTurn[i+1].flElapsedTime * a;
script.vecLocation = m_scriptTurn[i].vecLocation * (1 - a) + m_scriptTurn[i+1].vecLocation * a;
m_scriptTurn.InsertAfter( i, script );
return i + 1;
}
}
Assert( 0 );
return 0;
}
ConVar ai_path_insert_pause_at_obstruction( "ai_path_insert_pause_at_obstruction", "1" );
ConVar ai_path_adjust_speed_on_immediate_turns( "ai_path_adjust_speed_on_immediate_turns", "1" );
ConVar ai_path_insert_pause_at_est_end( "ai_path_insert_pause_at_est_end", "1" );
#define MIN_VELOCITY 0.0f
#define MIN_STEER_DOT 0.0f
void CAI_BlendedMotor::BuildVelocityScript( const AILocalMoveGoal_t &move )
{
AI_PROFILE_SCOPE(CAI_BlendedMotor_BuildVelocityScript);
int i;
float a;
float idealVelocity = GetIdealSpeed();
if (idealVelocity == 0)
{
idealVelocity = 50;
}
float idealAccel = GetIdealAccel();
if (idealAccel == 0)
{
idealAccel = 100;
}
AI_Movementscript_t script;
// set current location as start of script
script.vecLocation = GetAbsOrigin();
script.flMaxVelocity = GetCurSpeed();
m_scriptMove.AddToTail( script );
//-------------------------
extern ConVar npc_height_adjust;
if (npc_height_adjust.GetBool() && move.bHasTraced && move.directTrace.flTotalDist != move.thinkTrace.flTotalDist)
{
float flDist = (move.directTrace.vEndPosition - m_scriptMove[0].vecLocation).Length2D();
float flHeight = move.directTrace.vEndPosition.z - m_scriptMove[0].vecLocation.z;
float flDelta;
if (flDist > 0)
{
flDelta = flHeight / flDist;
}
else
{
flDelta = 0;
}
m_flPredictiveSpeedAdjust = 1.1 - fabs( flDelta );
m_flPredictiveSpeedAdjust = clamp( m_flPredictiveSpeedAdjust, (flHeight > 0.0f) ? 0.5f : 0.8f, 1.0f );
/*
if ((GetOuter()->m_debugOverlays & OVERLAY_NPC_SELECTED_BIT))
{
Msg("m_flPredictiveSpeedAdjust %.3f %.1f %.1f\n", m_flPredictiveSpeedAdjust, flHeight, flDist );
NDebugOverlay::Box( move.directTrace.vEndPosition, Vector( -2, -2, -2 ), Vector( 2, 2, 2 ), 0,255,255, 0, 0.12 );
}
*/
}
if (npc_height_adjust.GetBool())
{
float flDist = (move.thinkTrace.vEndPosition - m_vecPrevOrigin2).Length2D();
float flHeight = move.thinkTrace.vEndPosition.z - m_vecPrevOrigin2.z;
float flDelta;
if (flDist > 0)
{
flDelta = flHeight / flDist;
}
else
{
flDelta = 0;
}
float newSpeedAdjust = 1.1 - fabs( flDelta );
newSpeedAdjust = clamp( newSpeedAdjust, (flHeight > 0.0f) ? 0.5f : 0.8f, 1.0f );
// debounce speed adjust
if (newSpeedAdjust < m_flReactiveSpeedAdjust)
{
m_flReactiveSpeedAdjust = m_flReactiveSpeedAdjust * 0.2f + newSpeedAdjust * 0.8f;
}
else
{
m_flReactiveSpeedAdjust = m_flReactiveSpeedAdjust * 0.5f + newSpeedAdjust * 0.5f;
}
// filter through origins
m_vecPrevOrigin2 = m_vecPrevOrigin1;
m_vecPrevOrigin1 = GetAbsOrigin();
/*
if ((GetOuter()->m_debugOverlays & OVERLAY_NPC_SELECTED_BIT))
{
NDebugOverlay::Box( m_vecPrevOrigin2, Vector( -2, -2, -2 ), Vector( 2, 2, 2 ), 255,0,255, 0, 0.12 );
NDebugOverlay::Box( move.thinkTrace.vEndPosition, Vector( -2, -2, -2 ), Vector( 2, 2, 2 ), 255,0,255, 0, 0.12 );
Msg("m_flReactiveSpeedAdjust %.3f %.1f %.1f\n", m_flReactiveSpeedAdjust, flHeight, flDist );
}
*/
}
idealVelocity = idealVelocity * MIN( m_flReactiveSpeedAdjust, m_flPredictiveSpeedAdjust );
//-------------------------
bool bAddedExpected = false;
// add all waypoint locations and velocities
AI_Waypoint_t *pCurWaypoint = GetNavigator()->GetPath()->GetCurWaypoint();
// there has to be at least one waypoint
Assert( pCurWaypoint );
while (pCurWaypoint && (pCurWaypoint->NavType() == NAV_GROUND || pCurWaypoint->NavType() == NAV_FLY) /*&& flTotalDist / idealVelocity < 3.0*/) // limit lookahead to 3 seconds
{
script.Init();
AI_Waypoint_t *pNext = pCurWaypoint->GetNext();
if (ai_path_adjust_speed_on_immediate_turns.GetBool() && !bAddedExpected)
{
// hack in next expected immediate location for move
script.vecLocation = GetAbsOrigin() + move.dir * move.curExpectedDist;
bAddedExpected = true;
pNext = pCurWaypoint;
}
else
{
script.vecLocation = pCurWaypoint->vecLocation;
script.pWaypoint = pCurWaypoint;
}
//DevMsg("waypoint %.1f %.1f %.1f\n", script.vecLocation.x, script.vecLocation.y, script.vecLocation.z );
if (pNext)
{
switch( pNext->NavType())
{
case NAV_GROUND:
case NAV_FLY:
{
Vector d1 = pNext->vecLocation - script.vecLocation;
Vector d2 = script.vecLocation - m_scriptMove[m_scriptMove.Count()-1].vecLocation;
// remove very short, non terminal ground links
// FIXME: is this safe? Maybe just check for co-located ground points?
if (d1.Length2D() < 1.0)
{
/*
if (m_scriptMove.Count() > 1)
{
int i = m_scriptMove.Count() - 1;
m_scriptMove[i].vecLocation = pCurWaypoint->vecLocation;
m_scriptMove[i].pWaypoint = pCurWaypoint;
}
*/
pCurWaypoint = pNext;
continue;
}
d1.z = 0;
VectorNormalize( d1 );
d2.z = 0;
VectorNormalize( d2 );
// figure velocity
float dot = (DotProduct( d1, d2 ) + 0.2);
if (dot > 0)
{
dot = clamp( dot, 0.0f, 1.0f );
script.flMaxVelocity = idealVelocity * dot;
}
else
{
script.flMaxVelocity = 0;
}
}
break;
case NAV_JUMP:
// FIXME: information about what the jump should look like isn't stored in the waypoints
// this'll need to call
// GetMoveProbe()->MoveLimit( NAV_JUMP, GetLocalOrigin(), GetPath()->CurWaypointPos(), MASK_NPCSOLID, GetNavTargetEntity(), &moveTrace );
// to get how far/fast the jump will be, but this is also stateless, so it'd call it per frame.
// So far it's not clear that the moveprobe doesn't also call this.....
{
float minJumpHeight = 0;
float maxHorzVel = MAX( GetCurSpeed(), 100 );
float gravity = GetCurrentGravity() * GetOuter()->GetGravity();
Vector vecApex;
Vector rawJumpVel = GetMoveProbe()->CalcJumpLaunchVelocity(script.vecLocation, pNext->vecLocation, gravity, &minJumpHeight, maxHorzVel, &vecApex );
script.flMaxVelocity = rawJumpVel.Length2D();
// Msg("%.1f\n", script.flMaxVelocity );
}
break;
case NAV_CLIMB:
{
/*
CAI_Node *pClimbNode = GetNavigator()->GetNetwork()->GetNode(pNext->iNodeID);
check: pClimbNode->m_eNodeInfo
bits_NODE_CLIMB_BOTTOM,
bits_NODE_CLIMB_ON,
bits_NODE_CLIMB_OFF_FORWARD,
bits_NODE_CLIMB_OFF_LEFT,
bits_NODE_CLIMB_OFF_RIGHT
*/
script.flMaxVelocity = 0;
}
break;
/*
case NAV_FLY:
// FIXME: can there be a NAV_GROUND -> NAV_FLY transition?
script.flMaxVelocity = 0;
break;
*/
}
}
else
{
script.flMaxVelocity = GetNavigator()->GetArrivalSpeed();
// Assert( script.flMaxVelocity == 0 );
}
m_scriptMove.AddToTail( script );
pCurWaypoint = pNext;
}
//-------------------------
// update distances
float flTotalDist = 0;
for (i = 0; i < m_scriptMove.Count() - 1; i++ )
{
flTotalDist += m_scriptMove[i].flDist = (m_scriptMove[i+1].vecLocation - m_scriptMove[i].vecLocation).Length2D();
}
//-------------------------
if ( !m_bDeceleratingToGoal && m_scriptMove.Count() && flTotalDist > 0 )
{
float flNeededAccel = DeltaV( m_scriptMove[0].flMaxVelocity, m_scriptMove[m_scriptMove.Count() - 1].flMaxVelocity, flTotalDist );
m_bDeceleratingToGoal = (flNeededAccel < -idealAccel);
//Assert( flNeededAccel != idealAccel);
}
//-------------------------
// insert slowdown points due to blocking
if (ai_path_insert_pause_at_obstruction.GetBool() && move.directTrace.pObstruction)
{
float distToObstruction = (move.directTrace.vEndPosition - m_scriptMove[0].vecLocation).Length2D();
// HACK move obstruction out "stepsize" to account for it being based on stand position and not a trace
distToObstruction = distToObstruction + 16;
InsertSlowdown( distToObstruction, idealAccel, false );
}
if (ai_path_insert_pause_at_est_end.GetBool() && GetNavigator()->GetArrivalDistance() > 0.0)
{
InsertSlowdown( flTotalDist - GetNavigator()->GetArrivalDistance(), idealAccel, true );
}
// calc initial velocity based on immediate direction changes
if ( ai_path_adjust_speed_on_immediate_turns.GetBool() && m_scriptMove.Count() > 1)
{
/*
if ((GetOuter()->m_debugOverlays & OVERLAY_NPC_SELECTED_BIT))
{
Vector tmp = m_scriptMove[1].vecLocation - m_scriptMove[0].vecLocation;
VectorNormalize( tmp );
NDebugOverlay::Line( m_scriptMove[0].vecLocation + Vector( 0, 0, 10 ), m_scriptMove[0].vecLocation + tmp * 32 + Vector( 0, 0, 10 ), 255,255,255, true, 0.1 );
NDebugOverlay::Line( m_scriptMove[0].vecLocation + Vector( 0, 0, 10 ), m_scriptMove[1].vecLocation + Vector( 0, 0, 10 ), 255,0,0, true, 0.1 );
tmp = GetCurVel();
VectorNormalize( tmp );
NDebugOverlay::Line( m_scriptMove[0].vecLocation + Vector( 0, 0, 10 ), m_scriptMove[0].vecLocation + tmp * 32 + Vector( 0, 0, 10 ), 0,0,255, true, 0.1 );
}
*/
Vector d1 = m_scriptMove[1].vecLocation - m_scriptMove[0].vecLocation;
d1.z = 0;
VectorNormalize( d1 );
Vector d2 = GetCurVel();
d2.z = 0;
VectorNormalize( d2 );
float dot = (DotProduct( d1, d2 ) + MIN_STEER_DOT);
dot = clamp( dot, 0.0f, 1.0f );
m_scriptMove[0].flMaxVelocity = m_scriptMove[0].flMaxVelocity * dot;
}
// clamp forward velocities
for (i = 0; i < m_scriptMove.Count() - 1; i++ )
{
// find needed acceleration
float dv = m_scriptMove[i+1].flMaxVelocity - m_scriptMove[i].flMaxVelocity;
if (dv > 0.0)
{
// find time, distance to accel to next max vel
float t1 = dv / idealAccel;
float d1 = m_scriptMove[i].flMaxVelocity * t1 + 0.5 * (idealAccel) * t1 * t1;
// is there enough distance
if (d1 > m_scriptMove[i].flDist)
{
float r1, r2;
// clamp the next velocity to the possible accel in the given distance
if (SolveQuadratic( 0.5 * idealAccel, m_scriptMove[i].flMaxVelocity, -m_scriptMove[i].flDist, r1, r2 ))
{
m_scriptMove[i+1].flMaxVelocity = m_scriptMove[i].flMaxVelocity + idealAccel * r1;
}
}
}
}
// clamp decel velocities
for (i = m_scriptMove.Count() - 1; i > 0; i-- )
{
// find needed deceleration
float dv = m_scriptMove[i].flMaxVelocity - m_scriptMove[i-1].flMaxVelocity;
if (dv < 0.0)
{
// find time, distance to decal to next max vel
float t1 = -dv / idealAccel;
float d1 = m_scriptMove[i].flMaxVelocity * t1 + 0.5 * (idealAccel) * t1 * t1;
// is there enough distance
if (d1 > m_scriptMove[i-1].flDist)
{
float r1, r2;
// clamp the next velocity to the possible decal in the given distance
if (SolveQuadratic( 0.5 * idealAccel, m_scriptMove[i].flMaxVelocity, -m_scriptMove[i-1].flDist, r1, r2 ))
{
m_scriptMove[i-1].flMaxVelocity = m_scriptMove[i].flMaxVelocity + idealAccel * r1;
}
}
}
}
/*
for (i = 0; i < m_scriptMove.Count(); i++)
{
NDebugOverlay::Text( m_scriptMove[i].vecLocation, (const char *)CFmtStr( "%.2f ", m_scriptMove[i].flMaxVelocity ), false, 0.1 );
// DevMsg("%.2f ", m_scriptMove[i].flMaxVelocity );
}
// DevMsg("\n");
*/
// insert intermediate ideal velocities
for (i = 0; i < m_scriptMove.Count() - 1;)
{
// accel to ideal
float t1 = (idealVelocity - m_scriptMove[i].flMaxVelocity) / idealAccel;
float d1 = m_scriptMove[i].flMaxVelocity * t1 + 0.5 * (idealAccel) * t1 * t1;
// decel from ideal
float t2 = (idealVelocity - m_scriptMove[i+1].flMaxVelocity) / idealAccel;
float d2 = m_scriptMove[i+1].flMaxVelocity * t2 + 0.5 * (idealAccel) * t2 * t2;
m_scriptMove[i].flDist = (m_scriptMove[i+1].vecLocation - m_scriptMove[i].vecLocation).Length2D();
// is it possible to accel and decal to idealVelocity between next two nodes
if (d1 + d2 < m_scriptMove[i].flDist)
{
Vector start = m_scriptMove[i].vecLocation;
Vector end = m_scriptMove[i+1].vecLocation;
float dist = m_scriptMove[i].flDist;
// insert the two points needed to end accel and start decel
if (d1 > 1.0 && t1 > 0.1)
{
a = d1 / dist;
script.Init();
script.vecLocation = end * a + start * (1 - a);
script.flMaxVelocity = idealVelocity;
m_scriptMove.InsertAfter( i, script );
i++;
}
if (dist - d2 > 1.0 && t2 > 0.1)
{
// DevMsg("%.2f : ", a );
a = (dist - d2) / dist;
script.Init();
script.vecLocation = end * a + start * (1 - a);
script.flMaxVelocity = idealVelocity;
m_scriptMove.InsertAfter( i, script );
i++;
}
i++;
}
else
{
// check to see if the amount of change needed to reach target is less than the ideal acceleration
float flNeededAccel = fabs( DeltaV( m_scriptMove[i].flMaxVelocity, m_scriptMove[i+1].flMaxVelocity, m_scriptMove[i].flDist ) );
if (flNeededAccel < idealAccel)
{
// if so, they it's possible to get a bit towards the ideal velocity
float v1 = m_scriptMove[i].flMaxVelocity;
float v2 = m_scriptMove[i+1].flMaxVelocity;
float dist = m_scriptMove[i].flDist;
// based on solving:
// v1+A*t1-v2-A*t2=0
// v1*t1+0.5*A*t1*t1+v2*t2+0.5*A*t2*t2-D=0
float tmp = idealAccel*dist+0.5*v1*v1+0.5*v2*v2;
Assert( tmp >= 0 );
t1 = (-v1+sqrt( tmp )) / idealAccel;
t2 = (v1+idealAccel*t1-v2)/idealAccel;
// if this assert hits, write down the v1, v2, dist, and idealAccel numbers and send them to me (Ken).
// go ahead the comment it out, it's safe, but I'd like to know a test case where it's happening
//Assert( t1 > 0 && t2 > 0 );
// check to make sure it's really worth it
if (t1 > 0.0 && t2 > 0.0)
{
d1 = v1 * t1 + 0.5 * idealAccel * t1 * t1;
/*
d2 = v2 * t2 + 0.5 * idealAccel * t2 * t2;
Assert( fabs( d1 + d2 - dist ) < 0.001 );
*/
float a = d1 / m_scriptMove[i].flDist;
script.Init();
script.vecLocation = m_scriptMove[i+1].vecLocation * a + m_scriptMove[i].vecLocation * (1 - a);
script.flMaxVelocity = m_scriptMove[i].flMaxVelocity + idealAccel * t1;
if (script.flMaxVelocity < idealVelocity)
{
// DevMsg("insert %.2f %.2f %.2f\n", m_scriptMove[i].flMaxVelocity, script.flMaxVelocity, m_scriptMove[i+1].flMaxVelocity );
m_scriptMove.InsertAfter( i, script );
i += 1;
}
}
}
i += 1;
}
}
// clamp min velocities
for (i = 0; i < m_scriptMove.Count(); i++)
{
m_scriptMove[i].flMaxVelocity = MAX( m_scriptMove[i].flMaxVelocity, MIN_VELOCITY );
}
// rebuild fields
m_scriptMove[0].flElapsedTime = 0;
for (i = 0; i < m_scriptMove.Count() - 1; )
{
m_scriptMove[i].flDist = (m_scriptMove[i+1].vecLocation - m_scriptMove[i].vecLocation).Length2D();
if (m_scriptMove[i].flMaxVelocity == 0 && m_scriptMove[i+1].flMaxVelocity == 0)
{
// force a minimum velocity
Assert( 0 );
m_scriptMove[i+1].flMaxVelocity = 1.0;
}
float t = m_scriptMove[i].flDist / (0.5 * (m_scriptMove[i].flMaxVelocity + m_scriptMove[i+1].flMaxVelocity));
m_scriptMove[i].flTime = t;
/*
if (m_scriptMove[i].flDist < 0.01)
{
// Assert( m_scriptMove[i+1].pWaypoint == NULL );
m_scriptMove.Remove( i + 1 );
continue;
}
*/
m_scriptMove[i+1].flElapsedTime = m_scriptMove[i].flElapsedTime + m_scriptMove[i].flTime;
i++;
}
/*
for (i = 0; i < m_scriptMove.Count(); i++)
{
DevMsg("(%.2f : %.2f : %.2f)", m_scriptMove[i].flMaxVelocity, m_scriptMove[i].flDist, m_scriptMove[i].flTime );
// DevMsg("(%.2f:%.2f)", m_scriptMove[i].flTime, m_scriptMove[i].flElapsedTime );
}
DevMsg("\n");
*/
}
void CAI_BlendedMotor::InsertSlowdown( float distToObstruction, float idealAccel, bool bAlwaysSlowdown )
{
int i;
AI_Movementscript_t script;
if (distToObstruction <= 0.0)
return;
for (i = 0; i < m_scriptMove.Count() - 1; i++)
{
if (m_scriptMove[i].flDist > 0 && distToObstruction - m_scriptMove[i].flDist < 0)
{
float a = distToObstruction / m_scriptMove[i].flDist;
Assert( a >= 0 && a <= 1);
script.vecLocation = (1 - a) * m_scriptMove[i].vecLocation + a * m_scriptMove[i+1].vecLocation;
//NDebugOverlay::Line( m_scriptMove[i].vecLocation + Vector( 0, 0, 5 ), script.vecLocation + Vector( 0, 0, 5 ), 0,255,0, true, 0.1 );
//NDebugOverlay::Line( script.vecLocation + Vector( 0, 0, 5 ), m_scriptMove[i+1].vecLocation + Vector( 0, 0, 5 ), 0,0,255, true, 0.1 );
float r1, r2;
// clamp the next velocity to the possible accel in the given distance
if (!bAlwaysSlowdown && SolveQuadratic( -0.5 * idealAccel, m_scriptMove[0].flMaxVelocity, -distToObstruction, r1, r2 ))
{
script.flMaxVelocity = MAX( 10, m_scriptMove[0].flMaxVelocity - idealAccel * r1 );
}
else
{
script.flMaxVelocity = 10.0;
}
script.flMaxVelocity = 1.0; // as much as reasonable
script.pWaypoint = NULL;
script.flDist = m_scriptMove[i].flDist - distToObstruction;
m_scriptMove[i].flDist = distToObstruction;
m_scriptMove.InsertAfter( i, script );
break;
}
else
{
distToObstruction -= m_scriptMove[i].flDist;
}
}
}
//-----------------------------------------------------------------------------
// Purpose: issues turn gestures when it detects that the body has turned but the feet haven't compensated
//-----------------------------------------------------------------------------
void CAI_BlendedMotor::MaintainTurnActivity( void )
{
AI_PROFILE_SCOPE(CAI_BlendedMotor_MaintainTurnActivity);
if (m_flNextTurnGesture > gpGlobals->curtime || m_flNextTurnAct > gpGlobals->curtime || GetOuter()->IsMoving() )
{
// clear out turn detection if currently turing or moving
m_doTurn = m_doRight = m_doLeft = 0;
if ( GetOuter()->IsMoving())
{
m_flNextTurnAct = gpGlobals->curtime + 0.3;
}
}
else
{
// detect undirected turns
if (m_prevYaw != GetAbsAngles().y)
{
float diff = UTIL_AngleDiff( m_prevYaw, GetAbsAngles().y );
if (diff < 0.0)
{
m_doLeft += -diff;
}
else
{
m_doRight += diff;
}
m_prevYaw = GetAbsAngles().y;
}
// accumulate turn angle, delay response for short turns
m_doTurn += m_doRight + m_doLeft;
// accumulate random foot stick clearing
m_doTurn += random->RandomFloat( 0.4, 0.6 );
}
if (m_doTurn > 15.0f)
{
// mostly a foot stick clear
int iSeq = ACT_INVALID;
if (m_doLeft > m_doRight)
{
iSeq = SelectWeightedSequence( ACT_GESTURE_TURN_LEFT );
}
else
{
iSeq = SelectWeightedSequence( ACT_GESTURE_TURN_RIGHT );
}
m_doLeft = 0;
m_doRight = 0;
if (iSeq != ACT_INVALID)
{
int iLayer = GetOuter()->AddGestureSequence( iSeq );
if (iLayer != -1)
{
GetOuter()->SetLayerPriority( iLayer, 100 );
// increase speed if we're getting behind or they're turning quickly
float rate = random->RandomFloat( 0.8, 1.2 );
if (m_doTurn > 90.0)
{
rate *= 1.5;
}
GetOuter()->SetLayerPlaybackRate( iLayer, rate );
// disable turing for the duration of the gesture
m_flNextTurnAct = gpGlobals->curtime + GetOuter()->GetLayerDuration( iLayer );
}
else
{
// too many active gestures, try again in half a second
m_flNextTurnAct = gpGlobals->curtime + 0.3;
}
}
m_doTurn = m_doRight = m_doLeft = 0;
}
}
ConVar scene_flatturn( "scene_flatturn", "1" );
bool CAI_BlendedMotor::AddTurnGesture( float flYD )
{
// some funky bug with human turn gestures, disable for now
return false;
// try using a turn gesture
Activity activity = ACT_INVALID;
float weight = 1.0;
float turnCompletion = 1.0;
if (m_flNextTurnGesture > gpGlobals->curtime)
{
/*
if ( GetOuter()->m_debugOverlays & OVERLAY_NPC_SELECTED_BIT )
{
Msg( "%.1f : [ %.2f ]\n", flYD, m_flNextTurnAct - gpGlobals->curtime );
}
*/
return false;
}
if ( GetOuter()->IsMoving() || GetOuter()->IsCrouching() )
{
return false;
}
if (fabs( flYD ) < 15)
{
return false;
}
else if (flYD < -45)
{
activity = ACT_GESTURE_TURN_RIGHT90;
weight = flYD / -90;
turnCompletion = 0.36;
}
else if (flYD < 0)
{
activity = ACT_GESTURE_TURN_RIGHT45;
weight = flYD / -45;
turnCompletion = 0.4;
}
else if (flYD <= 45)
{
activity = ACT_GESTURE_TURN_LEFT45;
weight = flYD / 45;
turnCompletion = 0.4;
}
else
{
activity = ACT_GESTURE_TURN_LEFT90;
weight = flYD / 90;
turnCompletion = 0.36;
}
int seq = SelectWeightedSequence( activity );
if (scene_flatturn.GetBool() && GetOuter()->IsCurSchedule( SCHED_SCENE_GENERIC ))
{
Activity flatactivity = activity;
if (activity == ACT_GESTURE_TURN_RIGHT90)
{
flatactivity = ACT_GESTURE_TURN_RIGHT90_FLAT;
}
else if (activity == ACT_GESTURE_TURN_RIGHT45)
{
flatactivity = ACT_GESTURE_TURN_RIGHT45_FLAT;
}
else if (activity == ACT_GESTURE_TURN_LEFT90)
{
flatactivity = ACT_GESTURE_TURN_LEFT90_FLAT;
}
else if (activity == ACT_GESTURE_TURN_LEFT45)
{
flatactivity = ACT_GESTURE_TURN_LEFT45_FLAT;
}
if (flatactivity != activity)
{
int newseq = SelectWeightedSequence( flatactivity );
if (newseq != ACTIVITY_NOT_AVAILABLE)
{
seq = newseq;
}
}
}
if (seq != ACTIVITY_NOT_AVAILABLE)
{
int iLayer = GetOuter()->AddGestureSequence( seq );
if (iLayer != -1)
{
GetOuter()->SetLayerPriority( iLayer, 100 );
// vary the playback a bit
SetLayerPlaybackRate( iLayer, 1.0 );
float actualDuration = GetOuter()->GetLayerDuration( iLayer );
float rate = random->RandomFloat( 0.5f, 1.1f );
float diff = fabs( flYD );
float speed = (diff / (turnCompletion * actualDuration / rate)) * 0.1f;
speed = clamp( speed, 15.f, 35.f );
speed = MIN( speed, diff );
actualDuration = (diff / (turnCompletion * speed)) * 0.1 ;
GetOuter()->SetLayerDuration( iLayer, actualDuration );
SetLayerWeight( iLayer, weight );
SetYawSpeed( speed );
Remember( bits_MEMORY_TURNING );
// don't overlap the turn portion of the gestures, and don't play them too often
m_flNextTurnGesture = gpGlobals->curtime + MAX( turnCompletion * actualDuration, 0.3 );
/*
if ( GetOuter()->m_debugOverlays & OVERLAY_NPC_SELECTED_BIT )
{
Msg( "%.1f : %.2f %.2f : %.2f (%.2f)\n", flYD, weight, speed, actualDuration, turnCompletion * actualDuration );
}
*/
return true;
}
else
{
return false;
}
}
return false;
}
//-------------------------------------
#if 0
Activity CAI_BlendedMotor::GetTransitionActivity( )
{
AI_Waypoint_t *waypoint = GetNavigator()->GetPath()->GetTransitionWaypoint();
if ( waypoint->Flags() & bits_WP_TO_GOAL )
{
if ( waypoint->activity != ACT_INVALID)
{
return waypoint->activity;
}
return GetStoppedActivity( );
}
if (waypoint)
waypoint = waypoint->GetNext();
switch(waypoint->NavType() )
{
case NAV_JUMP:
return ACT_JUMP; // are jumps going to get a movement track added to them?
case NAV_GROUND:
return GetNavigator()->GetMovementActivity(); // yuck
case NAV_CLIMB:
return ACT_CLIMB_UP; // depends on specifics of climb node
default:
return ACT_IDLE;
}
}
#endif
//-------------------------------------
// Purpose: return a velocity that should be hit at the end of the interval to match goal
// Input : flInterval - time interval to consider
// : flGoalDistance - distance to goal
// : flGoalVelocity - desired velocity at goal
// : flCurVelocity - current velocity
// : flIdealVelocity - velocity to go at if goal is too far away
// : flAccelRate - maximum acceleration/deceleration rate
// Output : target velocity at time t+flInterval
//-------------------------------------
float ChangeDistance( float flInterval, float flGoalDistance, float flGoalVelocity, float flCurVelocity, float flIdealVelocity, float flAccelRate, float &flNewDistance, float &flNewVelocity )
{
float scale = 1.0;
if (flGoalDistance < 0)
{
flGoalDistance = - flGoalDistance;
flCurVelocity = -flCurVelocity;
scale = -1.0;
}
flNewVelocity = flCurVelocity;
flNewDistance = 0.0;
// if I'm too close, just go ahead and set the velocity
if (flGoalDistance < 0.01)
{
return flGoalVelocity * scale;
}
float flGoalAccel = DeltaV( flCurVelocity, flGoalVelocity, flGoalDistance );
flNewVelocity = flCurVelocity;
// --------------------------------------------
// if goal is close enough try to match the goal velocity, else try to go ideal velocity
// --------------------------------------------
if (flGoalAccel < 0 && flGoalAccel < -flAccelRate)
{
// I need to slow down;
flNewVelocity = flCurVelocity + flGoalAccel * flInterval;
if (flNewVelocity < 0)
flNewVelocity = 0;
}
else if (flGoalAccel > 0 && flGoalAccel >= flAccelRate)
{
// I need to speed up
flNewVelocity = flCurVelocity + flGoalAccel * flInterval;
if (flNewVelocity > flGoalVelocity)
flGoalVelocity = flGoalVelocity;
}
else if (flNewVelocity < flIdealVelocity)
{
// speed up to ideal velocity;
flNewVelocity = flCurVelocity + flAccelRate * flInterval;
if (flNewVelocity > flIdealVelocity)
flNewVelocity = flIdealVelocity;
// don't overshoot
if (0.5*(flNewVelocity + flCurVelocity) * flInterval > flGoalDistance)
{
flNewVelocity = 0.5 * (2 * flGoalDistance / flInterval - flCurVelocity);
}
}
else if (flNewVelocity > flIdealVelocity)
{
// slow down to ideal velocity;
flNewVelocity = flCurVelocity - flAccelRate * flInterval;
if (flNewVelocity < flIdealVelocity)
flNewVelocity = flIdealVelocity;
}
float flDist = 0.5*(flNewVelocity + flCurVelocity) * flInterval;
if (flDist > flGoalDistance)
{
flDist = flGoalDistance;
flNewVelocity = flGoalVelocity;
}
flNewVelocity = flNewVelocity * scale;
flNewDistance = (flGoalDistance - flDist) * scale;
return 0.0;
}
//-----------------------------------------------------------------------------
|