Skip to content

Commit e25f968

Browse files
committed
refactor(radar): Simplify function Radar::addObject (#1893)
1 parent b68c0b9 commit e25f968

File tree

3 files changed

+127
-113
lines changed

3 files changed

+127
-113
lines changed

Core/GameEngine/Include/Common/Radar.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -196,8 +196,8 @@ class Radar : public Snapshot,
196196
Bool tryEvent( RadarEventType event, const Coord3D *pos ); ///< try to make a "stealth" event
197197

198198
// adding and removing objects from the radar
199-
virtual RadarObjectType addObject( Object *obj ); ///< add object to radar
200-
virtual RadarObjectType removeObject( Object *obj ); ///< remove object from radar
199+
virtual RadarObjectType addObject( Object *obj ); ///< add object to radar
200+
virtual RadarObjectType removeObject( Object *obj ); ///< remove object from radar
201201

202202
// radar options
203203
void hide( Int playerIndex, Bool hide ) { m_radarHidden[playerIndex] = hide; } ///< hide/show the radar
@@ -252,6 +252,9 @@ class Radar : public Snapshot,
252252
// search the object list for an object that maps to the given logical radar coordinates
253253
Object *searchListForRadarLocationMatch( RadarObject *listHead, ICoord2D *radarMatch );
254254

255+
void linkRadarObject( RadarObject *newObj, RadarObject **list );
256+
void assignObjectColorToRadarObject( RadarObject *radarObj, Object *obj );
257+
255258
Bool m_radarHidden[MAX_PLAYER_COUNT]; ///< true when radar is not visible
256259
Bool m_radarForceOn[MAX_PLAYER_COUNT]; ///< true when radar is forced to be on
257260
RadarObject *m_objectList; ///< list of objects in the radar

Core/GameEngine/Source/Common/System/Radar.cpp

Lines changed: 120 additions & 111 deletions
Original file line numberDiff line numberDiff line change
@@ -426,50 +426,7 @@ RadarObjectType Radar::addObject( Object *obj )
426426
newObj->friend_setObject( obj );
427427

428428
// set color for this object on the radar
429-
const Player *player = obj->getControllingPlayer();
430-
Player *clientPlayer = rts::getObservedOrLocalPlayer();
431-
Bool useIndicatorColor = true;
432-
433-
if( obj->isKindOf( KINDOF_DISGUISER ) )
434-
{
435-
//Because we have support for disguised units pretending to be units from another
436-
//team, we need to intercept it here and make sure it's rendered appropriately
437-
//based on which client is rendering it.
438-
StealthUpdate *update = obj->getStealth();
439-
if( update )
440-
{
441-
if( update->isDisguised() )
442-
{
443-
Player *disguisedPlayer = ThePlayerList->getNthPlayer( update->getDisguisedPlayerIndex() );
444-
if( player->getRelationship( clientPlayer->getDefaultTeam() ) != ALLIES && clientPlayer->isPlayerActive() )
445-
{
446-
//Neutrals and enemies will see this disguised unit as the team it's disguised as.
447-
player = disguisedPlayer;
448-
if( player )
449-
useIndicatorColor = false;
450-
}
451-
//Otherwise, the color will show up as the team it really belongs to (already set above).
452-
}
453-
}
454-
}
455-
456-
if( obj->getContain() )
457-
{
458-
// To handle Stealth garrison, ask containers what color they are drawing with to the local player.
459-
// Local is okay because radar display is not synced.
460-
player = obj->getContain()->getApparentControllingPlayer( clientPlayer );
461-
if( player )
462-
useIndicatorColor = false;
463-
}
464-
465-
if( useIndicatorColor || (player == NULL) )
466-
{
467-
newObj->setColor( obj->getIndicatorColor() );
468-
}
469-
else
470-
{
471-
newObj->setColor( player->getPlayerColor() );
472-
}
429+
assignObjectColorToRadarObject( newObj, obj );
473430

474431
// set a chunk of radar data in the object
475432
obj->friend_setRadarData( newObj );
@@ -491,73 +448,7 @@ RadarObjectType Radar::addObject( Object *obj )
491448
}
492449

493450
// link object to master list at the head of it's priority section
494-
if( *list == NULL )
495-
*list = newObj; // trivial case, an empty list
496-
else
497-
{
498-
RadarPriorityType prevPriority, currPriority;
499-
RadarObject *currObject, *prevObject, *nextObject;
500-
501-
prevObject = NULL;
502-
prevPriority = RADAR_PRIORITY_INVALID;
503-
for( currObject = *list; currObject; currObject = nextObject )
504-
{
505-
506-
// get the next object
507-
nextObject = currObject->friend_getNext();
508-
509-
// get the priority of this entry in the list (currPriority)
510-
currPriority = currObject->friend_getObject()->getRadarPriority();
511-
512-
//
513-
// if there is no previous object, or the previous priority is less than the
514-
// our new priority, and the current object in the list has a priority
515-
// higher than our equal to our own we need to be inserted here
516-
//
517-
if( (prevObject == NULL || prevPriority < newPriority ) &&
518-
(currPriority >= newPriority) )
519-
{
520-
521-
// insert into the list just ahead of currObject
522-
if( prevObject )
523-
{
524-
525-
// the new entry next points to what the previous one used to point to
526-
newObj->friend_setNext( prevObject->friend_getNext() );
527-
528-
// the previous one next now points to the new entry
529-
prevObject->friend_setNext( newObj );
530-
531-
}
532-
else
533-
{
534-
535-
// the new object next points to the current object
536-
newObj->friend_setNext( currObject );
537-
538-
// new list head is now newObj
539-
*list = newObj;
540-
541-
}
542-
543-
break; // exit for, stop the insert
544-
545-
}
546-
else if( nextObject == NULL )
547-
{
548-
549-
// at the end of the list, put object here
550-
currObject->friend_setNext( newObj );
551-
552-
}
553-
554-
// our current object is now the previous object
555-
prevObject = currObject;
556-
prevPriority = currPriority;
557-
558-
}
559-
560-
}
451+
linkRadarObject(newObj, list);
561452

