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
22 from equiption
import *
24 from eventtype
import *
30 class ConsoleController
:
31 def __init__(self
,debug
,battle
,view
):
33 self
.__battle
= battle
36 battle
.setController(self
)
38 view
.setController(self
)
39 view
.setBattle(battle
)
43 def getBattleCommand(self
,vp
,hp
,f
):
47 print "\033[1;34m[SELCT]\033[1;0m Turn:" + f
.getName() + "; HP:" + str(f
.getStat("hp").getCurrent()) + "/" + str(f
.getStat("hp").getMax()) + " - MP:"+str(f
.getStat("mp").getCurrent())+"/"+str(f
.getStat("mp").getMax())
49 self
.__view
.printEventTypeMenu(f
,self
.__battle
.getEventTypeList())
51 t
= self
.__battle
.getEventType(f
.getEventTypes(self
.__battle
.getEventTypeList())[int(n
)-1])
53 if t
.getName() == "quit":
56 self
.__view
.printTargetSelectMenu(vp
,hp
)
59 if vp
.sizeOfParty() - int(n
) >= 0:
60 d
= vp
.getFighter(int(n
)-1)
63 d
= hp
.getFighter(int(n
)-vp
.sizeOfParty()-1)
66 def getMenuCommand(self
):
68 self
.__view
.printStartupMenu()
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)
90 choice
= self
.getMenuCommand()
93 self
.unloadStaticData()
94 self
.loadStaticData(True)
98 print "Path to Save File: ",
99 path
= sys
.stdin
.readline()
100 self
.load("" + path
.strip() + "")
104 print "Path to Save File: ",
105 path
= sys
.stdin
.readline()
106 self
.save("" + path
.strip() + "")
111 if self
.__battle
.getParty1() == None:
112 print "No game loaded."
115 self
.createRandomBattle()
116 print "============================================================="
120 print "Battle Starts."
121 self
.__view
.printState()
123 # initialize the event checker boolean
126 while not self
.__battle
.isOver():
128 wereEvents
= self
.__battle
.nextTurn()
130 # only print out state if something actually happened
132 self
.__view
.printState()
134 if self
.__battle
.getParty1().isAlive():
137 # sum exp from enemy party
139 for f
in self
.__battle
.getParty2().getFighters():
140 exp
+= f
.getStat("exp").getMax()
142 # hero party gets exp
143 self
.__battle
.getParty1().giveExp(exp
)
146 print "Final Results"
147 print "============================================================="
148 self
.__view
.printParty(self
.__battle
.getParty1())
149 print "============================================================="
150 print "Victory Fanfare. Hand's party wins "+str(exp
)+" exp!"
152 for f
in self
.__battle
.getParty1().getFighters():
153 # remove all statuses
154 for s
in f
.getStatus():
156 f
.getStat("mp").setCurrent(f
.getStat("mp").getMax())
159 print "Game Over. Asgard mourns."
161 # clear out the hero party - they're dead!
162 self
.__battle
.setParty1(None)
165 def load(self
,savefile
):
166 #clear party1 and it's fighters
167 self
.__battle
.setParty1(Party("Heroes"))
170 fptr
= open(savefile
,"rb")
177 # number of bytes read in so far
180 while readin
!= size
:
181 #read in struct descriptor
183 tstring
= fptr
.read(struct
.calcsize("!B"))
184 [type] = struct
.unpack("!B",tstring
)
185 readin
+= struct
.calcsize("!B")
189 fstring
= fptr
.read(struct
.calcsize("!20s20s20s20s20s20sB7I6B"))
190 readin
+= struct
.calcsize("!20s20s20s20s20s20sB7I6B")
195 fighter
= Fighter(binary
=fstring
)
196 self
.__battle
.getParty1().addFighter(fighter
)
201 def save(self
,savefile
):
202 """ Writes all playable fighters to savefile. """
203 # Retrieving playable fighters
204 fighters
= self
.__battle
.getParty1().getFighters()
206 # Writing each fighter to savefile
207 fptr
= open(savefile
,"wb")
210 # Convert fighter f to binary string
211 binStr
= f
.toBinaryString()
213 # Write binary string to savefile
214 fptr
.write(binStr
.strip())
218 def createRandomBattle(self
):
219 self
.__battle
.setParty2(Party("Villains"))
221 partySize
+= random
.randint(-1,2)
222 for x
in range(0, partySize
):
223 monstI
= random
.randint(0,len(self
.__battle
.getMonsters())-1)
225 monster
= copy
.deepcopy(self
.__battle
.getMonsters()[monstI
])
226 self
.__battle
.getParty2().addFighter(monster
)
228 for f
in self
.__battle
.getParty1().getFighters():
229 spd
= f
.getStat("spd").getCurrent()
230 tom
= round((1/float(spd
)*100))
231 f
.getStat("tom").setCurrent(tom
)
232 f
.getStat("tom").setMax(tom
)
233 f
.getStat("canMove").setCurrent(1)
234 f
.getStat("ai").setCurrent(0)
236 self
.__battle
.setCurrentTime(0)
238 def loadStaticData(self
,newGame
):
239 """Load static game data: eventtypes, fighters (with stats and jobs)"""
242 dir = listdir('./data/eventtypes/')
244 self
.__battle
.setEventTypeList([])
251 etTree
= libxml2
.parseFile('./data/eventtypes/'+et
)
254 eventType
= EventType(etTree
)
255 self
.__battle
.getEventTypeList().append(eventType
)
258 self
.__battle
.setParty1(Party("Heroes"))
260 ### Loading fighters ###
261 dir = listdir('./data/fighter/')
264 # f cannot be a "." file
268 # Create Fighter XML Tree
269 fTree
= libxml2
.parseFile('./data/fighter/'+f
)
271 # Create Fighter Object
272 fighter
= Fighter(fTree
=fTree
)
274 # Add fighter to respective party (1 or 2)
275 if fighter
.getPlayable() and newGame
:
276 self
.__battle
.getParty1().addFighter(fighter
)
277 elif not fighter
.getPlayable():
278 self
.__battle
.getMonsters().append(fighter
)
280 def unloadStaticData(self
):
281 """ Clears out all monsters and EventTypes. """
283 # Clearing out monsters
284 self
.__battle
.setMonsters([])
286 # Clearing out EventTypes
287 self
.__battle
.setEventTypeList([])