9
3

Transmute.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  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. sint32 item_stat_bonus = player->GetStat(ITEM_STAT_TRANSMUTING);
  76. if (!skill || (skill->current_val+item_stat_bonus) < required_skill) {
  77. client->Message(CHANNEL_COLOR_RED, "You need at least %u Transmuting skill to transmute the %s."
  78. " You have %u Transmuting skill.", required_skill, item->name.c_str(), skill ? (skill->current_val+item_stat_bonus) : 0);
  79. return;
  80. }
  81. client->SetTransmuteID(item_id);
  82. SendConfirmRequest(client, req_id, item);
  83. }
  84. void Transmute::SendConfirmRequest(Client* client, int32 req_id, Item* item) {
  85. PacketStruct* p = configReader.getStruct("WS_ChoiceWindow", client->GetVersion());
  86. if (!p) {
  87. client->SimpleMessage(CHANNEL_COLOR_RED, "Struct error for transmutation. Let a dev know.");
  88. return;
  89. }
  90. ostringstream ss;
  91. ss << "Are you sure you want to transmute the " << item->name << '?';
  92. p->setMediumStringByName("text", ss.str().c_str());
  93. p->setMediumStringByName("accept_text", "OK");
  94. ss.str("");
  95. ss << "targetitem " << req_id << ' ' << item->details.unique_id;
  96. string cancel_command = ss.str();
  97. ss << " 1";
  98. string accept_command = ss.str();
  99. p->setMediumStringByName("accept_command", accept_command.c_str());
  100. p->setMediumStringByName("cancel_text", "Cancel");
  101. p->setMediumStringByName("cancel_command", cancel_command.c_str());
  102. client->QueuePacket(p->serialize());
  103. delete p;
  104. }
  105. void Transmute::HandleConfirmResponse(Client* client, Player* player, int32 item_id) {
  106. Item* item = player->item_list.GetItemFromUniqueID(item_id);
  107. if (!item) {
  108. client->SimpleMessage(CHANNEL_COLOR_RED, "Item no longer exists!");
  109. return;
  110. }
  111. client->SetTransmuteID(item_id);
  112. ZoneServer* zone = player->GetZone();
  113. if (!zone) return;
  114. const int32 transmute_item_spell = 5163;
  115. Spell* spell = master_spell_list.GetSpell(transmute_item_spell, 1);
  116. if (!spell) {
  117. LogWrite(SPELL__ERROR, 0, "Transmute", "Could not find the Transmute Item spell : %u", transmute_item_spell);
  118. return;
  119. }
  120. zone->GetSpellProcess()->ProcessSpell(zone, spell, player);
  121. }
  122. void Transmute::CompleteTransmutation(Client* client, Player* player) {
  123. int32 item_id = client->GetTransmuteID();
  124. Item* item = player->item_list.GetItemFromUniqueID(item_id);
  125. if (!item) {
  126. client->SimpleMessage(CHANNEL_COLOR_RED, "Item no longer exists!");
  127. return;
  128. }
  129. int32 common_mat_id = 0;
  130. int32 rare_mat_id = 0;
  131. //Figure out the transmutation tier for our loot roll
  132. int32 item_level = item->generic_info.adventure_default_level;
  133. vector<TransmutingTier>& tiers = GetTransmutingTiers();
  134. for (auto& itr : tiers) {
  135. if (itr.min_level <= item_level && itr.max_level >= item_level) {
  136. //This is the correct tier
  137. int32 tier = item->details.tier;
  138. if (tier >= ITEM_TAG_FABLED) {
  139. common_mat_id = itr.infusion_id;
  140. rare_mat_id = itr.mana_id;
  141. }
  142. else if (tier >= ITEM_TAG_LEGENDARY) {
  143. common_mat_id = itr.powder_id;
  144. rare_mat_id = itr.infusion_id;
  145. }
  146. else {
  147. common_mat_id = itr.fragment_id;
  148. rare_mat_id = itr.powder_id;
  149. }
  150. break;
  151. }
  152. }
  153. if (common_mat_id == 0 || rare_mat_id == 0) {
  154. client->SimpleMessage(CHANNEL_COLOR_RED, "Could not complete transmutation! Tell a dev!");
  155. return;
  156. }
  157. //Do the loot roll
  158. const int32 BOTH_ITEMS_CHANCE_PERCENT = 15;
  159. //The common/rare roll only applies if the both items roll fails
  160. const int32 COMMON_MAT_CHANCE_PERCENT = 75;
  161. const int32 RARE_MAT_CHANCE_PERCENT = 25;
  162. Item* item1 = nullptr;
  163. Item* item2 = nullptr;
  164. int32 roll = MakeRandomInt(1, 100);
  165. if (roll <= BOTH_ITEMS_CHANCE_PERCENT) {
  166. item1 = master_item_list.GetItem(rare_mat_id);
  167. if (item1) item1 = new Item(item1);
  168. item2 = master_item_list.GetItem(common_mat_id);
  169. if (item2) item2 = new Item(item2);
  170. }
  171. else if (roll <= COMMON_MAT_CHANCE_PERCENT) {
  172. item1 = master_item_list.GetItem(common_mat_id);
  173. if (item1) item1 = new Item(item1);
  174. }
  175. else { //rare mat roll
  176. item2 = master_item_list.GetItem(rare_mat_id);
  177. if (item2) item2 = new Item(item2);
  178. }
  179. client->Message(89, "You transmute %s and create: ", item->CreateItemLink(client->GetVersion(), false).c_str());
  180. player->item_list.RemoveItem(item, true);
  181. PacketStruct* packet = configReader.getStruct("WS_QuestComplete", client->GetVersion());
  182. if (packet) {
  183. packet->setDataByName("title", "Item Transmuted!");
  184. }
  185. if (item1) {
  186. item1->details.count = 1;
  187. client->Message(89, " %s", item1->CreateItemLink(client->GetVersion(), false).c_str());
  188. bool itemDeleted = false;
  189. client->AddItem(item1, &itemDeleted);
  190. if (packet && !itemDeleted) {
  191. packet->setArrayDataByName("reward_id", item1->details.item_id, 0);
  192. if (client->GetVersion() < 860)
  193. packet->setItemArrayDataByName("item", item1, player, 0, 0, -1);
  194. else if (client->GetVersion() < 1193)
  195. packet->setItemArrayDataByName("item", item, player);
  196. else
  197. packet->setItemArrayDataByName("item", item1, player, 0, 0, 2);
  198. }
  199. }
  200. if (item2) {
  201. item2->details.count = 1;
  202. client->Message(89, " %s", item2->CreateItemLink(client->GetVersion(), false).c_str());
  203. bool itemDeleted = false;
  204. client->AddItem(item2, &itemDeleted);
  205. if (packet && !itemDeleted) {
  206. int32 dataIndex = 1;
  207. if (!item1) {
  208. packet->setArrayLengthByName("num_rewards", 1);
  209. dataIndex = 0;
  210. }
  211. packet->setArrayDataByName("reward_id", item2->details.item_id, dataIndex);
  212. if (client->GetVersion() < 860)
  213. packet->setItemArrayDataByName("item", item2, player, dataIndex, 0, -1);
  214. else if (client->GetVersion() < 1193)
  215. packet->setItemArrayDataByName("item", item2, player, dataIndex);
  216. else
  217. packet->setItemArrayDataByName("item", item2, player, dataIndex, 0, 2);
  218. }
  219. }
  220. if (packet) {
  221. client->QueuePacket(packet->serialize());
  222. delete packet;
  223. }
  224. //Check if we need to apply a skill-up
  225. Skill* skill = player->GetSkillByName("Transmuting");
  226. if (!skill) {
  227. //Shouldn't happen, sanity check
  228. LogWrite(SKILL__ERROR, 0, "Skill", "Unable to find the transmuting skill for the player %s", player->GetName());
  229. return;
  230. }
  231. //Skill up roll
  232. int32 max_trans_level = skill->current_val / 5 + 5;
  233. sint32 level_dif = (sint32)max_trans_level - (sint32)item_level;
  234. if (level_dif > 10 || skill->current_val >= skill->max_val) {
  235. //No skill up possible
  236. 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);
  237. return;
  238. }
  239. //50% Base chance of a skillup at max item level, 20% overall decrease per level difference
  240. const int32 SKILLUP_PERCENT_CHANCE_MAX = 50;
  241. int32 required_roll = SKILLUP_PERCENT_CHANCE_MAX * (1.f - (item_level <= 5 ? 0.f : (level_dif * .2f)));
  242. roll = MakeRandomInt(1, 100);
  243. //LogWrite(SKILL__ERROR, 0, "Skill", "Skill up roll results, roll = %u, required_roll = %u", roll, required_roll);
  244. if (roll <= required_roll) {
  245. player->skill_list.IncreaseSkill(skill, 1);
  246. }
  247. }
  248. void WorldDatabase::LoadTransmuting() {
  249. DatabaseResult result;
  250. if (!database_new.Select(&result,
  251. "SELECT min_level, max_level, fragment, powder, infusion, mana FROM `transmuting`")) {
  252. LogWrite(DATABASE__ERROR, 0, "Transmuting", "Error loading transmuting data!");
  253. return;
  254. }
  255. Transmute::ProcessDBResult(result);
  256. }
  257. vector<Transmute::TransmutingTier>& Transmute::GetTransmutingTiers() {
  258. static vector<TransmutingTier> gTransmutingTiers;
  259. return gTransmutingTiers;
  260. }
  261. void Transmute::ProcessDBResult(DatabaseResult& result) {
  262. vector<TransmutingTier>& tiers = GetTransmutingTiers();
  263. tiers.clear();
  264. tiers.reserve(result.GetNumRows());
  265. while (result.Next()) {
  266. tiers.emplace_back();
  267. TransmutingTier& t = tiers.back();
  268. int32_t i = 0;
  269. t.min_level = result.GetInt32(i++);
  270. t.max_level = result.GetInt32(i++);
  271. t.fragment_id = result.GetInt32(i++);
  272. t.powder_id = result.GetInt32(i++);
  273. t.infusion_id = result.GetInt32(i++);
  274. t.mana_id = result.GetInt32(i++);
  275. }
  276. }