Using LUA to switch ideology. Help!!

NPcomplete

Chieftain
Joined
Sep 14, 2011
Messages
29
I'm trying to make a mod that lets you switch the ideology without necessarily having a negative public opinion, i.e. you can change it whenever you want.

I saw that there is some code controlling the ideology in ChooseIdeologyPopup.lua and SocialPolicyPopup.lua. The former is called when you choose your ideology for the first time, and the later controls the switch ideology button in the tenets screen. So I modified SocialPolicyPopup.lua: Instead of enabling the change ideology button only if the public opinion is negative (at line 732), I enabled it at all times ("if (true) then..."). I also disabled some code lines related to the preferred ideology (as it doesn't exist if your public opinion is Ok).

The problem is that, right after losing your ideology, a notification makes you assign the free tenets, at the same turn. But you don't have your new ideology yet, so there is no way to assign them, and moving to the next turn is impossible. (Btw, enabling the game option that lets you pick policies in subsequent turns doesn't help).

So I tried this: Right after making the lua code lose your ideology (in code line "Network.SendChangeIdeology()" of function OnChangeIdeologyConfirmYes), I added "UI.incTurnTimerSemaphore()".

In this case the free tenets notification also appears right after losing your ideology, but now you can ignore it and go on to the next turn. The next turn the game notices you don't have ideology, so the standard lua script at ChooseIdeologyPopup.lua makes you choose your ideology as usual, as if you never had one. After you do, you can click at the free tenets notification and you can assign your free tenets to your freely chosen new ideology. So far so good.

However, even after the free tenets notification disappears, the "next turn" button doesn't work. You are stuck forever at that turn. Every time you press it, the Fire Tuner debugger tells you "UI thinks that we can't end turn, but the notification system disagrees", which is sent from ActionInfoPanel.lua. It looks that my "UI.incTurnTimerSemaphore()" instruction made a mess with what has to be done at the turn, and it just delayed the impossibility to move to the next turn... one turn later!

Alternatively, I also tried to directly set the new ideology by calling "Network.SendIdeologyChoice(Game.GetActivePlayer(), g_iChoice)" (as it is done in ChooseIdeologyPopup.lua) instead of calling "Network.SendChangeIdeology()" (as it is done in SocialPolicyPopup.lua). Unfortunately, this cannot reset your ideology when you already have one. Only Network.SendChangeIdeology() can.

Any ideas? Can anybody help me? :( I'm attaching my modified version of ChooseIdeologyPopup.lua.
 

Attachments

You may have to limit the ability to switch ideologies to only when there are no free tenets available from switching to the new ideology

That is, instead of
Code:
if (true) then
try
Code:
if (Game.GetNumFreePolicies(player:GetPublicOpinionPreferredIdeology()) == 0) then

Edit: BTW you can't stop the revolution, it's hard-coded in the DLL
 
Probably that trick would work, but then there would not be any reason to *voluntarily" switch the ideology. The ultimate goal of the mod is making ideology changes attractive, so I also want to set SWITCH_POLICY_BRANCHES_ANARCHY_TURNS to 0 (or maybe 1), and SWITCH_POLICY_BRANCHES_TENETS_LOST to 0 (or maybe 1), both in GlobalDefines.xml.

I have in mind a mod where switching from some ideologies to others maybe good depending on the situation. For instance: moving from freedom/autocracy to order could be useful if you arrive late to the industrial revolution and you quickly need factories (e.g. Russia and China), moving from freedom to autocracy could be useful if you are receiving too much pressure from the working class (e.g. Germany, Italy, and Spain in the 30s), moving from order to freedom/capitalism could be useful when the industrial revolution is complete and you want to boost the consumerism (China since the 80s), etc. Moreover, you could also switch your ideology just to change your powerful allies (e.g. Cuba in the 50s), etc.
 
IIRC first adopter gets two free tenets.

The problem you are running into is a) adopt ideology A and get two free tenets, b) immediately switch to ideology B and get two more free tenets, plus the two you had from A (assuming you set lost tenets to 0) - these are granted as two (free) and then another two (transferred), not four in one go. The code that is giving the free tenets on adopting B appears to be causing the issue you're seeing - and it's deep in the DLL - so if you don't want to limit the switch until the number of free tenets is 0, you're going to have to mod the DLL
 
Actually, I'm testing it in a duel map, settler difficulty, starting in modern era, and the two free tenets for being the first ideology adopter do no seem to matter too much. In turn 1 I immediately get 7 free tenets (I don't know whether it is 5 free tenets by default + 2 free tenets for being the first adopting ideology X, or just 7 free tenets). Then I switch the ideology in turn 3. Technically there is no revolution, as the game fails to identify the preferred ideology (the public opinion is Ok), so the ideology value is left as undefined or something. In turn 4, the game sees that I don't have ideology, and I'm given the chance to choose any of the three ideologies again, exactly as in turn 1. The funny thing is that, immediately after choosing it, I'm always given 5 free tenets to assign (I'm still using the standard SWITCH_POLICY_BRANCHES_TENETS_LOST value, i.e. 2), no matter whether the ideology I choose is new (i.e. it was not taken by the other civilization) or not.

By the way, what does UI.incTurnTimerSemaphore() exactly do? By experiments I know it lets you delay something, but I'm not sure. Is there any way I could revert its effect, so that the "UI thinks that we can't end turn, but the notification system disagrees" message does not appear any more? Could I know the reason why this message is being shown, and control it?

And how can I write my own DLL for the game? Do you have a link to a tutorial? I can write programs in C++, but I have never written a DLL.

It's sad the ideology mechanics are so hard-coded, no modularity at all... :(
 
Also: Is there a way to send a "free tenets event" to the next turn? Can specific events be delayed a number of turns?

EDIT: If I set SWITCH_POLICY_BRANCHES_TENETS_LOST to some high value (e.g. 30) then everything works fine: since there are no free tenets to assign, the game goes on after voluntarily switching the ideology with a positive public opinion. But I want to implement some way to keep the tenets...

(The game crashes when you want to exit game. Maybe it has something to do with making the ideology be set to an undefined value for one turn. But it lets you play without any problem anyway.)
 
Maybe I could just let the free policy notification be removed by right-clicking it, in order to let the end turn button be enabled again. In this case, would this notification appear again in the next turn (so that the free tenets can eventually be selected when the new ideology has already been chosen)? If so, how can I let free policies notifications be removed?

EDIT: I'm afraid function UI.RemoveNotification does not remove notifications which are shown in yellow. It has no effect. :(
 
I found a solution! :) Since UI.incTurnTimerSemaphore() gave me the chance to move to the next turn, I wondered whether the opposite action, i.e. UI.decTurnTimerSemaphore(), could fix the thing if it is triggered right in the next turn... and it does! :) Now I can go on after switching the idoleology (regardless of the public opinion) over and over again, it always works.

The game works fine while you are playing, but it crashes when you exit. It has nothing to with with the incTurnTimerSemaphore() and decTurnTimerSemaphore() trick. Even if you don't use it, the game crashes when you exit after switching the ideology without a bad public opinion. Maybe leaving the preferred ideology as undefined for a while is the reason.

Anyway, the game looks to be pretty stable... until you exit.
 
Back
Top Bottom