annotate gamelib/gamestate.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 af1bfeb648cb
children 296ce36fa7d9
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
16
af1bfeb648cb Sketch in gamestate
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
1 # -*- coding: utf-8 -*-
af1bfeb648cb Sketch in gamestate
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
2 # vim:fileencoding=utf-8 ai ts=4 sts=4 et sw=4
af1bfeb648cb Sketch in gamestate
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
3
af1bfeb648cb Sketch in gamestate
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
4 """The actual game state object"""
af1bfeb648cb Sketch in gamestate
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
5
af1bfeb648cb Sketch in gamestate
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
6 from gamelib import missions, lab, products
af1bfeb648cb Sketch in gamestate
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
7
af1bfeb648cb Sketch in gamestate
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
8
af1bfeb648cb Sketch in gamestate
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
9 class Game(object):
af1bfeb648cb Sketch in gamestate
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
10
af1bfeb648cb Sketch in gamestate
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
11 def __init__(self):
af1bfeb648cb Sketch in gamestate
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
12 self.lab = lab.Lab()
af1bfeb648cb Sketch in gamestate
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
13 # FIXME: Generate the initial tech set
af1bfeb648cb Sketch in gamestate
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
14 self.money = 1000
af1bfeb648cb Sketch in gamestate
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
15 # Will be updated on the next turn
af1bfeb648cb Sketch in gamestate
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
16 self.points = 0
af1bfeb648cb Sketch in gamestate
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
17 self.reputation = 0
af1bfeb648cb Sketch in gamestate
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
18 # instantiate the available missions
af1bfeb648cb Sketch in gamestate
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
19 self.missions = [cls() for cls in missions.Mission.__subclasses__()]
af1bfeb648cb Sketch in gamestate
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
20 # Missions being attempted
af1bfeb648cb Sketch in gamestate
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
21 self.cur_missions = []
af1bfeb648cb Sketch in gamestate
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
22 # Science allocation for the current turn
af1bfeb648cb Sketch in gamestate
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
23 self.cur_allocation = []
af1bfeb648cb Sketch in gamestate
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
24
af1bfeb648cb Sketch in gamestate
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
25 def start_turn(self):
af1bfeb648cb Sketch in gamestate
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
26 # Make more flexible?
af1bfeb648cb Sketch in gamestate
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
27 self.points += 3
af1bfeb648cb Sketch in gamestate
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
28 self.cur_missions = []
af1bfeb648cb Sketch in gamestate
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
29 self.cur_allocation = []
af1bfeb648cb Sketch in gamestate
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
30
af1bfeb648cb Sketch in gamestate
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
31 def get_available_equipment(self):
af1bfeb648cb Sketch in gamestate
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
32 """Return a list of equipment we can produce and afford"""
af1bfeb648cb Sketch in gamestate
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
33 available = [x for x in lab.science
af1bfeb648cb Sketch in gamestate
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
34 if isinstance(x, products.Product) and x.COST <= self.money]
af1bfeb648cb Sketch in gamestate
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
35 return available
af1bfeb648cb Sketch in gamestate
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
36
af1bfeb648cb Sketch in gamestate
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
37 def end_turn(self):
af1bfeb648cb Sketch in gamestate
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
38 # Attempt the missions
af1bfeb648cb Sketch in gamestate
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
39 mission_results = []
af1bfeb648cb Sketch in gamestate
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
40 for mission, equipment in self.cur_missions:
af1bfeb648cb Sketch in gamestate
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
41 mission_results.appned(mission.attempt(equipment, self))
af1bfeb648cb Sketch in gamestate
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
42 # Do the science
af1bfeb648cb Sketch in gamestate
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
43 self.points -= len(self.cur_allocation)
af1bfeb648cb Sketch in gamestate
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
44 if self.points < 0:
af1bfeb648cb Sketch in gamestate
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
45 raise RuntimeError('Spent too many points')
af1bfeb648cb Sketch in gamestate
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
46 new_stuff = self.lab.spend_points(self.cur_allocation, self.points)
af1bfeb648cb Sketch in gamestate
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
47 # Process mission results
af1bfeb648cb Sketch in gamestate
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
48 for result in mission_results:
af1bfeb648cb Sketch in gamestate
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
49 result.apply(self)
af1bfeb648cb Sketch in gamestate
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
50 # Update the science state with result of spend_points
af1bfeb648cb Sketch in gamestate
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
51 for science in new_stuff:
af1bfeb648cb Sketch in gamestate
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
52 self.lab.science.append(science)
af1bfeb648cb Sketch in gamestate
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
53 # FIXME: Update UI