1
0
mirror of https://github.com/zenorogue/hyperrogue.git synced 2024-12-18 15:00:26 +00:00
hyperrogue/blizzard.cpp

270 lines
7.6 KiB
C++
Raw Normal View History

// Hyperbolic Rogue -- Blizzard
// Copyright (C) 2011-2019 Zeno Rogue, see 'hyper.cpp' for details
/** \file blizzard.cpp
* \brief special graphical effects, such as the Blizzard and arrow traps
*/
#include "hyper.h"
namespace hr {
2017-09-30 10:20:12 +00:00
struct snowball {
transmatrix T;
shiftmatrix global;
2017-09-30 10:20:12 +00:00
snowball *prev;
snowball *next;
double phase;
snowball(int t) { T = rgpushxto0(randomPointIn(t)); phase = randd(); }
};
2019-08-09 19:18:13 +00:00
#if HDR
struct blizzardcell;
#endif
2017-09-30 10:20:12 +00:00
struct blizzardcell {
cell *c;
int frame;
int tmp;
shiftmatrix *gm;
2017-09-30 10:20:12 +00:00
char wmap;
int inward, outward, ward;
2020-01-18 15:03:32 +00:00
vector<int> qty;
2017-09-30 10:20:12 +00:00
vector<snowball*> inorder, outorder;
int inid, outid;
~blizzardcell() { for(auto i: inorder) delete i; }
};
2019-08-09 19:18:13 +00:00
EX map<cell*, blizzardcell> blizzardcells;
2017-09-30 10:20:12 +00:00
2019-08-09 19:00:52 +00:00
EX void set_blizzard_frame(cell *c, int frameid) {
blizzardcells[c].frame = frameid;
}
2019-08-09 19:18:13 +00:00
EX vector<blizzardcell*> bcells;
2017-09-30 10:20:12 +00:00
int blizzard_N;
2017-09-30 10:20:12 +00:00
2019-08-09 19:18:13 +00:00
EX blizzardcell* getbcell(cell *c) {
int i = c->listindex;
if(i<0 || i >= blizzard_N) return NULL;
2017-09-30 10:20:12 +00:00
if(bcells[i]->c != c) return NULL;
return bcells[i];
}
2019-08-09 19:00:52 +00:00
EX void drawBlizzards() {
#if CAP_SHAPES && CAP_FIELD
2017-10-14 23:08:40 +00:00
poly_outline = OUTLINE_NONE;
2017-09-30 10:20:12 +00:00
auto it = blizzardcells.begin();
bcells.clear();
while(it != blizzardcells.end())
if(it->second.frame != frameid || !gmatrix.count(it->first))
it = blizzardcells.erase(it);
else {
it->second.c = it->first;
bcells.push_back(&it->second);
it++;
}
blizzard_N = isize(bcells);
for(int i=0; i<blizzard_N; i++) {
2017-09-30 10:20:12 +00:00
auto& bc = *bcells[i];
bc.tmp = bc.c->listindex,
bc.c->listindex = i;
2017-09-30 10:20:12 +00:00
bc.gm = &gmatrix[bc.c];
bc.wmap = windmap::at(bc.c);
}
for(int i=0; i<blizzard_N; i++) {
2017-09-30 10:20:12 +00:00
auto& bc = *bcells[i];
cell *c = bc.c;
bc.inward = bc.outward = 0;
2020-01-18 15:03:32 +00:00
bc.qty.resize(c->type);
2017-09-30 10:20:12 +00:00
for(int i=0; i<c->type; i++) {
int& qty = bc.qty[i];
qty = 0;
cell *c2 = c->move(i);
2017-09-30 10:20:12 +00:00
if(!c2) continue;
auto bc2 = getbcell(c2);
if(!bc2) continue;
int z = (bc2->wmap - bc.wmap) & 255;
if(z >= windmap::NOWINDBELOW && z < windmap::NOWINDFROM)
bc.outward += qty = z / 8;
z = (-z) & 255;
if(z >= windmap::NOWINDBELOW && z < windmap::NOWINDFROM)
bc.inward += z / 8, qty = -z/8;
}
bc.ward = max(bc.inward, bc.outward);
2018-06-22 12:47:24 +00:00
while(isize(bc.inorder) < bc.ward) {
2017-09-30 10:20:12 +00:00
auto sb = new snowball(c->type);
bc.inorder.push_back(sb);
bc.outorder.push_back(sb);
}
for(auto& sb: bc.inorder) sb->prev = sb->next = NULL;
bc.inid = 0;
}
double at = fractick(40);
2017-09-30 10:20:12 +00:00
for(int i=0; i<blizzard_N; i++) {
2017-09-30 10:20:12 +00:00
auto& bc = *bcells[i];
for(auto sb: bc.inorder)
sb->global = (*bc.gm) * sb->T;
2017-10-14 23:08:40 +00:00
}
for(int i=0; i<blizzard_N; i++) {
2017-10-14 23:08:40 +00:00
auto& bc = *bcells[i];
cell *c = bc.c;
2017-09-30 10:20:12 +00:00
bc.outid = 0;
for(int d=0; d<c->type; d++) for(int k=0; k<bc.qty[d]; k++) {
auto& bc2 = *getbcell(c->move(d));
2017-09-30 10:20:12 +00:00
auto& sball = *bc.outorder[bc.outid++];
auto& sball2 = *bc2.inorder[bc2.inid++];
sball.next = &sball2;
sball2.prev = &sball;
hyperpoint t = inverse_shift(sball.global, tC0(sball2.global));
2017-09-30 10:20:12 +00:00
double at0 = at + sball.phase;
if(at0>1) at0 -= 1;
shiftmatrix tpartial = sball.global * rspintox(t) * xpush(hdist0(t) * at0);
2017-09-30 10:20:12 +00:00
if(wmascii || wmblack)
queuestr(tpartial, .2, ".", 0xFFFFFF);
else
queuepoly(tpartial, cgi.shSnowball, 0xFFFFFF80);
2017-09-30 10:20:12 +00:00
}
}
for(int ii=0; ii<blizzard_N; ii++) {
2017-09-30 10:20:12 +00:00
auto& bc = *bcells[ii];
/* if(isNeighbor(bc.c, mouseover)) {
if(againstWind(mouseover, bc.c))
queuepoly(*bc.gm, cgi.shHeptaMarker, 0x00C00040);
if(againstWind(bc.c, mouseover))
queuepoly(*bc.gm, cgi.shHeptaMarker, 0xC0000040);
} */
forCellIdEx(c2, i, bc.c) if(bc.c == mouseover || c2 == mouseover) {
color_t col = 0x00C00080;
if(c2 == mouseover)
col ^= 0xC0C00000;
if(isPlayerOn(c2))
col ^= 0x00000040;
if(isPlayerOn(bc.c))
col ^= 0x00000040;
if(againstWind(bc.c, c2))
queuepoly(*bc.gm * ddspin(bc.c, i) * xpush(cellgfxdist(bc.c, i)/2), cgi.shWindArrow, col);
}
2018-06-22 12:47:24 +00:00
int B = isize(bc.outorder);
2017-09-30 10:20:12 +00:00
if(B<2) continue;
int i = rand() % B;
int j = rand() % (B-1);
if(i==j) j++;
if(1) {
auto& sb1 = *bc.outorder[i];
auto& sb2 = *bc.outorder[j];
double swapcost = 0;
if(sb1.next) swapcost -= hdist(tC0(sb1.global), tC0(sb1.next->global));
if(sb2.next) swapcost -= hdist(tC0(sb2.global), tC0(sb2.next->global));
if(sb1.next) swapcost += hdist(tC0(sb2.global), tC0(sb1.next->global));
if(sb2.next) swapcost += hdist(tC0(sb1.global), tC0(sb2.next->global));
if(swapcost < 0) {
swap(bc.outorder[i], bc.outorder[j]);
swap(sb1.next, sb2.next);
if(sb1.next) sb1.next->prev = &sb1;
if(sb2.next) sb2.next->prev = &sb2;
}
}
if(1) {
auto& sb1 = *bc.inorder[i];
auto& sb2 = *bc.inorder[j];
double swapcost = 0;
if(sb1.prev) swapcost -= hdist(tC0(sb1.global), tC0(sb1.prev->global));
if(sb2.prev) swapcost -= hdist(tC0(sb2.global), tC0(sb2.prev->global));
if(sb1.prev) swapcost += hdist(tC0(sb2.global), tC0(sb1.prev->global));
if(sb2.prev) swapcost += hdist(tC0(sb1.global), tC0(sb2.prev->global));
if(swapcost < 0) {
swap(bc.inorder[i], bc.inorder[j]);
swap(sb1.prev, sb2.prev);
if(sb1.prev) sb1.prev->next = &sb1;
if(sb2.prev) sb2.prev->next = &sb2;
}
}
auto& sbp = *bc.inorder[i];
if(sbp.next && sbp.prev) {
double p1 = sbp.next->phase;
double p2 = sbp.prev->phase;
double d = p2-p1;
if(d<=.5) d+=1;
if(d>=.5) d-=1;
sbp.phase = p1 + d/2;
if(sbp.phase >= 1) sbp.phase -= 1;
if(sbp.phase < 0) sbp.phase += 1;
}
}
for(auto bc: bcells)
bc->c->listindex = bc->tmp;
2019-02-17 17:33:15 +00:00
#endif
2017-09-30 10:20:12 +00:00
}
2019-08-09 19:18:13 +00:00
EX vector<cell*> arrowtraps;
2017-09-30 10:20:12 +00:00
2019-08-09 19:00:52 +00:00
EX void drawArrowTraps() {
2017-10-04 19:26:26 +00:00
for(cell *c: arrowtraps) {
auto r = traplimits(c);
try {
shiftmatrix& t0 = gmatrix.at(r[0]);
shiftmatrix& t1 = gmatrix.at(r[4]);
2019-02-17 17:41:40 +00:00
ignore(t0);
ignore(t1);
2017-10-04 19:26:26 +00:00
2019-02-17 17:41:40 +00:00
#if CAP_QUEUE
2018-08-28 12:27:23 +00:00
queueline(tC0(t0), tC0(t1), 0xFF0000FF, 4 + vid.linequality, PPR::ITEM);
2019-02-17 17:41:40 +00:00
#endif
#if CAP_SHAPES
2017-10-12 09:33:26 +00:00
if((c->wparam & 7) == 3 && !shmup::on) {
2018-08-28 12:27:23 +00:00
// queueline(t0 * randomPointIn(r[0]->type), t1 * randomPointIn(r[1]->type), 0xFFFFFFFF, 4, PPR::ITEM);
int tt = int(fractick(64) * 401);
2017-10-04 19:26:26 +00:00
for(int u=0; u<2; u++) {
shiftmatrix& tu = u ? t0 : t1;
shiftmatrix& tv = u ? t1 : t0;
hyperpoint trel = inverse_shift(tu, tC0(tv));
shiftmatrix tpartial = tu * rspintox(trel) * xpush(hdist0(trel) * tt / 401.0);
tpartial = tpartial * ypush(.05);
2019-08-15 13:05:43 +00:00
if(GDIM == 3) tpartial = tpartial * cspin(1, 2, M_PI/2);
queuepoly(tpartial, cgi.shTrapArrow, 0xFFFFFFFF);
2017-10-04 19:26:26 +00:00
}
2017-09-30 10:20:12 +00:00
}
2019-02-17 17:41:40 +00:00
#endif
2017-09-30 10:20:12 +00:00
}
catch(out_of_range&) {}
2017-10-04 19:26:26 +00:00
}
}
2017-10-12 09:33:26 +00:00
auto ccm_blizzard = addHook(hooks_clearmemory, 0, [] () {
2017-10-12 09:33:26 +00:00
arrowtraps.clear();
blizzardcells.clear();
bcells.clear();
2018-01-26 00:45:49 +00:00
}) +
2019-05-30 14:12:38 +00:00
+ addHook(hooks_gamedata, 0, [] (gamedata* gd) {
gd->store(arrowtraps);
gd->store(blizzardcells);
gd->store(bcells);
gd->store(blizzard_N);
})
+ addHook(hooks_removecells, 0, [] () {
2018-01-26 00:45:49 +00:00
eliminate_if(arrowtraps, is_cell_removed);
2017-10-12 09:33:26 +00:00
});
}