Code Sketch


bhavesh shahare
By: Mhalsakant School
Category: Programming
// ============================================================================
// OPERATION SINDOOR - WORLD'S #1 NEXT-GEN QUANTUM CINEMATIC ENGINE (V9.1 FIXED)
// ============================================================================

cleari()
originBottomLeft()

// ============================================================================
// GLOBAL CONSTANTS & CONFIGURATIONS
// ============================================================================
val ENGINE_VERSION = "v9.1-ULTRA-QUANTUM-VEG-DEFENSE"
val MAX_STARS_COUNT = 650
val MAX_PARTICLE_CAP = 1200

// ============================================================================
// GAME STATE MACHINE VARIABLES
// ============================================================================
var currentGameState = 0 
// 0: Tactical Briefing Terminal
// 1: Commander Deployment Cinematic
// 2: Active Combat Strategy & Defense
// 3: Boss Wave Encounter
// 4: Cinematic Missile Strike Finale
// 5: Ultimate Tribute Screen

var briefingIndex = 0
var briefingDelta = 0.0
var deploymentTimer = 0.0

var combatTimer = 100.0
var globalElapsedTime = 0.0
var scoreDestroyedCount = 0
var quotaRequirement = 35
var commanderHP = 100.0
var hyperDriveActive = false

// ============================================================================
// ULTIMATE STRIKE FINALE VARIABLES (FIXED)
// ============================================================================
var ultimateStrikeX = 0.0
var ultimateStrikeY = 0.0
var ultimateStrikeAngle = 0.0

// ============================================================================
// BOSS CONFIGURATIONS
// ============================================================================
var flagshipBossActive = false
var flagshipBossHP = 600.0
var flagshipBossMaxHP = 600.0
var flagshipBossX = cwidth / 2.0
var flagshipBossY = cheight - 140.0
var bossMovementPhase = 0.0
var bossShieldActive = true

// ============================================================================
// ASSET POSITIONING & DYNAMICS
// ============================================================================
var commandoPosX = cwidth / 2.0 - 240.0
var commandoPosY = 35.0
var commandoState = "WALK"

var defenseTankPosX = cwidth / 2.0 + 90.0
val defenseTankPosY = 32.0
var mainTurretAngle = math.Pi / 2.0
var tankRecoilAmount = 0.0
var cannonMuzzleFlashTimer = 0

var siloXPos = cwidth / 2.0 - 260.0
var siloYPos = 145.0
var siloLauncherRotation = math.Pi / 2.0
var activeMissileTypeScript = 1

// VEG / STRATEGIC DEFENSE ASSETS
var akashBatteryX = cwidth / 2.0 - 90.0
var akashBatteryY = 110.0
var pinakaBatteryX = cwidth / 2.0 + 200.0
var pinakaBatteryY = 120.0
var pinakaSalvoCooldown = 0

// ============================================================================
// INTERACTION & ENGINE TIMERS
// ============================================================================
var mainGunCooldownTimer = 0
var siloLaunchCooldownTimer = 0
var radarRotationAngle = 0.0
var screenShakeMagnitude = 0.0
var globalColorFlash = 0.0
var universalClock = 0.0

// ============================================================================
// BRIEFING TEXT LOGS
// ============================================================================
val classifiedBriefingLog =
  "TOP SECRET DIRECTIVE: OPERATION SINDOOR - VEG & QUANTUM SHIELD\n" +
  "COMMAND AUTHORITY: DEFENSE STRATEGIC COMMAND HQ (SECURE NET)\n\n" +
  "Massive inbound enemy armadas detected. Perimeter integrity critical.\n" +
  "New Indigenous Veg/Air Defense Units (Akash & Pinaka Systems) Online.\n\n" +
  "TACTICAL COMMAND CONTROLS:\n" +
  "- Reposition Tanks & Adjust Turrets via Tactical HUD Matrix.\n" +
  "- Deploy Akash Surface-to-Air & Pinaka Multi-Barrel Rocket Grids.\n\n" +
  "Click 'INITIALIZE HYPER DEFENSE' to commence deployment."

// ============================================================================
// AUDIO SYNTHESIS SUBSYSTEM
// ============================================================================
var audioEngineEnabled = false
try {
  setNoteInstrument(Instrument.SYNTH_STRINGS)
  audioEngineEnabled = true
} catch {
  case _: Exception => audioEngineEnabled = false
}

def playTacticalTone(noteVal: Int, durationMillis: Int): Unit = {
  if (audioEngineEnabled) {
    try { playNote(noteVal, durationMillis) } catch { case _: Exception => }
  }
}

// ============================================================================
// PROCEDURAL STARFIELD ARRAYS
// ============================================================================
val starXCoords = Array.fill(MAX_STARS_COUNT)(randomDouble(0.0, cwidth))
val starYCoords = Array.fill(MAX_STARS_COUNT)(randomDouble(100.0, cheight))
val starSizes = Array.fill(MAX_STARS_COUNT)(randomDouble(0.3, 4.0))
val starTwinkleSpeeds = Array.fill(MAX_STARS_COUNT)(randomDouble(1.0, 7.0))

