First commit

master
Pierre-Yves Rollo 2019-04-16 16:29:32 +02:00
commit 0f30756239
6 changed files with 281 additions and 0 deletions

1
depends.txt Normal file
View File

@ -0,0 +1 @@
default

49
emerge.lua Normal file
View File

@ -0,0 +1,49 @@
--[[
FFIopt Demo - A demo mod for FFIopt Minetest mod
(c) Pierre-Yves Rollo
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published
by the Free Software Foundation, either version 2.1 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
--]]
local state = {}
local function emerge_callback(blockpos, action, calls_remaining, id)
if state[id].calls == nil then
state[id].calls = calls_remaining
end
if calls_remaining == 0 then
state[id] = nil
minetest.chat_send_all("Emerging done")
ffiopt_demo.emerged = true
else
if state[id].time ~= os.time() then
state[id].time = os.time()
minetest.chat_send_all(string.format("Emerging (%2d%%)", 100-100*calls_remaining / state[id].calls))
end
end
end
minetest.register_chatcommand("emerge", {
params = "",
description = "emerge map",
func = function()
local id = 1
while state[id] do
id = id + 1
end
state[id] = { time = os.time() - 1 }
minetest.emerge_area(ffiopt_demo.minp, ffiopt_demo.maxp, emerge_callback, id)
end
})

35
init.lua Normal file
View File

@ -0,0 +1,35 @@
--[[
FFIopt Demo - A demo mod for FFIopt Minetest mod
(c) Pierre-Yves Rollo
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published
by the Free Software Foundation, either version 2.1 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
--]]
ffiopt_demo = {}
ffiopt_demo.name = minetest.get_current_modname()
ffiopt_demo.path = minetest.get_modpath(ffiopt_demo.name)
ffiopt_demo.log = function(message)
minetest.chat_send_all(message)
minetest.log("action", "[ffiopt_demo] "..message)
end
-- A large area for voxelmanip demos
ffiopt_demo.minp = { x=-200, y=-200, z=-200 }
ffiopt_demo.maxp = { x=200, y=200, z=200 }
dofile(ffiopt_demo.path..'/emerge.lua')
dofile(ffiopt_demo.path..'/profile.lua')
dofile(ffiopt_demo.path..'/vmanipdemo.lua')
dofile(ffiopt_demo.path..'/noisedemo.lua')

67
noisedemo.lua Normal file
View File

@ -0,0 +1,67 @@
--[[
FFIopt Demo - A demo mod for FFIopt Minetest mod
(c) Pierre-Yves Rollo
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published
by the Free Software Foundation, either version 2.1 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
--]]
local log, profile = ffiopt_demo.log, ffiopt_demo.profile
local minp, maxp = ffiopt_demo.minp, ffiopt_demo.maxp
minetest.register_chatcommand("pn", {
params = "",
description = "PerlinNoise demo",
func = function()
log('Starting PerlinNoise demo')
log(string.format('Volume : %s to %s', minetest.pos_to_string(minp), minetest.pos_to_string(maxp)))
profile.init()
profile.start("total")
local params = {
offset = 0,
scale = 1,
spread = {x=2048, y=2048, z=2048},
seed = 1337,
octaves = 6,
persist = 0.6
}
local map_lengths_xyz = {x=maxp.x - minp.x + 1, y=maxp.y - minp.y + 1, z=maxp.z - minp.z + 1}
profile.start("get_perlin_map")
local perlin_map = minetest.get_perlin_map(params, map_lengths_xyz)
profile.stop("get_perlin_map")
profile.start("get3dMap_flat")
local flatmap = perlin_map:get3dMap_flat(minp)
profile.stop("get3dMap_flat")
profile.start("lua processing")
local perlin_index = 1
local histogram = {}
for z = minp.z, maxp.z do
for y = minp.y, maxp.y do
for x = minp.x, maxp.x do
-- Compute an histogram for fun
local histoix = math.floor(flatmap[perlin_index]*10)
histogram[histoix] = 1 + (histogram[histoix] or 0)
perlin_index = perlin_index + 1
end
end
end
profile.stop("lua processing")
profile.stop("total")
log('Ending VoxelManip demo')
profile.show(log)
end
})

49
profile.lua Normal file
View File

@ -0,0 +1,49 @@
--[[
FFIopt Demo - A demo mod for FFIopt Minetest mod
(c) Pierre-Yves Rollo
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published
by the Free Software Foundation, either version 2.1 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
--]]
local profile={}
-- Profiling
local counters={}
function profile.init()
counters = {}
end
function profile.start(counter)
if counters[counter] == nil then
counters[counter] = { start = nil, total = 0 }
end
counters[counter].start = os.clock()
end
function profile.stop(counter)
counters[counter].total = counters[counter].total +
os.clock() - counters[counter].start
counters[counter].start = nil
end
function profile.show(logfct)
logfct = logfct or print
for name, counter in pairs(counters) do
logfct(name..": "..string.format("%.2fms",counter.total*1000))
end
end
ffiopt_demo.profile = profile

80
vmanipdemo.lua Normal file
View File

@ -0,0 +1,80 @@
--[[
FFIopt Demo - A demo mod for FFIopt Minetest mod
(c) Pierre-Yves Rollo
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published
by the Free Software Foundation, either version 2.1 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
--]]
local log, profile = ffiopt_demo.log, ffiopt_demo.profile
local minp, maxp = ffiopt_demo.minp, ffiopt_demo.maxp
local c_stone = minetest.get_content_id("default:stone")
local c_glass = minetest.get_content_id("default:glass")
minetest.register_chatcommand("vm", {
params = "",
description = "VoxelManip demo",
func = function()
if not ffiopt_demo.emerged then
return false, "Map not emerged, please issue /emerge first"
end
local histogram = {}
log('Starting VoxelManip demo')
log(string.format('Volume : %s to %s', minetest.pos_to_string(minp), minetest.pos_to_string(maxp)))
profile.init()
profile.start("total")
local vm = minetest.get_voxel_manip()
profile.start("read_from_map")
local emin, emax = vm:read_from_map(minp, maxp)
profile.stop("read_from_map")
local area = VoxelArea:new{MinEdge=emin, MaxEdge=emax}
profile.start("get_data")
local data = vm:get_data()
profile.stop("get_data")
profile.start("lua processing")
-- Make some data processing on vmanip
for z = minp.z, maxp.z do
for y = minp.y, maxp.y do
local vmix = area:index(minp.x, y, z)
for x = minp.x, maxp.x do
-- Compute an histogram for fun
histogram[data[vmix]] = 1 + (histogram[data[vmix]] or 0)
vmix = vmix + 1
-- Do something visual : replace stone with glass and inversely
if data[vmix] == c_stone then
data[vmix] = c_glass
elseif data[vmix] == c_glass then
data[vmix] = c_stone
end
end
end
end
profile.stop("lua processing")
profile.start("set_data")
vm:set_data(data)
profile.stop("set_data")
profile.start("write_to_map")
vm:write_to_map(false)
profile.stop("write_to_map")
profile.stop("total")
log('Ending VoxelManip demo')
profile.show(log)
end
})