Skip to content

Commit

Permalink
Restore MT0.4 support, minor cleanup (#2)
Browse files Browse the repository at this point in the history
* Restore 0.4 support, cleanup

* Good check for buggy height, but it breaks generation in Flat worlds

Co-authored-by: FaceDeer <[email protected]>
  • Loading branch information
Maksim and FaceDeer committed Jan 18, 2020
1 parent e6e3150 commit 0bc56f6
Show file tree
Hide file tree
Showing 42 changed files with 249 additions and 475 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
This mod adds settlements on world generation. The settlements consist of a cluster of small buildings whose construction sites blend into the contours of the world around them and which are connected by non-rectilinear gravel paths. They are the equivalent of small hamlets or thorpes, not grand villages or towns.

## Types of settlements
## Types of settlements

With the exception of some optional commoditymarket node support, only nodes from the default minetest_game are used by the base schematics included in this mod.

Expand Down Expand Up @@ -44,4 +44,4 @@ Which was in turn based on "ruins" by BlockMen

## License

This mod is licensed under the MIT license.
This mod is licensed under the MIT license.
2 changes: 1 addition & 1 deletion modpack.conf
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
name = settlements
description = the settlements API and a collection of default settlement types
description = the settlements API and a collection of default settlement types
Empty file added modpack.txt
Empty file.
25 changes: 12 additions & 13 deletions settlements/admin_commands.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
-- internationalization boilerplate
local modpath = minetest.get_modpath(minetest.get_current_modname())
local S, NS = dofile(modpath.."/intllib.lua")
local S, NS = settlements.S, settlements.NS

local visual_range = tonumber(minetest.settings:get("settlements_visibility_range")) or 600

Expand All @@ -10,10 +9,10 @@ minetest.register_chatcommand("settlements_list", {
func = function(name, param)
local player = minetest.get_player_by_name(name)
local player_pos = player:get_pos()

local settlement_list = settlements.settlements_in_world:get_areas_in_area(
{x=-32000, y=-32000, z=-32000}, {x=32000, y=32000, z=32000}, true, true, true)

for _, settlement in pairs(settlement_list) do
local data = minetest.deserialize(settlement.data)
if data.discovered_by[name] then
Expand Down Expand Up @@ -67,7 +66,7 @@ local function get_nearest_settlement_within_range(pos, range, name)
local min_edge = vector.subtract(pos, range)
local max_edge = vector.add(pos, range)
local settlement_list = settlements.settlements_in_world:get_areas_in_area(min_edge, max_edge, true, true, true)

local min_dist = range + 1 -- start with number beyond range
local min_id = nil
local min_data = nil
Expand All @@ -77,12 +76,12 @@ local function get_nearest_settlement_within_range(pos, range, name)
local distance = vector.distance(pos, settlement.min)
if distance < min_dist and data.discovered_by[name] then
min_dist = distance
min_id = id
min_id = id
min_data = data
min_pos = settlement.min
end
end

return min_pos, min_id, min_data
end

Expand All @@ -93,9 +92,9 @@ minetest.register_chatcommand("settlements_rename_nearest", {
func = function(name, param)
local player = minetest.get_player_by_name(name)
local player_pos = player:get_pos()

local min_pos, min_id, min_data = get_nearest_settlement_within_range(player_pos, visual_range, name)

if min_id ~= nil then
if param == "" then
local def = settlements.registered_settlements[min_data.settlement_type]
Expand All @@ -115,7 +114,7 @@ minetest.register_chatcommand("settlements_rename_nearest", {
settlements.remove_all_hud_markers()
return
end

minetest.chat_send_player(name, S("No known settlements within @1m found.", visual_range))
end,
})
Expand Down Expand Up @@ -163,7 +162,7 @@ minetest.register_chatcommand("settlements_remove_nearest", {
local player = minetest.get_player_by_name(name)
local player_pos = player:get_pos()
local min_pos, min_id, min_data = get_nearest_settlement_within_range(player_pos, range, name)

if min_id ~= nil then
local result = settlements.settlements_in_world:remove_area(min_id)
if result then
Expand Down Expand Up @@ -196,7 +195,7 @@ minetest.register_chatcommand("settlements_create_in_mapchunk", {

local maxp = vector.add(minp, map_chunk_size-1)
local centerp = vector.add(minp, half_map_chunk_size)

local settlement_list = settlements.settlements_in_world:get_areas_in_area(minp, maxp, true)
if next(settlement_list) then
minetest.chat_send_player(name, S("Settlement already exists in this mapchunk"))
Expand All @@ -208,4 +207,4 @@ minetest.register_chatcommand("settlements_create_in_mapchunk", {
minetest.chat_send_player(name, S("Unable to create new settlement at @1", minetest.pos_to_string(centerp)))
end
end,
})
})
11 changes: 5 additions & 6 deletions settlements/admin_tools.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
-- internationalization boilerplate
local MP = minetest.get_modpath(minetest.get_current_modname())
local S, NS = dofile(MP.."/intllib.lua")
local S, NS = settlements.S, settlements.NS

-----------------------------------------------------------------------------
-- Admin tools
Expand All @@ -17,7 +16,7 @@ minetest.register_craftitem("settlements:settlement_tool", {
if not minetest.check_player_privs(placer, "server") then
minetest.chat_send_player(player_name, S("You need the server privilege to use this tool."))
return
end
end

local center_surface = pointed_thing.under
if center_surface then
Expand Down Expand Up @@ -59,8 +58,8 @@ minetest.register_craftitem("settlements:single_building_tool", {
if not minetest.check_player_privs(placer, "server") then
minetest.chat_send_player(placer:get_player_name(), S("You need the server privilege to use this tool."))
return
end
end

local center_surface = pointed_thing.under
if center_surface then
local selected_building = get_next_debug_building()
Expand All @@ -70,7 +69,7 @@ minetest.register_craftitem("settlements:single_building_tool", {
built_house.build_pos_min = center_surface
built_house.rotation = "0"
built_house.surface_mat = c_dirt_with_grass

local vm = minetest.get_voxel_manip()
local maxp = vector.add(center_surface, selected_building.schematic.size)
local emin, emax = vm:read_from_map(center_surface, maxp)
Expand Down
55 changes: 27 additions & 28 deletions settlements/bookgen.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ if not (minetest.settings:get_bool("settlements_generate_books", true) and modpa
end

-- internationalization boilerplate
local MP = minetest.get_modpath(minetest.get_current_modname())
local S, NS = dofile(MP.."/intllib.lua")
local S, NS = settlements.S, settlements.NS

-- values taken from default's craftitems.lua
local max_text_size = 10000
Expand All @@ -16,7 +15,7 @@ local lpp = 14
local generate_book = function(title, owner, text)
local book = ItemStack("default:book_written")
local meta = book:get_meta()

meta:set_string("title", title:sub(1, max_title_size))
meta:set_string("owner", owner)
local short_title = title
Expand All @@ -30,7 +29,7 @@ local generate_book = function(title, owner, text)
meta:set_string("text", text)
meta:set_int("page", 1)
meta:set_int("page_max", math.ceil((#text:gsub("[^\n]", "") + 1) / lpp))

return book
end

Expand All @@ -39,26 +38,26 @@ end
local half_map_chunk_size = settlements.half_map_chunk_size

minetest.register_abm({
label = "Settlement book authoring",
nodenames = {"default:bookshelf"},
interval = 86400, -- daily
-- Operation interval in seconds
chance = 2,
-- Chance of triggering `action` per-node per-interval is 1.0 / this value
catch_up = true,
-- If true, catch-up behaviour is enabled: The `chance` value is
-- temporarily reduced when returning to an area to simulate time lost
-- by the area being unattended. Note that the `chance` value can often
-- be reduced to 1.
label = "Settlement book authoring",
nodenames = {"default:bookshelf"},
interval = 86400, -- daily
-- Operation interval in seconds
chance = 2,
-- Chance of triggering `action` per-node per-interval is 1.0 / this value
catch_up = true,
-- If true, catch-up behaviour is enabled: The `chance` value is
-- temporarily reduced when returning to an area to simulate time lost
-- by the area being unattended. Note that the `chance` value can often
-- be reduced to 1.

action = function(pos, node, active_object_count, active_object_count_wider)
action = function(pos, node, active_object_count, active_object_count_wider)
local inv = minetest.get_inventory( {type="node", pos=pos} )

-- Can we fit a book?
if not inv or not inv:room_for_item("books", "default:book_written") then
return
end

-- find any settlements within the shelf's mapchunk
-- There's probably only ever going to be one, but might as well do a closeness check to be on the safe side.
local min_edge = vector.subtract(pos, half_map_chunk_size)
Expand All @@ -71,11 +70,11 @@ minetest.register_abm({
closest_settlement = {pos = target_pos, data = settlement.data}
end
end

if not closest_settlement then
return
end

-- Get the settlement def and, if it generate books, generate one
local data = minetest.deserialize(closest_settlement.data)
local town_name = data.name
Expand Down Expand Up @@ -113,7 +112,7 @@ local log_to_string = function(log_entry, market)
else
seller_name = seller.name
end

local itemname = log_entry.item
local item_def = minetest.registered_items[log_entry.item]
if item_def then
Expand Down Expand Up @@ -189,30 +188,30 @@ local get_random_settlement_within_range = function(pos, range_max, range_min)
if #settlements_within_range == 0 then
return
end

local target = settlements_within_range[math.random(#settlements_within_range)]
target.data = minetest.deserialize(target.data)
return target
end

local compass_dirs = {
[0] = S("west"),
[0] = S("west"),
S("west-southwest"),
S("southwest"),
S("south-southwest"),
S("south"),
S("south"),
S("south-southeast"),
S("southeast"),
S("east-southeast"),
S("east"),
S("east-northeast"),
S("northeast"),
S("north-northeast"),
S("north"),
S("north"),
S("north-northwest"),
S("northwest"),
S("west-northwest"),
S("west"),
S("west"),
}
local increment = 2*math.pi/#compass_dirs -- Divide the circle up into pieces
local reframe = math.pi - increment/2 -- Adjust the angle to run through a range divisible into indexes
Expand All @@ -221,7 +220,7 @@ local compass_direction = function(p1, p2)
local angle = math.atan2(dir.z, dir.x);
angle = angle + reframe
angle = math.ceil(angle / increment)
return compass_dirs[angle]
return compass_dirs[angle]
end

local get_altitude = function(pos)
Expand Down Expand Up @@ -252,7 +251,7 @@ settlements.generate_travel_guide = function(source_pos, source_name)
local distance = vector.distance(source_pos, target.pos)
local kilometers = string.format("%.1f", distance/1000)
local altitude = get_altitude(target.pos)

local text = S("In the @1 @2 kilometers to the @3 of @4 lies the settlement of @5.", altitude, kilometers, dir, source_name, target_name)
return generate_book(title, author, text)
end
Loading

0 comments on commit 0bc56f6

Please sign in to comment.