Experiments with layers.
[rocks.git] / rocks / pipes.lua
blobde76777ed55b7b103554f0eea9c6f6419b2cee9f
1 -- experimental slow pipe generator
3 local function brush(data,area,pos,radius,content,ores,pr)
4 local rsq=radius^2
5 local orect
6 local oresc
7 radius=radius+2
8 for _,ore in pairs(ores) do
9 if pr:next(0,ore.scarcity)==0 then
10 orect=ore.c_ore
11 oresc=ore.density
12 end
13 end
14 for x=-radius, radius do
15 for y=-radius, radius do
16 for z=-radius, radius do
17 if (x^2)+(y^2)+(z^2)<=rsq then
18 local di=area:index(x+pos.x,y+pos.y,z+pos.z)
19 if oresc and (pr:next(0,oresc)==0) then
20 data[di]=orect
21 else
22 data[di]=content
23 end
24 end
25 end end end
26 end
28 -- the public table of registered pipes
29 rocks.pipes={}
30 local examplepipe={
31 bedrock={ "rocks:limestone" },
32 startrock={ "rocks:limestone" },
33 ymin=-200, ymax=-6,
34 scarcity=80,
35 radius=3,
36 content="default:wood",
37 scatter=
39 { scarcity=7, density=4, ore="default:mese", cnt=0},
42 table.insert(rocks.pipes,examplepipe)
43 --profiling
44 table.insert(rocks.pipes,{
45 bedrock={ "rocks:limestone" },
46 startrock={ "rocks:limestone" },
47 ymin=-200, ymax=-6,
48 scarcity=80,
49 radius=3,
50 content="default:dirt",
51 scatter=
53 { scarcity=5, density=4, ore="default:mese", cnt=0},
57 rocksl.genpipe=function(minp,maxp,pr,vm,area,descr)
58 local t1 = os.clock()
59 local data = vm:get_data()
61 local chunksizer = maxp.x - minp.x + 1
62 local chunksize = chunksizer + 1
63 local pmapsize = {x = chunksize, y = chunksize, z = chunksize}
64 local minpxz = {x = minp.x, y = minp.z}
66 local bedrocks={}
67 for _,node in pairs(descr.bedrock) do bedrocks[minetest.get_content_id(node)]=true end
68 local startrocks={}
69 for _,node in pairs(descr.startrock) do startrocks[minetest.get_content_id(node)]=true end
70 local content=minetest.get_content_id(descr.content)
71 for _,des in pairs(descr.scatter) do
72 des.c_ore=minetest.get_content_id(des.ore)
73 end
74 local orepr=PseudoRandom(pr:next())
76 local numpipes_raw=(chunksize/descr.scarcity)
77 local numpipes = math.floor(numpipes_raw + (pr:next(0,99)/100))
79 for vc=1, numpipes do
80 local pointA=vector.new(pr:next(0,chunksizer)+minp.x,pr:next(0,chunksizer)+minp.y,pr:next(0,chunksizer)+minp.z)
81 if (#startrocks>0)and(startrocks[data[area:indexp(pointA)]]==nil) then break end
82 local pointB=vector.new(pr:next(0,chunksizer)+minp.x,pr:next(0,chunksizer)+minp.y,pr:next(0,chunksizer)+minp.z)
83 local pointC=vector.new(pr:next(0,chunksizer)+minp.x,pr:next(0,chunksizer)+minp.y,pr:next(0,chunksizer)+minp.z)
84 local step=(1.8*descr.radius)/(vector.distance(pointA,pointB)+vector.distance(pointB,pointC))
85 for t=0, 1, step do
86 local p=vector.multiply(pointA,(1-t)^2)
87 p=vector.add(p, vector.multiply(pointB,2*t*(1-t)) )
88 p=vector.add(p, vector.multiply(pointC,t*t) )
89 p=vector.round(p)
90 brush(data,area,p,descr.radius,content,descr.scatter,orepr)
91 end
92 end
94 vm:set_data(data)
95 minetest.log("action", "rocks/genpipe/ "..math.ceil((os.clock() - t1) * 1000).." ms ")
96 end