RandomLogic.lua 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. function getRandom1(conf,without,weight_index)
  2. local sum = 0
  3. for i = 1 ,#conf do
  4. if canCalc(i,without) then
  5. sum = sum + conf[i][weight_index]
  6. end
  7. end
  8. local r = math.random(1,sum)
  9. sum = 0
  10. for i = 1 ,#conf do
  11. if canCalc(i,without) then
  12. sum = sum + conf[i][weight_index]
  13. if sum >= r then
  14. return i
  15. end
  16. end
  17. end
  18. end
  19. function getRandom2(conf,without)
  20. local sum = 0
  21. for k,v in ipairs(conf) do
  22. if canCalc(k,without) then
  23. sum = sum + v.weight
  24. end
  25. end
  26. local r = math.random(1,sum)
  27. sum = 0
  28. for k,v in ipairs(conf) do
  29. if canCalc(k,without) then
  30. sum = sum + v.weight
  31. if sum >= r then
  32. return k
  33. end
  34. end
  35. end
  36. end
  37. function getRandom3(conf)
  38. local sum = 0
  39. for k,v in ipairs(conf) do
  40. sum = sum + v.weight
  41. end
  42. local r = math.random(1,sum)
  43. sum = 0
  44. for k,v in ipairs(conf) do
  45. sum = sum + v.weight
  46. if sum >= r then
  47. return k
  48. end
  49. end
  50. end
  51. -- µÈ¼¶ÏÞÖÆ
  52. function getRandom4(conf,without,weight_index,minLvIndex,maxLvIndex,human)
  53. local sum = 0
  54. for i = 1 ,#conf do
  55. if canCalc(i,without) and (not conf[i][minLvIndex] or
  56. human and human.db.lv >= conf[i][minLvIndex] and human.db.lv <= conf[i][maxLvIndex]) then
  57. sum = sum + conf[i][weight_index]
  58. end
  59. end
  60. local r = math.random(1,sum)
  61. sum = 0
  62. for i = 1 ,#conf do
  63. if canCalc(i,without) and (not conf[i][minLvIndex] or
  64. human and human.db.lv >= conf[i][minLvIndex] and human.db.lv <= conf[i][maxLvIndex]) then
  65. sum = sum + conf[i][weight_index]
  66. if sum >= r then
  67. return i
  68. end
  69. end
  70. end
  71. end
  72. function getRandom5(human,conf,without,weight_index,job_index,sex_index)
  73. local sum = 0
  74. for i = 1 ,#conf do
  75. if canCalc(i,without) then
  76. local job = conf[i][job_index]
  77. local sex = conf[i][sex_index]
  78. if (job == 0 or human.db.job == job) and (sex == 0 or human.db.sex == sex) then
  79. sum = sum + conf[i][weight_index]
  80. end
  81. end
  82. end
  83. if sum == 0 then
  84. assert()
  85. end
  86. local r = math.random(1,sum)
  87. sum = 0
  88. for i = 1 ,#conf do
  89. if canCalc(i,without) then
  90. local job = conf[i][job_index]
  91. local sex = conf[i][sex_index]
  92. if (job == 0 or human.db.job == job) and (sex == 0 or human.db.sex == sex) then
  93. sum = sum + conf[i][weight_index]
  94. if sum >= r then
  95. return i
  96. end
  97. end
  98. end
  99. end
  100. end
  101. function getRandom6(conf,without)
  102. local sum = 0
  103. for i = 1 ,#conf do
  104. if canCalc(i,without) then
  105. sum = sum + conf[i]
  106. end
  107. end
  108. if sum == 0 then
  109. assert()
  110. end
  111. local r = math.random(1,sum)
  112. sum = 0
  113. for i = 1 ,#conf do
  114. if canCalc(i,without) then
  115. sum = sum + conf[i]
  116. if sum >= r then
  117. return i
  118. end
  119. end
  120. end
  121. end
  122. function canCalc(index,without)
  123. if not without then
  124. return true
  125. end
  126. if without then
  127. for k,v in ipairs(without) do
  128. if index == v then
  129. return false
  130. end
  131. end
  132. end
  133. return true
  134. end