Help with lua code for traderoute yield modifier

Cou

Chieftain
Joined
Jul 21, 2016
Messages
8
Hi, I'm trying to make an UA that grants a yield modifier to a specific city based on how many outgoing international traderoutes it has. But I have some problems with the lua part, and would like a helping hand :D

This is my function to assign each "building" based on how many traderoutes goes out from the city:
Code:
local buildingTradeMod	= GameInfoTypes["BUILDING_COU_NORWAY_TRADEMOD"]
function COU_Nortraship_TradeBonus(playerID)
	local player = Players[playerID]
	if (player:IsAlive() and player:GetCivilizationType() == civilizationID) then
		for city in player:Cities() do
			local numTradeRoutes = COU_GetNumInternationalTradeRoutes(playerID)
			city:SetNumRealBuilding(buildingTradeMod, numTradeRoutes)
		end
	end
end
if isNorwayCivActive then
	GameEvents.PlayerDoTurn.Add(COU_Nortraship_TradeBonus)
end

and here is the function to get the amount of traderoutes in city:
Code:
function COU_GetNumInternationalTradeRoutes(playerID)
	local player = Players[playerID]
	local numTradeRoutes = 0 
	local tradeRoutes = player:GetTradeRoutes()
	for _, tradeRoute in ipairs(tradeRoutes) do
		if (tradeRoute.FromCivilizationType ~= tradeRoute.ToCivilizationType) then
			numTradeRoutes = numTradeRoutes + 1
		end
	end

	return numTradeRoutes
end

Any help would be appreciated. Is it anything wrong with the logic? Or do I need something in addition to this?
 
function to get the amount of traderoutes in city:
Code:
function COU_GetNumInternationalTradeRoutes(playerID)
    ...
end

The function counts the number of trade routes for the player, as it makes no allowance for which city the trade route is from/to
 
The function counts the number of trade routes for the player, as it makes no allowance for which city the trade route is from/to

Thanks for answer.
I see, would it be correct to do this then?
Code:
function COU_GetNumInternationalTradeRoutes(playerID, city)
	local player = Players[playerID]
	local numTradeRoutes = 0 
	local tradeRoutes = player:GetTradeRoutes()
	for _, tradeRoute in ipairs(tradeRoutes) do
		if (tradeRoute.FromCity == city) then
			numTradeRoutes = numTradeRoutes + 1
		end
	end

	return numTradeRoutes
end

But the big problem isn't really that. It seems that the main function doesn't run at all, or at least doesn't go through the cities.
I made it so that it should place the "building" in every city, but it still doesn't.
Code:
local buildingTradeMod	= GameInfoTypes["BUILDING_COU_NORWAY_TRADEMOD"]
function COU_Nortraship_TradeBonus(playerID)
	local player = Players[playerID]
	if (player:IsAlive() and player:GetCivilizationType() == civilizationID) then
		for city in player:Cities() do
			city:SetNumRealBuilding(buildingTradeMod, 1)
		end
	end
end
if isNorwayCivActive then
	GameEvents.PlayerDoTurn.Add(COU_Nortraship_TradeBonus)
end
Am I just failing to see the obvious?
 
You don't need lua to give your civ a free building in every city. There is Civilization_FreeBuildingClasses table.
 
You don't need lua to give your civ a free building in every city. There is Civilization_FreeBuildingClasses table.

I know, it's not supposed to be in every city, only in cities with outgoing trade routes.
If there are 2 outgoing trade routes in 1 city that city should get 2 of that building, if there are no trade routes, no buildings should be placed.

The last code example was just to test where the code isn't working, which I found to be before it got to the second function, which counts the trade routes in the specific city.

thanks anyway
 
You don't need lua to give your civ a free building in every city. There is Civilization_FreeBuildingClasses table.
This only adds the specified class of building in your capital city. It does not place the building in any other city.
Thanks for answer.
I see, would it be correct to do this then?
Code:
function COU_GetNumInternationalTradeRoutes(playerID, city)
	local player = Players[playerID]
	local numTradeRoutes = 0 
	local tradeRoutes = player:GetTradeRoutes()
	for _, tradeRoute in ipairs(tradeRoutes) do
		if (tradeRoute.FromCity == city) then
			numTradeRoutes = numTradeRoutes + 1
		end
	end

	return numTradeRoutes
end

But the big problem isn't really that. It seems that the main function doesn't run at all, or at least doesn't go through the cities.
I made it so that it should place the "building" in every city, but it still doesn't.
Code:
local buildingTradeMod	= GameInfoTypes["BUILDING_COU_NORWAY_TRADEMOD"]
function COU_Nortraship_TradeBonus(playerID)
	local player = Players[playerID]
	if (player:IsAlive() and player:GetCivilizationType() == civilizationID) then
		for city in player:Cities() do
			city:SetNumRealBuilding(buildingTradeMod, 1)
		end
	end
end
if isNorwayCivActive then
	GameEvents.PlayerDoTurn.Add(COU_Nortraship_TradeBonus)
end
Am I just failing to see the obvious?
Since you are only showing chunks of the code and not the entire file (or posting the mod itself) it is difficult to know where your trouble really is. I have my suspicions but without the full mod or at least the full file it is really just a guessing game.
 
Top Bottom