From 28d86d2859f0a3b6cf8b816967b1637ea5076daa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tam=C3=A1s=20B=C3=A1lint=20Misius?= Date: Thu, 7 Sep 2023 22:40:01 +0200 Subject: [PATCH] Unify InBounds implementations --- src/common/Vec2.h | 2 +- src/simulation/Air.cpp | 4 ++-- src/simulation/ElementDefs.h | 5 +++++ src/simulation/Simulation.cpp | 2 +- src/simulation/Simulation.h | 5 ----- src/simulation/elements/DRAY.cpp | 8 +------- src/simulation/elements/ETRD.cpp | 2 +- src/simulation/elements/STKM.cpp | 29 ++++++++++++----------------- 8 files changed, 23 insertions(+), 34 deletions(-) diff --git a/src/common/Vec2.h b/src/common/Vec2.h index c0ad17c5f..b17041fd3 100644 --- a/src/common/Vec2.h +++ b/src/common/Vec2.h @@ -391,7 +391,7 @@ public: return *this = *this & other; } - inline bool Contains(Vec2 point) const + constexpr bool Contains(Vec2 point) const { return point.X >= TopLeft.X && point.X <= BottomRight.X && point.Y >= TopLeft.Y && point.Y <= BottomRight.Y; } diff --git a/src/simulation/Air.cpp b/src/simulation/Air.cpp index c8d98ceeb..8a0996163 100644 --- a/src/simulation/Air.cpp +++ b/src/simulation/Air.cpp @@ -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]++; } } diff --git a/src/simulation/ElementDefs.h b/src/simulation/ElementDefs.h index 320214d4c..c808d1fc4 100644 --- a/src/simulation/ElementDefs.h +++ b/src/simulation/ElementDefs.h @@ -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; diff --git a/src/simulation/Simulation.cpp b/src/simulation/Simulation.cpp index 60ed9f6db..41e5c8eb4 100644 --- a/src/simulation/Simulation.cpp +++ b/src/simulation/Simulation.cpp @@ -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; diff --git a/src/simulation/Simulation.h b/src/simulation/Simulation.h index cd2df682b..38f59cee2 100644 --- a/src/simulation/Simulation.h +++ b/src/simulation/Simulation.h @@ -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); diff --git a/src/simulation/elements/DRAY.cpp b/src/simulation/elements/DRAY.cpp index 7a939639a..97218461d 100644 --- a/src/simulation/elements/DRAY.cpp +++ b/src/simulation/elements/DRAY.cpp @@ -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 && xInBounds(xCurrent, yCurrent)) + if (!InBounds(xCurrent, yCurrent)) break; int rr; // haven't found a particle yet, keep looking for one diff --git a/src/simulation/elements/ETRD.cpp b/src/simulation/elements/ETRD.cpp index 5fc3089fd..dfd2e7476 100644 --- a/src/simulation/elements/ETRD.cpp +++ b/src/simulation/elements/ETRD.cpp @@ -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) diff --git a/src/simulation/elements/STKM.cpp b/src/simulation/elements/STKM.cpp index 0f097e829..4c88356e7 100644 --- a/src/simulation/elements/STKM.cpp +++ b/src/simulation/elements/STKM.cpp @@ -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 (dllegs[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;