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)
92 choice
= self
.getMenuCommand()
95 self
.unloadStaticData()
96 self
.loadStaticData(True)
99 print "Path to Save File: ",
100 path
= sys
.stdin
.readline()
101 self
.load("" + path
+ "")
103 print "============================================================="
106 print "Path to Save File: ",
107 path
= sys
.stdin
.readline()
108 self
.save("" + path
+ "")
110 print "============================================================="
113 self
.createRandomBattle()
114 print "============================================================="
118 print "Battle Starts."
119 self
.__view
.printState()
121 # initialize the event checker boolean
124 while not self
.__battle
.isOver():
126 wereEvents
= self
.__battle
.nextTurn()
128 # only print out state if something actually happened
130 self
.__view
.printState()
132 if self
.__battle
.getParty1().isAlive():
133 # sum exp from enemy party
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!"
148 print "Game Over. Asgard mourns."
150 def load(self
,savefile
):
151 #clear party1 and it's fighters
152 self
.__battle
.setParty1(Party("Heroes"))
155 fptr
= open(savefile
,"rb")
162 # number of bytes read in so far
165 while readin
!= size
:
166 #read in struct descriptor
167 type = ord(fptr
.read(1))
171 fstring
= fptr
.read(struct
.calcsize("20s20s20s20s20s20sB7I6B"))
172 readin
+= struct
.calcsize("20s20s20s20s20s20sB7I6B")
175 fighter
= Fighter(binary
=fstring
)
177 self
.__battle
.getParty1().addFighter(fighter
)
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")
190 # Convert fighter f to binary string
191 binStr
= f
.toBinaryString()
193 # Write binary string to savefile
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([])
222 etTree
= libxml2
.parseFile('./data/eventtypes/'+et
)
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/')
235 # f cannot be a "." file
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
)
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([])