enemy pointers are now actually enemy clones. (3 rocs = 3 entirely separate rocs)
[asgard.git] / consolecontroller.py
blob377dacdf02015f569e492534969c10982a68ea89
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 utilities
29 class ConsoleController:
30 def __init__(self,debug,battle,view):
31 self.__debug = debug
32 self.__battle = battle
33 self.__view = view
35 battle.setController(self)
36 battle.setView(view)
37 view.setController(self)
38 view.setBattle(battle)
40 self.__gameLoop()
42 def getCommand(self,vp,hp,f):
44 command = []
46 print "\033[1;34m[SELCT]\033[1;0m Turn:" + f.getName() + "; HP:" + str(f.getStat("hp").getCurrent()) + "/" + str(f.getStat("hp").getMax())
48 self.__view.printEventTypeMenu(f)
49 n = raw_input("% ")
50 t = self.__battle.getEventType(f.getEventTypes()[int(n)-1])
52 if t.getName() == "quit":
53 sys.exit()
55 self.__view.printTargetSelectMenu(vp,hp)
56 n = raw_input("% ")
57 # Select Villain
58 if vp.sizeOfParty() - int(n) >= 0:
59 d = vp.getFighter(int(n)-1)
60 # Select Hero
61 else:
62 d = hp.getFighter(int(n)-vp.sizeOfParty()-1)
63 return [d,t]
65 def getDebug(self):
66 return self.__debug
68 def setDebug(self,d):
69 self.__debug = d
71 def __gameLoop(self):
72 """Retrieves events from Parties and Executes all Events in queue per iteration"""
74 print "Asgard Free Software RPG - Testing Module"
75 self.__view.printStartupMenu()
77 # temporarily new game by default
78 self.loadStaticData(True)
79 #self.load("test.save")
80 self.createRandomBattle()
82 print "Battle Starts."
83 self.__view.printState()
85 # initialize the event checker boolean
86 wereEvents = False
88 while not self.__battle.isOver():
89 # next iteration
90 wereEvents = self.__battle.nextTurn()
92 # only print out state if something actually happened
93 if wereEvents:
94 self.__view.printState()
96 if self.__battle.getParty1().isAlive():
97 # sum exp from enemy party
98 exp = 0
99 for f in self.__battle.getParty2().getFighters():
100 exp += f.getStat("exp").getMax()
102 # hero party gets exp
103 self.__battle.getParty1().giveExp(exp)
105 print "Final Results"
106 print "============================================================="
107 self.__view.printParty(self.__battle.getParty1())
108 print "============================================================="
109 print "Victory Fanfare. Hand's party wins "+str(exp)+" exp!"
110 return True
111 else:
112 print "Game Over. Asgard mourns."
113 return False
115 def load(self,savefile):
116 #clear party1 and it's fighters
117 self.__battle.setParty1(Party("Heroes"))
119 #open savefile
120 fptr = open(savefile,"rb")
122 fptr.seek(0,2)
123 size = fptr.tell()
125 fptr.seek(0,0)
127 # number of bytes read in so far
128 readin = 0
130 while readin != size:
131 #read in struct descriptor
132 type = ord(fptr.read(1))
133 readin += 1
135 if type == 0:
136 fstring = fptr.read(struct.calcsize("20s20s20s20s20s20sB7I6B"))
137 readin += struct.calcsize("20s20s20s20s20s20sB7I6B")
140 fighter = Fighter(binary=fstring)
142 self.__battle.getParty1().addFighter(fighter)
144 #close savefile
145 fptr.close()
147 def createRandomBattle(self):
148 partySize = random.randint(1,6)
149 for x in range(0, partySize):
150 monstI = random.randint(0,len(self.__battle.getMonsters())-1)
152 monster = copy.deepcopy(self.__battle.getMonsters()[monstI])
153 self.__battle.getParty2().addFighter(monster)
157 def loadStaticData(self,newGame):
158 """Load static game data: eventtypes, fighters (with stats and jobs)"""
161 dir = listdir('./data/eventtypes/')
163 self.__battle.setEventTypeList([])
165 for et in dir:
167 if et[0] == ".":
168 continue
170 etTree = libxml2.parseFile('./data/eventtypes/'+et)
172 #uncomment these
173 eventType = EventType(etTree)
174 self.__battle.getEventTypeList().append(eventType)
176 ### Loading parties ###
177 self.__battle.setParty1(Party("Heroes"))
178 self.__battle.setParty2(Party("Villains"))
180 ### Loading fighters ###
181 dir = listdir('./data/fighter/')
182 for f in dir:
184 # f cannot be a "." file
185 if f[0] == '.':
186 continue
188 # Create Fighter XML Tree
189 fTree = libxml2.parseFile('./data/fighter/'+f)
191 # Create Fighter Object
192 fighter = Fighter(fTree=fTree)
194 # Add fighter to respective party (1 or 2)
195 if fighter.getPlayable() and newGame:
196 self.__battle.getParty1().addFighter(fighter)
197 else:
198 self.__battle.getMonsters().append(fighter)
201 def save(self):
202 for f in self.__battle.getParty1().getFighters():
203 fptr = open("./data/fighter/" + f.getName(),"w")
204 e = f.getEquiption()
206 fptr.write(f.getName() + "\n")
207 fptr.write(f.getJobType() + "\n")
208 fptr.write(str(f.getStat("level").getCurrent()) + "\n")
209 fptr.write(str(f.getStat("exp").getMax()) + "\n")
210 fptr.write(str(f.getStat("exptnl").getMax()) + "\n")
211 fptr.write(str(f.getStat("exptnl").getCurrent()) + "\n")
212 fptr.write(str(f.getStat("hp").getMax()) + "\n")
213 fptr.write(str(f.getStat("hp").getCurrent()) + "\n")
214 fptr.write(str(f.getStat("mp").getMax()) + "\n")
215 fptr.write(str(f.getStat("mp").getCurrent()) + "\n")
216 fptr.write(str(f.getStat("str").getMax()) + "\n")
217 fptr.write(str(f.getStat("spd").getMax()) + "\n")
218 fptr.write(str(f.getStat("dex").getMax()) + "\n")
219 fptr.write(str(f.getStat("agl").getMax()) + "\n")
220 fptr.write(str(f.getStat("wis").getMax()) + "\n")
221 fptr.write(str(f.getStat("wil").getMax()) + "\n")
223 if f.getPlayable():
224 fptr.write(str(1) + "\n")
225 else:
226 fptr.write(str(0) + "\n")
228 fptr.write(e.getHead() + "\n")
229 fptr.write(e.getArmor() + "\n")
230 fptr.write(e.getLeft() + "\n")
231 fptr.write(e.getRight() + "\n")
232 fptr.write(str(1) + "\n")
233 fptr.close()
235 def getBattle(self):
236 return self.__battle
238 def getView(self):
239 return self.__view