// ============================================================================
// ADVANCED PARTICLE ENGINE
// ============================================================================
case class QuantumParticle(
  var posX: Double,
  var posY: Double,
  var velX: Double,
  var velY: Double,
  var lifeSpan: Double,
  var baseColor: Color,
  var scaleFactor: Double
)

val activeParticleSystem = ArrayBuffer.empty[QuantumParticle]

def spawnParticleExplosion(x: Double, y: Double, col: Color, count: Int): Unit = {
  for (_ <- 0 until count) {
    if (activeParticleSystem.length < MAX_PARTICLE_CAP) {
      activeParticleSystem.append(
        QuantumParticle(
          x, y,
          randomDouble(-8.0, 8.0),
          randomDouble(-7.5, 7.5),
          1.0,
          col,
          randomDouble(3.0, 10.0)
        )
      )
    }
  }
}

def updateParticleSystem(): Unit = {
  activeParticleSystem.filterInPlace(_.lifeSpan > 0.0)
  activeParticleSystem.foreach { p =>
    p.posX += p.velX
    p.posY += p.velY
    p.velX *= 0.94
    p.velY *= 0.94
    p.lifeSpan -= 0.02
    p.scaleFactor *= 0.982
  }
}

def renderParticleSystem(c: CanvasDraw): Unit = {
  activeParticleSystem.foreach { p =>
    val alphaVal = math.max(0, math.min(255, (p.lifeSpan * 255.0).toInt))
    c.fill(p.baseColor.getRed, p.baseColor.getGreen, p.baseColor.getBlue, alphaVal)
    c.noStroke()
    c.ellipse(p.posX, p.posY, p.scaleFactor, p.scaleFactor)
  }
}

// ============================================================================
// COMBAT SPARKS & SHOCKWAVES
// ============================================================================
class TacticalSpark(xPos: Double, yPos: Double, r: Int, g: Int, b: Int) {
  var px = xPos
  var py = yPos
  var vx = randomDouble(-11.0, 11.0)
  var vy = randomDouble(-11.0, 11.0)
  var life = 1.0

  def update(): Unit = {
    px += vx
    py += vy
    vx *= 0.88
    vy *= 0.88
    life -= 0.035
  }

  def render(c: CanvasDraw): Unit = {
    if (life > 0.0) {
      c.fill(r, g, b, math.max(0, math.min(255, (life * 255.0).toInt)))
      c.noStroke()
      c.ellipse(px, py, 6.0, 6.0)
    }
  }
}

val tacticalSparks = ArrayBuffer.empty[TacticalSpark]

class ShockwaveRing(val cx: Double, val cy: Double) {
  var radius = 5.0
  var alphaVal = 255.0
  var active = true

  def update(): Unit = {
    radius += 12.0
    alphaVal -= 16.0
    if (alphaVal <= 0.0) active = false
  }

  def render(c: CanvasDraw): Unit = {
    if (active) {
      c.noFill()
      c.stroke(0, 255, 255, math.max(0, math.min(255, alphaVal.toInt)))
      c.strokeWeight(4.0)
      c.ellipse(cx, cy, radius * 2.0, radius * 2.0)
    }
  }
}

val shockwaveRings = ArrayBuffer.empty[ShockwaveRing]

// ============================================================================
// PROJECTILES & MISSILES
// ============================================================================
class TankShell(var posX: Double, var posY: Double, val vx: Double, val vy: Double) {
  var active = true

  def update(): Unit = {
    posX += vx
    posY += vy
    if (posX < -50 || posX > cwidth + 50 || posY < -50 || posY > cheight + 50) active = false
  }

  def render(c: CanvasDraw): Unit = {
    c.fill(255, 255, 150)
    c.noStroke()
    c.ellipse(posX, posY, 14.0, 14.0)
  }
}

val tankShells = ArrayBuffer.empty[TankShell]

class EnemyDrone(var posX: Double, var posY: Double, val speedY: Double, val pattern: Int) {
  var active = true
  var destroyed = false
  val trail = ArrayBuffer.empty[(Double, Double)]

  def update(): Unit = {
    if (!destroyed) {
      posY += speedY
      if (pattern == 2) posX += math.sin(universalClock * 5.0 + posY * 0.02) * 4.0
      if (pattern == 3) posX += math.cos(universalClock * 6.0 + posY * 0.025) * 5.5
      
      trail.append((posX, posY))
      if (trail.length > 25) trail.remove(0)
      if (posY < -70.0) active = false
    }
  }

