-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathgetNextShipCodes_(cached).js
127 lines (116 loc) · 4.01 KB
/
getNextShipCodes_(cached).js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/*
GET NEXT SHIP TYPES FROM CURRENT SHIP CODE (CACHED VERSION)
Syntax: game.getNextShipCodes(type)
Returns: An array:
+ Empty if:
- The code does not match any ships in the game
- There are no proper ships in the next level
- parameter `next` in ship code is an empty array (`[]`)
- max level reached (`this.options.max_level`)
- The mod hasn't started yet (before the line "Mod started" appears in the terminal)
DO NOT TOUCH THIS PROPERTY SO THAT THE CODE WILL WORK PROPERLY:
+ game.custom.ships
Enjoy! :)
*/
/* Encapsuled version - paste the rest of this code to the end of your mod code */
;(function(){
let internals = game.custom.ships;
let uAr = function(array, noSort) {
let res = Array.from(new Set(array));
if (noSort) return res;
return res.sort(function(a,b){return a - b})
}
let getModel = function(code) {
code = getNum(code);
let t = internals.models.get(code);
if (code != null) return t;
return code%100
}
let getNum = function(num) {
let n = Number(num);
return isNaN(n)?num:n
}
let save = function(code, data) {
internals.cache.set(code, data);
return data
}
let __init__ = function(game) {
let default_ships = (game.options.reset_tree)?[]:[
,
[101],
[201, 202],
[301, 302, 303, 304],
[401, 402, 403, 404, 405, 406],
[501, 502, 503, 504, 505, 506, 507],
[601, 602, 603, 604, 605, 606, 607, 608],
[701, 702, 703, 704]
]
let default_nexts = new Map(game.options.reset_tree?[]:[[302, [403, 404]]]);
let default_options = {
ships: default_ships,
nexts: default_nexts,
models: new Map(),
cache: new Map()
}
game.custom.ships = default_options;
internals = game.custom.ships;
if (Array.isArray(game.options.ships))
for (let ship of game.options.ships)
{
try{
let prs= JSON.parse(ship);
let code = getNum(prs.typespec.code);
let next = getNum(prs.typespec.next);
let level = getNum(prs.typespec.level);
if (!Array.isArray(internals.ships[level])) internals.ships[level] = [];
internals.ships[level].push(code);
if (prs.typespec.model !== code%100) internals.models.set(code, prs.typespec.model);
let cnxt = uAr(Array.isArray(next)?next:[]);
if (cnxt.length > 0) internals.nexts.set(code, cnxt);
}
catch(e){
game.custom.ships = default_options;
internals = game.custom.ships;
break
}
}
for (let i in internals.ships) {
internals.ships[i] = uAr(internals.ships[i]).sort((a,b) => getModel(a) - getModel(b))
}
}
game.getNextShipCodes = function (code) {
if (!internals) return [];
code = getNum(code);
let cached = internals.cache.get(code);
if (Array.isArray(cached)) return cached;
for (let level in internals.ships) {
let current_ships = internals.ships[level];
if (current_ships.indexOf(code) != -1) {
if (isNaN(level) || Number(level) >= game.options.max_level) return save(code, []);
let custom_next = internals.nexts.get(code);
if (Array.isArray(custom_next)) {
let cnext = [], ships = Object.values(internals.ships).flat();
for (let type of custom_next) {
if (ships.indexOf(type) != -1) cnext.push(type);
}
return save(code, uAr(cnext))
}
else {
let nextLevel = Number(level) + 1;
let next_ships = internals.ships[nextLevel];
if (!next_ships) return [];
let model = code - level * 100 - 1;
let alpha = Math.max(0, Math.round(model / Math.max(current_ships.length - 1, 1) * (next_ships.length - 2)));
return save(code, next_ships.slice(alpha, alpha + 2))
}
}
}
return []
}
var tick = this.tick;
this.tick = function() {
try { !internals && __init__.apply(this, arguments) } catch(e){}
this.tick = tick;
return typeof tick == "function" && tick.apply(this, arguments)
}
}).call(this);