root/src/gameData.py @ 454

Revision 454, 4.2 KB (checked in by jelinson, 2 years ago)

fixed bug in heuristic and prioritizing events

Line 
1'''
2Created on Mar 24, 2012
3
4@author: Michael
5'''
6
7import copy
8
9from stateAnalysis import StateAnalysis
10from event import Event
11from globals import *
12
13
14class GameData:
15    '''
16        holds all of the game data for our game.
17    '''
18
19    def __init__(self, state):
20        '''
21            initializes based on a given state.
22        '''
23        self.oldState = copy.deepcopy(state)
24        self.currentState = state
25        self.stateAnalysis = StateAnalysis()
26        self.sceneQueue = []
27        self.enqueueScene('intro')
28
29    def endTurn(self):
30        '''
31            ends the turn, updating states as necessary.
32
33            input type: none
34            return type: none
35        '''
36        change = self.oldState.calcTurnChange()
37        self.currentState.updateSpec(change)
38
39        buildSceneText = ""
40        for block in self.currentState.buildingsToAdd:
41            for building in block:
42                buildSceneText += building.name
43        self.currentState.endTurn()
44        self.oldState = copy.deepcopy(self.currentState)
45
46        self.stateAnalysis.analyze(self.oldState)
47        event = self.stateAnalysis.popAndFlushEvent()
48        if event is not None:
49            self.handleEvent(event)
50           
51        if buildSceneText != "":
52            self.enqueueScene("build", buildSceneText)
53
54    def handleEvent(self, event):
55        self.oldState.specs += event.specChange
56        for building in event.buildingsToDelete:
57            if not self.oldState.deleteFirstBuilding(building):
58                print "Error in event: tried to delete %s, which wasn't present" % building
59        self.enqueueScene(event.sceneName, "")
60
61    def getMoney(self):
62        """
63            gets the current level of money.
64
65            input type: none
66            return type: int
67        """
68        return self.currentState.getMoney()
69
70    def undoTurn(self):
71        """
72            undoes all buildings and actions for this turn.
73
74            input type: none
75            return type: none
76        """
77        self.currentState = copy.deepcopy(self.oldState)
78        self.clearScene()
79
80    def deleteBuilding(self, blockNumber, indexNumber):
81        """
82            deletes the selected building from the map.
83
84            input type: blockNumber(int), indexNumber(int)
85            return type: none
86        """
87        self.currentState.deleteBuilding(blockNumber, indexNumber)
88
89    def getBlockBuildings(self, blockNumber):
90        """
91            returns the images associated with the buildings that exist in the
92            designated block.
93
94            input type: blockNumber(int)
95            return type: none
96        """
97        return self.currentState.getBlockBuildings(blockNumber)
98
99    def blockHasSpace(self, blockNumber):
100        """
101            returns whether the designated block has space for more buildings.
102
103            input type: blockNumber(int)
104            return type: boolean
105        """
106        return self.currentState.blockHasSpace(blockNumber)
107
108    def build(self, building, blockNumber):
109        """
110            builds the building. Assumes there are enough stats and space on
111            the designated block.
112
113            input type: building(str), blockNumber(int)
114            return type: none
115        """
116        self.currentState.addBuilding(building, blockNumber)
117
118    def research(self, researchType, value):
119        """
120            adds research to the designated type.
121
122            input type: type(int), value(int)
123            return type: none
124        """
125        self.currentState.research(researchType, value)
126
127    def getSpec(self):
128        """
129            returns the spec containing values of current resources.
130
131            input type: none
132            return type: spec
133        """
134        return self.currentState.getSpec()
135
136    def enqueueScene(self, sceneName, auxText=""):
137        self.sceneQueue.append((sceneName, auxText))
138
139    def dequeueScene(self):
140        if len(self.sceneQueue) != 0:
141            return self.sceneQueue.pop(0)
142        else:
143            return (None, None)
144
145    def clearScene(self):
146        self.sceneQueue = []
Note: See TracBrowser for help on using the browser.