morph takes cellmaps into account ( needed for prax next )

This commit is contained in:
titiger 2015-01-14 22:58:19 +01:00
parent 8284b3ecde
commit 7006c26707
5 changed files with 35 additions and 13 deletions

View File

@ -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>(field),unit,NULL,true);
int result = world->getMap()->isFreeCellsOrHasUnit(pos,unit->getType()->getSize(),static_cast<Field>(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>(field),NULL,NULL,true);
int result = world->getMap()->isFreeCellsOrHasUnit(pos,unitSize,static_cast<Field>(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);

View File

@ -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);

View File

@ -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) {

View File

@ -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

View File

@ -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());