Mercurial > traipse_dev
annotate orpg/dieroller/die.py @ 155:bf799efe7a8a alpha
Traipse Alpha 'OpenRPG' {091125-02}
Traipse is a distribution of OpenRPG that is designed to be easy to setup and go.
Traipse also makes it easy for developers to work on code without fear of
sacrifice. 'Ornery-Orc' continues the trend of 'Grumpy' and adds fixes to the
code. 'Ornery-Orc's main goal is to offer more advanced features and enhance the
productivity of the user.
Update Summary (Cleaning up for Beta)
Added Bookmarks
Fix to Remote Admin Commands
Minor fix to text based Server
Fix to Pretty Print, from Core
Fix to Splitter Nodes not being created
Fix to massive amounts of images loading, from Core
Added 'boot' command to remote admin
Added confirmation window for sent nodes
Minor changes to allow for portability to an OpenSUSE linux OS
Miniatures Layer pop up box allows users to turn off Mini labels, from FlexiRPG
Zoom Mouse plugin added
Images added to Plugin UI
Switching to Element Tree
Map efficiency, from FlexiRPG
Added Status Bar to Update Manager
default_manifest.xml renamed to default_upmana.xml
Cleaner clode for saved repositories
New TrueDebug Class in orpg_log (See documentation for usage)
Mercurial's hgweb folder is ported to upmana
Pretty important update that can help remove thousands of dead children from your
gametree.
Children, <forms />, <group_atts />, <horizontal />, <cols />, <rows />, <height
/>, etc... are all tags now. Check your gametree and look for dead children!!
New Gametree Recursion method, mapping, and context sensitivity. !Infinite Loops
return error instead of freezing the software!
New Syntax added for custom PC sheets
Tip of the Day added, from Core and community
Fixed Whiteboard ID to prevent random line or text deleting. Modified ID's to
prevent non updated clients from ruining the fix.
author | sirebral |
---|---|
date | Wed, 25 Nov 2009 12:32:34 -0600 |
parents | 449a8900f9ac |
children | 565ab3d84430 |
rev | line source |
---|---|
155 | 1 #!/usr/bin/env python |
2 # Copyright (C) 2000-2001 The OpenRPG Project | |
3 # | |
4 # openrpg-dev@lists.sourceforge.net | |
5 # | |
6 # This program is free software; you can redistribute it and/or modify | |
7 # it under the terms of the GNU General Public License as published by | |
8 # the Free Software Foundation; either version 2 of the License, or | |
9 # (at your option) any later version. | |
10 # | |
11 # This program is distributed in the hope that it will be useful, | |
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 # GNU General Public License for more details. | |
15 # | |
16 # You should have received a copy of the GNU General Public License | |
17 # along with this program; if not, write to the Free Software | |
18 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | |
19 # -- | |
20 # | |
21 # File: die.py | |
22 # Author: Andrew Bennett | |
23 # Maintainer: | |
24 # Version: | |
25 # $Id: die.py,v 1.13 2007/03/13 17:53:42 digitalxero Exp $ | |
26 # | |
27 # Description: This class is used to make working with dice easier | |
28 # | |
29 | |
30 __version__ = "$Id: die.py,v 1.13 2007/03/13 17:53:42 digitalxero Exp $" | |
31 | |
32 | |
33 import random | |
34 import UserList | |
35 import copy | |
36 #import string | |
37 | |
38 class die_base(UserList.UserList): | |
39 | |
40 | |
41 def __init__(self,source = []): | |
42 if isinstance(source, (int, float, basestring)): | |
43 s = [] | |
44 s.append(di(source)) | |
45 else: | |
46 s = source | |
47 UserList.UserList.__init__(self,s) | |
48 | |
49 | |
50 | |
51 def sum(self): | |
52 s = 0 | |
53 for a in self.data: | |
54 s += int(a) | |
55 return s | |
56 | |
57 | |
58 def __lshift__(self,other): | |
59 if type(other) == type(3) or type(other) == type(3.0): | |
60 o = other | |
61 elif hasattr(other,"sum"): | |
62 o = other.sum() | |
63 else: | |
64 return None | |
65 | |
66 result = [] | |
67 for die in self: | |
68 if die < o: | |
69 result.append(die) | |
70 return self.__class__(result) | |
71 | |
72 | |
73 def __rshift__(self,other): | |
74 | |
75 if type(other) == type(3) or type(other) == type(3.0): | |
76 o = other | |
77 elif hasattr(other,"sum"): | |
78 o = other.sum() | |
79 else: | |
80 return None | |
81 | |
82 result = [] | |
83 for die in self: | |
84 if die > o: | |
85 result.append(die) | |
86 return self.__class__(result) | |
87 | |
88 | |
89 def __rlshift__(self,other): | |
90 return self.__rshift__(other) | |
91 | |
92 | |
93 def __rrshift__(self,other): | |
94 return self.__lshift__(other) | |
95 | |
96 | |
97 | |
98 def __str__(self): | |
99 if len(self.data) > 0: | |
100 myStr = "[" + str(self.data[0]) | |
101 for a in self.data[1:]: | |
102 myStr += "," | |
103 myStr += str(a) | |
104 myStr += "] = (" + str(self.sum()) + ")" | |
105 else: | |
106 myStr = "[] = (0)" | |
107 return myStr | |
108 | |
109 | |
110 def __lt__(self,other): | |
111 if type(other) == type(3) or type(other) == type(3.0): | |
112 return (self.sum() < other) | |
113 elif hasattr(other,"sum"): | |
114 return (self.sum() < other.sum()) | |
115 else: | |
116 return UserList.UserList.__lt__(self,other) | |
117 | |
118 | |
119 def __le__(self,other): | |
120 if type(other) == type(3) or type(other) == type(3.0): | |
121 return (self.sum() <= other) | |
122 elif hasattr(other,"sum"): | |
123 return (self.sum() <= other.sum()) | |
124 else: | |
125 return UserList.UserList.__le__(self,other) | |
126 | |
127 | |
128 def __eq__(self,other): | |
129 if type(other) == type(3) or type(other) == type(3.0): | |
130 return (self.sum() == other) | |
131 elif hasattr(other,"sum"): | |
132 return (self.sum() == other.sum()) | |
133 else: | |
134 return UserList.UserList.__eq__(self,other) | |
135 | |
136 | |
137 def __ne__(self,other): | |
138 if type(other) == type(3) or type(other) == type(3.0): | |
139 return (self.sum() != other) | |
140 elif hasattr(other,"sum"): | |
141 return (self.sum() != other.sum()) | |
142 else: | |
143 return UserList.UserList.__ne__(self,other) | |
144 | |
145 | |
146 def __gt__(self,other): | |
147 if type(other) == type(3) or type(other) == type(3.0): | |
148 return (self.sum() > other) | |
149 elif hasattr(other,"sum"): | |
150 return (self.sum() > other.sum()) | |
151 else: | |
152 return UserList.UserList.__gt__(self,other) | |
153 | |
154 | |
155 def __ge__(self,other): | |
156 if type(other) == type(3) or type(other) == type(3.0): | |
157 return (self.sum() >= other) | |
158 elif hasattr(other,"sum"): | |
159 return (self.sum() >= other.sum()) | |
160 else: | |
161 return UserList.UserList.__ge__(self,other) | |
162 | |
163 | |
164 def __cmp__(self,other): | |
165 # this function included for backwards compatibility | |
166 # As of 2.1, lists implement the "rich comparison" | |
167 # methods overloaded above. | |
168 if type(other) == type(3) or type(other) == type(3.0): | |
169 return cmp(self.sum(), other) | |
170 elif hasattr(other,"sum"): | |
171 return cmp(self.sum(), other.sum()) | |
172 else: | |
173 return UserList.UserList.__cmp__(self,other) | |
174 | |
175 | |
176 | |
177 def __rcmp__(self,other): | |
178 return self.__cmp__(other) | |
179 | |
180 | |
181 def __add__(self,other): | |
182 mycopy = copy.deepcopy(self) | |
183 if type(other) == type(3) or type(other) == type(3.0): | |
184 #if other < 0: | |
185 # return self.__sub__(-other) | |
186 #other = [di(other,other)] | |
187 other = [static_di(other)] | |
188 #return self.sum() + other | |
189 | |
190 elif type(other) == type("test"): | |
191 return self | |
192 mycopy.extend(other) | |
193 #result = UserList.UserList.__add__(mycopy,other) | |
194 return mycopy | |
195 | |
196 | |
197 def __iadd__(self,other): | |
198 return self.__add__(other) | |
199 | |
200 | |
201 def __radd__(self,other): | |
202 mycopy = copy.deepcopy(self) | |
203 if type(other) == type(3) or type(other) == type(3.0): | |
204 new_die = di(0) | |
205 new_die.set_value(other) | |
206 other = new_die | |
207 mycopy.insert(0,other) | |
208 return mycopy | |
209 | |
210 | |
211 def __int__(self): | |
212 return self.sum() | |
213 | |
214 | |
215 def __sub__(self,other): | |
216 mycopy = copy.deepcopy(self) | |
217 if type(other) == type(3) or type(other) == type(3.0): | |
218 neg_die = static_di(-other) | |
219 #neg_die.set_value(-other) | |
220 other = [neg_die] | |
221 #return self.sum() - other | |
222 else: | |
223 other = -other | |
224 mycopy.extend(other) | |
225 return mycopy | |
226 | |
227 | |
228 def __rsub__(self,other): | |
229 mycopy = -copy.deepcopy(self) | |
230 #print type(other) | |
231 if type(other) == type(3) or type(other) == type(3.0): | |
232 new_die = di(0) | |
233 new_die.set_value(other) | |
234 other = new_die | |
235 mycopy.insert(0,other) | |
236 return mycopy | |
237 | |
238 | |
239 def __isub__(self,other): | |
240 return self.__sub__(other) | |
241 | |
242 | |
243 def __mul__(self,other): | |
244 if type(other) == type(3) or type(other) == type(3.0): | |
245 return self.sum() * other | |
246 elif hasattr(other,"sum"): | |
247 return other.sum() * self.sum() | |
248 else: | |
249 return UserList.UserList.__mul__(self,other) | |
250 | |
251 | |
252 def __rmul__(self,other): | |
253 return self.__mul__(other) | |
254 | |
255 | |
256 def __div__(self,other): | |
257 if type(other) == type(3) or type(other) == type(3.0): | |
258 return float(self.sum()) / other | |
259 elif hasattr(other,"sum"): | |
260 return float(self.sum()) / other.sum() | |
261 else: | |
262 return UserList.UserList.__div__(self,other) | |
263 | |
264 | |
265 def __rdiv__(self,other): | |
266 if type(other) == type(3) or type(other) == type(3.0): | |
267 return other / float(self.sum()) | |
268 elif hasattr(other,"sum"): | |
269 return other.sum() / float(self.sum()) | |
270 else: | |
271 return UserList.UserList.__rdiv__(self,other) | |
272 | |
273 | |
274 def __mod__(self,other): | |
275 if type(other) == type(3) or type(other) == type(3.0): | |
276 return self.sum()%other | |
277 elif hasattr(other,"sum"): | |
278 return self.sum() % other.sum() | |
279 else: | |
280 return UserList.UserList.__mod__(self,other) | |
281 | |
282 | |
283 def __rmod__(self,other): | |
284 if type(other) == type(3) or type(other) == type(3.0): | |
285 return other % self.sum() | |
286 elif hasattr(other,"sum"): | |
287 return other.sum() % self.sum() | |
288 else: | |
289 return UserList.UserList.__rmod__(self,other) | |
290 | |
291 | |
292 def __neg__(self): | |
293 for i in range(len(self.data)): | |
294 self.data[i] = -self.data[i] | |
295 return self | |
296 | |
297 | |
298 def __pos__(self): | |
299 for i in range(len(self.data)): | |
300 self.data[i] = +self.data[i] | |
301 return self | |
302 | |
303 | |
304 def __abs__(self): | |
305 for i in range(len(self.data)): | |
306 self.data[i] = abs(self.data[i]) | |
307 return self | |
308 #return abs(self.sum()) | |
309 | |
310 | |
311 def __pow__(self,other): | |
312 if type(other) == type(3) or type(other) == type(3.0): | |
313 return self.sum() ** other | |
314 elif hasattr(other,"sum"): | |
315 return self.sum() ** other.sum() | |
316 else: | |
317 return UserList.UserList.__pow__(self,other) | |
318 | |
319 | |
320 | |
321 def __rpow__(self,other): | |
322 # We're overloading exponentiation of ints to create "other" number of dice | |
323 | |
324 if other >= 1: | |
325 result = self.__class__(self[0].sides) | |
326 for t in range(other-1): | |
327 result+=self.__class__(self[0].sides) | |
328 else: | |
329 result = None | |
330 | |
331 return result | |
332 | |
333 ### di class to handle actual dice | |
334 | |
335 class di: | |
336 | |
337 def __init__(self,sides,min=1): | |
338 self.sides = sides | |
339 self.history = None | |
340 self.value = None | |
341 self.target = None | |
342 self.roll(min) | |
343 | |
344 | |
345 def __str__(self): | |
346 if len(self.history) > 1: | |
347 return str(self.history) | |
348 else: | |
349 return str(self.value) | |
350 | |
351 | |
352 def __neg__(self): | |
353 self.value = -self.value | |
354 for i in range(len(self.history)): | |
355 self.history[i] = -self.history[i] | |
356 return self | |
357 | |
358 | |
359 def __pos__(self): | |
360 self.value = +self.value | |
361 for i in range(len(self.history)): | |
362 self.history[i] = +self.history[i] | |
363 return self | |
364 | |
365 | |
366 def __abs__(self): | |
367 self.value = abs(self.value) | |
368 for i in range(len(self.history)): | |
369 self.history[i] = abs(self.history[i]) | |
370 return self | |
371 | |
372 | |
373 def __repr__(self): | |
374 if len(self.history) > 1: | |
375 return str(self.history) | |
376 else: | |
377 return str(self.value) | |
378 | |
379 | |
380 def __int__(self): | |
381 return self.value | |
382 | |
383 | |
384 | |
385 def __lt__(self,other): | |
386 if type(other) == type(3) or type(other) == type(3.0): | |
387 return self.value < other | |
388 elif hasattr(other,"value"): | |
389 return self.value < other.value | |
390 else: | |
391 return self < other | |
392 | |
393 | |
394 def __le__(self,other): | |
395 if type(other) == type(3) or type(other) == type(3.0): | |
396 return self.value <= other | |
397 elif hasattr(other,"value"): | |
398 return self.value <= other.value | |
399 else: | |
400 return self <= other | |
401 | |
402 | |
403 def __eq__(self,other): | |
404 if type(other) == type(3) or type(other) == type(3.0): | |
405 return self.value == other | |
406 elif hasattr(other,"value"): | |
407 return self.value == other.value | |
408 else: | |
409 return self == other | |
410 | |
411 | |
412 def __ne__(self,other): | |
413 if type(other) == type(3) or type(other) == type(3.0): | |
414 return self.value != other | |
415 elif hasattr(other,"value"): | |
416 return self.value != other.value | |
417 else: | |
418 return self != other | |
419 | |
420 | |
421 def __gt__(self,other): | |
422 if type(other) == type(3) or type(other) == type(3.0): | |
423 return self.value > other | |
424 elif hasattr(other,"value"): | |
425 return self.value > other.value | |
426 else: | |
427 return self > other | |
428 | |
429 | |
430 def __ge__(self,other): | |
431 if type(other) == type(3) or type(other) == type(3.0): | |
432 return self.value >= other | |
433 elif hasattr(other,"value"): | |
434 return self.value >= other.value | |
435 else: | |
436 return self >= other | |
437 | |
438 | |
439 def __cmp__(self,other): | |
440 # this function included for backwards compatibility | |
441 # As of 2.1, lists implement the "rich comparison" | |
442 # methods overloaded above. | |
443 if type(other) == type(3) or type(other) == type(3.0): | |
444 return cmp(self.value, other) | |
445 elif hasattr(other,"value"): | |
446 return cmp(self.value, other.value) | |
447 else: | |
448 return cmp(self,other) | |
449 | |
450 | |
451 def roll(self,min=1): | |
452 if isinstance(self.sides, basestring) and self.sides.lower() == 'f': | |
453 self.value = random.randint(-1, 1) | |
454 else: | |
0
4385a7d0efd1
Deleted and repushed it with the 'grumpy-goblin' branch. I forgot a y
sirebral
parents:
diff
changeset
|
455 #self.value = random.randint(min, self.sides) |
155 | 456 self.value = int(random.uniform(min, self.sides+1)) |
457 self.history = [] | |
458 self.history.append(self.value) | |
459 | |
460 | |
461 def extraroll(self): | |
462 if isinstance(self.sides, basestring) and self.sides.lower() == 'f': | |
463 result = random.randint(-1, 1) | |
464 else: | |
0
4385a7d0efd1
Deleted and repushed it with the 'grumpy-goblin' branch. I forgot a y
sirebral
parents:
diff
changeset
|
465 #result = random.randint(1, self.sides) |
155 | 466 result = int(random.uniform(1,self.sides+1)) |
467 | |
468 self.value += result | |
469 self.history.append(result) | |
470 | |
471 | |
472 def lastroll(self): | |
473 return self.history[len(self.history)-1] | |
474 | |
475 | |
476 def set_value(self,value): | |
477 self.value = value | |
478 self.history = [] | |
479 self.history.append(self.value) | |
480 | |
481 | |
482 def modify(self,mod): | |
483 self.value += mod | |
484 self.history.append(mod) | |
485 | |
486 | |
487 def gethistory(self): | |
488 return self.history[:] | |
489 | |
490 class static_di(di): | |
491 | |
492 def __init__(self,value): | |
493 di.__init__(self,value,value) | |
494 self.set_value(value) | |
495 | |
496 | |
497 class std(die_base): | |
498 | |
499 def __init__(self,source=[]): | |
500 die_base.__init__(self,source) | |
501 | |
502 # Examples of adding member functions through inheritance. | |
503 | |
504 | |
505 def ascending(self): | |
506 result = self[:] | |
507 result.sort() | |
508 return result | |
509 | |
510 | |
511 def descending(self): | |
512 result = self[:] | |
513 result.sort() | |
514 result.reverse() | |
515 return result | |
516 | |
517 | |
518 def takeHighest(self,num_dice): | |
519 return self.descending()[:num_dice] | |
520 | |
521 | |
522 def takeLowest(self,num_dice): | |
523 return self.ascending()[:num_dice] | |
524 | |
525 | |
526 def extra(self,num): | |
527 for i in range(len(self.data)): | |
528 if self.data[i].lastroll() >= num: | |
529 self.data[i].extraroll() | |
530 return self | |
531 | |
532 | |
533 def open(self,num): | |
534 if num <= 1: | |
535 self | |
536 done = 1 | |
537 for i in range(len(self.data)): | |
538 if self.data[i].lastroll() >= num: | |
539 self.data[i].extraroll() | |
540 done = 0 | |
541 if done: | |
542 return self | |
543 else: | |
544 return self.open(num) | |
545 | |
546 | |
547 def minroll(self,min): | |
548 for i in range(len(self.data)): | |
549 if self.data[i].lastroll() < min: | |
550 self.data[i].roll(min) | |
551 return self | |
552 | |
553 | |
554 def each(self,mod): | |
555 mod = int(mod) | |
556 for i in range(len(self.data)): | |
557 self.data[i].modify(mod) | |
558 return self | |
559 | |
560 | |
561 | |
562 def vs(self, target): | |
563 for dn in self.data: | |
564 dn.target = target | |
565 return self | |
566 | |
567 | |
568 ## If we are testing against a saving throw, we check for | |
569 ## greater than or equal to against the target value and | |
570 ## we only return the number of successful saves. A negative | |
571 ## value will never be generated. | |
572 | |
573 def sum(self): | |
574 retValue = 0 | |
575 for dn in self.data: | |
576 setValue = reduce( lambda x, y : int(x)+int(y), dn.history ) | |
577 if dn.target: | |
578 if setValue >= dn.target: | |
579 retValue += 1 | |
580 | |
581 else: | |
582 retValue += setValue | |
583 | |
584 return retValue |