-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmain.cpp
208 lines (166 loc) · 6.39 KB
/
main.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
#include "Item.h"
#include "Lair.h"
#include "Log.h"
#include "Random.h"
#include "Randomizer.h"
#include "ROMCheck.h"
#include "ROMUpdate.h"
#include "Sprite.h"
#include <fstream>
#include <iostream>
#include <stdlib.h>
#include <conio.h>
#define ROM_FILE_NAME "Soul Blazer (U) [!].smc"
#define MOD_ROM_FILE_NAME "Soul Blazer Randomized.smc"
#define SEED_FILE_NAME "seed"
#define HEADER_OFFSET 0x200
#define SEED_SIZE 10
#define MAX_NUMBER_OF_TRIES 5
//#define DONT_CREATE_SEED_FILE
using namespace std;
int main ( int argc, char** argv ) {
/****************************\
|* Check the original ROM *|
\****************************/
ROMStatus OriginalROMStatus;
/* Check that the ROM file is there, make the fstream instance */
fstream ROMFile(ROM_FILE_NAME, ios::in | ios::out | ios::binary | ios::ate);
if (!ROMFile.is_open()) {
cout << "ROM file \"" << ROM_FILE_NAME << "\" is not found!\n";
return 1;
}
else {
/* Check if this is the headered or the un-headered ROM */
OriginalROMStatus = CheckOriginalROM(ROMFile);
if (OriginalROMStatus == UNKNOWN) {
cout << "File \"" << ROM_FILE_NAME << "\" is not a known original Soul Blazer (U) ROM!\n";
return 1;
}
else {
cout << "This ROM is " << (OriginalROMStatus == UNHEADERED ? "un-" : "") << "headered.\n";
}
}
ROMFile.close();
ROMFile.clear();
/*********************\
|* Check Seed file *|
\*********************/
long Seed = 0;
/* Initialize randomness with provided seed, if any */
ifstream SeedFile(SEED_FILE_NAME, ios::in | ios::binary);
if (!SeedFile.is_open()) {
Seed = Random::RandomInit(0);
#ifndef DONT_CREATE_SEED_FILE
ofstream NewSeedFile(SEED_FILE_NAME);
NewSeedFile << Seed;
NewSeedFile.close();
#endif // DONT_CREATE_SEED_FILE
}
else {
char SeedChar[SEED_SIZE+1];
SeedFile.seekg(0, ios::beg);
SeedFile.readsome(SeedChar, SEED_SIZE);
SeedChar[SEED_SIZE] = '\0';
Seed = atoi(SeedChar);
Random::RandomInit(Seed);
}
SeedFile.close();
SeedFile.clear();
/***************************************************\
|* Delete old modified ROM / backup original ROM *|
\***************************************************/
/* If a modified ROM exists, delete it first */
int RemoveResult = remove(MOD_ROM_FILE_NAME);
if (RemoveResult == 0) {
cout << "Previous randomized ROM detected and removed.\n";
}
/* Back up the original ROM */
ifstream ROMFileOriginal(ROM_FILE_NAME, ios::binary);
ofstream ROMFileCopy (MOD_ROM_FILE_NAME, ios::binary);
ROMFileOriginal.seekg(0, ios::end);
long ROMFileSize = ROMFileOriginal.tellg();
if (OriginalROMStatus == HEADERED) {
/* For headered ROM, ignore the first 512 bytes */
ROMFileSize -= HEADER_OFFSET;
}
if(ROMFileOriginal.is_open() && ROMFileCopy.is_open()) {
unsigned char *DataBuffer = new unsigned char[ROMFileSize];
if (OriginalROMStatus == HEADERED) {
/* For headered ROM, ignore the first 512 bytes */
ROMFileOriginal.seekg(HEADER_OFFSET, ios::beg);
}
else {
ROMFileOriginal.seekg(0, ios::beg);
}
ROMFileOriginal.read((char*)DataBuffer, ROMFileSize);
ROMFileCopy.write((char*)DataBuffer, ROMFileSize);
delete[] DataBuffer;
}
else if(!ROMFileCopy.is_open()) {
cout << "Failure backing up the ROM!\n";
return 1;
}
else if(!ROMFileOriginal.is_open()) {
cout << "Failure opening the original ROM for copying!\n";
return 1;
}
ROMFileOriginal.close();
ROMFileCopy.close();
ROMFileOriginal.clear();
ROMFileCopy.clear();
/****************\
|* Randomize! *|
\****************/
cout << endl;
cout << "Starting randomization.\n";
/* Initialize the final lists of randomized lairs, chests and sprites */
vector<Lair> RandomizedLairList;
vector<Item> RandomizedItemList;
vector<Sprite> RandomizedSpriteList;
/* Re-open ROM to be modified */
ROMFile.open(MOD_ROM_FILE_NAME, ios::in | ios::out | ios::binary | ios::ate);
/* Call the main algorithm to randomize the progression through the game:
==> randomize item locations and revived NPCs */
int RandomizationTry;
bool RandomizationStatus = false;
for (RandomizationTry = 0; RandomizationTry < MAX_NUMBER_OF_TRIES; RandomizationTry++) {
RandomizationStatus = Randomizer::RandomizeProgression(RandomizedLairList,
RandomizedItemList,
ROMFile);
if (RandomizationStatus) {
break;
}
}
if (!RandomizationStatus) {
cout << " . . . Randomization failed!\n";
return 1;
}
cout << " . . . Randomization succeeded in " << RandomizationTry + 1
<< (RandomizationTry == 0 ? " try.\n" : " tries.\n");
cout << "Starting ROM modification.\n";
/* Randomize monster lair contents: enemy types, lair types, number of enemies and spawn rates */
Randomizer::RandomizeLairContents(RandomizedLairList);
/* Randomize static enemies in maps */
Randomizer::RandomizeMapSprites(RandomizedSpriteList, ROMFile);
/* Modify the ROM with the randomized lists */
ROMUpdate::ROMUpdateLairs(RandomizedLairList, ROMFile);
ROMUpdate::ROMUpdateMapSprites(RandomizedSpriteList, ROMFile);
ROMUpdate::ROMUpdateTextAndItems(RandomizedLairList,
RandomizedItemList,
ROMFile,
Seed);
/* Close the ROM file */
ROMFile.close();
ROMFile.clear();
cout << " . . . ROM modification complete.\n";
cout << "Starting Spoiler Log creation.\n";
/* Generate the Spoiler Log */
Log::CreateSpoilerLog(RandomizedLairList, RandomizedItemList);
cout << " . . . Spoiler Log created.\n";
/* Ask user to press any key to close the program */
cout << endl;
cout << "RandoBlazer execution was successful!\n"
<< "Press any key to exit the program.\n";
_getch();
return 0;
}