  def render(c: CanvasDraw): Unit = {
    if (!destroyed) {
      var i = 0
      while (i < trail.length) {
        val pt = trail(i)
        val alpha = math.max(0, math.min(255, (i.toDouble / trail.length.toDouble * 220.0).toInt))
        c.fill(255, 60, 60, alpha)
        c.noStroke()
        c.ellipse(pt._1, pt._2, 5.0, 5.0)
        i += 1
      }
      c.pushMatrix()
      c.translate(posX, posY)
      c.rotate(-math.Pi / 2.0)
      c.fill(230, 40, 40)
      c.stroke(100, 10, 10)
      c.strokeWeight(1.2)
      c.rect(-22, -9, 44, 18)
      c.fill(255, 200, 80)
      c.noStroke()
      c.ellipse(23, 0, 16, 16)
      c.popMatrix()
    }
  }
}

val enemyDrones = ArrayBuffer.empty[EnemyDrone]

class InterceptorMissile(var posX: Double, var posY: Double, val speed: Double, var angle: Float, val classType: Int) {
  var active = true
  val trail = ArrayBuffer.empty[(Double, Double)]

  def update(): Unit = {
    if (classType == 2) angle += 0.025f
    if (classType == 3) angle += math.sin(universalClock * 8.0).toFloat * 0.05f

    posX += math.cos(angle) * speed
    posY += math.sin(angle) * speed
    
    trail.append((posX, posY))
    if (trail.length > 25) trail.remove(0)
    if (posX < -150 || posX > cwidth + 150 || posY < -150 || posY > cheight + 150) active = false
  }

  def render(c: CanvasDraw): Unit = {
    var i = 0
    while (i < trail.length) {
      val pt = trail(i)
      val alpha = math.max(0, math.min(255, (i.toDouble / trail.length.toDouble * 230.0).toInt))
      c.fill(0, 255, 220, alpha)
      c.noStroke()
      c.ellipse(pt._1, pt._2, 6.0, 6.0)
      i += 1
    }
    c.pushMatrix()
    c.translate(posX, posY)
    c.rotate(angle)
    c.fill(240, 255, 255)
    c.stroke(0, 120, 150)
    c.strokeWeight(1.2)
    c.rect(-24, -9, 48, 18)
    c.fill(0, 240, 255)
    c.noStroke()
    c.ellipse(24, 0, 16, 16)
    c.popMatrix()
  }
}

val interceptorMissiles = ArrayBuffer.empty[InterceptorMissile]

class FireworkEffect {
  val colors = Array(cm.rgb(255, 153, 51), cm.rgb(255, 255, 255), cm.rgb(18, 136, 7), cm.rgb(0, 255, 255))
  val fColor: Color = colors(randomDouble(0.0, colors.length.toDouble).toInt)
  val cx = randomDouble(80.0, cwidth - 80.0)
  val cy = randomDouble(250.0, cheight - 100.0)
  var radius = 0.0
  var active = true

  def update(): Unit = {
    radius += 8.5
    if (radius > 160.0) active = false
  }

  def render(c: CanvasDraw): Unit = {
    if (active) {
      for (i <- 0 until 42) {
        val rad = i.toDouble * math.Pi * 2.0 / 42.0
        val dist = radius * (0.8 + math.sin(universalClock + i.toDouble) * 0.2)
        val px = cx + math.cos(rad) * dist
        val py = cy + math.sin(rad) * dist
        val alpha = math.max(0, math.min(255, (250.0 - radius).toInt))
        c.fill(fColor.getRed, fColor.getGreen, fColor.getBlue, alpha)
        c.noStroke()
        c.ellipse(px, py, 4.5, 4.5)
      }
    }
  }
}

val fireworkEffects = ArrayBuffer.empty[FireworkEffect]

def triggerCataclysmicExplosion(x: Double, y: Double): Unit = {
  shockwaveRings.append(new ShockwaveRing(x, y))
  for (_ <- 0 until 35) {
    tacticalSparks.append(new TacticalSpark(x, y, 255, random(140, 255), 80))
  }
  spawnParticleExplosion(x, y, cm.rgb(255, 190, 60), 28)
  screenShakeMagnitude = 14.0
  globalColorFlash = 0.4
  playTacticalTone(55, 80)
}

