Reducing happiness: is there a way?

Harald B

Warlord
Joined
Oct 20, 2010
Messages
135
I've been trying to create a mod which reduces a player's happiness under certain circumstances. (Specifically, based on the amount of fallout in their territory. But that's besides the point here.) I've managed to make the code to calculate by how much I want to reduce it and do the reduction, but there doesn't seem to be a right time to run it. Even when I use both Events.ActivePlayerTurnStart and Events.ActivePlayerTurnEnd, I can tell from the golden age progress that the happiness is recalculated separately before being applied.
I've read another thread suggesting using hidden buildings for this, but that only works when you want to increase the happiness. A quick test shows that even a building with -100 unmodded happiness still won't affect your empire.
So, is there some special event I could use or would I be forced to replace one of the game's own lua files? And if I would, which file would I be looking at?
 
First, are you trying to change happiness only for the human player or every civilisation (including AI)? Events.ActivePlayerTurnStart and Events.ActivePlayerTurnEnd are only triggered for human players, you need GameEvents.PlayerDoTurn for the AI.
Then, what happens exactly? Do you see your modded happiness in the top bar but it's not affecting the game, or does your script have no visible effect at all?
It's possible that Events.ActivePlayerTurnStart fires after happiness effects are applied, or before happiness is calculated. Also, happiness will be recalculated each turn, so using Events.ActivePlayerTurnEnd won't help.

You need an event that runs after happiness is calculated, but before it's effects are applied. I'm not sure that events does exist. Maybe if some C++ expert shows here, he will be able to tell from the source code what event to use (if any).

EDIT : A quick look from at the C++ code shows that a DoUpdateHappiness() function is called several times in CvPlayer.cpp, while an event like PlayerDoTurn is called only once. Unfortunately, the dissociated structure of C++ code makes it quite hard for me to know what happens first (i have a hard time not thinking in "follow the lines" scripted terms). Happiness is recalculated from sratch in DoUpdateHappiness().
This does not help you a lot, but if you are more comfortable with C++ than myself, it might actually be easier to create a new GameEvent that would trigger when happiness is recalculated, and would allow mods via Lua than try to change it from an existing event, as it seems like happiness is rather "volatile" in Civ5.
 
Ideally I want to adjust the happiness for everyone. I'll try GameEvents.PlayerDoTurn later.
I can see the modified happiness in the top bar and also in city growth rate bars when it gets below zero (when I click such a city, happiness is also recalculated; annoyingly though it seems it is NOT generally recalculated every time you enter any city, so running the function every time there is bad).
 
Well, it makes sense that happiness is recalculated several times during a player's turn. It must be in case you gain/loose access to a lux during your turn, or you rush-buy a happiness building, you become friend with a mercantile CS ...
That's why changing it only once at player start will probably only affect some happiness effects, and not all. :(
Also, there is no even that runs at the end of every player's turn (including AI) so it can be hard to keep the very unhappy combat penality.
The more i think of it, the more it seems like you need a new gameEvent. Of course, dll modding can make your mod incompatible with other mods. This can be an issue, or not, depending how big you plan your mod to be.
 
Tiny, so changing the dll would be rather overkill. (Plus I'm only barely getting used to lua.) I suppose I'll shelve the idea for now; maybe a useful event will get added in some patch or community dll.
 
Back
Top Bottom