Affecting unit costs with Lua?

Hypereon

He was a Consul of Rome!
Joined
Jun 19, 2012
Messages
414
Location
Helsinki, Finland
Hi! I need some help coding this unit ability:

(Undisclosed) starts weaker and more expensive than the Rifleman it replaces, but it gets cheaper and more powerful the more puppets and and overseas cities (or colonies with ExCE) there are in the empire.

Counting the cities and moving the unit costs is the part I'm concerned with, yet little clue... :confused:
 
This should ought to return the number of puppeted cities in an empire. I didn't find a direct one-liner method for doing it so I wrote as a scan-through-all cities.
Code:
function CountPuppetedCities(pPlayer)
	local iNumPuppets = 0
	for pCity in pPlayer:Cities() do
		if pCity:IsPuppet() then
			iNumPuppets = iNumPuppets + 1
		end
	end
	return iNumPuppets
end
So inside some other function you can do something like
Code:
local iNumberPuppet = CountPuppetedCities(pPlayer)
and "iNumberPuppet" will be set to the number of puppets in the empire, as calculated by the function "CountPuppetedCities".

I did not see any simple method for determining whether a city is on the same continent as the capital, however.

The unit-gets-stronger part could be handled either by promotion or by direct lua commands to alter the unit's base strength (I think), but I am stumbling on a method for the "becomes cheaper" part of it.
 
I did not see any simple method for determining whether a city is on the same continent as the capital, however.
Here is my code for my High Elf Civ, what you want is bolded:
Spoiler :
Code:
function HighElfLandMass(iPlayer, iCityX, iCityY)
	local pPlayer = Players[iPlayer]
	if pPlayer:GetCivilizationType() == eHighElf then
		[B]local pPlot = Map.GetPlot(iCityX, iCityY)
		local iArea = pPlot:GetArea()
		local pArea = Map.GetArea(iArea)[/B]
		local bAloneInLandMass = CheckCitiesHElfMass(pArea, iPlayer, pPlayer)
		if bAloneInLandMass then
			local pCity = pPlot:GetPlotCity()
			pCity:SetNumRealBuilding(eAloneBonusBd, 1)
		else
			print("Another civ is in this continent")
		end
	else
		local pPlot = Map.GetPlot(iCityX, iCityY)
		local iArea = pPlot:GetArea()
		local pArea = Map.GetArea(iArea)
		local bHighElfNotPresent, iHighElfID = CheckCitiesHElfMass(pArea, iPlayer, pPlayer)
		if (not bHighElfNotPresent) then
			for pCity in Players[iHighElfID]:Cities() do
				local pPlotCity = pCity:Plot()
				if pPlotCity:GetArea() == iArea then
					pCity:SetNumRealBuilding(eAloneBonusBd, 0)
					print("Another Civ founded a city in the same continent. Bonus removed.")
				end
			end
		end
	end
end

function CheckCitiesHElfMass(pArea, iPlayer, pPlayer)
	if pPlayer:GetCivilizationType() == eHighElf then
		for i=0, GameDefines.MAX_MAJOR_CIVS -1, 1 do
			if iPlayer ~= i and pArea:GetCitiesPerPlayer(i) > 0 then
				return false
			end
		end
	else
		for i=0, GameDefines.MAX_MAJOR_CIVS -1, 1 do
			if Players[i]:GetCivilizationType() == eHighElf and [B]pArea:GetCitiesPerPlayer(i) > 0[/B] then
				return false, i
			end
		end
	end
	return true
end

The unit-gets-stronger part could be handled either by promotion or by direct lua commands to alter the unit's base strength (I think), but I am stumbling on a method for the "becomes cheaper" part of it.
Agreed! I think promotions is the best way to do it.
'Becomes cheaper' could be multiple units with the same TXT_KEY's and characteristics with the exception of the cost, and control which one can be trained with the PlayerCanTrain GameEvent.
 
You could also have dummy buildings which increase production of the UnitClass by 1%, and grant a number of those dummy buildings to all of the player's Cities based on the puppets and overseas cities.

Edit: Whoops, forgot this only works for UnitCombatTypes, not UnitClasses. You'd actually just need to make them increase all unit production, but then you'd have to get tricky with Lua and set the buildings in the city only when the player is producing the unit in question, and remove them when they aren't.
 
You could also have dummy buildings which increase production of the UnitClass by 1%, and grant a number of those dummy buildings to all of the player's Cities based on the puppets and overseas cities.

Edit: Whoops, forgot this only works for UnitCombatTypes, not UnitClasses. You'd actually just need to make them increase all unit production, but then you'd have to get tricky with Lua and set the buildings in the city only when the player is producing the unit in question, and remove them when they aren't.

There is Unit_ProductionModifierBuildings which increases the production of individual unit types.
 
You could also have dummy buildings which increase production of the UnitClass by 1%, and grant a number of those dummy buildings to all of the player's Cities based on the puppets and overseas cities.

Edit: Whoops, forgot this only works for UnitCombatTypes, not UnitClasses. You'd actually just need to make them increase all unit production, but then you'd have to get tricky with Lua and set the buildings in the city only when the player is producing the unit in question, and remove them when they aren't.

There is Unit_ProductionModifierBuildings which increases the production of individual unit types.
Yeah -- that'll do it for constructing the unit, but won't effect the purchase cost with gold unfortunately.

He could also use a system similar to what I am doing in my Local City Resources mod where I check the current production item, and if it is a building on the list, I add a group of buildings with either <BuildingProductionModifier> or <WonderProdcutionModifier> set to 1. So if the adjustment needs to be 10% I add ten such buildings. That could easily be changed to <MilitaryProductionModifier> set to one (1) in the dummy building.

But neither method will deal with purchasing by gold or faith so far as the 'cheaper' part goes. Table <Building_HurryModifiers> is empire-wide and also effects all purchasing items, so it would not help in this case.
 
Top Bottom