DJSHenninger
Megas Basileus
So, for a trait of a certain civilization, I've got some lua code.. The trait is: Worked Farms and Plantations automatically contribute Production towards National Wonders. Basically, the code randomly chooses a National Wonder and adds Production, but only if that National Wonder can be built. Regardless, if it chooses a National Wonder that can't be built, no Production is added and the function ends. What I'd like is some kind of way to 're-fire' the lua, or the random bit, if the code chose a National Wonder that can't be built (yet), until it chooses a Wonder that can be built. So, for example, if the National Epic is the only buildable Wonder, all worked Farms/Plantations add Production to the National Epic that turn. Any idea how to do this.. easily? Because I'm not that familiar with lua and the terminology and all that..
CODE:
CODE:
Code:
function DJSH_KhmerAutoProdNationalWonders(PlayerID)
local player = Players[PlayerID]
if (player:GetCivilizationType() == GameInfoTypes["CIVILIZATION_DJSH_KHMER"] and player:IsAlive()) then
for city in player:Cities() do
for cityPlot = 0, city:GetNumCityPlots() - 1, 1 do
local plot = city:GetCityIndexPlot(cityPlot)
if plot ~= nil then
if plot:GetOwner() == city:GetOwner() then
if (plot:GetImprovementType() == GameInfoTypes["IMPROVEMENT_FARM"] or plot:GetImprovementType() == GameInfoTypes["IMPROVEMENT_PLANTATION"]) then
if city:IsWorkingPlot(plot) then
local random = Map.Rand(11, "Random National Wonder Bonuses Lua")
if random == 0 then
if city:CanConstruct(GameInfoTypes["BUILDING_NATIONAL_EPIC"]) then
local iProdValue = math.floor(city:GetBuildingProductionNeeded(GameInfoTypes["BUILDING_NATIONAL_EPIC"]) / 50)
city:ChangeBuildingProduction(GameInfoTypes["BUILDING_NATIONAL_EPIC"], -iProdValue)
if iProdValue > 0 then
Events.AddPopupTextEvent(HexToWorld(ToHexFromGrid(Vector2(plot:GetX(), plot:GetY()))), Locale.ConvertTextKey("+{1_Num} [COLOR_POSITIVE_TEXT][ICON_PRODUCTION]Production[ENDCOLOR] towards National Epic!", iProdValue))
end
end
end
if random == 1 then
if city:CanConstruct(GameInfoTypes["BUILDING_NATIONAL_COLLEGE"]) then
local iProdValue = math.floor(city:GetBuildingProductionNeeded(GameInfoTypes["BUILDING_NATIONAL_COLLEGE"]) / 50)
city:ChangeBuildingProduction(GameInfoTypes["BUILDING_NATIONAL_COLLEGE"], -iProdValue)
if iProdValue > 0 then
Events.AddPopupTextEvent(HexToWorld(ToHexFromGrid(Vector2(plot:GetX(), plot:GetY()))), Locale.ConvertTextKey("+{1_Num} [COLOR_POSITIVE_TEXT][ICON_PRODUCTION]Production[ENDCOLOR] towards National College!", iProdValue))
end
end
end
if random == 2 then
if city:CanConstruct(GameInfoTypes["BUILDING_NATIONAL_TREASURY"]) then
local iProdValue = math.floor(city:GetBuildingProductionNeeded(GameInfoTypes["BUILDING_NATIONAL_TREASURY"]) / 50)
city:ChangeBuildingProduction(GameInfoTypes["BUILDING_NATIONAL_TREASURY"], -iProdValue)
if iProdValue > 0 then
Events.AddPopupTextEvent(HexToWorld(ToHexFromGrid(Vector2(plot:GetX(), plot:GetY()))), Locale.ConvertTextKey("+{1_Num} [COLOR_POSITIVE_TEXT][ICON_PRODUCTION]Production[ENDCOLOR] towards East India Company!", iProdValue))
end
end
end
if random == 3 then
if city:CanConstruct(GameInfoTypes["BUILDING_HEROIC_EPIC"]) then
local iProdValue = math.floor(city:GetBuildingProductionNeeded(GameInfoTypes["BUILDING_HEROIC_EPIC"]) / 50)
city:ChangeBuildingProduction(GameInfoTypes["BUILDING_HEROIC_EPIC"], -iProdValue)
if iProdValue > 0 then
Events.AddPopupTextEvent(HexToWorld(ToHexFromGrid(Vector2(plot:GetX(), plot:GetY()))), Locale.ConvertTextKey("+{1_Num} [COLOR_POSITIVE_TEXT][ICON_PRODUCTION]Production[ENDCOLOR] towards Heroic Epic!", iProdValue))
end
end
end
if random == 4 then
if city:CanConstruct(GameInfoTypes["BUILDING_CIRCUS_MAXIMUS"]) then
local iProdValue = math.floor(city:GetBuildingProductionNeeded(GameInfoTypes["BUILDING_CIRCUS_MAXIMUS"]) / 50)
city:ChangeBuildingProduction(GameInfoTypes["BUILDING_CIRCUS_MAXIMUS"], -iProdValue)
if iProdValue > 0 then
Events.AddPopupTextEvent(HexToWorld(ToHexFromGrid(Vector2(plot:GetX(), plot:GetY()))), Locale.ConvertTextKey("+{1_Num} [COLOR_POSITIVE_TEXT][ICON_PRODUCTION]Production[ENDCOLOR] towards Circus Maximus!", iProdValue))
end
end
end
if random == 5 then
if city:CanConstruct(GameInfoTypes["BUILDING_GRAND_TEMPLE"]) then
local iProdValue = math.floor(city:GetBuildingProductionNeeded(GameInfoTypes["BUILDING_GRAND_TEMPLE"]) / 50)
city:ChangeBuildingProduction(GameInfoTypes["BUILDING_GRAND_TEMPLE"], -iProdValue)
if iProdValue > 0 then
Events.AddPopupTextEvent(HexToWorld(ToHexFromGrid(Vector2(plot:GetX(), plot:GetY()))), Locale.ConvertTextKey("+{1_Num} [COLOR_POSITIVE_TEXT][ICON_PRODUCTION]Production[ENDCOLOR] towards Grand Temple!", iProdValue))
end
end
end
if random == 6 then
if city:CanConstruct(GameInfoTypes["BUILDING_IRONWORKS"]) then
local iProdValue = math.floor(city:GetBuildingProductionNeeded(GameInfoTypes["BUILDING_IRONWORKS"]) / 50)
city:ChangeBuildingProduction(GameInfoTypes["BUILDING_IRONWORKS"], -iProdValue)
if iProdValue > 0 then
Events.AddPopupTextEvent(HexToWorld(ToHexFromGrid(Vector2(plot:GetX(), plot:GetY()))), Locale.ConvertTextKey("+{1_Num} [COLOR_POSITIVE_TEXT][ICON_PRODUCTION]Production[ENDCOLOR] towards Ironworks!", iProdValue))
end
end
end
if random == 7 then
if city:CanConstruct(GameInfoTypes["BUILDING_HERMITAGE"]) then
local iProdValue = math.floor(city:GetBuildingProductionNeeded(GameInfoTypes["BUILDING_HERMITAGE"]) / 50)
city:ChangeBuildingProduction(GameInfoTypes["BUILDING_HERMITAGE"], -iProdValue)
if iProdValue > 0 then
Events.AddPopupTextEvent(HexToWorld(ToHexFromGrid(Vector2(plot:GetX(), plot:GetY()))), Locale.ConvertTextKey("+{1_Num} [COLOR_POSITIVE_TEXT][ICON_PRODUCTION]Production[ENDCOLOR] towards Hermitage!", iProdValue))
end
end
end
if random == 8 then
if city:CanConstruct(GameInfoTypes["BUILDING_OXFORD_UNIVERSITY"]) then
local iProdValue = math.floor(city:GetBuildingProductionNeeded(GameInfoTypes["BUILDING_OXFORD_UNIVERSITY"]) / 50)
city:ChangeBuildingProduction(GameInfoTypes["BUILDING_OXFORD_UNIVERSITY"], -iProdValue)
if iProdValue > 0 then
Events.AddPopupTextEvent(HexToWorld(ToHexFromGrid(Vector2(plot:GetX(), plot:GetY()))), Locale.ConvertTextKey("+{1_Num} [COLOR_POSITIVE_TEXT][ICON_PRODUCTION]Production[ENDCOLOR] towards Oxford University!", iProdValue))
end
end
end
if random == 9 then
if city:CanConstruct(GameInfoTypes["BUILDING_TOURIST_CENTER"]) then
local iProdValue = math.floor(city:GetBuildingProductionNeeded(GameInfoTypes["BUILDING_TOURIST_CENTER"]) / 50)
city:ChangeBuildingProduction(GameInfoTypes["BUILDING_TOURIST_CENTER"], -iProdValue)
if iProdValue > 0 then
Events.AddPopupTextEvent(HexToWorld(ToHexFromGrid(Vector2(plot:GetX(), plot:GetY()))), Locale.ConvertTextKey("+{1_Num} [COLOR_POSITIVE_TEXT][ICON_PRODUCTION]Production[ENDCOLOR] towards National Visitor Center!", iProdValue))
end
end
end
if random == 10 then
if city:CanConstruct(GameInfoTypes["BUILDING_INTELLIGENCE_AGENCY"]) then
local iProdValue = math.floor(city:GetBuildingProductionNeeded(GameInfoTypes["BUILDING_INTELLIGENCE_AGENCY"]) / 50)
city:ChangeBuildingProduction(GameInfoTypes["BUILDING_INTELLIGENCE_AGENCY"], -iProdValue)
if iProdValue > 0 then
Events.AddPopupTextEvent(HexToWorld(ToHexFromGrid(Vector2(plot:GetX(), plot:GetY()))), Locale.ConvertTextKey("+{1_Num} [COLOR_POSITIVE_TEXT][ICON_PRODUCTION]Production[ENDCOLOR] towards National Intelligence Agency!", iProdValue))
end
end
end
end
end
end
end
end
end
end
end
GameEvents.PlayerDoTurn.Add(DJSH_KhmerAutoProdNationalWonders)