-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLevel.h
More file actions
366 lines (301 loc) · 9.17 KB
/
Level.h
File metadata and controls
366 lines (301 loc) · 9.17 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
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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
#pragma once
class LevelSprite {
public:
bool is_lua = false;
uint_fast8_t number = 0;
uint_fast16_t x_pos = 0;
uint_fast16_t y_pos = 0;
uint_fast8_t newstate = 0;
uint_fast8_t dir = 0;
void spawn()
{
spawnSpriteJFKMarioWorld(number, newstate, x_pos, y_pos, dir, is_lua);
}
};
vector<LevelSprite> LevelSprites; //U know what they say. All toasters toast.
void CheckSpritesInCam(int x_pos)
{
for (int i = 0; i < LevelSprites.size(); i++)
{
LevelSprite& CSprite = LevelSprites[i];
int x = int(CSprite.x_pos);
if (x > (x_pos - spawn_bound_x) && x < (x_pos + spawn_bound_x))
{
CSprite.spawn();
LevelSprites.erase(LevelSprites.begin() + i);
i--;
}
}
for (uint_fast8_t i = 0; i < 128; i++)
{
int x = RAM[0x2100 + i] + RAM[0x2180 + i] * 256;
if (x > (x_pos - spawn_bound_x) && x < (x_pos + spawn_bound_x))
{
if (!(RAM[0x2A80 + i] & 0b00000010))
{
RAM[0x2A80 + i] |= 0b00000010;
}
}
}
}
class Level
{
public:
Level() {}
//vector<string, int> data;
string current_level;
double start_x = 16;
double start_y = 16;
int chunks = 0;
unordered_map<string, uint_fast32_t> level_data;
uint_fast32_t request_level_entry(string name)
{
auto entry = level_data.find(name);
if (entry != level_data.end())
{
return entry->second;
}
cout << green
<< "[Level Manager] Couldn't find entry " << name
<< " : not created yet?" << white << endl;
return 0;
}
void add_entry(string name, uint_fast32_t value)
{
cout << green
<< "[Level Manager] Added entry " << name
<< " = " << dec << value << white << endl;
level_data.insert(make_pair(name, value));
}
string LoadLevelData(string FILENAME)
{
ifstream file;
file.open(FILENAME);
if (file.is_open())
{
stringstream strStream;
strStream << file.rdbuf(); //read the file
string str = strStream.str(); //str holds the content of the file
file.close();
return str;
}
file.close();
return "Error";
}
void LoadLevelFromFile(string FILENAME, uint_fast16_t num)
{
current_level = FILENAME;
cout << green
<< "[Level Manager] Loading " << FILENAME
<< white << endl;
LoadLevelFromString(LoadLevelData(FILENAME), num);
}
void LoadLevelFromString(string DLevel, uint_fast16_t num)
{
chunks = 0;
lua_loaded = false;
reset_map();
cout << green
<< "[Level Manager] Processing level string.."
<< white << endl;
string status;
string line;
istringstream str(DLevel); // string
LevelSprites.clear();
//int spr_index = 0;
while (getline(str, line)) {
// using printf() in all tests for consistency
if (line != "")
{
//cout << line << endl;
string CHECK = line.substr(0, 2);
if (CHECK != "//")
{
if (line.substr(0, 1) == "[")
{
status = line.substr(1, line.length() - 2);
cout << green << "[Level Manager] Status = " << status << white << endl;
//data.push_back();
continue;
}
if(status == "level_config")
{
line.erase(remove_if(line.begin(), line.end(), ::isspace),
line.end());
auto delimiterPos = line.find("=");
auto name = line.substr(0, delimiterPos);
auto value = line.substr(delimiterPos + 1);
if (name == "music")
{
ASM.Write_To_Ram(0x1DFB, stoi(value, nullptr, 16), 1);
continue;
}
if (name == "background")
{
ASM.Write_To_Ram(0x3F05, stoi(value, nullptr, 16), 1);
continue;
}
if (name == "size_x")
{
ASM.Write_To_Ram(0x3F00, stoi(value), 2);
mapWidth = stoi(value);
}
if (name == "size_y")
{
ASM.Write_To_Ram(0x3F02, stoi(value), 2);
mapHeight = stoi(value);
}
// cout << green << "[Level Manager] adding level config entry " << name << " = " << value << white << endl;
add_entry(name, stoi(value));
}
if (status == "scripts")
{
line.erase(remove_if(line.begin(), line.end(), ::isspace),
line.end());
auto delimiterPos = line.find("=");
auto name = line.substr(0, delimiterPos);
auto value = line.substr(delimiterPos + 1);
auto type = name.substr(name.length() - 3);
//cout << "[load script] type " << type << endl;
cout << green
<< "[Level Manager] Loading Script " << name << " of type " << type
<< white << endl;
// cout << green << "[Level Manager] adding level config entry " << name << " = " << value << white << endl;
if (type == "lua")
{
lua_loadfile("Levels/" + int_to_hex(num) + "/" + name);
lua_loaded = true;
lua_run_init();
}
if (type == "asm")
{
ASM.load_asm("Levels/" + int_to_hex(num) + "/" + name, location_rom_levelasm);
if (value == "init")
{
ASM.start_JFK_thread();
}
else
{
asm_loaded = true;
}
}
}
if (status == "level_data")
{
//cout << "Level loading Line = " + line << endl;
vector<string> v = split(line.c_str(), ',');
if (v.size() == 5) //Format type 1.
{
chunks++;
int tile = stoi(v[0], nullptr, 16);
int x_start = stoi(v[1]);
int y_start = stoi(v[2]);
int x_end = stoi(v[3]);
int y_end = stoi(v[4]);
for (int x = x_start; x <= x_end; x++)
{
for (int y = y_start; y <= y_end; y++)
{
map16_handler.replace_map_tile(tile, x, y);
}
}
}
if(v.size() == 3) //Format type 2
{
chunks++;
int tile = stoi(v[0], nullptr, 16);
int x = stoi(v[1]);
int y = stoi(v[2]);
map16_handler.replace_map_tile(tile, x, y);
}
}
if (status == "sprite_data")
{
vector<string> v = split(line.c_str(), ',');
/*RAM[0x2800 + spr_index] = v[0] == "lua" ? 1 : 0;
RAM[0x2000 + spr_index] = 1;
RAM[0x2080 + spr_index] = stoi(v[1], nullptr, 16);
RAM[0x2100 + spr_index] = stoi(v[2]) % 256;
RAM[0x2180 + spr_index] = stoi(v[2]) / 256;
RAM[0x2200 + spr_index] = 0;
RAM[0x2280 + spr_index] = stoi(v[3]) % 256;
RAM[0x2300 + spr_index] = stoi(v[3]) / 256;
RAM[0x2380 + spr_index] = 0;
RAM[0x2680 + spr_index] = stoi(v[4]);
RAM[0x2F80 + spr_index] = 0;*/
LevelSprites.push_back(
LevelSprite{
v[0] == "lua",
uint_fast8_t(stoi(v[1], nullptr, 16)),
uint_fast16_t(stoi(v[2])),
uint_fast16_t(stoi(v[3])),
1,
uint_fast8_t(stoi(v[4]))
}
);
}
}
}
}
cout << green
<< "[Level Manager] Finished loading level. " << chunks << " chunks loaded."
<< white << endl;
}
void Initialize_Level()
{
ASM.Write_To_Ram(0x1411, 1, 1);
ASM.Write_To_Ram(0x1412, 1, 1);
ASM.Write_To_Ram(0x1462, 0, 2);
ASM.Write_To_Ram(0x1464, 0, 2);
ASM.Write_To_Ram(0x1466, 0, 2);
ASM.Write_To_Ram(0x1468, 0, 2);
ASM.Write_To_Ram(0x14AF, 0, 1);
ASM.Write_To_Ram(0x7C, 96, 1);
ASM.Write_To_Ram(0x85, 0, 1);
ASM.Write_To_Ram(0x36, 0, 1);
ASM.Write_To_Ram(0x38, 0x20, 1);
ASM.Write_To_Ram(0x39, 0x20, 1);
ASM.Write_To_Ram(0x40, 0, 1);
ASM.Write_To_Ram(0x1493, 0, 1);
ResetDMAandHDMA();
}
void LoadLevel(uint_fast16_t num)
{
while (!level_data.empty())
{
level_data.erase(level_data.begin());
}
Initialize_Level();
cout << green
<< "[Level Manager] Loading level " << int_to_hex(num) << ".."
<< white << endl;
read_from_palette(path + "Levels/" + int_to_hex(num) + "/level_palette.mw3");
LoadLevelFromFile(path + "Levels/" + int_to_hex(num) + "/level_data.txt", num);
decode_graphics_file("Graphics/GFX" + int_to_hex(request_level_entry("gfx_1"), true) + ".bin", 0); //FG1
decode_graphics_file("Graphics/GFX" + int_to_hex(request_level_entry("gfx_2"), true) + ".bin", 1); //FG2
decode_graphics_file("Graphics/GFX" + int_to_hex(request_level_entry("gfx_3"), true) + ".bin", 2); //FG3
decode_graphics_file("Graphics/GFX" + int_to_hex(request_level_entry("gfx_4"), true) + ".bin", 3); //FG4
decode_graphics_file("Graphics/GFX" + int_to_hex(request_level_entry("gfx_5"), true) + ".bin", 4); //FG5
decode_graphics_file("Graphics/GFX" + int_to_hex(request_level_entry("gfx_6"), true) + ".bin", 5); //FG6
decode_graphics_file("Graphics/GFX" + int_to_hex(request_level_entry("gfx_7"), true) + ".bin", 6); //FG7
decode_graphics_file("Graphics/GFX" + int_to_hex(request_level_entry("gfx_8"), true) + ".bin", 7); //FG8
decode_graphics_file("Graphics/GFX" + int_to_hex(request_level_entry("sp_1"), true) + ".bin", 12); //SP1
decode_graphics_file("Graphics/GFX" + int_to_hex(request_level_entry("sp_2"), true) + ".bin", 13); //SP2
decode_graphics_file("Graphics/GFX" + int_to_hex(request_level_entry("sp_3"), true) + ".bin", 14); //SP3
decode_graphics_file("Graphics/GFX" + int_to_hex(request_level_entry("sp_4"), true) + ".bin", 15); //SP4
if (networking && !isClient)
{
midway_activated = false;
}
if (!midway_activated) //if is server or midway isn't activated, load the start pos
{
ASM.Write_To_Ram(0x3F0B, request_level_entry("start_x") * 16, 2);
ASM.Write_To_Ram(0x3F0D, request_level_entry("start_y") * 16, 2);
start_x = RAM[0x3F0B] + RAM[0x3F0C] * 256;
start_y = RAM[0x3F0D] + RAM[0x3F0E] * 256;
}
Do_RAM_Change();
Set_Server_RAM();
}
};
Level LevelManager;