Module:LootChest

function table.pack(...) return { n = select("#", ...), ... } end

local p = {

-- these define which sprite and label to use, in the result table -- TODO: use correct sprite names, correct labels ["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"]        = { "item", "Oak Sapling" }, ["iron-horse-armor"]   = { "item", "Iron Horse Armor" }, ["gold-horse-armor"]   = { "item", "Gold Horse Armor" }, ["diamond-horse-armor"] = { "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" }, ["oak-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" }, ["record-13"]          = { "item", "Record 13" }, ["record-cat"]         = { "item", "Record Cat" }, ["nametag"]            = { "item", "Nametag" }, ["rail"]               = { "item", "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 -- TODO: what is the mineshaft min?	-- TODO: the other chests ["chests"] = { ["village-blacksmith"] = { ["_rounds"]            = {3,8}, ["_total-weight"]		= 0, ["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}, ["sapling"]            = {3,7,5}, ["iron-horse-armor"]   = {1,1,1}, ["gold-horse-armor"]   = {1,1,1}, ["diamond-horse-armor"] = {1,1,1}, ["saddle"]             = {1,1,3}, },		["stronghold-altar"]    = { ["_rounds"]            = {2,4}, ["_total-weight"]		= 0 },		["stronghold-library"]  = { ["_rounds"]            = {1,4}, ["_total-weight"]		= 0 },		["stronghold-crossing"] = { ["_rounds"]            = {1,4}, ["_total-weight"]		= 0 },		["bonus"]               = { ["_rounds"]            = {10,10}, ["_total-weight"]		= 0 },		["dungeon"]             = { ["_rounds"]            = {8,8}, ["_total-weight"]		= 0 },		["mineshaft"]           = { ["_rounds"]            = {3,6}, ["_total-weight"]		= 0 },		["nether-fortress"]     = { ["_rounds"]            = {2,5}, ["_total-weight"]		= 0 },		["desert-temple"]       = { ["_rounds"]            = {2,6}, ["_total-weight"]		= 0 },		["jungle-temple"]       = { ["_rounds"]            = {2,6}, ["_total-weight"]		= 0 }	} }

function p.base( f ) local args = f	if f == mw.getCurrentFrame then args = require( 'Module:ProcessArgs' ).merge( true ) end -- if argument is 'all', arguments are all chests

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

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

table.sort( args )

-- for each chest that will ultimately display, -- tally the item total weight for chestname, chest in pairs(p.chests) do		if args[chestname] == nil then break end local total_weight = 0 for itemname, item in pairs(chest) do			if not ( itemname == "_rounds" or itemname == "_total-weight" ) then total_weight = total_weight + item[3] end end chest["_total-weight"] = total_weight end

-- for each chest that will ultimately display, -- put 'avg number per chest' in position 4, for each chest item -- put 'avg number chests to find any of this item' in position 5, for each chest item for chestname, chest in pairs(p.chests) do		if args[chestname] == nil then break end local avg_rounds = ( chest["_rounds"][1] + chest["_rounds"][2] ) / 2 local total_weight = chest["_total-weight"] for itemname, item in pairs(chest) do			if not ( itemname == "_rounds" or itemname == "_total-weight" ) then local avg_stacksize = ( item[1] + item[2] ) / 2 local item_weight = item[3] p.chests[chestname][itemname][4] = avg_rounds * avg_stacksize * item_weight / total_weight p.chests[chestname][itemname][5] = 1 / ( avg_rounds * item_weight / total_weight ) end end end

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