Module:LootChest

local p = {

-- these define which sprite and label to use, in the result table -- NOTE: order here doesn't matter. will sort by chance, then by avg#, then alphabetically.

items = { ["diamond"]            = { "item", "Diamond" }, ["iron-ingot"]         = { "item", "Iron Ingot" }, ["gold-ingot"]         = { "item", "Gold Ingot" }, ["emerald"]            = { "item", "Emerald" }, ["bread"]              = { "item", "Bread" }, ["apple"]              = { "item", "Apple" }, ["iron-pickaxe"]       = { "item", "Iron Pickaxe" }, ["iron-sword"]         = { "item", "Iron Sword" }, ["iron-chestplate"]    = { "item", "Iron Chestplace" }, ["iron-helmet"]        = { "item", "Iron Helmet" }, ["iron-leggings"]      = { "item", "Iron Leggings" }, ["iron-boots"]         = { "item", "Iron Boots" }, ["obsidian"]           = { "block", "Obsidian" }, ["oak-sapling"]        = { "block", "Oak Sapling" }, ["horse-armor-iron"]   = { "item", "Iron Horse Armor" }, ["horse-armor-gold"]   = { "item", "Gold Horse Armor" }, ["horse-armor-diamond"] = { "item", "Diamond Horse Armor" }, ["ender-pearl"]        = { "item", "Ender Pearl" }, ["redstone"]           = { "item", "Redstone" }, ["lapis-lazuli"]       = { "item", "Lapis Lazuli" }, ["golden-apple"]       = { "item", "Golden Apple" }, ["saddle"]             = { "item", "Saddle" }, ["enchanted-book"]     = { "item", "Enchanted Book" }, ["book"]               = { "item", "Book" }, ["paper"]              = { "item", "Paper" }, ["map"]                = { "item", "Map" }, ["compass"]            = { "item", "Compass" }, ["coal"]               = { "item", "Coal" }, ["stick"]              = { "item", "Stick" }, ["wood-planks"]        = { "block", "Oak Planks" }, ["oak-wood"]           = { "block", "Oak Wood" }, ["acacia-wood"]        = { "block", "Acacia Wood" }, ["stone-axe"]          = { "item", "Stone Axe" }, ["wooden-axe"]         = { "item", "Wooden Axe" }, ["stone-pickaxe"]      = { "item", "Stone Pickaxe" }, ["wooden-pickaxe"]     = { "item", "Wooden Pickaxe" }, ["wheat"]              = { "item", "Wheat" }, ["gunpowder"]          = { "item", "Gunpowder" }, ["string"]             = { "item", "String" }, ["bucket"]             = { "item", "Bucket" }, ["disc-13"]            = { "item", "Record 13" }, ["disc-cat"]           = { "item", "Record Cat" }, ["name-tag"]           = { "item", "Nametag" }, ["rail"]               = { "block", "Rail" }, ["melon-seeds"]        = { "item", "Melon Seeds" }, ["pumpkin-seeds"]      = { "item", "Pumpkin Seeds" }, ["golden-sword"]       = { "item", "Golden Sword" }, ["golden-chestplate"]  = { "item", "Golden Chestplate" }, ["flint-and-steel"]    = { "item", "Flint and Steel" }, ["nether-wart"]        = { "item", "Nether Wart" }, ["bone"]               = { "item", "Bone" }, ["rotten-flesh"]       = { "item", "Rotten Flesh" } },	-- accurate as of 1.8 -- NOTE: underscore _ prints spaces -- NOTE: hyphen - groups columns -- NOTE: order here doesn't matter. --		 * chests will sort by argument order, unless 'all', then this order. --      * items will sort by chance, then by avg#, then alphabetically. -- NOTE: any invalid item-id here will print as ??? chests = { ["village"] = { numstacks  = {3,8}, totalweight = nil, header     = "Village", items = { ["diamond"]            = {1,3,3}, ["iron-ingot"]         = {1,5,10}, ["gold-ingot"]         = {1,3,5}, ["bread"]              = {1,3,15}, ["apple"]              = {1,3,15}, ["iron-pickaxe"]       = {1,1,5}, ["iron-sword"]         = {1,1,5}, ["iron-chestplate"]    = {1,1,5}, ["iron-helmet"]        = {1,1,5}, ["iron-leggings"]      = {1,1,5}, ["iron-boots"]         = {1,1,5}, ["obsidian"]           = {3,7,5}, ["oak-sapling"]        = {3,7,5}, ["horse-armor-iron"]   = {1,1,1}, ["horse-armor-gold"]   = {1,1,1}, ["horse-armor-diamond"] = {1,1,1}, ["saddle"]             = {1,1,3} }		},		["stronghold-altar"] = { numstacks  = {2,4}, totalweight = nil, header     = "Altar", superheader = "Stronghold", items      = { ["diamond"]            = {1,3,3}, ["iron-ingot"]         = {1,5,10}, ["gold-ingot"]         = {1,3,5}, ["bread"]              = {1,3,5}, ["apple"]              = {1,3,15}, ["iron-pickaxe"]       = {1,1,5}, ["iron-sword"]         = {1,1,5}, ["iron-chestplate"]    = {1,1,5}, ["iron-helmet"]        = {1,1,5}, ["iron-leggings"]      = {1,1,5}, ["iron-boots"]         = {1,1,5}, ["horse-armor-iron"]   = {1,1,1}, ["horse-armor-gold"]   = {1,1,1}, ["horse-armor-diamond"] = {1,1,1}, ["ender-pearl"]        = {1,1,10}, ["redstone"]           = {4,9,5}, ["golden-apple"]       = {1,1,1}, ["saddle"]             = {1,1,1}, ["enchanted-book"]     = {1,1,1} }		},		["stronghold-library"] = { numstacks  = {1,4}, totalweight = nil, header     = "Library", superheader = "Stronghold", items      = { ["enchanted-book"]     = {1,5,2}, ["book"]               = {1,3,20}, ["paper"]              = {2,7,20}, ["map"]                = {1,1,1}, ["compass"]            = {1,1,1} }		},		["stronghold-storeroom"] = { numstacks  = {1,4}, totalweight = nil, header     = "Storeroom", superheader = "Stronghold", items      = { ["iron-ingot"]         = {1,5,10}, ["gold-ingot"]         = {1,3,5}, ["bread"]              = {1,3,5}, ["apple"]              = {1,3,5}, ["iron-pickaxe"]       = {1,1,1}, ["redstone"]           = {4,9,5}, ["enchanted-book"]     = {1,1,1}, ["coal"]               = {3,8,10} }		},		["bonus"] = { numstacks  = {10,10}, totalweight = nil, header     = "Bonus", items      = { ["bread"]              = {2,3,3}, ["apple"]              = {2,3,5}, ["stick"]              = {1,3,10}, ["wood-planks"]        = {1,3,10}, ["oak-wood"]           = {1,3,10}, ["acacia-wood"]        = {1,3,10}, ["stone-axe"]          = {1,1,3}, ["wooden-axe"]         = {1,1,5}, ["stone-pickaxe"]      = {1,1,3}, ["wooden-pickaxe"]     = {1,1,5} }		},		["dungeon"] = { numstacks  = {8,8}, totalweight = nil, header     = "Dungeon", items      = { ["iron-ingot"]         = {1,4,10}, ["bread"]              = {1,4,10}, ["horse-armor-iron"]   = {1,1,5}, ["horse-armor-gold"]   = {1,1,2}, ["horse-armor-diamond"] = {1,1,1}, ["redstone"]           = {1,4,10}, ["golden-apple"]       = {1,1,1}, ["saddle"]             = {1,1,10}, ["wheat"]              = {1,4,10}, ["gunpowder"]          = {1,4,10}, ["string"]             = {1,4,10}, ["bucket"]             = {1,1,10}, ["disc-13"]            = {1,1,4}, ["disc-cat"]           = {1,1,4}, ["name-tag"]           = {1,1,10} }		},		["mineshaft"] = { numstacks  = {3,6}, totalweight = nil, header     = "Mineshaft", items      = { ["diamond"]            = {1,2,3}, ["iron-ingot"]         = {1,5,10}, ["gold-ingot"]         = {1,3,5}, ["bread"]              = {1,3,15}, ["iron-pickaxe"]       = {1,1,1}, ["horse-armor-iron"]   = {1,1,1}, ["redstone"]           = {4,9,5}, ["lapis-lazuli"]       = {4,9,5}, ["saddle"]             = {1,1,3}, ["enchanted-book"]     = {1,1,1}, ["coal"]               = {3,8,10}, ["rail"]               = {4,8,1}, ["melon-seeds"]        = {2,4,10}, ["pumpkin-seeds"]      = {2,4,10} }		},		["nether-fortress"] = { numstacks  = {2,5}, totalweight = nil, header     = "Nether fortress", items      = { ["diamond"]            = {1,3,5}, ["iron-ingot"]         = {1,5,5}, ["gold-ingot"]         = {1,3,15}, ["obsidian"]           = {2,4,2}, ["horse-armor-iron"]   = {1,1,5}, ["horse-armor-gold"]   = {1,1,8}, ["horse-armor-diamond"] = {1,1,3}, ["saddle"]             = {1,1,10}, ["golden-sword"]       = {1,1,5}, ["golden-chestplate"]  = {1,1,5}, ["flint-and-steel"]    = {1,1,5}, ["nether-wart"]        = {5,7,5} }		},		["desert-jungle-temple"] = { numstacks  = {2,6}, totalweight = nil, header     = "Desert temple & jungle temple", items      = { ["diamond"]            = {1,3,3}, ["iron-ingot"]         = {1,5,10}, ["gold-ingot"]         = {2,7,15}, ["emerald"]            = {1,3,2}, ["horse-armor-iron"]   = {1,1,1}, ["horse-armor-gold"]   = {1,1,1}, ["horse-armor-diamond"] = {1,1,1}, ["saddle"]             = {1,1,3}, ["enchanted-book"]     = {1,1,1}, ["bone"]               = {4,6,20}, ["rotten-flesh"]       = {3,7,16} }		}	} }

function p.base( ... ) local args = arg[1] if args == mw.getCurrentFrame then args = require( 'Module:ProcessArgs' ).merge( true ) else args = arg end if #args == 0 then args = {"all"} end -- if argument 'all' exists, repopulate 'args' arguments with all chests

isall = false for key, val in pairs(args) do		if val == "all" then args = {} isall = true for key, val in pairs(p.chests) do				table.insert( args, key ) end break end end -- otherwise, remove invalid chest arguments

if not isall then for idx = #args, 1, -1 do			if p.chests[args[idx]] == nil then table.remove( args, idx ) end end end

table.sort( args )

-- for each chest that will ultimately display, -- tally the item total weight

for key, chestname in pairs(args) do		local chest = p.chests[chestname] if chest == nil then break end local total_weight = 0 for itemname, item in pairs(chest.items) do			total_weight = total_weight + item[3] end chest.totalweight = total_weight end

-- for each chest that will ultimately display, -- put 'avg number per chest' in position 4, for each chest item -- put 'chance to find any' in position 5, for each chest item for key, chestname in pairs(args) do		local chest = p.chests[chestname] if chest == nil then break end local avg_numstacks = ( chest.numstacks[1] + chest.numstacks[2] ) / 2 for itemname, item in pairs(chest.items) do			local avg_stacksize = ( item[1] + item[2] ) / 2 local item_weight = item[3] p.chests[chestname].items[itemname].avgamount = avg_numstacks * avg_stacksize * item_weight / chest.totalweight p.chests[chestname].items[itemname].chanceany = avg_numstacks * item_weight / chest.totalweight p.chests[chestname].items[itemname].itemname = itemname -- for sorting later end end -- for the first set of items, sort it by chance, then by avg amount, then alphabetically local items_sorted = {} for k,v in pairs(p.chests[args[1]].items) do		table.insert(items_sorted,v) end table.sort( items_sorted, function( e1, e2 )		if not ( e1.chanceany == e2.chanceany ) then return ( e1.chanceany > e2.chanceany ) end		if not ( e1.avgamount == e2.avgamount ) then return ( e1.avgamount > e2.avgamount ) end		return ( e1.itemname > e2.itemname )	end )

-- set up a row for each item local itemrows = {} local itemrowidxs = {} for key, chestname in pairs(args) do		local chest = p.chests[chestname] if chest == nil then break end for itemname, item in pairs(chest.items) do			if itemrowidxs[itemname] == nil then itemrowidxs[itemname] = {} end itemrows[chestname] = item end end

-- do some printing

-- local returnable = " " -- return returnable -- TODO: -- column headers -- make item rows that are sorted appropriately to the first chest, with the remaining items sorted alphabetically

return table.concat( args, ", " )

end

function p.doc

local keys = {} for key, val in pairs(p.chests) do		table.insert( keys, key ) end table.insert( keys, "all" ) table.sort(keys) return table.concat( keys, ", " )

end

return p