diff --git a/source/glest_game/game/script_manager.cpp b/source/glest_game/game/script_manager.cpp index 8a3b4fcd..20e133e5 100644 --- a/source/glest_game/game/script_manager.cpp +++ b/source/glest_game/game/script_manager.cpp @@ -1734,7 +1734,7 @@ int ScriptManager::isFreeCellsOrHasUnit(int field, int unitId, Vec2i pos) { if(unit == NULL) { throw megaglest_runtime_error("unit == NULL",true); } - int result = world->getMap()->isFreeCellsOrHasUnit(pos,unit->getType()->getSize(),static_cast(field),unit,NULL,true); + int result = world->getMap()->isFreeCellsOrHasUnit(pos,unit->getType()->getSize(),static_cast(field),unit); if(SystemFlags::getSystemSettingType(SystemFlags::debugLUA).enabled) SystemFlags::OutputDebug(SystemFlags::debugLUA,"In [%s] unitId = %d, [%s] pos [%s] field = %d result = %d\n",__FUNCTION__,unitId,unit->getType()->getName(false).c_str(),pos.getString().c_str(),field,result); @@ -1744,7 +1744,7 @@ int ScriptManager::isFreeCellsOrHasUnit(int field, int unitId, Vec2i pos) { int ScriptManager::isFreeCells(int unitSize, int field, Vec2i pos) { if(SystemFlags::getSystemSettingType(SystemFlags::debugLUA).enabled) SystemFlags::OutputDebug(SystemFlags::debugLUA,"In [%s::%s Line: %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); - int result = world->getMap()->isFreeCellsOrHasUnit(pos,unitSize,static_cast(field),NULL,NULL,true); + int result = world->getMap()->isFreeCellsOrHasUnit(pos,unitSize,static_cast(field),NULL); if(SystemFlags::getSystemSettingType(SystemFlags::debugLUA).enabled) SystemFlags::OutputDebug(SystemFlags::debugLUA,"In [%s] unitSize = %d, pos [%s] field = %d result = %d\n",__FUNCTION__,unitSize,pos.getString().c_str(),field,result); diff --git a/source/glest_game/type_instances/unit.cpp b/source/glest_game/type_instances/unit.cpp index bc4f2695..640a25ec 100644 --- a/source/glest_game/type_instances/unit.cpp +++ b/source/glest_game/type_instances/unit.cpp @@ -3632,7 +3632,7 @@ bool Unit::morph(const MorphCommandType *mct) { } map->clearUnitCells(this, pos, false); - if(map->isFreeCellsOrHasUnit(pos, morphUnitType->getSize(), morphUnitField, this,morphUnitType)) { + if(map->canMorph(pos,this,morphUnitType)) { map->clearUnitCells(this, pos, true); faction->deApplyStaticCosts(type,mct); diff --git a/source/glest_game/world/map.cpp b/source/glest_game/world/map.cpp index 200c7db6..965c2a66 100644 --- a/source/glest_game/world/map.cpp +++ b/source/glest_game/world/map.cpp @@ -817,13 +817,7 @@ bool Map::isFreeCells(const Vec2i & pos, int size, Field field) const { } bool Map::isFreeCellsOrHasUnit(const Vec2i &pos, int size, Field field, - const Unit *unit, const UnitType *munit,bool allowNullUnit) const { - if(unit == NULL && allowNullUnit == false) { - throw megaglest_runtime_error("unit == NULL"); - } - if(munit == NULL && allowNullUnit == false) { - throw megaglest_runtime_error("munit == NULL"); - } + const Unit *unit) const { for(int i = pos.x; i < pos.x + size; ++i) { for(int j = pos.y; j < pos.y + size; ++j) { if(isFreeCellOrHasUnit(Vec2i(i,j), field, unit) == false) { @@ -845,6 +839,34 @@ bool Map::isAproxFreeCells(const Vec2i &pos, int size, Field field, int teamInde return true; } +bool Map::canMorph(const Vec2i &pos,const Unit *currentUnit,const UnitType *targetUnitType ) const{ + Field field=targetUnitType->getField(); + const UnitType *ut=targetUnitType; + CardinalDir facing=currentUnit->getModelFacing(); + + if (ut->hasCellMap() && isInside(pos) && isInsideSurface(toSurfCoords(pos))) { + for (int y=0; y < ut->getSize(); ++y) { + for (int x=0; x < ut->getSize(); ++x) { + Vec2i cellPos = pos + Vec2i(x, y); + if(isInside(cellPos) && isInsideSurface(toSurfCoords(cellPos))) { + if (ut->getCellMapCell(x, y, facing)) { + if (isFreeCellOrHasUnit(cellPos, field, currentUnit) == false) { + return false; + } + } + } + else { + return false; + } + } + } + return true; + } + else { + return isFreeCellsOrHasUnit(pos, ut->getSize(), field,currentUnit); + } +} + bool Map::canOccupy(const Vec2i &pos, Field field, const UnitType *ut, CardinalDir facing) { if (ut->hasCellMap() && isInside(pos) && isInsideSurface(toSurfCoords(pos))) { for (int y=0; y < ut->getSize(); ++y) { diff --git a/source/glest_game/world/map.h b/source/glest_game/world/map.h index 9e650c0a..18e9415d 100644 --- a/source/glest_game/world/map.h +++ b/source/glest_game/world/map.h @@ -327,9 +327,9 @@ public: bool isFreeCellOrHasUnit(const Vec2i &pos, Field field, const Unit *unit) const; bool isAproxFreeCell(const Vec2i &pos, Field field, int teamIndex) const; bool isFreeCells(const Vec2i &pos, int size, Field field) const; - bool isFreeCellsOrHasUnit(const Vec2i &pos, int size, Field field, const Unit *unit, const UnitType *munit, bool allowNullUnit=false) const; + bool isFreeCellsOrHasUnit(const Vec2i &pos, int size, Field field, const Unit *unit) const; bool isAproxFreeCells(const Vec2i &pos, int size, Field field, int teamIndex) const; - + bool canMorph(const Vec2i &pos,const Unit *currentUnit,const UnitType *targetUnitType ) const; bool canOccupy(const Vec2i &pos, Field field, const UnitType *ut, CardinalDir facing); //unit placement diff --git a/source/glest_game/world/unit_updater.cpp b/source/glest_game/world/unit_updater.cpp index 8e449800..1243e222 100644 --- a/source/glest_game/world/unit_updater.cpp +++ b/source/glest_game/world/unit_updater.cpp @@ -2434,7 +2434,7 @@ void UnitUpdater::updateMorph(Unit *unit, int frameIndex) { if(unit->getCurrSkill()->getClass()!=scMorph){ //if not morphing, check space - if(map->isFreeCellsOrHasUnit(unit->getPos(), mct->getMorphUnit()->getSize(), mct->getMorphUnit()->getField(), unit, mct->getMorphUnit())){ + if(map->canMorph(unit->getPos(),unit,mct->getMorphUnit())){ unit->setCurrSkill(mct->getMorphSkillType()); // block space for morphing units ( block space before and after morph ! ) map->putUnitCells(unit, unit->getPos());