Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- """
- MagiCalc -- Magical Probability Calculator, Python edition
- by tchnmncr, hyperritual.com
- The MagiCalc class calculates magically modified probability per Peter J.
- Carroll's equations of magic in _The Octavo_, _The Apophenion_, and _Liber
- Kaos_. The public methods all require five parameters in the range of 0 to
- 1 (numbers outside the range are constrained to 0 or 1) for natural proba-
- bility, gnosis, link, subliminalization, and belief, respectively. For a
- graphical version of MagiCalc made with Processing, see
- http://hyperritual.com/blog/magicalc-2/.
- Ideally, I would rewrite the class to use Python's decimal module, but for
- now it uses float arithmetic.
- The public methods are:
- calc_spell: returns the calculated spell probability
- calc_antispell: returns the calculated antispell probability
- spell_is_strong: returns True if p_m > p, else False
- antispell_is_strong: returns True if p_m < p, else False
- calc_spell_verbose: prints the values for m and p_m, and tells you
- whether p_m > p
- calc_antispell_verbose: prints the values for m and p_m, and tells you
- whether p_m < p
- Again, each method has five parameters in the form of
- calc_spell(p, g, l, s, b)
- e.g.,
- calc_spell(0.5, 0.9, 0.8, 0.9, 0.7)
- Usage examples:
- >>> mc = MagiCalc() # instantiates a magical probability calculator
- >>> print(mc.calc_spell(0.5, 0.9, 0.8, 0.9, 0.7))
- 0.60287648000000005
- >>> print(mc.calc_antispell(0.5, 0.9, 0.8, 0.9, 0.7))
- 0.39712351999999995
- >>> print(mc.spell_is_strong(0.5, 0.9, 0.8, 0.9, 0.7))
- True
- >>> print(mc.antispell_is_strong(0.5, 0.9, 0.8, 0.9, 0.7))
- True
- >>> mc.calc_spell_verbose(.5, 1, 1, 1, .5)
- ** Spell Calculation **
- Magic: 0.5
- Magical probability: 0.625
- Your kung-fu is strong!
- >>> mc.calc_antispell_verbose(.5, 1, 1, 1, .5)
- ** Antispell Calculation **
- Magic: 0.5
- Magical probability: 0.375
- Your kung-fu is strong!
- >>> print(mc) # prints the current values of p, g, l, s, b, m, and p_m
- """
- class MagiCalc(object):
- p = 0.0 # natural (i.e. unmodified) probability
- g = 0.0 # gnosis
- l = 0.0 # link
- s = 0.0 # subliminalization
- b = 0.0 # belief
- m = 0.0 # magic (i.e. magical power)
- p_m = 0.0 # magical (i.e. modified) probability
- def __constrain(self, n, min_n, max_n):
- return max(min(max_n, n), min_n)
- def __set_pglsb(self, p, g, l, s, b):
- self.p = float(self.__constrain(p, 0.0, 1.0))
- self.g = float(self.__constrain(g, 0.0, 1.0))
- self.l = float(self.__constrain(l, 0.0, 1.0))
- self.s = float(self.__constrain(s, 0.0, 1.0))
- self.b = float(self.__constrain(b, 0.0, 1.0))
- def calc_spell(self, p, g, l, s, b):
- self.__set_pglsb(p, g, l, s, b)
- self.m = self.g * self.l * self.s * self.b
- self.p_m = self.p + (1 - self.p) * self.m ** (1 / self.p)
- return self.p_m
- def calc_antispell(self, p, g, l, s, b):
- self.__set_pglsb(p, g, l, s, b)
- self.m = self.g * self.l * self.s * self.b
- self.p_m = self.p - self.p * self.m ** (1 / (1 - self.p))
- return self.p_m
- def spell_is_strong(self, p, g, l, s, b):
- self.calc_spell(p, g, l, s, b)
- if self.p_m > self.p:
- return True
- else:
- return False
- def antispell_is_strong(self, p, g, l, s, b):
- self.calc_antispell(p, g, l, s, b)
- if self.p_m < self.p:
- return True
- else:
- return False
- def calc_spell_verbose(self, p, g, l, s, b):
- self.calc_spell(p, g, l, s, b)
- print()
- print("** Spell Calculation **")
- print("Magic:", self.m)
- print("Magical probability:", self.p_m)
- if self.p_m > self.p:
- print("Your kung-fu is strong!")
- else:
- print("You're gonna need a bigger wand.")
- print()
- def calc_antispell_verbose(self, p, g, l, s, b):
- self.calc_antispell(p, g, l, s, b)
- print()
- print("** Antispell Calculation **")
- print("Magic:", round(self.m, 3))
- print("Magical probability:", self.p_m)
- if self.p_m < self.p:
- print("Your kung-fu is strong!")
- else:
- print("You're gonna need a bigger wand.")
- print()
- def __repr__(self):
- labels = ["Natural Probability", "Gnosis", "Link", "Subliminalization",
- "Belief", "Magic", "Magical Probability"]
- values = [self.p, self.g, self.l, self.s, self.b, self.m, self.p_m]
- reply = ""
- for i in range(len(values)):
- reply = reply + ("%s: %f \n" % (labels[i], values[i]))
- return reply
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement