Skip to content

Commit

Permalink
objmem: Add transferFlagPositionToPlayer
Browse files Browse the repository at this point in the history
  • Loading branch information
past-due committed Nov 2, 2023
1 parent 6b9557a commit 4b38df6
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 9 deletions.
44 changes: 35 additions & 9 deletions src/objmem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -671,33 +671,59 @@ void addFlagPosition(FLAG_POSITION *psFlagPosToAdd)
apsFlagPosLists[psFlagPosToAdd->player] = psFlagPosToAdd;
}

/* Remove a Flag Position from the Lists */
void removeFlagPosition(FLAG_POSITION *psDel)
// Remove it from the list, but don't delete it!
static bool removeFlagPositionFromList(FLAG_POSITION *psRemove)
{
FLAG_POSITION *psPrev = nullptr, *psCurr;

ASSERT_OR_RETURN(, psDel != nullptr, "Invalid Flag Position pointer");
ASSERT_OR_RETURN(false, psRemove != nullptr, "Invalid Flag Position pointer");

if (apsFlagPosLists[psDel->player] == psDel)
if (apsFlagPosLists[psRemove->player] == psRemove)
{
apsFlagPosLists[psDel->player] = apsFlagPosLists[psDel->player]->psNext;
free(psDel);
apsFlagPosLists[psRemove->player] = apsFlagPosLists[psRemove->player]->psNext;
return true;
}
else
{
for (psCurr = apsFlagPosLists[psDel->player]; (psCurr != psDel) &&
(psCurr != nullptr); psCurr = psCurr->psNext)
for (psCurr = apsFlagPosLists[psRemove->player]; (psCurr != psRemove) &&
(psCurr != nullptr); psCurr = psCurr->psNext)
{
psPrev = psCurr;
}
if (psCurr != nullptr)
{
psPrev->psNext = psCurr->psNext;
free(psCurr);
return true;
}
}

return false;
}

/* Remove a Flag Position from the Lists */
void removeFlagPosition(FLAG_POSITION *psDel)
{
ASSERT_OR_RETURN(, psDel != nullptr, "Invalid Flag Position pointer");

if (removeFlagPositionFromList(psDel))
{
free(psDel);
}
else
{
ASSERT(false, "Did not find flag position in expected list?");
}
}

/* Transfer a Flag Position to a new player */
void transferFlagPositionToPlayer(FLAG_POSITION *psFlagPos, UDWORD originalPlayer, UDWORD newPlayer)
{
ASSERT_OR_RETURN(, psFlagPos != nullptr, "Invalid Flag Position pointer");
ASSERT(originalPlayer == psFlagPos->player, "Unexpected originalPlayer (%" PRIu32 ") does not match current flagPos->player (%" PRIu32 ")", originalPlayer, psFlagPos->player);
ASSERT(removeFlagPositionFromList(psFlagPos), "Did not find flag position in expected list?");
psFlagPos->player = newPlayer;
addFlagPosition(psFlagPos);
}

// free all flag positions
void freeAllFlagPositions()
Expand Down
2 changes: 2 additions & 0 deletions src/objmem.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ bool createFlagPosition(FLAG_POSITION **ppsNew, UDWORD player);
void addFlagPosition(FLAG_POSITION *psFlagPosToAdd);
/* Remove a Flag Position from the Lists */
void removeFlagPosition(FLAG_POSITION *psDel);
/* Transfer a Flag Position to a new player */
void transferFlagPositionToPlayer(FLAG_POSITION *psFlagPos, UDWORD originalPlayer, UDWORD newPlayer);
// free all flag positions
void freeAllFlagPositions();

Expand Down

0 comments on commit 4b38df6

Please sign in to comment.