LCOV - code coverage report
Current view: top level - engine/world - octaedit.cpp (source / functions) Coverage Total Hit
Test: Libprimis Test Coverage Lines: 9.6 % 1116 107
Test Date: 2025-06-27 04:34:40 Functions: 27.8 % 115 32

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

Generated by: LCOV version 2.0-1