// ============================================================================
// MOUSE INTERACTION & CONTROLS HANDLER
// ============================================================================
def handleInteractiveControls(): Unit = {
  if (currentGameState == 2 && isMousePressed) {
    val mx = mouseX
    val my = mouseY

    if (mx >= 20 && mx <= 70 && my >= 90 && my <= 125) {
      defenseTankPosX -= 6.0
    } else if (mx >= 75 && mx <= 125 && my >= 90 && my <= 125) {
      defenseTankPosX += 6.0
    } else if (mx >= 130 && mx <= 180 && my >= 90 && my <= 125) {
      mainTurretAngle -= 0.06
    } else if (mx >= 185 && mx <= 235 && my >= 90 && my <= 125) {
      mainTurretAngle += 0.06
    } else if (mx >= 240 && mx <= 295 && my >= 90 && my <= 125) {
      if (mainGunCooldownTimer <= 0) {
        val mX = defenseTankPosX + math.cos(mainTurretAngle) * 80.0
        val mY = defenseTankPosY + 48.0 + math.sin(mainTurretAngle) * 80.0
        tankShells.append(new TankShell(mX, mY, math.cos(mainTurretAngle) * 14.0, math.sin(mainTurretAngle) * 14.0))
        mainGunCooldownTimer = 7
        cannonMuzzleFlashTimer = 6
        tankRecoilAmount = 1.5
        playTacticalTone(72, 40)
      }
    } else if (mx >= 300 && mx <= 345 && my >= 90 && my <= 125) {
      siloLauncherRotation -= 0.06
    } else if (mx >= 350 && mx <= 395 && my >= 90 && my <= 125) {
      siloLauncherRotation += 0.06
    } else if (mx >= 400 && mx <= 445 && my >= 90 && my <= 125) {
      activeMissileTypeScript = 1
    } else if (mx >= 450 && mx <= 495 && my >= 90 && my <= 125) {
      activeMissileTypeScript = 2
    } else if (mx >= 500 && mx <= 545 && my >= 90 && my <= 125) {
      activeMissileTypeScript = 3
    } else if (mx >= 555 && mx <= 630 && my >= 90 && my <= 125) {
      if (siloLaunchCooldownTimer <= 0) {
        val tX = siloXPos + math.cos(siloLauncherRotation) * 65.0
        val tY = siloYPos + math.sin(siloLauncherRotation) * 65.0
        var speed = 9.0
        if (activeMissileTypeScript == 2) speed = 8.0
        if (activeMissileTypeScript == 3) speed = 11.5

        interceptorMissiles.append(new InterceptorMissile(tX, tY, speed, siloLauncherRotation.toFloat, activeMissileTypeScript))
        siloLaunchCooldownTimer = 12
        playTacticalTone(68, 50)
      }
    }
  }
}

// ============================================================================
// COLLISION EVALUATION ROUTINES
// ============================================================================
def evaluateCombatCollisions(): Unit = {
  repeatFor(enemyDrones) { drone =>
    if (!drone.destroyed) {
      repeatFor(tankShells) { shell =>
        if (shell.active && math.abs(shell.posX - drone.posX) < 38.0 && math.abs(shell.posY - drone.posY) < 38.0) {
          drone.destroyed = true
          shell.active = false
          scoreDestroyedCount += 1
          triggerCataclysmicExplosion(drone.posX, drone.posY)
        }
      }
      repeatFor(interceptorMissiles) { missile =>
        if (missile.active && math.abs(missile.posX - drone.posX) < 45.0 && math.abs(missile.posY - drone.posY) < 45.0) {
          drone.destroyed = true
          missile.active = false
          scoreDestroyedCount += 1
          triggerCataclysmicExplosion(drone.posX, drone.posY)
        }
      }
    }
  }

  if (flagshipBossActive) {
    repeatFor(tankShells) { shell =>
      if (shell.active && math.abs(shell.posX - flagshipBossX) < 65.0 && math.abs(shell.posY - flagshipBossY) < 65.0) {
        shell.active = false
        flagshipBossHP -= 15.0
        triggerCataclysmicExplosion(shell.posX, shell.posY)
      }
    }
    repeatFor(interceptorMissiles) { missile =>
      if (missile.active && math.abs(missile.posX - flagshipBossX) < 70.0 && math.abs(missile.posY - flagshipBossY) < 70.0) {
        missile.active = false
        flagshipBossHP -= 35.0
        triggerCataclysmicExplosion(missile.posX, missile.posY)
      }
    }
  }
}

// ============================================================================
// ENTITY REFRESH CYCLE
// ============================================================================
def refreshAllEntities(): Unit = {
  tankShells.filterInPlace(_.active)
  repeatFor(tankShells)(_.update())

  interceptorMissiles.filterInPlace(_.active)
  repeatFor(interceptorMissiles)(_.update())

  enemyDrones.filterInPlace(_.active)
  repeatFor(enemyDrones)(_.update())

  tacticalSparks.filterInPlace(_.life > 0.0)
  repeatFor(tacticalSparks)(_.update())

  shockwaveRings.filterInPlace(_.active)
  repeatFor(shockwaveRings)(_.update())

  fireworkEffects.filterInPlace(_.active)
  repeatFor(fireworkEffects)(_.update())
}

