@@ -1,5 +1,16 @@ |
| 1 | // ai.js - AI logic and behavior | 1 | // ai.js - AI logic and behavior |
| 2 | | 2 | |
| | 3 | +// ============= AI TECHNIQUE CONSTANTS ============= |
| | 4 | +const AI_WINDUP_SPEED = 0.15; // Base oscillation speed |
| | 5 | +const AI_WINDUP_SMOOTHNESS = 0.92; // Smooth transitions |
| | 6 | +const AI_WINDUP_RADIUS = 40; // Circular motion radius |
| | 7 | +const AI_WINDUP_MIN_TIME = 300; // Minimum windup duration |
| | 8 | +const AI_WINDUP_MAX_TIME = 600; // Maximum windup duration |
| | 9 | +const AI_BOP_AT_PEAK_CHANCE = 0.4; // Chance to bop at windup peak |
| | 10 | +const AI_CIRCULAR_MOTION = 0.7; // How circular vs linear the motion is |
| | 11 | +const AI_MOMENTUM_CARRY = 0.85; // How much momentum carries between moves |
| | 12 | +const AI_PHASE_SPEED = 0.08; // Speed of phase progression (radians per frame) |
| | 13 | + |
| 3 | // ============= AI SETTINGS ============= | 14 | // ============= AI SETTINGS ============= |
| 4 | const AI_SETTINGS = { | 15 | const AI_SETTINGS = { |
| 5 | easy: { | 16 | easy: { |
@@ -9,7 +20,13 @@ const AI_SETTINGS = { |
| 9 | prediction: 0.3, | 20 | prediction: 0.3, |
| 10 | aggression: 0.2, | 21 | aggression: 0.2, |
| 11 | oscillation: 0.3, | 22 | oscillation: 0.3, |
| 12 | - bopChance: 0.25 | 23 | + bopChance: 0.25, |
| | 24 | + // New windup parameters |
| | 25 | + windupSpeed: 0.1, |
| | 26 | + windupRadius: 30, |
| | 27 | + comboBopChance: 0.1, |
| | 28 | + circularMotion: 0.4, |
| | 29 | + phaseSpeed: 0.06 |
| 13 | }, | 30 | }, |
| 14 | medium: { | 31 | medium: { |
| 15 | reactionTime: 250, | 32 | reactionTime: 250, |
@@ -18,7 +35,13 @@ const AI_SETTINGS = { |
| 18 | prediction: 0.6, | 35 | prediction: 0.6, |
| 19 | aggression: 0.5, | 36 | aggression: 0.5, |
| 20 | oscillation: 0.7, | 37 | oscillation: 0.7, |
| 21 | - bopChance: 0.55 | 38 | + bopChance: 0.55, |
| | 39 | + // New windup parameters |
| | 40 | + windupSpeed: 0.15, |
| | 41 | + windupRadius: 40, |
| | 42 | + comboBopChance: 0.3, |
| | 43 | + circularMotion: 0.6, |
| | 44 | + phaseSpeed: 0.08 |
| 22 | }, | 45 | }, |
| 23 | hard: { | 46 | hard: { |
| 24 | reactionTime: 150, | 47 | reactionTime: 150, |
@@ -27,7 +50,13 @@ const AI_SETTINGS = { |
| 27 | prediction: 0.8, | 50 | prediction: 0.8, |
| 28 | aggression: 0.8, | 51 | aggression: 0.8, |
| 29 | oscillation: 1.0, | 52 | oscillation: 1.0, |
| 30 | - bopChance: 0.85 | 53 | + bopChance: 0.85, |
| | 54 | + // New windup parameters |
| | 55 | + windupSpeed: 0.2, |
| | 56 | + windupRadius: 50, |
| | 57 | + comboBopChance: 0.5, |
| | 58 | + circularMotion: 0.8, |
| | 59 | + phaseSpeed: 0.1 |
| 31 | } | 60 | } |
| 32 | }; | 61 | }; |
| 33 | | 62 | |
@@ -48,7 +77,22 @@ let aiState = { |
| 48 | aggressionLevel: 0.5, | 77 | aggressionLevel: 0.5, |
| 49 | lastHitTime: 0, | 78 | lastHitTime: 0, |
| 50 | | 79 | |
| 51 | - // Oscillation parameters | 80 | + // Enhanced windup system |
| | 81 | + windupPhase: 0, // 0 to 2π for circular motion |
| | 82 | + windupVelocity: 0, // Current oscillation speed |
| | 83 | + windupMomentum: {x: 0, y: 0}, // Momentum vector |
| | 84 | + windupCenter: 200, // Center point of circular motion |
| | 85 | + peakReached: false, // Track if we hit peak velocity |
| | 86 | + comboBop: false, // Planning windup+bop combo |
| | 87 | + maxVelocityPhase: 0, // Phase where max velocity occurs |
| | 88 | + |
| | 89 | + // Motion tracking |
| | 90 | + lastPositions: [], // Track last N positions for smoothing |
| | 91 | + currentVelocity: 0, // Actual paddle velocity |
| | 92 | + targetVelocity: 0, // Desired paddle velocity |
| | 93 | + smoothedTarget: 200, // Smoothed target position |
| | 94 | + |
| | 95 | + // Original oscillation parameters (keeping for compatibility) |
| 52 | windupDistance: 120, | 96 | windupDistance: 120, |
| 53 | swingPower: 1.05, | 97 | swingPower: 1.05, |
| 54 | timingWindow: 40, | 98 | timingWindow: 40, |
@@ -182,25 +226,48 @@ function handleAITracking(currentTime, ballPos, ballVel, aiSettings, |
| 182 | } | 226 | } |
| 183 | } | 227 | } |
| 184 | | 228 | |
| 185 | - // Execute bop | 229 | + // Execute bop at the right moment |
| 186 | if (aiState.consideringBop && ballApproaching) { | 230 | if (aiState.consideringBop && ballApproaching) { |
| 187 | let timeToBop = currentTime - aiState.bopDecisionTime; | 231 | let timeToBop = currentTime - aiState.bopDecisionTime; |
| 188 | let paddleY = rightPaddle.position.y; | 232 | let paddleY = rightPaddle.position.y; |
| 189 | let distanceToBall = Math.abs(ballPos.y - paddleY); | 233 | let distanceToBall = Math.abs(ballPos.y - paddleY); |
| 190 | | 234 | |
| | 235 | + // Refined bop execution conditions |
| 191 | let shouldBop = timeToBop > aiState.bopTiming && | 236 | let shouldBop = timeToBop > aiState.bopTiming && |
| 192 | - ballDistance < 150 && | 237 | + ballDistance < 150 && // Close enough |
| 193 | - distanceToBall < PADDLE_HEIGHT / 2 + 20 && | 238 | + distanceToBall < PADDLE_HEIGHT / 2 + 20 && // Paddle can reach ball |
| 194 | !bopState.right.active; | 239 | !bopState.right.active; |
| 195 | | 240 | |
| | 241 | + // Special handling for combo bops during windup |
| | 242 | + if (aiState.comboBop && aiState.mode === 'WINDING_UP') { |
| | 243 | + // Execute bop at peak velocity during windup |
| | 244 | + shouldBop = shouldBop || (aiState.peakReached && |
| | 245 | + ballDistance < 180 && |
| | 246 | + distanceToBall < PADDLE_HEIGHT / 2 + 30); |
| | 247 | + } |
| | 248 | + |
| 196 | if (shouldBop) { | 249 | if (shouldBop) { |
| 197 | activateBop('right', currentTime, rightPaddle, rightSupport, engine, particles); | 250 | activateBop('right', currentTime, rightPaddle, rightSupport, engine, particles); |
| 198 | aiState.consideringBop = false; | 251 | aiState.consideringBop = false; |
| | 252 | + |
| | 253 | + if (aiState.comboBop) { |
| | 254 | + console.log(`AI COMBO BOP! Phase: ${(aiState.windupPhase % (Math.PI * 2)).toFixed(2)}, Velocity: ${aiState.currentVelocity.toFixed(1)}`); |
| | 255 | + aiState.comboBop = false; |
| | 256 | + |
| | 257 | + // Transition to swing after combo |
| | 258 | + if (aiState.mode === 'WINDING_UP') { |
| | 259 | + aiState.mode = 'SWINGING'; |
| | 260 | + aiState.swingStartTime = currentTime; |
| | 261 | + } |
| | 262 | + } else { |
| 199 | console.log(`AI BOP! Difficulty: ${aiState.difficulty}, Speed: ${ballSpeed.toFixed(1)}`); | 263 | console.log(`AI BOP! Difficulty: ${aiState.difficulty}, Speed: ${ballSpeed.toFixed(1)}`); |
| 200 | } | 264 | } |
| | 265 | + } |
| 201 | | 266 | |
| | 267 | + // Cancel bop if opportunity missed |
| 202 | if (ballDistance > 200 || ballDistance < 50) { | 268 | if (ballDistance > 200 || ballDistance < 50) { |
| 203 | aiState.consideringBop = false; | 269 | aiState.consideringBop = false; |
| | 270 | + aiState.comboBop = false; |
| 204 | } | 271 | } |
| 205 | } | 272 | } |
| 206 | | 273 | |
@@ -232,8 +299,20 @@ function handleAITracking(currentTime, ballPos, ballVel, aiSettings, |
| 232 | Math.random() < aiSettings.oscillation * aiState.aggressionLevel * 0.3; | 299 | Math.random() < aiSettings.oscillation * aiState.aggressionLevel * 0.3; |
| 233 | | 300 | |
| 234 | if (shouldWindUp) { | 301 | if (shouldWindUp) { |
| | 302 | + // Start winding up for power shot |
| 235 | aiState.mode = 'WINDING_UP'; | 303 | aiState.mode = 'WINDING_UP'; |
| 236 | aiState.windupStartTime = currentTime; | 304 | aiState.windupStartTime = currentTime; |
| | 305 | + |
| | 306 | + // Initialize circular windup |
| | 307 | + aiState.windupCenter = paddlePos.y; // Start from current position |
| | 308 | + aiState.windupPhase = 0; |
| | 309 | + aiState.windupMomentum = {x: 0, y: 0}; |
| | 310 | + aiState.smoothedTarget = paddlePos.y; |
| | 311 | + aiState.lastPositions = [paddlePos.y]; |
| | 312 | + aiState.peakReached = false; |
| | 313 | + aiState.comboBop = false; |
| | 314 | + |
| | 315 | + // Determine initial direction based on intercept position |
| 237 | aiState.windupDirection = aiState.interceptY > paddlePos.y ? -1 : 1; | 316 | aiState.windupDirection = aiState.interceptY > paddlePos.y ? -1 : 1; |
| 238 | } else { | 317 | } else { |
| 239 | aiState.targetY = lerp(aiState.targetY, targetAnchorForIntercept, 0.3); | 318 | aiState.targetY = lerp(aiState.targetY, targetAnchorForIntercept, 0.3); |
@@ -247,34 +326,86 @@ function handleAITracking(currentTime, ballPos, ballVel, aiSettings, |
| 247 | function handleAIWindup(currentTime, ballPos, ballVel, aiSettings, | 326 | function handleAIWindup(currentTime, ballPos, ballVel, aiSettings, |
| 248 | rightPaddle, rightSupport, height, width) { | 327 | rightPaddle, rightSupport, height, width) { |
| 249 | let windupTime = currentTime - aiState.windupStartTime; | 328 | let windupTime = currentTime - aiState.windupStartTime; |
| 250 | - let maxWindupTime = 800; | 329 | + let maxWindupTime = AI_WINDUP_MIN_TIME + (AI_WINDUP_MAX_TIME - AI_WINDUP_MIN_TIME) * aiState.aggressionLevel; |
| 251 | - let timeProgress = Math.min(windupTime / maxWindupTime, 1.0); | 330 | + |
| | 331 | + // Update windup phase for circular motion |
| | 332 | + let phaseSpeed = aiSettings.phaseSpeed * (1 + aiState.aggressionLevel * 0.5); |
| | 333 | + aiState.windupPhase += phaseSpeed; |
| | 334 | + |
| | 335 | + // Calculate circular motion with momentum |
| | 336 | + let radius = aiSettings.windupRadius * aiState.aggressionLevel; |
| | 337 | + let circularBlend = aiSettings.circularMotion; |
| | 338 | + |
| | 339 | + // Pure circular motion components |
| | 340 | + let circularX = Math.sin(aiState.windupPhase) * radius * 0.3; // Slight X movement |
| | 341 | + let circularY = Math.cos(aiState.windupPhase) * radius; |
| | 342 | + |
| | 343 | + // Add momentum for more natural motion |
| | 344 | + let targetDeltaY = circularY - (aiState.smoothedTarget - aiState.windupCenter); |
| | 345 | + aiState.windupMomentum.y = aiState.windupMomentum.y * AI_MOMENTUM_CARRY + targetDeltaY * (1 - AI_MOMENTUM_CARRY); |
| 252 | | 346 | |
| 253 | - let easedProgress = timeProgress < 0.5 | 347 | + // Calculate the target position with smooth circular motion |
| 254 | - ? 2 * timeProgress * timeProgress | 348 | + let windupTargetY = aiState.windupCenter + aiState.windupMomentum.y; |
| 255 | - : 1 - Math.pow(-2 * timeProgress + 2, 3) / 2; | | |
| 256 | | 349 | |
| 257 | - aiState.windupProgress = easedProgress; | 350 | + // Smooth the target for more fluid motion |
| | 351 | + aiState.smoothedTarget = aiState.smoothedTarget * AI_WINDUP_SMOOTHNESS + |
| | 352 | + windupTargetY * (1 - AI_WINDUP_SMOOTHNESS); |
| 258 | | 353 | |
| 259 | - let windupTargetY = aiState.interceptY + aiState.windupDirection * | 354 | + // Keep within bounds |
| 260 | - aiState.windupDistance * aiState.aggressionLevel * easedProgress; | 355 | + aiState.smoothedTarget = Math.max(50, Math.min(height - 50, aiState.smoothedTarget)); |
| 261 | - windupTargetY = Math.max(50, Math.min(height - 50, windupTargetY)); | | |
| 262 | | 356 | |
| 263 | - let anchorOffsetNeeded = calculateAnchorOffset(windupTargetY, rightPaddle.position, rightSupport.position); | 357 | + // Convert paddle target to anchor target |
| 264 | - aiState.targetY = windupTargetY + anchorOffsetNeeded; | 358 | + let anchorOffsetNeeded = calculateAnchorOffset(aiState.smoothedTarget, rightPaddle.position, rightSupport.position); |
| | 359 | + aiState.targetY = aiState.smoothedTarget + anchorOffsetNeeded; |
| 265 | | 360 | |
| | 361 | + // Track velocity for combo detection |
| | 362 | + if (aiState.lastPositions.length > 5) { |
| | 363 | + aiState.lastPositions.shift(); |
| | 364 | + } |
| | 365 | + aiState.lastPositions.push(aiState.smoothedTarget); |
| | 366 | + |
| | 367 | + // Calculate current velocity |
| | 368 | + if (aiState.lastPositions.length > 1) { |
| | 369 | + let recentDelta = aiState.lastPositions[aiState.lastPositions.length - 1] - |
| | 370 | + aiState.lastPositions[aiState.lastPositions.length - 2]; |
| | 371 | + aiState.currentVelocity = Math.abs(recentDelta); |
| | 372 | + |
| | 373 | + // Check if we're at peak velocity (good time for combo bop) |
| | 374 | + if (aiState.currentVelocity > radius * phaseSpeed * 0.8 && !aiState.peakReached) { |
| | 375 | + aiState.peakReached = true; |
| | 376 | + aiState.maxVelocityPhase = aiState.windupPhase; |
| | 377 | + |
| | 378 | + // Consider combo bop at peak |
| | 379 | + if (!aiState.comboBop && !aiState.consideringBop && !bopState.right.active && |
| | 380 | + Math.random() < aiSettings.comboBopChance * aiState.aggressionLevel) { |
| | 381 | + aiState.comboBop = true; |
| | 382 | + aiState.consideringBop = true; |
| | 383 | + aiState.bopDecisionTime = currentTime; |
| | 384 | + aiState.bopTiming = 50; // Quick bop at peak |
| | 385 | + console.log("AI planning COMBO: Windup + Bop!"); |
| | 386 | + } |
| | 387 | + } |
| | 388 | + } |
| | 389 | + |
| | 390 | + // Check if it's time to swing |
| 266 | let ballDistance = width - ballPos.x; | 391 | let ballDistance = width - ballPos.x; |
| 267 | let ballSpeed = Math.sqrt(ballVel.x * ballVel.x + ballVel.y * ballVel.y); | 392 | let ballSpeed = Math.sqrt(ballVel.x * ballVel.x + ballVel.y * ballVel.y); |
| 268 | | 393 | |
| 269 | let shouldSwing = windupTime > maxWindupTime || | 394 | let shouldSwing = windupTime > maxWindupTime || |
| 270 | ballDistance < 120 || | 395 | ballDistance < 120 || |
| 271 | ballSpeed > 6 || | 396 | ballSpeed > 6 || |
| 272 | - easedProgress > 0.85; | 397 | + (aiState.windupPhase > Math.PI * 2 && ballDistance < 200); |
| 273 | | 398 | |
| 274 | if (shouldSwing) { | 399 | if (shouldSwing) { |
| 275 | aiState.mode = 'SWINGING'; | 400 | aiState.mode = 'SWINGING'; |
| 276 | aiState.swingStartTime = currentTime; | 401 | aiState.swingStartTime = currentTime; |
| 277 | - aiState.windupProgress = 0; | 402 | + aiState.windupPhase = 0; |
| | 403 | + aiState.peakReached = false; |
| | 404 | + aiState.comboBop = false; |
| | 405 | + aiState.lastPositions = []; |
| | 406 | + |
| | 407 | + // Carry momentum into swing |
| | 408 | + aiState.targetVelocity = aiState.currentVelocity * 2; |
| 278 | } | 409 | } |
| 279 | } | 410 | } |
| 280 | | 411 | |
@@ -333,13 +464,27 @@ function executeAIMovement(aiSettings, rightSupport) { |
| 333 | if (Math.abs(deltaY) > 1) { | 464 | if (Math.abs(deltaY) > 1) { |
| 334 | let baseSpeed = 0.12 * aiSettings.speed; | 465 | let baseSpeed = 0.12 * aiSettings.speed; |
| 335 | | 466 | |
| | 467 | + // Apply swing power during swing phase |
| 336 | if (aiState.mode === 'SWINGING') { | 468 | if (aiState.mode === 'SWINGING') { |
| 337 | baseSpeed *= aiState.swingPower * (1 + aiState.aggressionLevel * 0.3); | 469 | baseSpeed *= aiState.swingPower * (1 + aiState.aggressionLevel * 0.3); |
| | 470 | + |
| | 471 | + // Add momentum from windup if available |
| | 472 | + if (aiState.targetVelocity > 0) { |
| | 473 | + baseSpeed *= (1 + aiState.targetVelocity * 0.1); |
| | 474 | + aiState.targetVelocity *= 0.9; // Decay momentum |
| | 475 | + } |
| 338 | } else if (aiState.mode === 'WINDING_UP') { | 476 | } else if (aiState.mode === 'WINDING_UP') { |
| 339 | - let windupSpeedMultiplier = 0.3 + (aiState.windupProgress * 0.4); | 477 | + // Enhanced windup speed based on phase and settings |
| 340 | - baseSpeed *= windupSpeedMultiplier; | 478 | + let windupSpeedMultiplier = aiSettings.windupSpeed / AI_WINDUP_SPEED; |
| | 479 | + baseSpeed *= (1.5 + windupSpeedMultiplier); |
| | 480 | + |
| | 481 | + // Add extra speed at peak velocity points |
| | 482 | + if (aiState.peakReached) { |
| | 483 | + baseSpeed *= 1.3; |
| | 484 | + } |
| 341 | } | 485 | } |
| 342 | | 486 | |
| | 487 | + // Apply aggression multiplier |
| 343 | baseSpeed *= (1 + aiState.aggressionLevel * 0.3); | 488 | baseSpeed *= (1 + aiState.aggressionLevel * 0.3); |
| 344 | | 489 | |
| 345 | let movement = deltaY * baseSpeed; | 490 | let movement = deltaY * baseSpeed; |
@@ -352,6 +497,7 @@ function executeAIMovement(aiSettings, rightSupport) { |
| 352 | // Update input buffer for visual effects | 497 | // Update input buffer for visual effects |
| 353 | window.inputBuffer.right = movement / (SUPPORT_SPEED * 1.1); | 498 | window.inputBuffer.right = movement / (SUPPORT_SPEED * 1.1); |
| 354 | } else { | 499 | } else { |
| | 500 | + // Gradually reduce input buffer when AI is not moving |
| 355 | window.inputBuffer.right *= 0.95; | 501 | window.inputBuffer.right *= 0.95; |
| 356 | } | 502 | } |
| 357 | } | 503 | } |