Convert TR

InkAxis

King
Joined
Feb 24, 2020
Messages
779
Convert Trade Routes
DOWNLOAD

Just a simple mod I saw some requests for, this allows you to change any caravan into a cargo ship and the other way around.

By default this feature is always enabled but by going into convert.lua and changing the first false to true you can make it unlock at a technology.

upload_2022-1-2_15-3-11.png


Thanks for N. Core for writing compatibility with Unique trade units.
 
Last edited:
Interesting. Does the caravan have to be next to sea tile to do the conversion?
Yes. It would be possible to teleport it to the nearest city or something, but I mean, you can just move it anyway.
Amazing stuff! Does the AI use this? I'd like to be on equal footing with the AI but that's probably not that important, I doubt it's able to optimize trade routes like a human player anyway.
The AI does not use this. Honestly I'm not even sure if the AI does ever realize to delete a land trade route and make a water one. In either case I'm not really planning on making the AI use this, it would be very difficult and the AI would hardly gain anything.
 
Ok, so while it is in movement, it is seen as a land unit up until it makes contact with water? What occurs if it is attacked?
I think you're misunderstanding how the feature works, it's a button on the unit actions that turns it into a cargo ship/caravan. Otherwise they function as regular trade units.
 
Don't Caravans cost only 65% of what a cargo ship does (90:c5production: vs 140:c5production:, scaling with era)? Does this mod equalize their production cost?
 
Don't Caravans cost only 65% of what a cargo ship does (90:c5production: vs 140:c5production:, scaling with era)? Does this mod equalize their production cost?
Nope. So theoretically you could gain a bit of production if you really care about exploiting things.

To me this is more of a convenience feature for those that want it. Especially as I would recommend playing so that it unlocks later in the game when both units are basically 1 turn of production anyway.
 
Works great!

One minor suggestion, perhaps it should take 1 turn to accomplish in the same way it takes 1 turn to rebase a trade route.
 
I slightly edited the mod so that now it works with civs having UUs replacing caravans and cargo ships.
 

Attachments

  • Convert Caravans and Cargo Ships (v 1).zip
    3.1 KB · Views: 16
I slightly edited the mod so that now it works with civs having UUs replacing caravans and cargo ships.
I see that your implementation is using DB query (which is slightly heavier in performance IIRC).
I actually modified the script too to include Caravan & Cargo Ship UUs, albeit with a slightly different method.

This is my version:
C-like:
local GameEvents = GameEvents
local GameInfo = GameInfo
local GameInfoTypes = GameInfoTypes
local Players = Players
local Teams = Teams

-----------------------------------------------
-- SETTINGS
-----------------------------------------------
-- Select whether you want to unlock it with specified tech, or after discovering Cargo Ship's PrereqTech
local g_UnlockWithTech = false
local iTechnology = GameInfoTypes.TECH_INDUSTRIALIZATION --Set as fallback tech, you can change this if you want

local g_UnlockWithCargoShipTech = false

if not g_UnlockWithTech then
    g_UnlockWithCargoShipTech = true
end

local ePrereqTech = GameInfo.Units.UNIT_CARGO_SHIP.PrereqTech --So it can follow any changes from the database
if g_UnlockWithCargoShipTech and ePrereqTech ~= nil then
    iTechnology = GameInfoTypes.ePrereqTech
end
-----------------------------------------------
-- END SETTINGS
-----------------------------------------------

local iMissionLand = GameInfoTypes.MISSION_CONVERT_LAND
local iMissionSea = GameInfoTypes.MISSION_CONVERT_SEA

local iUnitClassCaravan = GameInfoTypes.UNITCLASS_CARAVAN
local iUnitClassCargoShip = GameInfoTypes.UNITCLASS_CARGO_SHIP

local CUSTOM_MISSION_NO_ACTION       = 0
local CUSTOM_MISSION_ACTION          = 1
local CUSTOM_MISSION_DONE            = 2
local CUSTOM_MISSION_ACTION_AND_DONE = 3

function OnCustomMissionPossible(iPlayer, iUnit, iMission, _, _, _, _, iPlotX, iPlotY, bTestVisible)
    if (iMission == iMissionLand or iMission == iMissionSea) then
        local pPlayer = Players[iPlayer]
        local iTeam = pPlayer:GetTeam()
        local pTeamTechs = Teams[iTeam]:GetTeamTechs()
        local pUnit = pPlayer:GetUnitByID(iUnit)
        local iUnitClass = pUnit:GetUnitClassType()
        local pUnitPlot = pUnit:GetPlot()

        if ((iUnitClass == iUnitClassCaravan and iMission == iMissionLand) or (iUnitClass == iUnitClassCargoShip and iMission == iMissionSea)) then
            if (pTeamTechs:HasTech(iTechnology)) then
                local pCity = pUnitPlot:GetPlotCity()
                if (pCity and (pCity:GetOwner() == iPlayer and pCity:IsCoastal())) then
                    return true
                end
                return bTestVisible --the right unit but can't do the action
            end
        end
    end
    return false
end

function OnCustomMissionStart(iPlayer, iUnit, iMission, _, _, _, _)
    if (iMission == iMissionLand or iMission == iMissionSea) then
        local pPlayer = Players[iPlayer]
        local iUnitCaravan = GameInfoTypes[GetCivSpecificUnit(pPlayer, "UNITCLASS_CARAVAN")]
        local iUnitCargoShip = GameInfoTypes[GetCivSpecificUnit(pPlayer, "UNITCLASS_CARGO_SHIP")]
        local pUnit = pPlayer:GetUnitByID(iUnit)
        local iUnitX = pUnit:GetX()
        local iUnitY = pUnit:GetY()

        if (iMission == iMissionLand) then
            local tradeUnit = pPlayer:InitUnit(iUnitCargoShip, iUnitX, iUnitY)
            tradeUnit:SetMoves(0)
        else
            local tradeUnit = pPlayer:InitUnit(iUnitCaravan, iUnitX, iUnitY)
            tradeUnit:SetMoves(0)
        end

        pUnit:Kill()
    end
    return CUSTOM_MISSION_NO_ACTION
end

GameEvents.CustomMissionStart.Add(OnCustomMissionStart)
GameEvents.CustomMissionPossible.Add(OnCustomMissionPossible)

function GetCivSpecificUnit(pPlayer, sUnitClass)
    local sUnitType = nil
    local sCivType = GameInfo.Civilizations[pPlayer:GetCivilizationType()].Type

    for pOverride in GameInfo.Civilization_UnitClassOverrides{CivilizationType = sCivType, UnitClassType = sUnitClass} do
        sUnitType = pOverride.UnitType
        break
    end

    if (sUnitType == nil) then
         sUnitType = GameInfo.UnitClasses[sUnitClass].DefaultUnit
    end

    return sUnitType
end
 
I see that your implementation is using DB query (which is slightly heavier in performance IIRC).
I actually modified the script too to include Caravan & Cargo Ship UUs, albeit with a slightly different method.
Your version looks more elegant, my lua got quite rusty.
one of @jarcast2's custom civ

If you refer to Nepal, hopefully soon it won't be the case anymore.
There are more civs replacing cargo ships, tho, albeit for vanilla.
 
It would have been nice if converting from Caravan -> Cargo Ship cost some Gold; when you do the opposite, it gives gold instead.
A Caravan is cheaper (Gold and Production) than Cargo Ship.
I added this feature by giving the gold cost, which is not always entirely accurate but it is in most cases. Though it also means if you have very little gold you can't convert your TR.
 
Top Bottom