mirror of
git://repo.or.cz/minetest_treasurer.git
synced 2024-12-04 04:43:50 +01:00
Initial 0.2 release candidate
New feature: Treasures can now be assigned groups and filtered by groups.
This commit is contained in:
commit
982d9f33f9
84
GROUPS_AND_PRECIOUSNESS
Normal file
84
GROUPS_AND_PRECIOUSNESS
Normal file
@ -0,0 +1,84 @@
|
||||
|
||||
If you wish to register treasures to Treasurer, it is recommended to assign the treasurer to a Treasurer group.
|
||||
Treasurer also requires you to assign each
|
||||
|
||||
It is not possible to assign a treasure to multiple groups. If you think a treasurer fits to two groups, create
|
||||
two seperate treasure definitions instead. But try to keep it as an exception.
|
||||
|
||||
Think of treasurer groups as drawers for treasurers to put into.
|
||||
|
||||
This file contains some guidelines for Treasurer groups and there are even guidelines for preciousness levels.
|
||||
Keep in mind these are only guidelines
|
||||
|
||||
Treasurer suggests to use the following standard Treasurer groups:
|
||||
|
||||
crafting_component:
|
||||
generic group for components in crafting recipes. If it is primarily a component for food, use raw_food instead.
|
||||
preciousness is based roughly on the preciousness of items it can create
|
||||
|
||||
fuel:
|
||||
fuel for furnaces
|
||||
preciousness is based on burning time
|
||||
|
||||
food:
|
||||
can be eaten and restores health
|
||||
preciousness should equal the number of hearts restored, but not higher than 7.
|
||||
|
||||
raw_food:
|
||||
food which is not fully processed and is not (quite) ready to be eaten.
|
||||
|
||||
melee_weapon:
|
||||
primarily used to damage close foes, i.e. a sword
|
||||
preciousness is based on attack speed and damage
|
||||
|
||||
ranged_weapon:
|
||||
primarily used to damage far away foes,
|
||||
preciousness is based on attack speed, damage and range
|
||||
|
||||
tool:
|
||||
a tool for other uses.
|
||||
preciousness is hard to determine; at least is should base on the number of uses.
|
||||
|
||||
|
||||
minetool:
|
||||
a tool to destroy blocks. Includes pickaxes, axes, shovels, …
|
||||
preciousness is based on power, number of uses and speed
|
||||
|
||||
deco:
|
||||
primarily just a decorational thing to place
|
||||
preciousness is based on beauty, highly subjective
|
||||
|
||||
light:
|
||||
is a light source
|
||||
preciousness is based on the brightness. For the maximum brightness, preciousness should be 3.
|
||||
|
||||
building_block:
|
||||
A block for buildings. Includes stairs, slabs, fences and similar things.
|
||||
Excludes all natural blocks.
|
||||
preciousness should be rougly based on the “cost” to craft the block.
|
||||
|
||||
seed:
|
||||
seeds and saplings
|
||||
preciousness is based on the “usefulness” of what can grow from the seed.
|
||||
|
||||
transport_vehicle
|
||||
a vehicle to transport players and stuff, i.e. a cart or a boat
|
||||
preciousness is hard to determine, maybe speed?
|
||||
|
||||
transport_structure
|
||||
a fixed structure which is neccessary for a transport vehicle to operate, i.e. rails
|
||||
preciousness is hard to dertermine …
|
||||
|
||||
ladder
|
||||
a ladder
|
||||
|
||||
default:
|
||||
This is the group your treasure get assigned to if you don’t specify a group.
|
||||
|
||||
|
||||
Do you miss a group? Feel free to invent your own!
|
||||
|
||||
|
||||
Note that Treasurer groups differ from the groups as defined by the Minetest API. These groups are handled differently.
|
||||
|
||||
|
201
README
Normal file
201
README
Normal file
@ -0,0 +1,201 @@
|
||||
= Treasurer’s README file for Treasurer version 0.1 =
|
||||
== Overview ==
|
||||
Name: Treasurer
|
||||
Technical name: treasurer
|
||||
Purpose: To provide an interface for mods which want to spawn ItemStacks randomly and
|
||||
an interface for mods which create new items.
|
||||
Version: 0.1
|
||||
Dependencies: none
|
||||
License: WTFPL
|
||||
|
||||
== Introduction ==
|
||||
Problem:
|
||||
There are a bunch of mods which have cool items but they won’t appear in the world by
|
||||
themselves.
|
||||
There are some mods which randomly distribute treasures into the world. Sadly, these only
|
||||
distribute the items they know—which are just the items of the mod “default” most of the
|
||||
time. The items of the other mods are completely missed.
|
||||
|
||||
The reason for this is that the distributing mods can’t know what sort of items are available
|
||||
unless they explicitly depend on the mods that defines these. Viewed the other way round,
|
||||
the item-defining mods that also distribute these items into the world are limited in the
|
||||
sense that they only know one means of distributing items.
|
||||
|
||||
There is a gap between defining items and distributing them. Every time a mod does both,
|
||||
flexibility is limited and expansion becomes difficult.
|
||||
|
||||
To bridge this gap, Treasurer has been written. Treasurer makes it possible a) for mods to define
|
||||
treasures without bothering _how_ these are distributed into the world and b) for mods to distribute
|
||||
treasures around the world without knowledge about _what_ treasures exactly are distributed.
|
||||
|
||||
== Technical side of Treasurer ==
|
||||
=== technical overview ===
|
||||
To get a working Treasurer architecture and actually get some treasures into the world,
|
||||
you need:
|
||||
* Treasurer
|
||||
* at least one treasure registration mod
|
||||
* at least one treasure spawning mod
|
||||
|
||||
=== treasurer registration mod ===
|
||||
Firstly, there are the treasure registration mods (TRMs). The task of TRMs is to tell
|
||||
Treasurer which items does it have to offer, which relative appearance probabilities these
|
||||
treasures should have, how “precious” the treasure is considered (on a scale from 0 to 10)
|
||||
, optionally how big the stack of items should be and optionally how much worn out it is.
|
||||
TRMs must depend on at least two mods: On treasurer and on the mod or mods
|
||||
where the items are defined. Technically, a TRM consists of nothing more than a long
|
||||
list of “registration” function calls. While this seems trivial, the task of balancing
|
||||
out probabilties and balancing out preciousness levels of treasures is not trivial
|
||||
and it may take a long time to get right.
|
||||
|
||||
It is strongly recommended that a TRM really does nothing
|
||||
more than registering treasures (and not defining items, for example). If you want
|
||||
to make your mod compatible to Treasurer, don’t change your mod, write a TRM for
|
||||
it instead.
|
||||
There is an example TRM, called “trm_default_example”. It registers some items
|
||||
of the default as treasures. Unsurprisingly, it depends on treasurer and default.
|
||||
|
||||
=== treasurer spawning mods ===
|
||||
Secondly, there are the treasure spawning mods (TSMs). The task of a TSM is to somehow
|
||||
distribute the available treasures into the world. This is also called “treasure
|
||||
spawning”. How exactly the TSM spawns the treasures is completely up the TSM. But a
|
||||
TSM has to request Treasurer to get some random treasures to distribute. A TSM may
|
||||
optionally request to filter out treasures outside certain preciousness levels, so
|
||||
it can be
|
||||
Treasurer can not guarantee to return the requestet amount of treasures, it may
|
||||
return an empty table, for two reasons:
|
||||
1) There is no TRM activated. There must be at least one to work
|
||||
2) The preciousness filter filtered out. This problem can be fixed by installing more
|
||||
TRMs or by balancing the existing TRMs to cover as many preciousness levels as
|
||||
possible. It also may be that the range specified by the TSM was too small. It is
|
||||
recommended to keep the requested range at least of a size of 1.
|
||||
Treasurer does, however, guarantee that the returned treasures are always in the
|
||||
requested boundsa.
|
||||
A TSM has to at least depend on Treasurer.
|
||||
Like for TRMs, it is strongly recommended that a TSM does nothing more than spawning
|
||||
treasures. This does not exclude the possibility that a TSM does not depend
|
||||
on any other mod.
|
||||
There are two example TSMs. The first one is a very basic one and called “tsm_gift_example”.
|
||||
It gives a “welcome gift” (1 random treasure) to players who just joined the server
|
||||
or who respawn. The preciousness filter is not used. It does only depend on Treasurer.
|
||||
The second one is called “tsm_chests_example” and pretty advanced for an example.
|
||||
It places chests of the mod “default” between 20 and 200 node lenghts below the water
|
||||
surface and puts 1-6 random treasures into these. The lower the chest, the higher
|
||||
the preciousness. It depends on treasurer and default (for the chests, of course).
|
||||
|
||||
=== Recap ===
|
||||
TRMs define treasures, TSMs spawn them. Treasurer manages the pool of available treasures.
|
||||
TRMs and TSMs do not have to know anything from each other.
|
||||
TRMs and TSMs do not neccessarily have to change any line of code of other mods to function.
|
||||
Treasurer depends on nothing.
|
||||
|
||||
Important: It should always only be neccessary for TRMs and TSMs to depend on Treasurer.
|
||||
All other mods do NOT HAVE TO and SHOULD NOT depend on Treasurer.
|
||||
|
||||
|
||||
|
||||
=== On rarity and preciousness ===
|
||||
==== rarity ====
|
||||
Rarity in Treasurer works in a pretty primitive way: The relative rarities of all
|
||||
treasures from the treasure pool are simply all added up. The probabilitiy of one
|
||||
certain treasure is then simply the rarity value divided by the sum.
|
||||
|
||||
==== preciousness ====
|
||||
How “precious” an item is, is highly subjective and also not always easy to categorize.
|
||||
Preciousness in Treasurer’s terms should be therefore viewed as “utility” or as
|
||||
“reward level” or “strength” or even “beauty” or whatever positive attributes you can
|
||||
think of for items.
|
||||
So, if you create a TRM and your treasure is something you want the player work
|
||||
hard for, assign it a high preciousness. Everyday items that are already easy to
|
||||
obtain in normal gameplay certainly deserve a lower precious than items that are
|
||||
expensive to craft.
|
||||
If your treasure consists of a powerful
|
||||
item, assign it a high preciousness. When in doubt, try to value gameplay over
|
||||
personal taste. Remember that TSMs can (and will!) filter out treasures based
|
||||
on their preciousness.
|
||||
For TSM authors, consider preciousness this way: If the trouble the player has
|
||||
to get through to in order to obtain the treasure is high, better filter
|
||||
out unprecious treasures. If your TSM distributes many treasures all over the world and these
|
||||
are easy to obtain, filter out precious treasures.
|
||||
|
||||
TSMs also can just completely ignore preciousness, then the given treasures base
|
||||
on sheer luck.
|
||||
|
||||
==== Recap ====
|
||||
Rarity determines the chance of a treasure, whereas preciousness determines
|
||||
the difficulty to obtain it.
|
||||
|
||||
== Overview of examples ==
|
||||
- trm_default_example - registers items of default mod
|
||||
- tsm_chests_example - spawns chests (from the default mod)
|
||||
- tsm_gift_example - gives one treasure as a “welcome gift” to joined or respawned players
|
||||
|
||||
== Treasurer API documentation ==
|
||||
=== API documentation for treasure registration mods ===
|
||||
The API consists of one function, which is called “treasurer.register_treasure”.
|
||||
|
||||
treasurer.register_treasure - registers a new treasure
|
||||
(this means the treasure will be ready to be spawned by treasure spawning mods.
|
||||
|
||||
parameters:
|
||||
name: name of resulting ItemStack, e.g. “mymod:item”
|
||||
rarity: rarity of treasure on a scale from 0 to 1 (inclusive). lower = rarer
|
||||
preciousness: subjective preciousness on a scale from 0 to 10 (inclusive)
|
||||
higher = more precious.
|
||||
count: optional value which specifies the multiplicity of the item. Default
|
||||
is 1. See count syntax help in this file.
|
||||
wear: optional value which specifies the wear of the item. Default is 0,
|
||||
which disables the wear. See wear syntax help in this file.
|
||||
note:
|
||||
This function does some basic parameter checking to catch the most obvious
|
||||
mistakes. If invalid parameters have been passed, the input is rejected and
|
||||
the function returns false. However, it does not cover every possible
|
||||
mistake, so some invalid treasures may slip through.
|
||||
note:
|
||||
Rarity does not imply preciousness. A rare treasure may not neccessarily a
|
||||
very precious one. A treasure chest with scorched stuff inside may be very
|
||||
rare, but it’s certainly also very unprecious.
|
||||
|
||||
returns:
|
||||
true on success,
|
||||
false on failure
|
||||
|
||||
=== data formats ===
|
||||
format of count type:
|
||||
A count can be a number or a table
|
||||
|
||||
count = number
|
||||
--> it’s always number times
|
||||
|
||||
count = {min, max}
|
||||
--> it’s pseudorandomly between min and max times, math.random() will be
|
||||
used to chose the value
|
||||
count = {min, max, prob_func}
|
||||
--> it’s between min and max times, and the value is given by
|
||||
prob_func (see below)
|
||||
|
||||
format of wear type:
|
||||
completely analogous to count
|
||||
|
||||
format of prob_func function:
|
||||
prob_func = function()
|
||||
parameters:
|
||||
(none)
|
||||
returns:
|
||||
--> a random or pseudorandom number between 0 (inclusive) and 1 (exclusive).
|
||||
notes:
|
||||
prob_func is entirely optional, if it’s not used, treasurer will
|
||||
default to math.random. You can use prob_func to define your own
|
||||
random function, in case you don’t wish your values to be evenly
|
||||
distributed.
|
||||
|
||||
=== API documentation for treasure spawning mods
|
||||
The API consists of one function, called “treasurer.select_random_treasures”.
|
||||
|
||||
treasurer.select_random_treasures - request some treasures from treasurer
|
||||
parameters:
|
||||
count: (optional) amount of treasures. If this value is nil, treasurer assumes a default of 1.
|
||||
minimal_preciousness: (optional) don’t consider treasures with a lower preciousness. If nil, there’s no lower bound.
|
||||
maximum_preciousness: (optional) don’t consider treasures with a higher preciousness. If nil, there’s no upper bound.
|
||||
returns:
|
||||
a table of ItemStacks (the requested treasures) - may be empty
|
||||
|
48
Treasurer_ANNOUNCEMENT
Normal file
48
Treasurer_ANNOUNCEMENT
Normal file
@ -0,0 +1,48 @@
|
||||
== Treasurer [treasurer] ==
|
||||
|
||||
With Treasurer I want to introduce a new way to define and distribute random treasures into the world. Treasurer itself does neither define any items nor does it attempt to distribute them.
|
||||
|
||||
Instead Treasurer provides two interfaces.
|
||||
|
||||
To actually get a game with treasures using Treasurer running, you need at least the following:
|
||||
[list]
|
||||
[*]Treasurer
|
||||
[*]at least one treasure registration mod
|
||||
[*]at least one treasure spawning mod
|
||||
[/list]
|
||||
|
||||
A treasure is basically just a fancy ItemStack. It consists of:
|
||||
- name of item
|
||||
- relative rarity
|
||||
- a preciousness
|
||||
- the amount of item (can be randomized)
|
||||
- a wear (can be randomized)
|
||||
|
||||
|
||||
A TRM does nothing more than telling Treasurer a bunch of treasures it has to offer. Note, however, that a TRM should really do nothing more than just that. The actual mods where the items originate from does most likely NOT have to be changed in order to register its items as treasures. TRMs are simple,
|
||||
|
||||
A TSM distributes (“spawns”) treasures, where it has to ask Treasurer for some treasures. Treasurer then chooses some treasures out of the pool of available treasures and returns a list of ItemStacks. The TSM also may ask Treasurer to filter out treasures which have an either too high or too low preciousness (“preciousness” is defined by the TRMs). Be warned: Treasurer may return no treasures at all, either because no TRM is activated or no treasure that matches the filter exists.
|
||||
“Spawning” here means to put the treasures into the world somehow. How a TSM does the task of spawning is up to the TSM. Treasurer
|
||||
|
||||
Recap: The TSMs will spawn treasures into the world out of a pool of treasures which is offered by TRMs. Treasurer manages the pool.
|
||||
|
||||
Examples for TRMs are:
|
||||
- registering bronze, gold, iron ingots and possibly other items of default (see trm_default_example)
|
||||
- registering bread, wheat, seeds, etc. of farming
|
||||
- registering super cool item X from awesome mod Y, where X could normally only obtained by “/give” or with creative mode.
|
||||
- registering the … bla bla bla, you get the idea
|
||||
|
||||
Examples for TSMs are:
|
||||
- let a mob drop a not mob-specific, random treasure on death
|
||||
- give a random treasure as a “welcome gift” to joining and respawning players (see tsm_gift_example)
|
||||
- placing chests around the world and spawning treasures into them (see tsm_chests_example)
|
||||
|
||||
Some of the above example have been implemented into already working mods, which are distributed with Treasurer
|
||||
as seperate mods.
|
||||
|
||||
|
||||
Dependencies of Treasurer: None!
|
||||
Minimal dependencies of all TRMs: treasurer and all mods that define the registered items
|
||||
Minimal dependency of all TSMs: treasurer
|
||||
|
||||
License: WTFPL
|
0
depends.txt
Normal file
0
depends.txt
Normal file
1
descripton.txt
Normal file
1
descripton.txt
Normal file
@ -0,0 +1 @@
|
||||
A framework which manages treasures which helps other mods to register and distribute treasures around the world.
|
363
init.lua
Normal file
363
init.lua
Normal file
@ -0,0 +1,363 @@
|
||||
--[==[
|
||||
Treasurer
|
||||
- A mod for Minetest
|
||||
version 0.2
|
||||
]==]
|
||||
|
||||
--[=[
|
||||
TABLE OF CONTENTS
|
||||
part 1: Initialization
|
||||
part 2: Treasure API
|
||||
part 3: Treasure spawning mod handling
|
||||
part 4: Internal functions
|
||||
]=]
|
||||
|
||||
--[=[
|
||||
part 1: Initialization
|
||||
]=]
|
||||
|
||||
-- This creates the main table; all functions of this mod are stored in this table
|
||||
treasurer = {}
|
||||
|
||||
-- Table which stores all the treasures
|
||||
treasurer.treasures = {}
|
||||
|
||||
-- This table stores the treasures again, but this time sorted by groups
|
||||
treasurer.groups = {}
|
||||
|
||||
-- Groups defined by the Treasurer API
|
||||
treasurer.groups.treasurer = {}
|
||||
treasurer.groups.treasurer.default = {}
|
||||
|
||||
-- Groups defined by the Minetest API
|
||||
treasurer.groups.minetest = {}
|
||||
|
||||
--[[
|
||||
format of treasure table:
|
||||
treasure = {
|
||||
name, -- treasure name, e.g. mymod:item
|
||||
rarity, -- relative rarity on a scale from 0 to 1 (inclusive).
|
||||
-- a rare treasure must not neccessarily be a precious treasure
|
||||
count, -- count (see below)
|
||||
preciousness, -- preciousness or “worth” of the treasure.
|
||||
-- ranges from 0 (“scorched stuff”) to 10 (“diamond block”)
|
||||
wear, -- wear (see below)
|
||||
metadata, -- unused at the moment
|
||||
|
||||
}
|
||||
treasures can be nodes or items
|
||||
|
||||
format of count type:
|
||||
count = number -- it’s always number times
|
||||
count = {min, max} -- it’s pseudorandomly between min and max times, math.random() will be used to chose the value
|
||||
count = {min, max, prob_func} -- it’s between min and max times, and the value is given by prob_func (which is not neccessarily random [in the strictly mathematical sense])
|
||||
|
||||
format of wear type:
|
||||
completely analogous to count type
|
||||
|
||||
format of prob_func function:
|
||||
prob_func = function()
|
||||
--> returns a random or pseudorandom number between 0 (inclusive) and 1 (exclusive)
|
||||
prob_func is entirely optional, if it’s not used, treasurer will default to math.random.
|
||||
You can use prob_func to define your own random function, in case you don’t like an even
|
||||
distribution
|
||||
|
||||
format of treasurer_groups:
|
||||
This is just a table of strings, each string stands for a group name.
|
||||
]]
|
||||
|
||||
|
||||
--[=[
|
||||
part 2: Treasurer API
|
||||
]=]
|
||||
|
||||
--[[
|
||||
treasurer.register_treasure - registers a new treasure
|
||||
(this means the treasure will be ready to be spawned by treasure spawning mods.
|
||||
|
||||
name: name of resulting ItemStack, e.g. “mymod:item”
|
||||
rarity: rarity of treasure on a scale from 0 to 1 (inclusive). lower = rarer
|
||||
preciousness: preciousness of treasure on a scale from 0 (“scorched stuff”) to 10 (“diamond block”).
|
||||
count: optional value which specifies the multiplicity of the item. Default is 1. See count syntax help in this file.
|
||||
wear: optional value which specifies the wear of the item. Default is 0, which disables the wear. See wear syntax help in this file.
|
||||
treasurer_groups: (optional) a table of group names to assign this treasure to. If omitted, the treasure is added to the default group.
|
||||
This function does some basic parameter checking to catch the most obvious mistakes. If invalid parameters have been passed, the input is rejected and the function returns false. However, it does not cover every possible mistake, so some invalid treasures may slip through.
|
||||
|
||||
returns: true on success, false on failure
|
||||
]]
|
||||
function treasurer.register_treasure(name, rarity, preciousness, count, wear, treasurer_groups )
|
||||
--[[ We don’t trust our input, so we first check if the parameters
|
||||
have the correct types and refuse to add the treasure if a
|
||||
parameter is malformed.
|
||||
What follows is a bunch of parameter checks.
|
||||
]]
|
||||
|
||||
-- check wheather name is a string
|
||||
if type(name) ~= "string" then
|
||||
minetest.log("error","[treasure] I rejected a treasure because the name was of type \""..type(name).."\" instead of \"string\".")
|
||||
return false
|
||||
end
|
||||
-- first check if rarity is even a number
|
||||
if type(rarity) == "number" then
|
||||
-- then check wheather the rarity lies in the allowed range
|
||||
if rarity < 0 or rarity > 1 then
|
||||
minetest.log("error", "[treasurer] I rejected the treasure \""..tostring(name).."\" because it’s rarity value is out of bounds. (it was "..tostring(rarity)..".)")
|
||||
return false
|
||||
end
|
||||
else
|
||||
minetest.log("error","[treasurer] I rejected the treasure \""..tostring(name).."\" because it had an illegal type of rarity. Given type was \""..type(rarity).."\".")
|
||||
return false
|
||||
end
|
||||
|
||||
-- check if preciousness is even a number
|
||||
if type(preciousness) == "number" then
|
||||
-- then check wheather the preciousness lies in the allowed range
|
||||
if preciousness < 0 or preciousness > 10 then
|
||||
minetest.log("error", "[treasurer] I rejected the treasure \""..tostring(name).."\" because it’s preciousness value is out of bounds. (it was "..tostring(preciousness)..".)")
|
||||
return false
|
||||
end
|
||||
else
|
||||
minetest.log("error","[treasurer] I rejected the treasure \""..tostring(name).."\" because it had an illegal type of preciousness. Given type was \""..type(preciousness).."\".")
|
||||
return false
|
||||
end
|
||||
|
||||
|
||||
-- first check if count is of a correct type
|
||||
if type(count) ~= "number" and type(count) ~= "nil" and type(count) ~= "table" then
|
||||
minetest.log("error", "[treasurer] I rejected the treasure \""..tostring(name).."\" because it had an illegal type of “count”. Given type was \""..type(count).."\".")
|
||||
return false
|
||||
end
|
||||
-- if count’s a table, check if it’s format is correct
|
||||
if type(count) == "table" then
|
||||
if(not (type(count[1]) == "number" and type(count[2]) == "number" and (type(count[3]) == "function" or type(count[3]) == "nil"))) then
|
||||
minetest.log("error","[treasurer] I rejected the treasure \""..tostring(name).."\" because it had a malformed table for the count parameter.")
|
||||
return false
|
||||
end
|
||||
end
|
||||
|
||||
-- now do the same for wear:
|
||||
-- first check if wear is of a correct type
|
||||
if type(wear) ~= "number" and type(wear) ~= "nil" and type(wear) ~= "table" then
|
||||
minetest.log("error","[treasurer] I rejected the treasure \""..tostring(name).."\" because it had an illegal type of “wear”. Given type was \""..type(wear).."\".")
|
||||
return false
|
||||
end
|
||||
-- if wear’s a table, check if it’s format is correct
|
||||
if type(wear) == "table" then
|
||||
if(not (type(wear[1]) == "number" and type(wear[2]) == "number" and (type(wear[3]) == "function" or type(wear[3]) == "nil"))) then
|
||||
minetest.log("error","[treasurer] I rejected the treasure \""..tostring(name).."\" because it had a malformed table for the wear parameter.")
|
||||
return false
|
||||
end
|
||||
end
|
||||
|
||||
-- check type of treasurer_group
|
||||
if type(treasurer_groups) ~= "table" and type(treasurer_groups) ~= "nil" and type(treasurer_groups) ~= "string" then
|
||||
minetest.log("error","[treasurer] I rejected the treasure \""..tostring(name).."\" because the treasure_group parameter is of type "..tosting(type(treasurer_groups)).." (expected: nil, string or table).")
|
||||
return false
|
||||
end
|
||||
|
||||
|
||||
|
||||
|
||||
--[[ End of checks. If we reached this point of the code, all checks have been passed
|
||||
and we finally register the treasure.]]
|
||||
|
||||
-- default count is 1
|
||||
if count == nil then count = 1 end
|
||||
-- default wear is 0
|
||||
if wear == nil then wear = 0 end
|
||||
local treasure = {
|
||||
name = name,
|
||||
rarity = rarity,
|
||||
count = count,
|
||||
wear = wear,
|
||||
preciousness = preciousness,
|
||||
metadata = "",
|
||||
}
|
||||
table.insert(treasurer.treasures, treasure)
|
||||
|
||||
--[[ Assign treasure to Treasurer group(s) or default if not provided ]]
|
||||
-- default Treasurer group is default
|
||||
if treasurer_groups == nil then treasurer_groups = "default" end
|
||||
|
||||
if(type(treasurer_groups) == "string") then
|
||||
if(treasurer.groups.treasurer[treasurer_groups] == nil) then
|
||||
treasurer.groups.treasurer[treasurer_groups] = {}
|
||||
end
|
||||
table.insert(treasurer.groups.treasurer[treasurer_groups], treasure)
|
||||
elseif(type(treasurer_groups) == "table") then
|
||||
for i=1,#treasurer_groups do
|
||||
-- assign to Treasurer group (create table if it does not exist yet)
|
||||
if(treasurer.groups.treasurer[treasurer_groups[i]] == nil) then
|
||||
treasurer.groups.treasurer[treasurer_groups[i]] = {}
|
||||
end
|
||||
table.insert(treasurer.groups.treasurer[treasurer_groups[i]], treasure)
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
minetest.log("info","[treasurer] Treasure successfully registered: "..name)
|
||||
return true
|
||||
end
|
||||
|
||||
|
||||
--[=[
|
||||
part 3: Treasure spawning mod (TSM) handling
|
||||
]=]
|
||||
|
||||
--[[
|
||||
treasurer.select_random_treasures - request some treasures from treasurer
|
||||
parameters:
|
||||
count: (optional) amount of items in the treasure. If this value is nil, treasurer assumes a default of 1.
|
||||
min_preciousness: (optional) don’t consider treasures with a lower preciousness. nil = no lower limit
|
||||
max_preciousness: (optional) don’t consider treasures with a higher preciousness. nil = no lower limit
|
||||
treasurer_group: (optional): Only consider treasures which are members of at least one of the members of the provided Treasurer group table. nil = consider all groups
|
||||
returns:
|
||||
a table of ItemStacks (the requested treasures) - may be empty
|
||||
on error, it returns false
|
||||
]]
|
||||
function treasurer.select_random_treasures(count, min_preciousness, max_preciousness, treasurer_groups)
|
||||
if #treasurer.treasures == 0 and count >= 1 then
|
||||
minetest.log("info","[treasurer] I was asked to return "..count.." treasure(s) but I can’t return any because no treasure was registered to me.")
|
||||
return {}
|
||||
end
|
||||
if count == nil then count = 1 end
|
||||
local sum = 0
|
||||
local cumulate = {}
|
||||
local randoms = {}
|
||||
|
||||
-- copy treasures into helper table
|
||||
local p_treasures = {}
|
||||
if(treasurer_groups == nil) then
|
||||
-- if the group filter is not used (defaul behaviour), copy all treasures
|
||||
for i=1,#treasurer.treasures do
|
||||
table.insert(p_treasures, treasurer.treasures[i])
|
||||
end
|
||||
|
||||
-- if the group filter IS used, copy only the treasures from the said groups
|
||||
elseif(type(treasurer_groups) == "string") then
|
||||
if(treasurer.groups.treasurer[treasurer_groups] ~= nil) then
|
||||
for i=1,#treasurer.groups.treasurer[treasurer_groups] do
|
||||
table.insert(p_treasures, treasurer.groups.treasurer[treasurer_groups][i])
|
||||
end
|
||||
else
|
||||
minetest.log("info","[treasurer] I was asked to return "..count.." treasure(s) but I can’t return any because no treasure which fits to the given Treasurer group “"..treasurer_groups.."”.")
|
||||
return {}
|
||||
end
|
||||
elseif(type(treasurer_groups) == "table") then
|
||||
for t=1,#treasurer_groups do
|
||||
if(treasurer.groups.treasurer[treasurer_groups[t]] ~= nil) then
|
||||
for i=1,#treasurer.groups.treasurer[treasurer_groups[t]] do
|
||||
table.insert(p_treasures, treasurer.groups.treasurer[treasurer_groups[t]][i])
|
||||
end
|
||||
end
|
||||
end
|
||||
else
|
||||
minetest.log("error","[treasurer] treasurer.select_random_treasures was called with a malformed treasurer_groups parameter!")
|
||||
return false
|
||||
end
|
||||
|
||||
if(min_preciousness ~= nil) then
|
||||
-- filter out too unprecious treasures
|
||||
for t=#p_treasures,1,-1 do
|
||||
if((p_treasures[t].preciousness) < min_preciousness) then
|
||||
table.remove(p_treasures,t)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
if(max_preciousness ~= nil) then
|
||||
-- filter out too precious treasures
|
||||
for t=#p_treasures,1,-1 do
|
||||
if(p_treasures[t].preciousness > max_preciousness) then
|
||||
table.remove(p_treasures,t)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
for t=1,#p_treasures do
|
||||
sum = sum + p_treasures[t].rarity
|
||||
cumulate[t] = sum
|
||||
end
|
||||
for c=1,count do
|
||||
randoms[c] = math.random() * sum
|
||||
end
|
||||
|
||||
local treasures = {}
|
||||
for c=1,count do
|
||||
for t=1,#p_treasures do
|
||||
if randoms[c] < cumulate[t] then
|
||||
table.insert(treasures, p_treasures[t])
|
||||
break
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
local itemstacks = {}
|
||||
for i=1,#treasures do
|
||||
itemstacks[i] = treasurer.treasure_to_itemstack(treasures[i])
|
||||
end
|
||||
if #itemstacks < count then
|
||||
minetest.log("info","[treasurer] I was asked to return "..count.." treasure(s) but I could only return "..(#itemstacks)..".")
|
||||
end
|
||||
return itemstacks
|
||||
end
|
||||
|
||||
--[=[
|
||||
Part 4: internal functions
|
||||
]=]
|
||||
|
||||
--[[ treasurer.treasure_to_itemstack - converts a treasure table to an
|
||||
ItemStack
|
||||
parameter:
|
||||
treasure: a treasure (see format in the head of this file)
|
||||
returns:
|
||||
an ItemStack
|
||||
]]
|
||||
function treasurer.treasure_to_itemstack(treasure)
|
||||
local itemstack = {}
|
||||
itemstack.name = treasure.name
|
||||
itemstack.count = treasurer.determine_count(treasure)
|
||||
itemstack.wear = treasurer.determine_wear(treasure)
|
||||
itemstack.metadata = treasure.metadata
|
||||
|
||||
return ItemStack(itemstack)
|
||||
end
|
||||
|
||||
--[[
|
||||
This determines the count of a treasure by taking the various different
|
||||
possible types of the count value into account
|
||||
This function assumes that the treasure table is valid.
|
||||
returns: the count
|
||||
]]
|
||||
function treasurer.determine_count(treasure)
|
||||
if(type(treasure.count)=="number") then
|
||||
return treasure.count
|
||||
else
|
||||
local min,max,prob = treasure.count[1], treasure.count[2], treasure.count[3]
|
||||
if(prob == nil) then
|
||||
return(math.floor(min + math.random() * (max-min)))
|
||||
else
|
||||
return(math.floor(min + prob() * (max-min)))
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
--[[
|
||||
This determines the wear of a treasure by taking the various different
|
||||
possible types of the wear value into account.
|
||||
This function assumes that the treasure table is valid.
|
||||
returns: the count
|
||||
]]
|
||||
function treasurer.determine_wear(treasure)
|
||||
if(type(treasure.wear)=="number") then
|
||||
return treasure.wear
|
||||
else
|
||||
local min,max,prob = treasure.wear[1], treasure.wear[2], treasure.wear[3]
|
||||
if(prob == nil) then
|
||||
return(math.floor(min + math.random() * (max-min)))
|
||||
else
|
||||
return(math.floor(min + prob() * (max-min)))
|
||||
end
|
||||
end
|
||||
end
|
||||
|
Loading…
Reference in New Issue
Block a user