9
3

Tradeskills.cpp 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869
  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 <boost/algorithm/string/predicate.hpp>
  17. #include <boost/algorithm/string.hpp>
  18. #include "Tradeskills.h"
  19. #include "../client.h"
  20. #include "../../common/ConfigReader.h"
  21. #include "../classes.h"
  22. //#include "../../common/debug.h"
  23. #include "../../common/Log.h"
  24. //#include "../zoneserver.h"
  25. //#include "../Skills.h"
  26. //#include "../classes.h"
  27. #include "../World.h"
  28. //#include "../LuaInterface.h"
  29. #include "../ClientPacketFunctions.h"
  30. #include "../WorldDatabase.h"
  31. #include "../Rules/Rules.h"
  32. extern Classes classes;
  33. extern ConfigReader configReader;
  34. extern MasterSkillList master_skill_list;
  35. extern MasterRecipeList master_recipe_list;
  36. extern MasterTradeskillEventsList master_tradeskillevent_list;
  37. extern WorldDatabase database;
  38. extern RuleManager rule_manager;
  39. TradeskillMgr::TradeskillMgr() {
  40. m_tradeskills.SetName("TradeskillMgr::tradeskillsList");
  41. // % chance for each was made up by me (Jabantiz) and may need some tweaking
  42. // 2% for crit fail
  43. m_success = rule_manager.GetGlobalRule(R_World, TradeskillSuccessChance)->GetFloat();
  44. m_critSuccess = rule_manager.GetGlobalRule(R_World, TradeskillCritSuccessChance)->GetFloat();
  45. m_fail = rule_manager.GetGlobalRule(R_World, TradeskillFailChance)->GetFloat();
  46. m_critFail = rule_manager.GetGlobalRule(R_World, TradeskillCritFailChance)->GetFloat();
  47. m_eventChance = rule_manager.GetGlobalRule(R_World, TradeskillEventChance)->GetFloat();
  48. if ((m_success + m_critSuccess + m_fail + m_critFail) != 100.0f) {
  49. LogWrite(TRADESKILL__ERROR, 0, "Tradeskills", "Success, crit success, fail, and crit fail MUST add up to 100, reverting to defaults...");
  50. m_success = 87.0f;
  51. m_critSuccess = 2.0f;
  52. m_fail = 10.0f;
  53. m_critFail = 1.0f;
  54. }
  55. }
  56. TradeskillMgr::~TradeskillMgr() {
  57. m_tradeskills.writelock(__FUNCTION__, __LINE__);
  58. map<Client*, Tradeskill*>::iterator itr;
  59. for (itr = tradeskillList.begin(); itr != tradeskillList.end(); itr++)
  60. safe_delete(itr->second);
  61. tradeskillList.clear();
  62. m_tradeskills.releasewritelock(__FUNCTION__, __LINE__);
  63. }
  64. void TradeskillMgr::Process() {
  65. m_tradeskills.writelock(__FUNCTION__, __LINE__);
  66. map<Client*, Tradeskill*>::iterator itr = tradeskillList.begin();
  67. while (itr != tradeskillList.end()) {
  68. Tradeskill* tradeskill = 0;
  69. tradeskill = itr->second;
  70. if (!tradeskill)
  71. continue;
  72. if (Timer::GetCurrentTime2() >= tradeskill->nextUpdateTime) {
  73. Client* client = itr->first;
  74. if(!client->GetPlayer()) {
  75. continue;
  76. }
  77. SetClientIdleVisualState(client, tradeskill);
  78. sint32 progress = 0;
  79. sint32 durability = 0;
  80. /*
  81. Following was grabbed from
  82. http://eq2.stratics.com/content/guides/padasher_crafting_2.php
  83. old but the base fail/succes should still be the same
  84. -100 Durability / -50 Progress (Critical Failure)
  85. -50 Durability / 0 Progress (Failure)
  86. -10 Durability / +50 Progress (Standard tick)
  87. +10 Durability / + 100 Progress (Critical Success)
  88. */
  89. float roll = MakeRandomFloat(0, 100);
  90. int8 effect = 0; //1 is critical success, 2 is success, 3 is failure, and 4 is critical failure.
  91. float success = m_success;
  92. float crit_success = m_critSuccess;
  93. float fail = m_fail;
  94. float crit_fail = m_critFail;
  95. // Modify the % chance for success based off of stats
  96. client->GetPlayer()->MStats.lock();
  97. fail -= client->GetPlayer()->stats[ITEM_STAT_SUCCESS_MOD];
  98. success += client->GetPlayer()->stats[ITEM_STAT_SUCCESS_MOD];
  99. client->GetPlayer()->MStats.unlock();
  100. // add values together for the if
  101. crit_success += crit_fail;
  102. fail += crit_success;
  103. success += fail;
  104. // Crit fail
  105. if (roll <= crit_fail) {
  106. progress = -50;
  107. durability = -100;
  108. effect = 4;
  109. client->SimpleMessage(CHANNEL_COLOR_YELLOW, "Critical failure!");
  110. }
  111. // Crit success
  112. else if (roll > crit_fail && roll <= crit_success) {
  113. progress = 100;
  114. durability = 10;
  115. effect = 1;
  116. client->SimpleMessage(CHANNEL_COLOR_YELLOW, "Critical success!");
  117. }
  118. // Fail
  119. else if (roll > crit_success && roll <= fail) {
  120. progress = 0;
  121. durability = -50;
  122. effect = 3;
  123. }
  124. // Success
  125. else if (roll > fail && roll <= success) {
  126. progress = 50;
  127. durability = -10;
  128. effect = 2;
  129. }
  130. else {
  131. // Just a debug, should never end up in this, if we do write out a log but treat as a success for the player
  132. LogWrite(TRADESKILL__ERROR, 0, "Tradeskills", "Process roll was not within valid range. roll = %f, crit fail = %f, crit success = %f, fail = %f, success = %f", roll, crit_fail, crit_success, fail, success);
  133. progress = 50;
  134. durability = -10;
  135. effect = 2;
  136. }
  137. // Check to see if there was an event, if there was give out the rewards/penalties for it
  138. if (tradeskill->CurrentEvent) {
  139. if (tradeskill->eventCountered) {
  140. progress += tradeskill->CurrentEvent->SuccessProgress;
  141. durability += tradeskill->CurrentEvent->SuccessDurability;
  142. }
  143. else {
  144. progress += tradeskill->CurrentEvent->FailProgress;
  145. durability += tradeskill->CurrentEvent->FailDurability;
  146. }
  147. }
  148. // Modify the progress/durability by the players stats
  149. client->GetPlayer()->MStats.lock();
  150. progress += client->GetPlayer()->stats[ITEM_STAT_PROGRESS_ADD];
  151. durability += client->GetPlayer()->stats[ITEM_STAT_DURABILITY_ADD];
  152. client->GetPlayer()->MStats.unlock();
  153. tradeskill->currentDurability += durability;
  154. tradeskill->currentProgress += progress;
  155. PacketStruct* packet = configReader.getStruct("WS_UpdateCreateItem", client->GetVersion());
  156. if (packet) {
  157. packet->setDataByName("spawn_id", client->GetPlayer()->GetIDWithPlayerSpawn(tradeskill->table));
  158. packet->setDataByName("effect", effect);
  159. packet->setDataByName("total_durability", tradeskill->currentDurability);
  160. packet->setDataByName("total_progress", tradeskill->currentProgress);
  161. packet->setDataByName("durability_change", durability);
  162. packet->setDataByName("progress_change", progress);
  163. if (tradeskill->currentProgress >= 1000)
  164. packet->setDataByName("progress_level", 4);
  165. else if (tradeskill->currentProgress >= 800)
  166. packet->setDataByName("progress_level", 3);
  167. else if (tradeskill->currentProgress >= 600)
  168. packet->setDataByName("progress_level", 2);
  169. else if (tradeskill->currentProgress >= 400)
  170. packet->setDataByName("progress_level", 1);
  171. else
  172. packet->setDataByName("progress_level", 0);
  173. // Reset the tradeskill event
  174. tradeskill->CurrentEvent = 0;
  175. tradeskill->eventChecked = false;
  176. tradeskill->eventCountered = false;
  177. // 15% chance for an event (change this to a rule probably)
  178. int eventRoll = MakeRandomFloat(0, 100);
  179. if (eventRoll <= m_eventChance) {
  180. // Get a vector of all possible events for this crafting technique
  181. vector<TradeskillEvent*>* events = master_tradeskillevent_list.GetEventByTechnique(tradeskill->recipe->GetTechnique());
  182. if (events) {
  183. // Get the size of the vector
  184. int size = events->size();
  185. // Get a random number from 0 to size - 1 to use as an index
  186. int index = MakeRandomInt(0, size - 1);
  187. // use the index to get an event
  188. TradeskillEvent* TSEvent = events->at(index);
  189. if (TSEvent) {
  190. // Now that we got a random event set it in the packet
  191. packet->setDataByName("reaction_icon", TSEvent->Icon);
  192. packet->setDataByName("reaction_name", TSEvent->Name);
  193. // Set the current tradeskill event
  194. tradeskill->CurrentEvent = TSEvent;
  195. }
  196. }
  197. }
  198. EQ2Packet* pack = packet->serialize();
  199. //packet->PrintPacket();
  200. client->QueuePacket(pack);
  201. safe_delete(packet);
  202. }
  203. if (tradeskill->currentProgress >= 1000) {
  204. itr++;
  205. StopCrafting(client, false);
  206. continue;
  207. }
  208. else
  209. tradeskill->nextUpdateTime = Timer::GetCurrentTime2() + 4000;
  210. }
  211. itr++;
  212. }
  213. m_tradeskills.releasewritelock(__FUNCTION__, __LINE__);
  214. }
  215. void TradeskillMgr::BeginCrafting(Client* client, vector<pair<int32,int16>> components) {
  216. Recipe* recipe = master_recipe_list.GetRecipe(client->GetPlayer()->GetCurrentRecipe());
  217. if (!recipe) {
  218. LogWrite(TRADESKILL__ERROR, 0, "Recipe", "Recipe (%u) not found in TradeskillMgr::BeginCrafting()", client->GetPlayer()->GetCurrentRecipe());
  219. ClientPacketFunctions::StopCrafting(client);
  220. return;
  221. }
  222. // TODO: use the vecotr to lock inventory slots
  223. vector<pair<int32, int16>>::iterator itr;
  224. bool missingItem = false;
  225. int32 itemid = 0;
  226. vector<Item*> tmpItems;
  227. for (itr = components.begin(); itr != components.end(); itr++) {
  228. itemid = itr->first;
  229. Item* item = client->GetPlayer()->item_list.GetItemFromUniqueID(itemid);
  230. int8 qty_req = 0;
  231. if(!item)
  232. {
  233. missingItem = true;
  234. break;
  235. }
  236. item->details.item_locked = true;
  237. tmpItems.push_back(item);
  238. }
  239. if(!recipe->ProvidedAllRequiredComponents(client, &tmpItems, &components)) {
  240. LogWrite(TRADESKILL__ERROR, 0, "Recipe", "Recipe (%u) quantity of items incorrect or component missing.", recipe->GetID());
  241. missingItem = true;
  242. }
  243. if (missingItem) {
  244. LogWrite(TRADESKILL__ERROR, 0, "Recipe", "Recipe (%u) player missing item when attempting to create recipe.", recipe->GetID());
  245. vector<Item*>::iterator itemitr;
  246. for (itemitr = tmpItems.begin(); itemitr != tmpItems.end(); itemitr++) {
  247. Item* tmpItem = *itemitr;
  248. tmpItem->details.item_locked = false;
  249. }
  250. ClientPacketFunctions::StopCrafting(client);
  251. return;
  252. }
  253. ClientPacketFunctions::SendItemCreationUI(client, recipe);
  254. Tradeskill* tradeskill = new Tradeskill;
  255. tradeskill->player = client->GetPlayer();
  256. tradeskill->table = client->GetPlayer()->GetTarget();
  257. tradeskill->recipe = recipe;
  258. tradeskill->currentDurability = 1000;
  259. tradeskill->currentProgress = 0;
  260. tradeskill->nextUpdateTime = Timer::GetCurrentTime2() + 500;
  261. tradeskill->usedComponents = components;
  262. tradeskill->CurrentEvent = 0;
  263. tradeskill->eventChecked = false;
  264. tradeskill->eventCountered = false;
  265. m_tradeskills.writelock(__FUNCTION__, __LINE__);
  266. tradeskillList.insert(make_pair(client, tradeskill));
  267. SetClientIdleVisualState(client, tradeskill);
  268. m_tradeskills.releasewritelock(__FUNCTION__, __LINE__);
  269. // Unlock TS Spells and lock all others
  270. client->GetPlayer()->UnlockTSSpells();
  271. client->ClearSentItemDetails();
  272. EQ2Packet* outapp = client->GetPlayer()->SendInventoryUpdate(client->GetVersion());
  273. if (outapp)
  274. client->QueuePacket(outapp);
  275. }
  276. void TradeskillMgr::StopCrafting(Client* client, bool lock) {
  277. if (lock)
  278. m_tradeskills.writelock(__FUNCTION__, __LINE__);
  279. if (tradeskillList.count(client) == 0) {
  280. if (lock)
  281. m_tradeskills.releasewritelock(__FUNCTION__, __LINE__);
  282. return;
  283. }
  284. Tradeskill* tradeskill = 0;
  285. tradeskill = tradeskillList[client];
  286. //TODO: unlock inventory slots, give the product to the player, give tradeskill xp
  287. ClientPacketFunctions::StopCrafting(client);
  288. int32 dur = tradeskill->currentDurability;
  289. int32 progress = tradeskill->currentProgress;
  290. Recipe* recipe = tradeskill->recipe;
  291. vector<pair<int32, int16>>::iterator itr;
  292. Item* item = 0;
  293. int32 item_id = 0;
  294. int8 i = 0;
  295. int8 qty = 0;
  296. Recipe* playerRecipe = client->GetPlayer()->GetRecipeList()->GetRecipe(recipe->GetID());
  297. if(!playerRecipe)
  298. {
  299. LogWrite(TRADESKILL__ERROR, 0, "Tradeskills", "%s: TradeskillMgr::StopCrafting Error finding player recipe in their recipe book for recipe id %u", client->GetPlayer()->GetName(), recipe->GetID());
  300. client->Message(CHANNEL_COLOR_RED, "%s: StopCrafting Error finding player recipe in their recipe book for recipe id %u!", client->GetPlayer()->GetName(), recipe->GetID());
  301. if (lock)
  302. m_tradeskills.releasewritelock(__FUNCTION__, __LINE__);
  303. return;
  304. }
  305. bool updateInvReq = false;
  306. // cycle through the list of used items and remove them
  307. for (itr = tradeskill->usedComponents.begin(); itr != tradeskill->usedComponents.end(); itr++, i++) {
  308. // Get the item in the players inventory and remove or reduce the quantity
  309. int32 itmid = itr->first;
  310. qty = itr->second > 0 ? itr->second : 1;
  311. item = client->GetPlayer()->item_list.GetItemFromUniqueID(itmid);
  312. if (item && item->details.count <= qty)
  313. {
  314. item->details.item_locked = false;
  315. client->GetPlayer()->item_list.RemoveItem(item);
  316. updateInvReq = true;
  317. }
  318. else if(item) {
  319. item->details.count -= qty;
  320. item->details.item_locked = false;
  321. item->save_needed = true;
  322. updateInvReq = true;
  323. }
  324. else
  325. {
  326. LogWrite(TRADESKILL__ERROR, 0, "Tradeskills", "%s: TradeskillMgr::StopCrafting Error finding item %u to remove quantity for recipe id %u", client->GetPlayer()->GetName(), itmid, recipe->GetID());
  327. client->Message(CHANNEL_COLOR_RED, "%s: StopCrafting Error finding item %u to remove quantity for recipe id %u!", client->GetPlayer()->GetName(), itmid, recipe->GetID());
  328. }
  329. }
  330. if(updateInvReq)
  331. {
  332. EQ2Packet* outapp = client->GetPlayer()->SendInventoryUpdate(client->GetVersion());
  333. if (outapp)
  334. client->QueuePacket(outapp);
  335. }
  336. item = 0;
  337. qty = recipe->GetFuelComponentQuantity();
  338. item_id = recipe->components[5][0];
  339. int32 byproduct_itemid = 0;
  340. int16 byproduct_qty = 0;
  341. float tsx = 0;
  342. bool success = false;
  343. int8 HS = playerRecipe->GetHighestStage();
  344. if (progress < 400) { //stage 0
  345. if (recipe->products.count(0) > 0) {
  346. item_id = recipe->products[0]->product_id;
  347. qty = recipe->products[0]->product_qty;
  348. byproduct_itemid = recipe->products[0]->byproduct_id;
  349. byproduct_qty = recipe->products[0]->byproduct_qty;
  350. }
  351. tsx = 1;
  352. }
  353. else if (progress >= 400 && progress < 600) { //stage 1
  354. if (HS & (1 << (1 - 1))) {
  355. }
  356. else {
  357. playerRecipe->SetHighestStage(HS + 1 );
  358. database.UpdatePlayerRecipe(client->GetPlayer(), recipe->GetID(), playerRecipe->GetHighestStage());
  359. }
  360. if (recipe->products.count(1) > 0) {
  361. item_id = recipe->products[1]->product_id;
  362. qty = recipe->products[1]->product_qty;
  363. byproduct_itemid = recipe->products[1]->byproduct_id;
  364. byproduct_qty = recipe->products[1]->byproduct_qty;
  365. }
  366. tsx = .45;
  367. }
  368. //else if (progress >= 600 && progress < 800) { //stage 2
  369. else if ((dur < 200 && progress >= 600) || (dur >= 200 && progress >= 600 && progress < 800)) { //stage 2
  370. if (HS & (1 << (2 - 1))) {
  371. }
  372. else {
  373. playerRecipe->SetHighestStage(HS + 2);
  374. database.UpdatePlayerRecipe(client->GetPlayer(), recipe->GetID(), playerRecipe->GetHighestStage());
  375. }
  376. if (recipe->products.count(2) > 0) {
  377. item_id = recipe->products[2]->product_id;
  378. qty = recipe->products[2]->product_qty;
  379. byproduct_itemid = recipe->products[2]->byproduct_id;
  380. byproduct_qty = recipe->products[2]->byproduct_qty;
  381. }
  382. tsx = .30;
  383. }
  384. else if ((dur >= 200 && dur < 800 && progress >= 800) || (dur >= 800 && progress >= 800 && progress < 1000)) { // stage 3
  385. //else if (progress >= 800 && progress < 1000) { // stage 3
  386. if (HS & (1 << (3 - 1))) {
  387. }
  388. else {
  389. playerRecipe->SetHighestStage(HS + 4);
  390. database.UpdatePlayerRecipe(client->GetPlayer(), recipe->GetID(), playerRecipe->GetHighestStage());
  391. }
  392. if (recipe->products.count(3) > 0) {
  393. item_id = recipe->products[3]->product_id;
  394. qty = recipe->products[3]->product_qty;
  395. byproduct_itemid = recipe->products[3]->byproduct_id;
  396. byproduct_qty = recipe->products[3]->byproduct_qty;
  397. }
  398. tsx = .15;
  399. }
  400. else if (dur >= 800 && progress >= 1000) { // stage 4
  401. //else if (progress >= 1000) { // stage 4
  402. if (HS & (1 << (4 - 1))) {
  403. }
  404. else {
  405. playerRecipe->SetHighestStage(HS + 8);
  406. database.UpdatePlayerRecipe(client->GetPlayer(), recipe->GetID(), playerRecipe->GetHighestStage());
  407. }
  408. if (recipe->products.count(4) > 0) {
  409. success = true;
  410. item_id = recipe->products[4]->product_id;
  411. qty = recipe->products[4]->product_qty;
  412. byproduct_itemid = recipe->products[4]->byproduct_id;
  413. byproduct_qty = recipe->products[4]->byproduct_qty;
  414. }
  415. }
  416. if(progress > 1000 && item_id < 1) {
  417. LogWrite(TRADESKILL__INFO, 0, "Tradeskills", "%s: TradeskillMgr::StopCrafting progress over 1000, but no item id set with recipe id %u, finding override. Highest Stage: %u, progress: %u, durability %u", client->GetPlayer()->GetName(), recipe->GetID(), HS, progress, dur);
  418. for(int i=4;i>=0;i--) {
  419. if (recipe->products.count(i) > 0) {
  420. item_id = recipe->products[i]->product_id;
  421. qty = recipe->products[i]->product_qty;
  422. byproduct_itemid = recipe->products[i]->byproduct_id;
  423. byproduct_qty = recipe->products[i]->byproduct_qty;
  424. break;
  425. }
  426. }
  427. }
  428. if(item_id) {
  429. LogWrite(TRADESKILL__INFO, 0, "Tradeskills", "%s: TradeskillMgr::StopCrafting crafted item %u with recipe id %u. Highest Stage: %u, progress: %u, durability %u", client->GetPlayer()->GetName(), item_id, recipe->GetID(), HS, progress, dur);
  430. item = new Item(master_item_list.GetItem(item_id));
  431. if (!item) {
  432. LogWrite(TRADESKILL__ERROR, 0, "Tradeskills", "Item (%u) not found.", item_id);
  433. }
  434. else {
  435. item->details.count = qty;
  436. // use CHANNEL_COLOR_CHAT_RELATIONSHIP as that is the same value (4) as it is in a log for this message
  437. client->Message(CHANNEL_COLOR_CHAT_RELATIONSHIP, "You created %s.", item->CreateItemLink(client->GetVersion()).c_str());
  438. client->AddItem(item);
  439. if(byproduct_itemid) {
  440. Item* byproductItem = new Item(master_item_list.GetItem(byproduct_itemid));
  441. byproductItem->details.count = byproduct_qty;
  442. client->Message(CHANNEL_COLOR_CHAT_RELATIONSHIP, "You received %s as a byproduct.", byproductItem->CreateItemLink(client->GetVersion()).c_str());
  443. client->AddItem(byproductItem);
  444. }
  445. //Check for crafting quest updates
  446. int8 update_amt = 0;
  447. if(item->stack_count > 1)
  448. update_amt = 1;
  449. else
  450. update_amt = qty;
  451. client->GetPlayer()->CheckQuestsCraftUpdate(item, update_amt);
  452. }
  453. }
  454. else {
  455. LogWrite(TRADESKILL__WARNING, 0, "Tradeskills", "%s: TradeskillMgr::StopCrafting no item summoned for player with recipe id %u. Highest Stage: %u, progress: %u, durability %u", client->GetPlayer()->GetName(), recipe->GetID(), HS, progress, dur);
  456. }
  457. float xp = client->GetPlayer()->CalculateTSXP(recipe->GetLevel());
  458. xp = xp - (xp * tsx);
  459. if (xp > 0) {
  460. int16 level = client->GetPlayer()->GetTSLevel();
  461. if (client->GetPlayer()->AddTSXP((int32)xp)) {
  462. client->Message(CHANNEL_REWARD, "You gain %u Tradeskill XP!", (int32)xp);
  463. LogWrite(PLAYER__DEBUG, 0, "Player", "Player: %s earned %u tradeskill experience.", client->GetPlayer()->GetName(), (int32)xp);
  464. if(client->GetPlayer()->GetTSLevel() != level)
  465. client->ChangeTSLevel(level, client->GetPlayer()->GetTSLevel());
  466. client->GetPlayer()->SetCharSheetChanged(true);
  467. }
  468. }
  469. if(tradeskill && tradeskill->recipe) {
  470. if(success) {
  471. int32 success_anim = GetTechniqueSuccessAnim(client->GetVersion(), tradeskill->recipe->GetTechnique());
  472. client->GetPlayer()->GetZone()->PlayAnimation(client->GetPlayer(), success_anim);
  473. }
  474. else {
  475. int32 failure_anim = GetTechniqueFailureAnim(client->GetVersion(), tradeskill->recipe->GetTechnique());
  476. client->GetPlayer()->GetZone()->PlayAnimation(client->GetPlayer(), failure_anim);
  477. }
  478. }
  479. tradeskillList.erase(client);
  480. safe_delete(tradeskill);
  481. if (lock)
  482. m_tradeskills.releasewritelock(__FUNCTION__, __LINE__);
  483. // Lock TS spells and unlock all others
  484. client->GetPlayer()->LockTSSpells();
  485. }
  486. bool TradeskillMgr::IsClientCrafting(Client* client) {
  487. bool ret = false;
  488. m_tradeskills.readlock(__FUNCTION__, __LINE__);
  489. ret = tradeskillList.count(client) > 0;
  490. m_tradeskills.releasereadlock(__FUNCTION__, __LINE__);
  491. return ret;
  492. }
  493. void TradeskillMgr::CheckTradeskillEvent(Client* client, int16 icon) {
  494. // Check to see if the given client is crafting
  495. if (!IsClientCrafting(client))
  496. return;
  497. m_tradeskills.writelock(__FUNCTION__, __LINE__);
  498. // check to see if the client currently has an event and if it does if we had already tried to counter it this round
  499. if (tradeskillList[client]->CurrentEvent == 0 || tradeskillList[client]->eventChecked) {
  500. // No current event, or we already tried to counter it, return out
  501. m_tradeskills.releasewritelock(__FUNCTION__, __LINE__);
  502. return;
  503. }
  504. // set the eventChecked flag so we don't try to counter it again
  505. tradeskillList[client]->eventChecked = true;
  506. // compare the event icon with the given spell icon to see if we countered it and store the result for the update
  507. bool countered = (icon == tradeskillList[client]->CurrentEvent->Icon);
  508. tradeskillList[client]->eventCountered = countered;
  509. // send the success or fail message to the client
  510. client->Message(CHANNEL_NARRATIVE, "You %s %s.", countered ? "successfully countered" : "failed to counter", tradeskillList[client]->CurrentEvent->Name);
  511. // unlock the list and send the result packet
  512. m_tradeskills.releasewritelock(__FUNCTION__, __LINE__);
  513. ClientPacketFunctions::CounterReaction(client, countered);
  514. }
  515. Tradeskill* TradeskillMgr::GetTradeskill(Client* client) {
  516. if (tradeskillList.count(client) == 0)
  517. return 0;
  518. return tradeskillList[client];
  519. }
  520. int32 TradeskillMgr::GetTechniqueSuccessAnim(int16 version, int32 technique) {
  521. switch(technique) {
  522. case SKILL_ID_SCULPTING: {
  523. if(version <= 561) {
  524. return 3007; // leatherworking_success
  525. }
  526. return 11785; // 3005 = failure, 3006 = idle. 11783 = failure, 11784 = idle
  527. break;
  528. }
  529. case SKILL_ID_ARTISTRY: {
  530. if(version <= 561) {
  531. return 2319; // cooking_success
  532. }
  533. return 11245; // 2317 = failure, 2318 = idle. 11243 = failure, 11244 = idle
  534. break;
  535. }
  536. case SKILL_ID_FLETCHING: {
  537. if(version <= 561) {
  538. return 2356; // woodworking_success
  539. }
  540. return 13309; // 2354 = failure, 2355 = idle. 13307 = failure, 13308 = idle
  541. break;
  542. }
  543. case SKILL_ID_METALWORKING:
  544. case SKILL_ID_METALSHAPING: {
  545. if(version <= 561) {
  546. return 2442; // metalworking_success
  547. }
  548. return 11813; // 2441 = failure, 1810 = idle. 11811 = failure, 11812 = idle
  549. break;
  550. }
  551. case SKILL_ID_TAILORING: {
  552. if(version <= 561) {
  553. return 2352; // tailoring_success
  554. }
  555. return 13040; // 2350 = failure, 2351 = idle. 13038 = failure, 13039 = idle
  556. break;
  557. }
  558. case SKILL_ID_CHEMISTRY:{
  559. if(version <= 561) {
  560. return 2298; // alchemy_success
  561. }
  562. return 10749; // 2296 = failure, 2297 = idle. 10747 = failure, 10748 = idle
  563. break;
  564. }
  565. case SKILL_ID_ARTIFICING:{
  566. if(version <= 561) {
  567. return 2304; // artificing_success
  568. }
  569. return 10767; // 2302 = failure, 2303 = idle. 10765 = failure, 10766 = idle
  570. break;
  571. }
  572. case SKILL_ID_SCRIBING: {
  573. if(version <= 561) {
  574. return 0; // ???
  575. }
  576. return 0; // ??? = failure, 3131 = idle. ??? = failure, 12193 = idle
  577. break;
  578. }
  579. }
  580. return 0;
  581. }
  582. int32 TradeskillMgr::GetTechniqueFailureAnim(int16 version, int32 technique) {
  583. switch(technique) {
  584. case SKILL_ID_SCULPTING: {
  585. if(version <= 561) {
  586. return 3005; // leatherworking_failure
  587. }
  588. return 11783; // 3005 = failure, 3006 = idle. 11783 = failure, 11784 = idle
  589. break;
  590. }
  591. case SKILL_ID_ARTISTRY: {
  592. if(version <= 561) {
  593. return 2317; // cooking_failure
  594. }
  595. return 11243; // 2317 = failure, 2318 = idle. 11243 = failure, 11244 = idle
  596. break;
  597. }
  598. case SKILL_ID_FLETCHING: {
  599. if(version <= 561) {
  600. return 2354; // woodworking_failure
  601. }
  602. return 13307; // 2354 = failure, 2355 = idle. 13307 = failure, 13308 = idle
  603. break;
  604. }
  605. case SKILL_ID_METALWORKING:
  606. case SKILL_ID_METALSHAPING: {
  607. if(version <= 561) {
  608. return 2441; // metalworking_failure
  609. }
  610. return 11811; // 2441 = failure, 1810 = idle. 11811 = failure, 11812 = idle
  611. break;
  612. }
  613. case SKILL_ID_TAILORING: {
  614. if(version <= 561) {
  615. return 2350; // tailoring_failure
  616. }
  617. return 13038; // 2350 = failure, 2351 = idle. 13038 = failure, 13039 = idle
  618. break;
  619. }
  620. case SKILL_ID_CHEMISTRY:{
  621. if(version <= 561) {
  622. return 2298; // alchemy_success
  623. }
  624. return 10749; // 2296 = failure, 2297 = idle. 10747 = failure, 10748 = idle
  625. break;
  626. }
  627. case SKILL_ID_ARTIFICING:{
  628. if(version <= 561) {
  629. return 2302; // artificing_failure
  630. }
  631. return 10765; // 2302 = failure, 2303 = idle. 10765 = failure, 10766 = idle
  632. break;
  633. }
  634. case SKILL_ID_SCRIBING: {
  635. if(version <= 561) {
  636. return 0; // ???
  637. }
  638. return 0; // ??? = failure, 3131 = idle. ??? = failure, 12193 = idle
  639. break;
  640. }
  641. }
  642. return 0;
  643. }
  644. int32 TradeskillMgr::GetTechniqueIdleAnim(int16 version, int32 technique) {
  645. switch(technique) {
  646. case SKILL_ID_SCULPTING: {
  647. if(version <= 561) {
  648. return 3006; // leatherworking_idle
  649. }
  650. return 11784; // 3005 = failure, 3006 = idle. 11783 = failure, 11784 = idle
  651. break;
  652. }
  653. case SKILL_ID_ARTISTRY: {
  654. if(version <= 561) {
  655. return 2318; // cooking_idle
  656. }
  657. return 11244; // 2317 = failure, 2318 = idle. 11243 = failure, 11244 = idle
  658. break;
  659. }
  660. case SKILL_ID_FLETCHING: {
  661. if(version <= 561) {
  662. return 2355; // woodworking_idle
  663. }
  664. return 13308; // 2354 = failure, 2355 = idle. 13307 = failure, 13308 = idle
  665. break;
  666. }
  667. case SKILL_ID_METALWORKING:
  668. case SKILL_ID_METALSHAPING: {
  669. if(version <= 561) {
  670. return 1810; // metalworking_idle
  671. }
  672. return 11812; // 2441 = failure, 1810 = idle. 11811 = failure, 11812 = idle
  673. break;
  674. }
  675. case SKILL_ID_TAILORING: {
  676. if(version <= 561) {
  677. return 2351; // tailoring_idle
  678. }
  679. return 13039; // 2350 = failure, 2351 = idle. 13038 = failure, 13039 = idle
  680. break;
  681. }
  682. case SKILL_ID_CHEMISTRY:{
  683. if(version <= 561) {
  684. return 2297; // alchemy_idle
  685. }
  686. return 10748; // 2296 = failure, 2297 = idle. 10747 = failure, 10748 = idle
  687. break;
  688. }
  689. case SKILL_ID_ARTIFICING:{
  690. if(version <= 561) {
  691. return 2303; // artificing_idle
  692. }
  693. return 10766; // 2302 = failure, 2303 = idle. 10765 = failure, 10766 = idle
  694. break;
  695. }
  696. case SKILL_ID_SCRIBING: {
  697. if(version <= 561) {
  698. return 3131; // scribing_idle
  699. }
  700. return 12193; // ??? = failure, 3131 = idle. ??? = failure, 12193 = idle
  701. break;
  702. }
  703. }
  704. return 0;
  705. }
  706. int32 TradeskillMgr::GetMissTargetAnim(int16 version) {
  707. if(version <= 561) {
  708. return 1144;
  709. }
  710. return 11814; // 11815 seems also possible?
  711. }
  712. int32 TradeskillMgr::GetKillMissTargetAnim(int16 version) {
  713. if(version <= 561) {
  714. return 33912;
  715. }
  716. return 44582; // 44583 seems also possible?
  717. }
  718. void TradeskillMgr::SetClientIdleVisualState(Client* client, Tradeskill* ts) {
  719. if(!client || !ts || !client->GetPlayer()) {
  720. return;
  721. }
  722. int32 idle_anim = 0;
  723. if(ts->recipe) {
  724. idle_anim = GetTechniqueIdleAnim(client->GetVersion(), ts->recipe->GetTechnique());
  725. }
  726. if(idle_anim) {
  727. client->GetPlayer()->SetTempVisualState(idle_anim);
  728. }
  729. }
  730. MasterTradeskillEventsList::MasterTradeskillEventsList() {
  731. m_eventList.SetName("MasterTradeskillEventsList::eventList");
  732. }
  733. MasterTradeskillEventsList::~MasterTradeskillEventsList() {
  734. m_eventList.writelock(__FUNCTION__, __LINE__);
  735. map<int32, vector<TradeskillEvent*> >::iterator itr;
  736. vector<TradeskillEvent*>::iterator ts_itr;
  737. for (itr = eventList.begin(); itr != eventList.end(); itr++){
  738. for (ts_itr = itr->second.begin(); ts_itr != itr->second.end(); ts_itr++){
  739. safe_delete(*ts_itr);
  740. }
  741. }
  742. eventList.clear();
  743. m_eventList.releasewritelock(__FUNCTION__, __LINE__);
  744. }
  745. void MasterTradeskillEventsList::AddEvent(TradeskillEvent* tradeskillEvent) {
  746. m_eventList.writelock(__FUNCTION__, __LINE__);
  747. eventList[tradeskillEvent->Technique].push_back(tradeskillEvent);
  748. m_eventList.releasewritelock(__FUNCTION__, __LINE__);
  749. }
  750. vector<TradeskillEvent*>* MasterTradeskillEventsList::GetEventByTechnique(int32 technique) {
  751. if (eventList.count(technique) == 0)
  752. return 0;
  753. return &eventList[technique];
  754. }
  755. int32 MasterTradeskillEventsList::Size() {
  756. int32 count = 0;
  757. m_eventList.readlock(__FUNCTION__, __LINE__);
  758. map<int32, vector<TradeskillEvent*> >::iterator itr;
  759. for (itr = eventList.begin(); itr != eventList.end(); itr++)
  760. count += itr->second.size();
  761. m_eventList.releasereadlock(__FUNCTION__, __LINE__);
  762. return count;
  763. }