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.

171 lines
6.5 KiB

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