From 8bd2fa46ceace5db79b3aac0a941f3b2869d4242 Mon Sep 17 00:00:00 2001 From: Raymoo Date: Tue, 2 Feb 2016 15:15:09 -0800 Subject: [PATCH] Initial Commit --- default_loot.lua | 124 +++++++++++++++++++++++++++++++++++++++++++++++ depends.txt | 2 + init.lua | 69 ++++++++++++++++++++++++++ loot_vault.lua | 84 ++++++++++++++++++++++++++++++++ loot_vault.mts | Bin 0 -> 413 bytes weighted.lua | 68 ++++++++++++++++++++++++++ 6 files changed, 347 insertions(+) create mode 100644 default_loot.lua create mode 100644 depends.txt create mode 100644 init.lua create mode 100644 loot_vault.lua create mode 100644 loot_vault.mts create mode 100644 weighted.lua diff --git a/default_loot.lua b/default_loot.lua new file mode 100644 index 0000000..550700b --- /dev/null +++ b/default_loot.lua @@ -0,0 +1,124 @@ +loot.register_loot( + { weights = { generic = 50 }, + payload = { stack = ItemStack("default:acacia_sapling") }, +}) + +loot.register_loot( + { weights = { generic = 200 }, + payload = { stack = ItemStack("default:apple"), + min_size = 1, + max_size = 20, + }, +}) + +loot.register_loot( + { weights = { generic = 500 }, + payload = { stack = ItemStack("default:copper_ingot"), + min_size = 1, + max_size = 10, + }, +}) + +loot.register_loot( + { weights = { generic = 500 }, + payload = { stack = ItemStack("default:copper_lump"), + min_size = 1, + max_size = 10, + }, +}) + + +loot.register_loot( + { weights = { generic = 100, + valuable = 100, }, + payload = { stack = ItemStack("default:diamond"), + min_size = 1, + max_size = 5, + }, +}) + +loot.register_loot( + { weights = { generic = 200, + valuable = 200, }, + payload = { stack = ItemStack("default:gold_ingot"), + min_size = 1, + max_size = 10, + }, +}) + +loot.register_loot( + { weights = { generic = 200, + valuable = 200, }, + payload = { stack = ItemStack("default:gold_lump"), + min_size = 1, + max_size = 5, + }, +}) + +loot.register_loot( + { weights = { generic = 500 }, + payload = { stack = ItemStack("default:steel_ingot"), + min_size = 1, + max_size = 10, + }, +}) + +loot.register_loot( + { weights = { generic = 500 }, + payload = { stack = ItemStack("default:iron_lump"), + min_size = 1, + max_size = 10, + }, +}) + +loot.register_loot( + { weights = { generic = 200, + valuable = 200, }, + payload = { stack = ItemStack("default:mese_crystal"), + min_size = 1, + max_size = 20, + }, +}) + +loot.register_loot( + { weights = { generic = 20, + valuable = 200, }, + payload = { stack = ItemStack("default:mese"), + min_size = 1, + max_size = 5, + }, +}) + +loot.register_loot( + { weights = { generic = 100 }, + payload = { stack = ItemStack("default:obsidian"), + min_size = 1, + max_size = 30, + }, +}) + +if not minetest.get_modpath("farming") then return end + +loot.register_loot( + { weights = { generic = 200 }, + payload = { stack = ItemStack("farming:bread"), + min_size = 1, + max_size = 50, + }, +}) + +loot.register_loot( + { weights = { generic = 200 }, + payload = { stack = ItemStack("farming:seed_cotton"), + min_size = 1, + max_size = 50, + }, +}) + +loot.register_loot( + { weights = { generic = 200 }, + payload = { stack = ItemStack("farming:seed_wheat"), + min_size = 1, + max_size = 50, + }, +}) diff --git a/depends.txt b/depends.txt new file mode 100644 index 0000000..88efa0a --- /dev/null +++ b/depends.txt @@ -0,0 +1,2 @@ +default +farming? diff --git a/init.lua b/init.lua new file mode 100644 index 0000000..87599ed --- /dev/null +++ b/init.lua @@ -0,0 +1,69 @@ +local modpath = minetest.get_modpath(minetest.get_current_modname()) .. "/" + +local newWR = dofile(modpath .. "weighted.lua") + +local categories = {} + + +local function get_or_make_cat(category) + + if categories[category] ~= nil then + return categories[category] + else + local newCat = newWR() + categories[category] = newCat + return newCat + end +end + + +loot = {} + +-- Format: +-- +-- { weights = { generic = 100 }, -- Likeliness per category +-- -- Diamond is 100 +-- payload = { stack = , +-- min_size = 1, -- optional +-- max_size = 10, -- optional +-- }, +-- } + + +loot.register_loot = function(def) + for c_name, weight in pairs(def.weights) do + local cat = get_or_make_cat(c_name) + cat:add(def.payload, weight) + end +end + + +local function payload_to_stack(pl) + local min, max = pl.min_size, pl.max_size + local stack = ItemStack(pl.stack) + + if min and max then + stack:set_count(math.random(min, max)) + end + + return stack +end + + +loot.generate_loot = function(category, count) + local cat = get_or_make_cat(category) + local pls = cat:get(count) + + for i, pl in ipairs(pls) do + pls[i] = payload_to_stack(pl) + end + + return pls +end + + +loot.modpath = modpath + + +dofile(modpath .. "loot_vault.lua") +dofile(modpath .. "default_loot.lua") diff --git a/loot_vault.lua b/loot_vault.lua new file mode 100644 index 0000000..c6fe16c --- /dev/null +++ b/loot_vault.lua @@ -0,0 +1,84 @@ +-- Creates loot vaults + + +-- Node that turns into a chest containing some loot +minetest.register_node("loot:loot_node", + { drawtype = "airlike", + pointable = false, + walkable = false, + diggable = false, +}) + + +minetest.register_abm({ + nodenames = {"loot:loot_node"}, + interval = 1, + chance = 1, + action = function(pos) + minetest.remove_node(pos) + minetest.place_node(pos, {name = "default:chest"}) + local inv = minetest.get_meta(pos):get_inventory() + + local valuable_count = math.random(1,3) + local generic_count = math.random(0,10) + + local valuables = loot.generate_loot("valuable", valuable_count) + local loots = loot.generate_loot("generic", generic_count) + + for i, v in ipairs(valuables) do + table.insert(loots,v) + end + + inv:set_list("main", loots) + end, +}) + + +local mts = loot.modpath .. "loot_vault.mts" + +minetest.register_on_generated(function(minp, maxp, seed) + if maxp.y < -10 then return end + local do_gen = seed % 131 == 0 + + if not do_gen then return end + + local face = tostring((seed % 4) * 90) + + local candidates = + minetest.find_nodes_in_area_under_air(minp, maxp, "group:soil") + + local choice = candidates[1] + + if not choice then return end + + local base = vector.add(choice, {x=-7, y=-13, z=-7}) + + minetest.log("action", + "Generating loot vault at " .. minetest.pos_to_string(base)) + + minetest.place_schematic(base, + mts, + face, + { ["default:chest"] = "loot:loot_node" }, + true) + +end) + +minetest.set_gen_notify("dungeon") + +minetest.register_on_generated(function(minp, maxp, blockseed) + + local notif = minetest.get_mapgen_object("gennotify") + + if not notif then return end + + local locations = notif.dungeon + + if not locations then return end + + for i, location in ipairs(locations) do + if math.random(3) == 1 then + minetest.place_node(location, { name = "loot:loot_node" }) + end + end +end) diff --git a/loot_vault.mts b/loot_vault.mts new file mode 100644 index 0000000000000000000000000000000000000000..c2119283278e52ed7623d313e4c4d701e01db485 GIT binary patch literal 413 zcmeYb3HD`RVc-YBdQ`x`&LEVMnwD6aQ({$IlAo8FRFs*V&A^!IB~G?1#+U$4ATjWRD!XYGHKtYRKuB zm1&k~$u(PRr(QpN)ibboLvcaa{wF?eub!MT_5QtRr&$)?xAG0@p?xiBW4z||f(er*YahQ* zXmMkk7Kq{q4JzI-uX$74?p2fctgbpPw|a49R`yEWijYeOj|eo^5jwg&6~IZ literal 0 HcmV?d00001 diff --git a/weighted.lua b/weighted.lua new file mode 100644 index 0000000..693cbe7 --- /dev/null +++ b/weighted.lua @@ -0,0 +1,68 @@ +-- Weighted randomness. + +local function add(self, value, weight) + self.size = self.size + weight + table.insert(self, { value = value, + weight = weight, + }) +end + + +local function get(self, count) + + if count == nil then count = 1 end + + local max = self.size + + if max == 0 then return {} end + + local nums = {} + local random = math.random + + for i = 1, count do + table.insert(nums, random(max)) + end + + table.sort(nums) + + local result = {} + + local numI = 1 + local elemI = 1 + + local curNum = nums[numI] + local curElem = self[elemI] + + local pos = curElem.weight + + while curNum ~= nil and curElem ~= nil do + + if curNum <= pos then + table.insert(result, curElem.value) + numI = numI + 1 + curNum = nums[numI] + else + elemI = elemI + 1 + curElem = self[elemI] + + pos = pos + curElem.weight + end + end + + return result +end + + +local methods = { add = add, + get = get, +} + + +local function newWRandom() + local wr = { size = 0 } + setmetatable(wr, { __index = methods }) + return wr +end + + +return newWRandom