PageRenderTime 50ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/world/lib/modules/map.module.js

https://bitbucket.org/Fenchurch/isoel
JavaScript | 171 lines | 129 code | 28 blank | 14 comment | 23 complexity | 25ce168cb958025de2d9d0694c66f44d MD5 | raw file
Possible License(s): GPL-2.0, WTFPL, MIT, BSD-3-Clause, Apache-2.0, 0BSD
  1. var _ = require("underscore");
  2. var Backbone = require("backbone");
  3. var PRNG = require("../util/PRNG");
  4. var SimplexNoise = require("../util/SimplexNoise");
  5. var planetModel = require("../models/planet.model");
  6. exports.map = Backbone.Collection.extend({
  7. name:"map MODULE",
  8. model:planetModel.planet,
  9. initialize: function(){
  10. var n =502; //number of spots
  11. // this.PRNG = new PRNG.PRNG("FUCK THIS SHIT, IAM GOING TO BE A POTATO");
  12. this.PRNG = new PRNG.PRNG("dasdasd");
  13. console.log("FFASD", this.PRNG.random());
  14. this.SimplexNoise = new SimplexNoise.SimplexNoise(this.PRNG);
  15. var imax = Math.floor(Math.sqrt(n)); // get max side length
  16. var x = 0;
  17. for(var i = 0; i< imax; i++){
  18. for(var j = 0; j< imax; j++){
  19. var h = this.SimplexNoise.noise(0.1*i, 0.1*j);
  20. // console.log(h);
  21. var coin = this.PRNG.random();
  22. if(coin + (0.5*Math.floor(h)) > 0.8){
  23. this.add({id:x, capacity:100 + (50 * this.PRNG.random()), position:{x:i*100,y:j*100,z:0}});
  24. x++;
  25. }
  26. }
  27. }
  28. },
  29. eUpgrade: function(target, sessionStorage){
  30. var resourceMod = 0.2;
  31. var effToIndustry = 0.0025;
  32. var mpcost = 0.5;
  33. var planet = this.get(target);
  34. if(!planet){return false;}
  35. var usedMP = planet.faction.attributes.manpower * mpcost;
  36. //check availability of resources
  37. if(planet.faction.attributes.resources > usedMP){
  38. resourceMod =1;
  39. planet.faction.attributes.resources -= (0.25 * usedMP);
  40. }else {
  41. planet.faction.attributes.resources = 0;
  42. }
  43. planet.faction.attributes.industry += (usedMP *effToIndustry) * resourceMod;
  44. planet.faction.attributes.manpower -= usedMP;
  45. },
  46. eGreen: function(target, sessionStorage){
  47. var resourceMod = 0.2;
  48. var effToIndustry = 0.0025;
  49. var mpcost = 0.5;
  50. var planet = this.get(target) ;
  51. if(!planet){return false;}
  52. var usedMP = planet.faction.attributes.manpower * mpcost;
  53. //check availability of resources
  54. if(planet.faction.attributes.resources > usedMP){
  55. resourceMod =1;
  56. planet.faction.attributes.resources -= (0.25 * usedMP);
  57. } else {
  58. planet.faction.attributes.resources = 0;
  59. }
  60. var reduction = 0.5 * (usedMP/100);
  61. planet.faction.attributes.pollution *= reduction;
  62. planet.faction.attributes.green += (usedMP *effToIndustry) * resourceMod;
  63. planet.faction.attributes.manpower -= usedMP;
  64. },
  65. eAttack:function(data, sessionStorage){
  66. var spd = 0.05;
  67. //console.log(data);
  68. var from = this.get(data.from);
  69. if(from.attributes.owner != sessionStorage.id){
  70. console("NO ATTACK:: wrong ID");
  71. return false;
  72. }
  73. var to = this.get(data.to);
  74. if(!from || !to){return false;}
  75. // console.log("FROM::", from.attributes.position);
  76. // console.log("TO::", to.attributes.position);
  77. var distance = Math.sqrt((Math.pow(from.attributes.position.x - to.attributes.position.x, 2)
  78. + (Math.pow(from.attributes.position.y - to.attributes.position.y, 2))
  79. + (Math.pow(from.attributes.position.z - to.attributes.position.z, 2))));
  80. // atk is used in delayed calculations about the combat
  81. var time = +new Date;
  82. var fn = 0+ from.attributes.faction.force/2;
  83. if(fn < 1){return false;}
  84. var cbtid = time + to.id + from.id + fn;
  85. var atk = {cbtid:cbtid, from: from, to:to, time:+new Date, delay: distance/spd, force:fn};
  86. // def is used for attributes and will be distributed to the client side
  87. var def = {cbtid:cbtid, from: from.id, to:to.id, time:+new Date, delay:distance/spd, force:fn};
  88. // console.log("DEF:", def);
  89. // push attack to the client side
  90. to.attributes.def.push(def);
  91. from.attributes.atk.push(def);
  92. from.attributes.faction.force *= 0.5;
  93. //delay combat calculations until gfx "roughly is there"
  94. _.delay(function(atk){
  95. var attacker = atk.force;
  96. var defender = atk.to.attributes.faction.force;
  97. var result = (attacker - defender);
  98. console.log("PEACE?", atk.from.attributes.owner,atk.to.attributes.owner );
  99. if(atk.from.attributes.owner == atk.to.attributes.owner){
  100. console.log("PEACE!");
  101. result = -1*(attacker + defender);
  102. }
  103. if(result < 0){
  104. //def win
  105. atk.to.attributes.faction.force = Math.abs(result);
  106. } else {
  107. atk.to.attributes.faction.force = result;
  108. atk.to.attributes.owner = atk.from.attributes.owner;
  109. atk.to.attributes.faction.owner = atk.from.attributes.owner;
  110. }
  111. to.attributes.def = _.reject(to.attributes.def, function(item){ return item.cbtid == atk.cbtid; });
  112. from.attributes.atk = _.reject(from.attributes.atk, function(item){ return item.cbtid == atk.cbtid; });
  113. }, distance/spd, atk);
  114. },
  115. eMobilize: function(target, sessionStorage){
  116. var resourceMod = 0.2;
  117. var effToIndustry = 0.1;
  118. var mpcost = 0.5;
  119. var planet = this.get(target);
  120. if(!planet){return false;}
  121. var usedMP = planet.faction.attributes.manpower * mpcost;
  122. //check availability of resources
  123. if(planet.faction.attributes.resources > usedMP){
  124. resourceMod =1;
  125. planet.faction.attributes.resources -= (0.25* usedMP);
  126. } else {
  127. planet.faction.attributes.resources = 0;
  128. }
  129. planet.faction.attributes.pollution += 0.1;
  130. planet.faction.attributes.force += (usedMP *effToIndustry) * resourceMod;
  131. planet.faction.attributes.manpower -= usedMP;
  132. },
  133. getStartingPlanet: function(){
  134. var planets = this.where({owner: null, burning:false, revolt:false});
  135. console.log("DEDICATED STARTING PLANET::AVAILABLE/ID", planets.length);
  136. return planets[0] || false;
  137. },
  138. update: function(){
  139. _.each(this.models, function(item){
  140. item.update();
  141. });
  142. }
  143. });