wyvern.lib.classes
Class Shop

java.lang.Object
  extended bywyvern.kernel.properties.PList
      extended bywyvern.kernel.maps.MapObject
          extended bywyvern.lib.classes.Shop
All Implemented Interfaces:
Broadcaster, Command, GameObject, HookCallback, MethodHookable, MethodHookCallback, PropertyList, RoomHookCallback

public class Shop
extends MapObject
implements RoomHookCallback, Command, HookCallback, MethodHookCallback

The basic Shop object. It handles buying, selling, listing, and other shop commands. It also prevents players from doing various things with unpaid items.

Version:
1.0, Oct 14, 1998
Author:
Steve Yegge

Field Summary
protected static Predicate checkUnpaid_
           
protected static int ITEM_REDUCTION
           
protected  java.util.HashSet items_
           
protected static int MAX_ITEMS
           
protected  java.util.LinkedList sellList_
           
protected  java.lang.String shopList_
           
protected  java.util.HashSet targetedHooks_
          This is a list of hook names, such as "applyPreHook", for commands that we don't allow on unpaid objects.
 
Fields inherited from class wyvern.kernel.maps.MapObject
IN_MAP_EDITOR, listenerList_
 
Fields inherited from class wyvern.kernel.properties.PList
readOnly_
 
Fields inherited from interface wyvern.lib.PropertyList
PROPERTY_PACKAGE
 
Constructor Summary
Shop()
           
 
Method Summary
 CommandEvent createEvent(CommandEvent initialInfo)
          Creates a CommandEvent that encapsulates all the state necessary to perform the command.
 boolean execute(CommandEvent event)
          Executes the specified CommandEvent.
static java.lang.String formatCurrency(int silver_value)
          Formats a currency as platinum, gold and silver.
 Rectangle getBounds()
          Returns the area covered by the shop.
 int[] getImageDescriptors()
          Returns the list of (x, y, tile) image triples for this object.
 int getItemPrice(GameObject item)
          Returns the price (in gold) the shop would pay for the item.
static int getSellPriceForItem(GameObject target, Monster agent)
          Tells them how much a player will get for selling the thing (which may be a group of things) Helper function used by sell/value.
 java.lang.String getShopList()
          Returns a string description of the shop's contents.
 boolean handleBuy(CommandEvent event, Monster agent)
          Agent wants to buy something.
 boolean handleList(CommandEvent event, Monster agent)
          Handles listing the contents of the shop.
 boolean handleRefund(CommandEvent event, Monster mon)
          Refunds the customer's money for the item.
 boolean handleSell(CommandEvent event, Monster agent)
          Agent wants to sell something.
 boolean handleUnsell(CommandEvent event, Monster mon)
          Allows a player to reverse a sale; i.e. get their item back and lose the money they got for it.
 boolean handleValue(CommandEvent event, Monster agent)
          Tells them how much the thing will cost (if unpaid), or how much they'd get for selling an unpaid item.
 void hookEvent(java.lang.String hookName, CommandEvent event)
          Movement hook handler - don't let them leave without paying.
 void hookEvent(java.lang.String hookName, Rectangle room, CommandEvent event)
          Player is trying to do something that we might want to veto.
 void initialize()
          Adds class-default properties.
 boolean knowsCommand(java.lang.String command)
          Returns true if this Command wants to handle the command.
 void methodCalled(java.lang.String hookName, MethodHookable target, java.lang.Object data)
          Map is finished loading - construct list of shop contents.
protected  void nukeExtraInventory()
          If we pass the threshold for too many sold items, we nuke the ones that have been there the longest.
 void registerCommands(GameMap map, Rectangle bounds)
          Registers the commands we want to hook.
protected  void resetShopList()
          Recomputes the cached string containing the shop contents.
 void scanObjects()
          Looks for objects to add to the shop's item-list.
 boolean sellAll(CommandEvent event)
          Agent wants to sell all non-kept stuff in inventory.
 void setMap(GameMap map, int x, int y)
          Puts the shop in the map.
 void setSize(int width, int height)
          Throws out the cached image descriptors when the size changes.
 
