- added some more lua commands:
vector<int> getUnitsForFaction(factionIndex,commandTypeName, field) int getUnitCurrentField(unitId)
This commit is contained in:
parent
45b92f4316
commit
c02c90427b
|
@ -324,6 +324,9 @@ void ScriptManager::init(World* world, GameCamera *gameCamera, const XmlNode *ro
|
|||
|
||||
luaScript.registerFunction(loadScenario, "loadScenario");
|
||||
|
||||
luaScript.registerFunction(getUnitsForFaction, "getUnitsForFaction");
|
||||
luaScript.registerFunction(getUnitCurrentField, "getUnitCurrentField");
|
||||
|
||||
//load code
|
||||
for(int i= 0; i<scenario->getScriptCount(); ++i){
|
||||
const Script* script= scenario->getScript(i);
|
||||
|
@ -1294,45 +1297,24 @@ const string ScriptManager::getPlayerName(int factionIndex) {
|
|||
}
|
||||
|
||||
void ScriptManager::loadScenario(const string &name, bool keepFactions) {
|
||||
//printf("[%s:%s] Line: %d\n",__FILE__,__FUNCTION__,__LINE__);
|
||||
|
||||
if(SystemFlags::getSystemSettingType(SystemFlags::debugLUA).enabled) SystemFlags::OutputDebug(SystemFlags::debugLUA,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);
|
||||
ScriptManager_STREFLOP_Wrapper streflopWrapper;
|
||||
|
||||
world->setQueuedScenario(name,keepFactions);
|
||||
/*
|
||||
vector<string> results;
|
||||
const vector<string> &dirList = Config::getInstance().getPathListForType(ptScenarios);
|
||||
//findDirs(dirList, results);
|
||||
string scenarioFile = Scenario::getScenarioPath(dirList, name);
|
||||
}
|
||||
|
||||
//printf("\nname [%s] scenarioFile [%s] results.size() = %lu\n",name.c_str(),scenarioFile.c_str(),results.size());
|
||||
vector<int> ScriptManager::getUnitsForFaction(int factionIndex,const string& commandTypeName, int field) {
|
||||
if(SystemFlags::getSystemSettingType(SystemFlags::debugLUA).enabled) SystemFlags::OutputDebug(SystemFlags::debugLUA,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);
|
||||
ScriptManager_STREFLOP_Wrapper streflopWrapper;
|
||||
|
||||
//printf("[%s:%s] Line: %d\n",__FILE__,__FUNCTION__,__LINE__);
|
||||
ScenarioInfo scenarioInfo;
|
||||
Scenario::loadScenarioInfo(scenarioFile, &scenarioInfo);
|
||||
return world->getUnitsForFaction(factionIndex,commandTypeName, field);
|
||||
}
|
||||
|
||||
//printf("[%s:%s] Line: %d\n",__FILE__,__FUNCTION__,__LINE__);
|
||||
GameSettings gameSettings;
|
||||
Scenario::loadGameSettings(dirList,&scenarioInfo, &gameSettings, scenarioFile);
|
||||
int ScriptManager::getUnitCurrentField(int unitId) {
|
||||
if(SystemFlags::getSystemSettingType(SystemFlags::debugLUA).enabled) SystemFlags::OutputDebug(SystemFlags::debugLUA,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);
|
||||
ScriptManager_STREFLOP_Wrapper streflopWrapper;
|
||||
|
||||
//Program *program = world->getGame()->getProgram();
|
||||
//program->setState(new Game(program, &gameSettings, false));
|
||||
|
||||
//world->end();
|
||||
world->getGame()->setGameSettings(&gameSettings);
|
||||
//world->getMapPtr()->end();
|
||||
world->end();
|
||||
world->clearTileset();
|
||||
world->getGame()->load();
|
||||
world->getGame()->init();
|
||||
|
||||
//printf("[%s:%s] Line: %d\n",__FILE__,__FUNCTION__,__LINE__);
|
||||
//Checksum checksum;
|
||||
//world->loadScenario(scenarioFile, &checksum, true);
|
||||
|
||||
//printf("[%s:%s] Line: %d\n",__FILE__,__FUNCTION__,__LINE__);
|
||||
*/
|
||||
return world->getUnitCurrentField(unitId);
|
||||
}
|
||||
|
||||
// ========================== lua callbacks ===============================================
|
||||
|
@ -2095,13 +2077,25 @@ int ScriptManager::getGameWon(LuaHandle* luaHandle){
|
|||
}
|
||||
|
||||
int ScriptManager::loadScenario(LuaHandle* luaHandle) {
|
||||
//printf("[%s:%s] Line: %d\n",__FILE__,__FUNCTION__,__LINE__);
|
||||
|
||||
LuaArguments luaArguments(luaHandle);
|
||||
thisScriptManager->loadScenario(luaArguments.getString(-2),luaArguments.getInt(-1));
|
||||
return luaArguments.getReturnCount();
|
||||
}
|
||||
|
||||
int ScriptManager::getUnitsForFaction(LuaHandle* luaHandle) {
|
||||
LuaArguments luaArguments(luaHandle);
|
||||
vector<int> units= thisScriptManager->getUnitsForFaction(luaArguments.getInt(-3),luaArguments.getString(-2), luaArguments.getInt(-1));
|
||||
luaArguments.returnVectorInt(units);
|
||||
return luaArguments.getReturnCount();
|
||||
|
||||
}
|
||||
|
||||
int ScriptManager::getUnitCurrentField(LuaHandle* luaHandle) {
|
||||
LuaArguments luaArguments(luaHandle);
|
||||
luaArguments.returnInt(thisScriptManager->getUnitCurrentField(luaArguments.getInt(-1)));
|
||||
return luaArguments.getReturnCount();
|
||||
}
|
||||
|
||||
void ScriptManager::saveGame(XmlNode *rootNode) {
|
||||
std::map<string,string> mapTagReplacements;
|
||||
XmlNode *scriptManagerNode = rootNode->addChild("ScriptManager");
|
||||
|
|
|
@ -316,6 +316,9 @@ private:
|
|||
const string getSystemMacroValue(const string &key);
|
||||
const string getPlayerName(int factionIndex);
|
||||
|
||||
vector<int> getUnitsForFaction(int factionIndex,const string& commandTypeName, int field);
|
||||
int getUnitCurrentField(int unitId);
|
||||
|
||||
void loadScenario(const string &name, bool keepFactions);
|
||||
|
||||
//callbacks, commands
|
||||
|
@ -415,6 +418,9 @@ private:
|
|||
static int scenarioDir(LuaHandle* luaHandle);
|
||||
|
||||
static int loadScenario(LuaHandle* luaHandle);
|
||||
|
||||
static int getUnitsForFaction(LuaHandle* luaHandle);
|
||||
static int getUnitCurrentField(LuaHandle* luaHandle);
|
||||
};
|
||||
|
||||
}}//end namespace
|
||||
|
|
|
@ -926,6 +926,63 @@ void World::giveResource(const string &resourceName, int factionIndex, int amoun
|
|||
}
|
||||
}
|
||||
|
||||
int World::getUnitCurrentField(int unitId) {
|
||||
int field = -1;
|
||||
if(SystemFlags::getSystemSettingType(SystemFlags::debugLUA).enabled) SystemFlags::OutputDebug(SystemFlags::debugLUA,"In [%s::%s Line: %d] unit [%d]\n",__FILE__,__FUNCTION__,__LINE__,unitId);
|
||||
Unit* unit= findUnitById(unitId);
|
||||
if(unit != NULL) {
|
||||
field = unit->getCurrField();
|
||||
}
|
||||
|
||||
return field;
|
||||
}
|
||||
|
||||
vector<int> World::getUnitsForFaction(int factionIndex,const string& commandTypeName, int field) {
|
||||
vector<int> units;
|
||||
|
||||
if(factionIndex < 0 || factionIndex > getFactionCount()) {
|
||||
throw runtime_error("Invalid faction index in getUnitsForFaction: " + intToStr(factionIndex));
|
||||
}
|
||||
Faction *faction = getFaction(factionIndex);
|
||||
if(faction != NULL) {
|
||||
CommandType *commandType = NULL;
|
||||
if(commandTypeName != "") {
|
||||
commandType = CommandTypeFactory::getInstance().newInstance(commandTypeName);
|
||||
}
|
||||
int unitCount = faction->getUnitCount();
|
||||
for(int i = 0; i < unitCount; ++i) {
|
||||
Unit *unit = faction->getUnit(i);
|
||||
if(unit != NULL) {
|
||||
bool addToList = true;
|
||||
if(commandType != NULL) {
|
||||
if(commandType->getClass() == ccAttack && field >= 0) {
|
||||
const AttackCommandType *act = unit->getType()->getFirstAttackCommand(static_cast<Field>(field));
|
||||
addToList = (act != NULL);
|
||||
}
|
||||
else if(commandType->getClass() == ccAttackStopped && field >= 0) {
|
||||
const AttackStoppedCommandType *asct = unit->getType()->getFirstAttackStoppedCommand(static_cast<Field>(field));
|
||||
addToList = (asct != NULL);
|
||||
}
|
||||
else {
|
||||
addToList = unit->getType()->hasCommandClass(commandType->getClass());
|
||||
}
|
||||
}
|
||||
else if(field >= 0) {
|
||||
addToList = (unit->getCurrField() == static_cast<Field>(field));
|
||||
}
|
||||
|
||||
if(addToList == true) {
|
||||
units.push_back(unit->getId());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
delete commandType;
|
||||
commandType = NULL;
|
||||
}
|
||||
return units;
|
||||
}
|
||||
|
||||
void World::givePositionCommand(int unitId, const string &commandName, const Vec2i &pos) {
|
||||
Unit* unit= findUnitById(unitId);
|
||||
if(unit != NULL) {
|
||||
|
|
|
@ -220,6 +220,8 @@ public:
|
|||
void morphToUnit(int unitId,const string &morphName,bool ignoreRequirements);
|
||||
void 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);
|
||||
void giveAttackCommand(int unitId, int unitToAttackId);
|
||||
void giveProductionCommand(int unitId, const string &producedName);
|
||||
void giveUpgradeCommand(int unitId, const string &upgradeName);
|
||||
|
|
|
@ -80,6 +80,7 @@ public:
|
|||
void returnInt(int value);
|
||||
void returnString(const string &value);
|
||||
void returnVec2i(const Vec2i &value);
|
||||
void returnVectorInt(const vector<int> &value);
|
||||
|
||||
private:
|
||||
void throwLuaError(const string &message) const;
|
||||
|
|
|
@ -614,6 +614,19 @@ void LuaArguments::returnVec2i(const Vec2i &value){
|
|||
lua_rawseti(luaState, -2, 2);
|
||||
}
|
||||
|
||||
void LuaArguments::returnVectorInt(const vector<int> &value) {
|
||||
//Lua_STREFLOP_Wrapper streflopWrapper;
|
||||
|
||||
++returnCount;
|
||||
|
||||
lua_newtable(luaState);
|
||||
|
||||
for(unsigned int i = 0; i < value.size(); ++i) {
|
||||
lua_pushnumber(luaState, value[i]);
|
||||
lua_rawseti(luaState, -2, i+1);
|
||||
}
|
||||
}
|
||||
|
||||
void LuaArguments::throwLuaError(const string &message) const{
|
||||
Lua_STREFLOP_Wrapper streflopWrapper;
|
||||
|
||||
|
|
Loading…
Reference in New Issue