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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
5
dd9046d0680c Sketch in mission objects
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
1 # -*- coding: utf-8 -*-
dd9046d0680c Sketch in mission objects
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
2 # vim:fileencoding=utf-8 ai ts=4 sts=4 et sw=4
dd9046d0680c Sketch in mission objects
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
3
dd9046d0680c Sketch in mission objects
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
4 from product import DoomsdayVirus, MachineGun, TeslaGun
dd9046d0680c Sketch in mission objects
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
5
dd9046d0680c Sketch in mission objects
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
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
dd9046d0680c Sketch in mission objects
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
26 class Mission(object):
dd9046d0680c Sketch in mission objects
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
27 """Base class for the mission objects.
dd9046d0680c Sketch in mission objects
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
28 Missions have a name, short description (for list displays) and
dd9046d0680c Sketch in mission objects
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
29 long description (which may contain clues about approaches)"""
dd9046d0680c Sketch in mission objects
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
30
dd9046d0680c Sketch in mission objects
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
31 NAME = "Generic Mission"
dd9046d0680c Sketch in mission objects
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
32 SHORT_DESCRIPTION = None
dd9046d0680c Sketch in mission objects
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
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
dd9046d0680c Sketch in mission objects
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
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
dd9046d0680c Sketch in mission objects
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
37 """Attempt the mission with the given equipment list.
dd9046d0680c Sketch in mission objects
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
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
dd9046d0680c Sketch in mission objects
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
41
dd9046d0680c Sketch in mission objects
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
42
dd9046d0680c Sketch in mission objects
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
43 class RansomChina(Mission):
dd9046d0680c Sketch in mission objects
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
44
dd9046d0680c Sketch in mission objects
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
45 NAME = "Hold China to ransom"
dd9046d0680c Sketch in mission objects
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
46 SHORT_DESCRIPTION = "Surely a path to riches and fame"
dd9046d0680c Sketch in mission objects
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
47 LONG_DESCRIPTION = "Holding China to ransom. The rewards for" \
dd9046d0680c Sketch in mission objects
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
48 " successfully threatening the largest country in the world" \
dd9046d0680c Sketch in mission objects
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
49 " are great, but the risks are significant. Without " \
dd9046d0680c Sketch in mission objects
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
50 "some serious firepower, the chances of success are small."
dd9046d0680c Sketch in mission objects
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
51
dd9046d0680c Sketch in mission objects
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
52 def __init__(self):
dd9046d0680c Sketch in mission objects
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
53 # Track prior approaches to this mission
dd9046d0680c Sketch in mission objects
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
54 self._prior_attempts = []
dd9046d0680c Sketch in mission objects
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
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
dd9046d0680c Sketch in mission objects
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
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
dd9046d0680c Sketch in mission objects
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
59 for item in equipment:
dd9046d0680c Sketch in mission objects
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
60 if isinstance(item, DoomsdayVirus) and \
dd9046d0680c Sketch in mission objects
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
61 item not in self._prior_attempts:
dd9046d0680c Sketch in mission objects
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
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
dd9046d0680c Sketch in mission objects
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
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
dd9046d0680c Sketch in mission objects
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
68 failures.append("'Hah, we've developed an antidote to your"
dd9046d0680c Sketch in mission objects
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
69 " virus, doctor'. You cannot threaten us with that"
dd9046d0680c Sketch in mission objects
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
70 " again'")
dd9046d0680c Sketch in mission objects
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
71 else:
dd9046d0680c Sketch in mission objects
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
72 failures.append("You fail to inspire fear with your %s"
dd9046d0680c Sketch in mission objects
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
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
dd9046d0680c Sketch in mission objects
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
75
dd9046d0680c Sketch in mission objects
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
76
dd9046d0680c Sketch in mission objects
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
77 class RobBank(Mission):
dd9046d0680c Sketch in mission objects
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
78
dd9046d0680c Sketch in mission objects
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
79 NAME = "Rob the local bank"
dd9046d0680c Sketch in mission objects
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
80 SHORT_DESCRIPTION = "A trivial challenge, but easy money"
dd9046d0680c Sketch in mission objects
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
81 LONG_DESCRIPTION = "The security guards and local police are of minor" \
dd9046d0680c Sketch in mission objects
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
82 " concern. Walk in, clean out the vault, walk out. Couldn't be" \
dd9046d0680c Sketch in mission objects
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
83 " simpler."
dd9046d0680c Sketch in mission objects
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
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
dd9046d0680c Sketch in mission objects
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
86 failures = []
dd9046d0680c Sketch in mission objects
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
87 for item in equipment:
dd9046d0680c Sketch in mission objects
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
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
dd9046d0680c Sketch in mission objects
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
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
dd9046d0680c Sketch in mission objects
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
101 else:
dd9046d0680c Sketch in mission objects
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
102 failures.append("You fail to inspire fear with your %s"
dd9046d0680c Sketch in mission objects
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
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.")