Ability: measurable

Description:

This ability is used for objects that can be "measured", such as fuel, potions and money. A measurable object has a unit of measure and quantity (for example 5 copper coins).

A measurable object may be coalescent. This means that when such an object is moved, it checks whether there is another measurable object with the same unit in its environment. If there is, then the two objects merge to form a single object (a good example of this is money).

You can specify what happens when a measurable object becomes empty.

Usage:

#include <ability.h>

inherit MEASURABLE;

Functions:

void set_quantity(int q)
Set the quantity to q, which must be a non-negative integer (negative integers will be changed to 0).

int query_quantity()
Return the quantity. If you are trying to find out whther the object is empty, use emptyp() instead.

void add_quantity(int q)
Change the quantity by q. Here q can be negative, but the final quantity will not be allowed to be negative. If the object becomes empty, then a special action may be triggered.

void set_unit(string u)
Set the unit of measure to u.

string query_unit()
Return the unit of measure.

int emptyp()
Return 1 if the object is empty, and 0 otherwise.

void set_emptyf(function f)
Set the function that is called when the object becomes empty via set_quantity() or add_quantity(). If f is 0 then no function will be called.

void set_empty_destruct()
This is equivalent to set_emptyf((:destruct, this_object():)), i.e., the object will be destroyed with destruct().

function query_emptyf()
Return the function that is called when the object becomes empty via set_quantity() or add_quantity(). If no such function is defined, return 0.

void set_coalescent(int m)
If m is 0 then the object is set to not coalescent, otherwise it is set to coalescent.

int query_coalescent()
Return 1 if the object is coalescent, and 0 otherwise.

void merge(mixed ob)
Attempt to merge this object with object ob. The merge happens if both object are coalescent and have the same unit. Return value is ob if the merge happened, and 0 otherwise.

If ob is a list of objects then this object is merged with the first object in the list that is coalescent and has the same unit. Return value is the object that this object was merged with, and 0 otherwise.

Note that this object is emptied if the merge happens, which may trigger a special action.

Source code:

/ability/measurable.c.

See also:

Class: money