]> git.lizzy.rs Git - minetest.git/blob - builtin/mainmenu/tab_local.lua
7887039abc1251c4c6c9aa28c5aaf3c6725cd24c
[minetest.git] / builtin / mainmenu / tab_local.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 current_game, singleplayer_refresh_gamebar
20 local valid_disabled_settings = {
21         ["enable_damage"]=true,
22         ["creative_mode"]=true,
23         ["enable_server"]=true,
24 }
25
26 -- Currently chosen game in gamebar for theming and filtering
27 function current_game()
28         local last_game_id = core.settings:get("menu_last_game")
29         local game = pkgmgr.find_by_gameid(last_game_id)
30
31         return game
32 end
33
34 -- Apply menu changes from given game
35 function apply_game(game)
36         core.set_topleft_text(game.name)
37         core.settings:set("menu_last_game", game.id)
38         menudata.worldlist:set_filtercriteria(game.id)
39
40         mm_game_theme.update("singleplayer", game) -- this refreshes the formspec
41
42         local index = filterlist.get_current_index(menudata.worldlist,
43                 tonumber(core.settings:get("mainmenu_last_selected_world")))
44         if not index or index < 1 then
45                 local selected = core.get_textlist_index("sp_worlds")
46                 if selected ~= nil and selected < #menudata.worldlist:get_list() then
47                         index = selected
48                 else
49                         index = #menudata.worldlist:get_list()
50                 end
51         end
52         menu_worldmt_legacy(index)
53 end
54
55 function singleplayer_refresh_gamebar()
56
57         local old_bar = ui.find_by_name("game_button_bar")
58         if old_bar ~= nil then
59                 old_bar:delete()
60         end
61
62         local function game_buttonbar_button_handler(fields)
63                 if fields.game_open_cdb then
64                         local maintab = ui.find_by_name("maintab")
65                         local dlg = create_store_dlg("game")
66                         dlg:set_parent(maintab)
67                         maintab:hide()
68                         dlg:show()
69                         return true
70                 end
71
72                 for _, game in ipairs(pkgmgr.games) do
73                         if fields["game_btnbar_" .. game.id] then
74                                 apply_game(game)
75                                 return true
76                         end
77                 end
78         end
79
80         local btnbar = buttonbar_create("game_button_bar",
81                 game_buttonbar_button_handler,
82                 {x=-0.3,y=5.9}, "horizontal", {x=12.4,y=1.15})
83
84         for _, game in ipairs(pkgmgr.games) do
85                 local btn_name = "game_btnbar_" .. game.id
86
87                 local image = nil
88                 local text = nil
89                 local tooltip = core.formspec_escape(game.title)
90
91                 if (game.menuicon_path or "") ~= "" then
92                         image = core.formspec_escape(game.menuicon_path)
93                 else
94                         local part1 = game.id:sub(1,5)
95                         local part2 = game.id:sub(6,10)
96                         local part3 = game.id:sub(11)
97
98                         text = part1 .. "\n" .. part2
99                         if part3 ~= "" then
100                                 text = text .. "\n" .. part3
101                         end
102                 end
103                 btnbar:add_button(btn_name, text, image, tooltip)
104         end
105
106         local plus_image = core.formspec_escape(defaulttexturedir .. "plus.png")
107         btnbar:add_button("game_open_cdb", "", plus_image, fgettext("Install games from ContentDB"))
108 end
109
110 local function get_disabled_settings(game)
111         if not game then
112                 return {}
113         end
114
115         local gameconfig = Settings(game.path .. "/game.conf")
116         local disabled_settings = {}
117         if gameconfig then
118                 local disabled_settings_str = (gameconfig:get("disabled_settings") or ""):split()
119                 for _, value in pairs(disabled_settings_str) do
120                         local state = false
121                         value = value:trim()
122                         if string.sub(value, 1, 1) == "!" then
123                                 state = true
124                                 value = string.sub(value, 2)
125                         end
126                         if valid_disabled_settings[value] then
127                                 disabled_settings[value] = state
128                         else
129                                 core.log("error", "Invalid disabled setting in game.conf: "..tostring(value))
130                         end
131                 end
132         end
133         return disabled_settings
134 end
135
136 local function get_formspec(tabview, name, tabdata)
137         local retval = ""
138
139         local index = filterlist.get_current_index(menudata.worldlist,
140                                 tonumber(core.settings:get("mainmenu_last_selected_world")))
141         local list = menudata.worldlist:get_list()
142         local world = list and index and list[index]
143         local game
144         if world then
145                 game = pkgmgr.find_by_gameid(world.gameid)
146         else
147                 game = current_game()
148         end
149         local disabled_settings = get_disabled_settings(game)
150
151         local creative, damage, host = "", "", ""
152
153         -- Y offsets for game settings checkboxes
154         local y = -0.2
155         local yo = 0.45
156
157         if disabled_settings["creative_mode"] == nil then
158                 creative = "checkbox[0,"..y..";cb_creative_mode;".. fgettext("Creative Mode") .. ";" ..
159                         dump(core.settings:get_bool("creative_mode")) .. "]"
160                 y = y + yo
161         end
162         if disabled_settings["enable_damage"] == nil then
163                 damage = "checkbox[0,"..y..";cb_enable_damage;".. fgettext("Enable Damage") .. ";" ..
164                         dump(core.settings:get_bool("enable_damage")) .. "]"
165                 y = y + yo
166         end
167         if disabled_settings["enable_server"] == nil then
168                 host = "checkbox[0,"..y..";cb_server;".. fgettext("Host Server") ..";" ..
169                         dump(core.settings:get_bool("enable_server")) .. "]"
170                 y = y + yo
171         end
172
173         retval = retval ..
174                         "button[3.9,3.8;2.8,1;world_delete;".. fgettext("Delete") .. "]" ..
175                         "button[6.55,3.8;2.8,1;world_configure;".. fgettext("Select Mods") .. "]" ..
176                         "button[9.2,3.8;2.8,1;world_create;".. fgettext("New") .. "]" ..
177                         "label[3.9,-0.05;".. fgettext("Select World:") .. "]"..
178                         creative ..
179                         damage ..
180                         host ..
181                         "textlist[3.9,0.4;7.9,3.45;sp_worlds;" ..
182                         menu_render_worldlist() ..
183                         ";" .. index .. "]"
184
185         if core.settings:get_bool("enable_server") and disabled_settings["enable_server"] == nil then
186                 retval = retval ..
187                                 "button[7.9,4.75;4.1,1;play;".. fgettext("Host Game") .. "]" ..
188                                 "checkbox[0,"..y..";cb_server_announce;" .. fgettext("Announce Server") .. ";" ..
189                                 dump(core.settings:get_bool("server_announce")) .. "]" ..
190                                 "field[0.3,2.85;3.8,0.5;te_playername;" .. fgettext("Name") .. ";" ..
191                                 core.formspec_escape(core.settings:get("name")) .. "]" ..
192                                 "pwdfield[0.3,4.05;3.8,0.5;te_passwd;" .. fgettext("Password") .. "]"
193
194                 local bind_addr = core.settings:get("bind_address")
195                 if bind_addr ~= nil and bind_addr ~= "" then
196                         retval = retval ..
197                                 "field[0.3,5.25;2.5,0.5;te_serveraddr;" .. fgettext("Bind Address") .. ";" ..
198                                 core.formspec_escape(core.settings:get("bind_address")) .. "]" ..
199                                 "field[2.85,5.25;1.25,0.5;te_serverport;" .. fgettext("Port") .. ";" ..
200                                 core.formspec_escape(core.settings:get("port")) .. "]"
201                 else
202                         retval = retval ..
203                                 "field[0.3,5.25;3.8,0.5;te_serverport;" .. fgettext("Server Port") .. ";" ..
204                                 core.formspec_escape(core.settings:get("port")) .. "]"
205                 end
206         else
207                 retval = retval ..
208                                 "button[7.9,4.75;4.1,1;play;" .. fgettext("Play Game") .. "]"
209         end
210
211         return retval
212 end
213
214 local function main_button_handler(this, fields, name, tabdata)
215
216         assert(name == "local")
217
218         local world_doubleclick = false
219
220         if fields["sp_worlds"] ~= nil then
221                 local event = core.explode_textlist_event(fields["sp_worlds"])
222                 local selected = core.get_textlist_index("sp_worlds")
223
224                 menu_worldmt_legacy(selected)
225
226                 if event.type == "DCL" then
227                         world_doubleclick = true
228                 end
229
230                 if event.type == "CHG" and selected ~= nil then
231                         core.settings:set("mainmenu_last_selected_world",
232                                 menudata.worldlist:get_raw_index(selected))
233                         return true
234                 end
235         end
236
237         if menu_handle_key_up_down(fields,"sp_worlds","mainmenu_last_selected_world") then
238                 return true
239         end
240
241         if fields["cb_creative_mode"] then
242                 core.settings:set("creative_mode", fields["cb_creative_mode"])
243                 local selected = core.get_textlist_index("sp_worlds")
244                 menu_worldmt(selected, "creative_mode", fields["cb_creative_mode"])
245
246                 return true
247         end
248
249         if fields["cb_enable_damage"] then
250                 core.settings:set("enable_damage", fields["cb_enable_damage"])
251                 local selected = core.get_textlist_index("sp_worlds")
252                 menu_worldmt(selected, "enable_damage", fields["cb_enable_damage"])
253
254                 return true
255         end
256
257         if fields["cb_server"] then
258                 core.settings:set("enable_server", fields["cb_server"])
259
260                 return true
261         end
262
263         if fields["cb_server_announce"] then
264                 core.settings:set("server_announce", fields["cb_server_announce"])
265                 local selected = core.get_textlist_index("srv_worlds")
266                 menu_worldmt(selected, "server_announce", fields["cb_server_announce"])
267
268                 return true
269         end
270
271         if fields["play"] ~= nil or world_doubleclick or fields["key_enter"] then
272                 local selected = core.get_textlist_index("sp_worlds")
273                 gamedata.selected_world = menudata.worldlist:get_raw_index(selected)
274
275                 if selected == nil or gamedata.selected_world == 0 then
276                         gamedata.errormessage =
277                                         fgettext("No world created or selected!")
278                         return true
279                 end
280
281                 -- Update last game
282                 local world = menudata.worldlist:get_raw_element(gamedata.selected_world)
283                 local game_obj
284                 if world then
285                         game_obj = pkgmgr.find_by_gameid(world.gameid)
286                         core.settings:set("menu_last_game", game_obj.id)
287                 end
288
289                 local disabled_settings = get_disabled_settings(game_obj)
290                 for k, _ in pairs(valid_disabled_settings) do
291                         local v = disabled_settings[k]
292                         if v ~= nil then
293                                 if k == "enable_server" and v == true then
294                                         error("Setting 'enable_server' cannot be force-enabled! The game.conf needs to be fixed.")
295                                 end
296                                 core.settings:set_bool(k, disabled_settings[k])
297                         end
298                 end
299
300                 if core.settings:get_bool("enable_server") then
301                         gamedata.playername = fields["te_playername"]
302                         gamedata.password   = fields["te_passwd"]
303                         gamedata.port       = fields["te_serverport"]
304                         gamedata.address    = ""
305
306                         core.settings:set("port",gamedata.port)
307                         if fields["te_serveraddr"] ~= nil then
308                                 core.settings:set("bind_address",fields["te_serveraddr"])
309                         end
310                 else
311                         gamedata.singleplayer = true
312                 end
313
314                 core.start()
315                 return true
316         end
317
318         if fields["world_create"] ~= nil then
319                 local create_world_dlg = create_create_world_dlg()
320                 create_world_dlg:set_parent(this)
321                 this:hide()
322                 create_world_dlg:show()
323                 mm_game_theme.update("singleplayer", current_game())
324                 return true
325         end
326
327         if fields["world_delete"] ~= nil then
328                 local selected = core.get_textlist_index("sp_worlds")
329                 if selected ~= nil and
330                         selected <= menudata.worldlist:size() then
331                         local world = menudata.worldlist:get_list()[selected]
332                         if world ~= nil and
333                                 world.name ~= nil and
334                                 world.name ~= "" then
335                                 local index = menudata.worldlist:get_raw_index(selected)
336                                 local delete_world_dlg = create_delete_world_dlg(world.name,index)
337                                 delete_world_dlg:set_parent(this)
338                                 this:hide()
339                                 delete_world_dlg:show()
340                                 mm_game_theme.update("singleplayer",current_game())
341                         end
342                 end
343
344                 return true
345         end
346
347         if fields["world_configure"] ~= nil then
348                 local selected = core.get_textlist_index("sp_worlds")
349                 if selected ~= nil then
350                         local configdialog =
351                                 create_configure_world_dlg(
352                                                 menudata.worldlist:get_raw_index(selected))
353
354                         if (configdialog ~= nil) then
355                                 configdialog:set_parent(this)
356                                 this:hide()
357                                 configdialog:show()
358                                 mm_game_theme.update("singleplayer",current_game())
359                         end
360                 end
361
362                 return true
363         end
364 end
365
366 local function on_change(type, old_tab, new_tab)
367         if (type == "ENTER") then
368                 local game = current_game()
369                 if game then
370                         apply_game(game)
371                 end
372
373                 singleplayer_refresh_gamebar()
374                 ui.find_by_name("game_button_bar"):show()
375         else
376                 menudata.worldlist:set_filtercriteria(nil)
377                 local gamebar = ui.find_by_name("game_button_bar")
378                 if gamebar then
379                         gamebar:hide()
380                 end
381                 core.set_topleft_text("")
382                 -- If new_tab is nil, a dialog is being shown; avoid resetting the theme
383                 if new_tab then
384                         mm_game_theme.update(new_tab,nil)
385                 end
386         end
387 end
388
389 --------------------------------------------------------------------------------
390 return {
391         name = "local",
392         caption = fgettext("Start Game"),
393         cbf_formspec = get_formspec,
394         cbf_button_handler = main_button_handler,
395         on_change = on_change
396 }