mirror of
https://gitlab.com/gaelysam/mapgen_rivers.git
synced 2025-02-22 06:50:29 +01:00
Localize all global functions in load.lua and geometry.lua
This commit is contained in:
parent
0c98fc0881
commit
c3a798933f
13
geometry.lua
13
geometry.lua
@ -1,3 +1,6 @@
|
|||||||
|
local sqrt, abs = math.sqrt, math.abs
|
||||||
|
local unpk = unpack
|
||||||
|
|
||||||
local function distance_to_segment(x1, y1, x2, y2, x, y)
|
local function distance_to_segment(x1, y1, x2, y2, x, y)
|
||||||
-- get the distance between point (x,y) and segment (x1,y1)-(x2,y2)
|
-- get the distance between point (x,y) and segment (x1,y1)-(x2,y2)
|
||||||
local a = (x1-x2)^2 + (y1-y2)^2 -- square of distance
|
local a = (x1-x2)^2 + (y1-y2)^2 -- square of distance
|
||||||
@ -5,13 +8,13 @@ local function distance_to_segment(x1, y1, x2, y2, x, y)
|
|||||||
local c = (x2-x)^2 + (y2-y)^2
|
local c = (x2-x)^2 + (y2-y)^2
|
||||||
if a + b < c then
|
if a + b < c then
|
||||||
-- The closest point of the segment is the extremity 1
|
-- The closest point of the segment is the extremity 1
|
||||||
return math.sqrt(b)
|
return sqrt(b)
|
||||||
elseif a + c < b then
|
elseif a + c < b then
|
||||||
-- The closest point of the segment is the extremity 2
|
-- The closest point of the segment is the extremity 2
|
||||||
return math.sqrt(c)
|
return sqrt(c)
|
||||||
else
|
else
|
||||||
-- The closest point is on the segment
|
-- The closest point is on the segment
|
||||||
return math.abs(x1 * (y2-y) + x2 * (y-y1) + x * (y1-y2)) / math.sqrt(a)
|
return abs(x1 * (y2-y) + x2 * (y-y1) + x * (y1-y2)) / sqrt(a)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -19,8 +22,8 @@ local function transform_quadri(X, Y, x, y)
|
|||||||
-- To index points in an irregular quadrilateral, giving x and y between 0 (one edge) and 1 (opposite edge)
|
-- To index points in an irregular quadrilateral, giving x and y between 0 (one edge) and 1 (opposite edge)
|
||||||
-- X, Y 4-vectors giving the coordinates of the 4 vertices
|
-- X, Y 4-vectors giving the coordinates of the 4 vertices
|
||||||
-- x, y position to index.
|
-- x, y position to index.
|
||||||
local x1, x2, x3, x4 = unpack(X)
|
local x1, x2, x3, x4 = unpk(X)
|
||||||
local y1, y2, y3, y4 = unpack(Y)
|
local y1, y2, y3, y4 = unpk(Y)
|
||||||
|
|
||||||
-- Compare distance to 2 opposite edges, they give the X coordinate
|
-- Compare distance to 2 opposite edges, they give the X coordinate
|
||||||
local d23 = distance_to_segment(x2,y2,x3,y3,x,y)
|
local d23 = distance_to_segment(x2,y2,x3,y3,x,y)
|
||||||
|
16
load.lua
16
load.lua
@ -1,12 +1,15 @@
|
|||||||
local worldpath = mapgen_rivers.world_data_path
|
local worldpath = mapgen_rivers.world_data_path
|
||||||
|
|
||||||
|
local floor = math.floor
|
||||||
|
local sbyte, schar = string.byte, string.char
|
||||||
|
local unpk = unpack
|
||||||
|
|
||||||
function mapgen_rivers.load_map(filename, bytes, signed, size, converter)
|
function mapgen_rivers.load_map(filename, bytes, signed, size, converter)
|
||||||
local file = io.open(worldpath .. filename, 'rb')
|
local file = io.open(worldpath .. filename, 'rb')
|
||||||
local data = file:read('*all')
|
local data = file:read('*all')
|
||||||
if #data < bytes*size then
|
if #data < bytes*size then
|
||||||
data = minetest.decompress(data)
|
data = minetest.decompress(data)
|
||||||
end
|
end
|
||||||
local sbyte = string.byte
|
|
||||||
|
|
||||||
local map = {}
|
local map = {}
|
||||||
|
|
||||||
@ -35,8 +38,6 @@ function mapgen_rivers.load_map(filename, bytes, signed, size, converter)
|
|||||||
return map
|
return map
|
||||||
end
|
end
|
||||||
|
|
||||||
local sbyte = string.byte
|
|
||||||
|
|
||||||
local loader_mt = {
|
local loader_mt = {
|
||||||
__index = function(loader, i)
|
__index = function(loader, i)
|
||||||
local file = loader.file
|
local file = loader.file
|
||||||
@ -75,9 +76,6 @@ end
|
|||||||
function mapgen_rivers.write_map(filename, data, bytes)
|
function mapgen_rivers.write_map(filename, data, bytes)
|
||||||
local size = #data
|
local size = #data
|
||||||
local file = io.open(worldpath .. filename, 'wb')
|
local file = io.open(worldpath .. filename, 'wb')
|
||||||
local mfloor = math.floor
|
|
||||||
local schar = string.char
|
|
||||||
local upack = unpack
|
|
||||||
|
|
||||||
local bytelist = {}
|
local bytelist = {}
|
||||||
for j=1, bytes do
|
for j=1, bytes do
|
||||||
@ -85,15 +83,15 @@ function mapgen_rivers.write_map(filename, data, bytes)
|
|||||||
end
|
end
|
||||||
|
|
||||||
for i=1, size do
|
for i=1, size do
|
||||||
local n = mfloor(data[i])
|
local n = floor(data[i])
|
||||||
data[i] = n
|
data[i] = n
|
||||||
for j=bytes, 2, -1 do
|
for j=bytes, 2, -1 do
|
||||||
bytelist[j] = n % 256
|
bytelist[j] = n % 256
|
||||||
n = mfloor(n / 256)
|
n = floor(n / 256)
|
||||||
end
|
end
|
||||||
bytelist[1] = n % 256
|
bytelist[1] = n % 256
|
||||||
|
|
||||||
file:write(schar(upack(bytelist)))
|
file:write(schar(unpk(bytelist)))
|
||||||
end
|
end
|
||||||
|
|
||||||
file:close()
|
file:close()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user