Help with Lua - Luxury from x number of improvements

Firebug

Not-so Great Engineer
Joined
Sep 25, 2014
Messages
1,271
Location
Clevedon, England
My Bulgaria mod is finally coming together thanks to some help. But i'll need help with coding a lua.

It would basically give a luxury resource for every 4 of a unique improvement.
simply put - 4x Improvement = 1x Luxury.
But it would also have to count the luxuries you've traded away. So as to stop you getting unlimited luxuries.

If anyone can help it'll be great!
thanks in advance.
 
As I recall, DarkScythe's Holo civ had a UI (Apple Orchard) that provided a copy of a unique luxury resource for every two copies of the UI, although I took a look at the Lua and it's a little difficult to puzzle through.
 
If all you want is number of improvements built as opposed to those worked by cities

At start of turn
- count improvements (there might be a pPlayer method that does this - edit: yep it's pPlayer:GetImprovementCount(iImprovement))
- divide by 4, call this X
- count hidden buildings(s) granting the unique lux in the capital, call this Y
- add/remove (X-Y) hidden buildings(s) granting the unique lux to the capital

The trade system should automatically handle traded away lux'es, in the same way it handles traded away Ally from Recycle Centers
 
No reason to setup a new thread for this.
What about a Rifleman UU that becomes ranged when it enters hills and mountains? is that able to be done?
 
What about a Rifleman UU that becomes ranged when it enters hills and mountains? is that able to be done?

Create unbuildable ranged unit, detect UU entering hills, switch UU (melee) unit with ranged unit, detect ranged unit exiting hills, switch back to UU

Yes it can "be done"

Would the AI understand how to use the UU (melee) unit - definitely not.
 
I think you can make an unit with 0 range, but with CombatRangeStrength (or whatever it is called, ranged strength) and then use two promotions, one with extra range and second that disables melee attack (based on OnlyDefensive... promotion).

However, it will happen the same as with unit that had 4 movements, but couldn't attack after making 2 moves. The AI will not handle it very well. The game will stop, no crash, but no action, when AI will try to melee-attack through the Hills and suddenly become unable to melee attack. I am not sure it can be solved in lua.
 
Or detect at game-start/game-reload not only whether the civ is in play but also whether or not they are human. If human, run one set of code. If AI, run a different set of code.

Human = transform back and forth between melee and ranged

AI = leave as melee, but give out some other buff or affect the AI is actually capable of handling, like a hills 'extra movement' or 'extra combat power' promotion.

'Course, there is still the issue of C-State Unique-Unit gifting.
 
So... maybe this won't work out? I like my mods to work as AI too. I'll consider something else. Maybe permanent ranged or maybe not ranged at all.
 
Would a promotion that increases range on hills work? Range of adjacent (not sure if 0 or 1) when not on hills, range of whatever you want when on hills.
 
Would a promotion that increases range on hills work? Range of adjacent (not sure if 0 or 1) when not on hills, range of whatever you want when on hills.
This is pretty much exactly what LastSword just proposed, and so carries with it the same problems - namely, that the AI won't be able to handle it correctly.
 
As I recall, DarkScythe's Holo civ had a UI (Apple Orchard) that provided a copy of a unique luxury resource for every two copies of the UI, although I took a look at the Lua and it's a little difficult to puzzle through.

I'm always really impressed by the level of coding that goes into fantasy civs, even if i don't like playing them.
Really, the level of work requires more notice.
 
I've been checking the code of DarkScythe's Holo Civ, but its written with a lot of complex locals with all these different parts working off each other and i'm having issues understanding how to go about doing it. I'm gonna try write my own code from stratch using some advise DJS gave me but its looking to be beyond my skill range.
 
My Halloween Civ gets extra XP for every one of their unique improvement. The code could be adapted for your use:

Spoiler :
Code:
function PumpkinXP(PlayerID)
	local pPlayer = Players[PlayerID]
	if (pPlayer:IsAlive() and pPlayer:GetCivilizationType() == GameInfoTypes["CIVILIZATION_GREAT_PUMPKIN"]) then
		local Impr = GameInfoTypes["IMPROVEMENT_PUMPKIN_LIBRARY"];
		local ImprCount = pPlayer:GetImprovementCount(Impr);
		local XPBldg = GameInfoTypes["BUILDING_HALL_XP_DUMMY"];
		if ImprCount > 20 then
				ImprCount = 20
		end
		for city in pPlayer:Cities() do
			--print("Setting XP buildings, count is " .. ImprCount .. ".");
			city:SetNumRealBuilding(XPBldg, ImprCount)
		end
	end
end

GameEvents.PlayerDoTurn.Add(PumpkinXP)

You would have to add a "divide by 4", and apply the building only to your capital instead of all cities. Each dummy building would grant one copy of the lux.
 
calcul8tor's solution is simple and straightforward (and therefore far preferred) for when all you care about is the number of improvements of type-x a player has.

You only need to go into the intense polt-iterating and plot-tracking and what-not found in DarkScythe's Holo Civ when you want to know which city is working the plot, or plan to change plot yield's directly, or only want that direct-plot-yield-change to apply to a particular civilization or even city within that civilization, or are wanting some other more-complicated mechanic than simply counting the number of improvements.
 
My Halloween Civ gets extra XP for every one of their unique improvement. The code could be adapted for your use:

Spoiler :
Code:
function PumpkinXP(PlayerID)
	local pPlayer = Players[PlayerID]
	if (pPlayer:IsAlive() and pPlayer:GetCivilizationType() == GameInfoTypes["CIVILIZATION_GREAT_PUMPKIN"]) then
		local Impr = GameInfoTypes["IMPROVEMENT_PUMPKIN_LIBRARY"];
		local ImprCount = pPlayer:GetImprovementCount(Impr);
		local XPBldg = GameInfoTypes["BUILDING_HALL_XP_DUMMY"];
		if ImprCount > 20 then
				ImprCount = 20
		end
		for city in pPlayer:Cities() do
			--print("Setting XP buildings, count is " .. ImprCount .. ".");
			city:SetNumRealBuilding(XPBldg, ImprCount)
		end
	end
end

GameEvents.PlayerDoTurn.Add(PumpkinXP)

You would have to add a "divide by 4", and apply the building only to your capital instead of all cities. Each dummy building would grant one copy of the lux.

Where would i add the divide by 4 code? I know how to change it so its capital only, thats simple enough.


calcul8tor's solution is simple and straightforward (and therefore far preferred) for when all you care about is the number of improvements of type-x a player has.

You only need to go into the intense polt-iterating and plot-tracking and what-not found in DarkScythe's Holo Civ when you want to know which city is working the plot, or plan to change plot yield's directly, or only want that direct-plot-yield-change to apply to a particular civilization or even city within that civilization, or are wanting some other more-complicated mechanic than simply counting the number of improvements.

Yikes, someone private messaged me offering to work out how Holo's code worked. Better message them back and let them know its not needed before they spend 4 hours trying to work it out.
 
Code:
local ImprCount = pPlayer:GetImprovementCount(Impr);

Hmm, I'd never known there was this function! I was going to suggest he walk through each of his cities and then walk through each of their tiles, and count the number of improvements that way. This is probably far more efficient. :P

Where would i add the divide by 4 code? I know how to change it so its capital only, thats simple enough.

That part's simple:

Code:
local iNumResources = math.floor(ImprCount * 0.25)

Math.floor will round down, so you won't get any extra copies for fractions. Math.ceil does the inverse.

In the end, you'd want your code to look something like this:

Code:
function FirebugResource(PlayerID)
	local pPlayer = Players[PlayerID]
	if (pPlayer:IsAlive() and pPlayer:GetCivilizationType() == GameInfoTypes["CIVILIZATION_FIREBUG_CIV"]) then
		local iImprCount = pPlayer:GetImprovementCount(Impr);
		local iNumResources = math.floor(iImprCount * 0.25)
		local ResourceBldg = GameInfoTypes["BUILDING_RESOURCE_DUMMY"];
		local pCapital = pPlayer:GetCapitalCity()
		if pCapital ~= nil then
			pCapital:SetNumRealBuilding(ResourceBldg, iNumResources)
		end
	end
end

GameEvents.PlayerDoTurn.Add(FirebugResource)
 
I'm just testing a code DJSHenninger gave me, if it doesn't work i'll try yours.
And sure as hell you're all getting credits in this mod for helping me here. I've got a bit more understanding of lua now...
which i will forget tomorrow because of my goldfish memory. But its the thought that counts.


Edit:
18271-good-news-everyone-i-was-just-kidding-professor-farnsworth-wallpaper-1280x1280-1-750x410.jpg


4 Rose Field improvements now gives you one copy of a Rose Otto luxury resource.
 
Back
Top Bottom