A pong clone, but with a twist!

139 lines
4.9 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 VIRTUAL_WIDTH - target.x < AI_SPEED then
  81. return target.y
  82. elseif target.dx > 0 then
  83. local ans = recursiveCalculations(px, target.x, target.y, target.dx, target.dy, 1)
  84. return ans
  85. else
  86. print("GO TO CENTER!!")
  87. return VIRTUAL_HEIGHT/2
  88. end
  89. end
  90. function recursiveCalculations(px, ex, ey, edx, edy, ifspecial)
  91. if (edy > 0) then
  92. print ("normal" .. ex .." " .. ey .. " " .. edx .. " " .. edy)
  93. local time = (VIRTUAL_HEIGHT-ey) / (ballSpeed * edy)
  94. local distance = (ballSpeed * edx) * time
  95. print(distance .. " " .. edx .. " " .. time .. " " .. (px-ex))
  96. if distance > (px - ex) then
  97. local anstime = (px - ex) / (ballSpeed * edx)
  98. local bonus = (ballSpeed * edy) * anstime
  99. print("results: " .. bonus .. " " .. edx .. " " .. anstime .. " " .. (px-ex))
  100. -- if (ifspecial == 0) then
  101. return ey + bonus
  102. -- else
  103. -- return -1
  104. --end
  105. else
  106. local emulatedx = ex + distance
  107. local emulatedy = VIRTUAL_HEIGHT
  108. return recursiveCalculations(px, emulatedx, emulatedy, edx, -edy, 0)
  109. end
  110. elseif edy == 0 then
  111. return ey
  112. else
  113. print ("inverse" .. ex .." " .. ey .. " " .. edx .. " " .. edy)
  114. local time = (ey) / math.abs((ballSpeed * edy))
  115. local distance = (ballSpeed * edx) * time
  116. print(distance .. " " .. edx .. " " .. time .. " " .. (px-ex))
  117. --print("Why th efuck ")
  118. if distance > (px - ex) then
  119. local anstime = (px - ex) / (ballSpeed * edx)
  120. local bonus = (ballSpeed * edy) * anstime
  121. print("results: " .. bonus .. " " .. edx .. " " .. anstime .. " " .. (px-ex))
  122. -- if (ifspecial == 0) then
  123. return ey + bonus
  124. -- else
  125. -- return -1
  126. -- end
  127. else
  128. local emulatedx = ex + distance
  129. local emulatedy = 0
  130. -- print("results: " .. bonus .. " " .. edx .. " " .. anstime .. " " .. (VIRTUAL_WIDTH-ex))
  131. return recursiveCalculations(px, emulatedx, emulatedy, edx, -edy, 0)
  132. end
  133. end
  134. end