SAGA2: Add fallback for invalid sector access

This commit is contained in:
a/ 2021-06-20 22:44:02 +09:00 committed by Eugene Sandulenko
parent 91882aca4e
commit 12535d74bc
No known key found for this signature in database
GPG Key ID: 014D387312D34F08
2 changed files with 20 additions and 5 deletions

View File

@ -794,6 +794,12 @@ public:
void cleanup(void);
Sector *getSector(int16 u, int16 v) {
if (v * sectorArraySize + u >= sectorArraySize * sectorArraySize ||
v * sectorArraySize + u < 0) {
warning("Sector::getSector: Invalid sector: (%d, %d)", u, v);
return nullptr;
}
return &(sectorArray)[v * sectorArraySize + u];
}

View File

@ -969,18 +969,27 @@ uint8 ProtoObj::getDamageSound(const ObjectSoundFXs &) {
void ProtoObj::doBackgroundUpdate(GameObject *obj) {
TilePoint location = obj->getLocation();
GameWorld *w = obj->world();
int u = location.u >> kSectorShift;
int v = location.v >> kSectorShift;
// XXX: Temporary crash prevention
// We should properly solve the problem
debug(3, "XXX: doBackgroundUpdate");
if (location.u == -1 && location.v == -1)
if (w == nullptr) {
obj->deactivate();
return;
}
Sector *sect = w->getSector(u, v);
if (sect == nullptr)
return;
if (w == NULL
|| !w->getSector(
location.u >> kSectorShift,
location.v >> kSectorShift)->isActivated())
if (!sect->isActivated()) {
obj->deactivate();
return;
}
}
// ------------------------------------------------------------------------