# Untitled

a guest
Oct 4th, 2009
1,462
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. # min hit miss percentage (constant)
2. mhmp = 5
3.
4. random2avg = function(value, rolls, n) {
5. result=rep(0,n)
6. for(i in 1:rolls) {
7. result = result + sample(1:value, n, T)
8. }
9.
10. result = result / rolls;
11. result
12. }
13.
14.
15. testhit = function(tohit, ev, n) {
16. (runif(n,0,100)<mhmp) | (sample(1:(tohit+1),n,T) >= random2avg(2*ev, 2, n))
17. }
18.
19. testhit_ranged = function(tohit, ev, n) {
20. (runif(n,0,100)<mhmp) | (sample(1:(tohit),n,T) >= random2avg(ev, 2, n))
21. }
22.
23. mons_tohit = function(hd, fighter) {
24. if(fighter) {
25. 18+hd*2.5
26. } else {
27. 18+hd*1.5
28. }
29. }
30.
31.
32.
33. hds = c(1,5,10,15,20,25,30)
34. cols = c('red', 'magenta', 'green', 'blue', 'orange', 'cyan', 'brown')
35. evs = seq(0,50,5)
36.
37. d = rep(NA,length(evs))
38. n = 100000
39.
40. doplot=function(hitfunc, fighter, title) {
41.
42. for(hdi in 1:length(hds)) {
43. hd = hds[hdi]
44. col = cols[hdi]
45.
46. for(evi in 1:length(evs)) {
47. ev = evs[evi]
48. d[evi] = sum(hitfunc(mons_tohit(hd, fighter), ev, n))/n
49. }
50. if (hdi == 1) {
51. plot(evs, 1-d, xlim=range(evs), ylim=c(0,1), t='l', col=col,xlab='EV', ylab='dodge rate', main=title)
52.
53. } else {
54. lines(evs, 1-d, col=col)
55. }
56.
57. points(evs, 1-d, col=col, pch='o')
58.
59. }
60.
61. for(y in seq(0,1,0.1)) { abline(h=y,lty=2,col='lightgrey') }
62. for(x in seq(0,50,5)) { abline(v=x,lty=2,col='lightgrey') }
63.
64. legend('topleft', legend=paste('HD=', hds), fill=cols)
65. }
66.
67. png(file='crawl_ev.png', width=1280, height=1024)
68. par(mfrow=c(2,2))
69.
70. doplot(testhit, F, 'melee, mons fighter=no')
71. doplot(testhit, T, 'melee, mons fighter=yes')
72.
73. doplot(testhit_ranged, F, 'ranged, mons fighter=no')
74. doplot(testhit_ranged, T, 'ranged, mons fighter=yes')
75. dev.off()