Lua ChangeProduction Event?

legojoshua12

Chieftain
Joined
Apr 19, 2018
Messages
13
Is there a way to detect when the player have changed what the city is currently producing? I need to be able to check this to process 20% production into city defense. Any help is greatly appreciated!
 
There's no event that I know of, but it's a simple case of storing what the city was producing last turn and compare it to what it's producing this turn
 
I actually achieved such thing for my Whomp's Fortress Civilisation using a dummy policy and SQL (and a bit of Lua to grant the dummy policy):
(Some code for a single special case was removed)

It utilises the <Policy_BuildingClassProductionModifiers>-column from the <Policies> table (that I believe is also used to grant bonus production to Shrines and Temples when adopting Piety)
Spoiler :

Code:
--===========Trait==============================================================================
--+20% Production Towards Defensive Buildings (I.e. Defense > 0 OR ExtraCityHitpoints > 0;
--Takes BuildingClassOverrides into account.
--                E.g. no bonus when override DOESN'T provides defenses (irregardless of the default building)
--                E.g. applied bonus when override DOES provide defenses (irregardless of the default building)
-- IGNORES Buildings that cannot be constructed (I.e. those that have a negative cost)
--Dummy policy is granted via LUA

INSERT INTO Policies
        (Type,                                                Description,                                                    Civilopedia,                                                        Help,                                                            PolicyBranchType,    CultureCost,    GridX,    GridY,    PortraitIndex,    IconAtlas,                        IconAtlasAchieved)
VALUES    ('POLICY_TRL_PRODUCTION_BONUS_DEFENSIVE_BUILDINGS',    'TXT_KEY_POLICY_TRL_PRODUCTION_BONUS_DEFENSIVE_BUILDINGS',    'TXT_KEY_POLICY_TRL_PRODUCTION_BONUS_DEFENSIVE_BUILDINGS_HELP',    'TXT_KEY_POLICY_TRL_PRODUCTION_BONUS_DEFENSIVE_BUILDINGS_HELP',    NULL,                -1,            -1,        -1,        0,                'EXPANSIONPATCH_POLICY_ATLAS',    'EXPANSIONPATCH_POLICY_ACHIEVED_ATLAS');

INSERT INTO Policy_BuildingClassProductionModifiers
            (PolicyType,                                            BuildingClassType,    ProductionModifier)
    SELECT 'POLICY_TRL_PRODUCTION_BONUS_DEFENSIVE_BUILDINGS',    Type,                20
    FROM BuildingClasses                --!=1 so that buildings like the Recycling Centre do get a bonus (as they're not wonders)
    WHERE (MaxGlobalInstances<0 AND MaxPlayerInstances!=1 AND MaxTeamInstances!=1)
          AND EXISTS
            (SELECT * FROM Buildings WHERE BuildingClass == BuildingClasses.Type AND
                (Cost>0 AND (Defense>0 OR ExtraCityHitPoints>0) AND
                    (
                        --The building is the default building of its buildingclass AND does NOT have an override for Whomp's Fortress (unless it overrides itself for w/e reason)
                        (Type==BuildingClasses.DefaultBuilding AND NOT EXISTS
                        (SELECT * FROM Civilization_BuildingClassOverrides WHERE CivilizationType=='CIVILIZATION_TRL_WHOMP_FORTRESS'
                            AND BuildingClassType == BuildingClasses.Type AND BuildingType!=Buildings.Type)
                        )
                        OR
                        --The building is NOT the default building of its buildingclass AND is the override for Whomp's Fortress
                        (Type!=BuildingClasses.DefaultBuilding AND EXISTS
                        (SELECT * FROM Civilization_BuildingClassOverrides WHERE CivilizationType=='CIVILIZATION_TRL_WHOMP_FORTRESS'
                            AND BuildingClassType == BuildingClasses.Type AND BuildingType==Buildings.Type)
                        )
                    )
                    --[..]snip[..]
                )
            );

I also included some Triggers in case any other mod adds/removes/updates defensive buildings
Spoiler :

Code:
--=================================================================================================
--Add some triggers

--Create a trigger in case someone adds more defensive buildings after our mod loads
CREATE TRIGGER TRL_DefensiveBuildingAdded
AFTER INSERT ON Buildings
WHEN (NEW.Defense>0 OR NEW.ExtraCityHitPoints>0)
BEGIN
    --delete every entry first
    DELETE FROM Policy_BuildingClassProductionModifiers WHERE PolicyType=='POLICY_TRL_PRODUCTION_BONUS_DEFENSIVE_BUILDINGS';

    --then re-add the entries (same code as the initial code above)
    INSERT INTO Policy_BuildingClassProductionModifiers
            (PolicyType,                                            BuildingClassType,    ProductionModifier)
    SELECT 'POLICY_TRL_PRODUCTION_BONUS_DEFENSIVE_BUILDINGS',    Type,                20
    FROM BuildingClasses                --!=1 so that buildings like the Recycling Centre do get a bonus (as they're not wonders)
    WHERE (MaxGlobalInstances<0 AND MaxPlayerInstances!=1 AND MaxTeamInstances!=1)
          AND EXISTS
            (SELECT * FROM Buildings WHERE BuildingClass == BuildingClasses.Type AND
                (Cost>0 AND (Defense>0 OR ExtraCityHitPoints>0) AND
                    (
                        --The building is the default building of its buildingclass AND does NOT have an override for Whomp's Fortress (unless it overrides itself for w/e reason)
                        (Type==BuildingClasses.DefaultBuilding AND NOT EXISTS
                        (SELECT * FROM Civilization_BuildingClassOverrides WHERE CivilizationType=='CIVILIZATION_TRL_WHOMP_FORTRESS'
                            AND BuildingClassType == BuildingClasses.Type AND BuildingType!=Buildings.Type)
                        )
                        OR
                        --The building is NOT the default building of its buildingclass AND is the override for Whomp's Fortress
                        (Type!=BuildingClasses.DefaultBuilding AND EXISTS
                        (SELECT * FROM Civilization_BuildingClassOverrides WHERE CivilizationType=='CIVILIZATION_TRL_WHOMP_FORTRESS'
                            AND BuildingClassType == BuildingClasses.Type AND BuildingType==Buildings.Type)
                        )
                    )
                --[..]snip[..]
                )
            );
END;

--Add a trigger in case someone updates the defensive stats of a building after our mod loads
CREATE TRIGGER TRL_DefensiveBuildingUpdated
AFTER UPDATE OF Defense, ExtraCityHitPoints ON Buildings
WHEN NEW.Defense != OLD.Defense OR NEW.ExtraCityHitPoints != OLD.ExtraCityHitPoints
BEGIN
    --delete every entry first
    DELETE FROM Policy_BuildingClassProductionModifiers WHERE PolicyType=='POLICY_TRL_PRODUCTION_BONUS_DEFENSIVE_BUILDINGS';

    --then re-add the entries (same code as the initial code above)
    INSERT INTO Policy_BuildingClassProductionModifiers
            (PolicyType,                                            BuildingClassType,    ProductionModifier)
    SELECT 'POLICY_TRL_PRODUCTION_BONUS_DEFENSIVE_BUILDINGS',    Type,                20
    FROM BuildingClasses                --!=1 so that buildings like the Recycling Centre do get a bonus (as they're not wonders)
    WHERE (MaxGlobalInstances<0 AND MaxPlayerInstances!=1 AND MaxTeamInstances!=1)
          AND EXISTS
            (SELECT * FROM Buildings WHERE BuildingClass == BuildingClasses.Type AND
                (Cost>0 AND (Defense>0 OR ExtraCityHitPoints>0) AND
                    (
                        --The building is the default building of its buildingclass AND does NOT have an override for Whomp's Fortress (unless it overrides itself for w/e reason)
                        (Type==BuildingClasses.DefaultBuilding AND NOT EXISTS
                        (SELECT * FROM Civilization_BuildingClassOverrides WHERE CivilizationType=='CIVILIZATION_TRL_WHOMP_FORTRESS'
                            AND BuildingClassType == BuildingClasses.Type AND BuildingType!=Buildings.Type)
                        )
                        OR
                        --The building is NOT the default building of its buildingclass AND is the override for Whomp's Fortress
                        (Type!=BuildingClasses.DefaultBuilding AND EXISTS
                        (SELECT * FROM Civilization_BuildingClassOverrides WHERE CivilizationType=='CIVILIZATION_TRL_WHOMP_FORTRESS'
                            AND BuildingClassType == BuildingClasses.Type AND BuildingType==Buildings.Type)
                        )
                    )
                   --[..]snip[..]
                )
            );
END;

--Create a trigger in case someone removes a defensive building after our mod loads
CREATE TRIGGER TRL_DefensiveBuildingDeleted
AFTER DELETE ON Buildings
WHEN (OLD.Defense>0 OR OLD.ExtraCityHitPoints>0)
BEGIN
    --delete every entry first
    DELETE FROM Policy_BuildingClassProductionModifiers WHERE PolicyType=='POLICY_TRL_PRODUCTION_BONUS_DEFENSIVE_BUILDINGS';

    --then re-add the entries (same code as the initial code above)
    INSERT INTO Policy_BuildingClassProductionModifiers
            (PolicyType,                                            BuildingClassType,    ProductionModifier)
    SELECT 'POLICY_TRL_PRODUCTION_BONUS_DEFENSIVE_BUILDINGS',    Type,                20
    FROM BuildingClasses                --!=1 so that buildings like the Recycling Centre do get a bonus (as they're not wonders)
    WHERE (MaxGlobalInstances<0 AND MaxPlayerInstances!=1 AND MaxTeamInstances!=1)
          AND EXISTS
            (SELECT * FROM Buildings WHERE BuildingClass == BuildingClasses.Type AND
                (Cost>0 AND (Defense>0 OR ExtraCityHitPoints>0) AND
                    (
                        --The building is the default building of its buildingclass AND does NOT have an override for Whomp's Fortress (unless it overrides itself for w/e reason)
                        (Type==BuildingClasses.DefaultBuilding AND NOT EXISTS
                        (SELECT * FROM Civilization_BuildingClassOverrides WHERE CivilizationType=='CIVILIZATION_TRL_WHOMP_FORTRESS'
                            AND BuildingClassType == BuildingClasses.Type AND BuildingType!=Buildings.Type)
                        )
                        OR
                        --The building is NOT the default building of its buildingclass AND is the override for Whomp's Fortress
                        (Type!=BuildingClasses.DefaultBuilding AND EXISTS
                        (SELECT * FROM Civilization_BuildingClassOverrides WHERE CivilizationType=='CIVILIZATION_TRL_WHOMP_FORTRESS'
                            AND BuildingClassType == BuildingClasses.Type AND BuildingType==Buildings.Type)
                        )
                    )
                   --[..]snip[..]
                )
            );
END;

--====================================================================================
--No Triggers neccesary for Civilization_BuildingClassOverrides as Whomp's Fortress won't get any anyways after this code loads.
--Also, BuildingclassOverrides for other civs won't trigger either, as they're not the default building of the Buildingclass


 
Back
Top Bottom