Conf.example: Re-add deleted noise parameter documentation
[oweals/minetest.git] / builtin / mainmenu / tab_multiplayer.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 --------------------------------------------------------------------------------
19 local function get_formspec(tabview, name, tabdata)
20         -- Update the cached supported proto info,
21         -- it may have changed after a change by the settings menu.
22         common_update_cached_supp_proto()
23         local fav_selected = menudata.favorites[tabdata.fav_selected]
24
25         local retval =
26                 "label[7.75,-0.15;" .. fgettext("Address / Port") .. "]" ..
27                 "label[7.75,1.05;" .. fgettext("Name / Password") .. "]" ..
28                 "field[8,0.75;3.3,0.5;te_address;;" ..
29                         core.formspec_escape(core.setting_get("address")) .. "]" ..
30                 "field[11.15,0.75;1.4,0.5;te_port;;" ..
31                         core.formspec_escape(core.setting_get("remote_port")) .. "]" ..
32                 "button[10.1,4.9;2,0.5;btn_mp_connect;" .. fgettext("Connect") .. "]" ..
33                 "field[8,1.95;2.95,0.5;te_name;;" ..
34                         core.formspec_escape(core.setting_get("name")) .. "]" ..
35                 "pwdfield[10.78,1.95;1.77,0.5;te_pwd;]" ..
36                 "box[7.73,2.35;4.3,2.28;#999999]"
37
38         if tabdata.fav_selected and fav_selected then
39                 if gamedata.fav then
40                         retval = retval .. "button[7.85,4.9;2.3,0.5;btn_delete_favorite;" ..
41                                 fgettext("Del. Favorite") .. "]"
42                 end
43                 if fav_selected.description then
44                         retval = retval .. "textarea[8.1,2.4;4.26,2.6;;" ..
45                                 core.formspec_escape((gamedata.serverdescription or ""), true) .. ";]"
46                 end
47         end
48
49         --favourites
50         retval = retval .. "tablecolumns[" ..
51                 image_column(fgettext("Favorite"), "favorite") .. ";" ..
52                 "color,span=3;" ..
53                 "text,align=right;" ..                -- clients
54                 "text,align=center,padding=0.25;" ..  -- "/"
55                 "text,align=right,padding=0.25;" ..   -- clients_max
56                 image_column(fgettext("Creative mode"), "creative") .. ",padding=1;" ..
57                 image_column(fgettext("Damage enabled"), "damage") .. ",padding=0.25;" ..
58                 image_column(fgettext("PvP enabled"), "pvp") .. ",padding=0.25;" ..
59                 "color,span=1;" ..
60                 "text,padding=1]" ..
61                 "table[-0.15,-0.1;7.75,5.5;favourites;"
62
63         if #menudata.favorites > 0 then
64                 local favs = core.get_favorites("local")
65                 if #favs > 0 then
66                         for i = 1, #favs do
67                         for j = 1, #menudata.favorites do
68                                 if menudata.favorites[j].address == favs[i].address and
69                                                 menudata.favorites[j].port == favs[i].port then
70                                         table.insert(menudata.favorites, i, table.remove(menudata.favorites, j))
71                                 end
72                         end
73                                 if favs[i].address ~= menudata.favorites[i].address then
74                                         table.insert(menudata.favorites, i, favs[i])
75                                 end
76                         end
77                 end
78                 retval = retval .. render_favorite(menudata.favorites[1], (#favs > 0))
79                 for i = 2, #menudata.favorites do
80                         retval = retval .. "," .. render_favorite(menudata.favorites[i], (i <= #favs))
81                 end
82         end
83
84         if tabdata.fav_selected then
85                 retval = retval .. ";" .. tabdata.fav_selected .. "]"
86         else
87                 retval = retval .. ";0]"
88         end
89
90         return retval
91 end
92
93 --------------------------------------------------------------------------------
94 local function main_button_handler(tabview, fields, name, tabdata)
95         if fields.te_name then
96                 gamedata.playername = fields.te_name
97                 core.setting_set("name", fields.te_name)
98         end
99
100         if fields.favourites then
101                 local event = core.explode_table_event(fields.favourites)
102                 local fav = menudata.favorites[event.row]
103
104                 if event.type == "DCL" then
105                         if event.row <= #menudata.favorites then
106                                 if menudata.favorites_is_public and
107                                                 not is_server_protocol_compat_or_error(
108                                                         fav.proto_min, fav.proto_max) then
109                                         return true
110                                 end
111
112                                 gamedata.address    = fav.address
113                                 gamedata.port       = fav.port
114                                 gamedata.playername = fields.te_name
115                                 gamedata.selected_world = 0
116
117                                 if fields.te_pwd then
118                                         gamedata.password = fields.te_pwd
119                                 end
120
121                                 gamedata.servername        = fav.name
122                                 gamedata.serverdescription = fav.description
123
124                                 if gamedata.address and gamedata.port then
125                                         core.setting_set("address", gamedata.address)
126                                         core.setting_set("remote_port", gamedata.port)
127                                         core.start()
128                                 end
129                         end
130                         return true
131                 end
132
133                 if event.type == "CHG" then
134                         if event.row <= #menudata.favorites then
135                                 gamedata.fav = false
136                                 local favs = core.get_favorites("local")
137                                 local address = fav.address
138                                 local port    = fav.port
139                                 gamedata.serverdescription = fav.description
140
141                                 for i = 1, #favs do
142                                         if fav.address == favs[i].address and
143                                                         fav.port == favs[i].port then
144                                                 gamedata.fav = true
145                                         end
146                                 end
147
148                                 if address and port then
149                                         core.setting_set("address", address)
150                                         core.setting_set("remote_port", port)
151                                 end
152                                 tabdata.fav_selected = event.row
153                         end
154                         return true
155                 end
156         end
157
158         if fields.key_up or fields.key_down then
159                 local fav_idx = core.get_table_index("favourites")
160                 local fav = menudata.favorites[fav_idx]
161
162                 if fav_idx then
163                         if fields.key_up and fav_idx > 1 then
164                                 fav_idx = fav_idx - 1
165                         elseif fields.key_down and fav_idx < #menudata.favorites then
166                                 fav_idx = fav_idx + 1
167                         end
168                 else
169                         fav_idx = 1
170                 end
171
172                 if not menudata.favorites or not fav then
173                         tabdata.fav_selected = 0
174                         return true
175                 end
176
177                 local address = fav.address
178                 local port    = fav.port
179
180                 if address and port then
181                         core.setting_set("address", address)
182                         core.setting_set("remote_port", port)
183                 end
184
185                 tabdata.fav_selected = fav_idx
186                 return true
187         end
188
189         if fields.btn_delete_favorite then
190                 local current_favourite = core.get_table_index("favourites")
191                 if not current_favourite then return end
192
193                 core.delete_favorite(current_favourite)
194                 asyncOnlineFavourites()
195                 tabdata.fav_selected = nil
196
197                 core.setting_set("address", "")
198                 core.setting_set("remote_port", "30000")
199                 return true
200         end
201
202         if (fields.btn_mp_connect or fields.key_enter) and fields.te_address and fields.te_port then
203                 gamedata.playername = fields.te_name
204                 gamedata.password   = fields.te_pwd
205                 gamedata.address    = fields.te_address
206                 gamedata.port       = fields.te_port
207                 gamedata.selected_world = 0
208                 local fav_idx = core.get_table_index("favourites")
209                 local fav = menudata.favorites[fav_idx]
210
211                 if fav_idx and fav_idx <= #menudata.favorites and
212                                 fav.address == fields.te_address and
213                                 fav.port    == fields.te_port then
214
215                         gamedata.servername        = fav.name
216                         gamedata.serverdescription = fav.description
217
218                         if menudata.favorites_is_public and
219                                         not is_server_protocol_compat_or_error(
220                                                 fav.proto_min, fav.proto_max) then
221                                 return true
222                         end
223                 else
224                         gamedata.servername        = ""
225                         gamedata.serverdescription = ""
226                 end
227
228                 core.setting_set("address",     fields.te_address)
229                 core.setting_set("remote_port", fields.te_port)
230
231                 core.start()
232                 return true
233         end
234         return false
235 end
236
237 local function on_change(type, old_tab, new_tab)
238         if type == "LEAVE" then return end
239         asyncOnlineFavourites()
240 end
241
242 --------------------------------------------------------------------------------
243 return {
244         name = "multiplayer",
245         caption = fgettext("Client"),
246         cbf_formspec = get_formspec,
247         cbf_button_handler = main_button_handler,
248         on_change = on_change
249 }