Possible fix to the convoy bug (NA/Europe 36 mod)

Nemovadit

Chieftain
Joined
Nov 3, 2005
Messages
33
It was painful but I think I managed to get something that works. It might be done better (for some reason I had to use isatdestination rather than moveconvoy so it's probably doing lots of unecessary tests but I'm not skilled enough so... feel free to try and edit/optimise.

As usual backup your files then in the relevant (named below) lua files:

I) in scriptamericaeuro 1936 find the convoy table where you'll add waypoints and waypoint lists:

1) For every US to France/UK route add what's in blue below

Spoiler :
[US_TO_FRANCE] = ((snip))
RandomDestination = false, -- false : sequential try in destination list
WaypointList = { {X=35, Y=40}, }, -- waypoints​
RandomWaypoint = false,
CivID = FRANCE, -- ((if France, other wise what you paste ends before Civ UK for instance))
((end snip))


2) For Every US to USSR route add what's in blue below
Spoiler :
RandomDestination = false, -- false : sequential try in destination list
WaypointList ={ {X=33, Y=33}, {X=66, Y=76}, },

RandomWaypoint = false,
CivID = USSR,


3) For Every Suez to US or UK (and maybe GERMANY?) routes add what's in blue below
Spoiler :
RandomDestination = false, -- false : sequential try in destination list
WaypointList ={ {X=55, Y=23}, },
RandomWaypoint = false,
CivID = XXX,
CivID = XXX,


II) in RedAI_Global:

1) find the function AIUnitControl find AI: fleet an add what's in blue below

Spoiler :
if g_UnitData[unitKey].OrderType then
if g_UnitData[unitKey].OrderType == RED_CONVOY or g_UnitData[unitKey].OrderType == RED_CONVOY_WAYP1 or g_UnitData[unitKey].OrderType == RED_CONVOY_WAYP2 then
MoveConvoy(unit)
elseif g_UnitData[unitKey].OrderType == RED_MOVE_TO_EMBARK then
MoveToEmbark(unit) -- reinforcement en route


2) find the function IsAtDestination find AI: fleet an add what's in blue below

Spoiler :
if g_UnitData[unitKey].OrderType == RED_CONVOY then ((snip: jump to the end of the red convoy part so that this if, and insert before the first elseif))

Dprint(" - Can't unload !", bDebug)
end
end
end
end

elseif g_UnitData[unitKey].OrderType == RED_CONVOY_WAYP1 then

local routeID = g_UnitData[unitKey].OrderReference
local firstWaypoint=GetConvoyFirstWaypoint(routeID)
local destination = g_UnitData[unitKey].OrderObjective

Dprint("Check if convoy has reached the first waypoint", bDebug)

local plotList = GetAdjacentPlots(plot, true) -- include central plot in list

for i, adjacentPlot in pairs(plotList) do
Dprint("begins proximity check("..firstWaypoint.X..","..firstWaypoint.Y, bDebug) if destination.X == adjacentPlot:GetX() and destination.Y == adjacentPlot:GetY() then
Dprint("At first waypoint", bDebug)

local secondWaypoint = GetNextConvoyWaypoint(routeID, firstWaypoint)
Dprint("Check if second waypoint exists", bDebug)

if secondWaypoint then
g_UnitData[unitKey].OrderType = RED_CONVOY_WAYP2
g_UnitData[unitKey].OrderObjective = secondWaypoint
MoveUnitTo (unit, GetPlot (secondWaypoint.X, secondWaypoint.Y ))
Dprint("Moving to second WP", bDebug)

else
g_UnitData[unitKey].OrderType = RED_CONVOY
local newdestination = GetConvoyDestination(routeID) -- wil print testing possible destination
g_UnitData[unitKey].OrderObjective = newdestination
MoveUnitTo (unit, GetPlot (newdestination.X, newdestination.Y ))
Dprint("Moves to destination", bDebug)

end

else
MoveUnitTo (unit, GetPlot (firstWaypoint.X, firstWaypoint.Y ))
Dprint("Moves to first waypoint", bDebug)
end
end

elseif g_UnitData[unitKey].OrderType == RED_CONVOY_WAYP2 then
local routeID = g_UnitData[unitKey].OrderReference
local firstWaypoint=GetConvoyFirstWaypoint(routeID)
local secondWaypoint = GetNextConvoyWaypoint(routeID, firstWaypoint)
g_UnitData[unitKey].OrderObjective = secondWaypoint
local destination = g_UnitData[unitKey].OrderObjective

Dprint("Check if second waypointis nearby", bDebug)
local plotList = GetAdjacentPlots(plot, true) -- include central plot in list
for i, adjacentPlot in pairs(plotList) do
if destination.X == adjacentPlot:GetX() and destination.Y == adjacentPlot:GetY() then
Dprint("At second WP", bDebug)

g_UnitData[unitKey].OrderType = RED_CONVOY
local newdestination = GetConvoyDestination(routeID)
g_UnitData[unitKey].OrderObjective = newdestination
MoveUnitTo (unit, GetPlot (newdestination.X, newdestination.Y ))
Dprint("Moving to destination", bDebug)


else
MoveUnitTo (unit, GetPlot (secondWaypoint.X, secondWaypoint.Y ))
Dprint("moving to second WP", bDebug)
end
end​

elseif g_UnitData[unitKey].OrderType == RED_MOVE_TO_EMBARK then((snip))


Then jump to the Convoy function near the end and add these functions
Spoiler :
function GetConvoyFirstWaypoint(routeID, bForceSequential) -- return {X=x ,Y=y}

