Skip to content

Commit 2bc7a3d

Browse files
committed
Fix for targeting incorrect chopper
Hopefully, there are a lot of nil checks that may be aborting the function and I am not changing anything. Always fun altering giants functions
1 parent c772e81 commit 2bc7a3d

File tree

2 files changed

+93
-13
lines changed

2 files changed

+93
-13
lines changed

scripts/ai/AIDriveStrategyChopperCourse.lua

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -781,27 +781,28 @@ end
781781

782782

783783
-- TODO Fix this currently any trailer in range will cause the chopper to drive
784-
-- This is current broken and any trailer nearby will cause it to be true. Maybe it is not .vehicle but a get implements to return the trailer object instead?
785784
function AIDriveStrategyChopperCourse:isChopperWaitingForUnloader()
786785
local trailer, targetObject = self:nearestChopperTrailer()
787786
local dischargeNode = self.pipeController:getDischargeNode()
788787
self:debugSparse('%s %s', dischargeNode, self:isAnyWorkAreaProcessing())
789788
if not (targetObject == nil or trailer == nil) then
790-
-- if targetObject and targetObject.rootVehicle.getIsCpActive and targetObject.rootVehicle:getIsCpActive() then
791-
-- local strategy = targetObject.rootVehicle:getCpDriveStrategy()
792-
-- if strategy.isAChopperUnloadAIDriver
793-
-- and self:getCurrentUnloader()
794-
-- and self:getCurrentUnloader().vehicle == targetObject.rootVehicle
795-
-- and self:getCurrentUnloader():readyToReceive() then
796-
-- self:debugSparse('Chopper has a CP Driven trailer now, continue')
797-
-- return false
798-
-- end
799-
-- else
789+
if targetObject and targetObject.rootVehicle.getIsCpActive and targetObject.rootVehicle:getIsCpActive() then
790+
local strategy = targetObject.rootVehicle:getCpDriveStrategy()
791+
if strategy.isAChopperUnloadAIDriver
792+
and self:getCurrentUnloader()
793+
and self:getCurrentUnloader().vehicle == targetObject.rootVehicle
794+
and self:getCurrentUnloader():readyToReceive() then
795+
self:debugSparse('Chopper has a CP Driven trailer now, continue')
796+
return false
797+
end
798+
else
800799
self:debugSparse('Chopper has a trailer now, continue')
801800
return false
802-
-- end
801+
end
803802
end
804803
self:debugSparse('Chopper waiting for trailer, discharge node %s, target object %s, trailer %s',
805804
tostring(dischargeNode), tostring(targetObject), tostring(trailer))
806805
return true
807806
end
807+
808+

scripts/ai/controllers/ChopperController.lua

Lines changed: 80 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ end
2727
function ChopperController:updateChopperFillType()
2828
--- Not exactly sure what this does, but without this the chopper just won't move.
2929
--- Copied from AIDriveStrategyCombine:update()
30+
-- This also exists in Giants AI drive strategy
3031
-- no pipe, no discharge node
3132
local capacity = 0
3233
local dischargeNode = self.implement:getCurrentDischargeNode()
@@ -59,4 +60,82 @@ function ChopperController:updateChopperFillType()
5960
end
6061
end
6162
end
62-
end
63+
end
64+
65+
-- Hack to force the chopper to only target CP unload drivers unless there is a human or none CP trailer in range
66+
function ChopperController:updateNearestObjectInTriggers(superFunc, ...)
67+
local spec = self.spec_pipe
68+
spec.nearestObjectInTriggers.objectId = nil
69+
spec.nearestObjectInTriggers.fillUnitIndex = 0
70+
local minDistance = math.huge
71+
local dischargeNode = self:getDischargeNodeByIndex(self:getPipeDischargeNodeIndex())
72+
local rootVehicle = self.getRootVehicle and self:getRootVehicle()
73+
74+
if not rootVehicle then
75+
return superFunc(self, ...)
76+
end
77+
-- We only want to use our modified version of this function when CP Chopper is driving
78+
if not (rootVehicle.getIsCpActive or rootVehicle:getIsCpActive()) then
79+
return superFunc(self, ...)
80+
end
81+
82+
local chopperDriver = rootVehicle:getCpDriveStrategy()
83+
84+
if not chopperDriver or not chopperDriver.isAChopperUnloadAIDriver then
85+
return superFunc(self, ...)
86+
end
87+
88+
if dischargeNode ~= nil then
89+
90+
local checkNode = Utils.getNoNil(dischargeNode.node, self.components[1].node)
91+
92+
for object, _ in pairs(spec.objectsInTriggers) do
93+
local outputFillType = self:getFillUnitLastValidFillType(dischargeNode.fillUnitIndex)
94+
95+
for fillUnitIndex, _ in ipairs(object.spec_fillUnit.fillUnits) do
96+
local allowedToFillByPipe = object:getFillUnitSupportsToolType(fillUnitIndex, ToolType.DISCHARGEABLE)
97+
local supportsFillType = object:getFillUnitSupportsFillType(fillUnitIndex, outputFillType) or outputFillType == FillType.UNKNOWN
98+
local fillLevel = object:getFillUnitFreeCapacity(fillUnitIndex, outputFillType, self:getOwnerFarmId())
99+
100+
if allowedToFillByPipe and supportsFillType and fillLevel > 0 then
101+
local targetPoint = object:getFillUnitAutoAimTargetNode(fillUnitIndex)
102+
local exactFillRootNode = object:getFillUnitExactFillRootNode(fillUnitIndex)
103+
104+
if targetPoint == nil then
105+
targetPoint = exactFillRootNode
106+
end
107+
108+
if targetPoint ~= nil then
109+
-- We have a target check to see if it is a CP driver, if not default to going to closest in range. Original functionality
110+
if targetPoint and targetPoint.rootVehicle.getIsCpActive and targetPoint.rootVehicle:getIsCpActive() then
111+
local strategy = targetPoint.rootVehicle:getCpDriveStrategy()
112+
if strategy.isAChopperUnloadAIDriver
113+
and chopperDriver:getCurrentUnloader()
114+
and chopperDriver:getCurrentUnloader().vehicle == targetPoint.rootVehicle then
115+
spec.nearestObjectInTriggers.objectId = NetworkUtil.getObjectId(object)
116+
spec.nearestObjectInTriggers.fillUnitIndex = fillUnitIndex
117+
break
118+
119+
end
120+
else
121+
local distance = calcDistanceFrom(checkNode, targetPoint)
122+
123+
if distance < minDistance then
124+
minDistance = distance
125+
spec.nearestObjectInTriggers.objectId = NetworkUtil.getObjectId(object)
126+
spec.nearestObjectInTriggers.fillUnitIndex = fillUnitIndex
127+
128+
break
129+
end
130+
end
131+
132+
end
133+
end
134+
end
135+
end
136+
else
137+
Logging.xmlWarning(self.xmlFile, "Unable to find discharge node index '%d' for pipe", self:getPipeDischargeNodeIndex())
138+
end
139+
end
140+
141+
Pipe.updateNearestObjectInTriggers = Utils.overwrittenFunction(Pipe.updateNearestObjectInTriggers, ChopperController.updateNearestObjectInTriggers)

0 commit comments

Comments
 (0)