Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Allow random menu images for subgames
  • Loading branch information
sfan5 authored and est31 committed Jul 21, 2015
1 parent fa7fe51 commit 8994913
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 7 deletions.
16 changes: 15 additions & 1 deletion builtin/common/misc_helpers.lua
Expand Up @@ -159,6 +159,7 @@ function dump(o, indent, nested, level)
return "{"..table.concat(t, ", ").."}"
end

--------------------------------------------------------------------------------
function string.split(str, delim, include_empty, max_splits, sep_is_pattern)
delim = delim or ","
max_splits = max_splits or -1
Expand All @@ -183,10 +184,23 @@ function string.split(str, delim, include_empty, max_splits, sep_is_pattern)
return items
end

--------------------------------------------------------------------------------
function table.indexof(list, val)
for i = 1, #list do
if list[i] == val then
return i
end
end
return -1
end

assert(table.indexof({"foo", "bar"}, "foo") == 1)
assert(table.indexof({"foo", "bar"}, "baz") == -1)

--------------------------------------------------------------------------------
function file_exists(filename)
local f = io.open(filename, "r")
if f==nil then
if f == nil then
return false
else
f:close()
Expand Down
30 changes: 24 additions & 6 deletions builtin/mainmenu/textures.lua
Expand Up @@ -129,23 +129,41 @@ 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
end

if mm_texture.texturepack ~= nil then
local path = mm_texture.texturepack .. DIR_DELIM ..
gamedetails.id .. "_menu_" .. identifier .. ".png"
if core.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 core.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)

This comment has been minimized.

Copy link
@RobertZenz

RobertZenz Jul 21, 2015

Contributor

If I'm seeing this correctly, n can be nil at this point which will yield an error. The error should occur if there are no menu images provided by the game.

 ERROR[main]: ...robert/src/minetest/bin/../builtin/mainmenu/textures.lua:157: bad argument #2 to 'random' (number expected, got nil)

Either initial n to zero or wrap the block in an if #menu_files > 0 then.

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

Expand Down

0 comments on commit 8994913

Please sign in to comment.