1. We have added the ability to collapse/expand forum categories and widgets on forum home.
    Dismiss Notice
  2. Photobucket has changed its policy concerning hotlinking images and now requires an account with a $399.00 annual fee to allow hotlink. More information is available at: this link.
    Dismiss Notice
  3. All Civ avatars are brought back and available for selection in the Avatar Gallery! There are 945 avatars total.
    Dismiss Notice
  4. To make the site more secure, we have installed SSL certificates and enabled HTTPS for both the main site and forums.
    Dismiss Notice
  5. Civ6 is released! Order now! (Amazon US | Amazon UK | Amazon CA | Amazon DE | Amazon FR)
    Dismiss Notice
  6. Dismiss Notice
  7. Forum account upgrades are available for ad-free browsing.
    Dismiss Notice

how to add free happiness to a player by lua event

Discussion in 'Civ5 - Creation & Customization' started by Seraphim8400, Jul 3, 2011.

  1. Seraphim8400

    Seraphim8400 Chieftain

    Joined:
    Nov 7, 2010
    Messages:
    112
    i test
    Player:SetHappiness( inewhappiness );
    it will effect until a new turn
    when the new turn start , the extrahappiness wound be lost
     
  2. Spatzimaus

    Spatzimaus Mad Scientist

    Joined:
    Sep 21, 2005
    Messages:
    3,063
    Location:
    Los Angeles, CA
    It's not even that it resets the next turn, it's actually resetting at the END of your turn, when the game does one final recalculation for all of the yields and such. So SetHappiness actually does nothing; while the UI will show a higher value for this turn, the value is reset BEFORE Happiness is used to increment Golden Ages or check for the effects of negative Happiness.

    Also, even if it DID work, you'd have an AI problem: the AI would simply not understand that the Happiness was going to be changed. So the behavior that causes the AI to try to stay about 0 happiness would be done without considering the extra amount you're adding. If it was at -5 and you gave it +10, the AI would still behave like it was at -5 instead of at +5, and try to get itself up to 0 (which'd mean +10).


    What I've had to do in my own mod is the following:
    1> Create a custom building. Let's call it BUILDING_PLUS_1.
    2> Set its cost to -1, so that no one can build it.
    3> Set its UnmoddedHappiness to 1. (It's important to use the unmodded for this, so that it doesn't cap by population.)
    4> Set its NeverCapture to "true" and NukeImmune to "true".
    (Note: you WILL need an icon for this building. I suggest the Future Tech icon, which is TECH_ATLAS_2, #10.)
    5> Create a custom building class. Let's call it BUILDINGCLASS_PLUS_1.
    6> Set the NoLimit flag on the building class to "true", so that you can put multiple copies in a city.
    7> Make all the appropriate text keys.

    Now, all you need to do is create an end-of-turn event (using Events.ActivePlayerTurnEnd or the new GameEvents alternative) that adjusts the number of copies of that building within a given city using

    pCity:SetNumRealBuilding(GameInfoTypes.BUILDING_PLUS_1,happyCount);

    where pCity is the city structure and happyCount is the number you want to have in that city at the start of the next turn. (Note that this is a Set, not a Change, so if you want to add one more to what was already there you'll need to find out how many the city already had.)

    In my mod, I use this functionality for the following:
    > If you start the game in a later era, you'll have a few of these in your capital for the first few turns while your empire gets up to speed.
    > I have an Empath specialist that adds +1 Happiness and +2 Food. While the Happiness value won't go up and down as you slot or unslot specialists, it WILL update at the end of the turn correctly. (The Monolith Improvement the Great Empath creates also adds a permanent +3 Happiness as well, through a different mechanism.)
    > Every time you research the repeatable tech at the end of the tech tree, you get a permanent +1 Happiness.

    It works great, and most importantly the AI understands it. Since there'll be an actual Happiness building in the city, the AI won't think it's running negative when it's not, and you can use Flavor ratings to make the AI associate the Specialists, etc. with Happiness.

    The only real headache, UI-wise, is that the custom building used will show up in the cities' lists, but you'll have no way to see how many copies of that building are in the city.
     
  3. Seraphim8400

    Seraphim8400 Chieftain

    Joined:
    Nov 7, 2010
    Messages:
    112
    err... it seems busy
    it need to many trigger to protect it work....
     
  4. Seraphim8400

    Seraphim8400 Chieftain

    Joined:
    Nov 7, 2010
    Messages:
    112
    Or can we update db by lua ?
     
  5. Spatzimaus

    Spatzimaus Mad Scientist

    Joined:
    Sep 21, 2005
    Messages:
    3,063
    Location:
    Los Angeles, CA
    No, only one trigger is needed. And it works perfectly well, mechanically. The main complaints against this method involve how well it meshes with the UI.

    Not well. And even if you could, a database update would apply to all players equally, which isn't generally what you want.

    The method I've described really isn't that complicated. It's not as simple as a single-line Lua change like you were hoping for, but until the devs give us a working Lua function for Happiness it's an acceptable substitute. I've been using it in my own mod for several months now, without any real problems.
     
  6. Seraphim8400

    Seraphim8400 Chieftain

    Joined:
    Nov 7, 2010
    Messages:
    112
    it need a real time trigger to log the total free happiness from diffevent files....
    need a real time trigger to maintain the city-system to prevention of cities status change(buy,sell,lost,burn,etc.)....
    ............

    errr.....
    it can update the 3th UB which replace the BUILDING_PALACE
    as same as events of civ4 do.

    I do know that this method is feasible because I also use
    But I think it's consumption of resources so I think the feature is not worth
    I would like to seek a better one
     
  7. The Geez

    The Geez Chieftain

    Joined:
    Oct 15, 2011
    Messages:
    6
    Cool idea about adding a happiness building. I just posted a new mod that uses that idea, addresses ICS, and facilitates expansion (and a bit more emphasis on a war game rather than city building alone).
     
  8. The Geez

    The Geez Chieftain

    Joined:
    Oct 15, 2011
    Messages:
    6
    Oh -- the mod name is Expand Easier (posted online on Oct 15).
     
  9. killmeplease

    killmeplease Mk Z on Steam

    Joined:
    Nov 22, 2007
    Messages:
    2,690
    Location:
    Samara
    Spatzimaus,
    is it possible to SetNumRealBuilding for a building class that has NoLimit=false?
    what i want is to create buildings with happiness per population effect (broadcast tower, cinema, tv company)...
     
  10. OverWind

    OverWind Chieftain

    Joined:
    Oct 4, 2011
    Messages:
    46
    Location:
    Denmark
    Interesting idea, using a building. What about a fake luxury instead (to avoid it showing in the city list)?

    Or maybe you can realtime modify the civs UA to include happiness?

    (I'm new to modding, so excuse me if I sound stupid) :)
     
  11. Spatzimaus

    Spatzimaus Mad Scientist

    Joined:
    Sep 21, 2005
    Messages:
    3,063
    Location:
    Los Angeles, CA
    Sure. My mythology mod does exactly that; the NoLimit flag ONLY determines whether you can safely have more than one copy in a city.

    There are a lot of things you can do with SetNumRealBuilding. In my mythology mod, for instance, I have 21 series of 4 buildings, where the Shrine of Water can "upgrade" to a Chuch of Water, which in turn upgrades to a Cathedral of Water, and so on. (The upgrade trigger is handled through a behind-the-scenes logic; you can't build any of these buildings manually.) I use SetNumRealBuilding at each of these upgrade steps, setting the number of instances to 0 for the old version and 1 for the new version.

    Also, I've added four more NoLimit buildings: one adds +1 Food, one adds +1 Production, one adds +1 Gold, and one adds +1 Research, and copies of each are allocated to cities through the SetNumRealBuilding function. This allows me to dynamically change a city's yields in a way that the AI can comprehend, while still allowing for some fun effects. For instance, a god of Seasons will give each city extra Food during Summer and Fall, but not Spring or Winter, and the other three yields rotate similarly (Production is Spring and Summer, and so on.) On the other hand, a god of Balance will add a bonus to whichever yield is lowest in a city, and less to the yields that city produces more of. (A Cathedral of Balance adds +3 to your lowest yield, +2 to the next lowest, and +1 to the second-highest.) Since the yield is handled through an actual building in the city, the AI's thresholds aren't thrown off and it'll behave fairly normally.

    Since Happiness isn't a yield, that per-population thing can only be done through Lua, but yes, it'll work just fine with the method I described. The only difficulty is what I described earlier: it's a Set, not a Change, so you need to make sure you aren't counting something twice or overwriting a previous increment. The more things you add to the list of happiness-increasing stuff, the harder it becomes to manage.
     
  12. killmeplease

    killmeplease Mk Z on Steam

    Joined:
    Nov 22, 2007
    Messages:
    2,690
    Location:
    Samara
    thank you Spatzimaus

    i think there should be a way to hide a fake building from the list
    i'll see what can be done
     
  13. General Tso

    General Tso Panzer General

    Joined:
    Oct 12, 2007
    Messages:
    1,518
    Location:
    U. S. of A.
    Thank you for the excellent suggestion Spatzimaus. That worked perfectly for my luxury mod. I made some simple changes so that my happiness building doesn't show up in the city and civilopedia screens. If you're still interested in doing the same thing let me know and I'll post the code.
     
  14. killmeplease

    killmeplease Mk Z on Steam

    Joined:
    Nov 22, 2007
    Messages:
    2,690
    Location:
    Samara
    i'm interested :)
     
  15. General Tso

    General Tso Panzer General

    Joined:
    Oct 12, 2007
    Messages:
    1,518
    Location:
    U. S. of A.
    You need to add two .lua files to your mod then modify them. They are UI\InGame\CityView\CityView.lua and UI\Civilopedia\CivilopediaScreen.lua. If you have the expansion they are located in assets\DLC\Expansion otherwise they are located in assets\UI. Add them to ModBuddy using the standard procedure - "Add Existing Item" then enable "Import into VFS".

    For CityView.lua: Find the AddBuildingButton function. In my file it starts at line 296. You want to add this code at the beginning of that function.

    Code:
    function AddBuildingButton( pCity, building ) -- This line already exists
    
        -- Add this code and use the building type that you created in your mod.
        if building.Type == "BUILDING_PLUS_1" then
            return;
        end
    	
        -- Existing code starts here.

    For CivilopediaScreen.lua: Find the AddArticle function located inside the CivilopediaCategory[CategoryBuildings].PopulateList function. In my file it starts at line 689. You want to add this code at the beginning of that function. Warning - there is more than one AddArticle function in this file so make sure you find the right one.

    Code:
    CivilopediaCategory[CategoryBuildings].PopulateList = function()
        -- add the instances of the building entries
    	
        sortedList[CategoryBuildings] = {};
    	
        function AddArticle(categoryID, entryID, building)  -- This line and the lines above already exist.
    
            -- Add this code and use the building type that you created in your mod.
            if building.Type == "BUILDING_PLUS_1" then
                return;
            end
    	
            -- Existing code starts here.

    There is one more step required for the CivilopediaScreen.lua file. Further down in the CivilopediaCategory[CategoryBuildings].PopulateList function there are 3 DB Query's. They use different

    formats but the strings themselves start like this (some with and some without the quotes) - "Buildings.ID, Buildings.Description, Buildings.PortraitIndex, Buildings.IconAtlas" change them so that

    they look like this - "Buildings.Type, Buildings.ID, Buildings.Description, Buildings.PortraitIndex, Buildings.IconAtlas". Basically you're just adding the Buildings.Type value.
     
  16. killmeplease

    killmeplease Mk Z on Steam

    Joined:
    Nov 22, 2007
    Messages:
    2,690
    Location:
    Samara

Share This Page