Skip to content

Commit

Permalink
Added error message if the trailer unload is to far away from the silo
Browse files Browse the repository at this point in the history
  • Loading branch information
schwiti6190 committed Sep 22, 2023
1 parent 017e827 commit 3179941
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 5 deletions.
4 changes: 4 additions & 0 deletions config/MasterTranslations.xml
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@
<Text language="de"><![CDATA[Keinen Abladepunkt gefunden.]]></Text>
<Text language="en"><![CDATA[No unloading point found.]]></Text>
</Translation>
<Translation name="CP_error_trailer_unload_to_far_away_from_silo">
<Text language="de"><![CDATA[Anhänger Abladepunkt ist zu weit vom Silo entfernt.]]></Text>
<Text language="en"><![CDATA[Trailer unloading point is to far away from the silo.]]></Text>
</Translation>
</Category>
<Category name="AI job parameters">
<Translation name="CP_jobParameters_fieldPosition_title">
Expand Down
24 changes: 20 additions & 4 deletions scripts/ai/jobs/CpAIJobSiloLoader.lua
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ local AIJobCombineUnloaderCp_mt = Class(CpAIJobSiloLoader, CpAIJob)
CpAIJobSiloLoader.TRAILER_SEARCH_LENGTH = 25
--- Trailer unload marker width, -TRAILER_SEARCH_WIDTH/2 to TRAILER_SEARCH_WIDTH/2
CpAIJobSiloLoader.TRAILER_SEARCH_WIDTH = 20
--- Max distance the trailer unload spot can be from the silo/heap.
CpAIJobSiloLoader.MAX_TRAILER_DISTANCE_FROM_SILO = 100

function CpAIJobSiloLoader.new(isServer, customMt)
local self = CpAIJob.new(isServer, customMt or AIJobCombineUnloaderCp_mt)
Expand Down Expand Up @@ -167,25 +169,31 @@ function CpAIJobSiloLoader:validate(farmId)
return false, g_i18n:getText("CP_error_no_unload_trigger_found")
end
else
local found, area = CpAIJobSiloLoader.getTrailerUnloadArea(self.cpJobParameters.unloadPosition)
local found, area, validDistanceToSilo = CpAIJobSiloLoader.getTrailerUnloadArea(
self.cpJobParameters.unloadPosition, self.bunkerSilo or self.heap)
if found then
self.trailerAreaPlot:setVisible(true)
self.trailerAreaPlot:setArea(area)
end
if not validDistanceToSilo then
return false, g_i18n:getText("CP_error_trailer_unload_to_far_away_from_silo")
end
end
end
return isValid, errorMessage
end

--- Gets the area for search for trailers
---@param position CpAIParameterPositionAngle
---@param silo CpSilo|nil
---@return boolean found?
---@return table area
function CpAIJobSiloLoader.getTrailerUnloadArea(position)
---@return boolean distance to silo is valid
function CpAIJobSiloLoader.getTrailerUnloadArea(position, silo)
local x, z = position:getPosition()
local dirX, dirZ = position:getDirection()
if x == nil or dirX == nil then
return false, {}
return false, {}, true
end
--- Rotation matrix to rotate Z directions to x directions
local dirX2 = dirX * math.cos(math.pi/2) - dirZ * math.sin(math.pi/2)
Expand Down Expand Up @@ -213,7 +221,15 @@ function CpAIJobSiloLoader.getTrailerUnloadArea(position)
z = z + dirZ * CpAIJobSiloLoader.TRAILER_SEARCH_LENGTH/2 + dirZ2 * CpAIJobSiloLoader.TRAILER_SEARCH_WIDTH/2
},
}
return true, area
if silo then
local fx, fz = silo:getFrontCenter()
local bx, bz = silo:getBackCenter()
if MathUtil.vector2Length(x-fx, z-fz) < CpAIJobSiloLoader.MAX_TRAILER_DISTANCE_FROM_SILO and
MathUtil.vector2Length(x-bx, z-bz) < CpAIJobSiloLoader.MAX_TRAILER_DISTANCE_FROM_SILO then
return true, area, false
end
end
return true, area, true
end

--- Gets the bunker silo or heap at the loading position in that order.
Expand Down
2 changes: 1 addition & 1 deletion scripts/silo/BunkerSiloWrapper.lua
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ end
function CpSilo:getBackCenter()
local length = self:getLength()
local fcx, fcz = self:getFrontCenter()
return fcx + self.dirXLength * length/2, fcz + self.dirZLength * length/2
return fcx + self.dirXLength * length, fcz + self.dirZLength * length
end

--- Is the point directly in the silo area?
Expand Down

0 comments on commit 3179941

Please sign in to comment.