9
3

Tradeskills.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  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. fail -= client->GetPlayer()->stats[ITEM_STAT_SUCCESS_MOD];
  91. success += client->GetPlayer()->stats[ITEM_STAT_SUCCESS_MOD];
  92. // add values together for the if
  93. crit_success += crit_fail;
  94. fail += crit_success;
  95. success += fail;
  96. // Crit fail
  97. if (roll <= crit_fail) {
  98. progress = -50;
  99. durability = -100;
  100. effect = 4;
  101. client->SimpleMessage(CHANNEL_COLOR_YELLOW, "Critical failure!");
  102. }
  103. // Crit success
  104. else if (roll > crit_fail && roll <= crit_success) {
  105. progress = 100;
  106. durability = 10;
  107. effect = 1;
  108. client->SimpleMessage(CHANNEL_COLOR_YELLOW, "Critical success!");
  109. }
  110. // Fail
  111. else if (roll > crit_success && roll <= fail) {
  112. progress = 0;
  113. durability = -50;
  114. effect = 3;
  115. }
  116. // Success
  117. else if (roll > fail && roll <= success) {
  118. progress = 50;
  119. durability = -10;
  120. effect = 2;
  121. }
  122. else {
  123. // Just a debug, should never end up in this, if we do write out a log but treat as a success for the player
  124. 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);
  125. progress = 50;
  126. durability = -10;
  127. effect = 2;
  128. }
  129. // Check to see if there was an event, if there was give out the rewards/penalties for it
  130. if (tradeskill->CurrentEvent) {
  131. if (tradeskill->eventCountered) {
  132. progress += tradeskill->CurrentEvent->SuccessProgress;
  133. durability += tradeskill->CurrentEvent->SuccessDurability;
  134. }
  135. else {
  136. progress += tradeskill->CurrentEvent->FailProgress;
  137. durability += tradeskill->CurrentEvent->FailDurability;
  138. }
  139. }
  140. // Modify the progress/durability by the players stats
  141. progress += client->GetPlayer()->stats[ITEM_STAT_PROGRESS_ADD];
  142. durability += client->GetPlayer()->stats[ITEM_STAT_DURABILITY_ADD];
  143. tradeskill->currentDurability += durability;
  144. tradeskill->currentProgress += progress;
  145. PacketStruct* packet = configReader.getStruct("WS_UpdateCreateItem", client->GetVersion());
  146. if (packet) {
  147. packet->setDataByName("spawn_id", client->GetPlayer()->player_spawn_reverse_id_map[tradeskill->table]);
  148. packet->setDataByName("effect", effect);
  149. packet->setDataByName("total_durability", tradeskill->currentDurability);
  150. packet->setDataByName("total_progress", tradeskill->currentProgress);
  151. packet->setDataByName("durability_change", durability);
  152. packet->setDataByName("progress_change", progress);
  153. if (tradeskill->currentProgress >= 1000)
  154. packet->setDataByName("progress_level", 4);
  155. else if (tradeskill->currentProgress >= 800)
  156. packet->setDataByName("progress_level", 3);
  157. else if (tradeskill->currentProgress >= 600)
  158. packet->setDataByName("progress_level", 2);
  159. else if (tradeskill->currentProgress >= 400)
  160. packet->setDataByName("progress_level", 1);
  161. else
  162. packet->setDataByName("progress_level", 0);
  163. // Reset the tradeskill event
  164. tradeskill->CurrentEvent = 0;
  165. tradeskill->eventChecked = false;
  166. tradeskill->eventCountered = false;
  167. // 15% chance for an event (change this to a rule probably)
  168. int eventRoll = MakeRandomFloat(0, 100);
  169. if (eventRoll <= m_eventChance) {
  170. // Get a vector of all possible events for this crafting technique
  171. vector<TradeskillEvent*>* events = master_tradeskillevent_list.GetEventByTechnique(tradeskill->recipe->GetTechnique());
  172. if (events) {
  173. // Get the size of the vector
  174. int size = events->size();
  175. // Get a random number from 0 to size - 1 to use as an index
  176. int index = MakeRandomInt(0, size - 1);
  177. // use the index to get an event
  178. TradeskillEvent* TSEvent = events->at(index);
  179. if (TSEvent) {
  180. // Now that we got a random event set it in the packet
  181. packet->setDataByName("reaction_icon", TSEvent->Icon);
  182. packet->setDataByName("reaction_name", TSEvent->Name);
  183. // Set the current tradeskill event
  184. tradeskill->CurrentEvent = TSEvent;
  185. }
  186. }
  187. }
  188. client->QueuePacket(packet->serialize());
  189. safe_delete(packet);
  190. }
  191. if (tradeskill->currentProgress >= 1000) {
  192. itr++;
  193. StopCrafting(client, false);
  194. continue;
  195. }
  196. else
  197. tradeskill->nextUpdateTime = Timer::GetCurrentTime2() + 4000;
  198. }
  199. itr++;
  200. }
  201. m_tradeskills.releasewritelock(__FUNCTION__, __LINE__);
  202. }
  203. void TradeskillMgr::BeginCrafting(Client* client, vector<int32> components) {
  204. Recipe* recipe = master_recipe_list.GetRecipe(client->GetPlayer()->GetCurrentRecipe());
  205. if (!recipe) {
  206. LogWrite(TRADESKILL__ERROR, 0, "Recipe", "Recipe (%u) not found in TradeskillMgr::BeginCrafting()", client->GetPlayer()->GetCurrentRecipe());
  207. ClientPacketFunctions::StopCrafting(client);
  208. return;
  209. }
  210. ClientPacketFunctions::SendItemCreationUI(client, recipe);
  211. Tradeskill* tradeskill = new Tradeskill;
  212. tradeskill->player = client->GetPlayer();
  213. tradeskill->table = client->GetPlayer()->GetTarget();
  214. tradeskill->recipe = recipe;
  215. tradeskill->currentDurability = 1000;
  216. tradeskill->currentProgress = 0;
  217. tradeskill->nextUpdateTime = Timer::GetCurrentTime2() + 500;
  218. tradeskill->usedComponents = components;
  219. tradeskill->CurrentEvent = 0;
  220. tradeskill->eventChecked = false;
  221. tradeskill->eventCountered = false;
  222. m_tradeskills.writelock(__FUNCTION__, __LINE__);
  223. tradeskillList.insert(make_pair(client, tradeskill));
  224. m_tradeskills.releasewritelock(__FUNCTION__, __LINE__);
  225. // Unlock TS Spells and lock all others
  226. client->GetPlayer()->UnlockTSSpells();
  227. // TODO: use the vecotr to lock inventory slots
  228. /*vector<Item*>::iterator itr;
  229. for (itr = components.begin(); itr != components.end(); itr++) {
  230. Item* item = *itr;
  231. //client->GetPlayer()->SendInventoryUpdate
  232. item->details.inv_slot_id;
  233. }*/
  234. }
  235. void TradeskillMgr::StopCrafting(Client* client, bool lock) {
  236. if (lock)
  237. m_tradeskills.writelock(__FUNCTION__, __LINE__);
  238. if (tradeskillList.count(client) == 0) {
  239. if (lock)
  240. m_tradeskills.releasewritelock(__FUNCTION__, __LINE__);
  241. return;
  242. }
  243. Tradeskill* tradeskill = 0;
  244. tradeskill = tradeskillList[client];
  245. //TODO: unlock inventory slots, give the product to the player, give tradeskill xp
  246. ClientPacketFunctions::StopCrafting(client);
  247. int32 dur = tradeskill->currentDurability;
  248. int32 progress = tradeskill->currentProgress;
  249. Recipe* recipe = tradeskill->recipe;
  250. vector<int32>::iterator itr;
  251. Item* item = 0;
  252. int32 item_id = 0;
  253. int8 i = 0;
  254. int8 qty = 0;
  255. // cycle through the list of used items and remove them
  256. for (itr = tradeskill->usedComponents.begin(); itr != tradeskill->usedComponents.end(); itr++, i++) {
  257. // get the quantity to remove, first item in the vectore is always the primary, last is always the fuel
  258. if (i == 0)
  259. qty = 1;
  260. else if (i == 1 && i != tradeskill->usedComponents.size() - 1)
  261. qty = recipe->GetBuild1ComponentQuantity();
  262. else if (i == 2 && i != tradeskill->usedComponents.size() - 1)
  263. qty = recipe->GetBuild2ComponentQuantity();
  264. else if (i == 3 && i != tradeskill->usedComponents.size() - 1)
  265. qty = recipe->GetBuild3ComponentQuantity();
  266. else if (i == 4 && i != tradeskill->usedComponents.size() - 1)
  267. qty = recipe->GetBuild4ComponentQuantity();
  268. else if (i == 5 || i == tradeskill->usedComponents.size() - 1)
  269. qty = recipe->GetFuelComponentQuantity();
  270. // Get the item in the players inventory and remove or reduce the quantity
  271. item = client->GetPlayer()->item_list.GetItemFromID(*itr);
  272. if (item->details.count <= qty)
  273. client->GetPlayer()->item_list.RemoveItem(item);
  274. else {
  275. item->details.count -= qty;
  276. item->save_needed = true;
  277. }
  278. }
  279. item = 0;
  280. qty = recipe->GetFuelComponentQuantity();
  281. item_id = recipe->components[5][0];
  282. if (progress >= 400 && progress < 600) {
  283. if (client->GetPlayer()->GetRecipeList()->GetRecipe(recipe->GetID())->GetHighestStage() < 1) {
  284. client->GetPlayer()->GetRecipeList()->GetRecipe(recipe->GetID())->SetHighestStage(1);
  285. database.UpdatePlayerRecipe(client->GetPlayer(), recipe->GetID(), 1);
  286. }
  287. if (recipe->products.count(1) > 0) {
  288. item_id = recipe->products[1]->product_id;
  289. qty = recipe->products[1]->product_qty;
  290. }
  291. }
  292. else if ((dur < 200 && progress >= 600) || (dur >= 200 && progress >= 600 && progress < 800)) {
  293. if (client->GetPlayer()->GetRecipeList()->GetRecipe(recipe->GetID())->GetHighestStage() < 2) {
  294. client->GetPlayer()->GetRecipeList()->GetRecipe(recipe->GetID())->SetHighestStage(2);
  295. database.UpdatePlayerRecipe(client->GetPlayer(), recipe->GetID(), 2);
  296. }
  297. if (recipe->products.count(2) > 0) {
  298. item_id = recipe->products[2]->product_id;
  299. qty = recipe->products[2]->product_qty;
  300. }
  301. }
  302. else if ((dur >= 200 && dur < 800 && progress >= 800) || (dur >= 800 && progress >= 800 && progress < 1000)) {
  303. if (client->GetPlayer()->GetRecipeList()->GetRecipe(recipe->GetID())->GetHighestStage() < 3) {
  304. client->GetPlayer()->GetRecipeList()->GetRecipe(recipe->GetID())->SetHighestStage(3);
  305. database.UpdatePlayerRecipe(client->GetPlayer(), recipe->GetID(), 3);
  306. }
  307. if (recipe->products.count(3) > 0) {
  308. item_id = recipe->products[3]->product_id;
  309. qty = recipe->products[3]->product_qty;
  310. }
  311. }
  312. else if (dur >= 800 && progress >= 1000) {
  313. if (client->GetPlayer()->GetRecipeList()->GetRecipe(recipe->GetID())->GetHighestStage() < 4) {
  314. client->GetPlayer()->GetRecipeList()->GetRecipe(recipe->GetID())->SetHighestStage(4);
  315. database.UpdatePlayerRecipe(client->GetPlayer(), recipe->GetID(), 4);
  316. }
  317. if (recipe->products.count(4) > 0) {
  318. item_id = recipe->products[4]->product_id;
  319. qty = recipe->products[4]->product_qty;
  320. }
  321. }
  322. item = new Item(master_item_list.GetItem(item_id));
  323. if (!item) {
  324. LogWrite(TRADESKILL__ERROR, 0, "Tradeskills", "Item (%u) not found.", item_id);
  325. }
  326. else {
  327. item->details.count = qty;
  328. // use CHANNEL_COLOR_CHAT_RELATIONSHIP as that is the same value (4) as it is in a log for this message
  329. client->Message(CHANNEL_COLOR_CHAT_RELATIONSHIP, "You created \\aITEM %u 0:%s\\/a.", item->details.item_id, item->name.c_str());
  330. client->AddItem(item);
  331. //Check for crafting quest updates
  332. int8 update_amt = 0;
  333. if(item->stack_count > 1)
  334. update_amt = 1;
  335. else
  336. update_amt = qty;
  337. client->GetPlayer()->CheckQuestsCraftUpdate(item, update_amt);
  338. }
  339. float xp = client->GetPlayer()->CalculateTSXP(recipe->GetLevel());
  340. if (xp > 0) {
  341. int16 level = client->GetPlayer()->GetTSLevel();
  342. if (client->GetPlayer()->AddTSXP((int32)xp)) {
  343. client->Message(CHANNEL_COLOR_EXP, "You gain %u Tradeskill XP!", (int32)xp);
  344. LogWrite(PLAYER__DEBUG, 0, "Player", "Player: %s earned %u tradeskill experience.", client->GetPlayer()->GetName(), (int32)xp);
  345. if(client->GetPlayer()->GetTSLevel() != level)
  346. client->ChangeTSLevel(level, client->GetPlayer()->GetTSLevel());
  347. client->GetPlayer()->SetCharSheetChanged(true);
  348. }
  349. }
  350. tradeskillList.erase(client);
  351. safe_delete(tradeskill);
  352. if (lock)
  353. m_tradeskills.releasewritelock(__FUNCTION__, __LINE__);
  354. // Lock TS spells and unlock all others
  355. client->GetPlayer()->LockTSSpells();
  356. }
  357. bool TradeskillMgr::IsClientCrafting(Client* client) {
  358. bool ret = false;
  359. m_tradeskills.readlock(__FUNCTION__, __LINE__);
  360. ret = tradeskillList.count(client) > 0;
  361. m_tradeskills.releasereadlock(__FUNCTION__, __LINE__);
  362. return ret;
  363. }
  364. void TradeskillMgr::CheckTradeskillEvent(Client* client, int16 icon) {
  365. // Check to see if the given client is crafting
  366. if (!IsClientCrafting(client))
  367. return;
  368. m_tradeskills.writelock(__FUNCTION__, __LINE__);
  369. // check to see if the client currently has an event and if it does if we had already tried to counter it this round
  370. if (tradeskillList[client]->CurrentEvent == 0 || tradeskillList[client]->eventChecked) {
  371. // No current event, or we already tried to counter it, return out
  372. m_tradeskills.releasewritelock(__FUNCTION__, __LINE__);
  373. return;
  374. }
  375. // set the eventChecked flag so we don't try to counter it again
  376. tradeskillList[client]->eventChecked = true;
  377. // compare the event icon with the given spell icon to see if we countered it and store the result for the update
  378. bool countered = (icon == tradeskillList[client]->CurrentEvent->Icon);
  379. tradeskillList[client]->eventCountered = countered;
  380. // send the success or fail message to the client
  381. client->Message(CHANNEL_COLOR_WHITE, "You %s %s.", countered ? "successfully countered" : "failed to counter", tradeskillList[client]->CurrentEvent->Name);
  382. // unlock the list and send the result packet
  383. m_tradeskills.releasewritelock(__FUNCTION__, __LINE__);
  384. ClientPacketFunctions::CounterReaction(client, countered);
  385. }
  386. Tradeskill* TradeskillMgr::GetTradeskill(Client* client) {
  387. if (tradeskillList.count(client) == 0)
  388. return 0;
  389. return tradeskillList[client];
  390. }
  391. MasterTradeskillEventsList::MasterTradeskillEventsList() {
  392. m_eventList.SetName("MasterTradeskillEventsList::eventList");
  393. }
  394. MasterTradeskillEventsList::~MasterTradeskillEventsList() {
  395. m_eventList.writelock(__FUNCTION__, __LINE__);
  396. map<int32, vector<TradeskillEvent*> >::iterator itr;
  397. vector<TradeskillEvent*>::iterator ts_itr;
  398. for (itr = eventList.begin(); itr != eventList.end(); itr++){
  399. for (ts_itr = itr->second.begin(); ts_itr != itr->second.end(); ts_itr++){
  400. safe_delete(*ts_itr);
  401. }
  402. }
  403. eventList.clear();
  404. m_eventList.releasewritelock(__FUNCTION__, __LINE__);
  405. }
  406. void MasterTradeskillEventsList::AddEvent(TradeskillEvent* tradeskillEvent) {
  407. m_eventList.writelock(__FUNCTION__, __LINE__);
  408. eventList[tradeskillEvent->Technique].push_back(tradeskillEvent);
  409. m_eventList.releasewritelock(__FUNCTION__, __LINE__);
  410. }
  411. vector<TradeskillEvent*>* MasterTradeskillEventsList::GetEventByTechnique(int32 technique) {
  412. if (eventList.count(technique) == 0)
  413. return 0;
  414. return &eventList[technique];
  415. }
  416. int32 MasterTradeskillEventsList::Size() {
  417. int32 count = 0;
  418. m_eventList.readlock(__FUNCTION__, __LINE__);
  419. map<int32, vector<TradeskillEvent*> >::iterator itr;
  420. for (itr = eventList.begin(); itr != eventList.end(); itr++)
  421. count += itr->second.size();
  422. m_eventList.releasereadlock(__FUNCTION__, __LINE__);
  423. return count;
  424. }