Last active
June 11, 2022 03:35
-
-
Save Drumsin/9a9839023d1b79c745445b78b4db4323 to your computer and use it in GitHub Desktop.
AoE4 Generated Map Function: Closest or Furthest edge functions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--tells you what is the closest and furthest edge from a given coordinate | |
--copy and paste code to preview at https://aoe4.app/ | |
--returns the closest edge of a given coordinate | |
--rowCoordinate (int) grid coordinate | |
--colCoordinate (int) grid coordinate | |
--edgesCompare (array) the edges to compare e.g. { 'left', 'right' } what is closest edge, left or right | |
--returns (string) 'top' 'right' 'bottom' 'left' | |
function closestEdge(rowCoordinate, colCoordinate, edgesCompare) | |
if (edgesCompare == nil) then | |
edgesCompare = { 'top', 'right', 'bottom', 'left' } | |
end | |
edges = {} | |
edges.top = rowCoordinate | |
edges.right = gridSize - colCoordinate + 1 | |
edges.bottom = gridSize - rowCoordinate + 1 | |
edges.left = colCoordinate | |
finalEdges = {} | |
for edgeIndex = 1, #edgesCompare do | |
edgeName = edgesCompare[edgeIndex] | |
edgeVal = edges[edgeName] | |
finalEdges[edgeName] = edgeVal | |
end | |
local edgeReturn | |
for i, v in pairs(finalEdges) do | |
edgeReturn = edgeReturn or i | |
if (v < finalEdges[edgeReturn]) then | |
edgeReturn = i | |
end | |
end | |
return edgeReturn | |
end | |
--returns the furthest edge of a given coordinate | |
--rowCoordinate (int) grid coordinate | |
--colCoordinate (int) grid coordinate | |
--edgesCompare (array) the edges to compare e.g. { 'left', 'right' } what is furthest edge, left or right | |
--returns (string) 'top' 'right' 'bottom' 'left' | |
function furthestEdge(rowCoordinate, colCoordinate, edgesCompare) | |
if (edgesCompare == nil) then | |
edgesCompare = { 'top', 'right', 'bottom', 'left' } | |
end | |
edges = {} | |
edges.top = rowCoordinate | |
edges.right = gridSize - colCoordinate + 1 | |
edges.bottom = gridSize - rowCoordinate + 1 | |
edges.left = colCoordinate | |
finalEdges = {} | |
for edgeIndex = 1, #edgesCompare do | |
edgeName = edgesCompare[edgeIndex] | |
edgeVal = edges[edgeName] | |
finalEdges[edgeName] = edgeVal | |
end | |
local edgeReturn | |
for i, v in pairs(finalEdges) do | |
edgeReturn = edgeReturn or i | |
if (v > finalEdges[edgeReturn]) then | |
edgeReturn = i | |
end | |
end | |
return edgeReturn | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment