root/src/state.py @ 302

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

More updates

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