Unify InBounds implementations

This commit is contained in:
Tamás Bálint Misius 2023-09-07 22:40:01 +02:00
parent 1b1ef99194
commit 28d86d2859
No known key found for this signature in database
GPG Key ID: 5B472A12F6ECA9F2
8 changed files with 23 additions and 34 deletions

View File

@ -391,7 +391,7 @@ public:
return *this = *this & other;
}
inline bool Contains(Vec2<T> point) const
constexpr bool Contains(Vec2<T> point) const
{
return point.X >= TopLeft.X && point.X <= BottomRight.X && point.Y >= TopLeft.Y && point.Y <= BottomRight.Y;
}

View File

@ -365,7 +365,7 @@ void Air::ApproximateBlockAirMaps()
if (type == PT_TTAN)
{
int x = ((int)(sim.parts[i].x+0.5f))/CELL, y = ((int)(sim.parts[i].y+0.5f))/CELL;
if (sim.InBounds(x, y))
if (InBounds(x, y))
{
bmap_blockair[y][x] = 1;
bmap_blockairh[y][x] = 0x8;
@ -375,7 +375,7 @@ void Air::ApproximateBlockAirMaps()
else if ((type == PT_HSWC && sim.parts[i].life != 10) || sim.elements[type].HeatConduct <= (sim.rng()%250))
{
int x = ((int)(sim.parts[i].x+0.5f))/CELL, y = ((int)(sim.parts[i].y+0.5f))/CELL;
if (sim.InBounds(x, y) && !(bmap_blockairh[y][x]&0x8))
if (InBounds(x, y) && !(bmap_blockairh[y][x]&0x8))
bmap_blockairh[y][x]++;
}
}

View File

@ -77,4 +77,9 @@ constexpr int PMAPID(int id)
}
constexpr int PT_NUM = 1 << PMAPBITS;
constexpr bool InBounds(int x, int y)
{
return RES.OriginRect().Contains({ x, y });
}
struct playerst;

View File

