-
Notifications
You must be signed in to change notification settings - Fork 0
/
randomizer.js
183 lines (142 loc) · 7.24 KB
/
randomizer.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
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
175
176
177
178
179
180
181
182
183
var fs = require('fs'),
xml2js = require('xml2js'),
seedrandom = require('seedrandom'),
Promise = require("promise");
var parser = new xml2js.Parser();
var randomProperty = function (obj) {
var keys = Object.keys(obj)
return keys[ keys.length * Math.random() << 0];
};
var banned = ['gargoyle', 'ghost', 'pawn', 'queen', 'bishop', 'rook', 'knight'];
var aoeEnemies = ['yeti', 'mushroom'];
var bannedItems = [];
var getRandomEnemy = function(clone) {
var enemy;
while (banned.indexOf(enemy = randomProperty(clone)) != -1
|| !enemy
|| !clone[enemy][0].$.id) {
if(!enemy) return false;
delete clone[enemy];
}
if(!enemy) return false;
var obj = clone[enemy];
var ranIndex = obj.length * Math.random() << 0;
var objEnemy = JSON.parse(JSON.stringify(clone[enemy].splice(ranIndex, 1)[0]));
if(!clone[enemy].length) delete clone[enemy];
return objEnemy;
};
var getRandomItem = function(clone) {
var item;
while (bannedItems.indexOf(item = randomProperty(clone)) != -1
|| !item
|| clone[item][0].$.levelEditor=="False"
|| !clone[item][0].$.coinCost) {
if(!item) return false;
delete clone[item];
}
if(!item) return false;
var obj = clone[item];
var ranIndex = obj.length * Math.random() << 0;
var objItem = JSON.parse(JSON.stringify(clone[item].splice(ranIndex, 1)[0]));
if(!clone[item].length) delete clone[item];
return objItem;
};
var randomizeND = function(options) {
options.seed = options.seed || (Math.floor(Math.random() * 16777217)).toString();
seedrandom(options.seed, { global: true });
var switches = {
enemies: '',
items: ''
}
var xmlToUse = options.original ? '/necrodancer-original.xml' : '/necrodancer-original-amplified.xml';
var promise = new Promise(function(resolve, reject) {
fs.readFile(__dirname + xmlToUse, function(err, data) {
parser.parseString(data, function (err, result) {
var origXMLObj = result,
clone = JSON.parse(JSON.stringify(origXMLObj)),
enemies = origXMLObj.necrodancer.enemies[0],
items = origXMLObj.necrodancer.items[0];
//switch ids and stats/optional stats
for(var enemy in enemies) {
if(banned.indexOf(enemy) != -1 || !enemies[enemy][0].$.id) continue;
for(var i = 0; i < enemies[enemy].length; i++) {
var ranEnemy = getRandomEnemy(clone.necrodancer.enemies[0]);
if(!ranEnemy) break;
switches.enemies += (enemies[enemy][i].$.friendlyName+new Array(23).join(' ')).slice(0, 23)+' → '+(ranEnemy.$.friendlyName||ranEnemy.$.id)+'\n';
var isMiniboss = false;
if(enemies[enemy][i].optionalStats && enemies[enemy][i].optionalStats[0].$ && enemies[enemy][i].optionalStats[0].$.miniboss=="True") {
isMiniboss = true;
}
delete enemies[enemy][i].optionalStats;
delete enemies[enemy][i].bouncer;
if(ranEnemy.optionalStats) enemies[enemy][i].optionalStats = ranEnemy.optionalStats;
else if(isMiniboss) enemies[enemy][i].optionalStats = [{$:{}}];
if(ranEnemy.bouncer) enemies[enemy][i].bouncer = ranEnemy.bouncer;
if(ranEnemy.stats[0].$.movement == "custom") {
// custom movement on enemies without it doesn't work
ranEnemy.stats[0].$.movement = enemies[enemy][i].stats[0].$.movement;
}
enemies[enemy][i].stats = ranEnemy.stats;
if(!options.unfair) {
// if "fair" aoe enemies can't have every beat movement
if(aoeEnemies.indexOf(enemy) != -1 && enemies[enemy][i].stats[0].$.beatsPerMove=="1") {
enemies[enemy][i].stats[0].$.beatsPerMove = "2";
}
// if "fair" and movement 1 every beat lower health
if(enemies[enemy][i].stats[0].$.beatsPerMove=="1") {
enemies[enemy][i].stats[0].$.health = "1";
}
}
// set food shopkeeper to have only one health to prevent crashing
if(enemies[enemy][i].$.id === "603") {
enemies[enemy][i].stats[0].$.health = "1";
}
if(enemies[enemy][i].optionalStats) {
if(isMiniboss) enemies[enemy][i].optionalStats[0].$.miniboss = "True";
else if(enemies[enemy][i].optionalStats[0].$ && enemies[enemy][i].optionalStats[0].$.miniboss) delete enemies[enemy][i].optionalStats[0].$.miniboss;
}
}
}
// switch items
for(var item in items) {
if(banned.indexOf(item) != -1 || items[item][0].$.levelEditor=="False" || !items[item][0].$.coinCost) continue;
for(var i = 0; i < items[item].length; i++) {
var ranItem = getRandomItem(clone.necrodancer.items[0]);
if(!ranItem) break;
switches.items += (items[item][i].$.flyaway.replace(/(\|\d+\||\|)/g,'')+new Array(28).join(' ')).slice(0, 28)+' → '+ranItem.$.flyaway.replace(/(\|\d+\||\|)/g,'')+'\n';
var switchArgs = [
'chestChance',
'shopChance',
'urnChance',
'coinCost',
'lockedChestChance',
'lockedShopChance',
//'slot'
];
for(var j = 0; j < switchArgs.length; j++) {
if(items[item][i].$[switchArgs[j]]) delete items[item][i].$[switchArgs[j]];
if(ranItem.$[switchArgs[j]]) items[item][i].$[switchArgs[j]] = ranItem.$[switchArgs[j]];
}
}
}
origXMLObj.necrodancer.enemies[0] = enemies;
origXMLObj.necrodancer.items[0] = items;
var builder = new xml2js.Builder();
var xml = builder.buildObject(origXMLObj);
xml = xml.replace(/^(<\?xml[^?]+\?>)/,'$1\n<!-- seed: '+options.seed+' -->')
+ '<!-- \n**** Enemy switches ****\n'
+ switches.enemies
+ '\n**** Item chances switches ****\n'
+ switches.items
+ '-->';
if(options.writeFile) {
fs.writeFile(options.dest || __dirname + '/necrodancer.xml', xml);
}
console.log(Date()+' - Generated XML for seed: '+options.seed);
resolve(xml);
});
});
});
return promise;
}
module.exports = randomizeND;