Skip to content

Instantly share code, notes, and snippets.

@jiaaro
Last active December 16, 2015 06:39
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jiaaro/5392862 to your computer and use it in GitHub Desktop.
Save jiaaro/5392862 to your computer and use it in GitHub Desktop.
Partially de-obfuscated simpleTarget() from nodewar.com
// nodewar.com library function…
// o.lib.targeting.simpleTarget(ship, pos)
//
// mostly deobfuscated/deminified
//
function simpleTarget(ship, pos) {
var i, r, h,
torque = 0,
thrust = 0,
dir = o.lib.targeting.dir(ship, pos);
if (Math.abs(dir) < Math.PI/8) {
thrust = 1;
}
else if (Math.abs(dir) < Math.PI/4) {
thrust = .5;
}
// ship is not turning
if (ship.a_vel === 0)
torque = (dir < 0) ? -1 : 1;
// ship is turning right, but target is left
else if (dir < 0 && ship.a_vel > 0)
torque = -1;
// ship is turning left, but target is right
else if (dir > 0 && ship.a_vel < 0)
torque = 1;
// we're turning the right way. just fine tuning...
else {
i = o.RULES.TORQUE_RANGE[1] / ship.m_i;
h = Math.abs(ship.a_vel) / i;
r = Math.abs(ship.a_vel) * h + .5 * -i * h * h;
if (Math.abs(dir) > r) {
torque = (dir > 0) ? 1 : -1;
}
else {
torque = (dir > 0) ? -1 : 1;
}
}
return {thrust: thrust, torque: torque, label: null};
}
@malgorithms
Copy link

sure, I can explain: this isn't the optimal way to target something. But here's what it does:

the thrust code

  1. if we're pointing within pi/8 radians (it's within a 45 degree field of view) apply full thrust
  2. otherwiuse, if within a 90 degree field of view, apply half thrust
  3. otherwise, we're basically pointed the wrong way, so no thrust.

the torque stuff, more complicated

  1. if the ship is not turning, apply a torque towards the target.

  2. if it is turning, but turning in the wrong direction, apply a counter torque.

  3. otherwise, if we're turning the right way, we need to decide whether to start slowing down our spin so we dont overshoot. So:

    a. figure out how long many more radians we'd spin if we tried to stop spinning by applying a counter-torque.
    b. compare this to the actual remaining radians needed to spin, and either torque towards target or away

for 3a and 3b, try to think of it as accelerating towards a finish line and slamming the brakes on at the right distance. only with spinning. The math is the same, and you might recognize the old physics formula: x = x0 + v0t + 1/2at^2, only applied to linear velocity, angular acceleration, and rotation.

Hope this helps. there may be bugs of course. And I call it simpleTarget because it isn't sensitive to a bunch of other factors, most notably its own velocity. If a ship is going fast and you try to simpleTarget something it might miss or even orbit the target.

-cc

@malgorithms
Copy link

for what it's worth, I expect a way to win Nodewar would be to stop using my simpleTarget and to write a better one.

@zerowidth
Copy link

I think the real improvements are to be gained with changing how the thrust works. The rotation code, as far as I could figure, was just about ideal: at the timescales involved (t=0.005 in x = x0 + vt + 1/2at^2) and taking the moment of inertia into account, the torque should always be maxed out in either direction.

It occurs to me that this may not hold with smaller fragments, though… hmm!

As a side note, I'm amused at how many (all?) of the leaders are based on the open-source bots, just with slightly different weightings.

@zerowidth
Copy link

There's also surely some advantage to be gained by adjusting weighting according to the moon field size, as well as taking gravity into account. More accurate navigation would quite likely trip up any of the existing contenders.

@jiaaro
Copy link
Author

jiaaro commented Apr 17, 2013

Thanks @malgorithms

@aniero yeah, that's why I'm trying to understand simpleTarget =D

my first goal is to change the targeting from the current "skate toward the puck" to "skate to where the puck is going". And use that when attacking.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment