root/src/state.py @ 285

Revision 285, 8.0 KB (checked in by mmorton, 2 years ago)

build menus working in full, build button broken

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