Mercurial > traipse_dev
comparison orpg/dieroller/srex.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: srex.py | |
23 # Original Author: Michael Edwards (AKA akoman) | |
24 # Maintainer: | |
25 # Original 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 | |
46 # Modified by: Darloth | |
47 # Mod Version: 1.1 | |
48 # Modified Desc: | |
49 # I've altered the vs call to make it report successes against every target number (tn) | |
50 # in a specified (default 3) range, with the original as median. | |
51 # This reduces rerolling if the TN was calculated incorrectly, and is also very useful | |
52 # when people are rolling against multiple TNs, which is the case with most area-effect spells. | |
53 # To aid in picking the specified TN out from the others, it will be in bold. | |
54 # vswide is a version which can be used with no arguments, or can be used to get a very wide range, by | |
55 # directly specifying the upper bound (Which is limited to 30) | |
56 | |
57 from die import * | |
58 | |
59 __version__ = "1.1" | |
60 | |
61 class srex(std): | |
62 def __init__(self,source=[]): | |
63 std.__init__(self,source) | |
64 | |
65 def vs(self,actualtarget=4,tnrange=3): #reports all tns around specified, max distance of range | |
66 return srVs(self,actualtarget,(actualtarget-tnrange),(actualtarget+tnrange)) | |
67 | |
68 def vswide(self,actualtarget=4,maxtarget=12): #wide simply means it reports TNs from 2 to a specified max. | |
69 return srVs(self,actualtarget,2,maxtarget) | |
70 | |
71 def open(self): #unchanged from standard shadowrun open. | |
72 return srOpen(self) | |
73 | |
74 class srVs(std): | |
75 def __init__(self,source=[],actualtarget=4,mintn=2,maxtn=12): | |
76 std.__init__(self, source) | |
77 if actualtarget > 30: | |
78 actualtarget = 30 | |
79 if mintn > 30: | |
80 mintn = 30 | |
81 if maxtn > 30: | |
82 maxtn = 30 | |
83 # In Shadowrun, not target number may be below 2. Any | |
84 # thing lower is scaled up. | |
85 if actualtarget < 2: | |
86 self.target = 2 | |
87 else: | |
88 self.target = actualtarget | |
89 #if the target number is higher than max (Mainly for wide rolls) then increase max to tn | |
90 if actualtarget > maxtn: | |
91 maxtn = actualtarget | |
92 #store minimum for later use as well, also in result printing section. | |
93 if mintn < 2: | |
94 self.mintn = 2 | |
95 else: | |
96 self.mintn = mintn | |
97 self.maxtn = maxtn #store for later use in printing results. (Yeah, these comments are now disordered) | |
98 | |
99 # Shadowrun was built to use the d6 but in the interests of experimentation I have | |
100 # made the dieroller generic enough to use any die type | |
101 self.openended(self[0].sides) | |
102 | |
103 def openended(self,num): | |
104 if num <= 1: | |
105 self | |
106 done = 1 | |
107 | |
108 #reroll dice if they hit the highest number, until they are greater than the max TN (recursive) | |
109 for i in range(len(self.data)): | |
110 if (self.data[i].lastroll() >= num) and (self.data[i] < self.maxtn): | |
111 self.data[i].extraroll() | |
112 done = 0 | |
113 if done: | |
114 return self | |
115 else: | |
116 return self.openended(num) | |
117 | |
118 #count successes, by looping through each die, and checking it against the currently set TN | |
119 def __sum__(self): | |
120 s = 0 | |
121 for r in self.data: | |
122 if r >= self.target: | |
123 s += 1 | |
124 return s | |
125 | |
126 #a modified sum, but this one takes a target argument, and is there because otherwise it is difficult to loop through | |
127 #tns counting successes against each one without changing target, which is rather dangerous as the original TN could | |
128 #easily be lost. | |
129 def xsum(self,curtarget): | |
130 s = 0 | |
131 for r in self.data: | |
132 if r >= curtarget: | |
133 s += 1 | |
134 return s | |
135 | |
136 | |
137 def __str__(self): | |
138 if len(self.data) > 0: | |
139 myStr = "[" + str(self.data[0]) | |
140 for a in self.data[1:]: | |
141 myStr += "," | |
142 myStr += str(a) | |
143 myStr += "] Results: " | |
144 #cycle through from mintn to maxtn, summing successes for each separate TN | |
145 for targ in range(self.mintn,self.maxtn+1): | |
146 if targ == self.target: | |
147 myStr += "<b>" | |
148 myStr += "(" + str(self.xsum(targ)) + " vs " + str(targ) + ") " | |
149 if targ == self.target: | |
150 myStr += "</b>" | |
151 else: | |
152 myStr = "[] = (0)" | |
153 | |
154 return myStr | |
155 | |
156 class srOpen(std): | |
157 def __init__(self,source=[]): | |
158 std.__init__(self,source) | |
159 self.openended(self[0].sides) | |
160 | |
161 def openended(self,num): | |
162 if num <= 1: | |
163 self | |
164 done = 1 | |
165 for i in range(len(self.data)): | |
166 if self.data[i].lastroll() == num: | |
167 self.data[i].extraroll() | |
168 done = 0 | |
169 if done: | |
170 return self | |
171 else: | |
172 return self.openended(num) | |
173 | |
174 def __sum__(self): | |
175 s = 0 | |
176 for r in self.data: | |
177 if r > s: | |
178 s = r | |
179 return s | |
180 | |
181 def __str__(self): | |
182 if len(self.data) > 0: | |
183 myStr = "[" + str(self.data[0]) | |
184 for a in self.data[1:]: | |
185 myStr += "," | |
186 myStr += str(a) | |
187 self.takeHighest(1) | |
188 myStr += "] for a result of (" + str(self.__sum__().__int__()) + ")" | |
189 else: | |
190 myStr = "[] = (0)" | |
191 | |
192 return myStr |