Make `options` local here.
[oweals/minetest.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
26 local max_supp_proto
27
28 function common_update_cached_supp_proto()
29         min_supp_proto = core.get_min_supp_proto()
30         max_supp_proto = core.get_max_supp_proto()
31 end
32 common_update_cached_supp_proto()
33 --------------------------------------------------------------------------------
34 -- Menu helper functions
35 --------------------------------------------------------------------------------
36
37 --------------------------------------------------------------------------------
38 local function render_client_count(n)
39         if n > 99 then
40                 return '99+'
41         elseif n >= 0 then
42                 return tostring(n)
43         else
44                 return '?'
45         end
46 end
47
48 local function configure_selected_world_params(idx)
49         local worldconfig = modmgr.get_worldconfig(
50                 menudata.worldlist:get_list()[idx].path)
51
52         if worldconfig.creative_mode ~= nil then
53                 core.setting_set("creative_mode", worldconfig.creative_mode)
54         end
55         if worldconfig.enable_damage ~= nil then
56                 core.setting_set("enable_damage", worldconfig.enable_damage)
57         end
58 end
59
60 --------------------------------------------------------------------------------
61 function image_column(tooltip, flagname)
62         return "image," ..
63                 "tooltip=" .. core.formspec_escape(tooltip) .. "," ..
64                 "0=" .. core.formspec_escape(defaulttexturedir .. "blank.png") .. "," ..
65                 "1=" .. core.formspec_escape(defaulttexturedir .. "server_flags_" .. flagname .. ".png")
66 end
67
68 --------------------------------------------------------------------------------
69 function order_favorite_list(list)
70         local res = {}
71         --orders the favorite list after support
72         for i=1,#list,1 do
73                 local fav = list[i]
74                 if is_server_protocol_compat(fav.proto_min, fav.proto_max) then
75                         res[#res + 1] = fav
76                 end
77         end
78         for i=1,#list,1 do
79                 local fav = list[i]
80                 if not is_server_protocol_compat(fav.proto_min, fav.proto_max) then
81                         res[#res + 1] = fav
82                 end
83         end
84         return res
85 end
86
87 --------------------------------------------------------------------------------
88 function render_favorite(spec,render_details)
89         local text = ""
90
91         if spec.name ~= nil then
92                 text = text .. core.formspec_escape(spec.name:trim())
93
94 --              if spec.description ~= nil and
95 --                      core.formspec_escape(spec.description):trim() ~= "" then
96 --                      text = text .. " (" .. core.formspec_escape(spec.description) .. ")"
97 --              end
98         else
99                 if spec.address ~= nil then
100                         text = text .. spec.address:trim()
101
102                         if spec.port ~= nil then
103                                 text = text .. ":" .. spec.port
104                         end
105                 end
106         end
107
108         if not render_details then
109                 return text
110         end
111
112         local details = ""
113         local grey_out = not is_server_protocol_compat(spec.proto_min, spec.proto_max)
114
115         if spec.clients ~= nil and spec.clients_max ~= nil then
116                 local clients_color = ''
117                 local clients_percent = 100 * spec.clients / spec.clients_max
118
119                 -- Choose a color depending on how many clients are connected
120                 -- (relatively to clients_max)
121                 if spec.clients == 0 then
122                         clients_color = ''        -- 0 players: default/white
123                 elseif spec.clients == spec.clients_max then
124                         clients_color = '#dd5b5b' -- full server: red (darker)
125                 elseif clients_percent <= 60 then
126                         clients_color = '#a1e587' -- 0-60%: green
127                 elseif clients_percent <= 90 then
128                         clients_color = '#ffdc97' -- 60-90%: yellow
129                 else
130                         clients_color = '#ffba97' -- 90-100%: orange
131                 end
132
133                 if grey_out then
134                         clients_color = '#aaaaaa'
135                 end
136
137                 details = details ..
138                                 clients_color .. ',' ..
139                                 render_client_count(spec.clients) .. ',' ..
140                                 '/,' ..
141                                 render_client_count(spec.clients_max) .. ','
142         elseif grey_out then
143                 details = details .. '#aaaaaa,?,/,?,'
144         else
145                 details = details .. ',?,/,?,'
146         end
147
148         if spec.creative then
149                 details = details .. "1,"
150         else
151                 details = details .. "0,"
152         end
153
154         if spec.damage then
155                 details = details .. "1,"
156         else
157                 details = details .. "0,"
158         end
159
160         if spec.pvp then
161                 details = details .. "1,"
162         else
163                 details = details .. "0,"
164         end
165
166         return details .. (grey_out and '#aaaaaa,' or ',') .. text
167 end
168
169 --------------------------------------------------------------------------------
170 os.tempfolder = function()
171         if core.setting_get("TMPFolder") then
172                 return core.setting_get("TMPFolder") .. DIR_DELIM .. "MT_" .. math.random(0,10000)
173         end
174
175         local filetocheck = os.tmpname()
176         os.remove(filetocheck)
177
178         local randname = "MTTempModFolder_" .. math.random(0,10000)
179         if DIR_DELIM == "\\" then
180                 local tempfolder = os.getenv("TEMP")
181                 return tempfolder .. filetocheck
182         else
183                 local backstring = filetocheck:reverse()
184                 return filetocheck:sub(0,filetocheck:len()-backstring:find(DIR_DELIM)+1) ..randname
185         end
186
187 end
188
189 --------------------------------------------------------------------------------
190 function menu_render_worldlist()
191         local retval = ""
192
193         local current_worldlist = menudata.worldlist:get_list()
194
195         for i,v in ipairs(current_worldlist) do
196                 if retval ~= "" then
197                         retval = retval ..","
198                 end
199
200                 retval = retval .. core.formspec_escape(v.name) ..
201                                         " \\[" .. core.formspec_escape(v.gameid) .. "\\]"
202         end
203
204         return retval
205 end
206
207 --------------------------------------------------------------------------------
208 function menu_handle_key_up_down(fields,textlist,settingname)
209         if fields["key_up"] then
210                 local oldidx = core.get_textlist_index(textlist)
211
212                 if oldidx ~= nil and oldidx > 1 then
213                         local newidx = oldidx -1
214                         core.setting_set(settingname,
215                                 menudata.worldlist:get_raw_index(newidx))
216
217                         configure_selected_world_params(newidx)
218                 end
219                 return true
220         end
221
222         if fields["key_down"] then
223                 local oldidx = core.get_textlist_index(textlist)
224
225                 if oldidx ~= nil and oldidx < menudata.worldlist:size() then
226                         local newidx = oldidx + 1
227                         core.setting_set(settingname,
228                                 menudata.worldlist:get_raw_index(newidx))
229
230                         configure_selected_world_params(newidx)
231                 end
232
233                 return true
234         end
235
236         return false
237 end
238
239 --------------------------------------------------------------------------------
240 function asyncOnlineFavourites()
241
242         if not menudata.public_known then
243                 menudata.public_known = {{
244                         name = fgettext("Loading..."),
245                         description = fgettext_ne("Try reenabling public serverlist and check your internet connection.")
246                 }}
247         end
248         menudata.favorites = menudata.public_known
249         core.handle_async(
250                 function(param)
251                         return core.get_favorites("online")
252                 end,
253                 nil,
254                 function(result)
255                         if core.setting_getbool("public_serverlist") then
256                                 local favs = order_favorite_list(result)
257                                 if favs[1] then
258                                         menudata.public_known = favs
259                                         menudata.favorites = menudata.public_known
260                                 end
261                                 core.event_handler("Refresh")
262                         end
263                 end
264         )
265 end
266
267 --------------------------------------------------------------------------------
268 function text2textlist(xpos,ypos,width,height,tl_name,textlen,text,transparency)
269         local textlines = core.splittext(text,textlen)
270
271         local retval = "textlist[" .. xpos .. "," .. ypos .. ";"
272                                                                 .. width .. "," .. height .. ";"
273                                                                 .. tl_name .. ";"
274
275         for i=1, #textlines, 1 do
276                 textlines[i] = textlines[i]:gsub("\r","")
277                 retval = retval .. core.formspec_escape(textlines[i]) .. ","
278         end
279
280         retval = retval .. ";0;"
281
282         if transparency then
283                 retval = retval .. "true"
284         end
285
286         retval = retval .. "]"
287
288         return retval
289 end
290
291 --------------------------------------------------------------------------------
292 function is_server_protocol_compat(server_proto_min, server_proto_max)
293         return not ((min_supp_proto > (server_proto_max or 24)) or (max_supp_proto < (server_proto_min or 13)))
294 end
295 --------------------------------------------------------------------------------
296 function is_server_protocol_compat_or_error(server_proto_min, server_proto_max)
297         if not is_server_protocol_compat(server_proto_min, server_proto_max) then
298                 local server_prot_ver_info
299                 local client_prot_ver_info
300                 if server_proto_min ~= server_proto_max then
301                         server_prot_ver_info = fgettext_ne("Server supports protocol versions between $1 and $2. ",
302                                 server_proto_min or 13, server_proto_max or 24)
303                 else
304                         server_prot_ver_info = fgettext_ne("Server enforces protocol version $1. ",
305                                 server_proto_min or 13)
306                 end
307                 if min_supp_proto ~= max_supp_proto then
308                         client_prot_ver_info= fgettext_ne("We support protocol versions between version $1 and $2.",
309                                 min_supp_proto, max_supp_proto)
310                 else
311                         client_prot_ver_info = fgettext_ne("We only support protocol version $1.", min_supp_proto)
312                 end
313                 gamedata.errormessage = fgettext_ne("Protocol version mismatch. ")
314                         .. server_prot_ver_info
315                         .. client_prot_ver_info
316                 return false
317         end
318
319         return true
320 end
321 --------------------------------------------------------------------------------
322 function menu_worldmt(selected, setting, value)
323         local world = menudata.worldlist:get_list()[selected]
324         if world then
325                 local filename = world.path .. DIR_DELIM .. "world.mt"
326                 local world_conf = Settings(filename)
327
328                 if value ~= nil then
329                         if not world_conf:write() then
330                                 core.log("error", "Failed to write world config file")
331                         end
332                         world_conf:set(setting, value)
333                         world_conf:write()
334                 else
335                         return world_conf:get(setting)
336                 end
337         else
338                 return nil
339         end
340 end
341
342 function menu_worldmt_legacy(selected)
343         local modes_names = {"creative_mode", "enable_damage", "server_announce"}
344         for _, mode_name in pairs(modes_names) do
345                 local mode_val = menu_worldmt(selected, mode_name)
346                 if mode_val ~= nil then
347                         core.setting_set(mode_name, mode_val)
348                 else
349                         menu_worldmt(selected, mode_name, core.setting_get(mode_name))
350                 end
351         end
352 end