mg-cd2025/villages.lua

68 lines
2.1 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)
btype = pr:next(1, #buildings)
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
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]
local scm = binfo.scm
local minx = pos.x-pos.bsizex-1
local miny = pos.y+binfo.ymin-1
local minz = pos.z-pos.bsizez-1
for x = math.max(pos.x-pos.bsizex, minp.x), math.min(pos.x+pos.bsizex, maxp.x) do
for y = math.max(pos.y+binfo.ymin, minp.y), math.min(pos.y+binfo.ymax, maxp.y) do
for z = math.max(pos.z-pos.bsizez, minp.z), math.min(pos.z+pos.bsizez, maxp.z) do
data[a:index(x,y,z)] = scm[y-miny][x-minx][z-minz]
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