$(function(){function p(a){var b=this;this.loaded=false;var c=new Image;c.src="images/"+a;this.preloadCount++;$(c).bind("load",function(){b.loadedCount++;if(b.loadedCount==b.preloadCount){b.loaded=true}});return c}function q(a,b,c){if(!c){c=".png"}var d=[];for(var e=0;e<b;e++){d.push(this.preloadImage(a+"-"+e+c))}return d}function r(){var a=this.health/this.hitPoints;if(a>.7){this.life="healthy"}else if(a>.4){this.life="damaged"}else{this.life="ultra-damaged"}}function s(){if(this.selected){b.strokeStyle="white";var a=5;var c=this.x*d.gridSize+d.viewportAdjustX+this.pixelOffsetX;var f=this.y*d.gridSize+d.viewportAdjustY+this.pixelOffsetY;var g=c+this.pixelLeft;var h=f+this.pixelTop;var i=g+this.pixelWidth;var 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);if(this.life=="healthy"){b.fillStyle="lightgreen"}else if(this.life=="damaged"){b.fillStyle="yellow"}else{b.fillStyle="red"}b.fill();b.beginPath();b.strokeStyle="black";b.rect(g,h-a-2,this.pixelWidth,a);b.stroke();if(this.primaryBuilding){b.drawImage(e.primaryBuildingImage,(g+i-e.primaryBuildingImage.width)/2,j-e.primaryBuildingImage.height)}}}function t(a,b){var c=this.x*d.gridSize+this.pixelOffsetX;var e=this.y*d.gridSize+this.pixelOffsetY;var f=c+this.pixelLeft;var g=e+this.pixelTop;var h=f+this.pixelWidth;var i=g+this.pixelHeight;if(a>=f&&a<=h&&b>=g&&b<=i){return true}return false}function u(a,b){if(!b)b=0;var c=[];if(!a){a=this}for(var e=d.units.length-1;e>=0;e--){var f=d.units[e];if(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];if(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];if(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 v(a,c){var e=d.obstructionGrid;try{e[c[1]][c[0]]=0;e[a[1]][a[0]]=0}catch(f){return[]}var g=E(e,a,c,"Euclidean");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 z(a,c,d,e,f){var g=a.width;var h=a.height;y.clearRect(0,0,g+1,h+1);y.drawImage(a,0,0);var i=y.getImageData(0,0,g,h);var j=i.data.length/4;for(var k=0;k<j;k++){var l=i.data[k*4];var m=i.data[k*4+1];var n=i.data[k*4+2];var o=i.data[k*4+2];if(e=="nod"){var p=[];if(f=="turret"||f=="building"||f=="mcv"||f=="harvester"){p=[{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]}]}else if(f=="vehicle"||f=="infantry"){i.data[k*4+0]=(l+m+n)/3;i.data[k*4+1]=(l+m+n)/3;i.data[k*4+2]=(l+m+n)/3}for(var q=p.length-1;q>=0;q--){if(l==p[q].gdi[0]&&m==p[q].gdi[1]&&n==p[q].gdi[2]){i.data[k*4+0]=p[q].nod[0];i.data[k*4+1]=p[q].nod[1];i.data[k*4+2]=p[q].nod[2]}}}if(m==255&&n<100){i.data[k*4]=0;i.data[k*4+1]=0;i.data[k*4+2]=0}}y.globalCompositeOperation="source-over";y.putImageData(i,0,0);y.globalCompositeOperation="destination-atop";y.drawImage(a,0,0);b.drawImage(x,0,0,g,h,c,d,g,h)}function A(a,b,c){a=Math.floor(a);b=Math.floor(b);if(a>=c/2){a=a-c}if(b>=c/2){b=b-c}diff=b-a;if(diff<-c/2){diff+=c}if(diff>c/2){diff-=c}return diff}function B(a,b,c){a=Math.round(a)+b;if(a>c-1){a-=c}if(a<0){a+=c}return a}function C(a,b,c){if(!c){c=32}if(!b){b=this}var d=a.y-b.y;var e=a.x-b.x;if(b.type=="turret"){d=d-.5;e=e-.5}var f=c/2+Math.round(Math.atan2(e,d)*c/(2*Math.PI));if(f<0){f+=c}if(f>=c){f-=c}return f}function D(a,b){var c=true;var 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);var g=.2*(e.y-d.y)/Math.abs(e.y-d.y);var h=g;var i={x:d.x+h*f,y:d.y+h};while(c&&Math.abs(i.y-e.y)>.3){if(b[Math.floor(i.y)][Math.floor(i.x)]>0){c=false}h+=g;i={x:d.x+h*f,y:d.y+h}}}else{var f=(e.y-d.y)/(e.x-d.x);var j=.2*(e.x-d.x)/Math.abs(e.x-d.x);var k=j;var i={x:d.x+k,y:d.y+f*k};while(c&&Math.abs(i.x-e.x)>=.3){if(b[Math.floor(i.y)][Math.floor(i.x)]>0){c=false}k+=j;i={x:d.x+k,y:d.y+f*k}}}if(c){a.splice(1,1)}}}var a=$("#canvas")[0];var b=a.getContext("2d");var c={x:0,y:0,gridX:0,gridY:0,gameX:0,gameY:0,insideCanvas:false,panDirection:"",panningThreshold:48,panningVelocity:24,handlePanning:function(){var a="";if(c.insideCanvas){if(c.y<=d.viewportTop+c.panningThreshold&&c.y>=d.viewportTop){d.viewportDeltaY=-c.panningVelocity;a+="_top"}else if(c.y>=d.viewportTop+d.viewportHeight-c.panningThreshold&&c.y<=d.viewportTop+d.viewportHeight){d.viewportDeltaY=c.panningVelocity;a+="_bottom"}else{d.viewportDeltaY=0;a+=""}if(c.x<c.panningThreshold&&c.y>=d.viewportTop&&c.y<=d.viewportTop+d.viewportHeight){d.viewportDeltaX=-c.panningVelocity;a+="_left"}else if(c.x>d.screenWidth-c.panningThreshold&&c.y>=d.viewportTop&&c.y<=d.viewportTop+d.viewportHeight){d.viewportDeltaX=c.panningVelocity;a+="_right"}else{d.viewportDeltaX=0;a+=""}}if(d.viewportX+d.viewportDeltaX<0||d.viewportX+d.viewportDeltaX+d.screenWidth>d.currentLevel.mapImage.width){d.viewportDeltaX=0}if(d.viewportY+d.viewportDeltaY<0||d.viewportY+d.viewportDeltaY+d.viewportHeight>d.currentLevel.mapImage.height){d.viewportDeltaY=0}if(a!=""){if(d.viewportDeltaX==0&&d.viewportDeltaY==0){a="no_pan"+a}else{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++;if(this.cursorLoop>=this.cursor.cursorSpeed*this.cursor.images.length){this.cursorLoop=0}if(this.dragSelect){var a=Math.min(this.gameX,this.dragX);var c=Math.min(this.gameY,this.dragY);var e=Math.abs(this.gameX-this.dragX);var f=Math.abs(this.gameY-this.dragY);b.strokeStyle="white";b.strokeRect(a+d.viewportAdjustX,c+d.viewportAdjustY,e,f)}var g=this.cursor.images[Math.floor(this.cursorLoop/this.cursor.cursorSpeed)];b.drawImage(g,this.x-this.cursor.x,this.y-this.cursor.y)},checkOverObject:function(){this.overObject=null;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}}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}}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){}else if(e.deployMode){var b=f.types[e.deployBuilding];var g=$.extend([],b.gridShape);g.push(g[g.length-1]);for(var h=0;h<g.length;h++){for(var i=0;i<g[h].length;i++){if(g[h][i]==1){if(d.obstructionGrid[c.gridY+h][c.gridX+i]==1){d.highlightGrid(c.gridX+i,c.gridY+h,1,1,e.placementRedImage)}else{d.highlightGrid(c.gridX+i,c.gridY+h,1,1,e.placementWhiteImage)}}}}}else if(e.repairMode){if(a&&a.team==d.currentLevel.team&&(a.type=="building"||a.type=="turret")&&a.life<a.hitPoints){this.cursor=this.cursors["repair"]}else{this.cursor=this.cursors["no_repair"]}}else if(e.sellMode){if(a&&a.team==d.currentLevel.team&&(a.type=="building"||a.type=="turret")){this.cursor=this.cursors["sell"]}else{this.cursor=this.cursors["no_sell"]}}else if(e.visible&&c.x>e.left){}else if(this.dragSelect){this.cursor=this.cursors["default"]}else if(a){if(a.team!=d.currentLevel.team&&d.selectedAttackers.length>0){this.cursor=this.cursors["attack"]}else if(a.selected){if(a.name=="mcv"){this.cursor=this.cursors["build_command"]}}else{this.cursor=this.cursors["select"]}}else if(this.panDirection&&this.panDirection!=""){this.cursor=this.cursors[this.panDirection]}else if(d.selectedAttackers.length>0){if(d.obstructionGrid[c.gridY]&&d.obstructionGrid[c.gridY][c.gridX]==1){this.cursor=this.cursors["no_move"]}else{this.cursor=this.cursors["move"]}}if(this.insideCanvas){this.drawCursor()}},click:function(a,b){if(c.y<=d.viewportTop&&c.y>d.viewportTop-15){if(c.x>=0&&c.x<160){}else if(c.x>=320&&c.x<480){}else if(c.x>=480&&c.x<640){e.visible=!e.visible}}else if(c.y>=d.viewportTop&&c.y<=d.viewportTop+d.viewportHeight){if(e.visible&&c.x>e.left){e.click(a,b)}else{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=true}}else{c.dragSelect=false}});$("#canvas").click(function(a){c.click(a,false);c.dragSelect=false;return false});$("#canvas").mousedown(function(a){if(a.which==1){c.buttonPressed=true;c.dragX=c.gameX;c.dragY=c.gameY;a.preventDefault()}return false});$("#canvas").bind("contextmenu",function(a){c.click(a,true);return false});$("#canvas").mouseup(function(a){if(a.which==1){if(c.dragSelect){if(!a.shiftKey){d.clearSelection()}var b=Math.min(c.gameX,c.dragX);var e=Math.min(c.gameY,c.dragY);var f=Math.max(c.gameX,c.dragX);var g=Math.max(c.gameY,c.dragY);for(var h=d.units.length-1;h>=0;h--){var i=d.units[h];if(!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=false}return false});$("#canvas").mouseleave(function(a){c.insideCanvas=false});$("#canvas").mouseenter(function(a){c.buttonPressed=false;c.insideCanvas=true})},loaded:false,preloadCount:0,loadedCount:0,preloadImage:p,loadImageArray:q,cursors:{},loadCursor:function(a,b,c,d,e){if(!b&&!c){b=0;c=0}if(!e){e=1}var f;if(d){f=this.loadImageArray("cursors/"+a,d,".gif")}else{f=[this.preloadImage("cursors/"+a+".gif")]}this.cursors[a]={x:b,y:c,name:a,images:f,cursorSpeed:e}},loadAllCursors:function(){c.loadCursor("default");c.loadCursor("no_default");c.loadCursor("move",15,12);c.loadCursor("no_move",15,12);c.loadCursor("pan_top",15,0);c.loadCursor("pan_top_right",30,0);c.loadCursor("pan_right",30,12);c.loadCursor("pan_bottom_right",30,24);c.loadCursor("pan_bottom",15,24);c.loadCursor("pan_bottom_left",0,24);c.loadCursor("pan_left",0,12);c.loadCursor("pan_top_left",0,0);c.loadCursor("no_pan_top",15,0);c.loadCursor("no_pan_top_right",30,0);c.loadCursor("no_pan_right",30,12);c.loadCursor("no_pan_bottom_right",30,24);c.loadCursor("no_pan_bottom",15,24);c.loadCursor("no_pan_bottom_left",0,24);c.loadCursor("no_pan_left",0,12);c.loadCursor("no_pan_top_left",0,0);c.loadCursor("no_repair",15,0);c.loadCursor("no_sell",15,12);c.loadCursor("build_command",15,12,8);c.loadCursor("sell",15,12,24);c.loadCursor("repair",15,0,24);c.loadCursor("attack",15,12,8);c.loadCursor("big_detonate",15,12,3);c.loadCursor("detonate",15,12,3);c.loadCursor("load_vehicle",15,12,3);c.loadCursor("select",15,12,6,2)}};var 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:false,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++){if(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){if(h.orders&&h.orders.type!="guard"){break}}}for(var f=this.overlay.length-1;f>=0;f--){var i=this.overlay[f];if(i.name=="tree"){d.obstructionGrid[i.y][i.x]=1}else if(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;if(g&&$(g).is("img")){b.drawImage(g,a*h+d.viewportAdjustX,c*h+d.viewportAdjustY,e*h,f*h)}else{if(g){b.fillStyle=g}else{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;var c=d.currentLevel.mapImage.width;var e=d.currentLevel.mapImage.height;var f=d.viewportX;var g=d.viewportY;var h=c/a;var 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--){if(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=false},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;var 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}}if(g){c.dead=true;g.health=g.health-Math.floor((c.damage?c.damage:10)+10*Math.random());if(g.health<0){g.status="destroy"}}b.fillStyle="red";b.fillRect(e+d.viewportAdjustX,f+d.viewportAdjustY,2,2)}if(c.range<=0){this.bullets.splice(a,1)}}},drawObjects:function(){var a=[];for(var b=this.buildings.length-1;b>=0;b--){if(this.buildings[b].status=="destroy"){this.buildings.splice(b,1)}}for(var b=this.units.length-1;b>=0;b--){if(this.units[b].status=="destroy"){this.units.splice(b,1)}}for(var b=this.turrets.length-1;b>=0;b--){if(this.turrets[b].status=="destroy"){this.turrets.splice(b,1)}}$.merge(a,this.units);$.merge(a,this.buildings);$.merge(a,this.overlay);$.merge(a,this.turrets);a.sort(function(a,b){return b.y-a.y});for(var b=a.length-1;b>=0;b--){a[b].draw()}},moveObjects:function(){for(var a=this.units.length-1;a>=0;a--){if(this.units[a].processOrders){this.units[a].processOrders()}this.units[a].move()}for(var a=this.turrets.length-1;a>=0;a--){if(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){if(typeof c=="object"){b+="<li>"+key+" : ";if(c instanceof HTMLImageElement){b+=c.src.replace(/^.+images\//,"")}else if(c instanceof Array){b+="Array["+c.length+"]"}else{b+="Object"}}else{b+="<li>"+key+" : "+c+"</li>"}}}}b+="</ul>";return b};var b="";b+="Level";b+=a(n);b+="Mouse";b+=a(c);if(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(h);b+="Buildings";b+=a(f);b+="Infantry";b+=a(g);$("#debugger").html(b)},animate:function(){if(d.debugMode){d.showDebugger()}if(!n.loaded||!e.loaded||!h.loaded||!g.loaded||!f.loaded){b.clearRect(0,0,a.width,a.height);return}b.save();e.draw();d.setViewport();d.drawMap();if(d.debugMode){d.drawGrid()}d.moveObjects();d.drawObjects();d.drawBullets();if(!d.debugMode){w.draw()}b.restore();d.drawMessage();c.draw()},messageVisible:true,messageHeadingVisible:true,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);if(!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=true;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];if(item.team==d.currentLevel.team){a.push(item)}else{f.push(item)}}for(var h=d.buildings.length-1;h>=0;h--){item=d.buildings[h];if(item.team==d.currentLevel.team){b.push(item)}else{e.push(item)}}for(var h=d.turrets.length-1;h>=0;h--){item=d.turrets[h];if(item.team==d.currentLevel.team){c.push(item)}else{g.push(item)}}if(a.length==0&&b.length==0){o.play("mission_failure");d.end()}if(g.length==0&&e.length==0&&f.length==0){o.play("mission_accomplished");d.end()}},selectedItems:[],selectedAttackers:[],clearSelection:function(){for(var a=this.selectedItems.length-1;a>=0;a--){this.selectedItems[a].selected=0;this.selectedItems.splice(a,1)}this.selectedAttackers=[]},selectItem:function(a){a.selected=true;this.selectedItems.push(a);if(a.type!="building"&&a.team==d.currentLevel.team){this.selectedAttackers.push(a);o.play(a.type+"_select")}},click:function(a,b){if(d.messageVisible){if(c.x>=290&&c.x<=350&&c.y>=310&&c.y<=325){d.messageVisible=false;return}}var f=c.checkOverObject();if(b){this.clearSelection();e.repairMode=false;e.deployMode=false;e.sellMode=false;return}if(e.repairMode){if(f&&f.team==d.currentLevel.team&&(f.type=="building"||f.type=="turret")&&f.life<f.hitPoints){}}else if(e.deployMode){e.finishDeployingBuilding()}else if(e.sellMode){if(f&&f.team==d.currentLevel.team&&(f.type=="building"||f.type=="turret")){f.status="sell";o.play("sell");e.cash+=f.cost/2}}else if(!b&&!c.dragSelect){if(f){if(d.selectedAttackers.length==1&&f.selected){if(f.name=="mcv"){this.clearSelection();f.orders={type:"build"}}}else if(f.team==d.currentLevel.team){if(!a.shiftKey){this.clearSelection()}this.selectItem(f)}else if(d.selectedAttackers.length>0){for(var g=d.selectedAttackers.length-1;g>=0;g--){if(d.selectedAttackers[g].primaryWeapon){d.selectedAttackers[g].orders={type:"attack",target:f};o.play(d.selectedAttackers[g].type+"_move")}}}else{if(!a.shiftKey){this.clearSelection()}this.selectItem(f)}}else{if(d.selectedAttackers.length>0){if(d.obstructionGrid[c.gridY]&&d.obstructionGrid[c.gridY][c.gridX]==1){}else{for(var g=d.selectedAttackers.length-1;g>=0;g--){d.selectedAttackers[g].orders={type:"move",to:{x:c.gridX,y:c.gridY}};o.play(d.selectedAttackers[g].type+"_move")}}}}}},start:function(){c.loadAllCursors();o.loadAll();m.loadAll();this.currentLevel=n.load("gdi1");this.overlay=this.currentLevel.overlay;e.load();c.listenEvents();w.init();d.viewportX=96;d.viewportY=264;e.visible=false;this.turrets.push(j.add({name:"gun-turret",x:8,y:6,turretDirection:16,team:"nod"}));this.turrets.push(j.add({name:"gun-turret",x:9,y:3,turretDirection:16,team:"nod"}));this.turrets.push(j.add({name:"gun-turret",x:7,y:5,turretDirection:16,team:"nod"}));this.turrets.push(j.add({name:"gun-turret",x:8,y:2,turretDirection:16,team:"nod"}));this.turrets.push(j.add({name:"gun-turret",x:16,y:25,turretDirection:24,team:"nod"}));this.turrets.push(j.add({name:"gun-turret",x:13,y:26,turretDirection:24,team:"nod"}));this.turrets.push(j.add({name:"gun-turret",x:11,y:23,turretDirection:18,team:"nod"}));this.turrets.push(j.add({name:"gun-turret",x:10,y:24,turretDirection:20,team:"nod"}));this.turrets.push(j.add({name:"gun-turret",x:9,y:25,turretDirection:24,team:"nod"}));this.buildings.push(f.add({name:"construction-yard",x:1,y:14,team:"nod"}));this.units.push(h.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(h.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(h.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(h.add({name:"light-tank",x:2,y:2,team:"nod",orders:{type:"patrol",from:{x:25,y:5},to:{x:17,y:25}}}));this.units.push(h.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(h.add({name:"light-tank",x:2,y:10,team:"nod",orders:{type:"protect",target:d.units[0]}}));this.units.push(h.add({name:"mcv",x:23,y:23,moveDirection:0}));this.units.push(h.add({name:"light-tank",x:22,y:25,moveDirection:0}));this.units.push(h.add({name:"light-tank",x:24,y:25,moveDirection:0}));this.animationLoop=setInterval(this.animate,this.animationTimeout);this.statusLoop=setInterval(d.missionStatus,3e3)},end:function(){clearInterval(this.statusLoop);e.visible=false;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. ")}};var e={loaded:true,preloadCount:0,loadedCount:0,preloadImage:p,tabsImage:null,width:160,visible:true,cash:0,finishDeployingBuilding:function(){for(var a=0;a<d.buildings.length;a++){if(d.buildings[a].name=="construction-yard"){d.buildings[a].status="construct";break}}d.buildings.push(f.add({name:e.deployBuilding,x:c.gridX,y:c.gridY,status:"build"}));o.play("construction");e.deployMode=false;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(g.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";d.units.push(h.add({name:a.name,x:b.x+1,y:b.y+2.5,moveDirection:16,turretDirection:16,orders:{type:"move",to:{x:Math.floor(b.x+3*Math.random()),y:Math.floor(b.y+3)}}}))}for(var c=this.rightButtons.length-1;c>=0;c--){if(this.rightButtons[c].dependency[0]==a.dependency[0]){this.rightButtons[c].status=""}}e.deployBuilding=null},click:function(a,b){var d=c.y-this.top;var f=c.x;if(d>=146&&d<=160){if(f>=485&&f<=530){this.repairMode=!this.repairMode;this.sellMode=this.mapMode=this.deployMode=false}else if(f>=538&&f<=582){this.sellMode=!this.sellMode;this.repairMode=this.mapMode=this.deployMode=false}else if(f>=590&&f<=635){this.mapMode=!this.mapMode;this.repairMode=this.sellMode=this.deployMode=false}}else if(d>=455&&d<=480){if(f>=500&&f<=530){if(this.leftButtonOffset>0){this.leftButtonOffset--;o.play("button")}}else if(f>=532&&f<=562){if(this.leftButtonOffset+6<this.leftButtons.length){this.leftButtonOffset++;o.play("button")}}else if(f>=570&&f<=600){if(this.rightButtonOffset>0){this.rightButtonOffset--;o.play("button")}}else if(f>=602&&f<=632){if(this.rightButtonOffset+6<this.rightButtons.length){this.rightButtonOffset++;o.play("button")}}}else 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;if(f>=500&&f<=564){i="left";j=this.leftButtonOffset+g;k=this.leftButtons}else if(f>=570&&f<=634){i="right";j=this.rightButtonOffset+g;k=this.rightButtons}if(k&&k.length>j){var l=k[j];if(l.status==""&&!b){if(l.cost<=e.cash){for(var h=k.length-1;h>=0;h--){if(k[h].dependency[0]==l.dependency[0]){k[h].status="disabled"}}l.status="building";l.counter=0;l.spent=l.cost;o.play("building")}else{o.play("insufficient_funds")}}else if(l.status=="building"&&!b){o.play("not_ready")}else if(l.status=="building"&&b){l.status="hold";o.play("on_hold")}else if(l.status=="hold"&&!b){l.status="building";o.play("building")}else if(l.status=="hold"&&b){l.status="";o.play("cancelled");for(var h=k.length-1;h>=0;h--){k[h].status=""}}else if(l.status=="ready"&&!b){if(l.type=="building"){e.deployMode=true;this.repairMode=this.sellMode=this.mapMode=false;e.deployBuilding=l.name}}else if(l.status=="disabled"){o.play("building_in_progress")}}}},allButtons:[],leftButtons:[],rightButtons:[],checkDependency:function(){for(var a=0;a<this.allButtons.length;a++){var b=this.allButtons[a];var c=true;for(var f=b.dependency.length-1;f>=0;f--){var g=false;var 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=true;break}}if(!g){c=false;break}}if(b.type=="building"){var k=false;var l;for(var f=this.leftButtons.length-1;f>=0;f--){if(this.leftButtons[f].name==b.name){k=true;l=f;break}else{}}if(c&&!k){this.leftButtons.push(b);b.status="";b.counter=0;b.speed=this.buildSpeedMultiplier/b.cost;o.play("new_construction_options");e.visible=true}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)}}else if(b.type=="infantry"||b.type=="vehicle"){var k=false;var l;for(var f=this.rightButtons.length-1;f>=0;f--){if(this.rightButtons[f].name==b.name){k=true;l=f;break}}if(c&&!k){this.rightButtons.push(b);b.status="";b.counter=0;b.speed=this.buildSpeedMultiplier/b.cost;o.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--){if(this.rightButtons[f].dependency[0]==this.rightButtons[l].dependency[0])this.rightButtons[f].status=""}}this.rightButtons.splice(l,1)}}}},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.top=d.viewportTop-2;this.left=a.width-this.width;var b=[{name:"power-plant",type:"building",cost:300,dependency:["construction-yard"]},{name:"barracks",type:"building",cost:300,dependency:["construction-yard","power-plant"]},{name:"weapons-factory",type:"building",cost:300,dependency:["construction-yard","power-plant"]},{name:"minigunner",type:"infantry",cost:100,dependency:["barracks"]},{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:-.14,drawButtonLabel:function(a,c,d){var e=this.iconWidth/2-a.width/2;var 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;var 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;var e=a=="left"?this.leftButtonOffset:this.rightButtonOffset;var f=d[c+e];var g=a=="left"?500:570;var h=165+this.top+c*this.iconHeight;b.drawImage(f.image,g,h);if(f.status=="ready"){this.drawButtonLabel(this.readyImage,g,h)}else if(f.status=="disabled"){b.fillStyle="rgba(200,200,200,0.6)";b.fillRect(g,h,this.iconWidth,this.iconHeight)}else if(f.status=="building"){y.clearRect(0,0,this.iconWidth,this.iconHeight);y.fillStyle="rgba(200,200,200,0.6)";y.beginPath();y.moveTo(this.iconWidth/2,this.iconHeight/2);y.arc(this.iconWidth/2,this.iconHeight/2,40,Math.PI*2*f.counter/100-Math.PI/2,-Math.PI/2);y.moveTo(this.iconWidth/2,this.iconHeight/2);y.fill();b.drawImage(x,0,0,this.iconWidth,this.iconHeight,g,h,this.iconWidth,this.iconHeight)}else if(f.status=="hold"){y.clearRect(0,0,this.iconWidth,this.iconHeight);y.fillStyle="rgba(100,100,100,0.6)";y.beginPath();y.moveTo(this.iconWidth/2,this.iconHeight/2);y.arc(this.iconWidth/2,this.iconHeight/2,40,Math.PI*2*f.counter/100-Math.PI/2,-Math.PI/2);y.moveTo(this.iconWidth/2,this.iconHeight/2);y.fill();b.drawImage(x,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;var d=a=="left"?this.leftButtonOffset:this.rightButtonOffset;var e=c[b+d];var f=a=="left"?500:570;var 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"){o.play("construction_complete")}else{if(e.type=="infantry"||e.type=="vehicle"){o.play("unit_ready");this.finishDeployingUnit(e)}}}}},powerOut:0,powerIn:0,lowPowerMode:false,powerScale:4,checkPower:function(){var a=this.left;var c=this.top+160;var e=320;var f=20;this.powerOut=0;this.powerIn=0;for(var g=d.buildings.length-1;g>=0;g--){var h=d.buildings[g];if(h.powerIn&&h.team==d.currentLevel.team){this.powerIn+=h.powerIn}if(h.powerOut&&h.team==d.currentLevel.team){this.powerOut+=h.powerOut}}var i="rgba(174,52,28,0.7)";var j="rgba(250,100,0,0.6)";var k="rgba(84,252,84,0.3)";if(this.powerOut/this.powerIn>=1.1){b.fillStyle=k;this.lowPowerMode=false}else if(this.powerOut/this.powerIn>=1){b.fillStyle=j;this.lowPowerMode=false}else if(this.powerOut<this.powerIn){b.fillStyle=i;if(this.lowPowerMode==false){o.play("low_power")}this.lowPowerMode=true}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;if(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.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)}};var f={types:[],buildingDetails:{"construction-yard":{name:"construction-yard",label:"Construction Yard",powerIn:15,powerOut:30,cost:5e3,sight:3,hitPoints:400,imagesToLoad:[{name:"build",count:32},{name:"healthy",count:4},{name:"damaged",count:4},{name:"ultra-damaged",count:1},{name:"healthy-construct",count:20},{name:"damaged-construct",count:20}],gridShape:[[1,1,1],[1,1,1]]},barracks:{name:"barracks",label:"Power Plant",powerIn:20,cost:300,sight:3,hitPoints:400,imagesToLoad:[{name:"build",count:20},{name:"healthy",count:10},{name:"damaged",count:10},{name:"ultra-damaged",count:1}],gridShape:[[1,1],[1,1]]},"power-plant":{name:"power-plant",label:"Power Plant",powerOut:100,cost:300,sight:2,hitPoints:200,imagesToLoad:[{name:"build",count:20},{name:"healthy",count:4},{name:"damaged",count:4}
,{name:"ultra-damaged",count:1}],gridShape:[[1,0],[1,1]]},"weapons-factory":{name:"weapons-factory",label:"Weapons Factory",powerIn:30,cost:2e3,sight:3,hitPoints:200,imagesToLoad:[{name:"build",count:20},{name:"healthy",count:1},{name:"damaged",count:1},{name:"ultra-damaged",count:0},{name:"healthy-construct",count:9},{name:"damaged-construct",count:9},{name:"healthy-base",count:1},{name:"damaged-base",count:1},{name:"ultra-damaged-base",count:1}],gridShape:[[1,1,1],[1,1,1],[1,1,1]]}},preloadImage:p,loadImageArray:q,preloadCount:0,loadedCount:0,draw:function(){b.drawImage(this.bibImage,this.x*d.gridSize+d.viewportAdjustX,(this.y+this.gridHeight-1)*d.gridSize+d.viewportAdjustY);var a=this.getLife();if(this.status=="build"||this.status=="sell"){imageCategory="build"}else if(this.status==""){imageCategory=this.life}else{imageCategory=this.life+"-"+this.status}var c=this.imageArray[this.life+"-base"];if(c&&c.length>0&&this.status!="build"&&this.status!="sell"){z(c[0],d.gridSize*this.x+d.viewportAdjustX,this.y*d.gridSize+d.viewportAdjustY,this.team,this.type)}var e=this.imageArray[imageCategory];if(!this.animationIndex){this.animationIndex=0}if(e.length>Math.floor(this.animationIndex/this.animationSpeed)){var f=e[Math.floor(this.animationIndex/this.animationSpeed)];if(this.status=="sell"){f=e[e.length-1-Math.floor(this.animationIndex/this.animationSpeed)]}this.currentImage=f;z(f,this.x*d.gridSize+d.viewportAdjustX,this.y*d.gridSize+d.viewportAdjustY,this.team,this.type)}this.animationIndex++;if(this.animationIndex/this.animationSpeed>=e.length){this.animationIndex=0;if(this.status=="build"||this.status=="construct"){this.status=""}else if(this.status=="sell"){this.status="destroy"}}this.drawSelection()},load:function(a){var b=this.buildingDetails[a];var c={};c.defaults={type:"building",draw:f.draw,underPoint:t,drawSelection:s,getLife:r,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};c.imageArray=[];for(var e=b.imagesToLoad.length-1;e>=0;e--){var g=b.imagesToLoad[e].count;var h=b.imagesToLoad[e].name;c.imageArray[h]=this.loadImageArray("buildings/"+a+"/"+a+"-"+h,g,".gif")}$.extend(c,b);this.types[a]=c},add:function(a){var b={};b.team=d.currentLevel.team;var c=a.name;$.extend(b,this.types[c].defaults);$.extend(b,this.types[c]);$.extend(b,a);return b}};var g={types:[],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:p,loadImageArray:q,preloadCount:0,loadedCount:0,collision:function(a){if(this==a){return false}var b=Math.pow(this.x-a.x,2)+Math.pow(this.y-a.y,2);var c=Math.pow((this.collisionRadius+a.collisionRadius)/d.gridSize,2);return b<=c},load:function(a){var b=this.infantryDetails[a];var c={};c.defaults={type:"infantry",draw:this.draw,drawSelection:s,underPoint:t,collision:this.collision,move:this.move,getLife:r,status:"stand",animationSpeed:4,health:b.hitPoints,pixelOffsetX:-50/2,pixelOffsetY:-39/2,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;var f=b.imagesToLoad[d].directionCount;var g=b.imagesToLoad[d].name;var 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++;if(this.animationIndex/this.animationSpeed>=a.length){this.animationIndex=0}var b=a[Math.floor(this.animationIndex/this.animationSpeed)];var c=this.x*d.gridSize+d.viewportAdjustX+this.pixelOffsetX;var e=this.y*d.gridSize+d.viewportAdjustY+this.pixelOffsetY;z(b,c,e,this.team,this.type);this.drawSelection()},add:function(a){var b={};var c=a.name;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);return b},move:function(){if(!this.speedCounter){this.speedCounter=0}this.speedCounter++;var a=this.moveDirection/8*2*Math.PI;if(this.status=="walk"){this.x=this.x-.005*this.speed*Math.sin(a);this.y=this.y-.005*this.speed*Math.cos(a)}if(this.speedCounter>=7){this.speedCounter=0;this.moveDirection=Math.floor(this.moveDirection+Math.round((Math.random()-.5)*10)*1/10);if(this.moveDirection>7){this.moveDirection=0}else if(this.moveDirection<0){this.moveDirection=7}this.status=Math.random()>.7?"fire":Math.random()>.7?"stand":"walk"}}};var h={types:[],vehicleDetails:{mcv:{name:"mcv",label:"Mobile Construction Vehicle",turnSpeed:5,speed:12,cost:5e3,hitPoints:200,sight:2,moveImageCount:32,pixelWidth:48,pixelHeight:48,pixelOffsetX:-24,pixelOffsetY:-24,collisionRadius:14,softCollisionRadius:18},"light-tank":{name:"light-tank",label:"Light Tank",turnSpeed:5,speed:18,cost:600,sight:3,hitPoints:300,primaryWeapon:9,reloadTime:2e3,moveImageCount:32,turretImageCount:32,pixelWidth:24,pixelHeight:24,pixelOffsetX:-12,pixelOffsetY:-12,collisionRadius:8,softCollisionRadius:11},jeep:{name:"jeep",label:"Hum-Vee",turnSpeed:10,speed:30,cost:400,sight:2,hitPoints:150,primaryWeapon:16,moveImageCount:32,turretImageCount:32,pixelWidth:24,pixelHeight:24,pixelOffsetX:-12,pixelOffsetY:-12,collisionRadius:6}},preloadImage:p,loadImageArray:q,preloadCount:0,loadedCount:0,collision:function(a){if(this==a){return false}var b=Math.pow(this.x-a.x,2)+Math.pow(this.y-a.y,2);var c=Math.pow((this.collisionRadius+a.collisionRadius)/d.gridSize,2);var e=Math.pow((this.softCollisionRadius+a.collisionRadius)/d.gridSize,2);var f=Math.pow((this.softCollisionRadius+a.softCollisionRadius)/d.gridSize,2);if(b<=c){return{type:"hard",distance:Math.pow(b,.5)}}else if(b<e){return{type:"soft-hard",distance:Math.pow(b,.5)}}else if(b<=f){return{type:"soft",distance:Math.pow(b,.5)}}else{return false}},load:function(a){var b=this.vehicleDetails[a];var c={};c.defaults={type:"vehicle",draw:this.draw,drawSelection:s,underPoint:t,processOrders:this.processOrders,moveTo:this.moveTo,move:this.move,collision:this.collision,getLife:r,animationSpeed:4,health:b.hitPoints,pixelLeft:0,pixelTop:0,pixelOffsetX:0,pixelOffsetY:0,moveDirection:0,turretDirection:0};if(!b.moveImages&&b.moveImageCount){c.moveImages=this.loadImageArray("units/vehicles/"+b.name+"/"+b.name,b.moveImageCount,".gif")}if(!b.turretImages&&b.turretImageCount){c.turretImages=this.loadImageArray("units/vehicles/"+b.name+"/"+b.name+"-turret",b.turretImageCount,".gif")}$.extend(c,b);this.types[a]=c},draw:function(){var a=this.moveImages[Math.floor(this.moveDirection)];var c=this.x*d.gridSize+this.pixelOffsetX+d.viewportAdjustX;var e=this.y*d.gridSize+this.pixelOffsetY+d.viewportAdjustY;z(a,c,e,this.team,this.name=="mcv"?"mcv":this.type);if(this.turretImageCount){var f=this.turretImages[Math.floor(this.turretDirection)];b.drawImage;z(f,c,e,this.team,this.type)}this.drawSelection();if(d.debugMode){b.beginPath();b.arc(this.x*d.gridSize+d.viewportAdjustX,this.y*d.gridSize+d.viewportAdjustY,this.collisionRadius,0,Math.PI*2);b.stroke()}},movementSpeed:0,moveTo:function(a,b){var c=[Math.floor(this.x),Math.floor(this.y)];var e=[a.x,a.y];this.path=v(c,e);this.instructions=[];if(this.path.length>1){var f=C(this.path[1],this.path[0],32);var g=this.movementSpeed*d.speedAdjustmentFactor/d.gridSize;var 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=false;var i;for(var j=d.units.length-1;j>=0;j--){if(i=this.collision(d.units[j])){if(i.distance<this.collisionDistance){this.collisionType=i.type;this.collisionDistance=i.distance;this.collisionWith=d.units[j];this.colliding=true}}}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(i=this.collision(l)){if(i.distance<this.collisionDistance){this.collisionType=i.type;this.collisionDistance=i.distance;this.collisionWith=l;this.colliding=true}}}}}this.x=this.x+g*Math.sin(h);this.y=this.y+g*Math.cos(h);if(this.colliding){var m=C(this.collisionWith,this,32);var n=A(this.moveDirection,m,32);var o=A(f,m,32);switch(this.collisionType){case"hard":this.movementSpeed=0;if(Math.abs(n)==0){if(Math.abs(o)>0){f=B(this.moveDirection,-1*o/Math.abs(o),32)}else{f=B(this.moveDirection,-1,32)}this.moveDirection=f}else if(Math.abs(n)<=2){f=B(this.moveDirection,-1*n/Math.abs(n),32);this.moveDirection=f}else if(Math.abs(n)<4){f=B(this.moveDirection,-1*n/Math.abs(n),32);this.moveDirection=f}else if(Math.abs(n)<9){f=B(this.moveDirection,-n/Math.abs(n),32);this.moveDirection=f}else{this.movementSpeed=this.speed}break;case"soft-hard":if(Math.abs(n)==0){this.movementSpeed=0;if(Math.abs(o)>0){f=B(this.moveDirection,-1*o/Math.abs(o),32)}else{f=B(this.moveDirection,-1,32)}this.moveDirection=f}else if(Math.abs(n)<=2){this.movementSpeed=0;f=B(this.moveDirection,-1*n/Math.abs(n),32);this.moveDirection=f}else if(Math.abs(n)<4){this.movementSpeed=0;f=B(this.moveDirection,-1*n/Math.abs(n),32);this.moveDirection=f}else if(Math.abs(n)<9){this.movementSpeed=0;f=B(this.moveDirection,-1*n/Math.abs(n),32);this.moveDirection=f}else{this.movementSpeed=this.speed}break;case"soft":if(Math.abs(n)==0){this.movementSpeed=this.speed*(this.collisionDistance-this.collisionRadius)/(this.softCollisionRadius-this.collisionRadius);if(this.movementSpeed<0){this.movementSpeed=0}if(Math.abs(o)>0){f=B(this.moveDirection,-1*o/Math.abs(o),32)}else{f=B(this.moveDirection,-1,32)}}else if(Math.abs(n)<=2){this.movementSpeed=this.speed*(this.collisionDistance-this.collisionRadius)/(this.softCollisionRadius-this.collisionRadius);if(this.movementSpeed<0){this.movementSpeed=0}f=B(this.moveDirection,-n*1,32)}else if(Math.abs(n)<4){this.movementSpeed=this.speed*(this.collisionDistance-this.collisionRadius)/(this.softCollisionRadius-this.collisionRadius);if(this.movementSpeed<0){this.movementSpeed=0}f=B(this.moveDirection,-n*1,32)}else if(Math.abs(n)<9){this.movementSpeed=this.speed;f=B(this.moveDirection,-n*1,32)}else{this.movementSpeed=this.speed}break}}else{this.movementSpeed=this.speed}if(this.movementSpeed>this.speed){this.movementSpeed=this.speed}else if(this.movementSpeed<-this.speed){this.movementSpeed=-this.speed}if(this.moveDirection!=f){this.instructions.push({type:"turn",toDirection:f})}var p=Math.abs(A(this.moveDirection,f,32));if(p<8||this.colliding){this.instructions.push({type:"move",distance:1})}var q;if(b){q=C(a,this,32)}else{q=C(this.path[1],this.path[0],32)}if(this.turretDirection!=q){this.instructions.push({type:"aim",toDirection:q})}}},processOrders:function(){this.colliding=false;this.collisionType="";this.collisionDistance=this.softCollisionRadius+1;this.collisionWith=null;this.movementSpeed=0;this.instructions=[];if(!this.orders){this.orders={type:"guard"}}if(this.orders.type=="make-way"){if(Math.abs(collDirection)>16){this.instructions.push({type:"move",distance:.25})}else{this.instructions.push({type:"move",distance:-.25})}this.movementSpeed=this.speed;this.orders={type:"guard"}}else if(this.orders.type=="move"){this.moveTo(this.orders.to);if(this.path.length<=1||this.colliding&&this.collisionType=="soft"&&this.path.length<=2||this.colliding&&this.collisionType=="soft-hard"&&this.path.length<=2||this.colliding&&this.collisionType=="hard"&&this.path.length<=2){this.orders={type:"guard"}}}else if(this.orders.type=="patrol"){var a=u(this,2);if(a.length>0){var b=a[0];this.orders={type:"attack",target:b};return}this.moveTo(this.orders.to);if(this.path.length<=1){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"){this.orders={type:"guard"};return}var c=this.orders.target.x;var e=this.orders.target.y;if(this.orders.target.type=="turret"){c+=this.orders.target.pixelWidth/(2*d.gridSize);e+=this.orders.target.pixelHeight/(2*d.gridSize)}if(this.orders.target.type=="building"){c+=this.orders.target.gridWidth/2;e+=this.orders.target.gridHeight}if(Math.pow(c-this.x,2)+Math.pow(e-this.y,2)<=Math.pow(this.sight,2)){if(this.orders.type=="attack"){var g=C({x:c,y:e},this,32);if(this.turretDirection==g){this.instructions.push({type:"fire"})}else{this.instructions.push({type:"aim",toDirection:g})}}}else{this.moveTo({x:Math.floor(c),y:Math.floor(e)},true)}}else if(this.orders.type=="build"){if(this.moveDirection!=15){this.instructions.push({type:"turn",toDirection:15})}else{this.status="destroy";o.play("construction");d.buildings.push(f.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 a=u(this,1);if(a.length>0){var b=a[0];this.orders={type:"attack",target:b}}}},move:function(){this.moving=false;this.attacking=false;if(!this.instructions){this.instructions=[]}if(this.instructions.length==0){return}for(var a=0;a<this.instructions.length;a++){var b=this.instructions[a];if(b.type=="turn"){if(b.toDirection==this.moveDirection){b.type="done"}if(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;if((this.moveDirection-b.toDirection)*(this.moveDirection+this.turnSpeed*.1-b.toDirection)<=0){this.moveDirection=b.toDirection}}else{this.moveDirection=this.moveDirection-this.turnSpeed*.1;if((this.moveDirection-b.toDirection)*(this.moveDirection-this.turnSpeed*.1-b.toDirection)<=0){this.moveDirection=b.toDirection}}if(this.moveDirection>31){this.moveDirection=0}else if(this.moveDirection<0){this.moveDirection=31}}if(b.type=="move"){if(b.distance<=0){b.type="done";return}this.moving=true;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=A(Math.floor(this.turretDirection),Math.floor(b.toDirection),32);if(Math.abs(f)<1){this.turretDirection=b.toDirection;b.type="done"}else{this.turretDirection=B(this.turretDirection,f/Math.abs(f),32)}}}if(b.type=="fire"){if(!this.bulletFiring){o.play("tank_fire");this.bulletFiring=true;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={};var c=a.name;b.team=d.currentLevel.team;$.extend(b,this.types[c].defaults);$.extend(b,this.types[c]);$.extend(b,a);return b}};var j={types:[],turretDetails:{"gun-turret":{name:"gun-turret",label:"Gun Turret",powerIn:20,primaryWeapon:12,cost:600,hitPoints:200,sight:5,turnSpeed:5,reloadTime:1500,pixelWidth:24,pixelHeight:24,buildImageCount:20,turretImageCount:32,pixelOffsetX:-12,pixelOffsetY:-12,pixelTop:12,pixelLeft:12}},preloadImage:p,loadImageArray:q,preloadCount:0,loadedCount:0,load:function(a){var b=this.turretDetails[a];var c={};c.defaults={type:"turret",status:"",draw:this.draw,drawSelection:s,processOrders:this.processOrders,underPoint:t,move:this.move,getLife:r,animationSpeed:4,health:b.hitPoints,pixelLeft:0,pixelTop:0,pixelOffsetX:0,pixelOffsetY:0,turretDirection:0};c.imageArray=[];c.imageArray["build"]=this.loadImageArray("turrets/"+b.name+"/"+b.name+"-build",b.buildImageCount,".gif");c.imageArray["healthy"]=this.loadImageArray("turrets/"+b.name+"/"+b.name+"-healthy",b.turretImageCount,".gif");c.imageArray["damaged"]=this.loadImageArray("turrets/"+b.name+"/"+b.name+"-damaged",b.turretImageCount,".gif");$.extend(c,b);this.types[a]=c},draw:function(){var a=this.getLife();if(this.status=="build"||this.status=="sell"){imageCategory="build"}else if(this.status==""){imageCategory=this.life;if(this.life=="ultra-damaged"){imageCategory="damaged"}}var b=this.imageArray[imageCategory];if(!this.animationIndex){this.animationIndex=0}if(this.status==""){var c=b[Math.floor(this.turretDirection)];z(c,this.x*d.gridSize+d.viewportAdjustX,this.y*d.gridSize+d.viewportAdjustY,this.team,this.type)}else{if(b.length>Math.floor(this.animationIndex/this.animationSpeed)){var c=b[Math.floor(this.animationIndex/this.animationSpeed)];if(this.status=="sell"){c=b[b.length-1-Math.floor(this.animationIndex/this.animationSpeed)]}this.currentImage=c;z(c,this.x*d.gridSize+d.viewportAdjustX,this.y*d.gridSize+d.viewportAdjustY,this.team,this.type)}this.animationIndex++;if(this.animationIndex/this.animationSpeed>=b.length){this.animationIndex=0;if(this.status=="build"||this.status=="construct"){this.status=""}else if(this.status=="sell"){this.status=="destroy"}}}this.drawSelection()},processOrders:function(){if(!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)];var b=this.orders.target.x;var c=this.orders.target.y;if(this.orders.target.type=="turret"){b+=this.orders.target.pixelWidth/(2*d.gridSize);c+=this.orders.target.pixelHeight/(2*d.gridSize)}if(this.orders.target.type=="building"){b+=this.orders.target.gridWidth/2;c+=this.orders.target.gridHeight}if(Math.pow(b-this.x,2)+Math.pow(c-this.y,2)>=Math.pow(this.sight,2)){this.orders={type:"guard"}}else{if(this.orders.type=="attack"){var e=C({x:b,y:c},this,32);if(this.turretDirection!=e){this.instructions.push({type:"aim",toDirection:e})}else{this.instructions.push({type:"fire"})}}}}else if(this.orders.type=="guard"){var f=u(this,1);if(f.length>0){var g=f[0];this.orders={type:"attack",target:g}}}},move:function(){if(!this.instructions){this.instructions=[]}if(this.instructions.length==0){return}for(var a=0;a<this.instructions.length;a++){var b=this.instructions[a];if(b.type=="aim"){if(b.toDirection==this.turretDirection){b.type="done"}if(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;if((this.turretDirection-b.toDirection)*(this.turretDirection+this.turnSpeed*.1-b.toDirection)<=0){this.turretDirection=b.toDirection}}else{this.turretDirection=this.turretDirection-this.turnSpeed*.1;if((this.turretDirection-b.toDirection)*(this.turretDirection-this.turnSpeed*.1-b.toDirection)<=0){this.turretDirection=b.toDirection}}if(this.turretDirection>31){this.turretDirection=0}else if(this.turretDirection<0){this.turretDirection=31}}if(b.type=="fire"){if(!this.bulletFiring){o.play("tank_fire");this.bulletFiring=true;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={};var c=a.name;b.team=d.currentLevel.team;$.extend(b,this.types[c].defaults);$.extend(b,this.types[c]);$.extend(b,a);return b}};var l={types:[],load:function(a){var b={name:a}},add:function(a){$.extend(a,this.types[name])}};var m={types:[],overlayDetails:{tiberium:{name:"tiberium",count:2,stageCount:12,gridOffsetX:0,gridOffsetY:0},tree:{name:"tree",count:1,stageCount:10,gridOffsetX:0,gridOffsetY:-1},trees:{name:"trees",count:1,stageCount:10,gridOffsetX:0,gridOffsetY:-1}},load:function(a){var b={name:a,draw:this.draw};var c=this.overlayDetails[a];var d=[];for(i=0;i<c.count;i++){d[i]=this.loadImageArray("tiles/temperate/"+a+"/"+a+"-"+i,c.stageCount,".gif")}b.imageArray=d;$.extend(b,c);this.types[a]=b},draw:function(){var a=this.imageArray[this.type][this.stage];var b=(this.x+this.gridOffsetX)*d.gridSize+d.viewportAdjustX;var c=(this.y+this.gridOffsetY)*d.gridSize+d.viewportAdjustY;z(a,b,c)},loadAll:function(){this.load("tiberium");this.load("tree");this.load("trees")},add:function(a){var b={type:0,stage:0};var c=a.name;$.extend(b,this.types[c]);$.extend(b,a);return b},loaded:true,preloadCount:0,loadedCount:0,preloadImage:p,loadImageArray:q};var n={levelDetails:{gdi1:{mapUrl:"maps/gdi/map01.jpeg",startingCash:4e3,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:12,y:15,name:"tiberium",stage:11},{x:13,y:15,name:"tiberium",stage:8},{x:13,y:16,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:["minigunner"],buildings:["construction-yard","barracks","power-plant","weapons-factory"],vehicles:["mcv","light-tank"],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:true,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 true}},{action:"sound",sound:"low_power"},{action:"destroyBuilding",id:"powerplant1"}]},{id:"wintrigger",actions:[{action:"wait",trigger:"condition",condition:function(){return units.enemyUnitCount()==0&&f.enemyBuildingsCount==0}},{action:"endLevel",type:"success"}]}]}},preloadImage:p,loaded:true,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--){h.load(this.levelDetails[a].items[item][d])}}if(item=="buildings"){for(var d=c.items[item].length-1;d>=0;d--){f.load(c.items[item][d])}}if(item=="infantry"){for(var d=c.items[item].length-1;d>=0;d--){g.load(c.items[item][d])}}if(item=="turrets"){for(var d=c.items[item].length-1;d>=0;d--){j.load(c.items[item][d])}}}var i=new Array;var k=new Array;for(var l=0;l<c.gridHeight;l++){i[l]=new Array;k[l]=new Array;for(var n=0;n<c.gridWidth;n++){i[l][n]=0}}for(var d=c.terrain.length-1;d>=0;d--){var o=c.terrain[d];for(var n=o.x1;n<=o.x2;n++){for(var l=o.y1;l<=o.y2;l++){i[l][n]=1;k[l][n]=o.type}}}var p=[];for(var d=c.overlay.length-1;d>=0;d--){p.push(m.add(c.overlay[d]))}b.mapGrid=k;b.obstructionGrid=i;b.overlay=p;e.cash=c.startingCash;return b}};var o={sound_list:[],loaded:true,load:function(a,b){var c=new Audio("audio/"+b+"/"+a+".ogg");c.load();return 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["reinforcements_have_arrived"]=[this.load("reinforcements_have_arrived","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["clock"]=[this.load("clock","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")]}};var w={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;var c=this.fogContext;var 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,false);c.fill()}}for(var f=d.buildings.length-1;f>=0;f--){var h=d.buildings[f];if(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,false);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,false);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)}};var x=document.createElement("canvas");var y=x.getContext("2d");var E=function(){function a(a,b,c,d,e,f,g,h,i,j,k,l,m){if(a){c&&!i[e][g]&&(l[m++]={x:g,y:e});d&&!i[e][h]&&(l[m++]={x:h,y:e})}if(b){c&&!i[f][g]&&(l[m++]={x:g,y:f});d&&!i[f][h]&&(l[m++]={x:h,y:f})}return l}function b(a,b,c,d,e,f,g,h,i,j,k,l,m){a=e>-1;b=f<j;c=g<k;d=h>-1;if(c){a&&!i[e][g]&&(l[m++]={x:g,y:e});b&&!i[f][g]&&(l[m++]={x:g,y:f})}if(d){a&&!i[e][h]&&(l[m++]={x:h,y:e});b&&!i[f][h]&&(l[m++]={x:h,y:f})}return 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;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});return 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;break}w||(w=b);do{z=n;A=0;for(x=0;x<t;++x){if((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;if(!(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()})})