Skip to content
Snippets Groups Projects
Commit b9c34c87 authored by Martin Vítek's avatar Martin Vítek
Browse files

Some changes, it's doing something...

parent ec773b8b
No related branches found
No related tags found
No related merge requests found
......@@ -27,11 +27,10 @@ dist.dir=dist
dist.jar=${dist.dir}/MyRobocode-robots.jar
dist.javadoc.dir=${dist.dir}/javadoc
excludes=
file.reference.robocode.jar=/home/martin.vitek/robocode/libs/robocode.jar
includes=**
jar.compress=false
javac.classpath=\
${file.reference.robocode.jar}
${libs.Robocode.classpath}
# Space-separated list of extra javac options
javac.compilerargs=
javac.deprecation=false
......
......@@ -9,8 +9,10 @@
package cz.zcu.martinv.MyRobocodeRobots;
import robocode.*;
import static robocode.util.Utils.normalRelativeAngleDegrees;
import java.awt.Color;
import java.util.ArrayList;
import java.util.Random;
/**
......@@ -21,14 +23,22 @@ import java.util.ArrayList;
*/
public class BadBot extends AdvancedRobot
{
/**
* All posible states of BadBot.
*/
private enum States
{
SCANNING, APROACHING, FIGHTING, RUNNING, RAMMING, UNCOLISIONING;
SCANNING, APROACHING, FIGHTING, RUNNING, RAMMING;
}
private RobotStatus status;
private States state;
private ArrayList<EnemyRobotStatus> enemyRobots;
private int target;
private int radarTurnDirection = 1;
private int fightDirection = 1;
private double previousEnergy;
private String lastHitBy;
/**
* Main method - here is all robot logic.
......@@ -51,8 +61,6 @@ public class BadBot extends AdvancedRobot
enemyRobots = new ArrayList<EnemyRobotStatus>(2);
//Start scaning other robots
state = States.SCANNING;
while(true)
......@@ -64,20 +72,18 @@ public class BadBot extends AdvancedRobot
case APROACHING: aproaching();
break;
/*
case FIGHTING: fighting();
break;
case RUNNING: running();
break;
case RAMMING: ramming();
break;
case UNCOLISIONING: uncolisioning();
break;
*/
}
execute();
}
}
/**
......@@ -88,56 +94,53 @@ public class BadBot extends AdvancedRobot
@Override
public void onScannedRobot(ScannedRobotEvent e)
{
out.format("Found: %s%n", e.getName());
switch(state)
{
case SCANNING: scanningOnScannedRobot(e);
break;
break;
case APROACHING: aproachingOnScannedRobot(e);
break;
case APROACHING: aproachingOnScannedRobot(e);
break;
case FIGHTING: fightingOnScannedRobot(e);
break;
case RUNNING: runningOnScannedRobot(e);
case RAMMING: rammingOnScannedRobot(e);
break;
}
//scan();
}
/**
* This method is called every turn.
* It's called by RoboCode.
* @param e passed by RoboCode
*/
@Override
public void onStatus(StatusEvent e)
{
status = e.getStatus();
//checkWalls();
}
@Override
public void onBulletHit(BulletHitEvent e)
{
scan();
}
@Override
public void onHitByBullet(HitByBulletEvent e)
{
//state = States.RUNNING;
//findNewTarget();
}
@Override
public void onHitRobot(HitRobotEvent e)
{
turnGunRight(normalRelativeAngleDegrees(getHeading() - getGunHeading() + e.getBearing()));
setFire(3.0);
setAhead(-100);
setTurnRight(90.0);
}
@Override
public void onHitWall(HitWallEvent e)
{
turnRight(180.0);
ahead(250);
}
@Override
public void onRobotDeath(RobotDeathEvent e)
{
findNewTarget();
}
/**
......@@ -148,13 +151,13 @@ public class BadBot extends AdvancedRobot
@Override
public void onWin(WinEvent e)
{
out.format("I'm not so bad after all :)%n");
turnRight(360);
}
private void scanning()
{
stop();
turnRadarRight(36.0);
setTurnRadarRight(360.0);
}
private void scanningOnScannedRobot(ScannedRobotEvent e)
......@@ -163,34 +166,233 @@ public class BadBot extends AdvancedRobot
enemyRobots.add(new EnemyRobotStatus(e));
//We have all robots in current battle
//We have scaned all robots in current battle
if (enemyRobots.size() == getOthers())
{
out.format("We have scanned all (%d) enemy robots%n", enemyRobots.size());
//Select weakest weakest (od nearest) robot as target
target = getNearest();
out.format("Selected: %s%n", enemyRobots.get(target).getName());
state = States.APROACHING;
}
}
private void aproaching()
{
ahead(500.0);
scanning();
}
private void aproachingOnScannedRobot(ScannedRobotEvent e)
{
updateEnemyRobots(e);
if (enemyRobots.size() == 0) return;
EnemyRobotStatus enemy = enemyRobots.get(target);
//Turn to enemy
double turnAngle = enemy.getBearing();
turnRight(turnAngle);
turnGunRight(turnAngle);
if (enemy.getDistance() > 600.0)
{
setAhead(enemy.getDistance()-150.0);
}
else if (enemy.getDistance() < 100.0)
{
setAhead(enemy.getDistance()-200.0);
}
else
{
setTurnRight(90.0);
state = States.FIGHTING;
}
}
private void fighting()
{
scanning();
Random rand = new Random();
setAhead(rand.nextInt(150)-75);
setTurnRight((double)(rand.nextInt(30)-15));
}
private void checkWalls()
private void fightingOnScannedRobot(ScannedRobotEvent e)
{
if ( (status.getX() < 1.8*getWidth()) ||
(status.getX() > (getBattleFieldWidth()-1.8*getWidth())) ||
(status.getY() < 1.8*getHeight()) ||
(status.getY() > (getBattleFieldHeight()-1.8*getHeight())) )
updateEnemyRobots(e);
if (enemyRobots.size() == 0) return;
EnemyRobotStatus enemy = enemyRobots.get(target);
//If we have low energy, go to running mode
if (getEnergy() < 20.0)
{
state = States.RUNNING;
return;
}
//Fire
setTurnGunRight(normalRelativeAngleDegrees(getHeading() - getGunHeading() + enemy.getBearing()));
waitFor(new TurnCompleteCondition(this));
if (getGunHeat() == 0.0)
{
double firePower;
if (enemy.getVelocity() < 0.2) firePower = 3.0;
else firePower = Math.min(300.0/enemy.getDistance(), 3.0);
setBulletColorWrapper(firePower);
setFire(firePower);
}
double energyChange = previousEnergy - enemy.getEnergy();
previousEnergy = enemy.getEnergy();
//Dodge bullet
if ((energyChange > 0.0) && (energyChange <= 3.0))
{
stop();
turnRight(90.0);
ahead(40000);
ahead(100.0 * fightDirection);
fightDirection *= -1;
}
//Ram if enemy have low energy
if (enemy.getEnergy() < 15.0)
{
state = States.RAMMING;
return;
}
//Face at 90° if difference is > 45°
setTurnRight(normalRelativeAngleDegrees(enemy.getBearing()+90.0));
//Get closer or leave space
if ((enemy.getDistance() < 40.0) || (enemy.getDistance() > 700.0))
{
state = States.APROACHING;
return;
}
}
private void ramming()
{
scanning();
}
private void rammingOnScannedRobot(ScannedRobotEvent e)
{
updateEnemyRobots(e);
if (enemyRobots.size() == 0) return;
EnemyRobotStatus enemy = enemyRobots.get(target);
//Face enemy
setTurnRight(normalRelativeAngleDegrees(enemy.getBearing()));
setTurnGunRight(normalRelativeAngleDegrees(getHeading() - getGunHeading() + enemy.getBearing()));
execute();
setAhead(enemy.getDistance() + 10);
waitFor(new MoveCompleteCondition(this));
fire(3.0);
}
private void running()
{
scanning();
setTurnRight(360);
setAhead(10000);
}
private void runningOnScannedRobot(ScannedRobotEvent e)
{
updateEnemyRobots(e);
for (EnemyRobotStatus enemy : enemyRobots)
{
turnGunRight(normalRelativeAngleDegrees(getHeading() - getGunHeading() + enemy.getBearing()));
fire(2.0);
}
if (getEnergy() > 40.0)
{
findNewTarget();
state = States.SCANNING;
}
}
private void updateEnemyRobots(ScannedRobotEvent e)
{
EnemyRobotStatus robotToUpdate = new EnemyRobotStatus(e);
int index = enemyRobots.indexOf(robotToUpdate);
if (index < 0) return;
enemyRobots.set(index, robotToUpdate);
}
private void findNewTarget()
{
target = 0;
enemyRobots.clear();
state = States.SCANNING;
}
private int getWeakest()
{
if (enemyRobots.size() < 2) return 0;
int weakest = 0;
boolean allSame = false;
for(int i=1; i<enemyRobots.size(); i++)
{
if (enemyRobots.get(weakest).getEnergy() > enemyRobots.get(i).getEnergy())
{
weakest = i;
}
if (enemyRobots.get(weakest).getEnergy() != enemyRobots.get(i).getEnergy())
{
allSame = true;
}
}
if (allSame) return getNearest();
else return weakest;
}
private int getNearest()
{
int nearest = 0;
for(int i=1; i<enemyRobots.size(); i++)
{
if (enemyRobots.get(nearest).getDistance() > enemyRobots.get(i).getDistance())
{
nearest = i;
}
}
return nearest;
}
/**
* Wrapper for setBulletColor().
* Sets bullet color based on power.
* @param power bullet power in range 0.0 - 3.0
*/
private void setBulletColorWrapper(double firePower)
{
if ((firePower >= 0.0) && (firePower < 1.0)) setBulletColor(Color.WHITE);
else if ((firePower >= 1.0) && (firePower < 2.0)) setBulletColor(Color.YELLOW);
else if ((firePower >= 2.0) && (firePower <= 3.0)) setBulletColor(Color.RED);
else return;
}
}
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
* ----------------------------------------------------------------------------------
* "THE BEER-WARE LICENSE" (Revision 42):
* <martinv@students.zcu.cz> wrote this file. As long as you retain this notice you
* can do whatever you want with this stuff. If we meet some day, and you think
* this stuff is worth it, you can buy me a beer in return. Martin Vítek
* ----------------------------------------------------------------------------------
*/
package cz.zcu.martinv.MyRobocodeRobots;
......@@ -45,7 +48,7 @@ public class EnemyRobotStatus
this.name = e.getName();
this.bearing = e.getBearing();
this.distance = e.getDistance();
this.energy = e.getDistance();
this.energy = e.getEnergy();
this.heading = e.getHeading();
this.velocity = e.getVelocity();
}
......
......@@ -210,7 +210,7 @@ public class ManualBot extends RateControlRobot
* Sets bullet color based on power.
* @param power bullet power in range 0.0 - 3.0
*/
private void setBulletColorWrapper(double power)
private void setBulletColorWrapper(double firePower)
{
if ((firePower >= 0.0) && (firePower < 1.0)) setBulletColor(Color.WHITE);
else if ((firePower >= 1.0) && (firePower < 2.0)) setBulletColor(Color.YELLOW);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment