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