Methods inherited from class wyvern.kernel.maps.MapObject
addContainerChangeListener, addDefaultProperty, addMapChangeListener, addMapMotionListener, addMethodHook, addMotionTracker, addObjectTracker, addPrefix, addSuffix, addToMap, bless, broadcast, broadcast, broadcast, broadcast, broadcast, canEnter, canEnterBlockedBy, canMove, canMove, canMoveBlockedBy, canMoveBlockedBy, canMoveTo, canMoveToBlockedBy, checkDrop, curse, cycleFrame, destroy, ensureListenerList, ensureWeight, externalize, fireContainerEntered, fireContainerExited, fireEnteredMap, fireExitedMap, fireMovedInMap, getAlpha, getAppearance, getArchetype, getBaseName, getBaseWeight, getBitmap, getCanonicalClassName, getCategory, getContainer, getCreator, getDamagedDescription, getDescription, getDirection, getGenderPossessive, getGenderPronoun, getImage, getImageDescriptor, getLayer, getLocations, getMap, getMapLink, getMaterial, getMaterialDescription, getMover, getOwningPlayer, getParentMap, getPrefixes, getQuantity, getReferenceLoc, getRelativeLocs, getShortDesc, getSuffixes, getTile, getTile, getValue, getWeight, groupString, inContainer, initLocs, inMapEditor, invalidate, invalidateImage, invalidateParent, invalidateText, isAnimated, isAttackable, isBlessed, isCommandable, isCursed, isDamned, isGroupable, isIdentified, isMonster, isMonsterOrPlayer, isPlayer, isTerrain, isUncursed, isUnpaid, isWizard, makeClone, markPaid, markUnpaid, move, occupies, positionAt, positionAtMap, remove, removeContainerChangeListener, removeFromMap, removeMapChangeListener, removeMapMotionListener, removeMethodHook, removeMotionTracker, removeObjectTracker, removePrefix, removeSuffix, runMethodHook, setAlpha, setAnimated, setBitmap, setCategory, setContainer, setDefaultBitmap, setDefaultCategory, setDefaultDesc, setDefaultImage, setDefaultIntProperty, setDefaultLayer, setDefaultMaterial, setDefaultProperty, setDefaultShortDesc, setDirection, setImage, setImage, setLayer, setLocations, setMapLink, setQuantity, setShape, setTransientSize, setWeight, setWeight, teleport, toString, updateMapLocation
 
Methods inherited from class wyvern.kernel.properties.PList
addProperty, addTransientProperty, adjustDoubleProperty, adjustIntProperty, adjustLongProperty, adjustTransientDoubleProperty, adjustTransientIntProperty, adjustTransientLongProperty, countLocalProperties, debugProperties, dismantlePropertyList, getDoubleProperty, getInheritedProperty, getInteger, getIntProperty, getLocalProperties, getLocalProperty, getLongProperty, getParent, getPersistentDoubleProperty, getPersistentIntProperty, getPersistentLocalProperties, getPersistentLongProperty, getPersistentProperty, getProfilingInfo, getProperties, getProperties, getPropertiesIncludingTransients, getPropertiesIncludingTransients, getProperty, getSerializableProperties, getSerializableProperty, getStringProperty, getTransientDoubleProperty, getTransientIntProperty, getTransientLongProperty, getTransientProperties, getTransientProperty, hasLocalProperty, hasPersistentProperty, hasProperty, hasTransientProperty, inheritProperty, isReadOnly, isRemoved, isTransientlyRemoved, printLocalProperties, printProperties, printProperties, printTransientProperties, removeProperty, removeTransientProperty, setDoubleProperty, setIntProperty, setLongProperty, setParent, setProperty, setReadOnly, setTransientDoubleProperty, setTransientIntProperty, setTransientLongProperty, setTransientProperty, toString, transientlyRemoveProperty
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface wyvern.lib.PropertyList
addProperty, addTransientProperty, adjustDoubleProperty, adjustIntProperty, adjustLongProperty, adjustTransientDoubleProperty, adjustTransientIntProperty, adjustTransientLongProperty, countLocalProperties, getDoubleProperty, getInheritedProperty, getIntProperty, getLocalProperties, getLocalProperty, getLongProperty, getParent, getPersistentDoubleProperty, getPersistentIntProperty, getPersistentLocalProperties, getPersistentLongProperty, getPersistentProperty, getProperties, getProperties, getPropertiesIncludingTransients, getPropertiesIncludingTransients, getProperty, getSerializableProperties, getSerializableProperty, getStringProperty, getTransientDoubleProperty, getTransientIntProperty, getTransientLongProperty, getTransientProperties, getTransientProperty, hasLocalProperty, hasPersistentProperty, hasProperty, hasTransientProperty, inheritProperty, isReadOnly, isRemoved, isTransientlyRemoved, printLocalProperties, printProperties, printProperties, printTransientProperties, removeProperty, removeTransientProperty, setDoubleProperty, setIntProperty, setLongProperty, setParent, setProperty, setReadOnly, setTransientDoubleProperty, setTransientIntProperty, setTransientLongProperty, setTransientProperty, toString, transientlyRemoveProperty
 

