root/src/state.py @ 300

Revision 300, 7.9 KB (checked in by ecoughlan, 2 years ago)

Finished editing style in files

Line 
1'''
2Created on Feb 18, 2012
3
4@author: TechTrek
5
6Stores the current game conditions.
7'''
8
9from spec import Spec
10from buildables import *
11from window import Window
12from buildables import buildingLUT
13from globals import *
14
15
16class State():
17    """ keeps track of stats, buildings that have been built and
18        research values""" 
19    def __init__(self, newSpec):
20        """
21            initializes a state object with the given spec and no buildings
22            or research
23
24            input type: Spec(newSpec)
25        """
26        self.specs = newSpec
27        self.currentBuildings = []
28        self.buildingsToAdd = []
29        self.currentResearch = [0, 0, 0]
30        for i in range(START_BLOCKS):
31            self.currentBuildings.append([])
32            self.buildingsToAdd.append([])
33        self.buildingLUT = buildingLUT
34
35    def checkThresh(self, building):
36        """
37            checks the thresholds of the given building to see if it can
38            be built
39
40            input type: Building(str)
41            return type: Bool
42        """
43        stats = self.buildingLUT[building].getStats()
44        thresholds = self.buildingLUT[building].getThreshold()
45        if stats.oneTimeSpecs.getMoney() <= self.specs.getMoney():
46            if stats.oneTimeSpecs.getMaterial() <= self.specs.getMaterial():
47                # check research thresholds
48                if thresholds[0] <= self.currentResearch[0] and \
49                thresholds[1] <= self.currentResearch[1] and \
50                thresholds[2] <= self.currentResearch[2]:
51                    return True
52        return False
53
54    # ONE DOES NOT SIMPLY ADD A BUILDING!!!
55    def addBuilding(self, building, blockNumber):
56        """
57            adds a building to current buildings and updates stats
58
59            input type: Building(str)
60            return type: None
61        """
62        self.buildingsToAdd[blockNumber].append(building)
63        buildingSpec = building.getStats().getOneTime()
64        self.updateSpec(buildingSpec)
65        if DEBUG == 0 or DEBUG == 1 or DEBUG == 2:
66            print "Added:", building,
67            print " Current buildings: ", self.currentBuildings
68
69    def deleteBuilding(self, blockNumber, indexNumber):
70        """
71            removes a building from the current buildings
72
73            input type: Building(building)
74            return type: None
75        """
76        self.currentBuildings[blockNumber].pop(indexNumber)
77
78    def research(self, researchType, amount):
79        """
80            adjusts money based on research
81
82            input type: int(researchType), int(amount)
83            return type: None
84        """
85        change = Spec(amount, 0, 0, 0, 0)
86        self.specs -= change
87        self.currentResearch[researchType] += amount
88
89#    def getResearchValues(self):
90#        """
91#            gets the research values for each category
92#
93#            return type: [power,transportation,material]
94#        """
95#        return self.currentResearch
96
97#    def getBuildings(self):
98#        """
99#            gets the current buildings
100#
101#            return type: [Buildings]
102#        """
103#        return self.currentBuildings
104
105    def calcTurnChange(self):
106        """
107            returns the updated states for the end of a turn/week
108
109            return type: Spec(spec)
110        """
111        spec = Spec(END_TURN_MONEY, 0, 0, 0, 0)
112        for block in self.currentBuildings:
113            for building in block:
114                spec += building.getStats().getCont()
115        return spec
116
117    def updateSpec(self, changeSpec):
118        """
119            updates spec with the given change
120
121            input type: Spec(changeSpec)
122            return type: None
123        """
124        self.specs += changeSpec
125
126    def getMoney(self):
127        """
128            gets money
129
130            return type: int
131        """
132        return self.specs.getMoney()
133
134    def displayStats(self):
135        """
136            gets the stat bar for specs
137
138            input type: None
139            return type: Window(statbars)
140        """
141        return self.specs.displayStats()
142
143#    def displayDetailedStat(self, statID):
144#        """
145#            creates a description of the stat
146#
147#            input type: str(statID)
148#            return type: Window(stat)
149#        """
150#        stat = Window('', 600, 480)
151#        stat.setDefaultResponse(Response('windows', [], True, True))
152#        details = WindowEntry(Response('windows', [], True, True))
153#        details.addRectangle(Rectangle((40, 50), STAT_W - 80, 70, MAUVE))
154#        statDescription = statID.capitalize() + ": "
155#        statDescription += str(self.specs.stringToValue(statID)) + " - "
156#        # this text will eventually be dynamic
157#        statDescription = statDescription + "To improve this stat, "
158#        statDescription += "try researching in something intelligent."
159#        details.addText(Text((40, 50), statDescription,
160#                             MD_TEXT, WHITE, STAT_W - 80))
161#
162#        stat.addEntry(details)
163#        return stat
164
165    def checkLoseConditions(self):
166        """
167            check if lose conditions have been passed
168
169            return type: [Bool,Bool,Bool,Bool]
170        """
171        # [alert pollution, alert food, end poll, end food]
172        warnings = [False, False, False, False]
173        if self.specs.getPollution() > 150:
174            warnings[0] = True
175        if self.specs.getSpace() < 30:
176            warnings[1] = True
177        if self.specs.getPollution() > 200:
178            warnings[2] = True
179        if self.specs.getSpace() < 0:
180            warnings[3] = True
181        return warnings
182
183    def checkWinConditions(self):
184        """
185            check if win condition has been meet
186
187            return type: Bool
188        """
189        for block in self.currentBuildings:
190            if 'spaceship' in block:
191                if DEBUG == 0 or DEBUG == 2:
192                    print "I win the game"
193                return True
194        return False
195
196    def __repr__(self):
197        """
198            display state as a string
199        """
200        output = ""
201        output += "Buildings: "
202        for block in self.currentBuildings:
203            for i in block:
204                output += i + ", "
205        output += "\n Specs: "
206
207        for i in self.specs.getAllStats():
208            statString = i[0] + ': ' + str(i[1]) + ', '
209            output += statString
210        return output
211
212    def getBlockBuildings(self, blockNumber):
213        """
214            returns the images associated with the buildings that exist in the
215            designated block.
216
217            input type: blockNumber(int)
218            return type: none
219        """
220        images = []
221        for building in self.currentBuildings[blockNumber]:
222            images.append(pygame.image.load(os.path.abspath(iconDir + self.buildingLUT[building.name].imageFileName)))
223        return images
224
225    def endTurn(self):
226        """
227            ends the turn, updating the states as necessary.
228
229            input type: none
230            return type: none
231        """
232        for i in range(len(self.buildingsToAdd)):
233            self.currentBuildings[i] += self.buildingsToAdd[i]
234        self.buildingsToAdd = []
235        for i in range(len(self.currentBuildings)):
236            self.buildingsToAdd.append([])
237
238    def getSpec(self):
239        """
240            returns the spec containing values of current resources.
241
242            input type: none
243            return type: spec
244        """
245        return self.specs.getAllStats()
246
247    def blockHasSpace(self, blockNumber):
248        """
249            returns whether the designated block has space for a building.
250
251            input type: blockNumber(int)
252            return type: boolean
253        """
254        if len(self.currentBuildings[blockNumber]) +\
255        len(self.buildingsToAdd[blockNumber]) < MAX_BUILDINGS:
256            return True
257        else:
258            return False
Note: See TracBrowser for help on using the browser.