local bDebug = true

Dprint(" - Check convoy first waypoint...", bDebug)

local Waypoint = nil
local WaypointList = g_Convoy[routeID].WaypointList
if not WaypointList then
Dprint(" - no waypoints for this route...", bDebug)
return nil -- this route has no waypoint...
end

if g_Convoy[routeID].RandomWaypoint and not bForceSequential then
-- route use one random waypoint
Dprint(" - search random waypoint route...", bDebug)
local randPlot = math.random( 1, #waypointList )
Waypoint = WaypointList[randPlot]
else
-- route use fixed waypoint
Dprint(" - search first waypoint...", bDebug)
Waypoint = WaypointList[1]
end

return Waypoint
end


and
Spoiler :
function GetNextConvoyWaypoint(routeID, previousWaypoint, bForceSequential) -- return {X=x ,Y=y}

local bDebug = true

Dprint(" - testing next destination waypoint...", bDebug)

local sdwaypoint = nil
local WaypointList = g_Convoy[routeID].WaypointList
if not WaypointList then
Dprint(" - no waypoints for this route...", bDebug)
return nil -- this route has no waypoint... How do we get here ?
end

--if g_Convoy[routeID].RandomWaypoint and not bForceSequential then
-- route use one random waypoint
--Dprint(" - only one random waypoint for this route !", bDebug)
--return nil
--else
-- route use fixed waypoint
for i, Waypoint in ipairs(WaypointList) do
if Waypoint == previousWaypoint and i+1 <= #WaypointList then -- manquait un = ?
Dprint(" - set next waypoint !", bDebug)
sdwaypoint = WaypointList[i+1]
end
end
--end

return sdwaypoint
end

III) Then in Redsupplyroutes find the function InitConvoyUnit and add what&#8217;s in blue again near the end of the function
Spoiler :

unit:SetMoves(0) -- don't move on first turn

local firstWaypoint = GetConvoyFirstWaypoint(routeID)
Dprint(" - Tentative de création de point de navigation...", bDebug)

if not firstWaypoint then
Dprint(" - Pas de point de navigation...", bDebug)
else
g_UnitData[unitKey].OrderType = RED_CONVOY_WAYP1
g_UnitData[unitKey].OrderObjective = firstWaypoint
Dprint(" - Point de navigation créé...", bDebug)
end

return true


IV) Lastly find the Order types in RedDefineRules and add

Spoiler :
RED_CONVOY_WAYP1 = 10
RED_CONVOY_WAYP2 = 11




I hope I didn't forget anything, good luck I've not seen a single convoy crash since.

Things worth checking for those better at coding than me:
1) Do the various waypoint/move things in moveconvoy or moveunit
2) Insert a distance test in IsAtDestination so that convoy don't check when they are 3 or more turns away from the harbour/waypoint
 
maybe using this approach an alert message could be put in where the convoy waits at a way point to get to a clear harbor something like the old route to canceled message from vanilla workers...
?
 
with indents in the paragraph line (as if tabbed once) or not?

From taking a look at the code, it seems like the indentations matter. if this is just an organizational asthetic, then please say so, because i don't want to take great pains screwing around with the lua

e2: 20 turns in, no crash yet. looks like allswell, but i dont want to sound overconfident yet. if i get to turn 100 without a crash, i'll call it a good quickfix
 
Mostly there to help identifying what's new actually. A good way to see if it's working is to lift the fog of war with IGE for instance and check if you see convoys going through Gibraltar or sailing the Atlantic toward murmansk - if they are the ones that caused the crashes for you too.

Side note: won't prevent a crash on an existing save if it comes from an existing (and thus registered) convoy. Probably safer to test with a fresh game anyway.
 
Into about turn 65 on an alt history as germany, and it's crashed a few times but only from American convoys from Suez. I'll keep going and see if i can isolate the issue.

Yes, every time its the american convoy crash from the Suez, originating at tile 118,6.

Will the suez hotfix work for american convoys too?
 
Stupid steam update of the mods removed the fix, so i just redid it but i think i screwed something up. No crashes yet, but playing as Germany, the convoys aren't moving themselves dwon from the top of the baltic and don't 'get used' once they reach Konigsburg. Any suggestion on what that might be?

edit: did it again, payed attention to make sure an extra return line seperated the 'new functions' and it seems to be working.


Someone with more experience coding and/or modding should submit stable files of the scripts to be included in the next update
 
I'm sure Gedemon will do that if a couple more people try this fix ;) What was updated exactly I don't remember reading about an update in the other threads?
 
Hello Nemovadit,
when I played this Scenario first, it keeps crashing during turn 5/6. Then I made your changes in the lua files. Playing with Germany, everything went fine. But it crashed in turn 56 (or was it 65 ?). I had alook with the IGE. England had a lot of units in the near of gibraltar. I removed the hill of gibraltar, just to look what will happen. The game went on two turns, then it crashed again. I think its the convoy bug again. But I can`t change player in the IGE to remove a convoy and see, which one is the problem convoy. When I change player in the IGE on save mode, i cannot remove the british units...when I try to change player the normal way, it crashes...
 
Go into IGE "safe mode" and it should not crash. (Right click on the IGE icon to get the option to choose safe mode.)
 
When I change player in the IGE safe mode (from Germany to England for example) i am not able to remove units as England...its not a real change of player...
 
OK, forget that, it works. So I can see, its the convoy bug again. Its british convoys at 55,44 and 49,33 and later on its russian convoys at 83,80 and 70,77.
 
Back
Top Bottom