9
3

Transmute.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. #include "Transmute.h"
  2. #include "../common/MiscFunctions.h"
  3. #include "../common/PacketStruct.h"
  4. #include "client.h"
  5. #include "Items/Items.h"
  6. #include <vector>
  7. #include <sstream>
  8. #include "zoneserver.h"
  9. #include "SpellProcess.h"
  10. #include "../common/Log.h"
  11. #include "WorldDatabase.h"
  12. extern ConfigReader configReader;
  13. extern MasterSpellList master_spell_list;
  14. using namespace std;
  15. int32 Transmute::CreateItemRequest(Client* client, Player* player) {
  16. PacketStruct* p = configReader.getStruct("WS_EqTargetItemCmd", client->GetVersion());
  17. if (!p) return 0;
  18. union {
  19. sint32 signed_request_id;
  20. int32 request_id;
  21. };
  22. do {
  23. signed_request_id = MakeRandomInt(-2147483648, 2147483647);
  24. } while (signed_request_id == 0);
  25. map<int32, Item*>* il = player->GetItemList();
  26. p->setDataByName("request_id", request_id);
  27. p->setDataByName("request_type", 1);
  28. p->setDataByName("unknownff", 0xff);
  29. vector<int32> transmutables;
  30. for (auto& itr : *il) {
  31. if (!itr.second) continue;
  32. if (ItemIsTransmutable(itr.second)) {
  33. transmutables.push_back(itr.first);
  34. }
  35. }
  36. p->setArrayLengthByName("item_array_size", transmutables.size());
  37. for (int i = 0; i < transmutables.size(); i++) {
  38. p->setArrayDataByName("item_id", transmutables[i], i);
  39. }
  40. client->QueuePacket(p->serialize());
  41. delete il;
  42. delete p;
  43. client->SetTransmuteID(request_id);
  44. return request_id;
  45. }
  46. bool Transmute::ItemIsTransmutable(Item* item) {
  47. //Item level > 0 AND Item is not LORE_EQUP, LORE, NO_VALUE etc AND item rarity is >= 5
  48. //(4 is treasured but the rarity used for journeyman spells)
  49. //I think flag 16384 is NO-TRANSMUTE but not positive
  50. const int32 disqualifyFlags = NO_ZONE | NO_VALUE | TEMPORARY | NO_DESTROY | NO_TRANSMUTE;
  51. const int32 disqualityFlags2 = ORNATE;
  52. if (item->generic_info.adventure_default_level > 0
  53. && (item->generic_info.item_flags & disqualifyFlags) == 0
  54. && (item->generic_info.item_flags2 & disqualityFlags2) == 0
  55. && item->details.tier >= 5
  56. && item->stack_count <= 1)
  57. {
  58. return true;
  59. }
  60. return false;
  61. }
  62. void Transmute::HandleItemResponse(Client* client, Player* player, int32 req_id, int32 item_id) {
  63. Item* item = player->item_list.GetItemFromUniqueID(item_id);
  64. if (!item) {
  65. client->SimpleMessage(CHANNEL_COLOR_RED, "Could not find the item you wish to transmute. Please try again.");
  66. return;
  67. }
  68. if (!ItemIsTransmutable(item)) {
  69. client->Message(CHANNEL_COLOR_RED, "%s is not transmutable.", item->name.c_str());
  70. return;
  71. }
  72. int32 item_level = item->generic_info.adventure_default_level;
  73. Skill* skill = player->GetSkillByName("Transmuting");
  74. int32 required_skill = (std::max<int32>(item_level, 5) - 5) * 5;
  75. if (!skill || skill->current_val < required_skill) {
  76. client->Message(CHANNEL_COLOR_RED, "You need at least %u Transmuting skill to transmute the %s."
  77. " You have %u Transmuting skill.", required_skill, item->name.c_str(), skill ? skill->current_val : 0);
  78. return;
  79. }
  80. client->SetTransmuteID(item_id);
  81. SendConfirmRequest(client, req_id, item);
  82. }
  83. void Transmute::SendConfirmRequest(Client* client, int32 req_id, Item* item) {
  84. PacketStruct* p = configReader.getStruct("WS_ChoiceWindow", client->GetVersion());
  85. if (!p) {
  86. client->SimpleMessage(CHANNEL_COLOR_RED, "Struct error for transmutation. Let a dev know.");
  87. return;
  88. }
  89. ostringstream ss;
  90. ss << "Are you sure you want to transmute the " << item->name << '?';
  91. p->setMediumStringByName("text", ss.str().c_str());
  92. p->setMediumStringByName("accept_text", "OK");
  93. ss.str("");
  94. ss << "targetitem " << req_id << ' ' << item->details.unique_id;
  95. string cancel_command = ss.str();
  96. ss << " 1";
  97. string accept_command = ss.str();
  98. p->setMediumStringByName("accept_command", accept_command.c_str());
  99. p->setMediumStringByName("cancel_text", "Cancel");
  100. p->setMediumStringByName("cancel_command", cancel_command.c_str());
  101. client->QueuePacket(p->serialize());
  102. delete p;
  103. }
  104. void Transmute::HandleConfirmResponse(Client* client, Player* player, int32 item_id) {
  105. Item* item = player->item_list.GetItemFromUniqueID(item_id);
  106. if (!item) {
  107. client->SimpleMessage(CHANNEL_COLOR_RED, "Item no longer exists!");
  108. return;
  109. }
  110. client->SetTransmuteID(item_id);
  111. ZoneServer* zone = player->GetZone();
  112. if (!zone) return;
  113. const int32 transmute_item_spell = 5163;
  114. Spell* spell = master_spell_list.GetSpell(transmute_item_spell, 1);
  115. if (!spell) {
  116. LogWrite(SPELL__ERROR, 0, "Transmute", "Could not find the Transmute Item spell : %u", transmute_item_spell);
  117. return;
  118. }
  119. zone->GetSpellProcess()->ProcessSpell(zone, spell, player);
  120. }
  121. void Transmute::CompleteTransmutation(Client* client, Player* player) {
  122. int32 item_id = client->GetTransmuteID();
  123. Item* item = player->item_list.GetItemFromUniqueID(item_id);
  124. if (!item) {
  125. client->SimpleMessage(CHANNEL_COLOR_RED, "Item no longer exists!");
  126. return;
  127. }
  128. int32 common_mat_id = 0;
  129. int32 rare_mat_id = 0;
  130. //Figure out the transmutation tier for our loot roll
  131. int32 item_level = item->generic_info.adventure_default_level;
  132. vector<TransmutingTier>& tiers = GetTransmutingTiers();
  133. for (auto& itr : tiers) {
  134. if (itr.min_level <= item_level && itr.max_level >= item_level) {
  135. //This is the correct tier
  136. int32 tier = item->details.tier;
  137. if (tier >= ITEM_TAG_FABLED) {
  138. common_mat_id = itr.infusion_id;
  139. rare_mat_id = itr.mana_id;
  140. }
  141. else if (tier >= ITEM_TAG_LEGENDARY) {
  142. common_mat_id = itr.powder_id;
  143. rare_mat_id = itr.infusion_id;
  144. }
  145. else {
  146. common_mat_id = itr.fragment_id;
  147. rare_mat_id = itr.powder_id;
  148. }
  149. break;
  150. }
  151. }
  152. if (common_mat_id == 0 || rare_mat_id == 0) {
  153. client->SimpleMessage(CHANNEL_COLOR_RED, "Could not complete transmutation! Tell a dev!");
  154. return;
  155. }
  156. //Do the loot roll
  157. const int32 BOTH_ITEMS_CHANCE_PERCENT = 15;
  158. //The common/rare roll only applies if the both items roll fails
  159. const int32 COMMON_MAT_CHANCE_PERCENT = 75;
  160. const int32 RARE_MAT_CHANCE_PERCENT = 25;
  161. Item* item1 = nullptr;
  162. Item* item2 = nullptr;
  163. int32 roll = MakeRandomInt(1, 100);
  164. if (roll <= BOTH_ITEMS_CHANCE_PERCENT) {
  165. item1 = master_item_list.GetItem(rare_mat_id);
  166. if (item1) item1 = new Item(item1);
  167. item2 = master_item_list.GetItem(common_mat_id);
  168. if (item2) item2 = new Item(item2);
  169. }
  170. else if (roll <= COMMON_MAT_CHANCE_PERCENT) {
  171. item1 = master_item_list.GetItem(common_mat_id);
  172. if (item1) item1 = new Item(item1);
  173. }
  174. else { //rare mat roll
  175. item2 = master_item_list.GetItem(rare_mat_id);
  176. if (item2) item2 = new Item(item2);
  177. }
  178. client->Message(89, "You transmute %s and create: ", item->CreateItemLink(client->GetVersion(), false).c_str());
  179. player->item_list.RemoveItem(item, true);
  180. PacketStruct* packet = configReader.getStruct("WS_QuestComplete", client->GetVersion());
  181. if (packet) {
  182. packet->setDataByName("title", "Item Transmuted!");
  183. }
  184. if (item1) {
  185. item1->details.count = 1;
  186. client->Message(89, " %s", item1->CreateItemLink(client->GetVersion(), false).c_str());
  187. bool itemDeleted = false;
  188. client->AddItem(item1, &itemDeleted);
  189. if (packet && !itemDeleted) {
  190. packet->setArrayDataByName("reward_id", item1->details.item_id, 0);
  191. if (client->GetVersion() < 860)
  192. packet->setItemArrayDataByName("item", item1, player, 0, 0, -1);
  193. else if (client->GetVersion() < 1193)
  194. packet->setItemArrayDataByName("item", item, player);
  195. else
  196. packet->setItemArrayDataByName("item", item1, player, 0, 0, 2);
  197. }
  198. }
  199. if (item2) {
  200. item2->details.count = 1;
  201. client->Message(89, " %s", item2->CreateItemLink(client->GetVersion(), false).c_str());
  202. bool itemDeleted = false;
  203. client->AddItem(item2, &itemDeleted);
  204. if (packet && !itemDeleted) {
  205. int32 dataIndex = 1;
  206. if (!item1) {
  207. packet->setArrayLengthByName("num_rewards", 1);
  208. dataIndex = 0;
  209. }
  210. packet->setArrayDataByName("reward_id", item2->details.item_id, dataIndex);
  211. if (client->GetVersion() < 860)
  212. packet->setItemArrayDataByName("item", item2, player, dataIndex, 0, -1);
  213. else if (client->GetVersion() < 1193)
  214. packet->setItemArrayDataByName("item", item2, player, dataIndex);
  215. else
  216. packet->setItemArrayDataByName("item", item2, player, dataIndex, 0, 2);
  217. }
  218. }
  219. if (packet) {
  220. client->QueuePacket(packet->serialize());
  221. delete packet;
  222. }
  223. //Check if we need to apply a skill-up
  224. Skill* skill = player->GetSkillByName("Transmuting");
  225. if (!skill) {
  226. //Shouldn't happen, sanity check
  227. LogWrite(SKILL__ERROR, 0, "Skill", "Unable to find the transmuting skill for the player %s", player->GetName());
  228. return;
  229. }
  230. //Skill up roll
  231. int32 max_trans_level = skill->current_val / 5 + 5;
  232. int32 level_dif = max_trans_level - item_level;
  233. if (level_dif >= 5 || skill->current_val >= skill->max_val) {
  234. //No skill up possible
  235. LogWrite(SKILL__DEBUG, 7, "Skill", "Transmuting skill up not possible. level_dif = %u, skill val = %u, skill max val = %u", level_dif, skill->current_val, skill->max_val);
  236. return;
  237. }
  238. //40% Base chance of a skillup at max item level, 20% overall decrease per level difference
  239. const int32 SKILLUP_PERCENT_CHANCE_MAX = 40;
  240. int32 required_roll = SKILLUP_PERCENT_CHANCE_MAX * (1.f - (item_level <= 5 ? 0.f : (level_dif * .2f)));
  241. roll = MakeRandomInt(1, 100);
  242. //LogWrite(SKILL__ERROR, 0, "Skill", "Skill up roll results, roll = %u, required_roll = %u", roll, required_roll);
  243. if (roll <= required_roll) {
  244. player->skill_list.IncreaseSkill(skill, 1);
  245. }
  246. }
  247. void WorldDatabase::LoadTransmuting() {
  248. DatabaseResult result;
  249. if (!database_new.Select(&result,
  250. "SELECT min_level, max_level, fragment, powder, infusion, mana FROM `transmuting`")) {
  251. LogWrite(DATABASE__ERROR, 0, "Transmuting", "Error loading transmuting data!");
  252. return;
  253. }
  254. Transmute::ProcessDBResult(result);
  255. }
  256. vector<Transmute::TransmutingTier>& Transmute::GetTransmutingTiers() {
  257. static vector<TransmutingTier> gTransmutingTiers;
  258. return gTransmutingTiers;
  259. }
  260. void Transmute::ProcessDBResult(DatabaseResult& result) {
  261. vector<TransmutingTier>& tiers = GetTransmutingTiers();
  262. tiers.clear();
  263. tiers.reserve(result.GetNumRows());
  264. while (result.Next()) {
  265. tiers.emplace_back();
  266. TransmutingTier& t = tiers.back();
  267. int32_t i = 0;
  268. t.min_level = result.GetInt32(i++);
  269. t.max_level = result.GetInt32(i++);
  270. t.fragment_id = result.GetInt32(i++);
  271. t.powder_id = result.GetInt32(i++);
  272. t.infusion_id = result.GetInt32(i++);
  273. t.mana_id = result.GetInt32(i++);
  274. }
  275. }