@ -2877,7 +2877,7 @@ killed:
fin_yf = parts[i].y;
fin_x = (int)(fin_xf+0.5f);
fin_y = (int)(fin_yf+0.5f);
bool closedEholeStart = this->InBounds(fin_x, fin_y) && (bmap[fin_y/CELL][fin_x/CELL] == WL_EHOLE && !emap[fin_y/CELL][fin_x/CELL]);
bool closedEholeStart = InBounds(fin_x, fin_y) && (bmap[fin_y/CELL][fin_x/CELL] == WL_EHOLE && !emap[fin_y/CELL][fin_x/CELL]);
while (1)
{
mv -= ISTP;

View File

@ -218,11 +218,6 @@ public:
Simulation();
~Simulation();
static bool InBounds(int x, int y)
{
return RES.OriginRect().Contains({ x, y });
}
// These don't really belong anywhere at the moment, so go here for loop edge mode
static int remainder_p(int x, int y);
static float remainder_p(float x, float y);

View File

@ -48,12 +48,6 @@ void Element::Element_DRAY()
CtypeDraw = &Element::ctypeDrawVInCtype;
}
//should probably be in Simulation.h
static bool InBounds(int x, int y)
{
return (x>=0 && y>=0 && x<XRES && y<YRES);
}
static int update(UPDATE_FUNC_ARGS)
{
int ctype = TYP(parts[i].ctype), ctypeExtra = ID(parts[i].ctype), copyLength = parts[i].tmp, copySpaces = parts[i].tmp2;
@ -86,7 +80,7 @@ static int update(UPDATE_FUNC_ARGS)
for (int xStep = rx*-1, yStep = ry*-1, xCurrent = x+xStep, yCurrent = y+yStep; ; xCurrent+=xStep, yCurrent+=yStep)
{
// Out of bounds, stop looking and don't copy anything
if (!sim->InBounds(xCurrent, yCurrent))
if (!InBounds(xCurrent, yCurrent))
break;
int rr;
// haven't found a particle yet, keep looking for one

View File

@ -123,7 +123,7 @@ int Element_ETRD_nearestSparkablePart(Simulation *sim, int targetId)
// deltaPos is sorted in order of ascending length, so foundDistance < checkDistance means all later items are further away.
break;
}
if (sim->InBounds(checkPos.X, checkPos.Y) && checkDistance <= foundDistance)
if (InBounds(checkPos.X, checkPos.Y) && checkDistance <= foundDistance)
{
int r = sim->pmap[checkPos.Y][checkPos.X];
if (r && TYP(r) == PT_ETRD && !parts[ID(r)].life && ID(r) != targetId && checkDistance < foundDistance)

View File

@ -98,11 +98,6 @@ static void changeType(ELEMENT_CHANGETYPE_FUNC_ARGS)
sim->player.spwn = 0;
}
constexpr bool INBOND(int x, int y)
{
return x >= 0 && y >= 0 && x < XRES && y < YRES;
}
void die(Simulation *sim, playerst *playerp, int i)
{
int x = (int)(sim->parts[i].x + 0.5f);
@ -258,7 +253,7 @@ int Element_STKM_run_stickman(playerst *playerp, UPDATE_FUNC_ARGS)
bool moved = false;
if (dl>dr)
{
if (INBOND(int(playerp->legs[4]), int(playerp->legs[5])) && !sim->eval_move(t, int(playerp->legs[4]), int(playerp->legs[5]), NULL))
if (InBounds(int(playerp->legs[4]), int(playerp->legs[5])) && !sim->eval_move(t, int(playerp->legs[4]), int(playerp->legs[5]), NULL))
{
playerp->accs[2] = -3*mvy-3*mvx;
playerp->accs[3] = 3*mvx-3*mvy;
@ -269,7 +264,7 @@ int Element_STKM_run_stickman(playerst *playerp, UPDATE_FUNC_ARGS)
}
else
{
if (INBOND(int(playerp->legs[12]), int(playerp->legs[13])) && !sim->eval_move(t, int(playerp->legs[12]), int(playerp->legs[13]), NULL))
if (InBounds(int(playerp->legs[12]), int(playerp->legs[13])) && !sim->eval_move(t, int(playerp->legs[12]), int(playerp->legs[13]), NULL))
{
playerp->accs[6] = -3*mvy-3*mvx;
playerp->accs[7] = 3*mvx-3*mvy;
@ -308,7 +303,7 @@ int Element_STKM_run_stickman(playerst *playerp, UPDATE_FUNC_ARGS)
bool moved = false;
if (dl<dr)
{
if (INBOND(int(playerp->legs[4]), int(playerp->legs[5])) && !sim->eval_move(t, int(playerp->legs[4]), int(playerp->legs[5]), NULL))
if (InBounds(int(playerp->legs[4]), int(playerp->legs[5])) && !sim->eval_move(t, int(playerp->legs[4]), int(playerp->legs[5]), NULL))
{
playerp->accs[2] = 3*mvy-3*mvx;
playerp->accs[3] = -3*mvx-3*mvy;
@ -319,7 +314,7 @@ int Element_STKM_run_stickman(playerst *playerp, UPDATE_FUNC_ARGS)
}
else
{
if (INBOND(int(playerp->legs[12]), int(playerp->legs[13])) && !sim->eval_move(t, int(playerp->legs[12]), int(playerp->legs[13]), NULL))
if (InBounds(int(playerp->legs[12]), int(playerp->legs[13])) && !sim->eval_move(t, int(playerp->legs[12]), int(playerp->legs[13]), NULL))
{
playerp->accs[6] = 3*mvy-3*mvx;
playerp->accs[7] = -3*mvx-3*mvy;
@ -385,8 +380,8 @@ int Element_STKM_run_stickman(playerst *playerp, UPDATE_FUNC_ARGS)
}
}
}
else if ((INBOND(int(playerp->legs[4]), int(playerp->legs[5])) && !sim->eval_move(t, int(playerp->legs[4]), int(playerp->legs[5]), NULL)) ||
(INBOND(int(playerp->legs[12]), int(playerp->legs[13])) && !sim->eval_move(t, int(playerp->legs[12]), int(playerp->legs[13]), NULL)))
else if ((InBounds(int(playerp->legs[4]), int(playerp->legs[5])) && !sim->eval_move(t, int(playerp->legs[4]), int(playerp->legs[5]), NULL)) ||
(InBounds(int(playerp->legs[12]), int(playerp->legs[13])) && !sim->eval_move(t, int(playerp->legs[12]), int(playerp->legs[13]), NULL)))
{
parts[i].vx -= 4*mvx;
parts[i].vy -= 4*mvy;
@ -398,10 +393,10 @@ int Element_STKM_run_stickman(playerst *playerp, UPDATE_FUNC_ARGS)
}
//Charge detector wall if foot inside
if (INBOND(int(playerp->legs[4]+0.5)/CELL, int(playerp->legs[5]+0.5)/CELL) &&
if (InBounds(int(playerp->legs[4]+0.5)/CELL, int(playerp->legs[5]+0.5)/CELL) &&
sim->bmap[(int)(playerp->legs[5]+0.5)/CELL][(int)(playerp->legs[4]+0.5)/CELL]==WL_DETECT)
sim->set_emap((int)playerp->legs[4]/CELL, (int)playerp->legs[5]/CELL);
if (INBOND(int(playerp->legs[12]+0.5)/CELL, int(playerp->legs[13]+0.5)/CELL) &&
if (InBounds(int(playerp->legs[12]+0.5)/CELL, int(playerp->legs[13]+0.5)/CELL) &&
sim->bmap[(int)(playerp->legs[13]+0.5)/CELL][(int)(playerp->legs[12]+0.5)/CELL]==WL_DETECT)
sim->set_emap((int)(playerp->legs[12]+0.5)/CELL, (int)(playerp->legs[13]+0.5)/CELL);
@ -558,27 +553,27 @@ int Element_STKM_run_stickman(playerst *playerp, UPDATE_FUNC_ARGS)
playerp->legs[8] += (playerp->legs[8]-parts[i].x)*d;
playerp->legs[9] += (playerp->legs[9]-parts[i].y)*d;
if (INBOND(int(playerp->legs[4]), int(playerp->legs[5])) && !sim->eval_move(t, int(playerp->legs[4]), int(playerp->legs[5]), NULL))
if (InBounds(int(playerp->legs[4]), int(playerp->legs[5])) && !sim->eval_move(t, int(playerp->legs[4]), int(playerp->legs[5]), NULL))
{
playerp->legs[4] = playerp->legs[6];
playerp->legs[5] = playerp->legs[7];
}
if (INBOND(int(playerp->legs[12]), int(playerp->legs[13])) && !sim->eval_move(t, int(playerp->legs[12]), int(playerp->legs[13]), NULL))
if (InBounds(int(playerp->legs[12]), int(playerp->legs[13])) && !sim->eval_move(t, int(playerp->legs[12]), int(playerp->legs[13]), NULL))
{
playerp->legs[12] = playerp->legs[14];
playerp->legs[13] = playerp->legs[15];
}
//This makes stick man "pop" from obstacles
if (INBOND(int(playerp->legs[4]), int(playerp->legs[5])) && !sim->eval_move(t, int(playerp->legs[4]), int(playerp->legs[5]), NULL))
if (InBounds(int(playerp->legs[4]), int(playerp->legs[5])) && !sim->eval_move(t, int(playerp->legs[4]), int(playerp->legs[5]), NULL))
{
float t;
t = playerp->legs[4]; playerp->legs[4] = playerp->legs[6]; playerp->legs[6] = t;
t = playerp->legs[5]; playerp->legs[5] = playerp->legs[7]; playerp->legs[7] = t;
}
if (INBOND(int(playerp->legs[12]), int(playerp->legs[13])) && !sim->eval_move(t, int(playerp->legs[12]), int(playerp->legs[13]), NULL))
if (InBounds(int(playerp->legs[12]), int(playerp->legs[13])) && !sim->eval_move(t, int(playerp->legs[12]), int(playerp->legs[13]), NULL))
{
float t;
t = playerp->legs[12]; playerp->legs[12] = playerp->legs[14]; playerp->legs[14] = t;