]> git.lizzy.rs Git - dragonfireclient.git/blob - builtin/mainmenu/common.lua
8db8bb8d17f19c0515d980c71da83c1efc2b7c87
[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 os.tmpname = function()
124         error('do not use') -- instead use core.get_temp_path()
125 end
126 --------------------------------------------------------------------------------
127
128 function menu_render_worldlist(show_gameid)
129         local retval = {}
130         local current_worldlist = menudata.worldlist:get_list()
131
132         local row
133         for i, v in ipairs(current_worldlist) do
134                 row = v.name
135                 if show_gameid == nil or show_gameid == true then
136                         row = row .. " [" .. v.gameid .. "]"
137                 end
138                 retval[#retval+1] = core.formspec_escape(row)
139
140         end
141
142         return table.concat(retval, ",")
143 end
144
145 function menu_handle_key_up_down(fields, textlist, settingname)
146         local oldidx, newidx = core.get_textlist_index(textlist), 1
147         if fields.key_up or fields.key_down then
148                 if fields.key_up and oldidx and oldidx > 1 then
149                         newidx = oldidx - 1
150                 elseif fields.key_down and oldidx and
151                                 oldidx < menudata.worldlist:size() then
152                         newidx = oldidx + 1
153                 end
154                 core.settings:set(settingname, menudata.worldlist:get_raw_index(newidx))
155                 configure_selected_world_params(newidx)
156                 return true
157         end
158         return false
159 end
160
161 function text2textlist(xpos, ypos, width, height, tl_name, textlen, text, transparency)
162         local textlines = core.wrap_text(text, textlen, true)
163         local retval = "textlist[" .. xpos .. "," .. ypos .. ";" .. width ..
164                         "," .. height .. ";" .. tl_name .. ";"
165
166         for i = 1, #textlines do
167                 textlines[i] = textlines[i]:gsub("\r", "")
168                 retval = retval .. core.formspec_escape(textlines[i]) .. ","
169         end
170
171         retval = retval .. ";0;"
172         if transparency then retval = retval .. "true" end
173         retval = retval .. "]"
174
175         return retval
176 end
177
178 function is_server_protocol_compat(server_proto_min, server_proto_max)
179         if (not server_proto_min) or (not server_proto_max) then
180                 -- There is no info. Assume the best and act as if we would be compatible.
181                 return true
182         end
183         return min_supp_proto <= server_proto_max and max_supp_proto >= server_proto_min
184 end
185
186 function is_server_protocol_compat_or_error(server_proto_min, server_proto_max)
187         if not is_server_protocol_compat(server_proto_min, server_proto_max) then
188                 local server_prot_ver_info, client_prot_ver_info
189                 local s_p_min = server_proto_min
190                 local s_p_max = server_proto_max
191
192                 if s_p_min ~= s_p_max then
193                         server_prot_ver_info = fgettext_ne("Server supports protocol versions between $1 and $2. ",
194                                 s_p_min, s_p_max)
195                 else
196                         server_prot_ver_info = fgettext_ne("Server enforces protocol version $1. ",
197                                 s_p_min)
198                 end
199                 if min_supp_proto ~= max_supp_proto then
200                         client_prot_ver_info= fgettext_ne("We support protocol versions between version $1 and $2.",
201                                 min_supp_proto, max_supp_proto)
202                 else
203                         client_prot_ver_info = fgettext_ne("We only support protocol version $1.", min_supp_proto)
204                 end
205                 gamedata.errormessage = fgettext_ne("Protocol version mismatch. ")
206                         .. server_prot_ver_info
207                         .. client_prot_ver_info
208                 return false
209         end
210
211         return true
212 end
213
214 function menu_worldmt(selected, setting, value)
215         local world = menudata.worldlist:get_list()[selected]
216         if world then
217                 local filename = world.path .. DIR_DELIM .. "world.mt"
218                 local world_conf = Settings(filename)
219
220                 if value then
221                         if not world_conf:write() then
222                                 core.log("error", "Failed to write world config file")
223                         end
224                         world_conf:set(setting, value)
225                         world_conf:write()
226                 else
227                         return world_conf:get(setting)
228                 end
229         else
230                 return nil
231         end
232 end
233
234 function menu_worldmt_legacy(selected)
235         local modes_names = {"creative_mode", "enable_damage", "server_announce"}
236         for _, mode_name in pairs(modes_names) do
237                 local mode_val = menu_worldmt(selected, mode_name)
238                 if mode_val then
239                         core.settings:set(mode_name, mode_val)
240                 else
241                         menu_worldmt(selected, mode_name, core.settings:get(mode_name))
242                 end
243         end
244 end