lua functions from CEP

ExpiredReign

Deity
Joined
Jan 3, 2013
Messages
2,450
Location
Tasmania
There are a few functions in CEP that may still, if wanted, need to be ported to here.

I'll try and pinpoint the most obvious ones that we could use. There are however a lot of functions that require CEP's secondary functions such as YieldLibrary.lua. As that entire function should be now obsolete a great many of these could be scrapped or redone if needed.
Where possible I'll post the function in its entirety, along with any 'includes' it calls.

I am not 100% aware of all the new DLL functions or existing modded DLL functions as in W's PNM version, so some of these may already be a given.

I'll update this post with additions/removals/suggestions etc.

------
1) function DoCitystateSurrender(player) -- Used in a custom trait assigned to Genghis. -- added as of 21/07/2014
Spoiler :
Code:
function DoCitystateSurrender(player)
	if not player:GetTraitInfo().BullySurrender then
		return
	end
	
	local playerID = player:GetID()
	local activePlayer = Players[Game.GetActivePlayer()]
	for csID, cs in pairs(Players) do
		if cs:IsAliveCiv() and cs:IsMinorCiv() and cs:CanMajorBullyUnit(playerID) and cs:IsAtPeace(player) then
			local capital = cs:GetCapitalCity()
				local alertText = string.format("%s surrenders in fear to %s!", cs:GetName(), player:GetName())
				log:Info(alertText)
				for city in cs:Cities() do
					player:AcquireCity(city, true, false)
				end
				if activePlayer:HasMet(cs) then
					Events.GameplayAlertMessage(alertText, player)
				end
		end
	end
end

This next one may be pretty contentious. If I am understanding it correctly, it was used to add extra yields to certain plots that are deemed underwhelming, we have already started this discussion elsewhere and it may have a bearing on that.

------
2) function UpdatePlotYields()
Spoiler :
Code:
function UpdatePlotYields()
	print("UpdatePlotYields Start")
	--
	if UI:IsLoadedGame() then
		return
	end
	--]]
	for plotID, plot in Plots() do
		local x				= plot:GetX()
		local y				= plot:GetY()
		
		if plot:GetTerrainType() == TerrainTypes.TERRAIN_DESERT and not plot:IsHills() and plot:GetFeatureType() == -1 then
			local resInfo = GameInfo.Resources[plot:GetResourceType()]
			if plot:IsFreshWater() then
				Game.SetPlotExtraYield( x, y, YieldTypes.YIELD_FOOD, 1)
			elseif resInfo then
				if resInfo.ResourceClassType == "RESOURCECLASS_BONUS" and resInfo.Type ~= "RESOURCE_STONE" then
					Game.SetPlotExtraYield( x, y, YieldTypes.YIELD_FOOD, 1)
				elseif resInfo.Happiness > 0 then
					Game.SetPlotExtraYield( x, y, YieldTypes.YIELD_GOLD, 1)
				elseif not resInfo.TechReveal then
					Game.SetPlotExtraYield( x, y, YieldTypes.YIELD_PRODUCTION, 1)
				end
			end
		end
	end
	print("UpdatePlotYields Done")
end
Events.SequenceGameInitComplete.Add(function() return SafeCall(UpdatePlotYields) end)

------
Also to do with plots, yields and improvements is:

