Add files via upload

This commit is contained in:
Tim7900 2021-12-29 19:01:19 -09:00 committed by GitHub
parent d2a6c9aa90
commit 183c3457ec
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 1648 additions and 0 deletions

View File

@ -0,0 +1 @@
CC0, except for code copied from e.g. minetest's builtin

View File

@ -0,0 +1,6 @@
TODO:
* maybe make the explosion table function return a perlin explosion table
* Figure out and implement 3D scanline search
* Add vector.hollowsphere, less positions than WorldEdit hollowsphere
* Add unit tests
* Use %a string format for vector.serialize so that it is reversible

View File

@ -0,0 +1,116 @@
-- http://www.adammil.net/blog/v126_A_More_Efficient_Flood_Fill.html
local can_go
local marked_places
local function calc_2d_index(x, y)
return (y + 32768) * 65536 + x + 32768
end
local function mark(x, y)
marked_places[calc_2d_index(x, y)] = true
end
local _fill
local function fill(x, y)
if can_go(x, y) then
_fill(x, y)
end
end
local corefill
function _fill(x, y)
while true do
local ox = x
local oy = y
while can_go(x, y-1) do
y = y-1
end
while can_go(x-1, y) do
x = x-1
end
if x == ox
and y == oy then
break
end
end
corefill(x, y)
end
function corefill(x, y)
local lastcnt = 0
repeat
local cnt = 0
local sx = x
if lastcnt ~= 0
and not can_go(y, x) then
-- go right to find the x start
repeat
lastcnt = lastcnt-1
if lastcnt == 0 then
return
end
x = x+1
until can_go(x, y)
sx = x
else
-- go left if possible, and mark and _fill above
while can_go(x-1, y) do
x = x-1
mark(x, y)
if can_go(x, y-1) then
_fill(x, y-1)
end
cnt = cnt+1
lastcnt = lastcnt+1
end
end
-- go right if possible, and mark
while can_go(sx, y) do
mark(sx, y)
cnt = cnt+1
sx = sx+1
end
if cnt < lastcnt then
local e = x + lastcnt
sx = sx+1
while sx < e do
if can_go(sx, y) then
corefill(sx, y)
end
sx = sx+1
end
elseif cnt > lastcnt then
local ux = x + lastcnt + 1
while ux < sx do
if can_go(ux, y-1) then
_fill(ux, y-1)
end
ux = ux+1
end
end
lastcnt = cnt
y = y+1
until lastcnt == 0
end
local function apply_fill(go_test, x0, y0, allow_revisit)
if allow_revisit then
can_go = go_test
else
local visited = {}
can_go = function(x, y)
local vi = calc_2d_index(x, y)
if visited[vi] then
return false
end
visited[vi] = true
return go_test(x, y)
end
end
marked_places = {}
fill(x0, y0)
return marked_places
end
return apply_fill

154
mods/vector_extras/doc.md Normal file
View File

