[kingdoms] initial commit
commit
8f43ea120e
|
@ -0,0 +1,36 @@
|
|||
-- Base table for configuration.
|
||||
kingdoms.config = {
|
||||
_defaults = {},
|
||||
}
|
||||
|
||||
-- Check if a setting is defined in minetest.conf
|
||||
function kingdoms.config._get(setting, default)
|
||||
if type(default) == "boolean" then
|
||||
local read = minetest.setting_getbool("kingdoms."..setting)
|
||||
if read == nil then
|
||||
return default
|
||||
else
|
||||
return read
|
||||
end
|
||||
elseif type(default) == "string" then
|
||||
return minetest.setting_get("kingdoms."..setting) or default
|
||||
elseif type(default) == "number" then
|
||||
return tonumber(minetest.setting_get("kingdoms."..setting) or default)
|
||||
end
|
||||
end
|
||||
|
||||
-- To set a default value: kingdoms.config.setting_name = value
|
||||
-- To get the default value or what is specified in minetest.conf: kingdoms.config.setting_name
|
||||
setmetatable(kingdoms.config, {
|
||||
__index = function(t, key)
|
||||
local default = t._defaults[key]
|
||||
-- If there is no default then the setting should not be used.
|
||||
if default == nil then
|
||||
return error(("'%s' is not a configuration option"):format(key))
|
||||
end
|
||||
return t._get(key, default)
|
||||
end,
|
||||
__newindex = function(t, key, value)
|
||||
t._defaults[key] = value
|
||||
end,
|
||||
})
|
|
@ -0,0 +1,109 @@
|
|||
kingdoms.corestone = {}
|
||||
-- Any mod can set this flag since they run in one thread.
|
||||
kingdoms.show_protection_messages = true
|
||||
-- Helper function to set the message display state.
|
||||
function kingdoms.spm(s)
|
||||
kingdoms.show_protection_messages = s
|
||||
end
|
||||
|
||||
function kingdoms.is_protected(r, pos, name)
|
||||
if not name or not pos then return false end
|
||||
local kingdom = kingdoms.player.kingdom(name)
|
||||
local positions = minetest.find_nodes_in_area(
|
||||
{x = pos.x - r, y = pos.y - r, z = pos.z - r},
|
||||
{x = pos.x + r, y = pos.y + r, z = pos.z + r},
|
||||
{"kingdoms:corestone"})
|
||||
for _, pos in ipairs(positions) do
|
||||
local meta = minetest.get_meta(pos)
|
||||
local kid = meta:get_string("kingdom.id")
|
||||
if not kingdom or kid ~= kingdom.id then
|
||||
if kingdoms.show_protection_messages then
|
||||
minetest.chat_send_player(name, ("This area is part of a kingdom ('%s') of which you are not a member."):format(kingdoms.db.kingdoms[kid].longname))
|
||||
end
|
||||
return false
|
||||
end
|
||||
end
|
||||
return true
|
||||
end
|
||||
|
||||
local old_is_protected = minetest.is_protected
|
||||
function minetest.is_protected(pos, digger, digging)
|
||||
if not kingdoms.is_protected(kingdoms.config.corestone_radius, pos, digger) then
|
||||
if protection_lagporter and digging then
|
||||
protection_lagporter.check(pos, digger)
|
||||
end
|
||||
return true
|
||||
end
|
||||
return old_is_protected(pos, digger, digging)
|
||||
end
|
||||
|
||||
minetest.register_node("kingdoms:corestone", {
|
||||
description = "Kingdom Core",
|
||||
drawtype = "nodebox",
|
||||
tiles = {
|
||||
"moreblocks_circle_stone_bricks.png",
|
||||
"moreblocks_circle_stone_bricks.png",
|
||||
"moreblocks_circle_stone_bricks.png^protector_logo.png"
|
||||
},
|
||||
sounds = default.node_sound_stone_defaults(),
|
||||
groups = {oddly_breakable_by_hand = 2, unbreakable = 1},
|
||||
is_ground_content = false,
|
||||
paramtype = "light",
|
||||
light_source = 0,
|
||||
|
||||
node_box = {
|
||||
type = "fixed",
|
||||
fixed = {
|
||||
{-0.5 ,-0.5, -0.5, 0.5, 0.5, 0.5},
|
||||
}
|
||||
},
|
||||
|
||||
on_blast = function(a, b)
|
||||
return
|
||||
end,
|
||||
|
||||
on_place = function(itemstack, placer, pointed_thing)
|
||||
if not placer or pointed_thing.type ~= "node" then
|
||||
return itemstack
|
||||
end
|
||||
|
||||
local kingdom = kingdoms.player.kingdom(placer:get_player_name())
|
||||
if not kingdom then
|
||||
minetest.chat_send_player(placer:get_player_name(), "You cannot place a corestone if you are not part of a kingdom.")
|
||||
return itemstack
|
||||
end
|
||||
|
||||
if pos.y < kingdoms.config.corestone_miny then
|
||||
minetest.chat_send_player(placer:get_player_name(), ("You cannot place a corestone below %d."):format(kingdoms.config.corestone_miny))
|
||||
return itemstack
|
||||
end
|
||||
|
||||
local radius = kingdoms.config.corestone_radius * 4
|
||||
|
||||
kingdoms.spm(false)
|
||||
local canplace = not protector.is_protected(radius, pointed_thing.under, placer:get_player_name()) or not protector.is_protected(radius, pointed_thing.above, placer:get_player_name())
|
||||
kingdoms.spm(true)
|
||||
if canplace then
|
||||
minetest.chat_send_player(placer:get_player_name(), "You cannot place a corestone this close to another.")
|
||||
return itemstack
|
||||
end
|
||||
|
||||
return minetest.item_place(itemstack, placer, pointed_thing)
|
||||
end,
|
||||
|
||||
after_place_node = function(pos, placer)
|
||||
local kingdom = kingdoms.player.kingdom(placer:get_player_name())
|
||||
local meta = minetest.get_meta(pos)
|
||||
meta:set_string("kingdom.id", kingdom.id)
|
||||
meta:set_string("infotext", ("Corestone of %s"):format(kingdom.longname))
|
||||
end,
|
||||
|
||||
on_use = function(itemstack, user, pointed_thing)
|
||||
end,
|
||||
|
||||
on_rightclick = function(pos, node, clicker, itemstack)
|
||||
end,
|
||||
|
||||
on_punch = function(pos, node, puncher)
|
||||
end,
|
||||
})
|
|
@ -0,0 +1,7 @@
|
|||
-- Save the database every <save_delay> seconds.
|
||||
kingdoms.config.save_delay = 10
|
||||
|
||||
-- A corestone extends in a radius of <corestone_radius>.
|
||||
kingdoms.config.corestone_radius = 5
|
||||
-- A corestone can be placed only above <corestone_miny>.
|
||||
kingdoms.config.corestone_miny = -32
|
|
@ -0,0 +1 @@
|
|||
default
|
|
@ -0,0 +1,64 @@
|
|||
-- Function to execute more files.
|
||||
local modpath = minetest.get_modpath("kingdoms")
|
||||
local function domodfile(f)
|
||||
dofile(modpath .. '/' .. f)
|
||||
end
|
||||
|
||||
-- Mod namespace.
|
||||
kingdoms = {}
|
||||
|
||||
function kingdoms.log(level, message)
|
||||
minetest.log(level, "[kingdoms] "..message)
|
||||
end
|
||||
|
||||
-- Initial configuration files.
|
||||
domodfile("config.lua")
|
||||
domodfile("defaults.lua")
|
||||
|
||||
-- Persistent database.
|
||||
kingdoms.db = {}
|
||||
|
||||
-- Contains meta functions operating on the database.
|
||||
kingdoms.dbmeta = {}
|
||||
kingdoms.dbmeta.file = minetest.get_worldpath() .. "/kingdoms_db"
|
||||
|
||||
function kingdoms.dbmeta.load()
|
||||
local file = io.open(kingdoms.dbmeta.file)
|
||||
if file then
|
||||
kingdoms.db = minetest.deserialize(file:read("*all")) or {}
|
||||
file:close()
|
||||
kingdoms.log("action", "Loading database.")
|
||||
else
|
||||
kingdoms.log("warning", "Database did not exist.")
|
||||
end
|
||||
end
|
||||
|
||||
function kingdoms.dbmeta.save()
|
||||
local f = io.open(kingdoms.dbmeta.file, "w")
|
||||
f:write(minetest.serialize(kingdoms.db))
|
||||
f:close()
|
||||
kingdoms.log("info", "Saved database.")
|
||||
end
|
||||
|
||||
function kingdoms.dbmeta.save_after()
|
||||
minetest.after(kingdoms.config.save_delay, kingdoms.dbmeta.save_after)
|
||||
kingdoms.dbmeta.save()
|
||||
end
|
||||
|
||||
-- Preform the initial load of the database.
|
||||
kingdoms.dbmeta.load()
|
||||
|
||||
-- Save at regular intervals and upon shutdown.
|
||||
minetest.after(kingdoms.config.save_delay, kingdoms.dbmeta.save_after)
|
||||
minetest.register_on_shutdown(kingdoms.dbmeta.save)
|
||||
|
||||
-- Core mod files.
|
||||
domodfile("utils.lua")
|
||||
|
||||
-- Mod interfaces.
|
||||
domodfile("kingdom.lua")
|
||||
domodfile("player.lua")
|
||||
domodfile("corestone.lua")
|
||||
|
||||
-- All done!
|
||||
kingdoms.log("action", "Completely loaded.")
|
|
@ -0,0 +1 @@
|
|||
kingdoms.db.kingdoms = {}
|
|
@ -0,0 +1,6 @@
|
|||
kingdoms.player = {}
|
||||
kingdoms.db.players = {}
|
||||
|
||||
function kingdoms.player.kingdom(name)
|
||||
return kingdoms.db.players[name]
|
||||
end
|
Binary file not shown.
After Width: | Height: | Size: 267 B |
Binary file not shown.
After Width: | Height: | Size: 138 B |
|
@ -0,0 +1,10 @@
|
|||
kingdoms.utils = {}
|
||||
|
||||
-- Return a UUID-like identifier.
|
||||
function kingdoms.utils.uniqueid()
|
||||
s = ("%x"):format(math.random(0, 0xFFFF))
|
||||
for i=2,10 do
|
||||
s = s .. ("-%x"):format(math.random(0, 0xFFFF))
|
||||
end
|
||||
return s
|
||||
end
|
Loading…
Reference in New Issue