123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559 |
- /*
- EQ2Emulator: Everquest II Server Emulator
- Copyright (C) 2007 EQ2EMulator Development Team (http://www.eq2emulator.net)
- This file is part of EQ2Emulator.
- EQ2Emulator is free software: you can redistribute it and/or modify
- it under the terms of the GNU General Public License as published by
- the Free Software Foundation, either version 3 of the License, or
- (at your option) any later version.
- EQ2Emulator is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
- You should have received a copy of the GNU General Public License
- along with EQ2Emulator. If not, see <http://www.gnu.org/licenses/>.
- */
- #include "Tradeskills.h"
- #include "../client.h"
- #include "../../common/ConfigReader.h"
- #include "../classes.h"
- //#include "../../common/debug.h"
- #include "../../common/Log.h"
- //#include "../zoneserver.h"
- //#include "../Skills.h"
- //#include "../classes.h"
- #include "../World.h"
- //#include "../LuaInterface.h"
- #include "../ClientPacketFunctions.h"
- #include "../WorldDatabase.h"
- #include "../Rules/Rules.h"
- extern Classes classes;
- extern ConfigReader configReader;
- extern MasterSkillList master_skill_list;
- extern MasterRecipeList master_recipe_list;
- extern MasterTradeskillEventsList master_tradeskillevent_list;
- extern WorldDatabase database;
- extern RuleManager rule_manager;
- TradeskillMgr::TradeskillMgr() {
- m_tradeskills.SetName("TradeskillMgr::tradeskillsList");
- // % chance for each was made up by me (Jabantiz) and may need some tweaking
- // 2% for crit fail
- m_success = rule_manager.GetGlobalRule(R_World, TradeskillSuccessChance)->GetFloat();
- m_critSuccess = rule_manager.GetGlobalRule(R_World, TradeskillCritSuccessChance)->GetFloat();
- m_fail = rule_manager.GetGlobalRule(R_World, TradeskillFailChance)->GetFloat();
- m_critFail = rule_manager.GetGlobalRule(R_World, TradeskillCritFailChance)->GetFloat();
- m_eventChance = rule_manager.GetGlobalRule(R_World, TradeskillEventChance)->GetFloat();
- if ((m_success + m_critSuccess + m_fail + m_critFail) != 100.0f) {
- LogWrite(TRADESKILL__ERROR, 0, "Tradeskills", "Success, crit success, fail, and crit fail MUST add up to 100, reverting to defaults...");
- m_success = 87.0f;
- m_critSuccess = 2.0f;
- m_fail = 10.0f;
- m_critFail = 1.0f;
- }
- }
- TradeskillMgr::~TradeskillMgr() {
- m_tradeskills.writelock(__FUNCTION__, __LINE__);
- map<Client*, Tradeskill*>::iterator itr;
- for (itr = tradeskillList.begin(); itr != tradeskillList.end(); itr++)
- safe_delete(itr->second);
- tradeskillList.clear();
- m_tradeskills.releasewritelock(__FUNCTION__, __LINE__);
- }
- void TradeskillMgr::Process() {
- m_tradeskills.writelock(__FUNCTION__, __LINE__);
- map<Client*, Tradeskill*>::iterator itr = tradeskillList.begin();
- while (itr != tradeskillList.end()) {
- Tradeskill* tradeskill = 0;
- tradeskill = itr->second;
- if (!tradeskill)
- continue;
- if (Timer::GetCurrentTime2() >= tradeskill->nextUpdateTime) {
- Client* client = itr->first;
- sint32 progress = 0;
- sint32 durability = 0;
- /*
- Following was grabbed from
- http://eq2.stratics.com/content/guides/padasher_crafting_2.php
- old but the base fail/succes should still be the same
- -100 Durability / -50 Progress (Critical Failure)
- -50 Durability / 0 Progress (Failure)
- -10 Durability / +50 Progress (Standard tick)
- +10 Durability / + 100 Progress (Critical Success)
- */
- float roll = MakeRandomFloat(0, 100);
- int8 effect = 0; //1 is critical success, 2 is success, 3 is failure, and 4 is critical failure.
- float success = m_success;
- float crit_success = m_critSuccess;
- float fail = m_fail;
- float crit_fail = m_critFail;
- // Modify the % chance for success based off of stats
- client->GetPlayer()->MStats.lock();
- fail -= client->GetPlayer()->stats[ITEM_STAT_SUCCESS_MOD];
- success += client->GetPlayer()->stats[ITEM_STAT_SUCCESS_MOD];
- client->GetPlayer()->MStats.unlock();
- // add values together for the if
- crit_success += crit_fail;
- fail += crit_success;
- success += fail;
- // Crit fail
- if (roll <= crit_fail) {
- progress = -50;
- durability = -100;
- effect = 4;
- client->SimpleMessage(CHANNEL_COLOR_YELLOW, "Critical failure!");
- }
- // Crit success
- else if (roll > crit_fail && roll <= crit_success) {
- progress = 100;
- durability = 10;
- effect = 1;
- client->SimpleMessage(CHANNEL_COLOR_YELLOW, "Critical success!");
- }
- // Fail
- else if (roll > crit_success && roll <= fail) {
- progress = 0;
- durability = -50;
- effect = 3;
- }
- // Success
- else if (roll > fail && roll <= success) {
- progress = 50;
- durability = -10;
- effect = 2;
- }
- else {
- // Just a debug, should never end up in this, if we do write out a log but treat as a success for the player
- 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);
- progress = 50;
- durability = -10;
- effect = 2;
- }
- // Check to see if there was an event, if there was give out the rewards/penalties for it
- if (tradeskill->CurrentEvent) {
- if (tradeskill->eventCountered) {
- progress += tradeskill->CurrentEvent->SuccessProgress;
- durability += tradeskill->CurrentEvent->SuccessDurability;
- }
- else {
- progress += tradeskill->CurrentEvent->FailProgress;
- durability += tradeskill->CurrentEvent->FailDurability;
- }
- }
- // Modify the progress/durability by the players stats
- client->GetPlayer()->MStats.lock();
- progress += client->GetPlayer()->stats[ITEM_STAT_PROGRESS_ADD];
- durability += client->GetPlayer()->stats[ITEM_STAT_DURABILITY_ADD];
- client->GetPlayer()->MStats.unlock();
- tradeskill->currentDurability += durability;
- tradeskill->currentProgress += progress;
- PacketStruct* packet = configReader.getStruct("WS_UpdateCreateItem", client->GetVersion());
- if (packet) {
- packet->setDataByName("spawn_id", client->GetPlayer()->GetIDWithPlayerSpawn(tradeskill->table));
- packet->setDataByName("effect", effect);
- packet->setDataByName("total_durability", tradeskill->currentDurability);
- packet->setDataByName("total_progress", tradeskill->currentProgress);
- packet->setDataByName("durability_change", durability);
- packet->setDataByName("progress_change", progress);
- if (tradeskill->currentProgress >= 1000)
- packet->setDataByName("progress_level", 4);
- else if (tradeskill->currentProgress >= 800)
- packet->setDataByName("progress_level", 3);
- else if (tradeskill->currentProgress >= 600)
- packet->setDataByName("progress_level", 2);
- else if (tradeskill->currentProgress >= 400)
- packet->setDataByName("progress_level", 1);
- else
- packet->setDataByName("progress_level", 0);
- // Reset the tradeskill event
- tradeskill->CurrentEvent = 0;
- tradeskill->eventChecked = false;
- tradeskill->eventCountered = false;
- // 15% chance for an event (change this to a rule probably)
- int eventRoll = MakeRandomFloat(0, 100);
- if (eventRoll <= m_eventChance) {
- // Get a vector of all possible events for this crafting technique
- vector<TradeskillEvent*>* events = master_tradeskillevent_list.GetEventByTechnique(tradeskill->recipe->GetTechnique());
- if (events) {
- // Get the size of the vector
- int size = events->size();
- // Get a random number from 0 to size - 1 to use as an index
- int index = MakeRandomInt(0, size - 1);
- // use the index to get an event
- TradeskillEvent* TSEvent = events->at(index);
- if (TSEvent) {
- // Now that we got a random event set it in the packet
- packet->setDataByName("reaction_icon", TSEvent->Icon);
- packet->setDataByName("reaction_name", TSEvent->Name);
- // Set the current tradeskill event
- tradeskill->CurrentEvent = TSEvent;
- }
- }
- }
- client->QueuePacket(packet->serialize());
- safe_delete(packet);
- }
- if (tradeskill->currentProgress >= 1000) {
- itr++;
- StopCrafting(client, false);
- continue;
- }
- else
- tradeskill->nextUpdateTime = Timer::GetCurrentTime2() + 4000;
- }
- itr++;
- }
- m_tradeskills.releasewritelock(__FUNCTION__, __LINE__);
- }
- void TradeskillMgr::BeginCrafting(Client* client, vector<pair<int32,int16>> components) {
- Recipe* recipe = master_recipe_list.GetRecipe(client->GetPlayer()->GetCurrentRecipe());
- if (!recipe) {
- LogWrite(TRADESKILL__ERROR, 0, "Recipe", "Recipe (%u) not found in TradeskillMgr::BeginCrafting()", client->GetPlayer()->GetCurrentRecipe());
- ClientPacketFunctions::StopCrafting(client);
- return;
- }
- // TODO: use the vecotr to lock inventory slots
- vector<pair<int32, int16>>::iterator itr;
- bool missingItem = false;
- int32 itemid = 0;
- vector<Item*> tmpItems;
- for (itr = components.begin(); itr != components.end(); itr++) {
- itemid = itr->first;
- Item* item = client->GetPlayer()->item_list.GetItemFromUniqueID(itemid);
- if(!item)
- {
- missingItem = true;
- break;
- }
-
- item->details.item_locked = true;
- tmpItems.push_back(item);
- }
- if (missingItem) {
- LogWrite(TRADESKILL__ERROR, 0, "Recipe", "Recipe (%u) player missing item %u",itemid);
- vector<Item*>::iterator itemitr;
- for (itemitr = tmpItems.begin(); itemitr != tmpItems.end(); itemitr++) {
- Item* tmpItem = *itemitr;
- tmpItem->details.item_locked = false;
- }
- ClientPacketFunctions::StopCrafting(client);
- return;
- }
- ClientPacketFunctions::SendItemCreationUI(client, recipe);
- Tradeskill* tradeskill = new Tradeskill;
- tradeskill->player = client->GetPlayer();
- tradeskill->table = client->GetPlayer()->GetTarget();
- tradeskill->recipe = recipe;
- tradeskill->currentDurability = 1000;
- tradeskill->currentProgress = 0;
- tradeskill->nextUpdateTime = Timer::GetCurrentTime2() + 500;
- tradeskill->usedComponents = components;
- tradeskill->CurrentEvent = 0;
- tradeskill->eventChecked = false;
- tradeskill->eventCountered = false;
- m_tradeskills.writelock(__FUNCTION__, __LINE__);
- tradeskillList.insert(make_pair(client, tradeskill));
- m_tradeskills.releasewritelock(__FUNCTION__, __LINE__);
- // Unlock TS Spells and lock all others
- client->GetPlayer()->UnlockTSSpells();
- client->ClearSentItemDetails();
- EQ2Packet* outapp = client->GetPlayer()->SendInventoryUpdate(client->GetVersion());
- if (outapp)
- client->QueuePacket(outapp);
- }
- void TradeskillMgr::StopCrafting(Client* client, bool lock) {
-
- if (lock)
- m_tradeskills.writelock(__FUNCTION__, __LINE__);
- if (tradeskillList.count(client) == 0) {
- if (lock)
- m_tradeskills.releasewritelock(__FUNCTION__, __LINE__);
- return;
- }
- Tradeskill* tradeskill = 0;
- tradeskill = tradeskillList[client];
- //TODO: unlock inventory slots, give the product to the player, give tradeskill xp
- ClientPacketFunctions::StopCrafting(client);
- int32 dur = tradeskill->currentDurability;
- int32 progress = tradeskill->currentProgress;
- Recipe* recipe = tradeskill->recipe;
- vector<pair<int32, int16>>::iterator itr;
- Item* item = 0;
- int32 item_id = 0;
- int8 i = 0;
- int8 qty = 0;
- Recipe* playerRecipe = client->GetPlayer()->GetRecipeList()->GetRecipe(recipe->GetID());
-
- if(!playerRecipe)
- {
- 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());
- client->Message(CHANNEL_COLOR_RED, "%s: StopCrafting Error finding player recipe in their recipe book for recipe id %u!", client->GetPlayer()->GetName(), recipe->GetID());
- if (lock)
- m_tradeskills.releasewritelock(__FUNCTION__, __LINE__);
- return;
- }
- bool updateInvReq = false;
- // cycle through the list of used items and remove them
- for (itr = tradeskill->usedComponents.begin(); itr != tradeskill->usedComponents.end(); itr++, i++) {
-
-
- // Get the item in the players inventory and remove or reduce the quantity
- int32 itmid = itr->first;
- qty = itr->second;
- item = client->GetPlayer()->item_list.GetItemFromUniqueID(itmid);
- if (item && item->details.count <= qty)
- {
- item->details.item_locked = false;
- client->GetPlayer()->item_list.RemoveItem(item);
- }
- else if(item) {
- item->details.count -= qty;
- item->details.item_locked = false;
- item->save_needed = true;
- updateInvReq = true;
- }
- else
- {
- 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());
- client->Message(CHANNEL_COLOR_RED, "%s: StopCrafting Error finding item %u to remove quantity for recipe id %u!", client->GetPlayer()->GetName(), itmid, recipe->GetID());
- }
- }
- if(updateInvReq)
- {
- EQ2Packet* outapp = client->GetPlayer()->SendInventoryUpdate(client->GetVersion());
- if (outapp)
- client->QueuePacket(outapp);
- }
- item = 0;
- qty = recipe->GetFuelComponentQuantity();
- item_id = recipe->components[5][0];
- float tsx = 0;
- int8 HS = playerRecipe->GetHighestStage();
- if (progress < 400) { //stage 0
- if (recipe->products.count(0) > 0) {
- item_id = recipe->products[0]->product_id;
- qty = recipe->products[0]->product_qty;
- }
- tsx = 1;
- }
- else if (progress >= 400 && progress < 600) { //stage 1
- if (HS & (1 << (1 - 1))) {
- }
- else {
- playerRecipe->SetHighestStage(HS + 1 );
- database.UpdatePlayerRecipe(client->GetPlayer(), recipe->GetID(), playerRecipe->GetHighestStage());
- }
- if (recipe->products.count(1) > 0) {
- item_id = recipe->products[1]->product_id;
- qty = recipe->products[1]->product_qty;
- }
- tsx = .45;
- }
- else if (progress >= 600 && progress < 800) { //stage 2
- //else if ((dur < 200 && progress >= 600) || (dur >= 200 && progress >= 600 && progress < 800)) { //stage 2
- if (HS & (1 << (2 - 1))) {
- }
- else {
- playerRecipe->SetHighestStage(HS + 2);
- database.UpdatePlayerRecipe(client->GetPlayer(), recipe->GetID(), playerRecipe->GetHighestStage());
- }
- if (recipe->products.count(2) > 0) {
- item_id = recipe->products[2]->product_id;
- qty = recipe->products[2]->product_qty;
- }
- tsx = .30;
- }
- //else if ((dur >= 200 && dur < 800 && progress >= 800) || (dur >= 800 && progress >= 800 && progress < 1000)) { // stage 3
- else if (progress >= 800 && progress < 1000) { // stage 3
- if (HS & (1 << (3 - 1))) {
- }
- else {
- playerRecipe->SetHighestStage(HS + 4);
- database.UpdatePlayerRecipe(client->GetPlayer(), recipe->GetID(), playerRecipe->GetHighestStage());
- }
- if (recipe->products.count(3) > 0) {
- item_id = recipe->products[3]->product_id;
- qty = recipe->products[3]->product_qty;
- }
- tsx = .15;
- }
- //else if (dur >= 800 && progress >= 1000) { // stage 4
- else if (progress >= 1000) { // stage 4
- if (HS & (1 << (4 - 1))) {
- }
- else {
- playerRecipe->SetHighestStage(HS + 8);
- database.UpdatePlayerRecipe(client->GetPlayer(), recipe->GetID(), playerRecipe->GetHighestStage());
- }
- if (recipe->products.count(4) > 0) {
- item_id = recipe->products[4]->product_id;
- qty = recipe->products[4]->product_qty;
- }
- }
- item = new Item(master_item_list.GetItem(item_id));
- if (!item) {
- LogWrite(TRADESKILL__ERROR, 0, "Tradeskills", "Item (%u) not found.", item_id);
- }
- else {
- item->details.count = qty;
- // use CHANNEL_COLOR_CHAT_RELATIONSHIP as that is the same value (4) as it is in a log for this message
- client->Message(CHANNEL_COLOR_CHAT_RELATIONSHIP, "You created %s.", item->CreateItemLink(client->GetVersion()).c_str());
- client->AddItem(item);
- //Check for crafting quest updates
- int8 update_amt = 0;
- if(item->stack_count > 1)
- update_amt = 1;
- else
- update_amt = qty;
- client->GetPlayer()->CheckQuestsCraftUpdate(item, update_amt);
- }
- float xp = client->GetPlayer()->CalculateTSXP(recipe->GetLevel());
- xp = xp - (xp * tsx);
- if (xp > 0) {
- int16 level = client->GetPlayer()->GetTSLevel();
- if (client->GetPlayer()->AddTSXP((int32)xp)) {
- client->Message(CHANNEL_REWARD, "You gain %u Tradeskill XP!", (int32)xp);
- LogWrite(PLAYER__DEBUG, 0, "Player", "Player: %s earned %u tradeskill experience.", client->GetPlayer()->GetName(), (int32)xp);
- if(client->GetPlayer()->GetTSLevel() != level)
- client->ChangeTSLevel(level, client->GetPlayer()->GetTSLevel());
- client->GetPlayer()->SetCharSheetChanged(true);
- }
- }
- tradeskillList.erase(client);
- safe_delete(tradeskill);
- if (lock)
- m_tradeskills.releasewritelock(__FUNCTION__, __LINE__);
- // Lock TS spells and unlock all others
- client->GetPlayer()->LockTSSpells();
- }
- bool TradeskillMgr::IsClientCrafting(Client* client) {
- bool ret = false;
- m_tradeskills.readlock(__FUNCTION__, __LINE__);
- ret = tradeskillList.count(client) > 0;
- m_tradeskills.releasereadlock(__FUNCTION__, __LINE__);
- return ret;
- }
- void TradeskillMgr::CheckTradeskillEvent(Client* client, int16 icon) {
- // Check to see if the given client is crafting
- if (!IsClientCrafting(client))
- return;
- m_tradeskills.writelock(__FUNCTION__, __LINE__);
- // check to see if the client currently has an event and if it does if we had already tried to counter it this round
- if (tradeskillList[client]->CurrentEvent == 0 || tradeskillList[client]->eventChecked) {
- // No current event, or we already tried to counter it, return out
- m_tradeskills.releasewritelock(__FUNCTION__, __LINE__);
- return;
- }
- // set the eventChecked flag so we don't try to counter it again
- tradeskillList[client]->eventChecked = true;
- // compare the event icon with the given spell icon to see if we countered it and store the result for the update
- bool countered = (icon == tradeskillList[client]->CurrentEvent->Icon);
- tradeskillList[client]->eventCountered = countered;
- // send the success or fail message to the client
- client->Message(CHANNEL_NARRATIVE, "You %s %s.", countered ? "successfully countered" : "failed to counter", tradeskillList[client]->CurrentEvent->Name);
-
- // unlock the list and send the result packet
- m_tradeskills.releasewritelock(__FUNCTION__, __LINE__);
- ClientPacketFunctions::CounterReaction(client, countered);
- }
- Tradeskill* TradeskillMgr::GetTradeskill(Client* client) {
- if (tradeskillList.count(client) == 0)
- return 0;
- return tradeskillList[client];
- }
- MasterTradeskillEventsList::MasterTradeskillEventsList() {
- m_eventList.SetName("MasterTradeskillEventsList::eventList");
- }
- MasterTradeskillEventsList::~MasterTradeskillEventsList() {
- m_eventList.writelock(__FUNCTION__, __LINE__);
- map<int32, vector<TradeskillEvent*> >::iterator itr;
- vector<TradeskillEvent*>::iterator ts_itr;
- for (itr = eventList.begin(); itr != eventList.end(); itr++){
- for (ts_itr = itr->second.begin(); ts_itr != itr->second.end(); ts_itr++){
- safe_delete(*ts_itr);
- }
- }
- eventList.clear();
- m_eventList.releasewritelock(__FUNCTION__, __LINE__);
- }
- void MasterTradeskillEventsList::AddEvent(TradeskillEvent* tradeskillEvent) {
- m_eventList.writelock(__FUNCTION__, __LINE__);
- eventList[tradeskillEvent->Technique].push_back(tradeskillEvent);
- m_eventList.releasewritelock(__FUNCTION__, __LINE__);
- }
- vector<TradeskillEvent*>* MasterTradeskillEventsList::GetEventByTechnique(int32 technique) {
- if (eventList.count(technique) == 0)
- return 0;
- return &eventList[technique];
- }
- int32 MasterTradeskillEventsList::Size() {
- int32 count = 0;
- m_eventList.readlock(__FUNCTION__, __LINE__);
- map<int32, vector<TradeskillEvent*> >::iterator itr;
- for (itr = eventList.begin(); itr != eventList.end(); itr++)
- count += itr->second.size();
- m_eventList.releasereadlock(__FUNCTION__, __LINE__);
- return count;
- }
|