bugfix when checking distance between buildings (and/or problem)
parent
17e9c4bcf8
commit
e9fc3feb77
|
@ -17,14 +17,18 @@ function settlements.build_schematic(pos, building, replace_wall, name)
|
|||
-- pick random material
|
||||
local material = wallmaterial[math.random(1,#wallmaterial)]
|
||||
-- schematic conversion to lua
|
||||
local schem_lua = minetest.serialize_schematic(building, "lua", {lua_use_comments = false, lua_num_indent_spaces = 0}).." return(schematic)"
|
||||
local schem_lua = minetest.serialize_schematic(building,
|
||||
"lua",
|
||||
{lua_use_comments = false, lua_num_indent_spaces = 0}).." return(schematic)"
|
||||
-- replace material
|
||||
if replace_wall == "y" then
|
||||
schem_lua = schem_lua:gsub("default:cobble", material)
|
||||
end
|
||||
schem_lua = schem_lua:gsub("default:dirt_with_grass", balcony_material)
|
||||
schem_lua = schem_lua:gsub("default:dirt_with_grass",
|
||||
balcony_material)
|
||||
-- special material for spawning npcs
|
||||
schem_lua = schem_lua:gsub("default:junglewood", "settlements:junglewood")
|
||||
schem_lua = schem_lua:gsub("default:junglewood",
|
||||
"settlements:junglewood")
|
||||
-- format schematic string
|
||||
local schematic = loadstring(schem_lua)()
|
||||
-- build foundation for the building an make room above
|
||||
|
@ -33,12 +37,20 @@ function settlements.build_schematic(pos, building, replace_wall, name)
|
|||
local height = schematic["size"]["y"]
|
||||
local possible_rotations = {"0", "90", "180", "270"}
|
||||
local rotation = possible_rotations[ math.random( #possible_rotations ) ]
|
||||
settlements.foundation(pos, width, depth, height, rotation)
|
||||
settlements.foundation(pos,
|
||||
width,
|
||||
depth,
|
||||
height,
|
||||
rotation)
|
||||
-- place schematic
|
||||
minetest.after(4, function()
|
||||
minetest.place_schematic(pos, schematic, rotation, nil, true)
|
||||
minetest.place_schematic(pos,
|
||||
schematic,
|
||||
rotation,
|
||||
nil,
|
||||
true)
|
||||
-- initialize special nodes (chests, furnace)
|
||||
minetest.after(2,settlements.initialize_nodes, pos, width, depth, height)
|
||||
minetest.after(2, settlements.initialize_nodes, pos, width, depth, height)
|
||||
end)
|
||||
end
|
||||
--
|
||||
|
@ -47,28 +59,36 @@ end
|
|||
function settlements.place_settlement_circle(minp, maxp)
|
||||
-- find center of chunk
|
||||
local half_map_chunk_size = 40
|
||||
local center = {x=maxp.x-half_map_chunk_size, y=maxp.y-half_map_chunk_size, z=maxp.z-half_map_chunk_size}
|
||||
local center = {x=maxp.x-half_map_chunk_size,
|
||||
y=maxp.y-half_map_chunk_size,
|
||||
z=maxp.z-half_map_chunk_size}
|
||||
-- find center_surcafe of chunk
|
||||
local center_surface = settlements.find_surface(center)
|
||||
-- go build settlement around center
|
||||
if center_surface then
|
||||
-- add settlement to list
|
||||
table.insert(settlements_in_world, center_surface)
|
||||
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
|
||||
building_all_info = schematic_table[1]
|
||||
settlements.build_schematic(center_surface, building_all_info["mts"],building_all_info["rplc"], building_all_info["name"])
|
||||
settlements.build_schematic(center_surface,
|
||||
building_all_info["mts"],
|
||||
building_all_info["rplc"],
|
||||
building_all_info["name"])
|
||||
-- add to settlement info table
|
||||
local index = 1
|
||||
settlement_info[index] = {pos = center_surface, name = building_all_info["name"], hsize = building_all_info["hsize"]}
|
||||
settlement_info[index] = {pos = center_surface,
|
||||
name = building_all_info["name"],
|
||||
hsize = building_all_info["hsize"]}
|
||||
--increase index for following buildings
|
||||
index = index + 1
|
||||
-- now some buildings around in a circle
|
||||
local x, z, r = center_surface.x, center_surface.z, 10
|
||||
-- draw 5 circles around center and increase radius by 5
|
||||
-- draw j circles around center and increase radius by 5
|
||||
for j = 1,10 do
|
||||
if number_built < number_of_buildings then
|
||||
-- set position on imaginary circle
|
||||
|
@ -78,13 +98,20 @@ function settlements.place_settlement_circle(minp, maxp)
|
|||
local pos1 = { x=ptx, y=center_surface.y, z=ptz}
|
||||
--
|
||||
local pos_surface = settlements.find_surface(pos1)
|
||||
if pos_surface then
|
||||
if settlements.pick_next_building(pos_surface) then
|
||||
settlements.build_schematic(pos_surface, building_all_info["mts"],building_all_info["rplc"], building_all_info["name"])
|
||||
if pos_surface
|
||||
then
|
||||
if settlements.pick_next_building(pos_surface)
|
||||
then
|
||||
settlements.build_schematic(pos_surface,
|
||||
building_all_info["mts"],building_all_info["rplc"],
|
||||
building_all_info["name"])
|
||||
number_built = number_built + 1
|
||||
settlement_info[index] = {pos = pos_surface, name = building_all_info["name"], hsize = building_all_info["hsize"]}
|
||||
settlement_info[index] = {pos = pos_surface,
|
||||
name = building_all_info["name"],
|
||||
hsize = building_all_info["hsize"]}
|
||||
index = index + 1
|
||||
if number_of_buildings == number_built then
|
||||
if number_of_buildings == number_built
|
||||
then
|
||||
break
|
||||
end
|
||||
end
|
||||
|
@ -122,11 +149,13 @@ function settlements.pick_next_building(pos_surface)
|
|||
local size = #randomized_schematic_table
|
||||
for i = size, 1, -1 do
|
||||
-- already enough buildings of that type?
|
||||
if count_buildings[randomized_schematic_table[i]["name"]] < randomized_schematic_table[i]["max_num"]*number_of_buildings then
|
||||
if count_buildings[randomized_schematic_table[i]["name"]] < randomized_schematic_table[i]["max_num"]*number_of_buildings then
|
||||
building_all_info = randomized_schematic_table[i]
|
||||
-- check distance to other buildings
|
||||
local distance_to_other_buildings_ok = settlements.check_distance(pos_surface, building_all_info["hsize"])
|
||||
if distance_to_other_buildings_ok then
|
||||
local distance_to_other_buildings_ok = settlements.check_distance(pos_surface,
|
||||
building_all_info["hsize"])
|
||||
if distance_to_other_buildings_ok
|
||||
then
|
||||
-- count built houses
|
||||
count_buildings[building_all_info["name"]] = count_buildings[building_all_info["name"]] +1
|
||||
return building_all_info["mts"]
|
||||
|
|
|
@ -21,7 +21,7 @@ schem_path = settlements.modpath.."/schematics/"
|
|||
-- list of schematics
|
||||
--
|
||||
schematic_table = {
|
||||
{name = "townhall", mts = schem_path.."townhall.mts", hsize = 20, max_num = 0, rplc = "n"},
|
||||
{name = "townhall", mts = schem_path.."townhall.mts", hsize = 15, max_num = 0, rplc = "n"},
|
||||
{name = "well", mts = schem_path.."well.mts", hsize = 11, max_num = 0.055, rplc = "n"},
|
||||
{name = "hut", mts = schem_path.."hut.mts", hsize = 11, max_num = 0.9, rplc = "y"},
|
||||
{name = "garden", mts = schem_path.."garden.mts", hsize = 11, max_num = 0.1, rplc = "n"},
|
||||
|
|
|
@ -9,7 +9,7 @@ function settlements.ground(pos) -- role model: Wendelsteinkircherl, Brannenburg
|
|||
while true do
|
||||
cnt = cnt+1
|
||||
if cnt > 50 then break end
|
||||
if cnt>math.random(2,4) then mat = "stone"end
|
||||
if cnt>math.random(2,4) then mat = "stone" end
|
||||
minetest.swap_node(p2, {name="default:"..mat})
|
||||
p2.y = p2.y-1
|
||||
end
|
||||
|
@ -37,7 +37,8 @@ function settlements.foundation(pos, width, depth, height, rotation)
|
|||
minetest.after(1,settlements.ground,p)--(p)
|
||||
else
|
||||
-- minetest.remove_node({x=p5.x+xi, y=p5.y+yi, z=p5.z+zi})
|
||||
minetest.swap_node({x=p5.x+xi, y=p5.y+yi, z=p5.z+zi}, {name="air"})
|
||||
minetest.swap_node({x=p5.x+xi, y=p5.y+yi, z=p5.z+zi},
|
||||
{name="air"})
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
35
init.lua
35
init.lua
|
@ -22,16 +22,26 @@ minetest.register_node("settlements:junglewood", {
|
|||
groups = {choppy=3, wood=2},
|
||||
sounds = default.node_sound_wood_defaults(),
|
||||
})
|
||||
|
||||
|
||||
|
||||
--
|
||||
-- register inhabitants
|
||||
--
|
||||
if minetest.get_modpath("mobs_npc") ~= nil then
|
||||
--mobs:register_spawn(name, nodes, max_light, min_light, chance, active_object_count, max_height, day_toggle)
|
||||
mobs:register_spawn("mobs_npc:npc", {"settlements:junglewood"}, 20, 0, 1, 7, 31000, nil)
|
||||
mobs:register_spawn("mobs_npc:trader", {"settlements:junglewood"}, 20, 0, 1, 7, 31000, nil)
|
||||
mobs:register_spawn("mobs_npc:npc", --name
|
||||
{"settlements:junglewood"}, --nodes
|
||||
20, --max_light
|
||||
0, --min_light
|
||||
20, --chance
|
||||
7, --active_object_count
|
||||
31000, --max_height
|
||||
nil) --day_toggle
|
||||
mobs:register_spawn("mobs_npc:trader", --name
|
||||
{"settlements:junglewood"}, --nodes
|
||||
20, --max_light
|
||||
0, --min_light
|
||||
20, --chance
|
||||
7, --active_object_count
|
||||
31000, --max_height
|
||||
nil)--day_toggle
|
||||
end
|
||||
--
|
||||
-- on map generation, try to build a settlement
|
||||
|
@ -42,9 +52,13 @@ minetest.register_on_generated(function(minp, maxp)
|
|||
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 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
|
||||
if dist_ok == false
|
||||
then
|
||||
return
|
||||
end
|
||||
settlements.place_settlement_circle(minp, maxp)
|
||||
|
@ -63,7 +77,10 @@ minetest.register_craftitem("settlements:tool", {
|
|||
local center_surface = pointed_thing.under
|
||||
if center_surface then
|
||||
local building_all_info = {name = "blacksmith", mts = schem_path.."blacksmith.mts", hsize = 13, max_num = 0.9, rplc = "n"}
|
||||
settlements.build_schematic(center_surface, building_all_info["mts"],building_all_info["rplc"], building_all_info["name"])
|
||||
settlements.build_schematic(center_surface,
|
||||
building_all_info["mts"],
|
||||
building_all_info["rplc"],
|
||||
building_all_info["name"])
|
||||
|
||||
-- settlements.convert_mts_to_lua()
|
||||
-- settlements.mts_save()
|
||||
|
|
23
utils.lua
23
utils.lua
|
@ -31,8 +31,10 @@ function settlements.find_surface(pos)
|
|||
if s == nil or s.name == "ignore" then return nil end
|
||||
for i, mats in ipairs(surface_mat) do
|
||||
-- if s and s.name == mats and not string.find(minetest.get_node_or_nil({ x=p6.x, y=p6.y+1, z=p6.z}).name,"water") then
|
||||
if s and s.name == mats and (string.find(minetest.get_node_or_nil({ x=p6.x, y=p6.y+1, z=p6.z}).name,"air") or
|
||||
string.find(minetest.get_node_or_nil({ x=p6.x, y=p6.y+1, z=p6.z}).name,"snow")) then
|
||||
if s and s.name == mats and
|
||||
(string.find(minetest.get_node_or_nil({ x=p6.x, y=p6.y+1, z=p6.z}).name,"air") or
|
||||
string.find(minetest.get_node_or_nil({ x=p6.x, y=p6.y+1, z=p6.z}).name,"snow"))
|
||||
then
|
||||
return p6
|
||||
end
|
||||
end
|
||||
|
@ -45,9 +47,12 @@ end
|
|||
-- check distance for new building
|
||||
--
|
||||
function settlements.check_distance(building_pos, building_size)
|
||||
local distance
|
||||
for i, built_house in ipairs(settlement_info) do
|
||||
local distance = math.sqrt(((building_pos.x - built_house["pos"].x)*(building_pos.x - built_house["pos"].x))+((building_pos.z - built_house["pos"].z)*(building_pos.z - built_house["pos"].z)))
|
||||
if distance < building_size and distance < built_house["hsize"] then
|
||||
distance = math.sqrt(((building_pos.x - built_house["pos"].x)*(building_pos.x - built_house["pos"].x))+((building_pos.z - built_house["pos"].z)*(building_pos.z - built_house["pos"].z)))
|
||||
if distance < building_size or
|
||||
distance < built_house["hsize"]
|
||||
then
|
||||
return false
|
||||
end
|
||||
end
|
||||
|
@ -131,11 +136,15 @@ end
|
|||
--
|
||||
function settlements.initialize_furnace(pos)
|
||||
-- find chests within radius
|
||||
local furnacepos = minetest.find_node_near(pos, 7, {"default:furnace"})
|
||||
local furnacepos = minetest.find_node_near(pos,
|
||||
7, --radius
|
||||
{"default:furnace"})
|
||||
-- initialize furnacepos (mts furnacepos don't have meta)
|
||||
if furnacepos then
|
||||
if furnacepos
|
||||
then
|
||||
local meta = minetest.get_meta(furnacepos)
|
||||
if meta:get_string("infotext") ~= "furnace" then
|
||||
if meta:get_string("infotext") ~= "furnace"
|
||||
then
|
||||
minetest.registered_nodes["default:furnace"].on_construct(furnacepos)
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in New Issue