Field Detail

MAX_ITEMS

protected static final int MAX_ITEMS
See Also:
Constant Field Values

ITEM_REDUCTION

protected static final int ITEM_REDUCTION
See Also:
Constant Field Values

targetedHooks_

protected transient java.util.HashSet targetedHooks_
This is a list of hook names, such as "applyPreHook", for commands that we don't allow on unpaid objects.


checkUnpaid_

protected static Predicate checkUnpaid_

items_

protected transient java.util.HashSet items_

sellList_

protected transient java.util.LinkedList sellList_

shopList_

protected java.lang.String shopList_
Constructor Detail

Shop

public Shop()
Method Detail

initialize

public void initialize()
Adds class-default properties.

Specified by:
initialize in interface GameObject
Overrides:
initialize in class MapObject

setMap

public void setMap(GameMap map,
                   int x,
                   int y)
Puts the shop in the map. Expands to fill the bounds specified in the "width" and "height" properties, or failing that, the entire bounds of the map.

Specified by:
setMap in interface GameObject
Overrides:
setMap in class MapObject
Parameters:
map - the map to put us in
x - the x-coordinate of our upper-left corner
y - the y-coordinate of our upper-left corner

getBounds

public Rectangle getBounds()
Returns the area covered by the shop.

Specified by:
getBounds in interface GameObject
Overrides:
getBounds in class MapObject

registerCommands

public void registerCommands(GameMap map,
                             Rectangle bounds)
Registers the commands we want to hook.

Parameters:
map - the map we're being added to
bounds - our bounds

getImageDescriptors

public int[] getImageDescriptors()
Returns the list of (x, y, tile) image triples for this object.

Specified by:
getImageDescriptors in interface GameObject
Overrides:
getImageDescriptors in class MapObject
Returns:
a list that has 3 ints for every location we occupy, so our little "shop" icon shows up in every location we're in. This is somewhat unsightly, but can be toggled off, and is very useful for debugging, and for help newbies understand that a single shop occupies multiple locations in their map.

setSize

public void setSize(int width,
                    int height)
Throws out the cached image descriptors when the size changes. Pretty much ONLY used in the map editor.

Specified by:
setSize in interface GameObject
Overrides:
setSize in class MapObject

hookEvent

public void hookEvent(java.lang.String hookName,
                      Rectangle room,
                      CommandEvent event)
Player is trying to do something that we might want to veto.

Specified by:
hookEvent in interface RoomHookCallback
Parameters:
hookName - the name of the hook (e.g. "applyPreHook")
room - the room in which the event took place, in case the callback is registered for the same hook in more than one room.
event - the command event

hookEvent

public void hookEvent(java.lang.String hookName,
                      CommandEvent event)
Movement hook handler - don't let them leave without paying.

Specified by:
hookEvent in interface HookCallback
Parameters:
hookName - hook name
event - the MoveEvent

knowsCommand

public boolean knowsCommand(java.lang.String command)
Returns true if this Command wants to handle the command. The Command can examine the arguments and determine that it doesn't actually understand the command, in which case it should return false.

This method exists to allow more than one game object to implement the same command, but with different expected arguments.

If two objects register for the same command and the same arguments, the object that was registered most recently gets first shot at the command.

Specified by:
knowsCommand in interface Command
Parameters:
command - the entire command string, including arguments
Returns:
true if we want to handle the command

