]> git.lizzy.rs Git - minetest.git/blobdiff - builtin/mainmenu/textures.lua
Allow random menu images for subgames
[minetest.git] / builtin / mainmenu / textures.lua
index 998fc219973b251b20ae22ff3baa71b398a86c82..700bdb5cc05a0caa957cc1cd12723000c0dffd26 100644 (file)
@@ -20,11 +20,11 @@ mm_texture = {}
 
 --------------------------------------------------------------------------------
 function mm_texture.init()
-       mm_texture.defaulttexturedir = engine.get_texturepath() .. DIR_DELIM .. "base" .. 
+       mm_texture.defaulttexturedir = core.get_texturepath() .. DIR_DELIM .. "base" ..
                                                DIR_DELIM .. "pack" .. DIR_DELIM
        mm_texture.basetexturedir = mm_texture.defaulttexturedir
        
-       mm_texture.texturepack = engine.setting_get("texture_path")
+       mm_texture.texturepack = core.setting_get("texture_path")
        
        mm_texture.gameid = nil
 end
@@ -55,14 +55,17 @@ function mm_texture.reset()
        
        mm_texture.clear("header")
        mm_texture.clear("footer")
-       engine.set_clouds(false)
+       core.set_clouds(false)
        
        mm_texture.set_generic("footer")
        mm_texture.set_generic("header")
        
-       if not have_bg and
-               engine.setting_getbool("enable_clouds") then
-                       engine.set_clouds(true)
+       if not have_bg then
+               if core.setting_getbool("menu_clouds") then
+                       core.set_clouds(true)
+               else
+                       mm_texture.set_dirt_bg()
+               end
        end
 end
 
@@ -72,7 +75,7 @@ function mm_texture.update_game(gamedetails)
                return
        end
        
-       local have_bg      = false 
+       local have_bg      = false
        local have_overlay = mm_texture.set_game("overlay",gamedetails)
        
        if not have_overlay then
@@ -81,11 +84,15 @@ function mm_texture.update_game(gamedetails)
        
        mm_texture.clear("header")
        mm_texture.clear("footer")
-       engine.set_clouds(false)
-       
-       if not have_bg and
-               engine.setting_getbool("enable_clouds") then
-                       engine.set_clouds(true)
+       core.set_clouds(false)
+       
+       if not have_bg then
+               
+               if core.setting_getbool("menu_clouds") then
+                       core.set_clouds(true)
+               else
+                       mm_texture.set_dirt_bg()
+               end
        end
        
        mm_texture.set_game("footer",gamedetails)
@@ -96,24 +103,24 @@ end
 
 --------------------------------------------------------------------------------
 function mm_texture.clear(identifier)
-       engine.set_background(identifier,"")
+       core.set_background(identifier,"")
 end
 
 --------------------------------------------------------------------------------
 function mm_texture.set_generic(identifier)
        --try texture pack first
        if mm_texture.texturepack ~= nil then
-               local path = mm_texture.texturepack .. DIR_DELIM .."menu_" .. 
+               local path = mm_texture.texturepack .. DIR_DELIM .."menu_" ..
                                                                                identifier .. ".png"
-               if engine.set_background(identifier,path) then
+               if core.set_background(identifier,path) then
                        return true
                end
        end
        
        if mm_texture.defaulttexturedir ~= nil then
-               local path = mm_texture.defaulttexturedir .. DIR_DELIM .."menu_" .. 
+               local path = mm_texture.defaulttexturedir .. DIR_DELIM .."menu_" ..
                                                                                identifier .. ".png"
-               if engine.set_background(identifier,path) then
+               if core.set_background(identifier,path) then
                        return true
                end
        end
@@ -122,7 +129,7 @@ function mm_texture.set_generic(identifier)
 end
 
 --------------------------------------------------------------------------------
-function mm_texture.set_game(identifier,gamedetails)
+function mm_texture.set_game(identifier, gamedetails)
        
        if gamedetails == nil then
                return false
@@ -130,17 +137,48 @@ function mm_texture.set_game(identifier,gamedetails)
 
        if mm_texture.texturepack ~= nil then
                local path = mm_texture.texturepack .. DIR_DELIM ..
-                                               gamedetails.id .. "_menu_" .. identifier .. ".png"
-               if engine.set_background(identifier,path) then
+                       gamedetails.id .. "_menu_" .. identifier .. ".png"
+               if core.set_background(identifier, path) then
                        return true
                end
        end
        
-       local path = gamedetails.path .. DIR_DELIM .."menu" .. 
-                                                                        DIR_DELIM .. identifier .. ".png"
-       if engine.set_background(identifier,path) then
+       -- Find out how many randomized textures the subgame provides
+       local n, filename
+       local menu_files = core.get_dir_list(gamedetails.path .. DIR_DELIM .. "menu", false)
+       for i = 1, #menu_files do
+               local filename = identifier .. "." .. i .. ".png"
+               if table.indexof(menu_files, filename) == -1 then
+                       n = i - 1
+                       break
+               end
+       end
+       -- Select random texture, 0 means standard texture
+       n = math.random(0, n)
+       if n == 0 then
+               filename = identifier .. ".png"
+       else
+               filename = identifier .. "." .. n .. ".png"
+       end
+
+       local path = gamedetails.path .. DIR_DELIM .. "menu" ..
+               DIR_DELIM .. filename
+       if core.set_background(identifier, path) then
                return true
        end
        
        return false
 end
+
+function mm_texture.set_dirt_bg()
+       if mm_texture.texturepack ~= nil then
+               local path = mm_texture.texturepack .. DIR_DELIM .."default_dirt.png"
+               if core.set_background("background", path, true, 128) then
+                       return true
+               end
+       end
+       
+       --use base pack
+       local minimalpath = defaulttexturedir .. "dirt_bg.png"
+       core.set_background("background", minimalpath, true, 128)
+end