Last active
March 9, 2022 00:49
-
-
Save Warr1024/13348164022ff49f5676d9395f34cdba to your computer and use it in GitHub Desktop.
Infinite IKEA Department Patch Mapgen
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
------------------------------------------------------------------------ | |
-- DEPARTMENT MAP GENERATION LOGIC | |
local perlin | |
minetest.after(0, function() | |
local pn = minetest.get_perlin(0, 1, 0, 1) | |
perlin = function(x, y, z) | |
local n = pn:get_3d({x = x, y = y, z = z}) | |
return n - math.floor(n) | |
end | |
end) | |
local function split(r, min, max, val, bd) | |
local div = min + bd + math.floor((max - min + 1 - bd * 2) * r) | |
if max - min <= 1 then div = max end | |
if val < div then return min, div - 1 end | |
return div, max | |
end | |
local function pickdept(opts, minx, maxx, minz, maxz, x, z) | |
local rand = perlin(minx, 2, minz) | |
local dept = opts.depts[math.floor(rand * #opts.depts) + 1] | |
local edges = { | |
w = x <= minx, | |
e = x >= (maxx - 1), | |
s = z <= minz, | |
n = z >= (maxz - 1) | |
} | |
return dept, edges | |
end | |
local function subdiv(opts, minx, maxx, minz, maxz, x, z) | |
local w = maxx - minx + 1 | |
local h = maxz - minz + 1 | |
if (w > opts.maxsize) or (h > opts.maxsize) or perlin(minx, 0, minz) < 0.5 then | |
if h > w then | |
local minz2, maxz2 = split(perlin(minx, 1, minz), | |
minz, maxz, z, opts.minsize) | |
local h2 = maxz2 - minz2 + 1 | |
if h2 < opts.minsize or (h - h2) < opts.minsize then | |
return pickdept(opts, minx, maxx, minz, maxz, x, z) | |
end | |
return subdiv(opts, minx, maxx, minz2, maxz2, x, z) | |
else | |
local minx2, maxx2 = split(perlin(minx, 1, minz), | |
minx, maxx, x, opts.minsize) | |
local w2 = maxx2 - minx2 + 1 | |
if w2 < opts.minsize or (w - w2) < opts.minsize then | |
return pickdept(opts, minx, maxx, minz, maxz, x, z) | |
end | |
return subdiv(opts, minx2, maxx2, minz, maxz, x, z) | |
end | |
end | |
return pickdept(opts, minx, maxx, minz, maxz, x, z) | |
end | |
local function getdept(opts, x, z) | |
return subdiv(opts, -4096, 4095, -4096, 4095, x, z) | |
end | |
------------------------------------------------------------------------ | |
-- MAPGEN VISUALIZATION EXAMPLE | |
local opts = { | |
depts = {}, | |
minsize = 4, | |
maxsize = 16 | |
} | |
minetest.after(0, function() | |
for k, v in pairs(minetest.registered_nodes) do | |
if v.walkable and v.drawtype == "normal" and not v.groups.falling_node then | |
opts.depts[#opts.depts + 1] = k | |
end | |
end | |
end) | |
local geny = -10 | |
minetest.register_on_generated(function(minp, maxp) | |
if minp.y > geny or maxp.y < geny then return end | |
local minx = -4096 | |
if minp.x > minx then minx = minp.x end | |
local maxx = 4095 | |
if maxp.x < maxx then maxx = maxp.x end | |
if minx > maxx then return end | |
local minz = -4096 | |
if minp.z > minz then minz = minp.z end | |
local maxz = 4095 | |
if maxp.z < maxz then maxz = maxp.z end | |
if minz > maxz then return end | |
for x = minx, maxx do | |
for z = minz, maxz do | |
local d = getdept(opts, x, z) | |
print(d) | |
minetest.set_node({x = x, y = geny, z = z}, | |
{name = d}) | |
end | |
end | |
end) |
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 (C)2019 Aaron Suen <[email protected]> | |
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. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment