| 1 |
#CFItemBroker.py |
| 2 |
#An often used bit of code to add or remove a number of objects |
| 3 |
#Useful for removing items (like in payment or as part of |
| 4 |
#an inventory check) This is also useful for setting the number |
| 5 |
#of a newly created item(s) as it will check for existing item(s) and add |
| 6 |
#the appropriate number of new items avoiding such silliness as the |
| 7 |
#number of the existing items being reset. |
| 8 |
#This will not check for the existence of an item as that would better |
| 9 |
#be done in the calling script so you can be flexible. |
| 10 |
# |
| 11 |
#ToddMitchell |
| 12 |
|
| 13 |
import Crossfire |
| 14 |
|
| 15 |
class Item: |
| 16 |
|
| 17 |
def __init__(self, object): |
| 18 |
self.object = object |
| 19 |
self.numberof = self.object.Quantity |
| 20 |
# 0 for non merging items |
| 21 |
if self.numberof == 0: |
| 22 |
self.numberof = 1 |
| 23 |
|
| 24 |
def add(self, number): |
| 25 |
tmp = (self.numberof + number)-1 |
| 26 |
self.object.Quantity=tmp |
| 27 |
return 1 |
| 28 |
|
| 29 |
def subtract(self, number): |
| 30 |
remainder = self.numberof - number |
| 31 |
if remainder >= number: |
| 32 |
self.object.Quantity=remainder |
| 33 |
return 1 |
| 34 |
elif remainder == 0: |
| 35 |
self.object.Remove() |
| 36 |
return 1 |
| 37 |
else: |
| 38 |
return 0 |
| 39 |
|
| 40 |
def quantity(self): |
| 41 |
return self.numberof |