M_equipment.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. #include "stdafx.h"
  2. #include "tinyxml2/tinyxml2.h"
  3. #include "LuaCfgHelper.h"
  4. #include <iostream>
  5. #include <boost/algorithm/string.hpp>
  6. #include "M_equipment.h"
  7. std::auto_ptr<M_equipment> M_equipment::msSingleton(nullptr);
  8. int M_equipment::GetCount()
  9. {
  10. return (int)mMapData.size();
  11. }
  12. const M_equipmentData* M_equipment::GetData(int ID)
  13. {
  14. auto it = mMapData.find(ID);
  15. if (it != mMapData.end())
  16. {
  17. return &it->second;
  18. }
  19. return NULL;
  20. }
  21. const std::map<int, M_equipmentData>& M_equipment::GetMapData()
  22. {
  23. return mMapData;
  24. }
  25. void M_equipment::Load()
  26. {
  27. tinyxml2::XMLDocument xmlDoc;
  28. std::string content = FileUtils::getInstance()->getStringFromFile("Config/M_equipment.xml");
  29. auto result = xmlDoc.Parse(content.c_str(), content.length());
  30. if (result != tinyxml2::XML_SUCCESS)
  31. {
  32. CCLOGERROR("Result:%d", result);
  33. CCASSERT(false, "result != tinyxml2::XML_SUCCESS");
  34. return;
  35. }
  36. auto root = xmlDoc.RootElement();
  37. if (root == NULL)
  38. {
  39. CCASSERT(false, "root == NULL");
  40. return;
  41. }
  42. auto element = root->FirstChildElement("Data");
  43. while (element != NULL)
  44. {
  45. M_equipmentData data;
  46. data.mID = element->IntAttribute("ID");
  47. data.mitemType = element->IntAttribute("itemType");
  48. data.mleve = element->IntAttribute("leve");
  49. data.mquality = element->Attribute("quality");
  50. {
  51. const char* cost1 = element->Attribute("cost1");
  52. std::vector<std::string> veccost1;
  53. boost::split(veccost1, cost1, boost::is_any_of(","));
  54. int temp;
  55. for (unsigned int i = 0; i < veccost1.size(); i++)
  56. {
  57. if (tinyxml2::XMLUtil::ToInt(veccost1[i].c_str(), &temp))
  58. {
  59. data.mcost1.push_back(temp);
  60. }
  61. }
  62. }
  63. {
  64. const char* cost2 = element->Attribute("cost2");
  65. std::vector<std::string> veccost2;
  66. boost::split(veccost2, cost2, boost::is_any_of(","));
  67. int temp;
  68. for (unsigned int i = 0; i < veccost2.size(); i++)
  69. {
  70. if (tinyxml2::XMLUtil::ToInt(veccost2[i].c_str(), &temp))
  71. {
  72. data.mcost2.push_back(temp);
  73. }
  74. }
  75. }
  76. {
  77. const char* reward = element->Attribute("reward");
  78. std::vector<std::string> vecreward;
  79. boost::split(vecreward, reward, boost::is_any_of(","));
  80. int temp;
  81. for (unsigned int i = 0; i < vecreward.size(); i++)
  82. {
  83. if (tinyxml2::XMLUtil::ToInt(vecreward[i].c_str(), &temp))
  84. {
  85. data.mreward.push_back(temp);
  86. }
  87. }
  88. }
  89. data.matk = element->IntAttribute("atk");
  90. data.mhp = element->IntAttribute("hp");
  91. data.matkpct = element->IntAttribute("atkpct");
  92. data.mhppct = element->IntAttribute("hppct");
  93. data.mcrtpct = element->IntAttribute("crtpct");
  94. data.mmovepct = element->IntAttribute("movepct");
  95. if (mMapData.find(data.mID) != mMapData.end())std::cout <<"data refind:" << data.mID << std::endl;
  96. CCASSERT(mMapData.find(data.mID) == mMapData.end(), "data.mID is exists");
  97. mMapData.insert(std::make_pair(data.mID, data));
  98. element = element->NextSiblingElement();
  99. }
  100. CCLOG("M_equipment Loaded. Load Data:%u", mMapData.size());
  101. }
  102. void M_equipment::LoadLua()
  103. {
  104. LuaEngine::getInstance()->executeScriptFile("config/M_equipment");
  105. lua_State* L = LuaEngine::getInstance()->getLuaStack()->getLuaState();
  106. lua_getglobal(L, "M_equipment");
  107. CCASSERT(lua_istable(L, -1) == 1, "is not table");
  108. lua_pushstring(L, "datas");
  109. lua_gettable(L, -2);
  110. CCASSERT(lua_istable(L, -1) == 1, "is not table");
  111. lua_pushnil(L);
  112. while(lua_next(L, 2))
  113. {
  114. CCASSERT(lua_istable(L, -1) == 1, "is not table");
  115. M_equipmentData data;
  116. LuaCfgHelper::readInt(L, "ID", data.mID);
  117. LuaCfgHelper::readInt(L, "itemType", data.mitemType);
  118. LuaCfgHelper::readInt(L, "leve", data.mleve);
  119. LuaCfgHelper::readString(L, "quality", data.mquality);
  120. LuaCfgHelper::readVectorInt(L, "cost1", data.mcost1);
  121. LuaCfgHelper::readVectorInt(L, "cost2", data.mcost2);
  122. LuaCfgHelper::readVectorInt(L, "reward", data.mreward);
  123. LuaCfgHelper::readInt(L, "atk", data.matk);
  124. LuaCfgHelper::readInt(L, "hp", data.mhp);
  125. LuaCfgHelper::readInt(L, "atkpct", data.matkpct);
  126. LuaCfgHelper::readInt(L, "hppct", data.mhppct);
  127. LuaCfgHelper::readInt(L, "crtpct", data.mcrtpct);
  128. LuaCfgHelper::readInt(L, "movepct", data.mmovepct);
  129. if (mMapData.find(data.mID) != mMapData.end())std::cout <<"data refind:" << data.mID << std::endl;
  130. CCASSERT(mMapData.find(data.mID) == mMapData.end(), "data.mID is exists");
  131. mMapData.insert(std::make_pair(data.mID, data));
  132. lua_pop(L, 1);
  133. }
  134. lua_settop(L, 0);
  135. CCLOG("M_equipment Loaded. Load Data:%u", mMapData.size());
  136. }
  137. void M_equipment::Reload()
  138. {
  139. mMapData.clear();
  140. Load();
  141. }
  142. M_equipment* M_equipment::GetSingleton()
  143. {
  144. if (msSingleton.get() == nullptr)
  145. {
  146. msSingleton.reset(new M_equipment());
  147. }
  148. return msSingleton.get();
  149. }
  150. void M_equipment::Release()
  151. {
  152. msSingleton.reset(nullptr);
  153. }