Putising some igneous stuff from sedimentaary.
[rocks.git] / ign.lua
blob4761bba7f2177e22711caff3e4b7b7e34a28d109
1 --
2 -- Igneous Layer
3 --
5 local layer={
6 top={
7 offset = -13, scale = 0,
8 spread = {x=80, y=80, z=80},
9 octaves = 0, persist = 0 },
10 bot={
11 offset = -180, scale = 10, seed=100,
12 spread = {x=80, y=80, z=80},
13 octaves = 2, persist = 0.7 },
14 primary={ name="rocks:basalt" },
15 localized={},
16 seedseq=100,
17 stats={ count=0, total=0, node={}, totalnodes=0 }
20 -- more rock defs
23 local reg=function(name,param)
24 layer.localized[name]={
25 spread=(param.spread or 20),
26 height=(param.height or 15),
27 treshold=(param.treshold or 0.85),
28 secondary=(param.secondary),
29 seed=seedseq,
31 layer.stats.node[name]=0
32 layer.seedseq=layer.seedseq+1
33 end
34 rocks.register_igneous=reg
36 -- rock registration
37 --eg: reg("default:stone_with_coal", { spread=48, height=14, treshold=0.45 })
39 minetest.register_on_generated(function(minp, maxp, seed)
40 if ( (layer.top.offset+layer.top.scale)>minp.y )
41 and ( (layer.bot.offset-layer.bot.scale)<maxp.y )
42 then
43 stone_ctx= minetest.get_content_id("default:stone")
44 air_ctx= minetest.get_content_id("air")
45 dirt_ctx= minetest.get_content_id("default:dirt")
46 layer.primary.ctx= minetest.get_content_id(layer.primary.name)
47 local timebefore=os.clock();
48 local manipulator, emin, emax = minetest.get_mapgen_object("voxelmanip")
49 local nodes = manipulator:get_data()
50 local area = VoxelArea:new{MinEdge=emin, MaxEdge=emax}
51 local side_length = (maxp.x - minp.x) + 1
52 local map_lengths_xyz = {x=side_length, y=side_length, z=side_length}
53 -- noises:
54 local bottom=minetest.get_perlin_map(layer.bot,map_lengths_xyz):get2dMap_flat({x=minp.x, y=minp.z})
55 local localized={}
56 for name,loc in pairs(layer.localized) do
57 --defaults and overrides
58 local np={ offset = 0, scale = 1, octaves = 1, persist = 0.7,
59 spread = {x=loc.spread, y=loc.height, z=loc.spread} }
60 --get noise and content ids
61 table.insert(localized,
63 noise=minetest.get_perlin_map(np,map_lengths_xyz):get3dMap_flat(minp),
64 treshold=loc.treshold,
65 ctx= minetest.get_content_id(name),
66 ndn=name
68 end
69 local noise2d_ix = 1
70 local noise3d_ix = 1
71 print("[rocks] igneous gen1 "..os.clock()-timebefore)
72 for z=minp.z,maxp.z,1 do
73 for y=minp.y,maxp.y,1 do
74 for x=minp.x,maxp.x,1 do
75 local pos = area:index(x, y, z)
76 if (y>bottom[noise2d_ix])
77 and ((nodes[pos]==stone_ctx) or (nodes[pos]==dirt_ctx))
78 then
79 layer.stats.totalnodes=layer.stats.totalnodes+1
80 if nodes[pos]==stone_ctx then nodes[pos] = air_ctx end --layer.primary.ctx
81 for k,loc in pairs(localized) do
82 if ( loc.noise[noise3d_ix] > loc.treshold) then
83 nodes[pos]=loc.ctx
84 layer.stats.node[loc.ndn]=layer.stats.node[loc.ndn]+1
85 end
86 end
87 end
88 noise2d_ix=noise2d_ix+1
89 noise3d_ix=noise3d_ix+1
90 end
91 noise2d_ix=noise2d_ix-side_length
92 end
93 noise2d_ix=noise2d_ix+side_length
94 end
95 manipulator:set_data(nodes)
96 --manipulator:calc_lighting()
97 manipulator:set_lighting({day=15,night=15})
98 --manipulator:update_liquids()
99 manipulator:write_to_map()
100 print("[rocks] igneous gen2 "..os.clock()-timebefore)
101 layer.stats.count=layer.stats.count+1
102 layer.stats.total=layer.stats.total+(os.clock()-timebefore)
103 layer.stats.side=side_length
105 end)
107 minetest.register_on_shutdown(function()
108 print("[rocks](ign) on_shutdown: generated total "..layer.stats.count.." chunks in "..layer.stats.total.." seconds ("..(layer.stats.total/layer.stats.count).." seconds per "..layer.stats.side.."^3 chunk)")
109 for name,total in pairs(layer.stats.node) do
110 print("[rocks](ign) "..name..": "..total.." nodes placed ("..(total*100)/(layer.stats.count*layer.stats.totalnodes).." %)")
112 end)
114 -- ~ Tomas Brod