Advertisement
Guest User

.lua

a guest
Mar 19th, 2021
12
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.45 KB | None | 0 0
  1. --[[
  2. -- Created by Keaton VanAuken, Nov 13 2017
  3. -- Copyright (c) Firaxis Games
  4. --]]
  5.  
  6. -- ===========================================================================
  7. -- Base File
  8. -- ===========================================================================
  9. include("CityStates");
  10.  
  11. -- ===========================================================================
  12. -- Variables
  13. -- ===========================================================================
  14.  
  15. local m_CityStateGovernors:table = {}
  16. local CITYSTATEBASE_DEFAULT_SIZEY :number = 48;
  17. local CQUI_InlineCityStateQuestFontSize = 10;
  18. local ICON_NAMES = {"%[ICON_TechBoosted%] ", "%[ICON_CivicBoosted%] ", "%[ICON_TradeRoute%] ", "%[ICON_Barbarian%] "};
  19. -- ===========================================================================
  20. -- CACHE BASE FUNCTIONS
  21. -- ===========================================================================
  22. BASE_GetData = GetData;
  23. BASE_AddCityStateRow = AddCityStateRow;
  24. BASE_AddInfluenceRow = AddInfluenceRow;
  25.  
  26. -- ===========================================================================
  27.  
  28. if Modding.IsModActive("1B28771A-C749-434B-9053-D1380C553DE9") or Modding.IsModActive("4873eb62-8ccc-4574-b784-dda455e74e68") then
  29. function GetData()
  30. BASE_GetData();
  31.  
  32. m_CityStateGovernors = {};
  33.  
  34. -- Check if any civilizations have assigned an ambassador to any city-state
  35. for i, pCityState in ipairs(PlayerManager.GetAliveMinors()) do
  36. local governorData:table = {
  37. Ambassadors = {},
  38. HasAnyAmbassador = false,
  39. };
  40.  
  41. for j, PlayerID in ipairs(PlayerManager.GetAliveMajorIDs()) do
  42.  
  43. local pPlayerGovernors:table = Players[PlayerID]:GetGovernors();
  44. for i,pCityStateCity in pCityState:GetCities():Members() do
  45.  
  46. local pAssignedGovernor:table = pPlayerGovernors ~= nil and pPlayerGovernors:GetAssignedGovernor(pCityStateCity) or nil;
  47. if pAssignedGovernor ~= nil then
  48. governorData.Ambassadors[PlayerID] = true;
  49. governorData.HasAnyAmbassador = true;
  50. end
  51. end
  52. end
  53.  
  54. m_CityStateGovernors[pCityState:GetID()] = governorData;
  55. end
  56. end
  57.  
  58. -- ===========================================================================
  59. function AddCityStateRow( kCityState:table )
  60. local kInst:table = BASE_AddCityStateRow( kCityState );
  61.  
  62. local ambassadorData:table = m_CityStateGovernors[kCityState.iPlayer];
  63. if ambassadorData.HasAnyAmbassador then
  64. kInst.AmbassadorButton:RegisterCallback( Mouse.eLClick, function() OpenSingleViewCityState( kCityState.iPlayer ); OnInfluencedByClick(); end );
  65.  
  66. local tooltip = Locale.Lookup("LOC_CITY_STATE_PANEL_HAS_AMBASSADOR_TOOLTIP");
  67. local localPlayerID:number = Game.GetLocalPlayer();
  68. local pLocalPlayerDiplomacy = Players[localPlayerID]:GetDiplomacy();
  69. for playerID, playerHasAmbassador in pairs(ambassadorData.Ambassadors) do
  70. local playerConfig = PlayerConfigurations[playerID];
  71. if (playerID == localPlayerID or pLocalPlayerDiplomacy:HasMet(playerID)) then
  72. tooltip = tooltip .. Locale.Lookup("LOC_CITY_STATE_PANEL_HAS_AMBASSADOR_TOOLTIP_ENTRY", Locale.Lookup(playerConfig:GetCivilizationDescription()), Locale.Lookup(playerConfig:GetPlayerName()));
  73. else
  74. tooltip = tooltip .. Locale.Lookup("LOC_CITY_STATE_PANEL_HAS_AMBASSADOR_TOOLTIP_ENTRY_UNMET", Locale.Lookup("LOC_LOYALTY_PANEL_UNMET_CIV"));
  75. end
  76. end
  77. kInst.AmbassadorButton:SetToolTipString(tooltip);
  78.  
  79. kInst.AmbassadorButton:SetHide(false);
  80. else
  81. kInst.AmbassadorButton:SetHide(true);
  82. end
  83. -- ==== BEGIN CQUI Modification ==== ----
  84. -- CQUI Inline City State Quests
  85. anyQuests = false;
  86. for _,kQuest in pairs( kCityState.Quests ) do
  87. anyQuests = true;
  88. questString = kQuest.Name;
  89. end
  90.  
  91. if (anyQuests) then
  92. kInst.QuestIcon:SetHide(true);
  93. -- Adjust the size of the container based on the font size of the Inline City State Quest
  94. kInst.CityStateBase:SetSizeY(CITYSTATEBASE_DEFAULT_SIZEY + CQUI_InlineCityStateQuestFontSize - 2);
  95. kInst.QuestRow:SetHide(false);
  96. kInst.CityStateQuest:SetFontSize(CQUI_InlineCityStateQuestFontSize);
  97. kInst.CityStateQuest:SetString(CQUI_RemoveQuestIconsFromString(questString));
  98. kInst.CityStateQuest:SetColor(kCityState.ColorSecondary);
  99. else
  100. kInst.CityStateBase:SetSizeY(CITYSTATEBASE_DEFAULT_SIZEY);
  101. kInst.QuestRow:SetHide(true);
  102. end
  103.  
  104. -- Determine the 2nd place (or first-place tie), produce text for Tooltip on the EnvoyCount label
  105. local envoyTable:table = {};
  106. -- Iterate through all players that have influenced this city state
  107. local localPlayerID = Game.GetLocalPlayer();
  108. for iOtherPlayer,influence in pairs(kCityState.Influence) do
  109. local pLocalPlayer :table = Players[localPlayerID];
  110. local civName :string = "LOCAL_CITY_STATES_UNKNOWN";
  111. local isLocalPlayer:boolean = false;
  112. if (pLocalPlayer ~= nil) then
  113. local pPlayerConfig :table = PlayerConfigurations[iOtherPlayer];
  114. if (localPlayerID == iOtherPlayer) then
  115. civName = Locale.Lookup("LOC_CITY_STATES_YOU") .. " (" .. Locale.Lookup(pPlayerConfig:GetPlayerName()) .. ")";
  116. isLocalPlayer = true;
  117. else
  118. if (pLocalPlayer:GetDiplomacy():HasMet(iOtherPlayer)) then
  119. civName = Locale.Lookup(pPlayerConfig:GetPlayerName());
  120. else
  121. civName = Locale.Lookup("LOCAL_CITY_STATES_UNKNOWN")
  122. end
  123. end
  124.  
  125. table.insert(envoyTable, {Name = civName, EnvoyCount = influence, IsLocalPlayer = isLocalPlayer});
  126. end
  127. end
  128.  
  129. if (#envoyTable > 0) then
  130. -- Sort the table by value descending, alphabetically where tied, favoring local player
  131. table.sort(envoyTable,
  132. function(a,b)
  133. if (a.EnvoyCount == b.EnvoyCount) then
  134. if (a.IsLocalPlayer) then
  135. return true;
  136. elseif (b.IsLocalPlayer) then
  137. return false;
  138. else
  139. return a.Name < b.Name;
  140. end
  141. else
  142. return a.EnvoyCount > b.EnvoyCount
  143. end
  144. end);
  145.  
  146. local envoysToolTip = Locale.Lookup("LOC_CITY_STATES_ENVOYS_SENT")..":";
  147. for i=1, #envoyTable do
  148. envoysToolTip = envoysToolTip .. "[NEWLINE] - " .. envoyTable.Name .. ": " .. envoyTable.EnvoyCount;
  149. end
  150.  
  151. kInst.EnvoyCount:SetToolTipString(envoysToolTip);
  152.  
  153. if (#envoyTable > 1 and kInst.SecondHighestName ~= nil) then
  154. -- Show 2nd place if there is one (recall Lua tables/arrays start at index 1)
  155. -- The check on kInst.SecondHighestName is for cases where another mod replaces the XML, but not the citystates lua file
  156. local secondPlaceIdx = 2;
  157.  
  158. -- is there a tie for first?
  159. if (envoyTable[1].EnvoyCount == envoyTable[2].EnvoyCount) then
  160. -- Already sorted above, so this is either local player or the leader appearing first alphabetically
  161. secondPlaceIdx = 1;
  162. end
  163.  
  164. local secondHighestIsPlayer = envoyTable[secondPlaceIdx].IsLocalPlayer;
  165. local secondHighestName = envoyTable[secondPlaceIdx].Name;
  166. local secondHighestEnvoys = envoyTable[secondPlaceIdx].EnvoyCount;
  167.  
  168. if (secondHighestIsPlayer) then
  169. secondHighestName = Locale.Lookup("LOC_CITY_STATES_YOU");
  170. end
  171.  
  172. -- Add changes to the actual UI object placeholders, which are created in the CityStates.xml file
  173. kInst.SecondHighestName:SetColor(secondHighestIsPlayer and kCityState.ColorSecondary or COLOR_ICON_BONUS_OFF);
  174. kInst.SecondHighestName:SetText(secondHighestName);
  175. kInst.SecondHighestEnvoys:SetColor(secondHighestIsPlayer and kCityState.ColorSecondary or COLOR_ICON_BONUS_OFF);
  176. kInst.SecondHighestEnvoys:SetText(secondHighestEnvoys);
  177. end
  178. end
  179.  
  180. -- ============ END CQUI Modification ===================[/code]
  181. return kInst;
  182. end
  183.  
  184. -- ===========================================================================
  185. function AddInfluenceRow(cityStateID:number, playerID:number, influence:number, largestInfluence:number)
  186. local kItem:table = BASE_AddInfluenceRow(cityStateID, playerID, influence, largestInfluence);
  187.  
  188. local playerConfig:table = PlayerConfigurations[playerID];
  189. local localPlayerID:number = Game.GetLocalPlayer();
  190. local playerHasAmbassador:boolean = m_CityStateGovernors[cityStateID] and m_CityStateGovernors[cityStateID].Ambassadors[playerID] or false;
  191. kItem.AmbassadorIcon:SetHide(not playerHasAmbassador);
  192. local pLocalPlayerDiplomacy = Players[localPlayerID]:GetDiplomacy();
  193. local playerName:string = Locale.Lookup("LOC_LOYALTY_PANEL_UNMET_CIV");
  194. if (playerID == localPlayerID or pLocalPlayerDiplomacy:HasMet(playerID)) then
  195. playerName = playerConfig:GetPlayerName();
  196. end
  197. kItem.AmbassadorIcon:SetToolTipString(Locale.Lookup("LOC_CITY_STATE_PANEL_CIV_AMBASSADOR_TOOLTIP", playerName));
  198.  
  199.  
  200. return kItem;
  201. end
  202. end
  203. function CQUI_RemoveQuestIconsFromString( inStr:string )
  204. local lookupStr :string = inStr;
  205. local outStr :string = lookupStr;
  206. for _,iconName in ipairs(ICON_NAMES) do
  207. if (string.find(lookupStr, iconName)) then
  208. outStr = string.gsub(lookupStr, iconName, "");
  209. end
  210. end
  211. return outStr;
  212. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement