Skip to content

Commit

Permalink
Prefer dynamic_cast over type id with casting
Browse files Browse the repository at this point in the history
  • Loading branch information
mmatyas committed May 30, 2024
1 parent e1a39d0 commit 1398a2c
Showing 1 changed file with 19 additions and 28 deletions.
47 changes: 19 additions & 28 deletions src/smw/ai.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "gamemodes/Tag.h"
#include "objects/IO_FlameCannon.h"

#include "objects/moving/MO_BonusHouseChest.h"
#include "objects/moving/MO_Boomerang.h"
#include "objects/moving/MO_CarriedObject.h"
#include "objects/moving/MO_Coin.h"
Expand Down Expand Up @@ -105,33 +106,24 @@ void CPlayerAI::Init()
//Scan yoshi's egg mode objects to make sure that we ignore eggs without matching yoshis
if (game_values.gamemode->gamemode == game_mode_eggs) {
bool fYoshi[4] = {false, false, false, false};

//Scan Yoshis to see which ones are present
for (const std::unique_ptr<CObject>& obj : objectcontainer[1].list()) {
if (object_moving == obj->getObjectType()) {
IO_MovingObject * movingobject = (IO_MovingObject*)obj.get();

if (movingobject_yoshi == movingobject->getMovingObjectType()) {
MO_Yoshi * yoshi = (MO_Yoshi*)movingobject;
fYoshi[yoshi->getColor()] = true;
}
if (auto* yoshi = dynamic_cast<MO_Yoshi*>(obj.get())) {
fYoshi[yoshi->getColor()] = true;
}
}

//Now scan eggs and ignore any egg that doesn't have a yoshi
for (const std::unique_ptr<CObject>& obj : objectcontainer[1].list()) {
if (object_moving == obj->getObjectType()) {
IO_MovingObject * movingobject = (IO_MovingObject*)obj.get();

if (movingobject_egg == movingobject->getMovingObjectType()) {
CO_Egg * egg = (CO_Egg*)movingobject;
if (!fYoshi[egg->getColor()]) {
AttentionObject * ao = new AttentionObject();
ao->iID = egg->iNetworkID;
ao->iType = 1; //Ignore this object
ao->iTimer = 0; //Ignore it forever

attentionObjects[ao->iID] = ao;
}
if (auto* egg = dynamic_cast<CO_Egg*>(obj.get())) {
if (!fYoshi[egg->getColor()]) {
AttentionObject * ao = new AttentionObject();
ao->iID = egg->iNetworkID;
ao->iType = 1; //Ignore this object
ao->iTimer = 0; //Ignore it forever

attentionObjects[ao->iID] = ao;
}
}
}
Expand Down Expand Up @@ -367,17 +359,17 @@ void CPlayerAI::Think(COutputControl * playerKeys)
if (!pPlayer->inair) playerKeys->game_down.fDown = true;
}

if (goal->getObjectType() == object_moving && ((IO_MovingObject*)goal)->getMovingObjectType() == movingobject_egg)
if (dynamic_cast<CO_Egg*>(goal))
playerKeys->game_turbo.fDown = true;
else if (goal->getObjectType() == object_moving && ((IO_MovingObject*) goal)->getMovingObjectType() == movingobject_star && pPlayer->throw_star == 0)
else if (dynamic_cast<CO_Star*>(goal) && pPlayer->throw_star == 0)
playerKeys->game_turbo.fDown = true;
else if (goal->getObjectType() == object_moving && ((IO_MovingObject*) goal)->getMovingObjectType() == movingobject_flag)
else if (dynamic_cast<CO_Flag*>(goal))
playerKeys->game_turbo.fDown = true;

//Drop current item if we're going after another carried item
if (carriedItem) {
if (goal->getObjectType() == object_moving) {
MovingObjectType goalobjecttype = ((IO_MovingObject*)goal)->getMovingObjectType();
if (auto* movingobject = dynamic_cast<IO_MovingObject*>(goal)) {
MovingObjectType goalobjecttype = movingobject->getMovingObjectType();
if (goalobjecttype == movingobject_egg || goalobjecttype == movingobject_flag ||
goalobjecttype == movingobject_star || goalobjecttype == movingobject_phantokey) {
MovingObjectType carriedobjecttype = carriedItem->getMovingObjectType();
Expand All @@ -392,7 +384,7 @@ void CPlayerAI::Think(COutputControl * playerKeys)
}

//Open treasure chests in bonus houses in world mode
if (goal->getObjectType() == object_moving && ((IO_MovingObject*)goal)->getMovingObjectType() == movingobject_treasurechest)
if (dynamic_cast<MO_BonusHouseChest*>(goal))
playerKeys->game_turbo.fPressed = true;
} else if (actionType == 2) { //Evade Threat
CObject * threat = nearestObjects.threat;
Expand Down Expand Up @@ -521,8 +513,7 @@ void CPlayerAI::Think(COutputControl * playerKeys)
}

//Drop live bombs if they are not yours
if (carriedobjecttype == movingobject_bomb) {
CO_Bomb * bomb = (CO_Bomb*)carriedItem;
if (auto* bomb = dynamic_cast<CO_Bomb*>(carriedItem)) {
if (bomb->iTeamID != pPlayer->teamID)
playerKeys->game_turbo.fDown = false;
} else if (carriedobjecttype == movingobject_carried || carriedobjecttype == movingobject_throwbox) { //Drop springs,spikes,shoes
Expand Down

0 comments on commit 1398a2c

Please sign in to comment.