aboutsummaryrefslogtreecommitdiff
path: root/builtin/game/forceloading.lua
blob: 7c5537e85bd134853ad7a89d01c0fb84ae59a9f0 (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
-- Prevent anyone else accessing those functions
local forceload_block = core.forceload_block
local forceload_free_block = core.forceload_free_block
core.forceload_block = nil
core.forceload_free_block = nil

local blocks_forceloaded
local blocks_temploaded = {}
local total_forceloaded = 0

local BLOCKSIZE = core.MAP_BLOCKSIZE
local function get_blockpos(pos)
	return {
		x = math.floor(pos.x/BLOCKSIZE),
		y = math.floor(pos.y/BLOCKSIZE),
		z = math.floor(pos.z/BLOCKSIZE)}
end

-- When we create/free a forceload, it's either transient or persistent. We want
-- to add to/remove from the table that corresponds to the type of forceload, but
-- we also need the other table because whether we forceload a block depends on
-- both tables.
-- This function returns the "primary" table we are adding to/removing from, and
-- the other table.
local function get_relevant_tables(transient)
	if transient then
		return blocks_temploaded, blocks_forceloaded
	else
		return blocks_forceloaded, blocks_temploaded
	end
end

function core.forceload_block(pos, transient)
	local blockpos = get_blockpos(pos)
	local hash = core.hash_node_position(blockpos)
	local relevant_table, other_table = get_relevant_tables(transient)
	if relevant_table[hash] ~= nil then
		relevant_table[hash] = relevant_table[hash] + 1
		return true
	elseif other_table[hash] ~= nil then
		relevant_table[hash] = 1
	else
		if total_forceloaded >= (tonumber(core.settings:get("max_forceloaded_blocks")) or 16) then
			return false
		end
		total_forceloaded = total_forceloaded+1
		relevant_table[hash] = 1
		forceload_block(blockpos)
		return true
	end
end

function core.forceload_free_block(pos, transient)
	local blockpos = get_blockpos(pos)
	local hash = core.hash_node_position(blockpos)
	local relevant_table, other_table = get_relevant_tables(transient)
	if relevant_table[hash] == nil then return end
	if relevant_table[hash] > 1 then
		relevant_table[hash] = relevant_table[hash] - 1
	elseif other_table[hash] ~= nil then
		relevant_table[hash] = nil
	else
		total_forceloaded = total_forceloaded-1
		relevant_table[hash] = nil
		forceload_free_block(blockpos)
	end
end

-- Keep the forceloaded areas after restart
local wpath = core.get_worldpath()
local function read_file(filename)
	local f = io.open(filename, "r")
	if f==nil then return {} end
	local t = f:read("*all")
	f:close()
	if t=="" or t==nil then return {} end
	return core.deserialize(t) or {}
end

local function write_file(filename, table)
	local f = io.open(filename, "w")
	f:write(core.serialize(table))
	f:close()
end

blocks_forceloaded = read_file(wpath.."/force_loaded.txt")
for _, __ in pairs(blocks_forceloaded) do
	total_forceloaded = total_forceloaded + 1
end

core.after(5, function()
	for hash, _ in pairs(blocks_forceloaded) do
		local blockpos = core.get_position_from_hash(hash)
		forceload_block(blockpos)
	end
end)

core.register_on_shutdown(function()
	write_file(wpath.."/force_loaded.txt", blocks_forceloaded)
end)
hl opt">++){ std::string end = *p; if(s.size() < end.size()) continue; if(s.substr(s.size()-end.size(), end.size()) == end) return s.substr(0, s.size() - end.size()); } return ""; } // Tests if two strings are equal, optionally case insensitive inline bool str_equal(const std::wstring& s1, const std::wstring& s2, bool case_insensitive = false) { if(case_insensitive) { if(s1.size() != s2.size()) return false; for(size_t i = 0; i < s1.size(); ++i) if(tolower(s1[i]) != tolower(s2[i])) return false; return true; } else { return s1 == s2; } } // Tests if the second string is a prefix of the first, optionally case insensitive inline bool str_starts_with(const std::wstring& str, const std::wstring& prefix, bool case_insensitive = false) { if(str.size() < prefix.size()) return false; if(case_insensitive) { for(size_t i = 0; i < prefix.size(); ++i) if(tolower(str[i]) != tolower(prefix[i])) return false; } else { for(size_t i = 0; i < prefix.size(); ++i) if(str[i] != prefix[i]) return false; } return true; } // Split a string using the given delimiter. Returns a vector containing // the component parts. inline std::vector<std::wstring> str_split(const std::wstring &str, wchar_t delimiter) { std::vector<std::wstring> parts; std::wstringstream sstr(str); std::wstring part; while(std::getline(sstr, part, delimiter)) parts.push_back(part); return parts; } inline std::string lowercase(const std::string &s) { std::string s2; for(size_t i=0; i<s.size(); i++) { char c = s[i]; if(c >= 'A' && c <= 'Z') c -= 'A' - 'a'; s2 += c; } return s2; } inline std::string trim(const std::string &s) { size_t front = 0; while(s[front] == ' ' || s[front] == '\t' || s[front] == '\r' || s[front] == '\n' ) ++front; size_t back = s.size(); while(back > front && (s[back-1] == ' ' || s[back-1] == '\t' || s[back-1] == '\r' || s[back-1] == '\n' ) ) --back; return s.substr(front, back - front); } inline bool is_yes(const std::string &s) { std::string s2 = lowercase(trim(s)); if(s2 == "y" || s2 == "yes" || s2 == "true" || atoi(s2.c_str()) != 0) return true; return false; } inline s32 mystoi(const std::string &s, s32 min, s32 max) { s32 i = atoi(s.c_str()); if(i < min) i = min; if(i > max) i = max; return i; } inline s64 stoi64(const std::string &s) { std::stringstream tmp(s); s64 t; tmp >> t; return t; } // MSVC2010 includes it's own versions of these //#if !defined(_MSC_VER) || _MSC_VER < 1600 inline s32 mystoi(const std::string &s) { return atoi(s.c_str()); } inline s32 mystoi(const std::wstring &s) { return atoi(wide_to_narrow(s).c_str()); } inline float mystof(const std::string &s) { // This crap causes a segfault in certain cases on MinGW /*float f; std::istringstream ss(s); ss>>f; return f;*/ // This works in that case return atof(s.c_str()); } //#endif #define stoi mystoi #define stof mystof inline std::string itos(s32 i) { std::ostringstream o; o<<i; return o.str(); } inline std::string i64tos(s64 i) { std::ostringstream o; o<<i; return o.str(); } inline std::string ftos(float f) { std::ostringstream o; o<<f; return o.str(); } inline void str_replace(std::string & str, std::string const & pattern, std::string const & replacement) { std::string::size_type start = str.find(pattern, 0); while(start != str.npos) { str.replace(start, pattern.size(), replacement); start = str.find(pattern, start+replacement.size()); } } inline void str_replace_char(std::string & str, char from, char to) { for(unsigned int i=0; i<str.size(); i++) { if(str[i] == from) str[i] = to; } } /* Checks if a string contains only supplied characters */ inline bool string_allowed(const std::string &s, const std::string &allowed_chars) { for(u32 i=0; i<s.size(); i++) { bool confirmed = false; for(u32 j=0; j<allowed_chars.size(); j++) { if(s[i] == allowed_chars[j]) { confirmed = true; break; } } if(confirmed == false) return false; } return true; } /* Checks if a string contains no blacklisted characters (opposite function of string_allowed()) */ inline bool string_allowed_blacklist(const std::string & s, const std::string & blacklisted_chars) { for(unsigned int i = 0; i < s.length(); i++) { bool invalid = false; for(unsigned int j = 0; j < blacklisted_chars.length(); j++) { if(s[i] == blacklisted_chars[j]) { invalid = true; break; } } if(invalid) return false; } return true; } /* Forcefully wraps string into rows using \n (no word wrap, used for showing paths in gui) */ inline std::string wrap_rows(const std::string &from, u32 rowlen) { std::string to; for(u32 i=0; i<from.size(); i++) { if(i != 0 && i%rowlen == 0) to += '\n'; to += from[i]; } return to; } /* Removes all \\ from a string that had been escaped (FormSpec strings) */ inline std::string unescape_string(std::string &s) { std::string res; for (size_t i = 0; i < s.length(); i++) { if (s[i] == '\\') i++; res += s[i]; } return res; } std::string translatePassword(std::string playername, std::wstring password); std::string urlencode(std::string str); std::string urldecode(std::string str); u32 readFlagString(std::string str, FlagDesc *flagdesc, u32 *flagmask); std::string writeFlagString(u32 flags, FlagDesc *flagdesc, u32 flagmask); size_t mystrlcpy(char *dst, const char *src, size_t size); char *mystrtok_r(char *s, const char *sep, char **lasts); u64 read_seed(const char *str); #endif