You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

170 lines
6.4 KiB

  1. function AI(target, ballCnt, diff)
  2. currentTarget = evaluateClosestBall(target);
  3. --print("CLOSEST TARGET IS " .. currentTarget)
  4. if diff < 1200 then
  5. --print ("Normal targeting ".. currentTarget .. " " .. target.x - ball[currentTarget].x .. " " .. ball[currentTarget].y - target.y)
  6. if (ball[currentTarget].y - target.y >= target.height and math.abs(target.x - ball[currentTarget].x) < diff) then
  7. target.dy = AI_SPEED
  8. elseif (target.y - ball[currentTarget].y >= -target.height/2 and math.abs(target.x - ball[currentTarget].x) < diff) then
  9. target.dy = -AI_SPEED
  10. else
  11. target.dy = 0
  12. end
  13. else
  14. --print("Complex targeting")
  15. if target.x < 100 then
  16. neededTarget = predictBall(ball[currentTarget], target.x)
  17. --print(target.x .. " found " .. neededTarget)
  18. if neededTarget ~= -1 then
  19. --print("Calculated target = " .. neededTarget)
  20. if (target.y + target.height/2 - neededTarget >= 15) then
  21. target.dy = -AI_SPEED
  22. elseif (neededTarget - target.y >= target.height*0.9) then
  23. target.dy = AI_SPEED
  24. else
  25. target.dy = 0
  26. end
  27. end
  28. else
  29. neededTarget1 = predictBall(ball[currentTarget], target.x)
  30. --print(target.x .. " found " .. neededTarget)
  31. if neededTarget1 ~= -1 then
  32. --print("Calculated target = " .. neededTarget)
  33. if (target.y + target.height/2 - neededTarget1 >= 10) then
  34. target.dy = -AI_SPEED
  35. elseif (neededTarget1 - (target.y+target.height/2) >= 10) then
  36. target.dy = AI_SPEED
  37. else
  38. target.dy = 0
  39. end
  40. end
  41. end
  42. end
  43. if
  44. difficultyl == 350 and player2reverbav == true and VIRTUAL_WIDTH - ball[currentTarget].x < 90 and
  45. math.abs(ball[currentTarget].y - targe.y) > 150
  46. then
  47. sounds["time"]:play()
  48. player2reverbav = false
  49. timeIsSlow2 = true
  50. originalPaddle = paddle_SPEED
  51. originalSpeed = ballSpeed
  52. player2reverbav = 0
  53. potentialnuke2 = 0
  54. potentialstrike2 = 0
  55. end
  56. if (player2nukescore > AI_STRIKEMOD and striken == 0) then
  57. player2striken = 1
  58. elseif (player2nukescore > AI_NUKEMOD and striken == 1) then
  59. if (areanuclear == 1) then
  60. maxspeed = maxspeed + 50
  61. end
  62. sounds["nuke"]:play()
  63. potentialstrike2 = 0
  64. areanuclear = 1
  65. ballSpeed = ballSpeed * 2
  66. if (synctype == 0) then
  67. paddle_SPEED = paddle_SPEED * 2
  68. end
  69. if (synctype == 1) then
  70. paddle_SPEED = ballSpeed / 10
  71. end
  72. if (synctype == 0) then
  73. AI_SPEED = AI_SPEED * 2.2
  74. end
  75. if (synctype == 1) then
  76. AI_SPEED = ballSpeed * 1.1 / 10
  77. end
  78. player2nukescore = 0
  79. player2reverbav = 0
  80. potentialnuke2 = 0
  81. end
  82. end
  83. function evaluateClosestBall(target)
  84. local ans = 0
  85. local min = 99999;
  86. for i = 1, maxBalls do
  87. if math.abs(target.x - ball[i].x ) < min then
  88. min = math.abs(target.x - ball[i].x)
  89. ans = i
  90. end
  91. end
  92. return ans
  93. end
  94. function predictBall(target, px)
  95. --print("BALLSTATS:" .. target.x .. " " .. target.y)
  96. if target.dx > 0 and px > 100 then
  97. local ans = recursiveCalculations(px, target.x, target.y, target.dx, target.dy, 1)
  98. return ans
  99. elseif target.dx < 0 and px < 100 then
  100. local ans = recursiveCalculations(px, target.x, target.y, target.dx, target.dy, 1)
  101. return ans
  102. else
  103. --print("GO TO CENTER!!")
  104. return VIRTUAL_HEIGHT/2
  105. end
  106. end
  107. function recursiveCalculations(px, ex, ey, edx, edy, ifspecial)
  108. if (edy > 0) then
  109. --print ("normal" .. ex .." " .. ey .. " " .. edx .. " " .. edy)
  110. local time = (VIRTUAL_HEIGHT-40-ey) / (ballSpeed * edy)
  111. local distance = math.abs(ballSpeed * edx) * time
  112. print("DOWNWARD" .. distance .. " " .. edx .. " " .. time .. " " .. math.abs(px-ex))
  113. if distance > math.abs(px - ex) then
  114. print("QQ")
  115. local anstime = math.abs(px - ex) / math.abs(ballSpeed * edx)
  116. local bonus = (ballSpeed * edy) * anstime
  117. --print("results: " .. bonus .. " " .. edx .. " " .. anstime .. " " .. (px-ex))
  118. -- if (ifspecial == 0) then
  119. local answer = ey + bonus
  120. --love.window.setTitle(tostring(answer) .. "Basiccalc")
  121. return ey + bonus
  122. -- else
  123. -- return -1
  124. --end
  125. else
  126. print("SS")
  127. local emulatedx = ex + distance * edx
  128. local emulatedy = VIRTUAL_HEIGHT-40
  129. print("EMULATED: " .. emulatedx .. " " .. emulatedy)
  130. local answer = recursiveCalculations(px, emulatedx, emulatedy, edx, -edy, 0)
  131. print("GOT EMULATION RESULT AS " .. answer)
  132. --love.window.setTitle(tostring(answer) .. "recursive calc bottom")
  133. return answer
  134. end
  135. elseif edy == 0 then
  136. return ey
  137. else
  138. --print ("inverse" .. ex .." " .. ey .. " " .. edx .. " " .. edy)
  139. local time = (ey) / math.abs((ballSpeed * edy))
  140. local distance = math.abs(ballSpeed * edx) * time
  141. print("UPWARD" .. distance .. " " .. edx .. " " .. time .. " " .. math.abs(px-ex))
  142. --print("Why th efuck ")
  143. if distance > math.abs(px - ex) then
  144. local anstime = math.abs(px - ex) / math.abs(ballSpeed * edx)
  145. local bonus = (ballSpeed * edy) * anstime
  146. print("results: " .. bonus .. " " .. edx .. " " .. anstime .. " " .. math.abs(px-ex))
  147. -- if (ifspecial == 0) then
  148. local answer = ey + bonus
  149. --love.window.setTitle(tostring(answer) .. "Basiccalc")
  150. return answer
  151. -- else
  152. -- return -1
  153. -- end
  154. else
  155. local emulatedx = ex + distance * edx
  156. local emulatedy = 0
  157. ----print("results: " .. bonus .. " " .. edx .. " " .. anstime .. " " .. (VIRTUAL_WIDTH-ex))
  158. local answer = recursiveCalculations(px, emulatedx, emulatedy, edx, -edy, 0)
  159. --love.window.setTitle(tostring(answer) .. "recursivecalc")
  160. return answer
  161. end
  162. end
  163. end