2 --Copyright (C) 2014 sapier
4 --This program is free software; you can redistribute it and/or modify
5 --it under the terms of the GNU Lesser General Public License as published by
6 --the Free Software Foundation; either version 2.1 of the License, or
7 --(at your option) any later version.
9 --This program is distributed in the hope that it will be useful,
10 --but WITHOUT ANY WARRANTY; without even the implied warranty of
11 --MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 --GNU Lesser General Public License for more details.
14 --You should have received a copy of the GNU Lesser General Public License along
15 --with this program; if not, write to the Free Software Foundation, Inc.,
16 --51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 -- Local cached values
22 local min_supp_proto, max_supp_proto
24 function common_update_cached_supp_proto()
25 min_supp_proto = core.get_min_supp_proto()
26 max_supp_proto = core.get_max_supp_proto()
28 common_update_cached_supp_proto()
30 -- Menu helper functions
32 local function render_client_count(n)
33 if n > 999 then return '99+'
34 elseif n >= 0 then return tostring(n)
38 local function configure_selected_world_params(idx)
39 local worldconfig = pkgmgr.get_worldconfig(menudata.worldlist:get_list()[idx].path)
40 if worldconfig.creative_mode then
41 core.settings:set("creative_mode", worldconfig.creative_mode)
43 if worldconfig.enable_damage then
44 core.settings:set("enable_damage", worldconfig.enable_damage)
48 function render_serverlist_row(spec)
51 text = text .. core.formspec_escape(spec.name:trim())
52 elseif spec.address then
53 text = text .. core.formspec_escape(spec.address:trim())
55 text = text .. ":" .. spec.port
59 local grey_out = not spec.is_compatible
63 if spec.lag or spec.ping then
64 local lag = (spec.lag or 0) * 1000 + (spec.ping or 0) * 250
66 table.insert(details, "1")
67 elseif lag <= 175 then
68 table.insert(details, "2")
69 elseif lag <= 250 then
70 table.insert(details, "3")
72 table.insert(details, "4")
75 table.insert(details, "0")
78 table.insert(details, ",")
80 local color = (grey_out and "#aaaaaa") or ((spec.is_favorite and "#ddddaa") or "#ffffff")
81 if spec.clients and (spec.clients_max or 0) > 0 then
82 local clients_percent = 100 * spec.clients / spec.clients_max
84 -- Choose a color depending on how many clients are connected
85 -- (relatively to clients_max)
87 if grey_out then clients_color = '#aaaaaa'
88 elseif spec.clients == 0 then clients_color = '' -- 0 players: default/white
89 elseif clients_percent <= 60 then clients_color = '#a1e587' -- 0-60%: green
90 elseif clients_percent <= 90 then clients_color = '#ffdc97' -- 60-90%: yellow
91 elseif clients_percent == 100 then clients_color = '#dd5b5b' -- full server: red (darker)
92 else clients_color = '#ffba97' -- 90-100%: orange
95 table.insert(details, clients_color)
96 table.insert(details, render_client_count(spec.clients) .. " / " ..
97 render_client_count(spec.clients_max))
99 table.insert(details, color)
100 table.insert(details, "?")
103 if spec.creative then
104 table.insert(details, "1") -- creative icon
106 table.insert(details, "0")
110 table.insert(details, "2") -- pvp icon
111 elseif spec.damage then
112 table.insert(details, "1") -- heart icon
114 table.insert(details, "0")
117 table.insert(details, color)
118 table.insert(details, text)
120 return table.concat(details, ",")
123 --------------------------------------------------------------------------------
124 os.tempfolder = function()
125 local temp = core.get_temp_path()
126 return temp .. DIR_DELIM .. "MT_" .. math.random(0, 10000)
129 os.tmpname = function()
130 local path = os.tempfolder()
131 io.open(path, "w"):close()
134 --------------------------------------------------------------------------------
136 function menu_render_worldlist()
138 local current_worldlist = menudata.worldlist:get_list()
140 for i, v in ipairs(current_worldlist) do
141 if retval ~= "" then retval = retval .. "," end
142 retval = retval .. core.formspec_escape(v.name) ..
143 " \\[" .. core.formspec_escape(v.gameid) .. "\\]"
149 function menu_handle_key_up_down(fields, textlist, settingname)
150 local oldidx, newidx = core.get_textlist_index(textlist), 1
151 if fields.key_up or fields.key_down then
152 if fields.key_up and oldidx and oldidx > 1 then
154 elseif fields.key_down and oldidx and
155 oldidx < menudata.worldlist:size() then
158 core.settings:set(settingname, menudata.worldlist:get_raw_index(newidx))
159 configure_selected_world_params(newidx)
165 function text2textlist(xpos, ypos, width, height, tl_name, textlen, text, transparency)
166 local textlines = core.wrap_text(text, textlen, true)
167 local retval = "textlist[" .. xpos .. "," .. ypos .. ";" .. width ..
168 "," .. height .. ";" .. tl_name .. ";"
170 for i = 1, #textlines do
171 textlines[i] = textlines[i]:gsub("\r", "")
172 retval = retval .. core.formspec_escape(textlines[i]) .. ","
175 retval = retval .. ";0;"
176 if transparency then retval = retval .. "true" end
177 retval = retval .. "]"
182 function is_server_protocol_compat(server_proto_min, server_proto_max)
183 if (not server_proto_min) or (not server_proto_max) then
184 -- There is no info. Assume the best and act as if we would be compatible.
187 return min_supp_proto <= server_proto_max and max_supp_proto >= server_proto_min
190 function is_server_protocol_compat_or_error(server_proto_min, server_proto_max)
191 if not is_server_protocol_compat(server_proto_min, server_proto_max) then
192 local server_prot_ver_info, client_prot_ver_info
193 local s_p_min = server_proto_min
194 local s_p_max = server_proto_max
196 if s_p_min ~= s_p_max then
197 server_prot_ver_info = fgettext_ne("Server supports protocol versions between $1 and $2. ",
200 server_prot_ver_info = fgettext_ne("Server enforces protocol version $1. ",
203 if min_supp_proto ~= max_supp_proto then
204 client_prot_ver_info= fgettext_ne("We support protocol versions between version $1 and $2.",
205 min_supp_proto, max_supp_proto)
207 client_prot_ver_info = fgettext_ne("We only support protocol version $1.", min_supp_proto)
209 gamedata.errormessage = fgettext_ne("Protocol version mismatch. ")
210 .. server_prot_ver_info
211 .. client_prot_ver_info
218 function menu_worldmt(selected, setting, value)
219 local world = menudata.worldlist:get_list()[selected]
221 local filename = world.path .. DIR_DELIM .. "world.mt"
222 local world_conf = Settings(filename)
225 if not world_conf:write() then
226 core.log("error", "Failed to write world config file")
228 world_conf:set(setting, value)
231 return world_conf:get(setting)
238 function menu_worldmt_legacy(selected)
239 local modes_names = {"creative_mode", "enable_damage", "server_announce"}
240 for _, mode_name in pairs(modes_names) do
241 local mode_val = menu_worldmt(selected, mode_name)
243 core.settings:set(mode_name, mode_val)
245 menu_worldmt(selected, mode_name, core.settings:get(mode_name))