Completed
Push — master ( a1717d...f25de8 )
by Andres
27s
created

supernova.js ➔ ... ➔ prestigeFormula   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
c 0
b 0
f 0
nc 1
dl 0
loc 7
rs 9.4285
nop 1
1
/**
2
 supernova
3
 Component that handles the exotic matter and prestige logic.
4
 It includes exotic matter production, exotic upgrades, and infusion of
5
 subatomic particles to boost exotic production.
6
 A prestige erases the progress of a single element, and produces exotic
7
 matter for said element.
8
9
 @namespace Components
10
 */
11
'use strict';
12
13
angular.module('game').component('supernova', {
14
  templateUrl: 'views/supernova.html',
15
  controller: ['state', 'format', 'visibility', 'upgrade', 'data', 'util', supernova],
16
  controllerAs: 'ct'
17
});
18
19
function supernova(state, format, visibility, upgrade, data, util) {
20
  let ct = this;
21
  ct.state = state;
22
  ct.data = data;
23
  ct.util = util;
24
  ct.format = format;
25
  ct.infuse = {};
26
27
  /* Exotic production is a function of the different resources of each
28
  element. Additionally, multi-element molecules count double, once for
29
  each participating element. */
30
  ct.exoticProduction = function() {
31
    let production = {};
32
    let exotic = data.elements[state.currentElement].exotic;
33
    production[exotic] = 0;
34
    for (let resource of data.elements[state.currentElement].includes) {
35
      if (!state.player.resources[resource].unlocked) {
36
        continue;
37
      }
38
      if (data.resources[resource].type.indexOf('molecule') !== -1) {
39
        let multiplier = 0;
40
        for (let key in data.resources[resource].elements) {
41
          let number = data.resources[resource].elements[key];
42
          multiplier += number;
43
        }
44
        for (let element in data.resources[resource].elements) {
45
          let newExotic = data.elements[element].exotic;
46
          production[newExotic] = production[newExotic] || 0;
47
          production[newExotic] += prestigeFormula(state.player.resources[resource].number)*multiplier;
48
        }
49
      }else{
50
        production[exotic] += prestigeFormula(state.player.resources[resource].number);
51
      }
52
    }
53
    for (let key in production) {
54
      // we adjust the production to start at 1e6 resources
55
      // if (production[key] >= 13) {
56
      //   production[key] -= 13;
57
      // } else {
58
      //   production[key] = 0;
59
      // }
60
      // we adjust the infusion
61
      production[key] = Math.floor(production[key]*ct.totalInfuseBoost());
62
    }
63
64
    return production;
65
  };
66
67
  function prestigeFormula(resource){
68
    let stepFactor = Math.max(Math.pow(10, Math.floor(Math.log10(resource))), 1);
69
    let step = stepFactor/1e5;
70
    let sigmoidQuotient = 1+Math.pow(Math.E, -(resource/stepFactor-5.747734128));
71
    let sigmoid = 1/sigmoidQuotient+0.1;
72
    return Math.floor(step * sigmoid);
73
  }
74
75
  ct.exoticPrestige = function() {
76
    let resources = state.player.resources;
77
    let production = ct.exoticProduction();
78
79
    for (let key in production) {
80
      resources[key].number += production[key];
81
      resources[key].unlocked = true;
82
    }
83
84
    for(let resource in ct.infuse){
85
      state.player.resources[resource].number -= ct.infuse[resource];
86
    }
87
88
    upgrade.resetElement(state.player, state.currentElement);
89
  };
90
91
  ct.buyExoticUpgrade = function(name, element) {
92
    let upgrades = state.player.elements[element].exotic_upgrades;
93
    let price = data.exotic_upgrades[name].price;
94
    let currency = data.elements[element].exotic;
95
    upgrade.buyUpgrade(state.player,
96
      upgrades,
97
      name,
98
      price,
99
      currency);
100
  };
101
102
  ct.setPercentage = function(resource, percentage) {
103
    ct.infuse[resource] = Math.floor(state.player.resources[resource].number*(percentage/100));
104
  };
105
106
  ct.fixNumber = function(resource) {
107
    ct.infuse[resource] = Math.max(0, Math.min(state.player.resources[resource].number, ct.infuse[resource]));
108
  };
109
110
  /* This function checks that values inserted in the text boxes are
111
  valid numbers */
112
  ct.isValidInfusion = function() {
113
    let valid = true;
114
    for(let resource in ct.infuse){
115
      valid = valid && Number.isFinite(ct.infuse[resource]);
116
    }
117
    return valid;
118
  };
119
120
  ct.infuseBoost = function(resource) {
121
      let number = Math.min(ct.infuse[resource], state.player.resources[resource].number);
122
      if(number === 0){
123
        return 1;
124
      }
125
      // log adds diminishing returns to the infusion
126
      return 1 + Math.log(number)/Math.log(1.25)*ct.data.constants.INFUSE_POWER;
127
  };
128
129
  /* The infusion boosts are multiplicative with respect to each other */
130
  ct.totalInfuseBoost = function() {
131
    let total = 1;
132
    for(let resource in ct.infuse){
133
      total *= ct.infuseBoost(resource);
134
    }
135
    return total;
136
  };
137
138
  ct.visibleExoticUpgrades = function(currentElement) {
139
    return visibility.visible(data.exotic_upgrades, isExoticUpgradeVisible, currentElement);
140
  };
141
142
  function isExoticUpgradeVisible(name, currentElement) {
143
    return visibility.isUpgradeVisible(name, currentElement, data.exotic_upgrades[name]);
144
  }
145
146
  ct.visibleSubatomic = function() {
147
    return visibility.visible(data.resources, isSubatomicVisible, '');
148
  };
149
150
  function isSubatomicVisible(name) {
151
    if (!state.player.resources[name].unlocked) {
152
      return false;
153
    }
154
155
    if(data.resources[name].type &&
156
       data.resources[name].type.indexOf('subatomic') !== -1){
157
        return true;
158
    }
159
160
    return false;
161
  }
162
}
163