added aglup to code
[asgard.git] / consolecontroller.py
blob9734ba05b416533314f77d93edfd69e1d0e9c006
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 statistic import *
20 from os import listdir
21 from battle import *
22 from equiption import *
23 from fighter import *
24 from eventtype import *
25 class ConsoleController:
26 def __init__(self,debug,battle,view):
27 self.__debug = debug
28 self.__battle = battle
29 self.__view = view
31 battle.setController(self)
32 battle.setView(view)
33 view.setController(self)
34 view.setBattle(battle)
36 self.load()
37 self.__gameLoop()
38 #self.save()
40 def getCommand(self,p,f):
42 command = []
44 print "\033[1;34m[SELCT]\033[1;0m Turn:" + f.getName() + "; HP:" + str(f.getStat("hp").getCurrent()) + "/" + str(f.getStat("hp").getMax())
46 self.__view.printEventTypeMenu(f)
47 n = raw_input("% ")
48 t = self.__battle.getEventType(f.getEventTypes()[int(n)-1])
50 if t.getName() == "quit":
51 sys.exit()
53 self.__view.printEnemySelectMenu(p)
54 n = raw_input("% ")
55 d = p.getFighter(int(n)-1)
57 return [d,t]
59 def getDebug(self):
60 return self.__debug
62 def setDebug(self,d):
63 self.__debug = d
65 def __gameLoop(self):
66 """Retrieves events from Parties and Executes all Events in queue per iteration"""
68 print "Battle Starts."
69 self.__view.printState()
71 # initialize the event checker boolean
72 wereEvents = False
74 while not self.__battle.isOver():
75 # next iteration
76 wereEvents = self.__battle.nextTurn()
78 # only print out state if something actually happened
79 if wereEvents:
80 self.__view.printState()
82 if self.__battle.getParty1().isAlive():
83 # sum exp from enemy party
84 exp = 0
85 for f in self.__battle.getParty2().getFighters():
86 exp += f.getStat("exp").getMax()
88 # hero party gets exp
89 self.__battle.getParty1().giveExp(exp)
91 print "Final Results"
92 print "============================================================="
93 self.__view.printParty(self.__battle.getParty1())
94 print "============================================================="
95 print "Victory Fanfare. Hand's party wins "+str(exp)+" exp!"
96 else:
97 print "Game Over. Asgard mourns."
100 def load(self):
101 """Load game."""
104 dir = listdir('./data/eventtypes/')
106 self.__battle.setEventTypeList([])
107 for et in dir:
109 if et[0] == ".":
110 continue
112 et = './data/eventtypes/'+et
113 fptr = open(et,"r")
114 lines = fptr.readlines()
116 status_list = []
117 elem_list = []
118 if len(lines) > 3:
119 # retrieve a list of statuses that the event type afflicts
120 if lines[3].strip() != "":
121 status_list = lines[3].strip().split(",")
123 # elemental tuple loading
124 x = 4
125 while x < len(lines):
126 elem = lines[x].split(",")
127 # elemental name, base, error
128 elem_list.append((elem[0].strip(),int(elem[1].strip()),float(elem[2].strip())))
129 x += 1
131 et_o = EventType(lines[1].strip(),lines[0].strip(),status_list,elem_list,int(lines[2].strip()))
132 self.__battle.getEventTypeList().append(et_o)
134 ### Loading parties ###
135 self.__battle.setParty1(None)
136 self.__battle.setParty2(None)
137 dir = listdir('./data/party/')
138 for p in dir:
140 if p[0] == ".":
141 continue
143 p = './data/party/'+p
144 fptr = open(p,'r')
145 lines = fptr.readlines()
146 if int(lines[0]) == 1:
147 self.__battle.setParty1(Party(lines[1].strip()))
148 elif int(lines[0]) == 2:
149 self.__battle.setParty2(Party(lines[1].strip()))
150 fptr.close()
152 ### Loading fighters ###
153 dir = listdir('./data/fighter/')
154 for f in dir:
156 statlist = []
158 # f cannot be a "." file
159 if f[0] == '.':
160 continue
162 # Change file into pathname
163 f = './data/fighter/'+f
165 # Open file f for 'r'eading
166 fptr = open(f,'r')
168 # Place lines in file f into list
169 lines = fptr.readlines()
172 name = lines[0].strip()
173 jobname = lines[1].strip()
175 job_fptr = open('./data/job/'+jobname.lower(),'r')
177 job_lines = job_fptr.readlines()
179 # chop up the lines
181 tom = round((1/float(lines[11])*100))
183 statlist.append(Statistic("level",int(lines[2]),99,False,1,1,0))
184 statlist.append(Statistic("exp",int(lines[3]),int(lines[3]),True,0,0,0))
185 statlist.append(Statistic("exptnl",int(lines[5]),int(lines[4]),True,0,0,float(job_lines[1])))
186 statlist.append(Statistic("hp",int(lines[7]),int(lines[6]),True,int(job_lines[2]),float(job_lines[10]),0))
187 statlist.append(Statistic("mp",int(lines[9]),int(lines[8]),True,int(job_lines[3]),float(job_lines[11]),0))
188 statlist.append(Statistic("str",int(lines[10]),int(lines[10]),False,int(job_lines[4]),float(job_lines[12]),0))
189 statlist.append(Statistic("spd",int(lines[11]),int(lines[11]),False,int(job_lines[5]),float(job_lines[13]),0))
190 statlist.append(Statistic("dex",int(lines[12]),int(lines[12]),False,int(job_lines[6]),float(job_lines[14]),0))
191 statlist.append(Statistic("agl",int(lines[13]),int(lines[13]),False,int(job_lines[7]),float(job_lines[15]),0))
192 statlist.append(Statistic("wis",int(lines[14]),int(lines[14]),False,int(job_lines[8]),float(job_lines[16]),0))
193 statlist.append(Statistic("wil",int(lines[15]),int(lines[15]),False,int(job_lines[9]),float(job_lines[17]),0))
194 statlist.append(Statistic("tom",tom,tom,False,0,0,0))
197 playable = bool(int(lines[16].strip()))
198 head = lines[17].strip()
199 body = lines[18].strip()
200 left = lines[19].strip()
201 right = lines[20].strip()
202 pn = int(lines[21])
204 job_fptr.close()
206 #this time of move calc. might should be moved to the fighter constructor
208 # create equiption object
209 equip = Equiption(head,body,left,right)
211 #create fighter
212 fighter1 = Fighter(name,jobname,playable,statlist,equip,[])
215 # Add fighter to respective party (1 or 2)
216 if pn == 1:
217 self.__battle.getParty1().addFighter(fighter1)
218 elif pn == 2:
219 self.__battle.getParty2().addFighter(fighter1)
220 # Close file f
221 fptr.close()
226 def save(self):
227 for f in self.__battle.getParty1().getFighters():
228 fptr = open("./data/fighter/" + f.getName(),"w")
229 e = f.getEquiption()
231 fptr.write(f.getName() + "\n")
232 fptr.write(f.getJobType() + "\n")
233 fptr.write(str(f.getStat("level").getCurrent()) + "\n")
234 fptr.write(str(f.getStat("exp").getMax()) + "\n")
235 fptr.write(str(f.getStat("exptnl").getMax()) + "\n")
236 fptr.write(str(f.getStat("exptnl").getCurrent()) + "\n")
237 fptr.write(str(f.getStat("hp").getMax()) + "\n")
238 fptr.write(str(f.getStat("hp").getCurrent()) + "\n")
239 fptr.write(str(f.getStat("mp").getMax()) + "\n")
240 fptr.write(str(f.getStat("mp").getCurrent()) + "\n")
241 fptr.write(str(f.getStat("str").getMax()) + "\n")
242 fptr.write(str(f.getStat("spd").getMax()) + "\n")
243 fptr.write(str(f.getStat("dex").getMax()) + "\n")
244 fptr.write(str(f.getStat("agl").getMax()) + "\n")
245 fptr.write(str(f.getStat("wis").getMax()) + "\n")
246 fptr.write(str(f.getStat("wil").getMax()) + "\n")
248 if f.getPlayable():
249 fptr.write(str(1) + "\n")
250 else:
251 fptr.write(str(0) + "\n")
253 fptr.write(e.getHead() + "\n")
254 fptr.write(e.getArmor() + "\n")
255 fptr.write(e.getLeft() + "\n")
256 fptr.write(e.getRight() + "\n")
257 fptr.write(str(1) + "\n")
258 fptr.close()
260 def getBattle(self):
261 return self.__battle
263 def getView(self):
264 return self.__view