Skip to content

Instantly share code, notes, and snippets.

@Drumsin
Last active June 11, 2022 20:37
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/ee39d17ba2379785f07f44136a430c3d to your computer and use it in GitHub Desktop.
Save Drumsin/ee39d17ba2379785f07f44136a430c3d to your computer and use it in GitHub Desktop.
AoE4 Generated Map Function: Draw number of tiles in a ring that are positioned randomly
--extends the GetAllSquaresOfTypeInRingAroundSquare() function in that you can choose the number of tiles to draw which are randomly placed in a ring
--rowCoord and colCoord is the square position for the ring centre
--numberOfTilesToDraw is the number of tiles you want to draw which are randomly positioned in the ring
--chosenTerrain is the terrain to draw
--ringRadius is the distance in squares from the center
--ringWidth is the thickness of the ring inwards towards the center
--terrainTypes is a list of terrain types that can be selcted in the ring. If a potential ring square does not match one of the types it won't be selected
--terrainLayoutResult is the coarse grid layout
function drawNumberOfTilesInRingRandom(rowCoord, colCoord, numberOfTilesToDraw, chosenTerrain, ringRadius, ringWidth, terrainTypes, terrainLayoutResult)
count = 0
neighbors = GetAllSquaresOfTypeInRingAroundSquare(rowCoord, colCoord, ringRadius, ringWidth, terrainTypes, terrainLayoutResult)
print('placeRandomNumberOfTilesInRing: '..#neighbors..' neighbors found')
if (#neighbors < 1) then
print('placeRandomNumberOfTilesInRing: No neighbor tiles found. Check selected terrainTypes')
return
end
if (numberOfTilesToDraw > #neighbors) then
print('placeRandomNumberOfTilesInRing: number of tiles to draw is less than found neighbors. Setting numberOfTilesToDraw to '..#neighbors)
numberOfTilesToDraw = #neighbors
end
::tryAgain::
print('placeRandomNumberOfTilesInRing: making pass to place random terrain')
for k, v in ipairs(neighbors) do
chosenIndex = GetRandomIndex(neighbors)
if (count < numberOfTilesToDraw and terrainLayoutResult[neighbors[chosenIndex][1]][neighbors[chosenIndex][2]].terrainType ~= chosenTerrain) then
terrainLayoutResult[neighbors[chosenIndex][1]][neighbors[chosenIndex][2]].terrainType = chosenTerrain
table.remove(neighbors, chosenIndex)
count = count + 1
end
end
if (count < numberOfTilesToDraw) then
goto tryAgain
end
end
--run the function!
numberOfTilesToDraw = 8
drawNumberOfTilesInRingRandom(mapHalfSize, mapHalfSize, numberOfTilesToDraw, tt_impasse_trees_plains_forest, 3, 1, { tt_none, tt_plains }, terrainLayoutResult)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment