diff --git a/buildings.lua b/buildings.lua index dac694a..c93cc6b 100644 --- a/buildings.lua +++ b/buildings.lua @@ -1,20 +1,23 @@ -- list of schematics local schematic_table = { - hut = {name = "hut", mts = schem_path.."hut.mts", hsize = 10, chance = 70}, - garden = {name = "garden", mts = schem_path.."garden.mts", hsize = 10, chance = 10}, - lamp = {name = "lamp", mts = schem_path.."lamp.mts", hsize = 7, chance = 10}, - tower = {name = "tower", mts = schem_path.."tower.mts", hsize = 10, chance = 10}, - well = {name = "well", mts = schem_path.."well.mts", hsize = 10, chance = 00} + hut = {name = "hut", mts = schem_path.."hut.mts", hsize = 11, max_num = 0.9, rplc = "y"}, + garden = {name = "garden", mts = schem_path.."garden.mts", hsize = 11, max_num = 0.2, rplc = "n"}, + lamp = {name = "lamp", mts = schem_path.."lamp.mts", hsize = 10, max_num = 0.2, rplc = "n"}, + tower = {name = "tower", mts = schem_path.."tower.mts", hsize = 11, max_num = 0.2, rplc = "n"}, + well = {name = "well", mts = schem_path.."well.mts", hsize = 11, max_num = 0, rplc = "n"} } +local count_buildings ={} -- iterate over whole table to get all keys local keyset = {} for k in pairs(schematic_table) do table.insert(keyset, k) end - +--local variables for buildings local building_all_info +local number_of_buildings +local number_built -function settlements.build_schematic(pos, building) +function settlements.build_schematic(pos, building, replace_wall) -- get building node material for better integration to surrounding local balcony_material = minetest.get_node_or_nil(pos).name -- pick random material @@ -22,7 +25,10 @@ function settlements.build_schematic(pos, building) -- schematic conversion to lua local schem_lua = minetest.serialize_schematic(building, "lua", {lua_use_comments = false, lua_num_indent_spaces = 0}).." return(schematic)" -- replace material - schem_lua = schem_lua:gsub("default:cobble", material):gsub("default:dirt_with_grass", balcony_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) -- format schematic string local schematic = loadstring(schem_lua)() -- build foundation for the building an make room above @@ -44,13 +50,9 @@ function settlements.place_settlement_circle(minp, maxp) local center_surface = settlements.find_surface(center) -- go build settlement around center if center_surface then - minetest.chat_send_all("Dorf") - -- settlement_info table reset - for k,v in pairs(settlement_info) do - settlement_info[k] = nil - end - -- randomize number of buildings - local number_of_buildings = 15 + last_time = os.time() + 30 + -- initialize all settlement information + settlements.initialize_settlement() -- build well in the center building_all_info = schematic_table["well"] settlements.build_schematic(center_surface, building_all_info["mts"]) @@ -63,7 +65,7 @@ function settlements.place_settlement_circle(minp, maxp) local x, z, r = center_surface.x, center_surface.z, 5 -- draw 5 circles around center and increase radius by 5 for j = 1,10 do - if number_of_buildings > 0 then + if number_built < number_of_buildings then -- set position on imaginary circle for j = 0, 360, 15 do local angle = j * math.pi / 180 @@ -72,30 +74,65 @@ function settlements.place_settlement_circle(minp, maxp) -- local pos_surface = settlements.find_surface(pos1) if pos_surface then - -- pick schematic based on chance - local random_number = math.random(1,100) - if random_number > 20 then - building_all_info = schematic_table["hut"] - else - building_all_info = schematic_table[keyset[math.random(#keyset)]] - end - -- before placing, 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 - settlements.build_schematic(pos_surface, building_all_info["mts"]) - number_of_buildings = number_of_buildings -1 + if settlements.pick_next_building(pos_surface) then + settlements.build_schematic(pos_surface, building_all_info["mts"],building_all_info["rplc"]) + number_built = number_built + 1 settlement_info[index] = {pos = pos_surface, name = building_all_info["name"], hsize = building_all_info["hsize"]} index = index + 1 - if number_of_buildings == 0 then + if number_of_buildings == number_built then break end end + else + break end end - else - break + r = r + 5 end - r = r + 10 end end +end +function settlements.initialize_settlement() + -- settlement_info table reset + for k,v in pairs(settlement_info) do + settlement_info[k] = nil + end + -- count_buildings table reset + for k,v in pairs(schematic_table) do +-- local name = schematic_table[v]["name"] + count_buildings[k] = 0 + end + + -- randomize number of buildings + number_of_buildings = 15--math.random(7,20) + number_built = 1 + minetest.chat_send_all("Dorf".. number_of_buildings) +end +-- +-- everything necessary to pick a fitting next building +-- +function settlements.pick_next_building(pos_surface) + -- building_all_info = schematic_table[keyset[math.random(#keyset)]] + -- pick schematic based on chance + local random_number = math.random(1,100) + if random_number > 85 and count_buildings["garden"] < schematic_table["garden"]["max_num"]*number_of_buildings then + building_all_info = schematic_table["garden"] + elseif random_number > 75 and count_buildings["tower"] < schematic_table["tower"]["max_num"]*number_of_buildings then + building_all_info = schematic_table["tower"] + elseif random_number > 65 and count_buildings["lamp"] < schematic_table["lamp"]["max_num"]*number_of_buildings then + building_all_info = schematic_table["lamp"] + else + building_all_info = schematic_table["hut"] + end + -- before placing, 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 + -- count built houses + count_buildings[building_all_info["name"]] = count_buildings[building_all_info["name"]] +1 + + return building_all_info["mts"] + --todo hier den count_up einfügen + else + return nil + end end \ No newline at end of file diff --git a/const.lua b/const.lua index 4540768..d300c47 100644 --- a/const.lua +++ b/const.lua @@ -1,10 +1,26 @@ -wallmaterial = {"default:junglewood", "default:pine_wood", "default:wood", "default:aspen_wood", - "default:acacia_wood", "default:stonebrick", "default:cobble", "default:desert_stonebrick", - "default:desert_cobble", "default:sandstone"} +wallmaterial = { + "default:junglewood", + "default:pine_wood", + "default:wood", + "default:aspen_wood", + "default:acacia_wood", + "default:stonebrick", + "default:cobble", + "default:desert_stonebrick", + "default:desert_cobble", + "default:sandstone" + } c_floor_material = "default:wood" -- not local because doors need it +last_time = os.time() -surface_mat = {"default:dirt_with_grass","default:dirt_with_snow","default:dirt_with_dray_grass"} +surface_mat = { + "default:dirt_with_grass", + "default:dirt_with_snow", + "default:dirt_with_dry_grass", + "default:dirt_with_coniferous_litter", + "default:sand" + } above_surface_mat = {"default:air","default:dirt_with_snow"} under_surface_mat = {"default:stone","default:dirt"} schem_path = settlements.modpath.."/schematics/" diff --git a/init.lua b/init.lua index 934e1f9..1b594c1 100644 --- a/init.lua +++ b/init.lua @@ -11,7 +11,6 @@ dofile(settlements.modpath.."/foundation.lua") --dofile(settlements.modpath.."/doors.lua") dofile(settlements.modpath.."/buildings.lua") -local last_time = os.time() local function place_settlement(minp, maxp) -- wait xx seconds until building a new settlement