Some other item functions.
1. DivideStack
Say, you need to do modify item (change its graphic or something), but it happens to be stackable: an example could be a unlit torch you wish to lit without affecting all n torches.
You could do 'if (item.amount > 1) // Throw error message: too many torches in stack!', but that would be rude and unprofessional; so, instead, write 'DivideStack(item, -1)' and you will get the stack subtracted to just one item, without item's being really taken from player.
Code:
/*
DivideStack
Divides item stack into defined sized
stacks.
Params:
Item Item stack
Integer Size of split stack
Returns:
Created stack or 0 if failed.
Note:
If 'amt' is below zero, created
stack will be sized .amount - amt.
If 'amt' is between 1 and item.amount,
created stack will be it's size.
Created stack will be placed next to
the original stack.
*/
function DivideStack( item, amt )
if (amt < 0)
// User wanted to leave amt items to original stack.
amt += item.amount;
if (amt < 1)
// There are too few.
return 0;
endif
elseif (item.amount <= amt)
// Stack should have amt + 1.
return 0;
endif
// Copy into abstract location.
var newitem := CreateItemCopyAtLocation(1, 1, 1, item, _DEFAULT_REALM);
if (!SubtractAmount(newitem, item.amount - amt))
// Subtracting required amount from new stack - Failed.
DestroyItem(newitem);
return 0;
elseif (!SubtractAmount(item, amt))
// Subtracting required amount from original stack - Failed.
DestroyItem(newitem);
return 0;
endif
// Move new item into same location (container or coordinates) the original is.
MoveItem(newitem, item);
// Reference to created item.
return newitem;
endfunction
2. MoveItem
Move item to another location, were it to be a container or just struct with x, y, z and realm.
Code:
/*
MoveItem
Moves item into a backpack, container or coordinates. If
container is full, moves item into container's container,
or eventually, onto ground.
Params:
Item Item
Object Mobile / Container / Coordinates
Returns:
True or error
Note:
Coordinates could be just struct, but you have to
define, not only "x", "y" and "z", but also "realm".
*/
function MoveItem( item, object )
if (object.isa(POLCLASS_MOBILE))
if (object.backpack)
// Move into backpack; if there's no backpack, at feet.
object := object.backpack;
endif
endif
if (object.isa(POLCLASS_CONTAINER))
// Check if container can take this.
if (MoveItemToContainer(item, object))
return 1;
endif
endif
// Container is full or object is coordinate-struct. Next
// step is to try object's container; if there is one.
while (object.container)
// Object is now it's container.
object := object.container;
if (MoveItemToContainer(item, object))
return 1;
endif
endwhile
// Every level of container's were full; or there were none.
// Last try is to move item onto ground level.
return MoveObjectToLocation(item, object.x, object.y, object.z, object.realm, MOVEOBJECT_FORCELOCATION);
endfunction