]> git.lizzy.rs Git - dragonfireclient.git/blob - builtin/mainmenu.lua
Remove link to #, add unlimited_player_transfer_distance to announce
[dragonfireclient.git] / builtin / mainmenu.lua
1 print = engine.debug
2 math.randomseed(os.time())
3 os.setlocale("C", "numeric")
4
5 local errorfct = error
6 error = function(text)
7         print(debug.traceback(""))
8         errorfct(text)
9 end
10
11 local scriptpath = engine.get_scriptdir()
12
13 mt_color_grey  = "#AAAAAA"
14 mt_color_blue  = "#0000DD"
15 mt_color_green = "#00DD00"
16 mt_color_dark_green = "#003300"
17
18 --for all other colors ask sfan5 to complete his worK!
19
20 dofile(scriptpath .. DIR_DELIM .. "misc_helpers.lua")
21 dofile(scriptpath .. DIR_DELIM .. "filterlist.lua")
22 dofile(scriptpath .. DIR_DELIM .. "modmgr.lua")
23 dofile(scriptpath .. DIR_DELIM .. "modstore.lua")
24 dofile(scriptpath .. DIR_DELIM .. "gamemgr.lua")
25 dofile(scriptpath .. DIR_DELIM .. "mm_textures.lua")
26 dofile(scriptpath .. DIR_DELIM .. "mm_menubar.lua")
27 dofile(scriptpath .. DIR_DELIM .. "async_event.lua")
28
29 menu = {}
30 local tabbuilder = {}
31 local worldlist = nil
32
33 --------------------------------------------------------------------------------
34 local function filter_texture_pack_list(list)
35         retval = {"None"}
36         for _,i in ipairs(list) do
37                 if i~="base" then
38                         table.insert(retval, i)
39                 end
40         end
41         return retval
42 end
43
44 --------------------------------------------------------------------------------
45 function menu.render_favorite(spec,render_details)
46         local text = ""
47
48         if spec.name ~= nil then
49                 text = text .. engine.formspec_escape(spec.name:trim())
50
51 --              if spec.description ~= nil and
52 --                      engine.formspec_escape(spec.description):trim() ~= "" then
53 --                      text = text .. " (" .. engine.formspec_escape(spec.description) .. ")"
54 --              end
55         else
56                 if spec.address ~= nil then
57                         text = text .. spec.address:trim()
58
59                         if spec.port ~= nil then
60                                 text = text .. ":" .. spec.port
61                         end
62                 end
63         end
64
65         if not render_details then
66                 return text
67         end
68
69         local details = ""
70         if spec.password == true then
71                 details = details .. "*"
72         else
73                 details = details .. "_"
74         end
75
76         if spec.creative then
77                 details = details .. "C"
78         else
79                 details = details .. "_"
80         end
81
82         if spec.damage then
83                 details = details .. "D"
84         else
85                 details = details .. "_"
86         end
87
88         if spec.pvp then
89                 details = details .. "P"
90         else
91                 details = details .. "_"
92         end
93         details = details .. " "
94
95         local playercount = ""
96
97         if spec.clients ~= nil and
98                 spec.clients_max ~= nil then
99                 playercount = string.format("%03d",spec.clients) .. "/" ..
100                                                 string.format("%03d",spec.clients_max) .. " "
101         end
102
103         return playercount .. engine.formspec_escape(details) ..  text
104 end
105
106 --------------------------------------------------------------------------------
107 os.tempfolder = function()
108         local filetocheck = os.tmpname()
109         os.remove(filetocheck)
110
111         local randname = "MTTempModFolder_" .. math.random(0,10000)
112         if DIR_DELIM == "\\" then
113                 local tempfolder = os.getenv("TEMP")
114                 return tempfolder .. filetocheck
115         else
116                 local backstring = filetocheck:reverse()
117                 return filetocheck:sub(0,filetocheck:len()-backstring:find(DIR_DELIM)+1) ..randname
118         end
119
120 end
121
122 --------------------------------------------------------------------------------
123 function init_globals()
124         --init gamedata
125         gamedata.worldindex = 0
126
127         worldlist = filterlist.create(
128                                         engine.get_worlds,
129                                         compare_worlds,
130                                         function(element,uid)
131                                                 if element.name == uid then
132                                                         return true
133                                                 end
134                                                 return false
135                                         end, --unique id compare fct
136                                         function(element,gameid)
137                                                 if element.gameid == gameid then
138                                                         return true
139                                                 end
140                                                 return false
141                                         end --filter fct
142                                         )
143
144         filterlist.add_sort_mechanism(worldlist,"alphabetic",sort_worlds_alphabetic)
145         filterlist.set_sortmode(worldlist,"alphabetic")
146 end
147
148 --------------------------------------------------------------------------------
149 function update_menu()
150
151         local formspec
152
153         -- handle errors
154         if gamedata.errormessage ~= nil then
155                 formspec = "size[12,5.2]" ..
156                         "field[1,2;10,2;;ERROR: " ..
157                         gamedata.errormessage ..
158                         ";]"..
159                         "button[4.5,4.2;3,0.5;btn_error_confirm;" .. fgettext("Ok") .. "]"
160         else
161                 formspec = tabbuilder.gettab()
162         end
163
164         engine.update_formspec(formspec)
165 end
166
167 --------------------------------------------------------------------------------
168 function menu.render_world_list()
169         local retval = ""
170
171         local current_worldlist = filterlist.get_list(worldlist)
172
173         for i,v in ipairs(current_worldlist) do
174                 if retval ~= "" then
175                         retval = retval ..","
176                 end
177
178                 retval = retval .. engine.formspec_escape(v.name) ..
179                                         " \\[" .. engine.formspec_escape(v.gameid) .. "\\]"
180         end
181
182         return retval
183 end
184
185 --------------------------------------------------------------------------------
186 function menu.render_texture_pack_list(list)
187         local retval = ""
188
189         for i,v in ipairs(list) do
190                 if retval ~= "" then
191                         retval = retval ..","
192                 end
193
194                 retval = retval .. v
195         end
196
197         return retval
198 end
199
200 --------------------------------------------------------------------------------
201 function menu.asyncOnlineFavourites()
202         menu.favorites = {}
203         engine.handle_async(
204                 function(param)
205                         return engine.get_favorites("online")
206                 end,
207                 nil,
208                 function(result)
209                         menu.favorites = result
210                         engine.event_handler("Refresh")
211                 end
212                 )
213 end
214
215 --------------------------------------------------------------------------------
216 function menu.init()
217         --init menu data
218         gamemgr.update_gamelist()
219
220         menu.last_game  = tonumber(engine.setting_get("main_menu_last_game_idx"))
221
222         if type(menu.last_game) ~= "number" then
223                 menu.last_game = 1
224         end
225
226         if engine.setting_getbool("public_serverlist") then
227                 menu.asyncOnlineFavourites()
228         else
229                 menu.favorites = engine.get_favorites("local")
230         end
231
232         menu.defaulttexturedir = engine.get_texturepath() .. DIR_DELIM .. "base" ..
233                                         DIR_DELIM .. "pack" .. DIR_DELIM
234 end
235
236 --------------------------------------------------------------------------------
237 function menu.lastgame()
238         if menu.last_game > 0 and menu.last_game <= #gamemgr.games then
239                 return gamemgr.games[menu.last_game]
240         end
241
242         if #gamemgr.games >= 1 then
243                 menu.last_game = 1
244                 return gamemgr.games[menu.last_game]
245         end
246
247         --error case!!
248         return nil
249 end
250
251 --------------------------------------------------------------------------------
252 function menu.update_last_game()
253
254         local current_world = filterlist.get_raw_element(worldlist,
255                                                         engine.setting_get("mainmenu_last_selected_world")
256                                                         )
257
258         if current_world == nil then
259                 return
260         end
261
262         local gamespec, i = gamemgr.find_by_gameid(current_world.gameid)
263         if i ~= nil then
264                 menu.last_game = i
265                 engine.setting_set("main_menu_last_game_idx",menu.last_game)
266         end
267 end
268
269 --------------------------------------------------------------------------------
270 function menu.handle_key_up_down(fields,textlist,settingname)
271
272         if fields["key_up"] then
273                 local oldidx = engine.get_textlist_index(textlist)
274
275                 if oldidx > 1 then
276                         local newidx = oldidx -1
277                         engine.setting_set(settingname,
278                                 filterlist.get_raw_index(worldlist,newidx))
279                 end
280         end
281
282         if fields["key_down"] then
283                 local oldidx = engine.get_textlist_index(textlist)
284
285                 if oldidx < filterlist.size(worldlist) then
286                         local newidx = oldidx + 1
287                         engine.setting_set(settingname,
288                                 filterlist.get_raw_index(worldlist,newidx))
289                 end
290         end
291 end
292
293 --------------------------------------------------------------------------------
294 function tabbuilder.dialog_create_world()
295         local mapgens = {"v6", "v7", "indev", "singlenode", "math"}
296
297         local current_seed = engine.setting_get("fixed_map_seed") or ""
298         local current_mg   = engine.setting_get("mg_name")
299
300         local mglist = ""
301         local selindex = 1
302         local i = 1
303         for k,v in pairs(mapgens) do
304                 if current_mg == v then
305                         selindex = i
306                 end
307                 i = i + 1
308                 mglist = mglist .. v .. ","
309         end
310         mglist = mglist:sub(1, -2)
311
312         local retval =
313                 "label[2,0;" .. fgettext("World name") .. "]"..
314                 "field[4.5,0.4;6,0.5;te_world_name;;]" ..
315
316                 "label[2,1;" .. fgettext("Seed") .. "]"..
317                 "field[4.5,1.4;6,0.5;te_seed;;".. current_seed .. "]" ..
318
319                 "label[2,2;" .. fgettext("Mapgen") .. "]"..
320                 "dropdown[4.2,2;6.3;dd_mapgen;" .. mglist .. ";" .. selindex .. "]" ..
321
322                 "label[2,3;" .. fgettext("Game") .. "]"..
323                 "textlist[4.2,3;5.8,2.3;games;" .. gamemgr.gamelist() ..
324                 ";" .. menu.last_game .. ";true]" ..
325
326                 "button[5,5.5;2.6,0.5;world_create_confirm;" .. fgettext("Create") .. "]" ..
327                 "button[7.5,5.5;2.8,0.5;world_create_cancel;" .. fgettext("Cancel") .. "]"
328
329         return retval
330 end
331
332 --------------------------------------------------------------------------------
333 function tabbuilder.dialog_delete_world()
334         return  "label[2,2;" ..
335                         fgettext("Delete World \"$1\"?", filterlist.get_raw_list(worldlist)[menu.world_to_del].name) .. "]"..
336                         "button[3.5,4.2;2.6,0.5;world_delete_confirm;" .. fgettext("Yes").. "]" ..
337                         "button[6,4.2;2.8,0.5;world_delete_cancel;" .. fgettext("No") .. "]"
338 end
339
340 --------------------------------------------------------------------------------
341
342 function tabbuilder.gettab()
343         local tsize = tabbuilder.tabsizes[tabbuilder.current_tab] or {width=12, height=5.2}
344         local retval = "size[" .. tsize.width .. "," .. tsize.height .. "]"
345
346         if tabbuilder.show_buttons then
347                 retval = retval .. tabbuilder.tab_header()
348         end
349
350         local buildfunc = tabbuilder.tabfuncs[tabbuilder.current_tab]
351         if buildfunc ~= nil then
352                 retval = retval .. buildfunc()
353         end
354
355         retval = retval .. modmgr.gettab(tabbuilder.current_tab)
356         retval = retval .. gamemgr.gettab(tabbuilder.current_tab)
357         retval = retval .. modstore.gettab(tabbuilder.current_tab)
358
359         return retval
360 end
361
362 --------------------------------------------------------------------------------
363 function tabbuilder.handle_create_world_buttons(fields)
364
365         if fields["world_create_confirm"] or
366                 fields["key_enter"] then
367
368                 local worldname = fields["te_world_name"]
369                 local gameindex = engine.get_textlist_index("games")
370
371                 if gameindex > 0 and
372                         worldname ~= "" then
373
374                         local message = nil
375
376                         if not filterlist.uid_exists_raw(worldlist,worldname) then
377                                 engine.setting_set("mg_name",fields["dd_mapgen"])
378                                 message = engine.create_world(worldname,gameindex)
379                         else
380                                 message = fgettext("A world named \"$1\" already exists", worldname)
381                         end
382
383                         engine.setting_set("fixed_map_seed", fields["te_seed"])
384
385                         if message ~= nil then
386                                 gamedata.errormessage = message
387                         else
388                                 menu.last_game = gameindex
389                                 engine.setting_set("main_menu_last_game_idx",gameindex)
390
391                                 filterlist.refresh(worldlist)
392                                 engine.setting_set("mainmenu_last_selected_world",
393                                                                         filterlist.raw_index_by_uid(worldlist,worldname))
394                         end
395                 else
396                         gamedata.errormessage =
397                                 fgettext("No worldname given or no game selected")
398                 end
399         end
400
401         if fields["games"] then
402                 tabbuilder.skipformupdate = true
403                 return
404         end
405
406         --close dialog
407         tabbuilder.is_dialog = false
408         tabbuilder.show_buttons = true
409         tabbuilder.current_tab = engine.setting_get("main_menu_tab")
410 end
411
412 --------------------------------------------------------------------------------
413 function tabbuilder.handle_delete_world_buttons(fields)
414
415         if fields["world_delete_confirm"] then
416                 if menu.world_to_del > 0 and
417                         menu.world_to_del <= #filterlist.get_raw_list(worldlist) then
418                         engine.delete_world(menu.world_to_del)
419                         menu.world_to_del = 0
420                         filterlist.refresh(worldlist)
421                 end
422         end
423
424         tabbuilder.is_dialog = false
425         tabbuilder.show_buttons = true
426         tabbuilder.current_tab = engine.setting_get("main_menu_tab")
427 end
428
429 --------------------------------------------------------------------------------
430 function tabbuilder.handle_multiplayer_buttons(fields)
431
432         if fields["te_name"] ~= nil then
433                 gamedata.playername = fields["te_name"]
434                 engine.setting_set("name", fields["te_name"])
435         end
436
437         if fields["favourites"] ~= nil then
438                 local event = explode_textlist_event(fields["favourites"])
439                 if event.typ == "DCL" then
440                         if event.index <= #menu.favorites then
441                                 gamedata.address = menu.favorites[event.index].address
442                                 gamedata.port = menu.favorites[event.index].port
443                                 gamedata.playername             = fields["te_name"]
444                                 if fields["te_pwd"] ~= nil then
445                                         gamedata.password               = fields["te_pwd"]
446                                 end
447                                 gamedata.selected_world = 0
448
449                                 if menu.favorites ~= nil then
450                                         gamedata.servername = menu.favorites[event.index].name
451                                         gamedata.serverdescription = menu.favorites[event.index].description
452                                 end
453
454                                 if gamedata.address ~= nil and
455                                         gamedata.port ~= nil then
456                                         engine.setting_set("address",gamedata.address)
457                                         engine.setting_set("remote_port",gamedata.port)
458                                         engine.start()
459                                 end
460                         end
461                 end
462
463                 if event.typ == "CHG" then
464                         if event.index <= #menu.favorites then
465                                 local address = menu.favorites[event.index].address
466                                 local port = menu.favorites[event.index].port
467
468                                 if address ~= nil and
469                                         port ~= nil then
470                                         engine.setting_set("address",address)
471                                         engine.setting_set("remote_port",port)
472                                 end
473
474                                 menu.fav_selected = event.index
475                         end
476                 end
477                 return
478         end
479
480         if fields["key_up"] ~= nil or
481                 fields["key_down"] ~= nil then
482
483                 local fav_idx = engine.get_textlist_index("favourites")
484
485                 if fields["key_up"] ~= nil and fav_idx > 1 then
486                         fav_idx = fav_idx -1
487                 else if fields["key_down"] and fav_idx < #menu.favorites then
488                         fav_idx = fav_idx +1
489                 end end
490
491                 local address = menu.favorites[fav_idx].address
492                 local port = menu.favorites[fav_idx].port
493
494                 if address ~= nil and
495                         port ~= nil then
496                         engine.setting_set("address",address)
497                         engine.setting_set("remote_port",port)
498                 end
499
500                 menu.fav_selected = fav_idx
501                 return
502         end
503
504         if fields["cb_public_serverlist"] ~= nil then
505                 engine.setting_set("public_serverlist", fields["cb_public_serverlist"])
506
507                 if engine.setting_getbool("public_serverlist") then
508                         menu.asyncOnlineFavourites()
509                 else
510                         menu.favorites = engine.get_favorites("local")
511                 end
512                 menu.fav_selected = nil
513                 return
514         end
515
516         if fields["btn_delete_favorite"] ~= nil then
517                 local current_favourite = engine.get_textlist_index("favourites")
518                 engine.delete_favorite(current_favourite)
519                 menu.favorites = engine.get_favorites()
520                 menu.fav_selected = nil
521
522                 engine.setting_set("address","")
523                 engine.setting_set("remote_port","30000")
524
525                 return
526         end
527
528         if fields["btn_mp_connect"] ~= nil or
529                 fields["key_enter"] ~= nil then
530
531                 gamedata.playername             = fields["te_name"]
532                 gamedata.password               = fields["te_pwd"]
533                 gamedata.address                = fields["te_address"]
534                 gamedata.port                   = fields["te_port"]
535
536                 local fav_idx = engine.get_textlist_index("favourites")
537
538                 if fav_idx > 0 and fav_idx <= #menu.favorites and
539                         menu.favorites[fav_idx].address == fields["te_address"] and
540                         menu.favorites[fav_idx].port    == fields["te_port"] then
541
542                         gamedata.servername                     = menu.favorites[fav_idx].name
543                         gamedata.serverdescription      = menu.favorites[fav_idx].description
544                 else
545                         gamedata.servername                     = ""
546                         gamedata.serverdescription      = ""
547                 end
548
549                 gamedata.selected_world = 0
550
551                 engine.setting_set("address",fields["te_address"])
552                 engine.setting_set("remote_port",fields["te_port"])
553
554                 engine.start()
555                 return
556         end
557 end
558
559 --------------------------------------------------------------------------------
560 function tabbuilder.handle_server_buttons(fields)
561
562         local world_doubleclick = false
563
564         if fields["srv_worlds"] ~= nil then
565                 local event = explode_textlist_event(fields["srv_worlds"])
566
567                 if event.typ == "DCL" then
568                         world_doubleclick = true
569                 end
570                 if event.typ == "CHG" then
571                         engine.setting_set("mainmenu_last_selected_world",
572                                 filterlist.get_raw_index(worldlist,engine.get_textlist_index("srv_worlds")))
573                 end
574         end
575
576         menu.handle_key_up_down(fields,"srv_worlds","mainmenu_last_selected_world")
577
578         if fields["cb_creative_mode"] then
579                 engine.setting_set("creative_mode", fields["cb_creative_mode"])
580         end
581
582         if fields["cb_enable_damage"] then
583                 engine.setting_set("enable_damage", fields["cb_enable_damage"])
584         end
585
586         if fields["cb_server_announce"] then
587                 engine.setting_set("server_announce", fields["cb_server_announce"])
588         end
589
590         if fields["start_server"] ~= nil or
591                 world_doubleclick or
592                 fields["key_enter"] then
593                 local selected = engine.get_textlist_index("srv_worlds")
594                 if selected > 0 then
595                         gamedata.playername             = fields["te_playername"]
596                         gamedata.password               = fields["te_passwd"]
597                         gamedata.port                   = fields["te_serverport"]
598                         gamedata.address                = ""
599                         gamedata.selected_world = filterlist.get_raw_index(worldlist,selected)
600
601                         engine.setting_set("port",gamedata.port)
602
603                         menu.update_last_game(gamedata.selected_world)
604                         engine.start()
605                 end
606         end
607
608         if fields["world_create"] ~= nil then
609                 tabbuilder.current_tab = "dialog_create_world"
610                 tabbuilder.is_dialog = true
611                 tabbuilder.show_buttons = false
612         end
613
614         if fields["world_delete"] ~= nil then
615                 local selected = engine.get_textlist_index("srv_worlds")
616                 if selected > 0 and
617                         selected <= filterlist.size(worldlist) then
618                         local world = filterlist.get_list(worldlist)[selected]
619                         if world ~= nil and
620                                 world.name ~= nil and
621                                 world.name ~= "" then
622                                 menu.world_to_del = filterlist.get_raw_index(worldlist,selected)
623                                 tabbuilder.current_tab = "dialog_delete_world"
624                                 tabbuilder.is_dialog = true
625                                 tabbuilder.show_buttons = false
626                         else
627                                 menu.world_to_del = 0
628                         end
629                 end
630         end
631
632         if fields["world_configure"] ~= nil then
633                 selected = engine.get_textlist_index("srv_worlds")
634                 if selected > 0 then
635                         modmgr.world_config_selected_world = filterlist.get_raw_index(worldlist,selected)
636                         if modmgr.init_worldconfig() then
637                                 tabbuilder.current_tab = "dialog_configure_world"
638                                 tabbuilder.is_dialog = true
639                                 tabbuilder.show_buttons = false
640                         end
641                 end
642         end
643 end
644
645 --------------------------------------------------------------------------------
646 function tabbuilder.handle_settings_buttons(fields)
647         if fields["cb_fancy_trees"] then
648                 engine.setting_set("new_style_leaves", fields["cb_fancy_trees"])
649         end
650         if fields["cb_smooth_lighting"] then
651                 engine.setting_set("smooth_lighting", fields["cb_smooth_lighting"])
652         end
653         if fields["cb_3d_clouds"] then
654                 engine.setting_set("enable_3d_clouds", fields["cb_3d_clouds"])
655         end
656         if fields["cb_opaque_water"] then
657                 engine.setting_set("opaque_water", fields["cb_opaque_water"])
658         end
659
660         if fields["cb_mipmapping"] then
661                 engine.setting_set("mip_map", fields["cb_mipmapping"])
662         end
663         if fields["cb_anisotrophic"] then
664                 engine.setting_set("anisotropic_filter", fields["cb_anisotrophic"])
665         end
666         if fields["cb_bilinear"] then
667                 engine.setting_set("bilinear_filter", fields["cb_bilinear"])
668         end
669         if fields["cb_trilinear"] then
670                 engine.setting_set("trilinear_filter", fields["cb_trilinear"])
671         end
672
673         if fields["cb_shaders"] then
674                 if (engine.setting_get("video_driver") == "direct3d8" or engine.setting_get("video_driver") == "direct3d9") then
675                         engine.setting_set("enable_shaders", "false")
676                         gamedata.errormessage = fgettext("To enable shaders the OpenGL driver needs to be used.")
677                 else
678                         engine.setting_set("enable_shaders", fields["cb_shaders"])
679                 end
680         end
681         if fields["cb_pre_ivis"] then
682                 engine.setting_set("preload_item_visuals", fields["cb_pre_ivis"])
683         end
684         if fields["cb_particles"] then
685                 engine.setting_set("enable_particles", fields["cb_particles"])
686         end
687         if fields["cb_finite_liquid"] then
688                 engine.setting_set("liquid_finite", fields["cb_finite_liquid"])
689         end
690
691         if fields["btn_change_keys"] ~= nil then
692                 engine.show_keys_menu()
693         end
694 end
695
696 --------------------------------------------------------------------------------
697 function tabbuilder.handle_singleplayer_buttons(fields)
698
699         local world_doubleclick = false
700
701         if fields["sp_worlds"] ~= nil then
702                 local event = explode_textlist_event(fields["sp_worlds"])
703
704                 if event.typ == "DCL" then
705                         world_doubleclick = true
706                 end
707
708                 if event.typ == "CHG" then
709                         engine.setting_set("mainmenu_last_selected_world",
710                                 filterlist.get_raw_index(worldlist,engine.get_textlist_index("sp_worlds")))
711                 end
712         end
713
714         menu.handle_key_up_down(fields,"sp_worlds","mainmenu_last_selected_world")
715
716         if fields["cb_creative_mode"] then
717                 engine.setting_set("creative_mode", fields["cb_creative_mode"])
718         end
719
720         if fields["cb_enable_damage"] then
721                 engine.setting_set("enable_damage", fields["cb_enable_damage"])
722         end
723
724         if fields["play"] ~= nil or
725                 world_doubleclick or
726                 fields["key_enter"] then
727                 local selected = engine.get_textlist_index("sp_worlds")
728                 if selected > 0 then
729                         gamedata.selected_world = filterlist.get_raw_index(worldlist,selected)
730                         gamedata.singleplayer   = true
731
732                         menu.update_last_game(gamedata.selected_world)
733
734                         engine.start()
735                 end
736         end
737
738         if fields["world_create"] ~= nil then
739                 tabbuilder.current_tab = "dialog_create_world"
740                 tabbuilder.is_dialog = true
741                 tabbuilder.show_buttons = false
742         end
743
744         if fields["world_delete"] ~= nil then
745                 local selected = engine.get_textlist_index("sp_worlds")
746                 if selected > 0 and
747                         selected <= filterlist.size(worldlist) then
748                         local world = filterlist.get_list(worldlist)[selected]
749                         if world ~= nil and
750                                 world.name ~= nil and
751                                 world.name ~= "" then
752                                 menu.world_to_del = filterlist.get_raw_index(worldlist,selected)
753                                 tabbuilder.current_tab = "dialog_delete_world"
754                                 tabbuilder.is_dialog = true
755                                 tabbuilder.show_buttons = false
756                         else
757                                 menu.world_to_del = 0
758                         end
759                 end
760         end
761
762         if fields["world_configure"] ~= nil then
763                 selected = engine.get_textlist_index("sp_worlds")
764                 if selected > 0 then
765                         modmgr.world_config_selected_world = filterlist.get_raw_index(worldlist,selected)
766                         if modmgr.init_worldconfig() then
767                                 tabbuilder.current_tab = "dialog_configure_world"
768                                 tabbuilder.is_dialog = true
769                                 tabbuilder.show_buttons = false
770                         end
771                 end
772         end
773 end
774
775 --------------------------------------------------------------------------------
776 function tabbuilder.handle_texture_pack_buttons(fields)
777         if fields["TPs"] ~= nil then
778                 local event = explode_textlist_event(fields["TPs"])
779                 if event.typ == "CHG" or event.typ=="DCL" then
780                         local index = engine.get_textlist_index("TPs")
781                         engine.setting_set("mainmenu_last_selected_TP",
782                                 index)
783                         local list = filter_texture_pack_list(engine.get_dirlist(engine.get_texturepath(), true))
784                         local current_index = engine.get_textlist_index("TPs")
785                         if #list >= current_index then
786                                 local new_path = engine.get_texturepath()..DIR_DELIM..list[current_index]
787                                 if list[current_index] == "None" then new_path = "" end
788
789                                 engine.setting_set("texture_path", new_path)
790                         end
791                 end
792         end
793 end
794
795 --------------------------------------------------------------------------------
796 function tabbuilder.tab_header()
797
798         if tabbuilder.last_tab_index == nil then
799                 tabbuilder.last_tab_index = 1
800         end
801
802         local toadd = ""
803
804         for i=1,#tabbuilder.current_buttons,1 do
805
806                 if toadd ~= "" then
807                         toadd = toadd .. ","
808                 end
809
810                 toadd = toadd .. tabbuilder.current_buttons[i].caption
811         end
812         return "tabheader[-0.3,-0.99;main_tab;" .. toadd ..";" .. tabbuilder.last_tab_index .. ";true;false]"
813 end
814
815 --------------------------------------------------------------------------------
816 function tabbuilder.handle_tab_buttons(fields)
817
818         if fields["main_tab"] then
819                 local index = tonumber(fields["main_tab"])
820                 tabbuilder.last_tab_index = index
821                 tabbuilder.current_tab = tabbuilder.current_buttons[index].name
822
823                 engine.setting_set("main_menu_tab",tabbuilder.current_tab)
824         end
825
826         --handle tab changes
827         if tabbuilder.current_tab ~= tabbuilder.old_tab then
828                 if tabbuilder.current_tab ~= "singleplayer" and not tabbuilder.is_dialog then
829                         menu.update_gametype(true)
830                 end
831         end
832
833         if tabbuilder.current_tab == "singleplayer" then
834                 menu.update_gametype()
835         end
836
837         tabbuilder.old_tab = tabbuilder.current_tab
838 end
839
840 --------------------------------------------------------------------------------
841 function tabbuilder.tab_multiplayer()
842
843         local retval =
844                 "vertlabel[0,-0.25;".. fgettext("CLIENT") .. "]" ..
845                 "label[1,-0.25;".. fgettext("Favorites:") .. "]"..
846                 "label[1,4.25;".. fgettext("Address/Port") .. "]"..
847                 "label[9,2.75;".. fgettext("Name/Password") .. "]" ..
848                 "field[1.25,5.25;5.5,0.5;te_address;;" ..engine.setting_get("address") .."]" ..
849                 "field[6.75,5.25;2.25,0.5;te_port;;" ..engine.setting_get("remote_port") .."]" ..
850                 "checkbox[1,3.6;cb_public_serverlist;".. fgettext("Public Serverlist") .. ";" ..
851                 dump(engine.setting_getbool("public_serverlist")) .. "]"
852
853         if not engine.setting_getbool("public_serverlist") then
854                 retval = retval ..
855                 "button[6.45,3.95;2.25,0.5;btn_delete_favorite;".. fgettext("Delete") .. "]"
856         end
857
858         retval = retval ..
859                 "button[9,4.95;2.5,0.5;btn_mp_connect;".. fgettext("Connect") .. "]" ..
860                 "field[9.3,3.75;2.5,0.5;te_name;;" ..engine.setting_get("name") .."]" ..
861                 "pwdfield[9.3,4.5;2.5,0.5;te_pwd;]" ..
862                 "textarea[9.3,0.25;2.5,2.75;;"
863         if menu.fav_selected ~= nil and
864                 menu.favorites[menu.fav_selected].description ~= nil then
865                 retval = retval ..
866                         engine.formspec_escape(menu.favorites[menu.fav_selected].description,true)
867         end
868
869         retval = retval ..
870                 ";]" ..
871                 "textlist[1,0.35;7.5,3.35;favourites;"
872
873         local render_details = engine.setting_getbool("public_serverlist")
874
875         if #menu.favorites > 0 then
876                 retval = retval .. menu.render_favorite(menu.favorites[1],render_details)
877
878                 for i=2,#menu.favorites,1 do
879                         retval = retval .. "," .. menu.render_favorite(menu.favorites[i],render_details)
880                 end
881         end
882
883         if menu.fav_selected ~= nil then
884                 retval = retval .. ";" .. menu.fav_selected .. "]"
885         else
886                 retval = retval .. ";0]"
887         end
888
889         return retval
890 end
891
892 --------------------------------------------------------------------------------
893 function tabbuilder.tab_server()
894
895         local index = filterlist.get_current_index(worldlist,
896                                 tonumber(engine.setting_get("mainmenu_last_selected_world"))
897                                 )
898
899         local retval =
900                 "button[4,4.15;2.6,0.5;world_delete;".. fgettext("Delete") .. "]" ..
901                 "button[6.5,4.15;2.8,0.5;world_create;".. fgettext("New") .. "]" ..
902                 "button[9.2,4.15;2.55,0.5;world_configure;".. fgettext("Configure") .. "]" ..
903                 "button[8.5,4.9;3.25,0.5;start_server;".. fgettext("Start Game") .. "]" ..
904                 "label[4,-0.25;".. fgettext("Select World:") .. "]"..
905                 "vertlabel[0,-0.25;".. fgettext("START SERVER") .. "]" ..
906                 "checkbox[0.5,0.25;cb_creative_mode;".. fgettext("Creative Mode") .. ";" ..
907                 dump(engine.setting_getbool("creative_mode")) .. "]"..
908                 "checkbox[0.5,0.7;cb_enable_damage;".. fgettext("Enable Damage") .. ";" ..
909                 dump(engine.setting_getbool("enable_damage")) .. "]"..
910                 "checkbox[0.5,1.15;cb_server_announce;".. fgettext("Public") .. ";" ..
911                 dump(engine.setting_getbool("server_announce")) .. "]"..
912                 "field[0.8,3.2;3,0.5;te_playername;".. fgettext("Name") .. ";" ..
913                 engine.setting_get("name") .. "]" ..
914                 "pwdfield[0.8,4.2;3,0.5;te_passwd;".. fgettext("Password") .. "]" ..
915                 "field[0.8,5.2;3,0.5;te_serverport;".. fgettext("Server Port") .. ";" ..
916                 engine.setting_get("port") .."]" ..
917                 "textlist[4,0.25;7.5,3.7;srv_worlds;" ..
918                 menu.render_world_list() ..
919                 ";" .. index .. "]"
920
921         return retval
922 end
923
924 --------------------------------------------------------------------------------
925 function tabbuilder.tab_settings()
926         return  "vertlabel[0,0;" .. fgettext("SETTINGS") .. "]" ..
927                         "checkbox[1,0.75;cb_fancy_trees;".. fgettext("Fancy trees") .. ";"
928                                         .. dump(engine.setting_getbool("new_style_leaves")) .. "]"..
929                         "checkbox[1,1.25;cb_smooth_lighting;".. fgettext("Smooth Lighting")
930                                         .. ";".. dump(engine.setting_getbool("smooth_lighting")) .. "]"..
931                         "checkbox[1,1.75;cb_3d_clouds;".. fgettext("3D Clouds") .. ";"
932                                         .. dump(engine.setting_getbool("enable_3d_clouds")) .. "]"..
933                         "checkbox[1,2.25;cb_opaque_water;".. fgettext("Opaque Water") .. ";"
934                                         .. dump(engine.setting_getbool("opaque_water")) .. "]"..
935
936                         "checkbox[4,0.75;cb_mipmapping;".. fgettext("Mip-Mapping") .. ";"
937                                         .. dump(engine.setting_getbool("mip_map")) .. "]"..
938                         "checkbox[4,1.25;cb_anisotrophic;".. fgettext("Anisotropic Filtering") .. ";"
939                                         .. dump(engine.setting_getbool("anisotropic_filter")) .. "]"..
940                         "checkbox[4,1.75;cb_bilinear;".. fgettext("Bi-Linear Filtering") .. ";"
941                                         .. dump(engine.setting_getbool("bilinear_filter")) .. "]"..
942                         "checkbox[4,2.25;cb_trilinear;".. fgettext("Tri-Linear Filtering") .. ";"
943                                         .. dump(engine.setting_getbool("trilinear_filter")) .. "]"..
944
945                         "checkbox[7.5,0.75;cb_shaders;".. fgettext("Shaders") .. ";"
946                                         .. dump(engine.setting_getbool("enable_shaders")) .. "]"..
947                         "checkbox[7.5,1.25;cb_pre_ivis;".. fgettext("Preload item visuals") .. ";"
948                                         .. dump(engine.setting_getbool("preload_item_visuals")) .. "]"..
949                         "checkbox[7.5,1.75;cb_particles;".. fgettext("Enable Particles") .. ";"
950                                         .. dump(engine.setting_getbool("enable_particles"))     .. "]"..
951                         "checkbox[7.5,2.25;cb_finite_liquid;".. fgettext("Finite Liquid") .. ";"
952                                         .. dump(engine.setting_getbool("liquid_finite")) .. "]"..
953
954                         "button[1,4.25;2.25,0.5;btn_change_keys;".. fgettext("Change keys") .. "]"
955 end
956
957 --------------------------------------------------------------------------------
958 function tabbuilder.tab_singleplayer()
959
960         local index = filterlist.get_current_index(worldlist,
961                                 tonumber(engine.setting_get("mainmenu_last_selected_world"))
962                                 )
963
964         return  "button[4,4.15;2.6,0.5;world_delete;".. fgettext("Delete") .. "]" ..
965                         "button[6.5,4.15;2.8,0.5;world_create;".. fgettext("New") .. "]" ..
966                         "button[9.2,4.15;2.55,0.5;world_configure;".. fgettext("Configure") .. "]" ..
967                         "button[8.5,4.95;3.25,0.5;play;".. fgettext("Play") .. "]" ..
968                         "label[4,-0.25;".. fgettext("Select World:") .. "]"..
969                         "vertlabel[0,-0.25;".. fgettext("SINGLE PLAYER") .. "]" ..
970                         "checkbox[0.5,0.25;cb_creative_mode;".. fgettext("Creative Mode") .. ";" ..
971                         dump(engine.setting_getbool("creative_mode")) .. "]"..
972                         "checkbox[0.5,0.7;cb_enable_damage;".. fgettext("Enable Damage") .. ";" ..
973                         dump(engine.setting_getbool("enable_damage")) .. "]"..
974                         "textlist[4,0.25;7.5,3.7;sp_worlds;" ..
975                         menu.render_world_list() ..
976                         ";" .. index .. "]" ..
977                         menubar.formspec
978 end
979
980 --------------------------------------------------------------------------------
981 function tabbuilder.tab_texture_packs()
982         local retval = "label[4,-0.25;".. fgettext("Select texture pack:") .. "]"..
983                         "vertlabel[0,-0.25;".. fgettext("TEXTURE PACKS") .. "]" ..
984                         "textlist[4,0.25;7.5,5.0;TPs;"
985
986         local current_texture_path = engine.setting_get("texture_path")
987         local list = filter_texture_pack_list(engine.get_dirlist(engine.get_texturepath(), true))
988         local index = tonumber(engine.setting_get("mainmenu_last_selected_TP"))
989
990         if index == nil then index = 1 end
991
992         if current_texture_path == "" then
993                 retval = retval ..
994                         menu.render_texture_pack_list(list) ..
995                         ";" .. index .. "]"
996                 return retval
997         end
998
999         local infofile = current_texture_path ..DIR_DELIM.."info.txt"
1000         local infotext = ""
1001         local f = io.open(infofile, "r")
1002         if f==nil then
1003                 infotext = fgettext("No information available")
1004         else
1005                 infotext = f:read("*all")
1006                 f:close()
1007         end
1008
1009         local screenfile = current_texture_path..DIR_DELIM.."screenshot.png"
1010         local no_screenshot = nil
1011         if not file_exists(screenfile) then
1012                 screenfile = nil
1013                 no_screenshot = engine.get_texturepath()..DIR_DELIM..
1014                                         "base"..DIR_DELIM.."pack"..DIR_DELIM.."no_screenshot.png"
1015         end
1016
1017         return  retval ..
1018                         menu.render_texture_pack_list(list) ..
1019                         ";" .. index .. "]" ..
1020                         "image[0.65,0.25;4.0,3.7;"..engine.formspec_escape(screenfile or no_screenshot).."]"..
1021                         "textarea[1.0,3.25;3.7,1.5;;"..engine.formspec_escape(infotext or "")..";]"
1022 end
1023
1024 --------------------------------------------------------------------------------
1025 function tabbuilder.tab_credits()
1026         local logofile = menu.defaulttexturedir .. "logo.png"
1027         return  "vertlabel[0,-0.5;CREDITS]" ..
1028                         "label[0.5,3;Minetest " .. engine.get_version() .. "]" ..
1029                         "label[0.5,3.3;http://minetest.net]" ..
1030                         "image[0.5,1;" .. engine.formspec_escape(logofile) .. "]" ..
1031                         "textlist[3.5,-0.25;8.5,5.8;list_credits;" ..
1032                         "#FFFF00" .. fgettext("Core Developers") .."," ..
1033                         "Perttu Ahola (celeron55) <celeron55@gmail.com>,"..
1034                         "Ryan Kwolek (kwolekr) <kwolekr@minetest.net>,"..
1035                         "PilzAdam <pilzadam@minetest.net>," ..
1036                         "Ilya Zhuravlev (xyz) <xyz@minetest.net>,"..
1037                         "Lisa Milne (darkrose) <lisa@ltmnet.com>,"..
1038                         "Maciej Kasatkin (RealBadAngel) <mk@realbadangel.pl>,"..
1039                         "proller <proler@gmail.com>,"..
1040                         "sfan5 <sfan5@live.de>,"..
1041                         "kahrl <kahrl@gmx.net>,"..
1042                         "sapier,"..
1043                         "ShadowNinja <shadowninja@minetest.net>,"..
1044                         "Nathanael Courant (Nore/Novatux) <nore@mesecons.net>,"..
1045                         "BlockMen,"..
1046                         ","..
1047                         "#FFFF00" .. fgettext("Active Contributors") .. "," ..
1048                         "Vanessa Ezekowitz (VanessaE) <vanessaezekowitz@gmail.com>,"..
1049                         "Jurgen Doser (doserj) <jurgen.doser@gmail.com>,"..
1050                         "Jeija <jeija@mesecons.net>,"..
1051                         "MirceaKitsune <mirceakitsune@gmail.com>,"..
1052                         "dannydark <the_skeleton_of_a_child@yahoo.co.uk>,"..
1053                         "0gb.us <0gb.us@0gb.us>,"..
1054                         "," ..
1055                         "#FFFF00" .. fgettext("Previous Contributors") .. "," ..
1056                         "Guiseppe Bilotta (Oblomov) <guiseppe.bilotta@gmail.com>,"..
1057                         "Jonathan Neuschafer <j.neuschaefer@gmx.net>,"..
1058                         "Nils Dagsson Moskopp (erlehmann) <nils@dieweltistgarnichtso.net>,"..
1059                         "Constantin Wenger (SpeedProg) <constantin.wenger@googlemail.com>,"..
1060                         "matttpt <matttpt@gmail.com>,"..
1061                         "JacobF <queatz@gmail.com>,"..
1062                         ";0;true]"
1063 end
1064
1065 --------------------------------------------------------------------------------
1066 function tabbuilder.init()
1067         tabbuilder.tabfuncs = {
1068                 singleplayer  = tabbuilder.tab_singleplayer,
1069                 multiplayer   = tabbuilder.tab_multiplayer,
1070                 server        = tabbuilder.tab_server,
1071                 settings      = tabbuilder.tab_settings,
1072                 texture_packs = tabbuilder.tab_texture_packs,
1073                 credits       = tabbuilder.tab_credits,
1074                 dialog_create_world = tabbuilder.dialog_create_world,
1075                 dialog_delete_world = tabbuilder.dialog_delete_world
1076         }
1077
1078         tabbuilder.tabsizes = {
1079                 dialog_create_world = {width=12, height=7},
1080                 dialog_delete_world = {width=12, height=5.2}
1081         }
1082
1083         tabbuilder.current_tab = engine.setting_get("main_menu_tab")
1084
1085         if tabbuilder.current_tab == nil or
1086                 tabbuilder.current_tab == "" then
1087                 tabbuilder.current_tab = "singleplayer"
1088                 engine.setting_set("main_menu_tab",tabbuilder.current_tab)
1089         end
1090
1091         --initialize tab buttons
1092         tabbuilder.last_tab = nil
1093         tabbuilder.show_buttons = true
1094
1095         tabbuilder.current_buttons = {}
1096         table.insert(tabbuilder.current_buttons,{name="singleplayer", caption=fgettext("Singleplayer")})
1097         table.insert(tabbuilder.current_buttons,{name="multiplayer", caption=fgettext("Client")})
1098         table.insert(tabbuilder.current_buttons,{name="server", caption=fgettext("Server")})
1099         table.insert(tabbuilder.current_buttons,{name="settings", caption=fgettext("Settings")})
1100         table.insert(tabbuilder.current_buttons,{name="texture_packs", caption=fgettext("Texture Packs")})
1101
1102         if engine.setting_getbool("main_menu_game_mgr") then
1103                 table.insert(tabbuilder.current_buttons,{name="game_mgr", caption=fgettext("Games")})
1104         end
1105
1106         if engine.setting_getbool("main_menu_mod_mgr") then
1107                 table.insert(tabbuilder.current_buttons,{name="mod_mgr", caption=fgettext("Mods")})
1108         end
1109         table.insert(tabbuilder.current_buttons,{name="credits", caption=fgettext("Credits")})
1110
1111
1112         for i=1,#tabbuilder.current_buttons,1 do
1113                 if tabbuilder.current_buttons[i].name == tabbuilder.current_tab then
1114                         tabbuilder.last_tab_index = i
1115                 end
1116         end
1117
1118         if tabbuilder.current_tab ~= "singleplayer" then
1119                 menu.update_gametype(true)
1120         else
1121                 menu.update_gametype()
1122         end
1123 end
1124
1125 --------------------------------------------------------------------------------
1126 function tabbuilder.checkretval(retval)
1127
1128         if retval ~= nil then
1129                 if retval.current_tab ~= nil then
1130                         tabbuilder.current_tab = retval.current_tab
1131                 end
1132
1133                 if retval.is_dialog ~= nil then
1134                         tabbuilder.is_dialog = retval.is_dialog
1135                 end
1136
1137                 if retval.show_buttons ~= nil then
1138                         tabbuilder.show_buttons = retval.show_buttons
1139                 end
1140
1141                 if retval.skipformupdate ~= nil then
1142                         tabbuilder.skipformupdate = retval.skipformupdate
1143                 end
1144
1145                 if retval.ignore_menu_quit == true then
1146                         tabbuilder.ignore_menu_quit = true
1147                 else
1148                         tabbuilder.ignore_menu_quit = false
1149                 end
1150         end
1151 end
1152
1153 --------------------------------------------------------------------------------
1154 --------------------------------------------------------------------------------
1155 -- initialize callbacks
1156 --------------------------------------------------------------------------------
1157 --------------------------------------------------------------------------------
1158 engine.button_handler = function(fields)
1159         --print("Buttonhandler: tab: " .. tabbuilder.current_tab .. " fields: " .. dump(fields))
1160
1161         if fields["btn_error_confirm"] then
1162                 gamedata.errormessage = nil
1163         end
1164
1165         local retval = modmgr.handle_buttons(tabbuilder.current_tab,fields)
1166         tabbuilder.checkretval(retval)
1167
1168         retval = gamemgr.handle_buttons(tabbuilder.current_tab,fields)
1169         tabbuilder.checkretval(retval)
1170
1171         retval = modstore.handle_buttons(tabbuilder.current_tab,fields)
1172         tabbuilder.checkretval(retval)
1173
1174         if tabbuilder.current_tab == "dialog_create_world" then
1175                 tabbuilder.handle_create_world_buttons(fields)
1176         end
1177
1178         if tabbuilder.current_tab == "dialog_delete_world" then
1179                 tabbuilder.handle_delete_world_buttons(fields)
1180         end
1181
1182         if tabbuilder.current_tab == "singleplayer" then
1183                 tabbuilder.handle_singleplayer_buttons(fields)
1184         end
1185
1186         if tabbuilder.current_tab == "texture_packs" then
1187                 tabbuilder.handle_texture_pack_buttons(fields)
1188         end
1189
1190         if tabbuilder.current_tab == "multiplayer" then
1191                 tabbuilder.handle_multiplayer_buttons(fields)
1192         end
1193
1194         if tabbuilder.current_tab == "settings" then
1195                 tabbuilder.handle_settings_buttons(fields)
1196         end
1197
1198         if tabbuilder.current_tab == "server" then
1199                 tabbuilder.handle_server_buttons(fields)
1200         end
1201
1202         --tab buttons
1203         tabbuilder.handle_tab_buttons(fields)
1204
1205         --menubar buttons
1206         menubar.handle_buttons(fields)
1207
1208         if not tabbuilder.skipformupdate then
1209                 --update menu
1210                 update_menu()
1211         else
1212                 tabbuilder.skipformupdate = false
1213         end
1214 end
1215
1216 --------------------------------------------------------------------------------
1217 engine.event_handler = function(event)
1218         if event == "MenuQuit" then
1219                 if tabbuilder.is_dialog then
1220                         if tabbuilder.ignore_menu_quit then
1221                                 return
1222                         end
1223
1224                         tabbuilder.is_dialog = false
1225                         tabbuilder.show_buttons = true
1226                         tabbuilder.current_tab = engine.setting_get("main_menu_tab")
1227                         menu.update_gametype()
1228                         update_menu()
1229                 else
1230                         engine.close()
1231                 end
1232         end
1233
1234         if event == "Refresh" then
1235                 update_menu()
1236         end
1237 end
1238
1239 --------------------------------------------------------------------------------
1240 function menu.update_gametype(reset)
1241         local game = menu.lastgame()
1242
1243         if reset or game == nil then
1244                 mm_texture.reset()
1245                 engine.set_topleft_text("")
1246                 filterlist.set_filtercriteria(worldlist,nil)
1247         else
1248                 mm_texture.update(tabbuilder.current_tab,game)
1249                 engine.set_topleft_text(game.name)
1250                 filterlist.set_filtercriteria(worldlist,game.id)
1251         end
1252 end
1253
1254 --------------------------------------------------------------------------------
1255 --------------------------------------------------------------------------------
1256 -- menu startup
1257 --------------------------------------------------------------------------------
1258 --------------------------------------------------------------------------------
1259 init_globals()
1260 mm_texture.init()
1261 menu.init()
1262 tabbuilder.init()
1263 menubar.refresh()
1264 modstore.init()
1265
1266 engine.sound_play("main_menu", true)
1267
1268 update_menu()