// ============================================================================
// STATE MACHINE ENGINE UPDATE
// ============================================================================
def processGameStateEngine(): Unit = {
  universalClock += 0.025
  radarRotationAngle += 0.08
  screenShakeMagnitude *= 0.85
  globalColorFlash *= 0.83

  if (mainGunCooldownTimer > 0) mainGunCooldownTimer -= 1
  if (siloLaunchCooldownTimer > 0) siloLaunchCooldownTimer -= 1
  if (cannonMuzzleFlashTimer > 0) cannonMuzzleFlashTimer -= 1
  if (pinakaSalvoCooldown > 0) pinakaSalvoCooldown -= 1
  tankRecoilAmount *= 0.86

  updateParticleSystem()

  // STATE 0: BRIEFING
  if (currentGameState == 0) {
    briefingDelta += 0.04
    if (briefingDelta >= 0.025 && briefingIndex < classifiedBriefingLog.length) {
      briefingIndex += 1
      briefingDelta = 0.0
      if (briefingIndex % 5 == 0) playTacticalTone(74, 10)
    }
    return
  }

  // STATE 1: COMMANDER ARRIVAL
  if (currentGameState == 1) {
    if (commandoPosX < defenseTankPosX - 30.0) {
      commandoPosX += 3.5
    } else {
      commandoState = "BOARDED"
      currentGameState = 2
      playTacticalTone(78, 120)
    }
    return
  }

  // STATE 2: ACTIVE DEFENSE COMBAT
  if (currentGameState == 2) {
    handleInteractiveControls()

    combatTimer -= 0.033
    globalElapsedTime += 0.033

    refreshAllEntities()
    evaluateCombatCollisions()

    if (pinakaSalvoCooldown <= 0 && enemyDrones.nonEmpty) {
      val targetDrone = enemyDrones.head
      interceptorMissiles.append(new InterceptorMissile(pinakaBatteryX, pinakaBatteryY, 12.0, math.Pi.toFloat / 2.0f, 3))
      pinakaSalvoCooldown = 45
    }

    if (defenseTankPosX < 60.0) defenseTankPosX = 60.0
    if (defenseTankPosX > cwidth - 60.0) defenseTankPosX = cwidth - 60.0

    if (combatTimer <= 40.0 && !flagshipBossActive && scoreDestroyedCount < quotaRequirement) {
      flagshipBossActive = true
    }

    if (randomDouble(0.0, 1.0) < 0.04) {
      val spawnX = randomDouble(90.0, cwidth - 90.0)
      val patternType = if (combatTimer < 35.0) 3 else 2
      enemyDrones.append(new EnemyDrone(spawnX, cheight + 35.0, -0.75, patternType))
    }

    if (flagshipBossActive) {
      bossMovementPhase += 0.04
      flagshipBossX = cwidth / 2.0 + math.sin(bossMovementPhase * 2.2) * 220.0
      if (flagshipBossHP <= 0.0 || combatTimer <= 0.0) {
        flagshipBossActive = false
        currentGameState = 4
        ultimateStrikeX = defenseTankPosX + 260.0
        ultimateStrikeY = cheight + 150.0
        playTacticalTone(42, 220)
      }
    } else {
      if (combatTimer <= 0.0 || scoreDestroyedCount >= quotaRequirement) {
        combatTimer = 0.0
        currentGameState = 4
        ultimateStrikeX = defenseTankPosX + 260.0
        ultimateStrikeY = cheight + 150.0
        playTacticalTone(42, 220)
      }
    }

    if (randomDouble(0.0, 1.0) < 0.03) {
      fireworkEffects.append(new FireworkEffect())
    }
    return
  }

  // STATE 4: CINEMATIC FINALE MISSILE
  if (currentGameState == 4) {
    ultimateStrikeX += (defenseTankPosX - ultimateStrikeX) * 0.08
    ultimateStrikeY -= 9.0

    spawnParticleExplosion(ultimateStrikeX, ultimateStrikeY + 25.0, cm.rgb(255, 150, 50), 3)
    refreshAllEntities()

    if (ultimateStrikeY < defenseTankPosY + 40.0) {
      triggerCataclysmicExplosion(defenseTankPosX, defenseTankPosY + 45.0)
      currentGameState = 5
      playTacticalTone(36, 280)
    }
    return
  }

  // STATE 5: TRIBUTE SCREEN
  if (currentGameState == 5) {
    refreshAllEntities()
    return
  }
}

// ============================================================================
// GRAPHICAL RENDERING MODULES
// ============================================================================
def renderStarfieldBackground(c: CanvasDraw): Unit = {
  for (i <- 0 until MAX_STARS_COUNT) {
    val alphaVal = math.max(60, math.min(255, (100.0 + math.abs(math.sin(universalClock * starTwinkleSpeeds(i) + i.toDouble)) * 155.0).toInt))
    c.fill(255, 255, 255, alphaVal)
    c.noStroke()
    val drift = math.sin(universalClock * 0.15 + i.toDouble) * 2.0
    c.ellipse(starXCoords(i) + drift, starYCoords(i), starSizes(i), starSizes(i))
  }
}

def renderAtmosphericBackdrop(c: CanvasDraw): Unit = {
  c.background(2, 5, 20)
  renderStarfieldBackground(c)

  c.fill(20, 60, 180, 35)
  c.noStroke()
  c.ellipse(220, cheight - 100, 420, 240)

  c.fill(150, 20, 190, 28)
  c.ellipse(cwidth - 220, 220, 380, 200)
}

