2018-02-08 23:40:26 +00:00
|
|
|
// Hyperbolic Rogue -- smart memory cleaner
|
|
|
|
// Copyright (C) 2011-2018 Zeno Rogue, see 'hyper.cpp' for details
|
|
|
|
|
2018-06-10 23:58:31 +00:00
|
|
|
namespace hr {
|
|
|
|
|
2018-01-26 01:10:40 +00:00
|
|
|
bool memory_saving_mode = true;
|
|
|
|
|
2018-06-10 23:58:31 +00:00
|
|
|
static const int LIM = 150;
|
2018-01-26 01:10:40 +00:00
|
|
|
|
|
|
|
heptagon *last_cleared;
|
|
|
|
|
|
|
|
void destroycellcontents(cell *c) {
|
|
|
|
c->land = laMemory;
|
|
|
|
c->wall = waChasm;
|
|
|
|
c->item = itNone;
|
|
|
|
if(!isMultitile(c->monst) && c->monst != moPair)
|
|
|
|
c->monst = moNone;
|
|
|
|
}
|
|
|
|
|
|
|
|
void degrade(cell *c) {
|
|
|
|
c->mpdist++;
|
|
|
|
forCellEx(c2, c)
|
|
|
|
if(c2->mpdist < c->mpdist - 1)
|
|
|
|
degrade(c2);
|
|
|
|
destroycellcontents(c);
|
|
|
|
}
|
|
|
|
|
|
|
|
vector<cell*> removed_cells;
|
|
|
|
|
|
|
|
|
|
|
|
void slow_delete_cell(cell *c) {
|
|
|
|
while(c->mpdist < BARLEV)
|
|
|
|
degrade(c);
|
|
|
|
for(int i=0; i<c->type; i++)
|
2018-08-17 22:46:45 +00:00
|
|
|
if(c->move(i))
|
|
|
|
c->move(i)->move(c->c.spin(i)) = NULL;
|
2018-01-26 01:10:40 +00:00
|
|
|
removed_cells.push_back(c);
|
|
|
|
delete c;
|
|
|
|
}
|
|
|
|
|
|
|
|
void delete_heptagon(heptagon *h2) {
|
|
|
|
cell *c = h2->c7;
|
2018-08-28 15:17:34 +00:00
|
|
|
if(BITRUNCATED) {
|
2018-01-26 01:10:40 +00:00
|
|
|
for(int i=0; i<c->type; i++)
|
2018-08-17 22:46:45 +00:00
|
|
|
if(c->move(i))
|
|
|
|
slow_delete_cell(c->move(i));
|
2018-01-26 01:10:40 +00:00
|
|
|
}
|
|
|
|
slow_delete_cell(c);
|
|
|
|
for(int i=0; i<S7; i++)
|
2018-08-17 22:46:45 +00:00
|
|
|
if(h2->move(i))
|
|
|
|
h2->move(i)->move(h2->c.spin(i)) = NULL;
|
2018-01-26 01:10:40 +00:00
|
|
|
delete h2;
|
|
|
|
}
|
|
|
|
|
|
|
|
void recursive_delete(heptagon *h, int i) {
|
2018-08-17 22:46:45 +00:00
|
|
|
heptagon *h2 = h->move(i);
|
2018-02-01 02:06:45 +00:00
|
|
|
{ for(int i=1; i<S7; i++)
|
2018-08-17 22:46:45 +00:00
|
|
|
if(h2->move(i) && h2->move(i)->move(0) == h2)
|
2018-02-01 02:06:45 +00:00
|
|
|
recursive_delete(h2, i); }
|
2018-01-26 01:10:40 +00:00
|
|
|
if(h2->alt && h2->alt->alt == h2->alt) {
|
2019-05-12 23:57:40 +00:00
|
|
|
DEBB(DF_MEMORY, ("destroying alternate map ", h2->alt));
|
2018-01-26 01:10:40 +00:00
|
|
|
for(hrmap *& hm: allmaps) {
|
2019-03-08 21:38:44 +00:00
|
|
|
if(hm->getOrigin() == h2->alt) {
|
2018-01-26 01:10:40 +00:00
|
|
|
delete hm;
|
|
|
|
hm = allmaps.back();
|
|
|
|
allmaps.pop_back();
|
2019-05-12 23:57:40 +00:00
|
|
|
DEBB(DF_MEMORY, ("map found (", isize(allmaps), " altmaps total)"));
|
2018-01-26 01:10:40 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(h2->alt) {
|
|
|
|
h2->alt->cdata = NULL;
|
|
|
|
}
|
|
|
|
delete_heptagon(h2);
|
2018-08-17 22:46:45 +00:00
|
|
|
h->move(i) = NULL;
|
2018-01-26 01:10:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool unsafeLand(cell *c) {
|
|
|
|
return
|
|
|
|
isCyclic(c->land) || isGravityLand(c->land) || isHaunted(c->land) ||
|
|
|
|
among(c->land, laCaribbean, laOcean, laGraveyard, laPrincessQuest);
|
|
|
|
}
|
|
|
|
|
|
|
|
void save_memory() {
|
2018-08-28 15:17:34 +00:00
|
|
|
if(quotient || !hyperbolic || NONSTDVAR) return;
|
2018-01-26 01:10:40 +00:00
|
|
|
if(!memory_saving_mode) return;
|
2018-08-17 22:46:45 +00:00
|
|
|
if(unsafeLand(cwt.at)) return;
|
|
|
|
int d = celldist(cwt.at);
|
2018-01-26 01:10:40 +00:00
|
|
|
if(d < LIM+10) return;
|
|
|
|
|
2018-08-17 22:46:45 +00:00
|
|
|
heptagon *at = cwt.at->master;
|
2018-01-26 01:10:40 +00:00
|
|
|
heptagon *orig = currentmap->gamestart()->master;
|
|
|
|
|
2019-05-29 00:21:47 +00:00
|
|
|
if(recallCell.at) {
|
|
|
|
if(unsafeLand(recallCell.at)) return;
|
|
|
|
heptagon *at2 = recallCell.at->master;
|
2018-01-26 01:10:40 +00:00
|
|
|
int t = 0;
|
|
|
|
while(at != at2) {
|
|
|
|
t++; if(t > 10000) return;
|
|
|
|
if(celldist(at->c7) > celldist(at2->c7))
|
2018-08-17 22:46:45 +00:00
|
|
|
at = at->move(0);
|
2018-01-26 01:10:40 +00:00
|
|
|
else
|
2018-08-17 22:46:45 +00:00
|
|
|
at2 = at2->move(0);
|
2018-01-26 01:10:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-17 22:46:45 +00:00
|
|
|
while(celldist(at->c7) > d-LIM) at = at->move(0);
|
2018-01-26 01:10:40 +00:00
|
|
|
|
|
|
|
// go back to such a point X that all the heptagons adjacent to the current 'at'
|
|
|
|
// are the children of X. This X becomes the new 'at'
|
|
|
|
if(true) {
|
|
|
|
heptagon *allh[9];
|
|
|
|
int hcount = 0;
|
|
|
|
allh[hcount++] = at;
|
|
|
|
for(int j=0; j<S7; j++)
|
2018-08-17 22:46:45 +00:00
|
|
|
if(allh[0]->move(j))
|
|
|
|
allh[hcount++] = at->move(j);
|
2018-01-26 01:10:40 +00:00
|
|
|
|
|
|
|
int deuniq_steps = 0;
|
|
|
|
|
|
|
|
int i = 1;
|
|
|
|
while(i < hcount) {
|
|
|
|
if(allh[i] == allh[0])
|
|
|
|
allh[i] = allh[hcount-1], hcount--;
|
|
|
|
else if(celldist(allh[i]->c7) > celldist(allh[0]->c7))
|
2018-08-17 22:46:45 +00:00
|
|
|
allh[i] = allh[i]->move(0);
|
2018-01-26 01:10:40 +00:00
|
|
|
else {
|
|
|
|
if(allh[0] == orig) return;
|
2018-08-17 22:46:45 +00:00
|
|
|
allh[0] = allh[0]->move(0);
|
2018-01-26 01:10:40 +00:00
|
|
|
i = 1;
|
|
|
|
deuniq_steps++;
|
|
|
|
if(deuniq_steps == 10) return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
at = allh[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
if(last_cleared && celldist(at->c7) < celldist(last_cleared->c7))
|
|
|
|
return;
|
|
|
|
|
2019-05-12 23:57:40 +00:00
|
|
|
DEBB(DF_MEMORY, ("celldist = ", make_pair(celldist(cwt.at), celldist(at->c7))));
|
2018-01-26 01:10:40 +00:00
|
|
|
|
|
|
|
heptagon *at1 = at;
|
|
|
|
while(at != last_cleared && at != orig) {
|
|
|
|
heptagon *atn = at;
|
2018-08-17 22:46:45 +00:00
|
|
|
at = at->move(0);
|
2018-01-26 01:10:40 +00:00
|
|
|
|
|
|
|
for(int i=1; i<S7; i++)
|
2018-08-17 22:46:45 +00:00
|
|
|
if(at->move(i) && at->move(i) != atn)
|
2018-01-26 01:10:40 +00:00
|
|
|
recursive_delete(at, i);
|
|
|
|
}
|
|
|
|
|
|
|
|
last_cleared = at1;
|
2019-05-12 23:57:40 +00:00
|
|
|
DEBB(DF_MEMORY, ("current cellcount = ", cellcount));
|
2018-01-26 01:10:40 +00:00
|
|
|
|
|
|
|
sort(removed_cells.begin(), removed_cells.end());
|
|
|
|
callhooks(hooks_removecells);
|
|
|
|
removed_cells.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
purehookset hooks_removecells;
|
|
|
|
|
|
|
|
bool is_cell_removed(cell *c) {
|
|
|
|
return binary_search(removed_cells.begin(), removed_cells.end(), c);
|
|
|
|
}
|
|
|
|
|
|
|
|
void set_if_removed(cell*& c, cell *val) {
|
|
|
|
if(is_cell_removed(c)) c = val;
|
|
|
|
}
|
2018-06-10 23:58:31 +00:00
|
|
|
|
|
|
|
}
|