Skip to content

Instantly share code, notes, and snippets.

@Drumsin
Last active June 11, 2022 03:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Drumsin/9a9839023d1b79c745445b78b4db4323 to your computer and use it in GitHub Desktop.
Save Drumsin/9a9839023d1b79c745445b78b4db4323 to your computer and use it in GitHub Desktop.
AoE4 Generated Map Function: Closest or Furthest edge functions
--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