Add POS_MOD macro and use for all positive modulos

This macro is to make it so it won't be error-prone to write the
semi-confusing `(a % b + b) % b` statement, and you can just use an easy
macro instead.

Currently, the only places a positive modulo is needed is when switching
tilesets, enemies, and warp directions in the editor, as well as when
getting a tile in the tower, since towers just repeat themselves
vertically. Towers used this weird while-loop to sort of emulate a
modulo, which isn't half-bad, but is unnecessary, and I don't think any
compiler would recognize it as a modulo. (And if it's not optimized to a
proper modulo... what happens if the number being moduloed is really,
really big?)
This commit is contained in:
Misa
2021-09-24 17:48:15 -07:00
parent 891ca527f9
commit cace685020
3 changed files with 12 additions and 9 deletions

View File

@@ -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)];