def renderMainDefenseTank(c: CanvasDraw): Unit = {
  c.fill(0, 0, 0, 120)
  c.noStroke()
  c.ellipse(defenseTankPosX, defenseTankPosY - 5, 150, 22)

  c.fill(40, 45, 55)
  c.stroke(20, 25, 35)
  c.strokeWeight(1.5)
  c.rect(defenseTankPosX - 65, defenseTankPosY, 130, 30)

  c.fill(100, 105, 100)
  for (i <- -4 to 4) {
    c.ellipse(defenseTankPosX + i * 13.5, defenseTankPosY + 15, 11.5, 11.5)
  }

  c.fill(70, 115, 62)
  c.rect(defenseTankPosX - 52, defenseTankPosY + 30, 104, 28)

  c.fill(60, 100, 55)
  c.ellipse(defenseTankPosX, defenseTankPosY + 56, 56, 30)

  val barrelLen = 85.0
  val tX = defenseTankPosX + math.cos(mainTurretAngle) * barrelLen
  val tY = defenseTankPosY + 56.0 + math.sin(mainTurretAngle) * barrelLen

  c.stroke(40, 75, 35)
  c.strokeWeight(10.0 + tankRecoilAmount * 3.5)
  c.line(defenseTankPosX, defenseTankPosY + 56.0, tX, tY)

  if (cannonMuzzleFlashTimer > 0) {
    c.noStroke()
    c.fill(255, 200, 60, 250)
    c.ellipse(tX, tY, 60, 60)
    c.fill(255, 255, 220, 250)
    c.ellipse(tX, tY, 26, 26)
  }
}

def renderStrategicSilo(c: CanvasDraw): Unit = {
  c.fill(60, 65, 75)
  c.stroke(25, 32, 40)
  c.strokeWeight(2.2)
  c.ellipse(siloXPos, siloYPos, 72, 72)

  c.pushMatrix()
  c.translate(siloXPos, siloYPos)
  c.rotate(siloLauncherRotation)
  c.fill(80, 105, 95)
  c.rect(-18, -22, 78, 44)
  c.fill(250, 50, 50)
  c.noStroke()
  c.rect(21, -22, 11, 44)
  c.popMatrix()
}

def renderVegDefenseBatteries(c: CanvasDraw): Unit = {
  c.fill(45, 85, 60)
  c.stroke(20, 45, 30)
  c.strokeWeight(2.0)
  c.rect(akashBatteryX - 35, akashBatteryY - 15, 70, 30)
  c.fill(0, 255, 180)
  c.noStroke()
  c.ellipse(akashBatteryX, akashBatteryY, 14, 14)
  c.fill(255, 255, 255)
  c.text("AKASH", akashBatteryX - 22, akashBatteryY - 22)

  c.fill(85, 65, 45)
  c.stroke(45, 32, 20)
  c.strokeWeight(2.0)
  c.rect(pinakaBatteryX - 40, pinakaBatteryY - 18, 80, 36)
  c.fill(255, 153, 51)
  c.noStroke()
  c.ellipse(pinakaBatteryX - 15, pinakaBatteryY, 8, 8)
  c.ellipse(pinakaBatteryX, pinakaBatteryY, 8, 8)
  c.ellipse(pinakaBatteryX + 15, pinakaBatteryY, 8, 8)
  c.fill(255, 255, 255)
  c.text("PINAKA", pinakaBatteryX - 25, pinakaBatteryY - 26)
}

def renderCommandoHero(c: CanvasDraw, x: Double, y: Double): Unit = {
  if (commandoState == "BOARDED") return

  val stride = math.sin(universalClock * 16.0) * 6.0

  c.fill(0, 0, 0, 90)
  c.noStroke()
  c.ellipse(x, y - 5, 30, 10)

  c.fill(250, 250, 255)
  c.stroke(180, 190, 205)
  c.strokeWeight(1.2)
  c.rect(x - 9, y, 18, 26)

  c.fill(0, 190, 255)
  c.ellipse(x, y + 33, 22, 22)

  c.stroke(240, 245, 255)
  c.strokeWeight(5.0)
  c.line(x - 3, y, x - 9 - stride, y - 14)
  c.line(x + 3, y, x + 9 + stride, y - 14)
  c.line(x - 10, y + 20, x - 17, y + 8)
  c.line(x + 10, y + 20, x + 17, y + 8)
}

def renderEmblemTricolorFlag(c: CanvasDraw): Unit = {
  val fX = cwidth - 160.0
  val fY = cheight - 180.0

  c.stroke(245, 245, 245)
  c.strokeWeight(6.5)
  c.line(fX, fY - 130.0, fX, fY + 100.0)

  var cXOffset = 0.0
  while (cXOffset < 135.0) {
    val wave = math.sin(universalClock * 4.5 + cXOffset * 0.08) * 8.0
    c.strokeWeight(3.8)

    c.stroke(255, 153, 51)
    c.line(fX + cXOffset, fY + 52.0 + wave, fX + cXOffset, fY + 75.0 + wave)

    c.stroke(255, 255, 255)
    c.line(fX + cXOffset, fY + 29.0 + wave, fX + cXOffset, fY + 52.0 + wave)

    c.stroke(18, 136, 7)
    c.line(fX + cXOffset, fY + wave, fX + cXOffset, fY + 29.0 + wave)

    cXOffset += 3.0
  }

  c.noFill()
  c.stroke(0, 0, 200)
  c.strokeWeight(2.5)
  c.ellipse(fX + 67.0, fY + 39.0, 22.0, 22.0)
}

