Skip to content

Commit

Permalink
Check if inside scroll limits before repair/RTB
Browse files Browse the repository at this point in the history
No more getting trolled by micro-AI at the start of Gamma 6.
  • Loading branch information
KJeff01 authored and past-due committed Jun 28, 2024
1 parent d6e1440 commit 93a63ce
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 8 deletions.
24 changes: 20 additions & 4 deletions src/display.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1059,10 +1059,26 @@ void resetScroll()
scrollDirUpDown = 0;
}

// Checks if coordinate is inside scroll limits, returns false if not.
bool CheckInScrollLimits(const int &xPos, const int &yPos)
{
int minX = world_coord(scrollMinX);
int maxX = world_coord(scrollMaxX - 1);
int minY = world_coord(scrollMinY);
int maxY = world_coord(scrollMaxY - 1);

if ((xPos < minX) || (xPos >= maxX) || (yPos < minY) || (yPos >= maxY))
{
return false;
}

return true;
}

// Check a coordinate is within the scroll limits, SDWORD version.
// Returns true if edge hit.
//
bool CheckInScrollLimits(SDWORD *xPos, SDWORD *zPos)
bool CheckInScrollLimitsCamera(SDWORD *xPos, SDWORD *zPos)
{
bool EdgeHit = false;
SDWORD minX, minY, maxX, maxY;
Expand Down Expand Up @@ -1105,7 +1121,7 @@ bool CheckScrollLimits()
{
SDWORD xp = playerPos.p.x;
SDWORD zp = playerPos.p.z;
bool ret = CheckInScrollLimits(&xp, &zp);
bool ret = CheckInScrollLimitsCamera(&xp, &zp);

playerPos.p.x = xp;
playerPos.p.z = zp;
Expand Down Expand Up @@ -1705,13 +1721,13 @@ static void dealWithLMBStructure(STRUCTURE *psStructure, SELECTION_TYPE selectio
{
bool ownStruct = (psStructure->player == selectedPlayer);
const bool isSpectator = bMultiPlayer && NetPlay.players[selectedPlayer].isSpectator;

if (isSpectator)
{
printStructureInfo(psStructure);
return;
}

if (selectedPlayer < MAX_PLAYERS && !aiCheckAlliances(psStructure->player, selectedPlayer))
{
/* We've clicked on an enemy building */
Expand Down
3 changes: 2 additions & 1 deletion src/display.h
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,8 @@ extern void shakeStop();
// reset the input state
void resetInput();

bool CheckInScrollLimits(SDWORD *xPos, SDWORD *zPos);
bool CheckInScrollLimits(const int &xPos, const int &yPos);
bool CheckInScrollLimitsCamera(SDWORD *xPos, SDWORD *zPos);
bool CheckScrollLimits();

BASE_OBJECT *mouseTarget();
Expand Down
21 changes: 18 additions & 3 deletions src/order.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1682,6 +1682,10 @@ void orderDroidBase(DROID *psDroid, DROID_ORDER_DATA *psOrder)
if (psStruct->pStructureType->type == REF_HQ)
{
Vector2i pos = psStruct->pos.xy();
if (!CheckInScrollLimits(pos.x, pos.y))
{
continue;
}

psDroid->order = *psOrder;
// Find a place to land for vtols. And Transporters in a multiPlay game.
Expand All @@ -1706,12 +1710,12 @@ void orderDroidBase(DROID *psDroid, DROID_ORDER_DATA *psOrder)
int iDY = getLandingY(psDroid->player);
Vector2i startPos = getPlayerStartPosition(psDroid->player);

if (iDX && iDY)
if (iDX && iDY && CheckInScrollLimits(iDX, iDY))
{
psDroid->order = *psOrder;
actionDroid(psDroid, DACTION_MOVE, iDX, iDY);
}
else if (bMultiPlayer && (startPos.x != 0 && startPos.y != 0))
else if (bMultiPlayer && (startPos.x != 0 && startPos.y != 0) && CheckInScrollLimits(startPos.x, startPos.y))
{
psDroid->order = *psOrder;
actionDroid(psDroid, DACTION_MOVE, startPos.x, startPos.y);
Expand Down Expand Up @@ -3391,11 +3395,18 @@ static inline RtrBestResult decideWhereToRepairAndBalance(DROID *psDroid)
{
if (psStruct->pStructureType->type == REF_HQ)
{
psHq = psStruct;
if (CheckInScrollLimits(psStruct->pos.x, psStruct->pos.y))
{
psHq = psStruct;
}
continue;
}
if (psStruct->pStructureType->type == REF_REPAIR_FACILITY && psStruct->status == SS_BUILT)
{
if (!CheckInScrollLimits(psStruct->pos.x, psStruct->pos.y))
{
continue;
}
thisDistToRepair = droidSqDist(psDroid, psStruct);
if (thisDistToRepair <= 0)
{
Expand Down Expand Up @@ -3428,6 +3439,10 @@ static inline RtrBestResult decideWhereToRepairAndBalance(DROID *psDroid)
if ((psCurr->droidType == DROID_REPAIR || psCurr->droidType == DROID_CYBORG_REPAIR)
&& secondaryGetState(psCurr, DSO_ACCEPT_RETREP))
{
if (!CheckInScrollLimits(psCurr->pos.x, psCurr->pos.y))
{
continue;
}
thisDistToRepair = droidSqDist(psDroid, psCurr);
if (thisDistToRepair <= 0)
{
Expand Down

0 comments on commit 93a63ce

Please sign in to comment.