Mercurial > sypikslang
annotate gamelib/gamestate.py @ 178:52cc28b429b7
Debugging
author | Neil Muller <drnlmuller@gmail.com> |
---|---|
date | Sat, 12 May 2012 11:42:43 +0200 |
parents | 4bbd4a1879f8 |
children | ed25c335fd67 |
rev | line source |
---|---|
16 | 1 # -*- coding: utf-8 -*- |
2 # vim:fileencoding=utf-8 ai ts=4 sts=4 et sw=4 | |
3 | |
4 """The actual game state object""" | |
5 | |
161
6fdc985429a7
Display research advice.
Jeremy Thurgood <firxen@gmail.com>
parents:
155
diff
changeset
|
6 from random import choice |
6fdc985429a7
Display research advice.
Jeremy Thurgood <firxen@gmail.com>
parents:
155
diff
changeset
|
7 |
45
1e8f7e694f0c
Refactor missions and sciences a bit to reduce duplication.
Jeremy Thurgood <firxen@gmail.com>
parents:
42
diff
changeset
|
8 from gamelib import missions, lab |
1e8f7e694f0c
Refactor missions and sciences a bit to reduce duplication.
Jeremy Thurgood <firxen@gmail.com>
parents:
42
diff
changeset
|
9 from gamelib.game_base import get_subclasses |
138
14917385a0fd
Better handling of mission results and turn-end messages.
Jeremy Thurgood <firxen@gmail.com>
parents:
137
diff
changeset
|
10 from gamelib.constants import M_VALS, INFO |
16 | 11 |
12 | |
13 class Game(object): | |
14 | |
22
296ce36fa7d9
Serialize and unserialize game state and missions
Neil Muller <drnlmuller@gmail.com>
parents:
16
diff
changeset
|
15 def __init__(self, init_data=None): |
16 | 16 self.money = 1000 |
96 | 17 self.milestone = "basement" |
16 | 18 # Will be updated on the next turn |
155
f4601492020b
Fix bug with starting minion count
Neil Muller <drnlmuller@gmail.com>
parents:
138
diff
changeset
|
19 self.minions = 0 |
16 | 20 self.points = 0 |
21 self.reputation = 0 | |
81
59afe9f92383
Hook up basic results screen
Neil Muller <drnlmuller@gmail.com>
parents:
80
diff
changeset
|
22 self.turn = 0 |
16 | 23 # Missions being attempted |
24 self.cur_missions = [] | |
25 # Science allocation for the current turn | |
26 self.cur_allocation = [] | |
22
296ce36fa7d9
Serialize and unserialize game state and missions
Neil Muller <drnlmuller@gmail.com>
parents:
16
diff
changeset
|
27 self.lab = None |
296ce36fa7d9
Serialize and unserialize game state and missions
Neil Muller <drnlmuller@gmail.com>
parents:
16
diff
changeset
|
28 self.missions = [] |
296ce36fa7d9
Serialize and unserialize game state and missions
Neil Muller <drnlmuller@gmail.com>
parents:
16
diff
changeset
|
29 if init_data: |
296ce36fa7d9
Serialize and unserialize game state and missions
Neil Muller <drnlmuller@gmail.com>
parents:
16
diff
changeset
|
30 self._load_data(init_data) |
296ce36fa7d9
Serialize and unserialize game state and missions
Neil Muller <drnlmuller@gmail.com>
parents:
16
diff
changeset
|
31 else: |
296ce36fa7d9
Serialize and unserialize game state and missions
Neil Muller <drnlmuller@gmail.com>
parents:
16
diff
changeset
|
32 self.lab = lab.Lab() |
296ce36fa7d9
Serialize and unserialize game state and missions
Neil Muller <drnlmuller@gmail.com>
parents:
16
diff
changeset
|
33 # instantiate all the available missions |
45
1e8f7e694f0c
Refactor missions and sciences a bit to reduce duplication.
Jeremy Thurgood <firxen@gmail.com>
parents:
42
diff
changeset
|
34 for cls in get_subclasses(missions.Mission): |
42
47c7e96cf9c8
Change initial mission creation logic to look for leaf classes
Neil Muller <drnlmuller@gmail.com>
parents:
41
diff
changeset
|
35 self.missions.append(cls()) |
16 | 36 |
37 def start_turn(self): | |
38 # Make more flexible? | |
99
531eb3239da2
Research points allocated based on milestone.
Jeremy Thurgood <firxen@gmail.com>
parents:
96
diff
changeset
|
39 self.points += 1 + M_VALS[self.milestone] * 2 |
124
685301e35f88
Add a minion cost to missions
Neil Muller <drnlmuller@gmail.com>
parents:
108
diff
changeset
|
40 self.minions += 1 + M_VALS[self.milestone] * 2 |
81
59afe9f92383
Hook up basic results screen
Neil Muller <drnlmuller@gmail.com>
parents:
80
diff
changeset
|
41 self.turn += 1 |
16 | 42 self.cur_missions = [] |
43 self.cur_allocation = [] | |
161
6fdc985429a7
Display research advice.
Jeremy Thurgood <firxen@gmail.com>
parents:
155
diff
changeset
|
44 # Suggest research |
6fdc985429a7
Display research advice.
Jeremy Thurgood <firxen@gmail.com>
parents:
155
diff
changeset
|
45 basic_research, suggestions = self.lab.suggest_research() |
6fdc985429a7
Display research advice.
Jeremy Thurgood <firxen@gmail.com>
parents:
155
diff
changeset
|
46 if basic_research: |
6fdc985429a7
Display research advice.
Jeremy Thurgood <firxen@gmail.com>
parents:
155
diff
changeset
|
47 suggestions.append(None) |
6fdc985429a7
Display research advice.
Jeremy Thurgood <firxen@gmail.com>
parents:
155
diff
changeset
|
48 self.advice = choice(suggestions) |
6fdc985429a7
Display research advice.
Jeremy Thurgood <firxen@gmail.com>
parents:
155
diff
changeset
|
49 if self.advice is None: |
6fdc985429a7
Display research advice.
Jeremy Thurgood <firxen@gmail.com>
parents:
155
diff
changeset
|
50 self.advice = ("Leave some researchers unassigned." |
6fdc985429a7
Display research advice.
Jeremy Thurgood <firxen@gmail.com>
parents:
155
diff
changeset
|
51 " They might turn up something cool.") |
6fdc985429a7
Display research advice.
Jeremy Thurgood <firxen@gmail.com>
parents:
155
diff
changeset
|
52 else: |
6fdc985429a7
Display research advice.
Jeremy Thurgood <firxen@gmail.com>
parents:
155
diff
changeset
|
53 self.advice = "Our work in %s is looking promising." % ( |
6fdc985429a7
Display research advice.
Jeremy Thurgood <firxen@gmail.com>
parents:
155
diff
changeset
|
54 self.advice.NAME,) |
16 | 55 |
56 def get_available_equipment(self): | |
57 """Return a list of equipment we can produce and afford""" | |
25 | 58 available = [x for x in self.lab.science |
49 | 59 if x.SCIENCE_TYPE == 'schematic' and x.COST <= self.money] |
16 | 60 return available |
61 | |
82
b0d97d51df51
Hook up simplistic equipment screen
Neil Muller <drnlmuller@gmail.com>
parents:
81
diff
changeset
|
62 def get_all_equipment(self): |
b0d97d51df51
Hook up simplistic equipment screen
Neil Muller <drnlmuller@gmail.com>
parents:
81
diff
changeset
|
63 """Return a list of equipment we could produce, regardless of cost""" |
b0d97d51df51
Hook up simplistic equipment screen
Neil Muller <drnlmuller@gmail.com>
parents:
81
diff
changeset
|
64 equipment = [x for x in self.lab.science |
b0d97d51df51
Hook up simplistic equipment screen
Neil Muller <drnlmuller@gmail.com>
parents:
81
diff
changeset
|
65 if x.SCIENCE_TYPE == 'schematic'] |
b0d97d51df51
Hook up simplistic equipment screen
Neil Muller <drnlmuller@gmail.com>
parents:
81
diff
changeset
|
66 return equipment |
b0d97d51df51
Hook up simplistic equipment screen
Neil Muller <drnlmuller@gmail.com>
parents:
81
diff
changeset
|
67 |
33
12c33aac7684
Gate certain missions by reputation.
Jeremy Thurgood <firxen@gmail.com>
parents:
27
diff
changeset
|
68 def get_available_missions(self): |
12c33aac7684
Gate certain missions by reputation.
Jeremy Thurgood <firxen@gmail.com>
parents:
27
diff
changeset
|
69 """Return a list of missions we can feasibly attempt""" |
41
e285b1e31a08
Add can_attempt method for future flexibility
Neil Muller <drnlmuller@gmail.com>
parents:
33
diff
changeset
|
70 available = [x for x in self.missions if x.can_attempt(self)] |
33
12c33aac7684
Gate certain missions by reputation.
Jeremy Thurgood <firxen@gmail.com>
parents:
27
diff
changeset
|
71 return available |
12c33aac7684
Gate certain missions by reputation.
Jeremy Thurgood <firxen@gmail.com>
parents:
27
diff
changeset
|
72 |
63
364ff3479ef2
Remove duplicate point tracking. Rename classes for future refactoring
Neil Muller <drnlmuller@gmail.com>
parents:
49
diff
changeset
|
73 def get_available_points(self): |
364ff3479ef2
Remove duplicate point tracking. Rename classes for future refactoring
Neil Muller <drnlmuller@gmail.com>
parents:
49
diff
changeset
|
74 return self.points - len(self.cur_allocation) |
364ff3479ef2
Remove duplicate point tracking. Rename classes for future refactoring
Neil Muller <drnlmuller@gmail.com>
parents:
49
diff
changeset
|
75 |
177
4bbd4a1879f8
Plagiarism! (Only be sure, please, to always call it research)
Neil Muller <drnlmuller@gmail.com>
parents:
161
diff
changeset
|
76 def apply_mission_special(self, new_milestone=None, new_schematic=None, |
4bbd4a1879f8
Plagiarism! (Only be sure, please, to always call it research)
Neil Muller <drnlmuller@gmail.com>
parents:
161
diff
changeset
|
77 new_science=None): |
138
14917385a0fd
Better handling of mission results and turn-end messages.
Jeremy Thurgood <firxen@gmail.com>
parents:
137
diff
changeset
|
78 if new_milestone: |
14917385a0fd
Better handling of mission results and turn-end messages.
Jeremy Thurgood <firxen@gmail.com>
parents:
137
diff
changeset
|
79 self.milestone = new_milestone |
177
4bbd4a1879f8
Plagiarism! (Only be sure, please, to always call it research)
Neil Muller <drnlmuller@gmail.com>
parents:
161
diff
changeset
|
80 if new_schematic: |
4bbd4a1879f8
Plagiarism! (Only be sure, please, to always call it research)
Neil Muller <drnlmuller@gmail.com>
parents:
161
diff
changeset
|
81 self.lab.steal_science(new_schematic) |
4bbd4a1879f8
Plagiarism! (Only be sure, please, to always call it research)
Neil Muller <drnlmuller@gmail.com>
parents:
161
diff
changeset
|
82 if new_science: |
4bbd4a1879f8
Plagiarism! (Only be sure, please, to always call it research)
Neil Muller <drnlmuller@gmail.com>
parents:
161
diff
changeset
|
83 self.lab.steal_science(new_science) |
138
14917385a0fd
Better handling of mission results and turn-end messages.
Jeremy Thurgood <firxen@gmail.com>
parents:
137
diff
changeset
|
84 |
16 | 85 def end_turn(self): |
86 # Attempt the missions | |
87 mission_results = [] | |
88 for mission, equipment in self.cur_missions: | |
137
fb8037bc22f1
More flexible (and less boilerplatey) mission stuff.
Jeremy Thurgood <firxen@gmail.com>
parents:
135
diff
changeset
|
89 mission_results.append(mission.attempt_mission(equipment, self)) |
135
8d1cf98e39b5
Reputation decays over time
Neil Muller <drnlmuller@gmail.com>
parents:
131
diff
changeset
|
90 if not self.cur_missions and self.reputation > 0: |
8d1cf98e39b5
Reputation decays over time
Neil Muller <drnlmuller@gmail.com>
parents:
131
diff
changeset
|
91 # If you're not doing stuff, you're not in the news |
8d1cf98e39b5
Reputation decays over time
Neil Muller <drnlmuller@gmail.com>
parents:
131
diff
changeset
|
92 self.reputation -= M_VALS[self.milestone] |
16 | 93 # Do the science |
94 self.points -= len(self.cur_allocation) | |
95 if self.points < 0: | |
96 raise RuntimeError('Spent too many points') | |
97 # Process mission results | |
80
a40a76012bd7
Refactor message handling so we can deal with in the gui
Neil Muller <drnlmuller@gmail.com>
parents:
63
diff
changeset
|
98 messages = [] |
16 | 99 for result in mission_results: |
100 result.apply(self) | |
138
14917385a0fd
Better handling of mission results and turn-end messages.
Jeremy Thurgood <firxen@gmail.com>
parents:
137
diff
changeset
|
101 messages.append((result.outcome, result.text, result.loot)) |
178 | 102 # Missions may give us science breakthroughs, so handle this after |
103 # mission results | |
104 new_stuff = self.lab.spend_points(self.cur_allocation, self.points) | |
105 self.points = 0 | |
106 self.minions = 0 | |
16 | 107 for science in new_stuff: |
23
f6a3b213857b
Fix up some game state logic, add very basic REPL game interface.
Jeremy Thurgood <firxen@gmail.com>
parents:
22
diff
changeset
|
108 # FIXME: Update UI better. |
138
14917385a0fd
Better handling of mission results and turn-end messages.
Jeremy Thurgood <firxen@gmail.com>
parents:
137
diff
changeset
|
109 messages.append((INFO, "SCIENCE breakthrough!", { |
14917385a0fd
Better handling of mission results and turn-end messages.
Jeremy Thurgood <firxen@gmail.com>
parents:
137
diff
changeset
|
110 science.SCIENCE_TYPE: science})) |
80
a40a76012bd7
Refactor message handling so we can deal with in the gui
Neil Muller <drnlmuller@gmail.com>
parents:
63
diff
changeset
|
111 return messages |
22
296ce36fa7d9
Serialize and unserialize game state and missions
Neil Muller <drnlmuller@gmail.com>
parents:
16
diff
changeset
|
112 |
296ce36fa7d9
Serialize and unserialize game state and missions
Neil Muller <drnlmuller@gmail.com>
parents:
16
diff
changeset
|
113 def save_data(self): |
296ce36fa7d9
Serialize and unserialize game state and missions
Neil Muller <drnlmuller@gmail.com>
parents:
16
diff
changeset
|
114 """Serialize the game state into a dict""" |
296ce36fa7d9
Serialize and unserialize game state and missions
Neil Muller <drnlmuller@gmail.com>
parents:
16
diff
changeset
|
115 data = {} |
296ce36fa7d9
Serialize and unserialize game state and missions
Neil Muller <drnlmuller@gmail.com>
parents:
16
diff
changeset
|
116 data['money'] = self.money |
124
685301e35f88
Add a minion cost to missions
Neil Muller <drnlmuller@gmail.com>
parents:
108
diff
changeset
|
117 data['minions'] = self.minions |
22
296ce36fa7d9
Serialize and unserialize game state and missions
Neil Muller <drnlmuller@gmail.com>
parents:
16
diff
changeset
|
118 data['reputation'] = self.reputation |
96 | 119 data['milestone'] = self.milestone |
22
296ce36fa7d9
Serialize and unserialize game state and missions
Neil Muller <drnlmuller@gmail.com>
parents:
16
diff
changeset
|
120 data['points'] = self.points |
87
d93e1ea2bd0d
Fix method name assumption bug
Neil Muller <drnlmuller@gmail.com>
parents:
82
diff
changeset
|
121 data['lab'] = self.lab.save_data() |
108 | 122 data['turn'] = self.turn |
22
296ce36fa7d9
Serialize and unserialize game state and missions
Neil Muller <drnlmuller@gmail.com>
parents:
16
diff
changeset
|
123 # Save mission state |
296ce36fa7d9
Serialize and unserialize game state and missions
Neil Muller <drnlmuller@gmail.com>
parents:
16
diff
changeset
|
124 data['missions'] = {} |
296ce36fa7d9
Serialize and unserialize game state and missions
Neil Muller <drnlmuller@gmail.com>
parents:
16
diff
changeset
|
125 for mission in self.missions: |
296ce36fa7d9
Serialize and unserialize game state and missions
Neil Muller <drnlmuller@gmail.com>
parents:
16
diff
changeset
|
126 miss_name = type(mission).__name__ |
296ce36fa7d9
Serialize and unserialize game state and missions
Neil Muller <drnlmuller@gmail.com>
parents:
16
diff
changeset
|
127 data['missions'][miss_name] = mission.save_data() |
296ce36fa7d9
Serialize and unserialize game state and missions
Neil Muller <drnlmuller@gmail.com>
parents:
16
diff
changeset
|
128 return data |
296ce36fa7d9
Serialize and unserialize game state and missions
Neil Muller <drnlmuller@gmail.com>
parents:
16
diff
changeset
|
129 |
296ce36fa7d9
Serialize and unserialize game state and missions
Neil Muller <drnlmuller@gmail.com>
parents:
16
diff
changeset
|
130 def _load_data(self, data): |
296ce36fa7d9
Serialize and unserialize game state and missions
Neil Muller <drnlmuller@gmail.com>
parents:
16
diff
changeset
|
131 """Restore the game state""" |
296ce36fa7d9
Serialize and unserialize game state and missions
Neil Muller <drnlmuller@gmail.com>
parents:
16
diff
changeset
|
132 self.money = data['money'] |
124
685301e35f88
Add a minion cost to missions
Neil Muller <drnlmuller@gmail.com>
parents:
108
diff
changeset
|
133 self.minions = data['minions'] |
22
296ce36fa7d9
Serialize and unserialize game state and missions
Neil Muller <drnlmuller@gmail.com>
parents:
16
diff
changeset
|
134 self.reputation = data['reputation'] |
296ce36fa7d9
Serialize and unserialize game state and missions
Neil Muller <drnlmuller@gmail.com>
parents:
16
diff
changeset
|
135 self.points = data['points'] |
108 | 136 self.turn = data['turn'] |
131 | 137 self.milestone = data['milestone'] |
22
296ce36fa7d9
Serialize and unserialize game state and missions
Neil Muller <drnlmuller@gmail.com>
parents:
16
diff
changeset
|
138 self.lab = lab.Lab(data['lab']) |
296ce36fa7d9
Serialize and unserialize game state and missions
Neil Muller <drnlmuller@gmail.com>
parents:
16
diff
changeset
|
139 for mis_class in missions.Mission.__subclasses__(): |
296ce36fa7d9
Serialize and unserialize game state and missions
Neil Muller <drnlmuller@gmail.com>
parents:
16
diff
changeset
|
140 miss_name = mis_class.__name__ |
296ce36fa7d9
Serialize and unserialize game state and missions
Neil Muller <drnlmuller@gmail.com>
parents:
16
diff
changeset
|
141 if miss_name in data['missions']: |
296ce36fa7d9
Serialize and unserialize game state and missions
Neil Muller <drnlmuller@gmail.com>
parents:
16
diff
changeset
|
142 self.missions.append(mis_class(data['missions'][miss_name])) |