Difference between revisions of "Quantum/Source (Java)"
< Quantum
Jump to navigation
Jump to search
m (Fix syntax highlighting) |
(Update to v1.0.0) |
||
(2 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
+ | The following code is functionally equivalent to [[Quantum/Source (Assembly)]] but when compiled with OpenJDK 17 it will be 265 bytes. | ||
+ | |||
<syntaxhighlight lang="java"> | <syntaxhighlight lang="java"> | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
package d414.nano; | package d414.nano; | ||
Line 61: | Line 13: | ||
// Tuning Knobs | // Tuning Knobs | ||
// --------------------------------------------------------------------------------------------- | // --------------------------------------------------------------------------------------------- | ||
− | static final double WALL_FORCE = 1; | + | static final double WALL_FORCE = 1.0; |
static final double AGRAV_DECAY = 0.9; | static final double AGRAV_DECAY = 0.9; | ||
− | static final double AHEAD_AMOUNT = 120; | + | static final double AHEAD_AMOUNT = 120.0; |
− | static final double AIM_START = 10; | + | static final double AIM_START = 10.0; |
static final double AIM_FACTOR = 1.008; | static final double AIM_FACTOR = 1.008; | ||
− | static final double RADAR_LOCK_THRESHOLD = 1; | + | static final double AIM_TOLERANCE = 18.0; |
− | static final | + | static final double BULLET_POWER = 2.49999; |
+ | static final double ENERGY_FACTOR = 7.0; | ||
+ | static final double RADAR_LOCK_THRESHOLD = 1.0; | ||
+ | |||
+ | // --------------------------------------------------------------------------------------------- | ||
+ | // Constants | ||
+ | // --------------------------------------------------------------------------------------------- | ||
+ | static final double BATTLEFIELD_SIZE = 1000.0; | ||
// --------------------------------------------------------------------------------------------- | // --------------------------------------------------------------------------------------------- | ||
Line 80: | Line 39: | ||
@Override | @Override | ||
public void run() { | public void run() { | ||
− | setAllColors(Color.cyan); | + | //setAllColors(Color.cyan); |
− | + | setTurnRadarRight(targetDistance = Double.POSITIVE_INFINITY); | |
− | |||
} | } | ||
− | |||
@Override | @Override | ||
− | public void onRobotDeath(RobotDeathEvent | + | public void onRobotDeath(RobotDeathEvent r1e) { |
− | + | targetDistance = Double.POSITIVE_INFINITY; | |
} | } | ||
@Override | @Override | ||
− | public void onScannedRobot(ScannedRobotEvent | + | public void onScannedRobot(ScannedRobotEvent r1e) { |
− | double | + | double r1d; |
− | double | + | double r3d; |
− | |||
− | + | setAdjustGunForRobotTurn(true); | |
− | + | setTurnLeftRadians(Utils.normalRelativeAngle( | |
− | + | (r3d = getHeadingRadians()) | |
− | + | - Math.atan2( | |
− | |||
− | Math.atan2( | ||
(xForce = xForce * AGRAV_DECAY | (xForce = xForce * AGRAV_DECAY | ||
− | - Math.sin( | + | - Math.sin(r3d = (r3d += r1e.getBearingRadians()) |
− | + | + (Math.sin(r1e.getHeadingRadians() - r3d) | |
− | + | * r1e.getVelocity() | |
− | / (AIM_START + Math.pow(AIM_FACTOR, | + | / (AIM_START + Math.pow(AIM_FACTOR, r1d = r1e.getDistance())) |
) | ) | ||
− | + | ) / r1d | |
− | ) / | ||
) | ) | ||
+ calcWallForce(getX()) | + calcWallForce(getX()) | ||
− | |||
, | , | ||
− | (yForce = yForce * AGRAV_DECAY - Math.cos( | + | (yForce = yForce * AGRAV_DECAY - Math.cos(r3d) / r1d) |
+ calcWallForce(getY()) | + calcWallForce(getY()) | ||
− | |||
) | ) | ||
− | |||
)); | )); | ||
− | + | setBack(Math.abs(getTurnRemaining()) - AHEAD_AMOUNT); | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | + | if (r1e.getName() == targetName || r1d < targetDistance) { | |
− | + | targetName = r1e.getName(); | |
− | + | targetDistance = r1d; | |
− | + | setTurnRadarRight((getGunHeat() - RADAR_LOCK_THRESHOLD) * getRadarTurnRemaining()); | |
− | + | setTurnGunLeftRadians(Utils.normalRelativeAngle(getGunHeadingRadians() - r3d)); | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | + | if (Math.abs(getGunTurnRemainingRadians()) < (AIM_TOLERANCE / r1d)) { | |
+ | setFire(Math.min(getEnergy() / ENERGY_FACTOR, BULLET_POWER)); | ||
+ | } | ||
} | } | ||
} | } | ||
− | + | public static double calcWallForce(double r0d) { | |
− | public static double calcWallForce(double | + | return (WALL_FORCE / r0d) - (WALL_FORCE / (BATTLEFIELD_SIZE - r0d)); |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | return (WALL_FORCE / | ||
} | } | ||
− | |||
} | } | ||
</syntaxhighlight> | </syntaxhighlight> |
Latest revision as of 03:28, 30 May 2024
The following code is functionally equivalent to Quantum/Source (Assembly) but when compiled with OpenJDK 17 it will be 265 bytes.
package d414.nano;
import robocode.*;
import robocode.util.Utils;
import java.awt.Color;
public class Quantum extends AdvancedRobot {
// ---------------------------------------------------------------------------------------------
// Tuning Knobs
// ---------------------------------------------------------------------------------------------
static final double WALL_FORCE = 1.0;
static final double AGRAV_DECAY = 0.9;
static final double AHEAD_AMOUNT = 120.0;
static final double AIM_START = 10.0;
static final double AIM_FACTOR = 1.008;
static final double AIM_TOLERANCE = 18.0;
static final double BULLET_POWER = 2.49999;
static final double ENERGY_FACTOR = 7.0;
static final double RADAR_LOCK_THRESHOLD = 1.0;
// ---------------------------------------------------------------------------------------------
// Constants
// ---------------------------------------------------------------------------------------------
static final double BATTLEFIELD_SIZE = 1000.0;
// ---------------------------------------------------------------------------------------------
// Globals
// ---------------------------------------------------------------------------------------------
static String targetName;
static double targetDistance;
static double xForce;
static double yForce;
@Override
public void run() {
//setAllColors(Color.cyan);
setTurnRadarRight(targetDistance = Double.POSITIVE_INFINITY);
}
@Override
public void onRobotDeath(RobotDeathEvent r1e) {
targetDistance = Double.POSITIVE_INFINITY;
}
@Override
public void onScannedRobot(ScannedRobotEvent r1e) {
double r1d;
double r3d;
setAdjustGunForRobotTurn(true);
setTurnLeftRadians(Utils.normalRelativeAngle(
(r3d = getHeadingRadians())
- Math.atan2(
(xForce = xForce * AGRAV_DECAY
- Math.sin(r3d = (r3d += r1e.getBearingRadians())
+ (Math.sin(r1e.getHeadingRadians() - r3d)
* r1e.getVelocity()
/ (AIM_START + Math.pow(AIM_FACTOR, r1d = r1e.getDistance()))
)
) / r1d
)
+ calcWallForce(getX())
,
(yForce = yForce * AGRAV_DECAY - Math.cos(r3d) / r1d)
+ calcWallForce(getY())
)
));
setBack(Math.abs(getTurnRemaining()) - AHEAD_AMOUNT);
if (r1e.getName() == targetName || r1d < targetDistance) {
targetName = r1e.getName();
targetDistance = r1d;
setTurnRadarRight((getGunHeat() - RADAR_LOCK_THRESHOLD) * getRadarTurnRemaining());
setTurnGunLeftRadians(Utils.normalRelativeAngle(getGunHeadingRadians() - r3d));
if (Math.abs(getGunTurnRemainingRadians()) < (AIM_TOLERANCE / r1d)) {
setFire(Math.min(getEnergy() / ENERGY_FACTOR, BULLET_POWER));
}
}
}
public static double calcWallForce(double r0d) {
return (WALL_FORCE / r0d) - (WALL_FORCE / (BATTLEFIELD_SIZE - r0d));
}
}