Initial Commit

This commit is contained in:
Raymoo 2016-02-02 15:15:09 -08:00
commit 8bd2fa46ce
6 changed files with 347 additions and 0 deletions

124
default_loot.lua Normal file
View File

@ -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,
},
})

2
depends.txt Normal file
View File

@ -0,0 +1,2 @@
default
farming?

69
init.lua Normal file
View File

@ -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 = <ItemStack>,
-- 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")

84
loot_vault.lua Normal file
View File

@ -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)

BIN
loot_vault.mts Normal file

Binary file not shown.

68
weighted.lua Normal file
View File

@ -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