Module gameobject
[hide private]
[frames] | no frames]

Source Code for Module gameobject

  1  #!/usr/bin/python3 
  2  """ This module implements the (foundation) GameObject Class """ 
  3  import sys 
  4  from random import randint 
  5  from base import Base 
  6  from gameaction import GameAction 
  7   
  8   
9 -class GameObject(Base):
10 """ 11 This is the base class for all artifacts, actors, and contexts. 12 The abilities of this base class are: 13 - own a list of objects (which can be added and retrieved) 14 - return a list of GameActions that it enables 15 - accept and process non-ATTACK GameActions 16 17 @ivar objects: list of owned/contained GameObjects 18 19 objects can be I{hidden} in which case they might not be returned 20 """
21 - def __init__(self, name="actor", descr=None):
22 """ 23 create a new GameObject 24 @param name: display name of this object 25 @param descr: for players description of this object 26 """
27
28 - def __str__(self):
29 """ 30 return the given name of this object 31 """
32
33 - def get_objects(self, hidden=False):
34 """ 35 return a list of GameObjects contained in/owned by this GameObject 36 37 if an object is hidden (has a positive RESISTANCE.SEARCH) it may not 38 be visible unless 39 - it has been successfully found (SEARCH > 0) 40 - caller specifies that hidden objects should be returned 41 42 @param hidden: return only hidden objects 43 @return: list of discoverd GameOjects 44 """
45 # check object's RESISTANCE.SEARCH and SEARCH attributes 46 # if hidden specified, find all hidden objects 47 # else find only visible objects 48
49 - def get_object(self, name):
50 """ 51 return a named object from my inventory 52 53 @param name: (string) of the desired object 54 @return: first matching object (or None) 55 """
56
57 - def add_object(self, item):
58 """ 59 add another object to my C{objects} list (if not already there) 60 """
61
62 - def accept_action(self, action, actor, context):
63 """ 64 called by C{GameAction.act()} to receive GameAction, determine effects 65 66 NOTE: this base class cannot process ATTACK actions. 67 Those are processed by the C{GameActor} sub-class. 68 This base class can only process actions which (if successful), 69 increment the property who's name matches the action verb. 70 71 @param action: GameAction being performed 72 @param actor: GameActor initiating the action 73 @param context: GameContext in which action is occuring 74 75 NOTE: this base class makes no use of the C{actor} or C{context} 76 parameters, but they might be useful to a subc-class that could 77 process actions before passing them down to us. 78 79 @return: <(boolean) success, (string) description of the effect> 80 """
81 # get the base verb and sub-type 82 # look up our base resistance 83 # see if we have a RESISTANCE.base-verb 84 # see if we have a RESISTANCE.base-verb.subtype 85 # if sum of RESISTANCE >= TO_HIT, action has been resisted 86 # for each STACK instance, roll to see if roll+RESISTANCE > TO_HIT 87 # accumulate the number that get through 88 # add number of successful STACKS to affected attribute 89 # (or if C{GameAction.TOTAL} is negative, subtract) 90 # special case: LIFE cannot be raised beyond HP 91 # return <whether or not any succeed, accumulated results> 92
93 - def possible_actions(self, actor, context):
94 """ 95 return list of C{GameAction}s this object enables 96 97 verbs come from our (comma-separated-verbs) ACTIONS attribute 98 for each C{GameAction}, ACCURACY, DAMAGE, POWER, STACKS are the sum of 99 - our base ACCURACY, DAMAGE, POWER, STACKS 100 - our ACCURACY.verb, DAMAGE.verb, POWER.verb, STACKS.verb, 101 102 @param actor: GameActor initiating the action 103 @param context: GameContext in which the action is taken 104 105 NOTE: this base class makes no use of the C{actor} and C{context} 106 parameters, but a sub-class might want to determine whether or not 107 B{this actor} could perform this action in B{this context}. 108 109 @return: list of possible GameActions 110 """
111 # get a list of possible actions with this object (e.g. weapon) 112 # get our base ACCURACY/DAMAGE/POWER/STACKS attributes 113 # instantiate a GameAction for each verb in our ACTIONS list 114 # accumulate base and sub-type attributes 115 # if a verb is compound (+), accumulate each sub-verb separately 116 # see if we have ATTACK sub-type ACCURACY/DAMAGE 117 # combine the base and sub-type values 118 # FIX GameAction.DAMAGE could be a (non-addable) D-formula 119 # see if we have verb sub-type POWER/STACKS 120 # FIX GameAction.STACKS could be a (non-addable) D-formula 121 # add accumulated ACCURACY/DAMAGE/POWER/STACKS to C{GameAction} 122 # append the new C{GameAction} to the list to be returned 123
124 - def load(self, filename):
125 """ 126 read object definitions from a file 127 - blank lines and lines beginning w/# are ignored 128 - NAME string ... is the name of an object 129 - DESCRIPTION string ... is the description of that object 130 - ACTIONS string ... is the list of supported verbs 131 - OBJECT ... introduces definition of an object in our inventory 132 - anything else is an atribute and value (strings should be quoted) 133 134 NOTE: The object being defined can contain other objects. 135 (e.g. guard has a sword, box contains a scroll) 136 But contained objects cannot, themselves, contain other objects. 137 138 @param filename: name of file to be read 139 """
140 # for each non-comment line, read name and value 141 # check for special names: NAME, DESCRIPTION, OBJECT 142 # anything else is just an attribute of latest object 143 144
145 -def _lex(line):
146 """ 147 helper function to lex a name and (potentially quoted) value from a line 148 - treat (single or double) quoted strings as a single token 149 - if second token is an integer, return it as such, else a string 150 151 @param line: string to be lexed 152 @return: (name, value) ... or (None, None) for blank/comment lines 153 """
154 # find the start of the first token 155 # if this is a comment or blank line, return (None, None) 156 # lex off first (blank separated) token as a name 157 # lex off next token as a value 158 # either single or double quotes can surround a value 159 # scan until the closing quote (or EOL) 160 # an un-quoted number should be returned as an int 161