-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdump_sve.cpp
239 lines (228 loc) · 4.96 KB
/
dump_sve.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
//#include "typedefs.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <iostream>
#include <map>
#include "SVEFormat.h"
const char* GovermentNames[] = {
"ANARCHY",
"DESPOTISM",
"MONARCHY",
"COMMUNISM",
"REPUBLIC",
"DEMOCRACY"
};
const char* LevelNames[] = {
"Chieftan",
"Warlord",
"Prince",
"King",
"Emperor"};
const char* TechNames[] = {
"ALPHABET",
"CODE_OF_LAWS",
"CURRENCY",
"ATOMIC_THEORY",
"DEMOCRACY_TECH",
"MONARCHY_TECH",
"ASTRONOMY",
"MAPMAKING",
"NAVIGATION",
"MATHEMATICS",
"MEDICINE",
"PHYSICS",
"ENGINEERING",
"UNIVERSITY",
"MAGNETISM",
"ELECTRONICS",
"MASONRY",
"BRONZE_WORKING",
"IRON_WORKING",
"BRIDGE_BUILDING",
"INVENTION",
"COMPUTERS",
"WRITING",
"STEAM_ENGINE",
"TRADE",
"CEREMONIAL_BURIAL",
"MYSTICISM",
"NUCLEAR_FISSION",
"PHILOSOPHY",
"RELIGION",
"LITERACY",
"HORSEBACK_RIDING",
"FEUDALISM",
"THE_WHEEL",
"GUNPOWDER",
"INDUSTRIALIZATION",
"CHEMISTRY",
"COMBUSTION",
"FLIGHT",
"ADVANCED_FLIGHT",
"SPACE_FLIGHT",
"MASS_PRODUCTION",
"POTTERY",
"COMMUNISM_TECH",
"THE_REPUBLIC",
"CONSTRUCTION",
"ROCKETRY",
"THE_CORPORATION",
"METALLURGY",
"RAILROAD",
"NUCLEAR_POWER",
"THEORY_OF_GRAVITY",
"STEEL",
"BANKING",
"ELECTRICITY",
"REFINING",
"EXPLOSIVES",
"SUPERCONDUCTOR",
"AUTOMOBILE",
"GENETIC_ENGINEERING",
"PLASTICS",
"RECYCLING",
"CHIVALRY",
"ROBOTICS",
"CONSCRIPTION",
"LABOR_UNION",
"FUSION_POWER"
};
const char* WonderNames[] = {
"The Pyramids",
"The Hanging Gardens",
"The Colossus",
"The Lighthouse",
"The Great Library",
"The Oracle",
"The Great Wall",
"Magellan's Expedition",
"Michelangelo's Chapel",
"Copernicus Observatory",
"Shakespeare's Theatre",
"Isaac Newton's College",
"J.S. Bach's Cathedral",
"Darwin's Voyage",
"Hoover Dam",
"Women's Suffrage",
"Manhattan Project",
"United Nations",
"Apollo Program",
"Seti Program",
"Cure For Cancer"
};
using namespace std;
struct Coords
{
int x, y;
Coords(int a_x, int a_y): x(a_x), y(a_y) {}
bool operator <(const Coords b) const
{
if(x != b.x)
return x < b.x;
return y < b.y;
}
};
class SVEDumper
{
SVEFile &sve;
typedef struct SVEFile::City City;
typedef map<Coords, City> CityMap;
CityMap cities_by_xy;
public:
SVEDumper( SVEFile& sveFile): sve(sveFile)
{
}
bool read(char* fname)
{
FILE *f = fopen(fname, "rb");
if(!f)
return false;
fread(&sveFile, sizeof sveFile, 1, f);
fclose(f);
return true;
}
char *city_name(City &city)
{
return sve.cityNames[city.city].cityName;
}
void dump_cities()
{
for(int i = 0; i < (sizeof(sve.cities) / sizeof(City)); i ++)
{
City city = sve.cities[i];
if(city.population)
{
cout << city_name(city) << ", "<< (int)city.population << "\tx: " << (int)city.x << "\ty: " << (int)city.y << "\t" << sve.nations[city.player].name << "\t" << (int)city.production << endl;
cities_by_xy[Coords(city.x, city.y)] = city;
}
}
}
void dump_units(int nation)
{
cout << "============================== UNITS of " << sve.nationsPlural[nation].name << "==============================\n";
for(int i = 0; i < sizeof sve.armies[0].unit / sizeof(SVEFile::Army::Unit); i++)
{
SVEFile::Army::Unit &unit = sve.armies[nation].unit[i];
if(unit.type != NONEXISTENT)
{
CityMap::iterator city = cities_by_xy.find(Coords(unit.x, unit.y));
CityMap::iterator goto_city = cities_by_xy.find(Coords(unit.goto_x, unit.goto_y));
cout << "x: " << (int)unit.x << "\ty: " << (int)unit.y << "\tsteps: " << (int)unit.steps << "\tstate: " << (int)unit.state << "\t" << sve.unitType[unit.type].unitName;
if(city != cities_by_xy.end())
{
cout << "\tin city " << city_name(city->second);
}
if(goto_city != cities_by_xy.end())
{
cout << "\tgoto " << city_name(goto_city->second);
}
cout << endl;
}
}
}
void dump_civilization(int civ)
{
printf("%s of %s, %s\n", GovermentNames[sve.govermentType[civ]], sve.nationsPlural[civ].name, sve.leaders[civ].name);
}
void dump()
{
char future[16];
sprintf(future, "FUTURE_TECH %i", sve.sveHeader.tech - FUTURE_TECH);
printf("%s %s of %s, %i, researching %s\n", LevelNames[sve.sveHeader.level], sve.leaders[sve.sveHeader.playerNum].name, sve.nationsPlural[sve.sveHeader.playerNum].name, sve.sveHeader.year, sve.sveHeader.tech < FUTURE_TECH? TechNames[sve.sveHeader.tech]: future);
dump_cities();
for(int nation = 0; nation < 8; nation++)
{
dump_units(nation);
}
for(int wonder = 0; wonder < CURE_FOR_CANCER; wonder++)
{
int city = sve.wonders_cities[wonder];
if(city > 0)
{
printf("%s in %s\n", WonderNames[wonder], city_name(sve.cities[city]));
}
}
for(int civ = 0; civ < 8; civ++)
{
if(sve.cityCount[civ])
{
dump_civilization(civ);
}
}
}
} sveDumper(sveFile);
int main(int argc, char* argv[])
{
if(argc < 2)
{
fprintf(stderr, "Usage: %s CIVIL0.SVE\n", argv[0]);
exit(-1);
}
if(!sveDumper.read(argv[1]))
{
fprintf(stderr, "Error occured: %s\n", strerror(errno));
}
sveDumper.dump();
}