-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenitems
More file actions
executable file
·174 lines (159 loc) · 4.23 KB
/
Copy pathgenitems
File metadata and controls
executable file
·174 lines (159 loc) · 4.23 KB
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#!/usr/bin/js
/* genitems
* Generate the items.js include from unite-db.com file (held_items.json)
* and the preprocessor code accessory (preprocitems.ini)
* Copyright (C) 2023 Jaret Jay Cantu
* Licensed under the AGPL
*
*/
// file utility functions
function readAndHashJson(f) {
// read file
var content = read(f);
if (content == '')
throw("Could not read file: " + f);
var parsed = JSON.parse(content);
if (!parsed || parsed.length == 0)
throw("Could not parse file: " + f);
// convert sequential array into a hash organized by name element
var rehashed = {};
for (var p=0; p<parsed.length; p++) {
var e = parsed[p];
var name = e.name.replaceAll(/\W/g, '');
rehashed[name] = e;
}
return rehashed;
}
function readAndHashIni(f) {
// read file
var content = read(f);
if (content == '')
throw("Could not read file: " + f);
var lines = content.split("\n");
var rehashed = {};
var item = '';
for (var i=0; i<lines.length; i++) {
var l = lines[i];
l = l.replace(/#.*$/, '');
if (l == '') continue;
if (l.match(/^\[.*\]\s*$/)) {
item = l.substring(1, l.length-1);
rehashed[item] = {};
} else if (item == '') {
throw("No Item declared for code: " + l);
} else if (l.match(/^\s*\w+\s*=/)) {
var p = l.match(/^\s*(\w+)\s*=\s*(\S.*)\s*$/);
// name/value pair lines
rehashed[item][p[1]] = p[2];
}
}
return rehashed;
}
// constants
var STAT_MAP = {
"Attack Speed": "aps",
"Attack": "attack",
"CD Reduction": "cdr",
"Critical-Hit Damage": "critdamage",
"Critical-Hit Rate": "critrate",
"Defense": "defense",
"Energy Rate": "charge",
"HP": "health",
"HP/5s": "recovery",
"Sp. Attack": "spattack",
"Sp. Defense": "spdefense",
"Speed": "movement",
};
var PERCENT_STATS = [
'cdr', 'critdamage', 'critrate', 'charge'
];
// parsing functions
function parseUnlock(json, code, lvl) {
var val = json["level"+lvl];
if (typeof(val) === 'undefined') {
val = 0;
} else {
var percAt = val.indexOf('%');
if (percAt != -1)
val = val.substring(0,percAt)/100.0;
}
if (typeof(code.unlock) !== 'undefined')
return code.unlock.replaceAll(/%/g, val);
return val;
}
// main function
function run(args) {
// check args
while (args.length && args[0].charAt(0) == '-') {
var arg = args.shift();
switch (arg) {
case '-h': case '--help':
print("./genitems [args?] [held_items.json " +
"[preprocitems.ini]]");
print(" -h/--help: Print this message");
quit(0);
default:
throw("Unknown argument: " + arg);
}
}
// get info
var items = readAndHashJson(scriptArgs && scriptArgs.length > 0
? scriptArgs[0] : "held_items.json");
var code = readAndHashIni(scriptArgs && scriptArgs.length > 1
? scriptArgs[1] : "preprocitems.ini");
// Define dummy item that prevents the need to check for a null item
items[''] = {};
// print header
print("// nessashot held item listing -- Generated file");
print("// https://github.com/jaretcantu/nessashot");
print("// " + (new Date()));
print("Item.LIST = {");
for (var k in items) {
var itm = items[k];
var c = code[k] || {};
print("\t" + (k==''?'""':k) + ": new Item(\"" + k + "\", [");
for (var l=0; l<30; l++) {
if (typeof(itm.stats) === 'undefined') {
print("\t\tnew Stats(),");
continue;
}
var statstr = '';
for (var s=0; s<itm.stats.length; s++) {
var stat = itm.stats[s];
if (statstr != '') statstr+= ', ';
var val;
if (l == 0) {
val = stat.initial;
} else {
if (typeof(stat.initial_diff)
!== 'undefined')
val = stat.initial_diff;
else
val = 0;
var mod = Math.ceil((1+l-stat.start)
/ (1+stat.skip));
val+= stat.increment * mod;
}
var statkey = STAT_MAP[stat.label];
if (PERCENT_STATS.indexOf(statkey) != -1)
val = (val/100.0).toFixed(3);
else if (stat.float)
val = val.toFixed(1);
else
val = Math.round(val);
statstr+= statkey + ': ' + val;
}
print("\t\tnew Stats({" + statstr + "}),");
}
var l1 = parseUnlock(itm, c, 1);
var l10 = parseUnlock(itm, c, 10);
var l20 = parseUnlock(itm, c, 20);
print("\t], [" + l1 + ", " + l10 + ", " + l20 + "], " +
(typeof(c.passive) !== 'undefined'
? c.passive : "Passive.DUMMY") +
"),");
}
// print footer
print("};");
};
run(scriptArgs);