Lua assistance (probably) needed

Viregel

, The Rt. Hon.
Joined
Jun 10, 2013
Messages
1,944
Location
Kingdom of the Britons
Hey! I have a bunch of Lua here that would be useful to finish. As a rundown, I'm pretty sure the first function is right, need some clarification on the second one (need to half unhappiness in coastal cities, there must be an easier way to do it) and then I have no idea where I'm going with the third one (double gold from trade routes in golden ages). None of it is tested anyway, so I have no clue how much would actually work.

Code:
function GE_KalmarGarrisonedBorderExpansion (playerID)
		local player = Players[playerID]
            for city in player:Cities() do
                 if city:GetGarrisonedUnit() == GameInfoTypes["UNIT_GE_KALMAR_UNION_BALTIC_WARSHIP"] then
                         city:SetNumRealBuilding(GameInfoTypes["BUILDING_GE_BALTIC_EXPANSION"], 1)
                 else
					if city:IsHasBuilding(GameInfoTypes["BUILDING_GE_BALTIC_EXPANSION"]) then
                                            city:SetNumRealBuilding(GameInfoTypes["BUILDING_GE_BALTIC_EXPANSION"], 0)
                                    end
						 end
			 end
    end
GameEvents.PlayerDoTurn.Add(GE_KalmarGarrisonedBorderExpansion)

function GE_KalmarCoastalUnhappiness (playerID)
	local player = Players[playerID]
	local CityPopulation = city:GetPopulation/2()
		if player:GetCivilizationType() == GameInfoTypes["CIVILIZATION_GE_KALMAR_UNION"] and player:IsEverAlive() then
			for city in player:Cities() do
				if city:IsCoastal() then 
					city:SetNumRealBuilding(GameInfoTypes["BUILDING_GE_HYGGE"], 2 + CityPopulation) 
                                    end
						 end
			 end
    end
GameEvents.PlayerDoTurn.Add(GE_KalmarGarrisonedBorderExpansion)


function GE_KalmarGoldenAgeTrade (playerID)
	local player = Players[playerID]
	local TradeGold = player.GetGoldPerTurnFromTradeRoutesTimes100
		if player:GetCivilizationType() == GameInfoTypes["CIVILIZATION_GE_KALMAR_UNION"] and player:IsEverAlive() then
			if player:IsGoldenAge() then
				TradeGold = TradeGold * 2
			end
		end
	end
GameEvents.PlayerDoTurn.Add(GE_KalmarGoldTrade)

Thank you very much for any help! :D
 
Well the first thing I notice is that the GameEventsDoTurn redirect to something different, but maybe it's temporary?

The other thing that I don't understand is

Code:
local CityPopulation = city:GetPopulation/2()

what does that "city" refers to? I don't think you have defined the variable "city" there. Moreover why did you put the "/2" between the function name and the parentheses? I'm no expert of lua code but I don't think that works. You should probably change it like this:

Code:
function GE_KalmarCoastalUnhappiness (playerID)
	local player = Players[playerID]
	local CityPopulation
	if player:GetCivilizationType() == GameInfoTypes["CIVILIZATION_GE_KALMAR_UNION"] and player:IsEverAlive() then
		for city in player:Cities() do
			if city:IsCoastal() then 
				CityPopulation = math.floor(city:GetPopulation() / 2)
				city:SetNumRealBuilding(GameInfoTypes["BUILDING_GE_HYGGE"], 2 + CityPopulation) 
			end
		 end
	end
end

with "math.floor" you'll be sure that you'll get an integer. If you prefer for it to be rounded up then use "math.ceil" instead.
 
Code:
function GE_KalmarCoastalUnhappiness (playerID)
	local player = Players[playerID]
	local CityPopulation = city:GetPopulation/2()
		if player:GetCivilizationType() == GameInfoTypes["CIVILIZATION_GE_KALMAR_UNION"] and player:IsEverAlive() then
			for city in player:Cities() do
				if city:IsCoastal() then 
					city:SetNumRealBuilding(GameInfoTypes["BUILDING_GE_HYGGE"], 2 + CityPopulation) 
                                    end
						 end
			 end
    end
GameEvents.PlayerDoTurn.Add(GE_KalmarGarrisonedBorderExpansion)

I'm pretty sure "city:GetPopulation/2()" needs to be "city:GetPopulation() / 2"

Code:
function GE_KalmarGoldenAgeTrade (playerID)
	local player = Players[playerID]
	local TradeGold = player.GetGoldPerTurnFromTradeRoutesTimes100
		if player:GetCivilizationType() == GameInfoTypes["CIVILIZATION_GE_KALMAR_UNION"] and player:IsEverAlive() then
			if player:IsGoldenAge() then
				TradeGold = TradeGold * 2
			end
		end
	end
