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