diff --git a/mods/CORE/MapGen/src/MapGen.lua b/mods/CORE/MapGen/src/MapGen.lua index afa0543..ceebe72 100644 --- a/mods/CORE/MapGen/src/MapGen.lua +++ b/mods/CORE/MapGen/src/MapGen.lua @@ -1,13 +1,15 @@ local assert = assert local pairs, ipairs = pairs, ipairs -local mathRound, mathRandom, mathClamp = math.round, math.random, math.clamp +local bitToBit, bitBxor, bitRshift, bitBand = bit.tobit, bit.bxor, bit.rshift, bit.band +local mathFloor, mathRound, mathRandom, mathClamp = math.floor, math.round, math.random, math.clamp local tableInsert, tableSort = table.insert, table.sort local v = vector.new local id = core.get_content_id local AIR_ID = core.CONTENT_AIR local corePosToString, coreGetUsTime = core.pos_to_string, core.get_us_time +local PcgRandom = PcgRandom local getInstance = Class.getInstance @@ -209,6 +211,32 @@ function MapGen:getTree(name) return self.treesByName[name] end +---Returns a pseudorandom number within +---a specified range based on coordinates. +--- +---Use this to generate deterministic random +---numbers within node generation functions. +---@param x integer +---@param y integer +---@param z integer +---@param min integer +---@param max integer +---@return integer +function MapGen:random(x, y, z, min, max) + local n = bitToBit(bitBxor(x, 0xB57B6AE1) * 0x9E3779B9) + n = bitToBit(bitBxor(n, y) * 0x85EBCA6B) + n = bitToBit(bitBxor(n, z) * 0xC2B2AE35) + + n = bitBxor(n, bit.rshift(n, 16)) + n = bitToBit(n * 0x27D4EB2D) + n = bitBxor(n, bit.rshift(n, 15)) + + n = bitBand(n, 0x7FFFFFFF) + + local range = max - min + 1 + return min + mathFloor((n / 2147483648) * range) +end + -- --- API METHODS: REGISTRATION --- @@ -928,6 +956,7 @@ local function generatorHandler(mapGenerator, layer, data, data2, index, x, y, z local endGetLayerTime = coreGetUsTime() local startNoiseCalcTime = endGetLayerTime + -- If the value of the noise is `nil`, -- then the point does not fall within any of the triangles. local height, temp, humidity = mapGenerator:getFactors(layer, x, y, z)