Skip to content

Commit

Permalink
Bug fixes
Browse files Browse the repository at this point in the history
1) Fixed issue with automated routes not loading correctly.
2) Optimization improvements with grouping
3) Fixed routes getting sorted in make trade route screen, when nothing
was changed.
  • Loading branch information
astog committed Jan 4, 2017
1 parent 07d65a5 commit 05044c8
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 118 deletions.
35 changes: 26 additions & 9 deletions UI/Choosers/TradeRouteChooser.lua
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,9 @@ local m_shiftDown:boolean = false;

-- Stores the sort settings.
local m_SortBySettings:table = {};
local m_SortSettingsChanged:boolean = false;
local m_FilterSettingsChanged:boolean = false;
local m_SortSettingsChanged:boolean = true;

local m_FilterSettingsChanged:boolean = true;

-- Default is ascending in turns to complete trade route
m_SortBySettings[1] = {
Expand Down Expand Up @@ -77,7 +78,6 @@ function Refresh()
m_LastTrader = selectedUnit:GetID()
-- Rebuild and re-sort
m_RebuildAvailableRoutes = true
m_SortSettingsChanged = true
else
m_RebuildAvailableRoutes = false
end
Expand Down Expand Up @@ -237,11 +237,16 @@ function RefreshChooserPanel()
end
end

-- Need to re-filter and re-sort
m_SortSettingsChanged = true
m_FilterSettingsChanged = true

-- Cache routes info.
CacheEmpty()
CacheRoutesInfo(m_AvailableTradeRoutes)

m_TurnBuiltRouteTable = Game.GetCurrentGameTurn()
m_RebuildAvailableRoutes = false -- done building routes
else
print("OPT: Not rebuilding routes")
end
Expand All @@ -267,16 +272,23 @@ function RefreshStack()
-- Filter Destinations by active Filter
if m_FilterSettingsChanged then
tradeRoutes = FilterTradeRoutes(m_AvailableTradeRoutes);
m_FilterSettingsChanged = false -- done filtering

-- Filter changed, need to re-sort
m_SortSettingsChanged = true
else
tradeRoutes = m_AvailableTradeRoutes;
end

-- Send Trade Route Paths to Engine (after filter applied)
UILens.ClearLayerHexes(LensLayers.TRADE_ROUTE);

-- If not sort settings, sort by destination city name
SortTradeRoutes(tradeRoutes, m_SortBySettings, m_SortSettingsChanged);
m_SortSettingsChanged = false
if m_SortSettingsChanged then
SortTradeRoutes(tradeRoutes, m_SortBySettings);
m_SortSettingsChanged = false -- done sorting
else
print("OPT: Not resorting.")
end

-- If a destination City is chosen, send path only for that
if m_destinationCity ~= nil and m_originCity ~= nil then
Expand All @@ -289,6 +301,7 @@ function RefreshStack()

-- for i, tradeRoute in ipairs(tradeRoutes) do
for i=1, #tradeRoutes do
-- If no destination city is selected, show all routes path on the map
if m_destinationCity == nil then
local pathPlots = tradeManager:GetTradeRoutePath(tradeRoutes[i].OriginCityPlayer, tradeRoutes[i].OriginCityID, tradeRoutes[i].DestinationCityPlayer, tradeRoutes[i].DestinationCityID);
local kVariations:table = {};
Expand Down Expand Up @@ -549,7 +562,6 @@ function OnFilterSelected( index:number, filterIndex:number )
Controls.FilterButton:SetText(m_filterList[m_filterSelected].FilterText);

m_FilterSettingsChanged = true
m_SortSettingsChanged = true;
Refresh();
end

Expand Down Expand Up @@ -891,6 +903,7 @@ end
-- ---------------------------------------------------------------------------
function OnGeneralNotSortBy(sortByID)
RemoveSortEntry(sortByID, m_SortBySettings);

m_SortSettingsChanged = true
Refresh();
end
Expand Down Expand Up @@ -1057,8 +1070,12 @@ function OnGameDebugReturn( context:string, contextTable:table )
return;
end

m_filterSelected = contextTable["filterIndex"];
m_destinationCity = contextTable["destinationCity"];
if contextTable["filterIndex"] ~= nil then
m_filterSelected = contextTable["filterIndex"];
end
if contextTable["destinationCity"] ~= nil then
m_destinationCity = contextTable["destinationCity"];
end

Refresh();
end
Expand Down
57 changes: 26 additions & 31 deletions UI/TradeOverview.lua
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ local SORT_ASCENDING = GetSortAscendingIdConstant();
local SORT_DESCENDING = GetSortDescendingIdConstant();

local SEMI_EXPAND_SETTINGS:table = {};
SEMI_EXPAND_SETTINGS[GROUP_BY_SETTINGS.NONE] = 10000; -- 100 * 100 possible cities. It can never be higher than this, right?
SEMI_EXPAND_SETTINGS[GROUP_BY_SETTINGS.ORIGIN] = 4;
SEMI_EXPAND_SETTINGS[GROUP_BY_SETTINGS.DESTINATION] = 2;

Expand Down Expand Up @@ -96,32 +95,16 @@ local m_GroupCollapseAll:boolean = false;
local m_GroupsFullyExpanded:table = {};
local m_GroupsFullyCollapsed:table = {};

-- Variables used for cycle trade units function
local m_TradeUnitIndex:number = 0;
local m_CurrentCyclingUnitsTradeRoute:number = -1;
local m_DisplayedTradeRoutes:number = 0;

local m_HasBuiltTradeRouteTable:boolean = false;
local m_LastTurnBuiltTradeRouteTable:number = -1;
local m_SortSettingsChanged:boolean = true;
local m_GroupSettingsChanged:boolean = true;
local m_FilterSettingsChanged:boolean = true;

-- Stores the sort settings.
local m_SortBySettings = {};
local m_GroupSortBySettings = {};

-- Default is ascending in turns to complete trade route
m_SortBySettings[1] = {
SortByID = SORT_BY_ID.TURNS_TO_COMPLETE;
SortOrder = SORT_ASCENDING;
};

-- Default is ascending in turns to complete trade route
m_GroupSortBySettings[1] = {
SortByID = SORT_BY_ID.GOLD;
SortOrder = SORT_DESCENDING;
};

local preRefreshClock = 0;

-- ===========================================================================
Expand Down Expand Up @@ -249,7 +232,6 @@ end

-- Show My Routes Tab
function ViewMyRoutes()
m_DisplayedTradeRoutes = 0;

-- Update Tabs
SetMyRoutesTabSelected(true);
Expand Down Expand Up @@ -354,7 +336,6 @@ end

-- Show Routes To My Cities Tab
function ViewRoutesToCities()
m_DisplayedTradeRoutes = 0;

-- Update Tabs
SetMyRoutesTabSelected(false);
Expand Down Expand Up @@ -409,7 +390,6 @@ end

-- Show Available Routes Tab
function ViewAvailableRoutes()
m_DisplayedTradeRoutes = 0;

-- Update Tabs
SetMyRoutesTabSelected(false);
Expand All @@ -427,7 +407,6 @@ function ViewAvailableRoutes()

-- Dont rebuild if the turn has not advanced
if (not m_HasBuiltTradeRouteTable) or Game.GetCurrentGameTurn() > m_LastTurnBuiltTradeRouteTable then
print("Trade Route table last built on: " .. m_LastTurnBuiltTradeRouteTable .. ". Current game turn: " .. Game.GetCurrentGameTurn());
RebuildAvailableTradeRoutesTable();
print("Time taken to build routes: " .. (os.clock()- preRefreshClock) .. " sec(s)");

Expand All @@ -442,6 +421,7 @@ function ViewAvailableRoutes()
m_FilterSettingsChanged = true;
m_GroupSettingsChanged = true;
else
print("Trade Route table last built on: " .. m_LastTurnBuiltTradeRouteTable .. ". Current game turn: " .. Game.GetCurrentGameTurn());
print("OPT: Not Rebuilding or recaching routes table")
end

Expand All @@ -460,6 +440,7 @@ function ViewAvailableRoutes()
if m_groupByList[m_groupBySelected].groupByID ~= GROUP_BY_SETTINGS.NONE then
-- Group routes. Use the filtered list of routes
if m_GroupSettingsChanged then
-- Group from the filtered routes
m_AvailableGroupedRoutes = GroupRoutes(m_FinalTradeRoutes, m_groupByList[m_groupBySelected].groupByID)
print("Time taken till group: " .. (os.clock() - preRefreshClock) .. " sec(s)")

Expand All @@ -473,15 +454,15 @@ function ViewAvailableRoutes()
if m_SortSettingsChanged then
-- Sort within each group
for i=1, #m_AvailableGroupedRoutes do
SortTradeRoutes(m_AvailableGroupedRoutes[i], m_SortBySettings, m_SortSettingsChanged)
SortTradeRoutes(m_AvailableGroupedRoutes[i], m_SortBySettings)
end
print("Time taken till within group sort: " .. (os.clock() - preRefreshClock) .. " sec(s)")

-- Sort the order of groups. You need to do this AFTER each group has been sorted
SortGroupedRoutes(m_AvailableGroupedRoutes, m_GroupSortBySettings, m_SortSettingsChanged);
SortGroupedRoutes(m_AvailableGroupedRoutes, m_GroupSortBySettings);
print("Time taken till group sort: " .. (os.clock()- preRefreshClock) .. " sec(s)");
else
print("OPT: Not refiltering and resorting within groups")
print("OPT: Not resorting within and of groups")
end

-- Show the groups
Expand All @@ -500,14 +481,17 @@ function ViewAvailableRoutes()
end
else
if m_FinalTradeRoutes ~= nil then
SortTradeRoutes(m_FinalTradeRoutes, m_GroupSortBySettings, (m_SortSettingsChanged or m_GroupSettingsChanged));
print("Time taken till sort: " .. (os.clock() - preRefreshClock) .. " sec(s)")

AddRouteInstancesFromTable(m_FinalTradeRoutes, SEMI_EXPAND_SETTINGS[GROUP_BY_SETTINGS.NONE]);
if m_SortSettingsChanged or m_GroupSettingsChanged then
SortTradeRoutes(m_FinalTradeRoutes, m_GroupSortBySettings);
print("Time taken till sort: " .. (os.clock() - preRefreshClock) .. " sec(s)")
else
print("OPT: Not resorting routes")
end
AddRouteInstancesFromTable(m_FinalTradeRoutes);
end
end

-- Routes are sorted if it reaches here
-- Everything is done if it reaches here
m_SortSettingsChanged = false;
m_FilterSettingsChanged = false;
m_GroupSettingsChanged = false;
Expand Down Expand Up @@ -808,7 +792,7 @@ function AddRouteInstanceFromRouteInfo( routeInfo:table )
-- Add button hookups for only this tab
elseif m_currentTab == TRADE_TABS.AVAILABLE_ROUTES then
-- Check if we have free traders
if m_AvailableTraders[routeInfo.OriginCityID] ~= nil and tableLength(m_AvailableTraders[routeInfo.OriginCityID]) > 0 then
if m_AvailableTraders[routeInfo.OriginCityID] ~= nil and table.count(m_AvailableTraders[routeInfo.OriginCityID]) > 0 then
-- Get first trader
local traderID = m_AvailableTraders[routeInfo.OriginCityID][1]
local tradeUnit:table = originPlayer:GetUnits():FindID(traderID);
Expand Down Expand Up @@ -1599,7 +1583,18 @@ function Close()
if not ContextPtr:IsHidden() then
UI.PlaySound("CityStates_Panel_Close");
end

m_AnimSupport.Hide();

-- Reset sort settings
m_SortBySettings = {};
m_GroupSortBySettings = {};

-- Reset tab
m_currentTab = TRADE_TABS.MY_ROUTES;

-- Reset filter
m_filterSelected = 1;
end

-- ===========================================================================
Expand Down
Loading

0 comments on commit 05044c8

Please sign in to comment.