Repository navigation
Expand file tree
/
Copy pathscript.js
More file actions
311 lines (247 loc) · 8.74 KB
/
Copy pathscript.js
File metadata and controls
311 lines (247 loc) · 8.74 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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
// GRAB EXTERNAL STUFF WE NEED
const slp = require('@slippi/slippi-js');
const SlippiGame = slp.default;
const fs = require('fs');
const path = require('path');
// -- COMBO FINDING VARIABLES --
// make sure folders exist before running script
const replay_path = "replays";
const output_path_and_filename = "clipShows/moments";
const using_absolute_paths = false; // absolute path would be something like "D:/schmoo/replays"
// times are in frames (60 frames in a second)
const skipCPUmatches = false;
const minimumGameTime = 1200; // 20 seconds
const captureTimePreInput = 600; // 10 seconds
const captureTimePostInput = 60; // 1 second
const captureLockoutTime = 300; // 5 seconds
const limitCharacters = false;
// possible characters: "Captain Falcon", "Donkey Kong", "Fox", "Mr. Game & Watch", "Kirby", "Bowser", "Link", "Luigi", "Mario", "Marth", "Mewtwo", "Ness", "Peach", "Pikachu", "Ice Climbers", "Jigglypuff", "Samus", "Yoshi", "Zelda", "Sheik", "Falco", "Young Link", "Dr. Mario", "Roy", "Pichu", "Ganondorf"
const acceptableCharacters = ["Fox"];
// the input it looks for to find moments.
// possible inputs: "A", "B", "X", "Y", "Z", "L", "R", "Start", "DpadRight", "DpadLeft", "DpadUp", "DpadDown"
// these are case sensitive
const captureInputCommand = ["L", "R", "Z"];
// -- COMBO FINDING VARIABLES END --
// -- MELEE CONSTANTS --
const Button = {
none : 0x0000,
dpadLeft : 0x0001,
dpadRight : 0x0002,
dpadDown : 0x0004,
dpadUp : 0x0008,
z : 0x0010,
r : 0x0020,
l : 0x0040,
a : 0x0100,
b : 0x0200,
x : 0x0400,
y : 0x0800,
start : 0x1000
}
// -- MELEE CONSTANTS END --
// ------- BIG BOI CODE --------
// Setting up a few useful functions
function GetCharName (players, p) {
var char_name = "";
for (var m=0;m<players.length;m++) {
if (players[m].playerIndex == p) {
if (players[m].characterId > 25) {
char_name = "Special";
}
else {
char_name = slp.characters.getCharacterName(players[m].characterId);
}
break;
}
}
return char_name;
}
function CharIsAcceptable(players, p) {
if (!limitCharacters) return true;
return _CharIsAcceptable(GetCharName(players, p));
}
function _CharIsAcceptable(character) {
for (var i=0;i<acceptableCharacters.length;i++) {
if (acceptableCharacters[i] == character) return true;
}
return false;
}
function GetLastFrame(game) {
var lf = game.getLatestFrame();
if (lf == null) return null;
if (lf == undefined) return null;
return lf.frame;
}
function logProgress(count, total){
process.stdout.clearLine();
process.stdout.cursorTo(0);
process.stdout.write(count + '/' + total);
}
function logMessage(message) {
process.stdout.clearLine();
process.stdout.cursorTo(0);
console.log(message);
}
function GetTimeText(n) {
var t = n/60;
var min = (Math.floor(t / 60)).toString();
var sec = (t % 60).toFixed(2);
return ((min.length < 2) ? "0" + min : min) + "m" + ((sec.length < 5) ? "0" + sec[0] : sec[0] + sec[1]) +"s";
}
function BCheck(buttons, buttonToCheck) {
// check if button is being held using bitwise
return (buttons & buttonToCheck) != Button.none;
}
// setting up input checks for each possible input
const inputChecks = {
"A" : function(pre) { return BCheck(pre.physicalButtons, Button.a); },
"B" : function(pre) { return BCheck(pre.physicalButtons, Button.b); },
"X" : function(pre) { return BCheck(pre.physicalButtons, Button.x); },
"Y" : function(pre) { return BCheck(pre.physicalButtons, Button.y); },
"Z" : function(pre) { return BCheck(pre.physicalButtons, Button.z); },
"L" : function(pre) { return (BCheck(pre.physicalButtons, Button.l) || pre.physicalLTrigger > 0.5); },
"R" : function(pre) { return (BCheck(pre.physicalButtons, Button.r) || pre.physicalRTrigger > 0.5); },
"Start" : function(pre) { return BCheck(pre.physicalButtons, Button.start); },
"DpadRight" : function(pre) { return BCheck(pre.physicalButtons, Button.dpadRight); },
"DpadLeft" : function(pre) { return BCheck(pre.physicalButtons, Button.dpadLeft); },
"DpadUp" : function(pre) { return BCheck(pre.physicalButtons, Button.dpadUp); },
"DpadDown" : function(pre) { return BCheck(pre.physicalButtons, Button.dpadDown); }
}
// Now heading into the main logic
// find all slp files
function SearchDirectory(cur_path) {
var dirs = fs.readdirSync(cur_path, {withFileTypes : true});
for (var i=0;i<dirs.length;i++) {
if (dirs[i].isFile()) {
if (path.extname(dirs[i].name) == ".slp") {
files.push((using_absolute_paths ? "" : (__dirname + "/") ) + cur_path + "/" + dirs[i].name);
}
}
else if (dirs[i].isDirectory()) {
SearchDirectory(cur_path + "/" + dirs[i].name);
}
}
}
var files = [];
SearchDirectory(replay_path);
console.log(`Found ${files.length} replay files!`);
// create output json object (what dolphin reads)
var output = {
"mode": "queue",
"replay": "",
"isRealTimeMode": false,
"outputOverlayFiles": true,
"queue": []
};
var momentCount = 0;
logProgress(0, files.length);
// for each file
for (var i=0;i<files.length;i++) {
logProgress(i+1, files.length);
// grab parsed game object from slp-parser-js
const game = new SlippiGame(files[i]);
const settings = game.getSettings();
// check if we should skip the file or if its broken
if (skipCPUmatches) {
var skip = false;
for (var n=0;n<settings.players.length;n++) {
// if cpu
if (settings.players[n].type != 0) {
skip = true;
break;
}
}
if (skip) continue;
}
const lastframe = GetLastFrame(game);
if (lastframe == null) {
logMessage("Bad file!");
continue;
}
/*if (!game.getGameEnd()) {
console.log("Bad file! Did the game crash?");
continue;
}*/
const frames = game.getFrames();
if (frames == null) {
logMessage("Bad file!");
continue;
}
// if game is too short
if (lastframe < minimumGameTime) continue;
// reset lockout
var lockout = 0;
// for each frame in the game
for (var n=-123;n<lastframe;n++) {
if (frames[n] == null) break;
// if currently locked out, skip test
if (lockout > 0) {
lockout--;
}
else {
// for each player
for (var p=0;p<frames[n].players.length;p++) {
if (frames[n].players[p] == null) continue;
// grab inputs
var pre = frames[n].players[p].pre;
// check input for the capture input command
var foundInputCommand = true;
for (var a=0;a<captureInputCommand.length;a++) {
if (!inputChecks[captureInputCommand[a]](pre)) {
foundInputCommand = false;
break;
}
}
if (foundInputCommand) {
// if input was found during (Ready! GO!)
if (n < 45) {
// ENDER FROM PREVIOUS GAME
// if prev game exists
if (i > 0) {
const prev_game = new SlippiGame(files[i-1]);
const prev_settings = prev_game.getSettings();
const prev_lastframe = GetLastFrame(prev_game);
if (prev_lastframe == null) {
logMessage("Found Ender with bad file!");
continue;
}
if (CharIsAcceptable(prev_settings.players, p)) {
output.queue[momentCount] = {
"path" : files[i-1],
"startFrame" : Math.max(-123, prev_lastframe-captureTimePreInput),
"endFrame" : prev_lastframe
}
lockout = captureLockoutTime;
momentCount++;
logMessage(`[MOMENT #${momentCount.toString().padStart(1, '0')}] Port ${p+1} | ${GetCharName(prev_settings.players, p)} | ${slp.stages.getStageName(prev_settings.stageId)} | ${GetTimeText(prev_lastframe)} into game | ENDER!`);
}
}
}
else {
if (CharIsAcceptable(settings.players, p)) {
// FOUND MOMENT!
output.queue[momentCount] = {
"path" : files[i],
"startFrame" : Math.max(-123, n-captureTimePreInput),
"endFrame" : Math.min(lastframe, n+captureTimePostInput)
}
lockout = captureLockoutTime;
momentCount++;
logMessage(`[MOMENT #${momentCount.toString().padStart(1, '0')}] Port ${p+1} | ${GetCharName(settings.players, p)} | ${slp.stages.getStageName(settings.stageId)} | ${GetTimeText(n)} into game`);
}
}
}
}
}
}
}
console.log("\n");
console.log(`Found ${momentCount} moments!`);
// now write the output to a json file
let jsonText = JSON.stringify(output);
fs.writeFile(`${output_path_and_filename}.json`, jsonText, function(err) {
if(err) {
return console.log(err);
}
console.log("Replay clip queue file was saved!");
});