1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
| public static double[] moveShipInCombat(Ship ship) { double destX = ship.getTarget().getX(); double destY = ship.getTarget().getY();
double directTheta = Math.atan2( destY - ship.getY(), destX - ship.getX()); double distanceSqrd = Point2D.distanceSq(destX, destY, ship.getX(), ship.getY()); if (Rimscape.DEBUG && Rimscape.gameInProgress && MainScreen.isScreenTarget(ship)) System.out.println("moving in combat, distance = " + Math.sqrt(distanceSqrd)); double dx = ship.getDx(), dy = ship.getDy(); double velocityTheta = Math.atan2(dy, dx); double offset = circleMod(velocityTheta - directTheta); if (distanceSqrd < Ship.RANGE_SQRD/9) { ship.accel(); } else if (distanceSqrd < Ship.RANGE_SQRD/6) { ship.reverse(); } else if (distanceSqrd < Ship.RANGE_SQRD/4) { } else if (distanceSqrd < Ship.RANGE_SQRD/2) { ship.decel(); } else { double accel = ship.getStatMax(Ship.STAT_ACCELERATION); double vSqrd = dx*dx + dy*dy; double stuffUnderRadical = 0; if (Math.abs(offset) < THETA_TOLERANCE/2 && (stuffUnderRadical = vSqrd - 2*accel*Math.sqrt(distanceSqrd)) < 0 || (vSqrd < accel || (2*Math.sqrt(vSqrd) - Math.sqrt(stuffUnderRadical)) < accel*accel)) { if (Rimscape.DEBUG && Rimscape.gameInProgress && MainScreen.isScreenTarget(ship)) System.out.println("speeding up to target"); if (vSqrd < REALLY_FAST*REALLY_FAST*2) ship.accel(); } else { if (Rimscape.DEBUG && Rimscape.gameInProgress && MainScreen.isScreenTarget(ship)) System.out.println("slowing down"); ship.decel(); } } double desiredTheta; if (distanceSqrd < Ship.RANGE_SQRD) desiredTheta = directTheta; else desiredTheta = directTheta - (Math.abs(offset) < THETA_TOLERANCE ? offset : 0); double thetaToGo = circleMod(desiredTheta - ship.getTheta()); if (thetaToGo < -ship.getStat(Ship.STAT_ROTATION)) ship.turnLeft(); else if (thetaToGo > ship.getStat(Ship.STAT_ROTATION)) ship.turnRight(); else ship.setTheta(desiredTheta); return new double[] {circleMod(directTheta - ship.getTheta()), distanceSqrd}; } |
There she is

Hopefully the comments help to illustrate what's going on. I felt this might be appropriate for the Shared Code forum, but since I'm interested in feedback and suggestions and it's also directly related to AI, I felt this forum was better.
Let me know what you guys think! If you wanna see it in action, start up Rimscape and piss someone off
