From 8d8ed759906698105df1d636ab75fd1aff3693af Mon Sep 17 00:00:00 2001 From: Mark Vejvoda Date: Tue, 22 Feb 2011 22:25:03 +0000 Subject: [PATCH] - bugfix for building units at edges of maps --- source/glest_game/graphics/renderer.cpp | 3 +- source/glest_game/world/map.cpp | 44 +++++++++++++++--------- source/glest_game/world/unit_updater.cpp | 4 ++- 3 files changed, 33 insertions(+), 18 deletions(-) diff --git a/source/glest_game/graphics/renderer.cpp b/source/glest_game/graphics/renderer.cpp index 7798427e..a9c8ef71 100644 --- a/source/glest_game/graphics/renderer.cpp +++ b/source/glest_game/graphics/renderer.cpp @@ -803,7 +803,8 @@ void Renderer::renderMouse3d() { glTranslatef(pos3f.x+offset, pos3f.y, pos3f.z+offset); //choose color - if(map->isFreeCells(pos, building->getSize(), fLand)){ + //if(map->isFreeCells(pos, building->getSize(), fLand)){ + if(map->isFreeCells(pos + Vec2i(building->getSize()/2, building->getSize()/2), building->getSize(), fLand)){ color= Vec4f(1.f, 1.f, 1.f, 0.5f); } else { diff --git a/source/glest_game/world/map.cpp b/source/glest_game/world/map.cpp index 702f499e..fcce5fa4 100644 --- a/source/glest_game/world/map.cpp +++ b/source/glest_game/world/map.cpp @@ -147,7 +147,9 @@ int Map::getCellArraySize() const { Cell *Map::getCell(int x, int y) const { int arrayIndex = y * w + x; if(arrayIndex >= getCellArraySize()) { - throw runtime_error("arrayIndex >= getCellArraySize(), arrayIndex = " + intToStr(arrayIndex) + " w = " + intToStr(w) + " h = " + intToStr(h)); + + abort(); + //throw runtime_error("arrayIndex >= getCellArraySize(), arrayIndex = " + intToStr(arrayIndex) + " w = " + intToStr(w) + " h = " + intToStr(h)); } else if(cells == NULL) { throw runtime_error("cells == NULL"); @@ -379,13 +381,14 @@ bool Map::isResourceNear(const Vec2i &pos, int size, const ResourceType *rt, Vec bool Map::isFreeCell(const Vec2i &pos, Field field) const { return isInside(pos) && + isInsideSurface(toSurfCoords(pos)) && getCell(pos)->isFree(field) && (field==fAir || getSurfaceCell(toSurfCoords(pos))->isFree()) && (field!=fLand || getDeepSubmerged(getCell(pos)) == false); } bool Map::isFreeCellOrHasUnit(const Vec2i &pos, Field field, const Unit *unit) const { - if(isInside(pos)) { + if(isInside(pos) && isInsideSurface(toSurfCoords(pos))) { Cell *c= getCell(pos); if(c->getUnit(unit->getCurrField()) == unit) { if(unit->getCurrField() == fAir) { @@ -414,7 +417,7 @@ bool Map::isFreeCellOrHasUnit(const Vec2i &pos, Field field, const Unit *unit) c } bool Map::isAproxFreeCell(const Vec2i &pos, Field field, int teamIndex) const { - if(isInside(pos)) { + if(isInside(pos) && isInsideSurface(toSurfCoords(pos))) { const SurfaceCell *sc= getSurfaceCell(toSurfCoords(pos)); if(sc->isVisible(teamIndex)) { @@ -434,7 +437,8 @@ bool Map::isFreeCells(const Vec2i & pos, int size, Field field) const { for(int i=pos.x; iisFree(field) = %d, getSurfaceCell(toSurfCoords(testPos))->isFree() = %d, getDeepSubmerged(getCell(testPos)) = %d\n",__FILE__,__FUNCTION__,__LINE__,testPos.getString().c_str(),field,getCell(testPos)->isFree(field),getSurfaceCell(toSurfCoords(testPos))->isFree(),getDeepSubmerged(getCell(testPos))); return false; } @@ -473,14 +477,20 @@ bool Map::isAproxFreeCells(const Vec2i &pos, int size, Field field, int teamInde } bool Map::canOccupy(const Vec2i &pos, Field field, const UnitType *ut, CardinalDir facing) { - if (ut->hasCellMap()) { + if (ut->hasCellMap() && isInside(pos) && isInsideSurface(toSurfCoords(pos))) { for (int y=0; y < ut->getSize(); ++y) { for (int x=0; x < ut->getSize(); ++x) { - if (ut->getCellMapCell(x, y, facing)) { - if (isFreeCell(pos + Vec2i(x, y), field) == false) { - return false; + Vec2i cellPos = pos + Vec2i(x, y); + if(isInside(cellPos) && isInsideSurface(toSurfCoords(cellPos))) { + if (ut->getCellMapCell(x, y, facing)) { + if (isFreeCell(cellPos, field) == false) { + return false; + } } } + else { + false; + } } } return true; @@ -818,14 +828,16 @@ bool Map::isInUnitTypeCells(const UnitType *ut, const Vec2i &pos, throw runtime_error("ut == NULL"); } - Cell *testCell = getCell(testPos); - for(int i=0; i < ut->getSize(); ++i){ - for(int j = 0; j < ut->getSize(); ++j) { - Vec2i currPos = pos + Vec2i(i, j); - if(isInside(currPos) == true) { - Cell *unitCell = getCell(currPos); - if(unitCell == testCell) { - return true; + if(isInside(testPos) && isInsideSurface(toSurfCoords(testPos))) { + Cell *testCell = getCell(testPos); + for(int i=0; i < ut->getSize(); ++i){ + for(int j = 0; j < ut->getSize(); ++j) { + Vec2i currPos = pos + Vec2i(i, j); + if(isInside(currPos) && isInsideSurface(toSurfCoords(currPos))) { + Cell *unitCell = getCell(currPos); + if(unitCell == testCell) { + return true; + } } } } diff --git a/source/glest_game/world/unit_updater.cpp b/source/glest_game/world/unit_updater.cpp index 5775381e..3f683b63 100644 --- a/source/glest_game/world/unit_updater.cpp +++ b/source/glest_game/world/unit_updater.cpp @@ -574,7 +574,9 @@ void UnitUpdater::updateBuild(Unit *unit) { switch(this->game->getGameSettings()->getPathFinderType()) { case pfBasic: SystemFlags::OutputDebug(SystemFlags::debugUnitCommands,"In [%s::%s Line: %d] tsArrived about to call map->isFreeCells() for command->getPos() = %s, ut->getSize() = %d\n",__FILE__,__FUNCTION__,__LINE__,command->getPos().getString().c_str(),ut->getSize()); - canOccupyCell = map->isFreeCells(command->getPos(), ut->getSize(), fLand); + //canOccupyCell = map->isFreeCells(command->getPos(), ut->getSize(), fLand); + //!!!return pos + Vec2i(type->getSize()/2, type->getSize()/2); + canOccupyCell = map->isFreeCells(command->getPos() + Vec2i(ut->getSize()/2, ut->getSize()/2), ut->getSize(), fLand); break; case pfRoutePlanner: canOccupyCell = map->canOccupy(command->getPos(), ut->getField(), ut, command->getFacing());