Mercurial > sypikslang
annotate gamelib/gamestate.py @ 267:a534629f490f default tip
Fix urls
author | Neil Muller <drnlmuller@gmail.com> |
---|---|
date | Tue, 17 Mar 2020 22:39:54 +0200 |
parents | e4f9513c9dd0 |
children |
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 |
207 | 23 self.income = 0 |
16 | 24 # Missions being attempted |
25 self.cur_missions = [] | |
26 # Science allocation for the current turn | |
27 self.cur_allocation = [] | |
22
296ce36fa7d9
Serialize and unserialize game state and missions
Neil Muller <drnlmuller@gmail.com>
parents:
16
diff
changeset
|
28 self.lab = None |
296ce36fa7d9
Serialize and unserialize game state and missions
Neil Muller <drnlmuller@gmail.com>
parents:
16
diff
changeset
|
29 self.missions = [] |
296ce36fa7d9
Serialize and unserialize game state and missions
Neil Muller <drnlmuller@gmail.com>
parents:
16
diff
changeset
|
30 if init_data: |
296ce36fa7d9
Serialize and unserialize game state and missions
Neil Muller <drnlmuller@gmail.com>
parents:
16
diff
changeset
|
31 self._load_data(init_data) |
296ce36fa7d9
Serialize and unserialize game state and missions
Neil Muller <drnlmuller@gmail.com>
parents:
16
diff
changeset
|
32 else: |
296ce36fa7d9
Serialize and unserialize game state and missions
Neil Muller <drnlmuller@gmail.com>
parents:
16
diff
changeset
|
33 self.lab = lab.Lab() |
296ce36fa7d9
Serialize and unserialize game state and missions
Neil Muller <drnlmuller@gmail.com>
parents:
16
diff
changeset
|
34 # 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
|
35 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
|
36 self.missions.append(cls()) |
16 | 37 |
38 def start_turn(self): | |
39 # Make more flexible? | |
99
531eb3239da2
Research points allocated based on milestone.
Jeremy Thurgood <firxen@gmail.com>
parents:
96
diff
changeset
|
40 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
|
41 self.minions += 1 + M_VALS[self.milestone] * 2 |
81
59afe9f92383
Hook up basic results screen
Neil Muller <drnlmuller@gmail.com>
parents:
80
diff
changeset
|
42 self.turn += 1 |
207 | 43 self.money += self.income |
16 | 44 self.cur_missions = [] |
45 self.cur_allocation = [] | |
161
6fdc985429a7
Display research advice.
Jeremy Thurgood <firxen@gmail.com>
parents:
155
diff
changeset
|
46 # Suggest research |
6fdc985429a7
Display research advice.
Jeremy Thurgood <firxen@gmail.com>
parents:
155
diff
changeset
|
47 basic_research, suggestions = self.lab.suggest_research() |
6fdc985429a7
Display research advice.
Jeremy Thurgood <firxen@gmail.com>
parents:
155
diff
changeset
|
48 if basic_research: |
6fdc985429a7
Display research advice.
Jeremy Thurgood <firxen@gmail.com>
parents:
155
diff
changeset
|
49 suggestions.append(None) |
221
bf92c982996e
Don't crash when we run out of suggestions
Neil Muller <drnlmuller@gmail.com>
parents:
207
diff
changeset
|
50 try: |
bf92c982996e
Don't crash when we run out of suggestions
Neil Muller <drnlmuller@gmail.com>
parents:
207
diff
changeset
|
51 self.advice = choice(suggestions) |
bf92c982996e
Don't crash when we run out of suggestions
Neil Muller <drnlmuller@gmail.com>
parents:
207
diff
changeset
|
52 except IndexError: |
bf92c982996e
Don't crash when we run out of suggestions
Neil Muller <drnlmuller@gmail.com>
parents:
207
diff
changeset
|
53 # We have an empty list of suggestions |
bf92c982996e
Don't crash when we run out of suggestions
Neil Muller <drnlmuller@gmail.com>
parents:
207
diff
changeset
|
54 self.advice = None |
161
6fdc985429a7
Display research advice.
Jeremy Thurgood <firxen@gmail.com>
parents:
155
diff
changeset
|
55 if self.advice is None: |
6fdc985429a7
Display research advice.
Jeremy Thurgood <firxen@gmail.com>
parents:
155
diff
changeset
|
56 self.advice = ("Leave some researchers unassigned." |
6fdc985429a7
Display research advice.
Jeremy Thurgood <firxen@gmail.com>
parents:
155
diff
changeset
|
57 " They might turn up something cool.") |
6fdc985429a7
Display research advice.
Jeremy Thurgood <firxen@gmail.com>
parents:
155
diff
changeset
|
58 else: |
6fdc985429a7
Display research advice.
Jeremy Thurgood <firxen@gmail.com>
parents:
155
diff
changeset
|
59 self.advice = "Our work in %s is looking promising." % ( |
6fdc985429a7
Display research advice.
Jeremy Thurgood <firxen@gmail.com>
parents:
155
diff
changeset
|
60 self.advice.NAME,) |
16 | 61 |
62 def get_available_equipment(self): | |
63 """Return a list of equipment we can produce and afford""" | |
25 | 64 available = [x for x in self.lab.science |
49 | 65 if x.SCIENCE_TYPE == 'schematic' and x.COST <= self.money] |
16 | 66 return available |
67 | |
82
b0d97d51df51
Hook up simplistic equipment screen
Neil Muller <drnlmuller@gmail.com>
parents:
81
diff
changeset
|
68 def get_all_equipment(self): |
b0d97d51df51
Hook up simplistic equipment screen
Neil Muller <drnlmuller@gmail.com>
parents:
81
diff
changeset
|
69 """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
|
70 equipment = [x for x in self.lab.science |
b0d97d51df51
Hook up simplistic equipment screen
Neil Muller <drnlmuller@gmail.com>
parents:
81
diff
changeset
|
71 if x.SCIENCE_TYPE == 'schematic'] |
b0d97d51df51
Hook up simplistic equipment screen
Neil Muller <drnlmuller@gmail.com>
parents:
81
diff
changeset
|
72 return equipment |
b0d97d51df51
Hook up simplistic equipment screen
Neil Muller <drnlmuller@gmail.com>
parents:
81
diff
changeset
|
73 |
33
12c33aac7684
Gate certain missions by reputation.
Jeremy Thurgood <firxen@gmail.com>
parents:
27
diff
changeset
|
74 def get_available_missions(self): |
12c33aac7684
Gate certain missions by reputation.
Jeremy Thurgood <firxen@gmail.com>
parents:
27
diff
changeset
|
75 """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
|
76 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
|
77 return available |
12c33aac7684
Gate certain missions by reputation.
Jeremy Thurgood <firxen@gmail.com>
parents:
27
diff
changeset
|
78 |
63
364ff3479ef2
Remove duplicate point tracking. Rename classes for future refactoring
Neil Muller <drnlmuller@gmail.com>
parents:
49
diff
changeset
|
79 def get_available_points(self): |
364ff3479ef2
Remove duplicate point tracking. Rename classes for future refactoring
Neil Muller <drnlmuller@gmail.com>
parents:
49
diff
changeset
|
80 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
|
81 |
177
4bbd4a1879f8
Plagiarism! (Only be sure, please, to always call it research)
Neil Muller <drnlmuller@gmail.com>
parents:
161
diff
changeset
|
82 def apply_mission_special(self, new_milestone=None, new_schematic=None, |
207 | 83 new_science=None, income=None): |
138
14917385a0fd
Better handling of mission results and turn-end messages.
Jeremy Thurgood <firxen@gmail.com>
parents:
137
diff
changeset
|
84 if new_milestone: |
14917385a0fd
Better handling of mission results and turn-end messages.
Jeremy Thurgood <firxen@gmail.com>
parents:
137
diff
changeset
|
85 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
|
86 if new_schematic: |
4bbd4a1879f8
Plagiarism! (Only be sure, please, to always call it research)
Neil Muller <drnlmuller@gmail.com>
parents:
161
diff
changeset
|
87 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
|
88 if new_science: |
4bbd4a1879f8
Plagiarism! (Only be sure, please, to always call it research)
Neil Muller <drnlmuller@gmail.com>
parents:
161
diff
changeset
|
89 self.lab.steal_science(new_science) |
207 | 90 if income: |
91 self.income += income | |
138
14917385a0fd
Better handling of mission results and turn-end messages.
Jeremy Thurgood <firxen@gmail.com>
parents:
137
diff
changeset
|
92 |
16 | 93 def end_turn(self): |
94 # Attempt the missions | |
95 mission_results = [] | |
96 for mission, equipment in self.cur_missions: | |
224
e4f9513c9dd0
Include mission name in the results
Neil Muller <drnlmuller@gmail.com>
parents:
221
diff
changeset
|
97 mission_results.append((mission.NAME, |
e4f9513c9dd0
Include mission name in the results
Neil Muller <drnlmuller@gmail.com>
parents:
221
diff
changeset
|
98 mission.attempt_mission(equipment, self))) |
135
8d1cf98e39b5
Reputation decays over time
Neil Muller <drnlmuller@gmail.com>
parents:
131
diff
changeset
|
99 if not self.cur_missions and self.reputation > 0: |
8d1cf98e39b5
Reputation decays over time
Neil Muller <drnlmuller@gmail.com>
parents:
131
diff
changeset
|
100 # 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
|
101 self.reputation -= M_VALS[self.milestone] |
16 | 102 # Do the science |
103 self.points -= len(self.cur_allocation) | |
104 if self.points < 0: | |
105 raise RuntimeError('Spent too many points') | |
106 # 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
|
107 messages = [] |
224
e4f9513c9dd0
Include mission name in the results
Neil Muller <drnlmuller@gmail.com>
parents:
221
diff
changeset
|
108 for miss, result in mission_results: |
16 | 109 result.apply(self) |
224
e4f9513c9dd0
Include mission name in the results
Neil Muller <drnlmuller@gmail.com>
parents:
221
diff
changeset
|
110 messages.append((miss, result.outcome, result.text, result.loot)) |
178 | 111 # Missions may give us science breakthroughs, so handle this after |
112 # mission results | |
113 new_stuff = self.lab.spend_points(self.cur_allocation, self.points) | |
114 self.points = 0 | |
115 self.minions = 0 | |
16 | 116 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
|
117 # FIXME: Update UI better. |
224
e4f9513c9dd0
Include mission name in the results
Neil Muller <drnlmuller@gmail.com>
parents:
221
diff
changeset
|
118 messages.append((None, INFO, "SCIENCE breakthrough!", { |
138
14917385a0fd
Better handling of mission results and turn-end messages.
Jeremy Thurgood <firxen@gmail.com>
parents:
137
diff
changeset
|
119 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
|
120 return messages |
22
296ce36fa7d9
Serialize and unserialize game state and missions
Neil Muller <drnlmuller@gmail.com>
parents:
16
diff
changeset
|
121 |
296ce36fa7d9
Serialize and unserialize game state and missions
Neil Muller <drnlmuller@gmail.com>
parents:
16
diff
changeset
|
122 def save_data(self): |
296ce36fa7d9
Serialize and unserialize game state and missions
Neil Muller <drnlmuller@gmail.com>
parents:
16
diff
changeset
|
123 """Serialize the game state into a dict""" |
296ce36fa7d9
Serialize and unserialize game state and missions
Neil Muller <drnlmuller@gmail.com>
parents:
16
diff
changeset
|
124 data = {} |
296ce36fa7d9
Serialize and unserialize game state and missions
Neil Muller <drnlmuller@gmail.com>
parents:
16
diff
changeset
|
125 data['money'] = self.money |
124
685301e35f88
Add a minion cost to missions
Neil Muller <drnlmuller@gmail.com>
parents:
108
diff
changeset
|
126 data['minions'] = self.minions |
22
296ce36fa7d9
Serialize and unserialize game state and missions
Neil Muller <drnlmuller@gmail.com>
parents:
16
diff
changeset
|
127 data['reputation'] = self.reputation |
96 | 128 data['milestone'] = self.milestone |
22
296ce36fa7d9
Serialize and unserialize game state and missions
Neil Muller <drnlmuller@gmail.com>
parents:
16
diff
changeset
|
129 data['points'] = self.points |
87
d93e1ea2bd0d
Fix method name assumption bug
Neil Muller <drnlmuller@gmail.com>
parents:
82
diff
changeset
|
130 data['lab'] = self.lab.save_data() |
108 | 131 data['turn'] = self.turn |
207 | 132 data['income'] = self.income |
22
296ce36fa7d9
Serialize and unserialize game state and missions
Neil Muller <drnlmuller@gmail.com>
parents:
16
diff
changeset
|
133 # Save mission state |
296ce36fa7d9
Serialize and unserialize game state and missions
Neil Muller <drnlmuller@gmail.com>
parents:
16
diff
changeset
|
134 data['missions'] = {} |
296ce36fa7d9
Serialize and unserialize game state and missions
Neil Muller <drnlmuller@gmail.com>
parents:
16
diff
changeset
|
135 for mission in self.missions: |
296ce36fa7d9
Serialize and unserialize game state and missions
Neil Muller <drnlmuller@gmail.com>
parents:
16
diff
changeset
|
136 miss_name = type(mission).__name__ |
296ce36fa7d9
Serialize and unserialize game state and missions
Neil Muller <drnlmuller@gmail.com>
parents:
16
diff
changeset
|
137 data['missions'][miss_name] = mission.save_data() |
296ce36fa7d9
Serialize and unserialize game state and missions
Neil Muller <drnlmuller@gmail.com>
parents:
16
diff
changeset
|
138 return data |
296ce36fa7d9
Serialize and unserialize game state and missions
Neil Muller <drnlmuller@gmail.com>
parents:
16
diff
changeset
|
139 |
296ce36fa7d9
Serialize and unserialize game state and missions
Neil Muller <drnlmuller@gmail.com>
parents:
16
diff
changeset
|
140 def _load_data(self, data): |
296ce36fa7d9
Serialize and unserialize game state and missions
Neil Muller <drnlmuller@gmail.com>
parents:
16
diff
changeset
|
141 """Restore the game state""" |
296ce36fa7d9
Serialize and unserialize game state and missions
Neil Muller <drnlmuller@gmail.com>
parents:
16
diff
changeset
|
142 self.money = data['money'] |
124
685301e35f88
Add a minion cost to missions
Neil Muller <drnlmuller@gmail.com>
parents:
108
diff
changeset
|
143 self.minions = data['minions'] |
22
296ce36fa7d9
Serialize and unserialize game state and missions
Neil Muller <drnlmuller@gmail.com>
parents:
16
diff
changeset
|
144 self.reputation = data['reputation'] |
296ce36fa7d9
Serialize and unserialize game state and missions
Neil Muller <drnlmuller@gmail.com>
parents:
16
diff
changeset
|
145 self.points = data['points'] |
108 | 146 self.turn = data['turn'] |
207 | 147 self.income = data['income'] |
131 | 148 self.milestone = data['milestone'] |
22
296ce36fa7d9
Serialize and unserialize game state and missions
Neil Muller <drnlmuller@gmail.com>
parents:
16
diff
changeset
|
149 self.lab = lab.Lab(data['lab']) |
296ce36fa7d9
Serialize and unserialize game state and missions
Neil Muller <drnlmuller@gmail.com>
parents:
16
diff
changeset
|
150 for mis_class in missions.Mission.__subclasses__(): |
296ce36fa7d9
Serialize and unserialize game state and missions
Neil Muller <drnlmuller@gmail.com>
parents:
16
diff
changeset
|
151 miss_name = mis_class.__name__ |
296ce36fa7d9
Serialize and unserialize game state and missions
Neil Muller <drnlmuller@gmail.com>
parents:
16
diff
changeset
|
152 if miss_name in data['missions']: |
296ce36fa7d9
Serialize and unserialize game state and missions
Neil Muller <drnlmuller@gmail.com>
parents:
16
diff
changeset
|
153 self.missions.append(mis_class(data['missions'][miss_name])) |