1
0
mirror of https://github.com/zenorogue/hyperrogue.git synced 2024-12-29 19:40:35 +00:00

text files are now read with scan(fhstream&...) functions and written with print(...) functions

This commit is contained in:
Zeno Rogue 2019-05-21 23:59:07 +02:00
parent ae100b1e72
commit 8518f97ded
5 changed files with 87 additions and 85 deletions

19
hyper.h
View File

@ -674,6 +674,7 @@ string XLAT(string x); // translate the sentence x
string XLATN(string x); // translate the sentence x string XLATN(string x); // translate the sentence x
string cts(char c); // character to string string cts(char c); // character to string
string its(int i); // int to string string its(int i); // int to string
string itsh8(int i); // int to string (8 hex digits)
// a random integer from [0..i), generated by the game's main generator // a random integer from [0..i), generated by the game's main generator
// we want the same world to be generated if the seed is the same. For this purpose, // we want the same world to be generated if the seed is the same. For this purpose,
@ -4652,6 +4653,23 @@ template<class C, class C1, class... CS> void print(hstream& hs, const C& c, con
template<class... CS> void println(hstream& hs, const CS&... cs) { print(hs, cs...); hs.write_char('\n'); } template<class... CS> void println(hstream& hs, const CS&... cs) { print(hs, cs...); hs.write_char('\n'); }
inline string spaced(int i) { return its(i); }
inline string spaced(color_t col) { return itsh8(col); }
inline string spaced(const string& s) { return s; }
inline string spaced(ld x) { return fts(x, 10); }
template<class T> string spaced_of(T a[], int q) { string s = spaced(a[0]); for(int i=1; i<q; i++) s += ' ', s += spaced(a[i]); return s; }
template<class T, int i> string spaced(const array<T,i>& a) { return spaced_of(&a[0], isize(a)); }
template<class C, class C1, class... CS> string spaced(const C& c, const C1& c1, const CS&... cs) { return spaced(c) + " " + spaced(c1, cs...); }
bool scan(fhstream& hs, int&);
bool scan(fhstream& hs, ld&);
bool scan(fhstream& hs, string&);
bool scan(fhstream& hs, color_t& c);
template<class C, class C1, class... CS> bool scan(fhstream& hs, C& c, C1& c1, CS&... cs) { return scan(hs, c) && scan(hs, c1, cs...); }
string scanline(fhstream& hs);
template<class T> T scan(fhstream& hs) { T t {}; scan(hs, t); return t; }
// copied from: https://stackoverflow.com/questions/16387354/template-tuple-calling-a-function-on-each-element // copied from: https://stackoverflow.com/questions/16387354/template-tuple-calling-a-function-on-each-element
namespace detail namespace detail
@ -4681,6 +4699,7 @@ void for_each_in_tuple(std::tuple<Ts...> const& t, F f)
inline void print(hstream& hs, const string& s) { hs.write_chars(s.c_str(), isize(s)); } inline void print(hstream& hs, const string& s) { hs.write_chars(s.c_str(), isize(s)); }
inline void print(hstream& hs, int i) { print(hs, its(i)); } inline void print(hstream& hs, int i) { print(hs, its(i)); }
inline void print(hstream& hs, ld x) { print(hs, fts(x)); } inline void print(hstream& hs, ld x) { print(hs, fts(x)); }
inline void print(hstream& hs, color_t col) { print(hs, itsh8(col)); }
template<class T> void print(hstream& hs, const walker<T>& w) { print(hs, "[", w.at, "/", w.spin, "/", w.mirrored, "]"); } template<class T> void print(hstream& hs, const walker<T>& w) { print(hs, "[", w.at, "/", w.spin, "/", w.mirrored, "]"); }

View File

@ -812,36 +812,35 @@ void start_game_on_created_map() {
} }
bool save_map(const string& fname) { bool save_map(const string& fname) {
FILE *f = fopen(fname.c_str(), "wt"); fhstream f(fname, "wt");
if(!f) return false; if(!f.f) return false;
auto& all = base->allcells(); auto& all = base->allcells();
int origcells = 0; int origcells = 0;
for(cellinfo& ci: cells) for(cellinfo& ci: cells)
if(ci.generation == 0) if(ci.generation == 0)
origcells++; origcells++;
fprintf(f, "%d %d %d\n", geometry, isize(all), origcells); println(f, spaced(int(geometry), isize(all), origcells));
for(auto h: all) { for(auto h: all) {
origcells = 0; origcells = 0;
for(auto i: cells_of_heptagon[h->master]) for(auto i: cells_of_heptagon[h->master])
if(cells[i].generation == 0) if(cells[i].generation == 0)
origcells++; origcells++;
fprintf(f, "%d\n", origcells); println(f, origcells);
for(auto i: cells_of_heptagon[h->master]) if(cells[i].generation == 0) { for(auto i: cells_of_heptagon[h->master]) if(cells[i].generation == 0) {
auto &ci = cells[i]; auto &ci = cells[i];
fprintf(f, "%lf %lf %lf\n", double(ci.p[0]), double(ci.p[1]), double(ci.p[GDIM])); println(f, spaced(ci.p[0], ci.p[1], ci.p[GDIM]));
} }
} }
fclose(f);
return true; return true;
} }
bool load_map(const string &fname) { bool load_map(const string &fname) {
FILE *f = fopen(fname.c_str(), "rt"); fhstream f(fname, "rt");
if(!f) return false; if(!f.f) return false;
auto& all = base->allcells(); auto& all = base->allcells();
int g, sa; int g, sa;
ignore(fscanf(f, "%d %d %d\n", &g, &sa, &cellcount)); scan(f, g, sa, cellcount);
if(sa != isize(all) || g != geometry) { printf("bad parameters\n"); addMessage(XLAT("bad format or bad map geometry")); return false; } if(sa != isize(all) || g != geometry) { printf("bad parameters\n"); addMessage(XLAT("bad format or bad map geometry")); return false; }
density = cellcount * 1. / isize(all); density = cellcount * 1. / isize(all);
@ -849,20 +848,19 @@ bool load_map(const string &fname) {
for(auto h: all) { for(auto h: all) {
int q = 0; int q = 0;
ignore(fscanf(f, "%d\n", &q)); scan(f, q);
if(q < 0 || q > cellcount) { runlevel = 0; return false; } if(q < 0 || q > cellcount) { runlevel = 0; return false; }
while(q--) { while(q--) {
cells.emplace_back(); cells.emplace_back();
cellinfo& s = cells.back(); cellinfo& s = cells.back();
s.patterndir = -1; s.patterndir = -1;
double a, b, c; double a, b, c;
ignore(fscanf(f, "%lf%lf%lf", &a, &b, &c)); scan(f, a, b, c);
s.p = hpxyz(a, b, c); s.p = hpxyz(a, b, c);
for(auto c0: all) s.relmatrices[c0] = calc_relative_matrix(c0, h, s.p); for(auto c0: all) s.relmatrices[c0] = calc_relative_matrix(c0, h, s.p);
s.owner = h; s.owner = h;
} }
} }
fclose(f);
make_cells_of_heptagon(); make_cells_of_heptagon();
runlevel = 2; runlevel = 2;

View File

@ -1622,19 +1622,13 @@ namespace mapeditor {
if(uni == COLORKEY) dsCur->color = colortouse; if(uni == COLORKEY) dsCur->color = colortouse;
} }
void writeHyperpoint(FILE *f, hyperpoint h) { void writeHyperpoint(hstream& f, hyperpoint h) {
for(int i=0; i<MDIM; i++) fprintf(f, "%lf ", double(h[i])); println(f, spaced_of(&h[0], MDIM));
fprintf(f, "\n");
} }
hyperpoint readHyperpoint(FILE *f) { hyperpoint readHyperpoint(fhstream& f) {
hyperpoint h; hyperpoint h;
for(int i=0; i<MDIM; i++) { for(int i=0; i<MDIM; i++) scan(f, h[i]);
double d;
int err = fscanf(f, "%lf", &d);
if(err != 1) printf("Warning: read error\n");
h[i] = d;
}
return h; return h;
} }
@ -1645,24 +1639,24 @@ namespace mapeditor {
bool onelayeronly; bool onelayeronly;
bool loadPicFile(const string& s) { bool loadPicFile(const string& s) {
FILE *f = fopen(picfile.c_str(), "rt"); fhstream f(picfile, "rt");
if(!f) { if(!f.f) {
addMessage(XLAT("Failed to load pictures from %1", picfile)); addMessage(XLAT("Failed to load pictures from %1", picfile));
return false; return false;
} }
int err; scanline(f);
char buf[200]; color_t vernum;
if(!fgets(buf, 200, f)) { scan(f, vernum);
addMessage(XLAT("Failed to load pictures from %1", picfile));
fclose(f); return false;
}
int vernum; err = fscanf(f, "%x", &vernum);
printf("vernum = %x\n", vernum); printf("vernum = %x\n", vernum);
if(vernum == 0) {
addMessage(XLAT("Failed to load pictures from %1", picfile));
return false;
}
if(vernum >= 0xA0A0) { if(vernum >= 0xA0A0) {
int tg, wp; int tg, wp;
int nt; int nt;
hr::ignore(fscanf(f, "%d%d%d%d\n", &tg, &nt, &wp, &patterns::subpattern_flags)); scan(f, tg, nt, wp, patterns::subpattern_flags);
patterns::whichPattern = patterns::ePattern(wp); patterns::whichPattern = patterns::ePattern(wp);
set_geometry(eGeometry(tg)); set_geometry(eGeometry(tg));
set_variation(eVariation(nt)); set_variation(eVariation(nt));
@ -1670,9 +1664,10 @@ namespace mapeditor {
} }
while(true) { while(true) {
int i, j, l, sym, rots, color, siz; int i, j, l, sym, rots, siz;
err = fscanf(f, "%d%d%d%d%d%x%d", &i, &j, &l, &sym, &rots, &color, &siz); color_t color;
if(i == -1 || err < 6) break; if(!scan(f, i, j, l, sym, rots, color, siz)) break;
if(i == -1) break;
if(siz < 0 || siz > 1000) break; if(siz < 0 || siz > 1000) break;
if(i >= 4) { if(i >= 4) {
@ -1683,19 +1678,18 @@ namespace mapeditor {
initShape(i, j); initShape(i, j);
usershapelayer& ds(usershapes[i][j]->d[l]); usershapelayer& ds(usershapes[i][j]->d[l]);
if(VERNUM_HEX >= 0xA608) { double z; err = fscanf(f, "%lf", &z); ds.zlevel = z; } if(vernum >= 0xA608) scan(f, ds.zlevel);
ds.shift = readHyperpoint(f); ds.shift = readHyperpoint(f);
ds.spin = readHyperpoint(f); ds.spin = readHyperpoint(f);
ds.list.clear(); ds.list.clear();
for(int i=0; i<siz; i++) { for(int i=0; i<siz; i++) {
ds.list.push_back(readHyperpoint(f)); ds.list.push_back(readHyperpoint(f));
writeHyperpoint(stdout, ds.list[i]); writeHyperpoint(hlog, ds.list[i]);
} }
ds.sym = sym; ds.sym = sym;
ds.rots = rots; ds.rots = rots;
ds.color = color; ds.color = color;
} }
fclose(f);
addMessage(XLAT("Pictures loaded from %1", picfile)); addMessage(XLAT("Pictures loaded from %1", picfile));
buildpolys(); buildpolys();
@ -1703,33 +1697,31 @@ namespace mapeditor {
} }
bool savePicFile(const string& s) { bool savePicFile(const string& s) {
FILE *f = fopen(picfile.c_str(), "wt"); fhstream f(picfile, "wt");
if(!f) { if(!f.f) {
addMessage(XLAT("Failed to save pictures to %1", picfile)); addMessage(XLAT("Failed to save pictures to %1", picfile));
return false; return false;
} }
fprintf(f, "HyperRogue saved picture\n"); println(f, "HyperRogue saved picture");
fprintf(f, "%x\n", VERNUM_HEX); println(f, format("%x\n", VERNUM_HEX));
if(VERNUM_HEX >= 0xA0A0) if(VERNUM_HEX >= 0xA0A0)
fprintf(f, "%d %d %d %d\n", geometry, int(variation), patterns::whichPattern, patterns::subpattern_flags); println(f, spaced(geometry, int(variation), patterns::whichPattern, patterns::subpattern_flags));
for(int i=0; i<USERSHAPEGROUPS; i++) for(auto usp: usershapes[i]) { for(int i=0; i<USERSHAPEGROUPS; i++) for(auto usp: usershapes[i]) {
usershape *us = usp.second; usershape *us = usp.second;
if(!us) continue; if(!us) continue;
for(int l=0; l<USERLAYERS; l++) if(isize(us->d[l].list)) { for(int l=0; l<USERLAYERS; l++) if(isize(us->d[l].list)) {
usershapelayer& ds(us->d[l]); usershapelayer& ds(us->d[l]);
fprintf(f, "\n%d %d %d %d %d %6x %d\n", println(f, spaced(i, usp.first, l, ds.sym, ds.rots, ds.color, int(isize(ds.list))));
i, usp.first, l, ds.sym, ds.rots, ds.color, int(isize(ds.list))); print(f, spaced(ds.zlevel), " ");
fprintf(f, "\n%lf", double(ds.zlevel));
writeHyperpoint(f, ds.shift); writeHyperpoint(f, ds.shift);
writeHyperpoint(f, ds.spin); writeHyperpoint(f, ds.spin);
fprintf(f,"\n"); println(f);
for(int i=0; i<isize(ds.list); i++) for(int i=0; i<isize(ds.list); i++)
writeHyperpoint(f, ds.list[i]); writeHyperpoint(f, ds.list[i]);
} }
} }
fprintf(f, "\n-1\n"); println(f, "-1");
fclose(f);
addMessage(XLAT("Pictures saved to %1", picfile)); addMessage(XLAT("Pictures saved to %1", picfile));
return true; return true;
} }
@ -1916,13 +1908,13 @@ namespace mapeditor {
for(int l=0; l<USERLAYERS; l++) if(isize(us->d[l].list)) { for(int l=0; l<USERLAYERS; l++) if(isize(us->d[l].list)) {
usershapelayer& ds(us->d[l]); usershapelayer& ds(us->d[l]);
printf("// %d %d %d [%06X %lf]\n", i, usp.first, l, ds.color, double(ds.zlevel)); println(hlog, spaced("//", i, usp.first, l, "[", ds.color, double(ds.zlevel), "]"));
printf(" ID, %d, %d, ", us->d[l].rots, us->d[l].sym?2:1); print(hlog, " ID, ", us->d[l].rots, ", ", us->d[l].sym?2:1, ", ");
for(int i=0; i<isize(us->d[l].list); i++) { for(int i=0; i<isize(us->d[l].list); i++) {
for(int d=0; d<DIM; d++) printf("%lf,", double(us->d[l].list[i][d])); for(int d=0; d<DIM; d++) print(hlog, fts(us->d[l].list[i][d]), ", ");
printf(" "); print(hlog, " ");
} }
printf("\n"); println(hlog);
} }
} }
} }

View File

@ -134,28 +134,25 @@ namespace hr { namespace netgen {
void loadData() { void loadData() {
FILE *f = fopen("papermodeldata.txt", "rt"); fhstream f("papermodeldata.txt", "rt");
if(!f) return; if(!f.f) return;
int err = fscanf(f, "%d %d %d %d %d %d %d %lf %d\n\n", if(!scan(f, CELLS, SX, SY, PX, PY, SCALE, BASE, el, created)) return;
&CELLS, &SX, &SY, &PX, &PY, &SCALE, &BASE, &el, &created);
if(err != 9) { fclose(f); return; }
loaded = true; loaded = true;
if(!created) { fclose(f); return; } if(!created) return;
for(int i=0; i<CELLS; i++) err = fscanf(f, "%d", &ct[i]); for(int i=0; i<CELLS; i++) scan(f, ct[i]);
for(int i=0; i<CELLS; i++) for(int j=0; j<16; j++) for(int i=0; i<CELLS; i++) for(int j=0; j<16; j++) scan(f, vx[i][j]);
err = fscanf(f, "%lf" ,&vx[i][j]);
for(int i=0; i<CELLS; i++) for(int i=0; i<CELLS; i++)
for(int j=0; j<7; j++) nei[i][j] = -1; for(int j=0; j<7; j++) nei[i][j] = -1;
while(true) { while(true) {
int a, b, c; int a, b, c;
err = fscanf(f, "%d%d%d", &a, &b, &c); scan(f, a, b, c);
if(a < 0) break; if(a < 0) break;
else nei[a][c] = b; else nei[a][c] = b;
} }
@ -163,51 +160,41 @@ namespace hr { namespace netgen {
for(int i=0; i<CELLS; i++) { for(int i=0; i<CELLS; i++) {
double dx, dy, dr; double dx, dy, dr;
int g; int g;
err = fscanf(f, "%lf%lf%lf%d\n", &dx, &dy, &dr, &g); scan(f, dx, dy, dr, g);
center[i] = vec(dx, dy); center[i] = vec(dx, dy);
rot[i] = dr; rot[i] = dr;
glued[i] = g; glued[i] = g;
} }
fclose(f);
} }
void saveData() { void saveData() {
// global parameters // global parameters
FILE *f = fopen("papermodeldata2.txt", "wt"); fhstream f("papermodeldata2.txt", "wt");
if(!f) { if(!f.f) {
addMessage("Could not save the paper model data"); addMessage("Could not save the paper model data");
return; return;
} }
fprintf(f, "%d %d %d %d %d %d %d %lf %d\n\n", CELLS, SX, SY, PX, PY, SCALE, BASE, el, created); println(f, spaced(CELLS, SX, SY, PX, PY, SCALE, BASE, el, created), "\n");
// net parameters: cell types // net parameters: cell types
for(int i=0; i<CELLS; i++) println(f, spaced_of(ct, CELLS));
fprintf(f, "%d ", ct[i]);
fprintf(f, "\n");
// net parameters: hcenters // net parameters: hcenters
for(int i=0; i<CELLS; i++) { for(int i=0; i<CELLS; i++) {
for(int k=0; k<16; k++) println(f, spaced_of(vx[i], 16));
fprintf(f, "%9.6lf ", vx[i][k]);
fprintf(f, "\n");
} }
fprintf(f, "\n\n"); println(f, "\n");
// create netgen // create netgen
for(int i=0; i<CELLS; i++) for(int j=0; j<CELLS; j++) { for(int i=0; i<CELLS; i++) for(int j=0; j<CELLS; j++) {
for(int k=0; k<ct[i]; k++) if(nei[i][k] == j) for(int k=0; k<ct[i]; k++) if(nei[i][k] == j)
fprintf(f, "%d %d %d ", i, j, k); print(f, spaced(i, j, k), " ");
} }
fprintf(f, "-1 -1 -1\n\n"); println(f, "-1 -1 -1");
// graphics // graphics
for(int i=0; i<CELLS; i++) for(int i=0; i<CELLS; i++)
fprintf(f, "%12.7lf %12.7lf %10.7lf %d\n", println(f, spaced(center[i].x, center[i].y, rot[i], glued[i]));
center[i].x, center[i].y, rot[i], glued[i]
);
fclose(f);
} }
// Simple graphical functions // Simple graphical functions

View File

@ -39,6 +39,12 @@ string ftssmart(ld x) {
return buf; return buf;
} }
bool scan(fhstream& hs, int& i) { return fscanf(hs.f, "%d", &i) == 1; }
bool scan(fhstream& hs, color_t& c) { return fscanf(hs.f, "%x", &c) == 1; }
bool scan(fhstream& hs, ld& x) { return fscanf(hs.f, "%lf", &x) == 1; }
bool scan(fhstream& hs, string& s) { char t[10000]; t[0] = 0; int err = fscanf(hs.f, "%9500s", t); s = t; return err == 1 && t[0]; }
string scanline(fhstream& hs) { char buf[10000]; buf[0] = 0; fgets(buf, 10000, hs.f); return buf; }
/* /*
string fts_smartdisplay(ld x, int maxdisplay) { string fts_smartdisplay(ld x, int maxdisplay) {
string rv; string rv;