From 86fdcb289b1cfa510c861c677731599b4f0f0519 Mon Sep 17 00:00:00 2001 From: MihailRis Date: Fri, 24 Jul 2026 19:19:07 +0300 Subject: [PATCH 1/5] fix the margin --- res/layouts/pages/new_world.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/res/layouts/pages/new_world.xml b/res/layouts/pages/new_world.xml index 84a9519fa..fc13d5084 100644 --- a/res/layouts/pages/new_world.xml +++ b/res/layouts/pages/new_world.xml @@ -11,7 +11,7 @@ @World generator From 6910a7e029bd039415cd28a85ee13167e5806ad3 Mon Sep 17 00:00:00 2001 From: MihailRis Date: Fri, 24 Jul 2026 19:57:40 +0300 Subject: [PATCH 2/5] add math.noise --- res/modules/internal/extensions/math.lua | 32 ++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/res/modules/internal/extensions/math.lua b/res/modules/internal/extensions/math.lua index df0eadf72..1d168a377 100644 --- a/res/modules/internal/extensions/math.lua +++ b/res/modules/internal/extensions/math.lua @@ -39,3 +39,35 @@ end function math.sign(x) return (x > 0) and 1 or (x < 0 and -1 or 0) end + +local noise_period = 3021 +local noise_rands = {} + +for i=1,noise_period do + table.insert(noise_rands, math.random() * 2.0 - 1.0) +end + +local function smoothstep(t) + return t * t * (3 - 2 * t) +end + +local function sample_noise(x) + x = x % noise_period + local left_cell = math.floor(x) + local right_cell = (left_cell + 1) % noise_period + local t = smoothstep(x - left_cell) + return noise_rands[left_cell + 1] * (1.0 - t) + + noise_rands[right_cell + 1] * t +end + +function math.noise(x, octaves) + octaves = octaves or 1 + local value = 0.0 + local mul = 1.0 + for i=1,octaves do + value = value * (1.0 - mul) + sample_noise(x + 37 * i) * mul + x = x * 2.0 + mul = mul * 0.5 + end + return value +end From 192dbd9b6ca85025c4e982dcd45bd24f9a48530d Mon Sep 17 00:00:00 2001 From: MihailRis Date: Fri, 24 Jul 2026 20:01:14 +0300 Subject: [PATCH 3/5] add math.noise2d --- res/modules/internal/extensions/math.lua | 46 ++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/res/modules/internal/extensions/math.lua b/res/modules/internal/extensions/math.lua index 1d168a377..97bbc33a6 100644 --- a/res/modules/internal/extensions/math.lua +++ b/res/modules/internal/extensions/math.lua @@ -71,3 +71,49 @@ function math.noise(x, octaves) end return value end + +local function lerp(a, b, t) + return a + (b - a) * t +end + +local function rand_2d(x, y) + return noise_rands[math.floor(x * 317 + y) % noise_period + 1] +end + +local function sample_noise2d(x, y) + local x0 = math.floor(x) + local y0 = math.floor(y) + local x1 = x0 + 1 + local y1 = y0 + 1 + + local tx = x - x0 + local ty = y - y0 + + local sx = smoothstep(tx) + local sy = smoothstep(ty) + + local c00 = rand_2d(x0, y0) + local c10 = rand_2d(x1, y0) + local c01 = rand_2d(x0, y1) + local c11 = rand_2d(x1, y1) + + local bottom = lerp(c00, c10, sx) + local top = lerp(c01, c11, sx) + + return lerp(bottom, top, sy) +end + + +function math.noise2d(x, y, octaves) + octaves = octaves or 1 + local value = 0.0 + local mul = 1.0 + for i=1,octaves do + value = value * (1.0 - mul) + + sample_noise2d(x + 37 * i, y + 73 * i) * mul + x = x * 2.0 + y = y * 2.0 + mul = mul * 0.5 + end + return value +end From e09eb41cc7120149cb2d479215a7e35ae5d5b346 Mon Sep 17 00:00:00 2001 From: MihailRis Date: Fri, 24 Jul 2026 20:27:42 +0300 Subject: [PATCH 4/5] update doc/*/scripting/extensions.md --- doc/ru/scripting/extensions.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/doc/ru/scripting/extensions.md b/doc/ru/scripting/extensions.md index e7b0fe292..f51ffcb07 100644 --- a/doc/ru/scripting/extensions.md +++ b/doc/ru/scripting/extensions.md @@ -158,6 +158,12 @@ math.sum(x: number, ... | t: table) -> number -- Возвращает целое число, указывающее знак числа (-1/0/1) math.sign(x: number) -> int + +-- Одномерный шум с значениями в диапазоне [-1..1]. +math.noise(x: number, [опционально] octaves: int = 1) -> number + +-- Двумерный шум с значениями в диапазоне [-1..1]. +math.noise2d(x: number, y: number [опционально] octaves: int = 1) -> number ``` ## Расширения для bit From 6b090e20c1beeb4a2bfdcc4b8c75c2322a589142 Mon Sep 17 00:00:00 2001 From: MihailRis Date: Fri, 24 Jul 2026 20:33:18 +0300 Subject: [PATCH 5/5] ignore negative noise arguments --- res/modules/internal/extensions/math.lua | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/res/modules/internal/extensions/math.lua b/res/modules/internal/extensions/math.lua index 97bbc33a6..f653bf80e 100644 --- a/res/modules/internal/extensions/math.lua +++ b/res/modules/internal/extensions/math.lua @@ -61,6 +61,7 @@ local function sample_noise(x) end function math.noise(x, octaves) + x = math.abs(x) octaves = octaves or 1 local value = 0.0 local mul = 1.0 @@ -81,6 +82,9 @@ local function rand_2d(x, y) end local function sample_noise2d(x, y) + x = math.abs(x) + y = math.abs(y) + local x0 = math.floor(x) local y0 = math.floor(y) local x1 = x0 + 1