Skip to content

Commit

Permalink
Fixed defect 136.
Browse files Browse the repository at this point in the history
  • Loading branch information
klei1984 committed Jul 23, 2024
1 parent 30c9e49 commit 4106af7
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 44 deletions.
51 changes: 19 additions & 32 deletions src/paths.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ static int32_t Paths_GetAngle(int32_t x, int32_t y);
static void Paths_DrawMissile(UnitInfo* unit, int32_t position_x, int32_t position_y);
static bool Paths_LoadUnit(UnitInfo* unit);
static void Paths_FinishMove(UnitInfo* unit);
static void Paths_TakeStep(UnitInfo* unit, int32_t cost, int32_t param);
static bool Paths_CalculateStep(UnitInfo* unit, int32_t cost, int32_t param, bool is_diagonal_step);
static void Paths_TakeStep(UnitInfo* unit, int32_t cost);
static bool Paths_CalculateStep(UnitInfo* unit, int32_t cost, bool is_diagonal_step);

static uint16_t Paths_AirPath_TypeIndex;
static RegisterClass Paths_AirPath_ClassRegister("AirPath", &Paths_AirPath_TypeIndex, &AirPath::Allocate);
Expand Down Expand Up @@ -272,11 +272,11 @@ bool AirPath::Execute(UnitInfo* unit) {
if (unit->flags & MOBILE_AIR_UNIT) {
int32_t speed = unit->speed;

if (unit->group_speed) {
if (unit->group_speed > 0) {
speed = unit->group_speed - 1;
}

if (!speed || !unit->engine) {
if (speed == 0 || unit->engine == 0) {
unit->SetOrderState(ORDER_STATE_EXECUTING_ORDER);
unit->MoveFinished();

Expand Down Expand Up @@ -356,7 +356,7 @@ bool AirPath::Execute(UnitInfo* unit) {
unit->FollowUnit();

if ((length % unit->max_velocity) == 0) {
Paths_TakeStep(unit, 1, 2);
Paths_TakeStep(unit, 1);

if (GameManager_SelectedUnit == unit) {
GameManager_UpdateInfoDisplay(unit);
Expand Down Expand Up @@ -715,7 +715,7 @@ bool GroundPath::Execute(UnitInfo* unit) {
is_diagonal_step = false;
}

if (Paths_CalculateStep(unit, cost, 2, is_diagonal_step)) {
if (Paths_CalculateStep(unit, cost, is_diagonal_step)) {
if ((unit->flags & (MOBILE_SEA_UNIT | MOBILE_LAND_UNIT)) == (MOBILE_SEA_UNIT | MOBILE_LAND_UNIT)) {
int32_t image_index;

Expand Down Expand Up @@ -1146,6 +1146,10 @@ AirPath* Paths_GetAirPath(UnitInfo* unit) {
if (steps_distance == 0 || distance == 0) {
unit->speed = 0;

if (unit->group_speed > 1) {
unit->group_speed = 1;
}

distance_x = end_x - grid_x;
distance_y = end_y - grid_y;

Expand Down Expand Up @@ -1400,14 +1404,11 @@ bool Paths_LoadUnit(UnitInfo* unit) {
}

void Paths_FinishMove(UnitInfo* unit) {
int32_t grid_x;
int32_t grid_y;
int32_t grid_x = unit->grid_x;
int32_t grid_y = unit->grid_y;

GameManager_RenderMinimapDisplay = true;

grid_x = unit->grid_x;
grid_y = unit->grid_y;

if (unit->flags & MISSILE_UNIT) {
SmartPointer<UnitInfo> parent = unit->GetParent();

Expand Down Expand Up @@ -1451,23 +1452,15 @@ void Paths_FinishMove(UnitInfo* unit) {
}
}

void Paths_TakeStep(UnitInfo* unit, int32_t cost, int32_t param) {
int32_t group_speed;
UnitValues* base_values;

void Paths_TakeStep(UnitInfo* unit, int32_t cost) {
if (GameManager_RealTime) {
param = 0;
cost = 0;
}

param = 0;

SDL_assert(unit->speed >= cost);

unit->speed -= cost;

if (unit->group_speed) {
group_speed = unit->group_speed - cost;
int32_t group_speed = unit->group_speed - cost;

if (group_speed < 1) {
group_speed = 1;
Expand All @@ -1476,20 +1469,19 @@ void Paths_TakeStep(UnitInfo* unit, int32_t cost, int32_t param) {
unit->group_speed = group_speed;
}

base_values = unit->GetBaseValues();
auto base_values = unit->GetBaseValues();

if (!base_values->GetAttribute(ATTRIB_MOVE_AND_FIRE)) {
int32_t shots;

shots = (base_values->GetAttribute(ATTRIB_ROUNDS) * unit->speed) / base_values->GetAttribute(ATTRIB_SPEED);
int32_t shots =
(base_values->GetAttribute(ATTRIB_ROUNDS) * unit->speed) / base_values->GetAttribute(ATTRIB_SPEED);

if (shots < unit->shots) {
unit->shots = shots;
}
}
}

bool Paths_CalculateStep(UnitInfo* unit, int32_t cost, int32_t param, bool is_diagonal_step) {
bool Paths_CalculateStep(UnitInfo* unit, int32_t cost, bool is_diagonal_step) {
int32_t max_velocity;
int32_t normalized_cost;
bool result;
Expand Down Expand Up @@ -1517,11 +1509,8 @@ bool Paths_CalculateStep(UnitInfo* unit, int32_t cost, int32_t param, bool is_di

if (is_diagonal_step) {
cost = (cost * 3) / 2;
param = (param * 3) / 2;
}

param = 0;

if (unit->move_fraction > cost) {
unit->move_fraction = cost;
}
Expand All @@ -1531,8 +1520,6 @@ bool Paths_CalculateStep(UnitInfo* unit, int32_t cost, int32_t param, bool is_di
normalized_cost = (cost + 3) / 4;

if (normalized_cost > unit->speed) {
SDL_assert((sizeof(unit->move_fraction) << 8) > (unit->speed * 4));

unit->move_fraction = unit->speed * 4;
unit->speed = 0;

Expand All @@ -1543,7 +1530,7 @@ bool Paths_CalculateStep(UnitInfo* unit, int32_t cost, int32_t param, bool is_di
result = false;

} else {
Paths_TakeStep(unit, normalized_cost, param);
Paths_TakeStep(unit, normalized_cost);

unit->move_fraction = (normalized_cost * 4) - cost;

Expand Down
4 changes: 2 additions & 2 deletions src/unitinfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2234,7 +2234,7 @@ void UnitInfo::AttackUnit(UnitInfo* enemy, int32_t attack_potential, int32_t dir
}
}

bool UnitInfo::ExpectAttack() {
bool UnitInfo::ExecuteMove() {
bool result;

RefreshScreen();
Expand Down Expand Up @@ -2483,7 +2483,7 @@ void UnitInfo::Move() {
}

if (state == ORDER_STATE_IN_PROGRESS && !team_visibility) {
ExpectAttack();
ExecuteMove();
}

} while (state == ORDER_STATE_IN_TRANSITION && !team_visibility);
Expand Down
2 changes: 1 addition & 1 deletion src/unitinfo.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ class UnitInfo : public FileObject {
void SetPosition(int32_t grid_x, int32_t grid_y, bool skip_map_status_update);
void RemoveDelayedTasks();
void AttackUnit(UnitInfo* enemy, int32_t attack_potential, int32_t direction);
bool ExpectAttack();
bool ExecuteMove();
void ClearBuildListAndPath();
void Move();
void AllocateUnitList();
Expand Down
18 changes: 9 additions & 9 deletions src/units_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ static void UnitsManager_PowerDownUnit(UnitInfo* unit);
static void UnitsManager_Animate(UnitInfo* unit);
static void UnitsManager_DeployMasterBuilder(UnitInfo* unit);
static bool UnitsManager_PursueEnemy(UnitInfo* unit);
static bool UnitsManager_UpdateAttackMoves(UnitInfo* unit);
static bool UnitsManager_InitUnitMove(UnitInfo* unit);
static void UnitsManager_Store(UnitInfo* unit);
static bool UnitsManager_AimAtTarget(UnitInfo* unit);
static void UnitsManager_BuildClearing(UnitInfo* unit, bool mode);
Expand All @@ -154,7 +154,7 @@ static bool UnitsManager_ShouldAttack(UnitInfo* unit1, UnitInfo* unit2);
static bool UnitsManager_CheckReaction(UnitInfo* unit1, UnitInfo* unit2);
static bool UnitsManager_IsReactionPending(SmartList<UnitInfo>* units, UnitInfo* unit);
static AirPath* UnitsManager_GetMissilePath(UnitInfo* unit);
static void UnitsManager_UpdateAttackPaths(UnitInfo* unit);
static void UnitsManager_InitUnitPath(UnitInfo* unit);
static bool UnitsManager_IsAttackScheduled();
static Point UnitsManager_GetAttackPosition(UnitInfo* unit1, UnitInfo* unit2);
static bool UnitsManager_CheckDelayedReactions(uint16_t team);
Expand Down Expand Up @@ -4391,7 +4391,7 @@ void UnitsManager_ProcessOrderMove(UnitInfo* unit) {
} break;

case ORDER_STATE_IN_PROGRESS: {
if (unit->ExpectAttack()) {
if (unit->ExecuteMove()) {
Ai_SetTasksPendingFlag("Moving");
unit->Move();
}
Expand All @@ -4416,8 +4416,8 @@ void UnitsManager_ProcessOrderMove(UnitInfo* unit) {
}
}

if (UnitsManager_UpdateAttackMoves(unit)) {
if (unit->ExpectAttack()) {
if (UnitsManager_InitUnitMove(unit)) {
if (unit->ExecuteMove()) {
Ai_SetTasksPendingFlag("Moving");
unit->Move();
}
Expand Down Expand Up @@ -4538,7 +4538,7 @@ void UnitsManager_ProcessOrderBuild(UnitInfo* unit) {
} break;

case ORDER_STATE_IN_PROGRESS: {
if (unit->ExpectAttack()) {
if (unit->ExecuteMove()) {
Ai_SetTasksPendingFlag("Moving");

unit->Move();
Expand Down Expand Up @@ -5449,9 +5449,9 @@ bool UnitsManager_PursueEnemy(UnitInfo* unit) {
return result;
}

bool UnitsManager_UpdateAttackMoves(UnitInfo* unit) {
bool UnitsManager_InitUnitMove(UnitInfo* unit) {
unit->RefreshScreen();
UnitsManager_UpdateAttackPaths(unit);
UnitsManager_InitUnitPath(unit);

if (unit->GetOrderState() == ORDER_STATE_INIT) {
unit->FollowUnit();
Expand Down Expand Up @@ -6643,7 +6643,7 @@ AirPath* UnitsManager_GetMissilePath(UnitInfo* unit) {
return result;
}

void UnitsManager_UpdateAttackPaths(UnitInfo* unit) {
void UnitsManager_InitUnitPath(UnitInfo* unit) {
unit->path = nullptr;

if (unit->flags & MISSILE_UNIT) {
Expand Down

0 comments on commit 4106af7

Please sign in to comment.