Heal should be implemented now. Heal removes sleep, poison, paralysis and madness.
[asgard.git] / consolecontroller.py
blob9c79164d1ede43acaecfe2a2ae57ef81ebb8278d
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 import struct
26 import copy
27 import sys
28 import utilities
30 class ConsoleController:
31 def __init__(self,debug,battle,view):
32 self.__debug = debug
33 self.__battle = battle
34 self.__view = view
36 battle.setController(self)
37 battle.setView(view)
38 view.setController(self)
39 view.setBattle(battle)
41 self.__gameLoop()
43 def getBattleCommand(self,vp,hp,f):
45 command = []
47 print "\033[1;34m[SELCT]\033[1;0m Turn:" + f.getName() + "; HP:" + str(f.getStat("hp").getCurrent()) + "/" + str(f.getStat("hp").getMax())
49 self.__view.printEventTypeMenu(f)
50 n = raw_input("% ")
51 t = self.__battle.getEventType(f.getEventTypes()[int(n)-1])
53 if t.getName() == "quit":
54 sys.exit()
56 self.__view.printTargetSelectMenu(vp,hp)
57 n = raw_input("% ")
58 # Select Villain
59 if vp.sizeOfParty() - int(n) >= 0:
60 d = vp.getFighter(int(n)-1)
61 # Select Hero
62 else:
63 d = hp.getFighter(int(n)-vp.sizeOfParty()-1)
64 return [d,t]
66 def getMenuCommand(self):
68 self.__view.printStartupMenu()
70 n = raw_input("% ")
72 return int(n)
74 def getDebug(self):
75 return self.__debug
77 def setDebug(self,d):
78 self.__debug = d
80 def __gameLoop(self):
81 """Retrieves events from Parties and Executes all Events in queue per iteration"""
83 print "Asgard Free Software RPG - Testing Module"
85 self.loadStaticData(False)
87 while True:
90 choice = 0
91 while choice != 4:
92 choice = self.getMenuCommand()
94 if choice == 1:
95 self.unloadStaticData()
96 self.loadStaticData(True)
97 else:
98 if choice == 2:
99 print "Path to Save File: ",
100 path = sys.stdin.readline()
101 self.load("" + path + "")
102 print "loaded..."
103 print "============================================================="
105 elif choice == 3:
106 print "Path to Save File: ",
107 path = sys.stdin.readline()
108 self.save("" + path + "")
109 print "saved..."
110 print "============================================================="
112 elif choice == 4:
113 self.createRandomBattle()
114 print "============================================================="
115 else:
116 sys.exit()
118 print "Battle Starts."
119 self.__view.printState()
121 # initialize the event checker boolean
122 wereEvents = False
124 while not self.__battle.isOver():
125 # next iteration
126 wereEvents = self.__battle.nextTurn()
128 # only print out state if something actually happened
129 if wereEvents:
130 self.__view.printState()
132 if self.__battle.getParty1().isAlive():
133 # sum exp from enemy party
134 exp = 0
135 for f in self.__battle.getParty2().getFighters():
136 exp += f.getStat("exp").getMax()
138 # hero party gets exp
139 self.__battle.getParty1().giveExp(exp)
141 print "Final Results"
142 print "============================================================="
143 self.__view.printParty(self.__battle.getParty1())
144 print "============================================================="
145 print "Victory Fanfare. Hand's party wins "+str(exp)+" exp!"
147 else:
148 print "Game Over. Asgard mourns."
150 def load(self,savefile):
151 #clear party1 and it's fighters
152 self.__battle.setParty1(Party("Heroes"))
154 #open savefile
155 fptr = open(savefile,"rb")
157 fptr.seek(0,2)
158 size = fptr.tell()
160 fptr.seek(0,0)
162 # number of bytes read in so far
163 readin = 0
165 while readin != size:
166 #read in struct descriptor
167 type = ord(fptr.read(1))
168 readin += 1
170 if type == 0:
171 fstring = fptr.read(struct.calcsize("20s20s20s20s20s20sB7I6B"))
172 readin += struct.calcsize("20s20s20s20s20s20sB7I6B")
175 fighter = Fighter(binary=fstring)
177 self.__battle.getParty1().addFighter(fighter)
179 #close savefile
180 fptr.close()
182 def save(self,savefile):
183 """ Writes all playable fighters to savefile. """
184 # Retrieving playable fighters
185 fighters = self.__battle.getParty1().getFighters()
187 # Writing each fighter to savefile
188 fptr = open(savefile,"wb")
189 for f in fighters:
190 # Convert fighter f to binary string
191 binStr = f.toBinaryString()
193 # Write binary string to savefile
194 fptr.write(binStr)
196 fptr.close()
198 def createRandomBattle(self):
199 self.__battle.setParty2(Party("Villains"))
200 partySize = int(random.gauss(2,1))
201 for x in range(0, partySize):
202 monstI = random.randint(0,len(self.__battle.getMonsters())-1)
204 monster = copy.deepcopy(self.__battle.getMonsters()[monstI])
205 self.__battle.getParty2().addFighter(monster)
209 def loadStaticData(self,newGame):
210 """Load static game data: eventtypes, fighters (with stats and jobs)"""
213 dir = listdir('./data/eventtypes/')
215 self.__battle.setEventTypeList([])
217 for et in dir:
219 if et[0] == ".":
220 continue
222 etTree = libxml2.parseFile('./data/eventtypes/'+et)
224 #uncomment these
225 eventType = EventType(etTree)
226 self.__battle.getEventTypeList().append(eventType)
228 ### Loading parties ###
229 self.__battle.setParty1(Party("Heroes"))
231 ### Loading fighters ###
232 dir = listdir('./data/fighter/')
233 for f in dir:
235 # f cannot be a "." file
236 if f[0] == '.':
237 continue
239 # Create Fighter XML Tree
240 fTree = libxml2.parseFile('./data/fighter/'+f)
242 # Create Fighter Object
243 fighter = Fighter(fTree=fTree)
245 # Add fighter to respective party (1 or 2)
246 if fighter.getPlayable() and newGame:
247 self.__battle.getParty1().addFighter(fighter)
248 else:
249 self.__battle.getMonsters().append(fighter)
251 def unloadStaticData(self):
252 """ Clears out all monsters and EventTypes. """
254 # Clearing out monsters
255 self.__battle.setMonsters([])
257 # Clearing out EventTypes
258 self.__battle.setEventTypeList([])
260 def getBattle(self):
261 return self.__battle
263 def getView(self):
264 return self.__view