Mercurial > sypikslang
annotate gamelib/gamestate.py @ 42:47c7e96cf9c8
Change initial mission creation logic to look for leaf classes
author | Neil Muller <drnlmuller@gmail.com> |
---|---|
date | Mon, 07 May 2012 14:00:18 +0200 |
parents | e285b1e31a08 |
children | 1e8f7e694f0c |
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 | |
6 from gamelib import missions, lab, products | |
7 | |
8 | |
9 class Game(object): | |
10 | |
22
296ce36fa7d9
Serialize and unserialize game state and missions
Neil Muller <drnlmuller@gmail.com>
parents:
16
diff
changeset
|
11 def __init__(self, init_data=None): |
16 | 12 self.money = 1000 |
13 # Will be updated on the next turn | |
14 self.points = 0 | |
15 self.reputation = 0 | |
16 # Missions being attempted | |
17 self.cur_missions = [] | |
18 # Science allocation for the current turn | |
19 self.cur_allocation = [] | |
22
296ce36fa7d9
Serialize and unserialize game state and missions
Neil Muller <drnlmuller@gmail.com>
parents:
16
diff
changeset
|
20 self.lab = None |
296ce36fa7d9
Serialize and unserialize game state and missions
Neil Muller <drnlmuller@gmail.com>
parents:
16
diff
changeset
|
21 self.missions = [] |
296ce36fa7d9
Serialize and unserialize game state and missions
Neil Muller <drnlmuller@gmail.com>
parents:
16
diff
changeset
|
22 if init_data: |
296ce36fa7d9
Serialize and unserialize game state and missions
Neil Muller <drnlmuller@gmail.com>
parents:
16
diff
changeset
|
23 self._load_data(init_data) |
296ce36fa7d9
Serialize and unserialize game state and missions
Neil Muller <drnlmuller@gmail.com>
parents:
16
diff
changeset
|
24 else: |
296ce36fa7d9
Serialize and unserialize game state and missions
Neil Muller <drnlmuller@gmail.com>
parents:
16
diff
changeset
|
25 self.lab = lab.Lab() |
296ce36fa7d9
Serialize and unserialize game state and missions
Neil Muller <drnlmuller@gmail.com>
parents:
16
diff
changeset
|
26 # instantiate all the available missions |
42
47c7e96cf9c8
Change initial mission creation logic to look for leaf classes
Neil Muller <drnlmuller@gmail.com>
parents:
41
diff
changeset
|
27 for cls in missions.Mission.__subclasses__(): |
47c7e96cf9c8
Change initial mission creation logic to look for leaf classes
Neil Muller <drnlmuller@gmail.com>
parents:
41
diff
changeset
|
28 if cls.__subclasses__(): |
47c7e96cf9c8
Change initial mission creation logic to look for leaf classes
Neil Muller <drnlmuller@gmail.com>
parents:
41
diff
changeset
|
29 # Not a leaf class, so base class for other missions |
47c7e96cf9c8
Change initial mission creation logic to look for leaf classes
Neil Muller <drnlmuller@gmail.com>
parents:
41
diff
changeset
|
30 continue |
47c7e96cf9c8
Change initial mission creation logic to look for leaf classes
Neil Muller <drnlmuller@gmail.com>
parents:
41
diff
changeset
|
31 # Add mission to the list |
47c7e96cf9c8
Change initial mission creation logic to look for leaf classes
Neil Muller <drnlmuller@gmail.com>
parents:
41
diff
changeset
|
32 self.missions.append(cls()) |
16 | 33 |
34 def start_turn(self): | |
35 # Make more flexible? | |
36 self.points += 3 | |
37 self.cur_missions = [] | |
38 self.cur_allocation = [] | |
39 | |
40 def get_available_equipment(self): | |
41 """Return a list of equipment we can produce and afford""" | |
25 | 42 available = [x for x in self.lab.science |
16 | 43 if isinstance(x, products.Product) and x.COST <= self.money] |
44 return available | |
45 | |
33
12c33aac7684
Gate certain missions by reputation.
Jeremy Thurgood <firxen@gmail.com>
parents:
27
diff
changeset
|
46 def get_available_missions(self): |
12c33aac7684
Gate certain missions by reputation.
Jeremy Thurgood <firxen@gmail.com>
parents:
27
diff
changeset
|
47 """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
|
48 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
|
49 return available |
12c33aac7684
Gate certain missions by reputation.
Jeremy Thurgood <firxen@gmail.com>
parents:
27
diff
changeset
|
50 |
16 | 51 def end_turn(self): |
52 # Attempt the missions | |
53 mission_results = [] | |
54 for mission, equipment in self.cur_missions: | |
25 | 55 mission_results.append(mission.attempt(equipment, self)) |
16 | 56 # Do the science |
57 self.points -= len(self.cur_allocation) | |
58 if self.points < 0: | |
59 raise RuntimeError('Spent too many points') | |
60 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
|
61 self.points = 0 |
16 | 62 # Process mission results |
63 for result in mission_results: | |
64 result.apply(self) | |
65 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
|
66 # FIXME: Update UI better. |
f6a3b213857b
Fix up some game state logic, add very basic REPL game interface.
Jeremy Thurgood <firxen@gmail.com>
parents:
22
diff
changeset
|
67 print "You learned new stuff:", science.NAME |
22
296ce36fa7d9
Serialize and unserialize game state and missions
Neil Muller <drnlmuller@gmail.com>
parents:
16
diff
changeset
|
68 |
296ce36fa7d9
Serialize and unserialize game state and missions
Neil Muller <drnlmuller@gmail.com>
parents:
16
diff
changeset
|
69 def save_data(self): |
296ce36fa7d9
Serialize and unserialize game state and missions
Neil Muller <drnlmuller@gmail.com>
parents:
16
diff
changeset
|
70 """Serialize the game state into a dict""" |
296ce36fa7d9
Serialize and unserialize game state and missions
Neil Muller <drnlmuller@gmail.com>
parents:
16
diff
changeset
|
71 data = {} |
296ce36fa7d9
Serialize and unserialize game state and missions
Neil Muller <drnlmuller@gmail.com>
parents:
16
diff
changeset
|
72 data['money'] = self.money |
296ce36fa7d9
Serialize and unserialize game state and missions
Neil Muller <drnlmuller@gmail.com>
parents:
16
diff
changeset
|
73 data['reputation'] = self.reputation |
296ce36fa7d9
Serialize and unserialize game state and missions
Neil Muller <drnlmuller@gmail.com>
parents:
16
diff
changeset
|
74 data['points'] = self.points |
296ce36fa7d9
Serialize and unserialize game state and missions
Neil Muller <drnlmuller@gmail.com>
parents:
16
diff
changeset
|
75 data['lab'] = self.lab.serialize() |
296ce36fa7d9
Serialize and unserialize game state and missions
Neil Muller <drnlmuller@gmail.com>
parents:
16
diff
changeset
|
76 # Save mission state |
296ce36fa7d9
Serialize and unserialize game state and missions
Neil Muller <drnlmuller@gmail.com>
parents:
16
diff
changeset
|
77 data['missions'] = {} |
296ce36fa7d9
Serialize and unserialize game state and missions
Neil Muller <drnlmuller@gmail.com>
parents:
16
diff
changeset
|
78 for mission in self.missions: |
296ce36fa7d9
Serialize and unserialize game state and missions
Neil Muller <drnlmuller@gmail.com>
parents:
16
diff
changeset
|
79 miss_name = type(mission).__name__ |
296ce36fa7d9
Serialize and unserialize game state and missions
Neil Muller <drnlmuller@gmail.com>
parents:
16
diff
changeset
|
80 data['missions'][miss_name] = mission.save_data() |
296ce36fa7d9
Serialize and unserialize game state and missions
Neil Muller <drnlmuller@gmail.com>
parents:
16
diff
changeset
|
81 return data |
296ce36fa7d9
Serialize and unserialize game state and missions
Neil Muller <drnlmuller@gmail.com>
parents:
16
diff
changeset
|
82 |
296ce36fa7d9
Serialize and unserialize game state and missions
Neil Muller <drnlmuller@gmail.com>
parents:
16
diff
changeset
|
83 def _load_data(self, data): |
296ce36fa7d9
Serialize and unserialize game state and missions
Neil Muller <drnlmuller@gmail.com>
parents:
16
diff
changeset
|
84 """Restore the game state""" |
296ce36fa7d9
Serialize and unserialize game state and missions
Neil Muller <drnlmuller@gmail.com>
parents:
16
diff
changeset
|
85 self.money = data['money'] |
296ce36fa7d9
Serialize and unserialize game state and missions
Neil Muller <drnlmuller@gmail.com>
parents:
16
diff
changeset
|
86 self.reputation = data['reputation'] |
296ce36fa7d9
Serialize and unserialize game state and missions
Neil Muller <drnlmuller@gmail.com>
parents:
16
diff
changeset
|
87 self.points = data['points'] |
296ce36fa7d9
Serialize and unserialize game state and missions
Neil Muller <drnlmuller@gmail.com>
parents:
16
diff
changeset
|
88 self.lab = lab.Lab(data['lab']) |
296ce36fa7d9
Serialize and unserialize game state and missions
Neil Muller <drnlmuller@gmail.com>
parents:
16
diff
changeset
|
89 for mis_class in missions.Mission.__subclasses__(): |
296ce36fa7d9
Serialize and unserialize game state and missions
Neil Muller <drnlmuller@gmail.com>
parents:
16
diff
changeset
|
90 miss_name = mis_class.__name__ |
296ce36fa7d9
Serialize and unserialize game state and missions
Neil Muller <drnlmuller@gmail.com>
parents:
16
diff
changeset
|
91 if miss_name in data['missions']: |
296ce36fa7d9
Serialize and unserialize game state and missions
Neil Muller <drnlmuller@gmail.com>
parents:
16
diff
changeset
|
92 self.missions.append(mis_class(data['missions'][miss_name])) |