Skip to content

Commit 336ba19

Browse files
committed
Improvements to clean things up, and improve stuck handling with automovers
1 parent 349765e commit 336ba19

File tree

3 files changed

+57
-24
lines changed

3 files changed

+57
-24
lines changed

Data/Base.rte/AI/HumanBehaviors.lua

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1182,23 +1182,6 @@ function HumanBehaviors.ToolSearch(AI, Owner, Abort)
11821182
return true;
11831183
end
11841184

1185-
function HumanBehaviors.GetRealVelocity(Owner)
1186-
-- Calculate a velocity based on our actual movement. This is because otherwise gravity falsely reports that we have a downward velocity, even if our net movement is zero.
1187-
-- Note - we use normal delta time, not AI delta time, because PrevPos is updated per-tick (not per-AI-tick)
1188-
return (Owner.Pos - Owner.PrevPos) / TimerMan.DeltaTimeSecs;
1189-
end
1190-
1191-
function HumanBehaviors.UpdateAverageVel(Owner, AverageVel)
1192-
-- Store an exponential moving average of our speed over the past seconds
1193-
local timeInSeconds = 1;
1194-
1195-
local ticksPerTime = timeInSeconds / TimerMan.AIDeltaTimeSecs;
1196-
AverageVel = AverageVel - (AverageVel / ticksPerTime);
1197-
AverageVel = AverageVel + (HumanBehaviors.GetRealVelocity(Owner) / ticksPerTime);
1198-
1199-
return AverageVel;
1200-
end
1201-
12021185
-- go prone if we can shoot from the prone position and return the result
12031186
function HumanBehaviors.GoProne(AI, Owner, TargetPos, targetID)
12041187
if (not Owner.Head or AI.proneState == AHuman.PRONE) or (Owner:NumberValueExists("AIDisableProne")) then

Data/Base.rte/AI/SharedBehaviors.lua

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,22 @@
11
SharedBehaviors = {};
22

3+
function SharedBehaviors.GetRealVelocity(Owner)
4+
-- Calculate a velocity based on our actual movement. This is because otherwise gravity falsely reports that we have a downward velocity, even if our net movement is zero.
5+
-- Note - we use normal delta time, not AI delta time, because PrevPos is updated per-tick (not per-AI-tick)
6+
return (Owner.Pos - Owner.PrevPos) / TimerMan.DeltaTimeSecs;
7+
end
8+
9+
function SharedBehaviors.UpdateAverageVel(Owner, AverageVel)
10+
-- Store an exponential moving average of our speed over the past seconds
11+
local timeInSeconds = 1;
12+
13+
local ticksPerTime = timeInSeconds / TimerMan.AIDeltaTimeSecs;
14+
AverageVel = AverageVel - (AverageVel / ticksPerTime);
15+
AverageVel = AverageVel + (SharedBehaviors.GetRealVelocity(Owner) / ticksPerTime);
16+
17+
return AverageVel;
18+
end
19+
320
-- move to the next waypoint
421
function SharedBehaviors.GoToWpt(AI, Owner, Abort)
522
-- check if we have arrived
@@ -57,6 +74,8 @@ function SharedBehaviors.GoToWpt(AI, Owner, Abort)
5774
local NeedsNewPath, Waypoint, HasMovePath, Dist, CurrDist;
5875
NeedsNewPath = true;
5976

77+
Owner:RemoveNumberValue("AI_StuckForTime");
78+
6079
while true do
6180
Waypoint = nil;
6281
HasMovePath = false;
@@ -134,7 +153,7 @@ function SharedBehaviors.GoToWpt(AI, Owner, Abort)
134153
ArrivedTimer:SetSimTimeLimitMS(0);
135154
end
136155

137-
AverageVel = HumanBehaviors.UpdateAverageVel(Owner, AverageVel);
156+
AverageVel = SharedBehaviors.UpdateAverageVel(Owner, AverageVel);
138157

139158
local stuckThreshold = 2.5; -- pixels per second of movement we need to be considered not stuck
140159

@@ -143,6 +162,9 @@ function SharedBehaviors.GoToWpt(AI, Owner, Abort)
143162

144163
-- Reset our stuck timer if we're moving
145164
if AverageVel:MagnitudeIsGreaterThan(stuckThreshold) then
165+
if StuckTimer:IsPastSimTimeLimit() then
166+
Owner:RemoveNumberValue("AI_StuckForTime");
167+
end
146168
StuckTimer:Reset();
147169
end
148170

@@ -165,6 +187,7 @@ function SharedBehaviors.GoToWpt(AI, Owner, Abort)
165187
Waypoint = nil;
166188
NeedsNewPath = true; -- update the path
167189
elseif StuckTimer:IsPastSimTimeLimit() then -- dislodge
190+
Owner:SetNumberValue("AI_StuckForTime", StuckTimer.ElapsedSimTimeMS);
168191
if AI.jump then
169192
if Owner.Jetpack and Owner.Jetpack.JetTimeLeft < AI.minBurstTime then -- out of fuel
170193
AI.jump = false;
@@ -643,5 +666,17 @@ function SharedBehaviors.GoToWpt(AI, Owner, Abort)
643666
if _abrt then return true end
644667
end
645668

669+
Owner:RemoveNumberValue("AI_StuckForTime");
646670
return true;
647-
end
671+
end
672+
673+
-- stop the user from inadvertently modifying the storage table
674+
local Proxy = {};
675+
local Mt = {
676+
__index = SharedBehaviors,
677+
__newindex = function(Table, k, v)
678+
error("The SharedBehaviors table is read-only.", 2);
679+
end
680+
}
681+
setmetatable(Proxy, Mt);
682+
SharedBehaviors = Proxy;

Data/Base.rte/Scenes/Objects/Bunkers/BunkerSystems/Automovers/Controller/Controller.lua

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1132,15 +1132,30 @@ automoverActorFunctions.updateDirectionsFromActorControllerInput = function(self
11321132
analogMove[actorData.centeringAxes[1]] = 0;
11331133
end
11341134

1135-
analogMove:Normalize();
1135+
if actor:NumberValueExists("AI_StuckForTime") and actorData.lastAnalogMove ~= nil then
1136+
-- Sometimes give no input to let things reset
1137+
local stuckForMS = actor:GetNumberValue("AI_StuckForTime");
1138+
if math.fmod(stuckForMS, 5000) < 500 then
1139+
analogMove = Vector();
1140+
else
1141+
if actorData.lastAnalogMove:IsZero() then
1142+
-- We tried a random direction and failed to get unstuck in 5 secs
1143+
-- so try the opposite direction as our waypoint
1144+
actorData.lastAnalogMove = analogMove * -1;
1145+
end
11361146

1137-
if (actor.Pos - actor.PrevPos):MagnitudeIsLessThan(0.05) then
1138-
-- choose a random direction to get unstuck
1139-
-- TODO, it'd be better if the AI logic can communicate this to us instead!
1140-
analogMove:RadRotate(RangeRand(-math.pi,math.pi));
1147+
-- add a random deviation to get unstuck
1148+
actorData.lastAnalogMove:RadRotate(RangeRand(-0.15,0.15));
1149+
analogMove = actorData.lastAnalogMove;
1150+
end
1151+
print(analogMove);
11411152
end
1153+
1154+
analogMove:Normalize();
11421155
end
11431156

1157+
actorData.lastAnalogMove = analogMove;
1158+
11441159
local deadZone = 0.1;
11451160
if analogMove:MagnitudeIsGreaterThan(deadZone) then
11461161
if math.abs(analogMove.X) < math.abs(analogMove.Y) then

0 commit comments

Comments
 (0)