diff --git a/config/resetbinds.cfg b/config/resetbinds.cfg index b2432716..8d86bdeb 100644 --- a/config/resetbinds.cfg +++ b/config/resetbinds.cfg @@ -158,6 +158,8 @@ bind RSHIFT "" bind LSHIFT "crouch" bind RCTRL "jump" bind LCTRL "jump" +editbind RCTRL " " +editbind LCTRL " " bind RALT "" bind LALT "showscores 0; toggleshowmap" bind RMETA "" diff --git a/source/src/bot/bot_waypoint.cpp b/source/src/bot/bot_waypoint.cpp index 3260bc48..920ed6be 100644 --- a/source/src/bot/bot_waypoint.cpp +++ b/source/src/bot/bot_waypoint.cpp @@ -19,8 +19,9 @@ CCubeWaypointClass WaypointClass; #endif VAR(xhairwpsel, 0, 1, 1); -extern block sel; -#define curselection (xhairwpsel ? vec(sel.x, sel.y, S(sel.x, sel.y)->floor+2.0f) : vec(player1->o.x, player1->o.y, player1->o.z)) +// FIXME: multiple selections support ? +extern vector sels; +#define curselection (xhairwpsel ? vec(sels.last().x, sels.last().y, S(sels.last().x, sels.last().y)->floor+2.0f) : vec(player1->o.x, player1->o.y, player1->o.z)) // Waypoint class begin diff --git a/source/src/editing.cpp b/source/src/editing.cpp index 429be5d1..f17d5df3 100644 --- a/source/src/editing.cpp +++ b/source/src/editing.cpp @@ -4,21 +4,14 @@ bool editmode = false; -// the current selection, used by almost all editing commands +// the current selections, used by almost all editing commands // invariant: all code assumes that these are kept inside MINBORD distance of the edge of the map +// => selections are checked when they are made or when the world is reloaded -block sel = -{ - variable("selx", 0, 0, 4096, &sel.x, NULL, false), - variable("sely", 0, 0, 4096, &sel.y, NULL, false), - variable("selxs", 0, 0, 4096, &sel.xs, NULL, false), - variable("selys", 0, 0, 4096, &sel.ys, NULL, false), -}; +vector sels; -int selh = 0; -bool selset = false; - -#define loopselxy(b) { makeundo(); loop(x,sel.xs) loop(y,sel.ys) { sqr *s = S(sel.x+x, sel.y+y); b; } remip(sel); } +#define loopselxy(sel, b) { makeundo(sel); loop(x,(sel).xs) loop(y,(sel).ys) { sqr *s = S((sel).x+x, (sel).y+y); b; } remip(sel); } +#define loopselsxy(b) { loopv(sels) loopselxy(sels[i], b); } int cx, cy, ch; @@ -46,7 +39,6 @@ void toggleedit(bool force) player1->attacking = false; } keyrepeat(editmode); - selset = false; editing = editmode ? 1 : 0; player1->state = editing ? CS_EDITING : CS_ALIVE; if(editing && player1->onladder) player1->onladder = false; @@ -58,29 +50,17 @@ void edittoggle() { toggleedit(false); } COMMAND(edittoggle, ARG_NONE); -char *editinfo() +bool correctsel(block &s) // ensures above invariant { - static string info; - if(!editmode) return NULL; - int e = closestent(); - if(e<0) return NULL; - entity &c = ents[e]; - formatstring(info)("closest entity = %s (%d, %d, %d, %d), selection = (%d, %d)", entnames[c.type], c.attr1, c.attr2, c.attr3, c.attr4, sel.xs, sel.ys); - return info; -} - -void correctsel() // ensures above invariant -{ - selset = !OUTBORD(sel.x, sel.y); - int bsize = ssize-MINBORD; - if(sel.xs+sel.x>bsize) sel.xs = bsize-sel.x; - if(sel.ys+sel.y>bsize) sel.ys = bsize-sel.y; - if(sel.xs<=0 || sel.ys<=0) selset = false; + int bsize = ssize - MINBORD; + if(s.xs+s.x>bsize) s.xs = bsize-s.x; + if(s.ys+s.y>bsize) s.ys = bsize-s.y; + if(s.xs<=0 || s.ys<=0) return false; + else return !OUTBORD(s.x, s.y); } bool noteditmode(const char* func) { - correctsel(); if(!editmode) { if(func && func[0]!='\0') conoutf("\f4[\f3%s\f4]\f5 is only allowed in edit mode", func); @@ -89,33 +69,86 @@ bool noteditmode(const char* func) return !editmode; } +inline bool selset() +{ + return (sels.length() > 0); +} + bool noselection() { - if(!selset) conoutf("no selection"); - return !selset; + if(!selset()) conoutf("no selection"); + return !selset(); } +char *editinfo() +{ + static string info; + if(!editmode) return NULL; + int e = closestent(); + if(e<0) return NULL; + entity &c = ents[e]; + string selinfo = "no selection"; + if(selset()) formatstring(selinfo)("selection = (%d, %d)", (sels.last()).xs, (sels.last()).ys); + formatstring(info)("closest entity = %s (%d, %d, %d, %d), %s", entnames[c.type], c.attr1, c.attr2, c.attr3, c.attr4, selinfo); + return info; +} + + #define EDITSEL if(noteditmode("EDITSEL") || noselection()) return #define EDITSELMP if(noteditmode("EDITSELMP") || noselection() || multiplayer()) return #define EDITMP if(noteditmode("EDITMP") || multiplayer()) return -void selectpos(int x, int y, int xs, int ys) +// multiple sels + +// add a selection to the list +void addselection(int x, int y, int xs, int ys, int h) { - block s = { x, y, xs, ys }; - sel = s; - selh = 0; - correctsel(); + block &s = sels.add(); + s.x = x; s.y = y; s.xs = xs; s.ys = ys; s.h = h; + if(!correctsel(s)) { sels.drop(); } } -void makesel() +// reset all selections +void resetselections() { - block s = { min(lastx,cx), min(lasty,cy), abs(lastx-cx)+1, abs(lasty-cy)+1 }; - sel = s; - selh = max(lasth,ch); - correctsel(); - if(selset) rtex = *S(sel.x, sel.y); + sels.shrink(0); } +// reset all invalid selections +void checkselections() +{ + loopv(sels) if(!correctsel(sels[i])) sels.remove(i); +} + +// selection from cubescript +void selectpos(int x, int y, int xs, int ys) +{ + resetselections(); + addselection(x, y, xs, ys, 0); +} + +// update current selection, or add a new one +void makesel(bool isnew) +{ + if(isnew) addselection(min(lastx, cx), min(lasty, cy), abs(lastx-cx)+1, abs(lasty-cy)+1, max(lasth, ch)); + else + { + block &cursel = sels.last(); + cursel.x = min(lastx, cx); cursel.y = min(lasty, cy); + cursel.xs = abs(lastx-cx)+1; cursel.ys = abs(lasty-cy)+1; + cursel.h = max(lasth, ch); + correctsel(cursel); + } + if(selset()) rtex = *S((sels.last()).x, (sels.last()).y); +} + +#define SEL_ATTR(attr) { string buf; loopv(sels) { concatformatstring(buf, "%d ", sels[i].attr); } result(buf); } +COMMANDF(selx, ARG_NONE, (void) { SEL_ATTR(x); }); +COMMANDF(sely, ARG_NONE, (void) { SEL_ATTR(y); }); +COMMANDF(selxs, ARG_NONE, (void) { SEL_ATTR(xs); }); +COMMANDF(selys, ARG_NONE, (void) { SEL_ATTR(ys); }); +#undef SEL_ATTR + VAR(flrceil,0,0,2); VAR(editaxis,0,0,13); @@ -149,7 +182,7 @@ void cursorupdate() // called every frame fr if(OUTBORD(cx, cy)) return; } - if(dragging) makesel(); + if(dragging) { makesel(false); }; const int GRIDSIZE = 5; const float GRIDW = 0.5f; @@ -192,10 +225,10 @@ void cursorupdate() // called every frame fr ch = (int)ih; } - if(selset) + if(selset()) { linestyle(GRIDS, 0xFF, 0x40, 0x40); - box(sel, (float)selh, (float)selh, (float)selh, (float)selh); + loopv(sels) box(sels[i], (float)sels[i].h, (float)sels[i].h, (float)sels[i].h, (float)sels[i].h); } glLineWidth(1); @@ -214,7 +247,7 @@ void pruneundos(int maxremain) // bound memory } } -void makeundo() +void makeundo(block &sel) { undos.add(blockcopy(sel)); pruneundos(undomegs<<20); @@ -229,27 +262,51 @@ void editundo() freeblock(p); } -block *copybuf = NULL; +vector copybuffers; + +void resetcopybuffers() +{ + loopv(copybuffers) freeblock(copybuffers[i]); + copybuffers.shrink(0); +} void copy() { EDITSELMP; - freeblock(copybuf); - copybuf = blockcopy(sel); + resetcopybuffers(); + loopv(sels) + { + block *b = blockcopy(sels[i]); + copybuffers.add(b); + } + } void paste() { - EDITMP; - if(!copybuf) { conoutf("nothing to paste"); return; } - sel.xs = copybuf->xs; - sel.ys = copybuf->ys; - correctsel(); - if(!selset || sel.xs!=copybuf->xs || sel.ys!=copybuf->ys) { conoutf("incorrect selection"); return; } - makeundo(); - copybuf->x = sel.x; - copybuf->y = sel.y; - blockpaste(*copybuf); + EDITSELMP; + if(!copybuffers.length()) { conoutf("nothing to paste"); return; } + + loopv(sels) + { + block &sel = sels[i]; + int selx = sel.x; + int sely = sel.y; + + loopvj(copybuffers) + { + block *copyblock = copybuffers[j]; + int dx = copyblock->x - copybuffers[0]->x, dy = copyblock->y - copybuffers[0]->y; + + sel.xs = copyblock->xs; + sel.ys = copyblock->ys; + sel.x = selx + dx; + sel.y = sely + dy; + if(!correctsel(sel) || sel.xs!=copyblock->xs || sel.ys!=copyblock->ys) { conoutf("incorrect selection"); return; } + makeundo(sel); + blockpaste(*copyblock, sel.x, sel.y); + } + } } // Count the walls of type "type" contained in the current selection @@ -262,7 +319,7 @@ int countwalls(int type) conoutf("invalid type"); return 0; } - loopselxy(if(s->type==type) counter++) + loopselsxy(if(s->type==type) counter++) return counter; } @@ -289,10 +346,14 @@ void editdrag(bool isdown) lastx = cx; lasty = cy; lasth = ch; - selset = false; tofronttex(); + + extern vector keyms; + bool ctrlpressed = false; + loopv(keyms) if(keyms[i].pressed && (keyms[i].code == SDLK_RCTRL || keyms[i].code == SDLK_LCTRL)) ctrlpressed = true; + if(!ctrlpressed) resetselections(); } - makesel(); + makesel(isdown); } // the core editing function. all the *xy functions perform the core operations @@ -301,7 +362,7 @@ void editdrag(bool isdown) void editheightxy(bool isfloor, int amount, block &sel) { - loopselxy(if(isfloor) + loopselxy(sel, if(isfloor) { s->floor += amount; if(s->floor>=s->ceil) s->floor = s->ceil-1; @@ -317,8 +378,11 @@ void editheight(int flr, int amount) { EDITSEL; bool isfloor = flr==0; - editheightxy(isfloor, amount, sel); - addmsg(SV_EDITH, "ri6", sel.x, sel.y, sel.xs, sel.ys, isfloor, amount); + loopv(sels) + { + editheightxy(isfloor, amount, sels[i]); + addmsg(SV_EDITH, "ri6", sels[i].x, sels[i].y, sels[i].xs, sels[i].ys, isfloor, amount); + } } COMMAND(editheight, ARG_2INT); @@ -327,7 +391,7 @@ COMMAND(editheight, ARG_2INT); void edittexxy(int type, int t, block &sel) { - loopselxy(switch(type) + loopselxy(sel, switch(type) { case 0: s->ftex = t; break; case 1: s->wtex = t; break; @@ -346,8 +410,11 @@ void edittex(int type, int dir) i = i<0 ? 0 : i+dir; curedittex[atype] = i = min(max(i, 0), 255); int t = lasttex = hdr.texlists[atype][i]; - edittexxy(type, t, sel); - addmsg(SV_EDITT, "ri6", sel.x, sel.y, sel.xs, sel.ys, type, t); + loopv(sels) + { + edittexxy(type, t, sels[i]); + addmsg(SV_EDITT, "ri6", sels[i].x, sels[i].y, sels[i].xs, sels[i].ys, type, t); + } } void settex(int texture, int type) @@ -366,8 +433,11 @@ void settex(int texture, int type) conoutf("invalid/unavaible texture"); return; } - edittexxy(type, t, sel); - addmsg(SV_EDITT, "ri6", sel.x, sel.y, sel.xs, sel.ys, type, t); + loopv(sels) + { + edittexxy(type, t, sels[i]); + addmsg(SV_EDITT, "ri6", sels[i].x, sels[i].y, sels[i].xs, sels[i].ys, type, t); + } } void replace() @@ -390,17 +460,21 @@ void replace() void edittypexy(int type, block &sel) { - loopselxy(s->type = type); + loopselxy(sel, s->type = type); } void edittype(int type) { EDITSEL; - if(type==CORNER && (sel.xs!=sel.ys || sel.xs==3 || (sel.xs>4 && sel.xs!=8) - || sel.x&~-sel.xs || sel.y&~-sel.ys)) - { conoutf("corner selection must be power of 2 aligned"); return; } - edittypexy(type, sel); - addmsg(SV_EDITS, "ri5", sel.x, sel.y, sel.xs, sel.ys, type); + loopv(sels) + { + block &sel = sels[i]; + if(type==CORNER && (sel.xs!=sel.ys || sel.xs==3 || (sel.xs>4 && sel.xs!=8) + || sel.x&~-sel.xs || sel.y&~-sel.ys)) + { conoutf("corner selection must be power of 2 aligned"); return; } + edittypexy(type, sel); + addmsg(SV_EDITS, "ri5", sel.x, sel.y, sel.xs, sel.ys, type); + } } void heightfield(int t) { edittype(t==0 ? FHF : CHF); } @@ -414,12 +488,12 @@ COMMAND(corner, ARG_NONE); void editequalisexy(bool isfloor, block &sel) { int low = 127, hi = -128; - loopselxy( + loopselxy(sel, { if(s->floorfloor; if(s->ceil>hi) hi = s->ceil; }); - loopselxy( + loopselxy(sel, { if(isfloor) s->floor = low; else s->ceil = hi; if(s->floor>=s->ceil) s->floor = s->ceil-1; @@ -430,23 +504,30 @@ void equalize(int flr) { bool isfloor = flr==0; EDITSEL; - editequalisexy(isfloor, sel); - addmsg(SV_EDITE, "ri5", sel.x, sel.y, sel.xs, sel.ys, isfloor); + loopv(sels) + { + block &sel = sels[i]; + editequalisexy(isfloor, sel); + addmsg(SV_EDITE, "ri5", sel.x, sel.y, sel.xs, sel.ys, isfloor); + } } COMMAND(equalize, ARG_1INT); void setvdeltaxy(int delta, block &sel) { - loopselxy(s->vdelta = max(s->vdelta+delta, 0)); + loopselxy(sel, s->vdelta = max(s->vdelta+delta, 0)); remipmore(sel); } void setvdelta(int delta) { EDITSEL; - setvdeltaxy(delta, sel); - addmsg(SV_EDITD, "ri5", sel.x, sel.y, sel.xs, sel.ys, delta); + loopv(sels) + { + setvdeltaxy(delta, sels[i]); + addmsg(SV_EDITD, "ri5", sels[i].x, sels[i].y, sels[i].xs, sels[i].ys, delta); + } } const int MAXARCHVERT = 50; @@ -467,43 +548,55 @@ void archvertex(int span, int vert, int delta) void arch(int sidedelta, int _a) { EDITSELMP; - sel.xs++; - sel.ys++; - if(sel.xs>MAXARCHVERT) sel.xs = MAXARCHVERT; - if(sel.ys>MAXARCHVERT) sel.ys = MAXARCHVERT; - loopselxy(s->vdelta = - sel.xs>sel.ys - ? (archverts[sel.xs-1][x] + (y==0 || y==sel.ys-1 ? sidedelta : 0)) - : (archverts[sel.ys-1][y] + (x==0 || x==sel.xs-1 ? sidedelta : 0))); - remipmore(sel); + loopv(sels) + { + block &sel = sels[i]; + sel.xs++; + sel.ys++; + if(sel.xs>MAXARCHVERT) sel.xs = MAXARCHVERT; + if(sel.ys>MAXARCHVERT) sel.ys = MAXARCHVERT; + loopselxy(sel, s->vdelta = + sel.xs>sel.ys + ? (archverts[sel.xs-1][x] + (y==0 || y==sel.ys-1 ? sidedelta : 0)) + : (archverts[sel.ys-1][y] + (x==0 || x==sel.xs-1 ? sidedelta : 0))); + remipmore(sel); + } } void slope(int xd, int yd) { EDITSELMP; - int off = 0; - if(xd<0) off -= xd*sel.xs; - if(yd<0) off -= yd*sel.ys; - sel.xs++; - sel.ys++; - loopselxy(s->vdelta = xd*x+yd*y+off); - remipmore(sel); + loopv(sels) + { + block &sel = sels[i]; + int off = 0; + if(xd<0) off -= xd*sel.xs; + if(yd<0) off -= yd*sel.ys; + sel.xs++; + sel.ys++; + loopselxy(sel, s->vdelta = xd*x+yd*y+off); + remipmore(sel); + } } void perlin(int scale, int seed, int psize) { EDITSELMP; - sel.xs++; - sel.ys++; - makeundo(); - sel.xs--; - sel.ys--; - perlinarea(sel, scale, seed, psize); - sel.xs++; - sel.ys++; - remipmore(sel); - sel.xs--; - sel.ys--; + loopv(sels) + { + block &sel = sels[i]; + sel.xs++; + sel.ys++; + makeundo(sel); + sel.xs--; + sel.ys--; + perlinarea(sel, scale, seed, psize); + sel.xs++; + sel.ys++; + remipmore(sel); + sel.xs--; + sel.ys--; + } } VARF(fullbright, 0, 0, 1, @@ -518,13 +611,13 @@ VARF(fullbright, 0, 0, 1, void edittag(int tag) { EDITSELMP; - loopselxy(s->tag = tag); + loopselsxy(s->tag = tag); } void newent(char *what, char *a1, char *a2, char *a3, char *a4) { EDITSEL; - newentity(-1, sel.x, sel.y, (int)camera1->o.z, what, ATOI(a1), ATOI(a2), ATOI(a3), ATOI(a4)); + loopv(sels) newentity(-1, sels[i].x, sels[i].y, (int)camera1->o.z, what, ATOI(a1), ATOI(a2), ATOI(a3), ATOI(a4)); } void movemap(int xo, int yo, int zo) // move whole map @@ -566,9 +659,9 @@ void movemap(int xo, int yo, int zo) // move whole map resetmap(false); } -void selfliprotate(int dir) +void selfliprotate(block &sel, int dir) { - makeundo(); + makeundo(sel); block *org = blockcopy(sel); const sqr *q = (const sqr *)(org + 1); int x1 = sel.x, x2 = sel.x + sel.xs - 1, y1 = sel.y, y2 = sel.y + sel.ys - 1, x, y; @@ -600,19 +693,21 @@ void selectionrotate(int dir) dir %= 4; if(dir < 0) dir += 4; if(!dir) return; - if(sel.xs != sel.ys) dir = 2; - selfliprotate(dir); + loopv(sels) + { + block &sel = sels[i]; + if(sel.xs != sel.ys) dir = 2; + selfliprotate(sel, dir); + } } void selectionflip(char *axis) { EDITSELMP; if(!axis || !*axis) return; - switch(toupper(*axis)) - { - case 'X': selfliprotate(11); break; - case 'Y': selfliprotate(12); break; - } + char c = toupper(*axis); + if(c != 'X' && c != 'Y') return; + loopv(sels) selfliprotate(sels[i], c == 'X' ? 11 : 12); } COMMANDN(select, selectpos, ARG_4INT); diff --git a/source/src/protos.h b/source/src/protos.h index 9ad3a945..467ceb33 100644 --- a/source/src/protos.h +++ b/source/src/protos.h @@ -472,6 +472,7 @@ extern void undodynlights(); extern void cleardynlights(); extern void removedynlights(physent *owner); extern block *blockcopy(const block &b); +extern void blockpaste(const block &b, int bx, int by); extern void blockpaste(const block &b); extern void freeblock(block *&b); @@ -554,6 +555,7 @@ extern void cursorupdate(); extern void toggleedit(bool force = false); extern char *editinfo(); extern void editdrag(bool isdown); +extern void checkselections(); extern void setvdeltaxy(int delta, block &sel); extern void editequalisexy(bool isfloor, block &sel); extern void edittypexy(int type, block &sel); diff --git a/source/src/world.cpp b/source/src/world.cpp index 9019f689..88d5a4d0 100644 --- a/source/src/world.cpp +++ b/source/src/world.cpp @@ -442,6 +442,8 @@ bool empty_world(int factor, bool force) // main empty world creation routine sqrdefault(S(x,y)); } + checkselections(); // assert no selection became invalid + strncpy(hdr.head, "ACMP", 4); hdr.version = MAPVERSION; hdr.headersize = sizeof(header); @@ -471,7 +473,7 @@ bool empty_world(int factor, bool force) // main empty world creation routine loopi(sizeof(hdr.reserved)/sizeof(hdr.reserved[0])) hdr.reserved[i] = 0; loopk(3) loopi(256) hdr.texlists[k][i] = i; ents.shrink(0); - block b = { 8, 8, ssize-16, ssize-16 }; + block b = { 8, 8, ssize-16, ssize - 16}; edittypexy(SPACE, b); } diff --git a/source/src/world.h b/source/src/world.h index b042b13a..ab904822 100644 --- a/source/src/world.h +++ b/source/src/world.h @@ -130,7 +130,7 @@ struct mapstats #define MAXHHITS 6000 #define MINFF 2500 -struct block { int x, y, xs, ys; }; +struct block { int x, y, xs, ys, h; }; // vertex array format diff --git a/source/src/worldlight.cpp b/source/src/worldlight.cpp index b1311556..34322f9d 100644 --- a/source/src/worldlight.cpp +++ b/source/src/worldlight.cpp @@ -337,13 +337,18 @@ block *blockcopy(const block &s) return b; } -void blockpaste(const block &b) +void blockpaste(const block &b, int bx, int by) { const sqr *q = (const sqr *)((&b)+1); - for(int y = b.y; y