Skip to content

Reduce code duplication#233

Merged
Asdow merged 14 commits into
masterfrom
CenterCellCoords
Oct 7, 2023
Merged

Reduce code duplication#233
Asdow merged 14 commits into
masterfrom
CenterCellCoords

Conversation

@Asdow
Copy link
Copy Markdown
Contributor

@Asdow Asdow commented Oct 4, 2023

This came about when I was looking at display cover functionality, again, and noticed that in the hot loop, we were using functions CenterX & CenterY to calculate coordinates for X and Y separately, duplicating work.

While going through the code for references to these functions, also noticed a lack of a utility function for calculating direction based on center coordinates, even though there was a clear need for one.

I'd like to chip away at the stupid amount of duplicate code we have and maybe this could serve as a starting point?

@Asdow Asdow requested review from majcosta and rftrdev October 4, 2023 20:25
Copy link
Copy Markdown
Contributor

@majcosta majcosta left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Comment thread Tactical/Soldier Control.cpp Outdated
return(atan8( sXPos2, sYPos2, sXPos, sYPos ));
}

INT16 GetDirectionFromCenterCellXYGridNo(INT32 sGridNoDest, INT32 sGridNoSrc)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why the 's' prefix?

Comment thread Tactical/Rotting Corpses.cpp Outdated
Comment thread Tactical/Weapons.cpp
if ( WillExplosiveWeaponFail( pSoldier, pObjHand ) )
{
INT16 sX, sY;
ConvertGridNoToCenterCellXY(pSoldier->sGridNo, &sX, &sY);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Having this next to sX and sY declaration is more readable, but are we sure none of the functions between here and the old CenterX/Y calls have any side-effects on pSoldier-sGridNo? (ditto for similar situations below)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IgniteExplosion and AddItemToPool have INT32 sGridNo as a parameter and at that point the functions have their own private copies of pSoldier->sGridNo instead of a reference, right?

Other functions, at least in this case do not use pSoldier->sGridNo at all, but good point. I should probably check if an unintended change can happen elsewhere where I did a similar thing.

Asdow added 5 commits October 6, 2023 20:24
ConvertGridNoToCenterCellXY and ConvertMapPosToWorldTileCenter do the exact same thing
Both functions calculate the same thing
The values end up being squared anyways making these unnecessary
@Asdow Asdow marked this pull request as ready for review October 7, 2023 12:13
@Asdow Asdow merged commit b8a870d into master Oct 7, 2023
@Asdow Asdow deleted the CenterCellCoords branch October 7, 2023 12:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants