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