def renderFlagshipBoss(c: CanvasDraw): Unit = {
  if (!flagshipBossActive) return

  c.fill(210, 50, 50)
  c.stroke(255, 220, 80)
  c.strokeWeight(3.0)
  c.ellipse(flagshipBossX, flagshipBossY, 110, 55)

  c.fill(60, 60, 75)
  c.rect(flagshipBossX - 38, flagshipBossY - 38, 76, 24)

  c.fill(50, 50, 50)
  c.rect(flagshipBossX - 60, flagshipBossY + 45, 120, 10)
  c.fill(255, 60, 60)
  c.rect(flagshipBossX - 60, flagshipBossY + 45, 120.0 * (flagshipBossHP / flagshipBossMaxHP), 10)
}

def renderHUDStatusOverlay(c: CanvasDraw): Unit = {
  c.fill(10, 15, 35, 250)
  c.stroke(0, 255, 255)
  c.strokeWeight(2.2)
  c.rect(18, cheight - 62, 490, 50)

  c.fill(255, 220, 0)
  c.text(s"TIME REMAINING: ${combatTimer.toInt}s", 28, cheight - 32)

  c.fill(120, 255, 230)
  c.text(s"TARGETS: $scoreDestroyedCount / $quotaRequirement", 185, cheight - 32)

  val ratio = math.max(0.0, math.min(1.0, combatTimer / 100.0))
  c.fill(30, 40, 70)
  c.rect(345, cheight - 50, 150, 10)
  c.fill(255, 153, 51)
  c.rect(345, cheight - 50, 150.0 * ratio, 10)
}

def renderTacticalControlPanel(c: CanvasDraw): Unit = {
  c.fill(14, 20, 45, 255)
  c.stroke(0, 255, 255)
  c.strokeWeight(1.8)
  c.rect(15, 88, 710, 52)

  c.fill(70, 75, 110)
  c.rect(20, 96, 52, 32)
  c.rect(78, 96, 52, 32)
  c.fill(255, 255, 255)
  c.text("T-", 38, 116)
  c.text("T+", 96, 116)

  c.fill(100, 50, 50)
  c.rect(136, 96, 52, 32)
  c.rect(194, 96, 52, 32)
  c.fill(255, 255, 255)
  c.text("C-", 154, 116)
  c.text("C+", 212, 116)

  c.fill(240, 100, 0)
  c.rect(252, 96, 60, 32)
  c.fill(255, 255, 255)
  c.text("FIRE", 267, 116)

  c.fill(50, 120, 120)
  c.rect(318, 96, 48, 32)
  c.rect(372, 96, 48, 32)
  c.fill(255, 255, 255)
  c.text("M-", 332, 116)
  c.text("M+", 386, 116)

  c.fill(if (activeMissileTypeScript == 1) 240 else 90, 35, 150)
  c.rect(426, 96, 48, 32)
  c.fill(if (activeMissileTypeScript == 2) 240 else 90, 35, 150)
  c.rect(480, 96, 48, 32)
  c.fill(if (activeMissileTypeScript == 3) 240 else 90, 35, 150)
  c.rect(534, 96, 48, 32)

  c.fill(255, 255, 255)
  c.text("A", 444, 116)
  c.text("B", 498, 116)
  c.text("C", 552, 116)

  c.fill(200, 0, 240)
  c.rect(592, 96, 120, 32)
  c.fill(255, 255, 255)
  c.text("HYPER LAUNCH", 602, 116)
}

def renderTacticalBriefingTerminal(c: CanvasDraw): Unit = {
  renderStarfieldBackground(c)

  val tX = cwidth / 2.0 - 310.0
  val tY = cheight / 2.0 - 180.0

  c.fill(10, 18, 45, 255)
  c.stroke(0, 255, 255)
  c.strokeWeight(3.5)
  c.rect(tX, tY, 620, 380)

  c.fill(255, 220, 0)
  c.text("OPERATION SINDOOR - VEG & QUANTUM TERMINAL V9.1", tX + 70, tY + 28)

  val partialText = classifiedBriefingLog.substring(0, briefingIndex)
  val lines = partialText.split("\n")
  var cursorY = tY + 60.0

  for (ln <- lines) {
    c.fill(240, 245, 255)
    c.text(ln, tX + 25, cursorY)
    cursorY += 20.0
  }

  if (briefingIndex >= classifiedBriefingLog.length) {
    val bX = tX + 185.0
    val bY = tY - 65.0

    c.fill(0, 230, 150)
    c.stroke(255, 255, 255)
    c.strokeWeight(3.0)
    c.rect(bX, bY, 250, 44)

    c.fill(255, 255, 255)
    c.text("INITIALIZE HYPER DEFENSE", bX + 20, bY + 28)

    if (isMousePressed && mouseX >= bX && mouseX <= bX + 250 && mouseY >= bY && mouseY <= bY + 44) {
      currentGameState = 1
      playTacticalTone(80, 140)
    }
  }
}

