Added destroying and retaining of items.

master
Robert Zenz 2016-01-09 13:15:21 +01:00
parent bb0efd496c
commit 2744b54d8f
3 changed files with 145 additions and 5 deletions

9
README
View File

@ -9,6 +9,7 @@ Features
* Items "explode" away from the player, spreading them over a fair distance in
all directions.
* Certain percentage of items can be retained or be destroyed.
* Completely configurable from the configuration.
@ -29,6 +30,14 @@ The system can be configured by adding settings to the `minetest.conf`:
# The name of the lists to drop, a comma separated list, defaults to "main".
deathinventorydrop_inventories = main
# The percentage of how many items are destroyed upon death, a random amount
# is picked based on the given range, defaults to "0.15, 0.35".
deathinventorydrop_percentage_destroyed_items = 0.15, 0.35
# The percentage of how many items are retained, meaning the player is
# allowed to keep after death, defaults to "0, 0.15".
deathinventorydrop_percentage_retained_items = 0, 0.15
# If the stacks that are split in some way, defaults to random.
# Possible values are:
# random: The dropped stacks are split randomly.

View File

@ -68,6 +68,10 @@
<td class="summary">Drops the given list from the given player, removing all items from
the list.</td>
</tr>
<tr>
<td class="name" nowrap><a href="#take_from_inventory">take_from_inventory (items, total_item_count, range)</a></td>
<td class="summary">Takes random amount from the given list and returns the taken items.</td>
</tr>
</table>
<h2><a href="#Fields">Fields</a></h2>
<table class="function_list">
@ -83,6 +87,14 @@
<td class="name" nowrap><a href="#inventories">inventories</a></td>
<td class="summary">The list of inventories/list names to drop, they need to be in players
inventory, defaults to "main".</td>
</tr>
<tr>
<td class="name" nowrap><a href="#percentage_destroyed_items">percentage_destroyed_items</a></td>
<td class="summary">The percentage of destroyed items.</td>
</tr>
<tr>
<td class="name" nowrap><a href="#percentage_retained_items">percentage_retained_items</a></td>
<td class="summary">The percentage of retained items.</td>
</tr>
<tr>
<td class="name" nowrap><a href="#split">split</a></td>
@ -177,6 +189,37 @@
</dd>
<dt>
<a name = "take_from_inventory"></a>
<strong>take_from_inventory (items, total_item_count, range)</strong>
</dt>
<dd>
Takes random amount from the given list and returns the taken items.
<h3>Parameters:</h3>
<ul>
<li><span class="parameter">items</span>
The list of items.
</li>
<li><span class="parameter">total_item_count</span>
The total count of items in the list.
</li>
<li><span class="parameter">range</span>
The range, having min and max values.
</li>
</ul>
<h3>Returns:</h3>
<ol>
The list of taken items.
</ol>
</dd>
</dl>
<h2><a name="Fields"></a>Fields</h2>
@ -223,6 +266,34 @@
</dd>
<dt>
<a name = "percentage_destroyed_items"></a>
<strong>percentage_destroyed_items</strong>
</dt>
<dd>
The percentage of destroyed items.
</dd>
<dt>
<a name = "percentage_retained_items"></a>
<strong>percentage_retained_items</strong>
</dt>
<dd>
The percentage of retained items.
</dd>
<dt>
<a name = "split"></a>

View File

@ -38,6 +38,12 @@ deathinventorydrop = {
-- inventory, defaults to "main".
inventories = settings.get_list("deathinventorydrop_inventories", "main"),
--- The percentage of destroyed items.
percentage_destroyed_items = settings.get_table("deathinventorydrop_percentage_destroyed_items", { min = 0.15, max = 0.35 }, "min", "max"),
--- The percentage of retained items.
percentage_retained_items = settings.get_table("deathinventorydrop_percentage_retained_items", { min = 0, max = 0.15 }, "min", "max"),
--- The split mode to use, can either be "random", "single" or "stack",
-- defaults to "random".
split = settings.get_string("deathinventorydrop_split", "random"),
@ -83,17 +89,71 @@ end
-- @param inventory The InvRef object.
-- @param list_name The name of the list of which to drop.
function deathinventorydrop.drop_inventory(player, inventory, list_name)
local items = inventory:get_list(list_name)
local total_item_count = 0
for index, value in ipairs(items) do
total_item_count = total_item_count + value:get_count()
end
-- Get the retained items.
local retained_items = deathinventorydrop.take_from_inventory(
items,
total_item_count,
deathinventorydrop.percentage_retained_items)
-- Destroy some items.
deathinventorydrop.take_from_inventory(
items,
total_item_count,
deathinventorydrop.percentage_destroyed_items)
itemutil.blop(
player,
inventory:get_list(list_name),
items,
deathinventorydrop.velocity.x,
deathinventorydrop.velocity.y,
deathinventorydrop.velocity.z,
deathinventorydrop.split)
-- Now clear the whole list.
for index = 1, inventory:get_size(list_name), 1 do
inventory:set_stack(list_name, index, nil)
end
inventory:set_list(list_name, retained_items)
end
--- Takes random amount from the given list and returns the taken items.
--
-- @param items The list of items.
-- @param total_item_count The total count of items in the list.
-- @param range The range, having min and max values.
-- @return The list of taken items.
function deathinventorydrop.take_from_inventory(items, total_item_count, range)
local taken_items = {}
if range.max > 0 then
local min_item_count = mathutil.round(total_item_count * range.min)
local max_item_count = mathutil.round(total_item_count * range.max)
local item_count = random.next_int(min_item_count, max_item_count)
local items_per_stack = mathutil.round(total_item_count / item_count)
while item_count > 0 do
for index, value in ipairs(items) do
if item_count > 0 and value:get_count() > 0 then
local max_count = math.min(value:get_count(), math.min(items_per_stack, item_count))
local count = random.next_int(0, max_count)
local taken_item = value:take_item(count)
if taken_items[index] == nil then
taken_items[index] = taken_item
else
taken_items[index]:add_item(taken_item)
end
item_count = item_count - count
end
end
end
end
return taken_items
end