Lua Request

Aheadatime

Prince
Joined
Dec 21, 2009
Messages
325
While working on the tech tree (including new buildings, wonders, techs, etc), I've come across an issue that I don't have the skill to resolve.

Essentially, I want a new Lua table similar to LocalResourceOrs and LocalResourceAnds, except that I want the table to scan for unimproved resources within working radius of a city.

Example building is the Garden. +1 food for each nearby Bananas, Wheat, and Truffles. Since some cities may have access to Bananas without access to the other resources, requiring the city to improve the Bananas to gain access to the Garden would feel crappy, as Bananas are much better unimproved.

I realize that I can just forgo any resource requirement for construction, but that would create visual clutter. Ie, you can build a Garden in cities that don't have Bananas, Wheat, or Truffles, and the building itself doesn't provide any flat bonuses. Why is it in my Production menu then?

Could anyone help me with this?
 
I think Whoward's PlotIterator could help you with scanning tiles (and then checking if there's a resource on that tile, which in turn adds an invisible dummy building to provide the appropriate yields) (NOTE: Not sure if that would be the most effective way).

I can also recall that LeeS made something with resources and the city working radius, I just can't recall what it specifically was. (I'm secretly hoping that LeeS will come and tell us now) EDIT: This is what I meant, though it's what we're looking for in this case

As for the production-issue, that's completely normal I think. The game simply doesn't care if a building produces 5000 faith per turn or no yields at all; if the building as a cost (and no resource requirements), it can be built. The game itself doesn't care about balance, which is why modders are able to create OP or UP things (unintentional or not :lol:)!
 
I think Whoward's PlotIterator could help you with scanning tiles (and then checking if there's a resource on that tile, which in turn adds an invisible dummy building to provide the appropriate yields) (NOTE: Not sure if that would be the most effective way).

I can also recall that LeeS made something with resources and the city working radius, I just can't recall what it specifically was. (I'm secretly hoping that LeeS will come and tell us now)

I'll check those out. I've tried reading Lua tutorials, and it's just too time consuming. I'm just a musician and psych major.. I've no background in programming or modding whatsoever before coming to these forums lol.
 
I realize that I can just forgo any resource requirement for construction, but that would create visual clutter. Ie, you can build a Garden in cities that don't have Bananas, Wheat, or Truffles, and the building itself doesn't provide any flat bonuses. Why is it in my Production menu then?

Could anyone help me with this?

Couldn't you restrict the construction of the Garden using PlayerCanConstruct? Admittedly, you wouldn't get a UI element in the civilopedia saying which resources are required, but you could still add the requirement to the tooltip so the player would know.

The building itself could modify the yields of resources fairly easily through Building_ResourceYieldChanges - the resources don't need to be improved to receive those bonuses.
 
Couldn't you restrict the construction of the Garden using PlayerCanConstruct?

I have no idea lol. Is that an Lua script? I know nilch about Lua, outside of the fact that XML references Lua.

The building itself could modify the yields of resources fairly easily through Building_ResourceYieldChanges - the resources don't need to be improved to receive those bonuses.

Yes this is how the code is currently written. It's just that the building appears for every city, regardless of nearby resources.
 
You want your building to be buildable only in cities that have unimproved resources?
 
Let me explain through an in-game example. This is what happens with the Mint;

1. Once tech is reached, game scans cities to see if Gold and/or Silver is within working radius.

2. If Gold and/or Silver is not found within a city's working radius, the Mint is not revealed in that city's production menu.

3. If Gold and/or Silver is found within a city's working radius, the Mint is revealed in that city's production menu and;

4. If Gold and/or Silver is unimproved within said city's working radius, you cannot construct the Mint.

5. If Gold and/or Silver is improved within said city's working radius, you can construct the Mint.

Simply put, change the word "cannot" to "can" within step 4 and this is what I'm requesting. I have a few buildings that I would like to utilize such a script.
 
Code:
local iNeededResource = GameInfoTypes.RESOURCE_GOLD
local iDummyBuilding = GameInfoTypes.BUILDING_MOD_DUMMY -- dummy building required to build your building
local iMine = GameInfoTypes.IMPROVEMENT_MINE

local function CheckCityPlots(pCity)
	for iCityPlot = 0, pCity:GetNumCityPlots() - 1 do
		local pPlot = pCity:GetCityIndexPlot(iCityPlot)
		if pPlot and pPlot:GetWorkingCity() == pCity and pPlot:GetResourceType() == iNeededResource and pPlot:GetImprovementType() == -1 then
			pCity:SetNumRealBuilding(iDummyBuilding, 1)
			break
		end
	end
end

local function OnPlayerCityFounded(iPlayer, x, y)
	local pCityPlot = Map.GetPlot(x,y)
	local pCity = pCityPlot:GetPlotCity()
	CheckCityPlots(pCity)
end
GameEvents.PlayerCityFounded.Add(OnPlayerCityFounded)

local function OnBuildFinished( iPlayerID, x, y, iImprovementID )
	if iImprovementID == iMine then
		local pPlot = Map.GetPlot(x, y)
		local pCity = pPlot:GetWorkingCity()
		if pCity and pCity:GetNumRealBuilding(iDummyBuilding) > 0 then
			pCity:SetNumRealBuilding(iDummyBuilding, 0)
			CheckCityPlots(pCity)
		end
	end
