]> git.lizzy.rs Git - elidragon_v2.git/blob - mods/elidragon_plot/init.lua
2cb33e3751827a90e3f771382469c91f9e7fa1d7
[elidragon_v2.git] / mods / elidragon_plot / init.lua
1 local plot = {}
2
3 minetest.register_on_generated(function(minp, maxp)
4         local config = assert(plot.config)
5         
6         local mgconfig = config.mapgen
7         
8         if not mgconfig then
9                 return
10         end
11
12         local min_y, max_y = mgconfig.min_y, mgconfig.max_y
13
14         if maxp.y < min_y or minp.y > max_y then
15                 return
16         end
17
18         local vm, emin, emax = minetest.get_mapgen_object("voxelmanip")
19         local data = vm:get_data()
20         local area = VoxelArea:new({MinEdge = emin, MaxEdge = emax})
21
22         local void_layer = mgconfig.void_layer
23
24         if void_layer then
25                 for idx in area:iter(minp.x, math.max(minp.y, void_layer.min_y), minp.z, maxp.x, math.min(maxp.y, void_layer.max_y), mapx.z) do
26                         data[idx] = void_layer.c_void
27                 end
28         end
29
30         local function do_multiples(low, high, base, add, func)
31                 for p = math.ceil(low / base), math.floor(high / base) do
32                         func(p * base + add)
33                 end
34         end
35
36         local function do_borders(low, high, base, road, func)
37                 local r = road / 2
38                 do_multiples(low - r, high - r, base,  r, func)
39                 do_multiples(low + r, high + r, base, -r, func)
40         end
41
42         do_borders(minp.x, maxp.x, config.gap, config.road_width, function(x)
43                 for idx in area:iter(x, math.max(minp.y, min_y), minp.z, x, math.min(maxp.y, max_y), maxp.z) do
44                         data[idx] = mgconfig.c_border
45                 end
46         end)
47
48         do_borders(minp.z, maxp.z, config.gap, config.road_width, function(z)
49                 for idx in area:iter(minp.x, math.max(minp.y, min_y), z, maxp.x, math.min(maxp.y, max_y), z) do
50                         data[idx] = mgconfig.c_border
51                 end
52         end)
53
54         vm:set_data(data)
55         vm:calc_lighting()
56         vm:update_liquids()
57         vm:write_to_map()
58 end)
59
60 elidragon.plot = plot