createEvent

public CommandEvent createEvent(CommandEvent initialInfo)
Creates a CommandEvent that encapsulates all the state necessary to perform the command. A well-designed CommandEvent exposes all of its "properties" so that hook objects can modify its behavior. For instance, many commands result in some sort of message being sent back to the client. The message should be among the properties that hooks can override.

This method is called by the kernel. The kernel passes the event to the pre-hook, then calls Command.execute() (below) to execute the event, and finally calls the post-hook.

Specified by:
createEvent in interface Command
Parameters:
initialInfo - a "blank" CommandEvent containing only the command text and the agent who's performing the command. This initial event is created by the originator of the event (e.g. the AI or player's command preprocessor).
Returns:
a CommandEvent subclass encapsulating this command's execution parameters and state. It should copy in the fields from the passed-in event.

execute

public boolean execute(CommandEvent event)
Executes the specified CommandEvent. The CommandEvent contains all the parameters and state required to execute the command; the parameters have been filtered through hook objects, so they may not be the same as when the event was created.

Specified by:
execute in interface Command
Parameters:
event - the CommandEvent to execute
Returns:
true if the event completed successfully, else false.

handleBuy

public boolean handleBuy(CommandEvent event,
                         Monster agent)
Agent wants to buy something.

Parameters:
event - the event containing the text of the thing to buy
agent - the agent to do the purchase
Returns:
true if the agent bought the thing successfully

handleSell

public boolean handleSell(CommandEvent event,
                          Monster agent)
Agent wants to sell something.

Parameters:
event - the event containing the text of the thing to sell
agent - the agent to do the sale
Returns:
true if the agent sold the thing successfully

sellAll

public boolean sellAll(CommandEvent event)
Agent wants to sell all non-kept stuff in inventory.


handleList

public boolean handleList(CommandEvent event,
                          Monster agent)
Handles listing the contents of the shop.

Parameters:
event - the event with the command & args
agent - the agent to send the list to
Returns:
true if we couldn't do it for some reason

methodCalled

public void methodCalled(java.lang.String hookName,
                         MethodHookable target,
                         java.lang.Object data)
Map is finished loading - construct list of shop contents.

Specified by:
methodCalled in interface MethodHookCallback
Parameters:
hookName - the name of the hook being run
target - the object on which the method was invoked: a GameMap or GameObject.
data - a method-specific data object; can be null. Check the documentation for the method hook to see if any data is passed to the callbacks.

scanObjects

public void scanObjects()
Looks for objects to add to the shop's item-list. Called when the map is done loading. If you add a shop to a map in code, you have to call this to get "list" to work in the shop.


resetShopList

protected void resetShopList()
Recomputes the cached string containing the shop contents. Iterates through the cache of unpaid items and makes a new list, which you can get by calling getShopList().


getShopList

public java.lang.String getShopList()
Returns a string description of the shop's contents.


nukeExtraInventory

protected void nukeExtraInventory()
If we pass the threshold for too many sold items, we nuke the ones that have been there the longest.


handleUnsell

public boolean handleUnsell(CommandEvent event,
                            Monster mon)
Allows a player to reverse a sale; i.e. get their item back and lose the money they got for it.

Parameters:
event - the event
mon - the player unselling the item

handleRefund

public boolean handleRefund(CommandEvent event,
                            Monster mon)
Refunds the customer's money for the item.


handleValue

public boolean handleValue(CommandEvent event,
                           Monster agent)
Tells them how much the thing will cost (if unpaid), or how much they'd get for selling an unpaid item.


getSellPriceForItem

public static int getSellPriceForItem(GameObject target,
                                      Monster agent)
Tells them how much a player will get for selling the thing (which may be a group of things) Helper function used by sell/value. We do quantity calculation in silver so you sell a lot of worthless things and get a gold coin.

Parameters:
target - the object to sell
agent - the player doing the selling
Returns:
the value in gold pieces

getItemPrice

public int getItemPrice(GameObject item)
Returns the price (in gold) the shop would pay for the item.


formatCurrency

public static java.lang.String formatCurrency(int silver_value)
Formats a currency as platinum, gold and silver.