]> git.lizzy.rs Git - dragonfireclient.git/blob - builtin/mainmenu/common.lua
Merge branch 'master' of https://github.com/minetest/minetest
[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 menudata = {}
20
21 -- Local cached values
22 local min_supp_proto, max_supp_proto
23
24 function common_update_cached_supp_proto()
25         min_supp_proto = core.get_min_supp_proto()
26         max_supp_proto = core.get_max_supp_proto()
27 end
28 common_update_cached_supp_proto()
29
30 -- Menu helper functions
31
32 local function render_client_count(n)
33         if     n > 999 then return '99+'
34         elseif n >= 0  then return tostring(n)
35         else return '?' end
36 end
37
38 local function configure_selected_world_params(idx)
39         local worldconfig = pkgmgr.get_worldconfig(menudata.worldlist:get_list()[idx].path)
40         if worldconfig.creative_mode then
41                 core.settings:set("creative_mode", worldconfig.creative_mode)
42         end
43         if worldconfig.enable_damage then
44                 core.settings:set("enable_damage", worldconfig.enable_damage)
45         end
46 end
47
48 function render_serverlist_row(spec)
49         local text = ""
50         if spec.name then
51                 text = text .. core.formspec_escape(spec.name:trim())
52         elseif spec.address then
53                 text = text .. core.formspec_escape(spec.address:trim())
54                 if spec.port then
55                         text = text .. ":" .. spec.port
56                 end
57         end
58
59         local grey_out = not spec.is_compatible
60
61         local details = {}
62
63         if spec.lag or spec.ping then
64                 local lag = (spec.lag or 0) * 1000 + (spec.ping or 0) * 250
65                 if lag <= 125 then
66                         table.insert(details, "1")
67                 elseif lag <= 175 then
68                         table.insert(details, "2")
69                 elseif lag <= 250 then
70                         table.insert(details, "3")
71                 else
72                         table.insert(details, "4")
73                 end
74         else
75                 table.insert(details, "0")
76         end
77
78         table.insert(details, ",")
79
80         local color = (grey_out and "#aaaaaa") or ((spec.is_favorite and "#ddddaa") or "#ffffff")
81         if spec.clients and (spec.clients_max or 0) > 0 then
82                 local clients_percent = 100 * spec.clients / spec.clients_max
83
84                 -- Choose a color depending on how many clients are connected
85                 -- (relatively to clients_max)
86                 local clients_color
87                 if     grey_out               then clients_color = '#aaaaaa'
88                 elseif spec.clients == 0      then clients_color = ''        -- 0 players: default/white
89                 elseif clients_percent <= 60  then clients_color = '#a1e587' -- 0-60%: green
90                 elseif clients_percent <= 90  then clients_color = '#ffdc97' -- 60-90%: yellow
91                 elseif clients_percent == 100 then clients_color = '#dd5b5b' -- full server: red (darker)
92                 else                               clients_color = '#ffba97' -- 90-100%: orange
93                 end
94
95                 table.insert(details, clients_color)
96                 table.insert(details, render_client_count(spec.clients) .. " / " ..
97                         render_client_count(spec.clients_max))
98         else
99                 table.insert(details, color)
100                 table.insert(details, "?")
101         end
102
103         if spec.creative then
104                 table.insert(details, "1") -- creative icon
105         else
106                 table.insert(details, "0")
107         end
108
109         if spec.pvp then
110                 table.insert(details, "2") -- pvp icon
111         elseif spec.damage then
112                 table.insert(details, "1") -- heart icon
113         else
114                 table.insert(details, "0")
115         end
116
117         table.insert(details, color)
118         table.insert(details, text)
119
120         return table.concat(details, ",")
121 end
122
123 --------------------------------------------------------------------------------
124 os.tempfolder = function()
125         local temp = core.get_temp_path()
126         return temp .. DIR_DELIM .. "MT_" .. math.random(0, 10000)
127 end
128
129 os.tmpname = function()
130         local path = os.tempfolder()
131         io.open(path, "w"):close()
132         return path
133 end
134 --------------------------------------------------------------------------------
135
136 function menu_render_worldlist()
137         local retval = ""
138         local current_worldlist = menudata.worldlist:get_list()
139
140         for i, v in ipairs(current_worldlist) do
141                 if retval ~= "" then retval = retval .. "," end
142                 retval = retval .. core.formspec_escape(v.name) ..
143                                 " \\[" .. core.formspec_escape(v.gameid) .. "\\]"
144         end
145
146         return retval
147 end
148
149 function menu_handle_key_up_down(fields, textlist, settingname)
150         local oldidx, newidx = core.get_textlist_index(textlist), 1
151         if fields.key_up or fields.key_down then
152                 if fields.key_up and oldidx and oldidx > 1 then
153                         newidx = oldidx - 1
154                 elseif fields.key_down and oldidx and
155                                 oldidx < menudata.worldlist:size() then
156                         newidx = oldidx + 1
157                 end
158                 core.settings:set(settingname, menudata.worldlist:get_raw_index(newidx))
159                 configure_selected_world_params(newidx)
160                 return true
161         end
162         return false
163 end
164
165 function text2textlist(xpos, ypos, width, height, tl_name, textlen, text, transparency)
166         local textlines = core.wrap_text(text, textlen, true)
167         local retval = "textlist[" .. xpos .. "," .. ypos .. ";" .. width ..
168                         "," .. height .. ";" .. tl_name .. ";"
169
170         for i = 1, #textlines do
171                 textlines[i] = textlines[i]:gsub("\r", "")
172                 retval = retval .. core.formspec_escape(textlines[i]) .. ","
173         end
174
175         retval = retval .. ";0;"
176         if transparency then retval = retval .. "true" end
177         retval = retval .. "]"
178
179         return retval
180 end
181
182 function is_server_protocol_compat(server_proto_min, server_proto_max)
183         if (not server_proto_min) or (not server_proto_max) then
184                 -- There is no info. Assume the best and act as if we would be compatible.
185                 return true
186         end
187         return min_supp_proto <= server_proto_max and max_supp_proto >= server_proto_min
188 end
189
190 function is_server_protocol_compat_or_error(server_proto_min, server_proto_max)
191         if not is_server_protocol_compat(server_proto_min, server_proto_max) then
192                 local server_prot_ver_info, client_prot_ver_info
193                 local s_p_min = server_proto_min
194                 local s_p_max = server_proto_max
195
196                 if s_p_min ~= s_p_max then
197                         server_prot_ver_info = fgettext_ne("Server supports protocol versions between $1 and $2. ",
198                                 s_p_min, s_p_max)
199                 else
200                         server_prot_ver_info = fgettext_ne("Server enforces protocol version $1. ",
201                                 s_p_min)
202                 end
203                 if min_supp_proto ~= max_supp_proto then
204                         client_prot_ver_info= fgettext_ne("We support protocol versions between version $1 and $2.",
205                                 min_supp_proto, max_supp_proto)
206                 else
207                         client_prot_ver_info = fgettext_ne("We only support protocol version $1.", min_supp_proto)
208                 end
209                 gamedata.errormessage = fgettext_ne("Protocol version mismatch. ")
210                         .. server_prot_ver_info
211                         .. client_prot_ver_info
212                 return false
213         end
214
215         return true
216 end
217
218 function menu_worldmt(selected, setting, value)
219         local world = menudata.worldlist:get_list()[selected]
220         if world then
221                 local filename = world.path .. DIR_DELIM .. "world.mt"
222                 local world_conf = Settings(filename)
223
224                 if value then
225                         if not world_conf:write() then
226                                 core.log("error", "Failed to write world config file")
227                         end
228                         world_conf:set(setting, value)
229                         world_conf:write()
230                 else
231                         return world_conf:get(setting)
232                 end
233         else
234                 return nil
235         end
236 end
237
238 function menu_worldmt_legacy(selected)
239         local modes_names = {"creative_mode", "enable_damage", "server_announce"}
240         for _, mode_name in pairs(modes_names) do
241                 local mode_val = menu_worldmt(selected, mode_name)
242                 if mode_val then
243                         core.settings:set(mode_name, mode_val)
244                 else
245                         menu_worldmt(selected, mode_name, core.settings:get(mode_name))
246                 end
247         end
248 end