1
0
Fork 0
mirror of https://github.com/TerryCavanagh/VVVVVV.git synced 2025-01-03 15:39:46 +01:00

Remove overcomplicated integer divisions

Believe it or not, there are still some remnants of the ActionScript
coding standards in the codebase! And one of them sometimes pops up
whenever an integer division happens.

As it so happens, it seems like division in ActionScript automatically
produces a decimal number. So to prevent that, the game sometimes
subtracts off the remainder of the number to be divided before
performing the division on it.

Thus, we get statements that look like

    (a - (a % b)) / b

And probably more parentheses surrounding it too, since it would be
copy-pasted into yet another larger expression, because of course it
would.

`(a % b)` here is subtracting the remainder of `a` divided by `b`, using
the modulo operator, before it gets divided by `b`. Thus, the number
will always be divisible by `b`, so dividing it will mathematically not
produce a decimal number.

Needless to say, this is unnecessary, and very unreadable. In fact, when
I saw these for the first time, I thought they were overcomplicated
_modulos_, _not_ integer division! In C and C++, dividing an integer by
an integer will always result in an integer, so there's no need to do
all this runaround just to divide two integers.

To find all of these, I used the command

    rg --pcre2 '(.+?).+?-.+?(?=\1).+?%.+?([\d]+?).+?\/.+?(?=\2)'

which basically matches expressions of the form 'a - a % b / b', where
'a' and 'b' are identical and there could be any characters in the
spaces.
This commit is contained in:
Misa 2021-09-24 17:21:46 -07:00
parent 6192269128
commit 891ca527f9
7 changed files with 23 additions and 33 deletions

View file

