Advertisement
CommanderC

healing.rb

Feb 12th, 2012
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 1.79 KB | None | 0 0
  1. #!/usr/bin/ruby
  2.  
  3. def random2(x)
  4.   rand(0...x)
  5. end
  6.  
  7. def roll_dice(n,d)
  8.   r = 0
  9.   for i in 1..n
  10.     r += 1 + random2(d)
  11.   end
  12.   return r
  13. end
  14.  
  15. def div_rand_round(n, d)
  16.   rem = n%d
  17.   q = n/d
  18.   if rem != 0 && random2(d) < rem
  19.     q += 1
  20.   end
  21.   return q
  22. end
  23.  
  24. def skill_rdiv(invo, n, d)
  25.   div_rand_round(invo*n*256, d*256)
  26. end
  27.  
  28. def do_ability(mon_type, same_species, invo)
  29.   pow = 10 + skill_rdiv(invo, 1, 3)
  30.   cast_healing(mon_type, same_species, pow, invo)
  31. end
  32.  
  33. def cast_healing(mon_type, same_species, pow, invo)
  34.   healed = pow + roll_dice(2, pow) - 2
  35.   can_pacify_monster(mon_type, same_species, healed, invo)
  36. end
  37.  
  38. def can_pacify_monster(mon_type, same_species, healed, invo)
  39.   factor = 1
  40.   divisor = 3
  41.  
  42.   case mon_type
  43.     when :animal
  44.       factor = 3
  45.     when :undead
  46.       divisor = 4
  47.     when :demon
  48.       divisor = 5
  49.     when :holy
  50.       divisor = 2
  51.   end
  52.  
  53.   factor = 2 if same_species
  54.  
  55.   factor * random2( ((invo*healed)+healed)/divisor )
  56. end
  57.  
  58. def prob(n, mon, same_species, invo)
  59.   hist = []
  60.   n.times do
  61.     h = do_ability(mon, same_species, invo)
  62.     hist[h] = 0 unless hist[h]
  63.     hist[h] += 1
  64.   end
  65.  
  66.   for i in 0...hist.size
  67.     hist[i] = 0 unless hist[i]
  68.   end
  69.  
  70.   p = []
  71.   for i in 0...hist.size
  72.     p[i] = 0
  73.     for j in 0...i
  74.       p[i] += hist[j]
  75.     end
  76.  
  77.     p[i] = 1 - (1.0*p[i]) / n
  78.   end
  79.   return p
  80. end
  81.  
  82. def main
  83.   n = 100000
  84.   p1 = prob(n, :animal, false, 27)
  85.   p2 = prob(n, :demon,  false, 27)
  86.   p3 = prob(n, :undead, false, 27)
  87.   p4 = prob(n, :other,  false, 27)
  88.  
  89.   size = [p1.size, p2.size, p3.size, p4.size].max
  90.  
  91.   for i in 0...size
  92.     data = [i, p1[i], p2[i], p3[i], p4[i]]
  93.     for j in 0...data.size
  94.       data[j] = 0 unless data[j]
  95.     end
  96.     print "%d %.5f %.5f %.5f %.5f \n" % data
  97.   end
  98. end
  99.  
  100. main
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement