diff --git a/source/g3d_viewer/main.cpp b/source/g3d_viewer/main.cpp index daddfdde..6ae47302 100644 --- a/source/g3d_viewer/main.cpp +++ b/source/g3d_viewer/main.cpp @@ -199,7 +199,7 @@ MainWindow::MainWindow( std::pair > unitToLoad, if(modelPath != "") { this->modelPathList.push_back(modelPath); - printf("Startup Adding model [%s] list size %d\n",modelPath.c_str(),this->modelPathList.size()); + printf("Startup Adding model [%s] list size %lu\n",modelPath.c_str(),this->modelPathList.size()); } if(particlePath != "") { this->particlePathList.push_back(particlePath); @@ -924,14 +924,14 @@ void MainWindow::loadModel(string path) { try { if(path != "" && fileExists(path) == true) { this->modelPathList.push_back(path); - printf("Adding model [%s] list size %d\n",path.c_str(),this->modelPathList.size()); + printf("Adding model [%s] list size %lu\n",path.c_str(),this->modelPathList.size()); } string titlestring=winHeader; for(unsigned int idx =0; idx < this->modelPathList.size(); idx++) { string modelPath = this->modelPathList[idx]; - printf("Loading model [%s] %d of %d\n",modelPath.c_str(),idx, this->modelPathList.size()); + printf("Loading model [%s] %u of %lu\n",modelPath.c_str(),idx, this->modelPathList.size()); timer->Stop(); delete model; diff --git a/source/glest_game/game/script_manager.cpp b/source/glest_game/game/script_manager.cpp index 25d5c6b5..dfe19866 100644 --- a/source/glest_game/game/script_manager.cpp +++ b/source/glest_game/game/script_manager.cpp @@ -82,11 +82,18 @@ void ScriptManager::init(World* world, GameCamera *gameCamera){ luaScript.registerFunction(clearDisplayText, "clearDisplayText"); luaScript.registerFunction(setCameraPosition, "setCameraPosition"); luaScript.registerFunction(createUnit, "createUnit"); + luaScript.registerFunction(destroyUnit, "destroyUnit"); + luaScript.registerFunction(morphToUnit, "morphToUnit"); + luaScript.registerFunction(playStaticSound, "playStaticSound"); + luaScript.registerFunction(playStreamingSound, "playStreamingSound"); + luaScript.registerFunction(stopStreamingSound, "stopStreamingSound"); + luaScript.registerFunction(stopAllSound, "stopAllSound"); luaScript.registerFunction(giveResource, "giveResource"); luaScript.registerFunction(givePositionCommand, "givePositionCommand"); luaScript.registerFunction(giveProductionCommand, "giveProductionCommand"); luaScript.registerFunction(giveAttackCommand, "giveAttackCommand"); luaScript.registerFunction(giveUpgradeCommand, "giveUpgradeCommand"); + luaScript.registerFunction(giveAttackStoppedCommand, "giveAttackStoppedCommand"); luaScript.registerFunction(disableAi, "disableAi"); luaScript.registerFunction(enableAi, "enableAi"); luaScript.registerFunction(getAiEnabled, "getAiEnabled"); @@ -412,6 +419,48 @@ void ScriptManager::createUnit(const string &unitName, int factionIndex, Vec2i p world->createUnit(unitName, factionIndex, pos); } +void ScriptManager::destroyUnit(int unitId){ + SystemFlags::OutputDebug(SystemFlags::debugLUA,"In [%s::%s Line: %d] unit [%d]\n",__FILE__,__FUNCTION__,__LINE__,unitId); + ScriptManager_STREFLOP_Wrapper streflopWrapper; + Unit *unit = world->findUnitById(unitId); + if(unit != NULL) { + // Make sure they die + unit->decHp(unit->getHp() * unit->getHp()); + unit->kill(); + // If called from an existing die event we get a stack overflow + //onUnitDied(unit); + } +} + +void ScriptManager::playStaticSound(const string &playSound) { + SystemFlags::OutputDebug(SystemFlags::debugLUA,"In [%s::%s Line: %d] playSound [%s]\n",__FILE__,__FUNCTION__,__LINE__,playSound.c_str()); + ScriptManager_STREFLOP_Wrapper streflopWrapper; + world->playStaticSound(playSound); +} +void ScriptManager::playStreamingSound(const string &playSound) { + SystemFlags::OutputDebug(SystemFlags::debugLUA,"In [%s::%s Line: %d] playSound [%s]\n",__FILE__,__FUNCTION__,__LINE__,playSound.c_str()); + ScriptManager_STREFLOP_Wrapper streflopWrapper; + world->playStreamingSound(playSound); +} + +void ScriptManager::stopStreamingSound(const string &playSound) { + SystemFlags::OutputDebug(SystemFlags::debugLUA,"In [%s::%s Line: %d] playSound [%s]\n",__FILE__,__FUNCTION__,__LINE__,playSound.c_str()); + ScriptManager_STREFLOP_Wrapper streflopWrapper; + world->stopStreamingSound(playSound); +} + +void ScriptManager::stopAllSound() { + SystemFlags::OutputDebug(SystemFlags::debugLUA,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); + ScriptManager_STREFLOP_Wrapper streflopWrapper; + world->stopAllSound(); +} + +void ScriptManager::morphToUnit(int unitId,const string &morphName, int ignoreRequirements) { + SystemFlags::OutputDebug(SystemFlags::debugLUA,"In [%s::%s Line: %d] unit [%d] morphName [%s] forceUpgradesIfRequired = %d\n",__FILE__,__FUNCTION__,__LINE__,unitId,morphName.c_str(),ignoreRequirements); + ScriptManager_STREFLOP_Wrapper streflopWrapper; + world->morphToUnit(unitId,morphName,ignoreRequirements); +} + void ScriptManager::giveResource(const string &resourceName, int factionIndex, int amount){ SystemFlags::OutputDebug(SystemFlags::debugLUA,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); ScriptManager_STREFLOP_Wrapper streflopWrapper; @@ -442,6 +491,12 @@ void ScriptManager::giveUpgradeCommand(int unitId, const string &producedName){ world->giveUpgradeCommand(unitId, producedName); } +void ScriptManager::giveAttackStoppedCommand(int unitId, const string &itemName,int ignoreRequirements) { + SystemFlags::OutputDebug(SystemFlags::debugLUA,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); + ScriptManager_STREFLOP_Wrapper streflopWrapper; + world->giveAttackStoppedCommand(unitId, itemName, ignoreRequirements); +} + void ScriptManager::disableAi(int factionIndex){ SystemFlags::OutputDebug(SystemFlags::debugLUA,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); ScriptManager_STREFLOP_Wrapper streflopWrapper; @@ -780,7 +835,7 @@ int ScriptManager::setCameraPosition(LuaHandle* luaHandle){ return luaArguments.getReturnCount(); } -int ScriptManager::createUnit(LuaHandle* luaHandle){ +int ScriptManager::createUnit(LuaHandle* luaHandle) { LuaArguments luaArguments(luaHandle); SystemFlags::OutputDebug(SystemFlags::debugLUA,"In [%s::%s Line: %d] unit [%s] factionIndex = %d\n",__FILE__,__FUNCTION__,__LINE__,luaArguments.getString(-3).c_str(),luaArguments.getInt(-2)); @@ -792,6 +847,52 @@ int ScriptManager::createUnit(LuaHandle* luaHandle){ return luaArguments.getReturnCount(); } +int ScriptManager::destroyUnit(LuaHandle* luaHandle) { + LuaArguments luaArguments(luaHandle); + + SystemFlags::OutputDebug(SystemFlags::debugLUA,"In [%s::%s Line: %d] unit [%d]\n",__FILE__,__FUNCTION__,__LINE__,luaArguments.getInt(-1)); + + thisScriptManager->destroyUnit(luaArguments.getInt(-1)); + return luaArguments.getReturnCount(); +} + +int ScriptManager::morphToUnit(LuaHandle* luaHandle) { + LuaArguments luaArguments(luaHandle); + + SystemFlags::OutputDebug(SystemFlags::debugLUA,"In [%s::%s Line: %d] unit [%d] morphName [%s] forceUpgrade = %d\n",__FILE__,__FUNCTION__,__LINE__,luaArguments.getInt(-3),luaArguments.getString(-2).c_str(),luaArguments.getInt(-1)); + + thisScriptManager->morphToUnit(luaArguments.getInt(-3),luaArguments.getString(-2),luaArguments.getInt(-1)); + return luaArguments.getReturnCount(); +} + +int ScriptManager::playStaticSound(LuaHandle* luaHandle) { + LuaArguments luaArguments(luaHandle); + SystemFlags::OutputDebug(SystemFlags::debugLUA,"In [%s::%s Line: %d] sound [%s]\n",__FILE__,__FUNCTION__,__LINE__,luaArguments.getString(-1).c_str()); + thisScriptManager->playStaticSound(luaArguments.getString(-1)); + return luaArguments.getReturnCount(); +} + +int ScriptManager::playStreamingSound(LuaHandle* luaHandle) { + LuaArguments luaArguments(luaHandle); + SystemFlags::OutputDebug(SystemFlags::debugLUA,"In [%s::%s Line: %d] sound [%s]\n",__FILE__,__FUNCTION__,__LINE__,luaArguments.getString(-1).c_str()); + thisScriptManager->playStreamingSound(luaArguments.getString(-1)); + return luaArguments.getReturnCount(); +} + +int ScriptManager::stopStreamingSound(LuaHandle* luaHandle) { + LuaArguments luaArguments(luaHandle); + SystemFlags::OutputDebug(SystemFlags::debugLUA,"In [%s::%s Line: %d] sound [%s]\n",__FILE__,__FUNCTION__,__LINE__,luaArguments.getString(-1).c_str()); + thisScriptManager->stopStreamingSound(luaArguments.getString(-1)); + return luaArguments.getReturnCount(); +} + +int ScriptManager::stopAllSound(LuaHandle* luaHandle) { + LuaArguments luaArguments(luaHandle); + SystemFlags::OutputDebug(SystemFlags::debugLUA,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); + thisScriptManager->stopAllSound(); + return luaArguments.getReturnCount(); +} + int ScriptManager::giveResource(LuaHandle* luaHandle){ LuaArguments luaArguments(luaHandle); thisScriptManager->giveResource(luaArguments.getString(-3), luaArguments.getInt(-2), luaArguments.getInt(-1)); @@ -831,6 +932,15 @@ int ScriptManager::giveUpgradeCommand(LuaHandle* luaHandle){ return luaArguments.getReturnCount(); } +int ScriptManager::giveAttackStoppedCommand(LuaHandle* luaHandle){ + LuaArguments luaArguments(luaHandle); + thisScriptManager->giveAttackStoppedCommand( + luaArguments.getInt(-3), + luaArguments.getString(-2), + luaArguments.getInt(-1)); + return luaArguments.getReturnCount(); +} + int ScriptManager::disableAi(LuaHandle* luaHandle){ LuaArguments luaArguments(luaHandle); thisScriptManager->disableAi(luaArguments.getInt(-1)); diff --git a/source/glest_game/game/script_manager.h b/source/glest_game/game/script_manager.h index 8be1f911..a9d07a31 100644 --- a/source/glest_game/game/script_manager.h +++ b/source/glest_game/game/script_manager.h @@ -176,11 +176,21 @@ private: void DisplayFormattedText(const char *fmt,...); void setCameraPosition(const Vec2i &pos); void createUnit(const string &unitName, int factionIndex, Vec2i pos); + + void destroyUnit(int unitId); + void morphToUnit(int unitId,const string &morphName, int ignoreRequirements); + void giveAttackStoppedCommand(int unitId, const string &valueName,int ignoreRequirements); + void playStaticSound(const string &playSound); + void playStreamingSound(const string &playSound); + void stopStreamingSound(const string &playSound); + void stopAllSound(); + void giveResource(const string &resourceName, int factionIndex, int amount); void givePositionCommand(int unitId, const string &producedName, const Vec2i &pos); void giveProductionCommand(int unitId, const string &producedName); void giveAttackCommand(int unitId, int unitToAttackId); void giveUpgradeCommand(int unitId, const string &upgradeName); + void disableAi(int factionIndex); void enableAi(int factionIndex); void disableConsume(int factionIndex); @@ -230,6 +240,15 @@ private: static int clearDisplayText(LuaHandle* luaHandle); static int setCameraPosition(LuaHandle* luaHandle); static int createUnit(LuaHandle* luaHandle); + + static int destroyUnit(LuaHandle* luaHandle); + static int morphToUnit(LuaHandle* luaHandle); + static int giveAttackStoppedCommand(LuaHandle* luaHandle); + static int playStaticSound(LuaHandle* luaHandle); + static int playStreamingSound(LuaHandle* luaHandle); + static int stopStreamingSound(LuaHandle* luaHandle); + static int stopAllSound(LuaHandle* luaHandle); + static int giveResource(LuaHandle* luaHandle); static int givePositionCommand(LuaHandle* luaHandle); static int giveProductionCommand(LuaHandle* luaHandle); diff --git a/source/glest_game/type_instances/faction.cpp b/source/glest_game/type_instances/faction.cpp index 47340f0d..3d1dad87 100644 --- a/source/glest_game/type_instances/faction.cpp +++ b/source/glest_game/type_instances/faction.cpp @@ -158,27 +158,29 @@ void Faction::finishUpgrade(const UpgradeType *ut){ // ==================== reqs ==================== //checks if all required units and upgrades are present and maxUnitCount is within limit -bool Faction::reqsOk(const RequirableType *rt) const{ +bool Faction::reqsOk(const RequirableType *rt) const { assert(rt != NULL); //required units - for(int i=0; igetUnitReqCount(); ++i){ - bool found=false; - for(int j=0; jgetUnitReqCount(); ++i) { + bool found = false; + for(int j = 0; j < getUnitCount(); ++j) { Unit *unit= getUnit(j); const UnitType *ut= unit->getType(); - if(rt->getUnitReq(i)==ut && unit->isOperative()){ + if(rt->getUnitReq(i) == ut && unit->isOperative()) { found= true; break; } } - if(!found){ + if(found == false) { + SystemFlags::OutputDebug(SystemFlags::debugLUA,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__, __LINE__); return false; } } //required upgrades - for(int i=0; igetUpgradeReqCount(); ++i) { + for(int i = 0; i < rt->getUpgradeReqCount(); ++i) { if(upgradeManager.isUpgraded(rt->getUpgradeReq(i)) == false) { + SystemFlags::OutputDebug(SystemFlags::debugLUA,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__, __LINE__); return false; } } @@ -187,6 +189,7 @@ bool Faction::reqsOk(const RequirableType *rt) const{ const UnitType *producedUnitType=(UnitType *) rt; if(producedUnitType != NULL && producedUnitType->getMaxUnitCount() > 0) { if(producedUnitType->getMaxUnitCount() <= getCountForMaxUnitCount(producedUnitType)) { + SystemFlags::OutputDebug(SystemFlags::debugLUA,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__, __LINE__); return false; } } @@ -218,12 +221,14 @@ bool Faction::reqsOk(const CommandType *ct) const { } if(ct->getProduced() != NULL && reqsOk(ct->getProduced()) == false) { + SystemFlags::OutputDebug(SystemFlags::debugLUA,"In [%s::%s Line: %d] reqsOk FAILED\n",__FILE__,__FUNCTION__,__LINE__); return false; } if(ct->getClass() == ccUpgrade) { const UpgradeCommandType *uct= static_cast(ct); if(upgradeManager.isUpgradingOrUpgraded(uct->getProducedUpgrade())) { + SystemFlags::OutputDebug(SystemFlags::debugLUA,"In [%s::%s Line: %d] upgrade check FAILED\n",__FILE__,__FUNCTION__,__LINE__); return false; } } diff --git a/source/glest_game/type_instances/unit.cpp b/source/glest_game/type_instances/unit.cpp index f35fd7f4..076682bb 100644 --- a/source/glest_game/type_instances/unit.cpp +++ b/source/glest_game/type_instances/unit.cpp @@ -182,6 +182,7 @@ Unit::Unit(int id, UnitPathInterface *unitpath, const Vec2i &pos, const UnitType this->visible = true; this->retryCurrCommandCount=0; this->screenPos = Vec3f(0.0); + this->ignoreCheckCommand = false; this->inBailOutAttempt = false; this->lastHarvestResourceTarget.first = Vec2i(0); //this->lastBadHarvestListPurge = 0; @@ -854,6 +855,7 @@ CommandResult Unit::finishCommand() { this->setCurrentUnitTitle(""); //is empty? if(commands.empty()) { + SystemFlags::OutputDebug(SystemFlags::debugLUA,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__, __LINE__); return crFailUndefined; } @@ -882,6 +884,7 @@ CommandResult Unit::cancelCommand() { //is empty? if(commands.empty()){ + SystemFlags::OutputDebug(SystemFlags::debugLUA,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__, __LINE__); return crFailUndefined; } @@ -1506,7 +1509,8 @@ CommandResult Unit::checkCommand(Command *command) const { if(isOperative() == false || command->getUnit() == this || getType()->hasCommandType(command->getCommandType()) == false || - this->getFaction()->reqsOk(command->getCommandType()) == false) { + (ignoreCheckCommand == false && this->getFaction()->reqsOk(command->getCommandType()) == false)) { + SystemFlags::OutputDebug(SystemFlags::debugLUA,"In [%s::%s Line: %d] isOperative() = %d, command->getUnit() = %p, getType()->hasCommandType(command->getCommandType()) = %d, this->getFaction()->reqsOk(command->getCommandType()) = %d\n",__FILE__,__FUNCTION__, __LINE__,isOperative(),command->getUnit(),getType()->hasCommandType(command->getCommandType()),this->getFaction()->reqsOk(command->getCommandType())); return crFailUndefined; } @@ -1514,6 +1518,7 @@ CommandResult Unit::checkCommand(Command *command) const { //if pos is not inside the world (if comand has not a pos, pos is (0, 0) and is inside world if(map->isInside(command->getPos()) == false) { + SystemFlags::OutputDebug(SystemFlags::debugLUA,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__, __LINE__); return crFailUndefined; } @@ -1528,11 +1533,11 @@ CommandResult Unit::checkCommand(Command *command) const { const ProducibleType *produced= command->getCommandType()->getProduced(); if(produced!=NULL) { - if(faction->reqsOk(produced) == false) { + if(ignoreCheckCommand == false && faction->reqsOk(produced) == false) { return crFailReqs; } - if(faction->checkCosts(produced) == false) { + if(ignoreCheckCommand == false && faction->checkCosts(produced) == false) { return crFailRes; } } @@ -1557,7 +1562,7 @@ CommandResult Unit::checkCommand(Command *command) const { } } //upgrade command specific, check that upgrade is not upgraded - else if(command->getCommandType()->getClass()==ccUpgrade) { + else if(command->getCommandType()->getClass() == ccUpgrade) { const UpgradeCommandType *uct= static_cast(command->getCommandType()); if(uct == NULL) { @@ -1567,6 +1572,7 @@ CommandResult Unit::checkCommand(Command *command) const { } if(faction->getUpgradeManager()->isUpgradingOrUpgraded(uct->getProducedUpgrade())){ + SystemFlags::OutputDebug(SystemFlags::debugLUA,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__, __LINE__); return crFailUndefined; } } diff --git a/source/glest_game/type_instances/unit.h b/source/glest_game/type_instances/unit.h index b342005d..8305b2df 100644 --- a/source/glest_game/type_instances/unit.h +++ b/source/glest_game/type_instances/unit.h @@ -313,6 +313,8 @@ private: static Game *game; + bool ignoreCheckCommand; + public: Unit(int id, UnitPathInterface *path, const Vec2i &pos, const UnitType *type, Faction *faction, Map *map, CardinalDir placeFacing); ~Unit(); @@ -320,6 +322,8 @@ public: static void setGame(Game *value) { game=value;} //queries + void setIgnoreCheckCommand(bool value) { ignoreCheckCommand=value;} + bool getIgnoreCheckCommand() const {return ignoreCheckCommand;} int getId() const {return id;} Field getCurrField() const {return currField;} int getLoadCount() const {return loadCount;} diff --git a/source/glest_game/world/world.cpp b/source/glest_game/world/world.cpp index 0816ac23..0b89270b 100644 --- a/source/glest_game/world/world.cpp +++ b/source/glest_game/world/world.cpp @@ -23,8 +23,9 @@ #include "game_settings.h" #include "cache_manager.h" #include "route_planner.h" - #include +#include "sound.h" +#include "sound_renderer.h" #include "leak_dumper.h" @@ -101,6 +102,18 @@ World::~World() { delete routePlanner; routePlanner = 0; + for(std::map::iterator iterMap = staticSoundList.begin(); + iterMap != staticSoundList.end(); iterMap++) { + delete iterMap->second; + } + staticSoundList.clear(); + + for(std::map::iterator iterMap = streamSoundList.begin(); + iterMap != streamSoundList.end(); iterMap++) { + delete iterMap->second; + } + streamSoundList.clear(); + SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); delete cartographer; @@ -121,6 +134,7 @@ void World::end(){ } factions.clear(); fogOfWarOverride = false; + //stats will be deleted by BattleEnd SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); } @@ -639,6 +653,53 @@ bool World::toRenderUnit(const Unit *unit) const { map.getSurfaceCell(Map::toSurfCoords(unit->getTargetPos()))->isExplored(thisTeamIndex)); } +void World::morphToUnit(int unitId,const string &morphName,bool ignoreRequirements) { + SystemFlags::OutputDebug(SystemFlags::debugLUA,"In [%s::%s Line: %d] unit [%d] morphName [%s] forceUpgradesIfRequired = %d\n",__FILE__,__FUNCTION__,__LINE__,unitId,morphName.c_str(),ignoreRequirements); + Unit* unit= findUnitById(unitId); + if(unit != NULL) { + //const SkillType *st = unit->getType()->getSkillType(morphName, scMorph); + + for(int i = 0; i < unit->getType()->getCommandTypeCount(); ++i) { + const CommandType *ct = unit->getType()->getCommandType(i); + const MorphCommandType *mct = dynamic_cast(ct); + if(mct != NULL && mct->getName() == morphName) { + SystemFlags::OutputDebug(SystemFlags::debugLUA,"In [%s::%s Line: %d] unit [%d] morphName [%s] comparing mct [%s]\n",__FILE__,__FUNCTION__,__LINE__,unitId,morphName.c_str(),mct->getName().c_str()); + + CommandResult cr = crFailUndefined; + try { + if(unit->getFaction()->reqsOk(mct) == false && ignoreRequirements == true) { + SystemFlags::OutputDebug(SystemFlags::debugLUA,"In [%s::%s Line: %d] unit [%d] morphName [%s] comparing mct [%s] mct->getUpgradeReqCount() = %d\n",__FILE__,__FUNCTION__,__LINE__,unitId,morphName.c_str(),mct->getName().c_str(),mct->getUpgradeReqCount()); + unit->setIgnoreCheckCommand(true); + } + + const UnitType* unitType = mct->getMorphUnit(); + cr = this->game->getCommander()->tryGiveCommand(unit, mct,unit->getPos(), unitType,CardinalDir::NORTH); + } + catch(const exception &ex) { + SystemFlags::OutputDebug(SystemFlags::debugLUA,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); + unit->setIgnoreCheckCommand(false); + + throw runtime_error(ex.what()); + } + + if(cr == crSuccess) { + SystemFlags::OutputDebug(SystemFlags::debugLUA,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); + } + else { + SystemFlags::OutputDebug(SystemFlags::debugLUA,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); + } + + SystemFlags::OutputDebug(SystemFlags::debugLUA,"In [%s::%s Line: %d] unit [%d] morphName [%s] comparing mct [%s] returned = %d\n",__FILE__,__FUNCTION__,__LINE__,unitId,morphName.c_str(),mct->getName().c_str(),cr); + + break; + } + } + + + SystemFlags::OutputDebug(SystemFlags::debugLUA,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); + } +} + void World::createUnit(const string &unitName, int factionIndex, const Vec2i &pos) { SystemFlags::OutputDebug(SystemFlags::debugUnitCommands,"In [%s::%s Line: %d] unitName [%s] factionIndex = %d\n",__FILE__,__FUNCTION__,__LINE__,unitName.c_str(),factionIndex); @@ -772,6 +833,92 @@ void World::giveProductionCommand(int unitId, const string &producedName) { } } +void World::giveAttackStoppedCommand(int unitId, const string &itemName, bool ignoreRequirements) { + Unit *unit= findUnitById(unitId); + if(unit != NULL) { + const UnitType *ut= unit->getType(); + + //Search for a command that can produce the unit + for(int i= 0; i < ut->getCommandTypeCount(); ++i) { + const CommandType* ct= ut->getCommandType(i); + if(ct != NULL && ct->getClass() == ccAttackStopped) { + const AttackStoppedCommandType *act= static_cast(ct); + if(act != NULL && act->getName() == itemName) { + SystemFlags::OutputDebug(SystemFlags::debugUnitCommands,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); + + try { + if(unit->getFaction()->reqsOk(act) == false && ignoreRequirements == true) { + SystemFlags::OutputDebug(SystemFlags::debugLUA,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); + unit->setIgnoreCheckCommand(true); + } + + unit->giveCommand(new Command(act)); + } + catch(const exception &ex) { + SystemFlags::OutputDebug(SystemFlags::debugLUA,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); + unit->setIgnoreCheckCommand(false); + + throw runtime_error(ex.what()); + } + + + SystemFlags::OutputDebug(SystemFlags::debugUnitCommands,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); + break; + } + } + } + } + else { + throw runtime_error("Invalid unitId index in giveAttackStoppedCommand: " + intToStr(unitId) + " itemName = " + itemName); + } +} + +void World::playStaticSound(const string &playSound) { + SystemFlags::OutputDebug(SystemFlags::debugLUA,"In [%s::%s Line: %d] playSound [%s]\n",__FILE__,__FUNCTION__,__LINE__,playSound.c_str()); + + SoundRenderer &soundRenderer= SoundRenderer::getInstance(); + + if(staticSoundList.find(playSound) == staticSoundList.end()) { + StaticSound *sound = new StaticSound(); + sound->load(playSound); + staticSoundList[playSound] = sound; + } + StaticSound *playSoundItem = staticSoundList[playSound]; + soundRenderer.playFx(playSoundItem); +} + +void World::playStreamingSound(const string &playSound) { + SystemFlags::OutputDebug(SystemFlags::debugLUA,"In [%s::%s Line: %d] playSound [%s]\n",__FILE__,__FUNCTION__,__LINE__,playSound.c_str()); + + SoundRenderer &soundRenderer= SoundRenderer::getInstance(); + + if(streamSoundList.find(playSound) == streamSoundList.end()) { + StrSound *sound = new StrSound(); + sound->open(playSound); + sound->setNext(sound); + streamSoundList[playSound] = sound; + } + StrSound *playSoundItem = streamSoundList[playSound]; + soundRenderer.playMusic(playSoundItem); +} + +void World::stopStreamingSound(const string &playSound) { + SystemFlags::OutputDebug(SystemFlags::debugLUA,"In [%s::%s Line: %d] playSound [%s]\n",__FILE__,__FUNCTION__,__LINE__,playSound.c_str()); + SoundRenderer &soundRenderer= SoundRenderer::getInstance(); + + if(streamSoundList.find(playSound) != streamSoundList.end()) { + StrSound *playSoundItem = streamSoundList[playSound]; + soundRenderer.stopMusic(playSoundItem); + } +} + +void World::stopAllSound() { + SystemFlags::OutputDebug(SystemFlags::debugLUA,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); + SoundRenderer &soundRenderer= SoundRenderer::getInstance(); + soundRenderer.stopAllSounds(); +} + + void World::giveUpgradeCommand(int unitId, const string &upgradeName) { Unit *unit= findUnitById(unitId); if(unit != NULL) { diff --git a/source/glest_game/world/world.h b/source/glest_game/world/world.h index aa841a84..bf1eda6b 100644 --- a/source/glest_game/world/world.h +++ b/source/glest_game/world/world.h @@ -46,6 +46,8 @@ class GameSettings; class ScriptManager; class Cartographer; class RoutePlanner; +class StaticSound; +class StrSound; // ===================================================== // class World @@ -134,6 +136,8 @@ private: bool perfTimerEnabled; bool staggeredFactionUpdates; + std::map staticSoundList; + std::map streamSoundList; public: World(); @@ -186,11 +190,18 @@ public: void addAttackEffects(const Unit *unit); //scripting interface + void morphToUnit(int unitId,const string &morphName,bool ignoreRequirements); void createUnit(const string &unitName, int factionIndex, const Vec2i &pos); void givePositionCommand(int unitId, const string &commandName, const Vec2i &pos); void giveAttackCommand(int unitId, int unitToAttackId); void giveProductionCommand(int unitId, const string &producedName); void giveUpgradeCommand(int unitId, const string &upgradeName); + void giveAttackStoppedCommand(int unitId, const string &itemName,bool ignoreRequirements); + void playStaticSound(const string &playSound); + void playStreamingSound(const string &playSound); + void stopStreamingSound(const string &playSound); + void stopAllSound(); + void giveResource(const string &resourceName, int factionIndex, int amount); int getResourceAmount(const string &resourceName, int factionIndex); Vec2i getStartLocation(int factionIndex);