@ -871,8 +871,8 @@ void customlevelclass::findstartpoint(void)
else else
{ {
//Start point spawn //Start point spawn
int tx=(customentities[testeditor].x-(customentities[testeditor].x%40))/40; int tx=customentities[testeditor].x/40;
int ty=(customentities[testeditor].y-(customentities[testeditor].y%30))/30; int ty=customentities[testeditor].y/30;
game.edsavex = ((customentities[testeditor].x%40)*8)-4; game.edsavex = ((customentities[testeditor].x%40)*8)-4;
game.edsavey = (customentities[testeditor].y%30)*8; game.edsavey = (customentities[testeditor].y%30)*8;
game.edsaverx = 100+tx; game.edsaverx = 100+tx;

View file

@ -721,7 +721,7 @@ void editorrender(void)
if(temp2==i) if(temp2==i)
{ {
graphics.bprint((customentities[i].x*8)- (ed.levx*40*8),(customentities[i].y*8)- (ed.levy*30*8)-8, graphics.bprint((customentities[i].x*8)- (ed.levx*40*8),(customentities[i].y*8)- (ed.levy*30*8)-8,
"("+help.String(((customentities[i].p1-int(customentities[i].p1%40))/40)+1)+","+help.String(((customentities[i].p2-int(customentities[i].p2%30))/30)+1)+")",210,210,255); "("+help.String(customentities[i].p1/40 + 1)+","+help.String(customentities[i].p2/30 + 1)+")",210,210,255);
} }
else else
{ {
@ -854,7 +854,7 @@ void editorrender(void)
if(ed.tilex+(ed.levx*40)==customentities[i].p1 && ed.tiley+(ed.levy*30)==customentities[i].p2) if(ed.tilex+(ed.levx*40)==customentities[i].p1 && ed.tiley+(ed.levy*30)==customentities[i].p2)
{ {
graphics.bprint((customentities[i].p1*8)- (ed.levx*40*8),(customentities[i].p2*8)- (ed.levy*30*8)-8, graphics.bprint((customentities[i].p1*8)- (ed.levx*40*8),(customentities[i].p2*8)- (ed.levy*30*8)-8,
"("+help.String(((customentities[i].x-int(customentities[i].x%40))/40)+1)+","+help.String(((customentities[i].y-int(customentities[i].y%30))/30)+1)+")",190,190,225); "("+help.String(customentities[i].x/40 + 1)+","+help.String(customentities[i].y/30 + 1)+")",190,190,225);
} }
else else
{ {
@ -2720,8 +2720,8 @@ void editorinput(void)
//if() on screen //if() on screen
if(customentities[i].t==16 && testeditor==-1) if(customentities[i].t==16 && testeditor==-1)
{ {
int tx=(customentities[i].x-(customentities[i].x%40))/40; int tx=customentities[i].x/40;
int ty=(customentities[i].y-(customentities[i].y%30))/30; int ty=customentities[i].y/30;
if(tx==ed.levx && ty==ed.levy) if(tx==ed.levx && ty==ed.levy)
{ {
testeditor=i; testeditor=i;
@ -2737,8 +2737,8 @@ void editorinput(void)
//if() on screen //if() on screen
if(customentities[i].t==10 && testeditor==-1) if(customentities[i].t==10 && testeditor==-1)
{ {
int tx=(customentities[i].x-(customentities[i].x%40))/40; int tx=customentities[i].x/40;
int ty=(customentities[i].y-(customentities[i].y%30))/30; int ty=customentities[i].y/30;
if(tx==ed.levx && ty==ed.levy) if(tx==ed.levx && ty==ed.levy)
{ {
testeditor=i; testeditor=i;
@ -2758,8 +2758,8 @@ void editorinput(void)
if(startpoint==0) if(startpoint==0)
{ {
//Checkpoint spawn //Checkpoint spawn
int tx=(customentities[testeditor].x-(customentities[testeditor].x%40))/40; int tx=customentities[testeditor].x/40;
int ty=(customentities[testeditor].y-(customentities[testeditor].y%30))/30; int ty=customentities[testeditor].y/30;
game.edsavex = (customentities[testeditor].x%40)*8 - 4; game.edsavex = (customentities[testeditor].x%40)*8 - 4;
game.edsavey = (customentities[testeditor].y%30)*8; game.edsavey = (customentities[testeditor].y%30)*8;
game.edsaverx = 100+tx; game.edsaverx = 100+tx;
@ -2779,8 +2779,8 @@ void editorinput(void)
else else
{ {
//Start point spawn //Start point spawn
int tx=(customentities[testeditor].x-(customentities[testeditor].x%40))/40; int tx=customentities[testeditor].x/40;
int ty=(customentities[testeditor].y-(customentities[testeditor].y%30))/30; int ty=customentities[testeditor].y/30;
game.edsavex = (customentities[testeditor].x%40)*8 - 4; game.edsavex = (customentities[testeditor].x%40)*8 - 4;
game.edsavey = (customentities[testeditor].y%30)*8; game.edsavey = (customentities[testeditor].y%30)*8;
game.edsaverx = 100+tx; game.edsaverx = 100+tx;

View file

@ -4076,8 +4076,7 @@ int entityclass::checkactivity(void)
int entityclass::getgridpoint( int t ) int entityclass::getgridpoint( int t )
{ {
t = (t - (t % 8)) / 8; return t / 8;
return t;
} }
bool entityclass::checkplatform(const SDL_Rect& temprect, int* px, int* py) bool entityclass::checkplatform(const SDL_Rect& temprect, int* px, int* py)

View file

@ -5779,7 +5779,7 @@ std::string Game::partimestring(void)
std::string tempstring = ""; std::string tempstring = "";
if (timetrialpar >= 60) if (timetrialpar >= 60)
{ {
tempstring = help.twodigits(int((timetrialpar - (timetrialpar % 60)) / 60)) + ":" + help.twodigits(timetrialpar % 60); tempstring = help.twodigits(timetrialpar / 60) + ":" + help.twodigits(timetrialpar % 60);
} }
else else
{ {
@ -5794,7 +5794,7 @@ std::string Game::resulttimestring(void)
std::string tempstring = ""; std::string tempstring = "";
if (timetrialresulttime >= 60) if (timetrialresulttime >= 60)
{ {
tempstring = help.twodigits(int((timetrialresulttime - (timetrialresulttime % 60)) / 60)) + ":" tempstring = help.twodigits(timetrialresulttime / 60) + ":"
+ help.twodigits(timetrialresulttime % 60); + help.twodigits(timetrialresulttime % 60);
} }
else else
@ -5811,7 +5811,7 @@ std::string Game::timetstring( int t )
std::string tempstring = ""; std::string tempstring = "";
if (t >= 60) if (t >= 60)
{ {
tempstring = help.twodigits(int((t - (t % 60)) / 60)) + ":" + help.twodigits(t % 60); tempstring = help.twodigits(t / 60) + ":" + help.twodigits(t % 60);
} }
else else
{ {

View file

@ -1250,8 +1250,8 @@ void gamelogic(void)
int edi=obj.entities[game.edteleportent].behave; int edi=obj.entities[game.edteleportent].behave;
int edj=obj.entities[game.edteleportent].para; int edj=obj.entities[game.edteleportent].para;
int edi2, edj2; int edi2, edj2;
edi2 = (edi-(edi%40))/40; edi2 = edi/40;
edj2 = (edj-(edj%30))/30; edj2 = edj/30;
map.warpto(100+edi2, 100+edj2, obj.getplayer(), edi%40, (edj%30)+2); map.warpto(100+edi2, 100+edj2, obj.getplayer(), edi%40, (edj%30)+2);
game.teleport = false; game.teleport = false;

View file

@ -20,10 +20,7 @@ towerclass::towerclass(void)
int towerclass::backat(int xp, int yp, int yoff) int towerclass::backat(int xp, int yp, int yoff)
{ {
yp = yp * 8; yp = (yp*8 + yoff) / 8;
yp += yoff;
yoff = yp % 8;
yp = (yp - yoff) / 8;
if (xp >= 0 && xp < 40) if (xp >= 0 && xp < 40)
{ {
@ -42,10 +39,7 @@ int towerclass::at(int xp, int yp, int yoff)
} }
else else
{ {
yp = yp * 8; yp = (yp*8 + yoff) / 8;
yp += yoff;
yoff = yp % 8;
yp = (yp - yoff) / 8;
while (yp < 0) yp += 700; while (yp < 0) yp += 700;
while (yp >= 700) yp -= 700; while (yp >= 700) yp -= 700;
@ -67,10 +61,7 @@ int towerclass::at(int xp, int yp, int yoff)
int towerclass::miniat(int xp, int yp, int yoff) int towerclass::miniat(int xp, int yp, int yoff)
{ {
yp = yp * 8; yp = (yp*8 + yoff) / 8;
yp += yoff;
yoff = yp % 8;
yp = (yp - yoff) / 8;
while (yp < 0) yp += 100; while (yp < 0) yp += 100;
while (yp >= 100) yp -= 100; while (yp >= 100) yp -= 100;

View file

@ -185,7 +185,7 @@ std::string UtilityClass::timestring( int t )
{ {
//given a time t in frames, return a time in seconds //given a time t in frames, return a time in seconds
std::string tempstring = ""; std::string tempstring = "";
int temp = (t - (t % 30)) / 30; int temp = t / 30;
if (temp < 60) //less than one minute if (temp < 60) //less than one minute
{ {
t = t % 30; t = t % 30;
@ -193,7 +193,7 @@ std::string UtilityClass::timestring( int t )
} }
else else
{ {
int temp2 = (temp - (temp % 60)) / 60; int temp2 = temp / 60;
temp = temp % 60; temp = temp % 60;
t = t % 30; t = t % 30;
tempstring = String(temp2) + ":" + twodigits(temp) + ":" + twodigits(t * 100 / 30); tempstring = String(temp2) + ":" + twodigits(temp) + ":" + twodigits(t * 100 / 30);