TradeskillsPackets.cpp 16 KB

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