package com.carter.twr;

import robocode.*;

import java.util.HashMap;
import java.util.Map;

/*
    T34, T70 and Fire buster
 */
public class Panther_II extends AdvancedRobot {

    private int moveDirection = 1;
    private double lastEnemyHeading = 0.0;
    private int successfulOffset = 0;
    private int evasiveActionCounter = 1;

    public void run() {
        setAdjustRadarForGunTurn(true);
        setAdjustGunForRobotTurn(true);
        setTurnRadarRight(1000);
        execute();
        while (true) {
            if (getRadarTurnRemaining() == 0) {
                setTurnRadarRight(90);
            }
            execute();
        }
    }

    public void onScannedRobot(ScannedRobotEvent e) {
        double firepower = Math.min(400 / e.getDistance(), 3);
        firepower = 3.0;
        double currentEnemyBearing = e.getBearing();
        double currentEnemyHeading = e.getHeading();
        double turnGunRightDegrees = getHeading() - getGunHeading() + currentEnemyBearing;
        out.println("turnGunRightDegrees: " + turnGunRightDegrees);
        out.println("enemyBearing: " + currentEnemyBearing);
        out.println("currentEnemyHeading: " + currentEnemyHeading);
        double adjustment = TankUtils.simpleRotationalTargeting(currentEnemyBearing);
        if (Math.abs(lastEnemyHeading - currentEnemyHeading) < 0.2) {
            adjustment = 0;
        }
        lastEnemyHeading = currentEnemyHeading;
        setTurnGunRight(TankUtils.normalizeBearing(turnGunRightDegrees + adjustment));
        setTurnRight(currentEnemyBearing);
        if (getVelocity() == 0)  {
            moveDirection *= -1;
        }
        setTurnRight(TankUtils.normalizeBearing(e.getBearing() + 90));
        if (getTime() % 20 == 0) {
            moveDirection *= -1;
            setAhead(150 * moveDirection);
        }
        if (getGunHeat() == 0 && Math.abs(getGunTurnRemaining()) < 10)  {
            Bullet bullet = setFireBullet(firepower);
        }
        setTurnRadarRight(getHeading() - getRadarHeading() + currentEnemyBearing);
    }

    @Override
    public void onHitWall(HitWallEvent e) {
        takeEvasiveAction();
    }

    private void takeEvasiveAction() {
        out.println("evasiveActionCounter: " + evasiveActionCounter);
        if (evasiveActionCounter <= 0) {
            moveDirection *= -1;
            setAhead(250 * moveDirection);
            execute();
            evasiveActionCounter = 2;
            out.println("taking evasive action");
        } else {
            evasiveActionCounter--;
        }
    }

    @Override
    public void onHitByBullet(HitByBulletEvent e) {
        takeEvasiveAction();
    }

    @Override
    public void onBulletHit(BulletHitEvent e) {
        out.println("hit scored");
    }


}

@@ -270,28 +270,25 @@
 int 
-successfulOffset = 0
+toggleFactor = -1
 ;%0A  
@@ -732,64 +732,8 @@
 er =
- Math.min(400 / e.getDistance(), 3);%0A        firepower =
  3.0
@@ -1443,16 +1443,17 @@
 grees + 
+(
 adjustme
@@ -1454,16 +1454,32 @@
 justment
+ * toggleFactor)
 ));%0A    
@@ -1857,24 +1857,24 @@
 )) %3C 10)  %7B%0A
-
             
@@ -1911,24 +1911,56 @@
 firepower);%0A
+            toggleFactor *= -1;%0A
         %7D%0A  

tweak firing algoeithm to be more competitive aginst t34 and panther too