Skip to content
This repository has been archived by the owner on Apr 29, 2024. It is now read-only.

Commit

Permalink
[sea_ai] AIShipCannonController debug draw + some refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
espkk committed Jan 16, 2022
1 parent c1a3975 commit b748727
Show file tree
Hide file tree
Showing 12 changed files with 559 additions and 541 deletions.
8 changes: 4 additions & 4 deletions src/libs/sea_ai/include/ai_flow_graph.h
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,9 @@ class AIFlowGraph
float GetDistance(uint32_t dwPnt)
{
float fDist = 0.0f;
for (uint32_t i = 0; i < aPoints.size(); i++)
for (auto &aPoint : aPoints)
{
point_t *pP = &aPoints[i];
point_t *pP = &aPoint;
fDist += pP->fDistance;
if (dwPnt == pP->dwPnt)
return fDist;
Expand Down Expand Up @@ -462,8 +462,8 @@ inline AIFlowGraph::VectorPath *AIFlowGraph::GetVectorPath(size_t dwP1, size_t d
Path *pPath = GetPath(dwP1, dwP2);
if (pPath)
{
for (uint32_t i = 0; i < pPath->aPoints.size(); i++)
pVPath->AddPoint(GetPointPos(pPath->aPoints[i].dwPnt));
for (auto &aPoint : pPath->aPoints)
pVPath->AddPoint(GetPointPos(aPoint.dwPnt));
}
STORM_DELETE(pPath);
return pVPath;
Expand Down
32 changes: 16 additions & 16 deletions src/libs/sea_ai/src/ai_balls.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -337,13 +337,13 @@ uint32_t AIBalls::AttributeChanged(ATTRIBUTES *pAttributeChanged)
{
if (*pAttributeChanged == "clear")
{
for (uint32_t i = 0; i < aBallTypes.size(); i++)
for (auto &aBallType : aBallTypes)
{
BALL_TYPE *pBallsType = &aBallTypes[i];
BALL_TYPE *pBallsType = &aBallType;

for (uint32_t j = 0; j < pBallsType->Balls.size(); j++)
for (auto &Ball : pBallsType->Balls)
{
BALL_PARAMS *pBall = &pBallsType->Balls[j];
BALL_PARAMS *pBall = &Ball;

pBall->sBallEvent.clear();

Expand Down Expand Up @@ -413,10 +413,10 @@ uint64_t AIBalls::ProcessMessage(MESSAGE &message)
{
if (message.Long() == MSG_MODEL_RELEASE)
{
for (uint32_t i = 0; i < aBallTypes.size(); i++)
for (uint32_t j = 0; j < aBallTypes[i].Balls.size(); j++)
for (auto &aBallType : aBallTypes)
for (uint32_t j = 0; j < aBallType.Balls.size(); j++)
{
BALL_PARAMS *pBall = &aBallTypes[i].Balls[j];
BALL_PARAMS *pBall = &aBallType.Balls[j];

if (pBall->pParticle)
{
Expand Down Expand Up @@ -451,13 +451,13 @@ void BALL_PARAMS::Save(CSaveLoad *pSL)

void AIBalls::Save(CSaveLoad *pSL)
{
for (uint32_t i = 0; i < aBallTypes.size(); i++)
for (auto &aBallType : aBallTypes)
{
pSL->SaveDword(aBallTypes[i].Balls.size());
pSL->SaveDword(aBallType.Balls.size());

for (uint32_t j = 0; j < aBallTypes[i].Balls.size(); j++)
for (uint32_t j = 0; j < aBallType.Balls.size(); j++)
{
aBallTypes[i].Balls[j].Save(pSL);
aBallType.Balls[j].Save(pSL);
}
}
}
Expand Down Expand Up @@ -485,24 +485,24 @@ void BALL_PARAMS::Load(CSaveLoad *pSL)

void AIBalls::Load(CSaveLoad *pSL)
{
for (uint32_t i = 0; i < aBallTypes.size(); i++)
for (auto &aBallType : aBallTypes)
{
const uint32_t dwNum = pSL->LoadDword();

auto balls_size = std::size(aBallTypes[i].Balls);
aBallTypes[i].Balls.resize(balls_size + dwNum);
auto balls_size = std::size(aBallType.Balls);
aBallType.Balls.resize(balls_size + dwNum);
for (uint32_t j = 0; j < dwNum; j++)
{
// BALL_PARAMS * pB = &aBallTypes[i].Balls[aBallTypes[i].Balls.Add()];
BALL_PARAMS &pB = aBallTypes[i].Balls[balls_size + j];
BALL_PARAMS &pB = aBallType.Balls[balls_size + j];
pB.Load(pSL);
if (pB.pParticle)
{
pB.pParticle = nullptr;
if (auto eidParticle = EntityManager::GetEntityId("particles"))
{
pB.pParticle = (VPARTICLE_SYSTEM *)core.Send_Message(
eidParticle, "lsffffffl", PS_CREATE_RIC, (char *)aBallTypes[i].sParticleName.c_str(), pB.vPos.x,
eidParticle, "lsffffffl", PS_CREATE_RIC, (char *)aBallType.sParticleName.c_str(), pB.vPos.x,
pB.vPos.y, pB.vPos.z, 0.0f, 1.0f, 0.0f, 100000);
}
}
Expand Down
32 changes: 16 additions & 16 deletions src/libs/sea_ai/src/ai_fort.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ AIFort::AIFort()
AIFort::~AIFort()
{
pAIFort = nullptr;
for (uint32_t i = 0; i < aForts.size(); i++)
STORM_DELETE(aForts[i]);
for (auto &aFort : aForts)
STORM_DELETE(aFort);
}

bool AIFort::Init()
Expand Down Expand Up @@ -90,17 +90,17 @@ void AIFort::Execute(uint32_t Delta_Time)
auto fMinDistance = 1e10f;
auto vCPos = pC->GetPos();
const auto fMaxFireDistance = AICannon::CalcMaxFireDistance(vCPos.y, fSpeedV0, 0.35f); // FIX-ME
for (uint32_t j = 0; j < AIShip::AIShips.size(); j++)
if (!AIShip::AIShips[j]->isDead() &&
Helper.isEnemy(pF->GetACharacter(), AIShip::AIShips[j]->GetACharacter()))
for (auto &j : AIShip::AIShips)
if (!j->isDead() &&
Helper.isEnemy(pF->GetACharacter(), j->GetACharacter()))
{
const auto fDistance = AIShip::AIShips[j]->GetDistance(vCPos);
const auto fDistance = j->GetDistance(vCPos);
if (fDistance > fMaxFireDistance)
continue;
if (fDistance < fMinDistance)
{
fMinDistance = fDistance;
pFireAIShip = AIShip::AIShips[j];
pFireAIShip = j;
}
}
if (pFireAIShip)
Expand Down Expand Up @@ -265,10 +265,10 @@ void AIFort::AddFortHit(int32_t iCharacterIndex, CVECTOR &vHitPos)

AIFort::AI_FORT *AIFort::FindFort(entid_t eidModel)
{
for (int32_t i = 0; i < aForts.size(); i++)
for (auto &aFort : aForts)
{
if (EntityManager::GetEntityPointer(aForts[i]->GetModelEID()) == EntityManager::GetEntityPointer(eidModel))
return aForts[i];
if (EntityManager::GetEntityPointer(aFort->GetModelEID()) == EntityManager::GetEntityPointer(eidModel))
return aFort;
}
return nullptr;
}
Expand Down Expand Up @@ -491,15 +491,15 @@ CVECTOR AIFort::AI_FORT::GetAttackPoint(VAI_INNEROBJ *pObj)
void AIFort::Save(CSaveLoad *pSL)
{
pSL->SaveFloat(fMinCannonDamageDistance);
for (uint32_t i = 0; i < aForts.size(); i++)
aForts[i]->Save(pSL);
for (auto &aFort : aForts)
aFort->Save(pSL);
}

void AIFort::Load(CSaveLoad *pSL)
{
fMinCannonDamageDistance = pSL->LoadFloat();
for (uint32_t i = 0; i < aForts.size(); i++)
aForts[i]->Load(pSL, GetId());
for (auto &aFort : aForts)
aFort->Load(pSL, GetId());
}

void AIFort::AI_FORT::Save(CSaveLoad *pSL)
Expand Down Expand Up @@ -553,6 +553,6 @@ void AIFort::AI_FORT::Load(CSaveLoad *pSL, entid_t eid)

void AIFort::Fire(const CVECTOR &vPos)
{
for (int32_t i = 0; i < aForts.size(); i++)
pShipsLights->AddDynamicLights(&aForts[i]->tmpObject, vPos);
for (auto &aFort : aForts)
pShipsLights->AddDynamicLights(&aFort->tmpObject, vPos);
}
61 changes: 29 additions & 32 deletions src/libs/sea_ai/src/ai_group.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,8 @@ void AIGroup::AddShip(entid_t eidShip, ATTRIBUTES *pACharacter, ATTRIBUTES *pASh

bool AIGroup::isMainGroup()
{
for (uint32_t i = 0; i < aGroupShips.size(); i++)
if (aGroupShips[i]->isMainCharacter())
for (auto &aGroupShip : aGroupShips)
if (aGroupShip->isMainCharacter())
return true;

return false;
Expand Down Expand Up @@ -124,20 +124,20 @@ void AIGroup::Execute(float fDeltaTime)
auto vNewGroupPos =
pG->vInitGroupPos + ((100.0f + FRAND(200.0f)) * CVECTOR(sinf(fNewAng), 0.0f, cosf(fNewAng)));

for (uint32_t i = 0; i < aGroupShips.size(); i++)
for (auto &aGroupShip : aGroupShips)
{
aGroupShips[i]->SetPos(vNewGroupPos);
aGroupShips[i]->CheckStartPosition();
aGroupShip->SetPos(vNewGroupPos);
aGroupShip->CheckStartPosition();
}
}
}

if (isMainGroup())
{
for (uint32_t i = 0; i < aGroupShips.size(); i++)
if (!aGroupShips[i]->isMainCharacter())
aGroupShips[i]->GetTaskController()->SetNewTask(PRIMARY_TASK, AITASK_DEFEND,
GetCommanderACharacter());
for (auto &aGroupShip : aGroupShips)
if (!aGroupShip->isMainCharacter())
aGroupShip->GetTaskController()->SetNewTask(PRIMARY_TASK, AITASK_DEFEND,
GetCommanderACharacter());
}

bFirstExecute = false;
Expand Down Expand Up @@ -165,21 +165,21 @@ void AIGroup::Execute(float fDeltaTime)
if (!isMainGroup())
{
auto fMinimalSpeed = 1e+10f;
for (uint32_t i = 0; i < aGroupShips.size(); i++)
for (auto &aGroupShip : aGroupShips)
{
const auto fCurSpeed = aGroupShips[i]->GetShipBasePointer()->GetCurrentSpeed();
const auto fCurSpeed = aGroupShip->GetShipBasePointer()->GetCurrentSpeed();
if (fCurSpeed < fMinimalSpeed)
fMinimalSpeed = fCurSpeed;
}

const auto bSetFixedSpeed = sCommand == "move";

for (uint32_t i = 0; i < aGroupShips.size(); i++)
aGroupShips[i]->GetShipBasePointer()->SetFixedSpeed(bSetFixedSpeed, fMinimalSpeed);
for (auto &aGroupShip : aGroupShips)
aGroupShip->GetShipBasePointer()->SetFixedSpeed(bSetFixedSpeed, fMinimalSpeed);
}

for (uint32_t i = 0; i < aGroupShips.size(); i++)
aGroupShips[i]->Execute(fDeltaTime);
for (auto &aGroupShip : aGroupShips)
aGroupShip->Execute(fDeltaTime);
}

void AIGroup::Realize(float fDeltaTime)
Expand All @@ -193,17 +193,17 @@ void AIGroup::Realize(float fDeltaTime)

bool AIGroup::isDead()
{
for (uint32_t i = 0; i < aGroupShips.size(); i++)
if (!aGroupShips[i]->isDead())
for (auto &aGroupShip : aGroupShips)
if (!aGroupShip->isDead())
return false;
return true;
}

float AIGroup::GetPower()
{
auto fPower = 0.0f;
for (uint32_t i = 0; i < aGroupShips.size(); i++)
fPower += aGroupShips[i]->GetPower();
for (auto &aGroupShip : aGroupShips)
fPower += aGroupShip->GetPower();
return fPower;
}

Expand All @@ -220,9 +220,8 @@ AIGroup *AIGroup::FindGroup(ATTRIBUTES *pACharacter)
{
if (!pACharacter)
return nullptr;
for (uint32_t i = 0; i < AIGroups.size(); i++)
for (auto pGroup : AIGroups)
{
AIGroup *pGroup = AIGroups[i];
for (uint32_t j = 0; j < pGroup->aGroupShips.size(); j++)
if (*pGroup->aGroupShips[j] == pACharacter)
return pGroup;
Expand All @@ -232,17 +231,17 @@ AIGroup *AIGroup::FindGroup(ATTRIBUTES *pACharacter)

AIGroup *AIGroup::FindGroup(const char *pGroupName)
{
for (uint32_t i = 0; i < AIGroups.size(); i++)
if (AIGroups[i]->GetName() == pGroupName)
return AIGroups[i];
for (auto &AIGroup : AIGroups)
if (AIGroup->GetName() == pGroupName)
return AIGroup;
return nullptr;
}

AIGroup *AIGroup::FindMainGroup()
{
for (uint32_t i = 0; i < AIGroups.size(); i++)
if (AIGroups[i]->isMainGroup())
return AIGroups[i];
for (auto &AIGroup : AIGroups)
if (AIGroup->isMainGroup())
return AIGroup;
return nullptr;
}

Expand All @@ -254,10 +253,8 @@ void AIGroup::SailMainGroup(CVECTOR vPos, float fAngle, ATTRIBUTES *pACharacter)

const auto eidSea = EntityManager::GetEntityId("sea");

for (uint32_t i = 0; i < pMG->aGroupShips.size(); i++)
for (auto pAIShip : pMG->aGroupShips)
{
AIShip *pAIShip = pMG->aGroupShips[i];

if (pAIShip->isDead())
continue;

Expand Down Expand Up @@ -452,8 +449,8 @@ void AIGroup::Save(CSaveLoad *pSL)
pSL->SaveDword(bFirstExecute);

pSL->SaveDword(aGroupShips.size());
for (uint32_t i = 0; i < aGroupShips.size(); i++)
aGroupShips[i]->Save(pSL);
for (auto &aGroupShip : aGroupShips)
aGroupShip->Save(pSL);
}

void AIGroup::Load(CSaveLoad *pSL)
Expand Down
8 changes: 4 additions & 4 deletions src/libs/sea_ai/src/ai_helper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -213,12 +213,12 @@ void AIHelper::Save(CSaveLoad *pSL)
pSL->SaveAPointer("seacameras", pASeaCameras);

pSL->SaveDword(aCharacters.size());
for (uint32_t i = 0; i < aCharacters.size(); i++)
pSL->SaveAPointer("character", aCharacters[i]);
for (auto &aCharacter : aCharacters)
pSL->SaveAPointer("character", aCharacter);

pSL->SaveDword(aMainCharacters.size());
for (uint32_t i = 0; i < aMainCharacters.size(); i++)
pSL->SaveAPointer("character", aMainCharacters[i]);
for (auto &aMainCharacter : aMainCharacters)
pSL->SaveAPointer("character", aMainCharacter);
}

void AIHelper::Load(CSaveLoad *pSL)
Expand Down
Loading

0 comments on commit b748727

Please sign in to comment.