diff --git a/source/game/game/script_manager.cpp b/source/game/game/script_manager.cpp index a88b125f0..76cee124d 100644 --- a/source/game/game/script_manager.cpp +++ b/source/game/game/script_manager.cpp @@ -449,6 +449,7 @@ namespace Game { luaScript.registerFunction(getStartLocation, "startLocation"); luaScript.registerFunction(getIsUnitAlive, "isUnitAlive"); + luaScript.registerFunction(areCellsFree, "areCellsFree"); luaScript.registerFunction(getUnitPosition, "unitPosition"); luaScript.registerFunction(setUnitPosition, "setUnitPosition"); luaScript.registerFunction(forceSetUnitPosition, "forceSetUnitPosition"); @@ -2452,6 +2453,16 @@ namespace Game { return world->getStartLocation(factionIndex); } + bool + ScriptManager::areCellsFree(Vec2i pos, int size, int field) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugLUA).enabled) + SystemFlags::OutputDebug(SystemFlags::debugLUA, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath(__FILE__). + c_str(), __FUNCTION__, __LINE__); + + return world->areCellsFree(pos, size, static_cast(field)); + } Vec2i ScriptManager::getUnitPosition(int unitId) { @@ -4468,6 +4479,21 @@ namespace Game { return luaArguments.getReturnCount(); } + int + ScriptManager::areCellsFree(LuaHandle * luaHandle) { + LuaArguments + luaArguments(luaHandle); + + try { + bool result = thisScriptManager->areCellsFree(luaArguments.getVec2i(-3), luaArguments.getInt(-2), luaArguments.getInt(-1)); + luaArguments.returnInt(result ? 1 : 0); + } catch (const game_runtime_error & ex) { + error(luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); + } + + return luaArguments.getReturnCount(); + } + int ScriptManager::getUnitPosition(LuaHandle * luaHandle) { LuaArguments diff --git a/source/game/game/script_manager.h b/source/game/game/script_manager.h index 69ed9e443..52af7d32f 100644 --- a/source/game/game/script_manager.h +++ b/source/game/game/script_manager.h @@ -629,6 +629,8 @@ namespace Game { getStartLocation(int factionIndex); Vec2i getUnitPosition(int unitId); + bool + areCellsFree(Vec2i pos, int size, int field); int getUnitFaction(int unitId); const string @@ -960,6 +962,7 @@ namespace Game { static int getLastCreatedUnitId(LuaHandle * luaHandle); + static int areCellsFree(LuaHandle * luaHandle); static int setUnitPosition(LuaHandle * luaHandle); static int diff --git a/source/game/world/map.cpp b/source/game/world/map.cpp index c8984b0b8..1186eb204 100644 --- a/source/game/world/map.cpp +++ b/source/game/world/map.cpp @@ -842,6 +842,16 @@ namespace Game { return false; } + bool Map::isAproxFreeCell(const Vec2i &pos, Field field) const { + for (int i = 0; i <= MAX_MAP_FACTIONCOUNT; i++) { + if (!isAproxFreeCell(pos, field, i)) + return false; + } + + //printf("[%s] Line: %d returning false\n",__FUNCTION__,__LINE__); + return true; + } + bool Map::isFreeCells(const Vec2i & pos, int size, Field field, bool buildingsOnly) const { for (int i = pos.x; i < pos.x + size; ++i) { for (int j = pos.y; j < pos.y + size; ++j) { @@ -877,6 +887,16 @@ namespace Game { return true; } + bool Map::isAproxFreeCells(const Vec2i &pos, int size, Field field) const { + for (int i = 0; i <= MAX_MAP_FACTIONCOUNT; i++) { + if (!isAproxFreeCells(pos, size, field, i)) + return false; + } + + //printf("[%s] Line: %d returning false\n",__FUNCTION__,__LINE__); + return true; + } + bool Map::canMorph(const Vec2i &pos, const Unit *currentUnit, const UnitType *targetUnitType) const { Field field = targetUnitType->getField(); const UnitType *ut = targetUnitType; @@ -1321,9 +1341,6 @@ namespace Game { bool Map::isInUnitTypeCells(const UnitType *ut, const Vec2i &pos, const Vec2i &testPos) const { assert(ut != NULL); - if (ut == NULL) { - throw game_runtime_error("ut == NULL"); - } if (isInside(testPos) && isInsideSurface(toSurfCoords(testPos))) { Cell *testCell = getCell(testPos); @@ -1372,9 +1389,7 @@ namespace Game { if (ut->hasCellMap() == false || ut->getCellMapCell(i, j, unit->getModelFacing())) { if (getCell(currPos)->getUnit(field) != NULL && getCell(currPos)->getUnit(field) != unit) { - // TT: is this ok ? - //If unit tries to move into a cell where another unit resides - // cancel the move command + //if unit tries to move into a cell where another unit resides cancel the move command if (unit->getCurrSkill() != NULL && unit->getCurrSkill()->getClass() == scMove) { canPutInCell = false; diff --git a/source/game/world/map.h b/source/game/world/map.h index 727d82fb8..29934a3eb 100644 --- a/source/game/world/map.h +++ b/source/game/world/map.h @@ -429,9 +429,11 @@ namespace Game { //free cells bool isFreeCell(const Vec2i &pos, Field field, bool buildingsOnly = false) const; bool isFreeCellOrHasUnit(const Vec2i &pos, Field field, const Unit *unit) const; + bool isAproxFreeCell(const Vec2i &pos, Field field) const; bool isAproxFreeCell(const Vec2i &pos, Field field, int teamIndex) const; bool isFreeCells(const Vec2i &pos, int size, Field field, bool buildingsOnly = false) const; bool isFreeCellsOrHasUnit(const Vec2i &pos, int size, Field field, const Unit *unit) const; + bool isAproxFreeCells(const Vec2i &pos, int size, Field field) 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); diff --git a/source/game/world/world.cpp b/source/game/world/world.cpp index 97f9fd398..f859f5782 100644 --- a/source/game/world/world.cpp +++ b/source/game/world/world.cpp @@ -1804,6 +1804,10 @@ namespace Game { } } + bool World::areCellsFree(Vec2i pos, int size, Field field) { + return map.isFreeCells(pos, size, field); + } + Vec2i World::getUnitPosition(int unitId) { Unit* unit = findUnitById(unitId); if (unit == NULL) { diff --git a/source/game/world/world.h b/source/game/world/world.h index 9b97ca2e9..73755608a 100644 --- a/source/game/world/world.h +++ b/source/game/world/world.h @@ -314,6 +314,7 @@ namespace Game { void giveResource(const string &resourceName, int factionIndex, int amount); int getResourceAmount(const string &resourceName, int factionIndex); Vec2i getStartLocation(int factionIndex); + bool areCellsFree(Vec2i pos, int size, Field field); Vec2i getUnitPosition(int unitId); void setUnitPosition(int unitId, Vec2i pos, bool forceSet = false); diff --git a/source/shared_lib/include/map/map_preview.h b/source/shared_lib/include/map/map_preview.h index f43d08af8..9b5d24b6b 100644 --- a/source/shared_lib/include/map/map_preview.h +++ b/source/shared_lib/include/map/map_preview.h @@ -57,7 +57,6 @@ namespace Shared { static const int MAX_MAP_CELL_HEIGHT = 20; static const int DEFAULT_MAP_CELL_HEIGHT = 10; - static const int MIN_MAP_FACTIONCOUNT = 1; static const int MAX_MAP_FACTIONCOUNT = 10; static const int DEFAULT_MAP_FACTIONCOUNT = 8; diff --git a/source/shared_lib/sources/map/map_preview.cpp b/source/shared_lib/sources/map/map_preview.cpp index 0f856ccbd..ae416a151 100644 --- a/source/shared_lib/sources/map/map_preview.cpp +++ b/source/shared_lib/sources/map/map_preview.cpp @@ -645,9 +645,9 @@ namespace Shared { } void MapPreview::resetFactions(int maxPlayers) { - if (maxPlayers < MIN_MAP_FACTIONCOUNT || maxPlayers > MAX_MAP_FACTIONCOUNT) { + if (maxPlayers < 0 || maxPlayers > MAX_MAP_FACTIONCOUNT) { char szBuf[8096] = ""; - snprintf(szBuf, 8096, "Max Players must be in the range %d-%d", MIN_MAP_FACTIONCOUNT, MAX_MAP_FACTIONCOUNT); + snprintf(szBuf, 8096, "Max Players must be in the range %d-%d", 0, MAX_MAP_FACTIONCOUNT); throw game_runtime_error(szBuf); }