Ja Mes
Chieftain
- Joined
- Dec 5, 2020
- Messages
- 10
I've been working on a tool to keep track of hex ownership and adjacency (borders).
Suffice to say, I am in need of dire assistance.
Here is the general process in theory
1. At the beginning of the game, create an array of the entire map. The keys are all of the plots (as their indices) with 7 (-1 through 5) values representing the ownership of the plot and its 6 bordering plots
2. When an plot changes ownership, it is put into an intermediary table that stores an increasing integer and the plot index
3. At the end of every turn, we go through the intermediary table and update the array, then empty the intermediary table
4. At the end of the player's turn, we go through the table of plots, check if they're owned, check if the owner is the England civilization, and then print the owners of the bordering plots
(None of this is optimized so there are some redundancies)
Yet the problem is, any hexes changed after the first turn still trigger the SerialEventHexCultureChanged (as evidenced by the print statements in my testing), yet for some reason the game prints these plots and their bordering plots as being unowned, despite the printed hex information showing otherwise. Additionally, on some occasions the Index will not be the correct one - it will output the wrong XY coordinates.
I believe the issue originates with some funkiness in the Lua tables. There isn't anything else in the mod so it's somewhere in the 100 lines.
Any help would be greatly appreciated, and all the best.
Suffice to say, I am in need of dire assistance.
Code:
local tPlotBorders = {}
local tIndices = {}
local NumOfIndices = 0
local iNumDirections = DirectionTypes.NUM_DIRECTION_TYPES - 1
function PlotsDoBeLikeThat()
for i = 0, Map.GetNumPlots() - 1, 1 do
local pPlot = Map.GetPlotByIndex(i)
tPlotBorders[i] = {}
tPlotBorders[i][-1] = pPlot:GetOwner()
for iDir = 0, iNumDirections, 1 do
local pAdjPlot = Map.PlotDirection(pPlot:GetX(), pPlot:GetY(), iDir)
if pAdjPlot then
tPlotBorders[i][iDir] = pAdjPlot:GetOwner()
else
tPlotBorders[i][iDir] = -1
end
end
end
end
Events.SequenceGameInitComplete.Add(PlotsDoBeLikeThat)
function OnTileNotification(hexX, hexY, playerID, isUnknown)
local index = Map.GetPlot(hexX, hexY):GetPlotIndex()
NumOfIndices = NumOfIndices + 1
tIndices[NumOfIndices] = index
print ("NumOfIndices " .. NumOfIndices .. " Hex: " .. hexX .. " " .. hexY .. " Index: " .. index)
end
Events.SerialEventHexCultureChanged.Add(OnTileNotification)
function ProcessIndices(iPlayer)
if iPlayer == 0 then
for k, v in pairs(tIndices) do
print("K: " .. k .. " V: " .. v)
print("X: " .. Map.GetPlotByIndex(v):GetX() .. " Y: " .. Map.GetPlotByIndex(v):GetY())
if Map.GetPlotByIndex(v):GetOwner() ~= -1 then
tPlotBorders[v] = {}
tPlotBorders[v][-1] = Map.GetPlotByIndex(v):GetOwner()
print("Processing Owned Indices" .. tPlotBorders[v][-1])
for iDir = 0, iNumDirections, 1 do
local pAdjPlot2 = Map.PlotDirection(Map.GetPlotByIndex(v):GetX(), Map.GetPlotByIndex(v):GetY(), iDir)
if pAdjPlot2 then
tPlotBorders[v][iDir] = pAdjPlot2:GetOwner()
else
tPlotBorders[v][iDir] = -1
end
end
end
tIndices[k] = nil
print("Emptied Table")
end
end
end
GameEvents.PlayerDoTurn.Add(ProcessIndices)
function CountBorderTiles(iPlayer)
local player = Players[iPlayer]
local PlayerIsHuman = (player:IsHuman() and player:IsTurnActive())
if PlayerIsHuman then
for k, v in pairs(tPlotBorders) do
if Map.GetPlotByIndex(k):GetOwner() ~= -1 then
if Players[Map.GetPlotByIndex(k):GetOwner()]:GetCivilizationType() == GameInfoTypes["CIVILIZATION_ENGLAND"] then
print("England")
for i = -1, 5, 1 do
print(v[i])
if v[i] ~= -1 and v[i] ~= Map.GetPlotByIndex(k):GetOwner() then
print("You Border Someone!")
end
end
end
end
end
end
end
GameEvents.PlayerDoTurn.Add(CountBorderTiles)
Here is the general process in theory
1. At the beginning of the game, create an array of the entire map. The keys are all of the plots (as their indices) with 7 (-1 through 5) values representing the ownership of the plot and its 6 bordering plots
2. When an plot changes ownership, it is put into an intermediary table that stores an increasing integer and the plot index
3. At the end of every turn, we go through the intermediary table and update the array, then empty the intermediary table
4. At the end of the player's turn, we go through the table of plots, check if they're owned, check if the owner is the England civilization, and then print the owners of the bordering plots
(None of this is optimized so there are some redundancies)
Yet the problem is, any hexes changed after the first turn still trigger the SerialEventHexCultureChanged (as evidenced by the print statements in my testing), yet for some reason the game prints these plots and their bordering plots as being unowned, despite the printed hex information showing otherwise. Additionally, on some occasions the Index will not be the correct one - it will output the wrong XY coordinates.
I believe the issue originates with some funkiness in the Lua tables. There isn't anything else in the mod so it's somewhere in the 100 lines.
Any help would be greatly appreciated, and all the best.