Skip to content

Commit

Permalink
Minor code refactoring.
Browse files Browse the repository at this point in the history
  • Loading branch information
klei1984 committed Sep 27, 2024
1 parent 2c17d9a commit 5f16a46
Show file tree
Hide file tree
Showing 21 changed files with 113 additions and 109 deletions.
7 changes: 3 additions & 4 deletions src/aiattack.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ bool AiAttack_IsValidSabotageTarget(UnitInfo* unit, UnitInfo* target) {
bool AiAttack_ProcessAttack(UnitInfo* attacker, UnitInfo* target) {
bool result;

if (GameManager_PlayMode != PLAY_MODE_TURN_BASED || GameManager_ActiveTurnTeam == attacker->team) {
if (GameManager_IsActiveTurn(attacker->team)) {
if (attacker->delayed_reaction || TaskManager_word_1731C0 == 2) {
SmartPointer<Task> wait_to_attack_task(new (std::nothrow) TaskWaitToAttack(attacker));

Expand Down Expand Up @@ -443,8 +443,7 @@ bool AiAttack_CanAttack(UnitInfo* attacker, UnitInfo* target) {
bool AiAttack_FindAttackSupport(UnitInfo* unit, SmartList<UnitInfo>* units, uint16_t team, int32_t caution_level) {
bool result = false;

if (unit->IsVisibleToTeam(team) &&
(GameManager_PlayMode != PLAY_MODE_TURN_BASED || GameManager_ActiveTurnTeam == team)) {
if (unit->IsVisibleToTeam(team) && GameManager_IsActiveTurn(team)) {
for (SmartList<UnitInfo>::Iterator it = units->Begin(); it != units->End(); ++it) {
if ((*it).team == team && (*it).shots > 0 && Task_IsReadyToTakeOrders(&*it) &&
AiAttack_CanAttack(&*it, unit)) {
Expand Down Expand Up @@ -697,7 +696,7 @@ bool AiAttack_EvaluateAttack(UnitInfo* unit, bool mode) {
bool result;

if (GameManager_PlayMode != PLAY_MODE_UNKNOWN) {
if (unit->ammo || (GameManager_PlayMode != PLAY_MODE_TURN_BASED || GameManager_ActiveTurnTeam == unit->team)) {
if (unit->ammo || GameManager_IsActiveTurn(unit->team)) {
if (unit->shots > 0 || !unit->GetBaseValues()->GetAttribute(ATTRIB_MOVE_AND_FIRE)) {
if (unit->delayed_reaction || TaskManager_word_1731C0 == 2) {
SmartPointer<Task> wait_to_attack_task(new (std::nothrow) TaskWaitToAttack(unit));
Expand Down
4 changes: 4 additions & 0 deletions src/game_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8489,3 +8489,7 @@ void GameManager_QuickBuildMenu() {

win_delete(window->id);
}

bool GameManager_IsActiveTurn(uint16_t team) {
return (GameManager_PlayMode != PLAY_MODE_TURN_BASED || GameManager_ActiveTurnTeam == team);
}
1 change: 1 addition & 0 deletions src/game_manager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ void GameManager_MenuUnitSelect(UnitInfo* unit);
void GameManager_FillOrRestoreWindow(uint8_t id, int32_t color, bool redraw);
void GameManager_UpdateInfoDisplay(UnitInfo* unit);
bool GameManager_IsInsideMapView(UnitInfo* unit);
bool GameManager_IsActiveTurn(uint16_t team);
bool GameManager_OptimizeProduction(uint16_t team, Complex* complex, bool is_player_team, bool mode);
void GameManager_SelectNextUnit(int32_t seek_direction);
bool GameManager_UpdateMapDrawBounds(int32_t ulx, int32_t uly);
Expand Down
6 changes: 3 additions & 3 deletions src/task_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ void TaskManager::CheckComputerReactions() {
AiLog log("Checking computer reactions");

for (SmartList<Task>::Iterator it = tasks.Begin(); it != tasks.End(); ++it) {
if (GameManager_PlayMode != PLAY_MODE_TURN_BASED || GameManager_ActiveTurnTeam == (*it).GetTeam()) {
if (GameManager_IsActiveTurn((*it).GetTeam())) {
if ((*it).CheckReactions()) {
return;
}
Expand All @@ -260,8 +260,8 @@ void TaskManager::CheckComputerReactions() {

if (TaskManager_word_1731C0 == 0) {
for (SmartList<UnitInfo>::Iterator it = units.Begin(); it != units.End(); ++it) {
if ((GameManager_PlayMode != PLAY_MODE_TURN_BASED || GameManager_ActiveTurnTeam == (*it).team) &&
(*it).hits > 0 && (*it).speed > 0 && Task_IsReadyToTakeOrders(&*it) &&
if (GameManager_IsActiveTurn((*it).team) && (*it).hits > 0 && (*it).speed > 0 &&
Task_IsReadyToTakeOrders(&*it) &&
UnitsManager_TeamInfo[(*it).team].team_type == TEAM_TYPE_COMPUTER) {
if ((*it).GetTask()) {
Task_RetreatFromDanger((*it).GetTask(), &*it, Ai_DetermineCautionLevel(&*it));
Expand Down
4 changes: 2 additions & 2 deletions src/taskactivate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ TaskActivate::~TaskActivate() {}

void TaskActivate::Activate() {
if (unit_to_activate != nullptr) {
if (GameManager_PlayMode != PLAY_MODE_TURN_BASED || GameManager_ActiveTurnTeam == team) {
if (GameManager_PlayMode != PLAY_MODE_UNKNOWN) {
if (GameManager_PlayMode != PLAY_MODE_UNKNOWN) {
if (GameManager_IsActiveTurn(team)) {
if (unit_to_activate->GetTask() == this && zone == nullptr) {
if (unit_to_activate->GetOrder() == ORDER_IDLE || unit_to_activate->GetOrder() == ORDER_BUILD ||
unit_to_activate->GetOrder() == ORDER_AWAIT) {
Expand Down
6 changes: 2 additions & 4 deletions src/taskassistmove.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ TaskAssistMove::TaskAssistMove(uint16_t team_) : Task(team_, nullptr, 0x2800) {}
TaskAssistMove::~TaskAssistMove() {}

void TaskAssistMove::RequestTransport(UnitInfo* unit1, UnitInfo* unit2) {
if (GameManager_PlayMode != PLAY_MODE_TURN_BASED || GameManager_ActiveTurnTeam == team) {
if (GameManager_IsActiveTurn(team)) {
if (unit1->GetUnitType() == AIRTRANS) {
unit1->SetParent(unit2);
UnitsManager_SetNewOrder(unit1, ORDER_LOAD, ORDER_STATE_INIT);
Expand Down Expand Up @@ -67,7 +67,7 @@ void TaskAssistMove::RequestTransport(UnitInfo* unit1, UnitInfo* unit2) {
}

void TaskAssistMove::CompleteTransport(UnitInfo* unit1, UnitInfo* unit2, Point site) {
if (GameManager_PlayMode != PLAY_MODE_TURN_BASED || GameManager_ActiveTurnTeam == team) {
if (GameManager_IsActiveTurn(team)) {
if (Access_GetTeamUnit(site.x, site.y, team, MOBILE_SEA_UNIT | MOBILE_LAND_UNIT)) {
SmartPointer<Zone> zone = new (std::nothrow) Zone(unit2, this);

Expand All @@ -81,8 +81,6 @@ void TaskAssistMove::CompleteTransport(UnitInfo* unit1, UnitInfo* unit2, Point s

unit1->SetParent(unit2);

SDL_assert(GameManager_PlayMode != PLAY_MODE_TURN_BASED || GameManager_ActiveTurnTeam == team);

if (unit1->GetUnitType() == AIRTRANS) {
UnitsManager_SetNewOrder(unit1, ORDER_UNLOAD, ORDER_STATE_INIT);

Expand Down
3 changes: 1 addition & 2 deletions src/taskautosurvey.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,7 @@ void TaskAutoSurvey::Begin() { unit->AddTask(this); }
bool TaskAutoSurvey::Execute(UnitInfo& unit_) {
bool result;

if (unit == unit_ && unit->IsReadyForOrders(this) &&
(GameManager_PlayMode != PLAY_MODE_TURN_BASED || GameManager_ActiveTurnTeam == team) && unit->speed > 0) {
if (unit == unit_ && unit->IsReadyForOrders(this) && GameManager_IsActiveTurn(team) && unit->speed > 0) {
AiLog log("Auto survey: Move %s at [%i,%i]", UnitsManager_BaseUnits[unit->GetUnitType()].singular_name,
unit->grid_x + 1, unit->grid_y + 1);

Expand Down
6 changes: 3 additions & 3 deletions src/taskclearzone.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ void TaskClearZone::PathFindResultCallback(Task* task, PathRequest* request, Poi
AiLog log("Clear zone: path result.");

if (path && (!clear_zone->moving_unit || clear_zone->moving_unit->IsReadyForOrders(clear_zone)) &&
(GameManager_PlayMode != PLAY_MODE_TURN_BASED || GameManager_ActiveTurnTeam == clear_zone->team)) {
GameManager_IsActiveTurn(clear_zone->team)) {
SDL_assert(clear_zone->moving_unit != nullptr);

clear_zone->state = CLEARZONE_STATE_MOVING_UNIT;
Expand Down Expand Up @@ -152,8 +152,8 @@ void TaskClearZone::BeginTurn() {
void TaskClearZone::EndTurn() {
AiLog log("Clear Zone: End Turn.");

if (GameManager_PlayMode != PLAY_MODE_TURN_BASED || GameManager_ActiveTurnTeam == team) {
if (GameManager_PlayMode != PLAY_MODE_UNKNOWN) {
if (GameManager_PlayMode != PLAY_MODE_UNKNOWN) {
if (GameManager_IsActiveTurn(team)) {
if (state != CLEARZONE_STATE_EXAMINING_ZONES && state != CLEARZONE_STATE_WAITING_FOR_PATH) {
if (state == CLEARZONE_STATE_MOVING_UNIT) {
state = CLEARZONE_STATE_WAITING;
Expand Down
12 changes: 6 additions & 6 deletions src/taskcreatebuilding.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,8 @@ void TaskCreateBuilding::RequestBuilder() {
}

void TaskCreateBuilding::AbandonSite() {
if (GameManager_PlayMode != PLAY_MODE_TURN_BASED || GameManager_ActiveTurnTeam == builder->team) {
if (GameManager_PlayMode != PLAY_MODE_UNKNOWN) {
if (GameManager_PlayMode != PLAY_MODE_UNKNOWN) {
if (GameManager_IsActiveTurn(builder->team)) {
builder->target_grid_x = builder->grid_x;
builder->target_grid_y = builder->grid_y;

Expand All @@ -135,7 +135,7 @@ bool TaskCreateBuilding::BuildRoad() {
if (builder->GetUnitType() == ENGINEER && Task_IsReadyToTakeOrders(&*builder) && builder->speed == 0 &&
(builder->grid_x != site.x || builder->grid_y != site.y)) {
if (ini_get_setting(INI_OPPONENT) >= MASTER || builder->storage >= 26) {
if (GameManager_PlayMode != PLAY_MODE_TURN_BASED || GameManager_ActiveTurnTeam == team) {
if (GameManager_IsActiveTurn(team)) {
if (builder->storage >= 2 && Access_IsAccessible(ROAD, team, builder->grid_x, builder->grid_y, 1)) {
SmartObjectArray<ResourceID> build_list = builder->GetBuildList();
ResourceID unit_type_ = ROAD;
Expand Down Expand Up @@ -196,8 +196,7 @@ void TaskCreateBuilding::BeginBuilding() {
if (!Ai_IsDangerousLocation(&*builder, site, CAUTION_LEVEL_AVOID_NEXT_TURNS_FIRE,
false)) {
if (CheckMaterials()) {
if (GameManager_PlayMode != PLAY_MODE_TURN_BASED ||
GameManager_ActiveTurnTeam == team) {
if (GameManager_IsActiveTurn(team)) {
build_list.Clear();

build_list.PushBack(&unit_type);
Expand Down Expand Up @@ -644,7 +643,8 @@ void TaskCreateBuilding::EndTurn() {
} else if (builder->GetOrderState() == ORDER_STATE_UNIT_READY && builder->GetTask() == this) {
Activate();

} else if (builder->GetOrder() == ORDER_BUILD && builder->GetOrderState() == ORDER_STATE_BUILD_IN_PROGRESS &&
} else if (builder->GetOrder() == ORDER_BUILD &&
builder->GetOrderState() == ORDER_STATE_BUILD_IN_PROGRESS &&
Ai_IsDangerousLocation(&*builder, site, CAUTION_LEVEL_AVOID_NEXT_TURNS_FIRE, false)) {
AbandonSite();
}
Expand Down
4 changes: 2 additions & 2 deletions src/taskcreateunit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ bool TaskCreateUnit::Task_vfunc28() { return op_state >= CREATE_UNIT_STATE_BUILD
void TaskCreateUnit::WaitForMaterials() {
SDL_assert(op_state == CREATE_UNIT_STATE_WAITING_FOR_MATERIALS);

if (GameManager_PlayMode != PLAY_MODE_TURN_BASED || GameManager_ActiveTurnTeam == team) {
if (GameManager_IsActiveTurn(team)) {
Cargo materials;
Cargo capacity;

Expand Down Expand Up @@ -317,7 +317,7 @@ bool TaskCreateUnit::IsUnitStillNeeded() {
bool result;

if (op_state <= CREATE_UNIT_STATE_BUILDING) {
if (GameManager_PlayMode != PLAY_MODE_TURN_BASED || GameManager_ActiveTurnTeam == team) {
if (GameManager_IsActiveTurn(team)) {
if (Task_EstimateTurnsTillMissionEnd() >
UnitsManager_GetCurrentUnitValues(&UnitsManager_TeamInfo[team], unit_type)
->GetAttribute(ATTRIB_TURNS) &&
Expand Down
2 changes: 1 addition & 1 deletion src/taskfrontalattack.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ void TaskFrontalAttack::IssueOrders() {
}
}

if (GameManager_PlayMode != PLAY_MODE_TURN_BASED || GameManager_ActiveTurnTeam == team) {
if (GameManager_IsActiveTurn(team)) {
for (SmartList<UnitInfo>::Iterator it = units1.Begin(); it != units1.End(); ++it) {
if (Task_IsReadyToTakeOrders(&*it)) {
if ((*it).speed == 0 || Access_GetDistance(&*it, spotted_unit->GetLastPosition()) <=
Expand Down
2 changes: 1 addition & 1 deletion src/taskgetmaterials.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ void TaskGetMaterials::DoTransfer() {

source->SetParent(requestor.Get());

SDL_assert(GameManager_PlayMode != PLAY_MODE_TURN_BASED || GameManager_ActiveTurnTeam == team);
SDL_assert(GameManager_IsActiveTurn(team));

log.Log("Order %i materials from %s at [%i,%i] for %s at [%i,%i] holding %i materials.", source->target_grid_x,
UnitsManager_BaseUnits[source->GetUnitType()].singular_name, source->grid_x + 1, source->grid_y + 1,
Expand Down
4 changes: 2 additions & 2 deletions src/taskgetresource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,8 @@ void TaskGetResource::EndTurn() {

if (source && source->GetTask() == this) {
if (Task_IsAdjacent(&*supplier, requestor->grid_x, requestor->grid_y)) {
if (GameManager_PlayMode != PLAY_MODE_TURN_BASED || GameManager_ActiveTurnTeam == team) {
if (GameManager_PlayMode != PLAY_MODE_UNKNOWN) {
if (GameManager_PlayMode != PLAY_MODE_UNKNOWN) {
if (GameManager_IsActiveTurn(team)) {
DoTransfer();
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/taskmove.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -966,7 +966,7 @@ void TaskMove::MoveAirUnit() {
if (passenger->IsReadyForOrders(this)) {
AiLog log("Move air unit.");

if (GameManager_PlayMode != PLAY_MODE_TURN_BASED || GameManager_ActiveTurnTeam == team) {
if (GameManager_IsActiveTurn(team)) {
SmartPointer<UnitInfo> unit;
bool flag = false;
int32_t step_index;
Expand Down Expand Up @@ -995,7 +995,8 @@ void TaskMove::MoveAirUnit() {
UnitsManager_BaseUnits[(*it).GetUnitType()].singular_name, (*it).grid_x + 1,
(*it).grid_y + 1);

if ((*it).GetOrder() == ORDER_MOVE && (*it).GetOrderState() != ORDER_STATE_EXECUTING_ORDER) {
if ((*it).GetOrder() == ORDER_MOVE &&
(*it).GetOrderState() != ORDER_STATE_EXECUTING_ORDER) {
log.Log("Blocker is moving.");

class RemindTurnEnd* reminder = new (std::nothrow) class RemindTurnEnd(*this);
Expand Down Expand Up @@ -1250,8 +1251,7 @@ bool TaskMove::FindWaypoint() {
}

void TaskMove::MoveUnit(GroundPath* path) {
if (passenger->IsReadyForOrders(this) && path &&
(GameManager_PlayMode != PLAY_MODE_TURN_BASED || GameManager_ActiveTurnTeam == team)) {
if (passenger->IsReadyForOrders(this) && path && GameManager_IsActiveTurn(team)) {
AiLog log("Moving unit to [%i,%i].", passenger_waypoint.x + 1, passenger_waypoint.y + 1);

if (caution_level > CAUTION_LEVEL_NONE && Task_ShouldReserveShot(&*passenger, passenger_waypoint)) {
Expand Down
2 changes: 1 addition & 1 deletion src/taskplacemines.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ bool TaskPlaceMines::Execute(UnitInfo& unit) {

if (info_map) {
if (info_map[unit.grid_x][unit.grid_y] & 2) {
if (GameManager_PlayMode != PLAY_MODE_TURN_BASED || GameManager_ActiveTurnTeam == team) {
if (GameManager_IsActiveTurn(team)) {
UnitsManager_SetNewOrder(&unit, ORDER_LAY_MINE, ORDER_STATE_PLACING_MINES);

info_map[unit.grid_x][unit.grid_y] &= ~0x02;
Expand Down
2 changes: 1 addition & 1 deletion src/taskremovemines.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ bool TaskRemoveMines::Execute(UnitInfo& unit_) {
result = DumpMaterials(&unit_);

} else if (target->grid_x == unit_.grid_x && target->grid_y == unit_.grid_y) {
if (GameManager_PlayMode != PLAY_MODE_TURN_BASED || GameManager_ActiveTurnTeam == team) {
if (GameManager_IsActiveTurn(team)) {
UnitsManager_SetNewOrder(&unit_, ORDER_LAY_MINE, ORDER_STATE_REMOVING_MINES);
target = nullptr;
}
Expand Down
4 changes: 2 additions & 2 deletions src/taskremoverubble.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ bool TaskRemoveRubble::Execute(UnitInfo& unit_) {

if (unit_.GetBaseValues()->GetAttribute(ATTRIB_STORAGE) != unit_.storage) {
if (unit_.grid_x == target->grid_x && unit_.grid_y == target->grid_y) {
if (GameManager_PlayMode != PLAY_MODE_TURN_BASED || GameManager_ActiveTurnTeam == team) {
if (GameManager_IsActiveTurn(team)) {
unit_.SetParent(&*target);

unit_.build_time = (target->flags & BUILDING) ? 4 : 1;
Expand Down Expand Up @@ -203,7 +203,7 @@ bool TaskRemoveRubble::DumpMaterials(UnitInfo* unit_) {
building != UnitsManager_StationaryUnits.End(); ++building) {
if ((*building).GetComplex() == complex) {
if (Task_IsAdjacent(building->Get(), unit_->grid_x, unit_->grid_y)) {
if (GameManager_PlayMode != PLAY_MODE_TURN_BASED || GameManager_ActiveTurnTeam == team) {
if (GameManager_IsActiveTurn(team)) {
unit_->target_grid_x =
std::min(static_cast<int32_t>(unit_->storage),
(*it).GetBaseValues()->GetAttribute(ATTRIB_STORAGE) - (*it).storage);
Expand Down
Loading

0 comments on commit 5f16a46

Please sign in to comment.