if (move === 0) {
//On the first turn, set messages[this.id] to the function I will call to move me
messages[this.id] = function(move, tJailed, eJailed, team, enemies, tFlag, eFlag, messages) {
//Arbitrary function to move to a point at some speed, which may be in the point
// If we are at the point, undefined is returned
var moveTo = function(p, max) {
if (!p) {
return {x:0, y:0};
}
max = Math.min(this.strength, max || p.max || 2);
var dx = p.x - this.x;
var dy = p.y - this.y;
var dist = Math.abs(dx)+Math.abs(dy);
if (dist === 0) {
return undefined;
} else if (dist < max) {
return {x: dx, y: dy};
}
var ux = Math.floor(max * dx / dist);
var uy = Math.floor(max * dy / dist);
while (Math.abs(ux) + Math.abs(uy) < max) {
if (ux + this.x !== p.x) {
ux += ux > 0 ? 1 : -1;
} else if (uy + this.y !== p.y) {
uy += uy > 0 ? 1 : -1;
} else {
break;
}
}
return {x: ux, y:uy};
}.bind(this);
//Set the way points
var points = [];
if (this.x > WIDTH/2) {
points.push({x: WIDTH-FIELD_PADDING, y:HEIGHT/2+5});
points.push({x: WIDTH-FIELD_PADDING, y:FIELD_PADDING, max: 5});
points.push({x: WIDTH-FIELD_PADDING, y:HEIGHT/2+25, max: 5});
} else {
points.push({x: FIELD_PADDING, y:HEIGHT/2+5});
points.push({x: FIELD_PADDING, y:FIELD_PADDING, max: 5});
points.push(undefined); //Special case to do nothing / hog the jail
}
//Move through the points
var state = messages[this.id].state || 0;
var ret;
while (!ret) {
//Special case: if we were doing nothing, make sure we're where we think we were
if (!points[state]) {
ret = moveTo(points[state-1]);
if (ret) {
state = 0;
}
}
//Move to the next point
ret = moveTo(points[state]);
if (!ret) {
state = (state + 1) % points.length;
}
}
messages[this.id].state = state;
return ret;
};
}
//Move me based on that function, which may be changed by my allies
return messages[this.id].call(this, move, tJailed, eJailed, team, enemies, tFlag, eFlag, messages);
if (move === 0) {
//On the first turn, set messages[this.id] to the function I will call to move me
messages[this.id] = function(move, tJailed, eJailed, team, enemies, tFlag, eFlag, messages) {
//Arbitrary function to move to a point at some speed, which may be in the point
// If we are at the point, undefined is returned
var moveTo = function(p, max) {
if (!p) {
return {x:0, y:0};
}
max = Math.min(this.strength, max || p.max || 2);
var dx = p.x - this.x;
var dy = p.y - this.y;
var dist = Math.abs(dx)+Math.abs(dy);
if (dist === 0) {
return undefined;
} else if (dist < max) {
return {x: dx, y: dy};
}
var ux = Math.floor(max * dx / dist);
var uy = Math.floor(max * dy / dist);
while (Math.abs(ux) + Math.abs(uy) < max) {
if (ux + this.x !== p.x) {
ux += ux > 0 ? 1 : -1;
} else if (uy + this.y !== p.y) {
uy += uy > 0 ? 1 : -1;
} else {
break;
}
}
return {x: ux, y:uy};
}.bind(this);
//Set the way points
var points = [];
if (this.x > WIDTH/2) {
points.push({x: WIDTH-FIELD_PADDING, y:HEIGHT/2+5});
points.push({x: WIDTH-FIELD_PADDING, y:FIELD_PADDING, max: 5});
points.push({x: WIDTH-FIELD_PADDING, y:HEIGHT/2+25, max: 5});
} else {
points.push({x: FIELD_PADDING, y:HEIGHT/2+5});
points.push({x: FIELD_PADDING, y:FIELD_PADDING, max: 5});
points.push(undefined); //Special case to do nothing / hog the jail
}
//Move through the points
var state = messages[this.id].state || 0;
var ret;
while (!ret) {
//Special case: if we were doing nothing, make sure we're where we think we were
if (!points[state]) {
ret = moveTo(points[state-1]);
if (ret) {
state = 0;
}
}
//Move to the next point
ret = moveTo(points[state]);
if (!ret) {
state = (state + 1) % points.length;
}
}
messages[this.id].state = state;
return ret;
};
}
//Move me based on that function, which may be changed by my allies
return messages[this.id].call(this, move, tJailed, eJailed, team, enemies, tFlag, eFlag, messages);