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())
49 self
.__view
.printEventTypeMenu(f
)
51 t
= self
.__battle
.getEventType(f
.getEventTypes()[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() + "")
102 print "============================================================="
105 print "Path to Save File: ",
106 path
= sys
.stdin
.readline()
107 self
.save("" + path
.strip() + "")
109 print "============================================================="
113 if self
.__battle
.getParty1().sizeOfParty() == 0:
114 print "No game loaded."
117 self
.createRandomBattle()
118 print "============================================================="
122 print "Battle Starts."
123 self
.__view
.printState()
125 # initialize the event checker boolean
128 while not self
.__battle
.isOver():
130 wereEvents
= self
.__battle
.nextTurn()
132 # only print out state if something actually happened
134 self
.__view
.printState()
136 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
)
145 print "Final Results"
146 print "============================================================="
147 self
.__view
.printParty(self
.__battle
.getParty1())
148 print "============================================================="
149 print "Victory Fanfare. Hand's party wins "+str(exp
)+" exp!"
152 print "Game Over. Asgard mourns."
154 # clear out the hero party - they're dead!
155 self
.__battle
.setParty1(None)
158 def load(self
,savefile
):
159 #clear party1 and it's fighters
160 self
.__battle
.setParty1(Party("Heroes"))
163 fptr
= open(savefile
,"rb")
170 # number of bytes read in so far
173 while readin
!= size
:
174 #read in struct descriptor
175 type = ord(fptr
.read(1))
179 fstring
= fptr
.read(struct
.calcsize("20s20s20s20s20s20sB7I6B"))
180 readin
+= struct
.calcsize("20s20s20s20s20s20sB7I6B")
183 fighter
= Fighter(binary
=fstring
)
185 self
.__battle
.getParty1().addFighter(fighter
)
190 def save(self
,savefile
):
191 """ Writes all playable fighters to savefile. """
192 # Retrieving playable fighters
193 fighters
= self
.__battle
.getParty1().getFighters()
195 # Writing each fighter to savefile
196 fptr
= open(savefile
,"wb")
198 # Convert fighter f to binary string
199 binStr
= f
.toBinaryString()
201 # Write binary string to savefile
206 def createRandomBattle(self
):
207 self
.__battle
.setParty2(Party("Villains"))
208 partySize
= int(random
.gauss(2,1))
209 for x
in range(0, partySize
):
210 monstI
= random
.randint(0,len(self
.__battle
.getMonsters())-1)
212 monster
= copy
.deepcopy(self
.__battle
.getMonsters()[monstI
])
213 self
.__battle
.getParty2().addFighter(monster
)
215 for f
in self
.__battle
.getParty1().getFighters():
216 spd
= f
.getStat("spd").getCurrent()
217 tom
= round((1/float(spd
)*100))
218 f
.getStat("tom").setCurrent(tom
)
219 f
.getStat("tom").setMax(tom
)
221 self
.__battle
.setCurrentTime(0)
225 def loadStaticData(self
,newGame
):
226 """Load static game data: eventtypes, fighters (with stats and jobs)"""
229 dir = listdir('./data/eventtypes/')
231 self
.__battle
.setEventTypeList([])
238 etTree
= libxml2
.parseFile('./data/eventtypes/'+et
)
241 eventType
= EventType(etTree
)
242 self
.__battle
.getEventTypeList().append(eventType
)
244 ### Loading parties ###
245 self
.__battle
.setParty1(Party("Heroes"))
247 ### Loading fighters ###
248 dir = listdir('./data/fighter/')
251 # f cannot be a "." file
255 # Create Fighter XML Tree
256 fTree
= libxml2
.parseFile('./data/fighter/'+f
)
258 # Create Fighter Object
259 fighter
= Fighter(fTree
=fTree
)
261 # Add fighter to respective party (1 or 2)
262 if fighter
.getPlayable() and newGame
:
263 self
.__battle
.getParty1().addFighter(fighter
)
265 self
.__battle
.getMonsters().append(fighter
)
267 def unloadStaticData(self
):
268 """ Clears out all monsters and EventTypes. """
270 # Clearing out monsters
271 self
.__battle
.setMonsters([])
273 # Clearing out EventTypes
274 self
.__battle
.setEventTypeList([])