d6e2588fad640b363771ef66bf548600bc43e73e
[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 = 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         menudata.favorites = {}
238         core.handle_async(
239                 function(param)
240                         return core.get_favorites("online")
241                 end,
242                 nil,
243                 function(result)
244                         if core.setting_getbool("public_serverlist") then
245                                 menudata.favorites = order_favorite_list(result)
246                                 core.event_handler("Refresh")
247                         end
248                 end
249                 )
250 end
251
252 --------------------------------------------------------------------------------
253 function text2textlist(xpos,ypos,width,height,tl_name,textlen,text,transparency)
254         local textlines = core.splittext(text,textlen)
255         
256         local retval = "textlist[" .. xpos .. "," .. ypos .. ";"
257                                                                 .. width .. "," .. height .. ";"
258                                                                 .. tl_name .. ";"
259         
260         for i=1, #textlines, 1 do
261                 textlines[i] = textlines[i]:gsub("\r","")
262                 retval = retval .. core.formspec_escape(textlines[i]) .. ","
263         end
264         
265         retval = retval .. ";0;"
266         
267         if transparency then
268                 retval = retval .. "true"
269         end
270         
271         retval = retval .. "]"
272
273         return retval
274 end
275
276 --------------------------------------------------------------------------------
277 function is_server_protocol_compat(proto_min, proto_max)
278         return not ((min_supp_proto > (proto_max or 24)) or (max_supp_proto < (proto_min or 13)))
279 end
280 --------------------------------------------------------------------------------
281 function is_server_protocol_compat_or_error(proto_min, proto_max)
282         if not is_server_protocol_compat(proto_min, proto_max) then
283                 gamedata.errormessage = fgettext_ne("Protocol version mismatch, server " ..
284                         ((proto_min ~= proto_max) and "supports protocols between $1 and $2" or "enforces protocol version $1") ..
285                         ", we " ..
286                         ((min_supp_proto ~= max_supp_proto) and "support protocols between version $3 and $4." or "only support protocol version $3"),
287                         proto_min or 13, proto_max or 24, min_supp_proto, max_supp_proto)
288                 return false
289         end
290
291         return true
292 end