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 # For Attack and Magic
54 if t
.getTargetSelection() == True:
55 self
.__view
.printTargetSelectMenu(vp
,hp
)
58 if vp
.sizeOfParty() - int(n
) >= 0:
59 d
= vp
.getFighter(int(n
)-1)
62 d
= hp
.getFighter(int(n
)-vp
.sizeOfParty()-1)
69 def getMenuCommand(self
):
71 self
.__view
.printStartupMenu()
84 """Retrieves events from Parties and Executes all Events in queue per iteration"""
86 print "Asgard Free Software RPG - Testing Module"
88 self
.loadStaticData(False)
93 choice
= self
.getMenuCommand()
96 self
.unloadStaticData()
97 self
.loadStaticData(True)
101 print "Path to Save File: ",
102 path
= sys
.stdin
.readline()
103 self
.load("" + path
.strip() + "")
107 print "Path to Save File: ",
108 path
= sys
.stdin
.readline()
109 self
.save("" + path
.strip() + "")
113 if self
.__battle
.getParty1() == None:
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
.getRunFlag():
140 print "Gotta get outta here!"
142 print "Hand's party runs away."
144 print "Battle's over. You've escaped."
146 self
.__battle
.setRunFlag(False)
148 elif self
.__battle
.getParty1().isAlive():
149 # sum exp from enemy party
151 for f
in self
.__battle
.getParty2().getFighters():
152 exp
+= f
.getStat("exp").getMax()
154 # hero party gets exp
155 self
.__battle
.getParty1().giveExp(exp
)
158 print "Final Results"
159 print "============================================================="
160 self
.__view
.printParty(self
.__battle
.getParty1())
161 print "============================================================="
162 print "Victory Fanfare. Hand's party wins "+str(exp
)+" exp!"
164 for f
in self
.__battle
.getParty1().getFighters():
165 # remove all statuses
166 for s
in f
.getStatus():
168 f
.getStat("mp").setCurrent(f
.getStat("mp").getMax())
171 print "Game Over. Asgard mourns."
173 # clear out the hero party - they're dead!
174 self
.__battle
.setParty1(None)
177 def load(self
,savefile
):
178 #clear party1 and it's fighters
179 self
.__battle
.setParty1(Party("Heroes"))
182 fptr
= open(savefile
,"rb")
189 # number of bytes read in so far
192 while readin
!= size
:
193 #read in struct descriptor
195 tstring
= fptr
.read(struct
.calcsize("!B"))
196 [type] = struct
.unpack("!B",tstring
)
197 readin
+= struct
.calcsize("!B")
201 fstring
= fptr
.read(struct
.calcsize("!20s20s20s20s20s20sB7I6B"))
202 readin
+= struct
.calcsize("!20s20s20s20s20s20sB7I6B")
207 fighter
= Fighter(binary
=fstring
)
208 self
.__battle
.getParty1().addFighter(fighter
)
213 def save(self
,savefile
):
214 """ Writes all playable fighters to savefile. """
215 # Retrieving playable fighters
216 fighters
= self
.__battle
.getParty1().getFighters()
218 # Writing each fighter to savefile
219 fptr
= open(savefile
,"wb")
222 # Convert fighter f to binary string
223 binStr
= f
.toBinaryString()
225 # Write binary string to savefile
226 fptr
.write(binStr
.strip())
230 def createRandomBattle(self
):
231 self
.__battle
.setParty2(Party("Villains"))
233 partySize
+= random
.randint(-1,2)
234 for x
in range(0, partySize
):
235 monstI
= random
.randint(0,len(self
.__battle
.getMonsters())-1)
237 monster
= copy
.deepcopy(self
.__battle
.getMonsters()[monstI
])
238 self
.__battle
.getParty2().addFighter(monster
)
240 for f
in self
.__battle
.getParty1().getFighters():
241 spd
= f
.getStat("spd").getCurrent()
242 tom
= round((1/float(spd
)*100))
243 f
.getStat("tom").setCurrent(tom
)
244 f
.getStat("tom").setMax(tom
)
245 f
.getStat("canMove").setCurrent(1)
246 f
.getStat("ai").setCurrent(0)
248 self
.__battle
.setCurrentTime(0)
250 def loadStaticData(self
,newGame
):
251 """Load static game data: eventtypes, fighters (with stats and jobs)"""
254 dir = listdir('./data/eventtypes/')
256 self
.__battle
.setEventTypeList([])
263 etTree
= libxml2
.parseFile('./data/eventtypes/'+et
)
266 eventType
= EventType(etTree
)
267 self
.__battle
.getEventTypeList().append(eventType
)
270 self
.__battle
.setParty1(Party("Heroes"))
272 ### Loading fighters ###
273 dir = listdir('./data/fighter/')
276 # f cannot be a "." file
280 # Create Fighter XML Tree
281 fTree
= libxml2
.parseFile('./data/fighter/'+f
)
283 # Create Fighter Object
284 fighter
= Fighter(fTree
=fTree
)
286 # Add fighter to respective party (1 or 2)
287 if fighter
.getPlayable() and newGame
:
288 self
.__battle
.getParty1().addFighter(fighter
)
289 elif not fighter
.getPlayable():
290 self
.__battle
.getMonsters().append(fighter
)
292 def unloadStaticData(self
):
293 """ Clears out all monsters and EventTypes. """
295 # Clearing out monsters
296 self
.__battle
.setMonsters([])
298 # Clearing out EventTypes
299 self
.__battle
.setEventTypeList([])