Mercurial > sypikslang
annotate gamelib/mission.py @ 13:c0966997e0c5
Use a results object instead of tuple (will hopefully make changign stuff easier later)
author | Neil Muller <drnlmuller@gmail.com> |
---|---|
date | Sun, 06 May 2012 15:56:22 +0200 |
parents | 9f8def7d70d0 |
children | 0849ab5304cf |
rev | line source |
---|---|
5 | 1 # -*- coding: utf-8 -*- |
2 # vim:fileencoding=utf-8 ai ts=4 sts=4 et sw=4 | |
3 | |
4 from product import DoomsdayVirus, MachineGun, TeslaGun | |
5 | |
6 | |
13
c0966997e0c5
Use a results object instead of tuple (will hopefully make changign stuff easier later)
Neil Muller <drnlmuller@gmail.com>
parents:
10
diff
changeset
|
7 class Result(object): |
c0966997e0c5
Use a results object instead of tuple (will hopefully make changign stuff easier later)
Neil Muller <drnlmuller@gmail.com>
parents:
10
diff
changeset
|
8 """Results of a mission""" |
c0966997e0c5
Use a results object instead of tuple (will hopefully make changign stuff easier later)
Neil Muller <drnlmuller@gmail.com>
parents:
10
diff
changeset
|
9 |
c0966997e0c5
Use a results object instead of tuple (will hopefully make changign stuff easier later)
Neil Muller <drnlmuller@gmail.com>
parents:
10
diff
changeset
|
10 def __init__(self, outcome, money, reputation, msg): |
c0966997e0c5
Use a results object instead of tuple (will hopefully make changign stuff easier later)
Neil Muller <drnlmuller@gmail.com>
parents:
10
diff
changeset
|
11 self.outcome = outcome |
c0966997e0c5
Use a results object instead of tuple (will hopefully make changign stuff easier later)
Neil Muller <drnlmuller@gmail.com>
parents:
10
diff
changeset
|
12 self.money = money |
c0966997e0c5
Use a results object instead of tuple (will hopefully make changign stuff easier later)
Neil Muller <drnlmuller@gmail.com>
parents:
10
diff
changeset
|
13 self.reputation = reputation |
c0966997e0c5
Use a results object instead of tuple (will hopefully make changign stuff easier later)
Neil Muller <drnlmuller@gmail.com>
parents:
10
diff
changeset
|
14 self.message = msg |
c0966997e0c5
Use a results object instead of tuple (will hopefully make changign stuff easier later)
Neil Muller <drnlmuller@gmail.com>
parents:
10
diff
changeset
|
15 self.applied = False |
c0966997e0c5
Use a results object instead of tuple (will hopefully make changign stuff easier later)
Neil Muller <drnlmuller@gmail.com>
parents:
10
diff
changeset
|
16 |
c0966997e0c5
Use a results object instead of tuple (will hopefully make changign stuff easier later)
Neil Muller <drnlmuller@gmail.com>
parents:
10
diff
changeset
|
17 def apply(self, state): |
c0966997e0c5
Use a results object instead of tuple (will hopefully make changign stuff easier later)
Neil Muller <drnlmuller@gmail.com>
parents:
10
diff
changeset
|
18 if not self.applied: |
c0966997e0c5
Use a results object instead of tuple (will hopefully make changign stuff easier later)
Neil Muller <drnlmuller@gmail.com>
parents:
10
diff
changeset
|
19 state.money += self.money |
c0966997e0c5
Use a results object instead of tuple (will hopefully make changign stuff easier later)
Neil Muller <drnlmuller@gmail.com>
parents:
10
diff
changeset
|
20 state.reputation += self.reputation |
c0966997e0c5
Use a results object instead of tuple (will hopefully make changign stuff easier later)
Neil Muller <drnlmuller@gmail.com>
parents:
10
diff
changeset
|
21 self.applied = True |
c0966997e0c5
Use a results object instead of tuple (will hopefully make changign stuff easier later)
Neil Muller <drnlmuller@gmail.com>
parents:
10
diff
changeset
|
22 else: |
c0966997e0c5
Use a results object instead of tuple (will hopefully make changign stuff easier later)
Neil Muller <drnlmuller@gmail.com>
parents:
10
diff
changeset
|
23 raise RuntimeError('attempted to apply result twice') |
c0966997e0c5
Use a results object instead of tuple (will hopefully make changign stuff easier later)
Neil Muller <drnlmuller@gmail.com>
parents:
10
diff
changeset
|
24 |
c0966997e0c5
Use a results object instead of tuple (will hopefully make changign stuff easier later)
Neil Muller <drnlmuller@gmail.com>
parents:
10
diff
changeset
|
25 |
5 | 26 class Mission(object): |
27 """Base class for the mission objects. | |
28 Missions have a name, short description (for list displays) and | |
29 long description (which may contain clues about approaches)""" | |
30 | |
31 NAME = "Generic Mission" | |
32 SHORT_DESCRIPTION = None | |
33 LONG_DESCRIPTION = None | |
10
9f8def7d70d0
Rework mission objects to include reputation
Neil Muller <drnlmuller@gmail.com>
parents:
5
diff
changeset
|
34 available = True |
5 | 35 |
10
9f8def7d70d0
Rework mission objects to include reputation
Neil Muller <drnlmuller@gmail.com>
parents:
5
diff
changeset
|
36 def attempt(self, equipment, state): |
5 | 37 """Attempt the mission with the given equipment list. |
38 | |
13
c0966997e0c5
Use a results object instead of tuple (will hopefully make changign stuff easier later)
Neil Muller <drnlmuller@gmail.com>
parents:
10
diff
changeset
|
39 Returns a result object with the results of the mission.""" |
c0966997e0c5
Use a results object instead of tuple (will hopefully make changign stuff easier later)
Neil Muller <drnlmuller@gmail.com>
parents:
10
diff
changeset
|
40 return Result(False, 0, 0, "You can't succceed at this mission") |
5 | 41 |
42 | |
43 class RansomChina(Mission): | |
44 | |
45 NAME = "Hold China to ransom" | |
46 SHORT_DESCRIPTION = "Surely a path to riches and fame" | |
47 LONG_DESCRIPTION = "Holding China to ransom. The rewards for" \ | |
48 " successfully threatening the largest country in the world" \ | |
49 " are great, but the risks are significant. Without " \ | |
50 "some serious firepower, the chances of success are small." | |
51 | |
52 def __init__(self): | |
53 # Track prior approaches to this mission | |
54 self._prior_attempts = [] | |
55 | |
10
9f8def7d70d0
Rework mission objects to include reputation
Neil Muller <drnlmuller@gmail.com>
parents:
5
diff
changeset
|
56 def attempt(self, equipment, state): |
5 | 57 failures = [] |
13
c0966997e0c5
Use a results object instead of tuple (will hopefully make changign stuff easier later)
Neil Muller <drnlmuller@gmail.com>
parents:
10
diff
changeset
|
58 reputation = 0 |
5 | 59 for item in equipment: |
60 if isinstance(item, DoomsdayVirus) and \ | |
61 item not in self._prior_attempts: | |
62 self._prior_attempts.add(item) | |
13
c0966997e0c5
Use a results object instead of tuple (will hopefully make changign stuff easier later)
Neil Muller <drnlmuller@gmail.com>
parents:
10
diff
changeset
|
63 return Result(True, 1000000, 1, "Trembling at the threat of" |
c0966997e0c5
Use a results object instead of tuple (will hopefully make changign stuff easier later)
Neil Muller <drnlmuller@gmail.com>
parents:
10
diff
changeset
|
64 " your doomsday virus, the chinese government pays the" |
c0966997e0c5
Use a results object instead of tuple (will hopefully make changign stuff easier later)
Neil Muller <drnlmuller@gmail.com>
parents:
10
diff
changeset
|
65 " ransom") |
5 | 66 elif isinstance(item, DoomsdayVirus): |
13
c0966997e0c5
Use a results object instead of tuple (will hopefully make changign stuff easier later)
Neil Muller <drnlmuller@gmail.com>
parents:
10
diff
changeset
|
67 reputation = -1 |
5 | 68 failures.append("'Hah, we've developed an antidote to your" |
69 " virus, doctor'. You cannot threaten us with that" | |
70 " again'") | |
71 else: | |
72 failures.append("You fail to inspire fear with your %s" | |
73 % item.name) | |
13
c0966997e0c5
Use a results object instead of tuple (will hopefully make changign stuff easier later)
Neil Muller <drnlmuller@gmail.com>
parents:
10
diff
changeset
|
74 return Result(False, 0, reputation, "\n".join(failures)) |
5 | 75 |
76 | |
77 class RobBank(Mission): | |
78 | |
79 NAME = "Rob the local bank" | |
80 SHORT_DESCRIPTION = "A trivial challenge, but easy money" | |
81 LONG_DESCRIPTION = "The security guards and local police are of minor" \ | |
82 " concern. Walk in, clean out the vault, walk out. Couldn't be" \ | |
83 " simpler." | |
84 | |
10
9f8def7d70d0
Rework mission objects to include reputation
Neil Muller <drnlmuller@gmail.com>
parents:
5
diff
changeset
|
85 def attempt(self, equipment, state): |
5 | 86 failures = [] |
87 for item in equipment: | |
88 if isinstance(item, DoomsdayVirus): | |
10
9f8def7d70d0
Rework mission objects to include reputation
Neil Muller <drnlmuller@gmail.com>
parents:
5
diff
changeset
|
89 if state.reputation < 10: |
9f8def7d70d0
Rework mission objects to include reputation
Neil Muller <drnlmuller@gmail.com>
parents:
5
diff
changeset
|
90 failures.append("The clerk doesn't realise the threat of" |
9f8def7d70d0
Rework mission objects to include reputation
Neil Muller <drnlmuller@gmail.com>
parents:
5
diff
changeset
|
91 " the vial you hold, and, although watching him" |
9f8def7d70d0
Rework mission objects to include reputation
Neil Muller <drnlmuller@gmail.com>
parents:
5
diff
changeset
|
92 " die in agony would be statisfying, you decide" |
9f8def7d70d0
Rework mission objects to include reputation
Neil Muller <drnlmuller@gmail.com>
parents:
5
diff
changeset
|
93 " it's not worth wasting this on something so" |
9f8def7d70d0
Rework mission objects to include reputation
Neil Muller <drnlmuller@gmail.com>
parents:
5
diff
changeset
|
94 " trivial") |
9f8def7d70d0
Rework mission objects to include reputation
Neil Muller <drnlmuller@gmail.com>
parents:
5
diff
changeset
|
95 else: |
13
c0966997e0c5
Use a results object instead of tuple (will hopefully make changign stuff easier later)
Neil Muller <drnlmuller@gmail.com>
parents:
10
diff
changeset
|
96 return Result(True, 1000, 0, "Holding up a bank with only" |
c0966997e0c5
Use a results object instead of tuple (will hopefully make changign stuff easier later)
Neil Muller <drnlmuller@gmail.com>
parents:
10
diff
changeset
|
97 " a small vial of clear liquid. Now that is power.") |
5 | 98 elif isinstance(item, MachineGun) or isinstance(item, TeslaGun): |
13
c0966997e0c5
Use a results object instead of tuple (will hopefully make changign stuff easier later)
Neil Muller <drnlmuller@gmail.com>
parents:
10
diff
changeset
|
99 return Result(True, 1000, 0, "The threat of your weapons is" |
c0966997e0c5
Use a results object instead of tuple (will hopefully make changign stuff easier later)
Neil Muller <drnlmuller@gmail.com>
parents:
10
diff
changeset
|
100 " enough to inspire an impressive level of cooperation") |
5 | 101 else: |
102 failures.append("You fail to inspire fear with your %s" | |
103 % item.name) | |
13
c0966997e0c5
Use a results object instead of tuple (will hopefully make changign stuff easier later)
Neil Muller <drnlmuller@gmail.com>
parents:
10
diff
changeset
|
104 return Result(False, 0, 0, "\n".join(failures)) |
10
9f8def7d70d0
Rework mission objects to include reputation
Neil Muller <drnlmuller@gmail.com>
parents:
5
diff
changeset
|
105 |
9f8def7d70d0
Rework mission objects to include reputation
Neil Muller <drnlmuller@gmail.com>
parents:
5
diff
changeset
|
106 |
9f8def7d70d0
Rework mission objects to include reputation
Neil Muller <drnlmuller@gmail.com>
parents:
5
diff
changeset
|
107 class DestroyMountRushmore(Mission): |
9f8def7d70d0
Rework mission objects to include reputation
Neil Muller <drnlmuller@gmail.com>
parents:
5
diff
changeset
|
108 |
9f8def7d70d0
Rework mission objects to include reputation
Neil Muller <drnlmuller@gmail.com>
parents:
5
diff
changeset
|
109 NAME = "Destroy Mount Rushmore" |
9f8def7d70d0
Rework mission objects to include reputation
Neil Muller <drnlmuller@gmail.com>
parents:
5
diff
changeset
|
110 SHORT_DESCRIPTION = "Monuments to other people? Intolerable" |
9f8def7d70d0
Rework mission objects to include reputation
Neil Muller <drnlmuller@gmail.com>
parents:
5
diff
changeset
|
111 LONG_DESCRIPTION = "While potentially expensive, destroying" \ |
9f8def7d70d0
Rework mission objects to include reputation
Neil Muller <drnlmuller@gmail.com>
parents:
5
diff
changeset
|
112 " major monument is a good way to secure your reputation." |
9f8def7d70d0
Rework mission objects to include reputation
Neil Muller <drnlmuller@gmail.com>
parents:
5
diff
changeset
|
113 |
9f8def7d70d0
Rework mission objects to include reputation
Neil Muller <drnlmuller@gmail.com>
parents:
5
diff
changeset
|
114 def attempt(self, equipment, state): |
9f8def7d70d0
Rework mission objects to include reputation
Neil Muller <drnlmuller@gmail.com>
parents:
5
diff
changeset
|
115 if not self.available: |
13
c0966997e0c5
Use a results object instead of tuple (will hopefully make changign stuff easier later)
Neil Muller <drnlmuller@gmail.com>
parents:
10
diff
changeset
|
116 raise RuntimeError('Cannot attempt an unavailable mission') |
10
9f8def7d70d0
Rework mission objects to include reputation
Neil Muller <drnlmuller@gmail.com>
parents:
5
diff
changeset
|
117 self.available = False |
13
c0966997e0c5
Use a results object instead of tuple (will hopefully make changign stuff easier later)
Neil Muller <drnlmuller@gmail.com>
parents:
10
diff
changeset
|
118 return Result(True, 0, 100, |
c0966997e0c5
Use a results object instead of tuple (will hopefully make changign stuff easier later)
Neil Muller <drnlmuller@gmail.com>
parents:
10
diff
changeset
|
119 "Mount Rushmore is remarkably easy to destroy.") |