comparison gamelib/lab.py @ 49:373c57ab4140

Product -> Schematic.
author Jeremy Thurgood <firxen@gmail.com>
date Mon, 07 May 2012 21:24:23 +0200
parents 3e3bed2ce248
children 52913ba12988
comparison
equal deleted inserted replaced
48:a2980cc9a060 49:373c57ab4140
1 # -*- test-case-name: gamelib.tests.test_lab -*- 1 # -*- test-case-name: gamelib.tests.test_lab -*-
2 2
3 from random import random, choice 3 from random import random, choice
4 4
5 from gamelib import research, products 5 from gamelib import research, schematics
6 from gamelib.game_base import get_subclasses 6 from gamelib.game_base import get_subclasses
7 7
8 8
9 class Lab(object): 9 class Lab(object):
10 BASIC_RESEARCH_SUCCESS_RATE = 0.05 10 BASIC_RESEARCH_SUCCESS_RATE = 0.05
11 BASIC_RESEARCH_SUCCESS_MULTIPLIER = 2 11 BASIC_RESEARCH_SUCCESS_MULTIPLIER = 2
12 12
13 def __init__(self, init_data=None): 13 def __init__(self, init_data=None):
14 self.science = [] 14 self.science = []
15 self.new_research = get_subclasses(research.ResearchArea) 15 self.new_research = get_subclasses(research.ResearchArea)
16 self.new_products = get_subclasses(products.Product) 16 self.new_schematics = get_subclasses(schematics.Schematic)
17 17
18 if init_data is not None: 18 if init_data is not None:
19 # Load stored state. 19 # Load stored state.
20 self._load_data(init_data) 20 self._load_data(init_data)
21 else: 21 else:
22 # New game. 22 # New game.
23 self._choose_initial_science() 23 self._choose_initial_science()
24 24
25 def _load_data(self, init_data): 25 def _load_data(self, init_data):
26 sciences = init_data['science'].copy() 26 sciences = init_data['science'].copy()
27 for science in self.new_products + self.new_research: 27 for science in self.new_schematics + self.new_research:
28 # Check if this science is one we should know. 28 # Check if this science is one we should know.
29 points = sciences.pop(science.save_name(), None) 29 points = sciences.pop(science.save_name(), None)
30 if points is not None: 30 if points is not None:
31 # It is! Learn it. 31 # It is! Learn it.
32 self._gain_science(science(points)) 32 self._gain_science(science(points))
36 36
37 def save_data(self): 37 def save_data(self):
38 return {'science': dict(s.save_data() for s in self.science)} 38 return {'science': dict(s.save_data() for s in self.science)}
39 39
40 def _choose_initial_science(self): 40 def _choose_initial_science(self):
41 # We always get all starting products. 41 # We always get all starting schematics.
42 for product in self.new_products[:]: 42 for schematic in self.new_schematics[:]:
43 if product.STARTING_PRODUCT: 43 if schematic.STARTING_PRODUCT:
44 self._gain_science(product()) 44 self._gain_science(schematic())
45 45
46 # We get three random sciences with no prerequisites. 46 # We get three random sciences with no prerequisites.
47 new_science = [] 47 new_science = []
48 for _ in range(3): 48 for _ in range(3):
49 science = choice(self.find_new_research())() 49 science = choice(self.find_new_research())()
50 self._gain_science(science) 50 self._gain_science(science)
51 new_science.append(science) 51 new_science.append(science)
52 52
53 # Add a point to each of our sciences, and see if we get products. 53 # Add a point to each of our sciences, and see if we get schematics.
54 self.spend_points(new_science, 0) 54 self.spend_points(new_science, 0)
55 55
56 def _gain_science(self, science): 56 def _gain_science(self, science):
57 self.science.append(science) 57 self.science.append(science)
58 if isinstance(science, research.ResearchArea): 58 if isinstance(science, research.ResearchArea):
59 self.new_research.remove(type(science)) 59 self.new_research.remove(type(science))
60 elif isinstance(science, products.Product): 60 elif isinstance(science, schematics.Schematic):
61 self.new_products.remove(type(science)) 61 self.new_schematics.remove(type(science))
62 62
63 def spend_points(self, things, basic_research): 63 def spend_points(self, things, basic_research):
64 breakthroughs = [] 64 breakthroughs = []
65 65
66 # First, allocate the points. 66 # First, allocate the points.
67 for thing in things: 67 for thing in things:
68 assert thing in self.science 68 assert thing in self.science
69 assert thing.can_spend(self) 69 assert thing.can_spend(self)
70 thing.spend_point() 70 thing.spend_point()
71 71
72 # Next, check for product breakthroughs and upgrades 72 # Next, check for schematic breakthroughs and upgrades
73 for thing in things: 73 for thing in things:
74 if isinstance(thing, research.ResearchArea): 74 if isinstance(thing, research.ResearchArea):
75 breakthroughs.extend(self.apply_area_research(thing)) 75 breakthroughs.extend(self.apply_area_research(thing))
76 76
77 # Finally, check for research breakthroughs. 77 # Finally, check for research breakthroughs.
96 return False 96 return False
97 base_points += level 97 base_points += level
98 total_points += my_science.points 98 total_points += my_science.points
99 return total_points - base_points >= extra 99 return total_points - base_points >= extra
100 100
101 def find_new_products(self): 101 def find_new_schematics(self):
102 available_products = [] 102 available_schematics = []
103 for product_class in self.new_products: 103 for schematic_class in self.new_schematics:
104 if self.meet_requirements(product_class): 104 if self.meet_requirements(schematic_class):
105 available_products.append(product_class) 105 available_schematics.append(schematic_class)
106 return available_products 106 return available_schematics
107 107
108 def find_new_research(self): 108 def find_new_research(self):
109 available_research = [] 109 available_research = []
110 for research_class in self.new_research: 110 for research_class in self.new_research:
111 if self.meet_requirements(research_class): 111 if self.meet_requirements(research_class):
112 available_research.append(research_class) 112 available_research.append(research_class)
113 return available_research 113 return available_research
114 114
115 def apply_area_research(self, research): 115 def apply_area_research(self, research):
116 options = [product for product in self.find_new_products() 116 options = [schema for schema in self.find_new_schematics()
117 if type(research) in [p[0] for p in product.PREREQUISITES]] 117 if type(research) in [p[0] for p in schema.PREREQUISITES]]
118 breakthroughs = [product for product in options 118 breakthroughs = [schematic for schematic in options
119 if random() < product.ACQUISITION_CHANCE] 119 if random() < schematic.ACQUISITION_CHANCE]
120 if breakthroughs: 120 if breakthroughs:
121 breakthrough = choice(breakthroughs)() 121 breakthrough = choice(breakthroughs)()
122 self._gain_science(breakthrough) 122 self._gain_science(breakthrough)
123 breakthroughs = [breakthrough] 123 breakthroughs = [breakthrough]
124 return breakthroughs 124 return breakthroughs