@ -0,0 +1,154 @@
# Vector helpers added by this mod
## Helpers which return many positions for a shape, e.g. a line
### Line functions
These may be deprecated since raycasting has been added to minetest.
See e.g. `minetest.line_of_sight`.
* `vector.line([pos, dir[, range][, alt]])`: returns a table of vectors
* `dir` is either a direction (when range is a number) or
the start position (when range is the end position).
* If alt is true, an old path calculation is used.
* `vector.twoline(x, y)`: can return e.g. `{{0,0}, {0,1}}`
* This is a lower-level function than `vector.line`; it can be used for
a 2D line.
* `vector.threeline(x, y, z)`: can return e.g. `{{0,0,0}, {0,1,0}}`
* Similar to `vector.twoline`; this one is for the 3D case.
* The parameters should be integers.
* `vector.rayIter(pos, dir)`: returns an iterator for a for loop
* `pos` can have non-integer values
* `vector.fine_line([pos, dir[, range], scale])`: returns a table of vectors
* Like `vector.line` but allows non-integer positions
* It uses `vector.rayIter`.
### Flood Fill
* `vector.search_2d(go_test, x0, y0, allow_revisit, give_map)`: returns e.g.
`{{0,0}, {0,1}}`
* This function uses a Flood Fill algorithm, so it can be used to detect
positions connected to each other in 2D.
* `go_test(x, y)` should be a function which returns true iff the algorithm
can "fill" at the position `(x, y)`.
* `(x0, y0)` defines the start position.
* If `allow_revisit` is false (the default), the function
invokes `go_test` only once at every potential position.
* If `give_map` is true (default is false), the function returns the
marked table, whose indices are 2D vector indices, instead of a list of
2D positions.
* `vector.search_3d(can_go, startpos, apply_move, moves)`: returns FIXME
* FIXME
### Other Shapes
* `vector.explosion_table(r)`: returns e.g. `{{pos1}, {pos2, true}}`
* The returned list of positions and boolean represents a sphere;
if the boolean is true, the position is on the outer side of the sphere.
* It might be used for explosion calculations; but `vector.explosion_perlin`
should make more realistic holes.
* `vector.explosion_perlin(rmin, rmax[, nparams])`: returns e.g.
`{{pos1}, {pos2, true}}`
* This function is similar to `vector.explosion_table`; the positions
do not represent a sphere but a more complex hole which is calculated
with the help of perlin noise.
* `rmin` and `rmax` represent the minimum and maximum radius,
and `nparams` (which has a default value) are parameters for the perlin
noise.
* `vector.circle(r)`: returns a table of vectors
* The returned positions represent a circle of radius `r` along the x and z
directions; the y coordinates are all zero.
* `vector.ring(r)`: returns a table of vectors
* This function is similar to `vector.circle`; the positions are all
touching each other (i.e. they are connected on whole surfaces and not
only infinitely thin edges), so it is called `ring` instead of `circle`
* `r` can be a non-integer number.
* `vector.throw_parabola(pos, vel, gravity, point_count, time)`
* FIXME: should return positions along a parabola so that moving objects
collisions can be calculated
* `vector.triangle(pos1, pos2, pos3)`: returns a table of positions, a number
and a table with barycentric coordinates
* This function calculates integer positions for a triangle defined by
`pos1`, `pos2` and `pos3`, so it can be used to place polygons in
minetest.
* The returned number is the number of positions.
* The barycentric coordinates are specified in a table with three elements;
the first one corresponds to `pos1`, etc.
## Helpers for various vector calculations
* `vector.sort_positions(ps[, preferred_coords])`
* Sorts a table of vectors `ps` along the coordinates specified in the
table `preferred_coords` in-place.
* If `preferred_coords` is omitted, it sorts along z, y and x in this order,
where z has the highest priority.
* `vector.maxnorm(v)`: returns the Tschebyshew norm of `v`
* `vector.sumnorm(v)`: returns the Manhattan norm of `v`
* `vector.pnorm(v, p)`: returns the `p` norm of `v`
* `vector.inside(pos, minp, maxp)`: returns a boolean
* Returns true iff `pos` is within the closed AABB defined by `minp`
and `maxp`.
* `vector.minmax(pos1, pos2)`: returns two vectors
* This does the same as `worldedit.sort_pos`.
* The components of the second returned vector are all bigger or equal to
those of the first one.
* `vector.move(pos1, pos2, length)`: returns a vector
* Go from `pos1` `length` metres to `pos2` and then round to the nearest
integer position.
* Made for rubenwardy
* `vector.from_number(i)`: returns `{x=i, y=i, z=i}`
* `vector.chunkcorner(pos)`: returns a vector
* Returns the mapblock position of the mapblock which contains
the integer position `pos`
* `vector.point_distance_minmax(p1, p2)`: returns two numbers
* Returns the minimum and maximum of the absolute component-wise distances
* `vector.collision(p1, p2)` FIXME
* `vector.update_minp_maxp(minp, maxp, pos)`
* Can change `minp` and `maxp` so that `pos` is within the AABB defined by
`minp` and `maxp`
* `vector.unpack(v)`: returns three numbers
* Returns `v.z, v.y, v.x`
* `vector.get_max_coord(v)`: returns a string
* Returns `"x"`, `"y"` or `"z"`, depending on which component has the
biggest value
* `vector.get_max_coords(v)`: returns three strings
* Similar to `vector.get_max_coord`; it returns the coordinates in the order
of their component values
* Example: `vector.get_max_coords{x=1, y=5, z=3}` returns `"y", "z", "x"`
* `vector.serialize(v)`: returns a string
* In comparison to `minetest.serialize`, this function uses a more compact
string for the serialization.
## Minetest-specific helper functions
* `vector.straightdelay([length, vel[, acc]])`: returns a number
* Returns the time an object takes to move `length` if it has velocity `vel`
and acceleration `acc`
* `vector.sun_dir([time])`: returns a vector or nil
* Returns the vector which points to the sun
* If `time` is omitted, it uses the current time.
* This function does not yet support the moon;
at night it simply returns `nil`.
## Helpers which I don't recommend to use now
* `vector.pos_to_string(pos)`: returns a string
* It is similar to `minetest.pos_to_string`; it uses a different format:
`"("..pos.x.."|"..pos.y.."|"..pos.z..")"`
* `vector.zero`
* The zero vector `{x=0, y=0, z=0}`
* `vector.quickadd(pos, [z],[y],[x])`
* Adds values to the vector components in-place
## Deprecated helpers
* `vector.plane`
* should be removed soon; it should have done the same as vector.triangle

