/world/lib/modules/map.module.js
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
- var _ = require("underscore");
- var Backbone = require("backbone");
- var PRNG = require("../util/PRNG");
- var SimplexNoise = require("../util/SimplexNoise");
- var planetModel = require("../models/planet.model");
- exports.map = Backbone.Collection.extend({
- name:"map MODULE",
- model:planetModel.planet,
- initialize: function(){
- var n =502; //number of spots
- // this.PRNG = new PRNG.PRNG("FUCK THIS SHIT, IAM GOING TO BE A POTATO");
-
- this.PRNG = new PRNG.PRNG("dasdasd");
- console.log("FFASD", this.PRNG.random());
- this.SimplexNoise = new SimplexNoise.SimplexNoise(this.PRNG);
- var imax = Math.floor(Math.sqrt(n)); // get max side length
- var x = 0;
- for(var i = 0; i< imax; i++){
- for(var j = 0; j< imax; j++){
- var h = this.SimplexNoise.noise(0.1*i, 0.1*j);
- // console.log(h);
- var coin = this.PRNG.random();
- if(coin + (0.5*Math.floor(h)) > 0.8){
- this.add({id:x, capacity:100 + (50 * this.PRNG.random()), position:{x:i*100,y:j*100,z:0}});
- x++;
- }
-
-
- }
- }
- },
- eUpgrade: function(target, sessionStorage){
- var resourceMod = 0.2;
- var effToIndustry = 0.0025;
- var mpcost = 0.5;
- var planet = this.get(target);
- if(!planet){return false;}
- var usedMP = planet.faction.attributes.manpower * mpcost;
- //check availability of resources
- if(planet.faction.attributes.resources > usedMP){
- resourceMod =1;
- planet.faction.attributes.resources -= (0.25 * usedMP);
- }else {
- planet.faction.attributes.resources = 0;
- }
- planet.faction.attributes.industry += (usedMP *effToIndustry) * resourceMod;
- planet.faction.attributes.manpower -= usedMP;
- },
- eGreen: function(target, sessionStorage){
- var resourceMod = 0.2;
- var effToIndustry = 0.0025;
- var mpcost = 0.5;
- var planet = this.get(target) ;
- if(!planet){return false;}
- var usedMP = planet.faction.attributes.manpower * mpcost;
- //check availability of resources
- if(planet.faction.attributes.resources > usedMP){
- resourceMod =1;
- planet.faction.attributes.resources -= (0.25 * usedMP);
- } else {
- planet.faction.attributes.resources = 0;
- }
- var reduction = 0.5 * (usedMP/100);
- planet.faction.attributes.pollution *= reduction;
- planet.faction.attributes.green += (usedMP *effToIndustry) * resourceMod;
- planet.faction.attributes.manpower -= usedMP;
- },
- eAttack:function(data, sessionStorage){
- var spd = 0.05;
- //console.log(data);
- var from = this.get(data.from);
- if(from.attributes.owner != sessionStorage.id){
- console("NO ATTACK:: wrong ID");
- return false;
- }
- var to = this.get(data.to);
- if(!from || !to){return false;}
- // console.log("FROM::", from.attributes.position);
- // console.log("TO::", to.attributes.position);
- var distance = Math.sqrt((Math.pow(from.attributes.position.x - to.attributes.position.x, 2)
- + (Math.pow(from.attributes.position.y - to.attributes.position.y, 2))
- + (Math.pow(from.attributes.position.z - to.attributes.position.z, 2))));
- // atk is used in delayed calculations about the combat
- var time = +new Date;
- var fn = 0+ from.attributes.faction.force/2;
- if(fn < 1){return false;}
- var cbtid = time + to.id + from.id + fn;
- var atk = {cbtid:cbtid, from: from, to:to, time:+new Date, delay: distance/spd, force:fn};
- // def is used for attributes and will be distributed to the client side
- var def = {cbtid:cbtid, from: from.id, to:to.id, time:+new Date, delay:distance/spd, force:fn};
- // console.log("DEF:", def);
- // push attack to the client side
- to.attributes.def.push(def);
- from.attributes.atk.push(def);
-
- from.attributes.faction.force *= 0.5;
- //delay combat calculations until gfx "roughly is there"
-
- _.delay(function(atk){
- var attacker = atk.force;
- var defender = atk.to.attributes.faction.force;
- var result = (attacker - defender);
- console.log("PEACE?", atk.from.attributes.owner,atk.to.attributes.owner );
- if(atk.from.attributes.owner == atk.to.attributes.owner){
- console.log("PEACE!");
- result = -1*(attacker + defender);
- }
- if(result < 0){
- //def win
- atk.to.attributes.faction.force = Math.abs(result);
- } else {
- atk.to.attributes.faction.force = result;
- atk.to.attributes.owner = atk.from.attributes.owner;
- atk.to.attributes.faction.owner = atk.from.attributes.owner;
- }
- to.attributes.def = _.reject(to.attributes.def, function(item){ return item.cbtid == atk.cbtid; });
- from.attributes.atk = _.reject(from.attributes.atk, function(item){ return item.cbtid == atk.cbtid; });
-
- }, distance/spd, atk);
-
- },
- eMobilize: function(target, sessionStorage){
- var resourceMod = 0.2;
- var effToIndustry = 0.1;
- var mpcost = 0.5;
- var planet = this.get(target);
-
- if(!planet){return false;}
- var usedMP = planet.faction.attributes.manpower * mpcost;
- //check availability of resources
- if(planet.faction.attributes.resources > usedMP){
- resourceMod =1;
- planet.faction.attributes.resources -= (0.25* usedMP);
- } else {
- planet.faction.attributes.resources = 0;
- }
- planet.faction.attributes.pollution += 0.1;
- planet.faction.attributes.force += (usedMP *effToIndustry) * resourceMod;
- planet.faction.attributes.manpower -= usedMP;
- },
- getStartingPlanet: function(){
- var planets = this.where({owner: null, burning:false, revolt:false});
- console.log("DEDICATED STARTING PLANET::AVAILABLE/ID", planets.length);
- return planets[0] || false;
- },
- update: function(){
- _.each(this.models, function(item){
- item.update();
- });
- }
- });