Mercurial > sypikslang
comparison gamelib/missions.py @ 23:f6a3b213857b
Fix up some game state logic, add very basic REPL game interface.
author | Jeremy Thurgood <firxen@gmail.com> |
---|---|
date | Sun, 06 May 2012 19:06:28 +0200 |
parents | gamelib/mission.py@296ce36fa7d9 |
children | 27570aca5d17 |
comparison
equal
deleted
inserted
replaced
22:296ce36fa7d9 | 23:f6a3b213857b |
---|---|
1 # -*- coding: utf-8 -*- | |
2 # vim:fileencoding=utf-8 ai ts=4 sts=4 et sw=4 | |
3 | |
4 from products import DoomsdayVirus, MachineGun, LightningGun | |
5 | |
6 MAJOR_SETBACK, FAILURE, SUCCESS, MAJOR_SUCCESS, GAME_WIN = range(5) | |
7 | |
8 | |
9 class Result(object): | |
10 """Results of a mission""" | |
11 | |
12 def __init__(self, outcome, money, reputation, msg): | |
13 self.outcome = outcome | |
14 self.money = money | |
15 self.reputation = reputation | |
16 self.message = msg | |
17 self.applied = False | |
18 | |
19 def apply(self, state): | |
20 if not self.applied: | |
21 state.money += self.money | |
22 state.reputation += self.reputation | |
23 self.applied = True | |
24 else: | |
25 raise RuntimeError('attempted to apply result twice') | |
26 | |
27 | |
28 class Mission(object): | |
29 """Base class for the mission objects. | |
30 Missions have a name, short description (for list displays) and | |
31 long description (which may contain clues about approaches)""" | |
32 | |
33 NAME = "Generic Mission" | |
34 SHORT_DESCRIPTION = None | |
35 LONG_DESCRIPTION = None | |
36 available = True | |
37 | |
38 def __init__(self, init_data=None): | |
39 pass | |
40 | |
41 def save_data(self): | |
42 """Serialize the mission state for saving, etc.""" | |
43 return [] | |
44 | |
45 def attempt(self, equipment, state): | |
46 """Attempt the mission with the given equipment list. | |
47 | |
48 Returns a result object with the results of the mission.""" | |
49 return Result(FAILURE, 0, 0, "You can't succceed at this mission") | |
50 | |
51 | |
52 class PlaygroundBully(Mission): | |
53 | |
54 NAME = "Rob kids in the playground" | |
55 SHORT_DESCRIPTION = "Steal from those significantly weaker than yourself" | |
56 LONG_DESCRIPTION = ("It's not menancing, or lucrative, but when the bills" | |
57 " are due, no one cares how you earn the money") | |
58 | |
59 def attempt(sefl, equipment, state): | |
60 return Result(SUCCESS, 100, -1, "You devote your resources to" | |
61 " robbing kids in a playpark. It's not the finest moment" | |
62 " in your reign of terror, but at least you walked away" | |
63 " with a surprising amount of small change.") | |
64 | |
65 | |
66 class RansomChina(Mission): | |
67 | |
68 NAME = "Hold China to ransom" | |
69 SHORT_DESCRIPTION = "Surely a path to riches and fame" | |
70 LONG_DESCRIPTION = "Holding China to ransom. The rewards for" \ | |
71 " successfully threatening the largest country in the world" \ | |
72 " are great, but the risks are significant. Without " \ | |
73 "some serious firepower, the chances of success are small." | |
74 | |
75 def __init__(self, init_data=None): | |
76 # Track prior approaches to this mission | |
77 self._prior_attempts = [] | |
78 if init_data: | |
79 self._prior_attempts = init_data | |
80 | |
81 def save_data(self): | |
82 return self._prior_attempts | |
83 | |
84 def attempt(self, equipment, state): | |
85 failures = [] | |
86 reputation = 0 | |
87 for item in equipment: | |
88 if isinstance(item, DoomsdayVirus) and \ | |
89 item.NAME not in self._prior_attempts: | |
90 self._prior_attempts.add(item.NAME) | |
91 return Result(SUCCESS, 1000000, 1, "Trembling at the threat of" | |
92 " your doomsday virus, the chinese government pays the" | |
93 " ransom") | |
94 elif isinstance(item, DoomsdayVirus): | |
95 reputation = -1 | |
96 failures.append("'Hah, we've developed an antidote to your" | |
97 " virus, doctor'. You cannot threaten us with that" | |
98 " again'") | |
99 else: | |
100 failures.append("You fail to inspire fear with your %s" | |
101 % item.name) | |
102 return Result(FAILURE, 0, reputation, "\n".join(failures)) | |
103 | |
104 | |
105 class RobBank(Mission): | |
106 | |
107 NAME = "Rob the local bank" | |
108 SHORT_DESCRIPTION = "A trivial challenge, but easy money" | |
109 LONG_DESCRIPTION = "The security guards and local police are of minor" \ | |
110 " concern. Walk in, clean out the vault, walk out. Couldn't be" \ | |
111 " simpler." | |
112 | |
113 def attempt(self, equipment, state): | |
114 failures = [] | |
115 for item in equipment: | |
116 if isinstance(item, DoomsdayVirus): | |
117 if state.reputation < 10: | |
118 failures.append("The clerk doesn't realise the threat of" | |
119 " the vial you hold, and, although watching him" | |
120 " die in agony would be statisfying, you decide" | |
121 " it's not worth wasting this on something so" | |
122 " trivial") | |
123 else: | |
124 return Result(SUCCESS, 1000, 0, "Holding up a bank with" | |
125 " only a small vial of clear liquid. Now that" | |
126 " is power.") | |
127 elif isinstance(item, (MachineGun, LightningGun)): | |
128 return Result(SUCCESS, 1000, 0, "The threat of your weapons is" | |
129 " enough to inspire an impressive level of cooperation") | |
130 else: | |
131 failures.append("You fail to inspire fear with your %s" | |
132 % item.name) | |
133 return Result(FAILURE, 0, 0, "\n".join(failures)) | |
134 | |
135 | |
136 class DestroyMountRushmore(Mission): | |
137 | |
138 NAME = "Destroy Mount Rushmore" | |
139 SHORT_DESCRIPTION = "Monuments to other people? Intolerable" | |
140 LONG_DESCRIPTION = "While potentially expensive, destroying" \ | |
141 " major monument is a good way to secure your reputation." | |
142 | |
143 def attempt(self, equipment, state): | |
144 if not self.available: | |
145 raise RuntimeError('Cannot attempt an unavailable mission') | |
146 self.available = False | |
147 return Result(SUCCESS, 0, 100, | |
148 "Mount Rushmore is remarkably easy to destroy.") |