end
GameEvents.BuildFinished.Add(OnBuildFinished)
 
So if I was using this script for a new building which gives +1 food to banana, wheat, and citrus, would it look like this?

Spoiler :
Code:
local iNeededResource = GameInfoTypes.RESOURCE_BANANA
local iDummyBuilding = GameInfoTypes.BUILDING_MOD_DUMMY -- dummy building required to build your building
local iPlantation = GameInfoTypes.IMPROVEMENT_PLANTATION

local function CheckCityPlots(pCity)
	for iCityPlot = 0, pCity:GetNumCityPlots() - 1 do
		local pPlot = pCity:GetCityIndexPlot(iCityPlot)
		if pPlot and pPlot:GetWorkingCity() == pCity and pPlot:GetResourceType() == iNeededResource and pPlot:GetImprovementType() == -1 then
			pCity:SetNumRealBuilding(iDummyBuilding, 1)
			break
		end
	end
end

local function OnPlayerCityFounded(iPlayer, x, y)
	local pCityPlot = Map.GetPlot(x,y)
	local pCity = pCityPlot:GetPlotCity()
	CheckCityPlots(pCity)
end
GameEvents.PlayerCityFounded.Add(OnPlayerCityFounded)

local function OnBuildFinished( iPlayerID, x, y, iImprovementID )
	if iImprovementID == iPlantation then
		local pPlot = Map.GetPlot(x, y)
		local pCity = pPlot:GetWorkingCity()
		if pCity and pCity:GetNumRealBuilding(iDummyBuilding) > 0 then
			pCity:SetNumRealBuilding(iDummyBuilding, 0)
			CheckCityPlots(pCity)
		end
	end
end
GameEvents.BuildFinished.Add(OnBuildFinished)

Where would I include the citrus and wheat? Just copy/paste the code three times over?
 
So if I was using this script for a new building which gives +1 food to banana, wheat, and citrus, would it look like this?

You could only need to make the GameInfoTypes.RESOURCE_CITRUS and GameInfoTypes.RESOURCE_WHEAT template. So, instead of copying you would need to place this at the top.
Code:
local iNeededResource1 = GameInfoTypes.RESOURCE_BANANA
local iNeededResource2 = GameInfoTypes.RESOURCE_CITRUS
local iNeededResource3 = GameInfoTypes.RESOURCE_WHEAT

and turn this code snippet somewhere in your code
Code:
pPlot:GetResourceType() == iNeededResource
into
Code:
 (pPlot:GetResourceType() == iNeededResource1 or pPlot:GetResourceType() == iNeededResource2 or pPlot:GetResourceType() == iNeededResource3)
 
I forgot about city border expansion. This script fixes it.
Code:
local iNeededResource1 = GameInfoTypes.RESOURCE_BANANA
local iNeededResource2 = GameInfoTypes.RESOURCE_CITRUS
local iNeededResource3 = GameInfoTypes.RESOURCE_WHEAT
local iDummyBuilding = GameInfoTypes.BUILDING_MOD_DUMMY -- dummy building required to build your building
local iPlantation = GameInfoTypes.IMPROVEMENT_PLANTATION

local function CheckCityPlots(pCity)
	for iCityPlot = 0, pCity:GetNumCityPlots() - 1 do
		local pPlot = pCity:GetCityIndexPlot(iCityPlot)
		if pPlot and pPlot:GetWorkingCity() == pCity and pPlot:GetImprovementType() == -1 then
			local iResource = pPlot:GetResourceType() 
			if iResource == iNeededResource1 or iResource == iNeededResource2 or iResource == iNeededResource3 then
				pCity:SetNumRealBuilding(iDummyBuilding, 1)
				break
			end
		end
	end
end

local function OnPlayerCityFounded(iPlayer, x, y)
	local pCityPlot = Map.GetPlot(x,y)
	local pCity = pCityPlot:GetPlotCity()
	CheckCityPlots(pCity)
end
GameEvents.PlayerCityFounded.Add(OnPlayerCityFounded)

local function OnBuildFinished( iPlayerID, x, y, iImprovementID )
	if iImprovementID == iPlantation then
		local pPlot = Map.GetPlot(x, y)
		local pCity = pPlot:GetWorkingCity()
		if pCity and pCity:GetNumRealBuilding(iDummyBuilding) > 0 then
			pCity:SetNumRealBuilding(iDummyBuilding, 0)
			CheckCityPlots(pCity)
		end
	end
end
GameEvents.BuildFinished.Add(OnBuildFinished)

local function OnCityBoughtPlot(iPlayer, iCity, iCityX, iCityY, bGold, bFaithOrCulture)
	local pCity = Players[iPlayer]:GetCityByID(iCity)
	if pCity:GetNumRealBuilding(iDummyBuilding) == 0 then
		CheckCityPlots(pCity)
	end
end
GameEvents.CityBoughtPlot.Add( OnCityBoughtPlot )
And you probably wanna do something when an improvement on one of those resources is build near city that has your building.
 
Top Bottom