Author Topic: [done] unitName(unitId)  (Read 698 times)

MuwuM

  • Ornithopter
  • *****
  • Posts: 426
  • No Game without Move(ment)
    • View Profile
    • MuwuM - Lexicons
[done] unitName(unitId)
« on: 17 May 2012, 12:27:14 »
... another try to submit useful Lua methods:
Code: [Select]
Index: source/glest_game/game/script_manager.cpp
===================================================================
--- source/glest_game/game/script_manager.cpp (revision 3330)
+++ source/glest_game/game/script_manager.cpp (working copy)
@@ -309,6 +309,7 @@
  luaScript.registerFunction(setUnitPosition, "setUnitPosition");
 
  luaScript.registerFunction(getUnitFaction, "unitFaction");
+ luaScript.registerFunction(getUnitName, "unitName");
  luaScript.registerFunction(getResourceAmount, "resourceAmount");
 
  luaScript.registerFunction(getLastCreatedUnitName, "lastCreatedUnitName");
@@ -1258,6 +1259,11 @@
  ScriptManager_STREFLOP_Wrapper streflopWrapper;
  return world->getUnitFactionIndex(unitId);
 }
+const string ScriptManager::getUnitName(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;
+ return world->getUnitName(unitId);
+}
 
 int ScriptManager::getResourceAmount(const string &resourceName, int factionIndex) {
  if(SystemFlags::getSystemSettingType(SystemFlags::debugLUA).enabled) SystemFlags::OutputDebug(SystemFlags::debugLUA,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);
@@ -1781,6 +1787,12 @@
  luaArguments.returnInt(factionIndex);
  return luaArguments.getReturnCount();
 }
+int ScriptManager::getUnitName(LuaHandle* luaHandle){
+ LuaArguments luaArguments(luaHandle);
+ const string unitname = thisScriptManager->getUnitName(luaArguments.getInt(-1));
+ luaArguments.returnString(unitname);
+ return luaArguments.getReturnCount();
+}
 
 int ScriptManager::getResourceAmount(LuaHandle* luaHandle){
  LuaArguments luaArguments(luaHandle);
Index: source/glest_game/game/script_manager.h
===================================================================
--- source/glest_game/game/script_manager.h (revision 3330)
+++ source/glest_game/game/script_manager.h (working copy)
@@ -304,6 +304,7 @@
  Vec2i getStartLocation(int factionIndex);
  Vec2i getUnitPosition(int unitId);
  int getUnitFaction(int unitId);
+ const string getUnitName(int unitId);
  int getResourceAmount(const string &resourceName, int factionIndex);
  const string &getLastCreatedUnitName();
  int getLastCreatedUnitId();
@@ -410,6 +411,7 @@
  static int getStartLocation(LuaHandle* luaHandle);
  static int getUnitPosition(LuaHandle* luaHandle);
  static int getUnitFaction(LuaHandle* luaHandle);
+ static int getUnitName(LuaHandle* luaHandle);
  static int getResourceAmount(LuaHandle* luaHandle);
  static int getLastCreatedUnitName(LuaHandle* luaHandle);
  static int getLastCreatedUnitId(LuaHandle* luaHandle);
Index: source/glest_game/world/world.cpp
===================================================================
--- source/glest_game/world/world.cpp (revision 3330)
+++ source/glest_game/world/world.cpp (working copy)
@@ -1285,6 +1285,13 @@
  }
  return unit->getFactionIndex();
 }
+const string World::getUnitName(int unitId) {
+ Unit* unit= findUnitById(unitId);
+ if(unit == NULL) {
+ throw megaglest_runtime_error("Can not find Faction unit to get position unitId = " + intToStr(unitId));
+ }
+ return unit->getFullName();
+}
 
 int World::getUnitCount(int factionIndex) {
  if(factionIndex < factions.size()) {
Index: source/glest_game/world/world.h
===================================================================
--- source/glest_game/world/world.h (revision 3330)
+++ source/glest_game/world/world.h (working copy)
@@ -248,6 +248,7 @@
  void setUnitPosition(int unitId, Vec2i pos);
 
  int getUnitFactionIndex(int unitId);
+ const string getUnitName(int unitId);
  int getUnitCount(int factionIndex);
  int getUnitCountOfType(int factionIndex, const string &typeName);
 
« Last Edit: 22 May 2012, 01:26:34 by softcoder »

softcoder

  • MegaGlest Team
  • Battle Machine
  • ********
  • Posts: 2,239
    • View Profile
Re: [Lua] unitName(unitId)
« Reply #1 on: 21 May 2012, 20:16:04 »
Applied in rev 3344

MuwuM

  • Ornithopter
  • *****
  • Posts: 426
  • No Game without Move(ment)
    • View Profile
    • MuwuM - Lexicons
Re: [Lua] unitName(unitId)
« Reply #2 on: 21 May 2012, 20:45:50 »
Thanks  :thumbup: