Mercurial > sypikslang
comparison gamelib/lab.py @ 17:10d3db1f1e08
Set up initial research and rework breakthroughs.
author | Jeremy Thurgood <firxen@gmail.com> |
---|---|
date | Sun, 06 May 2012 17:39:37 +0200 |
parents | 9d61abb3cfaf |
children | 718d1ec382f7 |
comparison
equal
deleted
inserted
replaced
16:af1bfeb648cb | 17:10d3db1f1e08 |
---|---|
1 # -*- test-case-name: gamelib.tests.test_lab -*- | 1 # -*- test-case-name: gamelib.tests.test_lab -*- |
2 | |
3 from random import random, choice | |
2 | 4 |
3 from gamelib import research, products | 5 from gamelib import research, products |
4 | 6 |
5 | 7 |
6 class Lab(object): | 8 class Lab(object): |
9 BASIC_RESEARCH_SUCCESS_RATE = 0.05 | |
10 BASIC_RESEARCH_SUCCESS_MULTIPLIER = 2 | |
11 | |
7 def __init__(self): | 12 def __init__(self): |
8 self.science = [] | 13 self.science = [] |
9 self.new_research = research.ResearchArea.__subclasses__() | 14 self.new_research = research.ResearchArea.__subclasses__() |
10 self.new_products = products.Product.__subclasses__() | 15 self.new_products = products.Product.__subclasses__() |
16 self._choose_initial_science() | |
17 | |
18 def _choose_initial_science(self): | |
19 # We always get all starting products. | |
20 for product in self.new_products[:]: | |
21 if product.STARTING_PRODUCT: | |
22 self._gain_science(product()) | |
23 | |
24 # We get three random sciences with no prerequisites. | |
25 new_science = [] | |
26 for _ in range(3): | |
27 science = choice(self.find_new_research())() | |
28 self._gain_science(science) | |
29 new_science.append(science) | |
30 | |
31 # Add a point to each of our sciences, and see if we get products. | |
32 self.spend_points(new_science, 0) | |
33 | |
34 def _gain_science(self, science): | |
35 self.science.append(science) | |
36 if isinstance(science, research.ResearchArea): | |
37 self.new_research.remove(type(science)) | |
38 elif isinstance(science, products.Product): | |
39 self.new_products.remove(type(science)) | |
11 | 40 |
12 def spend_points(self, things, basic_research): | 41 def spend_points(self, things, basic_research): |
13 new_stuff = [] | 42 breakthroughs = [] |
43 | |
44 # First, allocate the points. | |
14 for thing in things: | 45 for thing in things: |
15 assert thing in self.science | 46 assert thing in self.science |
16 thing.spend_points(1) | 47 assert thing.can_spend(self) |
48 thing.spend_point() | |
49 | |
50 # Next, check for product breakthroughs and upgrades | |
51 for thing in things: | |
17 if isinstance(thing, research.ResearchArea): | 52 if isinstance(thing, research.ResearchArea): |
18 new_stuff.extend(self.find_new_product(thing)[:1]) | 53 breakthroughs.extend(self.apply_area_research(thing)) |
19 new_stuff.extend(self.try_basic_research(basic_research)[:1]) | 54 |
20 return new_stuff | 55 # Finally, check for research breakthroughs. |
56 breakthroughs.extend(self.apply_basic_research(basic_research)) | |
57 | |
58 # Now that we have our breakthroughs, apply them. | |
59 print breakthroughs | |
60 | |
61 return breakthroughs | |
21 | 62 |
22 def _get_science(self, science_class): | 63 def _get_science(self, science_class): |
23 for science in self.science: | 64 for science in self.science: |
24 if isinstance(science, science_class): | 65 if isinstance(science, science_class): |
25 return science | 66 return science |
26 return None | 67 return None |
27 | 68 |
28 def _meet_requirements(self, science_class): | 69 def meet_requirements(self, science_class, extra=0): |
70 total_points = 0 | |
71 base_points = 0 | |
29 for science, level in science_class.PREREQUISITES: | 72 for science, level in science_class.PREREQUISITES: |
30 my_science = self._get_science(science) | 73 my_science = self._get_science(science) |
31 if my_science is None: | 74 if my_science is None: |
32 return False | 75 return False |
33 if my_science.points < level: | 76 if my_science.points < level: |
34 return False | 77 return False |
35 return True | 78 base_points += level |
79 total_points += my_science.points | |
80 return total_points - base_points >= extra | |
36 | 81 |
37 def find_new_products(self, research_area): | 82 def find_new_products(self, research_area): |
38 available_products = [] | 83 available_products = [] |
39 for product_class in self.new_products: | 84 for product_class in self.new_products: |
40 if self._meet_requirements(product_class): | 85 if self.meet_requirements(product_class): |
41 available_products.append(product_class) | 86 available_products.append(product_class) |
42 return available_products | 87 return available_products |
43 | 88 |
44 def find_new_research(self, basic_research): | 89 def find_new_research(self): |
45 available_research = [] | 90 available_research = [] |
46 for research_class in self.new_research: | 91 for research_class in self.new_research: |
47 if self._meet_requirements(research_class): | 92 if self.meet_requirements(research_class): |
48 available_research.append(research_class) | 93 available_research.append(research_class) |
49 return available_research | 94 return available_research |
95 | |
96 def apply_area_research(self, research): | |
97 options = self.find_new_products(research) | |
98 breakthroughs = [product for product in options | |
99 if random() < product.ACQUISITION_CHANCE] | |
100 if breakthroughs: | |
101 breakthrough = choice(breakthroughs)() | |
102 self._gain_science(breakthrough) | |
103 breakthroughs = [breakthrough] | |
104 return breakthroughs | |
105 | |
106 def apply_basic_research(self, basic_research): | |
107 if basic_research <= 1: | |
108 return [] | |
109 | |
110 options = self.find_new_research() | |
111 success_chance = self.BASIC_RESEARCH_SUCCESS_RATE * ( | |
112 self.BASIC_RESEARCH_SUCCESS_MULTIPLIER ** basic_research) | |
113 breakthroughs = [research for research in options | |
114 if random() < success_chance] | |
115 if breakthroughs: | |
116 breakthrough = choice(breakthroughs)(1) | |
117 self._gain_science(breakthrough) | |
118 breakthroughs = [breakthrough] | |
119 return breakthroughs |