(no commit message)
[asgard.git] / battle.py
blob35bfafdc6f413571aa71157148c91f782dc382cb
1 ########################################################
2 # Copyright (c) 2006 Russ Adams, Sean Eubanks, Asgard Contributors
3 # This file is part of Asgard.
4 # Asgard is free software; you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation; either version 2 of the License, or
7 # (at your option) any later version.
9 # Asgard is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
14 # You should have received a copy of the GNU General Public License
15 # along with Asgard; if not, write to the Free Software
16 # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17 ########################################################
19 from os import listdir
20 from time import *
21 from fighter import *
22 from party import *
23 from event import *
25 class Battle:
27 def __init__(self):
28 self.__currentTime = 0
31 ### Creating parties ###
32 # Reading data for parties
33 dir = listdir('./data/party/')
34 for p in dir:
36 if p[0] == ".":
37 continue
39 p = './data/party/'+p
40 fptr = open(p,'r')
41 lines = fptr.readlines()
42 if int(lines[0]) == 1:
43 self.__party1 = Party(lines[1].strip())
44 elif int(lines[0]) == 2:
45 self.__party2 = Party(lines[1].strip())
46 fptr.close()
48 ### Creating fighters ###
49 # Reading data for fighters
50 dir = listdir('./data/fighter/')
51 for f in dir:
52 # f cannot be a "." file
53 if f[0] == '.':
54 continue
55 # Change file into pathname
56 f = './data/fighter/'+f
57 # Open file f for 'r'eading
58 fptr = open(f,'r')
59 # Place lines in file f into list
60 lines = fptr.readlines()
62 tom = round((1/float(lines[4]))*100)
63 equip = Equiption(lines[6].strip(),lines[7].strip(),lines[8].strip(),lines[9].strip())
64 # Create fighter(name,hp,str,dex,head,l,r,body)
65 fighter1 = Fighter(lines[0].strip(),int(lines[2]),int(lines[3]),int(lines[4]),tom,bool(int(lines[5])),equip)
68 # Add fighter to respective party (1 or 2)
69 if int(lines[10]) == 1:
70 self.__party1.addFighter(fighter1)
71 elif int(lines[10]) == 2:
72 self.__party2.addFighter(fighter1)
73 # Close file f
74 fptr.close()
76 def isOver(self):
77 """ Is battle over?"""
79 return not self.__party1.isAlive() or not self.__party2.isAlive()
81 def nextTurn(self):
82 """ Execute next iteration in gameloop."""
84 sleep(.1)
86 wereEvents = False
88 # execute all available events
89 while self.executeNextEvent():
90 # if there was at least one event
91 wereEvents = True
92 continue
94 self.__currentTime += 1
96 return wereEvents
98 def executeNextEvent(self):
99 """ Execute next event in event queue."""
101 if self.isOver():
102 return False
104 # Try to get an event from party1
105 event = self.__party1.getNextEvent(self.__party2,self.__currentTime,self.__controller)
107 # If there was one, execute and return true
108 if event != None:
109 self.__view.printEvent(event)
110 event.execute()
111 return True
113 # Try to get an event from party2
114 event = self.__party2.getNextEvent(self.__party1,self.__currentTime,self.__controller)
116 # If there was one, execute and return true
117 if event != None:
118 self.__view.printEvent(event)
119 event.execute()
120 return True
122 # Otherwise, notify that there are no more events at this time.
123 return False
125 def getCurrentTime(self):
126 """ Retrieve current time."""
127 return self.__currentTime
129 def setCurrentTime(self,t):
130 """ Establish current time."""
131 self.__currentTime = t
133 def getParty1(self):
134 """ Retrieve party1."""
135 return self.__party1
137 def getParty2(self):
138 """ Retrieve party2."""
139 return self.__party2
141 def setView(self,v):
142 """ Establish view."""
143 self.__view = v
145 def setController(self,c):
146 """ Establish controller."""
147 self.__controller = c