]> git.lizzy.rs Git - dragonfireclient.git/blob - builtin/mainmenu/common.lua
01f9a30b99995621e11ecd3f5b7a7902e3120a90
[dragonfireclient.git] / builtin / mainmenu / common.lua
1 --Minetest
2 --Copyright (C) 2014 sapier
3 --
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.
8 --
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.
13 --
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.
17 --------------------------------------------------------------------------------
18 -- Global menu data
19 --------------------------------------------------------------------------------
20 menudata = {}
21
22 --------------------------------------------------------------------------------
23 -- Local cached values
24 --------------------------------------------------------------------------------
25 local min_supp_proto, max_supp_proto
26
27 function common_update_cached_supp_proto()
28         min_supp_proto = core.get_min_supp_proto()
29         max_supp_proto = core.get_max_supp_proto()
30 end
31 common_update_cached_supp_proto()
32 --------------------------------------------------------------------------------
33 -- Menu helper functions
34 --------------------------------------------------------------------------------
35
36 --------------------------------------------------------------------------------
37 local function render_client_count(n)
38         if     n > 99 then return '99+'
39         elseif n >= 0 then return tostring(n)
40         else return '?' end
41 end
42
43 local function configure_selected_world_params(idx)
44         local worldconfig = pkgmgr.get_worldconfig(menudata.worldlist:get_list()[idx].path)
45         if worldconfig.creative_mode then
46                 core.settings:set("creative_mode", worldconfig.creative_mode)
47         end
48         if worldconfig.enable_damage then
49                 core.settings:set("enable_damage", worldconfig.enable_damage)
50         end
51 end
52
53 --------------------------------------------------------------------------------
54 function image_column(tooltip, flagname)
55         return "image,tooltip=" .. core.formspec_escape(tooltip) .. "," ..
56                 "0=" .. core.formspec_escape(defaulttexturedir .. "blank.png") .. "," ..
57                 "1=" .. core.formspec_escape(defaulttexturedir ..
58                         (flagname and "server_flags_" .. flagname .. ".png" or "blank.png")) .. "," ..
59                 "2=" .. core.formspec_escape(defaulttexturedir .. "server_ping_4.png") .. "," ..
60                 "3=" .. core.formspec_escape(defaulttexturedir .. "server_ping_3.png") .. "," ..
61                 "4=" .. core.formspec_escape(defaulttexturedir .. "server_ping_2.png") .. "," ..
62                 "5=" .. core.formspec_escape(defaulttexturedir .. "server_ping_1.png")
63 end
64
65
66 --------------------------------------------------------------------------------
67 function render_serverlist_row(spec, is_favorite)
68         local text = ""
69         if spec.name then
70                 text = text .. core.formspec_escape(spec.name:trim())
71         elseif spec.address then
72                 text = text .. core.formspec_escape(spec.address:trim())
73                 if spec.port then
74                         text = text .. ":" .. spec.port
75                 end
76         end
77
78         local grey_out = not is_server_protocol_compat(spec.proto_min, spec.proto_max)
79
80         local details
81         if is_favorite then
82                 details = "1,"
83         else
84                 details = "0,"
85         end
86
87         if spec.ping then
88                 local ping = spec.ping * 1000
89                 if ping <= 50 then
90                         details = details .. "2,"
91                 elseif ping <= 100 then
92                         details = details .. "3,"
93                 elseif ping <= 250 then
94                         details = details .. "4,"
95                 else
96                         details = details .. "5,"
97                 end
98         else
99                 details = details .. "0,"
100         end
101
102         if spec.clients and spec.clients_max then
103                 local clients_percent = 100 * spec.clients / spec.clients_max
104
105                 -- Choose a color depending on how many clients are connected
106                 -- (relatively to clients_max)
107                 local clients_color
108                 if     grey_out               then clients_color = '#aaaaaa'
109                 elseif spec.clients == 0      then clients_color = ''        -- 0 players: default/white
110                 elseif clients_percent <= 60  then clients_color = '#a1e587' -- 0-60%: green
111                 elseif clients_percent <= 90  then clients_color = '#ffdc97' -- 60-90%: yellow
112                 elseif clients_percent == 100 then clients_color = '#dd5b5b' -- full server: red (darker)
113                 else                               clients_color = '#ffba97' -- 90-100%: orange
114                 end
115
116                 details = details .. clients_color .. ',' ..
117                         render_client_count(spec.clients) .. ',/,' ..
118                         render_client_count(spec.clients_max) .. ','
119
120         elseif grey_out then
121                 details = details .. '#aaaaaa,?,/,?,'
122         else
123                 details = details .. ',?,/,?,'
124         end
125
126         if spec.creative then
127                 details = details .. "1,"
128         else
129                 details = details .. "0,"
130         end
131
132         if spec.damage then
133                 details = details .. "1,"
134         else
135                 details = details .. "0,"
136         end
137
138         if spec.pvp then
139                 details = details .. "1,"
140         else
141                 details = details .. "0,"
142         end
143
144         return details .. (grey_out and '#aaaaaa,' or ',') .. text
145 end
146
147 --------------------------------------------------------------------------------
148 os.tempfolder = function()
149         if core.settings:get("TMPFolder") then
150                 return core.settings:get("TMPFolder") .. DIR_DELIM .. "MT_" .. math.random(0,10000)
151         end
152
153         local filetocheck = os.tmpname()
154         os.remove(filetocheck)
155
156         -- luacheck: ignore
157         -- https://blogs.msdn.microsoft.com/vcblog/2014/06/18/c-runtime-crt-features-fixes-and-breaking-changes-in-visual-studio-14-ctp1/
158         --   The C runtime (CRT) function called by os.tmpname is tmpnam.
159         --   Microsofts tmpnam implementation in older CRT / MSVC releases is defective.
160         --   tmpnam return values starting with a backslash characterize this behavior.
161         -- https://sourceforge.net/p/mingw-w64/bugs/555/
162         --   MinGW tmpnam implementation is forwarded to the CRT directly.
163         -- https://sourceforge.net/p/mingw-w64/discussion/723797/thread/55520785/
164         --   MinGW links to an older CRT release (msvcrt.dll).
165         --   Due to legal concerns MinGW will never use a newer CRT.
166         --
167         --   Make use of TEMP to compose the temporary filename if an old
168         --   style tmpnam return value is detected.
169         if filetocheck:sub(1, 1) == "\\" then
170                 local tempfolder = os.getenv("TEMP")
171                 return tempfolder .. filetocheck
172         end
173
174         local randname = "MTTempModFolder_" .. math.random(0,10000)
175         local backstring = filetocheck:reverse()
176         return filetocheck:sub(0, filetocheck:len() - backstring:find(DIR_DELIM) + 1) ..
177                 randname
178 end
179
180 --------------------------------------------------------------------------------
181 function menu_render_worldlist()
182         local retval = ""
183         local current_worldlist = menudata.worldlist:get_list()
184
185         for i, v in ipairs(current_worldlist) do
186                 if retval ~= "" then retval = retval .. "," end
187                 retval = retval .. core.formspec_escape(v.name) ..
188                                 " \\[" .. core.formspec_escape(v.gameid) .. "\\]"
189         end
190
191         return retval
192 end
193
194 --------------------------------------------------------------------------------
195 function menu_handle_key_up_down(fields, textlist, settingname)
196         local oldidx, newidx = core.get_textlist_index(textlist), 1
197         if fields.key_up or fields.key_down then
198                 if fields.key_up and oldidx and oldidx > 1 then
199                         newidx = oldidx - 1
200                 elseif fields.key_down and oldidx and
201                                 oldidx < menudata.worldlist:size() then
202                         newidx = oldidx + 1
203                 end
204                 core.settings:set(settingname, menudata.worldlist:get_raw_index(newidx))
205                 configure_selected_world_params(newidx)
206                 return true
207         end
208         return false
209 end
210
211 --------------------------------------------------------------------------------
212 function text2textlist(xpos, ypos, width, height, tl_name, textlen, text, transparency)
213         local textlines = core.wrap_text(text, textlen, true)
214         local retval = "textlist[" .. xpos .. "," .. ypos .. ";" .. width ..
215                         "," .. height .. ";" .. tl_name .. ";"
216
217         for i = 1, #textlines do
218                 textlines[i] = textlines[i]:gsub("\r", "")
219                 retval = retval .. core.formspec_escape(textlines[i]) .. ","
220         end
221
222         retval = retval .. ";0;"
223         if transparency then retval = retval .. "true" end
224         retval = retval .. "]"
225
226         return retval
227 end
228
229 --------------------------------------------------------------------------------
230 function is_server_protocol_compat(server_proto_min, server_proto_max)
231         if (not server_proto_min) or (not server_proto_max) then
232                 -- There is no info. Assume the best and act as if we would be compatible.
233                 return true
234         end
235         return min_supp_proto <= server_proto_max and max_supp_proto >= server_proto_min
236 end
237 --------------------------------------------------------------------------------
238 function is_server_protocol_compat_or_error(server_proto_min, server_proto_max)
239         if not is_server_protocol_compat(server_proto_min, server_proto_max) then
240                 local server_prot_ver_info, client_prot_ver_info
241                 local s_p_min = server_proto_min
242                 local s_p_max = server_proto_max
243
244                 if s_p_min ~= s_p_max then
245                         server_prot_ver_info = fgettext_ne("Server supports protocol versions between $1 and $2. ",
246                                 s_p_min, s_p_max)
247                 else
248                         server_prot_ver_info = fgettext_ne("Server enforces protocol version $1. ",
249                                 s_p_min)
250                 end
251                 if min_supp_proto ~= max_supp_proto then
252                         client_prot_ver_info= fgettext_ne("We support protocol versions between version $1 and $2.",
253                                 min_supp_proto, max_supp_proto)
254                 else
255                         client_prot_ver_info = fgettext_ne("We only support protocol version $1.", min_supp_proto)
256                 end
257                 gamedata.errormessage = fgettext_ne("Protocol version mismatch. ")
258                         .. server_prot_ver_info
259                         .. client_prot_ver_info
260                 return false
261         end
262
263         return true
264 end
265 --------------------------------------------------------------------------------
266 function menu_worldmt(selected, setting, value)
267         local world = menudata.worldlist:get_list()[selected]
268         if world then
269                 local filename = world.path .. DIR_DELIM .. "world.mt"
270                 local world_conf = Settings(filename)
271
272                 if value then
273                         if not world_conf:write() then
274                                 core.log("error", "Failed to write world config file")
275                         end
276                         world_conf:set(setting, value)
277                         world_conf:write()
278                 else
279                         return world_conf:get(setting)
280                 end
281         else
282                 return nil
283         end
284 end
285
286 function menu_worldmt_legacy(selected)
287         local modes_names = {"creative_mode", "enable_damage", "server_announce"}
288         for _, mode_name in pairs(modes_names) do
289                 local mode_val = menu_worldmt(selected, mode_name)
290                 if mode_val then
291                         core.settings:set(mode_name, mode_val)
292                 else
293                         menu_worldmt(selected, mode_name, core.settings:get(mode_name))
294                 end
295         end
296 end