Skip to content

Commit

Permalink
Merge pull request #545 from Wargus/clean_up
Browse files Browse the repository at this point in the history
Clean up
  • Loading branch information
Jarod42 authored Oct 19, 2023
2 parents 02b305f + 5b8d532 commit e21a81e
Show file tree
Hide file tree
Showing 18 changed files with 448 additions and 586 deletions.
12 changes: 6 additions & 6 deletions src/action/actions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -327,18 +327,18 @@ static void HandleBuffsEachCycle(CUnit &unit)

if (!unit.Type->CanCastSpell.empty()) {
// decrease spell countdown timers
for (unsigned int i = 0; i < SpellTypeTable.size(); ++i) {
if (unit.SpellCoolDownTimers[i] > 0) {
--unit.SpellCoolDownTimers[i];
for (auto &timer : unit.SpellCoolDownTimers) {
if (timer > 0) {
--timer;
}
}
}

const int SpellEffects[] = {BLOODLUST_INDEX, HASTE_INDEX, SLOW_INDEX, INVISIBLE_INDEX, UNHOLYARMOR_INDEX, POISON_INDEX};
// decrease spells effects time.
for (unsigned int i = 0; i < sizeof(SpellEffects) / sizeof(int); ++i) {
unit.Variable[SpellEffects[i]].Increase = -1;
IncreaseVariable(unit, SpellEffects[i]);
for (int index : SpellEffects) {
unit.Variable[index].Increase = -1;
IncreaseVariable(unit, index);
}
}

Expand Down
78 changes: 31 additions & 47 deletions src/ai/ai.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -193,24 +193,23 @@ static void AiCheckUnits()
// Count the already made build requests.
auto counter = AiGetBuildRequestsCount(*AiPlayer);

const int *unit_types_count = AiPlayer->Player->UnitTypesAiActiveCount;
const int(&unit_types_count)[UnitTypeMax] = AiPlayer->Player->UnitTypesAiActiveCount;

// Look if some unit-types are missing.
int n = AiPlayer->UnitTypeRequests.size();
for (int i = 0; i < n; ++i) {
const unsigned int t = AiPlayer->UnitTypeRequests[i].Type->Slot;
const int x = AiPlayer->UnitTypeRequests[i].Count;
for (AiRequestType &requestType : AiPlayer->UnitTypeRequests) {
const unsigned int t = requestType.Type->Slot;
const int x = requestType.Count;

// Add equivalent units
int e = unit_types_count[t];
if (t < AiHelpers.Equiv().size()) {
for (unsigned int j = 0; j < AiHelpers.Equiv()[t].size(); ++j) {
e += unit_types_count[AiHelpers.Equiv()[t][j]->Slot];
for (const auto *equivType : AiHelpers.Equiv()[t]) {
e += unit_types_count[equivType->Slot];
}
}
const int requested = x - e - counter[t];
if (requested > 0) { // Request it.
AiAddUnitTypeRequest(*AiPlayer->UnitTypeRequests[i].Type, requested);
AiAddUnitTypeRequest(*requestType.Type, requested);
counter[t] += requested;
}
counter[t] -= x;
Expand All @@ -219,32 +218,30 @@ static void AiCheckUnits()
AiPlayer->Force.CheckUnits(counter);

// Look if some upgrade-to are missing.
n = AiPlayer->UpgradeToRequests.size();
for (int i = 0; i < n; ++i) {
const unsigned int t = AiPlayer->UpgradeToRequests[i]->Slot;
for (CUnitType *unitType : AiPlayer->UpgradeToRequests) {
const unsigned int t = unitType->Slot;
const int x = 1;

// Add equivalent units
int e = unit_types_count[t];
if (t < AiHelpers.Equiv().size()) {
for (unsigned int j = 0; j < AiHelpers.Equiv()[t].size(); ++j) {
e += unit_types_count[AiHelpers.Equiv()[t][j]->Slot];
for (const auto *equivType : AiHelpers.Equiv()[t]) {
e += unit_types_count[equivType->Slot];
}
}

const int requested = x - e - counter[t];
if (requested > 0) { // Request it.
AiAddUpgradeToRequest(*AiPlayer->UpgradeToRequests[i]);
AiAddUpgradeToRequest(*unitType);
counter[t] += requested;
}
counter[t] -= x;
}

// Look if some researches are missing.
n = (int)AiPlayer->ResearchRequests.size();
for (int i = 0; i < n; ++i) {
if (UpgradeIdAllowed(*AiPlayer->Player, AiPlayer->ResearchRequests[i]->ID) == 'A') {
AiAddResearchRequest(AiPlayer->ResearchRequests[i]);
for (CUpgrade *upgrade : AiPlayer->ResearchRequests) {
if (UpgradeIdAllowed(*AiPlayer->Player, upgrade->ID) == 'A') {
AiAddResearchRequest(upgrade);
}
}
}
Expand Down Expand Up @@ -290,17 +287,12 @@ static void SaveAiPlayer(CFile &file, int plynr, const PlayerAi &ai)
}

file.printf("\n \"types\", { ");
const size_t unitTypesCounst = ai.Force[i].UnitTypes.size();
for (size_t j = 0; j != unitTypesCounst; ++j) {
const AiUnitType &aut = ai.Force[i].UnitTypes[j];
for (const AiUnitType &aut : ai.Force[i].UnitTypes) {
file.printf("%d, \"%s\", ", aut.Want, aut.Type->Ident.c_str());
}
file.printf("},\n \"units\", {");
const size_t unitsCount = ai.Force[i].Units.size();
for (size_t j = 0; j != unitsCount; ++j) {
const CUnit &aiunit = *ai.Force[i].Units[j];
file.printf(" %d, \"%s\",", UnitNumber(aiunit),
aiunit.Type->Ident.c_str());
for (const CUnit *aiunit : ai.Force[i].Units) {
file.printf(" %d, \"%s\",", UnitNumber(*aiunit), aiunit->Type->Ident.c_str());
}
file.printf("},\n \"state\", %d, \"goalx\", %d, \"goaly\", %d,",
ai.Force[i].State, ai.Force[i].GoalPos.x, ai.Force[i].GoalPos.y);
Expand Down Expand Up @@ -345,34 +337,28 @@ static void SaveAiPlayer(CFile &file, int plynr, const PlayerAi &ai)
// Requests
if (!ai.FirstExplorationRequest.empty()) {
file.printf(" \"exploration\", {");
const size_t FirstExplorationRequestCount = ai.FirstExplorationRequest.size();
for (size_t i = 0; i != FirstExplorationRequestCount; ++i) {
const AiExplorationRequest &ptr = ai.FirstExplorationRequest[i];
for (const AiExplorationRequest &ptr : ai.FirstExplorationRequest) {
file.printf("{%d, %d, %d}, ", ptr.pos.x, ptr.pos.y, ptr.Mask);
}
file.printf("},\n");
}
file.printf(" \"last-exploration-cycle\", %lu,\n", ai.LastExplorationGameCycle);
file.printf(" \"last-can-not-move-cycle\", %lu,\n", ai.LastCanNotMoveGameCycle);
file.printf(" \"unit-type\", {");
const size_t unitTypeRequestsCount = ai.UnitTypeRequests.size();
for (size_t i = 0; i != unitTypeRequestsCount; ++i) {
file.printf("\"%s\", ", ai.UnitTypeRequests[i].Type->Ident.c_str());
file.printf("%d, ", ai.UnitTypeRequests[i].Count);
for (const auto& requestType : ai.UnitTypeRequests) {
file.printf("\"%s\", %d, ", requestType.Type->Ident.c_str(), requestType.Count);
}
file.printf("},\n");

file.printf(" \"upgrade\", {");
const size_t upgradeToRequestsCount = ai.UpgradeToRequests.size();
for (size_t i = 0; i != upgradeToRequestsCount; ++i) {
file.printf("\"%s\", ", ai.UpgradeToRequests[i]->Ident.c_str());
for (const CUnitType *unitType : ai.UpgradeToRequests) {
file.printf("\"%s\", ", unitType->Ident.c_str());
}
file.printf("},\n");

file.printf(" \"research\", {");
const size_t researchRequestsCount = ai.ResearchRequests.size();
for (size_t i = 0; i != researchRequestsCount; ++i) {
file.printf("\"%s\", ", ai.ResearchRequests[i]->Ident.c_str());
for (const CUpgrade *upgrade : ai.ResearchRequests) {
file.printf("\"%s\", ", upgrade->Ident.c_str());
}
file.printf("},\n");

Expand Down Expand Up @@ -554,10 +540,9 @@ static void AiRemoveFromBuilt(PlayerAi &pai, const CUnitType &type)
}

// This could happen if an upgrade is ready, look for equivalent units.
int equivalents[UnitTypeMax + 1];
const int equivalentsCount = AiFindUnitTypeEquiv(type, equivalents);
for (int i = 0; i < equivalentsCount; ++i) {
if (AiRemoveFromBuilt2(pai, *UnitTypes[equivalents[i]])) {
const auto equivalents = AiFindUnitTypeEquiv(type);
for (int typeIndex : equivalents) {
if (AiRemoveFromBuilt2(pai, *UnitTypes[typeIndex])) {
return;
}
}
Expand Down Expand Up @@ -600,11 +585,10 @@ void AiReduceMadeInBuilt(PlayerAi &pai, const CUnitType &type)
return;
}
// This could happen if an upgrade is ready, look for equivalent units.
int equivs[UnitTypeMax + 1];
const unsigned int equivnb = AiFindUnitTypeEquiv(type, equivs);
const auto equivs = AiFindUnitTypeEquiv(type);

for (unsigned int i = 0; i < equivnb; ++i) {
if (AiReduceMadeInBuilt2(pai, *UnitTypes[equivs[i]])) {
for (int typeIndex : equivs) {
if (AiReduceMadeInBuilt2(pai, *UnitTypes[typeIndex])) {
return;
}
}
Expand Down
Loading

0 comments on commit e21a81e

Please sign in to comment.