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