-
Notifications
You must be signed in to change notification settings - Fork 0
/
simplefm2.cc
146 lines (129 loc) · 3.84 KB
/
simplefm2.cc
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
#include "simplefm2.h"
#include "cc-lib/util.h"
#include "tasbot.h"
using namespace std;
vector<uint8> SimpleFM2::ReadInputs(const string &filename) {
vector<string> contents = Util::ReadFileToLines(filename);
vector<uint8> out;
for (int i = 0; i < contents.size(); i++) {
const string &line = contents[i];
if (line.empty() || line[0] != '|') {
continue;
}
// Parse the line.
if (line.size() < 12) {
fprintf(stderr, "Illegal line: [%s]\n", line.c_str());
abort();
}
if (!(line[1] == '0' ||
(line[1] == '2' && out.empty()))) {
fprintf(stderr, "Command must be zero except hard "
"reset in first input: [%s]\n", line.c_str());
abort();
}
/*
|2|........|........||
|0|....T...|........||
*/
const string player = line.substr(3, 8);
uint8 command = 0;
for (int j = 0; j < 8; j++) {
if (player[j] != '.') {
command |= (1 << (7 - j));
}
}
out.push_back(command);
}
return out;
}
void SimpleFM2::WriteInputs(const string &outputfile,
const string &romfilename,
const string &romchecksum,
const vector<uint8> &inputs) {
vector<string> empty;
WriteInputsWithSubtitles(outputfile, romfilename, romchecksum,
inputs, empty);
}
void SimpleFM2::WriteInputsWithSubtitles(const string &outputfile,
const string &romfilename,
const string &romchecksum,
const vector<uint8> &inputs,
const vector<string> &subtitles) {
// XXX Create one of these by hashing inputs.
string fakeguid = "FDAEE33C-B32D-B38C-765C-FADEFACE0000";
FILE *f = fopen(outputfile.c_str(), "wb");
fprintf(f,
"version 3\n"
"emuversion 9815\n"
"romFilename %s\n"
"romChecksum %s\n"
"guid %s\n"
// Read from settings?
"palFlag 0\n"
"NewPPU 0\n"
"fourscore 0\n"
"microphone 0\n"
"port0 1\n"
"port1 1\n"
"port2 0\n"
// ?
"FDS 0\n"
"comment author tasbot-simplefm2\n",
romfilename.c_str(),
romchecksum.c_str(),
fakeguid.c_str());
const string *last = NULL;
for (int i = 0; i < subtitles.size(); i++) {
if (last == NULL || *last != subtitles[i]) {
fprintf(f, "subtitle %d %s\n", i, subtitles[i].c_str());
}
last = &subtitles[i];
}
for (int i = 0; i < inputs.size(); i++) {
fprintf(f, "|%c|", (i == 0) ? '2' : '0');
static const char gamepad[] = "RLDUTSBA";
for (int j = 0; j < 8; j++) {
fprintf(f, "%c",
(inputs[i] & (1 << (7 - j))) ? gamepad[j] : '.');
}
fprintf(f, "|........||\n");
}
fclose(f);
}
string SimpleFM2::InputToString(uint8 input) {
char f[9] = {0};
static const char gamepad[] = "RLDUTSBA";
for (int j = 0; j < 8; j++) {
f[j] = (input & (1 << (7 - j))) ? gamepad[j] : '.';
}
return (string)f;
}
string SimpleFM2::InputToColorString(uint8 input) {
string color = "";
string out;
static const char DOTCOLOR[] = "#999";
static const char gamepad[] = "RLDUTSBA";
static const char *colors[] = {
"#000",
"#000",
"#000",
"#000",
"#009",
"#009",
"#900",
"#900",
};
for (int j = 0; j < 8; j++) {
bool button_down = input & (1 << (7 - j));
string this_color = button_down ? colors[j] : DOTCOLOR;
char c = button_down ? gamepad[j] : '.';
if (color != this_color) {
if (color != "") out += "</span>";
out += "<span style=\"color:" + this_color + "\">";
color = this_color;
}
out += StringPrintf("%c", c);
}
if (color != "") out += "</span>";
return out;
}