LeeS
Imperator
I'm currently working on a unit production modifier system wherein a building will modify unit production based on a unit's class rather than on its UnitCombat type.
Currently we have the ability to set a building as have X modifier for unit domain and unit combat type (tables Building_DomainProductionModifiers and Building_UnitCombatProductionModifiers) as well as table Unit_ProductionModifierBuildings. None of these allow specification of a Unit-Class, however, and Unit_ProductionModifierBuildings is darn near useless as soon as one begins to consider other mods and units they may introduce into the game. Table Building_UnitCombatProductionModifiers is unfortunate especially as regards gunpowder units because so very many different unit-classes are contained within the larger UNITCOMBAT_GUN.
Everything in the code as I have it at the moment is functioning correctly, but I would like to make this system available to any who may want to use it, so:
-----------------------------------------------------------------------------------------------------------
For this SQL code:
Have I got everything correct in my "INSERT OR REPLACE INTO" and "CREATE TABLE IF NOT EXISTS" methods so that if three different mods are all running this system, the second and third will not cause file-loading failure, and there will only end within the database with one version of the new table, the building-class, and the building.
This is only the second time I've attempted these more advanced SQL methods, so I have zero confidence everything is correct re the usages when multiple mods are all trying to use the same system.
----------------------------------------------------------------------------------------------------------
Lua event hook advice:
As reference, here is the code I am currently using:
Currently we have the ability to set a building as have X modifier for unit domain and unit combat type (tables Building_DomainProductionModifiers and Building_UnitCombatProductionModifiers) as well as table Unit_ProductionModifierBuildings. None of these allow specification of a Unit-Class, however, and Unit_ProductionModifierBuildings is darn near useless as soon as one begins to consider other mods and units they may introduce into the game. Table Building_UnitCombatProductionModifiers is unfortunate especially as regards gunpowder units because so very many different unit-classes are contained within the larger UNITCOMBAT_GUN.
Everything in the code as I have it at the moment is functioning correctly, but I would like to make this system available to any who may want to use it, so:
-----------------------------------------------------------------------------------------------------------
For this SQL code:
Spoiler :
Code:
CREATE TABLE IF NOT EXISTS
Building_UnitClassProductionModifiers (
BuildingType text REFERENCES Buildings(TYPE) DEFAULT NULL,
UnitClassType text REFERENCES UnitClasses(TYPE) DEFAULT NULL,
Modifier integer DEFAULT 0);
--=======================================Following formats borrowed from JFD================================================
-- BuildingClasses
--==========================================================================================================================
INSERT OR REPLACE INTO BuildingClasses
(Type, DefaultBuilding, Description, NoLimit)
VALUES ('BUILDINGCLASS_UNITCLASS_PRODMOD', 'BUILDING_UNITCLASS_PRODMOD', 'TXT_KEY_BUILDING_UNITCLASS_PRODMOD', 1);
--==========================================================================================================================
-- Buildings
--==========================================================================================================================
INSERT OR REPLACE INTO Buildings
(Type, BuildingClass, Cost, FaithCost, GreatWorkCount, PrereqTech, MilitaryProductionModifier, Description, NeverCapture, IconAtlas, PortraitIndex)
VALUES ('BUILDING_UNITCLASS_PRODMOD', 'BUILDINGCLASS_UNITCLASS_PRODMOD', -1, -1, -1, NULL, 1, 'TXT_KEY_BUILDING_UNITCLASS_PRODMOD', 1, 'BW_ATLAS_1', 19);
This is only the second time I've attempted these more advanced SQL methods, so I have zero confidence everything is correct re the usages when multiple mods are all trying to use the same system.
----------------------------------------------------------------------------------------------------------
Lua event hook advice:
As reference, here is the code I am currently using:
Spoiler :
Code:
local iProductionModifierUnitClassesBuilding = GameInfoTypes.BUILDING_UNITCLASS_PRODMOD
local tBuildingsToCheck = {}
local tUnitsToCheck = {}
for row in GameInfo.Building_UnitClassProductionModifiers() do
local sUnitClassType = row.UnitClassType
local iBuildingID = GameInfoTypes[row.BuildingType]
local iModifier = row.Modifier
if tBuildingsToCheck[iBuildingID] == nil then
tBuildingsToCheck[iBuildingID] = "true"
end
for Unit in GameInfo.Units("Class = '" .. sUnitClassType .. "'") do
if tUnitsToCheck[Unit.ID] == nil then
tUnitsToCheck[Unit.ID] = {}
end
tUnitsToCheck[Unit.ID][iBuildingID] = iModifier
end
end
function GetNumberUnitClassProdModifiersToSet(pCity)
local iNumberOfBuildingsToSet = 0
if (pCity:GetProductionUnit() > -1) then
if (tUnitsToCheck[pCity:GetProductionUnit()]) then
for iBuildingID,iModifier in pairs(tUnitsToCheck[pCity:GetProductionUnit()]) do
if pCity:IsHasBuilding(iBuildingID) then
iNumberOfBuildingsToSet = iNumberOfBuildingsToSet + iModifier
end
end
end
end
return iNumberOfBuildingsToSet
end
function Building_UnitClassProductionModifiers(iPlayer)
local pPlayer = Players[iPlayer]
if pPlayer:GetCivilizationType() == GameInfoTypes["CIVILIZATION_BARBARIAN"] then return end
if not pPlayer:IsAlive() then return end
for pCity in pPlayer:Cities() do
pCity:SetNumRealBuilding(iProductionModifierUnitClassesBuilding, GetNumberUnitClassProdModifiersToSet(pCity))
end
end
function CityBuiltOrBoughtBuilding_UnitClassProductionModifiers(ownerId, cityId, buildingType, bGold, bFaithOrCulture)
if tBuildingsToCheck[buildingType] then
local pCity = Players[ownerId]:GetCityByID(cityId);
pCity:SetNumRealBuilding(iProductionModifierUnitClassesBuilding, GetNumberUnitClassProdModifiersToSet(pCity))
end
end
GameEvents.PlayerDoTurn.Add(Building_UnitClassProductionModifiers)
GameEvents.CityConstructed.Add(CityBuiltOrBoughtBuilding_UnitClassProductionModifiers)
- I am not concerned really with code as written so far. It functions properly and seems to be reasonably-well adapted to using data-driven techniques, so shouldn't be a terrible processing-time hog.
- What I don't have the moment, and am not sure what the best event hook would be, is to make the modifiers appear in the city as soon as the player selects one of the units that qualify for the modifier as their city's production-item.
- As-is, the code will only fire on turn processing, or upon completion of one of the buildings that give the modifier (in this case as a result of gold-purchasing would be the only realistic way this could happen)
- Will the suggested hook event fire also for the AI when they "select" their production item ?
- I haven't done much successful city-view or city-production-qeue modding, hence the request for advice on the best hook to use to accomplish the real-time addition of the modifiers.