minor updates.
[asgard.git] / transaction.py
blobef6c6c54b0b761e0deb1336ba8bde0e8201ec517
1 ########################################################
2 #Copyright (c) 2006 Russ Adams, Sean Eubanks, Asgard Contributors
3 #This file is part of Asgard.
5 #Asgard is free software; you can redistribute it and/or modify
6 #it under the terms of the GNU General Public License as published by
7 #the Free Software Foundation; either version 2 of the License, or
8 #(at your option) any later version.
10 #Asgard is distributed in the hope that it will be useful,
11 #but WITHOUT ANY WARRANTY; without even the implied warranty of
12 #MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 #GNU General Public License for more details.
15 #You should have received a copy of the GNU General Public License
16 #along with Asgard; if not, write to the Free Software
17 #Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 ########################################################
19 from status import *
21 class Transaction:
22 """ Model: Transaction Class """
24 __target = None
25 """ The Fighter that the transaction is being executed on. Can be the same as executor. """
27 def __init__(self,target):
28 """ Base Transaction. """
30 self.target = target
32 def execute(self):
33 pass
35 def getStatus(self):
36 pass
38 def setStatus(self,status):
39 pass
41 def getTarget(self):
42 return self.target
44 def setTarget(self,target):
45 self.target = target
47 class TransactionStatistic(Transaction):
48 """ Model: TransactionStatistic Class """
50 __stat = None
51 """ The stat name. i.e. "hp", "mp", "level". """
53 __change = 0
54 """ How much the stat is changed. """
56 def __init__(self,target,stat,change):
57 """ Construct a Transaction for modifying Statistics. """
59 Transaction.__init__(self,target)
60 self.__stat = stat
61 self.__change = change
63 def execute(self):
64 """ Perform transaction in target. Called by Event.execute().
65 (Note: If change = 0 and status != "", generate the Status object and it's
66 Transactions based off the target's Statistics. If add is set to True, add the
67 status, if add is set to False, remove it. (if this is a Status Transaction))
68 """
70 # If a Stat is supposed to be modified
71 if self.__stat != None:
72 alive_before_transaction = self.target.isAlive()
74 # Retrieve that Stat from the target
75 stat_to_change = self.target.getStat(self.__stat)
77 # change the value of that stat by the amount in "change"
78 stat_to_change.setCurrent(stat_to_change.getCurrent() + self.__change)
80 if self.target.getStat("hp").getCurrent() <= 0 and alive_before_transaction:
81 deathT = TransactionStatus(self.target,"death",True)
82 deathT.execute()
83 elif self.target.getStat("hp").getCurrent() > self.target.getStat("hp").getMax():
84 self.target.getStat("hp").setCurrent(self.target.getStat("hp").getMax())
86 def getStat(self):
87 return self.__stat
89 def setStat(self,stat):
90 self.__stat = stat
92 def getChange(self):
93 return self.__change
95 def setChange(self,change):
96 self.__change = change
98 class TransactionStatus(Transaction):
99 """ Model: TransactionStatus Class """
101 __status = None
102 """ The status this transaction inflicts. """
104 def __init__(self,target,status,add):
105 """ Construct a Transaction for adding or removing a Status. If
106 add is true, it adds the Status. """
108 Transaction.__init__(self,target)
109 self.__status = status
110 self.__add = add
112 def execute(self):
113 """ Perform transaction in target. Called by Event.execute().
114 (Note: If change = 0 and status != "", generate the Status object and it's
115 Transactions based off the target's Statistics. If add is set to True, add the
116 status, if add is set to False, remove it. (if this is a Status Transaction))
117 """
119 tlistPerTurn = []
120 tlistPerIter = []
121 tlistExit = []
122 tlistEntrance =[]
123 tlistPerTurnOnMove = []
125 if not self.__add:
126 self.target.removeStatus(self.__status)
127 else:
128 # poison status
129 if self.__status == "poison":
130 # remove 5% of hp per turn; this is lame and should be more dynamic
131 trans = TransactionStatistic(self.target,"hp",int(-1 * self.target.getStat("hp").getMax()*.05))
132 tlistPerTurnOnMove.append(trans)
133 elif self.__status == "aglup":
134 trans = TransactionStatistic(self.target,"agl",10)
135 tlistEntrance.append(trans)
136 trans = TransactionStatistic(self.target,"agl",-10)
137 tlistExit.append(trans)
138 elif self.__status == "sleep":
139 tlistEntrance.append(TransactionStatistic(self.target,"canMove",-1))
140 tlistExit.append(TransactionStatistic(self.target,"canMove",1))
141 elif self.__status == "death":
142 tlistEntrance.append(TransactionStatistic(self.target,"canMove",-1))
143 tlistEntrance.append(TransactionStatistic(self.target,"hp",self.target.getStat("hp").getCurrent()*-1))
144 tlistExit.append(TransactionStatistic(self.target,"canMove",1))
145 tlistExit.append(TransactionStatistic(self.target,"hp",int(self.target.getStat("hp").getMax() * .25)))
146 elif self.__status == "paralysis":
147 tlistEntrance.append(TransactionStatistic(self.target,"canMove",-1))
148 tlistExit.append(TransactionStatistic(self.target,"canMove",1))
149 elif self.__status == "madness":
150 ai = self.target.getStat("ai").getCurrent()
151 if ai == 0:
152 tlistEntrance.append(TransactionStatistic(self.target,"ai",2))
153 tlistExit.append(TransactionStatistic(self.target,"ai",-2))
154 elif ai == 1:
155 tlistEntrance.append(TransactionStatistic(self.target,"ai",1))
156 tlistExit.append(TransactionStatistic(self.target,"ai",-1))
158 # create the status object and add it to the target
159 self.target.addStatus(Status(self.__status,tlistEntrance,tlistPerTurn,tlistPerIter,tlistExit,tlistPerTurnOnMove))
162 def getStatus(self):
163 return self.__status
165 def setStatus(self,status):
166 self.__status = status
168 def getAdd(self):
169 return self.__add
171 def setAdd(self, add):
172 self.__add = add