$(function(){function h(a,b,c){a.spriteCanvas=document.createElement("canvas"),a.spriteImage=this.preloadImage(c+"/"+b.name+"-sprite-sheet.png",function(){i(a,b)}),a.spriteArray=[],a.spriteCount=0;for(var d=0;d<b.imagesToLoad.length;d++){var e=b.imagesToLoad[d].count,f=b.imagesToLoad[d].name;a.spriteArray[f]={name:f,count:e,offset:a.spriteCount},a.spriteCount+=e}}function i(a,b){a.spriteCanvas.width=a.spriteImage.width,a.spriteCanvas.height=a.spriteImage.height*2;var c=a.spriteCanvas.getContext("2d");c.drawImage(a.spriteImage,0,0),c.drawImage(a.spriteImage,0,a.spriteImage.height);var d=[{gdi:[198,170,93],nod:[218,0,0]},{gdi:[178,149,80],nod:[191,26,7]},{gdi:[97,76,36],nod:[108,0,0]},{gdi:[145,137,76],nod:[169,27,26]},{gdi:[125,117,64],nod:[133,39,30]},{gdi:[109,101,56],nod:[125,1,0]},{gdi:[89,85,44],nod:[96,41,24]},{gdi:[170,153,85],nod:[190,26,7]},{gdi:[194,174,97],nod:[220,0,0]},{gdi:[246,214,121],nod:[255,0,1]},{gdi:[222,190,105],nod:[246,1,0]}],e=c.getImageData(0,0,a.spriteCanvas.width,a.spriteCanvas.height),f=e.data,g=f.length/4;console.log(g+" "+b.type+" "+b.name);for(var h=g/2;h<g;h++){var i=f[h*4],j=f[h*4+1],k=f[h*4+2],l=f[h*4+2];if(b.type=="turret"||b.type=="building"||b.name=="mcv"||b.name=="harvester"){for(var m=d.length-1;m>=0;m--)if(i==d[m].gdi[0]&&j==d[m].gdi[1]&&k==d[m].gdi[2]){f[h*4+0]=d[m].nod[0],f[h*4+1]=d[m].nod[1],f[h*4+2]=d[m].nod[2];break}}else if(b.type=="vehicle"||b.type=="infantry")f[h*4+0]=(i+j+k)/3,f[h*4+1]=(i+j+k)/3,f[h*4+2]=(i+j+k)/3}for(var h=0;h<g;h++){var i=f[h*4],j=f[h*4+1],k=f[h*4+2],l=f[h*4+2];j==255&&(k==96||k==89||k==85)&&(i==0||i==85)&&(f[h*4]=0,f[h*4+1]=0,f[h*4+2]=0,e.data[h*4+3]=.8)}c.putImageData(e,0,0)}function r(a,b){var c=this;this.loaded=!1;var d=new Image;return d.src="images/"+a,this.preloadCount++,$(d).bind("load",function(){c.loadedCount++,c.loadedCount==c.preloadCount&&(c.loaded=!0),b&&b()}),d}function s(a,b,c){c||(c=".png");var d=[];for(var e=0;e<b;e++)d.push(this.preloadImage(a+"-"+(e<10?"0":"")+e+c));return d}function t(){var a=this.health/this.hitPoints;a>.7?this.life="healthy":a>.4?this.life="damaged":this.life="ultra-damaged"}function u(){if(this.selected){b.strokeStyle="white";var a=5,c=this.x*d.gridSize+d.viewportAdjustX+this.pixelOffsetX,f=this.y*d.gridSize+d.viewportAdjustY+this.pixelOffsetY,g=c+this.pixelLeft,h=f+this.pixelTop,i=g+this.pixelWidth,j=h+this.pixelHeight;b.beginPath(),b.moveTo(g,h+a),b.lineTo(g,h),b.lineTo(g+a,h),b.moveTo(i-a,h),b.lineTo(i,h),b.lineTo(i,h+a),b.moveTo(i,j-a),b.lineTo(i,j),b.lineTo(i-a,j),b.moveTo(g+a,j),b.lineTo(g,j),b.lineTo(g,j-a),b.stroke(),this.getLife(),b.beginPath(),b.rect(g,h-a-2,this.pixelWidth*this.health/this.hitPoints,a),this.life=="healthy"?b.fillStyle="lightgreen":this.life=="damaged"?b.fillStyle="yellow":b.fillStyle="red",b.fill(),b.beginPath(),b.strokeStyle="black",b.rect(g,h-a-2,this.pixelWidth,a),b.stroke(),this.primaryBuilding&&b.drawImage(e.primaryBuildingImage,(g+i-e.primaryBuildingImage.width)/2,j-e.primaryBuildingImage.height)}}function v(a,b){var c=this.x*d.gridSize+this.pixelOffsetX,e=this.y*d.gridSize+this.pixelOffsetY,f=c+this.pixelLeft,g=e+this.pixelTop,h=f+this.pixelWidth,i=g+this.pixelHeight;return a<f||a>h||b<g||b>i?!1:!0}function w(a){a||(a=this);var b,c;for(var e=0;e<d.buildings.length;e++){var f=d.buildings[e];if(f.name=="refinery"&&f.team==a.team){var g=Math.pow(f.x-a.x,2)+Math.pow(f.y-a.y,2);if(!b||b>g)c=f,b=g}}return c}function x(a){a||(a=this);var b,c;for(var e=0;e<d.overlay.length;e++){var f=d.overlay[e];if(f.name=="tiberium"&f.stage>0){var g=Math.pow(f.x-a.x,2)+Math.pow(f.y-a.y,2);if(!b||b>g)c=f,b=g}}return c}function y(a,b){b||(b=0);var c=[];a||(a=this);for(var e=d.units.length-1;e>=0;e--){var f=d.units[e];f.team!=a.team&&Math.pow(f.x-a.x,2)+Math.pow(f.y-a.y,2)<=Math.pow(a.sight+b,2)&&c.push(f)}for(var e=d.buildings.length-1;e>=0;e--){var f=d.buildings[e];f.team!=a.team&&Math.pow(f.x+f.gridWidth/2-a.x,2)+Math.pow(f.y+f.gridHeight/2-a.y,2)<=Math.pow(a.sight+b,2)&&c.push(f)}for(var e=d.turrets.length-1;e>=0;e--){var f=d.turrets[e];f.team!=a.team&&Math.pow(f.x+f.gridWidth/2-a.x,2)+Math.pow(f.y+f.gridHeight/2-a.y,2)<=Math.pow(a.sight+b,2)&&c.push(f)}return c}function z(a,c){var e=d.obstructionGrid;try{e[c[1]][c[0]]=0,e[a[1]][a[0]]}catch(f){return[{x:a[0],y:a[1]},{x:c[0],y:c[1]}]}var g=H(e,a,c,"Euclidean");G(g,e);if(g.length>1&&d.debugMode)for(k=0;k<g.length;k++)b.beginPath(),b.fillStyle="rgba(150,50,100,0.5)",b.arc((g[k].x+.5)*d.gridSize+d.viewportAdjustX,(g[k].y+.5)*d.gridSize+d.viewportAdjustY,5,0,2*Math.PI),b.fill();return g}function D(a,b,c){return a=Math.floor(a),b=Math.floor(b),a>=c/2&&(a-=c),b>=c/2&&(b-=c),diff=b-a,diff<-c/2&&(diff+=c),diff>c/2&&(diff-=c),diff}function E(a,b,c){return a=Math.round(a)+b,a>c-1&&(a-=c),a<0&&(a+=c),a}function F(a,b,c){c||(c=32),b||(b=this);var d=a.y-b.y,e=a.x-b.x;b.type=="turret"&&(d-=.5,e-=.5);var f=c/2+Math.round(Math.atan2(e,d)*c/(2*Math.PI));return f<0&&(f+=c),f>=c&&(f-=c),f}function G(a,b){var c=!0,d=a[0];while(c&&a.length>2){var e=a[2];if(Math.abs(e.y-d.y)>Math.abs(e.x-d.x)){var f=(e.x-d.x)/(e.y-d.y),g=.4*(e.y-d.y)/Math.abs(e.y-d.y),h=g,i={x:d.x+h*f,y:d.y+h};while(c&&Math.abs(i.y-e.y)>.3)b[Math.floor(i.y)][Math.floor(i.x)]>0&&(c=!1),h+=g,i={x:d.x+h*f,y:d.y+h}}else{var f=(e.y-d.y)/(e.x-d.x),j=.4*(e.x-d.x)/Math.abs(e.x-d.x),k=j,i={x:d.x+k,y:d.y+f*k};while(c&&Math.abs(i.x-e.x)>=.3)b[Math.floor(i.y)][Math.floor(i.x)]>0&&(c=!1),k+=j,i={x:d.x+k,y:d.y+f*k}}c&&a.splice(1,1)}}var a=$("#canvas")[0],b=a.getContext("2d"),c={x:0,y:0,gridX:0,gridY:0,gameX:0,gameY:0,insideCanvas:!1,panDirection:"",panningThreshold:48,panningVelocity:24,handlePanning:function(){var a="";c.insideCanvas&&(c.y>d.viewportTop+c.panningThreshold||c.y<d.viewportTop?c.y<d.viewportTop+d.viewportHeight-c.panningThreshold||c.y>d.viewportTop+d.viewportHeight?(d.viewportDeltaY=0,a+=""):(d.viewportDeltaY=c.panningVelocity,a+="_bottom"):(d.viewportDeltaY=-c.panningVelocity,a+="_top"),c.x>=c.panningThreshold||c.y<d.viewportTop||c.y>d.viewportTop+d.viewportHeight?c.x<=d.screenWidth-c.panningThreshold||c.y<d.viewportTop||c.y>d.viewportTop+d.viewportHeight?(d.viewportDeltaX=0,a+=""):(d.viewportDeltaX=c.panningVelocity,a+="_right"):(d.viewportDeltaX=-c.panningVelocity,a+="_left"));if(d.viewportX+d.viewportDeltaX<0||d.viewportX+d.viewportDeltaX+d.screenWidth+(e.visible?-e.width:0)>d.currentLevel.mapImage.width)d.viewportDeltaX=0;!e.visible&&d.viewportX+d.screenWidth>d.currentLevel.mapImage.width&&(d.viewportX=d.currentLevel.mapImage.width-d.screenWidth,d.viewportDeltaX=0);if(d.viewportY+d.viewportDeltaY<0||d.viewportY+d.viewportDeltaY+d.viewportHeight>d.currentLevel.mapImage.height)d.viewportDeltaY=0;a!=""&&(d.viewportDeltaX==0&&d.viewportDeltaY==0?a="no_pan"+a:a="pan"+a),c.panDirection=a,d.viewportX+=d.viewportDeltaX,d.viewportY+=d.viewportDeltaY,c.gameX=c.x+d.viewportX-d.viewportLeft,c.gameY=c.y+d.viewportY-d.viewportTop,d.viewportAdjustX=d.viewportLeft-d.viewportX,d.viewportAdjustY=d.viewportTop-d.viewportY},cursorLoop:0,drawCursor:function(){if(!this.insideCanvas)return;this.cursorLoop++,this.cursorLoop>=this.cursor.cursorSpeed*this.cursor.count&&(this.cursorLoop=0);if(this.dragSelect){var a=Math.min(this.gameX,this.dragX),c=Math.min(this.gameY,this.dragY),e=Math.abs(this.gameX-this.dragX),f=Math.abs(this.gameY-this.dragY);b.strokeStyle="white",b.strokeRect(a+d.viewportAdjustX,c+d.viewportAdjustY,e,f)}var g=this.cursor.spriteOffset+Math.floor(this.cursorLoop/this.cursor.cursorSpeed);b.drawImage(this.spriteImage,30*g,0,30,24,this.x-this.cursor.x,this.y-this.cursor.y,30,24)},checkOverObject:function(){this.overObject=null;for(var a=d.overlay.length-1;a>=0;a--){var b=d.overlay[a];b.name=="tiberium"&&this.gridX==b.x&&this.gridY==b.y&&(this.overObject=b)}for(var a=d.buildings.length-1;a>=0;a--)if(d.buildings[a].underPoint(this.gameX,this.gameY)){this.overObject=d.buildings[a];break}for(var a=d.turrets.length-1;a>=0;a--)if(d.turrets[a].underPoint(this.gameX,this.gameY)){this.overObject=d.turrets[a];break}for(var a=d.units.length-1;a>=0;a--)if(d.units[a].underPoint&&d.units[a].underPoint(this.gameX,this.gameY)){this.overObject=d.units[a];break}return this.overObject},draw:function(){this.cursor=this.cursors["default"];var a=this.checkOverObject();if(this.y>=d.viewportTop&&this.y<=d.viewportTop+d.viewportHeight)if(e.deployMode){var f=g.types[e.deployBuilding]||m.types[e.deployBuilding],h=$.extend([],f.gridShape);h.push(h[h.length-1]);for(var i=0;i<h.length;i++)for(var k=0;k<h[i].length;k++)h[i][k]==1&&(d.obstructionGrid[c.gridY+i][c.gridX+k]==1?d.highlightGrid(c.gridX+k,c.gridY+i,1,1,e.placementRedImage):d.highlightGrid(c.gridX+k,c.gridY+i,1,1,e.placementWhiteImage))}else if(e.repairMode)a&&a.team==d.currentLevel.team&&(a.type=="building"||a.type=="turret")&&a.health<a.hitPoints?this.cursor=this.cursors.repair:this.cursor=this.cursors.no_repair;else if(e.sellMode)!a||a.team!=d.currentLevel.team||a.type!="building"&&a.type!="turret"?this.cursor=this.cursors.no_sell:this.cursor=this.cursors.sell;else if(e.visible&&c.x>e.left){var n=e.hoveredButton();if(n){var o=n.type;switch(n.type){case"infantry":o=j.types[n.name].label;break;case"building":o=g.types[n.name].label;break;case"turret":o=m.types[n.name].label;break;case"vehicle":o=l.types[n.name].label}var p="$"+n.cost;b.fillStyle="black",b.fillRect(Math.round(this.x),Math.round(this.y+16),o.length*5.5+8,32),b.strokeStyle="darkgreen",b.strokeRect(Math.round(this.x),Math.round(this.y+16),o.length*5.5+8,32),b.fillStyle="darkgreen",b.font='12px "Command and Conquer"',b.fillText(o,Math.round(this.x+4),Math.round(this.y+30)),b.fillText(p,Math.round(this.x+4),Math.round(this.y+44))}}else this.dragSelect?this.cursor=this.cursors["default"]:a?a.team&&a.team!=d.currentLevel.team&&d.selectedAttackers.length>0?this.cursor=this.cursors.attack:d.selectedUnits.length!=1||d.selectedUnits[0].name!="harvester"||d.selectedUnits[0].team!=d.currentLevel.team||a.name!="tiberium"&&a.name!="refinery"?d.selectedUnits.length==1&&a.selected&&a.team==d.currentLevel.team?a.name=="mcv"&&(this.cursor=this.cursors.build_command):!a.selected&&a.name!="tiberium"?this.cursor=this.cursors.select:a.name=="tiberium"&&(d.obstructionGrid[c.gridY]&&d.obstructionGrid[c.gridY][c.gridX]==1?this.cursor=this.cursors.no_move:this.cursor=this.cursors.move):(a.name=="tiberium"&&(this.cursor=this.cursors.attack),a.name=="refinery"&&a.team==d.currentLevel.team&&(this.cursor=this.cursors.load_vehicle)):this.panDirection&&this.panDirection!=""?this.cursor=this.cursors[this.panDirection]:d.selectedUnits.length>0&&(d.obstructionGrid[c.gridY]&&d.obstructionGrid[c.gridY][c.gridX]==1?this.cursor=this.cursors.no_move:this.cursor=this.cursors.move);this.insideCanvas&&this.drawCursor()},click:function(a,b){c.y<=d.viewportTop&&c.y>d.viewportTop-15?(c.x<0||c.x>=160)&&(c.x<320||c.x>=480)&&c.x>=480&&c.x<640&&(e.visible=!e.visible):c.y>=d.viewportTop&&c.y<=d.viewportTop+d.viewportHeight&&(e.visible&&c.x>e.left?e.click(a,b):d.click(a,b))},listenEvents:function(){$("#canvas").mousemove(function(a){var b=$("#canvas").offset();c.x=a.pageX-b.left,c.y=a.pageY-b.top,c.gridX=Math.floor(c.gameX/d.gridSize),c.gridY=Math.floor(c.gameY/d.gridSize);if(c.buttonPressed){if(Math.abs(c.dragX-c.gameX)>5||Math.abs(c.dragY-c.gameY)>5)c.dragSelect=!0}else c.dragSelect=!1}),$("#canvas").click(function(a){return c.click(a,!1),c.dragSelect=!1,!1}),$("#canvas").mousedown(function(a){return a.which==1&&(c.buttonPressed=!0,c.dragX=c.gameX,c.dragY=c.gameY,a.preventDefault()),!1}),$("#canvas").bind("contextmenu",function(a){return c.click(a,!0),!1}),$("#canvas").mouseup(function(a){if(a.which==1){if(c.dragSelect){a.shiftKey||d.clearSelection();var b=Math.min(c.gameX,c.dragX),e=Math.min(c.gameY,c.dragY),f=Math.max(c.gameX,c.dragX),g=Math.max(c.gameY,c.dragY);for(var h=d.units.length-1;h>=0;h--){var i=d.units[h];!i.selected&&i.team==d.currentLevel.team&&b<=i.x*d.gridSize&&f>=i.x*d.gridSize&&e<=i.y*d.gridSize&&g>=i.y*d.gridSize&&d.selectItem(i)}}c.buttonPressed=!1}return!1}),$("#canvas").mouseleave(function(a){c.insideCanvas=!1}),$("#canvas").mouseenter(function(a){c.buttonPressed=!1,c.insideCanvas=!0})},loaded:!1,preloadCount:0,loadedCount:0,preloadImage:r,spriteImage:null,cursors:[],cursorCount:0,loadCursor:function(a,b,c,d,e){!b&&!c&&(b=0,c=0),e||(e=1),d||(d=1),this.cursors[a]={x:b,y:c,name:a,count:d,spriteOffset:this.cursorCount,cursorSpeed:e},this.cursorCount+=d},loadAllCursors:function(){c.spriteImage=this.preloadImage("cursors.png"),c.loadCursor("attack",15,12,8),c.loadCursor("big_detonate",15,12,3),c.loadCursor("build_command",15,12,9),c.loadCursor("default"),c.loadCursor("detonate",15,12,3),c.loadCursor("load_vehicle",15,12,3,2),c.loadCursor("unknown"),c.loadCursor("unknown"),c.loadCursor("move",15,12),c.loadCursor("no_default"),c.loadCursor("no_move",15,12),c.loadCursor("no_pan_bottom",15,24),c.loadCursor("no_pan_bottom_left",0,24),c.loadCursor("no_pan_bottom_right",30,24),c.loadCursor("no_pan_left",0,12),c.loadCursor("no_pan_right",30,12),c.loadCursor("no_pan_top",15,0),c.loadCursor("no_pan_top_left",0,0),c.loadCursor("no_pan_top_right",30,0),c.loadCursor("no_repair",15,0),c.loadCursor("no_sell",15,12),c.loadCursor("pan_bottom",15,24),c.loadCursor("pan_bottom_left",0,24),c.loadCursor("pan_bottom_right",30,24),c.loadCursor("pan_left",0,12),c.loadCursor("pan_right",30,12),c.loadCursor("pan_top",15,0),c.loadCursor("pan_top_left",0,0),c.loadCursor("pan_top_right",30,0),c.loadCursor("repair",15,0,24),c.loadCursor("select",15,12,6,2),c.loadCursor("sell",15,12,24)}},d={screenWidth:a.width,screenHeight:a.height,viewportTop:35,viewportLeft:0,viewportX:0,viewportY:0,viewportDeltaX:0,viewportDeltaY:0,gridSize:24,animationLoop:null,animationTimeout:50,debugMode:!1,speedAdjustmentFactor:.2,setViewport:function(){b.beginPath(),this.viewportWidth=e.visible?this.screenWidth-e.width:this.screenWidth,this.viewportHeight=480,b.rect(this.viewportLeft,this.viewportTop,this.viewportWidth-this.viewportLeft,this.viewportHeight),b.clip()},drawMap:function(){c.handlePanning(),b.drawImage(this.currentLevel.mapImage,this.viewportX,this.viewportY,this.viewportWidth,this.viewportHeight,this.viewportLeft,this.viewportTop,this.viewportWidth,this.viewportHeight),d.obstructionGrid=[];for(var a=0;a<this.currentLevel.obstructionGrid.length;a++){d.obstructionGrid[a]=[];for(var e=0;e<this.currentLevel.obstructionGrid[a].length;e++)d.obstructionGrid[a][e]=this.currentLevel.obstructionGrid[a][e]}for(var f=this.buildings.length-1;f>=0;f--){var g=this.buildings[f];for(var a=0;a<g.gridShape.length;a++)for(var e=0;e<g.gridShape[a].length;e++)g.gridShape[a][e]==1&&(d.obstructionGrid[a+g.y][e+g.x]=1)}for(var f=this.turrets.length-1;f>=0;f--)d.obstructionGrid[this.turrets[f].y][this.turrets[f].x]=1;for(var f=this.units.length-1;f>=0;f--){var h=this.units[f];if(!h.moving&&h.orders&&h.orders.type!="guard")break}for(var f=this.overlay.length-1;f>=0;f--){var i=this.overlay[f];i.name=="tree"?d.obstructionGrid[i.y][i.x]=1:i.name=="trees"&&(d.obstructionGrid[i.y][i.x]=1,d.obstructionGrid[i.y][i.x+1]=1)}},highlightGrid:function(a,c,e,f,g){var h=d.gridSize;g&&$(g).is("img")?b.drawImage(g,a*h+d.viewportAdjustX,c*h+d.viewportAdjustY,e*h,f*h):(g?b.fillStyle=g:b.fillStyle="rgba(225,225,225,0.5)",b.fillRect(a*h+d.viewportAdjustX,c*h+d.viewportAdjustY,e*h,f*h))},drawGrid:function(){var a=d.gridSize,c=d.currentLevel.mapImage.width,e=d.currentLevel.mapImage.height,f=d.viewportX,g=d.viewportY,h=c/a,i=e/a;b.beginPath(),b.strokeStyle="rgba(30,0,0,.6)";for(var j=0;j<h;j++)b.moveTo(j*a-f+d.viewportLeft,0-g+d.viewportTop),b.lineTo(j*a-f+d.viewportLeft,e-g+d.viewportTop);for(var j=0;j<i;j++)b.moveTo(0-f+d.viewportLeft,j*a-g+d.viewportTop),b.lineTo(c-f+d.viewportLeft,j*a-g+d.viewportTop);b.stroke();for(var j=d.obstructionGrid.length-1;j>=0;j--)for(var k=d.obstructionGrid[j].length-1;k>=0;k--)d.obstructionGrid[j][k]==1&&d.highlightGrid(k,j,1,1,"rgba(100,0,0,0.5)")},units:[],buildings:[],turrets:[],overlay:[],bullets:[],fireBullet:function(a){a.x=a.x-.5*Math.sin(a.angle),a.y=a.y-.5*Math.cos(a.angle),a.range=a.range-.5,this.bullets.push(a),setTimeout(function(){a.source.bulletFiring=!1},a.source.reloadTime)},drawBullets:function(){for(var a=this.bullets.length-1;a>=0;a--){var c=this.bullets[a];c.speed=5,c.range=c.range-.1*c.speed,c.x=c.x-.1*c.speed*Math.sin(c.angle),c.y=c.y-.1*c.speed*Math.cos(c.angle);var e=c.x*d.gridSize,f=c.y*d.gridSize;if(!c.dead){var g;for(var h=d.units.length-1;h>=0;h--)if(d.units[h].underPoint&&d.units[h].underPoint(e,f)&&d.units[h].team!=c.source.team){g=d.units[h];break}for(var h=d.buildings.length-1;h>=0;h--)if(d.buildings[h].underPoint(e,f)){g=d.buildings[h];break}for(var h=d.turrets.length-1;h>=0;h--)if(d.turrets[h].underPoint(e,f)){g=d.turrets[h];break}g&&(c.dead=!0,g.health=g.health-Math.floor((c.damage?c.damage:10)+10*Math.random()),g.health<=0&&(g.status="destroy")),b.fillStyle="red",b.fillRect(e+d.viewportAdjustX,f+d.viewportAdjustY,2,2)}c.range<=0&&this.bullets.splice(a,1)}},drawObjects:function(){var a=[];for(var b=this.buildings.length-1;b>=0;b--)this.buildings[b].status=="destroy"&&this.buildings.splice(b,1);for(var b=this.units.length-1;b>=0;b--)this.units[b].status=="destroy"&&this.units.splice(b,1);for(var b=this.turrets.length-1;b>=0;b--)this.turrets[b].status=="destroy"&&this.turrets.splice(b,1);for(var b=this.selectedItems.length-1;b>=0;b--)this.selectedItems[b].status=="destroy"&&this.selectedItems.splice(b,1);for(var b=this.selectedAttackers.length-1;b>=0;b--)this.selectedAttackers[b].status=="destroy"&&this.selectedAttackers.splice(b,1);for(var b=this.selectedUnits.length-1;b>=0;b--)this.selectedUnits[b].status=="destroy"&&this.selectedUnits.splice(b,1);$.merge(a,this.units),$.merge(a,this.buildings),$.merge(a,this.overlay),$.merge(a,this.turrets);var c=function(a){return a.type=="building"?a.y+a.gridShape.length/2:a.y};a.sort(function(a,b){return c(b)-c(a)});for(var b=this.overlay.length-1;b>=0;b--){var d=this.overlay[b];d.name=="tiberium"&&d.draw()}for(var b=a.length-1;b>=0;b--)a[b].name!="tiberium"&&a[b].draw()},moveObjects:function(){for(var a=this.units.length-1;a>=0;a--)this.units[a].processOrders&&this.units[a].processOrders(),this.units[a].move();for(var a=this.turrets.length-1;a>=0;a--)this.turrets[a].processOrders&&this.turrets[a].processOrders(),this.turrets[a].move()},showDebugger:function(){var a=function(a){var b="<ul>";for(key in a)if(a.hasOwnProperty(key)){var c=a[key];if(typeof c!="function"||c===null)typeof c=="object"?(b+="<li>"+key+" : ",c instanceof HTMLImageElement?b+=c.src.replace(/^.+images\//,""):c instanceof Array?b+="Array["+c.length+"]":b+="Object"):b+="<li>"+key+" : "+c+"</li>"}return b+="</ul>",b},b="";b+="Level",b+=a(p),b+="Mouse",b+=a(c),d.selectedItems.length==1&&(b+="Selected Item",b+=a(d.selectedItems[0])),b+="Game",b+=a(d),b+="Sidebar",b+=a(e),b+="Vehicles",b+=a(l),b+="Buildings",b+=a(g),b+="Infantry",b+=a(j),$("#debugger").html(b)},animate:function(){d.debugMode&&d.showDebugger();if(!p.loaded||!e.loaded||!l.loaded||!j.loaded||!g.loaded){b.clearRect(0,0,a.width,a.height);return}b.save(),e.draw(),d.setViewport(),d.drawMap(),d.debugMode&&d.drawGrid(),d.moveObjects(),d.drawObjects(),d.drawBullets(),d.debugMode||A.draw(),b.restore(),d.drawMessage(),c.draw()},messageVisible:!0,messageHeadingVisible:!0,messageText:"\nCreate a base by deploying your MCV. Build a power plant and weapons factory.\n\nUse your tanks to get rid of all enemy presence in the area.",drawMessage:function(){if(!this.messageVisible)return;b.drawImage(e.messageBox,d.viewportLeft+22,d.viewportTop+150),this.messageHeadingVisible||(b.fillStyle="black",b.fillRect(265,198,120,20)),b.fillStyle="green",b.font='16px "Command and Conquer"';var a=this.messageText.split("\n");for(var c=0;c<a.length;c++)b.fillText(a[c],d.viewportLeft+80,d.viewportTop+200+c*18)},displayMessage:function(a,b){this.messageText=a,this.messageVisible=!0,this.messageHeadingVisible=!!b},missionStatus:function(){var a=[],b=[],c=[],e=[],f=[],g=[];for(var h=d.units.length-1;h>=0;h--)item=d.units[h],item.team==d.currentLevel.team?a.push(item):f.push(item);for(var h=d.buildings.length-1;h>=0;h--)item=d.buildings[h],item.team==d.currentLevel.team?b.push(item):e.push(item);for(var h=d.turrets.length-1;h>=0;h--)item=d.turrets[h],item.team==d.currentLevel.team?c.push(item):g.push(item);a.length==0&&b.length==0&&(q.play("mission_failure"),d.end()),g.length==0&&e.length==0&&f.length==0&&(q.play("mission_accomplished"),d.end())},selectedItems:[],selectedAttackers:[],selectedUnits:[],clearSelection:function(){for(var a=this.selectedItems.length-1;a>=0;a--)this.selectedItems[a].selected=0,this.selectedItems.splice(a,1);this.selectedAttackers=[],this.selectedUnits=[]},selectItem:function(a){a.selected=!0,this.selectedItems.push(a),a.type!="building"&&a.team==d.currentLevel.team&&(this.selectedUnits.push(a),q.play(a.type+"_select"),a.primaryWeapon&&this.selectedAttackers.push(a))},click:function(a,b){if(!(!d.messageVisible||c.x<290||c.x>350||c.y<310||c.y>325)){d.messageVisible=!1;return}var f=c.checkOverObject();if(b){this.clearSelection(),e.repairMode=!1,e.deployMode=!1,e.sellMode=!1;return}if(e.repairMode)f&&f.team==d.currentLevel.team&&(f.type=="building"||f.type=="turret")&&f.health<f.hitPoints&&(f.repairing=!0);else if(e.deployMode){var h=g.types[e.deployBuilding]||m.types[e.deployBuilding],i=$.extend([],h.gridShape);i.push(i[i.length-1]);for(var j=0;j<i.length;j++)for(var k=0;k<i[j].length;k++)if(i[j][k]==1&&d.obstructionGrid[c.gridY+j][c.gridX+k]==1){q.play("cannot_deploy_here");return}e.finishDeployingBuilding()}else if(e.sellMode)f&&f.team==d.currentLevel.team&&(f.type=="building"||f.type=="turret")&&(f.status="sell",q.play("sell"),e.cash+=f.cost/2);else if(!b&&!c.dragSelect)if(f)if(d.selectedUnits.length==1&&f.selected&&f.team==d.currentLevel.team)f.name=="mcv"&&(this.clearSelection(),f.orders={type:"build"});else if(d.selectedUnits.length!=1||d.selectedUnits[0].name!="harvester"||d.selectedUnits[0].team!=d.currentLevel.team||f.name!="tiberium"&&f.name!="refinery")if(f.team==d.currentLevel.team)a.shiftKey||this.clearSelection(),this.selectItem(f);else if(d.selectedAttackers.length>0&&f.name!="tiberium")for(var l=d.selectedAttackers.length-1;l>=0;l--)d.selectedAttackers[l].primaryWeapon&&(d.selectedAttackers[l].orders={type:"attack",target:f},q.play(d.selectedAttackers[l].type+"_move"));else if(f.name=="tiberium"){if(d.selectedUnits.length>0)if(!d.obstructionGrid[c.gridY]||d.obstructionGrid[c.gridY][c.gridX]!=1)for(var l=d.selectedUnits.length-1;l>=0;l--)d.selectedUnits[l].orders={type:"move",to:{x:c.gridX,y:c.gridY}},q.play(d.selectedUnits[l].type+"_move")}else a.shiftKey||this.clearSelection(),this.selectItem(f);else f.name=="tiberium"&&(d.selectedUnits[0].orders={type:"harvest",to:{x:f.x,y:f.y}},q.play("vehicle_move")),f.name=="refinery"&&f.team==d.currentLevel.team&&(d.selectedUnits[0].orders={type:"harvest-return",to:f},q.play("vehicle_move"));else if(d.selectedUnits.length>0)if(!d.obstructionGrid[c.gridY]||d.obstructionGrid[c.gridY][c.gridX]!=1)for(var l=d.selectedUnits.length-1;l>=0;l--)d.selectedUnits[l].orders={type:"move",to:{x:c.gridX,y:c.gridY}},q.play(d.selectedUnits[l].type+"_move")},start:function(){c.loadAllCursors(),q.loadAll(),o.loadAll(),this.currentLevel=p.load("gdi1"),this.overlay=this.currentLevel.overlay,e.load(),c.listenEvents(),A.init(),d.viewportX=96,d.viewportY=264,e.visible=!1,this.turrets.push(m.add({name:"gun-turret",x:8,y:6,turretDirection:16,team:"nod"})),this.turrets.push(m.add({name:"gun-turret",x:9,y:3,turretDirection:16,team:"nod"})),this.turrets.push(m.add({name:"gun-turret",x:7,y:5,turretDirection:16,team:"nod"})),this.turrets.push(m.add({name:"gun-turret",x:8,y:2,turretDirection:16,team:"nod"})),this.turrets.push(m.add({name:"gun-turret",x:16,y:25,turretDirection:24,team:"nod"})),this.turrets.push(m.add({name:"gun-turret",x:13,y:26,turretDirection:24,team:"nod"})),this.turrets.push(m.add({name:"gun-turret",x:11,y:23,turretDirection:18,team:"nod"})),this.turrets.push(m.add({name:"gun-turret",x:10,y:24,turretDirection:20,team:"nod"})),this.turrets.push(m.add({name:"gun-turret",x:9,y:25,turretDirection:24,team:"nod"})),this.buildings.push(g.add({name:"refinery",team:"nod",x:26,y:8,status:"build",health:200})),this.buildings.push(g.add({name:"construction-yard",x:1,y:14,team:"nod"})),this.buildings.push(g.add({name:"power-plant",x:5,y:14,team:"nod"})),this.buildings.push(g.add({name:"hand-of-nod",x:5,y:19,team:"nod"})),this.units.push(l.add({name:"light-tank",x:7,y:6,team:"nod",orders:{type:"patrol",from:{x:9,y:24},to:{x:12,y:8}}})),this.units.push(l.add({name:"light-tank",x:2,y:20,team:"nod",orders:{type:"patrol",from:{x:2,y:5},to:{x:6,y:20}}})),this.units.push(l.add({name:"light-tank",x:5,y:10,team:"nod",orders:{type:"patrol",from:{x:17,y:12},to:{x:22,y:2}}})),this.units.push(l.add({name:"light-tank",x:4,y:23,team:"nod",orders:{type:"patrol",from:{x:4,y:23},to:{x:22,y:25}}})),this.units.push(l.add({name:"light-tank",x:2,y:10,team:"nod",orders:{type:"protect",target:d.units[0]}})),this.units.push(l.add({name:"mcv",x:23.5,y:23.5,moveDirection:0,orders:{type:"move",to:{x:23,y:21}}})),this.units.push(l.add({name:"light-tank",x:23,y:27,moveDirection:0,orders:{type:"move",to:{x:22,y:23}}})),this.units.push(l.add({name:"light-tank",x:24,y:27,moveDirection:0,orders:{type:"move",to:{x:24,y:23}}})),this.animationLoop=setInterval(this.animate,this.animationTimeout),this.tiberiumLoop=setInterval(function(){for(var a=0;a<d.overlay.length;a++){var b=d.overlay[a];b.name=="tiberium"&b.stage<11&&b.stage++}},d.animationTimeout*40*600),this.statusLoop=setInterval(d.missionStatus,3e3)},end:function(){clearInterval(this.statusLoop),clearInterval(this.tiberiumLoop),e.visible=!1,d.displayMessage("Thank you for trying this demo.This is still a work in progress. \nAny comments, feedback (including bugs), and advice is appreciated.\n\nIf you liked this demo, please share this page with all your friends. ")}},e={loaded:!0,preloadCount:0,loadedCount:0,preloadImage:r,tabsImage:null,width:160,visible:!0,cash:0,finishDeployingBuilding:function(){for(var a=0;a<d.buildings.length;a++)if(d.buildings[a].name=="construction-yard"&&d.buildings[a].team==d.currentLevel.team){d.buildings[a].status="construct";break}g.types[e.deployBuilding]?d.buildings.push(g.add({name:e.deployBuilding,x:c.gridX,y:c.gridY,status:"build"})):d.turrets.push(m.add({name:e.deployBuilding,x:c.gridX,y:c.gridY,status:"build"})),q.play("construction"),e.deployMode=!1;for(var a=this.leftButtons.length-1;a>=0;a--)this.leftButtons[a].status="";e.deployBuilding=null},finishDeployingUnit:function(a){var b;for(var c=0;c<d.buildings.length;c++)if(d.buildings[c].name==a.dependency[0]){b=d.buildings[c];break}if(a.type=="infantry")d.units.push(j.add({name:a.name,x:b.x+b.gridWidth/2,y:b.y+b.gridHeight,moveDirection:4,instructions:[{type:"move",distance:2}]}));else if(a.type=="vehicle"){b.status="construct";var f=l.add({name:a.name,x:b.x+1,y:b.y+3,moveDirection:16,turretDirection:16,orders:{type:"move",to:{x:Math.floor(b.x-1+Math.random()*4),y:Math.floor(b.y+5)}}});d.units.push(f)}for(var c=this.rightButtons.length-1;c>=0;c--)this.rightButtons[c].dependency[0]==a.dependency[0]&&(this.rightButtons[c].status="");e.deployBuilding=null},hoveredButton:function(){var a=c.y-e.top,b=c.x;if(a>=165&&a<=455){var d=0;for(var f=0;f<6;f++)if(a>=165+f*48&&a<=165+f*48+48){d=f;break}var g,h,i;b<500||b>564?b>=570&&b<=634&&(g="right",h=this.rightButtonOffset+d,i=e.rightButtons):(g="left",h=this.leftButtonOffset+d,i=e.leftButtons);if(i&&i.length>h){var j=i[h];return j}}},click:function(a,b){var d=c.y-this.top,f=c.x;if(d<146||d>160)if(d<455||d>480){if(d>=165&&d<=455){var g=0;for(var h=0;h<6;h++)if(d>=165+h*48&&d<=165+h*48+48){g=h;break}var i,j,k;f<500||f>564?f>=570&&f<=634&&(i="right",j=this.rightButtonOffset+g,k=this.rightButtons):(i="left",j=this.leftButtonOffset+g,k=this.leftButtons);if(k&&k.length>j){var l=k[j];if(l.status==""&&!b)if(l.cost>e.cash)q.play("insufficient_funds");else{for(var h=k.length-1;h>=0;h--)k[h].dependency[0]==l.dependency[0]&&(k[h].status="disabled");l.status="building",l.counter=0,l.spent=l.cost,q.play("building")}else if(l.status=="building"&&!b)q.play("not_ready");else if(l.status=="building"&&b)l.status="hold",q.play("on_hold");else if(l.status=="hold"&&!b)l.status="building",q.play("building");else if(l.status=="hold"&&b){l.status="",q.play("cancelled");for(var h=k.length-1;h>=0;h--)k[h].status=""}else l.status=="ready"&&!b?l.type=="building"&&(e.deployMode=!0,this.repairMode=this.sellMode=this.mapMode=!1,e.deployBuilding=l.name):l.status=="disabled"&&q.play("building_in_progress")}}}else f<500||f>530?f<532||f>562?f<570||f>600?f>=602&&f<=632&&this.rightButtonOffset+6<this.rightButtons.length&&(this.rightButtonOffset++,q.play("button")):this.rightButtonOffset>0&&(this.rightButtonOffset--,q.play("button")):this.leftButtonOffset+6<this.leftButtons.length&&(this.leftButtonOffset++,q.play("button")):this.leftButtonOffset>0&&(this.leftButtonOffset--,q.play("button"));else f<485||f>530?f<538||f>582?f>=590&&f<=635&&(this.mapMode=!this.mapMode,this.repairMode=this.sellMode=this.deployMode=!1):(this.sellMode=!this.sellMode,this.repairMode=this.mapMode=this.deployMode=!1):(this.repairMode=!this.repairMode,this.sellMode=this.mapMode=this.deployMode=!1)},allButtons:[],leftButtons:[],rightButtons:[],checkDependency:function(){for(var a=0;a<this.allButtons.length;a++){var b=this.allButtons[a],c=!0;for(var f=b.dependency.length-1;f>=0;f--){var g=!1,h=b.dependency[f];for(var i=d.buildings.length-1;i>=0;i--){var j=d.buildings[i];if(j.name==h&&j.status!="build"&&j.life!="ultra-damaged"&&j.team==d.currentLevel.team){g=!0;break}}if(!g){c=!1;break}}if(b.type=="building"){var k=!1,l;for(var f=this.leftButtons.length-1;f>=0;f--)if(this.leftButtons[f].name==b.name){k=!0,l=f;break}if(c&&!k)this.leftButtons.push(b),b.status="",b.counter=0,b.speed=this.buildSpeedMultiplier/b.cost,q.play("new_construction_options"),e.visible=!0;else if(k&&!c){if(this.leftButtons[l].status=="building"||this.leftButtons[l].status=="hold"||this.leftButtons[l].status=="ready")for(var f=this.leftButtons.length-1;f>=0;f--)this.leftButtons[f].status="";this.leftButtons.splice(l,1),this.leftButtonOffset=0}}else if(b.type=="infantry"||b.type=="vehicle"){var k=!1,l;for(var f=this.rightButtons.length-1;f>=0;f--)if(this.rightButtons[f].name==b.name){k=!0,l=f;break}if(c&&!k)this.rightButtons.push(b),b.status="",b.counter=0,b.speed=this.buildSpeedMultiplier/b.cost,q.play("new_construction_options");else if(k&&!c){if(this.rightButtons[l].status=="building"||this.rightButtons[l].status=="hold"||this.rightButtons[l].status=="ready")for(var f=this.rightButtons.length-1;f>=0;f--)this.rightButtons[f].dependency[0]==this.rightButtons[l].dependency[0]&&(this.rightButtons[f].status="");this.rightButtons.splice(l,1),this.rightButtonOffset=0}}}},load:function(){this.tabsImage=this.preloadImage("sidebar/tabs.png"),this.sidebarImage=this.preloadImage("sidebar/sidebar.png"),this.primaryBuildingImage=this.preloadImage("sidebar/primary.png"),this.readyImage=this.preloadImage("sidebar/ready.png"),this.holdImage=this.preloadImage("sidebar/hold.png"),this.placementWhiteImage=this.preloadImage("sidebar/placement-white.gif"),this.placementRedImage=this.preloadImage("sidebar/placement-red.gif"),this.powerIndicator=this.preloadImage("sidebar/power/power_indicator2.png"),this.messageBox=this.preloadImage("sidebar/message_box.jpg"),this.repairButtonPressed=this.preloadImage("sidebar/buttons/repair-pressed.png"),this.sellButtonPressed=this.preloadImage("sidebar/buttons/sell-pressed.png"),this.repairImageBig=this.preloadImage("sidebar/repair-big.png"),this.repairImageSmall=this.preloadImage("sidebar/repair-small.png"),this.top=d.viewportTop-2,this.left=a.width-this.width;var b=[{name:"power-plant",type:"building",cost:300,dependency:["construction-yard"]},{name:"advanced-power-plant",type:"building",cost:700,dependency:["construction-yard","power-plant"]},{name:"refinery",type:"building",cost:2e3,dependency:["construction-yard","power-plant"]},{name:"tiberium-silo",type:"building",cost:150,dependency:["construction-yard","refinery"]},{name:"weapons-factory",type:"building",cost:2e3,dependency:["construction-yard","power-plant","refinery"]},{name:"harvester",type:"vehicle",cost:1400,dependency:["weapons-factory","refinery"]},{name:"light-tank",type:"vehicle",cost:600,dependency:["weapons-factory"]}];this.allButtons=[];for(var c=0;c<b.length;c++){var e=b[c];this.allButtons.push({name:e.name,image:this.preloadImage("sidebar/icons/"+e.name+"-icon.png"),type:e.type,status:"",cost:e.cost,dependency:e.dependency})}},textBrightness:0,textBrightnessDelta:-0.1,drawButtonLabel:function(a,c,d){var e=this.iconWidth/2-a.width/2,f=this.iconHeight/2;b.globalAlpha=
this.textBrightness,b.drawImage(a,c+e,d+f),b.globalAlpha=1},drawButtonCost:function(a,c,d){var e=35,f=10;b.fillStyle="white",b.fillText(" "+a,c+e,d+f)},iconWidth:64,iconHeight:48,leftButtonOffset:0,rightButtonOffset:0,buildSpeedMultiplier:300,drawButton:function(a,c){var d=a=="left"?this.leftButtons:this.rightButtons,e=a=="left"?this.leftButtonOffset:this.rightButtonOffset,f=d[c+e],g=a=="left"?500:570,h=165+this.top+c*this.iconHeight;b.drawImage(f.image,g,h),f.status=="ready"?this.drawButtonLabel(this.readyImage,g,h):f.status=="disabled"?(b.fillStyle="rgba(200,200,200,0.6)",b.fillRect(g,h,this.iconWidth,this.iconHeight)):f.status=="building"?(C.clearRect(0,0,this.iconWidth,this.iconHeight),C.fillStyle="rgba(200,200,200,0.6)",C.beginPath(),C.moveTo(this.iconWidth/2,this.iconHeight/2),C.arc(this.iconWidth/2,this.iconHeight/2,40,Math.PI*2*f.counter/100-Math.PI/2,-Math.PI/2),C.moveTo(this.iconWidth/2,this.iconHeight/2),C.fill(),b.drawImage(B,0,0,this.iconWidth,this.iconHeight,g,h,this.iconWidth,this.iconHeight)):f.status=="hold"&&(C.clearRect(0,0,this.iconWidth,this.iconHeight),C.fillStyle="rgba(100,100,100,0.6)",C.beginPath(),C.moveTo(this.iconWidth/2,this.iconHeight/2),C.arc(this.iconWidth/2,this.iconHeight/2,40,Math.PI*2*f.counter/100-Math.PI/2,-Math.PI/2),C.moveTo(this.iconWidth/2,this.iconHeight/2),C.fill(),b.drawImage(B,0,0,this.iconWidth,this.iconHeight,g,h,this.iconWidth,this.iconHeight),this.drawButtonLabel(this.holdImage,g,h))},processButton:function(a,b){var c=a=="left"?this.leftButtons:this.rightButtons,d=0,e=c[b+d],f=a=="left"?500:570,g=165+this.top+b*this.iconHeight;if(e.status=="building"){e.counter+=e.speed,e.spent-=Math.round(e.cost*e.speed/100),this.cash-=Math.round(e.cost*e.speed/100);if(e.counter>99){this.cash-=e.spent,e.status="ready";if(a=="left")q.play("construction_complete");else if(e.type=="infantry"||e.type=="vehicle")q.play("unit_ready"),this.finishDeployingUnit(e)}}},powerOut:0,powerIn:0,lowPowerMode:!1,powerScale:4,checkPower:function(){var a=this.left,c=this.top+160,e=320,f=20;this.powerOut=0,this.powerIn=0;for(var g=d.buildings.length-1;g>=0;g--){var h=d.buildings[g];h.powerIn&&h.team==d.currentLevel.team&&(this.powerIn+=h.powerIn),h.powerOut&&h.team==d.currentLevel.team&&(this.powerOut+=h.powerOut)}var i="rgba(174,52,28,0.7)",j="rgba(250,100,0,0.6)",k="rgba(84,252,84,0.3)";this.powerOut/this.powerIn<1.1?this.powerOut/this.powerIn<1?this.powerOut<this.powerIn&&(b.fillStyle=i,this.lowPowerMode==0&&q.play("low_power"),this.lowPowerMode=!0):(b.fillStyle=j,this.lowPowerMode=!1):(b.fillStyle=k,this.lowPowerMode=!1),b.fillRect(a+8,c+e-this.powerOut/this.powerScale,f-14,this.powerOut/this.powerScale),b.drawImage(this.powerIndicator,a,c+e-this.powerIn/this.powerScale)},draw:function(){b.drawImage(this.tabsImage,0,this.top-this.tabsImage.height+2),b.fillStyle="lightgreen",b.font='12px "Command and Conquer"';var c=(this.cash+"").split("").join(" ");b.fillText(c,400-c.length*5/2,31),this.checkDependency(),this.textBrightness=this.textBrightness+this.textBrightnessDelta,this.textBrightness<0&&(this.textBrightness=1);for(var e=0;e<this.leftButtons.length;e++)this.processButton("left",e);for(var e=0;e<this.rightButtons.length;e++)this.processButton("right",e);if(this.visible){b.drawImage(this.sidebarImage,this.left,this.top),this.repairMode&&b.drawImage(this.repairButtonPressed,this.left+4,this.top+145),this.sellMode&&b.drawImage(this.sellButtonPressed,this.left+57,this.top+145),this.checkPower();var f=this.leftButtons.length>6?6:this.leftButtons.length;for(var e=0;e<f;e++)this.drawButton("left",e);var g=this.rightButtons.length>6?6:this.rightButtons.length;for(var e=0;e<g;e++)this.drawButton("right",e)}b.clearRect(0,d.viewportTop+d.viewportHeight,a.width,30)}},f={team:"nod",cash:0},g={types:[],buildingDetails:{"construction-yard":{name:"construction-yard",label:"Construction Yard",type:"building",powerIn:15,powerOut:30,cost:5e3,sight:3,hitPoints:400,imagesToLoad:[{name:"build",count:32},{name:"damaged",count:4},{name:"damaged-construct",count:20},{name:"healthy",count:4},{name:"healthy-construct",count:20},{name:"ultra-damaged",count:1}],gridShape:[[1,1,1],[1,1,1]]},refinery:{name:"refinery",label:"Tiberium Refinery",type:"building",powerIn:40,powerOut:10,cost:2e3,tiberiumStorage:1e3,sight:4,hitPoints:450,imagesToLoad:[{name:"build",count:20},{name:"damaged",count:12},{name:"damaged-unload",count:18},{name:"healthy",count:12},{name:"healthy-unload",count:18},{name:"ultra-damaged",count:1}],gridShape:[[1,1,1],[1,1,1],[1,1,1]]},barracks:{name:"barracks",label:"Barracks",type:"building",powerIn:20,cost:300,sight:3,hitPoints:400,imagesToLoad:[{name:"build",count:20},{name:"damaged",count:10},{name:"healthy",count:10},{name:"ultra-damaged",count:1}],gridShape:[[1,1],[1,1]]},"power-plant":{name:"power-plant",label:"Power Plant",type:"building",powerOut:100,cost:300,sight:2,hitPoints:200,imagesToLoad:[{name:"build",count:20},{name:"damaged",count:4},{name:"healthy",count:4},{name:"ultra-damaged",count:1}],gridShape:[[1,0],[1,1]]},"advanced-power-plant":{name:"advanced-power-plant",label:"Advanced Power Plant",type:"building",powerOut:200,cost:700,sight:2,hitPoints:300,imagesToLoad:[{name:"build",count:20},{name:"damaged",count:4},{name:"healthy",count:4},{name:"ultra-damaged",count:1}],gridShape:[[1,0],[1,1]]},"tiberium-silo":{name:"tiberium-silo",label:"Tiberium Silo",type:"building",powerIn:10,cost:150,sight:2,hitPoints:150,imagesToLoad:[{name:"build",count:20},{name:"damaged",count:5},{name:"healthy",count:5},{name:"ultra-damaged",count:1}],gridShape:[[1,1]]},"hand-of-nod":{name:"hand-of-nod",label:"Hand of Nod",type:"building",powerIn:20,cost:300,sight:3,hitPoints:400,imagesToLoad:[{name:"build",count:20},{name:"damaged",count:1},{name:"healthy",count:1},{name:"ultra-damaged",count:1}],gridShape:[[0,0],[1,1],[1,1]]},"weapons-factory":{name:"weapons-factory",label:"Weapons Factory",type:"building",powerIn:30,cost:2e3,sight:3,hitPoints:200,imagesToLoad:[{name:"build",count:20},{name:"damaged",count:1},{name:"damaged-base",count:1},{name:"damaged-construct",count:9},{name:"healthy",count:1},{name:"healthy-base",count:1},{name:"healthy-construct",count:9},{name:"ultra-damaged",count:0},{name:"ultra-damaged-base",count:1}],gridShape:[[1,1,1],[1,1,1],[1,1,1]]}},preloadImage:r,loadImageArray:s,preloadCount:0,loadedCount:0,draw:function(){var a=0;this.team!=d.currentLevel.team&&(a=this.pixelHeight),b.drawImage(this.bibImage,this.x*d.gridSize+d.viewportAdjustX,(this.y+this.gridHeight-1)*d.gridSize+d.viewportAdjustY);var c=this.getLife();this.status=="build"||this.status=="sell"?imageCategory="build":this.status==""||this.life=="ultra-damaged"?imageCategory=this.life:imageCategory=this.life+"-"+this.status;var g=this.gridShape[0].length*d.gridSize,h=this.spriteImage.height,i=this.spriteArray[this.life+"-base"];i&&this.status!="build"&&this.status!="sell"&&b.drawImage(this.spriteCanvas,i.offset*g,a,g,h,d.gridSize*this.x+d.viewportAdjustX,this.y*d.gridSize+d.viewportAdjustY,g,h);var j=this.spriteArray[imageCategory];this.animationIndex||(this.animationIndex=0);if(j.count>=Math.floor(this.animationIndex/this.animationSpeed)){var k=Math.floor(this.animationIndex/this.animationSpeed);this.status=="sell"&&(k=j.count-1-Math.floor(this.animationIndex/this.animationSpeed)),b.drawImage(this.spriteCanvas,(j.offset+k)*g,a,g,h,d.gridSize*this.x+d.viewportAdjustX,this.y*d.gridSize+d.viewportAdjustY,g,h)}this.animationIndex++;if(this.animationIndex/this.animationSpeed>=j.count){this.animationIndex=0;if(this.name!="refinery"||this.status!="build"&&this.status!="unload"){if(this.status=="build"||this.status=="construct"||this.status=="unload")this.status=""}else if(this.status=="build")d.units.push(l.add({name:"harvester",team:this.team,x:this.x+.5,y:this.y+2,moveDirection:14,orders:{type:"harvest",from:this}})),this.status="";else{if(this.harvester.tiberium){var m=this.harvester.tiberium>4?5:this.harvester.tiberium;this.team==d.currentLevel.team?e.cash+=m*50:f.cash+=m,this.harvester.tiberium-=m}this.harvester.tiberium||(d.units.push(l.add({name:"harvester",team:this.team,x:this.x+.5,y:this.y+2,health:this.harvester.health,moveDirection:14,orders:{type:"harvest",from:this,to:this.harvester.orders.from}})),this.harvester=null,this.status="")}this.status=="sell"&&(this.status="destroy")}this.drawSelection();if(this.repairing){b.globalAlpha=e.textBrightness,b.drawImage(e.repairImageBig,(this.x+this.gridShape[0].length/2-1)*d.gridSize+d.viewportAdjustX,(this.y+this.gridShape.length/2-1)*d.gridSize+d.viewportAdjustY),b.globalAlpha=1;if(this.health<this.hitPoints){var n=1;e.cash>n&&(e.cash-=n,this.health+=n*2*this.hitPoints/this.cost)}else this.repairing=!1,this.health=this.hitPoints}},load:function(a){var b=this.buildingDetails[a],c={};c.defaults={type:"building",draw:g.draw,underPoint:v,drawSelection:u,getLife:t,animationSpeed:2,status:"",health:b.hitPoints,gridHeight:b.gridShape.length,gridWidth:b.gridShape[0].length,pixelHeight:b.gridShape.length*d.gridSize,pixelWidth:b.gridShape[0].length*d.gridSize,bibImage:this.preloadImage("buildings/bib/bib-"+b.gridShape[0].length+".gif"),pixelOffsetX:0,pixelOffsetY:0,pixelTop:0,pixelLeft:0},this.loadSpriteSheet(c,b,"buildings"),$.extend(c,b),this.types[a]=c},loadSpriteSheet:h,add:function(a){var b={};b.team=d.currentLevel.team;var c=a.name;return $.extend(b,this.types[c].defaults),$.extend(b,this.types[c]),$.extend(b,a),b}},j={types:[],loaded:!0,infantryDetails:{minigunner:{name:"minigunner",label:"Minigunner",speed:8,cost:100,sight:1,hitPoints:50,collisionRadius:5,imagesToLoad:[{name:"stand",count:1,directionCount:8},{name:"walk",count:6,directionCount:8},{name:"fire",count:8,directionCount:8}]}},preloadImage:r,loadImageArray:s,preloadCount:0,loadedCount:0,collision:function(a){if(this==a)return!1;var b=Math.pow(this.x-a.x,2)+Math.pow(this.y-a.y,2),c=Math.pow((this.collisionRadius+a.collisionRadius)/d.gridSize,2);return b<=c},load:function(a){var b=this.infantryDetails[a],c={};c.defaults={type:"infantry",draw:this.draw,drawSelection:u,underPoint:v,collision:this.collision,move:this.move,getLife:t,status:"stand",animationSpeed:4,health:b.hitPoints,pixelOffsetX:-25,pixelOffsetY:-19.5,pixelWidth:16,pixelHeight:16,pixelTop:6,pixelLeft:16},c.imageArray=[];for(var d=b.imagesToLoad.length-1;d>=0;d--){var e=b.imagesToLoad[d].count,f=b.imagesToLoad[d].directionCount,g=b.imagesToLoad[d].name,h=[];for(var i=0;i<f;i++)h[i]=this.loadImageArray("units/infantry/"+a+"/"+a+"-"+g+"-"+i,e,".gif");c.imageArray[g]=h}$.extend(c,b),this.types[a]=c},draw:function(){var a=this.imageArray[this.status][this.moveDirection];this.animationIndex++,this.animationIndex/this.animationSpeed>=a.length&&(this.animationIndex=0);var b=a[Math.floor(this.animationIndex/this.animationSpeed)],c=this.x*d.gridSize+d.viewportAdjustX+this.pixelOffsetX,e=this.y*d.gridSize+d.viewportAdjustY+this.pixelOffsetY;drawSprite(b,c,e,this.team,this.type),this.drawSelection()},add:function(a){var b={},c=a.name;return b.moveDirection=0,b.animationIndex=0,b.team=d.currentLevel.team,$.extend(b,this.types[c].defaults),$.extend(b,this.types[c]),$.extend(b,a),b},move:function(){this.speedCounter||(this.speedCounter=0),this.speedCounter++;var a=this.moveDirection/8*2*Math.PI;this.status=="walk"&&(this.x=this.x-.005*this.speed*Math.sin(a),this.y=this.y-.005*this.speed*Math.cos(a)),this.speedCounter>=7&&(this.speedCounter=0,this.moveDirection=Math.floor(this.moveDirection+Math.round((Math.random()-.5)*10)*1/10),this.moveDirection>7?this.moveDirection=0:this.moveDirection<0&&(this.moveDirection=7),this.status=Math.random()>.7?"fire":Math.random()>.7?"stand":"walk")}},l={types:[],vehicleDetails:{mcv:{name:"mcv",label:"Mobile Construction Vehicle",type:"vehicle",turnSpeed:5,speed:12,cost:5e3,hitPoints:200,sight:2,moveImageCount:32,pixelWidth:48,pixelHeight:48,pixelOffsetX:-24,pixelOffsetY:-24,collisionRadius:12,softCollisionRadius:16,imagesToLoad:[{name:"move",count:32}]},harvester:{name:"harvester",label:"Harvester",type:"vehicle",turnSpeed:5,speed:12,cost:1400,hitPoints:600,sight:2,tiberium:0,moveImageCount:32,imagesToLoad:[{name:"move",count:32},{name:"harvest-00",count:4},{name:"harvest-04",count:4},{name:"harvest-08",count:4},{name:"harvest-12",count:4},{name:"harvest-16",count:4},{name:"harvest-20",count:4},{name:"harvest-24",count:4},{name:"harvest-28",count:4}],pixelWidth:48,pixelHeight:48,pixelOffsetX:-24,pixelOffsetY:-24,collisionRadius:6,softCollisionRadius:12},"light-tank":{name:"light-tank",label:"Light Tank",type:"vehicle",turnSpeed:5,speed:18,cost:600,sight:3,hitPoints:300,primaryWeapon:9,reloadTime:2e3,moveImageCount:32,turretImageCount:32,imagesToLoad:[{name:"move",count:32},{name:"turret",count:32}],pixelWidth:24,pixelHeight:24,pixelOffsetX:-12,pixelOffsetY:-12,collisionRadius:5,softCollisionRadius:9}},preloadImage:r,loadImageArray:s,preloadCount:0,loadedCount:0,collision:function(a){if(this==a)return!1;var b=Math.pow(this.x-a.x,2)+Math.pow(this.y-a.y,2),c=Math.pow((this.collisionRadius+a.collisionRadius)/d.gridSize,2),e=Math.pow((this.softCollisionRadius+a.collisionRadius)/d.gridSize,2),f=Math.pow((this.softCollisionRadius+a.softCollisionRadius)/d.gridSize,2);return b>c?b<e?{type:"soft-hard",distance:Math.pow(b,.5)}:b>f?!1:{type:"soft",distance:Math.pow(b,.5)}:{type:"hard",distance:Math.pow(b,.5)}},load:function(a){var b=this.vehicleDetails[a],c={};c.defaults={type:"vehicle",draw:this.draw,drawSelection:u,underPoint:v,processOrders:this.processOrders,moveTo:this.moveTo,move:this.move,collision:this.collision,getLife:t,animationSpeed:4,health:b.hitPoints,pixelLeft:0,pixelTop:0,pixelOffsetX:0,pixelOffsetY:0,moveDirection:0,turretDirection:0,status:""},this.loadSpriteSheet(c,b,"units/vehicles"),$.extend(c,b),this.types[a]=c},loadSpriteSheet:h,draw:function(){var a=this.pixelWidth,c=this.pixelHeight,e=Math.round(this.x*d.gridSize+this.pixelOffsetX+d.viewportAdjustX),f=Math.round(this.y*d.gridSize+this.pixelOffsetY+d.viewportAdjustY),g=0;this.team!=d.currentLevel.team&&(g=this.pixelHeight);if(this.status==""){var h=this.spriteArray.move,i=Math.floor(this.moveDirection);b.drawImage(this.spriteCanvas,(h.offset+i)*a,g,a,c,e,f,a,c)}else{this.animationIndex||(this.animationIndex=0);var h=this.spriteArray[this.status];if(h.count>=Math.floor(this.animationIndex/this.animationSpeed)){var i=Math.floor(this.animationIndex/this.animationSpeed);b.drawImage(this.spriteCanvas,(h.offset+i)*a,g,a,c,e,f,a,c)}this.animationIndex++,this.animationIndex/this.animationSpeed>=h.count&&(this.animationIndex=0,this.status.indexOf("harvest")>-1&&(this.tiberium||(this.tiberium=0),this.tiberium++,this.tiberium%5==0&&this.orders.to.stage--),this.status="")}if(this.turretDirection>=0){var j=this.spriteArray.turret;if(j){var i=Math.floor(this.turretDirection);b.drawImage(this.spriteCanvas,(j.offset+i)*a,g,a,c,e,f,a,c)}}this.drawSelection(),d.debugMode&&(b.fillStyle="white",b.fillText(this.orders.type,e,f),b.fillText(Math.floor(this.x)+","+Math.floor(this.y),e,f+10),this.orders.to&&b.fillText(this.orders.to.x+","+this.orders.to.y,e,f+20)),d.debugMode&&(b.fillStyle="rgba(100,200,100,0.4)",b.beginPath(),b.arc(this.x*d.gridSize+d.viewportAdjustX,this.y*d.gridSize+d.viewportAdjustY,this.softCollisionRadius,0,Math.PI*2),b.fill(),b.fillStyle="rgba(200,0,0,0.4)",b.beginPath(),b.arc(this.x*d.gridSize+d.viewportAdjustX,this.y*d.gridSize+d.viewportAdjustY,this.collisionRadius,0,Math.PI*2),b.fill())},movementSpeed:0,moveTo:function(a,b){var c=[Math.floor(this.x),Math.floor(this.y)],e=[a.x,a.y];this.path=z(c,e),this.instructions=[],this.path.length<=1&&Math.abs(this.x-a.x)<1&&Math.abs(this.y-a.y)<1&&(this.x!=e.x||this.y!=e.y)&&(this.path=[{x:c[0],y:c[1]},{x:e[0],y:e[1]}]);if(this.path.length>1){var f=F(this.path[1],this.path[0],32),g=this.movementSpeed*d.speedAdjustmentFactor/d.gridSize,h=this.moveDirection/32*2*Math.PI;this.x=this.x-g*Math.sin(h),this.y=this.y-g*Math.cos(h),this.colliding=!1;var i;for(var j=d.units.length-1;j>=0;j--)(i=this.collision(d.units[j]))&&i.distance<this.collisionDistance&&(this.collisionType=i.type,this.collisionDistance=i.distance,this.collisionWith=d.units[j],this.colliding=!0);for(var j=0;j<d.obstructionGrid.length;j++)for(var k=0;k<d.obstructionGrid[j].length;k++)if(d.obstructionGrid[j][k]>0){var l={x:k+.5,y:j+.5,collisionRadius:d.gridSize*.5,softCollisionRadius:d.gridSize*.7};(i=this.collision(l))&&i.distance<this.collisionDistance&&(this.collisionType=i.type,this.collisionDistance=i.distance,this.collisionWith=l,this.colliding=!0)}this.x=this.x+g*Math.sin(h),this.y=this.y+g*Math.cos(h);if(this.colliding){var m=F(this.collisionWith,this,32),n=D(this.moveDirection,m,32),o=D(f,m,32);switch(this.collisionType){case"hard":this.movementSpeed=0,Math.abs(n)==0?(Math.abs(o)>0?f=E(this.moveDirection,-1*o/Math.abs(o),32):f=E(this.moveDirection,-1,32),this.moveDirection=f):Math.abs(n)>2?Math.abs(n)<4?(f=E(this.moveDirection,-1*n/Math.abs(n),32),this.moveDirection=f):Math.abs(n)<9?(f=E(this.moveDirection,-n/Math.abs(n),32),this.moveDirection=f):this.movementSpeed=this.speed:(f=E(this.moveDirection,-1*n/Math.abs(n),32),this.moveDirection=f);break;case"soft-hard":Math.abs(n)==0?(this.movementSpeed=0,Math.abs(o)>0?f=E(this.moveDirection,-1*o/Math.abs(o),32):f=E(this.moveDirection,-1,32),this.moveDirection=f):Math.abs(n)>2?Math.abs(n)<4?(this.movementSpeed=0,f=E(this.moveDirection,-1*n/Math.abs(n),32),this.moveDirection=f):Math.abs(n)<9?(this.movementSpeed=0,f=E(this.moveDirection,-1*n/Math.abs(n),32),this.moveDirection=f):this.movementSpeed=this.speed:(this.movementSpeed=0,f=E(this.moveDirection,-1*n/Math.abs(n),32),this.moveDirection=f);break;case"soft":Math.abs(n)==0?(this.movementSpeed=this.speed*(this.collisionDistance-this.collisionRadius)/(this.softCollisionRadius-this.collisionRadius),this.movementSpeed<0&&(this.movementSpeed=0),Math.abs(o)>0?f=E(this.moveDirection,-1*o/Math.abs(o),32):f=E(this.moveDirection,-1,32)):Math.abs(n)>2?Math.abs(n)<4?(this.movementSpeed=this.speed*(this.collisionDistance-this.collisionRadius)/(this.softCollisionRadius-this.collisionRadius),this.movementSpeed<0&&(this.movementSpeed=0),f=E(this.moveDirection,-n*1,32)):Math.abs(n)<9?(this.movementSpeed=this.speed,f=E(this.moveDirection,-n*1,32)):this.movementSpeed=this.speed:(this.movementSpeed=this.speed*(this.collisionDistance-this.collisionRadius)/(this.softCollisionRadius-this.collisionRadius),this.movementSpeed<0&&(this.movementSpeed=0),f=E(this.moveDirection,-n*1,32))}}else this.movementSpeed=this.speed;this.movementSpeed>this.speed?this.movementSpeed=this.speed:this.movementSpeed<-this.speed&&(this.movementSpeed=-this.speed),this.moveDirection!=f&&this.instructions.push({type:"turn",toDirection:f});var p=Math.abs(D(this.moveDirection,f,32)),q;for(var j=0;j<d.obstructionGrid.length;j++)for(var k=0;k<d.obstructionGrid[j].length;k++)if(d.obstructionGrid[j][k]>0){var l={x:k+.5,y:j+.5,collisionRadius:d.gridSize*.5,softCollisionRadius:d.gridSize*.7};if(q=this.collision(l))break}(p<3||this.colliding)&&this.instructions.push({type:"move",distance:1});var r;b?r=F(a,this,32):r=F(this.path[1],this.path[0],32),this.turretDirection!=r&&this.instructions.push({type:"aim",toDirection:r})}},processOrders:function(){this.colliding=!1,this.collisionType="",this.collisionDistance=this.softCollisionRadius+1,this.collisionWith=null,this.movementSpeed=0,this.instructions=[],this.orders||(this.orders={type:"guard"});if(this.orders.type=="harvest"){this.orders.to||(this.orders.to=x(this));if(!this.orders.to){this.tiberium&&(this.orders={type:"harvest-return"});return}var a=Math.pow(Math.pow(this.orders.to.y+.5-this.y,2)+Math.pow(this.orders.to.x+.5-this.x,2),.5);if(a>1.5*this.softCollisionRadius/d.gridSize)this.moveTo(this.orders.to);else{if(this.tiberium&&this.tiberium>=14){this.orders={type:"harvest-return",to:this.orders.from,from:this.orders.to};return}this.orders.to.stage<1?this.orders.to=x(this):(!this.tiberium||this.tiberium<14)&&this.status==""&&(this.status="harvest-"+(Math.floor(this.moveDirection/4)*4<10?"0":"")+Math.floor(this.moveDirection/4)*4)}}else{if(this.orders.type=="harvest-return"){if(!this.orders.to){this.orders.to=w(this);if(!this.orders.to)return}var b={x:this.orders.to.x,y:this.orders.to.y+2},a=Math.pow(Math.pow(b.y-this.y,2)+Math.pow(b.x-this.x,2),.5);if(a>3*this.softCollisionRadius/d.gridSize)this.moveTo(b);else if(this.orders.to.life!="ultra-damaged"){if(this.tiberium==0){this.orders={type:"harvest",to:this.orders.from,from:this.orders.to};return}if(this.moveDirection!=14){this.instructions.push({type:"turn",toDirection:14});return}this.orders.to.status==""&&(this.status="destroy",this.orders.to.harvester=this,this.orders.to.status="unload",this.orders.to.animationIndex=0)}return}if(this.orders.type=="make-way")Math.abs(collDirection)>16?this.instructions.push({type:"move",distance:.25}):this.instructions.push({type:"move",distance:-0.25}),this.movementSpeed=this.speed,this.orders={type:"guard"};else if(this.orders.type=="move"){this.moveTo(this.orders.to);var a=Math.pow(Math.pow(this.orders.to.y+.5-this.y,2)+Math.pow(this.orders.to.x+.5-this.x,2),.5),c=this.softCollisionRadius/d.gridSize<.5?.5+this.softCollisionRadius/d.gridSize:this.softCollisionRadius/d.gridSize;if(a<=c||this.colliding&&this.collisionType=="soft"&&a<=c+this.collisionRadius/d.gridSize||this.colliding&&this.collisionType=="soft-hard"&&a<=c+2*this.collisionRadius/d.gridSize||this.colliding&&this.collisionType=="hard"&&a<=c+3*this.collisionRadius/d.gridSize)this.orders={type:"guard"}}else if(this.orders.type=="patrol"){var e=y(this,2);if(e.length>0){var f=e[0];this.orders={type:"attack",target:f,lastOrders:this.orders};return}this.moveTo(this.orders.to);var a=Math.pow(Math.pow(this.orders.to.y-this.y,2)+Math.pow(this.orders.to.x-this.x,2),.5);a<4*this.softCollisionRadius/d.gridSize&&(this.orders={type:"patrol",to:this.orders.from,from:this.orders.to})}else if(this.orders.type=="protect"||this.orders.type=="attack"){if(this.orders.target.status=="destroy"){var e=y(this,2);if(e.length>0){var f=e[0];this.orders={type:"attack",target:f,lastOrders:this.orders};return}this.orders.lastOrders?this.orders=this.orders.lastOrders:this.orders={type:"guard"};return}if(this.orders.type=="protect"){var e=y(this,2);if(e.length>0){var f=e[0];this.orders={type:"attack",target:f,lastOrders:this.orders};return}}var h=this.orders.target.x,i=this.orders.target.y,j=this.orders.target.x,k=this.orders.target.y;this.orders.target.type=="turret"&&(h+=this.orders.target.pixelWidth/(2*d.gridSize),i+=this.orders.target.pixelHeight/(2*d.gridSize),j=h,k=i),this.orders.target.type=="building"&&(h+=this.orders.target.gridWidth/2,i+=this.orders.target.gridHeight,j=h,k+=this.orders.target.gridHeight/2),Math.pow(h-this.x,2)+Math.pow(i-this.y,2)>Math.pow(this.sight-1,2)&&this.moveTo({x:Math.floor(h),y:Math.floor(i)},!0);if(Math.pow(h-this.x,2)+Math.pow(i-this.y,2)<=Math.pow(this.sight,2)&&this.orders.type=="attack"){var l=F({x:j,y:k},this,32);this.turretDirection==l?this.instructions.push({type:"fire"}):this.instructions.push({type:"aim",toDirection:l})}}else if(this.orders.type=="build")this.moveDirection!=15?this.instructions.push({type:"turn",toDirection:15}):(this.status="destroy",q.play("construction"),d.buildings.push(g.add({name:"construction-yard",x:Math.floor(this.x)-1,y:Math.floor(this.y)-1,status:"build"})));else if(this.orders.type=="guard"){var e=y(this,2);if(this.primaryWeapon&&e.length>0){var f=e[0];this.orders={type:"attack",target:f}}}}},move:function(){this.moving=!1,this.attacking=!1,this.instructions||(this.instructions=[]);if(this.instructions.length==0)return;for(var a=0;a<this.instructions.length;a++){var b=this.instructions[a];b.type=="turn"&&(b.toDirection==this.moveDirection&&(b.type="done"),b.toDirection>this.moveDirection&&b.toDirection-this.moveDirection<16||b.toDirection<this.moveDirection&&this.moveDirection-b.toDirection>16?(this.moveDirection=this.moveDirection+this.turnSpeed*.1,(this.moveDirection-b.toDirection)*(this.moveDirection+this.turnSpeed*.1-b.toDirection)<=0&&(this.moveDirection=b.toDirection)):(this.moveDirection=this.moveDirection-this.turnSpeed*.1,(this.moveDirection-b.toDirection)*(this.moveDirection-this.turnSpeed*.1-b.toDirection)<=0&&(this.moveDirection=b.toDirection)),this.moveDirection>31?this.moveDirection=0:this.moveDirection<0&&(this.moveDirection=31));if(b.type=="move"){if(b.distance<=0){b.type="done";return}this.moving=!0;var c=this.movementSpeed*d.speedAdjustmentFactor/d.gridSize;b.distance-=c;var e=this.moveDirection/32*2*Math.PI;this.x=this.x-c*Math.sin(e),this.y=this.y-c*Math.cos(e)}if(b.type=="aim")if(b.toDirection==this.turretDirection)b.type="done";else{var f=D(Math.floor(this.turretDirection),Math.floor(b.toDirection),32);Math.abs(f)<1?(this.turretDirection=b.toDirection,b.type="done"):this.turretDirection=E(this.turretDirection,f/Math.abs(f),32)}if(b.type=="fire"&&!this.bulletFiring){q.play("tank_fire"),this.bulletFiring=!0;var e=this.turretDirection/32*2*Math.PI;d.fireBullet({x:this.x,y:this.y,angle:e,range:this.sight,source:this})}}},add:function(a){var b={},c=a.name;return b.team=d.currentLevel.team,$.extend(b,this.types[c].defaults),$.extend(b,this.types[c]),$.extend(b,a),b}},m={types:[],turretDetails:{"gun-turret":{name:"gun-turret",label:"Gun Turret",type:"turret",powerIn:20,primaryWeapon:12,cost:600,hitPoints:200,sight:5,turnSpeed:5,reloadTime:1500,pixelWidth:24,pixelHeight:24,imagesToLoad:[{name:"build",count:20},{name:"damaged",count:32},{name:"healthy",count:32}],pixelOffsetX:-12,pixelOffsetY:-12,pixelTop:12,pixelLeft:12,gridShape:[[1]]},"guard-tower":{name:"guard-tower",label:"Guard Tower",type:"turret",powerIn:10,primaryWeapon:1,cost:500,hitPoints:200,sight:5,reloadTime:1e3,pixelWidth:24,pixelHeight:24,pixelOffsetX:-12,pixelOffsetY:-12,pixelTop:12,pixelLeft:12,imagesToLoad:[{name:"build",count:20},{name:"damaged",count:1},{name:"healthy",count:1}],gridShape:[[1,1]]}},preloadImage:r,loadImageArray:s,preloadCount:0,loadedCount:0,load:function(a){var b=this.turretDetails[a],c={};c.defaults={type:"turret",status:"",draw:this.draw,drawSelection:u,processOrders:this.processOrders,underPoint:v,move:this.move,getLife:t,animationSpeed:4,health:b.hitPoints,pixelLeft:0,pixelTop:0,pixelOffsetX:0,pixelOffsetY:0,turretDirection:0},this.loadSpriteSheet(c,b,"turrets"),$.extend(c,b),this.types[a]=c},loadSpriteSheet:h,draw:function(){var a=this.getLife(),c=0;this.team!=d.currentLevel.team&&(c=this.pixelHeight),this.status=="build"||this.status=="sell"?imageCategory="build":this.status==""&&(imageCategory=this.life,this.life=="ultra-damaged"&&(imageCategory="damaged"));var e=this.spriteArray[imageCategory],f=this.gridShape[0].length*d.gridSize,g=this.spriteImage.height,h=this.x*d.gridSize+d.viewportAdjustX,i=this.y*d.gridSize+d.viewportAdjustY;if(this.status==""){var j=Math.floor(this.turretDirection);b.drawImage(this.spriteCanvas,(e.offset+j)*f,c,f,g,h,i,f,g)}else{this.animationIndex||(this.animationIndex=0);if(e.count>=Math.floor(this.animationIndex/this.animationSpeed)){var j=Math.floor(this.animationIndex/this.animationSpeed);this.status=="sell"&&(j=e.count-1-Math.floor(this.animationIndex/this.animationSpeed)),b.drawImage(this.spriteCanvas,(e.offset+j)*f,c,f,g,h,i,f,g)}this.animationIndex++,this.animationIndex/this.animationSpeed>=e.count&&(this.animationIndex=0,this.status="",this.status=="sell"&&(this.status="destroy"))}if(this.turretDirection>=0){var k=this.spriteArray.turret;if(k){var j=Math.floor(this.turretDirection);b.drawImage(this.spriteImage,(k.offset+j)*f,c,f,g,h,i,f,g)}}this.drawSelection()},processOrders:function(){this.orders||(this.orders={type:"guard"});if(this.orders.type=="attack"){this.instructions=[];if(this.orders.target.status=="destroy"){this.orders={type:"guard"};return}var a=[Math.floor(this.x),Math.floor(this.y)],b=this.orders.target.x,c=this.orders.target.y;this.orders.target.type=="turret"&&(b+=this.orders.target.pixelWidth/(2*d.gridSize),c+=this.orders.target.pixelHeight/(2*d.gridSize)),this.orders.target.type=="building"&&(b+=this.orders.target.gridWidth/2,c+=this.orders.target.gridHeight/2);if(Math.pow(b-this.x,2)+Math.pow(c-this.y,2)<Math.pow(this.sight,2)){if(this.orders.type=="attack"){var e=F({x:b,y:c},this,32);this.turretDirection!=e?this.instructions.push({type:"aim",toDirection:e}):this.instructions.push({type:"fire"})}}else this.orders={type:"guard"}}else if(this.orders.type=="guard"){var f=y(this,0);if(f.length>0){var g=f[0];this.orders={type:"attack",target:g}}}},move:function(){this.instructions||(this.instructions=[]);if(this.instructions.length==0)return;for(var a=0;a<this.instructions.length;a++){var b=this.instructions[a];b.type=="aim"&&(b.toDirection==this.turretDirection&&(b.type="done"),b.toDirection>this.turretDirection&&b.toDirection-this.turretDirection<16||b.toDirection<this.turretDirection&&this.turretDirection-b.toDirection>16?(this.turretDirection=this.turretDirection+this.turnSpeed*.1,(this.turretDirection-b.toDirection)*(this.turretDirection+this.turnSpeed*.1-b.toDirection)<=0&&(this.turretDirection=b.toDirection)):(this.turretDirection=this.turretDirection-this.turnSpeed*.1,(this.turretDirection-b.toDirection)*(this.turretDirection-this.turnSpeed*.1-b.toDirection)<=0&&(this.turretDirection=b.toDirection)),this.turretDirection>31?this.turretDirection=0:this.turretDirection<0&&(this.turretDirection=31));if(b.type=="fire"&&!this.bulletFiring){q.play("tank_fire"),this.bulletFiring=!0;var c=this.turretDirection/32*2*Math.PI;d.fireBullet({x:this.x+.5,y:this.y+.5,angle:c,range:this.sight,source:this,damage:10})}}},add:function(a){var b={},c=a.name;return b.team=d.currentLevel.team,$.extend(b,this.types[c].defaults),$.extend(b,this.types[c]),$.extend(b,a),b}},n={types:[],load:function(a){var b={name:a}},add:function(a){$.extend(a,this.types[name])}},o={types:[],overlayDetails:{tiberium:{name:"tiberium",count:2,pixelWidth:24,pixelHeight:24,stageCount:12,gridOffsetX:0,gridOffsetY:0,imagesToLoad:[{name:"0",count:12},{name:"1",count:12}]},tree:{name:"tree",count:1,stageCount:10,pixelWidth:48,pixelHeight:48,gridOffsetX:0,gridOffsetY:-1,imagesToLoad:[{name:"0",count:10},{name:"1",count:10},{name:"2",count:10}]},trees:{name:"trees",count:1,stageCount:10,gridOffsetX:0,gridOffsetY:-1,pixelWidth:72,pixelHeight:48,imagesToLoad:[{name:"0",count:10}]}},loadSpriteSheet:h,load:function(a){var b={name:a,draw:this.draw},c=this.overlayDetails[a];this.loadSpriteSheet(b,c,"tiles/temperate"),$.extend(b,c),this.types[a]=b},draw:function(){var a=this.pixelWidth,c=this.pixelHeight,e=Math.round((this.x+this.gridOffsetX)*d.gridSize+d.viewportAdjustX),f=Math.round((this.y+this.gridOffsetY)*d.gridSize+d.viewportAdjustY),g=this.spriteArray[this.type],h=this.stage;b.drawImage(this.spriteCanvas,(g.offset+h)*a,0,a,c,e,f,a,c);return},loadAll:function(){this.load("tiberium"),this.load("tree"),this.load("trees")},add:function(a){var b={type:0,stage:0},c=a.name;return $.extend(b,this.types[c]),$.extend(b,a),b},loaded:!0,preloadCount:0,loadedCount:0,preloadImage:r,loadImageArray:s},p={levelDetails:{gdi1:{mapUrl:"maps/gdi/map01.jpeg",startingCash:5e3,terrain:[{x1:0,y1:27,x2:30,y2:30,type:"water"},{x1:0,y1:26,x2:6,y2:26,type:"water"},{x1:0,y1:25,x2:5,y2:25,type:"water"},{x1:0,y1:24,x2:4,y2:24,type:"water"},{x1:29,y1:17,x2:30,y2:22,type:"mountain"},{x1:7,y1:6,x2:8,y2:9,type:"mountain"},{x1:8,y1:10,x2:9,y2:11,type:"mountain"},{x1:9,y1:11,x2:10,y2:15,type:"mountain"},{x1:10,y1:15,x2:11,y2:19,type:"mountain"},{x1:11,y1:19,x2:12,y2:21,type:"mountain"},{x1:12,y1:21,x2:14,y2:23,type:"mountain"},{x1:12,y1:24,x2:13,y2:24,type:"mountain"},{x1:14,y1:21,x2:17,y2:22,type:"mountain"},{x1:16,y1:23,x2:16,y2:23,type:"mountain"}],overlay:[{x:10,y:10,name:"tree"},{x:16,y:3,name:"tree"},{x:14,y:2,name:"trees"},{x:9,y:2,name:"trees"},{x:19,y:12,name:"trees"},{x:15,y:13,name:"trees"},{x:0,y:1,name:"trees"},{x:2,y:1,name:"trees"},{x:4,y:1,name:"trees"},{x:8,y:1,name:"tree"},{x:6,y:0,name:"tree"},{x:7,y:0,name:"tree"},{x:28,y:11,name:"tiberium",stage:9},{x:29,y:11,name:"tiberium",stage:7},{x:28,y:12,name:"tiberium",stage:9},{x:29,y:12,name:"tiberium",stage:5},{x:28,y:13,name:"tiberium",stage:10},{x:29,y:13,name:"tiberium",stage:4},{x:28,y:14,name:"tiberium",stage:8},{x:29,y:14,name:"tiberium",stage:6},{x:28,y:15,name:"tiberium",stage:3},{x:27,y:15,name:"tiberium",stage:11},{x:27,y:14,name:"tiberium",stage:1},{x:27,y:13,name:"tiberium",stage:5},{x:13,y:16,name:"tiberium",stage:1},{x:14,y:16,name:"tiberium",stage:5},{x:15,y:17,name:"tiberium",stage:8},{x:14,y:17,name:"tiberium",stage:3},{x:16,y:17,name:"tiberium",stage:6}],gridWidth:32,gridHeight:32,team:"gdi",briefing:"This is a warning \n for all of you \n Kill enemy troops and have some fun",items:{infantry:[],buildings:["construction-yard","power-plant","refinery","weapons-factory"
,"advanced-power-plant","tiberium-silo","hand-of-nod"],vehicles:["mcv","light-tank","harvester"],ships:["bigboat"],turrets:["gun-turret"]},scriptedEvents:[{id:"trigger1",description:"Initial four reinforcement troops land on beach",actions:[{action:"wait",tigger:"time",time:100},{action:"sound",sound:"reinforcements_have_arrived"},{action:"addUnit",unit:{name:"hovercraft",type:"vehicle",unselectable:!0,id:"hovercraft1",x:30,y:30,direction:"up",carrying:[{name:"gunner"}]}},{action:"move",id:"hovercraft1",x:30,y:27},{action:"unload",id:"hovercraft1",x:30,y:28},{action:"move",id:"hovercraft1",x:30,y:30},{action:"removeUnit",id:"hovercraft1"}]},{id:"trigger2",description:"Blow up enemy powerplant when the time comes",actions:[{action:"wait",trigger:"condition",condition:function(){return!0}},{action:"sound",sound:"low_power"},{action:"destroyBuilding",id:"powerplant1"}]},{id:"wintrigger",actions:[{action:"wait",trigger:"condition",condition:function(){return units.enemyUnitCount()==0&&g.enemyBuildingsCount==0}},{action:"endLevel",type:"success"}]}]}},preloadImage:r,loaded:!0,preloadCount:0,loadedCount:0,load:function(a){var b={};b.id=a,b.mapImage=this.preloadImage(this.levelDetails[a].mapUrl);var c=this.levelDetails[a];for(item in c.items){if(item=="vehicles")for(var d=c.items[item].length-1;d>=0;d--)l.load(this.levelDetails[a].items[item][d]);if(item=="buildings")for(var d=c.items[item].length-1;d>=0;d--)g.load(c.items[item][d]);if(item=="infantry")for(var d=c.items[item].length-1;d>=0;d--)j.load(c.items[item][d]);if(item=="turrets")for(var d=c.items[item].length-1;d>=0;d--)m.load(c.items[item][d])}var f=new Array,h=new Array;for(var i=0;i<c.gridHeight;i++){f[i]=new Array,h[i]=new Array;for(var k=0;k<c.gridWidth;k++)f[i][k]=0}for(var d=c.terrain.length-1;d>=0;d--){var n=c.terrain[d];for(var k=n.x1;k<=n.x2;k++)for(var i=n.y1;i<=n.y2;i++)f[i][k]=1,h[i][k]=n.type}var p=[];for(var d=c.overlay.length-1;d>=0;d--)p.push(o.add(c.overlay[d]));return b.mapGrid=h,b.obstructionGrid=f,b.overlay=p,e.cash=c.startingCash,b.team=c.team,b}},q={sound_list:[],loaded:!0,load:function(a,b){var c=new Audio("audio/"+b+"/"+a+".ogg");return c.load(),c},play:function(a,b){var c=this.sound_list[a];if(c.length==1)c[0].play();else{var d=Math.floor(c.length*Math.random());c[d].play()}},loadAll:function(){this.sound_list.building_in_progress=[this.load("building_in_progress","voice")],this.sound_list.insufficient_funds=[this.load("insufficient_funds","voice")],this.sound_list.building=[this.load("building","voice")],this.sound_list.on_hold=[this.load("on_hold","voice")],this.sound_list.cancelled=[this.load("cancelled","voice")],this.sound_list.cannot_deploy_here=[this.load("cannot_deploy_here","voice")],this.sound_list.new_construction_options=[this.load("new_construction_options","voice")],this.sound_list.construction_complete=[this.load("construction_complete","voice")],this.sound_list.not_ready=[this.load("not_ready","voice")],this.sound_list.low_power=[this.load("low_power","voice")],this.sound_list.unit_ready=[this.load("unit_ready","voice")],this.sound_list.mission_accomplished=[this.load("mission_accomplished","voice")],this.sound_list.mission_failure=[this.load("mission_failure","voice")],this.sound_list.construction=[this.load("construction","sounds")],this.sound_list.crumble=[this.load("crumble","sounds")],this.sound_list.sell=[this.load("sell","sounds")],this.sound_list.button=[this.load("button","sounds")],this.sound_list.machine_gun=[this.load("machine_gun-0","sounds"),this.load("machine_gun-1","sounds")],this.sound_list.tank_fire=[this.load("tank-fire-0","sounds"),this.load("tank-fire-1","sounds"),this.load("tank-fire-2","sounds"),this.load("tank-fire-3","sounds")],this.sound_list.vehicle_select=[this.load("ready_and_waiting","talk"),this.load("vehicle_reporting","talk"),this.load("awaiting_orders","talk")],this.sound_list.vehicle_move=[this.load("affirmative","talk"),this.load("moving_out","talk"),this.load("acknowledged","talk"),this.load("over_and_out","talk")],this.sound_list.infantry_select=[this.load("reporting","talk"),this.load("unit_reporting","talk"),this.load("awaiting_orders","talk")],this.sound_list.infantry_move=[this.load("affirmative","talk"),this.load("yes_sir","talk"),this.load("acknowledged","talk"),this.load("right_away","talk")]}},A={fogCanvas:document.createElement("canvas"),canvasWidth:128,canvasHeight:128,init:function(){this.fogContext=this.fogCanvas.getContext("2d"),this.fogContext.fillStyle="rgba(0,0,0,1)",this.fogContext.fillRect(0,0,this.canvasWidth,this.canvasHeight)},draw:function(){var a=this.fogCanvas,c=this.fogContext,e=d.currentLevel.mapImage;c.save(),c.scale(this.canvasWidth/e.width,this.canvasHeight/e.height),c.fillStyle="rgba(200,200,200,1)";for(var f=d.units.length-1;f>=0;f--){var g=d.units[f];if(g.team==d.currentLevel.team||g.bulletFiring)c.beginPath(),c.globalCompositeOperation="destination-out",c.arc((Math.floor(g.x)+.5)*d.gridSize,(Math.floor(g.y)+.5)*d.gridSize,(g.sight+.5)*d.gridSize,0,2*Math.PI,!1),c.fill()}for(var f=d.buildings.length-1;f>=0;f--){var h=d.buildings[f];h.team==d.currentLevel.team&&(c.beginPath(),c.globalCompositeOperation="destination-out",c.arc(Math.floor(h.x)*d.gridSize+h.pixelWidth/2,Math.floor(h.y)*d.gridSize+h.pixelHeight/2,h.sight*d.gridSize,0,2*Math.PI,!1),c.fill())}for(var f=d.turrets.length-1;f>=0;f--){var i=d.turrets[f];if(i.team==d.currentLevel.team||i.bulletFiring)c.beginPath(),c.globalCompositeOperation="destination-out",c.arc(Math.floor(i.x)*d.gridSize+i.pixelWidth/2,Math.floor(i.y)*d.gridSize+i.pixelHeight/2,i.sight*d.gridSize,0,2*Math.PI,!1),c.fill()}c.restore(),b.drawImage(this.fogCanvas,0+d.viewportX*this.canvasWidth/e.width,0+d.viewportY*this.canvasHeight/e.height,d.viewportWidth*this.canvasWidth/e.width,d.viewportHeight*this.canvasHeight/e.height,d.viewportLeft,d.viewportTop,d.viewportWidth,d.viewportHeight)}},B=document.createElement("canvas"),C=B.getContext("2d"),H=function(){function a(a,b,c,d,e,f,g,h,i,j,k,l,m){return a&&(c&&!i[e][g]&&(l[m++]={x:g,y:e}),d&&!i[e][h]&&(l[m++]={x:h,y:e})),b&&(c&&!i[f][g]&&(l[m++]={x:g,y:f}),d&&!i[f][h]&&(l[m++]={x:h,y:f})),l}function b(a,b,c,d,e,f,g,h,i,j,k,l,m){return a=e>-1,b=f<j,c=g<k,d=h>-1,c&&(a&&!i[e][g]&&(l[m++]={x:g,y:e}),b&&!i[f][g]&&(l[m++]={x:g,y:f})),d&&(a&&!i[e][h]&&(l[m++]={x:h,y:e}),b&&!i[f][h]&&(l[m++]={x:h,y:f})),l}function c(a,b,c,d,e,f,g,h,i,j,k,l,m){return l}function d(a,b,c,d,e,f){var g=c-1,h=c+1,i=b+1,j=b-1,k=g>-1&&!d[g][b],l=h<e&&!d[h][b],m=i<f&&!d[c][i],n=j>-1&&!d[c][j],o=[],p=0;return k&&(o[p++]={x:b,y:g}),m&&(o[p++]={x:i,y:c}),l&&(o[p++]={x:b,y:h}),n&&(o[p++]={x:j,y:c}),a(k,l,m,n,g,h,i,j,d,e,f,o,p)}function e(a,b,c,d){return d(c(a.x-b.x),c(a.y-b.y))}function f(a,b,c,d){var e=a.x-b.x,f=a.y-b.y;return d(e*e+f*f)}function g(a,b,c,d){return c(a.x-b.x)+c(a.y-b.y)}function h(h,i,j,k){var l=h[0].length,m=h.length,n=l*m,o=Math.abs,p=Math.max,q={},r=[],s=[{x:i[0],y:i[1],f:0,g:0,v:i[0]+i[1]*l}],t=1,u,v,w,x,y,z,A,B,C;j={x:j[0],y:j[1],v:j[0]+j[1]*l};switch(k){case"Diagonal":w=a;case"DiagonalFree":v=e;break;case"Euclidean":w=a;case"EuclideanFree":p=Math.sqrt,v=f;break;default:v=g,w=c}w||(w=b);do{z=n,A=0;for(x=0;x<t;++x)(k=s[x].f)<z&&(z=k,A=x);B=s.splice(A,1)[0];if(B.v!=j.v){--t,C=d(w,B.x,B.y,h,m,l);for(x=0,y=C.length;x<y;++x)(u=C[x]).p=B,u.f=u.g=0,u.v=u.x+u.y*l,u.v in q||(u.f=(u.g=B.g+v(u,B,o,p))+v(u,j,o,p),s[t++]=u,q[u.v]=1)}else{x=t=0;do r[x++]={x:B.x,y:B.y};while(B=B.p);r.reverse()}}while(t);return r}return h}();d.start(),$("#debugger").toggle(),$("#debug_mode").bind("change",function(){d.debugMode=!d.debugMode,$("#debugger").toggle()})})