3) function CreateWorkboats(player, techID, changeID)
Spoiler :
Code:
function CreateWorkboats(player, techID, changeID)
	local playerID = player:GetID()
	if player:IsHuman() then
		return
	end
	local techType = GameInfo.Technologies[techID].Type
	for buildInfo in GameInfo.Builds(string.format("ImprovementType IS NOT NULL AND ShowInTechTree AND PrereqTech = '%s'", techType)) do
		local improveInfo = GameInfo.Improvements[buildInfo.ImprovementType]
		if improveInfo.Water then
			log:Info("Checking for workboats %s %s %s", player:GetName(), techType, improveInfo.Type)
			for city in player:Cities() do
				if city:IsCoastal() then
					for plot in Plot_GetPlotsInCircle(city:Plot(), 1, 2) do
						local resInfo = GameInfo.Resources[plot:GetResourceType()]
						if resInfo and plot:GetPlotType() == PlotTypes.PLOT_OCEAN and plot:GetImprovementType() == -1 then
							if Game.HasValue( {ImprovementType=improveInfo.Type, ResourceType=resInfo.Type}, GameInfo.Improvement_ResourceTypes ) then
								log:Info("  %s spawned for %s", city:GetName(), resInfo.Type)
								player:InitUnitClass("UNITCLASS_WORKBOAT", city:Plot())
							end
						end
					end
				end
			end
		end
	end
end
LuaEvents.NewTech.Add(CreateWorkboats)

I think this was to help CSs work their coasts more efficiently.

------
Next is one that does call on a function from YieldLibrary.

4) function GreatImprovementEraBonus(hexX, hexY, cultureArtID, continentArtID, playerID, engineImprovementTypeDoNotUse, improvementID, engineResourceTypeDoNotUse, resID, eraID, improvementState)
Spoiler :
Code:
function GreatImprovementEraBonus(hexX, hexY, cultureArtID, continentArtID, playerID, engineImprovementTypeDoNotUse, improvementID, engineResourceTypeDoNotUse, resID, eraID, improvementState)
	--print("OnImprovementCreated");
	--print("hexX: " .. hexX);
	--print("hexY: " .. hexY);
	--print("cultureArtID: " .. cultureArtID);
	--print("playerID: " .. playerID);
	--print("improvementID: " .. improvementID);
	--print("resID: " .. resID);
	--print("eraID: " .. eraID);
	--print("improvementState: " .. improvementState);
	--print("------------------");

	local impInfo	= GameInfo.Improvements[improvementID]
	local player	= Players[playerID]
	local plot		= Map.GetPlot(ToGridFromHex(hexX, hexY))
	if not impInfo.CreatedByGreatPerson or eraID == 0 then
		return
	end

	for yieldInfo in GameInfo.Yields() do
		local extraYield = GetImprovementExtraYield(impInfo.ID, yieldInfo.ID, player)

		if extraYield > 0 then
			log:Info("%s + %s %s", impInfo.Type, extraYield, yieldInfo.Type)
			Plot_ChangeYield(plot, yieldInfo.ID, extraYield)
		end
	end
end

I think the intent of that function is self-evident.

Edit: 19/7/14

------
5) function City_ChangeYieldStored(city, yieldID, amount, checkThreshold) -- added as of 22/07/2014
Spoiler :
Code:
function City_ChangeYieldStored(city, yieldID, amount, checkThreshold)
	if city == nil then
		log:Fatal("City_ChangeYieldStored city=nil")
	elseif itemTable and not itemID then
		log:Fatal("City_ChangeYieldStored itemID=nil")
	end
	local player = Players[city:GetOwner()]
	if yieldID == YieldTypes.YIELD_FOOD then
		city:ChangeFood(amount)
		local overflow = City_GetYieldStored(city, yieldID) - City_GetYieldNeeded(city, yieldID)
		if checkThreshold and overflow >= 0 then
			local totalYieldKept = 0
			for building in GameInfo.Buildings("FoodKept <> 0") do
				if city:IsHasBuilding(building.ID) then
					totalYieldKept = totalYieldKept + building.FoodKept / 100
				end
			end
			city:ChangePopulation(1,true)
			city:SetFood(0)
			if totalYieldKept > 0 then
				log:Warn(
					"%s add %s food = %s overflow + %s totalYieldKept * %s City_GetYieldNeeded", 
					city:GetName(), 
					overflow + totalYieldKept * City_GetYieldNeeded(city, yieldID), 
					overflow, 
					totalYieldKept, 
					City_GetYieldNeeded(city, yieldID)
				)
			end
			City_ChangeYieldStored(city, yieldID, overflow + totalYieldKept * City_GetYieldNeeded(city, yieldID), true)
		end
	elseif yieldID == YieldTypes.YIELD_PRODUCTION then
		city:ChangeProduction(amount)
	elseif yieldID == YieldTypes.YIELD_CULTURE then
		city:ChangeJONSCultureStored(amount)
		player:ChangeYieldStored(YieldTypes.YIELD_CULTURE, amount)
		local overflow = City_GetYieldStored(city, yieldID) - City_GetYieldNeeded(city, yieldID)
		if checkThreshold and overflow >= 0 then
			city:DoJONSCultureLevelIncrease()
			city:SetJONSCultureStored(0)
			City_ChangeYieldStored(city, yieldID, overflow, true)
		end
	elseif yieldID == YieldTypes.YIELD_POPULATION then
		city:ChangePopulation(amount, true)
	else
		player:ChangeYieldStored(yieldID, amount)
	end
