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-08-24 05:32:49 Functions: 27.8 % 115 32

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

Generated by: LCOV version 2.0-1