2017-05-16 21:05:17 +02:00
|
|
|
|
|
|
|
--
|
|
|
|
-- Mapgen
|
|
|
|
--
|
2019-08-28 17:31:41 +02:00
|
|
|
local S = minetest.get_translator("village")
|
2017-05-16 21:05:17 +02:00
|
|
|
|
|
|
|
local spawn_pos = minetest.setting_get_pos("static_spawnpoint") or {x = 0, y = 0, z = 0}
|
2017-06-27 21:13:30 +02:00
|
|
|
local spawn_radius = minetest.settings:get("static_spawn_radius") or 256
|
2019-03-20 15:01:03 +01:00
|
|
|
local mapseed = minetest.get_mapgen_setting("seed")
|
2017-05-16 21:05:17 +02:00
|
|
|
|
2019-09-03 16:39:07 +02:00
|
|
|
local bitwise_and = function(x,y)
|
|
|
|
local upper = math.floor(math.log(x, 2))
|
|
|
|
local sum = 0
|
|
|
|
for n=0, upper do
|
|
|
|
sum = sum + math.pow(2, n) * (math.floor(x/math.pow(2, n)) % 2) * ( math.floor(y / math.pow(2, n)) % 2)
|
|
|
|
end
|
|
|
|
return math.floor(sum)
|
|
|
|
end
|
|
|
|
|
|
|
|
local shortseed = bitwise_and(mapseed, 0xFFFFFF)
|
|
|
|
|
2017-05-16 21:05:17 +02:00
|
|
|
-- Nodes
|
|
|
|
|
2019-09-26 13:42:10 +02:00
|
|
|
local place_priv = function(itemstack, placer, pointed_thing)
|
2019-09-26 13:58:13 +02:00
|
|
|
if not minetest.get_player_privs(placer:get_player_name()).maphack then
|
|
|
|
minetest.chat_send_player(placer:get_player_name(), minetest.colorize("#FF0000", S("You need the “maphack” privilege to use this.")))
|
2019-09-26 13:42:10 +02:00
|
|
|
return itemstack
|
|
|
|
end
|
|
|
|
return minetest.item_place(itemstack, placer, pointed_thing)
|
|
|
|
end
|
|
|
|
|
2017-05-12 04:29:55 +02:00
|
|
|
minetest.register_node(
|
2015-09-01 17:15:24 +02:00
|
|
|
"village:entity_spawner",
|
|
|
|
{
|
2019-08-28 17:31:41 +02:00
|
|
|
description = S("Village Entity Spawner"),
|
2019-09-06 18:10:08 +02:00
|
|
|
drawtype = "airlike",
|
|
|
|
pointable = false,
|
|
|
|
inventory_image = "village_entity.png",
|
|
|
|
wield_image = "village_entity.png",
|
|
|
|
is_ground_content = true,
|
2019-09-04 14:24:07 +02:00
|
|
|
sunlight_propagates = true,
|
|
|
|
paramtype = "light",
|
|
|
|
post_effect_color = { a = 0x40, r = 0x8D, g = 0x75, b = 0x97 },
|
|
|
|
walkable = false,
|
2019-09-06 18:10:08 +02:00
|
|
|
floodable = true,
|
|
|
|
buildable_to = true,
|
2019-08-29 16:22:42 +02:00
|
|
|
drop = "",
|
2019-09-04 14:24:07 +02:00
|
|
|
groups = {dig_immediate = 3, not_in_creative_inventory = 1},
|
2019-09-06 18:10:08 +02:00
|
|
|
sounds = default.node_sound_defaults(),
|
2019-09-26 13:42:10 +02:00
|
|
|
on_place = place_priv,
|
2019-09-06 18:10:08 +02:00
|
|
|
on_timer = function(pos, elapsed)
|
|
|
|
-- Wait until some objects are nearby ...
|
|
|
|
local objs_around = minetest.get_objects_inside_radius(pos, 12)
|
|
|
|
-- ... but not TOO nearby (occupying the pos)
|
|
|
|
local objs_near = minetest.get_objects_inside_radius(pos, 1.2)
|
|
|
|
if #objs_around > 0 and #objs_near == 0 then
|
2019-09-25 11:47:42 +02:00
|
|
|
local ent_name = minetest.get_meta(pos):get_string("entity")
|
|
|
|
if ent_name ~= "" then
|
|
|
|
local ent = minetest.add_entity({x=pos.x, y=pos.y+0.6, z=pos.z}, ent_name)
|
|
|
|
-- All spawned animals are tamed
|
|
|
|
if ent ~= nil and ent:get_luaentity() ~= nil then
|
|
|
|
if minetest.registered_entities[ent_name].type == "animal" then
|
|
|
|
ent:get_luaentity().tamed = true
|
|
|
|
end
|
|
|
|
end
|
2019-09-06 18:10:08 +02:00
|
|
|
else
|
|
|
|
minetest.log("error", "[village] Entity spawner without 'entity' in meta set @ "..minetest.pos_to_string(pos))
|
|
|
|
end
|
|
|
|
minetest.remove_node(pos)
|
|
|
|
return
|
|
|
|
else
|
|
|
|
-- Don't spawn and try again later
|
|
|
|
minetest.get_node_timer(pos):start(5)
|
|
|
|
end
|
|
|
|
end,
|
2017-05-11 23:10:00 +02:00
|
|
|
})
|
2015-09-01 17:15:24 +02:00
|
|
|
|
2017-05-12 04:29:55 +02:00
|
|
|
minetest.register_node(
|
2015-09-01 17:15:24 +02:00
|
|
|
"village:grassland_village",
|
|
|
|
{
|
2019-08-28 17:31:41 +02:00
|
|
|
description = S("Village Spawner"),
|
2019-08-29 15:50:58 +02:00
|
|
|
tiles = {
|
|
|
|
"village_gen.png", "village_gen.png", "village_gen.png",
|
|
|
|
"village_gen.png", "village_gen.png^[transformFX", "village_gen.png^[transformFX",
|
|
|
|
},
|
2015-09-07 00:10:27 +02:00
|
|
|
is_ground_content = false,
|
|
|
|
groups = {dig_immediate = 2},
|
2019-08-29 14:58:02 +02:00
|
|
|
sounds = default.node_sound_dirt_defaults(),
|
2019-08-29 16:22:42 +02:00
|
|
|
drop = "",
|
2017-05-16 21:05:17 +02:00
|
|
|
|
2019-09-26 13:42:10 +02:00
|
|
|
on_place = place_priv,
|
2017-05-16 21:05:17 +02:00
|
|
|
on_construct = function(pos)
|
|
|
|
minetest.remove_node(pos)
|
|
|
|
|
2019-09-04 11:34:06 +02:00
|
|
|
local pr = PseudoRandom(shortseed + pos.x + pos.y + pos.z)
|
2017-05-16 21:05:17 +02:00
|
|
|
|
2019-09-05 20:06:31 +02:00
|
|
|
-- Spawn village on placement.
|
|
|
|
-- Guarantee that at least the well is placed, to avoid confusion.
|
|
|
|
village.spawn_village({x=pos.x,y=pos.y-1,z=pos.z}, pr, true)
|
2017-05-16 21:05:17 +02:00
|
|
|
end,
|
2017-05-11 23:10:00 +02:00
|
|
|
})
|
2015-09-01 17:15:24 +02:00
|
|
|
|
2019-09-03 16:13:21 +02:00
|
|
|
local function attempt_village_spawn(pos)
|
|
|
|
local spos = table.copy(pos)
|
|
|
|
spos.y = spos.y + 1
|
|
|
|
if minetest.settings:get_bool("mapgen_disable_villages") == true then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2019-09-03 16:39:07 +02:00
|
|
|
local pr = PseudoRandom(shortseed + spos.x + spos.y + spos.z)
|
2019-09-03 16:13:21 +02:00
|
|
|
|
2019-09-03 16:39:07 +02:00
|
|
|
if ((shortseed + spos.x + spos.y + spos.z) % 30) == 1 then
|
2019-09-03 16:13:21 +02:00
|
|
|
local nearest = village.get_nearest_village(spos)
|
|
|
|
|
|
|
|
if nearest.dist > village.min_spawn_dist then
|
|
|
|
if vector.distance(spawn_pos, spos) > spawn_radius then
|
|
|
|
minetest.log("action", "[village] Spawning a grassland village at " .. "(" .. spos.x
|
|
|
|
.. ", " .. spos.y .. ", " .. spos.z .. ")")
|
2019-09-05 16:54:25 +02:00
|
|
|
local ok = village.spawn_village({x=spos.x,y=spos.y-1,z=spos.z}, pr)
|
2019-09-05 16:34:59 +02:00
|
|
|
if not ok then
|
|
|
|
minetest.log("action", "[village] Village spawn failed")
|
|
|
|
end
|
2019-09-03 16:13:21 +02:00
|
|
|
else
|
|
|
|
minetest.log("action", "[village] Cannot spawn village, too near the static spawnpoint")
|
|
|
|
end
|
|
|
|
else
|
|
|
|
minetest.log("action", "[village] Cannot spawn village, too near another village")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2017-05-16 21:05:17 +02:00
|
|
|
|
2019-09-03 16:13:21 +02:00
|
|
|
local village_decoration_id
|
2017-06-27 21:13:30 +02:00
|
|
|
if not minetest.settings:get_bool("mapgen_disable_villages") then
|
2019-09-03 16:13:21 +02:00
|
|
|
-- Dummy decoration to find possible village spawn points
|
|
|
|
-- via gennotify.
|
2017-05-16 21:05:17 +02:00
|
|
|
minetest.register_decoration(
|
|
|
|
{
|
2019-09-03 16:13:21 +02:00
|
|
|
name = "village_grassland",
|
|
|
|
deco_type = "schematic",
|
2017-05-16 21:05:17 +02:00
|
|
|
place_on = "default:dirt_with_grass",
|
|
|
|
sidelen = 16,
|
|
|
|
fill_ratio = 0.005,
|
|
|
|
biomes = {
|
|
|
|
"Grassland",
|
|
|
|
},
|
2019-09-03 16:13:21 +02:00
|
|
|
-- empty schematic
|
|
|
|
schematic = {
|
|
|
|
size = { x = 1, y = 1, z = 1 },
|
|
|
|
data = {
|
|
|
|
{ name = "air", prob = 0 },
|
|
|
|
},
|
2017-05-16 21:05:17 +02:00
|
|
|
},
|
|
|
|
y_min = 1,
|
|
|
|
y_max = 1000,
|
|
|
|
})
|
2019-09-03 16:13:21 +02:00
|
|
|
village_decoration_id = minetest.get_decoration_id("village_grassland")
|
|
|
|
|
|
|
|
if village_decoration_id then
|
|
|
|
minetest.set_gen_notify({decoration=true}, {village_decoration_id})
|
|
|
|
minetest.register_on_generated(function(minp, maxp, blockseed)
|
|
|
|
local mgobj = minetest.get_mapgen_object("gennotify")
|
|
|
|
local deco = mgobj["decoration#"..village_decoration_id]
|
|
|
|
if deco then
|
|
|
|
for d=1, #deco do
|
|
|
|
attempt_village_spawn(deco[d])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end)
|
|
|
|
end
|
2017-05-16 21:05:17 +02:00
|
|
|
end
|
2019-09-03 16:13:21 +02:00
|
|
|
|
2019-09-03 16:17:11 +02:00
|
|
|
-- Legacy alias
|
|
|
|
minetest.register_alias("village:grassland_village_mg", "air")
|