Skip to content

Instantly share code, notes, and snippets.

@franciscolourenco
Forked from pachacamac/galcon_hacking.html
Last active May 29, 2016 14:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save franciscolourenco/3908979b2f28968b692108af3b03a087 to your computer and use it in GitHub Desktop.
Save franciscolourenco/3908979b2f28968b692108af3b03a087 to your computer and use it in GitHub Desktop.
galcon hacking
<style>*{margin:0;padding:0;box-sizing:border-box;}canvas{width:100%;height:100%}</style>
<canvas id='C'>
<script>
var c = C.getContext("2d"), R = Math.random;
C.width = window.innerWidth; C.height = window.innerHeight;
function dist(a,b,x,y){return Math.sqrt(Math.pow(a-x,2)+Math.pow(b-y,2))}
function generatePlanets(n){
var i, s = [50,100,150,200], m = s.slice(-1)[0];
function randPlanet(){return [R()*(C.width-m*2)+m,R()*(C.height-m*2)+m,s[Math.floor(R()*s.length)]]}
function isGoodPlanet(q,p){for(i=0;i<p.length;i++){if(dist(q[0],q[1],p[i][0],p[i][1])<(q[2]+p[i][2])){return false}}return true}
function newPlanet(p){var q;while(!isGoodPlanet(q=randPlanet(),p)){q=randPlanet()}return q}
var p=[];for(i=0;i<n;i++){p.push(newPlanet(p))};return p;
}
function attack(p,q,planets){
function animatePathDrawing(c,x0,y0,x1,y1,x2,y2,d) {
var start = null;
var step = function animatePathDrawingStep(ts) {
if(start===null) start=ts;
var delta = ts - start, progress = Math.min(delta / d, 1);
//c.clearRect(0, 0, c.canvas.width, c.canvas.height);
drawBezierSplit(c, x0, y0, x1, y1, x2, y2, 0, progress);
if(progress<1) window.requestAnimationFrame(step)
};
window.requestAnimationFrame(step);
}
function drawBezierSplit(c,x0,y0,x1,y1,x2,y2,t0,t1){
function li(i,j,t){return (1.0-t)*i+t*j} /*Linearly interpolate between two numbers i, j by t*/
c.beginPath();
if(0.0==t0&&t1==1.0){c.moveTo(x0,y0);c.quadraticCurveTo(x1,y1,x2,y2)}
else if(t0!=t1){
var t00=t0*t0,t01=1.0-t0,t02=t01*t01,t03=2.0*t0*t01;
var nx0=t02*x0+t03*x1+t00*x2,ny0=t02*y0+t03*y1+t00*y2;
t00=t1*t1;t01=1.0-t1;t02=t01*t01;t03=2.0*t1*t01;
var nx2=t02*x0+t03*x1+t00*x2,ny2=t02*y0+t03*y1+t00*y2;
var nx1=li(li(x0,x1,t0),li(x1,x2,t0),t1),ny1=li(li(y0,y1,t0),li(y1,y2,t0),t1);
c.moveTo(nx0,ny0);
c.quadraticCurveTo(nx1,ny1,nx2,ny2);
}
c.stroke();
c.closePath();
}
// function nearestPlanet(x,y){
// c.rect(x-10,y-10,20,20);c.stroke();
// var i,j,d=1/0,t;
// for(i=0;i<planets.length;i++)if((t=dist(planets[i][0],planets[i][1],x,y)) < d){d=t;j=i}
// c.rect(planets[j][0]-10,planets[j][1]-10,20,20);c.stroke();
// return planets[j];
// }
c.shadowBlur = 10;
c.shadowColor = c.strokeStyle = 'rgba(255,0,0,0.75)';
c.lineWidth = 1;
o = [(p[0]+q[0])/2 + R()*400-200, (p[1]+q[1])/2 + R()*400-200];//nearestPlanet((p[0]+q[0])/2,(p[1]+q[1])/2);
animatePathDrawing(c, p[0], p[1], o[0], o[1], q[0], q[1], dist(p[0],p[1],q[0],q[1])*3 );
}
var planets = generatePlanets(12), p,q;
for(var i=0;i<planets.length;i++){
c.beginPath();
c.arc(planets[i][0], planets[i][1], planets[i][2]/2, 0, 2 * Math.PI, false);
c.fillStyle = 'green';
c.strokeStyle = '#000';
c.fill();
c.stroke();
}
C.addEventListener('click',function(e){
function inPlanet(p,x,y){return dist(p[0],p[1],x,y)<p[2]}
var x=e.pageX,y=e.pageY,hit;
for(var i=0;i<planets.length;i++)if(inPlanet(planets[i],x,y)){hit=planets[i];break}
if(hit){
if(p){
q = hit;
attack(p,q,planets);
p=q=null;
}else{
p = hit;
}
console.log(hit);
}else{
p=q=null;
console.log(x,y);
}
},false);
</script>
<style>*{margin:0;padding:0;box-sizing:border-box;}canvas{width:100%;height:100%}</style>
<canvas id='C'>
<script>
var c = C.getContext("2d"), R = Math.random;
C.width = window.innerWidth; C.height = window.innerHeight;
function dist(a,b,x,y){return Math.sqrt(Math.pow(a-x,2)+Math.pow(b-y,2))}
function inPlanet(p,x,y){return dist(p[0],p[1],x,y)<p[2]}
//function inAnyPlanet(x,y,p){for(var i=0;i<p.length;i++)if(inPlanet(p[i],x,y))return i; return -1;}
function generatePlanets(n){
var i, s = [30,60,90,120], m = s.slice(-1)[0];
function randPlanet(){return [R()*(C.width-m*2)+m,R()*(C.height-m*2)+m,s[Math.floor(R()*s.length)]]}
function isGoodPlanet(q,p){for(i=0;i<p.length;i++){if(dist(q[0],q[1],p[i][0],p[i][1])<((q[2]+p[i][2])*1.5)){return false}}return true}
function newPlanet(p){var q;while(!isGoodPlanet(q=randPlanet(),p)){q=randPlanet()}return q}
var p=[];for(i=0;i<n;i++){p.push(newPlanet(p))};return p;
}
function attack(p,q,planets){
function animatePathDrawing(c,x0,y0,x1,y1,x2,y2,d) {
var start = null;
var step = function animatePathDrawingStep(ts) {
if(start===null) start=ts;
var delta = ts - start, progress = Math.min(delta / d, 1);
//c.clearRect(0, 0, c.canvas.width, c.canvas.height);
drawBezierSplit(c, x0, y0, x1, y1, x2, y2, 0, progress);
if(progress<1) window.requestAnimationFrame(step)
};
window.requestAnimationFrame(step);
}
function drawBezierSplit(c,x0,y0,x1,y1,x2,y2,t0,t1){
function li(i,j,t){return (1.0-t)*i+t*j} /*Linearly interpolate between two numbers i, j by t*/
c.beginPath();
if(0.0==t0&&t1==1.0){c.moveTo(x0,y0);c.quadraticCurveTo(x1,y1,x2,y2)}
else if(t0!=t1){
var t00=t0*t0,t01=1.0-t0,t02=t01*t01,t03=2.0*t0*t01,nx0=t02*x0+t03*x1+t00*x2,ny0=t02*y0+t03*y1+t00*y2;
t00=t1*t1;t01=1.0-t1;t02=t01*t01;t03=2.0*t1*t01;
var nx2=t02*x0+t03*x1+t00*x2,ny2=t02*y0+t03*y1+t00*y2,nx1=li(li(x0,x1,t0),li(x1,x2,t0),t1),ny1=li(li(y0,y1,t0),li(y1,y2,t0),t1);
c.moveTo(nx0,ny0);
c.quadraticCurveTo(nx1,ny1,nx2,ny2);
}
c.stroke();
c.closePath();
}
// function nearestPlanet(x,y){
// c.rect(x-10,y-10,20,20);c.stroke();
// var i,j,d=1/0,t;
// for(i=0;i<planets.length;i++)if((t=dist(planets[i][0],planets[i][1],x,y)) < d){d=t;j=i}
// c.rect(planets[j][0]-10,planets[j][1]-10,20,20);c.stroke();
// return planets[j];
// }
c.save();
c.shadowBlur = 5;
c.shadowColor = c.strokeStyle = 'rgba(255,0,0,0.5)';
c.lineWidth = 1;
for(var i=0;i<10;i++){
o = [(p[0]+q[0])/2 + R()*300-150, (p[1]+q[1])/2 + R()*300-150];//nearestPlanet((p[0]+q[0])/2,(p[1]+q[1])/2);
drawBezierSplit(c, p[0], p[1], o[0], o[1], q[0], q[1], 0, 0.5);
//animatePathDrawing(c, p[0], p[1], o[0], o[1], q[0], q[1], dist(p[0],p[1],q[0],q[1])*3 );
}
c.restore();
}
C.addEventListener('click',function(e){
var x=e.pageX,y=e.pageY,hit;
for(var i=0;i<planets.length;i++)if(inPlanet(planets[i],x,y)){hit=planets[i];break}
if(hit){
if(p){
q = hit;
//attack(p,q,planets);
for(var j=0;j<p[2]*0.25;j++)ships.push([
p[0]+R()*p[2]*2-p[2],
p[1]+R()*p[2]*2-p[2],
q,
R()*0.4-0.2,
R()*0.4-0.2
]);
p=q=null;
}else{
p = hit;
}
console.log(hit);
}else{
p=q=null;
console.log(x,y);
}
},false);
var ships = [];
var planets = generatePlanets(12), p,q;
function gameLoop() {
c.clearRect(0, 0, C.width, C.height);
//draw planets
c.save();
for(var i=0;i<planets.length;i++){
c.beginPath();
c.arc(planets[i][0], planets[i][1], planets[i][2], 0, 2 * Math.PI, false);
c.fillStyle = 'green';
c.strokeStyle = '#000';
c.fill();
c.stroke();
}
c.restore();
//handle ships
c.save();
c.fillStyle = 'blue';
for(var i=0;i<ships.length;i++){
var x1 = ships[i][0], y1 = ships[i][1], x2 = ships[i][2][0], y2 = ships[i][2][1], d = dist(x1,y1,x2,y2), dx = (x2-x1)/d, dy = (y2-y1)/d;
ships[i][0] += dx*3+ships[i][3];
ships[i][1] += dy*3+ships[i][4];
c.beginPath();
c.arc(ships[i][0],ships[i][1],3,0,2*Math.PI, false);
c.fill();
c.stroke();
if(inPlanet(ships[i][2], ships[i][0], ships[i][1])) ships.splice(i, 1);
}
c.restore();
window.requestAnimationFrame(gameLoop);
}
gameLoop();
// function fadeOut() {
// c.fillStyle = "rgba(255,255,255,0.3)";
// c.fillRect(0, 0, C.width, C.height);
// setTimeout(fadeOut,100);
// }
// fadeOut();
</script>
<style>*{margin:0;padding:0;box-sizing:border-box;}canvas{width:100%;height:100%}</style>
<canvas id='C' style='background:#000;'>
<script>
var c=C.getContext("2d"),R=Math.random,tick=Date.now(),p,q,i,j,ships=[],planets=[],sfx=[];
C.width = window.innerWidth; C.height = window.innerHeight;
c.font = '18px Helvetica'; c.textAlign = 'center'; c.textBaseline = 'middle';
function synth(f,d){
for(var t=0,s='RIFF_oO_WAVEfmt '+atob('EAAAAAEAAQBAHwAAQB8AAAEACAA')+'data';++t<d;)s+=String.fromCharCode(f(t));
return new Audio('data:audio/wav;base64,'+btoa(s));
}
function s_gameOver(t){return (Math.sin(t/(t>>9))*10)&255}
function dist(x1,y1,x2,y2){return Math.sqrt(Math.pow(x1-x2,2)+Math.pow(y1-y2,2))}
function inPlanet(x,y,p){return dist(p.x,p.y,x,y)<p.r}
function generatePlanets(n){
var i, s = [30,60,90,120], m = s.slice(-1)[0];
function randPlanet(){
var x = R()*(C.width-m*2)+m, y = R()*(C.height-m*2)+m, p = x<C.width*0.5 && y<C.height*0.5 ? 1 : x>C.width*0.5 && y>C.height*0.5 ? 2 : 0;
return {x: x, y: y, r: s[Math.floor(R()*s.length)], e: 10, p: p}
}
function isGoodPlanet(q,p){for(i=0;i<p.length;i++){if(dist(q.x,q.y,p[i].x,p[i].y)<((q.r+p[i].r)*1.5)){return false}}return true}
function newPlanet(p,q){while(!isGoodPlanet(q=randPlanet(),p)){q=randPlanet()}return q}
var p=[];for(i=0;i<n;i++){p.push(newPlanet(p))};return p;
}
function clickHandler(e){
e.preventDefault();
var x=e.changedTouches ? e.changedTouches[0].pageX : e.pageX,
y=e.changedTouches ? e.changedTouches[0].pageY : e.pageY,
hit;
for(var i=0;i<planets.length;i++)if(inPlanet(x,y,planets[i])){hit=planets[i];break}
if(hit){
if(p){
q = hit;
if(p.p!=0) attack(p,q);
p=q=null;
}else{
p = hit;
}
}else{
p=q=null;
}
}
function attack(p,q){
for(var i=0,n=Math.floor(p.e*0.5);i<Math.min(n,p.e);i++){ /*create ships based on planets energy*/
ships.push({from: p, to: q, p: p.p, x: p.x+R()*p.r-p.r/2, y: p.y+R()*p.r-p.r/2, rdx: R()*0.8-0.4, rdy: R()*0.8-0.4}); /*create ship*/
p.e--; /*take energy from planet*/
}
}
function shipCollision(s){
if(s.from==s.to)return s.to;
for(var i=0;i<planets.length;i++)if(planets[i]!=s.from&&(planets[i].p!=s.p||planets[i]==s.to)&&inPlanet(s.x,s.y,planets[i]))return planets[i];
return null;
}
function gameLoop() {
var tickAction=false,pc;
if(Date.now()-tick>2000){ tick = Date.now(); tickAction = true; }
c.clearRect(0, 0, C.width, C.height);
/* handle planets */
for(var i=0;i<planets.length;i++){
c.beginPath();
c.arc(planets[i].x, planets[i].y, planets[i].r, 0, 2 * Math.PI, false);
c.fillStyle = planets[i].p == 0 ? '#999' : planets[i].p == 1 ? '#900' : '#090';
c.fill();
c.fillStyle = 'rgba(255,255,255,0.8)';
c.fillText(planets[i].e, planets[i].x, planets[i].y);
if(tickAction && planets[i].p != 0){
if(planets[i].e < planets[i].r) planets[i].e=Math.floor(planets[i].e + planets[i].r/30); else planets[i].e--;
}
}
if(tickAction){
if(ships.length == 0){
if(!planets.find(function(p){return p.p==2})){ alert("Red wins"); newGame() }
else if(!planets.find(function(p){return p.p==1})){ alert("Green wins"); newGame() }
}
}
/* handle ships */
for(var i=0;i<ships.length;i++){
var x1 = ships[i].x, y1 = ships[i].y, x2 = ships[i].to.x, y2 = ships[i].to.y, d = dist(x1,y1,x2,y2),
dx = (x2-x1)/d, dy = (y2-y1)/d, a = Math.atan2(y2-y1,x2-x1);
ships[i].x += dx*3+ships[i].rdx;
ships[i].y += dy*3+ships[i].rdy;
// c.beginPath();
// c.arc(ships[i].x,ships[i].y,3,0,2*Math.PI, false);
// c.fillStyle = ships[i].p == 1 ? '#d00' : '#0d0';
// c.fill();
c.save();
c.translate(ships[i].x,ships[i].y);
c.rotate(a);
c.fillStyle = ships[i].p == 1 ? '#d00' : '#0d0';
c.beginPath();
c.moveTo(0,0);
c.lineTo(-16,-6);
c.lineTo(-16, 6);
c.closePath();
c.fill();
c.beginPath();
for(var j=0;j<9;j++){c.strokeStyle='rgba(255,'+Math.floor(99+R()*99)+',0,'+R()+')'; c.moveTo(-16, 0); c.lineTo(R()*-10-20, R()*8-4);c.stroke();}
c.restore();
if(pc=shipCollision(ships[i])){
if(pc.p==ships[i].p){
pc.e++;
sfx.push({x: pc.x, y: pc.y, r: pc.r, t: 1, f: 10});
}else if(pc.p!=ships[i].p){
pc.e--;
if(pc.e==0) pc.p=0; else if(pc.e<0) pc.p=ships[i].p;
sfx.push({x: ships[i].x, y: ships[i].y, t: 0, f: 10});
}
ships.splice(i, 1);
}
}
/* handle sfx */
for(var i=0;i<sfx.length;i++){
if(sfx[i].t==0){ //explosion
c.strokeStyle='rgba(255,'+Math.floor(99+R()*99)+',0,'+(R()*0.33+0.66)+')';
c.lineWidth = 2;
c.beginPath();
c.moveTo(sfx[i].x+R()*20-10,sfx[i].y+R()*20-10);
for(var j=0;j<3;j++)c.lineTo(sfx[i].x+R()*20-10,sfx[i].y+R()*20-10);
c.closePath();
c.stroke();
}else if(sfx[i].t==1){ //planet energy boost
c.strokeStyle='rgba(99,99,'+Math.floor(99+R()*99)+','+(R()*0.33+0.66)+')';
c.lineWidth = 2;
c.beginPath();
c.arc(sfx[i].x, sfx[i].y, sfx[i].r+R()*5, 0, 2 * Math.PI, false);
c.closePath();
c.stroke();
}
if(--sfx[i].f<0)sfx.splice(i, 1);
}
requestAnimationFrame(gameLoop);
}
function newGame(){ planets = generatePlanets(12); ships = []; }
C.addEventListener('touchstart',clickHandler, false);
C.addEventListener('click',clickHandler, false);
newGame();
gameLoop();
</script>
<style>*{margin:0;padding:0;box-sizing:border-box;}canvas{width:100%;height:100%}</style>
<canvas id='C' style='background:#000;'>
<script>
var c=C.getContext("2d"),R=Math.random,tick=Date.now(),selectedPlanets=new Set(),i,j,ships=[],planets=[],sfx=[];
C.width = window.innerWidth; C.height = window.innerHeight;
c.font = '18px Helvetica'; c.textAlign = 'center'; c.textBaseline = 'middle';
function synth(f,d){
for(var t=0,s='RIFF_oO_WAVEfmt '+atob('EAAAAAEAAQBAHwAAQB8AAAEACAA')+'data';++t<d;)s+=String.fromCharCode(f(t));
return new Audio('data:audio/wav;base64,'+btoa(s));
}
function s_gameOver(t){return (Math.sin(t/(t>>9))*10)&255}
function dist(x1,y1,x2,y2){return Math.sqrt(Math.pow(x1-x2,2)+Math.pow(y1-y2,2))}
function inPlanet(x,y,p){return dist(p.x,p.y,x,y)<p.r}
function generatePlanets(n){
var i, s = [30,60,90,120], m = s.slice(-1)[0];
function randPlanet(){
var x = R()*(C.width-m*2)+m, y = R()*(C.height-m*2)+m, p = x<C.width*0.5 && y<C.height*0.5 ? 1 : x>C.width*0.5 && y>C.height*0.5 ? 2 : 0;
return {x: x, y: y, r: s[Math.floor(R()*s.length)], e: 10, p: p}
}
function isGoodPlanet(q,p){for(i=0;i<p.length;i++){if(dist(q.x,q.y,p[i].x,p[i].y)<((q.r+p[i].r)*1.5)){return false}}return true}
function newPlanet(p,q){while(!isGoodPlanet(q=randPlanet(),p)){q=randPlanet()}return q}
var p=[];for(i=0;i<n;i++){p.push(newPlanet(p))};return p;
}
function sendShips(p,q){
for(var i=0,n=Math.floor(p.e*0.5);i<Math.min(n,p.e);i++){ /*create ships based on planets energy*/
ships.push({from: p, to: q, p: p.p, x: p.x+R()*p.r-p.r/2, y: p.y+R()*p.r-p.r/2, rdx: R()*0.8-0.4, rdy: R()*0.8-0.4}); /*create ship*/
p.e--; /*take energy from planet*/
}
}
function shipCollision(s){
if(s.from==s.to)return s.to;
for(var i=0;i<planets.length;i++)if(planets[i]!=s.from&&(planets[i].p!=s.p||planets[i]==s.to)&&inPlanet(s.x,s.y,planets[i]))return planets[i];
return null;
}
function gameLoop() {
var tickAction = false, pc;
if(Date.now()-tick>1000){ tick = Date.now(); tickAction = true; }
c.clearRect(0, 0, C.width, C.height);
/* handle planets */
for(var i=0;i<planets.length;i++){
c.beginPath();
c.arc(planets[i].x, planets[i].y, planets[i].r, 0, 2 * Math.PI, false);
c.fillStyle = planets[i].p == 0 ? '#999' : planets[i].p == 2 ? '#900' : '#090';
c.fill();
c.fillStyle = 'rgba(255,255,255,0.8)';
c.fillText(planets[i].e, planets[i].x, planets[i].y);
if(tickAction && planets[i].p != 0){
if(planets[i].e < planets[i].r) planets[i].e=Math.floor(planets[i].e + planets[i].r/30); else planets[i].e--;
}
}
/* winning condition */
if(tickAction){
if(!planets.find(function(p){return p.p==2}) && !ships.find(function(s){return s.p==2})){ alert("Green wins"); newGame() }
else if(!planets.find(function(p){return p.p==1}) && !ships.find(function(s){return s.p==1})){ alert("Red wins"); newGame() }
}
/* handle ships */
for(var i=0;i<ships.length;i++){
var x1 = ships[i].x, y1 = ships[i].y, x2 = ships[i].to.x, y2 = ships[i].to.y, d = dist(x1,y1,x2,y2),
dx = (x2-x1)/d, dy = (y2-y1)/d, a = Math.atan2(y2-y1,x2-x1);
ships[i].x += dx*3+ships[i].rdx;
ships[i].y += dy*3+ships[i].rdy;
c.save();
c.translate(ships[i].x,ships[i].y);
c.rotate(a);
c.fillStyle = ships[i].p == 2 ? '#d00' : '#0d0';
c.beginPath();
c.moveTo(0,0);
c.lineTo(-16,-6);
c.lineTo(-16, 6);
c.closePath();
c.fill();
c.beginPath();
for(var j=0;j<4;j++){c.strokeStyle='rgba(255,'+Math.floor(99+R()*99)+',0,'+R()+')'; c.moveTo(-16, 0); c.lineTo(R()*-10-25, R()*8-4);c.stroke();}
c.restore();
if(pc=shipCollision(ships[i])){
if(pc.p==ships[i].p){
pc.e++;
sfx.push({x: pc.x, y: pc.y, r: pc.r, t: 1, f: 10});
}else if(pc.p!=ships[i].p){
pc.e--;
if(pc.e==0) pc.p=0; else if(pc.e<0) pc.p=ships[i].p;
sfx.push({x: ships[i].x, y: ships[i].y, t: 0, f: 10});
}
ships.splice(i, 1);
}
}
/* handle sfx */
for(var i=0;i<sfx.length;i++){
if(sfx[i].t==0){ //explosion
c.strokeStyle='rgba(255,'+Math.floor(99+R()*99)+',0,'+(R()*0.33+0.66)+')';
c.lineWidth = 2;
c.beginPath();
c.moveTo(sfx[i].x+R()*20-10,sfx[i].y+R()*20-10);
for(var j=0;j<3;j++)c.lineTo(sfx[i].x+R()*20-10,sfx[i].y+R()*20-10);
c.closePath();
c.stroke();
}else if(sfx[i].t==1){ //planet energy boost
c.strokeStyle='rgba(99,99,'+Math.floor(99+R()*99)+','+(R()*0.33+0.66)+')';
c.lineWidth = 2;
c.beginPath();
c.arc(sfx[i].x, sfx[i].y, sfx[i].r+R()*5, 0, 2 * Math.PI, false);
c.closePath();
c.stroke();
}
if(--sfx[i].f<0)sfx.splice(i, 1);
}
if(tickAction){ aiMove(planets, 2) }
requestAnimationFrame(gameLoop);
}
function planetOnPosition(pos, teams){
for(var i=0;i<planets.length;i++)
if(inPlanet(pos[0], pos[1], planets[i]) && teams.indexOf(planets[i].p)!==-1 )
return planets[i];
return null;
}
function pos(e){
return [e.changedTouches ? e.changedTouches[0].pageX : e.pageX, e.changedTouches ? e.changedTouches[0].pageY : e.pageY];
}
function dragmove(e){
e.preventDefault();
var planet = planetOnPosition(pos(e), [1]);
if(planet) selectedPlanets.add(planet);
}
function drop(e){
var planet = planetOnPosition(pos(e), [0,1,2]);
if(planet) for(p of selectedPlanets) sendShips(p, planet);
selectedPlanets.clear();
}
function aiMove(planets, team){
var neutralPlanets = [], myPlanets = [], enemyPlanets = [];
planets.forEach(function(p){
if(p.p == 0) neutralPlanets.push(p);
else if(p.p == team) myPlanets.push(p);
else if(p.p != team) enemyPlanets.push(p);
});
myPlanets.sort(function(a, b){ return b.e - a.e }); // strongest first
enemyPlanets.sort(function(a, b){ return a.e - b.e }); // weakest first
neutralPlanets.sort(function(a, b){ return a.e - b.e }); // weakest first
if(myPlanets.length > 0 && enemyPlanets.length > 0 && myPlanets[0].e > 10) sendShips(myPlanets[0], enemyPlanets[0]);
if(neutralPlanets.length > 0) for(p of myPlanets) if(p.e > 20) sendShips(p, neutralPlanets[0]);
}
function newGame(){ planets = generatePlanets(12); ships = []; }
C.addEventListener('touchstart', dragmove, false);
C.addEventListener('mousedown', dragmove, false);
C.addEventListener('touchmove', dragmove, false);
C.addEventListener('mousemove', dragmove, false);
C.addEventListener('touchend', drop, false);
C.addEventListener('mouseup', drop, false);
newGame();
gameLoop();
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment