-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgirl.cpp
346 lines (333 loc) · 13.7 KB
/
girl.cpp
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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
#include <fstream>
#include <iostream>
#include <unordered_map>
#include <sstream>
#include <vector>
#include <algorithm>
#include <cstdint>
const std::unordered_map<std::string, std::uint8_t> ram_letters = {
{" ", 0},
{"あ", 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},
{"、", 62}, {"。", 63}, {"*", 64}, {"#", 65}, {"!", 66}, {"?", 67},
{"「", 68}, {"」", 69}, {"ー", 70}, {"・", 71},
{"0", 72}, {"1", 73}, {"2", 74}, {"3", 75}, {"4", 76},
{"5", 77}, {"6", 78}, {"7", 79}, {"8", 80}, {"9", 81},
{"A", 82}, {"B", 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},
{"H", 128}, {"T", 129}, {"U", 130}, {"ED ", 131}, {"SP ", 132},
{"が", 128+15}, {"ぎ", 128+16}, {"ぐ", 128+17}, {"げ", 128+18}, {"ご", 128+19},
{"ざ", 128+20}, {"じ", 128+21}, {"ず", 128+22}, {"ぜ", 128+23}, {"ぞ", 128+24},
{"だ", 128+25}, {"ぢ", 128+26}, {"づ", 128+27}, {"で", 128+28}, {"ど", 128+29},
{"ば", 128+35}, {"び", 128+36}, {"ぶ", 128+37}, {"べ", 128+38}, {"ぼ", 128+39},
{"ガ", 128+90}, {"ギ", 128+91}, {"グ", 128+92}, {"ゲ", 128+93}, {"ゴ", 128+94},
{"ザ", 128+95}, {"ジ", 128+96}, {"ズ", 128+97}, {"ゼ", 128+98},
{"ダ", 128+99}, {"ヂ", 128+100}, {"ヅ", 128+101}, {"デ", 128+102}, {"ド", 128+103},
{"バ", 128+107}, {"ビ", 128+108}, {"ブ", 128+109}, {"ボ", 128+110},
{"ぱ", 128+35+80}, {"ぴ", 128+36+80}, {"ぷ", 128+37+80}, {"ぺ", 128+38+80}, {"ぽ", 128+39+80},
{"パ", 128+107+15}, {"ピ", 128+108+15}, {"プ", 128+109+15}, {"ポ", 128+110+15},
};
const std::unordered_map<std::string, std::uint8_t> rom_letters = {
{" ", 0},
{"あ", 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},
{"、", 62}, {"。", 63}, {"*", 64}, {"#", 65}, {"!", 66}, {"?", 67},
{"「", 68}, {"」", 69}, {"ー", 70}, {"・", 71},
{"0", 72}, {"1", 73}, {"2", 74}, {"3", 75}, {"4", 76},
{"5", 77}, {"6", 78}, {"7", 79}, {"8", 80}, {"9", 81},
{"A", 82}, {"B", 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},
{"H", 128}, {"T", 129}, {"U", 130},
// 133 134 negative space
{"ぁ", 135}, // Last name
{"ぃ", 136}, // First name
// 137 crash
// 138 kills the next character
// 139 140 141 142 negative space
// 143 crash
{"が", 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-175 are various hiragana characters with handakuten
{"ガ", 176}, {"ギ", 177}, {"グ", 178}, {"ゲ", 179}, {"ゴ", 180},
{"ザ", 181}, {"ジ", 182}, {"ズ", 183}, {"ゼ", 184},
{"ダ", 185}, {"ヂ", 186}, {"ヅ", 187}, {"デ", 188}, {"ド", 189},
{"バ", 190}, {"ビ", 191}, {"ブ", 192}, {"ボ", 193},
{"パ", 194}, {"ピ", 195}, {"プ", 196}, {"ポ", 197},
// 198-204 is one-seven spaces
// 205-207 negative space
// 208: うしろの少女
// 209: ・・・
// 210: ひびの 「
// 211: あゆみ 「
// 212: うつぎ 「
// 213: たざき 「
// 214: こまだ 「
// 215: こうちょう
// 216: ようこ
// 217: せんせい
// 218-235: crazy
// 236: negative space
// 237-247: Crazy
// 248: Space
// 249-253: Crazy
// 254: Newline
// 255: End of message
};
const std::unordered_map<char, int> fceux_widths = {
{'a', 4}, {'b', 4}, {'c', 4}, {'d', 4}, {'e', 4}, {'f', 4}, {'g', 4}, {'h', 4},
{'i', 1}, {'j', 3}, {'k', 4}, {'l', 1}, {'m', 5}, {'n', 4}, {'o', 4}, {'p', 4},
{'q', 4}, {'r', 4}, {'s', 4}, {'t', 3}, {'u', 4}, {'v', 4}, {'w', 5}, {'x', 4},
{'y', 4}, {'z', 4}, {'A', 5}, {'B', 5}, {'C', 5}, {'D', 5}, {'E', 5}, {'F', 5},
{'G', 5}, {'H', 5}, {'I', 3}, {'J', 5}, {'K', 5}, {'L', 5}, {'M', 5}, {'N', 5},
{'O', 6}, {'P', 5}, {'Q', 5}, {'R', 5}, {'S', 5}, {'T', 5}, {'U', 5}, {'V', 5},
{'W', 5}, {'X', 5}, {'Y', 5}, {'Z', 5}, {'-', 4}, {'!', 2}, {'?', 5}, {':', 2},
{'"', 4}, {'\'', 2}, {'.', 2}, {' ', 5}, {'\\', 0}, {',', 2}, {'0', 5}, {'1', 5},
{'2', 5}, {'3', 5}, {'4', 5}, {'5', 5}, {'6', 5}, {'7', 5}, {'8', 5}, {'9', 5},
{'*', 5}, {'/', 3}
};
int get_fceux_width(const std::string& str)
{
int result = 0;
for (auto c : str) {
result += fceux_widths.at(c);
}
result += str.size() - 1;
return result;
}
int get_line_spacing(const std::string& line)
{
auto pos = line.find(": \"");
if (pos == std::string::npos) return -1;
else return std::min(get_fceux_width(line.substr(0, pos + 3)), 50);
}
void write_letter(std::vector<std::uint8_t>& output, const std::string& letter)
{
auto this_char = ram_letters.at(letter);
if (!this_char) return;
if (this_char >= 128 + 107 + 15) {
this_char -= (128 + 15);
output.push_back(61);
if (this_char == 26) output.push_back(1);
else output.push_back(this_char);
}
else if (this_char >= 128 + 35 + 80) {
this_char -= (128 + 80);
output.push_back(61);
if (this_char == 26) output.push_back(1);
else output.push_back(this_char);
}
else if (this_char >= 128) {
this_char -= 128;
output.push_back(60);
if (this_char == 26) output.push_back(1);
else output.push_back(this_char);
}
else {
if (this_char == 26) output.push_back(1);
else output.push_back(this_char);
}
}
std::string space_english(const std::vector<std::string>& lines, int position)
{
std::vector<int> spacings;
for (const auto& line : lines) {
spacings.push_back(get_line_spacing(line));
}
const int spacing = *std::ranges::max_element(spacings);
std::string result;
const auto C_max_width = position == 4 ? 207 : 143;
for (int i = 0; i < ssize(lines); ++i) {
auto line = lines[i];
while (!line.empty()) {
try {
auto pos = line.find_first_not_of(' ');
if (pos != std::string::npos) line.erase(0, pos);
line.insert(0, std::string(spacing - get_line_spacing(line), '\\'));
pos = 0;
auto previous_pos = 0UL;
while (get_fceux_width(line.substr(0, pos)) < C_max_width) {
if (pos == std::string::npos) {
previous_pos = std::string::npos;
break;
}
previous_pos = pos;
pos = line.find(' ', pos + 1);
}
if (previous_pos == std::string::npos) {
result += line;
result.push_back('\n');
break;
}
else {
result += line.substr(0, previous_pos);
result.push_back('\n');
line = line.substr(previous_pos);
}
}
catch (std::out_of_range& e) {
std::cerr << "Error found in English line " << line << "\n";
throw;
}
}
}
result.push_back('\0');
return result;
}
void write_messages()
{
auto messages_file = std::ifstream("girl_messages.txt");
struct output_t {
std::vector<std::uint8_t> japanese;
std::string english;
};
auto output_data = std::vector<output_t>();
int i = 0;
auto current_japanese = std::vector<std::uint8_t>();
auto current_english = std::vector<std::string>();
auto position = 4;
for (std::string line; std::getline(messages_file, line);) {
if (i < 4) {
try {
if (!line.empty()) {
if (line[0] != ' ' and
static_cast<std::uint8_t>(line[0]) <= 127) {
position = line[0] - '0';
line = line.substr(1);
}
line = line.substr(line.find_first_not_of(' '));
for (int j = 0; j < ssize(line) / 3; ++j) {
auto letter = rom_letters.at(line.substr(j*3, 3));
if (letter) {
if (letter == 26) current_japanese.push_back(5);
else current_japanese.push_back(letter);
}
}
}
}
catch (std::out_of_range& e) {
std::cerr << "Error found in Japanese line " << line << "\n";
throw;
}
}
else if (i < 8) { }
else {
if (!line.empty()) {
current_english.push_back(std::move(line));
}
}
++i;
if (i == 12) {
i = 0;
auto english = space_english(current_english, position);
english.insert(english.begin(), position);
position = 4;
current_japanese.push_back(0);
output_data.push_back(output_t{
std::move(current_japanese),
std::move(english)
});
current_japanese = {};
current_english = {};
}
}
std::sort(output_data.begin(), output_data.end(),
[](const auto& a, const auto& b)
{
return a.japanese < b.japanese;
});
auto output_file = std::ofstream("girl_messages.bin", std::ios::binary);
for (const auto& [japanese, english] : output_data) {
output_file.write((char*)japanese.data(), japanese.size());
output_file.write((char*)english.data(), english.size());
}
}
void write_options()
{
auto options_file = std::ifstream("girl_options.txt");
auto output = std::vector<std::uint8_t>();
output.reserve(100'000);
bool japanese = true;
for (std::string line; std::getline(options_file, line);) {
if (japanese) {
try {
for (int i = 0; i < ssize(line) / 3; ++i) {
write_letter(output, line.substr(i*3, 3));
}
}
catch (std::out_of_range& e) {
std::cerr << "Error found in Japanese option " << line << "\n";
throw;
}
}
else {
auto pos = 0UL;
auto previous_pos = 0UL;
while (get_fceux_width(line.substr(0, pos)) < 56) {
if (pos == std::string::npos) {
previous_pos = std::string::npos;
break;
}
previous_pos = pos;
pos = line.find(' ', pos + 1);
}
if (previous_pos != std::string::npos) {
line[previous_pos] = '\n';
}
for (auto c : line) {
output.push_back(static_cast<std::uint8_t>(c));
}
}
output.push_back(0);
japanese = !japanese;
}
auto output_file = std::ofstream("girl_options.bin", std::ios::binary);
output_file.write((char*)output.data(), output.size());
}
int main()
{
write_messages();
write_options();
}