Line data Source code
1 : // worldio.cpp: loading & saving of maps and savegames
2 :
3 : #include "../libprimis-headers/cube.h"
4 : #include "../../shared/geomexts.h"
5 : #include "../../shared/glexts.h"
6 : #include "../../shared/stream.h"
7 :
8 : #include <format>
9 :
10 : #include "light.h"
11 : #include "octaedit.h"
12 : #include "octaworld.h"
13 : #include "raycube.h"
14 : #include "world.h"
15 :
16 : #include "interface/console.h"
17 : #include "interface/cs.h"
18 : #include "interface/menus.h"
19 :
20 : #include "render/octarender.h"
21 : #include "render/renderwindow.h"
22 : #include "render/shaderparam.h"
23 : #include "render/texture.h"
24 :
25 : namespace
26 : {
27 : std::string clientmap = "";
28 :
29 1 : void validmapname(char *dst, const char *src, const char *prefix = nullptr, const char *alt = "untitled", size_t maxlen = 100)
30 : {
31 1 : if(prefix)
32 : {
33 0 : while(*prefix)
34 : {
35 0 : *dst++ = *prefix++;
36 : }
37 : }
38 1 : const char *start = dst;
39 1 : if(src)
40 : {
41 1 : for(int i = 0; i < static_cast<int>(maxlen); ++i)
42 : {
43 1 : char c = *src++;
44 1 : if(iscubealnum(c) || c == '_' || c == '-' || c == '/' || c == '\\')
45 : {
46 0 : *dst++ = c;
47 : }
48 : else
49 : {
50 1 : break;
51 : }
52 : }
53 : }
54 1 : if(dst > start)
55 : {
56 0 : *dst = '\0';
57 : }
58 1 : else if(dst != alt)
59 : {
60 1 : copystring(dst, alt, maxlen);
61 : }
62 1 : }
63 :
64 0 : void savevslot(stream *f, const VSlot &vs, int prev)
65 : {
66 0 : f->put<int>(vs.changed);
67 0 : f->put<int>(prev);
68 0 : if(vs.changed & (1 << VSlot_ShParam))
69 : {
70 0 : f->put<ushort>(vs.params.size());
71 0 : for(const SlotShaderParam& p : vs.params)
72 : {
73 0 : f->put<ushort>(std::strlen(p.name));
74 0 : f->write(p.name, std::strlen(p.name));
75 0 : for(int k = 0; k < 4; ++k)
76 : {
77 0 : f->put<float>(p.val[k]);
78 : }
79 : }
80 : }
81 0 : if(vs.changed & (1 << VSlot_Scale))
82 : {
83 0 : f->put<float>(vs.scale);
84 : }
85 0 : if(vs.changed & (1 << VSlot_Rotation))
86 : {
87 0 : f->put<int>(vs.rotation);
88 : }
89 0 : if(vs.changed & (1 << VSlot_Angle))
90 : {
91 0 : f->put<float>(vs.angle.x);
92 0 : f->put<float>(vs.angle.y);
93 0 : f->put<float>(vs.angle.z);
94 : }
95 0 : if(vs.changed & (1 << VSlot_Offset))
96 : {
97 0 : for(int k = 0; k < 2; ++k)
98 : {
99 0 : f->put<int>(vs.offset[k]);
100 : }
101 : }
102 0 : if(vs.changed & (1 << VSlot_Scroll))
103 : {
104 0 : for(int k = 0; k < 2; ++k)
105 : {
106 0 : f->put<float>(vs.scroll[k]);
107 : }
108 : }
109 0 : if(vs.changed & (1 << VSlot_Alpha))
110 : {
111 0 : f->put<float>(vs.alphafront);
112 0 : f->put<float>(vs.alphaback);
113 : }
114 0 : if(vs.changed & (1 << VSlot_Color))
115 : {
116 0 : for(int k = 0; k < 3; ++k)
117 : {
118 0 : f->put<float>(vs.colorscale[k]);
119 : }
120 : }
121 0 : if(vs.changed & (1 << VSlot_Refract))
122 : {
123 0 : f->put<float>(vs.refractscale);
124 0 : for(int k = 0; k < 3; ++k)
125 : {
126 0 : f->put<float>(vs.refractcolor[k]);
127 : }
128 : }
129 0 : }
130 :
131 : static int savemapprogress = 0;
132 : }
133 :
134 : //used in iengine.h
135 0 : const char * getclientmap()
136 : {
137 0 : return clientmap.c_str();
138 : }
139 :
140 0 : void setmapname(const char * newname)
141 : {
142 0 : clientmap = std::string(newname);
143 0 : }
144 :
145 0 : bool cubeworld::loadmapheader(stream *f, const char *ogzname, mapheader &hdr, octaheader &ohdr) const
146 : {
147 0 : if(f->read(&hdr, 3*sizeof(int)) != 3*sizeof(int))
148 : {
149 0 : conoutf(Console_Error, "map %s has malformatted header", ogzname);
150 0 : return false;
151 : }
152 0 : if(!std::memcmp(hdr.magic, "TMAP", 4))
153 : {
154 0 : if(hdr.version>currentmapversion)
155 : {
156 0 : conoutf(Console_Error, "map %s requires a newer version of Tesseract", ogzname);
157 0 : return false;
158 : }
159 0 : if(f->read(&hdr.worldsize, 6*sizeof(int)) != 6*sizeof(int))
160 : {
161 0 : conoutf(Console_Error, "map %s has malformatted header", ogzname);
162 0 : return false;
163 : }
164 0 : if(hdr.worldsize <= 0|| hdr.numents < 0)
165 : {
166 0 : conoutf(Console_Error, "map %s has malformatted header", ogzname);
167 0 : return false;
168 : }
169 : }
170 0 : else if(!std::memcmp(hdr.magic, "OCTA", 4))
171 : {
172 0 : if(hdr.version!=octaversion)
173 : {
174 0 : conoutf(Console_Error, "map %s uses an unsupported map format version", ogzname);
175 0 : return false;
176 : }
177 0 : if(f->read(&ohdr.worldsize, 7*sizeof(int)) != 7*sizeof(int))
178 : {
179 0 : conoutf(Console_Error, "map %s has malformatted header", ogzname);
180 0 : return false;
181 : }
182 0 : if(ohdr.worldsize <= 0|| ohdr.numents < 0)
183 : {
184 0 : conoutf(Console_Error, "map %s has malformatted header", ogzname);
185 0 : return false;
186 : }
187 0 : std::memcpy(hdr.magic, "TMAP", 4);
188 0 : hdr.version = 0;
189 0 : hdr.headersize = sizeof(hdr);
190 0 : hdr.worldsize = ohdr.worldsize;
191 0 : hdr.numents = ohdr.numents;
192 0 : hdr.numvars = ohdr.numvars;
193 0 : hdr.numvslots = ohdr.numvslots;
194 : }
195 : else
196 : {
197 0 : conoutf(Console_Error, "map %s uses an unsupported map type", ogzname);
198 0 : return false;
199 : }
200 :
201 0 : return true;
202 : }
203 :
204 : VARP(savebak, 0, 2, 2);
205 :
206 0 : void cubeworld::setmapfilenames(const char *fname, const char *cname)
207 : {
208 : string name;
209 0 : validmapname(name, fname);
210 0 : formatstring(ogzname, "media/map/%s.ogz", name);
211 0 : formatstring(picname, "media/map/%s.png", name);
212 0 : if(savebak==1)
213 : {
214 0 : formatstring(bakname, "media/map/%s.BAK", name);
215 : }
216 : else
217 : {
218 : string baktime;
219 0 : time_t t = std::time(nullptr);
220 0 : size_t len = std::strftime(baktime, sizeof(baktime), "%Y-%m-%d_%H.%M.%S", std::localtime(&t));
221 0 : baktime[std::min(len, sizeof(baktime)-1)] = '\0';
222 0 : formatstring(bakname, "media/map/%s_%s.BAK", name, baktime);
223 : }
224 0 : validmapname(name, cname ? cname : fname);
225 0 : formatstring(cfgname, "media/map/%s.cfg", name);
226 0 : path(ogzname);
227 0 : path(bakname);
228 0 : path(cfgname);
229 0 : path(picname);
230 0 : }
231 :
232 1 : void mapcfgname()
233 : {
234 1 : const char *mname = clientmap.c_str();
235 : string name;
236 1 : validmapname(name, mname);
237 1 : std::string cfgname = std::format("media/map/{}.cfg", name);
238 1 : cfgname = path(cfgname);
239 1 : result(cfgname.c_str());
240 1 : }
241 :
242 0 : void backup(const char *name, const char *backupname)
243 : {
244 : string backupfile;
245 0 : copystring(backupfile, findfile(backupname, "wb"));
246 0 : std::remove(backupfile);
247 0 : std::rename(findfile(name, "wb"), backupfile);
248 0 : }
249 :
250 : enum OctaSave
251 : {
252 : OctaSave_Children = 0,
253 : OctaSave_Empty,
254 : OctaSave_Solid,
255 : OctaSave_Normal
256 : };
257 :
258 0 : void cubeworld::savec(const std::array<cube, 8> &c, const ivec &o, int size, stream * const f)
259 : {
260 0 : if((savemapprogress++&0xFFF)==0)
261 : {
262 0 : renderprogress(static_cast<float>(savemapprogress)/allocnodes, "saving octree...");
263 : }
264 0 : for(int i = 0; i < 8; ++i) //loop through children (there's always eight in an octree)
265 : {
266 0 : ivec co(i, o, size);
267 0 : if(c[i].children) //recursively note existence of children & call this fxn again
268 : {
269 0 : f->putchar(OctaSave_Children);
270 0 : savec(*(c[i].children), co, size>>1, f);
271 : }
272 : else //once we're done with all cube children within cube *c given
273 : {
274 0 : int oflags = 0,
275 0 : surfmask = 0,
276 0 : totalverts = 0;
277 0 : if(c[i].material!=Mat_Air)
278 : {
279 0 : oflags |= 0x40;
280 : }
281 0 : if(c[i].isempty()) //don't need tons of info saved if we know it's just empty
282 : {
283 0 : f->putchar(oflags | OctaSave_Empty);
284 : }
285 : else
286 : {
287 0 : if(c[i].merged)
288 : {
289 0 : oflags |= 0x80;
290 : }
291 0 : if(c[i].ext)
292 : {
293 0 : for(int j = 0; j < 6; ++j)
294 : {
295 : {
296 0 : const surfaceinfo &surf = c[i].ext->surfaces[j];
297 0 : if(!surf.used())
298 : {
299 0 : continue;
300 : }
301 0 : oflags |= 0x20;
302 0 : surfmask |= 1<<j;
303 0 : totalverts += surf.totalverts();
304 : }
305 : }
306 : }
307 0 : if(c[i].issolid())
308 : {
309 0 : f->putchar(oflags | OctaSave_Solid);
310 : }
311 : else
312 : {
313 0 : f->putchar(oflags | OctaSave_Normal);
314 0 : f->write(c[i].edges, 12);
315 : }
316 : }
317 :
318 0 : for(int j = 0; j < 6; ++j) //for each face (there's always six) save the texture slot
319 : {
320 0 : f->put<ushort>(c[i].texture[j]);
321 : }
322 0 : if(oflags&0x40) //0x40 is the code for a material (water, lava, alpha, etc.)
323 : {
324 0 : f->put<ushort>(c[i].material);
325 : }
326 0 : if(oflags&0x80) //0x80 is the code for a merged cube (remipping merged this cube with neighbors)
327 : {
328 0 : f->putchar(c[i].merged);
329 : }
330 0 : if(oflags&0x20)
331 : {
332 0 : f->putchar(surfmask);
333 0 : f->putchar(totalverts);
334 0 : for(int j = 0; j < 6; ++j)
335 : {
336 0 : if(surfmask&(1<<j))
337 : {
338 0 : surfaceinfo surf = c[i].ext->surfaces[j]; //intentional copy
339 0 : const vertinfo *verts = c[i].ext->verts() + surf.verts;
340 0 : int layerverts = surf.numverts&Face_MaxVerts,
341 0 : numverts = surf.totalverts(),
342 0 : vertmask = 0,
343 0 : vertorder = 0,
344 0 : dim = DIMENSION(j),
345 0 : vc = C[dim],
346 0 : vr = R[dim];
347 0 : if(numverts)
348 : {
349 0 : if(c[i].merged&(1<<j))
350 : {
351 0 : vertmask |= 0x04;
352 0 : if(layerverts == 4)
353 : {
354 0 : ivec v[4] = { verts[0].getxyz(), verts[1].getxyz(), verts[2].getxyz(), verts[3].getxyz() };
355 0 : for(int k = 0; k < 4; ++k)
356 : {
357 0 : const ivec &v0 = v[k],
358 0 : &v1 = v[(k+1)&3],
359 0 : &v2 = v[(k+2)&3],
360 0 : &v3 = v[(k+3)&3];
361 0 : if(v1[vc] == v0[vc] && v1[vr] == v2[vr] && v3[vc] == v2[vc] && v3[vr] == v0[vr])
362 : {
363 0 : vertmask |= 0x01;
364 0 : vertorder = k;
365 0 : break;
366 : }
367 : }
368 : }
369 : }
370 : else
371 : {
372 0 : int vis = visibletris(c[i], j, co, size);
373 0 : if(vis&4 || faceconvexity(c[i], j) < 0)
374 : {
375 0 : vertmask |= 0x01;
376 : }
377 0 : if(layerverts < 4 && vis&2)
378 : {
379 0 : vertmask |= 0x02;
380 : }
381 : }
382 0 : bool matchnorm = true;
383 0 : for(int k = 0; k < numverts; ++k)
384 : {
385 0 : const vertinfo &v = verts[k];
386 0 : if(v.norm)
387 : {
388 0 : vertmask |= 0x80;
389 0 : if(v.norm != verts[0].norm)
390 : {
391 0 : matchnorm = false;
392 : }
393 : }
394 : }
395 0 : if(matchnorm)
396 : {
397 0 : vertmask |= 0x08;
398 : }
399 : }
400 0 : surf.verts = vertmask;
401 0 : f->write(&surf, sizeof(surf));
402 0 : bool hasxyz = (vertmask&0x04)!=0,
403 0 : hasnorm = (vertmask&0x80)!=0;
404 0 : if(layerverts == 4)
405 : {
406 0 : if(hasxyz && vertmask&0x01)
407 : {
408 0 : ivec v0 = verts[vertorder].getxyz(),
409 0 : v2 = verts[(vertorder+2)&3].getxyz();
410 0 : f->put<ushort>(v0[vc]); f->put<ushort>(v0[vr]);
411 0 : f->put<ushort>(v2[vc]); f->put<ushort>(v2[vr]);
412 0 : hasxyz = false;
413 : }
414 : }
415 0 : if(hasnorm && vertmask&0x08)
416 : {
417 0 : f->put<ushort>(verts[0].norm);
418 0 : hasnorm = false;
419 : }
420 0 : if(hasxyz || hasnorm)
421 : {
422 0 : for(int k = 0; k < layerverts; ++k)
423 : {
424 0 : const vertinfo &v = verts[(k+vertorder)%layerverts];
425 0 : if(hasxyz)
426 : {
427 0 : ivec xyz = v.getxyz();
428 0 : f->put<ushort>(xyz[vc]); f->put<ushort>(xyz[vr]);
429 : }
430 0 : if(hasnorm)
431 : {
432 0 : f->put<ushort>(v.norm);
433 : }
434 : }
435 : }
436 : }
437 : }
438 : }
439 : }
440 : }
441 0 : }
442 :
443 : std::array<cube, 8> *loadchildren(stream *f, const ivec &co, int size, bool &failed);
444 :
445 : /**
446 : * @param Loads a cube, possibly containing its child cubes.
447 : *
448 : * Sets the contents of the cube passed depending on the leading flag embedded
449 : * in the string.
450 : *
451 : * If OctaSave_Children, begins recursive loading of cubes into the passed cube's `children` field
452 : *
453 : * If OctaSave_Empty, clears the cube
454 : *
455 : * If OctaSave_Solid, fills the cube completely
456 : *
457 : * If OctaSave_Normal, reads and sets the twelve edges of the cube
458 : *
459 : * If none of these are passed, failed flag is set and nothing is done.
460 : *
461 : * Once OctaSave_Empty/Solid/Normal has been initiated, loads texture, material,
462 : * normal data, and other meta information for the cube c passed
463 : */
464 0 : void loadc(stream *f, cube &c, const ivec &co, int size, bool &failed)
465 : {
466 : static constexpr uint layerdup (1<<7); //if numverts is larger than this, get additional precision
467 :
468 0 : int octsav = f->getchar();
469 0 : switch(octsav&0x7)
470 : {
471 0 : case OctaSave_Children:
472 0 : c.children = loadchildren(f, co, size>>1, failed);
473 0 : return;
474 :
475 0 : case OctaSave_Empty:
476 : {
477 0 : setcubefaces(c, faceempty);
478 0 : break;
479 : }
480 0 : case OctaSave_Solid:
481 : {
482 0 : setcubefaces(c, facesolid);
483 0 : break;
484 : }
485 0 : case OctaSave_Normal:
486 : {
487 0 : f->read(c.edges, 12);
488 0 : break;
489 : }
490 0 : default:
491 : {
492 0 : failed = true;
493 0 : return;
494 : }
495 : }
496 0 : for(uint i = 0; i < 6; ++i)
497 : {
498 0 : c.texture[i] = f->get<ushort>();
499 : }
500 0 : if(octsav&0x40)
501 : {
502 0 : c.material = f->get<ushort>();
503 : }
504 0 : if(octsav&0x80)
505 : {
506 0 : c.merged = f->getchar();
507 : }
508 0 : if(octsav&0x20)
509 : {
510 0 : int surfmask = f->getchar(),
511 0 : totalverts = std::max(f->getchar(), 0);
512 0 : newcubeext(c, totalverts, false);
513 0 : c.ext->surfaces.fill({0,0});
514 0 : std::memset(c.ext->verts(), 0, totalverts*sizeof(vertinfo));
515 0 : int offset = 0;
516 0 : for(int i = 0; i < 6; ++i)
517 : {
518 0 : if(surfmask&(1<<i))
519 : {
520 0 : surfaceinfo &surf = c.ext->surfaces[i];
521 0 : f->read(&surf, sizeof(surf));
522 0 : int vertmask = surf.verts,
523 0 : numverts = surf.totalverts();
524 0 : if(!numverts)
525 : {
526 0 : surf.verts = 0;
527 0 : continue;
528 : }
529 0 : surf.verts = offset;
530 0 : vertinfo *verts = c.ext->verts() + offset;
531 0 : offset += numverts;
532 0 : std::array<ivec, 4> v;
533 0 : ivec n(0,0,0),
534 0 : vo = ivec(co).mask(0xFFF).shl(3);
535 0 : int layerverts = surf.numverts&Face_MaxVerts, dim = DIMENSION(i), vc = C[dim], vr = R[dim], bias = 0;
536 0 : genfaceverts(c, i, v);
537 0 : bool hasxyz = (vertmask&0x04)!=0,
538 0 : hasnorm = (vertmask&0x80)!=0;
539 0 : if(hasxyz)
540 : {
541 0 : ivec e1, e2, e3;
542 0 : n.cross((e1 = v[1]).sub(v[0]), (e2 = v[2]).sub(v[0]));
543 0 : if(!n)
544 : {
545 0 : n.cross(e2, (e3 = v[3]).sub(v[0]));
546 : }
547 0 : bias = -n.dot(ivec(v[0]).mul(size).add(vo));
548 : }
549 : else
550 : {
551 0 : int vis = layerverts < 4 ? (vertmask&0x02 ? 2 : 1) : 3, order = vertmask&0x01 ? 1 : 0, k = 0;
552 0 : verts[k++].setxyz(v[order].mul(size).add(vo));
553 0 : if(vis&1)
554 : {
555 0 : verts[k++].setxyz(v[order+1].mul(size).add(vo));
556 : }
557 0 : verts[k++].setxyz(v[order+2].mul(size).add(vo));
558 0 : if(vis&2)
559 : {
560 0 : verts[k++].setxyz(v[(order+3)&3].mul(size).add(vo));
561 : }
562 : }
563 0 : if(layerverts == 4)
564 : {
565 0 : if(hasxyz && vertmask&0x01)
566 : {
567 0 : ushort c1 = f->get<ushort>(),
568 0 : r1 = f->get<ushort>(),
569 0 : c2 = f->get<ushort>(),
570 0 : r2 = f->get<ushort>();
571 0 : ivec xyz;
572 0 : xyz[vc] = c1;
573 0 : xyz[vr] = r1;
574 0 : xyz[dim] = n[dim] ? -(bias + n[vc]*xyz[vc] + n[vr]*xyz[vr])/n[dim] : vo[dim];
575 0 : verts[0].setxyz(xyz);
576 0 : xyz[vc] = c1;
577 0 : xyz[vr] = r2;
578 0 : xyz[dim] = n[dim] ? -(bias + n[vc]*xyz[vc] + n[vr]*xyz[vr])/n[dim] : vo[dim];
579 0 : verts[1].setxyz(xyz);
580 0 : xyz[vc] = c2;
581 0 : xyz[vr] = r2;
582 0 : xyz[dim] = n[dim] ? -(bias + n[vc]*xyz[vc] + n[vr]*xyz[vr])/n[dim] : vo[dim];
583 0 : verts[2].setxyz(xyz);
584 0 : xyz[vc] = c2;
585 0 : xyz[vr] = r1;
586 0 : xyz[dim] = n[dim] ? -(bias + n[vc]*xyz[vc] + n[vr]*xyz[vr])/n[dim] : vo[dim];
587 0 : verts[3].setxyz(xyz);
588 0 : hasxyz = false;
589 : }
590 : }
591 0 : if(hasnorm && vertmask&0x08)
592 : {
593 0 : ushort norm = f->get<ushort>();
594 0 : for(int k = 0; k < layerverts; ++k)
595 : {
596 0 : verts[k].norm = norm;
597 : }
598 0 : hasnorm = false;
599 : }
600 0 : if(hasxyz || hasnorm)
601 : {
602 0 : for(int k = 0; k < layerverts; ++k)
603 : {
604 0 : vertinfo &v = verts[k];
605 0 : if(hasxyz)
606 : {
607 0 : ivec xyz;
608 0 : xyz[vc] = f->get<ushort>(); xyz[vr] = f->get<ushort>();
609 0 : xyz[dim] = n[dim] ? -(bias + n[vc]*xyz[vc] + n[vr]*xyz[vr])/n[dim] : vo[dim];
610 0 : v.setxyz(xyz);
611 : }
612 0 : if(hasnorm)
613 : {
614 0 : v.norm = f->get<ushort>();
615 : }
616 : }
617 : }
618 0 : if(surf.numverts & layerdup)
619 : {
620 0 : for(int k = 0; k < layerverts; ++k)
621 : {
622 0 : f->get<ushort>();
623 0 : f->get<ushort>();
624 : }
625 : }
626 : }
627 : }
628 : }
629 : }
630 :
631 : /**
632 : * @brief Returns a heap-allocated std::array of cubes read from a file.
633 : *
634 : * These cubes must be freed using freeocta() when destroyed to prevent a leak.
635 : *
636 : * All eight cubes are read, unless the stream does not contain a valid leading
637 : * digit (see OctaSave enum), whereupon all loading thereafter is not executed.
638 : */
639 0 : std::array<cube, 8> *loadchildren(stream *f, const ivec &co, int size, bool &failed)
640 : {
641 0 : std::array<cube, 8> *c = newcubes();
642 0 : for(int i = 0; i < 8; ++i)
643 : {
644 0 : loadc(f, (*c)[i], ivec(i, co, size), size, failed);
645 0 : if(failed)
646 : {
647 0 : break;
648 : }
649 : }
650 0 : return c;
651 : }
652 :
653 : VAR(debugvars, 0, 0, 1);
654 :
655 :
656 0 : void savevslots(stream *f, int numvslots)
657 : {
658 0 : if(vslots.empty())
659 : {
660 0 : return;
661 : }
662 0 : int *prev = new int[numvslots];
663 0 : std::memset(prev, -1, numvslots*sizeof(int));
664 0 : for(int i = 0; i < numvslots; ++i)
665 : {
666 0 : VSlot *vs = vslots[i];
667 0 : if(vs->changed)
668 : {
669 0 : continue;
670 : }
671 : for(;;)
672 : {
673 0 : VSlot *cur = vs;
674 : do
675 : {
676 0 : vs = vs->next;
677 0 : } while(vs && vs->index >= numvslots);
678 0 : if(!vs)
679 : {
680 0 : break;
681 : }
682 0 : prev[vs->index] = cur->index;
683 0 : }
684 : }
685 0 : int lastroot = 0;
686 0 : for(int i = 0; i < numvslots; ++i)
687 : {
688 0 : VSlot &vs = *vslots[i];
689 0 : if(!vs.changed)
690 : {
691 0 : continue;
692 : }
693 0 : if(lastroot < i)
694 : {
695 0 : f->put<int>(-(i - lastroot));
696 : }
697 0 : savevslot(f, vs, prev[i]);
698 0 : lastroot = i+1;
699 : }
700 0 : if(lastroot < numvslots)
701 : {
702 0 : f->put<int>(-(numvslots - lastroot));
703 : }
704 0 : delete[] prev;
705 : }
706 :
707 0 : void loadvslot(stream *f, VSlot &vs, int changed)
708 : {
709 0 : vs.changed = changed;
710 0 : if(vs.changed & (1 << VSlot_ShParam))
711 : {
712 0 : int numparams = f->get<ushort>();
713 : string name;
714 0 : for(int i = 0; i < numparams; ++i)
715 : {
716 0 : vs.params.emplace_back();
717 0 : SlotShaderParam &p = vs.params.back();
718 0 : int nlen = f->get<ushort>();
719 0 : f->read(name, std::min(nlen, maxstrlen-1));
720 0 : name[std::min(nlen, maxstrlen-1)] = '\0';
721 0 : if(nlen >= maxstrlen)
722 : {
723 0 : f->seek(nlen - (maxstrlen-1), SEEK_CUR);
724 : }
725 0 : p.name = getshaderparamname(name);
726 0 : p.loc = SIZE_MAX;
727 0 : for(int k = 0; k < 4; ++k)
728 : {
729 0 : p.val[k] = f->get<float>();
730 : }
731 : }
732 : }
733 : //vslot properties (set by e.g. v-commands)
734 0 : if(vs.changed & (1 << VSlot_Scale)) //scale <factor>
735 : {
736 0 : vs.scale = f->get<float>();
737 : }
738 0 : if(vs.changed & (1 << VSlot_Rotation)) //rotate <index>
739 : {
740 0 : vs.rotation = std::clamp(f->get<int>(), 0, 7);
741 : }
742 : /*
743 : * angle uses three parameters to prebake sine/cos values for the angle it
744 : * stores despite there being only one parameter (angle) passed
745 : */
746 0 : if(vs.changed & (1 << VSlot_Angle)) //angle <angle>
747 : {
748 0 : for(int k = 0; k < 3; ++k)
749 : {
750 0 : vs.angle[k] = f->get<float>();
751 : }
752 : }
753 0 : if(vs.changed & (1 << VSlot_Offset)) //offset <x> <y>
754 : {
755 0 : for(int k = 0; k < 2; ++k)
756 : {
757 0 : vs.offset[k] = f->get<int>();
758 : }
759 : }
760 0 : if(vs.changed & (1 << VSlot_Scroll)) //scroll <x> <y>
761 : {
762 0 : for(int k = 0; k < 2; ++k)
763 : {
764 0 : vs.scroll[k] = f->get<float>();
765 : }
766 : }
767 0 : if(vs.changed & (1 << VSlot_Alpha)) //alpha <f> <b>
768 : {
769 0 : vs.alphafront = f->get<float>();
770 0 : vs.alphaback = f->get<float>();
771 : }
772 0 : if(vs.changed & (1 << VSlot_Color)) //color <r> <g> <b>
773 : {
774 0 : for(int k = 0; k < 3; ++k)
775 : {
776 0 : vs.colorscale[k] = f->get<float>();
777 : }
778 : }
779 0 : if(vs.changed & (1 << VSlot_Refract)) //refract <r> <g> <b>
780 : {
781 0 : vs.refractscale = f->get<float>();
782 0 : for(int k = 0; k < 3; ++k)
783 : {
784 0 : vs.refractcolor[k] = f->get<float>();
785 : }
786 : }
787 0 : }
788 :
789 0 : void loadvslots(stream *f, int numvslots)
790 : {
791 : //no point if loading 0 vslots
792 0 : if(numvslots == 0)
793 : {
794 0 : return;
795 : }
796 0 : uint *prev = new uint[numvslots];
797 0 : if(!prev)
798 : {
799 0 : return;
800 : }
801 0 : std::memset(prev, -1, numvslots*sizeof(int));
802 0 : while(numvslots > 0)
803 : {
804 0 : int changed = f->get<int>();
805 0 : if(changed < 0)
806 : {
807 0 : for(int i = 0; i < -changed; ++i)
808 : {
809 0 : vslots.push_back(new VSlot(nullptr, vslots.size()));
810 : }
811 0 : numvslots += changed;
812 : }
813 : else
814 : {
815 0 : prev[vslots.size()] = f->get<int>();
816 0 : vslots.push_back(new VSlot(nullptr, vslots.size()));
817 0 : loadvslot(f, *vslots.back(), changed);
818 0 : numvslots--;
819 : }
820 : }
821 0 : for(uint i = 0; i < vslots.size(); i++)
822 : {
823 0 : if(vslots.size() > prev[i])
824 : {
825 0 : vslots.at(prev[i])->next = vslots[i];
826 : }
827 : }
828 0 : delete[] prev;
829 : }
830 :
831 0 : bool cubeworld::save_world(const char *mname, const char *gameident)
832 : {
833 0 : if(!*mname)
834 : {
835 0 : mname = clientmap.c_str();
836 : }
837 0 : setmapfilenames(*mname ? mname : "untitled");
838 0 : if(savebak)
839 : {
840 0 : backup(ogzname, bakname);
841 : }
842 0 : stream *f = opengzfile(ogzname, "wb");
843 0 : if(!f)
844 : {
845 0 : conoutf(Console_Warn, "could not write map to %s", ogzname);
846 0 : return false;
847 : }
848 0 : uint numvslots = vslots.size();
849 0 : if(!multiplayer)
850 : {
851 0 : numvslots = compactvslots();
852 0 : allchanged();
853 : }
854 :
855 0 : savemapprogress = 0;
856 0 : renderprogress(0, "saving map...");
857 :
858 : mapheader hdr;
859 0 : std::memcpy(hdr.magic, "TMAP", 4);
860 0 : hdr.version = currentmapversion;
861 0 : hdr.headersize = sizeof(hdr);
862 0 : hdr.worldsize = mapsize();
863 0 : hdr.numents = 0;
864 0 : const std::vector<extentity *> &ents = entities::getents();
865 0 : for(extentity* const& e : ents)
866 : {
867 0 : if(e->type!=EngineEnt_Empty)
868 : {
869 0 : hdr.numents++;
870 : }
871 : }
872 0 : hdr.numvars = 0;
873 0 : hdr.numvslots = numvslots;
874 0 : for(auto& [k, id] : idents)
875 : {
876 0 : if((id.type == Id_Var || id.type == Id_FloatVar || id.type == Id_StringVar) &&
877 0 : id.flags&Idf_Override &&
878 0 : !(id.flags&Idf_ReadOnly) &&
879 0 : id.flags&Idf_Overridden)
880 : {
881 0 : hdr.numvars++;
882 : }
883 : }
884 0 : f->write(&hdr, sizeof(hdr));
885 :
886 0 : for(auto& [k, id] : idents)
887 : {
888 0 : if((id.type!=Id_Var && id.type!=Id_FloatVar && id.type!=Id_StringVar) ||
889 0 : !(id.flags&Idf_Override) ||
890 0 : id.flags&Idf_ReadOnly ||
891 0 : !(id.flags&Idf_Overridden))
892 : {
893 0 : continue;
894 : }
895 0 : f->putchar(id.type);
896 0 : f->put<ushort>(std::strlen(id.name));
897 0 : f->write(id.name, std::strlen(id.name));
898 0 : switch(id.type)
899 : {
900 0 : case Id_Var:
901 0 : if(debugvars)
902 : {
903 0 : conoutf(Console_Debug, "wrote var %s: %d", id.name, *id.val.storage.i);
904 : }
905 0 : f->put<int>(*id.val.storage.i);
906 0 : break;
907 :
908 0 : case Id_FloatVar:
909 0 : if(debugvars)
910 : {
911 0 : conoutf(Console_Debug, "wrote fvar %s: %f", id.name, *id.val.storage.f);
912 : }
913 0 : f->put<float>(*id.val.storage.f);
914 0 : break;
915 :
916 0 : case Id_StringVar:
917 0 : if(debugvars)
918 : {
919 0 : conoutf(Console_Debug, "wrote svar %s: %s", id.name, *id.val.storage.s);
920 : }
921 0 : f->put<ushort>(std::strlen(*id.val.storage.s));
922 0 : f->write(*id.val.storage.s, std::strlen(*id.val.storage.s));
923 0 : break;
924 : }
925 : }
926 0 : if(debugvars)
927 : {
928 0 : conoutf(Console_Debug, "wrote %d vars", hdr.numvars);
929 : }
930 0 : f->putchar(static_cast<int>(std::strlen(gameident)));
931 0 : f->write(gameident, static_cast<int>(std::strlen(gameident)+1));
932 : //=== padding for compatibility (extent properties no longer a feature)
933 0 : f->put<ushort>(0);
934 0 : f->put<ushort>(0);
935 0 : f->write(0, 0);
936 : //=== end of padding
937 0 : f->put<ushort>(texmru.size());
938 0 : for(const ushort &i : texmru)
939 : {
940 0 : f->put<ushort>(i);
941 : }
942 0 : for(const entity *i : ents)
943 : {
944 0 : if(i->type!=EngineEnt_Empty)
945 : {
946 0 : entity tmp = *i;
947 0 : f->write(&tmp, sizeof(entity));
948 : }
949 : }
950 0 : savevslots(f, numvslots);
951 0 : renderprogress(0, "saving octree...");
952 0 : savec(*worldroot, ivec(0, 0, 0), rootworld.mapsize()>>1, f);
953 0 : delete f;
954 0 : conoutf("wrote map file %s", ogzname);
955 0 : return true;
956 : }
957 :
958 0 : uint cubeworld::getmapcrc() const
959 : {
960 0 : return mapcrc;
961 : }
962 :
963 0 : void cubeworld::clearmapcrc()
964 : {
965 0 : mapcrc = 0;
966 0 : }
967 :
968 0 : bool cubeworld::load_world(const char *mname, const char *gameident, const char *gameinfo, const char *cname)
969 : {
970 0 : const int loadingstart = SDL_GetTicks();
971 0 : setmapfilenames(mname, cname);
972 0 : stream *f = opengzfile(ogzname, "rb");
973 0 : if(!f)
974 : {
975 0 : conoutf(Console_Error, "could not read map %s", ogzname);
976 0 : return false;
977 : }
978 : mapheader hdr;
979 : octaheader ohdr;
980 0 : std::memset(&ohdr, 0, sizeof(ohdr));
981 0 : if(!loadmapheader(f, ogzname, hdr, ohdr))
982 : {
983 0 : delete f;
984 0 : return false;
985 : }
986 0 : resetmap();
987 0 : const Texture *mapshot = textureload(picname, 3, true, false);
988 0 : renderbackground("loading...", mapshot, mname, gameinfo);
989 0 : setvar("mapversion", hdr.version, true, false);
990 0 : renderprogress(0, "clearing world...");
991 0 : freeocta(worldroot);
992 0 : worldroot = nullptr;
993 0 : int loadedworldscale = 0;
994 0 : while(1<<loadedworldscale < hdr.worldsize)
995 : {
996 0 : loadedworldscale++;
997 : }
998 0 : worldscale = loadedworldscale;
999 0 : renderprogress(0, "loading vars...");
1000 0 : for(int i = 0; i < hdr.numvars; ++i)
1001 : {
1002 0 : const int type = f->getchar(),
1003 0 : ilen = f->get<ushort>();
1004 : string name;
1005 0 : f->read(name, std::min(ilen, maxstrlen-1));
1006 0 : name[std::min(ilen, maxstrlen-1)] = '\0';
1007 0 : if(ilen >= maxstrlen)
1008 : {
1009 0 : f->seek(ilen - (maxstrlen-1), SEEK_CUR);
1010 : }
1011 0 : const ident *id = getident(name);
1012 : tagval val;
1013 : string str;
1014 0 : switch(type)
1015 : {
1016 0 : case Id_Var:
1017 : {
1018 0 : val.setint(f->get<int>());
1019 0 : break;
1020 : }
1021 0 : case Id_FloatVar:
1022 : {
1023 0 : val.setfloat(f->get<float>());
1024 0 : break;
1025 : }
1026 0 : case Id_StringVar:
1027 : {
1028 0 : const int slen = f->get<ushort>();
1029 0 : f->read(str, std::min(slen, maxstrlen-1));
1030 0 : str[std::min(slen, maxstrlen-1)] = '\0';
1031 0 : if(slen >= maxstrlen)
1032 : {
1033 0 : f->seek(slen - (maxstrlen-1), SEEK_CUR);
1034 : }
1035 0 : val.setstr(str);
1036 0 : break;
1037 : }
1038 0 : default:
1039 : {
1040 0 : continue;
1041 : }
1042 0 : }
1043 0 : if(id && id->flags&Idf_Override)
1044 : {
1045 0 : switch(id->type)
1046 : {
1047 0 : case Id_Var:
1048 : {
1049 0 : const int i = val.getint();
1050 0 : if(id->val.i.min <= id->val.i.max && i >= id->val.i.min && i <= id->val.i.max)
1051 : {
1052 0 : setvar(name, i);
1053 0 : if(debugvars)
1054 : {
1055 0 : conoutf(Console_Debug, "read var %s: %d", name, i);
1056 : }
1057 : }
1058 0 : break;
1059 : }
1060 0 : case Id_FloatVar:
1061 : {
1062 0 : const float f = val.getfloat();
1063 0 : if(id->val.f.min <= id->val.f.max && f >= id->val.f.min && f <= id->val.f.max)
1064 : {
1065 0 : setfvar(name, f);
1066 0 : if(debugvars)
1067 : {
1068 0 : conoutf(Console_Debug, "read fvar %s: %f", name, f);
1069 : }
1070 : }
1071 0 : break;
1072 : }
1073 0 : case Id_StringVar:
1074 : {
1075 0 : setsvar(name, val.getstr());
1076 0 : if(debugvars)
1077 : {
1078 0 : conoutf(Console_Debug, "read svar %s: %s", name, val.getstr());
1079 : }
1080 0 : break;
1081 : }
1082 : }
1083 : }
1084 : }
1085 0 : if(debugvars)
1086 : {
1087 0 : conoutf(Console_Debug, "read %d vars", hdr.numvars);
1088 : }
1089 : string gametype;
1090 0 : bool samegame = true;
1091 0 : const int len = f->getchar();
1092 0 : if(len >= 0)
1093 : {
1094 0 : f->read(gametype, len+1);
1095 : }
1096 0 : gametype[std::max(len, 0)] = '\0';
1097 0 : if(std::strcmp(gametype, gameident)!=0)
1098 : {
1099 0 : samegame = false;
1100 0 : conoutf(Console_Warn, "WARNING: loading map from %s game, ignoring entities except for lights/mapmodels", gametype);
1101 : }
1102 0 : int eif = f->get<ushort>(),
1103 0 : extrasize = f->get<ushort>();
1104 0 : std::vector<char> extras;
1105 0 : extras.reserve(extrasize);
1106 0 : f->read(&(*extras.begin()), extrasize);
1107 0 : texmru.clear();
1108 0 : const ushort nummru = f->get<ushort>();
1109 0 : for(int i = 0; i < nummru; ++i)
1110 : {
1111 0 : texmru.push_back(f->get<ushort>());
1112 : }
1113 0 : renderprogress(0, "loading entities...");
1114 0 : std::vector<extentity *> &ents = entities::getents();
1115 0 : for(int i = 0; i < (std::min(hdr.numents, maxents)); ++i)
1116 : {
1117 0 : extentity &e = *entities::newentity();
1118 0 : ents.push_back(&e);
1119 0 : f->read(&e, sizeof(entity));
1120 : //delete entities from other games
1121 0 : if(!samegame)
1122 : {
1123 0 : if(eif > 0)
1124 : {
1125 0 : f->seek(eif, SEEK_CUR);
1126 : }
1127 0 : if(e.type>=EngineEnt_GameSpecific)
1128 : {
1129 0 : entities::deleteentity(ents.back());
1130 0 : ents.pop_back();
1131 0 : continue;
1132 : }
1133 : }
1134 0 : if(!insideworld(e.o))
1135 : {
1136 0 : if(e.type != EngineEnt_Light && e.type != EngineEnt_Spotlight)
1137 : {
1138 0 : conoutf(Console_Warn, "warning: ent outside of world: index %d (%f, %f, %f)", i, e.o.x, e.o.y, e.o.z);
1139 : }
1140 : }
1141 : }
1142 0 : if(hdr.numents > maxents)
1143 : {
1144 0 : conoutf(Console_Warn, "warning: map has %d entities", hdr.numents);
1145 0 : f->seek((hdr.numents-maxents)*(samegame ? sizeof(entity) : eif), SEEK_CUR);
1146 : }
1147 0 : renderprogress(0, "loading slots...");
1148 0 : loadvslots(f, hdr.numvslots);
1149 0 : renderprogress(0, "loading octree...");
1150 0 : bool failed = false;
1151 0 : worldroot = loadchildren(f, ivec(0, 0, 0), hdr.worldsize>>1, failed);
1152 0 : if(failed)
1153 : {
1154 0 : conoutf(Console_Error, "garbage in map");
1155 : }
1156 0 : renderprogress(0, "validating...");
1157 0 : validatec(worldroot, hdr.worldsize>>1);
1158 :
1159 0 : mapcrc = f->getcrc();
1160 0 : delete f;
1161 0 : conoutf("read map %s (%.1f seconds)", ogzname, (SDL_GetTicks()-loadingstart)/1000.0f);
1162 0 : clearmainmenu();
1163 :
1164 0 : identflags |= Idf_Overridden;
1165 0 : execfile("config/default_map_settings.cfg", false);
1166 0 : execfile(cfgname, false);
1167 0 : identflags &= ~Idf_Overridden;
1168 0 : renderbackground("loading...", mapshot, mname, gameinfo);
1169 :
1170 0 : if(maptitle[0] && std::strcmp(maptitle, "Untitled Map by Unknown"))
1171 : {
1172 0 : conoutf(Console_Echo, "%s", maptitle);
1173 : }
1174 0 : return true;
1175 : }
1176 :
1177 1 : void initworldiocmds()
1178 : {
1179 1 : addcommand("mapcfgname", reinterpret_cast<identfun>(mapcfgname), "", Id_Command);
1180 2 : addcommand("mapversion", reinterpret_cast<identfun>(+[] () {intret(currentmapversion);}), "", Id_Command);
1181 1 : }
|