end

A number of UBs were setup to use this function. This function is used by another custom function (function BuildingCreated(player, city, buildingID)) to provide an instant YIELD as defined in a new table: Building_YieldInstant. That table is created thus:

Code:
CREATE TABLE IF NOT EXISTS
	Building_YieldInstant (
	BuildingType		text REFERENCES Buildings(Type),
	YieldType			text,
	Yield				integer NOT NULL 
);
 
Added another function I came across as I looked through the CEP building list.

See #5 above.
 
CS surrender function added, with notifications and everything. Works as advertised. You can scratch it off your list. (I love little changes like this - 5 minutes to write that change. So much easier than AI stuff!!)

I'll work on the 'instant yield' one and give it a function table in the XML. 2-4 don't feel terribly necessary, but perhaps I'm not reading them correctly.

2.) if we're going to buff tiles in the xml, we don't really need to use lua to do more than that (unless I'm misunderstanding the function).

3.) Increasing AI proclivity for building workboats with flavors would accomplish this without simply giving the AI units, I think.

4.) I think buffing great people improvements from the start will help them do better in the long run. Plus, isn't there already code to improve them with techs?

If my explanation doesn't make sense, I've probably misunderstood what the function is for.

Cheers,
G
 
2) I think the way that function works is to seed more stone (or other bonus resources) into weaker terrain rather than to make the stone better.

Other two I'd agree with your take that they aren't needed.
 
I'm fine with leaving these functions alone if we want. I only added them for discussion because as I look through the CEP code I don't automatically see a new function from the modded DLL that does the same.
 
2) I think the way that function works is to seed more stone (or other bonus resources) into weaker terrain rather than to make the stone better.

Other two I'd agree with your take that they aren't needed.

Ah, I didn't realize it was worldgen code. I haven't messed with the worldgen part of the dll, but I'll give it a look.
G
 
I haven't messed with the worldgen part of the dll,

I wouldn't bother, almost all of the worldgen related code in the DLL was moved into "that script which must not be named"
 
#5 is done, works for all yields (including golden age, great admiral and great general), so you can scratch that one off. Added two new yieldtypes - 'YIELD_POPULATION' and 'YIELD_CULTURE_LOCAL' for this function. Former adds population to the city only, whereas the latter works for border growth (but doesn't add to player culture amount). These new yields do not work for any other function (for now), and I'm not going to torture poor Whoward with more yields to add to his spaghetti.

Do we want this to work for all the other great people? It'll increase the number of yields we have dramatically...is that worth it, especially when we can add rates to buildings already?

G
 
Do we want this to work for all the other great people? It'll increase the number of yields we have dramatically...is that worth it, especially when we can add rates to buildings already?

Well... I'm of the opinion that it is better to have it in the code and available, though not initially used, than to want to use it somewhere and it isn't in the code to do so.

But I'm not the one doing the coding so... Would it hurt to include it? I defer to those with greater knowledge on this.
 
Back
Top Bottom