From 891ca527f978a8dfe93081588de1d233e4b030b8 Mon Sep 17 00:00:00 2001 From: Misa Date: Fri, 24 Sep 2021 17:21:46 -0700 Subject: [PATCH] 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. --- desktop_version/src/CustomLevels.cpp | 4 ++-- desktop_version/src/Editor.cpp | 20 ++++++++++---------- desktop_version/src/Entity.cpp | 3 +-- desktop_version/src/Game.cpp | 6 +++--- desktop_version/src/Logic.cpp | 4 ++-- desktop_version/src/Tower.cpp | 15 +++------------ desktop_version/src/UtilityClass.cpp | 4 ++-- 7 files changed, 23 insertions(+), 33 deletions(-) diff --git a/desktop_version/src/CustomLevels.cpp b/desktop_version/src/CustomLevels.cpp index 0bd06b9c..6aabccbb 100644 --- a/desktop_version/src/CustomLevels.cpp +++ b/desktop_version/src/CustomLevels.cpp @@ -871,8 +871,8 @@ void customlevelclass::findstartpoint(void) else { //Start point spawn - int tx=(customentities[testeditor].x-(customentities[testeditor].x%40))/40; - int ty=(customentities[testeditor].y-(customentities[testeditor].y%30))/30; + int tx=customentities[testeditor].x/40; + int ty=customentities[testeditor].y/30; game.edsavex = ((customentities[testeditor].x%40)*8)-4; game.edsavey = (customentities[testeditor].y%30)*8; game.edsaverx = 100+tx; diff --git a/desktop_version/src/Editor.cpp b/desktop_version/src/Editor.cpp index 850bf2e0..eeacf4d8 100644 --- a/desktop_version/src/Editor.cpp +++ b/desktop_version/src/Editor.cpp @@ -721,7 +721,7 @@ void editorrender(void) if(temp2==i) { 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 { @@ -854,7 +854,7 @@ void editorrender(void) 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, - "("+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 { @@ -2720,8 +2720,8 @@ void editorinput(void) //if() on screen if(customentities[i].t==16 && testeditor==-1) { - int tx=(customentities[i].x-(customentities[i].x%40))/40; - int ty=(customentities[i].y-(customentities[i].y%30))/30; + int tx=customentities[i].x/40; + int ty=customentities[i].y/30; if(tx==ed.levx && ty==ed.levy) { testeditor=i; @@ -2737,8 +2737,8 @@ void editorinput(void) //if() on screen if(customentities[i].t==10 && testeditor==-1) { - int tx=(customentities[i].x-(customentities[i].x%40))/40; - int ty=(customentities[i].y-(customentities[i].y%30))/30; + int tx=customentities[i].x/40; + int ty=customentities[i].y/30; if(tx==ed.levx && ty==ed.levy) { testeditor=i; @@ -2758,8 +2758,8 @@ void editorinput(void) if(startpoint==0) { //Checkpoint spawn - int tx=(customentities[testeditor].x-(customentities[testeditor].x%40))/40; - int ty=(customentities[testeditor].y-(customentities[testeditor].y%30))/30; + int tx=customentities[testeditor].x/40; + int ty=customentities[testeditor].y/30; game.edsavex = (customentities[testeditor].x%40)*8 - 4; game.edsavey = (customentities[testeditor].y%30)*8; game.edsaverx = 100+tx; @@ -2779,8 +2779,8 @@ void editorinput(void) else { //Start point spawn - int tx=(customentities[testeditor].x-(customentities[testeditor].x%40))/40; - int ty=(customentities[testeditor].y-(customentities[testeditor].y%30))/30; + int tx=customentities[testeditor].x/40; + int ty=customentities[testeditor].y/30; game.edsavex = (customentities[testeditor].x%40)*8 - 4; game.edsavey = (customentities[testeditor].y%30)*8; game.edsaverx = 100+tx; diff --git a/desktop_version/src/Entity.cpp b/desktop_version/src/Entity.cpp index 4bdf8379..fba49cb1 100644 --- a/desktop_version/src/Entity.cpp +++ b/desktop_version/src/Entity.cpp @@ -4076,8 +4076,7 @@ int entityclass::checkactivity(void) int entityclass::getgridpoint( int t ) { - t = (t - (t % 8)) / 8; - return t; + return t / 8; } bool entityclass::checkplatform(const SDL_Rect& temprect, int* px, int* py) diff --git a/desktop_version/src/Game.cpp b/desktop_version/src/Game.cpp index d3a59d25..674034ca 100644 --- a/desktop_version/src/Game.cpp +++ b/desktop_version/src/Game.cpp @@ -5779,7 +5779,7 @@ std::string Game::partimestring(void) std::string tempstring = ""; 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 { @@ -5794,7 +5794,7 @@ std::string Game::resulttimestring(void) std::string tempstring = ""; if (timetrialresulttime >= 60) { - tempstring = help.twodigits(int((timetrialresulttime - (timetrialresulttime % 60)) / 60)) + ":" + tempstring = help.twodigits(timetrialresulttime / 60) + ":" + help.twodigits(timetrialresulttime % 60); } else @@ -5811,7 +5811,7 @@ std::string Game::timetstring( int t ) std::string tempstring = ""; 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 { diff --git a/desktop_version/src/Logic.cpp b/desktop_version/src/Logic.cpp index cb6f35e4..00358146 100644 --- a/desktop_version/src/Logic.cpp +++ b/desktop_version/src/Logic.cpp @@ -1250,8 +1250,8 @@ void gamelogic(void) int edi=obj.entities[game.edteleportent].behave; int edj=obj.entities[game.edteleportent].para; int edi2, edj2; - edi2 = (edi-(edi%40))/40; - edj2 = (edj-(edj%30))/30; + edi2 = edi/40; + edj2 = edj/30; map.warpto(100+edi2, 100+edj2, obj.getplayer(), edi%40, (edj%30)+2); game.teleport = false; diff --git a/desktop_version/src/Tower.cpp b/desktop_version/src/Tower.cpp index 0c551196..eaa622d5 100644 --- a/desktop_version/src/Tower.cpp +++ b/desktop_version/src/Tower.cpp @@ -20,10 +20,7 @@ towerclass::towerclass(void) int towerclass::backat(int xp, int yp, int yoff) { - yp = yp * 8; - yp += yoff; - yoff = yp % 8; - yp = (yp - yoff) / 8; + yp = (yp*8 + yoff) / 8; if (xp >= 0 && xp < 40) { @@ -42,10 +39,7 @@ int towerclass::at(int xp, int yp, int yoff) } else { - yp = yp * 8; - yp += yoff; - yoff = yp % 8; - yp = (yp - yoff) / 8; + yp = (yp*8 + yoff) / 8; while (yp < 0) 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) { - yp = yp * 8; - yp += yoff; - yoff = yp % 8; - yp = (yp - yoff) / 8; + yp = (yp*8 + yoff) / 8; while (yp < 0) yp += 100; while (yp >= 100) yp -= 100; diff --git a/desktop_version/src/UtilityClass.cpp b/desktop_version/src/UtilityClass.cpp index 0b58b5ef..7a576776 100644 --- a/desktop_version/src/UtilityClass.cpp +++ b/desktop_version/src/UtilityClass.cpp @@ -185,7 +185,7 @@ std::string UtilityClass::timestring( int t ) { //given a time t in frames, return a time in seconds std::string tempstring = ""; - int temp = (t - (t % 30)) / 30; + int temp = t / 30; if (temp < 60) //less than one minute { t = t % 30; @@ -193,7 +193,7 @@ std::string UtilityClass::timestring( int t ) } else { - int temp2 = (temp - (temp % 60)) / 60; + int temp2 = temp / 60; temp = temp % 60; t = t % 30; tempstring = String(temp2) + ":" + twodigits(temp) + ":" + twodigits(t * 100 / 30);