common.lua 7.4 KB

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