Skip to content

Commit

Permalink
Fixed issue which caused a crash if two wall tiles were being built a…
Browse files Browse the repository at this point in the history
…t the same time while adjacent to each other
  • Loading branch information
Andrettin committed Oct 20, 2021
1 parent ee2334c commit d4bcc08
Show file tree
Hide file tree
Showing 3 changed files with 2 additions and 91 deletions.
7 changes: 1 addition & 6 deletions src/action/action_built.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -357,12 +357,7 @@ static void Finish(COrder_Built &order, CUnit &unit)

// Set the direction of the building if it supports them
if (type.get_num_directions() > 1 && type.BoolFlag[NORANDOMPLACING_INDEX].value == false) {
if (type.BoolFlag[WALL_INDEX].value) { // Special logic for walls
CorrectWallDirections(unit);
CorrectWallNeighBours(unit);
} else {
unit.Direction = wyrmgus::random::get()->generate(256); // random heading
}
unit.Direction = random::get()->generate(256); // random heading
UnitUpdateHeading(unit);
}

Expand Down
81 changes: 1 addition & 80 deletions src/unit/unit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3853,13 +3853,6 @@ void CUnit::Place(const Vec2i &pos, const int z)
// Vision
MapMarkUnitSight(*this);

// Correct directions for wall units
if (this->Type->BoolFlag[WALL_INDEX].value && this->CurrentAction() != UnitAction::Built) {
CorrectWallDirections(*this);
UnitUpdateHeading(*this, false);
CorrectWallNeighBours(*this);
}

//Wyrmgus start
if (this->IsAlive()) {
if (this->Type->BoolFlag[BUILDING_INDEX].value) {
Expand Down Expand Up @@ -4136,12 +4129,7 @@ void CUnit::Remove(CUnit *host)
MapMarkUnitSight(*this);
}

Removed = 1;

// Correct surrounding walls directions
if (this->Type->BoolFlag[WALL_INDEX].value) {
CorrectWallNeighBours(*this);
}
this->Removed = 1;

// Remove unit from the current selection
if (this->Selected) {
Expand Down Expand Up @@ -4470,73 +4458,6 @@ enum {
W_EAST = 0x80
};

/**
** Correct direction for placed wall.
**
** @param unit The wall unit.
*/
void CorrectWallDirections(CUnit &unit)
{
assert_throw(unit.Type->BoolFlag[WALL_INDEX].value);
assert_throw(unit.Type->get_num_directions() == 16);
assert_throw(!unit.Type->Flip);

if (!CMap::get()->Info->IsPointOnMap(unit.tilePos, unit.MapLayer)) {
return;
}
const struct {
Vec2i offset;
const int dirFlag;
} configs[] = {{Vec2i(0, -1), W_NORTH}, {Vec2i(1, 0), W_EAST},
{Vec2i(0, 1), W_SOUTH}, {Vec2i(-1, 0), W_WEST}
};
int flags = 0;

for (int i = 0; i != sizeof(configs) / sizeof(*configs); ++i) {
const Vec2i pos = unit.tilePos + configs[i].offset;
const int dirFlag = configs[i].dirFlag;

if (CMap::get()->Info->IsPointOnMap(pos, unit.MapLayer) == false) {
flags |= dirFlag;
} else {
const CUnitCache &unitCache = CMap::get()->Field(pos, unit.MapLayer->ID)->UnitCache;
const CUnit *neighbor = unitCache.find(HasSamePlayerAndTypeAs(unit));

if (neighbor != nullptr) {
flags |= dirFlag;
}
}
}
unit.Direction = flags;
}

/**
** Correct the surrounding walls.
**
** @param unit The wall unit.
*/
void CorrectWallNeighBours(CUnit &unit)
{
assert_throw(unit.Type->BoolFlag[WALL_INDEX].value);

const Vec2i offset[] = {Vec2i(1, 0), Vec2i(-1, 0), Vec2i(0, 1), Vec2i(0, -1)};

for (unsigned int i = 0; i < sizeof(offset) / sizeof(*offset); ++i) {
const Vec2i pos = unit.tilePos + offset[i];

if (CMap::get()->Info->IsPointOnMap(pos, unit.MapLayer) == false) {
continue;
}
CUnitCache &unitCache = unit.MapLayer->Field(pos)->UnitCache;
CUnit *neighbor = unitCache.find(HasSamePlayerAndTypeAs(unit));

if (neighbor != nullptr) {
CorrectWallDirections(*neighbor);
UnitUpdateHeading(*neighbor);
}
}
}

/**
** This function should get called when a unit goes under fog of war.
**
Expand Down
5 changes: 0 additions & 5 deletions src/unit/unit.h
Original file line number Diff line number Diff line change
Expand Up @@ -944,11 +944,6 @@ extern void RescueUnits();
/// Convert direction (dx,dy) to heading (0-255)
extern int DirectionToHeading(const Vec2i &dir);

///Correct directions for placed wall.
extern void CorrectWallDirections(CUnit &unit);
/// Correct the surrounding walls.
extern void CorrectWallNeighBours(CUnit &unit);

/// Update frame from heading
extern void UnitUpdateHeading(CUnit &unit, const bool notify = true);
/// Heading and frame from delta direction
Expand Down

0 comments on commit d4bcc08

Please sign in to comment.