]> git.lizzy.rs Git - dragonfireclient.git/blobdiff - builtin/mainmenu.lua
Omnicleanup: header cleanup, add ModApiUtil shared between game and mainmenu
[dragonfireclient.git] / builtin / mainmenu.lua
index 56143e700b518b4b9caf54d9d4388a8433af2677..926f3f2d92dc3a58b35dee1e7d36534eca1fe3ab 100644 (file)
@@ -1,58 +1,99 @@
+print = engine.debug
+math.randomseed(os.time())
+os.setlocale("C", "numeric")
+
 local scriptpath = engine.get_scriptdir()
 
+mt_color_grey  = "#AAAAAA"
+mt_color_blue  = "#0000DD"
+mt_color_green = "#00DD00"
+mt_color_dark_green = "#003300"
+
+--for all other colors ask sfan5 to complete his worK!
+
+dofile(scriptpath .. DIR_DELIM .. "misc_helpers.lua")
+dofile(scriptpath .. DIR_DELIM .. "filterlist.lua")
 dofile(scriptpath .. DIR_DELIM .. "modmgr.lua")
 dofile(scriptpath .. DIR_DELIM .. "modstore.lua")
 dofile(scriptpath .. DIR_DELIM .. "gamemgr.lua")
+dofile(scriptpath .. DIR_DELIM .. "mm_textures.lua")
+dofile(scriptpath .. DIR_DELIM .. "mm_menubar.lua")
 
-local menu = {}
+menu = {}
 local tabbuilder = {}
-local menubar = {}
+local worldlist = nil
+
+--------------------------------------------------------------------------------
+local function filterTP(TPlist)
+       TPlist2 = {"None"}
+       for _,i in ipairs(TPlist) do
+               if i~="base" then
+                       table.insert(TPlist2, i)
+               end
+       end
+       return TPlist2
+end
 
 --------------------------------------------------------------------------------
-function render_favourite(spec)
+function menu.render_favorite(spec,render_details)
        local text = ""
        
        if spec.name ~= nil then
-               text = text .. spec.name:trim()
+               text = text .. fs_escape_string(spec.name:trim())
                
-               if spec.description ~= nil then
-                       --TODO make sure there's no invalid chat in spec.description
-                       text = text .. " (" .. fs_escape_string(spec.description) .. ")"
-               end
+--             if spec.description ~= nil and
+--                     fs_escape_string(spec.description):trim() ~= "" then
+--                     text = text .. " (" .. fs_escape_string(spec.description) .. ")"
+--             end
        else
                if spec.address ~= nil then
                        text = text .. spec.address:trim()
+                       
+                       if spec.port ~= nil then
+                               text = text .. ":" .. spec.port
+                       end
                end
        end
        
+       if not render_details then
+               return text
+       end
+       
        local details = ""
        if spec.password == true then
-               details = *"
+               details = details .. "*"
        else
-               details = "  "
+               details = details .. "_"
        end
        
        if spec.creative then
                details = details .. "C"
        else
-               details = details .. " "
+               details = details .. "_"
        end
        
        if spec.damage then
                details = details .. "D"
        else
-               details = details .. " "
+               details = details .. "_"
        end
        
        if spec.pvp then
                details = details .. "P"
        else
-               details = details .. " "
+               details = details .. "_"
        end
+       details = details .. " "
+       
+       local playercount = ""
        
-       text = text .. ":" .. spec.port:trim()
+       if spec.clients ~= nil and
+               spec.clients_max ~= nil then
+               playercount = string.format("%03d",spec.clients) .. "/" ..
+                                               string.format("%03d",spec.clients_max) .. " "
+       end
        
-       return text
+       return playercount .. fs_escape_string(details) ..  text
 end
 
 --------------------------------------------------------------------------------
@@ -71,159 +112,31 @@ os.tempfolder = function()
 
 end
 
