Advertisement
krock186

Minetest FFI mapgen

Jan 3rd, 2018
238
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.66 KB | None | 0 0
  1. diff --git a/src/script/lua_api/l_vmanip.cpp b/src/script/lua_api/l_vmanip.cpp
  2. index ca837b6..abf3353 100644
  3. --- a/src/script/lua_api/l_vmanip.cpp
  4. +++ b/src/script/lua_api/l_vmanip.cpp
  5. @@ -30,6 +30,14 @@ with this program; if not, write to the Free Software Foundation, Inc.,
  6.  #include "mapgen/mapgen.h"
  7.  #include "voxelalgorithms.h"
  8.  
  9. +#if USE_LUAJIT
  10. +extern "C" void VoxelManip_get(void **lvmp, MapNode **data)
  11. +{
  12. +   MMVManip *vm = (*(LuaVoxelManip **)lvmp)->vm;
  13. +   *data = vm->m_data;
  14. +}
  15. +#endif
  16. +
  17.  // garbage collector
  18.  int LuaVoxelManip::gc_object(lua_State *L)
  19.  {
  20.  
  21. ---------------------------------------------------------
  22. -- File: testmod/init.lua (added to secure.trusted_mods)
  23.  
  24. local ie = minetest.request_insecure_environment()
  25. local ffi = ie.require("ffi")
  26. ffi.cdef[[
  27. typedef struct {
  28.     uint16_t id;
  29.     uint8_t param1;
  30.     uint8_t param2;
  31. } MapNode;
  32. void VoxelManip_get(void **vm, MapNode **data);
  33. ]]
  34.  
  35. local c_air = minetest.get_content_id("air")
  36. local c_ice = minetest.get_content_id("default:ice")
  37. local avg = 0
  38.  
  39. function generate_this_stuff(minp, maxp, seed)
  40.     local uffi = false
  41.  
  42.     local t1 = os.clock()
  43.     local sidelen1d = maxp.x - minp.x + 1
  44.     local sidelen3d = {x=sidelen1d, y=sidelen1d, z=sidelen1d}
  45.  
  46.     local vm, emin, emax = minetest.get_mapgen_object("voxelmanip")
  47.     local area = VoxelArea:new{MinEdge=emin, MaxEdge=emax}
  48.     local data
  49.  
  50.     if uffi then
  51.         local __data = ffi.new("MapNode*[0]")
  52.         ffi.C.VoxelManip_get(vm, __data)
  53.         data = __data[0]
  54.     else
  55.         data = vm:get_data()
  56.     end
  57.  
  58.     local nvals_caves = minetest.get_perlin_map({
  59.         offset = 0,
  60.         scale = 1,
  61.         spread = {x=64, y=64, z=64},
  62.         octaves = 4,
  63.         seed = -11842,
  64.         persist = 0.4,
  65.         flags = "eased"
  66.     }, sidelen3d):get3dMap_flat(minp)
  67.  
  68.     local nixyz = 1
  69.     for z = minp.z, maxp.z do
  70.     for y = minp.y, maxp.y do
  71.         local vi = area:index(minp.x, y, z)
  72.         for x = minp.x, maxp.x do
  73.             local cave = nvals_caves[nixyz] + math.max(-1, y / 32)
  74.  
  75.             if uffi then
  76.                 if cave < 0 then
  77.                     data[vi].id = c_ice
  78.                 else
  79.                     data[vi].id = c_air
  80.                 end
  81.                 data[vi].param2 = 0
  82.             else
  83.                 if cave < 0 then
  84.                     data[vi] = c_ice
  85.                 else
  86.                     data[vi] = c_air
  87.                 end
  88.             end
  89.  
  90.             nixyz = nixyz + 1
  91.             vi = vi + 1
  92.         end
  93.     end
  94.     end
  95.  
  96.     if not uffi then
  97.         vm:set_data(data)
  98.         vm:set_lighting({day=0, night=0})
  99.     end
  100.  
  101.     vm:calc_lighting()
  102.     vm:write_to_map()
  103.     vm:update_liquids()
  104.     local dtime = (os.clock() - t1) * 1000
  105.     avg = (avg == 0) and dtime or (avg * 25 + dtime) / 26
  106.    
  107.  
  108.     minetest.log("action", "this = " ..
  109.         math.ceil(dtime) .. " ms, avg = " ..
  110.         math.ceil(avg) .. " ms")
  111. end
  112.  
  113. minetest.after(0, table.insert,
  114.     minetest.registered_on_generateds, 1, generate_this_stuff)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement