A pong clone, but with a twist!

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