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