2727function 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