TradeskillsPackets.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. /*
  2. EQ2Emulator: Everquest II Server Emulator
  3. Copyright (C) 2007 EQ2EMulator Development Team (http://www.eq2emulator.net)
  4. This file is part of EQ2Emulator.
  5. EQ2Emulator is free software: you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation, either version 3 of the License, or
  8. (at your option) any later version.
  9. EQ2Emulator is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with EQ2Emulator. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #include <algorithm>
  17. #include "../ClientPacketFunctions.h"
  18. #include "../client.h"
  19. #include "../../common/ConfigReader.h"
  20. #include "../../common/PacketStruct.h"
  21. #include "../Recipes/Recipe.h"
  22. #include "../../common/Log.h"
  23. #include "../Spells.h"
  24. #include "../../common/MiscFunctions.h"
  25. extern ConfigReader configReader;
  26. extern MasterRecipeList master_recipe_list;
  27. extern MasterSpellList master_spell_list;
  28. void ClientPacketFunctions::SendCreateFromRecipe(Client* client, int32 recipeID) {
  29. // if recipeID is 0 we are repeating the last recipe, if not set the players current recipe to the new one
  30. if (recipeID == 0)
  31. recipeID = client->GetPlayer()->GetCurrentRecipe();
  32. else
  33. client->GetPlayer()->SetCurrentRecipe(recipeID);
  34. // Get the recipe
  35. Recipe* recipe = master_recipe_list.GetRecipe(recipeID);
  36. if (!recipe) {
  37. LogWrite(TRADESKILL__ERROR, 0, "Recipes", "Error loading recipe (%u) in ClientPacketFunctions::SendCreateFromRecipe()", recipeID);
  38. return;
  39. }
  40. // Create the packet
  41. PacketStruct* packet = configReader.getStruct("WS_CreateFromRecipe", client->GetVersion());
  42. if (!packet) {
  43. LogWrite(TRADESKILL__ERROR, 0, "Recipes", "Error loading WS_CreateFromRecipe in ClientPacketFunctions::SendCreateFromRecipe()");
  44. return;
  45. }
  46. vector<int32>::iterator itr;
  47. int8 i = 0;
  48. int32 firstID = 0;
  49. int32 primary_comp_id = 0;
  50. Item* item = 0;
  51. // Recipe and crafting table info
  52. packet->setDataByName("crafting_station", recipe->GetDevice());
  53. packet->setDataByName("recipe_name", recipe->GetName());
  54. packet->setDataByName("tier", recipe->GetTier());
  55. // Product info
  56. item = master_item_list.GetItem(recipe->GetProductID());
  57. packet->setDataByName("product_name", item->name.c_str());
  58. packet->setDataByName("icon", item->details.icon);
  59. packet->setDataByName("product_qty", recipe->GetProductQuantity());
  60. packet->setDataByName("primary_title", recipe->GetPrimaryBuildComponentTitle());
  61. packet->setDataByName("primary_qty", 1);
  62. // Reset item to 0
  63. item = 0;
  64. // Check to see if we have a primary component (slot = 0)
  65. if (recipe->components.count(0) > 0) {
  66. vector<int32> rc = recipe->components[0];
  67. packet->setArrayLengthByName("num_primary_choices", rc.size());
  68. for (itr = rc.begin(); itr != rc.end(); itr++, i++) {
  69. if (firstID == 0)
  70. firstID = *itr;
  71. item = master_item_list.GetItem(*itr);
  72. if(!item)
  73. {
  74. LogWrite(TRADESKILL__ERROR, 0, "Recipes", "Error creating packet to client missing item %u", *itr);
  75. client->Message(CHANNEL_COLOR_RED, "Error producing create recipe packet! Recipe is trying to find item %u, but it is missing!", *itr);
  76. safe_delete(packet);
  77. return;
  78. }
  79. packet->setArrayDataByName("primary_component", item->name.c_str(), i);
  80. packet->setArrayDataByName("primary_item_id", (*itr), i);
  81. packet->setArrayDataByName("primary_icon", item->details.icon, i);
  82. item = 0;
  83. item = client->GetPlayer()->item_list.GetItemFromID((*itr));
  84. if (item)
  85. packet->setArrayDataByName("primary_total_quantity", item->details.count, i);
  86. }
  87. // store the id of the primary comp
  88. primary_comp_id = firstID;
  89. // Set the default item id to the first component id
  90. packet->setDataByName("primary_item_selected", 1);
  91. packet->setDataByName("primary_default_selected_id", firstID);
  92. item = 0;
  93. item = client->GetPlayer()->item_list.GetItemFromID(firstID);
  94. if (item)
  95. packet->setDataByName("primary_selected_item_qty", min((int8)1, (int8)item->details.count));
  96. // Reset the variables we will reuse
  97. i = 0;
  98. firstID = 0;
  99. item = 0;
  100. }
  101. else {
  102. LogWrite(TRADESKILL__ERROR, 0, "Recipes", "Recipe has no primary component");
  103. return;
  104. }
  105. // Check to see if we have build components (slot = 1 - 4)
  106. int8 total_build_components = 0;
  107. if (recipe->components.count(1) > 0)
  108. total_build_components++;
  109. if (recipe->components.count(2))
  110. total_build_components++;
  111. if (recipe->components.count(3))
  112. total_build_components++;
  113. if (recipe->components.count(4))
  114. total_build_components++;
  115. if (total_build_components > 0) {
  116. packet->setArrayLengthByName("num_build_components", total_build_components);
  117. for (int8 index = 0; index < 4; index++) {
  118. if (recipe->components.count(index + 1) == 0)
  119. continue;
  120. packet->setArrayDataByName("build_slot", index, index);
  121. vector<int32> rc = recipe->components[index + 1];
  122. packet->setSubArrayLengthByName("num_build_choices", rc.size(), index);
  123. for (itr = rc.begin(); itr != rc.end(); itr++, i++) {
  124. if (firstID == 0)
  125. firstID = *itr;
  126. item = master_item_list.GetItem(*itr);
  127. packet->setSubArrayDataByName("build_component", item->name.c_str(), index, i);
  128. packet->setSubArrayDataByName("build_item_id", (*itr), index, i);
  129. packet->setSubArrayDataByName("build_icon", item->details.icon, index, i);
  130. item = 0;
  131. item = client->GetPlayer()->item_list.GetItemFromID((*itr));
  132. if (item)
  133. packet->setSubArrayDataByName("build_total_quantity", item->details.count, index, i);
  134. }
  135. // Set the default item id to the first component id
  136. packet->setArrayDataByName("build_item_selected", 1, index);
  137. packet->setArrayDataByName("build_selected_item_id", firstID, index);
  138. item = 0;
  139. item = client->GetPlayer()->item_list.GetItemFromID(firstID);
  140. int8 qty = 0;
  141. if (item) {
  142. qty = (int8)item->details.count;
  143. if (qty > 0 && firstID == primary_comp_id)
  144. qty -= 1;
  145. }
  146. if (index == 0) {
  147. packet->setArrayDataByName("build_title", recipe->GetBuild1ComponentTitle(), index);
  148. packet->setArrayDataByName("build_qty", recipe->GetBuild1ComponentQuantity(), index);
  149. if (item)
  150. packet->setArrayDataByName("build_selected_item_qty", min(qty, recipe->GetBuild1ComponentQuantity()), index);
  151. }
  152. else if (index == 1) {
  153. packet->setArrayDataByName("build_title", recipe->GetBuild2ComponentTitle(), index);
  154. packet->setArrayDataByName("build_qty", recipe->GetBuild2ComponentQuantity(), index);
  155. if (item)
  156. packet->setArrayDataByName("build_selected_item_qty", min(qty, recipe->GetBuild2ComponentQuantity()), index);
  157. }
  158. else if (index == 2) {
  159. packet->setArrayDataByName("build_title", recipe->GetBuild3ComponentTitle(), index);
  160. packet->setArrayDataByName("build_qty", recipe->GetBuild3ComponentQuantity(), index);
  161. if (item)
  162. packet->setArrayDataByName("build_selected_item_qty", min(qty, recipe->GetBuild3ComponentQuantity()), index);
  163. }
  164. else {
  165. packet->setArrayDataByName("build_title", recipe->GetBuild4ComponentTitle(), index);
  166. packet->setArrayDataByName("build_qty", recipe->GetBuild4ComponentQuantity(), index);
  167. if (item)
  168. packet->setArrayDataByName("build_selected_item_qty", min(qty, recipe->GetBuild4ComponentQuantity()), index);
  169. }
  170. // Reset the variables we will reuse
  171. i = 0;
  172. firstID = 0;
  173. item = 0;
  174. }
  175. }
  176. // Check to see if we have a fuel component (slot = 5)
  177. if (recipe->components.count(5) > 0) {
  178. vector<int32> rc = recipe->components[5];
  179. packet->setArrayLengthByName("num_fuel_choices", rc.size());
  180. for (itr = rc.begin(); itr != rc.end(); itr++, i++) {
  181. if (firstID == 0)
  182. firstID = *itr;
  183. item = master_item_list.GetItem(*itr);
  184. packet->setArrayDataByName("fuel_component", item->name.c_str(), i);
  185. packet->setArrayDataByName("fuel_item_id", (*itr), i);
  186. packet->setArrayDataByName("fuel_icon", item->details.icon, i);
  187. item = 0;
  188. item = client->GetPlayer()->item_list.GetItemFromID((*itr));
  189. if (item)
  190. packet->setArrayDataByName("fuel_total_quantity", item->details.count, i);
  191. }
  192. // Set the default item id to the first component id
  193. packet->setDataByName("fuel_selected_item_id", firstID);
  194. packet->setDataByName("fuel_item_selected", 1);
  195. item = 0;
  196. item = client->GetPlayer()->item_list.GetItemFromID(firstID);
  197. if (item)
  198. packet->setDataByName("fuel_selected_item_qty", min(recipe->GetFuelComponentQuantity(), (int8)item->details.count));
  199. packet->setDataByName("fuel_title",recipe->GetFuelComponentTitle());
  200. packet->setDataByName("fuel_qty",recipe->GetFuelComponentQuantity());
  201. // Reset the variables we will reuse
  202. i = 0;
  203. firstID = 0;
  204. item = 0;
  205. }
  206. else {
  207. LogWrite(TRADESKILL__ERROR, 0, "Recipes", "Recipe has no fuel component");
  208. return;
  209. }
  210. packet->setDataByName("unknown1", recipeID);
  211. packet->setDataByName("unknown8", 1); // Possible array for amount you can craft
  212. packet->setDataByName("unknown8", 1, 1); // amounts you can craft
  213. //packet->PrintPacket();
  214. // Send the packet
  215. client->QueuePacket(packet->serialize());
  216. safe_delete(packet);
  217. }
  218. void ClientPacketFunctions::SendItemCreationUI(Client* client, Recipe* recipe) {
  219. // Check for valid recipe
  220. if (!recipe) {
  221. LogWrite(TRADESKILL__ERROR, 0, "Recipes", "Recipe = null in ClientPacketFunctions::SendItemCreationUI()");
  222. return;
  223. }
  224. // Load the packet
  225. PacketStruct* packet = configReader.getStruct("WS_ShowItemCreation", client->GetVersion());
  226. if (!packet) {
  227. LogWrite(TRADESKILL__ERROR, 0, "Recipes", "Error loading WS_ShowItemCreation in ClientPacketFunctions::SendItemCreationUI()");
  228. return;
  229. }
  230. int16 item_version = GetItemPacketType(packet->GetVersion());
  231. Item* item = 0;
  232. RecipeProducts* rp = 0;
  233. packet->setDataByName("max_possible_durability", 1000);
  234. packet->setDataByName("max_possible_progress", 1000);
  235. // All the packets I have looked at these unknowns are always the same
  236. // so hardcoding them until they are identified.
  237. packet->setDataByName("unknown2", 1045220557, 0);
  238. packet->setDataByName("unknown2", 1061997773, 1);
  239. // Highest stage the player has been able to complete
  240. // TODO: store this for the player, for now use 0 (none known)
  241. packet->setDataByName("progress_levels_known", client->GetPlayer()->GetRecipeList()->GetRecipe(recipe->GetID())->GetHighestStage());
  242. packet->setArrayLengthByName("num_process", 4);
  243. for (int8 i = 0; i < 4; i++) {
  244. // Don't like this code but need to change the IfVariableNotSet value on unknown3 element
  245. // to point to the currect progress_needed
  246. vector<DataStruct*> dataStructs = packet->GetDataStructs();
  247. vector<DataStruct*>::iterator itr;
  248. for (itr = dataStructs.begin(); itr != dataStructs.end(); itr++) {
  249. DataStruct* data = *itr;
  250. char tmp[20] = {0};
  251. sprintf(tmp,"_%i",i);
  252. string name = "unknown3";
  253. name.append(tmp);
  254. if (strcmp(data->GetName(), name.c_str()) == 0) {
  255. name = "progress_needed";
  256. name.append(tmp);
  257. data->SetIfNotSetVariable(name.c_str());
  258. }
  259. }
  260. if (i == 1)
  261. packet->setArrayDataByName("progress_needed", 400, i);
  262. else if (i == 2)
  263. packet->setArrayDataByName("progress_needed", 600, i);
  264. else if (i == 3)
  265. packet->setArrayDataByName("progress_needed", 800, i);
  266. // get the product for this stage, if none found default to fuel
  267. if (recipe->products.count(i) > 0)
  268. rp = recipe->products[i];
  269. if (!rp) {
  270. rp = new RecipeProducts;
  271. rp->product_id = recipe->components[5].front();
  272. rp->product_qty = recipe->GetFuelComponentQuantity();
  273. rp->byproduct_id = 0;
  274. rp->byproduct_qty = 0;
  275. recipe->products[i] = rp;
  276. }
  277. item = master_item_list.GetItem(rp->product_id);
  278. if (!item) {
  279. LogWrite(TRADESKILL__ERROR, 0, "Recipe", "Error loading item (%u) in ClientPacketFunctions::SendItemCreationUI()", rp->product_id);
  280. return;
  281. }
  282. packet->setArrayDataByName("item_name", item->name.c_str(), i);
  283. packet->setArrayDataByName("item_icon", item->details.icon, i);
  284. if(client->GetVersion() < 860)
  285. packet->setItemArrayDataByName("item", item, client->GetPlayer(), i, 0, -1);
  286. else if (client->GetVersion() < 1193)
  287. packet->setItemArrayDataByName("item", item, client->GetPlayer(), i);
  288. else
  289. packet->setItemArrayDataByName("item", item, client->GetPlayer(), i, 0, 2);
  290. if (rp->byproduct_id > 0) {
  291. item = 0;
  292. item = master_item_list.GetItem(rp->byproduct_id);
  293. if (item) {
  294. packet->setArrayDataByName("item_byproduct_name", item->name.c_str(), i);
  295. packet->setArrayDataByName("item_byproduct_icon", item->details.icon, i);
  296. }
  297. }
  298. packet->setArrayDataByName("packettype", item_version, i);
  299. packet->setArrayDataByName("packetsubtype", 0xFF, i);
  300. item = 0;
  301. rp = 0;
  302. }
  303. packet->setDataByName("product_progress_needed", 1000);
  304. rp = recipe->products[4];
  305. item = master_item_list.GetItem(rp->product_id);
  306. packet->setDataByName("product_item_name", item->name.c_str());
  307. packet->setDataByName("product_item_icon", item->details.icon);
  308. if(client->GetVersion() < 860)
  309. packet->setItemByName("product_item", item, client->GetPlayer(), 0, -1);
  310. else if (client->GetVersion() < 1193)
  311. packet->setItemByName("product_item", item, client->GetPlayer());
  312. else
  313. packet->setItemByName("product_item", item, client->GetPlayer(), 0, 2);
  314. //packet->setItemByName("product_item", item, client->GetPlayer());
  315. if (rp->byproduct_id > 0) {
  316. item = 0;
  317. item = master_item_list.GetItem(rp->byproduct_id);
  318. if (item) {
  319. packet->setDataByName("product_byproduct_name", item->name.c_str());
  320. packet->setDataByName("product_byproduct_icon", item->details.icon);
  321. }
  322. }
  323. packet->setDataByName("packettype", item_version);
  324. packet->setDataByName("packetsubtype", 0xFF);
  325. // Start of basic work to get the skills to show on the tradeskill window
  326. // not even close to accurate but skills do get put on the ui
  327. int8 index = 0;
  328. int8 size = 0;
  329. vector<int32>::iterator itr;
  330. vector<int32> spells = client->GetPlayer()->GetSpellBookSpellIDBySkill(recipe->GetTechnique());
  331. for (itr = spells.begin(); itr != spells.end(); itr++) {
  332. size++;
  333. Spell* spell = master_spell_list.GetSpell(*itr,1);
  334. if (size > 6) {
  335. // only 6 slots for skills on the ui
  336. break;
  337. }
  338. packet->setDataByName("skill_id", *itr ,spell->GetSpellData()->ts_loc_index -1);
  339. }
  340. EQ2Packet* outapp = packet->serialize();
  341. //DumpPacket(outapp);
  342. client->QueuePacket(outapp);
  343. safe_delete(packet);
  344. }
  345. void ClientPacketFunctions::StopCrafting(Client* client) {
  346. client->QueuePacket(new EQ2Packet(OP_StopItemCreationMsg, 0, 0));
  347. }
  348. void ClientPacketFunctions::CounterReaction(Client* client, bool countered) {
  349. PacketStruct* packet = configReader.getStruct("WS_TSEventReaction", client->GetVersion());
  350. if (packet) {
  351. packet->setDataByName("counter_reaction", countered ? 1 : 0);
  352. client->QueuePacket(packet->serialize());
  353. }
  354. safe_delete(packet);
  355. }