(no commit message)
[asgard.git] / consolecontroller.py
bloba80142b7551f9dfd8d1fa764a0ad1970f0aa6fc4
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 os import listdir
20 from battle import *
21 from equiption import *
22 from fighter import *
24 class ConsoleController:
25 def __init__(self,debug,battle,view):
26 self.__debug = debug
27 self.__battle = battle
28 self.__view = view
30 battle.setController(self)
31 battle.setView(view)
32 view.setController(self)
33 view.setBattle(battle)
35 self.load()
36 self.__gameLoop()
37 self.save()
39 def getCommand(self,p,f):
41 command = []
43 print "\033[1;34m[SELCT]\033[1;0m Turn:" + f.getName() + "; HP:" + str(f.getHpCurrent()) + "/" + str(f.getHpMax())
45 self.__view.printEnemySelectMenu(p)
46 n = raw_input("% ")
47 d = p.getFighter(int(n)-1)
49 self.__view.printEventTypeMenu(f)
50 n = raw_input("% ")
51 t = f.getEventTypes()[int(n)-1]
53 return [d,t]
55 def getDebug(self):
56 return self.__debug
58 def setDebug(self,d):
59 self.__debug = d
61 def __gameLoop(self):
62 """Retrieves events from Parties and Executes all Events in queue per iteration"""
64 print "Battle Starts."
65 self.__view.printState()
67 # initialize the event checker boolean
68 wereEvents = False
70 while not self.__battle.isOver():
71 # next iteration
72 wereEvents = self.__battle.nextTurn()
74 # only print out state if something actually happened
75 if wereEvents:
76 self.__view.printState()
78 if self.__battle.getParty1().isAlive():
79 # sum exp from enemy party
80 exp = 0
81 for f in self.__battle.getParty2().getFighters():
82 exp += f.getExp()
84 # hero party gets exp
85 self.__battle.getParty1().giveExp(exp)
87 print "Final Results"
88 print "============================================================="
89 self.__view.printParty(self.__battle.getParty1())
90 print "============================================================="
91 print "Victory Fanfare. Heros win "+str(exp)+" exp!"
92 else:
93 print "Game Over. Imps win."
96 def load(self):
97 """Load game."""
99 ### Loading job classes ###
100 dir = listdir('./data/job')
101 self.__battle.setJobList([])
102 for j in dir:
104 if j[0] == ".":
105 continue
107 j = './data/job/'+j
108 fptr = open(j,'r')
109 lines = fptr.readlines()
110 self.__battle.getJobList().append(Job(lines[0].strip(),float(lines[1]),int(lines[2]),int(lines[3]),int(lines[4]),int(lines[5]),int(lines[6]),int(lines[7]),int(lines[8]),float(lines[9]),float(lines[10]),float(lines[11]),float(lines[12]),float(lines[13]),float(lines[14]),float(lines[15])))
111 fptr.close()
113 ### Loading parties ###
114 self.__battle.setParty1(None)
115 self.__battle.setParty2(None)
116 dir = listdir('./data/party/')
117 for p in dir:
119 if p[0] == ".":
120 continue
122 p = './data/party/'+p
123 fptr = open(p,'r')
124 lines = fptr.readlines()
125 if int(lines[0]) == 1:
126 self.__battle.setParty1(Party(lines[1].strip()))
127 elif int(lines[0]) == 2:
128 self.__battle.setParty2(Party(lines[1].strip()))
129 fptr.close()
131 ### Loading fighters ###
132 dir = listdir('./data/fighter/')
133 for f in dir:
134 # f cannot be a "." file
135 if f[0] == '.':
136 continue
137 # Change file into pathname
138 f = './data/fighter/'+f
139 # Open file f for 'r'eading
140 fptr = open(f,'r')
141 # Place lines in file f into list
142 lines = fptr.readlines()
144 # chop up the lines
145 name = lines[0].strip()
146 jobname = lines[1].strip()
147 level = int(lines[2])
148 exp = int(lines[3])
149 exptnlm = int(lines[4])
150 exptnl = int(lines[5])
151 maxhp = int(lines[6])
152 currenthp = int(lines[7])
153 str = int(lines[8])
154 speed = int(lines[9])
155 dex = int(lines[10])
156 agility = int(lines[11])
157 wisdom = int(lines[12])
158 will = int(lines[13])
159 playable = bool(int(lines[14].strip()))
160 head = lines[15].strip()
161 body = lines[16].strip()
162 left = lines[17].strip()
163 right = lines[18].strip()
164 pn = int(lines[19])
166 #this tom calc. might should be moved to the fighter constructor
167 tom = round((1/float(speed))*100)
168 equip = Equiption(head,body,left,right)
170 for job in self.__battle.getJobList():
171 if job.getName() == jobname:
172 break
174 # Create fighter(name,hp,str,dex,head,l,r,body)
175 fighter1 = Fighter(name,level,exp,exptnlm,exptnl,maxhp,currenthp,str,speed,dex,agility,wisdom,will,tom,playable,equip,job)
178 # Add fighter to respective party (1 or 2)
179 if pn == 1:
180 self.__battle.getParty1().addFighter(fighter1)
181 elif pn == 2:
182 self.__battle.getParty2().addFighter(fighter1)
183 # Close file f
184 fptr.close()
187 def save(self):
188 for f in self.__battle.getParty1().getFighters():
189 fptr = open("./data/fighter/" + f.getName(),"w")
190 e = f.getEquiption()
191 j = f.getJob()
192 fptr.write(f.getName() + "\n")
193 fptr.write(j.getName() + "\n")
194 fptr.write(str(f.getLevel()) + "\n")
195 fptr.write(str(f.getExp()) + "\n")
196 fptr.write(str(f.getExpTillNextLevelMax()) + "\n")
197 fptr.write(str(f.getExpTillNextLevel()) + "\n")
198 fptr.write(str(f.getHpMax()) + "\n")
199 fptr.write(str(f.getHpCurrent()) + "\n")
200 fptr.write(str(f.getStrength()) + "\n")
201 fptr.write(str(f.getSpeed()) + "\n")
202 fptr.write(str(f.getDexterity()) + "\n")
203 fptr.write(str(f.getAgility()) + "\n")
204 fptr.write(str(f.getWisdom()) + "\n")
205 fptr.write(str(f.getWill()) + "\n")
207 if f.getPlayable():
208 fptr.write(str(1) + "\n")
209 else:
210 fptr.write(str(0) + "\n")
212 fptr.write(e.getHead() + "\n")
213 fptr.write(e.getArmor() + "\n")
214 fptr.write(e.getLeft() + "\n")
215 fptr.write(e.getRight() + "\n")
216 fptr.write(str(1) + "\n")
217 fptr.close()