mg-cd2025/villages.lua

103 lines
2.8 KiB
Lua
Raw Normal View History

2013-09-21 15:07:43 +02:00
function village_at_point(minp)
local bseed
for xi = -2, 2 do
for zi = -2, 0 do
if xi~=0 or zi~=0 then
local pi = PseudoRandom(get_bseed({x=minp.x+80*xi, z=minp.z+80*zi}))
if pi:next(1,400)<=10 then return 0,0,0,0 end
end
end
end
local pr = PseudoRandom(get_bseed(minp))
if pr:next(1,400)>10 then return 0,0,0,0 end
local x = pr:next(minp.x, minp.x+79)
local z = pr:next(minp.z, minp.z+79)
local size = pr:next(20, 40)
local height = pr:next(5, 20)
print("A village spawned at: x="..x..", z="..z)
return x,z,size,height
end
local function dist_center2(ax, bsizex, az, bsizez)
return math.max((ax+bsizex)*(ax+bsizex),(ax-bsizex)*(ax-bsizex))+math.max((az+bsizez)*(az+bsizez),(az-bsizez)*(az-bsizez))
end
local function generate_bpos(vx, vz, vs, vh, pr)
local l={}
for i=1, 100 do
bx = pr:next(vx-vs, vx+vs)
bz = pr:next(vz-vs, vz+vs)
2013-09-21 19:16:04 +02:00
::choose::
btype = pr:next(1, #buildings)
2013-09-21 19:16:04 +02:00
if buildings[btype].chance ~= nil then
if pr:next(1, buildings[btype].chance) ~= 1 then
goto choose
end
end
if buildings[btype].pervillage ~= nil then
local n = 0
for j=1, #l do
if l[j].btype == btype then
n = n + 1
end
end
if n >= buildings[btype].pervillage then
goto choose
end
end
2013-09-21 15:07:43 +02:00
bsizex = buildings[btype].sizex
bsizez = buildings[btype].sizez
if dist_center2(bx-vx, bsizex, bz-vz, bsizez)>vs*vs then goto out end
for _, a in ipairs(l) do
if math.abs(bx-a.x)<=(bsizex+a.bsizex)/2 and math.abs(bz-a.z)<=(bsizez+a.bsizez)/2 then goto out end
2013-09-21 15:07:43 +02:00
end
l[#l+1] = {x=bx, y=vh, z=bz, btype=btype, bsizex=bsizex, bsizez=bsizez}
::out::
end
return l
end
local function generate_building(pos, minp, maxp, data, a, pr, extranodes)
2013-09-21 15:07:43 +02:00
local binfo = buildings[pos.btype]
2013-09-21 20:46:18 +02:00
if binfo.no_rotate == nil then
rot = pr:next(1, 2)
if rot == 1 then
pfunc = function(x,y,z) return x,y,z end
elseif rot == 2 then
pfunc = function(x,y,z) return z,y,x end
end
else
pfunc = function(x,y,z) return x,y,z end
end
2013-09-21 15:07:43 +02:00
local scm = binfo.scm
2013-09-21 20:46:18 +02:00
local xx, yy, zz
local t
for x = 0, pos.bsizex-1 do
for y = 0, binfo.ysize-1 do
for z = 0, pos.bsizez-1 do
ax, ay, az = pos.x+x, pos.y+y+binfo.yoff, pos.z+z
if (ax > minp.x and ax < maxp.x) and (ay > minp.y and ay < maxp.y) and (az > minp.z and az < maxp.z) then
xx, yy, zz = pfunc(x+1, y+1, z+1)
t = scm[yy][xx][zz]
if type(t) == "table" then
table.insert(extranodes, {node=t.node, meta=t.meta, pos={x=ax, y=ay, z=az},})
else
data[a:index(ax, ay, az)] = t
end
2013-09-21 19:16:04 +02:00
end
2013-09-21 15:07:43 +02:00
end
end
end
end
function generate_village(vx, vz, vs, vh, minp, maxp, data, a)
local seed = get_bseed({x=vx, z=vz})
local pr = PseudoRandom(seed)
local bpos = generate_bpos(vx, vz, vs, vh, pr)
local extranodes = {}
2013-09-21 15:07:43 +02:00
for _, pos in ipairs(bpos) do
generate_building(pos, minp, maxp, data, a, pr, extranodes)
2013-09-21 15:07:43 +02:00
end
return extranodes
2013-09-21 15:07:43 +02:00
end