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, ",")
122 ---------------------------------------------------------------------------------
123 os.tmpname = function()
124 error('do not use') -- instead use core.get_temp_path()
126 --------------------------------------------------------------------------------
128 function menu_render_worldlist(show_gameid)
130 local current_worldlist = menudata.worldlist:get_list()
133 for i, v in ipairs(current_worldlist) do
135 if show_gameid == nil or show_gameid == true then
136 row = row .. " [" .. v.gameid .. "]"
138 retval[#retval+1] = core.formspec_escape(row)
142 return table.concat(retval, ",")
145 function menu_handle_key_up_down(fields, textlist, settingname)
146 local oldidx, newidx = core.get_textlist_index(textlist), 1
147 if fields.key_up or fields.key_down then
148 if fields.key_up and oldidx and oldidx > 1 then
150 elseif fields.key_down and oldidx and
151 oldidx < menudata.worldlist:size() then
154 core.settings:set(settingname, menudata.worldlist:get_raw_index(newidx))
155 configure_selected_world_params(newidx)
161 function text2textlist(xpos, ypos, width, height, tl_name, textlen, text, transparency)
162 local textlines = core.wrap_text(text, textlen, true)
163 local retval = "textlist[" .. xpos .. "," .. ypos .. ";" .. width ..
164 "," .. height .. ";" .. tl_name .. ";"
166 for i = 1, #textlines do
167 textlines[i] = textlines[i]:gsub("\r", "")
168 retval = retval .. core.formspec_escape(textlines[i]) .. ","
171 retval = retval .. ";0;"
172 if transparency then retval = retval .. "true" end
173 retval = retval .. "]"
178 function is_server_protocol_compat(server_proto_min, server_proto_max)
179 if (not server_proto_min) or (not server_proto_max) then
180 -- There is no info. Assume the best and act as if we would be compatible.
183 return min_supp_proto <= server_proto_max and max_supp_proto >= server_proto_min
186 function is_server_protocol_compat_or_error(server_proto_min, server_proto_max)
187 if not is_server_protocol_compat(server_proto_min, server_proto_max) then
188 local server_prot_ver_info, client_prot_ver_info
189 local s_p_min = server_proto_min
190 local s_p_max = server_proto_max
192 if s_p_min ~= s_p_max then
193 server_prot_ver_info = fgettext_ne("Server supports protocol versions between $1 and $2. ",
196 server_prot_ver_info = fgettext_ne("Server enforces protocol version $1. ",
199 if min_supp_proto ~= max_supp_proto then
200 client_prot_ver_info= fgettext_ne("We support protocol versions between version $1 and $2.",
201 min_supp_proto, max_supp_proto)
203 client_prot_ver_info = fgettext_ne("We only support protocol version $1.", min_supp_proto)
205 gamedata.errormessage = fgettext_ne("Protocol version mismatch. ")
206 .. server_prot_ver_info
207 .. client_prot_ver_info
214 function menu_worldmt(selected, setting, value)
215 local world = menudata.worldlist:get_list()[selected]
217 local filename = world.path .. DIR_DELIM .. "world.mt"
218 local world_conf = Settings(filename)
221 if not world_conf:write() then
222 core.log("error", "Failed to write world config file")
224 world_conf:set(setting, value)
227 return world_conf:get(setting)
234 function menu_worldmt_legacy(selected)
235 local modes_names = {"creative_mode", "enable_damage", "server_announce"}
236 for _, mode_name in pairs(modes_names) do
237 local mode_val = menu_worldmt(selected, mode_name)
239 core.settings:set(mode_name, mode_val)
241 menu_worldmt(selected, mode_name, core.settings:get(mode_name))