Compare commits

...

5 Commits

Author SHA1 Message Date
Wuzzy 7395c5edcb Version 1.0.1 2019-09-29 11:56:24 +02:00
Wuzzy 2bd52f3f33 Add chatcommands on init time 2019-09-29 11:55:38 +02:00
Wuzzy 65e854b8ef Version 1.0.0 2019-08-27 20:19:16 +02:00
Wuzzy 17927aaa11 Add basic support for finding default biome 2019-08-27 20:18:41 +02:00
Wuzzy a3653bdab1 Indent code 2019-08-27 16:42:09 +02:00
2 changed files with 84 additions and 30 deletions

View File

@ -5,10 +5,14 @@ This is a mod to help with mod/game development for Minetest.
It adds a command (“findbiome”) to find a biome nearby and teleport you to it It adds a command (“findbiome”) to find a biome nearby and teleport you to it
and another command (“listbiomes”) to list biomes. and another command (“listbiomes”) to list biomes.
Version: 0.2.0 Version: 1.0.1
## Known limitations ## Known limitations
- For exotic biomes restricted by height, this mod does not guarantee you will teleport to land There's no guarantee you will always find the biome, even if it exists in the world.
This can happen if the biome is very obscure or small, but usually you should be
able to find the biome.
If the biome could not be found, just move to somewhere else and try again.
## Authors ## Authors
- paramat (MIT License) - paramat (MIT License)

106
init.lua
View File

@ -78,6 +78,36 @@ local function adjust_pos_to_biome_limits(pos, biome_id)
return bpos, out_of_bounds return bpos, out_of_bounds
end end
-- Find the special default biome
local function find_default_biome()
local all_biomes = minetest.registered_biomes
local biome_count = 0
for b, biome in pairs(all_biomes) do
biome_count = biome_count + 1
end
-- Trivial case: No biomes registered, default biome is everywhere.
if biome_count == 0 then
local y = minetest.get_spawn_level(0, 0)
if not y then
y = 0
end
return { x = 0, y = y, z = 0 }
end
local pos = {}
-- Just check a lot of random positions
-- It's a crappy algorithm but better than nothing.
for i=1, 100 do
pos.x = math.random(-playable_limit, playable_limit)
pos.y = math.random(-playable_limit, playable_limit)
pos.z = math.random(-playable_limit, playable_limit)
local biome_data = minetest.get_biome_data(pos)
if biome_data and minetest.get_biome_name(biome_data.biome) == "default" then
return pos
end
end
return nil
end
local function find_biome(pos, biomes) local function find_biome(pos, biomes)
pos = vector.round(pos) pos = vector.round(pos)
-- Pos: Starting point for biome checks. This also sets the y co-ordinate for all -- Pos: Starting point for biome checks. This also sets the y co-ordinate for all
@ -117,38 +147,37 @@ local function find_biome(pos, biomes)
local function search() local function search()
local attempt = 1 local attempt = 1
while attempt < 3 do while attempt < 3 do
for iter = 1, checks do for iter = 1, checks do
local biome_data = minetest.get_biome_data(pos) local biome_data = minetest.get_biome_data(pos)
-- Sometimes biome_data is nil -- Sometimes biome_data is nil
local biome = biome_data and biome_data.biome local biome = biome_data and biome_data.biome
for id_ind = 1, #biome_ids do for id_ind = 1, #biome_ids do
local biome_id = biome_ids[id_ind] local biome_id = biome_ids[id_ind]
pos = adjust_pos_to_biome_limits(pos, biome_id) pos = adjust_pos_to_biome_limits(pos, biome_id)
local spos = table.copy(pos) local spos = table.copy(pos)
if biome == biome_id then if biome == biome_id then
local good_spawn_height = pos.y <= water_level + 16 and pos.y >= water_level local good_spawn_height = pos.y <= water_level + 16 and pos.y >= water_level
local spawn_y = minetest.get_spawn_level(spos.x, spos.z) local spawn_y = minetest.get_spawn_level(spos.x, spos.z)
if spawn_y then if spawn_y then
spawn_pos = {x = spos.x, y = spawn_y, z = spos.z} spawn_pos = {x = spos.x, y = spawn_y, z = spos.z}
elseif not good_spawn_height then elseif not good_spawn_height then
spawn_pos = {x = spos.x, y = spos.y, z = spos.z} spawn_pos = {x = spos.x, y = spos.y, z = spos.z}
elseif attempt >= 2 then elseif attempt >= 2 then
spawn_pos = {x = spos.x, y = spos.y, z = spos.z} spawn_pos = {x = spos.x, y = spos.y, z = spos.z}
end end
if spawn_pos then if spawn_pos then
local adjusted_pos, outside = adjust_pos_to_biome_limits(spawn_pos, biome_id) local adjusted_pos, outside = adjust_pos_to_biome_limits(spawn_pos, biome_id)
if is_in_world(spawn_pos) and not outside then if is_in_world(spawn_pos) and not outside then
return true return true
end
end end
end end
end end
pos = next_pos()
end end
attempt = attempt + 1
pos = next_pos()
end end
attempt = attempt + 1
end
return false return false
end end
local function search_v6() local function search_v6()
@ -194,12 +223,21 @@ local function find_biome(pos, biomes)
end end
local mods_loaded = false
minetest.register_on_mods_loaded(function() minetest.register_on_mods_loaded(function()
mods_loaded = true
end)
-- Regiver chat commands
do
minetest.register_chatcommand("findbiome", { minetest.register_chatcommand("findbiome", {
description = S("Find and teleport to biome"), description = S("Find and teleport to biome"),
params = S("<biome>"), params = S("<biome>"),
privs = { debug = true, teleport = true }, privs = { debug = true, teleport = true },
func = function(name, param) func = function(name, param)
if not mods_loaded then
return false
end
local player = minetest.get_player_by_name(name) local player = minetest.get_player_by_name(name)
if not player then if not player then
return false, S("No player.") return false, S("No player.")
@ -218,6 +256,15 @@ minetest.register_on_mods_loaded(function()
end end
end end
else else
if param == "default" then
local biome_pos = find_default_biome()
if biome_pos then
player:set_pos(biome_pos)
return true, S("Biome found at @1.", minetest.pos_to_string(biome_pos))
else
return false, S("No biome found!")
end
end
local id = minetest.get_biome_id(param) local id = minetest.get_biome_id(param)
if id then if id then
invalid_biome = false invalid_biome = false
@ -241,6 +288,9 @@ minetest.register_on_mods_loaded(function()
params = "", params = "",
privs = { debug = true }, privs = { debug = true },
func = function(name, param) func = function(name, param)
if not mods_loaded then
return false
end
local biomes local biomes
local b = 0 local b = 0
if mg_name == "v6" then if mg_name == "v6" then
@ -267,4 +317,4 @@ minetest.register_on_mods_loaded(function()
end end
end, end,
}) })
end) end