Mercurial > sypikslang
annotate gamelib/gamestate.py @ 130:8f717b2b3378
Advance milestone
author | Neil Muller <drnlmuller@gmail.com> |
---|---|
date | Thu, 10 May 2012 16:46:18 +0200 |
parents | 685301e35f88 |
children | 31ab0af26bb5 |
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 | |
45
1e8f7e694f0c
Refactor missions and sciences a bit to reduce duplication.
Jeremy Thurgood <firxen@gmail.com>
parents:
42
diff
changeset
|
6 from gamelib import missions, lab |
1e8f7e694f0c
Refactor missions and sciences a bit to reduce duplication.
Jeremy Thurgood <firxen@gmail.com>
parents:
42
diff
changeset
|
7 from gamelib.game_base import get_subclasses |
130 | 8 from gamelib.constants import (NEW_SCIENCE, NEW_SCHEMATIC, M_VALS, MILESTONES, |
9 NEW_MILESTONE) | |
16 | 10 |
11 | |
12 class Game(object): | |
13 | |
22
296ce36fa7d9
Serialize and unserialize game state and missions
Neil Muller <drnlmuller@gmail.com>
parents:
16
diff
changeset
|
14 def __init__(self, init_data=None): |
16 | 15 self.money = 1000 |
124
685301e35f88
Add a minion cost to missions
Neil Muller <drnlmuller@gmail.com>
parents:
108
diff
changeset
|
16 self.minions = 1 |
96 | 17 self.milestone = "basement" |
16 | 18 # Will be updated on the next turn |
19 self.points = 0 | |
20 self.reputation = 0 | |
81
59afe9f92383
Hook up basic results screen
Neil Muller <drnlmuller@gmail.com>
parents:
80
diff
changeset
|
21 self.turn = 0 |
16 | 22 # Missions being attempted |
23 self.cur_missions = [] | |
24 # Science allocation for the current turn | |
25 self.cur_allocation = [] | |
22
296ce36fa7d9
Serialize and unserialize game state and missions
Neil Muller <drnlmuller@gmail.com>
parents:
16
diff
changeset
|
26 self.lab = None |
296ce36fa7d9
Serialize and unserialize game state and missions
Neil Muller <drnlmuller@gmail.com>
parents:
16
diff
changeset
|
27 self.missions = [] |
296ce36fa7d9
Serialize and unserialize game state and missions
Neil Muller <drnlmuller@gmail.com>
parents:
16
diff
changeset
|
28 if init_data: |
296ce36fa7d9
Serialize and unserialize game state and missions
Neil Muller <drnlmuller@gmail.com>
parents:
16
diff
changeset
|
29 self._load_data(init_data) |
296ce36fa7d9
Serialize and unserialize game state and missions
Neil Muller <drnlmuller@gmail.com>
parents:
16
diff
changeset
|
30 else: |
296ce36fa7d9
Serialize and unserialize game state and missions
Neil Muller <drnlmuller@gmail.com>
parents:
16
diff
changeset
|
31 self.lab = lab.Lab() |
296ce36fa7d9
Serialize and unserialize game state and missions
Neil Muller <drnlmuller@gmail.com>
parents:
16
diff
changeset
|
32 # 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
|
33 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
|
34 self.missions.append(cls()) |
16 | 35 |
36 def start_turn(self): | |
37 # Make more flexible? | |
99
531eb3239da2
Research points allocated based on milestone.
Jeremy Thurgood <firxen@gmail.com>
parents:
96
diff
changeset
|
38 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
|
39 self.minions += 1 + M_VALS[self.milestone] * 2 |
81
59afe9f92383
Hook up basic results screen
Neil Muller <drnlmuller@gmail.com>
parents:
80
diff
changeset
|
40 self.turn += 1 |
16 | 41 self.cur_missions = [] |
42 self.cur_allocation = [] | |
43 | |
44 def get_available_equipment(self): | |
45 """Return a list of equipment we can produce and afford""" | |
25 | 46 available = [x for x in self.lab.science |
49 | 47 if x.SCIENCE_TYPE == 'schematic' and x.COST <= self.money] |
16 | 48 return available |
49 | |
82
b0d97d51df51
Hook up simplistic equipment screen
Neil Muller <drnlmuller@gmail.com>
parents:
81
diff
changeset
|
50 def get_all_equipment(self): |
b0d97d51df51
Hook up simplistic equipment screen
Neil Muller <drnlmuller@gmail.com>
parents:
81
diff
changeset
|
51 """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
|
52 equipment = [x for x in self.lab.science |
b0d97d51df51
Hook up simplistic equipment screen
Neil Muller <drnlmuller@gmail.com>
parents:
81
diff
changeset
|
53 if x.SCIENCE_TYPE == 'schematic'] |
b0d97d51df51
Hook up simplistic equipment screen
Neil Muller <drnlmuller@gmail.com>
parents:
81
diff
changeset
|
54 return equipment |
b0d97d51df51
Hook up simplistic equipment screen
Neil Muller <drnlmuller@gmail.com>
parents:
81
diff
changeset
|
55 |
33
12c33aac7684
Gate certain missions by reputation.
Jeremy Thurgood <firxen@gmail.com>
parents:
27
diff
changeset
|
56 def get_available_missions(self): |
12c33aac7684
Gate certain missions by reputation.
Jeremy Thurgood <firxen@gmail.com>
parents:
27
diff
changeset
|
57 """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
|
58 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
|
59 return available |
12c33aac7684
Gate certain missions by reputation.
Jeremy Thurgood <firxen@gmail.com>
parents:
27
diff
changeset
|
60 |
63
364ff3479ef2
Remove duplicate point tracking. Rename classes for future refactoring
Neil Muller <drnlmuller@gmail.com>
parents:
49
diff
changeset
|
61 def get_available_points(self): |
364ff3479ef2
Remove duplicate point tracking. Rename classes for future refactoring
Neil Muller <drnlmuller@gmail.com>
parents:
49
diff
changeset
|
62 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
|
63 |
16 | 64 def end_turn(self): |
65 # Attempt the missions | |
66 mission_results = [] | |
67 for mission, equipment in self.cur_missions: | |
25 | 68 mission_results.append(mission.attempt(equipment, self)) |
16 | 69 # Do the science |
70 self.points -= len(self.cur_allocation) | |
71 if self.points < 0: | |
72 raise RuntimeError('Spent too many points') | |
73 new_stuff = self.lab.spend_points(self.cur_allocation, self.points) | |
27
4d9610c59efa
Reset points after spending them.
Jeremy Thurgood <firxen@gmail.com>
parents:
25
diff
changeset
|
74 self.points = 0 |
124
685301e35f88
Add a minion cost to missions
Neil Muller <drnlmuller@gmail.com>
parents:
108
diff
changeset
|
75 self.minions = 0 |
16 | 76 # 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
|
77 messages = [] |
16 | 78 for result in mission_results: |
79 result.apply(self) | |
81
59afe9f92383
Hook up basic results screen
Neil Muller <drnlmuller@gmail.com>
parents:
80
diff
changeset
|
80 messages.append((result.outcome, result.message)) |
130 | 81 if result.outcome == NEW_MILESTONE: |
82 self.milestone = MILESTONES[M_VALS[self.milestone] + 1] | |
16 | 83 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
|
84 # FIXME: Update UI better. |
80
a40a76012bd7
Refactor message handling so we can deal with in the gui
Neil Muller <drnlmuller@gmail.com>
parents:
63
diff
changeset
|
85 if science.SCIENCE_TYPE == 'research': |
a40a76012bd7
Refactor message handling so we can deal with in the gui
Neil Muller <drnlmuller@gmail.com>
parents:
63
diff
changeset
|
86 messages.append((NEW_SCIENCE, |
a40a76012bd7
Refactor message handling so we can deal with in the gui
Neil Muller <drnlmuller@gmail.com>
parents:
63
diff
changeset
|
87 "You've started a new line of research in %s" |
a40a76012bd7
Refactor message handling so we can deal with in the gui
Neil Muller <drnlmuller@gmail.com>
parents:
63
diff
changeset
|
88 % science.NAME)) |
a40a76012bd7
Refactor message handling so we can deal with in the gui
Neil Muller <drnlmuller@gmail.com>
parents:
63
diff
changeset
|
89 else: |
a40a76012bd7
Refactor message handling so we can deal with in the gui
Neil Muller <drnlmuller@gmail.com>
parents:
63
diff
changeset
|
90 messages.append((NEW_SCHEMATIC, |
a40a76012bd7
Refactor message handling so we can deal with in the gui
Neil Muller <drnlmuller@gmail.com>
parents:
63
diff
changeset
|
91 "You've developed blueprints for a %s" % science.NAME)) |
a40a76012bd7
Refactor message handling so we can deal with in the gui
Neil Muller <drnlmuller@gmail.com>
parents:
63
diff
changeset
|
92 return messages |
22
296ce36fa7d9
Serialize and unserialize game state and missions
Neil Muller <drnlmuller@gmail.com>
parents:
16
diff
changeset
|
93 |
296ce36fa7d9
Serialize and unserialize game state and missions
Neil Muller <drnlmuller@gmail.com>
parents:
16
diff
changeset
|
94 def save_data(self): |
296ce36fa7d9
Serialize and unserialize game state and missions
Neil Muller <drnlmuller@gmail.com>
parents:
16
diff
changeset
|
95 """Serialize the game state into a dict""" |
296ce36fa7d9
Serialize and unserialize game state and missions
Neil Muller <drnlmuller@gmail.com>
parents:
16
diff
changeset
|
96 data = {} |
296ce36fa7d9
Serialize and unserialize game state and missions
Neil Muller <drnlmuller@gmail.com>
parents:
16
diff
changeset
|
97 data['money'] = self.money |
124
685301e35f88
Add a minion cost to missions
Neil Muller <drnlmuller@gmail.com>
parents:
108
diff
changeset
|
98 data['minions'] = self.minions |
22
296ce36fa7d9
Serialize and unserialize game state and missions
Neil Muller <drnlmuller@gmail.com>
parents:
16
diff
changeset
|
99 data['reputation'] = self.reputation |
96 | 100 data['milestone'] = self.milestone |
22
296ce36fa7d9
Serialize and unserialize game state and missions
Neil Muller <drnlmuller@gmail.com>
parents:
16
diff
changeset
|
101 data['points'] = self.points |
87
d93e1ea2bd0d
Fix method name assumption bug
Neil Muller <drnlmuller@gmail.com>
parents:
82
diff
changeset
|
102 data['lab'] = self.lab.save_data() |
108 | 103 data['turn'] = self.turn |
22
296ce36fa7d9
Serialize and unserialize game state and missions
Neil Muller <drnlmuller@gmail.com>
parents:
16
diff
changeset
|
104 # Save mission state |
296ce36fa7d9
Serialize and unserialize game state and missions
Neil Muller <drnlmuller@gmail.com>
parents:
16
diff
changeset
|
105 data['missions'] = {} |
296ce36fa7d9
Serialize and unserialize game state and missions
Neil Muller <drnlmuller@gmail.com>
parents:
16
diff
changeset
|
106 for mission in self.missions: |
296ce36fa7d9
Serialize and unserialize game state and missions
Neil Muller <drnlmuller@gmail.com>
parents:
16
diff
changeset
|
107 miss_name = type(mission).__name__ |
296ce36fa7d9
Serialize and unserialize game state and missions
Neil Muller <drnlmuller@gmail.com>
parents:
16
diff
changeset
|
108 data['missions'][miss_name] = mission.save_data() |
296ce36fa7d9
Serialize and unserialize game state and missions
Neil Muller <drnlmuller@gmail.com>
parents:
16
diff
changeset
|
109 return data |
296ce36fa7d9
Serialize and unserialize game state and missions
Neil Muller <drnlmuller@gmail.com>
parents:
16
diff
changeset
|
110 |
296ce36fa7d9
Serialize and unserialize game state and missions
Neil Muller <drnlmuller@gmail.com>
parents:
16
diff
changeset
|
111 def _load_data(self, data): |
296ce36fa7d9
Serialize and unserialize game state and missions
Neil Muller <drnlmuller@gmail.com>
parents:
16
diff
changeset
|
112 """Restore the game state""" |
296ce36fa7d9
Serialize and unserialize game state and missions
Neil Muller <drnlmuller@gmail.com>
parents:
16
diff
changeset
|
113 self.money = data['money'] |
124
685301e35f88
Add a minion cost to missions
Neil Muller <drnlmuller@gmail.com>
parents:
108
diff
changeset
|
114 self.minions = data['minions'] |
22
296ce36fa7d9
Serialize and unserialize game state and missions
Neil Muller <drnlmuller@gmail.com>
parents:
16
diff
changeset
|
115 self.reputation = data['reputation'] |
296ce36fa7d9
Serialize and unserialize game state and missions
Neil Muller <drnlmuller@gmail.com>
parents:
16
diff
changeset
|
116 self.points = data['points'] |
108 | 117 self.turn = data['turn'] |
22
296ce36fa7d9
Serialize and unserialize game state and missions
Neil Muller <drnlmuller@gmail.com>
parents:
16
diff
changeset
|
118 self.lab = lab.Lab(data['lab']) |
296ce36fa7d9
Serialize and unserialize game state and missions
Neil Muller <drnlmuller@gmail.com>
parents:
16
diff
changeset
|
119 for mis_class in missions.Mission.__subclasses__(): |
296ce36fa7d9
Serialize and unserialize game state and missions
Neil Muller <drnlmuller@gmail.com>
parents:
16
diff
changeset
|
120 miss_name = mis_class.__name__ |
296ce36fa7d9
Serialize and unserialize game state and missions
Neil Muller <drnlmuller@gmail.com>
parents:
16
diff
changeset
|
121 if miss_name in data['missions']: |
296ce36fa7d9
Serialize and unserialize game state and missions
Neil Muller <drnlmuller@gmail.com>
parents:
16
diff
changeset
|
122 self.missions.append(mis_class(data['missions'][miss_name])) |