Code:
-- 000---------------------
--******* Original scripts and concepts by : Prime from Steam
--******* Mod "Your People's Trust"
--******* February 2016
-- 000---------------------
--******* Current scripts and concepts by: Jake Armitage and ivanpera from TWC
--******* Mod "Aequilibrium Fidei" for DeI 1.2.4
--******* December 2018
-- 000---------------------
--******* For embedding it into other mods, authors' (Prime, Jake Armitage, ivanpera) permissions are required
-- 000---------------------
-- 000---------------------
-- ************** FIDES OECONOMIAE **************
-- 000---------------------
-- 000---------------------
module(..., package.seeall);
_G.main_env = getfenv(1);
-- ###------------------------- START *******
-- LIBRARIES REQUIRED ---------------------------------------------------------------------
-- ###-------------------------
local scripting = require "lua_scripts.EpisodicScripting"
local constants_lib = require "script._lib.lib_AeF_03_econ_constants"
-- ###---------------------------------- END*
-- ###------------------------- START *******
-- LOCAL EQUIVALENCES ---------------------------------------------------------------------
-- ###-------------------------
-- *** LOGBOOL
local logbool = false;
-- *** TRUST
local AeF_03_econ_trust = 500;
local old_trust = 0;
-- *** TRUST TOOLTIP
local trust_tooltip = WHITE;
-- *** TRUST COMPONENTS
local tt_TaxLevel = 0;
local tt_Treasury = 0;
local tt_Random = 0;
local tt_Threshold = 0;
local random_Gain_Ratio = 0.5;
-- ###---------------------------------- END*
-- ###------------------------- START *******
-- UTILITY FUNCTIONS ---------------------------------------------------------------------
-- ###-------------------------
function Log(callback_name, message)
if not logbool
then
return;
else
local t = os.date("%d-%m-%Y %X")
local log = io.open("Trust.txt","a")
log :write(t .. " : " .. tostring(callback_name) .. " ".. tostring(message) .. "\n")
log :flush()
log :close()
end
end
-- ###---------------------------------- END*
-- ###------------------------- START *******
-- TRUST REAL TIME REBUILD ---------------------------------------------------------------------
-- ###-------------------------
function AeF_03_econ_UpdateTrust_Components(delta, positive, source)
colour = GREEN1;
if positive == 0
then
delta = -1 * delta;
colour = RED1;
end
-- *** AFFECTS MAIN KPI
AeF_03_econ_trust = AeF_03_econ_trust + delta;
if source == "TaxLevel" -- *** TRUST TOOLTIP REAL TIME REBUILD FOR COMPONENTS
then
tt_TaxLevel = tt_TaxLevel + delta;
curr_value = tt_TaxLevel;
param_string = WHITE.."\n - Trust Components :\nTax Level : ";
elseif source == "Treasury"
then
tt_Treasury = tt_Treasury + delta;
curr_value = tt_Treasury;
param_string = trust_tooltip.."\nTreasury : ";
elseif source == "Random"
then
tt_Random = tt_Random + delta;
curr_value = tt_Random;
param_string = trust_tooltip.."\nRandom : ";
end
trust_tooltip = param_string..colour..tostring(curr_value)..WHITE;
end
function AeF_03_econ_UpdateTrust_Thresholds()
local length = #TRUST_LEVEL_THRESHOLDS;
for i = 1, length do
if AeF_03_econ_trust <= TRUST_LEVEL_THRESHOLDS[i] or i == length
then
AeF_03_econ_trust = AeF_03_econ_trust + TRUST_THRESHOLD_POINTS[i]
trust_tooltip = trust_tooltip..LINE1..WHITE..
"\n - Trust Variables :\nTrust Threshold : "..
COLOR1_TABLE[i]..tostring(TRUST_THRESHOLD_POINTS[i])..COLOR2_TABLE[i];
break;
end
end
end
-- ###---------------------------------- END*
-- ###------------------------- START *******
-- APPLY TRUST EFFECTS ---------------------------------------------------------------------
-- ###-------------------------
-- *** REMOVE EFFECT
function AeF_03_econ_ResetEffects(faction_name)
for i = 1, #SEGMENT_LOWER_BOUNDS
do
local effect = "AeF_03_econ_" .. tostring(i);
scripting.game_interface:remove_effect_bundle(effect, faction_name);
end
end
-- *** APPLY EFFECT
function AeF_03_econ_ApplyEffects(faction_name)
local k = 1;
for i = 1, #SEGMENT_LOWER_BOUNDS do
if AeF_03_econ_trust >= SEGMENT_LOWER_BOUNDS[i] and AeF_03_econ_trust <= SEGMENT_UPPER_BOUNDS[i]
then
local effect = "AeF_03_econ_" .. tostring(k);
scripting.game_interface:apply_effect_bundle(effect, faction_name, 2);
segment = k
return
end
k = k + 1;
end
end
-- ###---------------------------------- END*
-- ###------------------------- START *******
-- CALLBACK EVENT IMPLEMENTATIONS ---------------------------------------------------------------------
-- ###-------------------------
-- *** LOADING
local function AeF_03_econ_Initialize(context)
AeF_03_econ_trust = scripting.game_interface:load_named_value("AeF_03_econ_trust", 500, context);
old_trust = scripting.game_interface:load_named_value("old_trust", 500, context);
trust_tooltip = scripting.game_interface:load_named_value("trust_tooltip", WHITE..
"\nWelcome to Fides Oeconomiae."..
"Realtime trust data will be available"..
"as soon as events occur or game actions are taken.", context);
end
-- *** SAVING
local function AeF_03_econ_Save(context)
scripting.game_interface:save_named_value("AeF_03_econ_trust", AeF_03_econ_trust, context);
scripting.game_interface:save_named_value("old_trust", old_trust, context);
scripting.game_interface:save_named_value("trust_tooltip", trust_tooltip, context);
end
-- ###---------------------------------- END*
-- ###------------------------- START *******
-- APPLY TRUST START TURN ---------------------------------------------------------------------
-- ###-------------------------
local function AeF_03_econ_FactionStartTurn(context)
if context:faction():is_human()
then
local _faction_ = context:faction():name();
if scripting.game_interface:model():turn_number() == 1
then
AeF_03_econ_trust = 500;
old_trust = 500;
tooltip_reset = true;
math.randomseed(os.time());
end
-- *** APPLY EFFECTS
AeF_03_econ_ResetEffects(_faction_);
AeF_03_econ_ApplyEffects(_faction_);
end
end
-- ###---------------------------------- END*
-- ###------------------------- START *******
-- APPLY TRUST END TURN ---------------------------------------------------------------------
-- ###-------------------------
local function AeF_03_econ_FactionEndTurn(context)
if context:faction():is_human()
then
-- *** TOOLTIP PROCEDURES
if tooltip_reset == true
then
-- *** RESET TOOLTIP LAYOUT
trust_tooltip = "";
-- *** SAVE OLD TRUST
old_trust = AeF_03_econ_trust;
-- *** RESET TOOLTIP
tooltip_reset = false;
end
-- *** LOCAL EQUIVALENCES
local _faction_ = context:faction():name();
-- /// TRUST COMPONENT: TAX LEVEL
local tax_level = context:faction():tax_level();
length = #TAX_LEVEL_THRESHOLDS;
for i = 1,length do
level = TAX_LEVEL_THRESHOLDS[i]
if tax_level < level or i == length
then
AeF_03_econ_UpdateTrust_Components(TAX_EFFECTS[i],1,"TaxLevel");
break
end
end
-- /// TRUST COMPONENT: TREASURY
local treasury = context:faction():treasury();
if treasury >= TREASURY_LEVEL_THRESHOLD
then
AeF_03_econ_UpdateTrust_Components(TREASURY_1,0,"Treasury");
else
AeF_03_econ_UpdateTrust_Components(TREASURY_2,0,"Treasury");
end
gain = 0;
-- /// TRUST COMPONENT: RANDOM
if math.random() > random_Gain_Ratio
then
gain = 1;
end
AeF_03_econ_UpdateTrust_Components(math.random(RANDOM_TRUST_MIN,RANDOM_TRUST_MAX),gain,"Random");
-- *** TRUST THRESHOLD FOR MAIN KPI
AeF_03_econ_UpdateTrust_Thresholds();
-- *** APPLY EFFECTS
AeF_03_econ_ResetEffects(_faction_);
AeF_03_econ_ApplyEffects(_faction_);
-- *** RESET TOOLTIP
tooltip_reset = true;
end
end
-- ###---------------------------------- END*
-- ###------------------------- START *******
-- TOOLTIPS ---------------------------------------------------------------------
-- ###-------------------------
local function AeF_03_econ_DisplayTooltip(context)
-- *** BASE COLOR
local color1 = "";
local color2 = "";
local color3 = "";
local color4 = "";
-- *** CHANGE VANILLA UI TOOLTIP
if context.string == "tab_regions"
then
local tt_text = UIComponent(context.component):GetTooltipText();
local string1 = "Provinces";
local string2 = "Fides Oeconomicae";
-- *** CHANGE COLORS
if string.find(tt_text, string1) or string.find(tt_text, string2) or tt_text == ""
then
local length = #TRUST_LEVEL_THRESHOLDS;
done1 = false; done2 = false;
for i = 1, length do
if (AeF_03_econ_trust <= TRUST_LEVEL_THRESHOLDS[i] or i == length) and done1 == false
then
color1 = COLOR1_TABLE[i];
color2 = COLOR2_TABLE[i];
done1 = true;
end
if (old_trust <= TRUST_LEVEL_THRESHOLDS[i] or i == length) and done2 == false
then
color3 = COLOR1_TABLE[i];
color4 = COLOR2_TABLE[i];
done2 = true;
end
if done1 and done2
then
break;
end
end
-- *** SET TOOLTIP
local index = #TRUST_LEVEL_THRESHOLDS
local trust_max = TRUST_LEVEL_THRESHOLDS[index]
local ratio = math.ceil(old_trust/trust_max*100);
local component = UIComponent(context.component):Id();
UIComponent(context.component):SetTooltipText(WHITE..
"CURRENT\nFides Oeconomicae :\n"..
color1..AeF_03_econ_trust..WHITE.."/"..trust_max..
"\n\nPREVIOUS\nFides Oeconomicae :\n "..
color3..old_trust..WHITE.."/"..trust_max..
" "..LINE1..trust_tooltip.."");
end
end
end
-- ###---------------------------------- END*
-- ###------------------------- START *******
-- CALLBACKS ---------------------------------------------------------------------
-- ###-------------------------
scripting.AddEventCallBack("LoadingGame",AeF_03_econ_Initialize);
scripting.AddEventCallBack("SavingGame",AeF_03_econ_Save);
scripting.AddEventCallBack("FactionTurnStart", AeF_03_econ_FactionStartTurn);
scripting.AddEventCallBack("FactionTurnStart", AeF_03_econ_FactionEndTurn);
scripting.AddEventCallBack("ComponentMouseOn", AeF_03_econ_DisplayTooltip);
-- ###---------------------------------- END*