]> git.lizzy.rs Git - minetest.git/blob - builtin/mainmenu/common.lua
f32d77f2a4a03c11c85782517e8b19c2eb3f5b46
[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 --------------------------------------------------------------------------------
44 function image_column(tooltip, flagname)
45         return "image," ..
46                 "tooltip=" .. core.formspec_escape(tooltip) .. "," ..
47                 "0=" .. core.formspec_escape(defaulttexturedir .. "blank.png") .. "," ..
48                 "1=" .. core.formspec_escape(defaulttexturedir .. "server_flags_" .. flagname .. ".png")
49 end
50
51 --------------------------------------------------------------------------------
52 function order_favorite_list(list)
53         local res = {}
54         --orders the favorite list after support
55         for i=1,#list,1 do
56                 local fav = list[i]
57                 if is_server_protocol_compat(fav.proto_min, fav.proto_max) then
58                         table.insert(res, fav)
59                 end
60         end
61         for i=1,#list,1 do
62                 local fav = list[i]
63                 if not is_server_protocol_compat(fav.proto_min, fav.proto_max) then
64                         table.insert(res, fav)
65                 end
66         end
67         return res
68 end
69
70 --------------------------------------------------------------------------------
71 function render_favorite(spec,render_details)
72         local text = ""
73
74         if spec.name ~= nil then
75                 text = text .. core.formspec_escape(spec.name:trim())
76
77 --              if spec.description ~= nil and
78 --                      core.formspec_escape(spec.description):trim() ~= "" then
79 --                      text = text .. " (" .. core.formspec_escape(spec.description) .. ")"
80 --              end
81         else
82                 if spec.address ~= nil then
83                         text = text .. spec.address:trim()
84
85                         if spec.port ~= nil then
86                                 text = text .. ":" .. spec.port
87                         end
88                 end
89         end
90
91         if not render_details then
92                 return text
93         end
94
95         local details = ""
96         local grey_out = not is_server_protocol_compat(spec.proto_max, spec.proto_min)
97
98         if spec.clients ~= nil and spec.clients_max ~= nil then
99                 local clients_color = ''
100                 local clients_percent = 100 * spec.clients / spec.clients_max
101
102                 -- Choose a color depending on how many clients are connected
103                 -- (relatively to clients_max)
104                 if spec.clients == 0 then
105                         clients_color = ''        -- 0 players: default/white
106                 elseif spec.clients == spec.clients_max then
107                         clients_color = '#dd5b5b' -- full server: red (darker)
108                 elseif clients_percent <= 60 then
109                         clients_color = '#a1e587' -- 0-60%: green
110                 elseif clients_percent <= 90 then
111                         clients_color = '#ffdc97' -- 60-90%: yellow
112                 else
113                         clients_color = '#ffba97' -- 90-100%: orange
114                 end
115
116                 if grey_out then
117                         clients_color = '#aaaaaa'
118                 end
119
120                 details = details ..
121                                 clients_color .. ',' ..
122                                 render_client_count(spec.clients) .. ',' ..
123                                 '/,' ..
124                                 render_client_count(spec.clients_max) .. ','
125         elseif grey_out then
126                 details = details .. '#aaaaaa,?,/,?,'
127         else
128                 details = details .. ',?,/,?,'
129         end
130
131         if spec.creative then
132                 details = details .. "1,"
133         else
134                 details = details .. "0,"
135         end
136
137         if spec.damage then
138                 details = details .. "1,"
139         else
140                 details = details .. "0,"
141         end
142
143         if spec.pvp then
144                 details = details .. "1,"
145         else
146                 details = details .. "0,"
147         end
148
149         return details .. (grey_out and '#aaaaaa,' or ',') .. text
150 end
151
152 --------------------------------------------------------------------------------
153 os.tempfolder = function()
154         if core.setting_get("TMPFolder") then
155                 return core.setting_get("TMPFolder") .. DIR_DELIM .. "MT_" .. math.random(0,10000)
156         end
157
158         local filetocheck = os.tmpname()
159         os.remove(filetocheck)
160
161         local randname = "MTTempModFolder_" .. math.random(0,10000)
162         if DIR_DELIM == "\\" then
163                 local tempfolder = os.getenv("TEMP")
164                 return tempfolder .. filetocheck
165         else
166                 local backstring = filetocheck:reverse()
167                 return filetocheck:sub(0,filetocheck:len()-backstring:find(DIR_DELIM)+1) ..randname
168         end
169
170 end
171
172 --------------------------------------------------------------------------------
173 function menu_render_worldlist()
174         local retval = ""
175
176         local current_worldlist = menudata.worldlist:get_list()
177
178         for i,v in ipairs(current_worldlist) do
179                 if retval ~= "" then
180                         retval = retval ..","
181                 end
182
183                 retval = retval .. core.formspec_escape(v.name) ..
184                                         " \\[" .. core.formspec_escape(v.gameid) .. "\\]"
185         end
186
187         return retval
188 end
189
190 --------------------------------------------------------------------------------
191 function menu_handle_key_up_down(fields,textlist,settingname)
192
193         if fields["key_up"] then
194                 local oldidx = core.get_textlist_index(textlist)
195
196                 if oldidx ~= nil and oldidx > 1 then
197                         local newidx = oldidx -1
198                         core.setting_set(settingname,
199                                 menudata.worldlist:get_raw_index(newidx))
200                 end
201                 return true
202         end
203
204         if fields["key_down"] then
205                 local oldidx = core.get_textlist_index(textlist)
206
207                 if oldidx ~= nil and oldidx < menudata.worldlist:size() then
208                         local newidx = oldidx + 1
209                         core.setting_set(settingname,
210                                 menudata.worldlist:get_raw_index(newidx))
211                 end
212                 
213                 return true
214         end
215         
216         return false
217 end
218
219 --------------------------------------------------------------------------------
220 function asyncOnlineFavourites()
221
222         menudata.favorites = {}
223         core.handle_async(
224                 function(param)
225                         return core.get_favorites("online")
226                 end,
227                 nil,
228                 function(result)
229                         if core.setting_getbool("public_serverlist") then
230                                 menudata.favorites = order_favorite_list(result)
231                                 core.event_handler("Refresh")
232                         end
233                 end
234                 )
235 end
236
237 --------------------------------------------------------------------------------
238 function text2textlist(xpos,ypos,width,height,tl_name,textlen,text,transparency)
239         local textlines = core.splittext(text,textlen)
240         
241         local retval = "textlist[" .. xpos .. "," .. ypos .. ";"
242                                                                 .. width .. "," .. height .. ";"
243                                                                 .. tl_name .. ";"
244         
245         for i=1, #textlines, 1 do
246                 textlines[i] = textlines[i]:gsub("\r","")
247                 retval = retval .. core.formspec_escape(textlines[i]) .. ","
248         end
249         
250         retval = retval .. ";0;"
251         
252         if transparency then
253                 retval = retval .. "true"
254         end
255         
256         retval = retval .. "]"
257
258         return retval
259 end
260
261 --------------------------------------------------------------------------------
262 function is_server_protocol_compat(proto_min, proto_max)
263         return not ((min_supp_proto > (proto_max or 24)) or (max_supp_proto < (proto_min or 13)))
264 end
265 --------------------------------------------------------------------------------
266 function is_server_protocol_compat_or_error(proto_min, proto_max)
267         if not is_server_protocol_compat(proto_min, proto_max) then
268                 gamedata.errormessage = fgettext_ne("Protocol version mismatch, server " ..
269                         ((proto_min ~= proto_max) and "supports protocols between $1 and $2" or "enforces protocol version $1") ..
270                         ", we " ..
271                         ((min_supp_proto ~= max_supp_proto) and "support protocols between version $3 and $4." or "only support protocol version $3"),
272                         proto_min or 13, proto_max or 24, min_supp_proto, max_supp_proto)
273                 return false
274         end
275
276         return true
277 end