From a1407a1d1fce5f96e6baed694f748f7ae2ada037 Mon Sep 17 00:00:00 2001 From: MihailRis Date: Mon, 14 Oct 2024 07:38:54 +0300 Subject: [PATCH] extend stdmin.lua & add module base:generation/ores --- .../base/generators/demo.files/script.lua | 27 +------- res/content/base/modules/generation/ores.lua | 29 +++++++++ res/scripts/stdlib.lua | 65 ------------------- res/scripts/stdmin.lua | 65 +++++++++++++++++++ 4 files changed, 97 insertions(+), 89 deletions(-) create mode 100644 res/content/base/modules/generation/ores.lua diff --git a/res/content/base/generators/demo.files/script.lua b/res/content/base/generators/demo.files/script.lua index 8e138fd2..7596d226 100644 --- a/res/content/base/generators/demo.files/script.lua +++ b/res/content/base/generators/demo.files/script.lua @@ -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 diff --git a/res/content/base/modules/generation/ores.lua b/res/content/base/modules/generation/ores.lua new file mode 100644 index 00000000..3b2c2f91 --- /dev/null +++ b/res/content/base/modules/generation/ores.lua @@ -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 diff --git a/res/scripts/stdlib.lua b/res/scripts/stdlib.lua index f6e2603a..8fbc779f 100644 --- a/res/scripts/stdlib.lua +++ b/res/scripts/stdlib.lua @@ -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 diff --git a/res/scripts/stdmin.lua b/res/scripts/stdmin.lua index 9fd982bc..a063ee8d 100644 --- a/res/scripts/stdmin.lua +++ b/res/scripts/stdmin.lua @@ -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