From 16a9a079d9590b74d97051153d9822f6ffc65000 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Brada?= Date: Sun, 5 Jul 2015 14:28:58 +0200 Subject: [PATCH] Deduplication, param cleanup. Veins. --- depositgen/init.lua | 7 +++++-- depositgen/pipes.lua | 15 ++++++++------- depositgen/utils.lua | 16 ++++++++++++++++ depositgen/veins.lua | 36 ++++++++++++++++++++++++------------ 4 files changed, 53 insertions(+), 21 deletions(-) create mode 100644 depositgen/utils.lua diff --git a/depositgen/init.lua b/depositgen/init.lua index 83d9147..0f265e5 100644 --- a/depositgen/init.lua +++ b/depositgen/init.lua @@ -15,7 +15,7 @@ l.OnInit={} l.ToResolve={} local includes={ -"pipes","veins" +"utils","pipes","veins" } minetest.register_on_generated(function(minp,maxp,seed) @@ -23,9 +23,12 @@ minetest.register_on_generated(function(minp,maxp,seed) local vm, emin, emax = minetest.get_mapgen_object("voxelmanip") local area = VoxelArea:new{MinEdge=emin, MaxEdge=emax} local pr=PseudoRandom(seed) + local data=vm:get_data() + local mgc={minp=minp,maxp=maxp,pr=pr,data=data,area=area} for i=1, #l.OnGenerate do - l.OnGenerate[i](minp,maxp,pr,vm,area) + l.OnGenerate[i](mgc) end + vm:set_data(data) vm:write_to_map() end) diff --git a/depositgen/pipes.lua b/depositgen/pipes.lua index 40b7f44..d9c9db9 100644 --- a/depositgen/pipes.lua +++ b/depositgen/pipes.lua @@ -44,24 +44,26 @@ end depositgen.l.ToResolve.air={name="air"} -local function generate(minp,maxp,pr,vm,area) +local function generate(mgc) local t1 = os.clock() - local data = vm:get_data() + local data = mgc.data + local area = mgc.area + local pr=mgc.pr + local minp,maxp=mgc.minp,mgc.maxp local chunksizer = maxp.x - minp.x + 1 local chunksize = chunksizer + 1 local pmapsize = {x = chunksize, y = chunksize, z = chunksize} local minpxz = {x = minp.x, y = minp.z} for _,descr in pairs(regs) do - --local orepr=PseudoRandom(pr:next()) local numpipes_raw=(chunksize/descr.scarcity) local numpipes = math.floor(numpipes_raw + (pr:next(0,99)/100)) for vc=1, numpipes do - local pointA=vector.new(pr:next(0,chunksizer)+minp.x,pr:next(0,chunksizer)+minp.y,pr:next(0,chunksizer)+minp.z) - local pointB=vector.new(pr:next(0,chunksizer)+minp.x,pr:next(0,chunksizer)+minp.y,pr:next(0,chunksizer)+minp.z) - local pointC=vector.new(pr:next(0,chunksizer)+minp.x,pr:next(0,chunksizer)+minp.y,pr:next(0,chunksizer)+minp.z) + local pointA=l.rndvector(mgc) + local pointB=l.rndvector(mgc) + local pointC=l.rndvector(mgc) local step=(1.8)/(vector.distance(pointA,pointB)+vector.distance(pointB,pointC)) for t=0, 1, step do @@ -107,7 +109,6 @@ local function generate(minp,maxp,pr,vm,area) end -- - vm:set_data(data) l.print("pipes "..math.ceil((os.clock() - t1) * 1000).." ms ") end diff --git a/depositgen/utils.lua b/depositgen/utils.lua new file mode 100644 index 0000000..25cedd0 --- /dev/null +++ b/depositgen/utils.lua @@ -0,0 +1,16 @@ +function depositgen.l.SpawnCluster(mgc,pos,wherein,size,content,density) + for y=pos.y-size, pos.y+size do + for x=pos.x-size, pos.x+size do + for z=pos.z-size, pos.z+size do + if mgc.pr:next(0,density)==0 then + di=mgc.area:index(x,y,z) + if mgc.data[di]==wherein then + mgc.data[di]=content + end + end + end end end +end +function depositgen.l.rndvector(mgc) + local chunksizer = mgc.maxp.x - mgc.minp.x + 1 + return vector.new(mgc.pr:next(0,chunksizer)+mgc.minp.x,mgc.pr:next(0,chunksizer)+mgc.minp.y,mgc.pr:next(0,chunksizer)+mgc.minp.z) +end \ No newline at end of file diff --git a/depositgen/veins.lua b/depositgen/veins.lua index c847e0c..cd68a47 100644 --- a/depositgen/veins.lua +++ b/depositgen/veins.lua @@ -35,34 +35,34 @@ local sample_vein_def={ scarcity=80, content="default:dirt", scatter={ - --{ scarcity=7, density=4, content="default:mese", size=2 } + { scarcity=7, density=4, content="default:mese", size=2 } } } depositgen.register_vein(sample_vein_def) end -local function generate(minp,maxp,pr,vm,area) +local function generate(mgc) local t1 = os.clock() - local data = vm:get_data() + local data = mgc.data + local area = mgc.area + local pr=mgc.pr - local chunksizer = maxp.x - minp.x + 1 + local chunksizer = mgc.maxp.x - mgc.minp.x + 1 local chunksize = chunksizer + 1 local pmapsize = {x = chunksize, y = chunksize, z = chunksize} - local minpxz = {x = minp.x, y = minp.z} + local minpxz = {x = mgc.minp.x, y = mgc.minp.z} for _,descr in pairs(regs) do local num = math.floor( (chunksize/descr.scarcity) + (pr:next(0,99)/100) ) for vc=1, num do - local A=vector.new(pr:next(0,chunksizer)+minp.x,pr:next(0,chunksizer)+minp.y,pr:next(0,chunksizer)+minp.z) - local B=vector.new(pr:next(0,chunksizer)+minp.x,pr:next(0,chunksizer)+minp.y,pr:next(0,chunksizer)+minp.z) - local C=vector.new(pr:next(0,chunksizer)+minp.x,pr:next(0,chunksizer)+minp.y,pr:next(0,chunksizer)+minp.z) - local D=vector.new(pr:next(0,chunksizer)+minp.x,pr:next(0,chunksizer)+minp.y,pr:next(0,chunksizer)+minp.z) + local A=l.rndvector(mgc) + local B=l.rndvector(mgc) + local C=l.rndvector(mgc) + local D=l.rndvector(mgc) local l1=vector.distance(A,C)+vector.distance(C,B) local l2=vector.distance(A,D)+vector.distance(D,B) local step=2/math.max(l1,l2) - local scarcity=6 - local ocn=pr:next(0,scarcity)+(scarcity/2) for t=0, 1, step do local P=vector.multiply(A,(1-t)^2) P=vector.add(P, vector.multiply(B,t*t) ) @@ -79,10 +79,22 @@ local function generate(minp,maxp,pr,vm,area) -- end end + -- + local cluster=descr.scatter[1] + for xxx=1, 5 do + local t=pr:next(0,100)/100 + local u=pr:next(0,100)/100 + local P=vector.multiply(A,(1-t)^2) + P=vector.add(P, vector.multiply(B,t*t) ) + local Q=vector.add(P, vector.multiply(D,2*t*(1-t)) ) + P=vector.add(P, vector.multiply(C,2*t*(1-t)) ) + local R=vector.add(vector.multiply(P,(1-u)), vector.multiply(Q,u) ) + l.SpawnCluster(mgc,vector.round(R),l.ToResolve.air.id,cluster.size,cluster.content.id,cluster.density) + end + -- end end - vm:set_data(data) print("vein "..math.ceil((os.clock() - t1) * 1000).." ms ") end