Last active
September 2, 2024 06:12
-
-
Save cameronpcampbell/6fc0649261957261abd5b8c4acfe459b to your computer and use it in GitHub Desktop.
This file contains hidden or 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
--[[ | |
Copyright 2024 Cameron Campbell | |
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: | |
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | |
]] | |
--!strict | |
--!native | |
--!optimize 2 | |
local function PositionToIndex(rows: number, cols: number) | |
return function(x: number, y: number) | |
return (x - 1) * cols + y | |
end | |
end | |
-- Checks To If A Point Intersects The Radius Of Another Point. | |
local function IsValid( | |
rows: number, cols: number, | |
regionX: number, regionZ: number, | |
cellSize: number, | |
radius: number, points: { Vector3 }, grid: { number }, | |
positionToIndex: (x: number, y: number) -> number | |
) | |
return function(candidate: Vector3): boolean | |
local candidateX, candidateZ = candidate.X, candidate.Z | |
if candidateX >= 0 and candidateZ >= 0 and candidateX < regionX and candidateZ < regionZ then | |
local cellX, cellZ = math.floor(candidateX / cellSize), math.floor(candidateZ / cellSize) | |
local searchStartX, searchEndX = math.max(0, cellX - 2), math.min(cellX + 2, rows - 1) | |
local searchStartZ, searchEndZ = math.max(0, cellZ - 2), math.min(cellZ + 2, cols - 1) | |
for x = searchStartX, searchEndX do | |
for z = searchStartZ, searchEndZ do | |
local pointIndex = grid[positionToIndex(x + 1, z + 1)] - 1 | |
if pointIndex ~= -1 and (candidate - points[pointIndex + 1]).Magnitude < radius then | |
return false | |
end | |
end | |
end | |
return true | |
end | |
return false | |
end | |
end | |
return function(radius: number, region: Vector3, samples: number, random: Random) | |
local cellSize = radius / math.sqrt(2) | |
local regionX, regionZ = region.X, region.Z | |
local rows, cols = math.ceil(regionX / cellSize), math.ceil(regionZ / cellSize) | |
local grid: { number } = table.create(rows * (cols+1), 0) | |
local points: { Vector3 } = {} | |
local spawnPoints: { Vector3 } = {} | |
local positionToIndex = PositionToIndex(rows, cols) | |
local isValid = IsValid(rows, cols, regionX, regionZ, cellSize, radius, points, grid, positionToIndex) | |
table.insert(spawnPoints, region/2) | |
while #spawnPoints > 0 do | |
local spawnIndex = random:NextInteger(1, #spawnPoints) | |
local spawnCentre = spawnPoints[spawnIndex] | |
local candidateAccepted = false | |
for count = 1, samples do | |
local angle = random:NextNumber() * math.pi * 2 | |
local dir = Vector3.new(math.sin(angle), 0, math.cos(angle)) | |
local candidate = spawnCentre + dir * random:NextInteger(radius, 2*radius) | |
if isValid(candidate) then | |
table.insert(points, candidate) | |
table.insert(spawnPoints, candidate) | |
grid[positionToIndex(math.ceil(candidate.X / cellSize), math.ceil(candidate.Z / cellSize))] = #points | |
candidateAccepted = true | |
break | |
end | |
end | |
if not candidateAccepted then | |
table.remove(spawnPoints, spawnIndex) | |
end | |
end | |
return points | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example Usage