-
Notifications
You must be signed in to change notification settings - Fork 1
/
uwsav.cpp
269 lines (239 loc) · 8.23 KB
/
uwsav.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
#include <memory>
#include <stdint.h>
#include <string>
#include <string.h>
#include <vector>
#include "uwsav/uwsav_data.h"
#include "utils/platform.h"
#include "utils/filestream.h"
#include "utils/stream.h"
#include "utils/str_utils.h"
void write_text(Stream &out, const std::string &s)
{
out.Write(s.c_str(), s.size());
}
void write_text(Stream &out, const char *cstr)
{
size_t len = strlen(cstr);
out.Write(cstr, len);
}
void write_text_ln(Stream &out, const std::string &s)
{
out.Write(s.c_str(), s.size());
out.Write("\n", 1);
}
void write_text_ln(Stream &out, const char *cstr)
{
size_t len = strlen(cstr);
out.Write(cstr, len);
out.Write("\n", 1);
}
enum TileGlyphExtra
{
kTileExtraDoor = kTileSlopeW + 1,
kTileExtraUnknown
};
// Prints tilemap in ASCII
void print_tilemap(Stream &out, const LevelData &level)
{
write_text_ln(out, "--------------------------------------------------------------------");
write_text_ln(out, " 0000000000111111111122222222223333333333444444444455555555556666");
write_text_ln(out, " 0123456789012345678901234567890123456789012345678901234567890123");
write_text_ln(out, " -----------------------------------------------------------------");
const char* tile_glyph[] = { "X", " ", "p", "q", "b", "d", " ", " ", " ", " ", "=", "?" };
std::string line;
for (uint16_t y = 0; y < level.Height; ++y)
{
uint16_t uw_y = level.Height - y - 1; // y axis is inverse
write_text(out, StrPrint("%02d|", uw_y));
line.clear();
for (uint16_t x = 0; x < level.Width; ++x)
{
const TileData& tile = level.tiles[uw_y * level.Width + x];
if (tile.IsDoor)
line.append(tile_glyph[kTileExtraDoor]);
else if (tile.Type >= kTileSolid && tile.Type <= kTileSlopeW)
line.append(tile_glyph[tile.Type]);
else
line.append(tile_glyph[kTileExtraUnknown]);
}
line.append("|");
write_text_ln(out, line);
}
write_text_ln(out, " -----------------------------------------------------------------");
}
void print_objlinkedlist(Stream &out, const LevelData &level,
uint16_t obj_index, uint16_t &obj_mob_count, uint16_t &obj_static_count,
const std::string &indent)
{
std::string line;
std::vector<uint16_t> containers;
while (obj_index > 0)
{
if (obj_index < 256)
obj_mob_count++;
else
obj_static_count++;
if (line.size() >= 80)
{
write_text_ln(out, line);
line = indent + ">> ";
}
const ObjectData& obj = level.objs[obj_index];
// NPCs or containers: save for later
if ((obj.ItemID >= 0x0040 && obj.ItemID <= 0x007f) ||
(obj.ItemID >= 0x0080 && obj.ItemID <= 0x008f))
{
containers.push_back(obj_index);
}
else
{
std::string s = StrPrint(" 0x%03x", obj.ItemID);
if (obj.Quantity > 1)
s.append(StrPrint(" (*%03u) |", obj.Quantity));
else
s.append(" |");
line.append(s);
}
uint16_t next_index = obj.NextObjLink;
if (next_index == obj_index)
break; // safety skip, prevent endless loop
obj_index = next_index;
}
write_text_ln(out, line);
for (auto cont_index : containers)
{
const ObjectData &obj = level.objs[cont_index];
const char *tag = (obj.ItemID >= 0x0040 && obj.ItemID <= 0x007f) ? "npc" : "inv";
const char *has_inv = (obj.SpecialLink > 0) ? "+" : "-";
const char *has_inv2 = (obj.SpecialLink > 0) ? ":" : " ";
write_text(out, indent + ">> " + StrPrint(" 0x%03x (%s%s)%s ", obj.ItemID, has_inv, tag, has_inv2));
if (obj.SpecialLink > 0)
print_objlinkedlist(out, level, obj.SpecialLink, obj_mob_count, obj_static_count,
indent + " ");
else
write_text_ln(out, "");
}
}
// Prints master objects list
void print_objlist(Stream &out, const LevelData &level)
{
write_text_ln(out, "--------------------------------------------------------------------");
write_text_ln(out, " Objects in Tiles: ");
auto sum_pos = out.GetPosition();
const char *sum_format = "Total: %04d / %04d (%05.2f%%)\nMobile: %04d / %04d (%05.2f%%)\nStatic: %04d / %04d (%05.2f%%)";
write_text_ln(out, StrPrint(sum_format, 0, 0, 0.f, 0, 0, 0.f, 0, 0, 0.f));
uint16_t obj_mob_count = 0, obj_static_count = 0;
for (uint16_t y = 0; y < level.Height; ++y)
{
for (uint16_t x = 0; x < level.Width; ++x)
{
const TileData& tile = level.tiles[y * level.Width + x];
uint16_t obj_index = tile.FirstObjLink;
if (obj_index == 0)
continue;
write_text(out, StrPrint(" T [%02dx%02d]: ", x, y));
print_objlinkedlist(out, level, obj_index,
obj_mob_count, obj_static_count, " ");
}
}
// Print object summary
auto end_pos = out.GetPosition();
out.Seek(sum_pos, kSeekBegin);
write_text_ln(out, StrPrint(sum_format,
obj_mob_count + obj_static_count, LevelData::MaxObjects,
(obj_mob_count + obj_static_count) * 100.f / LevelData::MaxObjects,
obj_mob_count, LevelData::MaxMobiles, obj_mob_count * 100.f / LevelData::MaxMobiles,
obj_static_count, LevelData::MaxStatic, obj_static_count * 100.f / LevelData::MaxStatic));
out.Seek(end_pos, kSeekBegin);
}
struct CommandOptions
{
bool PrintHelp = false;
bool UW2 = false; // read as Ultima Underworld 2
bool PrintMaps = true;
bool PrintObjs = false;
};
void print_levels(Stream &out, const std::vector<LevelData> &levels, const CommandOptions &opts)
{
for (const auto &level : levels)
{
write_text_ln(out, "==========================================");
if (level.WorldID > 0)
write_text_ln(out, StrPrint(" World %d, Level %d", level.WorldID, level.LevelID));
else
write_text_ln(out, StrPrint(" Level %d", level.LevelID));
if (opts.PrintMaps)
print_tilemap(out, level);
if (opts.PrintObjs)
print_objlist(out, level);
}
}
void print_help()
{
printf(
#if (PLATFORM_OS_WINDOWS)
"Usage: uwsav-dump.exe [OPTIONS] <input-lvl.ark> <output-text-file>\n"
#else
"Usage: uwsav-dump [OPTIONS] <input-lvl.ark> <output-text-file>\n"
#endif
//--------------------------------------------------------------------------------|
"\nOptions:\n"
" -?, --help print this help message and stop\n"
" -uw2 assume \"Ultima Underworld 2\" data\n"
" -po print map's objects list\n"
//--------------------------------------------------------------------------------|
"\nExample:\n"
#if (PLATFORM_OS_WINDOWS)
" uwsav-dump.exe -uw2 -po UW2/SAVE1/lev.ark save1_levels.txt\n"
#else
" uwsav-dump -uw2 -po ./UW2/SAVE1/lev.ark ./save1_levels.txt\n"
#endif
);
}
int main(int argc, char **argv)
{
if (argc < 2)
{
print_help();
return -1;
}
CommandOptions opts;
int argi;
for (argi = 1; argi < argc && strncmp(argv[argi], "-", 1) == 0; ++argi)
{
if (strcmp(argv[argi], "-?") == 0 || strcmp(argv[argi], "--help") == 0)
opts.PrintHelp = true;
if (strcmp(argv[argi], "-uw2") == 0)
opts.UW2 = true;
if (strcmp(argv[argi], "-po") == 0)
opts.PrintObjs = true;
}
const char *in_filename = argv[argi++];
const char *out_filename = (argi < argc) ? argv[argi++] : nullptr;
if (opts.PrintHelp || !out_filename)
{
print_help();
return 0;
}
std::vector<LevelData> levels;
{
Stream in(FileStream::TryOpen(in_filename, kFileMode_Open, kStream_Read));
if (in)
{
if (opts.UW2)
ReadLevelsUW2(in, levels);
else
ReadLevelsUW1(in, levels);
}
}
if (out_filename)
{
Stream out(FileStream::TryOpen(out_filename, kFileMode_CreateAlways, kStream_Write));
if (out)
{
print_levels(out, levels, opts);
}
}
return 0;
}