Press "F" to dock with this Space Station.')):($.UI.messageIsOpen&&$.UI.closeMessage(),this.dockingRange=!1)}},$.Pl.dock=function(){this.docked=!0,this.ve.x=0,this.ve.Y=0,this.fuel.total=Math.round(this.fuel.total),this.fuel.hull=Math.round(this.hull.total),this.closestStation.dockedShips++,$.UI.showStationMenu(this.closestStation)},$.Pl.undock=function(){this.docked=!1,this.closestStation.dockedShips--,this.cleanInventory()},$.Pl.cleanInventory=function(){for(var t in this.cargoBay)this.cargoBay[t].qnt<=0&&delete this.cargoBay[t]},$.Pl.fireWeapon=function(){var t=Math.atan2($.KM.mousePos.y-$.screenH/2,$.KM.mousePos.x-$.screenW/2),e=(Math.sqrt(this.ve.x*this.ve.x+this.ve.y*this.ve.y)+this.laser.speed,{x:this.x,y:this.y});e={x:e.x+=40*Math.cos(t),y:e.y+=40*Math.sin(t),velX:this.ve.x,velY:this.ve.y,faction:this.faction,id:this.id},$.gameTime>this.lastShot+this.laser.coolDown&&($.Projectiles.add(t,e,this.laser.speed,this.laser.colour,this.laser.width,this.laser.height,this.laser.hp),this.lastShot=$.gameTime)},$.Pl.hit=function(t){if(!this.docked){if(this.shield.total>0){this.shield.total-=t;var e=new $.ParticleEmiter;e.init(this.x,this.y,"shield",this),$.paInstances.push(e)}else if(this.hull.total-=t,this.hull.total<=0){var e=new $.ParticleEmiter;e.init(this.x,this.y,"explosion",this),$.paInstances.push(e),this.ve.x=0,this.ve.y=0,this.dead=!0}$.UI.hit()}},$.shieldHit=function(){},$.Pl.update=function(){this.docked||this.movement(),this.draw()},$.Pl.draw=function(){if(!this.dead){this.cs.save(),this.cs.translate($.screenW/2,$.screenH/2),this.cs.rotate(this.rotation);for(var t in this.spSVG)this.cs.fillStyle=this.spSVG[t].colour,this.cs.fill(this.spSVG[t].shape);this.cs.restore()}},$.Location=function(){},$.Location.prototype={init:function(t,e){this.id=Math.floor(1e6*Math.random()),this.name="TERRAN STATION",this.cs=$.objectsCanvas,this.x=t,this.y=e,this.radius=200,this.size=this.radius,this.center={x:this.x+this.size,y:this.y+this.size},this.type="station",this.drawX=0,this.drawY=0,this.rotation=0,this.discovered=!1,this.spSVG=station,this.ve={x:0,y:0},this.stock=this.stockInventory(),this.lastStockUpdate=0,this.stockUpdateFrequency=600,this.dockedShips=0,this.services={fuel:{cost:.3},hull:{cost:1}},this.credits=1e4,this.fuzzShipsCount=0,this.tradeShipsCount=0},spawnShips:function(){if(0===this.fuzzShipsCount){var t=new $.FuzzShip;t.init(this),$.Unv.sps.push(t),this.fuzzShipsCount++}if(0===this.tradeShipsCount){var e=new $.TradingShip;e.init(this),$.Unv.sps.push(e),this.tradeShipsCount++}},stockInventory:function(){function t(t){a.hasOwnProperty(t.name)?a[t.name].qnt++:a[t.name]={price:t.price,rrp:t.price,qnt:1,description:t.description}}for(var e=1e5,i=warezListGenerator(),s=i.length-1,a={},n=1e7,r=0;r=n;){var o=getRandomNum(0,s);i[o].price<=e&&(t(i[o]),e-=i[o].price)}return a},updateStockPrices:function(){if($.gameTime>=this.lastStockUpdate+this.stockUpdateFrequency&&0===this.dockedShips){for(var t in this.stock){var e=this.stock[t].rrp/1e3,i=50-this.stock[t].qnt;this.stock[t].price=this.stock[t].rrp+Math.round(e*i)}this.lastStockUpdate=$.gameTime}},buyItem:function(t,e){if(e.cargoBay.hasOwnProperty(t)){e.cargoBay[t].qnt++,e.cargoBay[t].paidValues.push(this.stock[t].price),e.cargoBay[t].netValue=0;for(var i=0;i=e.distance?(t={x:i.x*(this.topSpeed*(e.distance/a)),y:i.y*(this.topSpeed*(e.distance/a))},this.td.distance<=1&&(t={x:0,y:0})):t={x:i.x*this.topSpeed,y:i.y*this.topSpeed}}.bind(this),s=function(e){var i={x:this.x-e.x,y:this.y-e.y};i=normalize(i),t={x:i.x*this.topSpeed,y:i.y*this.topSpeed}}.bind(this),a=function(){var t=100,e=10,i={x:this.ve.x,y:this.ve.y},s=normalize(i),a={x:this.ve.x,y:this.ve.y};a=normalize(a),a={x:a.x*(.5*t*2/this.topSpeed),y:a.y*(.5*t*2/this.topSpeed)};var n=(this.ve.x+this.ve.y)/this.topSpeed,r={x:this.x+s.x*n,y:this.y+s.y*n},o=function(e){var i={x:this.x+s.x*t*.5,y:this.y+s.y*t*.5};return getDistance(e.x,e.y,r.x,r.y)<=e.size||getDistance(e.x,e.y,i.x,i.y)<=e.size||getDistance(e.x,e.y,this.x,this.y)<=e.size}.bind(this),h=function(){for(var t=null,e=$.Unv.lns.concat($.Unv.sps),i=0;i0?(this.fleeTarget.x=this.potentialThreats[0].x,this.fleeTarget.y=this.potentialThreats[0].y,this.fleeTarget.ve.x=this.potentialThreats[0].ve.x,this.fleeTarget.ve.y=this.potentialThreats[0].ve.y,this.mode="flee"):(this.mode="seek",this.td.x=this.potentialTargets[0].x,this.td.y=this.potentialTargets[0].y,this.td.ve.x=this.potentialTargets[0].ve.x,this.td.ve.y=this.potentialTargets[0].ve.y,this.td.distance<=this.fireRange&&this.fireAtTarget())},hit:function(t,e){if(!this.docked){this.shield.total>0?this.shield.total-=t:this.hull.total-=t;var i=new $.ParticleEmiter;i.init(this.x,this.y,"shield",this),$.paInstances.push(i),this.hitList[e]++,this.firedAtBy=e,this.lastHit=$.gameTime}this.hull.total<=0&&this.die()},die:function(){for(var t=$.Unv.sps.length;t--;)$.Unv.sps[t].id===this.id&&$.Unv.sps.splice(t,1);"police"===this.type&&this.home.fuzzShipsCount--,"cargo sp"===this.type&&this.home.tradeShipsCount--,"pirate"===this.type&&$.Unv.pirateShipCount--;var e=new $.ParticleEmiter;e.init(this.x,this.y,"explosion",this),$.paInstances.push(e),delete this},update:function(){this.drawX=$.Unv.offsetX+this.x,this.drawY=$.Unv.offsetY+this.y,this.movement(),this.draw()},casualUpdate:function(){this.ai(),this.shield.totalthis.lastHit+5&&!e.docked&&!e.dead&&"station"!==e.type&&"outlaw"!==e.faction&&(this.potentialTargets.push({id:e.id,x:e.x,y:e.y,ve:{x:e.ve.x,y:e.ve.y},faction:e.faction,priority:this.priority(e)}),"police"===e.type&&this.potentialThreats.push({id:e.id,x:e.x,y:e.y,ve:{x:e.ve.x,y:e.ve.y},faction:e.faction,priority:this.priority(e)}))}this.potentialTargets.sort(function(t,e){return t.prioritye.priority?-1:0}),this.potentialTargets.length>0?this.combatMode():(this.mode="seek",(this.td.distance<200||0===this.td.x&&0===this.td.y)&&(this.td.x=getRandomNum(0,$.Unv.width),this.td.y=getRandomNum(0,$.Unv.height),this.td.ve.x=0,this.td.ve.y=0))},$.PirateShip.prototype.priority=function(t){var e=0;return this.hitList[t.id]&&(e+=this.hitList[t.id]),e},$.PirateShip.prototype.fireAtTarget=function(){if($.gameTime>this.lastShot+this.laser.coolDown){var t=Math.atan2(this.potentialTargets[0].y-this.y,this.potentialTargets[0].x-this.x),e={x:this.x,y:this.y};e={x:e.x+=40*Math.cos(t),y:e.y+=40*Math.sin(t),velX:this.ve.x,velY:this.ve.y,faction:this.faction},$.Projectiles.add(t,e,this.laser.speed,this.laser.colour,this.laser.width,this.laser.height,this.laser.hp),this.lastShot=$.gameTime}},$.TradingShip=function(){$.Ship.call(this)},$.TradingShip.prototype=new $.Ship,$.TradingShip.prototype.constructor=$.TradingShip,$.TradingShip.prototype.create=function(){this.spSVG=cargoShip,this.width=120,this.height=60,this.radius=80,this.targetRange=2e3,this.topSpeed=5,this.ac=.05,this.faction="tradeAlliance",this.type="cargo sp",this.stationList=[],this.credits=1e3,this.cargoSpace=20,this.cargoBay={},this.x=this.home.x+400,this.y=this.home.y+400},$.TradingShip.prototype.ai=function(){var t=function(){if(0===this.stationList.length){for(var t in $.Unv.lns)$.Unv.lns[t].id!==this.station.id&&this.stationList.push({id:$.Unv.lns[t].id,x:$.Unv.lns[t].x,y:$.Unv.lns[t].y});this.stationList.sort(function(t,e){var i=getDistance(this.x,this.y,t.x,t.y),s=getDistance(this.x,this.y,e.x,e.y);return i>s?1:-1}.bind(this)),this.td.x=this.stationList[0].x,this.td.y=this.stationList[0].y,this.td.ve.x=0,this.td.ve.y=0}}.bind(this);t(),this.td.distance<200&&this.td.distance>0&&!this.docked&&(this.dockingRange=!0,this.docked=!0,this.station=getStationByID(this.stationList[0].id),this.timeDocked=$.gameTime,this.station.dockedShips++),this.docked&&(this.finishedTrade?$.gameTime>=this.timeDocked+this.dockDuration&&(this.stationList.splice(0,1),t(),this.td.x=this.stationList[0].x,this.td.y=this.stationList[0].y,this.td.ve.x=0,this.td.ve.y=0,this.docked=!1,this.finishedTrade=!1,this.station.dockedShips--):this.tradeGoods())},$.TradingShip.prototype.tradeGoods=function(){var t=[];for(var e in this.cargoBay)if(this.cargoBay[e].avgPrice=a;){var e=getRandomNum(0,s.length-1);if(!(this.station.stock[s[e]].qnt>0))break;this.station.stock[s[e]].price<=this.credits&&this.station.buyItem(s[e],this)}this.finishedTrade=!0},$.FuzzShip=function(){$.Ship.call(this)},$.FuzzShip.prototype=new $.Ship,$.FuzzShip.prototype.constructor=$.FuzzShip,$.FuzzShip.prototype.create=function(){this.spSVG=fuzzShip,this.width=60,this.height=60,this.currentTarget=null,this.potentialTargets=[],this.targetRange=2e3,this.fireRange=800,this.lastShot=0,this.laser={coolDown:2,hp:20,speed:11,width:40,height:2,colour:"36,213,243"},this.topSpeed=9,this.ac=.05,this.faction="tradeAlliance",this.type="police",this.x=this.home.x,this.y=this.home.y,this.atHome=!1},$.FuzzShip.prototype.ai=function(){this.closeObjects=findCloseObjects(this.home.x,this.home.y,2e3),getDistance(this.home.x,this.home.y,$.Pl.x,$.Pl.y)<=2e3&&this.closeObjects.push($.Pl),this.potentialTargets=[];for(var t=this.closeObjects.length;t--;){var e=this.closeObjects[t];e.faction!==this.faction&&e.bounty>0&&this.potentialTargets.push({id:e.id,x:e.x,y:e.y,ve:{x:e.ve.x,y:e.ve.y},faction:e.faction,priority:this.priority})}this.potentialTargets.sort(function(t,e){return t.prioritye.priority?-1:0}),this.potentialTargets.length>0?this.attackMode():(this.mode="seek",this.td.x=this.home.x+250,this.td.y=this.home.y+250,this.td.ve.x=0,this.td.ve.y=0)},$.FuzzShip.prototype.priority=function(t){var e=0;return"pirate"===t.faction&&(e+=10),this.hitList[t.id]&&(e+=this.hitList[t.id]),e},$.FuzzShip.prototype.attackMode=function(){this.atHome=!1,this.combatMode()},$.FuzzShip.prototype.fireAtTarget=function(){if($.gameTime>this.lastShot+this.laser.coolDown){var t=Math.atan2(this.potentialTargets[0].y-this.y,this.potentialTargets[0].x-this.x),e={x:this.x,y:this.y};e={x:e.x+=40*Math.cos(t),y:e.y+=40*Math.sin(t),velX:this.ve.x,velY:this.ve.y,faction:this.faction},$.Projectiles.add(t,e,this.laser.speed,this.laser.colour,this.laser.width,this.laser.height,this.laser.hp),this.lastShot=$.gameTime}},$.Unv=function(){},$.Unv.init=function(){this.cs=$.unvCanvas,this.width=6e4,this.height=6e4,this.offsetX=0,this.offsetY=0,this.lns=[],this.sps=[],this.objects=[],this.starLayerWidth=$.screenW,this.starLayerHeight=$.screenH,this.starsLayers=[],this.spawnLocations(13),this.maxPirateShips=12,this.pirateShipCount=0,this.createStars()},$.Unv.spawnLocations=function(t){for(var e=this.width/t*2;t;){var i=Math.floor(Math.random()*$.Unv.width),s=Math.floor(Math.random()*$.Unv.height);if(checkSpacing(i,s,this.lns,e)){var a=new $.Location;a.init(i,s),this.lns.push(a),t--}}},$.Unv.spawnShips=function(){if(this.pirateShipCounti;i++)e.push({radius:1,brightness:getRandomNumFloat(.1,1),x:getRandomNum(0,$.Unv.starLayerWidth),y:getRandomNum(0,$.Unv.starLayerHeight),depth:getRandomNum(1,10)});return e}for(var e=0;3>e;e++)this.starsLayers.push(t(e))},$.Unv.paintStars=function(){for(var t=this.starsLayers.length;t--;)for(var e=this.starsLayers[t].length;e--;){this.starsLayers[t][e].x<0&&(this.starsLayers[t][e].x+=$.screenW),this.starsLayers[t][e].x>$.screenW&&(this.starsLayers[t][e].x-=$.screenW),this.starsLayers[t][e].y<0&&(this.starsLayers[t][e].y+=$.screenH),this.starsLayers[t][e].y>$.screenH&&(this.starsLayers[t][e].y-=$.screenH);var i=this.starsLayers[t][e].x-=$.Pl.ve.x/(20*this.starsLayers[t][e].depth),s=this.starsLayers[t][e].y-=$.Pl.ve.y/(20*this.starsLayers[t][e].depth),a=this.starsLayers[t][e].radius,n=this.starsLayers[t][e].brightness;
this.cs.beginPath(),this.cs.rect(i,s,a,a),this.cs.fillStyle="rgba(255,255,255,"+n+")",this.cs.fill()}},$.Unv.update=function(){this.offsetX=-($.Pl.x-$.screenW/2),this.offsetY=-($.Pl.y-$.screenH/2);for(var t=0;t=t.stock[i].price&&t.stock[i].qnt>0?'class="button" onclick="$.UI.buyItem(\''+i+"')\"":'class="button disable"',a.insertCell(0).innerHTML=t.stock[i].qnt,a.insertCell(1).innerHTML=i,a.insertCell(2).innerHTML=t.stock[i].description,a.insertCell(3).innerHTML=t.stock[i].price+'c',a.insertCell(4).innerHTML=""}},$.UI.renderInventoryTable=function(){var t=$.Pl.closestStation,e=document.getElementById("cargobay");e.innerHTML="";for(var i in $.Pl.cargoBay){var s=e.rows.length,a=e.insertRow(s);a.className=i.replace(/\\/g,"");var n="",r=0;$.Pl.cargoBay[i].qnt>0?(r=t.stock[i].price-$.Pl.cargoBay[i].avgPrice,n='class="button" onclick="$.UI.sellItem(\''+i+"')\""):n='class="button disable"',a.insertCell(0).innerHTML=$.Pl.cargoBay[i].qnt,a.insertCell(1).innerHTML=i,a.insertCell(2).innerHTML=$.Pl.cargoBay[i].netValue+'c',a.insertCell(3).innerHTML=t.stock[i].price+'c',a.insertCell(4).innerHTML=r.toFixed(2)+'c',a.insertCell(5).innerHTML="'}},$.UI.renderServices=function(){document.querySelector("#hull .amount").innerHTML=0,document.querySelector("#fuel .amount").innerHTML=0;var t=document.getElementById("hull_slider");t.setAttribute("max",0);var e=$.Pl.hull.capacity-$.Pl.hull.total,i=$.Pl.credits/1;e>i&&(e=i),e>=1?(t.setAttribute("max",e),t.removeAttribute("disabled"),document.querySelector("#hull .button").className="button"):(t.setAttribute("disabled","true"),document.querySelector("#hull .button").className="button disable");var s=document.getElementById("fuel_slider");s.setAttribute("max",0);var a=$.Pl.fuel.capacity-$.Pl.fuel.total,n=$.Pl.credits/.3;a>n&&(a=n),a>=1?(s.setAttribute("max",a),s.removeAttribute("disabled"),document.querySelector("#fuel .button").className="button"):(s.setAttribute("disabled","true"),document.querySelector("#fuel .button").className="button disable")},$.UI.renderUpgrades=function(){document.getElementById("credits").innerHTML=$.Pl.credits.toFixed(2);var t=document.getElementById("upgrades_table");t.innerHTML="";for(var e in $.Upgrades){for(var i=t.rows.length,s=t.insertRow(i),a="",n=$.Upgrades[e].currentLevel,r=$.Upgrades[e].lvs["lv"+(n+1)].price,o="",h=1;5>=h;h++)o+=h>n?'':'';a=$.Pl.credits>=r?'class="button" onclick="$.UI.buyUpgrade(\''+e+"')\"":'class="button disable"',s.insertCell(0).innerHTML=e,s.insertCell(1).innerHTML=$.Upgrades[e].description,s.insertCell(2).innerHTML=o,s.insertCell(3).innerHTML=r+'c',s.insertCell(4).innerHTML=""}},$.UI.buyUpgrade=function(t){$.Upgrades[t].currentLevel++;var e=$.Upgrades[t].currentLevel,i=$.Upgrades[t].lvs["lv"+e].price,s=$.Upgrades[t].lvs["lv"+e];("hull"===t||"shield"===t)&&($.Pl[t].capacity=s.total,$.Pl[t].total=s.total),"shield"===t&&($.Pl[t].regenerationRate=s.regenerationRate),"engine"===t&&($.Pl.topSpeed=s.topSpeed,$.Pl.ac=s.ac),"laser"===t&&($.Pl.laser.coolDown=s.coolDown,$.Pl.laser.hp=s.hp,$.Pl.laser.speed=s.speed,$.Pl.laser.width=s.width,$.Pl.laser.height=s.height),"radar"===t&&(this.radarRadius=s.radius),$.Pl.credits-=i,this.render()},$.UI.buyItem=function(t){$.Pl.closestStation.buyItem(t,$.Pl),document.getElementById("credits").innerHTML=$.Pl.credits.toFixed(2),this.render()},$.UI.sellItem=function(t){$.Pl.closestStation.sellItem(t,$.Pl),document.getElementById("credits").innerHTML=$.Pl.credits.toFixed(2),this.render()},$.UI.buyService=function(t){var e=document.getElementById(t+"_slider").value;$.Pl.closestStation.buyService(t,$.Pl,e),document.getElementById("credits").innerHTML=$.Pl.credits.toFixed(2),this.render()},$.UI.servicesRange=function(t){var e=document.getElementById(t+"_slider").value,i=$.Pl.closestStation.services[t].cost,s=(e*i).toFixed(2)+'c';document.querySelector("#"+t+" .amount").innerHTML=s},$.UI.closeStationMenu=function(){this.stationMenuIsOpen=!1;var t=document.getElementById("station-menu");t.className="gm";var e=document.getElementById("market_stock");e.innerHTML="",$.Pl.undock()},$.UI.startGame=function(){$.paused=!1,document.getElementById("start-screen").className="gm"},$.UI.hit=function(){var t=document.getElementById("game-container");t.className="canvas-wrap shake",setTimeout(function(){t.className="canvas-wrap"},50)},$.UI.update=function(){this.plotMap(),$.UI.statusBars()},$.init=function(){function t(){$.paused||($.render(),$.update()),requestAnimationFrame(t)}this.resizeElements();var e=document.getElementById("pl"),i=document.getElementById("objects"),s=document.getElementById("sps"),a=document.getElementById("pas"),n=document.getElementById("projectiles"),r=document.getElementById("unv");$.plCanvas=e.getContext("2d"),$.plCanvas.imageSmoothingEnabled=!0,$.unvCanvas=r.getContext("2d"),$.unvCanvas.imageSmoothingEnabled=!0,$.objectsCanvas=i.getContext("2d"),$.objectsCanvas.imageSmoothingEnabled=!0,$.spsCanvas=s.getContext("2d"),$.spsCanvas.imageSmoothingEnabled=!0,$.pasCanvas=a.getContext("2d"),$.pasCanvas.imageSmoothingEnabled=!0,$.projectilesCanvas=n.getContext("2d"),$.projectilesCanvas.imageSmoothingEnabled=!0,$.gameTime=0,$.renderTime=0,$.score=0,$.paused=!0,$.paInstances=[],$.KM.init(),$.Unv.init(),$.Pl.init(),$.Projectiles.init(),$.UI.init();this.fps=0,t(),setInterval(function(){$.paused||$.casualUpdate()},100)},$.resizeElements=function(){$.screenW=document.getElementById("game-container").offsetWidth,$.screenH=document.getElementById("game-container").offsetHeight;for(var t=document.querySelectorAll(".canvas_layer.main"),e=t.length;e--;)t[e].width=$.screenW,t[e].height=$.screenH},$.casualUpdate=function(){this.gameTime++,$.Pl.casualUpdate(),$.UI.update(),$.Unv.casualUpdate()},$.update=function(){$.Pl.update(),$.Unv.update(),$.Projectiles.update()},$.render=function(){$.plCanvas.clearRect(0,0,this.screenW,this.screenH),$.objectsCanvas.clearRect(0,0,this.screenW,this.screenH),$.spsCanvas.clearRect(0,0,this.screenW,this.screenH),$.projectilesCanvas.clearRect(0,0,this.screenW,this.screenH);for(var t=this.paInstances.length;t--;)this.paInstances[t].update()},$.init();