Tradeskills.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  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 "Tradeskills.h"
  17. #include "../client.h"
  18. #include "../../common/ConfigReader.h"
  19. #include "../classes.h"
  20. //#include "../../common/debug.h"
  21. #include "../../common/Log.h"
  22. //#include "../zoneserver.h"
  23. //#include "../Skills.h"
  24. //#include "../classes.h"
  25. #include "../World.h"
  26. //#include "../LuaInterface.h"
  27. #include "../ClientPacketFunctions.h"
  28. #include "../WorldDatabase.h"
  29. #include "../Rules/Rules.h"
  30. extern Classes classes;
  31. extern ConfigReader configReader;
  32. extern MasterSkillList master_skill_list;
  33. extern MasterRecipeList master_recipe_list;
  34. extern MasterTradeskillEventsList master_tradeskillevent_list;
  35. extern WorldDatabase database;
  36. extern RuleManager rule_manager;
  37. TradeskillMgr::TradeskillMgr() {
  38. m_tradeskills.SetName("TradeskillMgr::tradeskillsList");
  39. // % chance for each was made up by me (Jabantiz) and may need some tweaking
  40. // 2% for crit fail
  41. m_success = rule_manager.GetGlobalRule(R_World, TradeskillSuccessChance)->GetFloat();
  42. m_critSuccess = rule_manager.GetGlobalRule(R_World, TradeskillCritSuccessChance)->GetFloat();
  43. m_fail = rule_manager.GetGlobalRule(R_World, TradeskillFailChance)->GetFloat();
  44. m_critFail = rule_manager.GetGlobalRule(R_World, TradeskillCritFailChance)->GetFloat();
  45. m_eventChance = rule_manager.GetGlobalRule(R_World, TradeskillEventChance)->GetFloat();
  46. if ((m_success + m_critSuccess + m_fail + m_critFail) != 100.0f) {
  47. LogWrite(TRADESKILL__ERROR, 0, "Tradeskills", "Success, crit success, fail, and crit fail MUST add up to 100, reverting to defaults...");
  48. m_success = 87.0f;
  49. m_critSuccess = 2.0f;
  50. m_fail = 10.0f;
  51. m_critFail = 1.0f;
  52. }
  53. }
  54. TradeskillMgr::~TradeskillMgr() {
  55. m_tradeskills.writelock(__FUNCTION__, __LINE__);
  56. map<Client*, Tradeskill*>::iterator itr;
  57. for (itr = tradeskillList.begin(); itr != tradeskillList.end(); itr++)
  58. safe_delete(itr->second);
  59. tradeskillList.clear();
  60. m_tradeskills.releasewritelock(__FUNCTION__, __LINE__);
  61. }
  62. void TradeskillMgr::Process() {
  63. m_tradeskills.writelock(__FUNCTION__, __LINE__);
  64. map<Client*, Tradeskill*>::iterator itr = tradeskillList.begin();
  65. while (itr != tradeskillList.end()) {
  66. Tradeskill* tradeskill = 0;
  67. tradeskill = itr->second;
  68. if (!tradeskill)
  69. continue;
  70. if (Timer::GetCurrentTime2() >= tradeskill->nextUpdateTime) {
  71. Client* client = itr->first;
  72. sint32 progress = 0;
  73. sint32 durability = 0;
  74. /*
  75. Following was grabbed from
  76. http://eq2.stratics.com/content/guides/padasher_crafting_2.php
  77. old but the base fail/succes should still be the same
  78. -100 Durability / -50 Progress (Critical Failure)
  79. -50 Durability / 0 Progress (Failure)
  80. -10 Durability / +50 Progress (Standard tick)
  81. +10 Durability / + 100 Progress (Critical Success)
  82. */
  83. float roll = MakeRandomFloat(0, 100);
  84. int8 effect = 0; //1 is critical success, 2 is success, 3 is failure, and 4 is critical failure.
  85. float success = m_success;
  86. float crit_success = m_critSuccess;
  87. float fail = m_fail;
  88. float crit_fail = m_critFail;
  89. // Modify the % chance for success based off of stats
  90. client->GetPlayer()->MStats.lock();
  91. fail -= client->GetPlayer()->stats[ITEM_STAT_SUCCESS_MOD];
  92. success += client->GetPlayer()->stats[ITEM_STAT_SUCCESS_MOD];
  93. client->GetPlayer()->MStats.unlock();
  94. // add values together for the if
  95. crit_success += crit_fail;
  96. fail += crit_success;
  97. success += fail;
  98. // Crit fail
  99. if (roll <= crit_fail) {
  100. progress = -50;
  101. durability = -100;
  102. effect = 4;
  103. client->SimpleMessage(CHANNEL_COLOR_YELLOW, "Critical failure!");
  104. }
  105. // Crit success
  106. else if (roll > crit_fail && roll <= crit_success) {
  107. progress = 100;
  108. durability = 10;
  109. effect = 1;
  110. client->SimpleMessage(CHANNEL_COLOR_YELLOW, "Critical success!");
  111. }
  112. // Fail
  113. else if (roll > crit_success && roll <= fail) {
  114. progress = 0;
  115. durability = -50;
  116. effect = 3;
  117. }
  118. // Success
  119. else if (roll > fail && roll <= success) {
  120. progress = 50;
  121. durability = -10;
  122. effect = 2;
  123. }
  124. else {
  125. // Just a debug, should never end up in this, if we do write out a log but treat as a success for the player
  126. 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);
  127. progress = 50;
  128. durability = -10;
  129. effect = 2;
  130. }
  131. // Check to see if there was an event, if there was give out the rewards/penalties for it
  132. if (tradeskill->CurrentEvent) {
  133. if (tradeskill->eventCountered) {
  134. progress += tradeskill->CurrentEvent->SuccessProgress;
  135. durability += tradeskill->CurrentEvent->SuccessDurability;
  136. }
  137. else {
  138. progress += tradeskill->CurrentEvent->FailProgress;
  139. durability += tradeskill->CurrentEvent->FailDurability;
  140. }
  141. }
  142. // Modify the progress/durability by the players stats
  143. client->GetPlayer()->MStats.lock();
  144. progress += client->GetPlayer()->stats[ITEM_STAT_PROGRESS_ADD];
  145. durability += client->GetPlayer()->stats[ITEM_STAT_DURABILITY_ADD];
  146. client->GetPlayer()->MStats.unlock();
  147. tradeskill->currentDurability += durability;
  148. tradeskill->currentProgress += progress;
  149. PacketStruct* packet = configReader.getStruct("WS_UpdateCreateItem", client->GetVersion());
  150. if (packet) {
  151. packet->setDataByName("spawn_id", client->GetPlayer()->GetIDWithPlayerSpawn(tradeskill->table));
  152. packet->setDataByName("effect", effect);
  153. packet->setDataByName("total_durability", tradeskill->currentDurability);
  154. packet->setDataByName("total_progress", tradeskill->currentProgress);
  155. packet->setDataByName("durability_change", durability);
  156. packet->setDataByName("progress_change", progress);
  157. if (tradeskill->currentProgress >= 1000)
  158. packet->setDataByName("progress_level", 4);
  159. else if (tradeskill->currentProgress >= 800)
  160. packet->setDataByName("progress_level", 3);
  161. else if (tradeskill->currentProgress >= 600)
  162. packet->setDataByName("progress_level", 2);
  163. else if (tradeskill->currentProgress >= 400)
  164. packet->setDataByName("progress_level", 1);
  165. else
  166. packet->setDataByName("progress_level", 0);
  167. // Reset the tradeskill event
  168. tradeskill->CurrentEvent = 0;
  169. tradeskill->eventChecked = false;
  170. tradeskill->eventCountered = false;
  171. // 15% chance for an event (change this to a rule probably)
  172. int eventRoll = MakeRandomFloat(0, 100);
  173. if (eventRoll <= m_eventChance) {
  174. // Get a vector of all possible events for this crafting technique
  175. vector<TradeskillEvent*>* events = master_tradeskillevent_list.GetEventByTechnique(tradeskill->recipe->GetTechnique());
  176. if (events) {
  177. // Get the size of the vector
  178. int size = events->size();
  179. // Get a random number from 0 to size - 1 to use as an index
  180. int index = MakeRandomInt(0, size - 1);
  181. // use the index to get an event
  182. TradeskillEvent* TSEvent = events->at(index);
  183. if (TSEvent) {
  184. // Now that we got a random event set it in the packet
  185. packet->setDataByName("reaction_icon", TSEvent->Icon);
  186. packet->setDataByName("reaction_name", TSEvent->Name);
  187. // Set the current tradeskill event
  188. tradeskill->CurrentEvent = TSEvent;
  189. }
  190. }
  191. }
  192. client->QueuePacket(packet->serialize());
  193. safe_delete(packet);
  194. }
  195. if (tradeskill->currentProgress >= 1000) {
  196. itr++;
  197. StopCrafting(client, false);
  198. continue;
  199. }
  200. else
  201. tradeskill->nextUpdateTime = Timer::GetCurrentTime2() + 4000;
  202. }
  203. itr++;
  204. }
  205. m_tradeskills.releasewritelock(__FUNCTION__, __LINE__);
  206. }
  207. void TradeskillMgr::BeginCrafting(Client* client, vector<int32> components) {
  208. Recipe* recipe = master_recipe_list.GetRecipe(client->GetPlayer()->GetCurrentRecipe());
  209. if (!recipe) {
  210. LogWrite(TRADESKILL__ERROR, 0, "Recipe", "Recipe (%u) not found in TradeskillMgr::BeginCrafting()", client->GetPlayer()->GetCurrentRecipe());
  211. ClientPacketFunctions::StopCrafting(client);
  212. return;
  213. }
  214. ClientPacketFunctions::SendItemCreationUI(client, recipe);
  215. Tradeskill* tradeskill = new Tradeskill;
  216. tradeskill->player = client->GetPlayer();
  217. tradeskill->table = client->GetPlayer()->GetTarget();
  218. tradeskill->recipe = recipe;
  219. tradeskill->currentDurability = 1000;
  220. tradeskill->currentProgress = 0;
  221. tradeskill->nextUpdateTime = Timer::GetCurrentTime2() + 500;
  222. tradeskill->usedComponents = components;
  223. tradeskill->CurrentEvent = 0;
  224. tradeskill->eventChecked = false;
  225. tradeskill->eventCountered = false;
  226. m_tradeskills.writelock(__FUNCTION__, __LINE__);
  227. tradeskillList.insert(make_pair(client, tradeskill));
  228. m_tradeskills.releasewritelock(__FUNCTION__, __LINE__);
  229. // Unlock TS Spells and lock all others
  230. client->GetPlayer()->UnlockTSSpells();
  231. // TODO: use the vecotr to lock inventory slots
  232. /*vector<Item*>::iterator itr;
  233. for (itr = components.begin(); itr != components.end(); itr++) {
  234. Item* item = *itr;
  235. //client->GetPlayer()->SendInventoryUpdate
  236. item->details.inv_slot_id;
  237. }*/
  238. }
  239. void TradeskillMgr::StopCrafting(Client* client, bool lock) {
  240. if (lock)
  241. m_tradeskills.writelock(__FUNCTION__, __LINE__);
  242. if (tradeskillList.count(client) == 0) {
  243. if (lock)
  244. m_tradeskills.releasewritelock(__FUNCTION__, __LINE__);
  245. return;
  246. }
  247. Tradeskill* tradeskill = 0;
  248. tradeskill = tradeskillList[client];
  249. //TODO: unlock inventory slots, give the product to the player, give tradeskill xp
  250. ClientPacketFunctions::StopCrafting(client);
  251. int32 dur = tradeskill->currentDurability;
  252. int32 progress = tradeskill->currentProgress;
  253. Recipe* recipe = tradeskill->recipe;
  254. vector<int32>::iterator itr;
  255. Item* item = 0;
  256. int32 item_id = 0;
  257. int8 i = 0;
  258. int8 qty = 0;
  259. Recipe* playerRecipe = client->GetPlayer()->GetRecipeList()->GetRecipe(recipe->GetID());
  260. if(!playerRecipe)
  261. {
  262. 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());
  263. client->Message(CHANNEL_COLOR_RED, "%s: StopCrafting Error finding player recipe in their recipe book for recipe id %u!", client->GetPlayer()->GetName(), recipe->GetID());
  264. if (lock)
  265. m_tradeskills.releasewritelock(__FUNCTION__, __LINE__);
  266. return;
  267. }
  268. // cycle through the list of used items and remove them
  269. for (itr = tradeskill->usedComponents.begin(); itr != tradeskill->usedComponents.end(); itr++, i++) {
  270. // get the quantity to remove, first item in the vectore is always the primary, last is always the fuel
  271. if (i == 0)
  272. qty = 1;
  273. else if (i == 1 && i != tradeskill->usedComponents.size() - 1)
  274. qty = recipe->GetBuild1ComponentQuantity();
  275. else if (i == 2 && i != tradeskill->usedComponents.size() - 1)
  276. qty = recipe->GetBuild2ComponentQuantity();
  277. else if (i == 3 && i != tradeskill->usedComponents.size() - 1)
  278. qty = recipe->GetBuild3ComponentQuantity();
  279. else if (i == 4 && i != tradeskill->usedComponents.size() - 1)
  280. qty = recipe->GetBuild4ComponentQuantity();
  281. else if (i == 5 || i == tradeskill->usedComponents.size() - 1)
  282. qty = recipe->GetFuelComponentQuantity();
  283. // Get the item in the players inventory and remove or reduce the quantity
  284. int32 itmid = *itr;
  285. item = client->GetPlayer()->item_list.GetItemFromID(itmid);
  286. if (item && item->details.count <= qty)
  287. client->GetPlayer()->item_list.RemoveItem(item);
  288. else if(item) {
  289. item->details.count -= qty;
  290. item->save_needed = true;
  291. }
  292. else
  293. {
  294. 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());
  295. client->Message(CHANNEL_COLOR_RED, "%s: StopCrafting Error finding item %u to remove quantity for recipe id %u!", client->GetPlayer()->GetName(), itmid, recipe->GetID());
  296. }
  297. }
  298. item = 0;
  299. qty = recipe->GetFuelComponentQuantity();
  300. item_id = recipe->components[5][0];
  301. if (progress >= 400 && progress < 600) {
  302. if (playerRecipe->GetHighestStage() < 1) {
  303. playerRecipe->SetHighestStage(1);
  304. database.UpdatePlayerRecipe(client->GetPlayer(), recipe->GetID(), 1);
  305. }
  306. if (recipe->products.count(1) > 0) {
  307. item_id = recipe->products[1]->product_id;
  308. qty = recipe->products[1]->product_qty;
  309. }
  310. }
  311. else if ((dur < 200 && progress >= 600) || (dur >= 200 && progress >= 600 && progress < 800)) {
  312. if (playerRecipe->GetHighestStage() < 2) {
  313. playerRecipe->SetHighestStage(2);
  314. database.UpdatePlayerRecipe(client->GetPlayer(), recipe->GetID(), 2);
  315. }
  316. if (recipe->products.count(2) > 0) {
  317. item_id = recipe->products[2]->product_id;
  318. qty = recipe->products[2]->product_qty;
  319. }
  320. }
  321. else if ((dur >= 200 && dur < 800 && progress >= 800) || (dur >= 800 && progress >= 800 && progress < 1000)) {
  322. if (playerRecipe->GetHighestStage() < 3) {
  323. playerRecipe->SetHighestStage(3);
  324. database.UpdatePlayerRecipe(client->GetPlayer(), recipe->GetID(), 3);
  325. }
  326. if (recipe->products.count(3) > 0) {
  327. item_id = recipe->products[3]->product_id;
  328. qty = recipe->products[3]->product_qty;
  329. }
  330. }
  331. else if (dur >= 800 && progress >= 1000) {
  332. if (playerRecipe->GetHighestStage() < 4) {
  333. playerRecipe->SetHighestStage(4);
  334. database.UpdatePlayerRecipe(client->GetPlayer(), recipe->GetID(), 4);
  335. }
  336. if (recipe->products.count(4) > 0) {
  337. item_id = recipe->products[4]->product_id;
  338. qty = recipe->products[4]->product_qty;
  339. }
  340. }
  341. item = new Item(master_item_list.GetItem(item_id));
  342. if (!item) {
  343. LogWrite(TRADESKILL__ERROR, 0, "Tradeskills", "Item (%u) not found.", item_id);
  344. }
  345. else {
  346. item->details.count = qty;
  347. // use CHANNEL_COLOR_CHAT_RELATIONSHIP as that is the same value (4) as it is in a log for this message
  348. client->Message(CHANNEL_COLOR_CHAT_RELATIONSHIP, "You created %s.", item->CreateItemLink(client->GetVersion()).c_str());
  349. client->AddItem(item);
  350. //Check for crafting quest updates
  351. int8 update_amt = 0;
  352. if(item->stack_count > 1)
  353. update_amt = 1;
  354. else
  355. update_amt = qty;
  356. client->GetPlayer()->CheckQuestsCraftUpdate(item, update_amt);
  357. }
  358. float xp = client->GetPlayer()->CalculateTSXP(recipe->GetLevel());
  359. if (xp > 0) {
  360. int16 level = client->GetPlayer()->GetTSLevel();
  361. if (client->GetPlayer()->AddTSXP((int32)xp)) {
  362. client->Message(CHANNEL_REWARD, "You gain %u Tradeskill XP!", (int32)xp);
  363. LogWrite(PLAYER__DEBUG, 0, "Player", "Player: %s earned %u tradeskill experience.", client->GetPlayer()->GetName(), (int32)xp);
  364. if(client->GetPlayer()->GetTSLevel() != level)
  365. client->ChangeTSLevel(level, client->GetPlayer()->GetTSLevel());
  366. client->GetPlayer()->SetCharSheetChanged(true);
  367. }
  368. }
  369. tradeskillList.erase(client);
  370. safe_delete(tradeskill);
  371. if (lock)
  372. m_tradeskills.releasewritelock(__FUNCTION__, __LINE__);
  373. // Lock TS spells and unlock all others
  374. client->GetPlayer()->LockTSSpells();
  375. }
  376. bool TradeskillMgr::IsClientCrafting(Client* client) {
  377. bool ret = false;
  378. m_tradeskills.readlock(__FUNCTION__, __LINE__);
  379. ret = tradeskillList.count(client) > 0;
  380. m_tradeskills.releasereadlock(__FUNCTION__, __LINE__);
  381. return ret;
  382. }
  383. void TradeskillMgr::CheckTradeskillEvent(Client* client, int16 icon) {
  384. // Check to see if the given client is crafting
  385. if (!IsClientCrafting(client))
  386. return;
  387. m_tradeskills.writelock(__FUNCTION__, __LINE__);
  388. // check to see if the client currently has an event and if it does if we had already tried to counter it this round
  389. if (tradeskillList[client]->CurrentEvent == 0 || tradeskillList[client]->eventChecked) {
  390. // No current event, or we already tried to counter it, return out
  391. m_tradeskills.releasewritelock(__FUNCTION__, __LINE__);
  392. return;
  393. }
  394. // set the eventChecked flag so we don't try to counter it again
  395. tradeskillList[client]->eventChecked = true;
  396. // compare the event icon with the given spell icon to see if we countered it and store the result for the update
  397. bool countered = (icon == tradeskillList[client]->CurrentEvent->Icon);
  398. tradeskillList[client]->eventCountered = countered;
  399. // send the success or fail message to the client
  400. client->Message(CHANNEL_NARRATIVE, "You %s %s.", countered ? "successfully countered" : "failed to counter", tradeskillList[client]->CurrentEvent->Name);
  401. // unlock the list and send the result packet
  402. m_tradeskills.releasewritelock(__FUNCTION__, __LINE__);
  403. ClientPacketFunctions::CounterReaction(client, countered);
  404. }
  405. Tradeskill* TradeskillMgr::GetTradeskill(Client* client) {
  406. if (tradeskillList.count(client) == 0)
  407. return 0;
  408. return tradeskillList[client];
  409. }
  410. MasterTradeskillEventsList::MasterTradeskillEventsList() {
  411. m_eventList.SetName("MasterTradeskillEventsList::eventList");
  412. }
  413. MasterTradeskillEventsList::~MasterTradeskillEventsList() {
  414. m_eventList.writelock(__FUNCTION__, __LINE__);
  415. map<int32, vector<TradeskillEvent*> >::iterator itr;
  416. vector<TradeskillEvent*>::iterator ts_itr;
  417. for (itr = eventList.begin(); itr != eventList.end(); itr++){
  418. for (ts_itr = itr->second.begin(); ts_itr != itr->second.end(); ts_itr++){
  419. safe_delete(*ts_itr);
  420. }
  421. }
  422. eventList.clear();
  423. m_eventList.releasewritelock(__FUNCTION__, __LINE__);
  424. }
  425. void MasterTradeskillEventsList::AddEvent(TradeskillEvent* tradeskillEvent) {
  426. m_eventList.writelock(__FUNCTION__, __LINE__);
  427. eventList[tradeskillEvent->Technique].push_back(tradeskillEvent);
  428. m_eventList.releasewritelock(__FUNCTION__, __LINE__);
  429. }
  430. vector<TradeskillEvent*>* MasterTradeskillEventsList::GetEventByTechnique(int32 technique) {
  431. if (eventList.count(technique) == 0)
  432. return 0;
  433. return &eventList[technique];
  434. }
  435. int32 MasterTradeskillEventsList::Size() {
  436. int32 count = 0;
  437. m_eventList.readlock(__FUNCTION__, __LINE__);
  438. map<int32, vector<TradeskillEvent*> >::iterator itr;
  439. for (itr = eventList.begin(); itr != eventList.end(); itr++)
  440. count += itr->second.size();
  441. m_eventList.releasereadlock(__FUNCTION__, __LINE__);
  442. return count;
  443. }