View File

@ -0,0 +1,53 @@
-- Algorithm created by sofar and changed by others:
-- https://github.com/minetest/minetest/commit/d7908ee49480caaab63d05c8a53d93103579d7a9
local function search(go, p, apply_move, moves)
local num_moves = #moves
-- We make a stack, and manually maintain size for performance.
-- Stored in the stack, we will maintain tables with pos, and
-- last neighbor visited. This way, when we get back to each
-- node, we know which directions we have already walked, and
-- which direction is the next to walk.
local s = {}
local n = 0
-- The neighbor order we will visit from our table.
local v = 1
while true do
-- Push current pos onto the stack.
n = n + 1
s[n] = {p = p, v = v}
-- Select next node from neighbor list.
p = apply_move(p, moves[v])
-- Now we check out the node. If it is in need of an update,
-- it will let us know in the return value (true = updated).
if not go(p) then
-- If we don't need to "recurse" (walk) to it then pop
-- our previous pos off the stack and continue from there,
-- with the v value we were at when we last were at that
-- node
repeat
local pop = s[n]
p = pop.p
v = pop.v
s[n] = nil
n = n - 1
-- If there's nothing left on the stack, and no
-- more sides to walk to, we're done and can exit
if n == 0 and v == num_moves then
return
end
until v < num_moves
-- The next round walk the next neighbor in list.
v = v + 1
else
-- If we did need to walk the neighbor, then
-- start walking it from the walk order start (1),
-- and not the order we just pushed up the stack.
v = 1
end
end
end
return search

1040
mods/vector_extras/init.lua Normal file

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,94 @@
local funcs = vector_extras_functions
function funcs.scalar(v1, v2)
minetest.log("deprecated", "[vector_extras] vector.scalar is " ..
"deprecated, use vector.dot instead.")
return vector.dot(v1, v2)
end
function funcs.get_data_from_pos(tab, z,y,x)
minetest.log("deprecated", "[vector_extras] get_data_from_pos is " ..
"deprecated, use the minetest pos hash function instead.")
local data = tab[z]
if data then
data = data[y]
if data then
return data[x]
end
end
end
function funcs.set_data_to_pos(tab, z,y,x, data)
minetest.log("deprecated", "[vector_extras] set_data_to_pos is " ..
"deprecated, use the minetest pos hash function instead.")
if tab[z] then
if tab[z][y] then
tab[z][y][x] = data
return
end
tab[z][y] = {[x] = data}
return
end
tab[z] = {[y] = {[x] = data}}
end
function funcs.set_data_to_pos_optional(tab, z,y,x, data)
minetest.log("deprecated", "[vector_extras] set_data_to_pos_optional is " ..
"deprecated, use the minetest pos hash function instead.")
if vector.get_data_from_pos(tab, z,y,x) ~= nil then
return
end
funcs.set_data_to_pos(tab, z,y,x, data)
end
function funcs.remove_data_from_pos(tab, z,y,x)
minetest.log("deprecated", "[vector_extras] remove_data_from_pos is " ..
"deprecated, use the minetest pos hash function instead.")
if vector.get_data_from_pos(tab, z,y,x) == nil then
return
end
tab[z][y][x] = nil
if not next(tab[z][y]) then
tab[z][y] = nil
end
if not next(tab[z]) then
tab[z] = nil
end
end
function funcs.get_data_pos_table(tab)
minetest.log("deprecated", "[vector_extras] get_data_pos_table likely " ..
"is deprecated, use the minetest pos hash function instead.")
local t,n = {},1
local minz, miny, minx, maxz, maxy, maxx
for z,yxs in pairs(tab) do
if not minz then
minz = z
maxz = z
else
minz = math.min(minz, z)
maxz = math.max(maxz, z)
end
for y,xs in pairs(yxs) do
if not miny then
miny = y
maxy = y
else
miny = math.min(miny, y)
maxy = math.max(maxy, y)
end
for x,v in pairs(xs) do
if not minx then
minx = x
maxx = x
else
minx = math.min(minx, x)
maxx = math.max(maxx, x)
end
t[n] = {z,y,x, v}
n = n+1
end
end
end
return t, {x=minx, y=miny, z=minz}, {x=maxx, y=maxy, z=maxz}, n-1
end

View File

@ -0,0 +1 @@
name = vector_extras

View File