---------------------------------------------------------------------------------
-function cleanup_path(temppath)
-       
-       local parts = temppath:split("-")
-       temppath = ""   
-       for i=1,#parts,1 do
-               if temppath ~= "" then
-                       temppath = temppath .. "_"
-               end
-               temppath = temppath .. parts[i]
-       end
-       
-       parts = temppath:split(".")
-       temppath = ""   
-       for i=1,#parts,1 do
-               if temppath ~= "" then
-                       temppath = temppath .. "_"
-               end
-               temppath = temppath .. parts[i]
-       end
-       
-       parts = temppath:split("'")
-       temppath = ""   
-       for i=1,#parts,1 do
-               if temppath ~= "" then
-                       temppath = temppath .. ""
-               end
-               temppath = temppath .. parts[i]
-       end
-       
-       parts = temppath:split(" ")
-       temppath = ""   
-       for i=1,#parts,1 do
-               if temppath ~= "" then
-                       temppath = temppath
-               end
-               temppath = temppath .. parts[i]
-       end
-       
-       return temppath
-end
-
---------------------------------------------------------------------------------
-function menu.update_gametype()
-       if (menu.game_last_check == nil or
-               menu.game_last_check ~= menu.last_game) and
-               tabbuilder.current_tab == "singleplayer" then
-               
-               local gamedetails = menu.lastgame()
-               engine.set_topleft_text(gamedetails.name)
-               
-               --background
-               local path_background_texture = gamedetails.path .. DIR_DELIM .."menu" .. 
-                                                                                                DIR_DELIM .. "background.png"
-               if engine.set_background("background",path_background_texture) then
-                       engine.set_clouds(false)
-               else
-                       engine.set_clouds(true)
-               end
-               
-               --overlay
-               local path_overlay_texture = gamedetails.path .. DIR_DELIM .."menu" .. 
-                                                                                                DIR_DELIM .. "overlay.png"
-               engine.set_background("overlay",path_overlay_texture)
-               
-               --header
-               local path_overlay_texture = gamedetails.path .. DIR_DELIM .."menu" .. 
-                                                                                                DIR_DELIM .. "header.png"
-               engine.set_background("header",path_overlay_texture)
-               
-               --footer
-               local path_overlay_texture = gamedetails.path .. DIR_DELIM .."menu" .. 
-                                                                                                DIR_DELIM .. "footer.png"
-               engine.set_background("footer",path_overlay_texture)
-               
-               menu.game_last_check = menu.last_game
-       else
-               if menu.game_last_check ~= menu.last_game then
-                       menu.game_last_check = menu.last_game
-                       menu.reset_gametype()
-               end
-       end
-end
-
---------------------------------------------------------------------------------
-function menu.reset_gametype()
-       menu.game_last_check = nil
-       engine.set_clouds(true)
-       engine.set_background("background","")
-       engine.set_background("overlay",menu.basetexturedir .. "menu_overlay.png")
-       engine.set_background("header",menu.basetexturedir .. "menu_header.png")
-       engine.set_background("footer",menu.basetexturedir .. "menu_footer.png")
-       engine.set_topleft_text("")
-end
-
---------------------------------------------------------------------------------
-function get_last_folder(text,count)
-       local parts = text:split(DIR_DELIM)
-       
-       if count == nil then
-               return parts[#parts]
-       end
-       
-       local retval = ""
-       for i=1,count,1 do
-               retval = retval .. parts[#parts - (count-i)] .. DIR_DELIM
-       end
-       
-       return retval
-end
-
 --------------------------------------------------------------------------------
 function init_globals()
        --init gamedata
        gamedata.worldindex = 0
-end
-
---------------------------------------------------------------------------------
-function identify_filetype(name)
-
-       if name:sub(-3):lower() == "zip" then
-               return {
-                               name = name,
-                               type = "zip"
-                               }
-       end
-       
-       if name:sub(-6):lower() == "tar.gz" or
-               name:sub(-3):lower() == "tgz"then
-               return {
-                               name = name,
-                               type = "tgz"
-                               }
-       end
        
-       if name:sub(-6):lower() == "tar.bz2" then
-               return {
-                               name = name,
-                               type = "tbz"
-                               }
-       end
-       
-       if name:sub(-2):lower() == "7z" then
-               return {
-                               name = name,
-                               type = "7z"
-                               }
-       end
-
-       return {
-               name = name,
-               type = "ukn"
-       }
+       worldlist = filterlist.create(
+                                       engine.get_worlds,
+                                       compare_worlds,
+                                       function(element,uid)
+                                               if element.name == uid then
+                                                       return true
+                                               end
+                                               return false
+                                       end, --unique id compare fct
+                                       function(element,gameid)
+                                               if element.gameid == gameid then
+                                                       return true
+                                               end
+                                               return false
+                                       end --filter fct
+                                       )
+                                       
+       filterlist.add_sort_mechanism(worldlist,"alphabetic",sort_worlds_alphabetic)
+       filterlist.set_sortmode(worldlist,"alphabetic")
+                                       
 end
 
 --------------------------------------------------------------------------------
@@ -246,73 +159,47 @@ function update_menu()
 end
 
 --------------------------------------------------------------------------------
-function menu.filtered_game_list()
+function menu.render_world_list()
        local retval = ""
-
-       local current_game = menu.lastgame()
        
-       for i=1,#menu.worldlist,1 do
-               if menu.worldlist[i].gameid == current_game.id then
-                       if retval ~= "" then
-                               retval = retval ..","
-                       end
-                       
-                       retval = retval .. menu.worldlist[i].name .. 
-                                               " [[" .. menu.worldlist[i].gameid .. "]]"
-               end
-       end
-       
-       return retval
-end
-
---------------------------------------------------------------------------------
-function menu.filtered_game_list_raw()
-       local retval =  {}
-
-       local current_game = menu.lastgame()
+       local current_worldlist = filterlist.get_list(worldlist)
        
-       for i=1,#menu.worldlist,1 do
-               if menu.worldlist[i].gameid == current_game.id then
-                       table.insert(retval,menu.worldlist[i])
+       for i,v in ipairs(current_worldlist) do
+               if retval ~= "" then
+                       retval = retval ..","
                end
+               
+               retval = retval .. v.name .. 
+                                       " \\[" .. v.gameid .. "\\]"
        end
-       
+
        return retval
 end
 
 --------------------------------------------------------------------------------
-function menu.filtered_index_to_plain(filtered_index)
+function menu.render_TP_list(TPlist)
+       local retval = ""
 
-       local current_game = menu.lastgame()
-       
-       local temp_idx = 0
-       
-       for i=1,#menu.worldlist,1 do
-               if menu.worldlist[i].gameid == current_game.id then
-                       temp_idx = temp_idx +1
-               end
-               
-               if temp_idx == filtered_index then
-                       return i
+       --local current_TP = filterlist.get_list(TPlist)
+
+       for i,v in ipairs(TPlist) do
+               if retval ~= "" then
+                       retval = retval ..","
                end
+
+               retval = retval .. v
        end
-       return -1
+
+       return retval
 end
 
 --------------------------------------------------------------------------------
 function menu.init()
        --init menu data
        gamemgr.update_gamelist()
-
-       menu.worldlist = engine.get_worlds()
        
-       menu.last_world = tonumber(engine.setting_get("main_menu_last_world_idx"))
        menu.last_game  = tonumber(engine.setting_get("main_menu_last_game_idx"))
        
-       if type(menu.last_world) ~= "number" then
-               menu.last_world = 1
-       end
-       
        if type(menu.last_game) ~= "number" then
                menu.last_game = 1
        end
@@ -323,10 +210,8 @@ function menu.init()
                menu.favorites = engine.get_favorites("local")
        end
        
-       
-       menu.basetexturedir = engine.get_gamepath() .. DIR_DELIM .. ".." ..
-                                               DIR_DELIM .. "textures" .. DIR_DELIM .. "base" .. 
-                                               DIR_DELIM .. "pack" .. DIR_DELIM
+       menu.defaulttexturedir = engine.get_texturepath() .. DIR_DELIM .. "base" .. 
+                                       DIR_DELIM .. "pack" .. DIR_DELIM
 end
 
 --------------------------------------------------------------------------------
@@ -345,93 +230,46 @@ function menu.lastgame()
 end
 
 --------------------------------------------------------------------------------
-function menu.lastworld()
-       if menu.last_world ~= nil and 
-               menu.last_world > 0 and 
-               menu.last_world <= #menu.worldlist then
-               return menu.worldlist[menu.last_world]
-       end
-       
-       if #menu.worldlist >= 1 then
-               menu.last_world = 1
-               return menu.worldlist[menu.last_world]
-       end
-       
-       --error case!!
-       return nil
-end
+function menu.update_last_game()
 
---------------------------------------------------------------------------------
-function menu.update_last_game(world_idx)
-       if gamedata.selected_world <= #menu.worldlist then
-               local world = menu.worldlist[gamedata.selected_world]
-               
-               for i=1,#gamemgr.games,1 do             
-                       if gamemgr.games[i].id == world.gameid then
-                               menu.last_game = i
-                               engine.setting_set("main_menu_last_game_idx",menu.last_game)
-                               break
-                       end
-               end
+       local current_world = filterlist.get_raw_element(worldlist,
+                                                       engine.setting_get("mainmenu_last_selected_world")
+                                                       )
+                                                       
+       if current_world == nil then
+               return
        end
-end
-
---------------------------------------------------------------------------------
-function menubar.handle_buttons(fields)
-       for i=1,#menubar.buttons,1 do
-               if fields[menubar.buttons[i].btn_name] ~= nil then
-                       menu.last_game = menubar.buttons[i].index
+               
+       for i=1,#gamemgr.games,1 do             
+               if gamemgr.games[i].id == current_world.gameid then
+                       menu.last_game = i
                        engine.setting_set("main_menu_last_game_idx",menu.last_game)
-                       menu.update_gametype()
+                       break
                end
        end
 end
 
 --------------------------------------------------------------------------------
-function menubar.refresh()
-       menubar.formspec = "box[-2,7.625;15.75,1.75;BLK]"
-       menubar.buttons = {}
+function menu.handle_key_up_down(fields,textlist,settingname)
 
-       local button_base = -1.8
-       
-       local maxbuttons = #gamemgr.games
-       
-       if maxbuttons > 12 then
-               maxbuttons = 12
+       if fields["key_up"] then
+               local oldidx = engine.get_textlist_index(textlist)
+               
+               if oldidx > 1 then
+                       local newidx = oldidx -1
+                       engine.setting_set(settingname,
+                               filterlist.get_raw_index(worldlist,newidx))
+               end
        end
        
-       for i=1,maxbuttons,1 do
-
-               local btn_name = "menubar_btn_" .. gamemgr.games[i].id
-               local buttonpos = button_base + (i-1) * 1.3
-               if gamemgr.games[i].menuicon_path ~= nil and
-                       gamemgr.games[i].menuicon_path ~= "" then
-
-                       menubar.formspec = menubar.formspec ..
-                               "image_button[" .. buttonpos ..  ",7.9;1.3,1.3;"  ..
-                               gamemgr.games[i].menuicon_path .. ";" .. btn_name .. ";;true;false]"
-               else
+       if fields["key_down"] then
+               local oldidx = engine.get_textlist_index(textlist)
                
-                       local part1 = gamemgr.games[i].id:sub(1,5)
-                       local part2 = gamemgr.games[i].id:sub(6,10)
-                       local part3 = gamemgr.games[i].id:sub(11)
-                       
-                       local text = part1 .. "\n" .. part2
-                       if part3 ~= nil and
-                               part3 ~= "" then
-                               text = text .. "\n" .. part3
-                       end
-                       menubar.formspec = menubar.formspec ..
-                               "image_button[" .. buttonpos ..  ",7.9;1.3,1.3;;" ..btn_name ..
-                               ";" .. text .. ";true;true]"
+               if oldidx < filterlist.size(worldlist) then
+                       local newidx = oldidx + 1
+                       engine.setting_set(settingname,
+                               filterlist.get_raw_index(worldlist,newidx))
                end
-               
-               local toadd = {
-                       btn_name = btn_name,
-                       index = i,
-               }
-               
-               table.insert(menubar.buttons,toadd)
        end
 end
 
@@ -470,7 +308,7 @@ end
 
 --------------------------------------------------------------------------------
 function tabbuilder.dialog_delete_world()
-       return  "label[2,2;Delete World \"" .. menu.lastworld().name .. "\"?]"..
+       return  "label[2,2;Delete World \"" .. filterlist.get_raw_list(worldlist)[menu.world_to_del].name .. "\"?]"..
                        "button[3.5,4.2;2.6,0.5;world_delete_confirm;Yes]" ..
                        "button[6,4.2;2.8,0.5;world_delete_cancel;No]"
 end
@@ -499,6 +337,10 @@ function tabbuilder.gettab()
                retval = retval .. tabbuilder.tab_settings()
        end
        
+       if tabbuilder.current_tab == "texture_packs" then
+               retval = retval .. tabbuilder.tab_TP()
+       end
+       
        if tabbuilder.current_tab == "credits" then
                retval = retval .. tabbuilder.tab_credits()
        end
@@ -521,41 +363,33 @@ end
 --------------------------------------------------------------------------------
 function tabbuilder.handle_create_world_buttons(fields)
        
-       if fields["world_create_confirm"] then
+       if fields["world_create_confirm"] or
+               fields["key_enter"] then
                
                local worldname = fields["te_world_name"]
                local gameindex = engine.get_textlist_index("games")
                
                if gameindex > 0 and
                        worldname ~= "" then
-                       engine.setting_set("mg_name",fields["dd_mapgen"])
-                       local message = engine.create_world(worldname,gameindex)
                        
-                       menu.last_game = gameindex
-                       engine.setting_set("main_menu_last_game_idx",gameindex)
+                       local message = nil
+                       
+                       if not filterlist.uid_exists_raw(worldlist,worldname) then
+                               engine.setting_set("mg_name",fields["dd_mapgen"])
+                               message = engine.create_world(worldname,gameindex)
+                       else
+                               message = "A world named \"" .. worldname .. "\" already exists"
+                       end
                        
                        if message ~= nil then
                                gamedata.errormessage = message
                        else
-                               menu.worldlist = engine.get_worlds()
-                               
-                               local worldlist = menu.worldlist
+                               menu.last_game = gameindex
+                               engine.setting_set("main_menu_last_game_idx",gameindex)
                                
-                               if tabbuilder.current_tab == "singleplayer" then
-                                       worldlist = menu.filtered_game_list_raw()
-                               end
-                               
-                               local index = 0
-                               
-                               for i=1,#worldlist,1 do
-                                       if worldlist[i].name == worldname then
-                                               index = i
-                                               break
-                                       end
-                               end
-
-                               engine.setting_set("main_menu_singleplayer_world_idx", index)
-                               menu.last_world = index
+                               filterlist.refresh(worldlist)
+                               engine.setting_set("mainmenu_last_selected_world",
+                                                                       filterlist.raw_index_by_uid(worldlist,worldname))
                        end
                else
                        gamedata.errormessage = "No worldname given or no game selected"
@@ -567,6 +401,7 @@ function tabbuilder.handle_create_world_buttons(fields)
                return
        end
        
+       --close dialog
        tabbuilder.is_dialog = false
        tabbuilder.show_buttons = true
        tabbuilder.current_tab = engine.setting_get("main_menu_tab")
@@ -576,11 +411,11 @@ end
 function tabbuilder.handle_delete_world_buttons(fields)
        
        if fields["world_delete_confirm"] then
-               if menu.last_world > 0 and 
-                       menu.last_world < #menu.worldlist then
-                       engine.delete_world(menu.last_world)
-                       menu.worldlist = engine.get_worlds()
-                       menu.last_world = 1
+               if menu.world_to_del > 0 and 
+                       menu.world_to_del <= #filterlist.get_raw_list(worldlist) then
+                       engine.delete_world(menu.world_to_del)
+                       menu.world_to_del = 0
+                       filterlist.refresh(worldlist)
                end
        end
        
@@ -591,18 +426,28 @@ end
 
 --------------------------------------------------------------------------------
 function tabbuilder.handle_multiplayer_buttons(fields)
+       
+       if fields["te_name"] ~= nil then
+               gamedata.playername = fields["te_name"]
+               engine.setting_set("name", fields["te_name"])
+       end
+       
        if fields["favourites"] ~= nil then
                local event = explode_textlist_event(fields["favourites"])
                if event.typ == "DCL" then
-                       gamedata.address = menu.favorites[event.index].name
-                       if gamedata.address == nil then
-                               gamedata.address = menu.favorites[event.index].address
-                       end
+                       gamedata.address = menu.favorites[event.index].address
                        gamedata.port = menu.favorites[event.index].port
                        gamedata.playername             = fields["te_name"]
-                       gamedata.password               = fields["te_pwd"]
+                       if fields["te_pwd"] ~= nil then
+                               gamedata.password               = fields["te_pwd"]
+                       end
                        gamedata.selected_world = 0
                        
+                       if menu.favorites ~= nil then
+                               gamedata.servername = menu.favorites[event.index].name
+                               gamedata.serverdescription = menu.favorites[event.index].description
+                       end
+                       
                        if gamedata.address ~= nil and
                                gamedata.port ~= nil then
                                
@@ -611,10 +456,7 @@ function tabbuilder.handle_multiplayer_buttons(fields)
                end
                
                if event.typ == "CHG" then
-                       local address = menu.favorites[event.index].name
-                       if address == nil then
-                               address = menu.favorites[event.index].address
-                       end
+                       local address = menu.favorites[event.index].address
                        local port = menu.favorites[event.index].port
                        
                        if address ~= nil and
@@ -622,7 +464,33 @@ function tabbuilder.handle_multiplayer_buttons(fields)
                                engine.setting_set("address",address)
                                engine.setting_set("port",port)
                        end
+                       
+                       menu.fav_selected = event.index
+               end
+               return
+       end
+       
+       if fields["key_up"] ~= nil or
+               fields["key_down"] ~= nil then
+               
+               local fav_idx = engine.get_textlist_index("favourites")
+               
+               if fields["key_up"] ~= nil and fav_idx > 1 then
+                       fav_idx = fav_idx -1
+               else if fields["key_down"] and fav_idx < #menu.favorites then
+                       fav_idx = fav_idx +1
+               end end
+               
+               local address = menu.favorites[fav_idx].address
+               local port = menu.favorites[fav_idx].port
+               
+               if address ~= nil and
+                       port ~= nil then
+                       engine.setting_set("address",address)
+                       engine.setting_set("port",port)
                end
+               
+               menu.fav_selected = fav_idx
                return
        end
        
@@ -635,12 +503,15 @@ function tabbuilder.handle_multiplayer_buttons(fields)
                else
                        menu.favorites = engine.get_favorites("local")
                end
+               menu.fav_selected = nil
+               return
        end
 
        if fields["btn_delete_favorite"] ~= nil then
                local current_favourite = engine.get_textlist_index("favourites")
                engine.delete_favorite(current_favourite)
                menu.favorites = engine.get_favorites()
+               menu.fav_selected = nil
                
                engine.setting_set("address","")
                engine.setting_get("port","")
@@ -648,13 +519,29 @@ function tabbuilder.handle_multiplayer_buttons(fields)
                return
        end
 
-       if fields["btn_mp_connect"] ~= nil then
+       if fields["btn_mp_connect"] ~= nil or
+               fields["key_enter"] then
+               
                gamedata.playername             = fields["te_name"]
                gamedata.password               = fields["te_pwd"]
                gamedata.address                = fields["te_address"]
                gamedata.port                   = fields["te_port"]
-               gamedata.selected_world = 0
+               
+               local fav_idx = engine.get_textlist_index("favourites")
+               
+               if fav_idx > 0 and fav_idx <= #menu.favorites and
+                       menu.favorites[fav_idx].address == fields["te_address"] and
+                       menu.favorites[fav_idx].port == fields["te_port"] then
+                       
+                       gamedata.servername                     = menu.favorites[fav_idx].name
+                       gamedata.serverdescription      = menu.favorites[fav_idx].description
+               else
+                       gamedata.servername = ""
+                       gamedata.serverdescription = ""
+               end
 
+               gamedata.selected_world = 0
+               
                engine.start()
                return
        end
@@ -665,14 +552,20 @@ function tabbuilder.handle_server_buttons(fields)
 
        local world_doubleclick = false
 
-       if fields["worlds"] ~= nil then
-               local event = explode_textlist_event(fields["worlds"])
+       if fields["srv_worlds"] ~= nil then
+               local event = explode_textlist_event(fields["srv_worlds"])
                
-               if event.typ == "DBL" then
+               if event.typ == "DCL" then
                        world_doubleclick = true
                end
+               if event.typ == "CHG" then
+                       engine.setting_set("mainmenu_last_selected_world",
+                               filterlist.get_raw_index(worldlist,engine.get_textlist_index("srv_worlds")))
+               end
        end
        
+       menu.handle_key_up_down(fields,"srv_worlds","mainmenu_last_selected_world")
+       
        if fields["cb_creative_mode"] then
                engine.setting_setbool("creative_mode",tabbuilder.tobool(fields["cb_creative_mode"]))
        end
@@ -681,18 +574,20 @@ function tabbuilder.handle_server_buttons(fields)
                engine.setting_setbool("enable_damage",tabbuilder.tobool(fields["cb_enable_damage"]))
        end
 
+       if fields["cb_server_announce"] then
+               engine.setting_setbool("server_announce",tabbuilder.tobool(fields["cb_server_announce"]))
+       end
+       
        if fields["start_server"] ~= nil or
-               world_doubleclick then
+               world_doubleclick or
+               fields["key_enter"] then
                local selected = engine.get_textlist_index("srv_worlds")
                if selected > 0 then
                        gamedata.playername             = fields["te_playername"]
-                       gamedata.password               = fields["te_pwd"]
-                       gamedata.address                = ""
+                       gamedata.password               = fields["te_passwd"]
                        gamedata.port                   = fields["te_serverport"]
-                       gamedata.selected_world = selected
-                       
-                       engine.setting_set("main_menu_tab",tabbuilder.current_tab)
-                       engine.setting_set("main_menu_last_world_idx",gamedata.selected_world)
+                       gamedata.address                = ""
+                       gamedata.selected_world = filterlist.get_raw_index(worldlist,selected)
                        
                        menu.update_last_game(gamedata.selected_world)
                        engine.start()
@@ -707,16 +602,18 @@ function tabbuilder.handle_server_buttons(fields)
        
        if fields["world_delete"] ~= nil then
                local selected = engine.get_textlist_index("srv_worlds")
-               if selected > 0 then
-                       menu.last_world = engine.get_textlist_index("worlds")
-                       if menu.lastworld() ~= nil and
-                               menu.lastworld().name ~= nil and
-                               menu.lastworld().name ~= "" then
+               if selected > 0 and
+                       selected <= filterlist.size(worldlist) then
+                       local world = filterlist.get_list(worldlist)[selected]
+                       if world ~= nil and
+                               world.name ~= nil and
+                               world.name ~= "" then
+                               menu.world_to_del = filterlist.get_raw_index(worldlist,selected)
                                tabbuilder.current_tab = "dialog_delete_world"
                                tabbuilder.is_dialog = true
                                tabbuilder.show_buttons = false
                        else
-                               menu.last_world = 0
+                               menu.world_to_del = 0
                        end
                end
        end
@@ -724,7 +621,7 @@ function tabbuilder.handle_server_buttons(fields)
        if fields["world_configure"] ~= nil then
                selected = engine.get_textlist_index("srv_worlds")
                if selected > 0 then
-                       modmgr.world_config_selected_world = selected
+                       modmgr.world_config_selected_world = filterlist.get_raw_index(worldlist,selected)
                        if modmgr.init_worldconfig() then
                                tabbuilder.current_tab = "dialog_configure_world"
                                tabbuilder.is_dialog = true
@@ -748,7 +645,6 @@ function tabbuilder.handle_settings_buttons(fields)
        if fields["cb_fancy_trees"] then
                engine.setting_setbool("new_style_leaves",tabbuilder.tobool(fields["cb_fancy_trees"]))
        end
-               
        if fields["cb_smooth_lighting"] then
                engine.setting_setbool("smooth_lighting",tabbuilder.tobool(fields["cb_smooth_lighting"]))
        end
@@ -758,7 +654,10 @@ function tabbuilder.handle_settings_buttons(fields)
        if fields["cb_opaque_water"] then
                engine.setting_setbool("opaque_water",tabbuilder.tobool(fields["cb_opaque_water"]))
        end
-                       
+       if fields["old_style_modselection"] then
+               engine.setting_setbool("old_style_mod_selection",tabbuilder.tobool(fields["old_style_modselection"]))
+       end
+       
        if fields["cb_mipmapping"] then
                engine.setting_setbool("mip_map",tabbuilder.tobool(fields["cb_mipmapping"]))
        end
@@ -801,8 +700,15 @@ function tabbuilder.handle_singleplayer_buttons(fields)
                if event.typ == "DCL" then
                        world_doubleclick = true
                end
+               
+               if event.typ == "CHG" then
+                       engine.setting_set("mainmenu_last_selected_world",
+                               filterlist.get_raw_index(worldlist,engine.get_textlist_index("sp_worlds")))
+               end
        end
        
+       menu.handle_key_up_down(fields,"sp_worlds","mainmenu_last_selected_world")
+       
        if fields["cb_creative_mode"] then
                engine.setting_setbool("creative_mode",tabbuilder.tobool(fields["cb_creative_mode"]))
        end
@@ -812,14 +718,12 @@ function tabbuilder.handle_singleplayer_buttons(fields)
        end
 
        if fields["play"] ~= nil or
-               world_doubleclick then
+               world_doubleclick or
+               fields["key_enter"] then
                local selected = engine.get_textlist_index("sp_worlds")
                if selected > 0 then
-                       gamedata.selected_world = menu.filtered_index_to_plain(selected)
+                       gamedata.selected_world = filterlist.get_raw_index(worldlist,selected)
                        gamedata.singleplayer   = true
-
-                       engine.setting_set("main_menu_tab",tabbuilder.current_tab)
-                       engine.setting_set("main_menu_singleplayer_world_idx",selected)
                        
                        menu.update_last_game(gamedata.selected_world)
                        
@@ -835,16 +739,18 @@ function tabbuilder.handle_singleplayer_buttons(fields)
        
        if fields["world_delete"] ~= nil then
                local selected = engine.get_textlist_index("sp_worlds")
-               if selected > 0 then
-                       menu.last_world = menu.filtered_index_to_plain(selected)
-                       if menu.lastworld() ~= nil and
-                               menu.lastworld().name ~= nil and
-                               menu.lastworld().name ~= "" then
+               if selected > 0 and
+                       selected <= filterlist.size(worldlist) then
+                       local world = filterlist.get_list(worldlist)[selected]
+                       if world ~= nil and
+                               world.name ~= nil and
+                               world.name ~= "" then
+                               menu.world_to_del = filterlist.get_raw_index(worldlist,selected)
                                tabbuilder.current_tab = "dialog_delete_world"
                                tabbuilder.is_dialog = true
                                tabbuilder.show_buttons = false
                        else
-                               menu.last_world = 0
+                               menu.world_to_del = 0
                        end
                end
        end
@@ -852,7 +758,7 @@ function tabbuilder.handle_singleplayer_buttons(fields)
        if fields["world_configure"] ~= nil then
                selected = engine.get_textlist_index("sp_worlds")
                if selected > 0 then
-                       modmgr.world_config_selected_world = menu.filtered_index_to_plain(selected)
+                       modmgr.world_config_selected_world = filterlist.get_raw_index(worldlist,selected)
                        if modmgr.init_worldconfig() then
                                tabbuilder.current_tab = "dialog_configure_world"
                                tabbuilder.is_dialog = true
@@ -862,6 +768,23 @@ function tabbuilder.handle_singleplayer_buttons(fields)
        end
 end
 
+--------------------------------------------------------------------------------
+function tabbuilder.handle_TP_buttons(fields)
+       if fields["TPs"] ~= nil then
+               local event = explode_textlist_event(fields["TPs"])
+               if event.typ == "CHG" or event.typ=="DCL" then
+                       local index = engine.get_textlist_index("TPs")
+                       engine.setting_set("mainmenu_last_selected_TP",
+                               index)
+                       local TPlist = filterTP(engine.get_dirlist(engine.get_texturepath(), true))
+                       local TPname = TPlist[engine.get_textlist_index("TPs")]
+                       local TPpath = engine.get_texturepath()..DIR_DELIM..TPname
+                       if TPname == "None" then TPpath = "" end
+                       engine.setting_set("texture_path", TPpath)
+               end
+       end
+end
+
 --------------------------------------------------------------------------------
 function tabbuilder.tab_header()
 
@@ -896,7 +819,7 @@ function tabbuilder.handle_tab_buttons(fields)
        --handle tab changes
        if tabbuilder.current_tab ~= tabbuilder.old_tab then
                if tabbuilder.current_tab ~= "singleplayer" then
-                       menu.reset_gametype()
+                       menu.update_gametype(true)
                end
        end
        
@@ -917,7 +840,6 @@ function tabbuilder.init()
                engine.setting_set("main_menu_tab",tabbuilder.current_tab)
        end
        
-       
        --initialize tab buttons
        tabbuilder.last_tab = nil
        tabbuilder.show_buttons = true
@@ -927,6 +849,7 @@ function tabbuilder.init()
        table.insert(tabbuilder.current_buttons,{name="multiplayer", caption="Client"})
        table.insert(tabbuilder.current_buttons,{name="server", caption="Server"})
        table.insert(tabbuilder.current_buttons,{name="settings", caption="Settings"})
+       table.insert(tabbuilder.current_buttons,{name="texture_packs", caption="Texture Packs"})
        
        if engine.setting_getbool("main_menu_game_mgr") then
                table.insert(tabbuilder.current_buttons,{name="game_mgr", caption="Games"})
@@ -944,41 +867,72 @@ function tabbuilder.init()
                end
        end
        
-       menu.update_gametype()
+       if tabbuilder.current_tab ~= "singleplayer" then
+               menu.update_gametype(true)
+       else
+               menu.update_gametype()
+       end
 end
 
 --------------------------------------------------------------------------------
 function tabbuilder.tab_multiplayer()
+
        local retval =
                "vertlabel[0,-0.25;CLIENT]" ..
                "label[1,-0.25;Favorites:]"..
                "label[1,4.25;Address/Port]"..
-               "label[9,0;Name/Password]" ..
+               "label[9,2.75;Name/Password]" ..
                "field[1.25,5.25;5.5,0.5;te_address;;" ..engine.setting_get("address") .."]" ..
                "field[6.75,5.25;2.25,0.5;te_port;;" ..engine.setting_get("port") .."]" ..
-               "button[6.45,3.95;2.25,0.5;btn_delete_favorite;Delete]" ..
-               "button[9,4.95;2.5,0.5;btn_mp_connect;Connect]" ..
-               "field[9.25,1;2.5,0.5;te_name;;" ..engine.setting_get("name") .."]" ..
-               "pwdfield[9.25,1.75;2.5,0.5;te_pwd;]" ..
                "checkbox[1,3.6;cb_public_serverlist;Public Serverlist;" ..
-               dump(engine.setting_getbool("public_serverlist")) .. "]" ..
+               dump(engine.setting_getbool("public_serverlist")) .. "]"
+               
+       if not engine.setting_getbool("public_serverlist") then
+               retval = retval .. 
+               "button[6.45,3.95;2.25,0.5;btn_delete_favorite;Delete]"
+       end
+       
+       retval = retval ..
+               "button[9,4.95;2.5,0.5;btn_mp_connect;Connect]" ..
+               "field[9.3,3.75;2.5,0.5;te_name;;" ..engine.setting_get("name") .."]" ..
+               "pwdfield[9.3,4.5;2.5,0.5;te_pwd;]" ..
+               "textarea[9.3,0.25;2.5,2.75;;"
+       if menu.fav_selected ~= nil and 
+               menu.favorites[menu.fav_selected].description ~= nil then
+               retval = retval .. 
+                       fs_escape_string(menu.favorites[menu.fav_selected].description,true)
+       end
+       
+       retval = retval .. 
+               ";]" ..
                "textlist[1,0.35;7.5,3.35;favourites;"
 
+       local render_details = engine.setting_getbool("public_serverlist")
+
        if #menu.favorites > 0 then
-               retval = retval .. render_favourite(menu.favorites[1])
+               retval = retval .. menu.render_favorite(menu.favorites[1],render_details)
                
                for i=2,#menu.favorites,1 do
-                       retval = retval .. "," .. render_favourite(menu.favorites[i])
+                       retval = retval .. "," .. menu.render_favorite(menu.favorites[i],render_details)
                end
        end
 
-       retval = retval .. ";1]"
+       if menu.fav_selected ~= nil then
+               retval = retval .. ";" .. menu.fav_selected .. "]"
+       else
+               retval = retval .. ";0]"
+       end
 
        return retval
 end
 
 --------------------------------------------------------------------------------
 function tabbuilder.tab_server()
+
+       local index = filterlist.get_current_index(worldlist,
+                               tonumber(engine.setting_get("mainmenu_last_selected_world"))
+                               )
+       
        local retval = 
                "button[4,4.15;2.6,0.5;world_delete;Delete]" ..
                "button[6.5,4.15;2.8,0.5;world_create;New]" ..
@@ -990,23 +944,15 @@ function tabbuilder.tab_server()
                dump(engine.setting_getbool("creative_mode")) .. "]"..
                "checkbox[0.5,0.7;cb_enable_damage;Enable Damage;" ..
                dump(engine.setting_getbool("enable_damage")) .. "]"..
-               "field[0.8,2.2;3,0.5;te_playername;Name;" ..
+               "checkbox[0.5,1.15;cb_server_announce;Public;" ..
+               dump(engine.setting_getbool("server_announce")) .. "]"..
+               "field[0.8,3.2;3,0.5;te_playername;Name;" ..
                engine.setting_get("name") .. "]" ..
-               "pwdfield[0.8,3.2;3,0.5;te_passwd;Password]" ..
+               "pwdfield[0.8,4.2;3,0.5;te_passwd;Password]" ..
                "field[0.8,5.2;3,0.5;te_serverport;Server Port;30000]" ..
-               "textlist[4,0.25;7.5,3.7;srv_worlds;"
-       
-       if #menu.worldlist > 0 then
-               retval = retval .. menu.worldlist[1].name .. 
-                                               " [[" .. menu.worldlist[1].gameid .. "]]"
-                               
-               for i=2,#menu.worldlist,1 do
-                       retval = retval .. "," .. menu.worldlist[i].name .. 
-                                               " [[" .. menu.worldlist[i].gameid .. "]]"
-               end
-       end
-                               
-       retval = retval .. ";" .. menu.last_world .. "]"
+               "textlist[4,0.25;7.5,3.7;srv_worlds;" ..
+               menu.render_world_list() ..
+               ";" .. index .. "]"
                
        return retval
 end
@@ -1018,6 +964,7 @@ function tabbuilder.tab_settings()
                        "checkbox[1,1.25;cb_smooth_lighting;Smooth Lighting;".. dump(engine.setting_getbool("smooth_lighting")) .. "]"..
                        "checkbox[1,1.75;cb_3d_clouds;3D Clouds;"                       .. dump(engine.setting_getbool("enable_3d_clouds"))     .. "]"..
                        "checkbox[1,2.25;cb_opaque_water;Opaque Water;"                 .. dump(engine.setting_getbool("opaque_water"))         .. "]"..
+                       "checkbox[1,2.75;old_style_modselection;Old style modmgr;" .. dump(engine.setting_getbool("old_style_mod_selection"))           .. "]"..
                        
                        "checkbox[4,0.75;cb_mipmapping;Mip-Mapping;"            .. dump(engine.setting_getbool("mip_map"))                      .. "]"..
                        "checkbox[4,1.25;cb_anisotrophic;Anisotropic Filtering;".. dump(engine.setting_getbool("anisotropic_filter"))   .. "]"..
@@ -1029,16 +976,15 @@ function tabbuilder.tab_settings()
                        "checkbox[7.5,1.75;cb_particles;Enable Particles;"      .. dump(engine.setting_getbool("enable_particles"))     .. "]"..
                        "checkbox[7.5,2.25;cb_finite_liquid;Finite Liquid;"     .. dump(engine.setting_getbool("liquid_finite"))                .. "]"..
                        
-                       "button[1,3.75;2.25,0.5;btn_change_keys;Change keys]"
+                       "button[1,4.25;2.25,0.5;btn_change_keys;Change keys]"
 end
 
 --------------------------------------------------------------------------------
 function tabbuilder.tab_singleplayer()
-       local index = engine.setting_get("main_menu_singleplayer_world_idx")
-
-       if index == nil then
-               index = 0
-       end
+       
+       local index = filterlist.get_current_index(worldlist,
+                               tonumber(engine.setting_get("mainmenu_last_selected_world"))
+                               )
 
        return  "button[4,4.15;2.6,0.5;world_delete;Delete]" ..
                        "button[6.5,4.15;2.8,0.5;world_create;New]" ..
@@ -1051,40 +997,81 @@ function tabbuilder.tab_singleplayer()
                        "checkbox[0.5,0.7;cb_enable_damage;Enable Damage;" ..
                        dump(engine.setting_getbool("enable_damage")) .. "]"..
                        "textlist[4,0.25;7.5,3.7;sp_worlds;" ..
-                       menu.filtered_game_list() ..
+                       menu.render_world_list() ..
                        ";" .. index .. "]" ..
                        menubar.formspec
 end
 
+--------------------------------------------------------------------------------
+function tabbuilder.tab_TP()
+       local TPpath = engine.setting_get("texture_path")
+       local TPlist = filterTP(engine.get_dirlist(engine.get_texturepath(), true))
+       local index = tonumber(engine.setting_get("mainmenu_last_selected_TP"))
+       if index == nil then index = 1 end
+       if TPpath == "" then
+               return  "label[4,-0.25;Select texture pack:]"..
+                       "vertlabel[0,-0.25;TEXTURE PACKS]" ..
+                       "textlist[4,0.25;7.5,5.0;TPs;" ..
+                       menu.render_TP_list(TPlist) ..
+                       ";" .. index .. "]"
+       end
+       local TPinfofile = TPpath..DIR_DELIM.."info.txt"
+       local f = io.open(TPinfofile, "r")
+       if f==nil then
+               menu.TPinfo = "No information available" 
+       else
+               menu.TPinfo = f:read("*all")
+               f:close()
+       end
+       local TPscreenfile = TPpath..DIR_DELIM.."screenshot.png"
+       local f = io.open(TPscreenfile, "r")
+       if f==nil then
+               menu.TPscreen = nil
+       else
+               menu.TPscreen = TPscreenfile
+               f:close()
+       end
+       
+       local no_screenshot = engine.get_texturepath()..DIR_DELIM.."base"..DIR_DELIM.."pack"..DIR_DELIM.."no_screenshot.png"
+
+       return  "label[4,-0.25;Select texture pack:]"..
+                       "vertlabel[0,-0.25;TEXTURE PACKS]" ..
+                       "textlist[4,0.25;7.5,5.0;TPs;" ..
+                       menu.render_TP_list(TPlist) ..
+                       ";" .. index .. "]" ..
+                       "image[0.65,0.25;4.0,3.7;"..(menu.TPscreen or no_screenshot).."]"..
+                       "textarea[1.0,3.25;3.7,1.5;;"..(menu.TPinfo or "")..";]"
+end
+
 --------------------------------------------------------------------------------
 function tabbuilder.tab_credits()
        return  "vertlabel[0,-0.5;CREDITS]" ..
                        "label[0.5,3;Minetest " .. engine.get_version() .. "]" ..
                        "label[0.5,3.3;http://minetest.net]" .. 
-                       "image[0.5,1;" .. menu.basetexturedir .. "logo.png]" ..
+                       "image[0.5,1;" .. menu.defaulttexturedir .. "logo.png]" ..
                        "textlist[3.5,-0.25;8.5,5.8;list_credits;" ..
-                       "#YLWCore Developers," ..
+                       "#FFFF00Core Developers," ..
                        "Perttu Ahola (celeron55) <celeron55@gmail.com>,"..
                        "Ryan Kwolek (kwolekr) <kwolekr@minetest.net>,"..
                        "PilzAdam <pilzadam@minetest.net>," ..
-                       "IIya Zhuravlev (thexyz) <xyz@minetest.net>,"..
+                       "Ilya Zhuravlev (xyz) <xyz@minetest.net>,"..
                        "Lisa Milne (darkrose) <lisa@ltmnet.com>,"..
                        "Maciej Kasatkin (RealBadAngel) <mk@realbadangel.pl>,"..
                        "proller <proler@gmail.com>,"..
                        "sfan5 <sfan5@live.de>,"..
                        "kahrl <kahrl@gmx.net>,"..
                        ","..
-                       "#YLWActive Contributors," ..
+                       "#FFFF00Active Contributors," ..
                        "sapier,"..
                        "Vanessa Ezekowitz (VanessaE) <vanessaezekowitz@gmail.com>,"..
                        "Jurgen Doser (doserj) <jurgen.doser@gmail.com>,"..
                        "Jeija <jeija@mesecons.net>,"..
                        "MirceaKitsune <mirceakitsune@gmail.com>,"..
-                       "ShadowNinja"..
-                       "dannydark <the_skeleton_of_a_child@yahoo.co.uk>"..
+                       "ShadowNinja,"..
+                       "dannydark <the_skeleton_of_a_child@yahoo.co.uk>,"..
                        "0gb.us <0gb.us@0gb.us>,"..
                        "," ..
-                       "#YLWPrevious Contributors," ..
+                       "#FFFF00Previous Contributors," ..
                        "Guiseppe Bilotta (Oblomov) <guiseppe.bilotta@gmail.com>,"..
                        "Jonathan Neuschafer <j.neuschaefer@gmx.net>,"..
                        "Nils Dagsson Moskopp (erlehmann) <nils@dieweltistgarnichtso.net>,"..
@@ -1149,6 +1136,10 @@ engine.button_handler = function(fields)
                tabbuilder.handle_singleplayer_buttons(fields)
        end
        
+       if tabbuilder.current_tab == "texture_packs" then
+               tabbuilder.handle_TP_buttons(fields)
+       end
+       
        if tabbuilder.current_tab == "multiplayer" then
                tabbuilder.handle_multiplayer_buttons(fields)
        end
@@ -1182,6 +1173,7 @@ engine.event_handler = function(event)
                        tabbuilder.is_dialog = false
                        tabbuilder.show_buttons = true
                        tabbuilder.current_tab = engine.setting_get("main_menu_tab")
+                       menu.update_gametype()
                        update_menu()
                else
                        engine.close()
@@ -1189,14 +1181,32 @@ engine.event_handler = function(event)
        end
 end
 
+--------------------------------------------------------------------------------
+function menu.update_gametype(reset)
+       if reset then
+               mm_texture.reset()
+               engine.set_topleft_text("")
+               filterlist.set_filtercriteria(worldlist,nil)
+       else
+               local game = menu.lastgame()
+               mm_texture.update(tabbuilder.current_tab,game)
+               engine.set_topleft_text(game.name)
+               filterlist.set_filtercriteria(worldlist,game.id)
+       end
+end
+
 --------------------------------------------------------------------------------
 --------------------------------------------------------------------------------
 -- menu startup
 --------------------------------------------------------------------------------
 --------------------------------------------------------------------------------
 init_globals()
+mm_texture.init()
 menu.init()
 tabbuilder.init()
 menubar.refresh()
 modstore.init()
+
+engine.sound_play("main_menu", true)
+
 update_menu()