Finding a Civ's UB (and its default building) with lua

turingmachine

Emperor
Joined
May 4, 2008
Messages
1,438
Okay, I'm wondering if you guys can help me out with something.

In Civ IV with python I could do something like:

Code:
civtype = pPlayer.getCivilizationType()
for iBuilding in range(gc.getNumBuildingClassInfos()):
	iUniqueBuilding = gc.getCivilizationInfo(civtype).getCivilizationBuildings(iBuilding);
	iDefaultBuilding = gc.getBuildingClassInfo(iBuilding).getDefaultBuildingIndex();

My lua isn't as good as my python. Can something similar be done in Civ V (and if so, any advice as to how to write it in lua)?
 
Something like this:

Code:
function GetUbOverride(iPlayer)
	local player = Players[iPlayer];
	local civType = GameInfo.Civilizations[player:GetCivilizationType()].Type;
	local constraint = string.format("CivilizationType = '%s'", civType);
	for buildingOverride in GameInfo.Civilization_BuildingClassOverrides(constraint)() do
		-- only first
		return buildingOverride;
	end
	-- ... or nothing
	return nil;
end

function DoSomething(iPlayer)
	buildingOverride = GetUbOverride(iPlayer);
	if buildingOverride ~= nil then
		local originalClass = buildingOverride.BuildingClassType;
		local UbType = buildingOverride.BuildingType;
	end
end
GameEvents.PlayerDoTurn.Add( DoSomething );

I didn't test, but it should be close.
 
Something like this:

Code:
function GetUbOverride(iPlayer)
	local player = Players[iPlayer];
	local civType = GameInfo.Civilizations[player:GetCivilizationType()].Type;
	local constraint = string.format("CivilizationType = '%s'", civType);
	for buildingOverride in GameInfo.Civilization_BuildingClassOverrides(constraint)() do
		-- only first
		return buildingOverride;
	end
	-- ... or nothing
	return nil;
end

function DoSomething(iPlayer)
	buildingOverride = GetUbOverride(iPlayer);
	if buildingOverride ~= nil then
		local originalClass = buildingOverride.BuildingClassType;
		local UbType = buildingOverride.BuildingType;
	end
end
GameEvents.PlayerDoTurn.Add( DoSomething );

I didn't test, but it should be close.

Wow, thanks a lot!!!!!!!!! I had given up on this.

On follow up question. I see at the end I get the original building class, but how can I get the original building type. Basically, I need to check a city and see if it has the default building and if so replace it with the UB (all of which I know how to do if I have the original building stored to compare).
 
That would be:

local defaultBuildingType = GameInfo.BuildingClasses[originalClass].DefaultBuilding;

Basically you can access any table and index it with a column as long as that column has unique values. E.g.:

local defaultBuilding = GameInfo.Buildings[defaultBuildingType];

Because the Type is unique for Buildings.
 
Back
Top Bottom