save settlements to file and check distance between settlements
parent
b3d233bf75
commit
7ff6f60a4c
|
@ -50,7 +50,10 @@ function settlements.place_settlement_circle(minp, maxp)
|
|||
local center_surface = settlements.find_surface(center)
|
||||
-- go build settlement around center
|
||||
if center_surface then
|
||||
last_time = os.time() + 30
|
||||
-- add settlement to list
|
||||
table.insert(settlements_in_world, center_surface)
|
||||
-- save list to file
|
||||
settlements.save()
|
||||
-- initialize all settlement information
|
||||
settlements.initialize_settlement()
|
||||
-- build well in the center
|
||||
|
|
|
@ -24,4 +24,5 @@ surface_mat = {
|
|||
above_surface_mat = {"default:air","default:dirt_with_snow"}
|
||||
under_surface_mat = {"default:stone","default:dirt"}
|
||||
schem_path = settlements.modpath.."/schematics/"
|
||||
settlement_info = {}
|
||||
settlement_info = {}
|
||||
settlements_in_world = {}
|
36
init.lua
36
init.lua
|
@ -8,37 +8,27 @@ settlements.modpath = minetest.get_modpath("settlements");
|
|||
dofile(settlements.modpath.."/const.lua")
|
||||
dofile(settlements.modpath.."/utils.lua")
|
||||
dofile(settlements.modpath.."/foundation.lua")
|
||||
--dofile(settlements.modpath.."/doors.lua")
|
||||
dofile(settlements.modpath.."/buildings.lua")
|
||||
-- load settlements on server
|
||||
settlements_in_world = settlements.load()
|
||||
|
||||
|
||||
local function place_settlement(minp, maxp)
|
||||
-- wait xx seconds until building a new settlement
|
||||
last_time = os.time() + 30
|
||||
-- find locations for buildings
|
||||
local location_list = settlements.find_locations(minp, maxp)
|
||||
if location_list then
|
||||
minetest.chat_send_all("Dorf")
|
||||
-- for each location, build something
|
||||
for i, mpos in ipairs(location_list) do
|
||||
minetest.after(0.5, function()
|
||||
settlements.build_schematic(mpos)
|
||||
end)
|
||||
end
|
||||
end
|
||||
end
|
||||
--
|
||||
-- on map generation, try to build a settlement
|
||||
--
|
||||
minetest.register_on_generated(function(minp, maxp, seed)
|
||||
if maxp.y < 0 then
|
||||
return
|
||||
end
|
||||
if math.random(0,10)<9 or os.time() < last_time then
|
||||
minetest.register_on_generated(function(minp, maxp)
|
||||
if maxp.y < 0 then
|
||||
return
|
||||
end
|
||||
if math.random(0,10)<9 then
|
||||
-- check if too close to other settlements
|
||||
local center_of_chunk = {x=maxp.x-40, y=maxp.y-40, z=maxp.z-40}
|
||||
local dist_ok = settlements.check_distance_other_settlements(center_of_chunk)
|
||||
if dist_ok == false then
|
||||
return
|
||||
end
|
||||
-- place_settlement(minp, maxp)
|
||||
settlements.place_settlement_circle(minp, maxp)
|
||||
settlements.place_settlement_circle(minp, maxp)
|
||||
end
|
||||
end)
|
||||
|
||||
--
|
||||
|
|
68
utils.lua
68
utils.lua
|
@ -52,50 +52,32 @@ function settlements.check_distance(building_pos, building_size)
|
|||
return true
|
||||
end
|
||||
--
|
||||
-- Function to find random positions
|
||||
-- returns array with coords where houses are built
|
||||
--
|
||||
function settlements.find_locations(minp, maxp)
|
||||
-- Anzahl Gebäude
|
||||
local amount_of_buildings = 10 --math.random(5,10)
|
||||
local location_list = {}
|
||||
-- Mindest und maxi Abstand
|
||||
local radius = 1000
|
||||
local housemindist = 7
|
||||
local housemaxdist = 1000
|
||||
local centeroftown -- Erste location ist Mittelpunkt des Dorfes
|
||||
local tries = 500 -- 500 Versuche, ne geeignete Location zu finden
|
||||
local count = 0
|
||||
--
|
||||
for i = 1,amount_of_buildings do
|
||||
-- Zufallslocation finden
|
||||
::neuerversuch:: -- Sprungpunkt, falls Abstand nicht passt
|
||||
count = count + 1
|
||||
-- nicht unendlich oft probieren, sonst endlos schleife
|
||||
if count > tries then return nil end
|
||||
local tpos = {x=math.random(minp.x,maxp.x), y=math.random(minp.y,maxp.y), z=math.random(minp.z,maxp.z)}
|
||||
if tpos.y < 0 then goto neuerversuch end
|
||||
local mpos = settlements.find_surface(tpos)
|
||||
if not mpos or mpos == nil or mpos.y < 0 then goto neuerversuch end
|
||||
function settlements.save()
|
||||
local file = io.open(minetest.get_worldpath().."/settlements.txt", "w")
|
||||
if file then
|
||||
file:write(minetest.serialize(settlements_in_world))
|
||||
file:close()
|
||||
end
|
||||
end
|
||||
|
||||
-- vor dem Ablegen in die Liste, Abstand zu bisherigen locations finden, sobald mehr als eine location gefunden wurde
|
||||
if i > 1 then
|
||||
-- bisherige Liste durchgehen und mit aktueller mpos vergleichen
|
||||
for j, saved_location in ipairs(location_list) do
|
||||
local distanceToCenter = math.sqrt(((centeroftown.x - mpos.x)*(centeroftown.x - mpos.x))+((centeroftown.y - mpos.y)*(centeroftown.y - mpos.y)))
|
||||
local distanceTohouses = math.sqrt(((saved_location.x - mpos.x)*(saved_location.x - mpos.x))+((saved_location.y - mpos.y)*(saved_location.y - mpos.y)))
|
||||
|
||||
-- nicht weiter als
|
||||
-- if distanceToCenter > radius or distanceTohouses < housemindist or distanceTohouses > housemaxdist then
|
||||
if distanceTohouses < housemindist then
|
||||
goto neuerversuch
|
||||
end
|
||||
end
|
||||
location_list[i] = mpos
|
||||
else
|
||||
location_list[i] = mpos
|
||||
centeroftown = mpos
|
||||
function settlements.load()
|
||||
local file = io.open(minetest.get_worldpath().."/settlements.txt", "r")
|
||||
if file then
|
||||
local table = minetest.deserialize(file:read("*all"))
|
||||
if type(table) == "table" then
|
||||
return table
|
||||
end
|
||||
end
|
||||
return location_list
|
||||
return {}
|
||||
end
|
||||
|
||||
function settlements.check_distance_other_settlements(center_new_chunk)
|
||||
local min_dist_settlements = 1000
|
||||
for i, pos in ipairs(settlements_in_world) do
|
||||
local distance = vector.distance(center_new_chunk, pos)
|
||||
if distance < min_dist_settlements then
|
||||
return false
|
||||
end
|
||||
end
|
||||
return true
|
||||
end
|
Loading…
Reference in New Issue