Skip to content

Commit

Permalink
Fixed defect 205.
Browse files Browse the repository at this point in the history
  • Loading branch information
klei1984 committed Sep 24, 2023
1 parent 00ec687 commit 458661c
Show file tree
Hide file tree
Showing 3 changed files with 137 additions and 113 deletions.
226 changes: 123 additions & 103 deletions src/taskcreatebuilding.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ void TaskCreateBuilding::Abort() {
manager->ChildComplete(this);
}

if (parent && &*parent != &*manager) {
if (parent && parent.Get() != manager.Get()) {
parent->ChildComplete(this);
}

Expand All @@ -298,6 +298,8 @@ void TaskCreateBuilding::Abort() {
tasks.Clear();

Finish();

unit_type = INVALID_ID;
}

void TaskCreateBuilding::Finish() {
Expand Down Expand Up @@ -409,64 +411,70 @@ bool TaskCreateBuilding::RequestRubbleRemoval() {
}

char* TaskCreateBuilding::WriteStatusLog(char* buffer) const {
sprintf(buffer, "Create a %s at [%i,%i]", UnitsManager_BaseUnits[unit_type].singular_name, site.x + 1, site.y + 1);
if (unit_type != INVALID_ID) {
sprintf(buffer, "Create a %s at [%i,%i]", UnitsManager_BaseUnits[unit_type].singular_name, site.x + 1,
site.y + 1);

switch (op_state) {
case CREATE_BUILDING_STATE_INITIALIZING: {
strcat(buffer, ": initializing");
} break;
switch (op_state) {
case CREATE_BUILDING_STATE_INITIALIZING: {
strcat(buffer, ": initializing");
} break;

case CREATE_BUILDING_STATE_WAITING_FOR_PLATFORM: {
strcat(buffer, ": waiting for platform");
} break;
case CREATE_BUILDING_STATE_WAITING_FOR_PLATFORM: {
strcat(buffer, ": waiting for platform");
} break;

case CREATE_BUILDING_STATE_REMOVING_MINE: {
strcat(buffer, ": removing mines");
} break;
case CREATE_BUILDING_STATE_REMOVING_MINE: {
strcat(buffer, ": removing mines");
} break;

case CREATE_BUILDING_STATE_GETTING_BUILDER: {
strcat(buffer, ": get builder");
} break;
case CREATE_BUILDING_STATE_GETTING_BUILDER: {
strcat(buffer, ": get builder");
} break;

case CREATE_BUILDING_STATE_GETTING_MATERIALS: {
strcat(buffer, ": get materials");
} break;
case CREATE_BUILDING_STATE_GETTING_MATERIALS: {
strcat(buffer, ": get materials");
} break;

case CREATE_BUILDING_STATE_EVALUTING_SITE: {
strcat(buffer, ": evaluating site");
} break;
case CREATE_BUILDING_STATE_EVALUTING_SITE: {
strcat(buffer, ": evaluating site");
} break;

case CREATE_BUILDING_STATE_SITE_BLOCKED: {
strcat(buffer, ": site blocked");
} break;
case CREATE_BUILDING_STATE_SITE_BLOCKED: {
strcat(buffer, ": site blocked");
} break;

case CREATE_BUILDING_STATE_MOVING_TO_SITE: {
strcat(buffer, ": move to site");
} break;
case CREATE_BUILDING_STATE_MOVING_TO_SITE: {
strcat(buffer, ": move to site");
} break;

case CREATE_BUILDING_STATE_CLEARING_SITE: {
strcat(buffer, ": clear site");
} break;
case CREATE_BUILDING_STATE_CLEARING_SITE: {
strcat(buffer, ": clear site");
} break;

case CREATE_BUILDING_STATE_BUILDING: {
strcat(buffer, ": building");
} break;

case CREATE_BUILDING_STATE_BUILDING: {
strcat(buffer, ": building");
} break;
case CREATE_BUILDING_STATE_MOVING_OFF_SITE: {
strcat(buffer, ": move off site");
} break;

case CREATE_BUILDING_STATE_MOVING_OFF_SITE: {
strcat(buffer, ": move off site");
} break;
case CREATE_BUILDING_STATE_FINISHED: {
strcat(buffer, ": finished.");
} break;

case CREATE_BUILDING_STATE_FINISHED: {
strcat(buffer, ": finished.");
} break;
default: {
strcat(buffer, ": UNKNOWN STATE!");
} break;
}

default: {
strcat(buffer, ": UNKNOWN STATE!");
} break;
}
if (builder && builder->GetBuildRate() > 1) {
strcat(buffer, " at x2 rate");
}

if (builder && builder->GetBuildRate() > 1) {
strcat(buffer, " at x2 rate");
} else {
sprintf(buffer, "Create building aborted.");
}

return buffer;
Expand Down Expand Up @@ -497,70 +505,73 @@ bool TaskCreateBuilding::IsNeeded() {
}

void TaskCreateBuilding::AddUnit(UnitInfo& unit) {
AiLog log("Build %s at [%i,%i]: Add %s", UnitsManager_BaseUnits[unit_type].singular_name, unit.grid_x + 1,
unit.grid_y + 1, UnitsManager_BaseUnits[unit.unit_type].singular_name);

if (unit.unit_type == unit_type && unit.grid_x == site.x && unit.grid_y == site.y && unit.orders != ORDER_IDLE) {
SmartPointer<Task> create_building_task(this);
if (unit_type != INVALID_ID) {
AiLog log("Build %s at [%i,%i]: Add %s", UnitsManager_BaseUnits[unit_type].singular_name, site.x + 1,
site.y + 1, UnitsManager_BaseUnits[unit.unit_type].singular_name);

op_state = CREATE_BUILDING_STATE_FINISHED;
if (unit.unit_type == unit_type && unit.grid_x == site.x && unit.grid_y == site.y &&
unit.orders != ORDER_IDLE) {
SmartPointer<Task> create_building_task(this);

unit.RemoveTasks();
op_state = CREATE_BUILDING_STATE_FINISHED;

if (manager) {
manager->ChildComplete(this);
manager->AddUnit(unit);
}
unit.RemoveTasks();

if (unit_type == WTRPLTFM || unit_type == BRIDGE) {
if (&*manager != &*parent) {
parent->AddUnit(unit);
if (manager) {
manager->ChildComplete(this);
manager->AddUnit(unit);
}

} else {
TaskManager.RemindAvailable(&unit);
}
if (unit_type == WTRPLTFM || unit_type == BRIDGE) {
if (manager.Get() != parent.Get()) {
parent->AddUnit(unit);
}

if (&*manager != &*parent) {
ChildComplete(this);
}
} else {
TaskManager.RemindAvailable(&unit);
}

if (manager.Get() != parent.Get()) {
ChildComplete(this);
}

if (builder && builder->GetTask() == this && parent && unit_type == WTRPLTFM &&
parent->GetType() == TaskType_TaskCreateBuilding) {
TaskCreateBuilding* create_building = dynamic_cast<TaskCreateBuilding*>(&*parent);
if (builder && builder->GetTask() == this && parent && unit_type == WTRPLTFM &&
parent->GetType() == TaskType_TaskCreateBuilding) {
TaskCreateBuilding* create_building = dynamic_cast<TaskCreateBuilding*>(parent.Get());

if (create_building->op_state == CREATE_BUILDING_STATE_WAITING_FOR_PLATFORM &&
Builder_GetBuilderType(create_building->GetUnitType()) == builder->unit_type) {
SmartPointer<UnitInfo> backup(builder);
if (create_building->op_state == CREATE_BUILDING_STATE_WAITING_FOR_PLATFORM &&
Builder_GetBuilderType(create_building->GetUnitType()) == builder->unit_type) {
SmartPointer<UnitInfo> backup(builder);

backup->RemoveTasks();
backup->RemoveTasks();

dynamic_cast<TaskCreateBuilding*>(&*parent)->AddUnit(*backup);
dynamic_cast<TaskCreateBuilding*>(parent.Get())->AddUnit(*backup);
}
}
}

parent = nullptr;
manager = nullptr;
building = nullptr;
parent = nullptr;
manager = nullptr;
building = nullptr;

tasks.Clear();
tasks.Clear();

Finish();
Finish();

} else if (unit.unit_type == Builder_GetBuilderType(unit_type)) {
if (!builder) {
if (op_state < CREATE_BUILDING_STATE_BUILDING) {
op_state = CREATE_BUILDING_STATE_GETTING_MATERIALS;
builder = unit;
builder->AddTask(this);
Task_RemindMoveFinished(&*builder);
} else if (unit.unit_type == Builder_GetBuilderType(unit_type)) {
if (!builder) {
if (op_state < CREATE_BUILDING_STATE_BUILDING) {
op_state = CREATE_BUILDING_STATE_GETTING_MATERIALS;
builder = unit;
builder->AddTask(this);
Task_RemindMoveFinished(&*builder);

} else {
log.Log("Task already complete.");
}

} else {
log.Log("Task already complete.");
log.Log("Already have builder.");
}

} else {
log.Log("Already have builder.");
}
}
}
Expand Down Expand Up @@ -649,8 +660,8 @@ void TaskCreateBuilding::EndTurn() {
} break;
}

} else if (op_state != CREATE_BUILDING_STATE_GETTING_BUILDER && op_state != CREATE_BUILDING_STATE_BUILDING &&
!tasks.GetCount()) {
} else if (unit_type != INVALID_ID && op_state != CREATE_BUILDING_STATE_GETTING_BUILDER &&
op_state != CREATE_BUILDING_STATE_BUILDING && !tasks.GetCount()) {
RequestBuilder();
}
}
Expand Down Expand Up @@ -724,6 +735,8 @@ bool TaskCreateBuilding::Execute(UnitInfo& unit) {
}

} else {
log.Log("Site blocked.");

Abort();

result = false;
Expand Down Expand Up @@ -889,26 +902,33 @@ void TaskCreateBuilding::FindBuildSite() {
SDL_assert(manager->GetType() == TaskType_TaskManageBuildings);

if (manager->ChangeSite(this, new_site)) {
site = new_site;
if (unit_type != INVALID_ID) {
site = new_site;

log.Log("New site for %s is [%i, %i].", UnitsManager_BaseUnits[unit_type].singular_name, site.x + 1,
site.y + 1);

log.Log("New site is for %s is [%i, %i].", UnitsManager_BaseUnits[unit_type].singular_name, site.x + 1,
site.y + 1);
SDL_assert(site.x > 0 && site.y > 0);

SDL_assert(site.x > 0 && site.y > 0);
op_state = CREATE_BUILDING_STATE_MOVING_TO_SITE;

op_state = CREATE_BUILDING_STATE_MOVING_TO_SITE;
if (builder) {
if (!RequestWaterPlatform() && !RequestMineRemoval() && !RequestRubbleRemoval() &&
!IsScheduledForTurnEnd()) {
TaskManager.AppendReminder(new (std::nothrow) class RemindTurnEnd(*this));
}

if (builder) {
if (!RequestWaterPlatform() && !RequestMineRemoval() && !RequestRubbleRemoval() &&
!IsScheduledForTurnEnd()) {
TaskManager.AppendReminder(new (std::nothrow) class RemindTurnEnd(*this));
} else {
log.Log("Requirements for site pre-empted builder.");
}

} else {
log.Log("Requirements for site pre-empted builder.");
log.Log("Site blocked and no alternative found.");
}

} else {
log.Log("Site blocked and no alternative found.");

Abort();
}

Expand Down Expand Up @@ -1317,7 +1337,7 @@ bool TaskCreateBuilding::FindBridgePath(uint8_t** map, int32_t value) {
manager->AddCreateOrder(&*create_building_task);

} else {
log.Log("No manager!");
log.Log("No building manager!");

TaskManager.AppendTask(*create_building_task);
}
Expand Down
20 changes: 12 additions & 8 deletions src/taskobtainunits.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
#include "units_manager.hpp"

TaskObtainUnits::TaskObtainUnits(Task* task, Point point)
: Task(task->GetTeam(), task, task->GetFlags()), point(point), field_27(true), field_28(true) {}
: Task(task->GetTeam(), task, task->GetFlags()), point(point), init(true), reinit(true) {}

TaskObtainUnits::~TaskObtainUnits() {}

Expand Down Expand Up @@ -204,18 +204,18 @@ void TaskObtainUnits::AddUnit(UnitInfo& unit) {
}

void TaskObtainUnits::Begin() {
if (field_28) {
if (reinit) {
RemindTurnEnd(true);

field_27 = true;
field_28 = false;
init = true;
reinit = false;
}
}

void TaskObtainUnits::BeginTurn() {
AiLog log("Obtain Unit: Begin Turn");

field_28 = true;
reinit = true;
EndTurn();
}

Expand All @@ -224,7 +224,7 @@ void TaskObtainUnits::EndTurn() {
units.Clear();
}

if (field_27) {
if (init) {
for (int32_t i = units.GetCount() - 1; i >= 0; --i) {
UnitInfo* unit = FindUnit(*units[i], false);

Expand Down Expand Up @@ -262,7 +262,7 @@ void TaskObtainUnits::EndTurn() {
}
}

field_27 = false;
init = false;
}

if (!units.GetCount()) {
Expand All @@ -283,4 +283,8 @@ void TaskObtainUnits::RemoveSelf() {
TaskManager.RemoveTask(*this);
}

void TaskObtainUnits::AddUnit(ResourceID unit_type) { units.PushBack(&unit_type); }
void TaskObtainUnits::AddUnit(ResourceID unit_type) {
SDL_assert(unit_type != INVALID_ID);

units.PushBack(&unit_type);
}
4 changes: 2 additions & 2 deletions src/taskobtainunits.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@
class TaskObtainUnits : public Task {
SmartObjectArray<ResourceID> units;
Point point;
bool field_27;
bool field_28;
bool init;
bool reinit;

bool IsValidCandidate(UnitInfo* unit, bool mode);

Expand Down

0 comments on commit 458661c

Please sign in to comment.