2 --Copyright (C) 2014 sapier
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.
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.
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.
19 local current_game, singleplayer_refresh_gamebar
20 local valid_disabled_settings = {
21 ["enable_damage"]=true,
22 ["creative_mode"]=true,
23 ["enable_server"]=true,
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)
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)
40 mm_game_theme.update("singleplayer", game) -- this refreshes the formspec
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
49 index = #menudata.worldlist:get_list()
52 menu_worldmt_legacy(index)
55 function singleplayer_refresh_gamebar()
57 local old_bar = ui.find_by_name("game_button_bar")
58 if old_bar ~= nil then
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)
72 for _, game in ipairs(pkgmgr.games) do
73 if fields["game_btnbar_" .. game.id] then
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})
84 for _, game in ipairs(pkgmgr.games) do
85 local btn_name = "game_btnbar_" .. game.id
89 local tooltip = core.formspec_escape(game.title)
91 if (game.menuicon_path or "") ~= "" then
92 image = core.formspec_escape(game.menuicon_path)
94 local part1 = game.id:sub(1,5)
95 local part2 = game.id:sub(6,10)
96 local part3 = game.id:sub(11)
98 text = part1 .. "\n" .. part2
100 text = text .. "\n" .. part3
103 btnbar:add_button(btn_name, text, image, tooltip)
106 local plus_image = core.formspec_escape(defaulttexturedir .. "plus.png")
107 btnbar:add_button("game_open_cdb", "", plus_image, fgettext("Install games from ContentDB"))
110 local function get_disabled_settings(game)
115 local gameconfig = Settings(game.path .. "/game.conf")
116 local disabled_settings = {}
118 local disabled_settings_str = (gameconfig:get("disabled_settings") or ""):split()
119 for _, value in pairs(disabled_settings_str) do
122 if string.sub(value, 1, 1) == "!" then
124 value = string.sub(value, 2)
126 if valid_disabled_settings[value] then
127 disabled_settings[value] = state
129 core.log("error", "Invalid disabled setting in game.conf: "..tostring(value))
133 return disabled_settings
136 local function get_formspec(tabview, name, tabdata)
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]
145 game = pkgmgr.find_by_gameid(world.gameid)
147 game = current_game()
149 local disabled_settings = get_disabled_settings(game)
151 local creative, damage, host = "", "", ""
153 -- Y offsets for game settings checkboxes
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")) .. "]"
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")) .. "]"
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")) .. "]"
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:") .. "]"..
181 "textlist[3.9,0.4;7.9,3.45;sp_worlds;" ..
182 menu_render_worldlist() ..
185 if core.settings:get_bool("enable_server") and disabled_settings["enable_server"] == nil then
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") .. "]"
194 local bind_addr = core.settings:get("bind_address")
195 if bind_addr ~= nil and bind_addr ~= "" then
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")) .. "]"
203 "field[0.3,5.25;3.8,0.5;te_serverport;" .. fgettext("Server Port") .. ";" ..
204 core.formspec_escape(core.settings:get("port")) .. "]"
208 "button[7.9,4.75;4.1,1;play;" .. fgettext("Play Game") .. "]"
214 local function main_button_handler(this, fields, name, tabdata)
216 assert(name == "local")
218 local world_doubleclick = false
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")
224 menu_worldmt_legacy(selected)
226 if event.type == "DCL" then
227 world_doubleclick = true
230 if event.type == "CHG" and selected ~= nil then
231 core.settings:set("mainmenu_last_selected_world",
232 menudata.worldlist:get_raw_index(selected))
237 if menu_handle_key_up_down(fields,"sp_worlds","mainmenu_last_selected_world") then
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"])
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"])
257 if fields["cb_server"] then
258 core.settings:set("enable_server", fields["cb_server"])
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"])
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)
275 if selected == nil or gamedata.selected_world == 0 then
276 gamedata.errormessage =
277 fgettext("No world created or selected!")
282 local world = menudata.worldlist:get_raw_element(gamedata.selected_world)
285 game_obj = pkgmgr.find_by_gameid(world.gameid)
286 core.settings:set("menu_last_game", game_obj.id)
289 local disabled_settings = get_disabled_settings(game_obj)
290 for k, _ in pairs(valid_disabled_settings) do
291 local v = disabled_settings[k]
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.")
296 core.settings:set_bool(k, disabled_settings[k])
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 = ""
306 core.settings:set("port",gamedata.port)
307 if fields["te_serveraddr"] ~= nil then
308 core.settings:set("bind_address",fields["te_serveraddr"])
311 gamedata.singleplayer = true
318 if fields["world_create"] ~= nil then
319 local create_world_dlg = create_create_world_dlg()
320 create_world_dlg:set_parent(this)
322 create_world_dlg:show()
323 mm_game_theme.update("singleplayer", current_game())
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]
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)
339 delete_world_dlg:show()
340 mm_game_theme.update("singleplayer",current_game())
347 if fields["world_configure"] ~= nil then
348 local selected = core.get_textlist_index("sp_worlds")
349 if selected ~= nil then
351 create_configure_world_dlg(
352 menudata.worldlist:get_raw_index(selected))
354 if (configdialog ~= nil) then
355 configdialog:set_parent(this)
358 mm_game_theme.update("singleplayer",current_game())
366 local function on_change(type, old_tab, new_tab)
367 if (type == "ENTER") then
368 local game = current_game()
373 singleplayer_refresh_gamebar()
374 ui.find_by_name("game_button_bar"):show()
376 menudata.worldlist:set_filtercriteria(nil)
377 local gamebar = ui.find_by_name("game_button_bar")
381 core.set_topleft_text("")
382 -- If new_tab is nil, a dialog is being shown; avoid resetting the theme
384 mm_game_theme.update(new_tab,nil)
389 --------------------------------------------------------------------------------
392 caption = fgettext("Start Game"),
393 cbf_formspec = get_formspec,
394 cbf_button_handler = main_button_handler,
395 on_change = on_change