comparison orpg/dieroller/shadowrun.py @ 0:4385a7d0efd1 grumpy-goblin

Deleted and repushed it with the 'grumpy-goblin' branch. I forgot a y
author sirebral
date Tue, 14 Jul 2009 16:41:58 -0500
parents
children 449a8900f9ac
comparison
equal deleted inserted replaced
-1:000000000000 0:4385a7d0efd1
1 ## a vs die roller as used by WOD games
2 #!/usr/bin/env python
3 # Copyright (C) 2000-2001 The OpenRPG Project
4 #
5 # openrpg-dev@lists.sourceforge.net
6 #
7 # This program is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 2 of the License, or
10 # (at your option) any later version.
11 #
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with this program; if not, write to the Free Software
19 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 # --
21 #
22 # File: shadowrun.py
23 # Author: Michael Edwards (AKA akoman)
24 # Maintainer:
25 # Version: 1.0
26 #
27 # Description: A modified form of the World of Darkness die roller to
28 # conform to ShadowRun rules-sets. Thanks to the ORPG team
29 # for the original die rollers.
30 # Thanks to tdb30_ for letting me think out loud with him.
31 # I take my hint from the HERO dieroller: It creates for wildly variant options
32 # Further, .vs and .open do not work together in any logical way. One method of
33 # chaining them results in a [Bad Dice Format] and the other results in a standard
34 # output from calling .open()
35
36 # vs is a classic 'comparison' method function, with one difference. It uses a
37 # c&p'ed .open(int) from die.py but makes sure that once the target has been exceeded
38 # then it stops rerolling. The overhead from additional boolean checking is probably
39 # greater than the gains from not over-rolling. The behaviour is in-line with
40 # Shadowrun Third Edition which recommends not rolling once you've exceeded the target
41 # open is an override of .open(int) in die.py. The reason is pretty simple. In die.py open
42 # refers to 'open-ended rolling' whereas in Shadowrun it refers to an 'Open Test' where
43 # the objective is to find the highest die total out of rolled dice. This is then generally
44 # used as the target in a 'Success Test' (for which .vs functions)
45 from die import *
46
47 __version__ = "1.0"
48
49 class shadowrun(std):
50 def __init__(self,source=[],target=2):
51 std.__init__(self,source)
52
53 def vs(self,target):
54 return srVs(self, target)
55
56 def open(self):
57 return srOpen(self)
58
59 class srVs(std):
60 def __init__(self,source=[], target=2):
61 std.__init__(self, source)
62 # In Shadowrun, not target number may be below 2. All defaults are set to two and any
63 # thing lower is scaled up.
64 if target < 2:
65 self.target = 2
66 else:
67 self.target = target
68 # Shadowrun was built to use the d6 but in the interests of experimentation I have
69 # made the dieroller generic enough to use any die type
70 self.openended(self[0].sides)
71
72 def openended(self,num):
73 if num <= 1:
74 self
75 done = 1
76 for i in range(len(self.data)):
77 if (self.data[i].lastroll() >= num) and (self.data[i] < self.target):
78 self.data[i].extraroll()
79 done = 0
80 if done:
81 return self
82 else:
83 return self.openended(num)
84
85 def __sum__(self):
86 s = 0
87 for r in self.data:
88 if r >= self.target:
89 s += 1
90 return s
91
92 def __str__(self):
93 if len(self.data) > 0:
94 myStr = "[" + str(self.data[0])
95 for a in self.data[1:]:
96 myStr += ","
97 myStr += str(a)
98 myStr += "] vs " + str(self.target) + " for a result of (" + str(self.sum()) + ")"
99 else:
100 myStr = "[] = (0)"
101
102 return myStr
103
104 class srOpen(std):
105 def __init__(self,source=[]):
106 std.__init__(self,source)
107 self.openended(self[0].sides)
108
109 def openended(self,num):
110 if num <= 1:
111 self
112 done = 1
113 for i in range(len(self.data)):
114 if self.data[i].lastroll() == num:
115 self.data[i].extraroll()
116 done = 0
117 if done:
118 return self
119 else:
120 return self.openended(num)
121
122 def __sum__(self):
123 s = 0
124 for r in self.data:
125 if r > s:
126 s = r
127 return s
128
129 def __str__(self):
130 if len(self.data) > 0:
131 myStr = "[" + str(self.data[0])
132 for a in self.data[1:]:
133 myStr += ","
134 myStr += str(a)
135 self.takeHighest(1)
136 myStr += "] for a result of (" + str(self.__sum__().__int__()) + ")"
137 else:
138 myStr = "[] = (0)"
139
140 return myStr