Added forceSetUnitPosition function

This commit is contained in:
mathusummut
2019-02-05 23:17:00 +01:00
parent 381703bf6f
commit b69dafdd78
6 changed files with 69 additions and 46 deletions

View File

@@ -451,6 +451,7 @@ namespace Game {
luaScript.registerFunction(getIsUnitAlive, "isUnitAlive");
luaScript.registerFunction(getUnitPosition, "unitPosition");
luaScript.registerFunction(setUnitPosition, "setUnitPosition");
luaScript.registerFunction(forceSetUnitPosition, "forceSetUnitPosition");
luaScript.registerFunction(addCellMarker, "addCellMarker");
luaScript.registerFunction(removeCellMarker, "removeCellMarker");
@@ -1686,7 +1687,7 @@ namespace Game {
}
}
void
int
ScriptManager::createUnit(const string & unitName, int factionIndex,
Vec2i pos) {
if (SystemFlags::getSystemSettingType(SystemFlags::debugLUA).enabled)
@@ -1695,10 +1696,10 @@ namespace Game {
extractFileFromDirectoryPath(__FILE__).
c_str(), __FUNCTION__, __LINE__,
unitName.c_str(), factionIndex);
world->createUnit(unitName, factionIndex, pos);
return world->createUnit(unitName, factionIndex, pos)->getId();
}
void
int
ScriptManager::createUnitNoSpacing(const string & unitName,
int factionIndex, Vec2i pos) {
if (SystemFlags::getSystemSettingType(SystemFlags::debugLUA).enabled)
@@ -1707,7 +1708,7 @@ namespace Game {
extractFileFromDirectoryPath(__FILE__).
c_str(), __FUNCTION__, __LINE__,
unitName.c_str(), factionIndex);
world->createUnit(unitName, factionIndex, pos, false);
return world->createUnit(unitName, factionIndex, pos, false)->getId();
}
void
@@ -2480,7 +2481,18 @@ namespace Game {
extractFileFromDirectoryPath(__FILE__).
c_str(), __FUNCTION__, __LINE__);
return world->setUnitPosition(unitId, pos);
return world->setUnitPosition(unitId, pos, false);
}
void
ScriptManager::forceSetUnitPosition(int unitId, Vec2i pos) {
if (SystemFlags::getSystemSettingType(SystemFlags::debugLUA).enabled)
SystemFlags::OutputDebug(SystemFlags::debugLUA,
"In [%s::%s Line: %d]\n",
extractFileFromDirectoryPath(__FILE__).
c_str(), __FUNCTION__, __LINE__);
return world->setUnitPosition(unitId, pos, true);
}
void
@@ -3569,9 +3581,10 @@ namespace Game {
luaArguments.getInt(-2));
try {
thisScriptManager->createUnit(luaArguments.getString(-3),
int result = thisScriptManager->createUnit(luaArguments.getString(-3),
luaArguments.getInt(-2),
luaArguments.getVec2i(-1));
luaArguments.returnInt(result);
} catch (const game_runtime_error & ex) {
error(luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__);
}
@@ -3641,9 +3654,11 @@ namespace Game {
luaArguments.getInt(-2));
try {
thisScriptManager->createUnitNoSpacing(luaArguments.getString(-3),
luaArguments.getInt(-2),
luaArguments.getVec2i(-1));
int result = thisScriptManager->createUnitNoSpacing(luaArguments.getString(-3),
luaArguments.getInt(-2),
luaArguments.getVec2i(-1));
luaArguments.returnInt(result);
} catch (const game_runtime_error & ex) {
error(luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__);
}
@@ -4482,6 +4497,20 @@ namespace Game {
return luaArguments.getReturnCount();
}
int
ScriptManager::forceSetUnitPosition(LuaHandle * luaHandle) {
LuaArguments
luaArguments(luaHandle);
try {
thisScriptManager->forceSetUnitPosition(luaArguments.getInt(-2),
luaArguments.getVec2i(-1));
} catch (const game_runtime_error & ex) {
error(luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__);
}
return luaArguments.getReturnCount();
}
int
ScriptManager::addCellMarker(LuaHandle * luaHandle) {
LuaArguments

View File

@@ -482,9 +482,9 @@ namespace Game {
void
shakeCamera(int shakeIntensity, int shakeDuration,
bool cameraDistanceAffected, int unitId);
void
int
createUnit(const string & unitName, int factionIndex, Vec2i pos);
void
int
createUnitNoSpacing(const string & unitName, int factionIndex,
Vec2i pos);
@@ -646,6 +646,9 @@ namespace Game {
void
setUnitPosition(int unitId, Vec2i pos);
void
forceSetUnitPosition(int unitId, Vec2i pos);
void
addCellMarker(Vec2i pos, int factionIndex, const string & note,
const string & textureFile);
@@ -959,6 +962,8 @@ namespace Game {
static int
setUnitPosition(LuaHandle * luaHandle);
static int
forceSetUnitPosition(LuaHandle * luaHandle);
static int
addCellMarker(LuaHandle * luaHandle);

View File

@@ -1343,12 +1343,9 @@ namespace Game {
}
//put a units into the cells
void Map::putUnitCells(Unit *unit, const Vec2i &pos, bool ignoreSkill, bool threaded) {
void Map::putUnitCells(Unit *unit, const Vec2i &pos, bool ignoreSkill, bool threaded, bool forcePut) {
assert(unit != NULL);
if (unit == NULL) {
throw game_runtime_error("ut == NULL");
}
putUnitCellsPrivate(unit, pos, unit->getType(), false, threaded);
putUnitCellsPrivate(unit, pos, unit->getType(), false, threaded, forcePut);
// block space for morphing units
if (ignoreSkill == false &&
@@ -1363,11 +1360,8 @@ namespace Game {
}
}
void Map::putUnitCellsPrivate(Unit *unit, const Vec2i &pos, const UnitType *ut, bool isMorph, bool threaded) {
void Map::putUnitCellsPrivate(Unit *unit, const Vec2i &pos, const UnitType *ut, bool isMorph, bool threaded, bool forcePut) {
assert(unit != NULL);
if (unit == NULL) {
throw game_runtime_error("ut == NULL");
}
bool canPutInCell = true;
Field field = ut->getField();
@@ -1375,16 +1369,12 @@ namespace Game {
for (int j = 0; j < ut->getSize(); ++j) {
Vec2i currPos = pos + Vec2i(i, j);
assert(isInside(currPos));
if (isInside(currPos) == false) {
throw game_runtime_error("isInside(currPos) == false");
}
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;
@@ -1423,9 +1413,11 @@ namespace Game {
getCell(currPos)->setUnit(unit->getCurrField(), unit);
}
} else if (canPutInCell == true) {
char szBuf[8096] = "";
snprintf(szBuf, 8096, "Trying to move unit [%d - %s] into occupied cell [%s] and field = %d, unit already in cell [%d - %s] ", unit->getId(), unit->getType()->getName(false).c_str(), pos.getString().c_str(), field, getCell(currPos)->getUnit(field)->getId(), getCell(currPos)->getUnit(field)->getType()->getName(false).c_str());
throw game_runtime_error(szBuf);
if (!forcePut) {
char szBuf[8096] = "";
snprintf(szBuf, 8096, "Trying to move unit [%d - %s] into occupied cell [%s] and field = %d, unit already in cell [%d - %s] ", unit->getId(), unit->getType()->getName(false).c_str(), pos.getString().c_str(), field, getCell(currPos)->getUnit(field)->getId(), getCell(currPos)->getUnit(field)->getType()->getName(false).c_str());
throw game_runtime_error(szBuf);
}
}
} else if (ut->hasCellMap() == true &&
ut->getAllowEmptyCellMap() == true &&
@@ -1447,9 +1439,6 @@ namespace Game {
//removes a unit from cells
void Map::clearUnitCells(Unit *unit, const Vec2i &pos, bool ignoreSkill) {
assert(unit != NULL);
if (unit == NULL) {
throw game_runtime_error("unit == NULL");
}
const UnitType *ut = unit->getType();
Field currentField = unit->getCurrField();
@@ -1472,9 +1461,6 @@ namespace Game {
for (int j = 0; j < ut->getSize(); ++j) {
Vec2i currPos = pos + Vec2i(i, j);
assert(isInside(currPos));
if (isInside(currPos) == false) {
throw game_runtime_error("isInside(currPos) == false");
}
if (ut->hasCellMap() == false || ut->getCellMapCell(i, j, unit->getModelFacing())) {
// This seems to be a bad assert since you can clear the cell

View File

@@ -439,7 +439,7 @@ namespace Game {
//unit placement
bool aproxCanMove(const Unit *unit, const Vec2i &pos1, const Vec2i &pos2, std::map<Vec2i, std::map<Vec2i, std::map<int, std::map<int, std::map<Field, bool> > > > > *lookupCache = NULL) const;
bool canMove(const Unit *unit, const Vec2i &pos1, const Vec2i &pos2, std::map<Vec2i, std::map<Vec2i, std::map<int, std::map<Field, bool> > > > *lookupCache = NULL) const;
void putUnitCells(Unit *unit, const Vec2i &pos, bool ignoreSkill = false, bool threaded = false);
void putUnitCells(Unit *unit, const Vec2i &pos, bool ignoreSkill = false, bool threaded = false, bool forcePut = false);
void clearUnitCells(Unit *unit, const Vec2i &pos, bool ignoreSkill = false);
Vec2i computeRefPos(const Selection *selection) const;
@@ -718,7 +718,7 @@ namespace Game {
void smoothSurface(Tileset *tileset);
void computeNearSubmerged();
void computeCellColors();
void putUnitCellsPrivate(Unit *unit, const Vec2i &pos, const UnitType *ut, bool isMorph, bool threaded);
void putUnitCellsPrivate(Unit *unit, const Vec2i &pos, const UnitType *ut, bool isMorph, bool threaded, bool forcePut = false);
};

View File

@@ -1168,7 +1168,7 @@ namespace Game {
}
//clears a unit old position from map and places new position
void World::moveUnitCells(Unit *unit, bool threaded) {
void World::moveUnitCells(Unit *unit, bool threaded, bool forceMove) {
if (unit == NULL) {
throw game_runtime_error("unit == NULL");
}
@@ -1181,7 +1181,7 @@ namespace Game {
// from the old one
if (newPos != unit->getPos()) {
map.clearUnitCells(unit, unit->getPos());
map.putUnitCells(unit, newPos, false, threaded);
map.putUnitCells(unit, newPos, false, threaded, forceMove);
}
// Add resources close by to the faction's cache
unit->getFaction()->addCloseResourceTargetToCache(newPos);
@@ -1319,9 +1319,10 @@ namespace Game {
}
}
void World::createUnit(const string &unitName, int factionIndex, const Vec2i &pos, bool spaciated) {
Unit* World::createUnit(const string &unitName, int factionIndex, const Vec2i &pos, bool spaciated) {
if (SystemFlags::getSystemSettingType(SystemFlags::debugUnitCommands).enabled) SystemFlags::OutputDebug(SystemFlags::debugUnitCommands, "In [%s::%s Line: %d] unitName [%s] factionIndex = %d\n", __FILE__, __FUNCTION__, __LINE__, unitName.c_str(), factionIndex);
Unit* unit = NULL;
if (factionIndex < (int) factions.size()) {
Faction* faction = factions[factionIndex];
@@ -1341,7 +1342,7 @@ namespace Game {
throw game_runtime_error("detected unsupported pathfinder type!", true);
}
Unit* unit = new Unit(getNextUnitId(faction), newpath, pos, ut, faction, &map, CardinalDir(CardinalDir::NORTH));
unit = new Unit(getNextUnitId(faction), newpath, pos, ut, faction, &map, CardinalDir(CardinalDir::NORTH));
if (SystemFlags::getSystemSettingType(SystemFlags::debugUnitCommands).enabled) SystemFlags::OutputDebug(SystemFlags::debugUnitCommands, "In [%s::%s Line: %d] unit created for unit [%s]\n", __FILE__, __FUNCTION__, __LINE__, unit->toString().c_str());
@@ -1368,6 +1369,8 @@ namespace Game {
}
if (SystemFlags::getSystemSettingType(SystemFlags::debugUnitCommands).enabled) SystemFlags::OutputDebug(SystemFlags::debugUnitCommands, "In [%s::%s Line: %d]\n", __FILE__, __FUNCTION__, __LINE__);
return unit;
}
void World::giveResource(const string &resourceName, int factionIndex, int amount) {
@@ -1809,13 +1812,13 @@ namespace Game {
return unit->getPos();
}
void World::setUnitPosition(int unitId, Vec2i pos) {
void World::setUnitPosition(int unitId, Vec2i pos, bool forceSet) {
Unit* unit = findUnitById(unitId);
if (unit == NULL) {
throw game_runtime_error("Can not find unit to set position unitId = " + intToStr(unitId), true);
}
unit->setTargetPos(pos);
this->moveUnitCells(unit, false);
this->moveUnitCells(unit, false, forceSet);
}
void World::addCellMarker(Vec2i pos, int factionIndex, const string &note, const string textureFile) {

View File

@@ -280,7 +280,7 @@ namespace Game {
const UnitType* findUnitTypeById(const FactionType* factionType, int id);
const UnitType *findUnitTypeByName(const string factionName, const string unitTypeName);
bool placeUnit(const Vec2i &startLoc, int radius, Unit *unit, bool spaciated = false, bool threaded = false);
void moveUnitCells(Unit *unit, bool threaded);
void moveUnitCells(Unit *unit, bool threaded, bool forceMove = false);
bool toRenderUnit(const Unit *unit, const Quad2i &visibleQuad) const;
bool toRenderUnit(const Unit *unit) const;
@@ -292,7 +292,7 @@ namespace Game {
//scripting interface
void morphToUnit(int unitId, const string &morphName, bool ignoreRequirements);
void createUnit(const string &unitName, int factionIndex, const Vec2i &pos, bool spaciated = true);
Unit* createUnit(const string &unitName, int factionIndex, const Vec2i &pos, bool spaciated = true);
void givePositionCommand(int unitId, const string &commandName, const Vec2i &pos);
vector<int> getUnitsForFaction(int factionIndex, const string& commandTypeName, int field);
int getUnitCurrentField(int unitId);
@@ -315,7 +315,7 @@ namespace Game {
int getResourceAmount(const string &resourceName, int factionIndex);
Vec2i getStartLocation(int factionIndex);
Vec2i getUnitPosition(int unitId);
void setUnitPosition(int unitId, Vec2i pos);
void setUnitPosition(int unitId, Vec2i pos, bool forceSet = false);
void addCellMarker(Vec2i pos, int factionIndex, const string &note, const string textureFile);
void removeCellMarker(Vec2i pos, int factionIndex);