1
0
Fork 0
mirror of https://github.com/AquariaOSE/Aquaria.git synced 2024-11-29 03:33:48 +00:00

Unfuck & optimize Game::getWallNormal()

Another float->int->float roundtrip fixed.
This commit is contained in:
fgenesis 2023-04-18 02:16:18 +02:00
parent d2c0e3e241
commit d7bf48fc30

View file

@ -702,63 +702,36 @@ void Game::dilateGrid(unsigned int radius, ObsType test, ObsType set, ObsType al
Vector Game::getWallNormal(Vector pos, int sampleArea, float *dist, int obs) Vector Game::getWallNormal(Vector pos, int sampleArea, float *dist, int obs)
{ {
TileVector t(pos); const TileVector t(pos); // snap to grid
//Vector p = t.worldVector();
Vector avg; Vector avg;
float mindist = HUGE_VALF;
const float szf = (TILE_SIZE*(sampleArea-1));
int c = 0; int c = 0;
//float maxLen = -1;
std::vector<Vector> vs;
if (dist != NULL)
*dist = -1;
for (int x = t.x-sampleArea; x <= t.x+sampleArea; x++) for (int x = t.x-sampleArea; x <= t.x+sampleArea; x++)
{ {
for (int y = t.y-sampleArea; y <= t.y+sampleArea; y++) for (int y = t.y-sampleArea; y <= t.y+sampleArea; y++)
{ {
if (x == t.x && y == t.y) continue; if (x == t.x && y == t.y) continue;
TileVector ct(x,y); const TileVector ct(x,y);
Vector vt = ct.worldVector();
if (isObstructed(ct, obs)) if (isObstructed(ct, obs))
{ {
int xDiff = pos.x-vt.x; Vector v = pos - ct.worldVector();
int yDiff = pos.y-vt.y; const float d = v.getLength2D();
Vector v(xDiff, yDiff); if (d < mindist)
vs.push_back (v); mindist = d;
if (d < szf)
if (dist!=NULL)
{ {
float d = (vt-pos).getLength2D(); v.setLength2D(szf - d);
if (*dist == -1 || d < *dist) avg += v;
{ ++c;
*dist = d;
}
} }
} }
} }
} }
const int sz = (TILE_SIZE*(sampleArea-1)); if(dist)
for (size_t i = 0; i < vs.size(); i++) *dist = c ? mindist : -1.0f;
{ avg.normalize2D();
float len = vs[i].getLength2D();
if (len < sz)
{
vs[i].setLength2D(sz - len);
c++;
avg += vs[i];
}
}
if (c)
{
avg /= c;
if (avg.x != 0 || avg.y != 0)
{
avg.normalize2D();
avg.z = 0;
}
}
else
{
avg.x = avg.y = 0;
}
return avg; return avg;
} }