562453
return objectType;
563454
}
@@ -1606,3 +1497,121 @@ Bool Radar::isPriorityVisible( RadarPriorityType priority )
16061497
}
16071498

16081499
}
1500+
1501+
// ------------------------------------------------------------------------------------------------
1502+
// ------------------------------------------------------------------------------------------------
1503+
void Radar::linkRadarObject( RadarObject *newObj, RadarObject **list )
1504+
{
1505+
if( *list == NULL )
1506+
{
1507+
// trivial case, an empty list
1508+
*list = newObj;
1509+
return;
1510+
}
1511+
1512+
RadarPriorityType newPriority = newObj->friend_getObject()->getRadarPriority();
1513+
RadarPriorityType prevPriority;
1514+
RadarPriorityType currPriority;
1515+
RadarObject *currObject;
1516+
RadarObject *prevObject;
1517+
RadarObject *nextObject;
1518+
1519+
DEBUG_ASSERTCRASH(newObj->friend_getNext() == NULL, ("newObj->friend_getNext is not NULL"));
1520+
1521+
prevObject = NULL;
1522+
prevPriority = RADAR_PRIORITY_INVALID;
1523+
for( currObject = *list; currObject; currObject = nextObject )
1524+
{
1525+
// get the next object
1526+
nextObject = currObject->friend_getNext();
1527+
1528+
// get the priority of this entry in the list (currPriority)
1529+
currPriority = currObject->friend_getObject()->getRadarPriority();
1530+
1531+
//
1532+
// if there is no previous object, or the previous priority is less than the
1533+
// our new priority, and the current object in the list has a priority
1534+
// higher than our equal to our own we need to be inserted here
1535+
//
1536+
if( (prevObject == NULL || prevPriority < newPriority ) && (currPriority >= newPriority) )
1537+
{
1538+
// insert into the list just ahead of currObject
1539+
if( prevObject )
1540+
{
1541+
// the new entry next points to what the previous one used to point to
1542+
newObj->friend_setNext( prevObject->friend_getNext() );
1543+
1544+
// the previous one next now points to the new entry
1545+
prevObject->friend_setNext( newObj );
1546+
}
1547+
else
1548+
{
1549+
// the new object next points to the current object
1550+
newObj->friend_setNext( currObject );
1551+
1552+
// new list head is now newObj
1553+
*list = newObj;
1554+
}
1555+
break;
1556+
}
1557+
else if( nextObject == NULL )
1558+
{
1559+
// at the end of the list, put object here
1560+
currObject->friend_setNext( newObj );
1561+
}
1562+
1563+
// our current object is now the previous object
1564+
prevObject = currObject;
1565+
prevPriority = currPriority;
1566+
}
1567+
}
1568+
1569+
// ------------------------------------------------------------------------------------------------
1570+
// ------------------------------------------------------------------------------------------------
1571+
void Radar::assignObjectColorToRadarObject( RadarObject *radarObj, Object *obj )
1572+
{
1573+
const Player *player = obj->getControllingPlayer();
1574+
Player *clientPlayer = rts::getObservedOrLocalPlayer();
1575+
Bool useIndicatorColor = true;
1576+
1577+
if( obj->isKindOf( KINDOF_DISGUISER ) )
1578+
{
1579+
//Because we have support for disguised units pretending to be units from another
1580+
//team, we need to intercept it here and make sure it's rendered appropriately
1581+
//based on which client is rendering it.
1582+
StealthUpdate *update = obj->getStealth();
1583+
if( update )
1584+
{
1585+
if( update->isDisguised() )
1586+
{
1587+
Player *disguisedPlayer = ThePlayerList->getNthPlayer( update->getDisguisedPlayerIndex() );
1588+
if( player->getRelationship( clientPlayer->getDefaultTeam() ) != ALLIES && clientPlayer->isPlayerActive() )
1589+
{
1590+
//Neutrals and enemies will see this disguised unit as the team it's disguised as.
1591+
player = disguisedPlayer;
1592+
if( player )
1593+
useIndicatorColor = false;
1594+
}
1595+
//Otherwise, the color will show up as the team it really belongs to (already set above).
1596+
}
1597+
}
1598+
}
1599+
1600+
if( obj->getContain() )
1601+
{
1602+
// To handle Stealth garrison, ask containers what color they are drawing with to the local player.
1603+
// Local is okay because radar display is not synced.
1604+
player = obj->getContain()->getApparentControllingPlayer( clientPlayer );
1605+
if( player )
1606+
useIndicatorColor = false;
1607+
}
1608+
1609+
if( useIndicatorColor || (player == NULL) )
1610+
{
1611+
radarObj->setColor( obj->getIndicatorColor() );
1612+
}
1613+
else
1614+
{
1615+
radarObj->setColor( player->getPlayerColor() );
1616+
}
1617+
}

Core/GameEngineDevice/Source/W3DDevice/Common/System/W3DRadar.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1018,6 +1018,8 @@ RadarObjectType W3DRadar::addObject( Object* obj )
10181018
}
10191019

10201020
//-------------------------------------------------------------------------------------------------
1021+
// TheSuperHackers @bugfix xezon 05/07/2025 Now removes the cached hero immediately because
1022+
// otherwise the object pointer could be dangling and used for a bit too long.
10211023
//-------------------------------------------------------------------------------------------------
10221024
RadarObjectType W3DRadar::removeObject( Object* obj )
10231025
{

0 commit comments

Comments
 (0)