diff --git a/desktop_version/src/Editor.cpp b/desktop_version/src/Editor.cpp index eeacf4d8..70ce3f2b 100644 --- a/desktop_version/src/Editor.cpp +++ b/desktop_version/src/Editor.cpp @@ -4175,7 +4175,7 @@ void editorclass::switch_tileset(const bool reversed) } const int modulus = SDL_arraysize(tilesets); - tiles = (tiles % modulus + modulus) % modulus; + tiles = POS_MOD(tiles, modulus); cl.setroomtileset(levx, levy, tiles); clamp_tilecol(levx, levy, false); @@ -4270,7 +4270,7 @@ void editorclass::switch_enemy(const bool reversed) } const int modulus = 10; - enemy = (enemy % modulus + modulus) % modulus; + enemy = POS_MOD(enemy, modulus); cl.setroomenemytype(levx, levy, enemy); note = "Enemy Type Changed"; @@ -4293,7 +4293,7 @@ void editorclass::switch_warpdir(const bool reversed) ++warpdir; } - warpdir = (warpdir % modulus + modulus) % modulus; + warpdir = POS_MOD(warpdir, modulus); cl.setroomwarpdir(levx, levy, warpdir); switch (warpdir) diff --git a/desktop_version/src/Tower.cpp b/desktop_version/src/Tower.cpp index eaa622d5..1d2a0471 100644 --- a/desktop_version/src/Tower.cpp +++ b/desktop_version/src/Tower.cpp @@ -5,6 +5,7 @@ #include "Constants.h" #include "MakeAndPlay.h" +#include "UtilityClass.h" towerclass::towerclass(void) { @@ -24,8 +25,7 @@ int towerclass::backat(int xp, int yp, int yoff) if (xp >= 0 && xp < 40) { - while (yp < 0) yp += 120; - while (yp >= 120) yp -= 120; + yp = POS_MOD(yp, 120); return back[TILE_IDX(xp, yp)]; } return 0; @@ -41,8 +41,7 @@ int towerclass::at(int xp, int yp, int yoff) { yp = (yp*8 + yoff) / 8; - while (yp < 0) yp += 700; - while (yp >= 700) yp -= 700; + yp = POS_MOD(yp, 700); if (xp >= 0 && xp < 40) { return contents[TILE_IDX(xp, yp)]; @@ -63,8 +62,7 @@ int towerclass::miniat(int xp, int yp, int yoff) { yp = (yp*8 + yoff) / 8; - while (yp < 0) yp += 100; - while (yp >= 100) yp -= 100; + yp = POS_MOD(yp, 100); if (xp >= 0 && xp < 40) { return minitower[TILE_IDX(xp, yp)]; diff --git a/desktop_version/src/UtilityClass.h b/desktop_version/src/UtilityClass.h index 45d6ecc8..4fc7f5ae 100644 --- a/desktop_version/src/UtilityClass.h +++ b/desktop_version/src/UtilityClass.h @@ -88,6 +88,11 @@ void _VVV_between( } \ while (false) +/* Positive modulo, because C/C++'s modulo operator sucks and is negative given + * negative divisors. + * WARNING! This double- and triple- evaluates. */ +#define POS_MOD(a, b) (((a) % (b) + (b)) % (b)) + //helperClass class UtilityClass {