def renderUltimateTributeScreen(c: CanvasDraw): Unit = {
  c.background(0, 0, 0)

  for (i <- 0 until 140) {
    val px = math.abs(math.sin(universalClock * 0.4 + i.toDouble)) * (cwidth - 1.0)
    val py = 40.0 + math.abs(math.sin(i.toDouble * 1.6)) * (cheight - 80.0)

    c.fill(255, 255, 255, 200)
    c.noStroke()
    c.ellipse(px, py, 2.5, 2.5)
  }

  c.fill(255, 255, 255)
  c.text("MISSION ACCOMPLISHED WITH SUPREME GLORY", cwidth / 2.0 - 130, cheight / 2.0 + 80)

  c.fill(255, 235, 180)
  c.text("SALUTE TO OUR BRAVE DEFENDERS & VEG SYSTEMS", cwidth / 2.0 - 160, cheight / 2.0 + 20)

  c.fill(250, 250, 250)
  c.text("Ultimate Valor. Absolute Honor. Invincible Protection.", cwidth / 2.0 - 140, cheight / 2.0 - 30)

  c.fill(255, 220, 100)
  c.text("JAI HIND", cwidth / 2.0 - 45, cheight / 2.0 - 90)

  c.strokeWeight(5.5)
  c.stroke(255, 153, 51)
  c.line(cwidth / 2.0 - 220, 65, cwidth / 2.0 + 220, 65)

  c.stroke(255, 255, 255)
  c.line(cwidth / 2.0 - 220, 56, cwidth / 2.0 + 220, 56)

  c.stroke(18, 136, 7)
  c.line(cwidth / 2.0 - 220, 47, cwidth / 2.0 + 220, 47)
}

// ============================================================================
// MAIN PIPELINE & EXECUTION RENDERER
// ============================================================================
def renderGamePipeline(c: CanvasDraw): Unit = {
  if (currentGameState == 0) {
    renderTacticalBriefingTerminal(c)
    return
  }

  if (currentGameState == 1) {
    renderAtmosphericBackdrop(c)
    renderMainDefenseTank(c)
    renderStrategicSilo(c)
    renderVegDefenseBatteries(c)
    renderCommandoHero(c, commandoPosX, commandoPosY)
    renderEmblemTricolorFlag(c)
    c.fill(255, 220, 90)
    c.text("HYPER QUANTUM COMMANDER DEPLOYMENT IN PROGRESS...", cwidth / 2.0 - 150, cheight - 35)
    return
  }

  if (currentGameState == 5) {
    renderUltimateTributeScreen(c)
    return
  }

  renderAtmosphericBackdrop(c)

  if (screenShakeMagnitude > 0.5) {
    c.pushMatrix()
    c.translate(randomDouble(-screenShakeMagnitude, screenShakeMagnitude), randomDouble(-screenShakeMagnitude, screenShakeMagnitude))
  }

  renderEmblemTricolorFlag(c)
  renderStrategicSilo(c)
  renderMainDefenseTank(c)
  renderVegDefenseBatteries(c)
  renderFlagshipBoss(c)

  renderParticleSystem(c)

  repeatFor(fireworkEffects)(_.render(c))
  repeatFor(tankShells)(_.render(c))
  repeatFor(interceptorMissiles)(_.render(c))
  repeatFor(enemyDrones)(_.render(c))
  repeatFor(tacticalSparks)(_.render(c))
  repeatFor(shockwaveRings)(_.render(c))

  if (currentGameState == 2) {
    renderTacticalControlPanel(c)
    renderHUDStatusOverlay(c)
  }

  if (currentGameState == 4) {
    c.pushMatrix()
    c.translate(ultimateStrikeX, ultimateStrikeY)
    c.rotate(ultimateStrikeAngle.toFloat)
    c.fill(250, 80, 60)
    c.stroke(100, 20, 20)
    c.strokeWeight(3.0)
    c.rect(-40, -15, 80, 30)
    c.fill(255, 235, 120)
    c.noStroke()
    c.ellipse(38, 0, 26, 26)
    c.popMatrix()

    c.fill(255, 120, 80)
    c.text("ULTIMATE HYPER QUANTUM STRIKE SEQUENCE ACTIVE", cwidth / 2.0 - 145, cheight - 40)
  }

  if (screenShakeMagnitude > 0.5) {
    c.popMatrix()
  }

  if (globalColorFlash > 0.01) {
    c.fill(255, 245, 200, math.max(0, math.min(255, (globalColorFlash * 255.0).toInt)))
    c.noStroke()
    c.rect(0, 0, cwidth, cheight)
  }
}

// ============================================================================
// CORE ANIMATION EXECUTION LOOP
// ============================================================================
animateWithCanvasDraw { c =>
  processGameStateEngine()
  renderGamePipeline(c)
}