diff options
| author | xolatile | 2025-08-06 22:54:55 +0200 |
|---|---|---|
| committer | xolatile | 2025-08-06 22:54:55 +0200 |
| commit | 0a1172b75f571685c264a8b9d8ee224bbf11381f (patch) | |
| tree | d041fdc68a60f0ebb48a3852bbcce6d9432f83d5 /src/engine/worldio.cpp | |
| parent | affde05dc07a94643f1fd2751b2b441f57f73d7d (diff) | |
| download | xolatile-badassbug-0a1172b75f571685c264a8b9d8ee224bbf11381f.tar.xz xolatile-badassbug-0a1172b75f571685c264a8b9d8ee224bbf11381f.tar.zst | |
Please do not hate me, it makes sense...
Diffstat (limited to 'src/engine/worldio.cpp')
| -rw-r--r-- | src/engine/worldio.cpp | 369 |
1 files changed, 102 insertions, 267 deletions
diff --git a/src/engine/worldio.cpp b/src/engine/worldio.cpp index 4e309a8..898b2bb 100644 --- a/src/engine/worldio.cpp +++ b/src/engine/worldio.cpp @@ -2,12 +2,10 @@ #include "engine.h" -void validmapname(char *dst, const char *src, const char *prefix = NULL, const char *alt = "untitled", size_t maxlen = 100) -{ +void validmapname(char *dst, const char *src, const char *prefix = NULL, const char *alt = "untitled", size_t maxlen = 100) { if(prefix) while(*prefix) *dst++ = *prefix++; const char *start = dst; - if(src) loopi(maxlen) - { + if(src) loopi(maxlen) { char c = *src++; if(iscubealnum(c) || c == '_' || c == '-' || c == '/' || c == '\\') *dst++ = c; else break; @@ -16,32 +14,27 @@ void validmapname(char *dst, const char *src, const char *prefix = NULL, const c else if(dst != alt) copystring(dst, alt, maxlen); } -void fixmapname(char *name) -{ +void fixmapname(char *name) { validmapname(name, name, NULL, ""); } -void getmapfilenames(const char *fname, const char *cname, char *pakname, char *mapname, char *cfgname) -{ +void getmapfilenames(const char *fname, const char *cname, char *pakname, char *mapname, char *cfgname) { if(!cname) cname = fname; string name; validmapname(name, cname); char *slash = strpbrk(name, "/\\"); - if(slash) - { + if(slash) { copystring(pakname, name, slash-name+1); copystring(cfgname, slash+1, MAXSTRLEN); } - else - { + else { copystring(pakname, "maps", MAXSTRLEN); copystring(cfgname, name, MAXSTRLEN); } validmapname(mapname, fname, strpbrk(fname, "/\\") ? NULL : "maps/"); } -bool loadents(const char *fname, vector<entity> &ents, uint *crc) -{ +bool loadents(const char *fname, vector<entity> &ents, uint *crc) { string pakname, mapname, mcfgname, ogzname; getmapfilenames(fname, NULL, pakname, mapname, mcfgname); formatstring(ogzname, "packages/%s.ogz", mapname); @@ -53,21 +46,16 @@ bool loadents(const char *fname, vector<entity> &ents, uint *crc) lilswap(&hdr.version, 6); if(memcmp(hdr.magic, "OCTA", 4) || hdr.worldsize <= 0|| hdr.numents < 0) { conoutf(CON_ERROR, "map %s has malformatted header", ogzname); delete f; return false; } if(hdr.version>MAPVERSION) { conoutf(CON_ERROR, "map %s requires a newer version of Cube 2: Sauerbraten", ogzname); delete f; return false; } - lilswap(&hdr.numvslots, 1); - - loopi(hdr.numvars) - { + loopi(hdr.numvars) { int type = f->getchar(), ilen = f->getlil<ushort>(); f->seek(ilen, SEEK_CUR); - switch(type) - { + switch(type) { case ID_VAR: f->getlil<int>(); break; case ID_FVAR: f->getlil<float>(); break; case ID_SVAR: { int slen = f->getlil<ushort>(); f->seek(slen, SEEK_CUR); break; } } } - string gametype; copystring(gametype, "fps"); int eif = 0; @@ -76,32 +64,24 @@ bool loadents(const char *fname, vector<entity> &ents, uint *crc) eif = f->getlil<ushort>(); int extrasize = f->getlil<ushort>(); f->seek(extrasize, SEEK_CUR); - ushort nummru = f->getlil<ushort>(); f->seek(nummru*sizeof(ushort), SEEK_CUR); - - loopi(min(hdr.numents, MAXENTS)) - { + loopi(min(hdr.numents, MAXENTS)) { entity &e = ents.add(); f->read(&e, sizeof(entity)); lilswap(&e.o.x, 3); lilswap(&e.attr1, 5); if(eif > 0) f->seek(eif, SEEK_CUR); - if(e.type>=ET_GAMESPECIFIC) - { + if(e.type>=ET_GAMESPECIFIC) { ents.pop(); continue; } } - - if(crc) - { + if(crc) { f->seek(0, SEEK_END); *crc = f->getcrc(); } - delete f; - return true; } @@ -110,25 +90,21 @@ string ogzname, bakname, cfgname, picname; VARP(savebak, 0, 2, 2); -void setmapfilenames(const char *fname, const char *cname = NULL) -{ +void setmapfilenames(const char *fname, const char *cname = NULL) { string pakname, mapname, mcfgname; getmapfilenames(fname, cname, pakname, mapname, mcfgname); - formatstring(ogzname, "packages/%s.ogz", mapname); if(savebak==1) formatstring(bakname, "packages/%s.BAK", mapname); else formatstring(bakname, "packages/%s_%d.BAK", mapname, totalmillis); formatstring(cfgname, "packages/%s/%s.cfg", pakname, mcfgname); formatstring(picname, "packages/%s.png", mapname); - path(ogzname); path(bakname); path(cfgname); path(picname); } -void mapcfgname() -{ +void mapcfgname() { const char *mname = game::getclientmap(); string pakname, mapname, mcfgname; getmapfilenames(mname, NULL, pakname, mapname, mcfgname); @@ -139,8 +115,7 @@ void mapcfgname() COMMAND(mapcfgname, ""); -void backup(char *name, char *backupname) -{ +void backup(char *name, char *backupname) { string backupfile; copystring(backupfile, findfile(backupname, "wb")); remove(backupfile); @@ -151,30 +126,22 @@ enum { OCTSAV_CHILDREN = 0, OCTSAV_EMPTY, OCTSAV_SOLID, OCTSAV_NORMAL, OCTSAV_LO static int savemapprogress = 0; -void savec(cube *c, const ivec &o, int size, stream *f, bool nolms) -{ +void savec(cube *c, const ivec &o, int size, stream *f, bool nolms) { if((savemapprogress++&0xFFF)==0) renderprogress(float(savemapprogress)/allocnodes, "saving octree..."); - - loopi(8) - { + loopi(8) { ivec co(i, o, size); - if(c[i].children) - { + if(c[i].children) { f->putchar(OCTSAV_CHILDREN); savec(c[i].children, co, size>>1, f, nolms); } - else - { + else { int oflags = 0, surfmask = 0, totalverts = 0; if(c[i].material!=MAT_AIR) oflags |= 0x40; if(isempty(c[i])) f->putchar(oflags | OCTSAV_EMPTY); - else - { - if(!nolms) - { + else { + if(!nolms) { if(c[i].merged) oflags |= 0x80; - if(c[i].ext) loopj(6) - { + if(c[i].ext) loopj(6) { const surfaceinfo &surf = c[i].ext->surfaces[j]; if(!surf.used()) continue; oflags |= 0x20; @@ -182,34 +149,26 @@ void savec(cube *c, const ivec &o, int size, stream *f, bool nolms) totalverts += surf.totalverts(); } } - if(isentirelysolid(c[i])) f->putchar(oflags | OCTSAV_SOLID); - else - { + else { f->putchar(oflags | OCTSAV_NORMAL); f->write(c[i].edges, 12); } } - loopj(6) f->putlil<ushort>(c[i].texture[j]); - if(oflags&0x40) f->putlil<ushort>(c[i].material); if(oflags&0x80) f->putchar(c[i].merged); - if(oflags&0x20) - { + if(oflags&0x20) { f->putchar(surfmask); f->putchar(totalverts); - loopj(6) if(surfmask&(1<<j)) - { + loopj(6) if(surfmask&(1<<j)) { surfaceinfo surf = c[i].ext->surfaces[j]; vertinfo *verts = c[i].ext->verts() + surf.verts; int layerverts = surf.numverts&MAXFACEVERTS, numverts = surf.totalverts(), vertmask = 0, vertorder = 0, uvorder = 0, dim = dimension(j), vc = C[dim], vr = R[dim]; - if(numverts) - { - if(c[i].merged&(1<<j)) - { + if(numverts) { + if(c[i].merged&(1<<j)) { vertmask |= 0x04; if(layerverts == 4) { @@ -226,22 +185,19 @@ void savec(cube *c, const ivec &o, int size, stream *f, bool nolms) } } } - else - { + else { int vis = visibletris(c[i], j, co, size); if(vis&4 || faceconvexity(c[i], j) < 0) vertmask |= 0x01; if(layerverts < 4 && vis&2) vertmask |= 0x02; } bool matchnorm = true; - loopk(numverts) - { + loopk(numverts) { const vertinfo &v = verts[k]; if(v.u || v.v) vertmask |= 0x40; if(v.norm) { vertmask |= 0x80; if(v.norm != verts[0].norm) matchnorm = false; } } if(matchnorm) vertmask |= 0x08; - if(vertmask&0x40 && layerverts == 4) - { + if(vertmask&0x40 && layerverts == 4) { loopk(4) { const vertinfo &v0 = verts[k], &v1 = verts[(k+1)&3], &v2 = verts[(k+2)&3], &v3 = verts[(k+3)&3]; @@ -263,17 +219,14 @@ void savec(cube *c, const ivec &o, int size, stream *f, bool nolms) surf.verts = vertmask; f->write(&surf, sizeof(surfaceinfo)); bool hasxyz = (vertmask&0x04)!=0, hasuv = (vertmask&0x40)!=0, hasnorm = (vertmask&0x80)!=0; - if(layerverts == 4) - { - if(hasxyz && vertmask&0x01) - { + if(layerverts == 4) { + if(hasxyz && vertmask&0x01) { ivec v0 = verts[vertorder].getxyz(), v2 = verts[(vertorder+2)&3].getxyz(); f->putlil<ushort>(v0[vc]); f->putlil<ushort>(v0[vr]); f->putlil<ushort>(v2[vc]); f->putlil<ushort>(v2[vr]); hasxyz = false; } - if(hasuv && vertmask&0x02) - { + if(hasuv && vertmask&0x02) { const vertinfo &v0 = verts[uvorder], &v2 = verts[(uvorder+2)&3]; f->putlil<ushort>(v0.u); f->putlil<ushort>(v0.v); f->putlil<ushort>(v2.u); f->putlil<ushort>(v2.v); @@ -287,19 +240,16 @@ void savec(cube *c, const ivec &o, int size, stream *f, bool nolms) } } if(hasnorm && vertmask&0x08) { f->putlil<ushort>(verts[0].norm); hasnorm = false; } - if(hasxyz || hasuv || hasnorm) loopk(layerverts) - { + if(hasxyz || hasuv || hasnorm) loopk(layerverts) { const vertinfo &v = verts[(k+vertorder)%layerverts]; - if(hasxyz) - { + if(hasxyz) { ivec xyz = v.getxyz(); f->putlil<ushort>(xyz[vc]); f->putlil<ushort>(xyz[vr]); } if(hasuv) { f->putlil<ushort>(v.u); f->putlil<ushort>(v.v); } if(hasnorm) f->putlil<ushort>(v.norm); } - if(surf.numverts&LAYER_DUP) loopk(layerverts) - { + if(surf.numverts&LAYER_DUP) loopk(layerverts) { const vertinfo &v = verts[layerverts + (k+vertorder)%layerverts]; if(hasuv) { f->putlil<ushort>(v.u); f->putlil<ushort>(v.v); } } @@ -309,36 +259,30 @@ void savec(cube *c, const ivec &o, int size, stream *f, bool nolms) } } -struct surfacecompat -{ +struct surfacecompat { uchar texcoords[8]; uchar w, h; ushort x, y; uchar lmid, layer; }; -struct normalscompat -{ +struct normalscompat { bvec normals[4]; }; -struct mergecompat -{ +struct mergecompat { ushort u1, u2, v1, v2; }; cube *loadchildren(stream *f, const ivec &co, int size, bool &failed); -void loadc(stream *f, cube &c, const ivec &co, int size, bool &failed) -{ +void loadc(stream *f, cube &c, const ivec &co, int size, bool &failed) { bool haschildren = false; int octsav = f->getchar(); - switch(octsav&0x7) - { + switch(octsav&0x7) { case OCTSAV_CHILDREN: c.children = loadchildren(f, co, size>>1, failed); return; - case OCTSAV_LODCUBE: haschildren = true; break; case OCTSAV_EMPTY: emptyfaces(c); break; case OCTSAV_SOLID: solidfaces(c); break; @@ -346,12 +290,10 @@ void loadc(stream *f, cube &c, const ivec &co, int size, bool &failed) default: failed = true; return; } loopi(6) c.texture[i] = mapversion<14 ? f->getchar() : f->getlil<ushort>(); - - { + { if(octsav&0x40) c.material = f->getlil<ushort>(); if(octsav&0x80) c.merged = f->getchar(); - if(octsav&0x20) - { + if(octsav&0x20) { int surfmask, totalverts; surfmask = f->getchar(); totalverts = max(f->getchar(), 0); @@ -359,8 +301,7 @@ void loadc(stream *f, cube &c, const ivec &co, int size, bool &failed) memset(c.ext->surfaces, 0, sizeof(c.ext->surfaces)); memset(c.ext->verts(), 0, totalverts*sizeof(vertinfo)); int offset = 0; - loopi(6) if(surfmask&(1<<i)) - { + loopi(6) if(surfmask&(1<<i)) { surfaceinfo &surf = c.ext->surfaces[i]; f->read(&surf, sizeof(surfaceinfo)); int vertmask = surf.verts, numverts = surf.totalverts(); @@ -372,25 +313,21 @@ void loadc(stream *f, cube &c, const ivec &co, int size, bool &failed) int layerverts = surf.numverts&MAXFACEVERTS, dim = dimension(i), vc = C[dim], vr = R[dim], bias = 0; genfaceverts(c, i, v); bool hasxyz = (vertmask&0x04)!=0, hasuv = (vertmask&0x40)!=0, hasnorm = (vertmask&0x80)!=0; - if(hasxyz) - { + if(hasxyz) { ivec e1, e2, e3; n.cross((e1 = v[1]).sub(v[0]), (e2 = v[2]).sub(v[0])); if(n.iszero()) n.cross(e2, (e3 = v[3]).sub(v[0])); bias = -n.dot(ivec(v[0]).mul(size).add(vo)); } - else - { + else { int vis = layerverts < 4 ? (vertmask&0x02 ? 2 : 1) : 3, order = vertmask&0x01 ? 1 : 0, k = 0; verts[k++].setxyz(v[order].mul(size).add(vo)); if(vis&1) verts[k++].setxyz(v[order+1].mul(size).add(vo)); verts[k++].setxyz(v[order+2].mul(size).add(vo)); if(vis&2) verts[k++].setxyz(v[(order+3)&3].mul(size).add(vo)); } - if(layerverts == 4) - { - if(hasxyz && vertmask&0x01) - { + if(layerverts == 4) { + if(hasxyz && vertmask&0x01) { ushort c1 = f->getlil<ushort>(), r1 = f->getlil<ushort>(), c2 = f->getlil<ushort>(), r2 = f->getlil<ushort>(); ivec xyz; xyz[vc] = c1; xyz[vr] = r1; xyz[dim] = n[dim] ? -(bias + n[vc]*xyz[vc] + n[vr]*xyz[vr])/n[dim] : vo[dim]; @@ -403,16 +340,14 @@ void loadc(stream *f, cube &c, const ivec &co, int size, bool &failed) verts[3].setxyz(xyz); hasxyz = false; } - if(hasuv && vertmask&0x02) - { + if(hasuv && vertmask&0x02) { int uvorder = (vertmask&0x30)>>4; vertinfo &v0 = verts[uvorder], &v1 = verts[(uvorder+1)&3], &v2 = verts[(uvorder+2)&3], &v3 = verts[(uvorder+3)&3]; v0.u = f->getlil<ushort>(); v0.v = f->getlil<ushort>(); v2.u = f->getlil<ushort>(); v2.v = f->getlil<ushort>(); v1.u = v0.u; v1.v = v2.v; v3.u = v2.u; v3.v = v0.v; - if(surf.numverts&LAYER_DUP) - { + if(surf.numverts&LAYER_DUP) { vertinfo &b0 = verts[4+uvorder], &b1 = verts[4+((uvorder+1)&3)], &b2 = verts[4+((uvorder+2)&3)], &b3 = verts[4+((uvorder+3)&3)]; b0.u = f->getlil<ushort>(); b0.v = f->getlil<ushort>(); b2.u = f->getlil<ushort>(); b2.v = f->getlil<ushort>(); @@ -422,17 +357,14 @@ void loadc(stream *f, cube &c, const ivec &co, int size, bool &failed) hasuv = false; } } - if(hasnorm && vertmask&0x08) - { + if(hasnorm && vertmask&0x08) { ushort norm = f->getlil<ushort>(); loopk(layerverts) verts[k].norm = norm; hasnorm = false; } - if(hasxyz || hasuv || hasnorm) loopk(layerverts) - { + if(hasxyz || hasuv || hasnorm) loopk(layerverts) { vertinfo &v = verts[k]; - if(hasxyz) - { + if(hasxyz) { ivec xyz; xyz[vc] = f->getlil<ushort>(); xyz[vr] = f->getlil<ushort>(); xyz[dim] = n[dim] ? -(bias + n[vc]*xyz[vc] + n[vr]*xyz[vr])/n[dim] : vo[dim]; @@ -441,8 +373,7 @@ void loadc(stream *f, cube &c, const ivec &co, int size, bool &failed) if(hasuv) { v.u = f->getlil<ushort>(); v.v = f->getlil<ushort>(); } if(hasnorm) v.norm = f->getlil<ushort>(); } - if(surf.numverts&LAYER_DUP) loopk(layerverts) - { + if(surf.numverts&LAYER_DUP) loopk(layerverts) { vertinfo &v = verts[k+layerverts], &t = verts[k]; v.setxyz(t.x, t.y, t.z); if(hasuv) { v.u = f->getlil<ushort>(); v.v = f->getlil<ushort>(); } @@ -451,30 +382,24 @@ void loadc(stream *f, cube &c, const ivec &co, int size, bool &failed) } } } - c.children = (haschildren ? loadchildren(f, co, size>>1, failed) : NULL); } -cube *loadchildren(stream *f, const ivec &co, int size, bool &failed) -{ +cube *loadchildren(stream *f, const ivec &co, int size, bool &failed) { cube *c = newcubes(); - loopi(8) - { + loopi(8) { loadc(f, c[i], ivec(i, co, size), size, failed); if(failed) break; } return c; } -void savevslot(stream *f, VSlot &vs, int prev) -{ +void savevslot(stream *f, VSlot &vs, int prev) { f->putlil<int>(vs.changed); f->putlil<int>(prev); - if(vs.changed & (1<<VSLOT_SHPARAM)) - { + if(vs.changed & (1<<VSLOT_SHPARAM)) { f->putlil<ushort>(vs.params.length()); - loopv(vs.params) - { + loopv(vs.params) { SlotShaderParam &p = vs.params[i]; f->putlil<ushort>(strlen(p.name)); f->write(p.name, strlen(p.name)); @@ -483,39 +408,32 @@ void savevslot(stream *f, VSlot &vs, int prev) } if(vs.changed & (1<<VSLOT_SCALE)) f->putlil<float>(vs.scale); if(vs.changed & (1<<VSLOT_ROTATION)) f->putlil<int>(vs.rotation); - if(vs.changed & (1<<VSLOT_OFFSET)) - { + if(vs.changed & (1<<VSLOT_OFFSET)) { f->putlil<int>(vs.offset.x); f->putlil<int>(vs.offset.y); } - if(vs.changed & (1<<VSLOT_SCROLL)) - { + if(vs.changed & (1<<VSLOT_SCROLL)) { f->putlil<float>(vs.scroll.x); f->putlil<float>(vs.scroll.y); } if(vs.changed & (1<<VSLOT_LAYER)) f->putlil<int>(vs.layer); - if(vs.changed & (1<<VSLOT_ALPHA)) - { + if(vs.changed & (1<<VSLOT_ALPHA)) { f->putlil<float>(vs.alphafront); f->putlil<float>(vs.alphaback); } - if(vs.changed & (1<<VSLOT_COLOR)) - { + if(vs.changed & (1<<VSLOT_COLOR)) { loopk(3) f->putlil<float>(vs.colorscale[k]); } } -void savevslots(stream *f, int numvslots) -{ +void savevslots(stream *f, int numvslots) { if(vslots.empty()) return; int *prev = new int[numvslots]; for(int i=0;i<numvslots;++i)prev[i]=-1; - loopi(numvslots) - { + loopi(numvslots) { VSlot *vs = vslots[i]; if(vs->changed) continue; - for(;;) - { + for(;;) { VSlot *cur = vs; do vs = vs->next; while(vs && vs->index >= numvslots); if(!vs) break; @@ -523,8 +441,7 @@ void savevslots(stream *f, int numvslots) } } int lastroot = 0; - loopi(numvslots) - { + loopi(numvslots) { VSlot &vs = *vslots[i]; if(!vs.changed) continue; if(lastroot < i) f->putlil<int>(-(i - lastroot)); @@ -535,15 +452,12 @@ void savevslots(stream *f, int numvslots) delete[] prev; } -void loadvslot(stream *f, VSlot &vs, int changed) -{ +void loadvslot(stream *f, VSlot &vs, int changed) { vs.changed = changed; - if(vs.changed & (1<<VSLOT_SHPARAM)) - { + if(vs.changed & (1<<VSLOT_SHPARAM)) { int numparams = f->getlil<ushort>(); string name; - loopi(numparams) - { + loopi(numparams) { SlotShaderParam &p = vs.params.add(); int nlen = f->getlil<ushort>(); f->read(name, min(nlen, MAXSTRLEN-1)); @@ -556,43 +470,35 @@ void loadvslot(stream *f, VSlot &vs, int changed) } if(vs.changed & (1<<VSLOT_SCALE)) vs.scale = f->getlil<float>(); if(vs.changed & (1<<VSLOT_ROTATION)) vs.rotation = clamp(f->getlil<int>(), 0, 7); - if(vs.changed & (1<<VSLOT_OFFSET)) - { + if(vs.changed & (1<<VSLOT_OFFSET)) { vs.offset.x = f->getlil<int>(); vs.offset.y = f->getlil<int>(); } - if(vs.changed & (1<<VSLOT_SCROLL)) - { + if(vs.changed & (1<<VSLOT_SCROLL)) { vs.scroll.x = f->getlil<float>(); vs.scroll.y = f->getlil<float>(); } if(vs.changed & (1<<VSLOT_LAYER)) vs.layer = f->getlil<int>(); - if(vs.changed & (1<<VSLOT_ALPHA)) - { + if(vs.changed & (1<<VSLOT_ALPHA)) { vs.alphafront = f->getlil<float>(); vs.alphaback = f->getlil<float>(); } - if(vs.changed & (1<<VSLOT_COLOR)) - { + if(vs.changed & (1<<VSLOT_COLOR)) { loopk(3) vs.colorscale[k] = f->getlil<float>(); } } -void loadvslots(stream *f, int numvslots) -{ +void loadvslots(stream *f, int numvslots) { int *prev = new (false) int[numvslots]; if(!prev) return; for(int i=0;i<numvslots;++i)prev[i]=-1; - while(numvslots > 0) - { + while(numvslots > 0) { int changed = f->getlil<int>(); - if(changed < 0) - { + if(changed < 0) { loopi(-changed) vslots.add(new VSlot(NULL, vslots.length())); numvslots += changed; } - else - { + else { prev[vslots.length()] = f->getlil<int>(); loadvslot(f, *vslots.add(new VSlot(NULL, vslots.length())), changed); numvslots--; @@ -602,24 +508,19 @@ void loadvslots(stream *f, int numvslots) delete[] prev; } -bool save_world(const char *mname, bool nolms) -{ +bool save_world(const char *mname, bool nolms) { if(!*mname) mname = game::getclientmap(); setmapfilenames(mname); if(savebak) backup(ogzname, bakname); stream *f = opengzfile(ogzname, "wb"); if(!f) { conoutf(CON_WARN, "could not write map to %s", ogzname); return false; } - int numvslots = vslots.length(); - if(!nolms && !multiplayer(false)) - { + if(!nolms && !multiplayer(false)) { numvslots = compactvslots(); allchanged(); } - savemapprogress = 0; renderprogress(0, "saving map..."); - octaheader hdr; memcpy(hdr.magic, "OCTA", 4); hdr.version = MAPVERSION; @@ -632,36 +533,29 @@ bool save_world(const char *mname, bool nolms) hdr.lightmaps = nolms ? 0 : lightmaps.length(); hdr.numvars = 0; hdr.numvslots = numvslots; - enumerate(idents, ident, id, - { + enumerate(idents, ident, id, { if((id.type == ID_VAR || id.type == ID_FVAR || id.type == ID_SVAR) && id.flags&IDF_OVERRIDE && !(id.flags&IDF_READONLY) && id.flags&IDF_OVERRIDDEN) hdr.numvars++; }); lilswap(&hdr.version, 9); f->write(&hdr, sizeof(hdr)); - - enumerate(idents, ident, id, - { + enumerate(idents, ident, id, { if((id.type!=ID_VAR && id.type!=ID_FVAR && id.type!=ID_SVAR) || !(id.flags&IDF_OVERRIDE) || id.flags&IDF_READONLY || !(id.flags&IDF_OVERRIDDEN)) continue; f->putchar(id.type); f->putlil<ushort>(strlen(id.name)); f->write(id.name, strlen(id.name)); - switch(id.type) - { + switch(id.type) { case ID_VAR: f->putlil<int>(*id.storage.i); break; - case ID_FVAR: f->putlil<float>(*id.storage.f); break; - case ID_SVAR: f->putlil<ushort>(strlen(*id.storage.s)); f->write(*id.storage.s, strlen(*id.storage.s)); break; } }); - f->putchar((int)strlen(game::gameident())); f->write(game::gameident(), (int)strlen(game::gameident())+1); f->putlil<ushort>(entities::extraentinfosize()); @@ -669,14 +563,11 @@ bool save_world(const char *mname, bool nolms) game::writegamedata(extras); f->putlil<ushort>(extras.length()); f->write(extras.getbuf(), extras.length()); - f->putlil<ushort>(texmru.length()); loopv(texmru) f->putlil<ushort>(texmru[i]); char *ebuf = new char[entities::extraentinfosize()]; - loopv(ents) - { - if(ents[i]->type!=ET_EMPTY || nolms) - { + loopv(ents) { + if(ents[i]->type!=ET_EMPTY || nolms) { entity tmp = *ents[i]; lilswap(&tmp.o.x, 3); lilswap(&tmp.attr1, 5); @@ -685,21 +576,15 @@ bool save_world(const char *mname, bool nolms) } } delete[] ebuf; - savevslots(f, numvslots); - renderprogress(0, "saving octree..."); savec(worldroot, ivec(0, 0, 0), worldsize>>1, f, nolms); - - if(!nolms) - { + if(!nolms) { if(lightmaps.length()) renderprogress(0, "saving lightmaps..."); - loopv(lightmaps) - { + loopv(lightmaps) { LightMap &lm = lightmaps[i]; f->putchar(lm.type | (lm.unlitx>=0 ? 0x80 : 0)); - if(lm.unlitx>=0) - { + if(lm.unlitx>=0) { f->putlil<ushort>(ushort(lm.unlitx)); f->putlil<ushort>(ushort(lm.unlity)); } @@ -707,7 +592,6 @@ bool save_world(const char *mname, bool nolms) renderprogress(float(i+1)/lightmaps.length(), "saving lightmaps..."); } } - delete f; conoutf("wrote map file %s", ogzname); return true; @@ -718,8 +602,7 @@ static uint mapcrc = 0; uint getmapcrc() { return mapcrc; } void clearmapcrc() { mapcrc = 0; } -bool load_world(const char *mname, const char *cname) // still supports all map formats that have existed since the earliest cube betas! -{ +bool load_world(const char *mname, const char *cname) { // still supports all map formats that have existed since the earliest cube betas! { int loadingstart = SDL_GetTicks(); setmapfilenames(mname, cname); stream *f = opengzfile(ogzname, "rb"); @@ -729,32 +612,21 @@ bool load_world(const char *mname, const char *cname) // still supports all map lilswap(&hdr.version, 6); if(memcmp(hdr.magic, "OCTA", 4) || hdr.worldsize <= 0|| hdr.numents < 0) { conoutf(CON_ERROR, "map %s has malformatted header", ogzname); delete f; return false; } if(hdr.version>MAPVERSION) { conoutf(CON_ERROR, "map %s requires a newer version of Cube 2: Sauerbraten", ogzname); delete f; return false; } - resetmap(); - Texture *mapshot = textureload(picname, 3, true, false); renderbackground("loading...", mapshot, mname, game::getmapinfo()); - game::loadingmap(cname ? cname : mname); - setvar("mapversion", hdr.version, true, false); - lilswap(&hdr.numvslots, 1); - renderprogress(0, "clearing world..."); - freeocta(worldroot); worldroot = NULL; - int worldscale = 0; while(1<<worldscale < hdr.worldsize) worldscale++; setvar("mapsize", 1<<worldscale, true, false); setvar("mapscale", worldscale, true, false); - renderprogress(0, "loading vars..."); - - loopi(hdr.numvars) - { + loopi(hdr.numvars) { int type = f->getchar(), ilen = f->getlil<ushort>(); string name; f->read(name, min(ilen, MAXSTRLEN-1)); @@ -762,24 +634,18 @@ bool load_world(const char *mname, const char *cname) // still supports all map if(ilen >= MAXSTRLEN) f->seek(ilen - (MAXSTRLEN-1), SEEK_CUR); ident *id = getident(name); bool exists = id && id->type == type && id->flags&IDF_OVERRIDE; - switch(type) - { - case ID_VAR: - { + switch(type) { + case ID_VAR: { int val = f->getlil<int>(); if(exists && id->minval <= id->maxval) setvar(name, val); break; } - - case ID_FVAR: - { + case ID_FVAR: { float val = f->getlil<float>(); if(exists && id->minvalf <= id->maxvalf) setfvar(name, val); break; } - - case ID_SVAR: - { + case ID_SVAR: { int slen = f->getlil<ushort>(); string val; f->read(val, min(slen, MAXSTRLEN-1)); @@ -790,73 +656,54 @@ bool load_world(const char *mname, const char *cname) // still supports all map } } } - string gametype; copystring(gametype, "fps"); - int eif = 0; int len = f->getchar(); f->read(gametype, len+1); - eif = f->getlil<ushort>(); int extrasize = f->getlil<ushort>(); vector<char> extras; f->read(extras.pad(extrasize), extrasize); game::readgamedata(extras); - texmru.shrink(0); ushort nummru = f->getlil<ushort>(); loopi(nummru) texmru.add(f->getlil<ushort>()); - renderprogress(0, "loading entities..."); - vector<extentity *> &ents = entities::getents(); int einfosize = entities::extraentinfosize(); char *ebuf = einfosize > 0 ? new char[einfosize] : NULL; - loopi(min(hdr.numents, MAXENTS)) - { + loopi(min(hdr.numents, MAXENTS)) { extentity &e = *entities::newentity(); ents.add(&e); f->read(&e, sizeof(entity)); lilswap(&e.o.x, 3); lilswap(&e.attr1, 5); if(einfosize > 0) f->read(ebuf, einfosize); - if(!insideworld(e.o)) - { - if(e.type != ET_LIGHT && e.type != ET_SPOTLIGHT) - { + if(!insideworld(e.o)) { + if(e.type != ET_LIGHT && e.type != ET_SPOTLIGHT) { conoutf(CON_WARN, "warning: ent outside of world: enttype[%s] index %d (%f, %f, %f)", entities::entname(e.type), i, e.o.x, e.o.y, e.o.z); } } } if(ebuf) delete[] ebuf; - - if(hdr.numents > MAXENTS) - { + if(hdr.numents > MAXENTS) { conoutf(CON_WARN, "warning: map has %d entities", hdr.numents); f->seek((hdr.numents-MAXENTS)*(sizeof(entity) + einfosize), SEEK_CUR); } - renderprogress(0, "loading slots..."); loadvslots(f, hdr.numvslots); - renderprogress(0, "loading octree..."); bool failed = false; worldroot = loadchildren(f, ivec(0, 0, 0), hdr.worldsize>>1, failed); if(failed) conoutf(CON_ERROR, "garbage in map"); - renderprogress(0, "validating..."); validatec(worldroot, hdr.worldsize>>1); - - if(!failed) - { - loopi(hdr.lightmaps) - { + if(!failed) { + loopi(hdr.lightmaps) { renderprogress(i/(float)hdr.lightmaps, "loading lightmaps..."); - LightMap &lm = lightmaps.add(); - { + LightMap &lm = lightmaps.add(); { int type = f->getchar(); lm.type = type&0x7F; - if(type&0x80) - { + if(type&0x80) { lm.unlitx = f->getlil<ushort>(); lm.unlity = f->getlil<ushort>(); } @@ -867,37 +714,25 @@ bool load_world(const char *mname, const char *cname) // still supports all map lm.finalize(); } } - mapcrc = f->getcrc(); delete f; - conoutf("read map %s (%.1f seconds)", ogzname, (SDL_GetTicks()-loadingstart)/1000.0f); - clearmainmenu(); - identflags |= IDF_OVERRIDDEN; execfile("data/default_map.cfg", false); execfile(cfgname, false); identflags &= ~IDF_OVERRIDDEN; - preloadusedmapmodels(true); - game::preload(); flushpreloadedmodels(); - preloadmapsounds(); - entitiesinoctanodes(); attachentities(); initlights(); allchanged(true); - renderbackground("loading...", mapshot, mname, game::getmapinfo()); - if(maptitle[0] && strcmp(maptitle, "Untitled Map by Unknown")) conoutf(CON_ECHO, "%s", maptitle); - startmap(cname ? cname : mname); - return true; } |
