Line data Source code
1 : /**
2 : * @brief world modification core functionality
3 : *
4 : * modifying the octree grid can be done by changing the states of cube nodes within
5 : * the world, which is made easier with octaedit.cpp's notions of selections (a
6 : * rectangular selection of cubes with which to modify together). Selections can
7 : * be modified all at once, copied, and pasted throughout the world instead of individual
8 : * cubes being modified.
9 : *
10 : * additionally, this file contains core functionality for rendering of selections
11 : * and other constructs generally useful for modifying the level, such as entity
12 : * locations and radii.
13 : */
14 : #include "../libprimis-headers/cube.h"
15 : #include "../../shared/geomexts.h"
16 : #include "../../shared/glemu.h"
17 : #include "../../shared/glexts.h"
18 : #include "../../shared/stream.h"
19 :
20 : #include "light.h"
21 : #include "octaedit.h"
22 : #include "octaworld.h"
23 : #include "raycube.h"
24 :
25 : #include "interface/console.h"
26 : #include "interface/control.h"
27 : #include "interface/input.h"
28 :
29 : #include "render/hud.h"
30 : #include "render/octarender.h"
31 : #include "render/rendergl.h"
32 : #include "render/renderlights.h"
33 : #include "render/renderva.h"
34 : #include "render/renderwindow.h"
35 : #include "render/shader.h"
36 : #include "render/shaderparam.h"
37 : #include "render/texture.h"
38 :
39 : #include "heightmap.h"
40 : #include "material.h"
41 : #include "world.h"
42 :
43 : //used in iengine.h
44 0 : void boxs(int orient, vec o, const vec &s, float size, bool boxoutline)
45 : {
46 0 : int d = DIMENSION(orient),
47 0 : dc = DIM_COORD(orient);
48 0 : float f = boxoutline ? (dc>0 ? 0.2f : -0.2f) : 0;
49 0 : o[D[d]] += dc * s[D[d]] + f;
50 :
51 0 : vec r(0, 0, 0),
52 0 : c(0, 0, 0);
53 0 : r[R[d]] = s[R[d]];
54 0 : c[C[d]] = s[C[d]];
55 :
56 0 : vec v1 = o,
57 0 : v2 = vec(o).add(r),
58 0 : v3 = vec(o).add(r).add(c),
59 0 : v4 = vec(o).add(c);
60 :
61 0 : r[R[d]] = 0.5f*size;
62 0 : c[C[d]] = 0.5f*size;
63 :
64 0 : gle::defvertex();
65 0 : gle::begin(GL_TRIANGLE_STRIP);
66 0 : gle::attrib(vec(v1).sub(r).sub(c));
67 0 : gle::attrib(vec(v1).add(r).add(c));
68 :
69 0 : gle::attrib(vec(v2).add(r).sub(c));
70 0 : gle::attrib(vec(v2).sub(r).add(c));
71 :
72 0 : gle::attrib(vec(v3).add(r).add(c));
73 0 : gle::attrib(vec(v3).sub(r).sub(c));
74 :
75 0 : gle::attrib(vec(v4).sub(r).add(c));
76 0 : gle::attrib(vec(v4).add(r).sub(c));
77 :
78 0 : gle::attrib(vec(v1).sub(r).sub(c));
79 0 : gle::attrib(vec(v1).add(r).add(c));
80 0 : xtraverts += gle::end();
81 0 : }
82 :
83 : //used in iengine.h
84 : //boxsquare, draws a 2d square at a specified location
85 0 : void boxs(int orient, vec origin, const vec &s, bool boxoutline)
86 : {
87 0 : int d = DIMENSION(orient),
88 0 : dc = DIM_COORD(orient);
89 0 : float f = boxoutline ? (dc>0 ? 0.2f : -0.2f) : 0;
90 0 : origin[D[d]] += dc * s[D[d]] + f;
91 :
92 0 : gle::defvertex();
93 0 : gle::begin(GL_LINE_LOOP);
94 : //draw four lines
95 0 : gle::attrib(origin); origin[R[d]] += s[R[d]];
96 0 : gle::attrib(origin); origin[C[d]] += s[C[d]];
97 0 : gle::attrib(origin); origin[R[d]] -= s[R[d]];
98 0 : gle::attrib(origin);
99 :
100 0 : xtraverts += gle::end();
101 0 : }
102 :
103 : //used in iengine.h
104 0 : void boxs3D(const vec &origin, vec s, int g, bool boxoutline)
105 : {
106 0 : s.mul(g); //multiply displacement by g(ridpower)
107 0 : for(int i = 0; i < 6; ++i) //for each face
108 : {
109 0 : boxs(i, origin, s, boxoutline);
110 : }
111 0 : }
112 :
113 : //used in iengine.h
114 0 : void boxsgrid(int orient, vec origin, vec s, int g, bool boxoutline)
115 : {
116 0 : int d = DIMENSION(orient),
117 0 : dc = DIM_COORD(orient);
118 0 : float ox = origin[R[d]],
119 0 : oy = origin[C[d]],
120 0 : xs = s[R[d]],
121 0 : ys = s[C[d]],
122 0 : f = boxoutline ? (dc>0 ? 0.2f : -0.2f) : 0;
123 :
124 0 : origin[D[d]] += dc * s[D[d]]*g + f;
125 :
126 0 : gle::defvertex();
127 0 : gle::begin(GL_LINES);
128 0 : for(int x = 0; x < xs; ++x)
129 : {
130 0 : origin[R[d]] += g;
131 0 : gle::attrib(origin);
132 0 : origin[C[d]] += ys*g;
133 0 : gle::attrib(origin);
134 0 : origin[C[d]] = oy;
135 : }
136 0 : for(int y = 0; y < ys; ++y)
137 : {
138 0 : origin[C[d]] += g;
139 0 : origin[R[d]] = ox;
140 0 : gle::attrib(origin);
141 0 : origin[R[d]] += xs*g;
142 0 : gle::attrib(origin);
143 : }
144 0 : xtraverts += gle::end();
145 0 : }
146 :
147 : selinfo sel, lastsel; //lastsel is used only in iengine
148 : static selinfo savedsel;
149 :
150 0 : bool selinfo::validate()
151 : {
152 0 : if(grid <= 0 || grid >= rootworld.mapsize())
153 : {
154 0 : return false;
155 : }
156 0 : if(o.x >= rootworld.mapsize() || o.y >= rootworld.mapsize() || o.z >= rootworld.mapsize())
157 : {
158 0 : return false;
159 : }
160 0 : if(o.x < 0)
161 : {
162 0 : s.x -= (grid - 1 - o.x)/grid;
163 0 : o.x = 0;
164 : }
165 0 : if(o.y < 0)
166 : {
167 0 : s.y -= (grid - 1 - o.y)/grid;
168 0 : o.y = 0;
169 : }
170 0 : if(o.z < 0)
171 : {
172 0 : s.z -= (grid - 1 - o.z)/grid;
173 0 : o.z = 0;
174 : }
175 0 : s.x = std::clamp(s.x, 0, (rootworld.mapsize() - o.x)/grid);
176 0 : s.y = std::clamp(s.y, 0, (rootworld.mapsize() - o.y)/grid);
177 0 : s.z = std::clamp(s.z, 0, (rootworld.mapsize() - o.z)/grid);
178 0 : return s.x > 0 && s.y > 0 && s.z > 0;
179 : }
180 :
181 : int orient = 0,
182 : gridsize = 8;
183 : static ivec lastcur;
184 : ivec cor, lastcor, cur; //used in iengine
185 :
186 : bool editmode = false,
187 : multiplayer = false,
188 : allowediting = false,
189 : havesel = false;
190 : int horient = 0,
191 : entmoving = 0;
192 :
193 : //used in iengine
194 0 : VARF(entediting, 0, 0, 1,
195 : {
196 : if(!entediting)
197 : {
198 : entcancel();
199 : }
200 : });
201 :
202 : //used in iengine
203 3 : void multiplayerwarn()
204 : {
205 3 : conoutf(Console_Error, "operation not available in multiplayer");
206 3 : }
207 :
208 : //used in iengine
209 0 : bool pointinsel(const selinfo &sel, const vec &origin)
210 : {
211 0 : return(origin.x <= sel.o.x+sel.s.x*sel.grid
212 0 : && origin.x >= sel.o.x
213 0 : && origin.y <= sel.o.y+sel.s.y*sel.grid
214 0 : && origin.y >= sel.o.y
215 0 : && origin.z <= sel.o.z+sel.s.z*sel.grid
216 0 : && origin.z >= sel.o.z);
217 : }
218 :
219 : //used in iengine
220 0 : VARF(dragging, 0, 0, 1,
221 : if(!dragging || cor[0]<0)
222 : {
223 : return;
224 : }
225 : lastcur = cur;
226 : lastcor = cor;
227 : sel.grid = gridsize;
228 : sel.orient = orient;
229 : );
230 :
231 : int moving = 0;
232 :
233 0 : VARF(gridpower, 0, 3, 12,
234 : {
235 : if(dragging)
236 : {
237 : return;
238 : }
239 : gridsize = 1<<gridpower;
240 : if(gridsize>=rootworld.mapsize())
241 : {
242 : gridsize = rootworld.mapsize()/2;
243 : }
244 : cancelsel();
245 : });
246 :
247 : VAR(passthroughsel, 0, 0, 1); //used in iengine
248 : VAR(selectcorners, 0, 0, 1); //used in iengine
249 0 : VARF(hmapedit, 0, 0, 1, horient = sel.orient); //used in iengine
250 :
251 : //used in iengine
252 2 : void forcenextundo()
253 : {
254 2 : lastsel.orient = -1;
255 2 : }
256 :
257 2 : static void cubecancel()
258 : {
259 2 : havesel = false;
260 2 : moving = dragging = hmapedit = passthroughsel = 0;
261 2 : forcenextundo();
262 2 : hmapcancel();
263 2 : }
264 :
265 : //used in iengine
266 1 : void cancelsel()
267 : {
268 1 : cubecancel();
269 1 : entcancel();
270 1 : }
271 :
272 : //used in iengine
273 0 : bool haveselent()
274 : {
275 0 : return entgroup.size() > 0;
276 : }
277 :
278 : //used in iengine
279 5 : bool noedit(bool inview, bool msg)
280 : {
281 5 : if(!editmode)
282 : {
283 5 : if(msg)
284 : {
285 5 : conoutf(Console_Error, "operation only allowed in edit mode");
286 : }
287 5 : return true;
288 : }
289 0 : if(inview || haveselent())
290 : {
291 0 : return false;
292 : }
293 0 : vec o(sel.o),
294 0 : s(sel.s);
295 0 : s.mul(sel.grid / 2.0f);
296 0 : o.add(s);
297 0 : float r = std::max(std::max(s.x, s.y), s.z);
298 0 : bool viewable = view.isvisiblesphere(r, o) != ViewFrustumCull_NotVisible;
299 0 : if(!viewable && msg)
300 : {
301 0 : conoutf(Console_Error, "selection not in view");
302 : }
303 0 : return !viewable;
304 : }
305 :
306 : //used in iengine
307 1 : void reorient()
308 : {
309 1 : sel.cx = 0;
310 1 : sel.cy = 0;
311 1 : sel.cxs = sel.s[R[DIMENSION(orient)]]*2;
312 1 : sel.cys = sel.s[C[DIMENSION(orient)]]*2;
313 1 : sel.orient = orient;
314 1 : }
315 :
316 : ///////// selection support /////////////
317 :
318 0 : cube &blockcube(int x, int y, int z, const block3 &b, int rgrid) // looks up a world cube, based on coordinates mapped by the block
319 : {
320 0 : int dim = DIMENSION(b.orient),
321 0 : dc = DIM_COORD(b.orient);
322 : //ivec::ivec(int d, int row, int col, int depth)
323 0 : ivec s(dim, x*b.grid, y*b.grid, dc*(b.s[dim]-1)*b.grid);
324 0 : s.add(b.o);
325 0 : if(dc)
326 : {
327 0 : s[dim] -= z*b.grid;
328 : }
329 : else
330 : {
331 0 : s[dim] += z*b.grid;
332 : }
333 0 : return rootworld.lookupcube(s, rgrid);
334 : }
335 :
336 : ////////////// cursor ///////////////
337 :
338 : int selchildcount = 0, //used in iengine
339 : selchildmat = -1; //used in iengine
340 :
341 : //used in iengine.h
342 0 : void countselchild(const std::array<cube, 8> &c, const ivec &cor, int size)
343 : {
344 0 : ivec ss = ivec(sel.s).mul(sel.grid);
345 0 : uchar possible = octaboxoverlap(cor, size, sel.o, ivec(sel.o).add(ss));
346 0 : for(int i = 0; i < 8; ++i)
347 : {
348 0 : if(possible&(1<<i))
349 : {
350 0 : ivec o(i, cor, size);
351 0 : if(c[i].children)
352 : {
353 0 : countselchild(*(c[i].children), o, size/2);
354 : }
355 : else
356 : {
357 0 : selchildcount++;
358 0 : if(c[i].material != Mat_Air && selchildmat != Mat_Air)
359 : {
360 0 : if(selchildmat < 0)
361 : {
362 0 : selchildmat = c[i].material;
363 : }
364 0 : else if(selchildmat != c[i].material)
365 : {
366 0 : selchildmat = Mat_Air;
367 : }
368 : }
369 : }
370 : }
371 : }
372 0 : }
373 :
374 : //used in iengine.h
375 0 : void normalizelookupcube(const ivec &o)
376 : {
377 0 : if(lusize>gridsize)
378 : {
379 0 : lu.x += (o.x-lu.x)/gridsize*gridsize;
380 0 : lu.y += (o.y-lu.y)/gridsize*gridsize;
381 0 : lu.z += (o.z-lu.z)/gridsize*gridsize;
382 : }
383 0 : else if(gridsize>lusize)
384 : {
385 0 : lu.x &= ~(gridsize-1);
386 0 : lu.y &= ~(gridsize-1);
387 0 : lu.z &= ~(gridsize-1);
388 : }
389 0 : lusize = gridsize;
390 0 : }
391 :
392 : //used in iengine.h
393 0 : void updateselection()
394 : {
395 0 : sel.o.x = std::min(lastcur.x, cur.x);
396 0 : sel.o.y = std::min(lastcur.y, cur.y);
397 0 : sel.o.z = std::min(lastcur.z, cur.z);
398 0 : sel.s.x = std::abs(lastcur.x-cur.x)/sel.grid+1;
399 0 : sel.s.y = std::abs(lastcur.y-cur.y)/sel.grid+1;
400 0 : sel.s.z = std::abs(lastcur.z-cur.z)/sel.grid+1;
401 0 : }
402 :
403 0 : bool editmoveplane(const vec &o, const vec &ray, int d, float off, vec &handle, vec &dest, bool first)
404 : {
405 0 : plane pl(d, off);
406 0 : float dist = 0.0f;
407 0 : if(!pl.rayintersect(player->o, ray, dist))
408 : {
409 0 : return false;
410 : }
411 0 : dest = vec(ray).mul(dist).add(player->o);
412 0 : if(first)
413 : {
414 0 : handle = vec(dest).sub(o);
415 : }
416 0 : dest.sub(handle);
417 0 : return true;
418 : }
419 :
420 : //////////// ready changes to vertex arrays ////////////
421 :
422 0 : static void readychanges(const ivec &bbmin, const ivec &bbmax, std::array<cube, 8> &c, const ivec &cor, int size)
423 : {
424 0 : LOOP_OCTA_BOX(cor, size, bbmin, bbmax)
425 : {
426 0 : ivec o(i, cor, size);
427 0 : if(c[i].ext)
428 : {
429 0 : if(c[i].ext->va) // removes va s so that octarender will recreate
430 : {
431 0 : int hasmerges = c[i].ext->va->hasmerges;
432 0 : destroyva(c[i].ext->va);
433 0 : c[i].ext->va = nullptr;
434 0 : if(hasmerges)
435 : {
436 0 : invalidatemerges(c[i]);
437 : }
438 : }
439 0 : freeoctaentities(c[i]);
440 0 : c[i].ext->tjoints = -1;
441 : }
442 0 : if(c[i].children)
443 : {
444 0 : if(size<=1)
445 : {
446 0 : setcubefaces(c[i], facesolid);
447 0 : c[i].discardchildren(true);
448 0 : brightencube(c[i]);
449 : }
450 : else
451 : {
452 0 : readychanges(bbmin, bbmax, *(c[i].children), o, size/2);
453 : }
454 : }
455 : else
456 : {
457 0 : brightencube(c[i]);
458 : }
459 : }
460 0 : }
461 :
462 0 : void cubeworld::commitchanges(bool force)
463 : {
464 0 : if(!force && !haschanged)
465 : {
466 0 : return;
467 : }
468 0 : haschanged = false;
469 0 : int oldlen = valist.size();
470 0 : resetclipplanes();
471 0 : entitiesinoctanodes();
472 0 : inbetweenframes = false;
473 0 : octarender();
474 0 : inbetweenframes = true;
475 0 : setupmaterials(oldlen);
476 0 : clearshadowcache();
477 0 : updatevabbs();
478 : }
479 :
480 0 : void cubeworld::changed(const ivec &bbmin, const ivec &bbmax, bool commit)
481 : {
482 0 : readychanges(bbmin, bbmax, *worldroot, ivec(0, 0, 0), mapsize()/2);
483 0 : haschanged = true;
484 :
485 0 : if(commit)
486 : {
487 0 : commitchanges();
488 : }
489 0 : }
490 :
491 0 : void cubeworld::changed(const block3 &sel, bool commit)
492 : {
493 0 : if(!sel.s)
494 : {
495 0 : return;
496 : }
497 0 : readychanges(ivec(sel.o).sub(1), ivec(sel.s).mul(sel.grid).add(sel.o).add(1), *worldroot, ivec(0, 0, 0), mapsize()/2);
498 0 : haschanged = true;
499 0 : if(commit)
500 : {
501 0 : commitchanges();
502 : }
503 : }
504 :
505 : //////////// copy and undo /////////////
506 0 : static void copycube(const cube &src, cube &dst)
507 : {
508 0 : dst = src;
509 0 : dst.visible = 0;
510 0 : dst.merged = 0;
511 0 : dst.ext = nullptr; // src cube is responsible for va destruction
512 : //recursively apply to children
513 0 : if(src.children)
514 : {
515 0 : dst.children = newcubes(faceempty);
516 0 : for(int i = 0; i < 8; ++i)
517 : {
518 0 : copycube((*src.children)[i], (*dst.children)[i]);
519 : }
520 : }
521 0 : }
522 :
523 0 : void pastecube(const cube &src, cube &dst)
524 : {
525 0 : dst.discardchildren();
526 0 : copycube(src, dst);
527 0 : }
528 :
529 0 : static void blockcopy(const block3 &s, int rgrid, block3 *b)
530 : {
531 0 : *b = s;
532 0 : cube *q = b->c();
533 0 : uint i = 0;
534 0 : LOOP_XYZ(s, rgrid, copycube(c, q[i]); i++);
535 0 : }
536 :
537 : //used in iengine.h
538 0 : block3 *blockcopy(const block3 &s, int rgrid)
539 : {
540 0 : int bsize = sizeof(block3)+sizeof(cube)*s.size();
541 0 : if(bsize <= 0 || bsize > (100<<20))
542 : {
543 0 : return nullptr;
544 : }
545 0 : block3 *b = reinterpret_cast<block3 *>(new uchar[bsize]); //create a new block3 pointing to an appropriate sized memory area
546 0 : if(b) //should always be true
547 : {
548 0 : blockcopy(s, rgrid, b); //copy the block3 s to b
549 : }
550 0 : return b;
551 : }
552 :
553 : //used in iengine.h
554 0 : void freeblock(block3 *b, bool alloced = true)
555 : {
556 0 : cube *q = b->c();
557 0 : uint j = 0;
558 0 : for(int i = 0; i < b->size(); ++i)
559 : {
560 0 : (q[j]).discardchildren();
561 0 : j++;
562 : }
563 0 : if(alloced)
564 : {
565 0 : delete[] b;
566 : }
567 0 : }
568 :
569 0 : void selgridmap(const selinfo &sel, uchar *g)
570 : {
571 0 : for(int z = 0; z < sel.s[D[DIMENSION(sel.orient)]]; ++z)
572 : {
573 0 : for(int y = 0; y < sel.s[C[DIMENSION(sel.orient)]]; ++y)
574 : {
575 0 : for(int x = 0; x < sel.s[R[DIMENSION(sel.orient)]]; ++x)
576 : {
577 0 : blockcube(x,y,z,sel,-sel.grid);
578 0 : *g++ = BITSCAN(lusize);
579 : }
580 : }
581 : }
582 0 : }
583 :
584 0 : void freeundo(undoblock *u)
585 : {
586 0 : if(!u->numents)
587 : {
588 0 : freeblock(u->block(), false);
589 : }
590 0 : delete[] reinterpret_cast<uchar *>(u); //re-cast to uchar array so it can be destructed properly
591 0 : }
592 :
593 0 : static int undosize(undoblock *u)
594 : {
595 0 : if(u->numents)
596 : {
597 0 : return u->numents*sizeof(undoent);
598 : }
599 : else
600 : {
601 0 : const block3 *b = u->block();
602 0 : const cube *q = b->getcube();
603 0 : int size = b->size(),
604 0 : total = size;
605 0 : uint i = 0;
606 0 : for(int j = 0; j < size; ++j)
607 : {
608 0 : total += familysize(q[i])*sizeof(cube);
609 0 : i++;
610 : }
611 0 : return total;
612 : }
613 : }
614 :
615 : std::deque<undoblock *> undos, redos;
616 : VARP(undomegs, 0, 5, 100); // bounded by n megs, zero means no undo history
617 : int totalundos = 0;
618 :
619 1 : void pruneundos(int maxremain) // bound memory
620 : {
621 1 : while(totalundos > maxremain && !undos.empty())
622 : {
623 0 : undoblock *u = undos.front();
624 0 : undos.pop_front();
625 0 : totalundos -= u->size;
626 0 : freeundo(u);
627 : }
628 : //conoutf(CON_DEBUG, "undo: %d of %d(%%%d)", totalundos, undomegs<<20, totalundos*100/(undomegs<<20));
629 1 : while(!redos.empty())
630 : {
631 0 : undoblock *u = redos.front();
632 0 : redos.pop_front();
633 0 : totalundos -= u->size;
634 0 : freeundo(u);
635 : }
636 1 : }
637 :
638 0 : undoblock *newundocube(const selinfo &s)
639 : {
640 0 : int ssize = s.size(),
641 0 : selgridsize = ssize,
642 0 : blocksize = sizeof(block3)+ssize*sizeof(cube);
643 0 : if(blocksize <= 0 || blocksize > (undomegs<<20))
644 : {
645 0 : return nullptr;
646 : }
647 0 : undoblock *u = reinterpret_cast<undoblock *>(new uchar[sizeof(undoblock) + blocksize + selgridsize]);
648 0 : if(!u)
649 : {
650 0 : return nullptr;
651 : }
652 0 : u->numents = 0;
653 0 : block3 *b = u->block();
654 0 : blockcopy(s, -s.grid, b);
655 0 : uchar *g = u->gridmap();
656 0 : selgridmap(s, g);
657 0 : return u;
658 : }
659 :
660 0 : void addundo(undoblock *u)
661 : {
662 0 : u->size = undosize(u);
663 0 : u->timestamp = totalmillis;
664 0 : undos.push_back(u);
665 0 : totalundos += u->size;
666 0 : pruneundos(undomegs<<20);
667 0 : }
668 :
669 : VARP(nompedit, 0, 1, 1);
670 :
671 0 : static int countblock(const cube * const c, int n = 8)
672 : {
673 0 : int r = 0;
674 0 : for(int i = 0; i < n; ++i)
675 : {
676 0 : if(c[i].children)
677 : {
678 0 : r += countblock(c[i].children->data());
679 : }
680 : else
681 : {
682 0 : ++r;
683 : }
684 : }
685 0 : return r;
686 : }
687 :
688 0 : int countblock(block3 *b)
689 : {
690 0 : return countblock(b->getcube(), b->size());
691 : }
692 :
693 : std::vector<editinfo *> editinfos;
694 :
695 : template<class B>
696 0 : static void packcube(const cube &c, B &buf)
697 : {
698 : //recursvely apply to children
699 0 : if(c.children)
700 : {
701 0 : buf.push_back(0xFF);
702 0 : for(int i = 0; i < 8; ++i)
703 : {
704 0 : packcube((*c.children)[i], buf);
705 : }
706 : }
707 : else
708 : {
709 0 : const cube &data = c;
710 0 : buf.push_back(c.material&0xFF);
711 0 : buf.push_back(c.material>>8);
712 0 : for(uint i = 0; i < sizeof(data.edges); ++i)
713 : {
714 0 : buf.push_back(data.edges[i]);
715 : }
716 0 : for(uint i = 0; i < sizeof(data.texture); ++i)
717 : {
718 0 : buf.push_back(reinterpret_cast<const uchar *>(data.texture)[i]);
719 : }
720 : }
721 0 : }
722 :
723 : template<class B>
724 0 : static bool packblock(const block3 &b, B &buf)
725 : {
726 0 : if(b.size() <= 0 || b.size() > (1<<20))
727 : {
728 0 : return false;
729 : }
730 0 : block3 hdr = b;
731 0 : for(uint i = 0; i < sizeof(hdr); ++i)
732 : {
733 0 : buf.push_back(reinterpret_cast<const uchar *>(&hdr)[i]);
734 : }
735 0 : const cube *c = b.getcube();
736 0 : for(int i = 0; i < b.size(); ++i)
737 : {
738 0 : packcube(c[i], buf);
739 : }
740 0 : return true;
741 : }
742 :
743 : struct VSlotHeader final
744 : {
745 : ushort index;
746 : ushort slot;
747 : };
748 :
749 0 : static void packvslots(const cube &c, std::vector<uchar> &buf, std::vector<ushort> &used)
750 : {
751 : //recursively apply to children
752 0 : if(c.children)
753 : {
754 0 : for(int i = 0; i < 8; ++i)
755 : {
756 0 : packvslots((*c.children)[i], buf, used);
757 : }
758 : }
759 : else
760 : {
761 0 : for(int i = 0; i < 6; ++i) //for each face
762 : {
763 0 : ushort index = c.texture[i];
764 0 : if((vslots.size() > index) && vslots[index]->changed && std::find(used.begin(), used.end(), index) != used.end())
765 : {
766 0 : used.push_back(index);
767 0 : VSlot &vs = *vslots[index];
768 0 : for(uint i = 0; i < sizeof(VSlotHeader); ++i)
769 : {
770 0 : buf.emplace_back();
771 : }
772 0 : VSlotHeader &hdr = *reinterpret_cast<VSlotHeader *>(&(*(buf.end())) - sizeof(VSlotHeader));
773 0 : hdr.index = index;
774 0 : hdr.slot = vs.slot->index;
775 0 : packvslot(buf, vs);
776 : }
777 : }
778 : }
779 0 : }
780 :
781 0 : static void packvslots(const block3 &b, std::vector<uchar> &buf)
782 : {
783 0 : std::vector<ushort> used;
784 0 : const cube *c = b.getcube();
785 0 : for(int i = 0; i < b.size(); ++i)
786 : {
787 0 : packvslots(c[i], buf, used);
788 : }
789 0 : for(uint i = 0; i < sizeof(VSlotHeader); ++i)
790 : {
791 0 : buf.push_back(0);
792 : }
793 0 : }
794 :
795 : template<class B>
796 0 : static void unpackcube(cube &c, B &buf)
797 : {
798 0 : int mat = buf.get();
799 0 : if(mat == 0xFF)
800 : {
801 0 : c.children = newcubes(faceempty);
802 : //recursively apply to children
803 0 : for(int i = 0; i < 8; ++i)
804 : {
805 0 : unpackcube((*c.children)[i], buf);
806 : }
807 : }
808 : else
809 : {
810 0 : c.material = mat | (buf.get()<<8);
811 0 : buf.get(c.edges, sizeof(c.edges));
812 0 : buf.get(reinterpret_cast<uchar *>(c.texture), sizeof(c.texture));
813 : }
814 0 : }
815 :
816 : template<class B>
817 0 : static bool unpackblock(block3 *&b, B &buf)
818 : {
819 0 : if(b)
820 : {
821 0 : freeblock(b);
822 0 : b = nullptr;
823 : }
824 0 : block3 hdr;
825 0 : if(buf.get(reinterpret_cast<uchar *>(&hdr), sizeof(hdr)) < static_cast<int>(sizeof(hdr)))
826 : {
827 0 : return false;
828 : }
829 0 : if(hdr.size() > (1<<20) || hdr.grid <= 0 || hdr.grid > (1<<12))
830 : {
831 0 : return false;
832 : }
833 0 : b = reinterpret_cast<block3 *>(new uchar[sizeof(block3)+hdr.size()*sizeof(cube)]);
834 0 : if(!b)
835 : {
836 0 : return false;
837 : }
838 0 : *b = hdr;
839 0 : cube *c = b->c();
840 0 : std::memset(c, 0, b->size()*sizeof(cube));
841 0 : for(int i = 0; i < b->size(); ++i)
842 : {
843 0 : unpackcube(c[i], buf);
844 : }
845 0 : return true;
846 : }
847 :
848 : struct vslotmap final
849 : {
850 : int index;
851 : VSlot *vslot;
852 :
853 : vslotmap() {}
854 0 : vslotmap(int index, VSlot *vslot) : index(index), vslot(vslot) {}
855 : };
856 :
857 : static std::vector<vslotmap> remappedvslots;
858 :
859 : //used in iengine.h so remappedvslots does not need to be exposed
860 0 : void clearremappedvslots()
861 : {
862 0 : remappedvslots.clear();
863 0 : }
864 : static std::vector<vslotmap> unpackingvslots;
865 :
866 0 : static void unpackvslots(cube &c, ucharbuf &buf)
867 : {
868 : //recursively apply to children
869 0 : if(c.children)
870 : {
871 0 : for(int i = 0; i < 8; ++i)
872 : {
873 0 : unpackvslots((*c.children)[i], buf);
874 : }
875 : }
876 : else
877 : {
878 0 : for(int i = 0; i < 6; ++i) //one for each face
879 : {
880 0 : ushort tex = c.texture[i];
881 0 : for(size_t j = 0; j < unpackingvslots.size(); j++)
882 : {
883 0 : if(unpackingvslots[j].index == tex)
884 : {
885 0 : c.texture[i] = unpackingvslots[j].vslot->index;
886 0 : break;
887 : }
888 : }
889 : }
890 : }
891 0 : }
892 :
893 0 : static void unpackvslots(block3 &b, ucharbuf &buf)
894 : {
895 0 : while(buf.remaining() >= static_cast<int>(sizeof(VSlotHeader)))
896 : {
897 0 : VSlotHeader &hdr = *reinterpret_cast<VSlotHeader *>(buf.pad(sizeof(VSlotHeader)));
898 0 : if(!hdr.index)
899 : {
900 0 : break;
901 : }
902 0 : VSlot &vs = *lookupslot(hdr.slot, false).variants;
903 0 : VSlot ds;
904 0 : if(!unpackvslot(buf, ds, false))
905 : {
906 0 : break;
907 : }
908 0 : if(vs.index < 0 || vs.index == Default_Sky)
909 : {
910 0 : continue;
911 : }
912 0 : VSlot *edit = editvslot(vs, ds);
913 0 : unpackingvslots.emplace_back(vslotmap(hdr.index, edit ? edit : &vs));
914 0 : }
915 :
916 0 : cube *c = b.c();
917 0 : for(int i = 0; i < b.size(); ++i)
918 : {
919 0 : unpackvslots(c[i], buf);
920 : }
921 :
922 0 : unpackingvslots.clear();
923 0 : }
924 :
925 0 : static bool compresseditinfo(const uchar *inbuf, int inlen, uchar *&outbuf, int &outlen)
926 : {
927 0 : uLongf len = compressBound(inlen);
928 0 : if(len > (1<<20))
929 : {
930 0 : return false;
931 : }
932 0 : outbuf = new uchar[len];
933 0 : if(!outbuf || compress2(static_cast<Bytef *>(outbuf), &len, static_cast<const Bytef *>(inbuf), inlen, Z_BEST_COMPRESSION) != Z_OK || len > (1<<16))
934 : {
935 0 : delete[] outbuf;
936 0 : outbuf = nullptr;
937 0 : return false;
938 : }
939 0 : outlen = len;
940 0 : return true;
941 : }
942 :
943 : //used in iengine.h
944 0 : bool uncompresseditinfo(const uchar *inbuf, int inlen, uchar *&outbuf, int &outlen)
945 : {
946 0 : if(compressBound(outlen) > (1<<20))
947 : {
948 0 : return false;
949 : }
950 0 : uLongf len = outlen;
951 0 : outbuf = new uchar[len];
952 0 : if(!outbuf || uncompress(static_cast<Bytef *>(outbuf), &len, static_cast<const Bytef *>(inbuf), inlen) != Z_OK)
953 : {
954 0 : delete[] outbuf;
955 0 : outbuf = nullptr;
956 0 : return false;
957 : }
958 0 : outlen = len;
959 0 : return true;
960 : }
961 :
962 : //used in iengine.h
963 0 : bool packeditinfo(const editinfo *e, int &inlen, uchar *&outbuf, int &outlen)
964 : {
965 0 : std::vector<uchar> buf;
966 0 : if(!e || !e->copy || !packblock(*e->copy, buf))
967 : {
968 0 : return false;
969 : }
970 0 : packvslots(*e->copy, buf);
971 0 : inlen = buf.size();
972 0 : return compresseditinfo(buf.data(), buf.size(), outbuf, outlen);
973 0 : }
974 :
975 : //used in iengine.h
976 0 : bool unpackeditinfo(editinfo *&e, const uchar *inbuf, int inlen, int outlen)
977 : {
978 0 : if(e && e->copy)
979 : {
980 0 : freeblock(e->copy);
981 0 : e->copy = nullptr;
982 : }
983 0 : uchar *outbuf = nullptr;
984 0 : if(!uncompresseditinfo(inbuf, inlen, outbuf, outlen))
985 : {
986 0 : return false;
987 : }
988 0 : ucharbuf buf(outbuf, outlen);
989 0 : if(!e)
990 : {
991 0 : editinfo *e = nullptr;
992 0 : editinfos.push_back(e);
993 : }
994 0 : if(!unpackblock(e->copy, buf))
995 : {
996 0 : delete[] outbuf;
997 0 : return false;
998 : }
999 0 : unpackvslots(*e->copy, buf);
1000 0 : delete[] outbuf;
1001 0 : return true;
1002 : }
1003 :
1004 : //used in iengine.h
1005 0 : void freeeditinfo(editinfo *&e)
1006 : {
1007 0 : if(!e)
1008 : {
1009 0 : return;
1010 : }
1011 0 : editinfos.erase(std::find(editinfos.begin(), editinfos.end(), e));
1012 0 : if(e->copy)
1013 : {
1014 0 : freeblock(e->copy);
1015 : }
1016 0 : delete e;
1017 0 : e = nullptr;
1018 : }
1019 :
1020 : //used in iengine.h
1021 0 : bool packundo(undoblock *u, int &inlen, uchar *&outbuf, int &outlen)
1022 : {
1023 0 : std::vector<uchar> buf;
1024 0 : buf.reserve(512);
1025 0 : for(uint i = 0; i < sizeof(ushort); ++i)
1026 : {
1027 0 : buf.emplace_back();
1028 : }
1029 0 : *reinterpret_cast<ushort *>(buf.data()) = static_cast<ushort>(u->numents);
1030 0 : if(u->numents)
1031 : {
1032 0 : const undoent *ue = u->ents();
1033 0 : for(int i = 0; i < u->numents; ++i)
1034 : {
1035 0 : for(uint i = 0; i < sizeof(ushort); ++i)
1036 : {
1037 0 : buf.emplace_back();
1038 : }
1039 0 : *reinterpret_cast<ushort *>(&(*buf.end()) - sizeof(ushort)) = static_cast<ushort>(ue[i].i);
1040 0 : for(uint i = 0; i < sizeof(entity); ++i)
1041 : {
1042 0 : buf.emplace_back();
1043 : }
1044 0 : entity &e = *reinterpret_cast<entity *>(&(*buf.end()) - sizeof(entity));
1045 0 : e = ue[i].e;
1046 : }
1047 : }
1048 : else
1049 : {
1050 0 : const block3 &b = *u->block();
1051 0 : if(!packblock(b, buf))
1052 : {
1053 0 : return false;
1054 : }
1055 0 : for(int i = 0; i < b.size(); ++i)
1056 : {
1057 0 : buf.push_back(u->gridmap()[i]);
1058 : }
1059 0 : packvslots(b, buf);
1060 : }
1061 0 : inlen = buf.size();
1062 0 : return compresseditinfo(buf.data(), buf.size(), outbuf, outlen);
1063 0 : }
1064 :
1065 : //used in iengine.h
1066 0 : bool packundo(bool undo, int &inlen, uchar *&outbuf, int &outlen)
1067 : {
1068 0 : if(undo)
1069 : {
1070 0 : return !undos.empty() && packundo(undos.back(), inlen, outbuf, outlen);
1071 : }
1072 : else
1073 : {
1074 0 : return !redos.empty() && packundo(redos.back(), inlen, outbuf, outlen);
1075 : }
1076 : }
1077 :
1078 : struct prefab final : editinfo
1079 : {
1080 : std::string name;
1081 : GLuint ebo, vbo;
1082 : int numtris, numverts;
1083 :
1084 0 : prefab() : name(""), ebo(0), vbo(0), numtris(0), numverts(0) {}
1085 0 : ~prefab()
1086 : {
1087 0 : if(copy)
1088 : {
1089 0 : freeblock(copy);
1090 : }
1091 0 : }
1092 :
1093 0 : void cleanup()
1094 : {
1095 0 : if(ebo)
1096 : {
1097 0 : glDeleteBuffers(1, &ebo);
1098 0 : ebo = 0;
1099 : }
1100 0 : if(vbo)
1101 : {
1102 0 : glDeleteBuffers(1, &vbo);
1103 0 : vbo = 0;
1104 : }
1105 0 : numtris = numverts = 0;
1106 0 : }
1107 : };
1108 :
1109 : static std::unordered_map<std::string, prefab> prefabs;
1110 :
1111 0 : void cleanupprefabs()
1112 : {
1113 0 : for(auto &[k, i] : prefabs)
1114 : {
1115 0 : i.cleanup();
1116 : }
1117 0 : }
1118 :
1119 0 : void pasteundoblock(block3 *b, const uchar *g)
1120 : {
1121 0 : const cube *s = b->c();
1122 0 : uint i = 0;
1123 0 : LOOP_XYZ(*b, 1<<std::min(static_cast<int>(*g++), rootworld.mapscale()-1), pastecube(s[i], c); i++; );
1124 0 : }
1125 :
1126 : /**
1127 : * @brief Unpacks an undocube into a uchar buffer
1128 : *
1129 : * used in client prefab unpacking, handles the octree unpacking (not the entities,
1130 : * which are game-dependent)
1131 : *
1132 : * @param buf the buffer to unpack
1133 : * @param outbuf output buffer of the buf intput
1134 : */
1135 0 : void unpackundocube(ucharbuf &buf, uchar *outbuf)
1136 : {
1137 0 : block3 *b = nullptr;
1138 0 : if(!unpackblock(b, buf) || b->grid >= rootworld.mapsize() || buf.remaining() < b->size())
1139 : {
1140 0 : freeblock(b);
1141 0 : delete[] outbuf;
1142 0 : return;
1143 : }
1144 0 : uchar *g = buf.pad(b->size());
1145 0 : unpackvslots(*b, buf);
1146 0 : pasteundoblock(b, g);
1147 0 : rootworld.changed(*b, false);
1148 0 : freeblock(b);
1149 : }
1150 :
1151 0 : void makeundo(selinfo &s)
1152 : {
1153 0 : undoblock *u = newundocube(s);
1154 0 : if(u)
1155 : {
1156 0 : addundo(u);
1157 : }
1158 0 : }
1159 :
1160 0 : void makeundo() // stores state of selected cubes before editing
1161 : {
1162 0 : if(lastsel==sel || !sel.s)
1163 : {
1164 0 : return;
1165 : }
1166 0 : lastsel=sel;
1167 0 : makeundo(sel);
1168 : }
1169 :
1170 0 : void pasteblock(const block3 &b, selinfo &sel, bool local)
1171 : {
1172 0 : sel.s = b.s;
1173 0 : int o = sel.orient;
1174 0 : sel.orient = b.orient;
1175 0 : const cube *s = b.getcube();
1176 0 : uint i = 0;
1177 0 : LOOP_SEL_XYZ(if(!(s[i].isempty()) || s[i].children || s[i].material != Mat_Air) pastecube(s[i], c); i++); // 'transparent'. old opaque by 'delcube; paste'
1178 0 : sel.orient = o;
1179 0 : }
1180 :
1181 : struct prefabheader final
1182 : {
1183 : std::array<char, 4> magic;
1184 : int version;
1185 : };
1186 :
1187 0 : prefab *loadprefab(const char *name, bool msg = true)
1188 : {
1189 0 : static std::unordered_map<std::string, prefab>::iterator itr = prefabs.find(name);
1190 0 : if(itr != prefabs.end())
1191 : {
1192 0 : return &(*itr).second;
1193 : }
1194 0 : DEF_FORMAT_STRING(filename, "media/prefab/%s.obr", name);
1195 0 : path(filename);
1196 0 : stream *f = opengzfile(filename, "rb");
1197 0 : if(!f)
1198 : {
1199 0 : if(msg)
1200 : {
1201 0 : conoutf(Console_Error, "could not read prefab %s", filename);
1202 : }
1203 0 : return nullptr;
1204 : }
1205 : prefabheader hdr;
1206 0 : if(f->read(&hdr, sizeof(hdr)) != sizeof(prefabheader) || std::memcmp(hdr.magic.data(), "OEBR", 4))
1207 : {
1208 0 : delete f;
1209 0 : if(msg)
1210 : {
1211 0 : conoutf(Console_Error, "prefab %s has malformatted header", filename);
1212 0 : return nullptr;
1213 : }
1214 : }
1215 0 : if(hdr.version != 0)
1216 : {
1217 0 : delete f;
1218 0 : if(msg)
1219 : {
1220 0 : conoutf(Console_Error, "prefab %s uses unsupported version", filename);
1221 0 : return nullptr;
1222 : }
1223 : }
1224 0 : streambuf<uchar> s(f);
1225 0 : block3 *copy = nullptr;
1226 0 : if(!unpackblock(copy, s))
1227 : {
1228 0 : delete f;
1229 0 : if(msg)
1230 : {
1231 0 : conoutf(Console_Error, "could not unpack prefab %s", filename);
1232 0 : return nullptr;
1233 : }
1234 : }
1235 0 : delete f;
1236 :
1237 0 : prefab *b = &(*prefabs.insert_or_assign(name, prefab()).first).second;
1238 0 : b->name = name ? name : "";
1239 0 : b->copy = copy;
1240 :
1241 0 : return b;
1242 : }
1243 :
1244 : class prefabmesh final
1245 : {
1246 : public:
1247 : struct vertex final
1248 : {
1249 : vec pos;
1250 : vec4<uchar> norm;
1251 : };
1252 :
1253 : std::vector<vertex> verts;
1254 : std::vector<int> chain;
1255 : std::vector<ushort> tris;
1256 :
1257 0 : prefabmesh()
1258 0 : {
1259 0 : table.fill(-1);
1260 0 : }
1261 :
1262 0 : int addvert(const vec &pos, const bvec &norm)
1263 : {
1264 0 : vertex vtx;
1265 0 : vtx.pos = pos;
1266 0 : vtx.norm = norm;
1267 0 : return addvert(vtx);
1268 : }
1269 :
1270 0 : void setup(prefab &p)
1271 : {
1272 0 : if(tris.empty())
1273 : {
1274 0 : return;
1275 : }
1276 0 : p.cleanup();
1277 :
1278 0 : for(size_t i = 0; i < verts.size(); i++)
1279 : {
1280 0 : verts[i].norm.flip();
1281 : }
1282 0 : if(!p.vbo)
1283 : {
1284 0 : glGenBuffers(1, &p.vbo);
1285 : }
1286 0 : gle::bindvbo(p.vbo);
1287 0 : glBufferData(GL_ARRAY_BUFFER, verts.size()*sizeof(vertex), verts.data(), GL_STATIC_DRAW);
1288 0 : gle::clearvbo();
1289 0 : p.numverts = verts.size();
1290 :
1291 0 : if(!p.ebo)
1292 : {
1293 0 : glGenBuffers(1, &p.ebo);
1294 : }
1295 0 : gle::bindebo(p.ebo);
1296 0 : glBufferData(GL_ELEMENT_ARRAY_BUFFER, tris.size()*sizeof(ushort), tris.data(), GL_STATIC_DRAW);
1297 0 : gle::clearebo();
1298 0 : p.numtris = tris.size()/3;
1299 : }
1300 : private:
1301 : static constexpr int prefabmeshsize = 1<<9;
1302 : std::array<int, prefabmeshsize> table;
1303 0 : int addvert(const vertex &v)
1304 : {
1305 : auto vechash = std::hash<vec>();
1306 0 : uint h = vechash(v.pos)&(prefabmeshsize-1);
1307 0 : for(int i = table[h]; i>=0; i = chain[i])
1308 : {
1309 0 : const vertex &c = verts[i];
1310 0 : if(c.pos==v.pos && c.norm==v.norm)
1311 : {
1312 0 : return i;
1313 : }
1314 : }
1315 0 : if(verts.size() >= USHRT_MAX)
1316 : {
1317 0 : return -1;
1318 : }
1319 0 : verts.emplace_back(v);
1320 0 : chain.emplace_back(table[h]);
1321 0 : return table[h] = verts.size()-1;
1322 : }
1323 :
1324 : };
1325 :
1326 0 : static void genprefabmesh(prefabmesh &r, const cube &c, const ivec &co, int size)
1327 : {
1328 : //recursively apply to children
1329 0 : if(c.children)
1330 : {
1331 0 : neighborstack[++neighbordepth] = &(*c.children)[0];
1332 0 : for(int i = 0; i < 8; ++i)
1333 : {
1334 0 : ivec o(i, co, size/2);
1335 0 : genprefabmesh(r, (*c.children)[i], o, size/2);
1336 : }
1337 0 : --neighbordepth;
1338 : }
1339 0 : else if(!(c.isempty()))
1340 : {
1341 : int vis;
1342 0 : for(int i = 0; i < 6; ++i) //for each face
1343 : {
1344 0 : if((vis = visibletris(c, i, co, size)))
1345 : {
1346 0 : std::array<ivec, 4> v;
1347 0 : genfaceverts(c, i, v);
1348 0 : int convex = 0;
1349 0 : if(!flataxisface(c, i))
1350 : {
1351 0 : convex = faceconvexity(v);
1352 : }
1353 0 : int order = vis&4 || convex < 0 ? 1 : 0, numverts = 0;
1354 0 : vec vo(co);
1355 0 : std::array<vec, 4> pos, norm;
1356 0 : pos[numverts++] = vec(v[order]).mul(size/8.0f).add(vo);
1357 0 : if(vis&1)
1358 : {
1359 0 : pos[numverts++] = vec(v[order+1]).mul(size/8.0f).add(vo);
1360 : }
1361 0 : pos[numverts++] = vec(v[order+2]).mul(size/8.0f).add(vo);
1362 0 : if(vis&2)
1363 : {
1364 0 : pos[numverts++] = vec(v[(order+3)&3]).mul(size/8.0f).add(vo);
1365 : }
1366 0 : guessnormals(pos.data(), numverts, norm.data());
1367 : std::array<int, 4> index;
1368 0 : for(int j = 0; j < numverts; ++j)
1369 : {
1370 0 : index[j] = r.addvert(pos[j], bvec(norm[j]));
1371 : }
1372 0 : for(int j = 0; j < numverts-2; ++j)
1373 : {
1374 0 : if(index[0]!=index[j+1] && index[j+1]!=index[j+2] && index[j+2]!=index[0])
1375 : {
1376 0 : r.tris.emplace_back(index[0]);
1377 0 : r.tris.emplace_back(index[j+1]);
1378 0 : r.tris.emplace_back(index[j+2]);
1379 : }
1380 : }
1381 : }
1382 : }
1383 : }
1384 0 : }
1385 :
1386 0 : void cubeworld::genprefabmesh(prefab &p)
1387 : {
1388 0 : block3 b = *p.copy;
1389 0 : b.o = ivec(0, 0, 0);
1390 :
1391 0 : std::array<cube, 8> *oldworldroot = worldroot;
1392 0 : int oldworldscale = worldscale;
1393 :
1394 0 : worldroot = newcubes();
1395 0 : worldscale = 1;
1396 0 : while(mapscale() < std::max(std::max(b.s.x, b.s.y), b.s.z)*b.grid)
1397 : {
1398 0 : worldscale++;
1399 : }
1400 :
1401 0 : cube *s = p.copy->c();
1402 0 : uint i = 0;
1403 0 : LOOP_XYZ(b, b.grid, if(!(s[i].isempty()) || s[i].children) pastecube(s[i], c); i++);
1404 :
1405 0 : prefabmesh r;
1406 0 : neighborstack[++neighbordepth] = &(*worldroot)[0];
1407 : //recursively apply to children
1408 0 : for(int i = 0; i < 8; ++i)
1409 : {
1410 0 : ::genprefabmesh(r, (*worldroot)[i], ivec(i, ivec(0, 0, 0), mapsize()/2), mapsize()/2);
1411 : }
1412 0 : --neighbordepth;
1413 0 : r.setup(p);
1414 :
1415 0 : freeocta(worldroot);
1416 :
1417 0 : worldroot = oldworldroot;
1418 0 : worldscale = oldworldscale;
1419 :
1420 0 : useshaderbyname("prefab");
1421 0 : }
1422 :
1423 0 : static void renderprefab(prefab &p, const vec &o, float yaw, float pitch, float roll, float size, const vec &color)
1424 : {
1425 0 : if(!p.numtris)
1426 : {
1427 0 : rootworld.genprefabmesh(p);
1428 0 : if(!p.numtris)
1429 : {
1430 0 : return;
1431 : }
1432 : }
1433 :
1434 0 : block3 &b = *p.copy;
1435 :
1436 0 : matrix4 m;
1437 0 : m.identity();
1438 0 : m.settranslation(o);
1439 0 : if(yaw)
1440 : {
1441 0 : m.rotate_around_z(yaw/RAD);
1442 : }
1443 0 : if(pitch)
1444 : {
1445 0 : m.rotate_around_x(pitch/RAD);
1446 : }
1447 0 : if(roll)
1448 : {
1449 0 : m.rotate_around_y(-roll/RAD);
1450 : }
1451 0 : matrix3 w(m);
1452 0 : if(size > 0 && size != 1)
1453 : {
1454 0 : m.scale(size);
1455 : }
1456 0 : m.translate(vec(b.s).mul(-b.grid*0.5f));
1457 :
1458 0 : gle::bindvbo(p.vbo);
1459 0 : gle::bindebo(p.ebo);
1460 0 : gle::enablevertex();
1461 0 : gle::enablenormal();
1462 0 : prefabmesh::vertex *v = nullptr;
1463 0 : gle::vertexpointer(sizeof(prefabmesh::vertex), v->pos.data());
1464 0 : gle::normalpointer(sizeof(prefabmesh::vertex), v->norm.data(), GL_BYTE);
1465 :
1466 0 : matrix4 pm;
1467 0 : pm.mul(camprojmatrix, m);
1468 0 : GLOBALPARAM(prefabmatrix, pm);
1469 0 : GLOBALPARAM(prefabworld, w);
1470 0 : SETSHADER(prefab);
1471 0 : gle::color(vec(color).mul(ldrscale));
1472 0 : glDrawRangeElements(GL_TRIANGLES, 0, p.numverts-1, p.numtris*3, GL_UNSIGNED_SHORT, nullptr);
1473 :
1474 0 : glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
1475 0 : enablepolygonoffset(GL_POLYGON_OFFSET_LINE);
1476 :
1477 0 : pm.mul(camprojmatrix, m);
1478 0 : GLOBALPARAM(prefabmatrix, pm);
1479 0 : SETSHADER(prefab);
1480 0 : gle::color((outlinecolor).tocolor().mul(ldrscale));
1481 0 : glDrawRangeElements(GL_TRIANGLES, 0, p.numverts-1, p.numtris*3, GL_UNSIGNED_SHORT, nullptr);
1482 :
1483 0 : disablepolygonoffset(GL_POLYGON_OFFSET_LINE);
1484 0 : glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
1485 :
1486 0 : gle::disablevertex();
1487 0 : gle::disablenormal();
1488 0 : gle::clearebo();
1489 0 : gle::clearvbo();
1490 : }
1491 :
1492 0 : void renderprefab(const char *name, const vec &o, float yaw, float pitch, float roll, float size, const vec &color)
1493 : {
1494 0 : prefab *p = loadprefab(name, false);
1495 0 : if(p)
1496 : {
1497 0 : renderprefab(*p, o, yaw, pitch, roll, size, color);
1498 : }
1499 0 : }
1500 :
1501 0 : void previewprefab(const char *name, const vec &color)
1502 : {
1503 0 : prefab *p = loadprefab(name, false);
1504 0 : if(p)
1505 : {
1506 0 : block3 &b = *p->copy;
1507 : float yaw;
1508 0 : vec o = calcmodelpreviewpos(vec(b.s).mul(b.grid*0.5f), yaw);
1509 0 : renderprefab(*p, o, yaw, 0, 0, 1, color);
1510 : }
1511 0 : }
1512 :
1513 : std::vector<int *> editingvslots;
1514 :
1515 0 : void compacteditvslots()
1516 : {
1517 0 : for(size_t i = 0; i < editingvslots.size(); i++)
1518 : {
1519 0 : if(*editingvslots[i])
1520 : {
1521 0 : compactvslot(*editingvslots[i]);
1522 : }
1523 : }
1524 0 : for(size_t i = 0; i < unpackingvslots.size(); i++)
1525 : {
1526 0 : compactvslot(*unpackingvslots[i].vslot);
1527 : }
1528 0 : for(size_t i = 0; i < editinfos.size(); i++)
1529 : {
1530 0 : const editinfo *e = editinfos[i];
1531 0 : compactvslots(e->copy->c(), e->copy->size());
1532 : }
1533 0 : for(undoblock *u : undos)
1534 : {
1535 0 : if(!u->numents)
1536 : {
1537 0 : compactvslots(u->block()->c(), u->block()->size());
1538 : }
1539 : }
1540 0 : for(undoblock *u : redos)
1541 : {
1542 0 : if(!u->numents)
1543 : {
1544 0 : compactvslots(u->block()->c(), u->block()->size());
1545 : }
1546 : }
1547 0 : }
1548 :
1549 : ///////////// height maps ////////////////
1550 :
1551 0 : ushort getmaterial(const cube &c)
1552 : {
1553 0 : if(c.children)
1554 : {
1555 0 : ushort mat = getmaterial((*c.children)[7]);
1556 0 : for(int i = 0; i < 7; ++i)
1557 : {
1558 0 : if(mat != getmaterial((*c.children)[i]))
1559 : {
1560 0 : return Mat_Air;
1561 : }
1562 : }
1563 0 : return mat;
1564 : }
1565 0 : return c.material;
1566 : }
1567 :
1568 : /////////// texture editing //////////////////
1569 :
1570 : int curtexindex = -1;
1571 : std::vector<ushort> texmru;
1572 :
1573 : int reptex = -1;
1574 :
1575 0 : static VSlot *remapvslot(int index, bool delta, const VSlot &ds)
1576 : {
1577 0 : for(vslotmap &v : remappedvslots)
1578 : {
1579 0 : if(v.index == index)
1580 : {
1581 0 : return v.vslot;
1582 : }
1583 : }
1584 0 : VSlot &vs = lookupvslot(index, false);
1585 0 : if(vs.index < 0 || vs.index == Default_Sky)
1586 : {
1587 0 : return nullptr;
1588 : }
1589 0 : VSlot *edit = nullptr;
1590 0 : if(delta)
1591 : {
1592 0 : VSlot ms;
1593 0 : mergevslot(ms, vs, ds);
1594 0 : edit = ms.changed ? editvslot(vs, ms) : vs.slot->variants;
1595 0 : }
1596 : else
1597 : {
1598 0 : edit = ds.changed ? editvslot(vs, ds) : vs.slot->variants;
1599 : }
1600 0 : if(!edit)
1601 : {
1602 0 : edit = &vs;
1603 : }
1604 0 : remappedvslots.emplace_back(vslotmap(vs.index, edit));
1605 0 : return edit;
1606 : }
1607 :
1608 0 : void remapvslots(cube &c, bool delta, const VSlot &ds, int orient, bool &findrep, VSlot *&findedit)
1609 : {
1610 : //recursively apply to children
1611 0 : if(c.children)
1612 : {
1613 0 : for(int i = 0; i < 8; ++i)
1614 : {
1615 0 : remapvslots((*c.children)[i], delta, ds, orient, findrep, findedit);
1616 : }
1617 0 : return;
1618 : }
1619 0 : static VSlot ms;
1620 0 : if(orient<0)
1621 : {
1622 0 : for(int i = 0; i < 6; ++i) //for each face
1623 : {
1624 0 : VSlot *edit = remapvslot(c.texture[i], delta, ds);
1625 0 : if(edit)
1626 : {
1627 0 : c.texture[i] = edit->index;
1628 0 : if(!findedit)
1629 : {
1630 0 : findedit = edit;
1631 : }
1632 : }
1633 : }
1634 : }
1635 : else
1636 : {
1637 0 : int i = visibleorient(c, orient);
1638 0 : VSlot *edit = remapvslot(c.texture[i], delta, ds);
1639 0 : if(edit)
1640 : {
1641 0 : if(findrep)
1642 : {
1643 0 : if(reptex < 0)
1644 : {
1645 0 : reptex = c.texture[i];
1646 : }
1647 0 : else if(reptex != c.texture[i])
1648 : {
1649 0 : findrep = false;
1650 : }
1651 : }
1652 0 : c.texture[i] = edit->index;
1653 0 : if(!findedit)
1654 : {
1655 0 : findedit = edit;
1656 : }
1657 : }
1658 : }
1659 : }
1660 :
1661 0 : void compactmruvslots()
1662 : {
1663 : static int lasttex = 0;
1664 0 : remappedvslots.clear();
1665 0 : for(int i = static_cast<int>(texmru.size()); --i >=0;) //note reverse iteration
1666 : {
1667 0 : if(vslots.size() > texmru[i])
1668 : {
1669 0 : VSlot &vs = *vslots[texmru[i]];
1670 0 : if(vs.index >= 0)
1671 : {
1672 0 : texmru[i] = vs.index;
1673 0 : continue;
1674 : }
1675 : }
1676 0 : if(curtexindex > i)
1677 : {
1678 0 : curtexindex--;
1679 : }
1680 0 : else if(curtexindex == i)
1681 : {
1682 0 : curtexindex = -1;
1683 : }
1684 0 : texmru.erase(texmru.begin() + i);
1685 : }
1686 0 : if(vslots.size() > static_cast<size_t>(lasttex))
1687 : {
1688 0 : VSlot &vs = *vslots[lasttex];
1689 0 : lasttex = vs.index >= 0 ? vs.index : 0;
1690 : }
1691 : else
1692 : {
1693 0 : lasttex = 0;
1694 : }
1695 0 : reptex = (vslots.size() > static_cast<size_t>(reptex)) ? vslots[reptex]->index : -1;
1696 0 : }
1697 :
1698 0 : void edittexcube(cube &c, int tex, int orient, bool &findrep)
1699 : {
1700 0 : if(orient<0)
1701 : {
1702 0 : for(int i = 0; i < 6; ++i) //for each face
1703 : {
1704 0 : c.texture[i] = tex;
1705 : }
1706 : }
1707 : else
1708 : {
1709 0 : int i = visibleorient(c, orient);
1710 0 : if(findrep)
1711 : {
1712 0 : if(reptex < 0)
1713 : {
1714 0 : reptex = c.texture[i];
1715 : }
1716 0 : else if(reptex != c.texture[i])
1717 : {
1718 0 : findrep = false;
1719 : }
1720 : }
1721 0 : c.texture[i] = tex;
1722 : }
1723 : //recursively apply to children
1724 0 : if(c.children)
1725 : {
1726 0 : for(int i = 0; i < 8; ++i)
1727 : {
1728 0 : edittexcube((*c.children)[i], tex, orient, findrep);
1729 : }
1730 : }
1731 0 : }
1732 :
1733 : /**
1734 : * @brief Sets a cube's materials, given a material & filter to use
1735 : *
1736 : * @param c the cube object to use
1737 : * @param mat material index to apply
1738 : * @param matmask material mask
1739 : * @param filtermat if nonzero, determines what existing mats to apply to
1740 : * @param filtermask filter material mask
1741 : * @param filtergeom type of geometry inside the cube (empty, solid, partially solid)
1742 : */
1743 0 : void cube::setmat(ushort mat, ushort matmask, ushort filtermat, ushort filtermask, int filtergeom)
1744 : {
1745 : //recursively sets material for all child nodes
1746 0 : if(children)
1747 : {
1748 0 : for(int i = 0; i < 8; ++i)
1749 : {
1750 0 : (*children)[i].setmat( mat, matmask, filtermat, filtermask, filtergeom);
1751 : }
1752 : }
1753 0 : else if((material&filtermask) == filtermat)
1754 : {
1755 0 : switch(filtergeom)
1756 : {
1757 0 : case EditMatFlag_Empty:
1758 : {
1759 0 : if(isempty())
1760 : {
1761 0 : break;
1762 : }
1763 0 : return;
1764 : }
1765 0 : case EditMatFlag_NotEmpty:
1766 : {
1767 0 : if(!(isempty()))
1768 : {
1769 0 : break;
1770 : }
1771 0 : return;
1772 : }
1773 0 : case EditMatFlag_Solid:
1774 : {
1775 0 : if(issolid())
1776 : {
1777 0 : break;
1778 : }
1779 0 : return;
1780 : }
1781 0 : case EditMatFlag_NotSolid:
1782 : {
1783 0 : if(!(issolid()))
1784 : {
1785 0 : break;
1786 : }
1787 0 : return;
1788 : }
1789 : }
1790 0 : if(mat!=Mat_Air)
1791 : {
1792 0 : material &= matmask;
1793 0 : material |= mat;
1794 : }
1795 : else
1796 : {
1797 0 : material = Mat_Air;
1798 : }
1799 : }
1800 : }
1801 :
1802 0 : void rendertexturepanel(int w, int h)
1803 : {
1804 : static int texpaneltimer = 0;
1805 0 : if((texpaneltimer -= curtime)>0 && editmode)
1806 : {
1807 0 : glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1808 :
1809 0 : pushhudmatrix();
1810 0 : hudmatrix.scale(h/1800.0f, h/1800.0f, 1);
1811 0 : flushhudmatrix(false);
1812 0 : SETSHADER(hudrgb);
1813 0 : int y = 50,
1814 0 : gap = 10;
1815 0 : gle::defvertex(2);
1816 0 : gle::deftexcoord0();
1817 :
1818 0 : for(int i = 0; i < 7; ++i)
1819 : {
1820 0 : int s = (i == 3 ? 285 : 220),
1821 0 : ti = curtexindex+i-3;
1822 0 : if(static_cast<int>(texmru.size()) > ti)
1823 : {
1824 0 : VSlot &vslot = lookupvslot(texmru[ti]);
1825 0 : Slot &slot = *vslot.slot;
1826 0 : Texture *tex = slot.sts.empty() ? notexture : slot.sts[0].t,
1827 0 : *glowtex = nullptr;
1828 0 : if(slot.texmask&(1 << Tex_Glow))
1829 : {
1830 0 : for(const Slot::Tex &t : slot.sts)
1831 : {
1832 0 : if(t.type == Tex_Glow)
1833 : {
1834 0 : glowtex = t.t;
1835 0 : break;
1836 : }
1837 : }
1838 : }
1839 0 : float sx = std::min(1.0f, tex->xs/static_cast<float>(tex->ys)),
1840 0 : sy = std::min(1.0f, tex->ys/static_cast<float>(tex->xs));
1841 0 : int x = w*1800/h-s-50,
1842 0 : r = s;
1843 0 : std::array<vec2, 4> tc = { vec2(0, 0), vec2(1, 0), vec2(1, 1), vec2(0, 1) };
1844 0 : float xoff = vslot.offset.x(),
1845 0 : yoff = vslot.offset.y();
1846 0 : if(vslot.rotation)
1847 : {
1848 0 : const TexRotation &r = texrotations[vslot.rotation];
1849 0 : if(r.swapxy)
1850 : {
1851 0 : std::swap(xoff, yoff);
1852 0 : for(int k = 0; k < 4; ++k)
1853 : {
1854 0 : std::swap(tc[k].x, tc[k].y);
1855 : }
1856 : }
1857 0 : if(r.flipx)
1858 : {
1859 0 : xoff *= -1;
1860 0 : for(int k = 0; k < 4; ++k)
1861 : {
1862 0 : tc[k].x *= -1;
1863 : }
1864 : }
1865 0 : if(r.flipy)
1866 : {
1867 0 : yoff *= -1;
1868 0 : for(int k = 0; k < 4; ++k)
1869 : {
1870 0 : tc[k].y *= -1;
1871 : }
1872 : }
1873 : }
1874 0 : for(int k = 0; k < 4; ++k)
1875 : {
1876 0 : tc[k].x = tc[k].x/sx - xoff/tex->xs;
1877 0 : tc[k].y = tc[k].y/sy - yoff/tex->ys;
1878 : }
1879 0 : glBindTexture(GL_TEXTURE_2D, tex->id);
1880 0 : for(int j = 0; j < (glowtex ? 3 : 2); ++j)
1881 : {
1882 0 : if(j < 2)
1883 : {
1884 0 : gle::color(vec(vslot.colorscale).mul(j), texpaneltimer/1000.0f);
1885 : }
1886 : else
1887 : {
1888 0 : glBindTexture(GL_TEXTURE_2D, glowtex->id);
1889 0 : glBlendFunc(GL_SRC_ALPHA, GL_ONE);
1890 0 : gle::color(vslot.glowcolor, texpaneltimer/1000.0f);
1891 : }
1892 0 : gle::begin(GL_TRIANGLE_STRIP);
1893 0 : gle::attribf(x, y); gle::attrib(tc[0]);
1894 0 : gle::attribf(x+r, y); gle::attrib(tc[1]);
1895 0 : gle::attribf(x, y+r); gle::attrib(tc[3]);
1896 0 : gle::attribf(x+r, y+r); gle::attrib(tc[2]);
1897 0 : xtraverts += gle::end();
1898 0 : if(!j)
1899 : {
1900 0 : r -= 10;
1901 0 : x += 5;
1902 0 : y += 5;
1903 : }
1904 0 : else if(j == 2)
1905 : {
1906 0 : glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1907 : }
1908 : }
1909 : }
1910 0 : y += s+gap;
1911 : }
1912 :
1913 0 : pophudmatrix(true, false);
1914 0 : resethudshader();
1915 : }
1916 0 : }
1917 :
1918 0 : static int bounded(int n)
1919 : {
1920 0 : return n<0 ? 0 : (n>8 ? 8 : n);
1921 : }
1922 :
1923 0 : static void pushedge(uchar &edge, int dir, int dc)
1924 : {
1925 0 : int ne = bounded(EDGE_GET(edge, dc)+dir);
1926 0 : EDGE_SET(edge, dc, ne);
1927 0 : int oe = EDGE_GET(edge, 1-dc);
1928 0 : if((dir<0 && dc && oe>ne) || (dir>0 && dc==0 && oe<ne))
1929 : {
1930 0 : EDGE_SET(edge, 1-dc, ne);
1931 : }
1932 0 : }
1933 :
1934 : //used in iengine
1935 0 : void linkedpush(cube &c, int d, int x, int y, int dc, int dir)
1936 : {
1937 0 : ivec v, p;
1938 0 : getcubevector(c, d, x, y, dc, v);
1939 :
1940 0 : for(int i = 0; i < 2; ++i)
1941 : {
1942 0 : for(int j = 0; j < 2; ++j)
1943 : {
1944 0 : getcubevector(c, d, i, j, dc, p);
1945 0 : if(v==p)
1946 : {
1947 0 : pushedge(CUBE_EDGE(c, d, i, j), dir, dc);
1948 : }
1949 : }
1950 : }
1951 0 : }
1952 :
1953 1 : void initoctaeditcmds()
1954 : {
1955 : //some of these commands use code only needed for the command itself, so
1956 : //they are declared as lambdas inside the local scope
1957 :
1958 : //others use functions in the global namespace, which are implemented elsewhere
1959 : //in this file
1960 :
1961 : //static to make sure that these lambdas have constant location in memory for identmap to look up
1962 1 : static auto movingcmd = [] (const int *n) -> void
1963 : {
1964 1 : if(*n >= 0)
1965 : {
1966 0 : if(!*n || (moving<=1 && !pointinsel(sel, vec(cur).add(1))))
1967 : {
1968 0 : moving = 0;
1969 : }
1970 0 : else if(!moving)
1971 : {
1972 0 : moving = 1;
1973 : }
1974 : }
1975 1 : intret(moving);
1976 1 : };
1977 : //unary + operator converts to function pointer
1978 1 : addcommand("moving", reinterpret_cast<identfun>(+movingcmd), "b", Id_Command);
1979 :
1980 1 : addcommand("entcancel", reinterpret_cast<identfun>(entcancel), "", Id_Command); ///
1981 1 : addcommand("cubecancel", reinterpret_cast<identfun>(cubecancel), "", Id_Command); ///
1982 1 : addcommand("cancelsel", reinterpret_cast<identfun>(cancelsel), "", Id_Command); ///
1983 1 : addcommand("reorient", reinterpret_cast<identfun>(reorient), "", Id_Command); ///
1984 :
1985 1 : static auto selextend = [] () -> void
1986 : {
1987 1 : if(noedit(true))
1988 : {
1989 1 : return;
1990 : }
1991 0 : for(int i = 0; i < 3; ++i)
1992 : {
1993 0 : if(cur[i]<sel.o[i])
1994 : {
1995 0 : sel.s[i] += (sel.o[i]-cur[i])/sel.grid;
1996 0 : sel.o[i] = cur[i];
1997 : }
1998 0 : else if(cur[i]>=sel.o[i]+sel.s[i]*sel.grid)
1999 : {
2000 0 : sel.s[i] = (cur[i]-sel.o[i])/sel.grid+1;
2001 : }
2002 : }
2003 : };
2004 1 : addcommand("selextend", reinterpret_cast<identfun>(+selextend), "", Id_Command);
2005 :
2006 1 : static auto selmoved = [] () -> void
2007 : {
2008 1 : if(noedit(true))
2009 : {
2010 1 : return;
2011 : }
2012 0 : intret(sel.o != savedsel.o ? 1 : 0);
2013 : };
2014 :
2015 1 : static auto selsave = [] () -> void
2016 : {
2017 1 : if(noedit(true))
2018 : {
2019 1 : return;
2020 : }
2021 0 : savedsel = sel;
2022 : };
2023 :
2024 1 : static auto selrestore = [] () -> void
2025 : {
2026 1 : if(noedit(true))
2027 : {
2028 1 : return;
2029 : }
2030 0 : sel = savedsel;
2031 : };
2032 :
2033 1 : static auto selswap = [] () -> void
2034 : {
2035 1 : if(noedit(true))
2036 : {
2037 1 : return;
2038 : }
2039 0 : std::swap(sel, savedsel);
2040 : };
2041 :
2042 1 : addcommand("selmoved", reinterpret_cast<identfun>(+selmoved), "", Id_Command);
2043 1 : addcommand("selsave", reinterpret_cast<identfun>(+selsave), "", Id_Command);
2044 1 : addcommand("selrestore", reinterpret_cast<identfun>(+selrestore), "", Id_Command);
2045 1 : addcommand("selswap", reinterpret_cast<identfun>(+selswap), "", Id_Command);
2046 :
2047 1 : static auto haveselcmd = [] () -> void
2048 : {
2049 1 : intret(havesel ? selchildcount : 0);
2050 1 : };
2051 :
2052 1 : addcommand("havesel", reinterpret_cast<identfun>(+haveselcmd), "", Id_Command);
2053 :
2054 :
2055 1 : static auto selchildcountcmd = [] () -> void
2056 : {
2057 1 : if(selchildcount < 0)
2058 : {
2059 0 : result(tempformatstring("1/%d", -selchildcount));
2060 : }
2061 : else
2062 : {
2063 1 : intret(selchildcount);
2064 : }
2065 1 : };
2066 1 : addcommand("selchildnum", reinterpret_cast<identfun>(+selchildcountcmd), "", Id_Command);
2067 :
2068 :
2069 1 : static auto selchildmatcmd = [] (const char *prefix) -> void
2070 : {
2071 1 : if(selchildmat > 0)
2072 : {
2073 0 : result(getmaterialdesc(selchildmat, prefix));
2074 : }
2075 1 : };
2076 1 : addcommand("selchildmat", reinterpret_cast<identfun>(+selchildmatcmd), "s", Id_Command);
2077 :
2078 1 : static auto clearundos = [] () -> void
2079 : {
2080 1 : pruneundos(0);
2081 1 : };
2082 1 : addcommand("clearundos", reinterpret_cast<identfun>(+clearundos), "", Id_Command); //run pruneundos but with a cache size of zero
2083 :
2084 1 : static auto delprefab = [] (const char *name) -> void
2085 : {
2086 3 : static std::unordered_map<std::string, prefab>::iterator itr = prefabs.find(name);
2087 1 : if(itr != prefabs.end())
2088 : {
2089 0 : (*itr).second.cleanup();
2090 0 : prefabs.erase(name);
2091 0 : conoutf("deleted prefab %s", name);
2092 : }
2093 : else
2094 : {
2095 1 : conoutf("no such prefab %s", name);
2096 : }
2097 1 : };
2098 1 : addcommand("delprefab", reinterpret_cast<identfun>(+delprefab), "s", Id_Command);
2099 :
2100 : /** @brief Saves the current selection to a prefab file.
2101 : *
2102 : * Using the global variables for selection information, writes the current selection
2103 : * to a prefab file with the given name. Does not save slot information, so pasting
2104 : * into a map with a different texture slot list will result in meaningless textures.
2105 : *
2106 : * @param name a string containing the name of the prefab to save (sans file type)
2107 : *
2108 : */
2109 1 : static auto saveprefab = [] (const char *name) -> void
2110 : {
2111 1 : if(!name[0] || noedit(true) || (nompedit && multiplayer))
2112 : {
2113 1 : multiplayerwarn();
2114 1 : return;
2115 : }
2116 0 : static std::unordered_map<std::string, prefab>::iterator itr = prefabs.find(name);
2117 0 : prefab *b = nullptr;
2118 0 : if(itr == prefabs.end())
2119 : {
2120 0 : b = &(*prefabs.insert( { std::string(name), prefab() } ).first).second;
2121 0 : b->name = newstring(name);
2122 0 : b->name = name ? name : "";
2123 : }
2124 : else
2125 : {
2126 0 : b = &(*itr).second;
2127 : }
2128 0 : if(b->copy)
2129 : {
2130 0 : freeblock(b->copy);
2131 : }
2132 0 : PROTECT_SEL(b->copy = blockcopy(block3(sel), sel.grid));
2133 0 : rootworld.changed(sel);
2134 0 : DEF_FORMAT_STRING(filename, "media/prefab/%s.obr", name);
2135 0 : path(filename);
2136 0 : stream *f = opengzfile(filename, "wb");
2137 0 : if(!f)
2138 : {
2139 0 : conoutf(Console_Error, "could not write prefab to %s", filename);
2140 0 : return;
2141 : }
2142 : prefabheader hdr;
2143 0 : std::string headermagic = "OEBR";
2144 0 : std::copy(headermagic.begin(), headermagic.end(), hdr.magic.begin());
2145 0 : hdr.version = 0;
2146 0 : f->write(&hdr, sizeof(hdr));
2147 0 : streambuf<uchar> s(f);
2148 0 : if(!packblock(*b->copy, s))
2149 : {
2150 0 : delete f;
2151 0 : conoutf(Console_Error, "could not pack prefab %s", filename);
2152 0 : return;
2153 : }
2154 0 : delete f;
2155 0 : conoutf("wrote prefab file %s", filename);
2156 0 : };
2157 1 : addcommand("saveprefab", reinterpret_cast<identfun>(+saveprefab), "s", Id_Command);
2158 :
2159 1 : static auto pasteprefab = [] (const char *name) -> void
2160 : {
2161 1 : if(!name[0] || noedit() || (nompedit && multiplayer))
2162 : {
2163 1 : multiplayerwarn();
2164 1 : return;
2165 : }
2166 0 : prefab *b = loadprefab(name, true);
2167 0 : if(b)
2168 : {
2169 0 : pasteblock(*b->copy, sel, true);
2170 : }
2171 : };
2172 1 : addcommand("pasteprefab", reinterpret_cast<identfun>(+pasteprefab), "s", Id_Command);
2173 :
2174 : //defines editing readonly variables, useful for the HUD
2175 : #define EDITSTAT(name, val) \
2176 : static auto name = [] () -> void\
2177 : { \
2178 : static int laststat = 0; \
2179 : static int prevstat = 0; \
2180 : static int curstat = 0; \
2181 : if(totalmillis - laststat >= statrate) \
2182 : { \
2183 : prevstat = curstat; \
2184 : laststat = totalmillis - (totalmillis%statrate); \
2185 : } \
2186 : if(prevstat == curstat) curstat = (val); \
2187 : intret(curstat); \
2188 : }; \
2189 : addcommand(#name, reinterpret_cast<identfun>(+name), "", Id_Command);
2190 :
2191 2 : EDITSTAT(wtr, wtris);
2192 2 : EDITSTAT(vtr, (vtris*100)/std::max(wtris, 1));
2193 2 : EDITSTAT(wvt, wverts);
2194 2 : EDITSTAT(vvt, (vverts*100)/std::max(wverts, 1));
2195 2 : EDITSTAT(evt, xtraverts);
2196 2 : EDITSTAT(eva, xtravertsva);
2197 2 : EDITSTAT(octa, allocnodes*8);
2198 2 : EDITSTAT(va, allocva);
2199 2 : EDITSTAT(gldes, glde);
2200 2 : EDITSTAT(geombatch, gbatches);
2201 2 : EDITSTAT(oq, occlusionengine.getnumqueries());
2202 :
2203 : #undef EDITSTAT
2204 1 : }
|