超超机器人

来源:互联网 发布:三维立体图制作软件 编辑:程序博客网 时间:2024/04/28 04:58
package com.water;
import java.awt.Color;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;


import robocode.AdvancedRobot;
import robocode.HitByBulletEvent;
import robocode.RobotDeathEvent;
import robocode.ScannedRobotEvent;


public class MyRobot extends AdvancedRobot {
static Point2D myLocation, last;
static EnemyInfo aim;// 敌人的信息
static HashMap<String, EnemyInfo> enemies;
static HashMap<Object, Object> stats = new HashMap<Object, Object>();// 统计敌人的信息


@Override
public void run() {// 重写run方法
// 设置机器人
setColors(Color.BLUE, Color.BLUE, Color.BLUE);
setAdjustGunForRobotTurn(true);
setAdjustRadarForGunTurn(true);
// 雷达疯狂的旋转
setTurnRadarRight(Double.POSITIVE_INFINITY);// Double型正无穷大的常量
// 初始化数据
enemies = new HashMap<String, EnemyInfo>();
Point2D next = aim = null;
// 死循环
while (true) {
myLocation = new Point2D.Double(getX(), getY());// Double类以double精度定义指定点。
if (aim != null) {
if (next == null) {
next = last = myLocation;
}
if (next.distance(myLocation) < 15 || getOthers() > 1) {
boolean changed = false;
double angle = 0;
double moveDist = myLocation.distance(aim);
moveDist = (getOthers() == 1 ? Math.random() * moveDist
: Math.max(500, moveDist)) * 0.5;
do {
Point2D p;
p = projectPoint(myLocation, angle, moveDist);// 调用计算发射位置方法
if (inField(p) && findRisk(p) < findRisk(next)) {
changed = true;
next = p;
}
angle += 0.1;
} while (angle < Math.PI * 2);
if (changed) {
last = myLocation;
}
}
double a1, a2;
a2 = robocode.util.Utils.normalRelativeAngle(angle(next,
myLocation) - getHeadingRadians());
a1 = Math.atan(Math.tan(a2));
setTurnRightRadians(a1);
setAhead(Math.abs(getTurnRemainingRadians()) > 1 ? 0
: (a1 == a2 ? 1.0 : -0.1) * next.distance(myLocation));
}
execute();
}
}


/**
* 发现风险
* **/
private double findRisk(Point2D point) {
double risk = 0;
Collection<EnemyInfo> enemySet = enemies.values();
Iterator<EnemyInfo> it = enemySet.iterator();
do {
EnemyInfo e = (EnemyInfo) it.next();
double thisrisk = (e.energy + 50) / point.distance(e);
int closer = 0;
Iterator<EnemyInfo> it2 = enemySet.iterator();
do {
EnemyInfo e2 = (EnemyInfo) it2.next();
if (e.distance(e2) * 0.9 > e.distance(point)) {
closer++;
}
} while (it2.hasNext());
if (closer <= 1 || e.lastHit > getTime() - 200 || e == aim) {
thisrisk *= 2 + 2
* Math.abs(Math.cos(angle(myLocation, point)))
- angle(e, myLocation);
}
risk += thisrisk;
} while (it.hasNext());
if (getOthers() > 1) {
risk += Math.random() / last.distanceSq(point);
}
risk += Math.random() / 5 / last.distanceSq(point);
risk += Math.random() / 5 / myLocation.distanceSq(point);
return risk;
}


/**

* 雷达锁定最近的敌人和能量最小的敌人

* **/
public void onScannedRobot(ScannedRobotEvent e) {
String name = e.getName();
EnemyInfo enemy = (EnemyInfo) enemies.get(name);
if (enemy == null) {
enemies.put(name, enemy = new EnemyInfo());
}
double distance, absBearing;
Point2D loc, myLocation;
myLocation = new Point2D.Double(getX(), getY());
absBearing = getHeadingRadians() + e.getBearingRadians();
distance = e.getDistance();
loc = projectPoint(myLocation, absBearing, distance);
enemy.setLocation(loc);
if (aim == null || distance < myLocation.distance(aim)) {
aim = enemy;
}
double power;// 火力
double energy;// 能量
double a = Math.min(getEnergy(), energy = enemy.energy = e.getEnergy());
power = Math.min(Math.min(3, 1200 / distance), a / 4) - 0.6;
if (enemy == aim) {
if (getOthers() == 1 || getGunHeat() < 0.8) {
setTurnRadarLeft(getRadarTurnRemaining());
}
setTurnGunRightRadians(robocode.util.Utils
.normalRelativeAngle(absBearing - getGunHeadingRadians()));
if (Math.max(Math.abs(getGunTurnRemaining()), energy / getEnergy()) < 5) {
setFire(power);
}
}
}


/**
* 如果zai Hashmap中找不到击中我的敌人就将他保存下来

* **/
public void onHitByBullet(HitByBulletEvent e) {
EnemyInfo enemy;
if ((enemy = (EnemyInfo) enemies.get(e.getName())) != null) {
enemy.lastHit = getTime();
}
}


/**
* 敌人死亡 移出


* // *
**/
public void onRobotDeath(RobotDeathEvent e) {
if (enemies.remove(e.getName()) == aim) {
aim = null;
}
}


/**
* 发射的位置 开始位置 敌人的相对角度 距离
* **/
public static Point2D projectPoint(Point2D startPoint, double theta,
double dist) {
Point2D.Double point = new Point2D.Double(startPoint.getX() + dist
* Math.sin(theta), startPoint.getY() + dist * Math.cos(theta));
return point;
}


/**
* 计算角度

* **/
public static double angle(Point2D point2, Point2D point1) {
return Math.atan2(point2.getX() - point1.getX(),
point2.getY() - point1.getY());
}


/**
* 计算战场
* **/
public boolean inField(Point2D p) {
return new Rectangle2D.Double(30, 30, getBattleFieldWidth() - 60,
getBattleFieldHeight() - 60).contains(p);
}
/**
* 敌人内部类


* **/
class EnemyInfo extends Point2D.Double{
/**

*/
private static final long serialVersionUID = 1L;
long lastHit;//上一次被谁打
double energy;//能量
}
}
0 0
原创粉丝点击