GameEvents.PlayerDoTurn.Add(GE_KalmarGoldTrade)

I'm not entirely sure how gold from trading routes is calculated. It appears to be on a city-by-city basis, so I'm guessing you'd want to use something like

Code:
local tradeGoldTotal = 0
for city in player:Cities() do
    tradeGoldTotal = tradeGoldTotal + player:GetRouteGoldTimes100( city ) / 100
end
player:ChangeGold(tradeGoldTotal)

instead of "TradeGold = TradeGold * 2"
 
Well the first thing I notice is that the GameEventsDoTurn redirect to something different, but maybe it's temporary?
Code:
GameEvents.PlayerDoTurn.Add(GE_KalmarGarrisonedBorderExpansion)
is the hook statement used in CIV5 to tell the game when to run the function specified in the .Add(). This tells the game to run the function every turn for every player during "NEXT TURN" processing. Which is why he needs the
Code:
if player:GetCivilizationType() == GameInfoTypes["CIVILIZATION_GE_KALMAR_UNION"] and player:IsEverAlive() then
lines, so that the 'meat' of the function only continues to run for a specific player every turn during "NEXT TURN" processing.

Which brings me to a possible issue. I am assuming the UNIT_GE_KALMAR_UNION_BALTIC_WARSHIP is a naval unit, which means as I recall that militaristic city-states won't "pick" that unit as a gift-unit. But If I'm wrong and it is actually a land unit in some way, then I think the BUILDING_GE_BALTIC_EXPANSION building can be given to the wrong civ, especially if the mod is active but the civilization is not "in-game".
 
It is a naval unit, though I don't quite get your point. The ability's tied to the unit anyway, so it would work across civs if the naval unit was gifted.
Well that was my kinda my point. I was (I guess not very clearly) trying to ask if that was your intention (ie, the ability follows the unit regardless of which civ has a copy of the unit) or if you forgot to account for that.

But I think it is only relevant to land units so far as the city-states giving units to players. But it will be relevant for naval units if they are captured by another civ that has naval units with the PROMOTION_PRIZE_SHIPS.
 
Code:
GameEvents.PlayerDoTurn.Add(GE_KalmarGarrisonedBorderExpansion)
is the hook statement used in CIV5 to tell the game when to run the function specified in the .Add(). This tells the game to run the function every turn for every player during "NEXT TURN" processing. Which is why he needs the
Code:
if player:GetCivilizationType() == GameInfoTypes["CIVILIZATION_GE_KALMAR_UNION"] and player:IsEverAlive() then
lines, so that the 'meat' of the function only continues to run for a specific player every turn during "NEXT TURN" processing.

yeah but the problem is this:

Code:
function GE_KalmarGarrisonedBorderExpansion (playerID)
		local player = Players[playerID]
            for city in player:Cities() do
                 if city:GetGarrisonedUnit() == GameInfoTypes["UNIT_GE_KALMAR_UNION_BALTIC_WARSHIP"] then
                         city:SetNumRealBuilding(GameInfoTypes["BUILDING_GE_BALTIC_EXPANSION"], 1)
                 else
					if city:IsHasBuilding(GameInfoTypes["BUILDING_GE_BALTIC_EXPANSION"]) then
                                            city:SetNumRealBuilding(GameInfoTypes["BUILDING_GE_BALTIC_EXPANSION"], 0)
                                    end
						 end
			 end
    end
GameEvents.PlayerDoTurn.Add([COLOR="Red"]GE_KalmarGarrisonedBorderExpansion[/COLOR])

function [COLOR="Red"]GE_KalmarCoastalUnhappiness[/COLOR] (playerID)
	local player = Players[playerID]
	local CityPopulation = city:GetPopulation/2()
		if player:GetCivilizationType() == GameInfoTypes["CIVILIZATION_GE_KALMAR_UNION"] and player:IsEverAlive() then
			for city in player:Cities() do
				if city:IsCoastal() then 
					city:SetNumRealBuilding(GameInfoTypes["BUILDING_GE_HYGGE"], 2 + CityPopulation) 
                                    end
						 end
			 end
    end
GameEvents.PlayerDoTurn.Add([COLOR="Red"]GE_KalmarGarrisonedBorderExpansion[/COLOR])


function GE_KalmarGoldenAgeTrade (playerID)
	local player = Players[playerID]
	local TradeGold = player.GetGoldPerTurnFromTradeRoutesTimes100
		if player:GetCivilizationType() == GameInfoTypes["CIVILIZATION_GE_KALMAR_UNION"] and player:IsEverAlive() then
			if player:IsGoldenAge() then
				TradeGold = TradeGold * 2
			end
		end
	end
GameEvents.PlayerDoTurn.Add(GE_KalmarGoldTrade)

There is no event that calls the function "GE_KalmarCoastalUnhappiness" and instead you have two that calls the function "GE_KalmarGarrisonedBorderExpansion". That's what I was pointing out, but I guess he fixed that already.
 
Sorry to ressurect this, but I'm still having issues with this:

Code:
function GE_KalmarGarrisonedBorderExpansion (playerID)
		local player = Players[playerID]
            for city in player:Cities() do
                 if city:GetGarrisonedUnit():GetUnitType() == GameInfoTypes["UNIT_GE_KALMAR_UNION_BALTIC_WARSHIP"] then
                         city:SetNumRealBuilding(GameInfoTypes["BUILDING_GE_BALTIC_EXPANSION"], 1)
                 else
					if city:IsHasBuilding(GameInfoTypes["BUILDING_GE_BALTIC_EXPANSION"]) then
                                            city:SetNumRealBuilding(GameInfoTypes["BUILDING_GE_BALTIC_EXPANSION"], 0)
                                    end
						 end
			 end
    end
GameEvents.PlayerDoTurn.Add(GE_KalmarGarrisonedBorderExpansion)

function GE_KalmarCoastalUnhappiness (playerID)
	local player = Players[playerID]
		for city in player:Cities() do
		local CityPopulation = math.floor(city:GetPopulation() / 2)
			if player:GetCivilizationType() == GameInfoTypes["CIVILIZATION_GE_KALMAR_UNION"] and player:IsEverAlive() then
				if city:IsCoastal() then 
					city:SetNumRealBuilding(GameInfoTypes["BUILDING_GE_HYGGE"], 2 + CityPopulation) 
				 else
						if city:IsHasBuilding(GameInfoTypes["BUILDING_GE_HYGGE"]) then
											  city:SetNumRealBuilding(GameInfoTypes["BUILDING_GE_HYGGE"], 0)
									 end
							end
				end
		end
end
GameEvents.PlayerDoTurn.Add(GE_KalmarCoastalUnhappiness)


function GE_KalmarGoldenAgeTrade (playerID)
	local player = Players[playerID]
	local TradeGold = player.GetGoldPerTurnFromTradeRoutesTimes100
		if player:GetCivilizationType() == GameInfoTypes["CIVILIZATION_GE_KALMAR_UNION"] and player:IsEverAlive() then
			if player:IsGoldenAge() then
				local tradeGoldTotal = 0
					for city in player:Cities() do
						tradeGoldTotal = tradeGoldTotal + GetGoldPerTurnFromTradeRoutesTimes100(city) / 100
					end
					player:ChangeGold(tradeGoldTotal)
			end
		end
	end
GameEvents.PlayerDoTurn.Add(GE_KalmarGoldenAgeTrade)

Lua log (note I changed 'GetRouteGoldTimes100' to 'GetGoldPerTurnFromTradeRoutesTimes100', but I doubt that'll make it work):

Code:
[3428768.718] Runtime Error: C:\Users\Viregel\Documents\My Games\Sid Meier's Civilization 5\MODS\Mods\Greater Europe's Kalmar Union (v 1)\Lua/KalmarUnionFunctions.lua:11: attempt to index a nil value
[3428768.718] Runtime Error: C:\Users\Viregel\Documents\My Games\Sid Meier's Civilization 5\MODS\Mods\Greater Europe's Kalmar Union (v 1)\Lua/KalmarUnionFunctions.lua:47: attempt to call method 'GetRouteGoldTimes100' (a nil value)

The first function is now apparently not working, which may be an issue, and I can't figure out the logic for the third. This thread (which looks like it might be incredibly useful for most of us) probably has all that is likely needed for this, but does anyone have any idea what the Lua would be to double gold from trade routes in golden ages? Again, thanks for any help.
 
http://forums.civfanatics.com/showthread.php?t=500747
Code:
Player.GetGoldPerTurnFromTradeRoutes
Player.GetGoldPerTurnFromTradeRoutesTimes100
Player:ChangeGold(Player:GetGoldPerTurnFromTradeRoutes())

Code:
city:GetGarrisonedUnit():GetUnitType(
This probably triggers first error (you have to check if this unit exists). And please copy whole code, because "nctions.lua:11: attempt" shows the line of error, but in provided code line 11 is "end".

Good luck.
 
Top Bottom