Advertisement
tchnmncr

magicalc.py

Dec 30th, 2015
344
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.71 KB | None | 0 0
  1. """
  2.    MagiCalc -- Magical Probability Calculator, Python edition
  3.    by tchnmncr, hyperritual.com
  4.    
  5.    The MagiCalc class calculates magically modified probability per Peter J.
  6.     Carroll's equations of magic in _The Octavo_, _The Apophenion_, and _Liber
  7.     Kaos_. The public methods all require five parameters in the range of 0 to
  8.     1 (numbers outside the range are constrained to 0 or 1) for natural proba-
  9.     bility, gnosis, link, subliminalization, and belief, respectively. For a
  10.     graphical version of MagiCalc made with Processing, see
  11.     http://hyperritual.com/blog/magicalc-2/.
  12.    
  13.     Ideally, I would rewrite the class to use Python's decimal module, but for
  14.     now it uses float arithmetic.
  15.    
  16.     The public methods are:
  17.     calc_spell: returns the calculated spell probability
  18.     calc_antispell: returns the calculated antispell probability
  19.     spell_is_strong: returns True if p_m > p, else False
  20.     antispell_is_strong: returns True if p_m < p, else False
  21.     calc_spell_verbose: prints the values for m and p_m, and tells you
  22.         whether p_m > p
  23.     calc_antispell_verbose: prints the values for m and p_m, and tells you
  24.         whether p_m < p
  25.    
  26.     Again, each method has five parameters in the form of
  27.     calc_spell(p, g, l, s, b)
  28.     e.g.,
  29.     calc_spell(0.5, 0.9, 0.8, 0.9, 0.7)
  30.        
  31.     Usage examples:
  32.     >>> mc = MagiCalc() # instantiates a magical probability calculator
  33.     >>> print(mc.calc_spell(0.5, 0.9, 0.8, 0.9, 0.7))
  34.     0.60287648000000005
  35.     >>> print(mc.calc_antispell(0.5, 0.9, 0.8, 0.9, 0.7))
  36.     0.39712351999999995
  37.     >>> print(mc.spell_is_strong(0.5, 0.9, 0.8, 0.9, 0.7))
  38.     True
  39.     >>> print(mc.antispell_is_strong(0.5, 0.9, 0.8, 0.9, 0.7))
  40.     True
  41.     >>> mc.calc_spell_verbose(.5, 1, 1, 1, .5)
  42.     ** Spell Calculation **
  43.     Magic: 0.5
  44.     Magical probability: 0.625
  45.     Your kung-fu is strong!
  46.     >>> mc.calc_antispell_verbose(.5, 1, 1, 1, .5)
  47.     ** Antispell Calculation **
  48.     Magic: 0.5
  49.     Magical probability: 0.375
  50.     Your kung-fu is strong!
  51.     >>> print(mc) # prints the current values of p, g, l, s, b, m, and p_m
  52.    
  53. """
  54.  
  55. class MagiCalc(object):
  56.  
  57.     p = 0.0    # natural (i.e. unmodified) probability
  58.     g = 0.0    # gnosis
  59.     l = 0.0    # link
  60.     s = 0.0    # subliminalization
  61.     b = 0.0    # belief
  62.     m = 0.0    # magic (i.e. magical power)
  63.     p_m = 0.0  # magical (i.e. modified) probability
  64.    
  65.     def __constrain(self, n, min_n, max_n):
  66.         return max(min(max_n, n), min_n)
  67.    
  68.     def __set_pglsb(self, p, g, l, s, b):
  69.         self.p = float(self.__constrain(p, 0.0, 1.0))
  70.         self.g = float(self.__constrain(g, 0.0, 1.0))
  71.         self.l = float(self.__constrain(l, 0.0, 1.0))
  72.         self.s = float(self.__constrain(s, 0.0, 1.0))
  73.         self.b = float(self.__constrain(b, 0.0, 1.0))
  74.    
  75.     def calc_spell(self, p, g, l, s, b):
  76.         self.__set_pglsb(p, g, l, s, b)
  77.         self.m = self.g * self.l * self.s * self.b
  78.         self.p_m = self.p + (1 - self.p) * self.m ** (1 / self.p)
  79.         return self.p_m
  80.        
  81.     def calc_antispell(self, p, g, l, s, b):
  82.         self.__set_pglsb(p, g, l, s, b)
  83.         self.m = self.g * self.l * self.s * self.b
  84.         self.p_m = self.p - self.p * self.m ** (1 / (1 - self.p))
  85.         return self.p_m
  86.    
  87.     def spell_is_strong(self, p, g, l, s, b):
  88.         self.calc_spell(p, g, l, s, b)
  89.         if self.p_m > self.p:
  90.             return True
  91.         else:
  92.             return False
  93.    
  94.     def antispell_is_strong(self, p, g, l, s, b):
  95.         self.calc_antispell(p, g, l, s, b)
  96.         if self.p_m < self.p:
  97.             return True
  98.         else:
  99.             return False
  100.    
  101.     def calc_spell_verbose(self, p, g, l, s, b):
  102.         self.calc_spell(p, g, l, s, b)
  103.         print()
  104.         print("** Spell Calculation **")
  105.         print("Magic:", self.m)
  106.         print("Magical probability:", self.p_m)
  107.         if self.p_m > self.p:
  108.             print("Your kung-fu is strong!")
  109.         else:
  110.             print("You're gonna need a bigger wand.")
  111.         print()
  112.    
  113.     def calc_antispell_verbose(self, p, g, l, s, b):
  114.         self.calc_antispell(p, g, l, s, b)
  115.         print()
  116.         print("** Antispell Calculation **")
  117.         print("Magic:", round(self.m, 3))
  118.         print("Magical probability:", self.p_m)
  119.         if self.p_m < self.p:
  120.             print("Your kung-fu is strong!")
  121.         else:
  122.             print("You're gonna need a bigger wand.")
  123.         print()
  124.    
  125.     def __repr__(self):
  126.         labels = ["Natural Probability", "Gnosis", "Link", "Subliminalization",
  127.             "Belief", "Magic", "Magical Probability"]
  128.         values = [self.p, self.g, self.l, self.s, self.b, self.m, self.p_m]
  129.         reply = ""
  130.         for i in range(len(values)):
  131.             reply = reply + ("%s: %f \n" % (labels[i], values[i]))
  132.         return reply
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement