I made a comment to uncomment the commented lines.
[asgard.git] / consolecontroller.py
blobb9067745626ba0667f11b98892703a4a99e73ce9
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.__gameLoop()
38 def getCommand(self,vp,hp,f):
40 command = []
42 print "\033[1;34m[SELCT]\033[1;0m Turn:" + f.getName() + "; HP:" + str(f.getStat("hp").getCurrent()) + "/" + str(f.getStat("hp").getMax())
44 self.__view.printEventTypeMenu(f)
45 n = raw_input("% ")
46 t = self.__battle.getEventType(f.getEventTypes()[int(n)-1])
48 if t.getName() == "quit":
49 sys.exit()
51 self.__view.printTargetSelectMenu(vp,hp)
52 n = raw_input("% ")
53 # Select Villain
54 if vp.sizeOfParty() - int(n) >= 0:
55 d = vp.getFighter(int(n)-1)
56 # Select Hero
57 else:
58 d = hp.getFighter(int(n)-vp.sizeOfParty()-1)
59 return [d,t]
61 def getDebug(self):
62 return self.__debug
64 def setDebug(self,d):
65 self.__debug = d
67 def __gameLoop(self):
68 """Retrieves events from Parties and Executes all Events in queue per iteration"""
70 # temporarily new game by default
71 self.loadStaticData(True)
73 print "Battle Starts."
74 self.__view.printState()
76 # initialize the event checker boolean
77 wereEvents = False
79 while not self.__battle.isOver():
80 # next iteration
81 wereEvents = self.__battle.nextTurn()
83 # only print out state if something actually happened
84 if wereEvents:
85 self.__view.printState()
87 if self.__battle.getParty1().isAlive():
88 # sum exp from enemy party
89 exp = 0
90 for f in self.__battle.getParty2().getFighters():
91 exp += f.getStat("exp").getMax()
93 # hero party gets exp
94 self.__battle.getParty1().giveExp(exp)
96 print "Final Results"
97 print "============================================================="
98 self.__view.printParty(self.__battle.getParty1())
99 print "============================================================="
100 print "Victory Fanfare. Hand's party wins "+str(exp)+" exp!"
101 return True
102 else:
103 print "Game Over. Asgard mourns."
104 return False
107 def loadStaticData(self,newGame):
108 """Load static game data: eventtypes, fighters (with stats and jobs)"""
111 dir = listdir('./data/eventtypes/')
113 self.__battle.setEventTypeList([])
115 for et in dir:
117 if et[0] == ".":
118 continue
120 etTree = libxml2.parseFile('./data/eventtypes/'+et)
122 #uncomment these
123 #eventType = EventType(etTree)
124 #self.__battle.getEventTypeList().append(eventType)
126 ### Loading parties ###
127 self.__battle.setParty1(Party("Heroes"))
128 self.__battle.setParty2(Party("Villains"))
130 ### Loading fighters ###
131 dir = listdir('./data/fighter/')
132 for f in dir:
134 # f cannot be a "." file
135 if f[0] == '.':
136 continue
138 # Create Fighter XML Tree
139 fTree = libxml2.parseFile('./data/fighter/'+f)
141 # Find correct job XML file
142 j = fTree.xpathEval("//job")[0].content + '.xml'
144 # Create Job XML Tree
145 jTree = libxml2.parseFile('./data/job/'+j)
147 # Create Fighter Object
148 fighter = Fighter(jTree,fTree)
150 # Add fighter to respective party (1 or 2)
151 print fighter.getPlayable()
152 if fighter.getPlayable() and newGame:
153 print fighter.getName()
154 self.__battle.getParty1().addFighter(fighter)
155 else:
156 self.__battle.getMonsters().append(fighter)
159 def save(self):
160 for f in self.__battle.getParty1().getFighters():
161 fptr = open("./data/fighter/" + f.getName(),"w")
162 e = f.getEquiption()
164 fptr.write(f.getName() + "\n")
165 fptr.write(f.getJobType() + "\n")
166 fptr.write(str(f.getStat("level").getCurrent()) + "\n")
167 fptr.write(str(f.getStat("exp").getMax()) + "\n")
168 fptr.write(str(f.getStat("exptnl").getMax()) + "\n")
169 fptr.write(str(f.getStat("exptnl").getCurrent()) + "\n")
170 fptr.write(str(f.getStat("hp").getMax()) + "\n")
171 fptr.write(str(f.getStat("hp").getCurrent()) + "\n")
172 fptr.write(str(f.getStat("mp").getMax()) + "\n")
173 fptr.write(str(f.getStat("mp").getCurrent()) + "\n")
174 fptr.write(str(f.getStat("str").getMax()) + "\n")
175 fptr.write(str(f.getStat("spd").getMax()) + "\n")
176 fptr.write(str(f.getStat("dex").getMax()) + "\n")
177 fptr.write(str(f.getStat("agl").getMax()) + "\n")
178 fptr.write(str(f.getStat("wis").getMax()) + "\n")
179 fptr.write(str(f.getStat("wil").getMax()) + "\n")
181 if f.getPlayable():
182 fptr.write(str(1) + "\n")
183 else:
184 fptr.write(str(0) + "\n")
186 fptr.write(e.getHead() + "\n")
187 fptr.write(e.getArmor() + "\n")
188 fptr.write(e.getLeft() + "\n")
189 fptr.write(e.getRight() + "\n")
190 fptr.write(str(1) + "\n")
191 fptr.close()
193 def getBattle(self):
194 return self.__battle
196 def getView(self):
197 return self.__view