@ -0,0 +1,183 @@
vector.meta = vector.meta or {}
vector.meta.nodes = {}
vector.meta.nodes_file = {
load = function()
local nodesfile = io.open(minetest.get_worldpath()..'/vector_nodes.txt', "r")
if nodesfile then
local contents = nodesfile:read('*all')
io.close(nodesfile)
if contents ~= nil then
local lines = string.split(contents, "\n")
for _,entry in ipairs(lines) do
local name, px, py, pz, meta = unpack(string.split(entry, "°"))
vector.meta.set_node({x=px, y=py, z=pz}, name, meta)
end
end
end
end,
save = function() --WRITE CHANGES TO FILE
local output = ''
for x,ys in pairs(vector.meta.nodes) do
for y,zs in pairs(ys) do
for z,names in pairs(zs) do
for name,meta in pairs(names) do
output = name.."°"..x.."°"..y.."°"..z.."°"..dump(meta).."\n"
end
end
end
end
local f = io.open(minetest.get_worldpath()..'/vector_nodes.txt', "w")
f:write(output)
io.close(f)
end
}
local function table_empty(tab) --looks if it's an empty table
if next(tab) == nil then
return true
end
return false
end
function vector.meta.nodes_info() --returns an info string of the node table
local tmp = "[vector] "..dump(vector.meta.nodes).."\n[vector]:\n"
for x,a in pairs(vector.meta.nodes) do
for y,b in pairs(a) do
for z,c in pairs(b) do
for name,meta in pairs(c) do
tmp = tmp..">\t"..name.." "..x.." "..y.." "..z.." "..dump(meta).."\n"
end
end
end
end
return tmp
end
function vector.meta.clean_node_table() --replaces {} with nil
local again = true
while again do
again = false
for x,ys in pairs(vector.meta.nodes) do
if table_empty(ys) then
vector.meta.nodes[x] = nil
again = true
else
for y,zs in pairs(ys) do
if table_empty(zs) then
vector.meta.nodes[x][y] = nil
again = true
else
for z,names in pairs(zs) do
if table_empty(names) then
vector.meta.nodes[x][y][z] = nil
again = true
else
for name,meta in pairs(names) do
if table_empty(meta)
or meta == "" then
vector.meta.nodes[x][y][z][name] = nil
again = true
end
end
end
end
end
end
end
end
end
end
function vector.meta.complete_node_table(pos, name) --neccesary because tab[1] wouldn't work if tab is not a table
local tmp = vector.meta.nodes[pos.x]
if not tmp then
vector.meta.nodes[pos.x] = {}
end
tmp = vector.meta.nodes[pos.x][pos.y]
if not tmp then
vector.meta.nodes[pos.x][pos.y] = {}
end
tmp = vector.meta.nodes[pos.x][pos.y][pos.z]
if not tmp then
vector.meta.nodes[pos.x][pos.y][pos.z] = {}
end
tmp = vector.meta.nodes[pos.x][pos.y][pos.z][name]
if not tmp then
vector.meta.nodes[pos.x][pos.y][pos.z][name] = {}
end
end
function vector.meta.get_node(pos, name)
if not pos then
return false
end
local tmp = vector.meta.nodes[pos.x]
if not tmp
or table_empty(tmp) then
return false
end
tmp = vector.meta.nodes[pos.x][pos.y]
if not tmp
or table_empty(tmp) then
return false
end
tmp = vector.meta.nodes[pos.x][pos.y][pos.z]
if not tmp
or table_empty(tmp) then
return false
end
-- if name isn't mentioned, just look if there's a node
if not name then
return true
end
tmp = vector.meta.nodes[pos.x][pos.y][pos.z][name]
if not tmp
or table_empty(tmp) then
return false
end
return tmp
end
function vector.meta.remove_node(pos)
if not pos then
return false
end
if vector.meta.get_node(pos) then
vector.meta.nodes[pos.x][pos.y][pos.z] = nil
local xarr = vector.meta.nodes[pos.x]
if table_empty(xarr[pos.y]) then
vector.meta.nodes[pos.x][pos.y] = nil
end
if table_empty(xarr) then
vector.meta.nodes[pos.x] = nil
end
else
print("[vector_extras] Warning: The node at "..vector.pos_to_string(pos).." wasn't stored in vector.meta.nodes.")
end
end
function vector.meta.set_node(pos, name, meta)
if not (name or pos) then
return false
end
vector.meta.complete_node_table(pos, name)
meta = meta or true
vector.meta.nodes[pos.x][pos.y][pos.z][name] = meta
end
minetest.register_chatcommand('cleanvectormetatable',{
description = 'Tidy up it.',
params = "",
privs = {},
func = function(name)
vector.meta.clean_node_table()
local tmp = vector.meta.nodes_info()
minetest.chat_send_player(name, tmp)
print("[vector_extras] "..tmp)
end
})
vector.meta.nodes_file.load()