Author Topic: [done] giveMorphCommand  (Read 702 times)

MuwuM

  • Ornithopter
  • *****
  • Posts: 426
  • No Game without Move(ment)
    • View Profile
    • MuwuM - Lexicons
[done] giveMorphCommand
« on: 16 May 2012, 15:01:11 »
I'd like to have a Lua-method to give a unit the command to morph into another unit.

Patch for SVN head:
Code: [Select]
Index: source/glest_game/game/script_manager.cpp
===================================================================
--- source/glest_game/game/script_manager.cpp   (revision 3326)
+++ source/glest_game/game/script_manager.cpp   (working copy)
@@ -265,6 +265,7 @@
    luaScript.registerFunction(giveResource, "giveResource");
    luaScript.registerFunction(givePositionCommand, "givePositionCommand");
    luaScript.registerFunction(giveProductionCommand, "giveProductionCommand");
+   luaScript.registerFunction(giveMorphCommand, "giveMorphCommand");
    luaScript.registerFunction(giveAttackCommand, "giveAttackCommand");
    luaScript.registerFunction(giveUpgradeCommand, "giveUpgradeCommand");
    luaScript.registerFunction(giveAttackStoppedCommand, "giveAttackStoppedCommand");
@@ -925,7 +926,11 @@
    ScriptManager_STREFLOP_Wrapper streflopWrapper;
    world->giveUpgradeCommand(unitId, producedName);
 }
-
+void ScriptManager::giveMorphCommand(int unitId, const string &producedName){
+   if(SystemFlags::getSystemSettingType(SystemFlags::debugLUA).enabled) SystemFlags::OutputDebug(SystemFlags::debugLUA,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);
+   ScriptManager_STREFLOP_Wrapper streflopWrapper;
+   world->giveMorphCommand(unitId, producedName);
+}
 void ScriptManager::giveAttackStoppedCommand(int unitId, const string &itemName,int ignoreRequirements) {
    if(SystemFlags::getSystemSettingType(SystemFlags::debugLUA).enabled) SystemFlags::OutputDebug(SystemFlags::debugLUA,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);
    ScriptManager_STREFLOP_Wrapper streflopWrapper;
@@ -1578,6 +1583,13 @@
       luaArguments.getString(-1));
    return luaArguments.getReturnCount();
 }
+int ScriptManager::giveMorphCommand(LuaHandle* luaHandle){
+   LuaArguments luaArguments(luaHandle);
+   thisScriptManager->giveMorphCommand(
+      luaArguments.getInt(-2),
+      luaArguments.getString(-1));
+   return luaArguments.getReturnCount();
+}
 
 int ScriptManager::giveUpgradeCommand(LuaHandle* luaHandle){
    LuaArguments luaArguments(luaHandle);
Index: source/glest_game/game/script_manager.h
===================================================================
--- source/glest_game/game/script_manager.h   (revision 3326)
+++ source/glest_game/game/script_manager.h   (working copy)
@@ -261,6 +261,7 @@
    void giveProductionCommand(int unitId, const string &producedName);
    void giveAttackCommand(int unitId, int unitToAttackId);
    void giveUpgradeCommand(int unitId, const string &upgradeName);
+   void giveMorphCommand(int unitId, const string &producedName);
 
    void disableAi(int factionIndex);
    void enableAi(int factionIndex);
@@ -363,6 +364,7 @@
    static int giveResource(LuaHandle* luaHandle);
    static int givePositionCommand(LuaHandle* luaHandle);
    static int giveProductionCommand(LuaHandle* luaHandle);
+   static int giveMorphCommand(LuaHandle* luaHandle);
    static int giveAttackCommand(LuaHandle* luaHandle);
    static int giveUpgradeCommand(LuaHandle* luaHandle);
 
Index: source/glest_game/world/world.cpp
===================================================================
--- source/glest_game/world/world.cpp   (revision 3326)
+++ source/glest_game/world/world.cpp   (working copy)
@@ -1076,7 +1076,32 @@
       throw megaglest_runtime_error("Invalid unitId index in giveProductionCommand: " + intToStr(unitId) + " producedName = " + producedName);
    }
 }
+void World::giveMorphCommand(int unitId, const string &producedName) {
+   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() == ccMorph) {
+            const MorphCommandType* pct= static_cast<const MorphCommandType*>(ct);
+            if(pct != NULL && pct->getMorphUnit()->getName() == producedName) {
+               if(SystemFlags::getSystemSettingType(SystemFlags::debugUnitCommands).enabled) SystemFlags::OutputDebug(SystemFlags::debugUnitCommands,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);
+
+               unit->giveCommand(new Command(pct));
+
+               if(SystemFlags::getSystemSettingType(SystemFlags::debugUnitCommands).enabled) SystemFlags::OutputDebug(SystemFlags::debugUnitCommands,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);
+               break;
+            }
+         }
+      }
+   }
+   else {
+      throw megaglest_runtime_error("Invalid unitId index in giveProductionCommand: " + intToStr(unitId) + " producedName = " + producedName);
+   }
+}
+
 void World::giveAttackStoppedCommand(int unitId, const string &itemName, bool ignoreRequirements) {
    Unit *unit= findUnitById(unitId);
    if(unit != NULL) {
Index: source/glest_game/world/world.h
===================================================================
--- source/glest_game/world/world.h   (revision 3326)
+++ source/glest_game/world/world.h   (working copy)
@@ -230,6 +230,7 @@
    bool getIsUnitAlive(int unitId);
    void giveAttackCommand(int unitId, int unitToAttackId);
    void giveProductionCommand(int unitId, const string &producedName);
+   void giveMorphCommand(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);
« Last Edit: 16 May 2012, 15:08:42 by tomreyn »

softcoder

  • MegaGlest Team
  • Battle Machine
  • ********
  • Posts: 2,239
    • View Profile
Re: giveMorphCommand
« Reply #1 on: 16 May 2012, 15:03:29 »
You mean like the already existing:

void morphToUnit(int unitId,const string &morphName, int ignoreRequirements);

MuwuM

  • Ornithopter
  • *****
  • Posts: 426
  • No Game without Move(ment)
    • View Profile
    • MuwuM - Lexicons
Re: giveMorphCommand
« Reply #2 on: 16 May 2012, 15:06:26 »
my bad, sorry

 

anything