aboutsummaryrefslogtreecommitdiff
path: root/src/quicktune.h
blob: a733ccda4795e14a58061e65459d8134f2b5af78 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/*
Minetest-c55
Copyright (C) 2012 celeron55, Perttu Ahola <celeron55@gmail.com>

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/

/*
	Used for tuning constants when developing.

	Eg. if you have this constant somewhere that you just can't get right
	by changing it and recompiling all over again:
		v3f wield_position = v3f(55, -35, 65);
	
	Make it look like this:
		v3f wield_position = v3f(55, -35, 65);
		QUICKTUNE_AUTONAME(QVT_FLOAT, wield_position.X, 0, 100);
		QUICKTUNE_AUTONAME(QVT_FLOAT, wield_position.Y, -80, 20);
		QUICKTUNE_AUTONAME(QVT_FLOAT, wield_position.Z, 0, 100);

	Then you can modify the values at runtime, using the keys
		keymap_quicktune_prev
		keymap_quicktune_next
		keymap_quicktune_dec
		keymap_quicktune_inc
	
	Once you have modified the values at runtime and then quit, the game
	will print out all the modified values at the end:
		Modified quicktune values:
		wield_position.X = 60
		wield_position.Y = -30
		wield_position.Z = 65
	
	The QUICKTUNE macros shouldn't generally be left in committed code.
*/

#ifndef QUICKTUNE_HEADER
#define QUICKTUNE_HEADER

#include <string>
#include <map>
#include <vector>

enum QuicktuneValueType{
	QVT_NONE,
	QVT_FLOAT
};
struct QuicktuneValue
{
	QuicktuneValueType type;
	union{
		struct{
			float current;
			float min;
			float max;
		} value_QVT_FLOAT;
	};
	bool modified;

	QuicktuneValue():
		type(QVT_NONE),
		modified(false)
	{}
	std::string getString();
	void relativeAdd(float amount);
};

std::vector<std::string> getQuicktuneNames();
QuicktuneValue getQuicktuneValue(const std::string &name);
void setQuicktuneValue(const std::string &name, const QuicktuneValue &val);

void updateQuicktuneValue(const std::string &name, QuicktuneValue &val);

#ifndef NDEBUG
	#define QUICKTUNE(type_, var, min_, max_, name){\
		QuicktuneValue qv;\
		qv.type = type_;\
		qv.value_##type_.current = var;\
		qv.value_##type_.min = min_;\
		qv.value_##type_.max = max_;\
		updateQuicktuneValue(name, qv);\
		var = qv.value_##type_.current;\
	}
#else // NDEBUG
	#define QUICKTUNE(type, var, min_, max_, name){}
#endif

#define QUICKTUNE_AUTONAME(type_, var, min_, max_)\
	QUICKTUNE(type_, var, min_, max_, #var)

#endif

ass="hl kwd">privs_to_string({a=true,b=true}) == "a,b") core.auth_file_path = core.get_worldpath().."/auth.txt" core.auth_table = {} local function read_auth_file() local newtable = {} local file, errmsg = io.open(core.auth_file_path, 'rb') if not file then core.log("info", core.auth_file_path.." could not be opened for reading ("..errmsg.."); assuming new world") return end for line in file:lines() do if line ~= "" then local fields = line:split(":", true) local name, password, privilege_string, last_login = unpack(fields) last_login = tonumber(last_login) if not (name and password and privilege_string) then error("Invalid line in auth.txt: "..dump(line)) end local privileges = core.string_to_privs(privilege_string) newtable[name] = {password=password, privileges=privileges, last_login=last_login} end end io.close(file) core.auth_table = newtable core.notify_authentication_modified() end local function save_auth_file() local newtable = {} -- Check table for validness before attempting to save for name, stuff in pairs(core.auth_table) do assert(type(name) == "string") assert(name ~= "") assert(type(stuff) == "table") assert(type(stuff.password) == "string") assert(type(stuff.privileges) == "table") assert(stuff.last_login == nil or type(stuff.last_login) == "number") end local file, errmsg = io.open(core.auth_file_path, 'w+b') if not file then error(core.auth_file_path.." could not be opened for writing: "..errmsg) end for name, stuff in pairs(core.auth_table) do local priv_string = core.privs_to_string(stuff.privileges) local parts = {name, stuff.password, priv_string, stuff.last_login or ""} file:write(table.concat(parts, ":").."\n") end io.close(file) end read_auth_file() core.builtin_auth_handler = { get_auth = function(name) assert(type(name) == "string") -- Figure out what password to use for a new player (singleplayer -- always has an empty password, otherwise use default, which is -- usually empty too) local new_password_hash = "" -- If not in authentication table, return nil if not core.auth_table[name] then return nil end -- Figure out what privileges the player should have. -- Take a copy of the privilege table local privileges = {} for priv, _ in pairs(core.auth_table[name].privileges) do privileges[priv] = true end -- If singleplayer, give all privileges except those marked as give_to_singleplayer = false if core.is_singleplayer() then for priv, def in pairs(core.registered_privileges) do if def.give_to_singleplayer then privileges[priv] = true end end -- For the admin, give everything elseif name == core.settings:get("name") then for priv, def in pairs(core.registered_privileges) do privileges[priv] = true end end -- All done return { password = core.auth_table[name].password, privileges = privileges, -- Is set to nil if unknown last_login = core.auth_table[name].last_login, } end, create_auth = function(name, password) assert(type(name) == "string") assert(type(password) == "string") core.log('info', "Built-in authentication handler adding player '"..name.."'") core.auth_table[name] = { password = password, privileges = core.string_to_privs(core.settings:get("default_privs")), last_login = os.time(), } save_auth_file() end, set_password = function(name, password) assert(type(name) == "string") assert(type(password) == "string") if not core.auth_table[name] then core.builtin_auth_handler.create_auth(name, password) else core.log('info', "Built-in authentication handler setting password of player '"..name.."'") core.auth_table[name].password = password save_auth_file() end return true end, set_privileges = function(name, privileges) assert(type(name) == "string") assert(type(privileges) == "table") if not core.auth_table[name] then core.builtin_auth_handler.create_auth(name, core.get_password_hash(name, core.settings:get("default_password"))) end core.auth_table[name].privileges = privileges core.notify_authentication_modified(name) save_auth_file() end, reload = function() read_auth_file() return true end, record_login = function(name) assert(type(name) == "string") assert(core.auth_table[name]).last_login = os.time() save_auth_file() end, } function core.register_authentication_handler(handler) if core.registered_auth_handler then error("Add-on authentication handler already registered by "..core.registered_auth_handler_modname) end core.registered_auth_handler = handler core.registered_auth_handler_modname = core.get_current_modname() handler.mod_origin = core.registered_auth_handler_modname end function core.get_auth_handler() return core.registered_auth_handler or core.builtin_auth_handler end local function auth_pass(name) return function(...) local auth_handler = core.get_auth_handler() if auth_handler[name] then return auth_handler[name](...) end return false end end core.set_player_password = auth_pass("set_password") core.set_player_privs = auth_pass("set_privileges") core.auth_reload = auth_pass("reload") local record_login = auth_pass("record_login") core.register_on_joinplayer(function(player) record_login(player:get_player_name()) end) core.register_on_prejoinplayer(function(name, ip) local auth = core.auth_table if auth[name] ~= nil then return end local name_lower = name:lower() for k in pairs(auth) do if k:lower() == name_lower then return string.format("\nCannot create new player called '%s'. ".. "Another account called '%s' is already registered. ".. "Please check the spelling if it's your account ".. "or use a different nickname.", name, k) end end end)