setLockedUnitForFaction

*the actual lua function "setLockedUnitForFaction"
This commit is contained in:
titison 2014-11-12 15:14:52 +01:00
parent 04bc3b9f06
commit bcb3288d74
4 changed files with 62 additions and 0 deletions

View File

@ -252,6 +252,7 @@ void ScriptManager::init(World* world, GameCamera *gameCamera, const XmlNode *ro
luaScript.registerFunction(shakeCameraOnUnit, "shakeCameraOnUnit");
luaScript.registerFunction(createUnit, "createUnit");
luaScript.registerFunction(createUnitNoSpacing, "createUnitNoSpacing");
luaScript.registerFunction(setLockedUnitForFaction, "setLockedUnitForFaction");
luaScript.registerFunction(destroyUnit, "destroyUnit");
luaScript.registerFunction(giveKills, "giveKills");
luaScript.registerFunction(morphToUnit, "morphToUnit");
@ -1048,6 +1049,16 @@ void ScriptManager::createUnitNoSpacing(const string &unitName, int factionIndex
world->createUnit(unitName, factionIndex, pos, false);
}
void ScriptManager::setLockedUnitForFaction(const string &unitName, int factionIndex , bool lock){
if(SystemFlags::getSystemSettingType(SystemFlags::debugLUA).enabled) SystemFlags::OutputDebug(SystemFlags::debugLUA,"In [%s::%s Line: %d] unit [%s] factionIndex = %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,unitName.c_str(),factionIndex);
if(world->getFactionCount()>factionIndex) {
const UnitType *ut= world->getFaction(factionIndex)->getType()->getUnitType(unitName);
world->getFaction(factionIndex)->setLockedUnitForFaction(ut,lock);
} else {
throw megaglest_runtime_error("Invalid faction index in setLockedUnitForFaction: " + intToStr(factionIndex),true);
}
}
void ScriptManager::destroyUnit(int unitId){
if(SystemFlags::getSystemSettingType(SystemFlags::debugLUA).enabled) SystemFlags::OutputDebug(SystemFlags::debugLUA,"In [%s::%s Line: %d] unit [%d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,unitId);
Unit *unit = world->findUnitById(unitId);
@ -2320,6 +2331,33 @@ int ScriptManager::destroyUnit(LuaHandle* luaHandle) {
return luaArguments.getReturnCount();
}
int ScriptManager::setLockedUnitForFaction(LuaHandle* luaHandle) {
LuaArguments luaArguments(luaHandle);
if(SystemFlags::getSystemSettingType(SystemFlags::debugLUA).enabled) SystemFlags::OutputDebug(SystemFlags::debugLUA,"In [%s::%s Line: %d] unit [%s] factionIndex = %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,luaArguments.getString(-3).c_str(),luaArguments.getInt(-2));
try {
thisScriptManager->setLockedUnitForFaction(
luaArguments.getString(-3),
luaArguments.getInt(-2),
(luaArguments.getInt(-1) == 0 ? false : true));
}
catch(const megaglest_runtime_error &ex) {
char szErrBuf[8096]="";
snprintf(szErrBuf,8096,"In [%s::%s %d]",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__);
string sErrBuf = string(szErrBuf) + string("\nThe game may no longer be stable!\nerror [") + string(ex.what()) + string("]\n");
SystemFlags::OutputDebug(SystemFlags::debugError,sErrBuf.c_str());
if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,sErrBuf.c_str());
thisScriptManager->addMessageToQueue(ScriptManagerMessage(sErrBuf.c_str(), "error",-1,-1,true));
thisScriptManager->onMessageBoxOk(false);
}
return luaArguments.getReturnCount();
}
int ScriptManager::giveKills(LuaHandle* luaHandle) {
LuaArguments luaArguments(luaHandle);

View File

@ -282,6 +282,7 @@ private:
void createUnit(const string &unitName, int factionIndex, Vec2i pos);
void createUnitNoSpacing(const string &unitName, int factionIndex, Vec2i pos);
void setLockedUnitForFaction(const string &unitName, int factionIndex , bool lock);
void destroyUnit(int unitId);
void giveKills(int unitId, int amount);
void morphToUnit(int unitId,const string &morphName, int ignoreRequirements);
@ -448,6 +449,7 @@ private:
static int createUnit(LuaHandle* luaHandle);
static int createUnitNoSpacing(LuaHandle* luaHandle);
static int setLockedUnitForFaction(LuaHandle* luaHandle);
static int destroyUnit(LuaHandle* luaHandle);
static int giveKills(LuaHandle* luaHandle);
static int morphToUnit(LuaHandle* luaHandle);

View File

@ -673,6 +673,19 @@ bool Faction::canUnitsPathfind() {
return result;
}
void Faction::setLockedUnitForFaction(const UnitType *ut, bool lock) {
if (lock) {
LockedUnits.insert(ut);
} else {
std::set<const UnitType*>::iterator it;
it=LockedUnits.find(ut);
if(it!=LockedUnits.end()) {
LockedUnits.erase(it);
}
}
}
void Faction::signalWorkerThread(int frameIndex) {
if(workerThread != NULL) {
workerThread->signalPathfinder(frameIndex);
@ -913,6 +926,10 @@ bool Faction::reqsOk(const RequirableType *rt) const {
return false;
}
}
if(producedUnitType != NULL && isUnitLocked(producedUnitType)) {
return false;
}
}
return true;

View File

@ -174,6 +174,8 @@ private:
std::map<int,int> unitsMovingList;
std::map<int,int> unitsPathfindingList;
std::set<const UnitType*> LockedUnits;
TechTree *techTree;
const XmlNode *loadWorldNode;
@ -244,6 +246,9 @@ public:
void clearUnitsPathfinding();
bool canUnitsPathfind();
void setLockedUnitForFaction(const UnitType *ut, bool lock);
bool isUnitLocked(const UnitType *ut) const { return LockedUnits.find(ut)!=LockedUnits.end(); }
void init(
FactionType *factionType, ControlType control, TechTree *techTree, Game *game,
int factionIndex, int teamIndex, int startLocationIndex, bool thisFaction,