extend stdmin.lua & add module base:generation/ores

This commit is contained in:
MihailRis 2024-10-14 07:38:54 +03:00
parent 4c3ce8c174
commit a1407a1d1f
4 changed files with 97 additions and 89 deletions

View File

@ -1,31 +1,10 @@
local _, dir = parse_path(__DIR__)
ores = file.read_combined_list(dir.."/ores.json")
local function place_ores(placements, x, z, w, d, seed, hmap, chunk_height)
local BLOCKS_PER_CHUNK = w * d * chunk_height
for _, ore in ipairs(ores) do
local count = BLOCKS_PER_CHUNK / ore.rarity
-- average count is less than 1
local addchance = math.fmod(count, 1.0)
if math.random() < addchance then
count = count + 1
end
for i=1,count do
local sx = math.random() * w
local sz = math.random() * d
local sy = math.random() * (chunk_height * 0.5)
if sy < hmap:at(sx, sz) * chunk_height - 6 then
table.insert(placements, {ore.struct, {sx, sy, sz}, math.random()*4, -1})
end
end
end
end
local ores = require "base:generation/ores"
ores.load(dir)
function place_structures(x, z, w, d, seed, hmap, chunk_height)
local placements = {}
place_ores(placements, x, z, w, d, seed, hmap, chunk_height)
ores.place(placements, x, z, w, d, seed, hmap, chunk_height)
return placements
end

View File

@ -0,0 +1,29 @@
local ores = {}
function ores.load(directory)
ores.ores = file.read_combined_list(directory.."/ores.json")
end
function ores.place(placements, x, z, w, d, seed, hmap, chunk_height)
local BLOCKS_PER_CHUNK = w * d * chunk_height
for _, ore in ipairs(ores.ores) do
local count = BLOCKS_PER_CHUNK / ore.rarity
-- average count is less than 1
local addchance = math.fmod(count, 1.0)
if math.random() < addchance then
count = count + 1
end
for i=1,count do
local sx = math.random() * w
local sz = math.random() * d
local sy = math.random() * (chunk_height * 0.5)
if sy < hmap:at(sx, sz) * chunk_height - 6 then
table.insert(placements, {ore.struct, {sx, sy, sz}, math.random()*4, -1})
end
end
end
end
return ores

View File

@ -2,71 +2,6 @@
------ Extended kit of standard functions ------
------------------------------------------------
package = {
loaded={}
}
local __cached_scripts = {}
local __warnings_hidden = {}
function on_deprecated_call(name, alternatives)
if __warnings_hidden[name] then
return
end
__warnings_hidden[name] = true
if alternatives then
debug.warning("deprecated function called ("..name.."), use "..
alternatives.." instead\n"..debug.traceback())
else
debug.warning("deprecated function called ("..name..")\n"..debug.traceback())
end
end
-- Load script with caching
--
-- path - script path `contentpack:filename`.
-- Example `base:scripts/tests.lua`
--
-- nocache - ignore cached script, load anyway
local function __load_script(path, nocache)
local packname, filename = parse_path(path)
-- __cached_scripts used in condition because cached result may be nil
if not nocache and __cached_scripts[path] ~= nil then
return package.loaded[path]
end
if not file.isfile(path) then
error("script '"..filename.."' not found in '"..packname.."'")
end
local script, err = load(file.read(path), path)
if script == nil then
error(err)
end
local result = script()
if not nocache then
__cached_scripts[path] = script
package.loaded[path] = result
end
return result
end
function __scripts_cleanup()
print("cleaning scripts cache")
for k, v in pairs(__cached_scripts) do
local packname, _ = parse_path(k)
if packname ~= "core" then
print("unloaded "..k)
__cached_scripts[k] = nil
package.loaded[k] = nil
end
end
end
function require(path)
local prefix, file = parse_path(path)
return __load_script(prefix..":modules/"..file..".lua")
end
function sleep(timesec)
local start = time.uptime()
while time.uptime() - start < timesec do

View File

@ -222,3 +222,68 @@ function file.readlines(path)
end
return lines
end
package = {
loaded={}
}
local __cached_scripts = {}
local __warnings_hidden = {}
function on_deprecated_call(name, alternatives)
if __warnings_hidden[name] then
return
end
__warnings_hidden[name] = true
if alternatives then
debug.warning("deprecated function called ("..name.."), use "..
alternatives.." instead\n"..debug.traceback())
else
debug.warning("deprecated function called ("..name..")\n"..debug.traceback())
end
end
-- Load script with caching
--
-- path - script path `contentpack:filename`.
-- Example `base:scripts/tests.lua`
--
-- nocache - ignore cached script, load anyway
local function __load_script(path, nocache)
local packname, filename = parse_path(path)
-- __cached_scripts used in condition because cached result may be nil
if not nocache and __cached_scripts[path] ~= nil then
return package.loaded[path]
end
if not file.isfile(path) then
error("script '"..filename.."' not found in '"..packname.."'")
end
local script, err = load(file.read(path), path)
if script == nil then
error(err)
end
local result = script()
if not nocache then
__cached_scripts[path] = script
package.loaded[path] = result
end
return result
end
function require(path)
local prefix, file = parse_path(path)
return __load_script(prefix..":modules/"..file..".lua")
end
function __scripts_cleanup()
print("cleaning scripts cache")
for k, v in pairs(__cached_scripts) do
local packname, _ = parse_path(k)
if packname ~= "core" then
print("unloaded "..k)
__cached_scripts[k] = nil
package.loaded[k] = nil
end
end
end