World.cpp 73 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316
  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 <assert.h>
  17. #include "World.h"
  18. #include "Items/Items.h"
  19. #include "Items/Items_ToV.h"
  20. #include "Items/Items_DoV.h"
  21. #include "Spells.h"
  22. #include "client.h"
  23. #include "WorldDatabase.h"
  24. #include "../common/debug.h"
  25. #include "races.h"
  26. #include "classes.h"
  27. #include "VisualStates.h"
  28. #include "Appearances.h"
  29. #include "Skills.h"
  30. #include "LoginServer.h"
  31. #include "Quests.h"
  32. #include "Factions.h"
  33. #include "Guilds/Guild.h"
  34. #include "Collections/Collections.h"
  35. #include "Achievements/Achievements.h"
  36. #include "Recipes/Recipe.h"
  37. #include "Rules/Rules.h"
  38. #include "IRC/IRC.h"
  39. #include "../common/Log.h"
  40. #include "Traits/Traits.h"
  41. #include "Chat/Chat.h"
  42. #include "Tradeskills/Tradeskills.h"
  43. #include "AltAdvancement/AltAdvancement.h"
  44. #include "LuaInterface.h"
  45. #include "HeroicOp/HeroicOp.h"
  46. #include "RaceTypes/RaceTypes.h"
  47. MasterQuestList master_quest_list;
  48. MasterItemList master_item_list;
  49. MasterSpellList master_spell_list;
  50. MasterTraitList master_trait_list;
  51. MasterHeroicOPList master_ho_list;
  52. MasterSkillList master_skill_list;
  53. MasterFactionList master_faction_list;
  54. MasterCollectionList master_collection_list;
  55. MasterAchievementList master_achievement_list;
  56. MasterRecipeList master_recipe_list;
  57. MasterRecipeBookList master_recipebook_list;
  58. MasterTradeskillEventsList master_tradeskillevent_list;
  59. MasterAAList master_aa_list;
  60. MasterRaceTypeList race_types_list;
  61. MasterAANodeList master_tree_nodes;
  62. ClientList client_list;
  63. ZoneList zone_list;
  64. ZoneAuth zone_auth;
  65. int32 Spawn::next_id = 1;
  66. int32 WorldDatabase::next_id = 0;
  67. Commands commands;
  68. Variables variables;
  69. VisualStates visual_states;
  70. Appearances master_appearance_list;
  71. Classes classes;
  72. Races races;
  73. map<int16,OpcodeManager*>EQOpcodeManager;
  74. map<int16, int16> EQOpcodeVersions;
  75. WorldDatabase database;
  76. GuildList guild_list;
  77. IRC irc;
  78. Chat chat;
  79. extern ConfigReader configReader;
  80. extern LoginServer loginserver;
  81. extern World world;
  82. extern RuleManager rule_manager;
  83. World::World() : save_time_timer(300000), time_tick_timer(3000), vitality_timer(3600000), player_stats_timer(60000), server_stats_timer(60000), /*remove_grouped_player(30000),*/ guilds_timer(60000), lotto_players_timer(500), watchdog_timer(10000) {
  84. save_time_timer.Start();
  85. time_tick_timer.Start();
  86. vitality_timer.Start();
  87. player_stats_timer.Start();
  88. server_stats_timer.Start();
  89. //remove_grouped_player.Start();
  90. guilds_timer.Start();
  91. lotto_players_timer.Start();
  92. watchdog_timer.Start();
  93. xp_rate = -1;
  94. ts_xp_rate = -1;
  95. vitality_frequency = 0xFFFFFFFF;
  96. vitality_amount = -1;
  97. last_checked_time = 0;
  98. items_loaded = false;
  99. spells_loaded = false;
  100. achievments_loaded = false;
  101. merchant_inventory_items.clear();
  102. MHouseZones.SetName("World::m_houseZones");
  103. MPlayerHouses.SetName("World::m_playerHouses");
  104. }
  105. World::~World(){
  106. // At this point the log system is already shut down so no calls to LogWrite are allowed in any of the functions called by this deconstructor
  107. DeleteSpawns();
  108. if(database.GetStatus() == database.Connected)
  109. WriteServerStatistics();
  110. RemoveServerStatistics();
  111. DeleteMerchantsInfo();
  112. MutexMap<int32, LottoPlayer*>::iterator itr = lotto_players.begin();
  113. while (itr.Next())
  114. safe_delete(itr->second);
  115. map<int32, HouseZone*>::iterator itr2;
  116. for (itr2 = m_houseZones.begin(); itr2 != m_houseZones.end(); itr2++)
  117. safe_delete(itr2->second);
  118. m_houseZones.clear();
  119. tov_itemstat_conversion.clear();
  120. PurgeStartingLists();
  121. }
  122. void World::init(){
  123. WorldDatabase::next_id = database.GetMaxHotBarID();
  124. LogWrite(COMMAND__DEBUG, 1, "Command", "-Loading Commands...");
  125. database.LoadCommandList();
  126. LogWrite(COMMAND__DEBUG, 1, "Command", "-Load Commands complete!");
  127. LogWrite(FACTION__DEBUG, 1, "Faction", "-Loading Factions...");
  128. database.LoadFactionList();
  129. LogWrite(FACTION__DEBUG, 1, "Faction", "-Load Factions complete!...");
  130. LogWrite(SKILL__DEBUG, 1, "Skill", "-Loading Skills...");
  131. database.LoadSkills();
  132. LogWrite(SKILL__DEBUG, 1, "Skill", "-Load Skills complete...");
  133. LogWrite(WORLD__DEBUG, 1, "World", "-Loading `variables`...");
  134. database.LoadGlobalVariables();
  135. LogWrite(WORLD__DEBUG, 1, "World", "-Load `variables` complete!");
  136. LogWrite(WORLD__DEBUG, 1, "World", "-Loading `appearances`...");
  137. database.LoadAppearanceMasterList();
  138. LogWrite(WORLD__DEBUG, 1, "World", "-Load `appearances` complete!");
  139. LogWrite(WORLD__DEBUG, 1, "World", "-Loading `visual_states`...");
  140. database.LoadVisualStates();
  141. LogWrite(WORLD__DEBUG, 1, "World", "-Load `visual states` complete!");
  142. LoadStartingLists();
  143. LogWrite(WORLD__DEBUG, 1, "World", "-Setting system parameters...");
  144. Variable* var = variables.FindVariable("gametime");
  145. const char* time_string = 0;
  146. char default_time[] = "0/0/3800 8:30";
  147. if(var)
  148. time_string = var->GetValue();
  149. if(!time_string)
  150. time_string = default_time;
  151. int year, month, day, hour, minute;
  152. sscanf (time_string, "%d/%d/%d %d:%d", &month, &day, &year, &hour, &minute);
  153. LogWrite(WORLD__DEBUG, 3, "World", "--Setting World Time to %s...", time_string);
  154. world_time.month = month;
  155. world_time.day = day;
  156. world_time.year = year;
  157. world_time.hour = hour;
  158. world_time.minute = minute;
  159. LogWrite(WORLD__DEBUG, 3, "World", "--Loading Vitality Information...");
  160. LoadVitalityInformation();
  161. LogWrite(WORLD__DEBUG, 3, "World", "--Loading Server Statistics...");
  162. database.LoadServerStatistics();
  163. LogWrite(WORLD__DEBUG, 3, "World", "--Setting Server Start Time...");
  164. UpdateServerStatistic(STAT_SERVER_START_TIME, Timer::GetUnixTimeStamp(), true);
  165. LogWrite(WORLD__DEBUG, 3, "World", "--Resetting Accepted Connections to 0...");
  166. UpdateServerStatistic(STAT_SERVER_ACCEPTED_CONNECTION, 0, true);
  167. LogWrite(WORLD__DEBUG, 3, "World", "--Resetting Active Zones to 0...");
  168. UpdateServerStatistic(STAT_SERVER_NUM_ACTIVE_ZONES, 0, true);
  169. // Clear all online players at server startup
  170. LogWrite(WORLD__DEBUG, 3, "World", "--Resetting characters online flags...");
  171. database.ToggleCharacterOnline();
  172. LogWrite(WORLD__DEBUG, 1, "World", "-Set system parameters complete!");
  173. LogWrite(RULESYS__DEBUG, 1, "Rules", "-Loading Rule Sets...");
  174. database.LoadRuleSets();
  175. LogWrite(RULESYS__DEBUG, 1, "Rules", "-Load Rule Sets complete!");
  176. LogWrite(CHAT__DEBUG, 1, "IRC", "-Starting IRC thread...");
  177. LoadItemBlueStats();
  178. //PopulateTOVStatMap();
  179. group_buff_updates.Start(rule_manager.GetGlobalRule(R_Client, GroupSpellsTimer)->GetInt32());
  180. irc.Start();
  181. }
  182. PacketStruct* World::GetWorldTime(int16 version){
  183. PacketStruct* packet = configReader.getStruct("WS_GameWorldTime", version);
  184. if(packet){
  185. packet->setDataByName("year", world_time.year);
  186. packet->setDataByName("month", world_time.month);
  187. packet->setDataByName("day", world_time.day);
  188. packet->setDataByName("hour", world_time.hour);
  189. packet->setDataByName("minute", world_time.minute);
  190. packet->setDataByName("unknown", 250);
  191. packet->setDataByName("unix_time", Timer::GetUnixTimeStamp());
  192. packet->setDataByName("unknown2", 1);
  193. }
  194. return packet;
  195. }
  196. float World::GetXPRate(){
  197. if(xp_rate >= 0)
  198. return xp_rate;
  199. xp_rate = rule_manager.GetGlobalRule(R_Player, XPMultiplier)->GetFloat();
  200. LogWrite(WORLD__DEBUG, 0, "World", "Setting Global XP Rate to: %.2f", xp_rate);
  201. return xp_rate;
  202. }
  203. float World::GetTSXPRate()
  204. {
  205. if(ts_xp_rate >= 0)
  206. return ts_xp_rate;
  207. ts_xp_rate = rule_manager.GetGlobalRule(R_Player, TSXPMultiplier)->GetFloat();
  208. LogWrite(WORLD__DEBUG, 0, "World", "Setting Global Tradeskill XP Rate to: %.2f", ts_xp_rate);
  209. return ts_xp_rate;
  210. }
  211. void World::Process(){
  212. if(last_checked_time > Timer::GetCurrentTime2())
  213. return;
  214. last_checked_time = Timer::GetCurrentTime2() + 1000;
  215. if(save_time_timer.Check())
  216. database.SaveWorldTime(&world_time);
  217. if(time_tick_timer.Check())
  218. WorldTimeTick();
  219. if(vitality_timer.Check())
  220. UpdateVitality();
  221. if (player_stats_timer.Check())
  222. WritePlayerStatistics();
  223. if (server_stats_timer.Check())
  224. WriteServerStatistics();
  225. /*if(remove_grouped_player.Check())
  226. CheckRemoveGroupedPlayer();*/
  227. if (group_buff_updates.Check())
  228. GetGroupManager()->UpdateGroupBuffs();
  229. if (guilds_timer.Check())
  230. SaveGuilds();
  231. if (lotto_players_timer.Check())
  232. CheckLottoPlayers();
  233. if(watchdog_timer.Check())
  234. zone_list.WatchdogHeartbeat();
  235. }
  236. vector<Variable*>* World::GetClientVariables(){
  237. return variables.GetVariables("cl_");
  238. }
  239. void World::LoadVitalityInformation()
  240. {
  241. int32 timestamp = Timer::GetUnixTimeStamp();
  242. int32 diff = 0;
  243. // fetch vitalitytimer value from `variables` table
  244. Variable* timer_var = variables.FindVariable("vitalitytimer");
  245. if(timer_var)
  246. {
  247. try
  248. {
  249. diff = timestamp - atoul(timer_var->GetValue());
  250. diff *= 1000; //convert seconds to milliseconds
  251. }
  252. catch(...)
  253. {
  254. LogWrite(WORLD__ERROR, 0, "World", "Error parsing vitalitytimer, value: %s", timer_var->GetValue());
  255. }
  256. }
  257. // Now using Rules System to set vitality parameters
  258. vitality_amount = rule_manager.GetGlobalRule(R_Player, VitalityAmount)->GetFloat();
  259. vitality_frequency = rule_manager.GetGlobalRule(R_Player, VitalityFrequency)->GetInt32();
  260. vitality_frequency *= 1000; //convert seconds to milliseconds
  261. if(diff >= vitality_frequency)
  262. UpdateVitality(); //update now
  263. else
  264. vitality_timer.SetTimer(vitality_frequency - diff);
  265. }
  266. void World::UpdateVitality()
  267. {
  268. // push new vitalitytimer to `variables` table
  269. database.UpdateVitality(Timer::GetUnixTimeStamp(), vitality_amount);
  270. if(vitality_timer.GetDuration() != vitality_frequency)
  271. vitality_timer.SetTimer(vitality_frequency);
  272. zone_list.UpdateVitality(vitality_amount);
  273. }
  274. void ZoneList::UpdateVitality(float amount)
  275. {
  276. list<ZoneServer*>::iterator zone_iter;
  277. ZoneServer* tmp = 0;
  278. MZoneList.readlock(__FUNCTION__, __LINE__);
  279. for(zone_iter=zlist.begin(); zone_iter!=zlist.end(); zone_iter++)
  280. {
  281. tmp = *zone_iter;
  282. if(tmp)
  283. tmp->UpdateVitality(amount);
  284. }
  285. MZoneList.releasereadlock(__FUNCTION__, __LINE__);
  286. }
  287. void World::WorldTimeTick(){
  288. world_time.minute++;
  289. //I know it looks complicated, but the nested ifs are to avoid checking all of them every 3 seconds
  290. if(world_time.minute >= 60){ // >= in case of bad time from db
  291. world_time.minute = 0;
  292. world_time.hour++;
  293. if(world_time.hour >= 24){
  294. world_time.hour = 0;
  295. world_time.day++;
  296. if(world_time.day>=30){
  297. world_time.day = 0;
  298. world_time.month++;
  299. if(world_time.month >= 12){
  300. world_time.month = 0;
  301. world_time.year++;
  302. }
  303. }
  304. }
  305. }
  306. }
  307. ZoneList::ZoneList() {
  308. MZoneList.SetName("ZoneList::MZoneList");
  309. }
  310. ZoneList::~ZoneList() {
  311. list<ZoneServer*>::iterator zone_iter;
  312. ZoneServer* zs = 0;
  313. for(zone_iter=zlist.begin(); zone_iter!=zlist.end();){
  314. zs = *zone_iter;
  315. zone_iter = zlist.erase(zone_iter);
  316. safe_delete(zs);
  317. }
  318. }
  319. void ZoneList::CheckFriendList(Client* client) {
  320. LogWrite(WORLD__DEBUG, 0, "World", "Sending FriendList...");
  321. MClientList.lock();
  322. map<string,Client*>::iterator itr;
  323. for(itr = client_map.begin(); itr != client_map.end(); itr++){
  324. if(itr->second != client && itr->second){
  325. if(itr->second->GetPlayer()->IsFriend(client->GetPlayer()->GetName())){
  326. itr->second->SendFriendList();
  327. itr->second->Message(CHANNEL_COLOR_CHAT_RELATIONSHIP, "Friend: %s has logged in.", client->GetPlayer()->GetName());
  328. }
  329. }
  330. }
  331. MClientList.unlock();
  332. }
  333. void ZoneList::CheckFriendZoned(Client* client){
  334. MClientList.lock();
  335. map<string,Client*>::iterator itr;
  336. for(itr = client_map.begin(); itr != client_map.end(); itr++){
  337. if(itr->second != client && itr->second){
  338. if(itr->second->GetPlayer()->IsFriend(client->GetPlayer()->GetName())){
  339. itr->second->SendFriendList();
  340. }
  341. }
  342. }
  343. MClientList.unlock();
  344. }
  345. bool ZoneList::HandleGlobalChatMessage(Client* from, char* to, int16 channel, const char* message, const char* channel_name){
  346. if (!from) {
  347. LogWrite(WORLD__ERROR, 0, "World", "HandleGlobalChatMessage() called with an invalid client");
  348. return false;
  349. }
  350. if(channel == CHANNEL_PRIVATE_TELL){
  351. Client* find_client = zone_list.GetClientByCharName(to);
  352. if(!find_client || find_client->GetPlayer()->IsIgnored(from->GetPlayer()->GetName()))
  353. return false;
  354. else if(find_client == from)
  355. {
  356. from->Message(CHANNEL_COLOR_RED,"You must be very lonely...(ERROR: Cannot send tell to self)");
  357. }
  358. else
  359. {
  360. find_client->HandleTellMessage(from, message);
  361. from->HandleTellMessage(from, message);
  362. if (find_client->GetPlayer()->get_character_flag(CF_AFK)) {
  363. find_client->HandleTellMessage(find_client, find_client->GetPlayer()->GetAwayMessage().c_str());
  364. from->HandleTellMessage(find_client, find_client->GetPlayer()->GetAwayMessage().c_str());
  365. }
  366. }
  367. }
  368. else if(channel == CHANNEL_GROUP_SAY) {
  369. GroupMemberInfo* gmi = from->GetPlayer()->GetGroupMemberInfo();
  370. if(gmi)
  371. world.GetGroupManager()->GroupMessage(gmi->group_id, message);
  372. }
  373. else{
  374. list<ZoneServer*>::iterator zone_iter;
  375. ZoneServer* zs = 0;
  376. MZoneList.readlock(__FUNCTION__, __LINE__);
  377. for(zone_iter=zlist.begin(); zone_iter!=zlist.end();zone_iter++){
  378. zs = *zone_iter;
  379. if(zs)
  380. zs->HandleChatMessage(from->GetPlayer(), to, channel, message, 0, channel_name);
  381. }
  382. MZoneList.releasereadlock(__FUNCTION__, __LINE__);
  383. }
  384. return true;
  385. }
  386. void ZoneList::LoadSpellProcess(){
  387. list<ZoneServer*>::iterator zone_iter;
  388. ZoneServer* zone = 0;
  389. MZoneList.readlock(__FUNCTION__, __LINE__);
  390. for (zone_iter=zlist.begin(); zone_iter!=zlist.end();zone_iter++){
  391. zone = *zone_iter;
  392. if (zone)
  393. zone->LoadSpellProcess();
  394. }
  395. MZoneList.releasereadlock(__FUNCTION__, __LINE__);
  396. }
  397. void ZoneList::DeleteSpellProcess(){
  398. list<ZoneServer*>::iterator zone_iter;
  399. ZoneServer* zone = 0;
  400. MZoneList.readlock(__FUNCTION__, __LINE__);
  401. for (zone_iter=zlist.begin(); zone_iter!=zlist.end();zone_iter++){
  402. zone = *zone_iter;
  403. if (zone)
  404. zone->DeleteSpellProcess();
  405. }
  406. MZoneList.releasereadlock(__FUNCTION__, __LINE__);
  407. }
  408. void ZoneList::HandleGlobalBroadcast(const char* message) {
  409. list<ZoneServer*>::iterator zone_iter;
  410. ZoneServer* zone = 0;
  411. MZoneList.readlock(__FUNCTION__, __LINE__);
  412. for (zone_iter=zlist.begin(); zone_iter!=zlist.end();zone_iter++){
  413. zone = *zone_iter;
  414. if (zone)
  415. zone->HandleBroadcast(message);
  416. }
  417. MZoneList.releasereadlock(__FUNCTION__, __LINE__);
  418. }
  419. void ZoneList::HandleGlobalAnnouncement(const char* message) {
  420. list<ZoneServer*>::iterator zone_iter;
  421. ZoneServer* zone = 0;
  422. MZoneList.readlock(__FUNCTION__, __LINE__);
  423. for (zone_iter=zlist.begin(); zone_iter!=zlist.end();zone_iter++){
  424. zone = *zone_iter;
  425. if (zone)
  426. zone->HandleAnnouncement(message);
  427. }
  428. MZoneList.releasereadlock(__FUNCTION__, __LINE__);
  429. }
  430. int32 ZoneList::Count(){
  431. return zlist.size();
  432. }
  433. void ZoneList::Add(ZoneServer* zone) {
  434. MZoneList.writelock(__FUNCTION__, __LINE__);
  435. zlist.push_back(zone);
  436. MZoneList.releasewritelock(__FUNCTION__, __LINE__);
  437. }
  438. void ZoneList::Remove(ZoneServer* zone) {
  439. MZoneList.writelock(__FUNCTION__, __LINE__);
  440. zlist.remove(zone);
  441. MZoneList.releasewritelock(__FUNCTION__, __LINE__);
  442. }
  443. ZoneServer* ZoneList::Get(const char* zone_name, bool loadZone) {
  444. list<ZoneServer*>::iterator zone_iter;
  445. ZoneServer* tmp = 0;
  446. ZoneServer* ret = 0;
  447. MZoneList.readlock(__FUNCTION__, __LINE__);
  448. for(zone_iter=zlist.begin(); zone_iter!=zlist.end(); zone_iter++){
  449. tmp = *zone_iter;
  450. if (!tmp->isZoneShuttingDown() && !tmp->IsInstanceZone() && strlen(zone_name) == strlen(tmp->GetZoneName()) &&
  451. strncasecmp(tmp->GetZoneName(), zone_name, strlen(zone_name))==0){
  452. if(tmp->NumPlayers() < 30 || tmp->IsCityZone())
  453. ret = tmp;
  454. break;
  455. }
  456. }
  457. MZoneList.releasereadlock(__FUNCTION__, __LINE__);
  458. if(!ret )
  459. {
  460. if ( loadZone )
  461. {
  462. ret = new ZoneServer(zone_name);
  463. database.LoadZoneInfo(ret);
  464. ret->Init();
  465. }
  466. }
  467. return ret;
  468. }
  469. ZoneServer* ZoneList::Get(int32 id, bool loadZone) {
  470. list<ZoneServer*>::iterator zone_iter;
  471. ZoneServer* tmp = 0;
  472. ZoneServer* ret = 0;
  473. MZoneList.readlock(__FUNCTION__, __LINE__);
  474. for(zone_iter=zlist.begin(); zone_iter!=zlist.end(); zone_iter++){
  475. tmp = *zone_iter;
  476. if(!tmp->isZoneShuttingDown() && !tmp->IsInstanceZone() && tmp->GetZoneID() == id){
  477. if(tmp->NumPlayers() < 30 || tmp->IsCityZone())
  478. ret = tmp;
  479. break;
  480. }
  481. }
  482. MZoneList.releasereadlock(__FUNCTION__, __LINE__);
  483. if(ret)
  484. tmp = ret;
  485. else if (loadZone) {
  486. string zonename = database.GetZoneName(id);
  487. if(zonename.length() >0){
  488. tmp = new ZoneServer(zonename.c_str());
  489. database.LoadZoneInfo(tmp);
  490. tmp->Init();
  491. }
  492. }
  493. return tmp;
  494. }
  495. void ZoneList::SendZoneList(Client* client) {
  496. list<ZoneServer*>::iterator zone_iter;
  497. ZoneServer* tmp = 0;
  498. MZoneList.readlock(__FUNCTION__, __LINE__);
  499. int zonesListed = 0;
  500. for(zone_iter=zlist.begin(); zone_iter!=zlist.end(); zone_iter++){
  501. tmp = *zone_iter;
  502. if ( zonesListed > 20 )
  503. {
  504. client->Message(CHANNEL_COLOR_YELLOW,"Reached max zone list of 20.");
  505. break;
  506. }
  507. zonesListed++;
  508. client->Message(CHANNEL_COLOR_YELLOW,"Zone(ID): %s(%i), Instance ID: %i, Description: %s.",tmp->GetZoneName(),tmp->GetZoneID(),
  509. tmp->GetInstanceID(),tmp->GetZoneDescription());
  510. }
  511. MZoneList.releasereadlock(__FUNCTION__, __LINE__);
  512. }
  513. ZoneServer* ZoneList::GetByInstanceID(int32 id, int32 zone_id) {
  514. list<ZoneServer*>::iterator zone_iter;
  515. ZoneServer* tmp = 0;
  516. ZoneServer* ret = 0;
  517. MZoneList.readlock(__FUNCTION__, __LINE__);
  518. if ( id > 0 )
  519. {
  520. for(zone_iter=zlist.begin(); zone_iter!=zlist.end(); zone_iter++){
  521. tmp = *zone_iter;
  522. if(tmp->GetInstanceID() == id){
  523. ret = tmp;
  524. break;
  525. }
  526. }
  527. }
  528. MZoneList.releasereadlock(__FUNCTION__, __LINE__);
  529. if(ret)
  530. tmp = ret;
  531. else if ( zone_id > 0 ){
  532. string zonename = database.GetZoneName(zone_id);
  533. if(zonename.length() > 0){
  534. tmp = new ZoneServer(zonename.c_str());
  535. // the player is trying to preload an already existing instance but it isn't loaded
  536. if ( id > 0 )
  537. tmp->SetupInstance(id);
  538. database.LoadZoneInfo(tmp);
  539. tmp->Init();
  540. }
  541. }
  542. return tmp;
  543. }
  544. ZoneServer* ZoneList::GetByLowestPopulation(int32 zone_id) {
  545. ZoneServer* ret = 0;
  546. ZoneServer* zone = 0;
  547. int32 clients = 0;
  548. list<ZoneServer*>::iterator itr;
  549. MZoneList.readlock(__FUNCTION__, __LINE__);
  550. if (zone_id) {
  551. for (itr = zlist.begin(); itr != zlist.end(); itr++) {
  552. zone = *itr;
  553. if (zone) {
  554. // check the zone id's
  555. if (zone->GetZoneID() == zone_id) {
  556. // check this zones client count
  557. if (clients == 0 || zone->GetClientCount() < clients) {
  558. ret = zone;
  559. clients = zone->GetClientCount();
  560. }
  561. }
  562. }
  563. }
  564. }
  565. MZoneList.releasereadlock(__FUNCTION__, __LINE__);
  566. return ret;
  567. }
  568. bool ZoneList::ClientConnected(int32 account_id){
  569. bool ret = false;
  570. map<string, Client*>::iterator itr;
  571. MClientList.lock();
  572. for(itr=client_map.begin(); itr != client_map.end(); itr++){
  573. if(itr->second && itr->second->GetAccountID() == account_id && (itr->second->GetPlayer()->GetActivityStatus() & ACTIVITY_STATUS_LINKDEAD) == 0){
  574. ret = true;
  575. break;
  576. }
  577. else if(!itr->second){
  578. client_map.erase(itr);
  579. if(client_map.size() > 0){
  580. itr=client_map.begin();
  581. if(itr == client_map.end()){
  582. if(itr->second && itr->second->GetAccountID() == account_id && (itr->second->GetPlayer()->GetActivityStatus() & ACTIVITY_STATUS_LINKDEAD) == 0)
  583. ret = true;
  584. break;
  585. }
  586. }
  587. else
  588. break;
  589. }
  590. }
  591. MClientList.unlock();
  592. return ret;
  593. }
  594. void ZoneList::ReloadClientQuests(){
  595. list<ZoneServer*>::iterator zone_iter;
  596. ZoneServer* tmp = 0;
  597. MZoneList.readlock(__FUNCTION__, __LINE__);
  598. for(zone_iter=zlist.begin(); zone_iter!=zlist.end(); zone_iter++){
  599. tmp = *zone_iter;
  600. if(tmp)
  601. tmp->ReloadClientQuests();
  602. }
  603. MZoneList.releasereadlock(__FUNCTION__, __LINE__);
  604. }
  605. void ZoneList::ProcessWhoQuery(vector<string>* queries, ZoneServer* zone, vector<Entity*>* players, bool isGM){
  606. Entity* player = 0;
  607. bool add_player = true;
  608. bool found_match = false;
  609. int8 lower = 0;
  610. int8 upper = 0;
  611. vector<Entity*> tmpPlayers;
  612. vector<Entity*>::iterator spawn_iter;
  613. if(!zone->isZoneShuttingDown()){
  614. tmpPlayers = zone->GetPlayers();
  615. for(spawn_iter = tmpPlayers.begin(); spawn_iter!=tmpPlayers.end(); spawn_iter++){
  616. player = *spawn_iter;
  617. add_player = true;
  618. Client* find_client = zone_list.GetClientByCharName(player->GetName());
  619. if (find_client == NULL) continue;
  620. int flags = find_client->GetPlayer()->GetInfoStruct()->flags;
  621. int flags2 = find_client->GetPlayer()->GetInfoStruct()->flags2;
  622. for(int32 i=0;add_player && queries && i<queries->size();i++){
  623. found_match = false;
  624. if(queries->at(i) == "ALL")
  625. continue;
  626. if(queries->at(i).length() > 3 && classes.GetClassID(queries->at(i).c_str()) > 0){
  627. if(player->GetAdventureClass() != classes.GetClassID(queries->at(i).c_str()))
  628. add_player = false;
  629. found_match = true;
  630. }
  631. else if(queries->at(i).length() > 2 && races.GetRaceID(queries->at(i).c_str()) > 0){
  632. if(player->GetRace() != races.GetRaceID(queries->at(i).c_str()))
  633. add_player = false;
  634. found_match = true;
  635. }
  636. if(!found_match && queries->at(i) == "GOOD"){
  637. if(player->GetDeity() != 1)
  638. add_player = false;
  639. found_match = true;
  640. }
  641. else if(!found_match && queries->at(i) == "EVIL"){
  642. if(player->GetDeity() == 1)
  643. add_player = false;
  644. found_match = true;
  645. }
  646. if((queries->at(i) == "GUIDE") && (find_client->GetAdminStatus() > 0) && ((find_client->GetAdminStatus() >> 4) < 5))
  647. found_match = true;
  648. else if((queries->at(i) == "GM") && ((find_client->GetAdminStatus() >> 4) > 4))
  649. found_match = true;
  650. else if((queries->at(i) == "LFG") && (flags & (1 << CF_LFG)))
  651. found_match = true;
  652. else if((queries->at(i) == "LFW") && (flags & (1 << CF_LFW)))
  653. found_match = true;
  654. else if((queries->at(i) == "ROLEPLAYING") && (flags & (1 << CF_ROLEPLAYING)))
  655. found_match = true;
  656. else if(strspn(queries->at(i).c_str(),"0123456789") == queries->at(i).length()){
  657. try{
  658. if(lower == 0)
  659. lower = atoi(queries->at(i).c_str());
  660. else
  661. upper = atoi(queries->at(i).c_str());
  662. }
  663. catch(...){}
  664. found_match = true;
  665. }
  666. if(!found_match){
  667. string name = string(player->GetName());
  668. name = ToUpper(name);
  669. if(name.find(queries->at(i)) == name.npos)
  670. add_player = false;
  671. }
  672. }
  673. if(lower > 0 && upper > 0){
  674. if(player->GetLevel() < lower || player->GetLevel() > upper)
  675. add_player = false;
  676. }
  677. else if(lower > 0){
  678. if(player->GetLevel() != lower)
  679. add_player = false;
  680. }
  681. if((flags2 & (1 << (CF_GM_HIDDEN - 32))) && !isGM) {
  682. add_player = false;
  683. found_match = true;
  684. }
  685. if(add_player)
  686. players->push_back(player);
  687. }
  688. }
  689. }
  690. void ZoneList::ProcessWhoQuery(const char* query, Client* client){
  691. list<ZoneServer*>::iterator zone_iter;
  692. vector<Entity*> players;
  693. vector<Entity*>::iterator spawn_iter;
  694. Entity* player = 0;
  695. //for now display all clients
  696. bool all = false;
  697. vector<string>* queries = 0;
  698. bool isGM = ((client->GetAdminStatus() >> 4) > 4);
  699. if(query){
  700. string query_string = string(query);
  701. query_string = ToUpper(query_string);
  702. queries = SplitString(query_string, ' ');
  703. }
  704. if(queries && queries->size() > 0 && queries->at(0) == "ALL")
  705. all = true;
  706. if(all){
  707. MZoneList.readlock(__FUNCTION__, __LINE__);
  708. for(zone_iter=zlist.begin(); zone_iter!=zlist.end(); zone_iter++){
  709. ZoneServer* tmp = *zone_iter;
  710. ProcessWhoQuery(queries, tmp, &players, isGM);
  711. }
  712. MZoneList.releasereadlock(__FUNCTION__, __LINE__);
  713. }
  714. else{
  715. ProcessWhoQuery(queries, client->GetCurrentZone(), &players, isGM);
  716. }
  717. PacketStruct* packet = configReader.getStruct("WS_WhoQueryReply", client->GetVersion());
  718. if(packet){
  719. packet->setDataByName("account_id", client->GetAccountID());
  720. packet->setDataByName("unknown", 0xFFFFFFFF);
  721. int8 num_characters = players.size();
  722. int8 max_who_results = 10;
  723. int8 max_who_results_status_override = 100;
  724. Variable* var = variables.FindVariable("max_who_results_status_override");
  725. if ( var ){
  726. max_who_results_status_override = atoi(var->GetValue());
  727. }
  728. // AdnaeDMorte
  729. if ( client->GetAdminStatus() >= max_who_results_status_override ){
  730. client->Message(CHANNEL_COLOR_RED, "** ADMIN-MODE ** ");
  731. }
  732. Variable* var1 = variables.FindVariable("max_who_results");
  733. if ( var1 ){
  734. max_who_results = atoi(var1->GetValue());
  735. }
  736. if(num_characters > max_who_results && client->GetAdminStatus() < max_who_results_status_override){
  737. num_characters = max_who_results;
  738. packet->setDataByName("response", 3); //response 1 = error message, 3 == capped
  739. }
  740. else
  741. packet->setDataByName("response", 2);
  742. packet->setArrayLengthByName("num_characters", num_characters);
  743. packet->setDataByName("unknown10", 1);
  744. int i=0;
  745. for(spawn_iter = players.begin(); spawn_iter!=players.end(); spawn_iter++, i++){
  746. if(i == num_characters)
  747. break;
  748. player = *spawn_iter;
  749. Client* find_client = zone_list.GetClientByCharName(player->GetName());
  750. int flags = find_client->GetPlayer()->GetInfoStruct()->flags;
  751. int flags2 = find_client->GetPlayer()->GetInfoStruct()->flags2;
  752. packet->setArrayDataByName("char_name", player->GetName(), i);
  753. packet->setArrayDataByName("level", player->GetLevel(), i);
  754. packet->setArrayDataByName("admin_level", ((flags2 & (1 << (CF_HIDE_STATUS - 32))) && !isGM)?0:(find_client->GetAdminStatus() >> 4), i);
  755. packet->setArrayDataByName("class", player->GetAdventureClass(), i);
  756. packet->setArrayDataByName("unknown4", 0xFF, i); //probably tradeskill class
  757. packet->setArrayDataByName("flags", (((flags >> CF_ANONYMOUS) & 1) << 0 ) |
  758. (((flags >> CF_LFG) & 1) << 1 ) |
  759. (((flags >> CF_ANONYMOUS) & 1) << 2 ) |
  760. /*(((flags >> CF_HIDDEN) & 1) << 3 ) |*/
  761. (((flags >> CF_ROLEPLAYING) & 1) << 4 ) |
  762. (((flags >> CF_AFK) & 1) << 5 ) |
  763. (((flags >> CF_LFW) & 1) << 6 ) /*|
  764. (((flags >> CF_NOTA) & 1) << 7 )*/, i);
  765. packet->setArrayDataByName("race", player->GetRace(), i);
  766. if(player->GetZone() && player->GetZone()->GetZoneDescription())
  767. packet->setArrayDataByName("zone", player->GetZone()->GetZoneDescription(), i);
  768. if(player->appearance.sub_title) {
  769. int32 sub_title_length = strlen(player->appearance.sub_title);
  770. char tmp_title[255];
  771. int32 index = 0;
  772. int32 index_tmp = 0;
  773. while (index < sub_title_length) {
  774. if (player->appearance.sub_title[index] != '<' && player->appearance.sub_title[index] != '>') {
  775. memcpy(tmp_title + index_tmp, player->appearance.sub_title + index, 1);
  776. index_tmp++;
  777. }
  778. index++;
  779. }
  780. tmp_title[index_tmp] = '\0';
  781. packet->setArrayDataByName("guild", tmp_title, i);
  782. }
  783. }
  784. client->QueuePacket(packet->serialize());
  785. safe_delete(packet);
  786. }
  787. }
  788. bool ZoneList::DepopFinished(){
  789. list<ZoneServer*>::iterator zone_iter;
  790. MZoneList.readlock(__FUNCTION__, __LINE__);
  791. bool finished_depop = true;
  792. for(zone_iter=zlist.begin(); zone_iter!=zlist.end(); zone_iter++){
  793. if(!(*zone_iter)->FinishedDepop())
  794. finished_depop = false;
  795. }
  796. MZoneList.releasereadlock(__FUNCTION__, __LINE__);
  797. return finished_depop;
  798. }
  799. void ZoneList::Depop(){
  800. list<ZoneServer*>::iterator zone_iter;
  801. MZoneList.readlock(__FUNCTION__, __LINE__);
  802. for(zone_iter=zlist.begin(); zone_iter!=zlist.end(); zone_iter++){
  803. (*zone_iter)->Depop();
  804. }
  805. MZoneList.releasereadlock(__FUNCTION__, __LINE__);
  806. }
  807. void ZoneList::Repop(){
  808. list<ZoneServer*>::iterator zone_iter;
  809. MZoneList.readlock(__FUNCTION__, __LINE__);
  810. for(zone_iter=zlist.begin(); zone_iter!=zlist.end(); zone_iter++){
  811. (*zone_iter)->Depop(false, true);
  812. }
  813. MZoneList.releasereadlock(__FUNCTION__, __LINE__);
  814. }
  815. void ZoneList::ReloadSpawns() {
  816. MZoneList.readlock(__FUNCTION__, __LINE__);
  817. list<ZoneServer*>::iterator itr;
  818. for (itr = zlist.begin(); itr != zlist.end(); itr++)
  819. (*itr)->ReloadSpawns();
  820. MZoneList.releasereadlock(__FUNCTION__, __LINE__);
  821. }
  822. bool World::ReportBug(string data, char* player_name, int32 account_id, const char* spawn_name, int32 spawn_id, int32 zone_id){
  823. //loginserver
  824. vector<string> list;
  825. int32 offset = 0;
  826. int32 old_offset = 0;
  827. while((offset = data.find(7, old_offset+1)) < 0xFFFFFFFF){
  828. if(old_offset > 0)
  829. list.push_back(data.substr(old_offset+1, offset-old_offset-1));
  830. else
  831. list.push_back(data.substr(old_offset, offset));
  832. old_offset = offset;
  833. }
  834. if(list.size() > 0 && list.size() < 7){
  835. string output = "Invalid bug list:\n";
  836. for(int32 i=0;i<list.size();i++)
  837. output = output.append("\t").append(list[i]).append("\n");
  838. LogWrite(WORLD__ERROR, 0, "World", "%s", output.c_str());
  839. return false;
  840. }
  841. ServerPacket* outpack = new ServerPacket(ServerOP_BugReport, sizeof(BugReport));
  842. BugReport* report = (BugReport*)outpack->pBuffer;
  843. if (list.size() < 7) {
  844. strncpy(report->category, "AutoBug", 7);
  845. strncpy(report->subcategory, "AutoGenerate", 12);
  846. strncpy(report->causes_crash, "N", 1);
  847. strncpy(report->reproducible, "Y", 1);
  848. strncpy(report->summary, data.c_str(), data.length() > 127 ? 127 : data.length());
  849. strncpy(report->description, data.c_str(), data.length() > 1999 ? 1999 : data.length());
  850. strncpy(report->version, "CUR", 3);
  851. }
  852. else
  853. {
  854. strncpy(report->category, list[0].c_str(), list[0].length() > 63 ? 63 : list[0].length());
  855. strncpy(report->subcategory, list[1].c_str(), list[1].length() > 63 ? 63 : list[1].length());
  856. strncpy(report->causes_crash, list[2].c_str(), list[2].length() > 63 ? 63 : list[2].length());
  857. strncpy(report->reproducible, list[3].c_str(), list[3].length() > 63 ? 63 : list[3].length());
  858. strncpy(report->summary, list[4].c_str(), list[4].length() > 127 ? 127 : list[4].length());
  859. strncpy(report->description, list[5].c_str(), list[5].length() > 1999 ? 1999 : list[5].length());
  860. strncpy(report->version, list[6].c_str(), list[6].length() > 31 ? 31 : list[6].length());
  861. }
  862. strncpy(report->player, player_name, strlen(player_name) > 63 ? 63 : strlen(player_name));
  863. strncpy(report->spawn_name, spawn_name, strlen(spawn_name) > 63 ? 63 : strlen(spawn_name));
  864. report->spawn_id = spawn_id;
  865. report->account_id = account_id;
  866. report->zone_id = zone_id;
  867. loginserver.SendPacket(outpack);
  868. database.SaveBugReport(report->category, report->subcategory, report->causes_crash, report->reproducible, report->summary, report->description, report->version, report->player, account_id, spawn_name, spawn_id, zone_id);
  869. safe_delete(outpack);
  870. return true;
  871. }
  872. void ZoneList::WritePlayerStatistics() {
  873. list<ZoneServer*>::iterator zone_itr;
  874. MZoneList.readlock(__FUNCTION__, __LINE__);
  875. for (zone_itr = zlist.begin(); zone_itr != zlist.end(); zone_itr++)
  876. (*zone_itr)->WritePlayerStatistics();
  877. MZoneList.releasereadlock(__FUNCTION__, __LINE__);
  878. }
  879. void ZoneList::ShutDownZones(){
  880. LogWrite(WORLD__INFO, 0, "World", "Shutting down all zones, please wait...");
  881. list<ZoneServer*>::iterator zone_itr;
  882. int32 size = 0;
  883. MZoneList.readlock(__FUNCTION__, __LINE__);
  884. for (zone_itr = zlist.begin(); zone_itr != zlist.end(); zone_itr++){
  885. (*zone_itr)->Shutdown();
  886. }
  887. size = zlist.size();
  888. MZoneList.releasereadlock(__FUNCTION__, __LINE__);
  889. while(size > 0){
  890. Sleep(10);
  891. MZoneList.readlock(__FUNCTION__, __LINE__);
  892. size = zlist.size();
  893. MZoneList.releasereadlock(__FUNCTION__, __LINE__);
  894. }
  895. LogWrite(WORLD__INFO, 0, "World", "Zone shutdown complete");
  896. }
  897. void ZoneList::ReloadMail() {
  898. map<string, Client*>::iterator itr;
  899. MClientList.writelock(__FUNCTION__, __LINE__);
  900. for (itr = client_map.begin(); itr != client_map.end(); itr++) {
  901. itr->second->GetPlayer()->DeleteMail();
  902. database.LoadPlayerMail(itr->second);
  903. }
  904. MClientList.releasewritelock(__FUNCTION__, __LINE__);
  905. }
  906. void World::AddSpawnScript(int32 id, const char* name){
  907. MSpawnScripts.lock();
  908. if(name)
  909. spawn_scripts[id] = string(name);
  910. MSpawnScripts.unlock();
  911. }
  912. void World::AddSpawnEntryScript(int32 id, const char* name){
  913. MSpawnScripts.lock();
  914. if(name)
  915. spawnentry_scripts[id] = string(name);
  916. MSpawnScripts.unlock();
  917. }
  918. void World::AddSpawnLocationScript(int32 id, const char* name){
  919. MSpawnScripts.lock();
  920. if(name)
  921. spawnlocation_scripts[id] = string(name);
  922. MSpawnScripts.unlock();
  923. }
  924. void World::AddZoneScript(int32 id, const char* name) {
  925. MZoneScripts.lock();
  926. if (name)
  927. zone_scripts[id] = string(name);
  928. MZoneScripts.unlock();
  929. }
  930. const char* World::GetSpawnScript(int32 id){
  931. LogWrite(SPAWN__TRACE, 1, "Spawn", "Enter %s", __FUNCTION__);
  932. const char* ret = 0;
  933. MSpawnScripts.lock();
  934. if(spawn_scripts.count(id) > 0)
  935. ret = spawn_scripts[id].c_str();
  936. MSpawnScripts.unlock();
  937. LogWrite(SPAWN__TRACE, 1, "Spawn", "Exit %s", __FUNCTION__);
  938. return ret;
  939. }
  940. const char* World::GetSpawnEntryScript(int32 id){
  941. LogWrite(SPAWN__TRACE, 1, "Spawn", "Enter %s", __FUNCTION__);
  942. const char* ret = 0;
  943. MSpawnScripts.lock();
  944. if(spawnentry_scripts.count(id) > 0)
  945. ret = spawnentry_scripts[id].c_str();
  946. MSpawnScripts.unlock();
  947. LogWrite(SPAWN__TRACE, 1, "Spawn", "Exit %s", __FUNCTION__);
  948. return ret;
  949. }
  950. const char* World::GetSpawnLocationScript(int32 id){
  951. LogWrite(SPAWN__TRACE, 1, "Spawn", "Enter %s", __FUNCTION__);
  952. const char* ret = 0;
  953. MSpawnScripts.lock();
  954. if(spawnlocation_scripts.count(id) > 0)
  955. ret = spawnlocation_scripts[id].c_str();
  956. MSpawnScripts.unlock();
  957. LogWrite(SPAWN__TRACE, 1, "Spawn", "Exit %s", __FUNCTION__);
  958. return ret;
  959. }
  960. const char* World::GetZoneScript(int32 id) {
  961. const char* ret = 0;
  962. MZoneScripts.lock();
  963. if (zone_scripts.count(id) > 0)
  964. ret = zone_scripts[id].c_str();
  965. MZoneScripts.unlock();
  966. return ret;
  967. }
  968. void World::ResetSpawnScripts(){
  969. MSpawnScripts.lock();
  970. spawn_scripts.clear();
  971. spawnentry_scripts.clear();
  972. spawnlocation_scripts.clear();
  973. MSpawnScripts.unlock();
  974. }
  975. void World::ResetZoneScripts() {
  976. MZoneScripts.lock();
  977. zone_scripts.clear();
  978. MZoneScripts.unlock();
  979. }
  980. vector<MerchantItemInfo>* World::GetMerchantItemList(int32 merchant_id, int8 merchant_type, Player* player)
  981. {
  982. vector<MerchantItemInfo>* ret = 0;
  983. MMerchantList.lock();
  984. if(merchant_info.count(merchant_id) > 0)
  985. {
  986. MerchantInfo* info = merchant_info[merchant_id];
  987. vector<MerchantItemInfo>::iterator itr;
  988. int32 inventory_id = 0;
  989. Item* item = 0;
  990. for(int i=info->inventory_ids.size()-1;i>=0;i--)
  991. {
  992. inventory_id = info->inventory_ids[i];
  993. if(merchant_inventory_items.count(inventory_id) > 0)
  994. {
  995. for(itr = merchant_inventory_items[inventory_id].begin(); itr != merchant_inventory_items[inventory_id].end(); itr++)
  996. {
  997. if(!ret)
  998. ret = new vector<MerchantItemInfo>;
  999. item = master_item_list.GetItem((*itr).item_id);
  1000. // if NOT spell merchant, OR
  1001. // skill req is any skill, OR player has the skill, AND
  1002. // skill req2 is any skill, OR player has the skill2
  1003. if(item && ( (merchant_type & MERCHANT_TYPE_SPELLS) == 0 || ( (item->generic_info.skill_req1 == 0xFFFFFFFF || player->GetSkills()->HasSkill(item->generic_info.skill_req1)) && (item->generic_info.skill_req2 == 0xFFFFFFFF || player->GetSkills()->HasSkill(item->generic_info.skill_req2)) ) ) )
  1004. (*ret).push_back(*itr);
  1005. }
  1006. }
  1007. }
  1008. }
  1009. MMerchantList.unlock();
  1010. return ret;
  1011. }
  1012. vector<MerchantItemInfo>* World::GetMerchantList(int32 merchant_id){
  1013. vector<MerchantItemInfo>* ret = 0;
  1014. MMerchantList.lock();
  1015. if(merchant_info.count(merchant_id) > 0){
  1016. MerchantInfo* info = merchant_info[merchant_id];
  1017. map<int32, int16>::iterator itr;
  1018. int32 inventory_id = 0;
  1019. for(int i=info->inventory_ids.size()-1;i>=0;i--){
  1020. inventory_id = info->inventory_ids[i];
  1021. if(merchant_inventory_items.count(inventory_id) > 0){
  1022. ret = &merchant_inventory_items[inventory_id];
  1023. }
  1024. }
  1025. }
  1026. MMerchantList.unlock();
  1027. return ret;
  1028. }
  1029. void World::AddMerchantItem(int32 inventory_id, MerchantItemInfo ItemInfo){
  1030. MMerchantList.lock();
  1031. merchant_inventory_items[inventory_id].push_back(ItemInfo);
  1032. MMerchantList.unlock();
  1033. }
  1034. void World::DeleteMerchantItems(){
  1035. MMerchantList.lock();
  1036. merchant_inventory_items.clear();
  1037. MMerchantList.unlock();
  1038. }
  1039. void World::RemoveMerchantItem(int32 inventory_id, int32 item_id){
  1040. MMerchantList.lock();
  1041. if(merchant_inventory_items.count(inventory_id) > 0) {
  1042. vector<MerchantItemInfo>::iterator itr;
  1043. for(itr = merchant_inventory_items[inventory_id].begin(); itr != merchant_inventory_items[inventory_id].end(); itr++){
  1044. if ((*itr).item_id == item_id) {
  1045. merchant_inventory_items[inventory_id].erase(itr);
  1046. break;
  1047. }
  1048. }
  1049. }
  1050. MMerchantList.unlock();
  1051. }
  1052. int16 World::GetMerchantItemQuantity(int32 merchant_id, int32 item_id){
  1053. int16 amount = 0;
  1054. int32 inventory_id = GetInventoryID(merchant_id, item_id);
  1055. if(inventory_id > 0){
  1056. MMerchantList.lock();
  1057. vector<MerchantItemInfo>::iterator itr;
  1058. for(itr = merchant_inventory_items[inventory_id].begin(); itr != merchant_inventory_items[inventory_id].end(); itr++){
  1059. if ((*itr).item_id == item_id)
  1060. amount = (*itr).quantity;
  1061. }
  1062. MMerchantList.unlock();
  1063. }
  1064. return amount;
  1065. }
  1066. int32 World::GetInventoryID(int32 merchant_id, int32 item_id){
  1067. int32 ret = 0;
  1068. MMerchantList.lock();
  1069. if(merchant_info.count(merchant_id) > 0){
  1070. MerchantInfo* info = merchant_info[merchant_id];
  1071. vector<MerchantItemInfo>::iterator itr;
  1072. int32 inventory_id = 0;
  1073. for(int i=info->inventory_ids.size()-1;i>=0;i--){
  1074. inventory_id = info->inventory_ids[i];
  1075. if(merchant_inventory_items.count(inventory_id) > 0){
  1076. for(itr = merchant_inventory_items[inventory_id].begin(); itr != merchant_inventory_items[inventory_id].end(); itr++){
  1077. if((*itr).item_id == item_id){
  1078. ret = inventory_id;
  1079. break;
  1080. }
  1081. }
  1082. if(ret > 0)
  1083. break;
  1084. }
  1085. }
  1086. }
  1087. MMerchantList.unlock();
  1088. return ret;
  1089. }
  1090. void World::DecreaseMerchantQuantity(int32 merchant_id, int32 item_id, int16 amount){
  1091. int16 total_left = GetMerchantItemQuantity(merchant_id, item_id);
  1092. if(total_left > 0 && total_left < 0xFFFF){
  1093. int32 inventory_id = GetInventoryID(merchant_id, item_id);
  1094. if(inventory_id > 0){
  1095. MMerchantList.lock();
  1096. vector<MerchantItemInfo>::iterator itr;
  1097. for(itr = merchant_inventory_items[inventory_id].begin(); itr != merchant_inventory_items[inventory_id].end(); itr++){
  1098. if ((*itr).item_id == item_id) {
  1099. if(total_left <= amount) {
  1100. merchant_inventory_items[inventory_id].erase(itr);
  1101. amount = 0;
  1102. break;
  1103. }
  1104. else
  1105. (*itr).quantity -= amount;
  1106. amount = (*itr).quantity;
  1107. }
  1108. }
  1109. MMerchantList.unlock();
  1110. }
  1111. }
  1112. }
  1113. MerchantInfo* World::GetMerchantInfo(int32 merchant_id){
  1114. MerchantInfo* ret = 0;
  1115. MMerchantList.lock();
  1116. if(merchant_info.count(merchant_id) > 0)
  1117. ret = merchant_info[merchant_id];
  1118. MMerchantList.unlock();
  1119. return ret;
  1120. }
  1121. void World::AddMerchantInfo(int32 merchant_id, MerchantInfo* info){
  1122. MMerchantList.lock();
  1123. if(merchant_info.count(merchant_id) > 0){
  1124. safe_delete(merchant_info[merchant_id]);
  1125. }
  1126. merchant_info[merchant_id] = info;
  1127. MMerchantList.unlock();
  1128. }
  1129. map<int32, MerchantInfo*>* World::GetMerchantInfo() {
  1130. return &merchant_info;
  1131. }
  1132. void World::DeleteMerchantsInfo(){
  1133. MMerchantList.lock();
  1134. map<int32, MerchantInfo*>::iterator itr;
  1135. for(itr = merchant_info.begin(); itr != merchant_info.end(); itr++){
  1136. safe_delete(itr->second);
  1137. }
  1138. merchant_info.clear();
  1139. MMerchantList.unlock();
  1140. }
  1141. void World::DeleteSpawns(){
  1142. //reloading = true;
  1143. //ClearLootTables();
  1144. /*
  1145. map<int32, NPC*>::iterator npc_list_iter;
  1146. for(npc_list_iter=npc_list.begin();npc_list_iter!=npc_list.end();npc_list_iter++) {
  1147. safe_delete(npc_list_iter->second);
  1148. }
  1149. npc_list.clear();
  1150. map<int32, Object*>::iterator object_list_iter;
  1151. for(object_list_iter=object_list.begin();object_list_iter!=object_list.end();object_list_iter++) {
  1152. safe_delete(object_list_iter->second);
  1153. }
  1154. object_list.clear();
  1155. map<int32, GroundSpawn*>::iterator groundspawn_list_iter;
  1156. for(groundspawn_list_iter=groundspawn_list.begin();groundspawn_list_iter!=groundspawn_list.end();groundspawn_list_iter++) {
  1157. safe_delete(groundspawn_list_iter->second);
  1158. }
  1159. groundspawn_list.clear();
  1160. map<int32, Widget*>::iterator widget_list_iter;
  1161. for(widget_list_iter=widget_list.begin();widget_list_iter!=widget_list.end();widget_list_iter++) {
  1162. safe_delete(widget_list_iter->second);
  1163. }
  1164. widget_list.clear();
  1165. map<int32, Sign*>::iterator sign_list_iter;
  1166. for(sign_list_iter=sign_list.begin();sign_list_iter!=sign_list.end();sign_list_iter++) {
  1167. safe_delete(sign_list_iter->second);
  1168. }
  1169. sign_list.clear();*/
  1170. map<int32, AppearanceData*>::iterator appearance_list_iter;
  1171. for(appearance_list_iter=npc_appearance_list.begin();appearance_list_iter!=npc_appearance_list.end();appearance_list_iter++) {
  1172. safe_delete(appearance_list_iter->second);
  1173. }
  1174. npc_appearance_list.clear();
  1175. /*
  1176. map<int32, vector<EntityCommand*>* >::iterator command_list_iter;
  1177. for(command_list_iter=entity_command_list.begin();command_list_iter!=entity_command_list.end();command_list_iter++) {
  1178. vector<EntityCommand*>* v = command_list_iter->second;
  1179. if(v){
  1180. for(int32 i=0;i<v->size();i++){
  1181. safe_delete(v->at(i));
  1182. }
  1183. safe_delete(v);
  1184. }
  1185. }
  1186. entity_command_list.clear();
  1187. */
  1188. //DeleteGroundSpawnItems();
  1189. //DeleteTransporters();
  1190. //DeleteTransporterMaps();
  1191. }
  1192. void World::ReloadGuilds() {
  1193. guild_list.GetGuilds()->clear(true);
  1194. database.LoadGuilds();
  1195. }
  1196. int8 World::GetClassID(const char* name){
  1197. return classes.GetClassID(name);
  1198. }
  1199. void World::WritePlayerStatistics() {
  1200. zone_list.WritePlayerStatistics();
  1201. }
  1202. void World::WriteServerStatistics() {
  1203. map<int32, Statistic*>::iterator itr;
  1204. Statistic* stat = 0;
  1205. for (itr = server_statistics.begin(); itr != server_statistics.end(); itr++) {
  1206. stat = itr->second;
  1207. if (stat->save_needed) {
  1208. stat->save_needed = false;
  1209. database.WriteServerStatistic(stat);
  1210. }
  1211. }
  1212. database.WriteServerStatisticsNeededQueries();
  1213. }
  1214. void World::AddServerStatistic(int32 stat_id, sint32 stat_value, int32 stat_date) {
  1215. if (server_statistics.count(stat_id) == 0) {
  1216. Statistic* stat = new Statistic;
  1217. stat->stat_id = stat_id;
  1218. stat->stat_value = stat_value;
  1219. stat->stat_date = stat_date;
  1220. stat->save_needed = false;
  1221. server_statistics[stat_id] = stat;
  1222. }
  1223. }
  1224. void World::UpdateServerStatistic(int32 stat_id, sint32 stat_value, bool overwrite) {
  1225. if (server_statistics.count(stat_id) == 0)
  1226. AddServerStatistic(stat_id, stat_value, 0);
  1227. Statistic* stat = server_statistics[stat_id];
  1228. overwrite == true ? stat->stat_value = stat_value : stat->stat_value += stat_value;
  1229. stat->stat_date = Timer::GetUnixTimeStamp();
  1230. stat->save_needed = true;
  1231. }
  1232. sint32 World::GetServerStatisticValue(int32 stat_id) {
  1233. if (server_statistics.count(stat_id) > 0)
  1234. return server_statistics[stat_id]->stat_value;
  1235. return 0;
  1236. }
  1237. void World::RemoveServerStatistics() {
  1238. map<int32, Statistic*>::iterator stat_itr;
  1239. for (stat_itr = server_statistics.begin(); stat_itr != server_statistics.end(); stat_itr++)
  1240. safe_delete(stat_itr->second);
  1241. server_statistics.clear();
  1242. }
  1243. void World::SendGroupQuests(PlayerGroup* group, Client* client){
  1244. return;
  1245. /*if(!group)
  1246. return;
  1247. GroupMemberInfo* info = 0;
  1248. MGroups.readlock(__FUNCTION__, __LINE__);
  1249. deque<GroupMemberInfo*>::iterator itr;
  1250. for(itr = group->members.begin(); itr != group->members.end(); itr++){
  1251. info = *itr;
  1252. if(info->client){
  1253. LogWrite(PLAYER__DEBUG, 0, "Player", "Send Quest Journal...");
  1254. info->client->SendQuestJournal(false, client);
  1255. client->SendQuestJournal(false, info->client);
  1256. }
  1257. }
  1258. MGroups.releasereadlock(__FUNCTION__, __LINE__);*/
  1259. }
  1260. /*void World::CheckRemoveGroupedPlayer(){
  1261. map<GroupMemberInfo*, int32>::iterator itr;
  1262. GroupMemberInfo* found = 0;
  1263. MGroups.readlock(__FUNCTION__, __LINE__);
  1264. for(itr = group_removal_pending.begin(); itr != group_removal_pending.end(); itr++){
  1265. if(itr->second < Timer::GetCurrentTime2()){
  1266. found = itr->first;
  1267. break;
  1268. }
  1269. }
  1270. MGroups.releasereadlock(__FUNCTION__, __LINE__);
  1271. if(found){
  1272. if(!found->client || (found->client && found->client->IsConnected() == false))
  1273. DeleteGroupMember(found);
  1274. else{
  1275. MGroups.writelock(__FUNCTION__, __LINE__);
  1276. group_removal_pending.erase(found);
  1277. MGroups.releasewritelock(__FUNCTION__, __LINE__);
  1278. }
  1279. }
  1280. }*/
  1281. void World::RejoinGroup(Client* client, int32 group_id){
  1282. if (!group_id) // no need if no group id!
  1283. return;
  1284. world.GetGroupManager()->GroupLock(__FUNCTION__, __LINE__);
  1285. PlayerGroup* group = world.GetGroupManager()->GetGroup(group_id);
  1286. deque<GroupMemberInfo*>* members = 0;
  1287. if (group)
  1288. members = group->GetMembers();
  1289. string name = string(client->GetPlayer()->GetName());
  1290. if (!members)
  1291. {
  1292. // group does not exist!
  1293. Query query;
  1294. query.AddQueryAsync(client->GetCharacterID(), &database, Q_INSERT, "UPDATE characters set group_id = 0 where id = %u",
  1295. client->GetCharacterID());
  1296. LogWrite(PLAYER__ERROR, 0, "Player", "Group did not exist for player %s to group id %i, async query to group_id = 0.", name.c_str(), group_id);
  1297. world.GetGroupManager()->ReleaseGroupLock(__FUNCTION__, __LINE__);
  1298. return;
  1299. }
  1300. deque<GroupMemberInfo*>::iterator itr;
  1301. GroupMemberInfo* info = 0;
  1302. bool match = false;
  1303. group->MGroupMembers.writelock();
  1304. for (itr = members->begin(); itr != members->end(); itr++) {
  1305. info = *itr;
  1306. if (info && info->name == name)
  1307. {
  1308. info->client = client;
  1309. info->member = client->GetPlayer();
  1310. client->GetPlayer()->SetGroup(group);
  1311. client->GetPlayer()->SetGroupMemberInfo(info);
  1312. client->GetPlayer()->UpdateGroupMemberInfo(true, true);
  1313. LogWrite(PLAYER__DEBUG, 0, "Player", "Identified group match for player %s to group id %u", name.c_str(), group_id);
  1314. match = true;
  1315. break;
  1316. }
  1317. }
  1318. group->MGroupMembers.releasewritelock();
  1319. // must be done after cause it needs a readlock
  1320. if (match)
  1321. group->SendGroupUpdate();
  1322. if (!match)
  1323. LogWrite(PLAYER__ERROR, 0, "Player", "Identified group match for player %s to group id %u, however the player name was not present in the group! May be an old group id that has been re-used.", name.c_str(), group_id);
  1324. world.GetGroupManager()->ReleaseGroupLock(__FUNCTION__, __LINE__);
  1325. }
  1326. void World::AddBonuses(ItemStatsValues* values, int16 type, sint32 value, Entity* entity){
  1327. if(values){
  1328. switch(type){
  1329. case ITEM_STAT_STR:{
  1330. values->str += value;
  1331. break;
  1332. }
  1333. case ITEM_STAT_STA:{
  1334. values->sta += value;
  1335. break;
  1336. }
  1337. case ITEM_STAT_AGI:{
  1338. values->agi += value;
  1339. break;
  1340. }
  1341. case ITEM_STAT_WIS:{
  1342. values->wis += value;
  1343. break;
  1344. }
  1345. case ITEM_STAT_INT:{
  1346. values->int_ += value;
  1347. break;
  1348. }
  1349. case ITEM_STAT_VS_SLASH:{
  1350. values->vs_slash += value;
  1351. break;
  1352. }
  1353. case ITEM_STAT_VS_CRUSH:{
  1354. values->vs_crush += value;
  1355. break;
  1356. }
  1357. case ITEM_STAT_VS_PIERCE:{
  1358. values->vs_pierce += value;
  1359. break;
  1360. }
  1361. case ITEM_STAT_VS_HEAT:{
  1362. values->vs_heat += value;
  1363. break;
  1364. }
  1365. case ITEM_STAT_VS_COLD:{
  1366. values->vs_cold += value;
  1367. break;
  1368. }
  1369. case ITEM_STAT_VS_MAGIC:{
  1370. values->vs_magic += value;
  1371. break;
  1372. }
  1373. case ITEM_STAT_VS_MENTAL:{
  1374. values->vs_mental += value;
  1375. break;
  1376. }
  1377. case ITEM_STAT_VS_DIVINE:{
  1378. values->vs_divine += value;
  1379. break;
  1380. }
  1381. case ITEM_STAT_VS_DISEASE:{
  1382. values->vs_disease += value;
  1383. break;
  1384. }
  1385. case ITEM_STAT_VS_POISON:{
  1386. values->vs_poison += value;
  1387. break;
  1388. }
  1389. case ITEM_STAT_HEALTH:{
  1390. values->health += value;
  1391. break;
  1392. }
  1393. case ITEM_STAT_POWER:{
  1394. values->power += value;
  1395. break;
  1396. }
  1397. case ITEM_STAT_CONCENTRATION:{
  1398. values->concentration += value;
  1399. break;
  1400. }
  1401. case ITEM_STAT_ABILITY_MODIFIER:{
  1402. values->ability_modifier += value;
  1403. break;
  1404. }
  1405. case ITEM_STAT_CRITICALMITIGATION:{
  1406. values->criticalmitigation += value;
  1407. break;
  1408. }
  1409. case ITEM_STAT_EXTRASHIELDBLOCKCHANCE:{
  1410. values->extrashieldblockchance += value;
  1411. break;
  1412. }
  1413. case ITEM_STAT_BENEFICIALCRITCHANCE:{
  1414. values->beneficialcritchance += value;
  1415. break;
  1416. }
  1417. case ITEM_STAT_CRITBONUS:{
  1418. values->critbonus += value;
  1419. break;
  1420. }
  1421. /*case ITEM_STAT_POTENCY:{
  1422. values->potency += value;
  1423. break;
  1424. }*/
  1425. case ITEM_STAT_HATEGAINMOD:{
  1426. values->hategainmod += value;
  1427. break;
  1428. }
  1429. case ITEM_STAT_ABILITYREUSESPEED:{
  1430. values->abilityreusespeed += value;
  1431. break;
  1432. }
  1433. case ITEM_STAT_ABILITYCASTINGSPEED:{
  1434. values->abilitycastingspeed += value;
  1435. break;
  1436. }
  1437. case ITEM_STAT_ABILITYRECOVERYSPEED:{
  1438. values->abilityrecoveryspeed += value;
  1439. break;
  1440. }
  1441. case ITEM_STAT_SPELLREUSESPEED:{
  1442. values->spellreusespeed += value;
  1443. break;
  1444. }
  1445. case ITEM_STAT_SPELLMULTIATTACKCHANCE:{
  1446. values->spellmultiattackchance += value;
  1447. break;
  1448. }
  1449. case ITEM_STAT_DPS:{
  1450. values->dps += value;
  1451. break;
  1452. }
  1453. case ITEM_STAT_ATTACKSPEED:{
  1454. values->attackspeed += value;
  1455. break;
  1456. }
  1457. case ITEM_STAT_MULTIATTACKCHANCE:{
  1458. values->multiattackchance += value;
  1459. break;
  1460. }
  1461. case ITEM_STAT_AEAUTOATTACKCHANCE:{
  1462. values->aeautoattackchance += value;
  1463. break;
  1464. }
  1465. case ITEM_STAT_STRIKETHROUGH:{
  1466. values->strikethrough += value;
  1467. break;
  1468. }
  1469. case ITEM_STAT_ACCURACY:{
  1470. values->accuracy += value;
  1471. break;
  1472. }
  1473. /*case ITEM_STAT_OFFENSIVESPEED:{
  1474. values->offensivespeed += value;
  1475. break;
  1476. }*/
  1477. default: {
  1478. if (entity)
  1479. entity->stats[type] += value;
  1480. break;
  1481. }
  1482. }
  1483. }
  1484. }
  1485. void World::CreateGuild(const char* guild_name, Client* leader, int32 group_id) {
  1486. deque<GroupMemberInfo*>::iterator itr;
  1487. GroupMemberInfo* gmi;
  1488. Guild *guild;
  1489. assert(guild_name);
  1490. guild = new Guild();
  1491. guild->SetName(guild_name);
  1492. guild->SetFormedDate(Timer::GetUnixTimeStamp());
  1493. database.LoadGuildDefaultRanks(guild);
  1494. database.LoadGuildDefaultEventFilters(guild);
  1495. database.SaveGuild(guild, true);
  1496. database.SaveGuildEvents(guild);
  1497. database.SaveGuildRanks(guild);
  1498. database.SaveGuildEventFilters(guild);
  1499. database.SaveGuildRecruiting(guild);
  1500. guild_list.AddGuild(guild);
  1501. if (leader && !leader->GetPlayer()->GetGuild())
  1502. guild->AddNewGuildMember(leader, 0, GUILD_RANK_LEADER);
  1503. database.SaveGuildMembers(guild);
  1504. if (leader && group_id > 0) {
  1505. GetGroupManager()->GroupLock(__FUNCTION__, __LINE__);
  1506. PlayerGroup* group = world.GetGroupManager()->GetGroup(group_id);
  1507. if (group)
  1508. {
  1509. group->MGroupMembers.readlock(__FUNCTION__, __LINE__);
  1510. deque<GroupMemberInfo*>* members = group->GetMembers();
  1511. for (itr = members->begin(); itr != members->end(); itr++) {
  1512. gmi = *itr;
  1513. if (gmi->client && gmi->client != leader && !gmi->client->GetPlayer()->GetGuild())
  1514. guild->InvitePlayer(gmi->client, leader->GetPlayer()->GetName());
  1515. }
  1516. group->MGroupMembers.releasereadlock(__FUNCTION__, __LINE__);
  1517. }
  1518. GetGroupManager()->ReleaseGroupLock(__FUNCTION__, __LINE__);
  1519. }
  1520. }
  1521. void World::SaveGuilds() {
  1522. MutexMap<int32, Guild*>* guilds = guild_list.GetGuilds();
  1523. MutexMap<int32, Guild*>::iterator itr = guilds->begin();
  1524. while (itr.Next()) {
  1525. Guild* guild = itr.second;
  1526. if (guild->GetSaveNeeded())
  1527. database.SaveGuild(guild);
  1528. if (guild->GetMemberSaveNeeded())
  1529. database.SaveGuildMembers(guild);
  1530. if (guild->GetEventsSaveNeeded())
  1531. database.SaveGuildEvents(guild);
  1532. if (guild->GetRanksSaveNeeded())
  1533. database.SaveGuildRanks(guild);
  1534. if (guild->GetEventFiltersSaveNeeded())
  1535. database.SaveGuildEventFilters(guild);
  1536. if (guild->GetPointsHistorySaveNeeded())
  1537. database.SaveGuildPointsHistory(guild);
  1538. if (guild->GetRecruitingSaveNeeded())
  1539. database.SaveGuildRecruiting(guild);
  1540. }
  1541. }
  1542. void World::PickRandomLottoDigits(int32* digits) {
  1543. if (digits) {
  1544. for (int32 i = 0; i < 6; i++) {
  1545. bool found = true;
  1546. int32 num = 0;
  1547. while (found) {
  1548. num = ((int32)rand() % 36) + 1;
  1549. for (int32 j = 0; j < 6; j++) {
  1550. if (digits[j] == num)
  1551. break;
  1552. if (j == 5)
  1553. found = false;
  1554. }
  1555. }
  1556. digits[i] = num;
  1557. }
  1558. }
  1559. }
  1560. void World::AddLottoPlayer(int32 character_id, int32 end_time) {
  1561. LottoPlayer* lp;
  1562. if (lotto_players.count(character_id) == 0) {
  1563. lp = new LottoPlayer;
  1564. lotto_players.Put(character_id, lp);
  1565. }
  1566. else
  1567. lp = lotto_players.Get(character_id);
  1568. lp->end_time = end_time;
  1569. lp->num_matches = 0;
  1570. lp->set = false;
  1571. }
  1572. void World::RemoveLottoPlayer(int32 character_id) {
  1573. if (lotto_players.count(character_id) > 0)
  1574. lotto_players.erase(character_id, false, true);
  1575. }
  1576. void World::SetLottoPlayerNumMatches(int32 character_id, int8 num_matches) {
  1577. if (lotto_players.count(character_id) > 0) {
  1578. lotto_players.Get(character_id)->num_matches = num_matches;
  1579. lotto_players.Get(character_id)->set = true;
  1580. }
  1581. }
  1582. void World::CheckLottoPlayers() {
  1583. MutexMap<int32, LottoPlayer*>::iterator itr = lotto_players.begin();
  1584. while (itr.Next()) {
  1585. LottoPlayer* lp = itr->second;
  1586. if (Timer::GetCurrentTime2() >= lp->end_time && lp->set) {
  1587. int8 num_matches = lp->num_matches;
  1588. LogWrite(PLAYER__DEBUG, 0, "Player", "Num matches: %u", lp->num_matches);
  1589. Client* client = zone_list.GetClientByCharID(itr->first);
  1590. if (client && num_matches >= 2) {
  1591. if (num_matches == 2) {
  1592. client->SimpleMessage(CHANNEL_COLOR_YELLOW, "You receive 10 silver.");
  1593. client->SendPopupMessage(0, "Congratulations! You have won 10 silver!", "", 2, 0xFF, 0xFF, 0x99);
  1594. client->GetPlayer()->AddCoins(1000);
  1595. client->GetPlayer()->GetZone()->SendCastSpellPacket(869, client->GetPlayer());
  1596. }
  1597. else if (num_matches == 3) {
  1598. client->SimpleMessage(CHANNEL_COLOR_YELLOW, "You receive 50 silver.");
  1599. client->SendPopupMessage(0, "Congratulations! You have won 50 silver!", "", 2, 0xFF, 0xFF, 0x99);
  1600. client->GetPlayer()->AddCoins(5000);
  1601. client->GetPlayer()->GetZone()->SendCastSpellPacket(870, client->GetPlayer());
  1602. }
  1603. else if (num_matches == 4) {
  1604. client->SimpleMessage(CHANNEL_COLOR_YELLOW, "You receive 2 gold 50 silver.");
  1605. client->SendPopupMessage(0, "Congratulations! You have won 2 gold 50 silver!", "", 2, 0xFF, 0xFF, 0x99);
  1606. client->GetPlayer()->AddCoins(25000);
  1607. client->GetPlayer()->GetZone()->SendCastSpellPacket(871, client->GetPlayer());
  1608. }
  1609. else if (num_matches == 5) {
  1610. client->SimpleMessage(CHANNEL_COLOR_YELLOW, "You receive 25 gold.");
  1611. client->SendPopupMessage(0, "Congratulations! You have won 25 gold!", "", 2, 0xFF, 0xFF, 0x99);
  1612. client->GetPlayer()->AddCoins(250000);
  1613. client->GetPlayer()->GetZone()->SendCastSpellPacket(872, client->GetPlayer());
  1614. }
  1615. else if (num_matches == 6) {
  1616. Variable* var = variables.FindVariable("gambling_current_jackpot");
  1617. if (var) {
  1618. int64 jackpot = 0;
  1619. try {
  1620. jackpot = atoul(var->GetValue());
  1621. }
  1622. catch (...) {
  1623. jackpot = 10000;
  1624. }
  1625. char coin_message[128];
  1626. char message[512];
  1627. char announcement[512];
  1628. memset(coin_message, 0, sizeof(coin_message));
  1629. memset(message, 0, sizeof(message));
  1630. memset(announcement, 0, sizeof(announcement));
  1631. sprintf(coin_message, "%s", client->GetCoinMessage(jackpot).c_str());
  1632. sprintf(message, "Congratulations! You have won %s!", coin_message);
  1633. sprintf(announcement, "%s as won the jackpot containing a total of %s!", client->GetPlayer()->GetName(), coin_message);
  1634. client->Message(CHANNEL_COLOR_YELLOW, "You receive %s.", coin_message);
  1635. client->SendPopupMessage(0, message, "", 2, 0xFF, 0xFF, 0x99);
  1636. zone_list.HandleGlobalAnnouncement(announcement);
  1637. client->GetPlayer()->AddCoins(jackpot);
  1638. client->GetPlayer()->GetZone()->SendCastSpellPacket(843, client->GetPlayer());
  1639. client->GetPlayer()->GetZone()->SendCastSpellPacket(1413, client->GetPlayer());
  1640. }
  1641. }
  1642. }
  1643. RemoveLottoPlayer(itr->first);
  1644. }
  1645. }
  1646. }
  1647. void World::AddHouseZone(int32 id, string name, int64 cost_coins, int32 cost_status, int64 upkeep_coins, int32 upkeep_status, int8 vault_slots, int8 alignment, int8 guild_level, int32 zone_id, int32 exit_zone_id, float exit_x, float exit_y, float exit_z, float exit_heading) {
  1648. MHouseZones.writelock(__FUNCTION__, __LINE__);
  1649. if (m_houseZones.count(id) == 0) {
  1650. HouseZone* hz = new HouseZone;
  1651. //ZeroMemory(hz, sizeof(HouseZone));
  1652. hz->id = id;
  1653. hz->name = name;
  1654. hz->cost_coin = cost_coins;
  1655. hz->cost_status = cost_status;
  1656. hz->upkeep_coin = upkeep_coins;
  1657. hz->upkeep_status = upkeep_status;
  1658. hz->vault_slots = vault_slots;
  1659. hz->alignment = alignment;
  1660. hz->guild_level = guild_level;
  1661. hz->zone_id = zone_id;
  1662. hz->exit_zone_id = exit_zone_id;
  1663. hz->exit_x = exit_x;
  1664. hz->exit_y = exit_y;
  1665. hz->exit_z = exit_z;
  1666. hz->exit_heading = exit_heading;
  1667. m_houseZones[id] = hz;
  1668. }
  1669. else {
  1670. LogWrite(WORLD__ERROR, 0, "Housing", "Duplicate house id (%u) for %s", id, name.c_str());
  1671. }
  1672. MHouseZones.releasewritelock(__FUNCTION__, __LINE__);
  1673. }
  1674. HouseZone* World::GetHouseZone(int32 id) {
  1675. HouseZone* ret = 0;
  1676. MHouseZones.readlock(__FUNCTION__, __LINE__);
  1677. if (m_houseZones.count(id) > 0)
  1678. ret = m_houseZones[id];
  1679. MHouseZones.releasereadlock(__FUNCTION__, __LINE__);
  1680. return ret;
  1681. }
  1682. void World::AddPlayerHouse(int32 char_id, int32 house_id, int64 unique_id, int32 instance_id, int32 upkeep_due, int64 escrow_coins, int32 escrow_status, string player_name) {
  1683. MPlayerHouses.writelock(__FUNCTION__, __LINE__);
  1684. if (m_playerHouses.count(house_id) == 0 || m_playerHouses[house_id].count(char_id) == 0) {
  1685. PlayerHouse* ph = new PlayerHouse;
  1686. ph->house_id = house_id;
  1687. ph->unique_id = unique_id;
  1688. ph->instance_id = instance_id;
  1689. ph->escrow_coins = escrow_coins;
  1690. ph->escrow_status = escrow_status;
  1691. ph->upkeep_due = upkeep_due;
  1692. ph->player_name = player_name;
  1693. ReloadHouseData(ph);
  1694. m_playerHouses[house_id][char_id] = ph;
  1695. }
  1696. MPlayerHouses.releasewritelock(__FUNCTION__, __LINE__);
  1697. }
  1698. void World::ReloadHouseData(PlayerHouse* ph)
  1699. {
  1700. database.LoadDeposits(ph);
  1701. database.LoadHistory(ph);
  1702. }
  1703. PlayerHouse* World::GetPlayerHouseByHouseID(int32 char_id, int32 house_id) {
  1704. PlayerHouse* ret = 0;
  1705. MPlayerHouses.readlock(__FUNCTION__, __LINE__);
  1706. if (m_playerHouses.count(house_id) > 0 && m_playerHouses[house_id].count(char_id) > 0)
  1707. ret = m_playerHouses[house_id][char_id];
  1708. MPlayerHouses.releasereadlock(__FUNCTION__, __LINE__);
  1709. return ret;
  1710. }
  1711. PlayerHouse* World::GetPlayerHouseByUniqueID(int64 unique_id) {
  1712. PlayerHouse* ret = 0;
  1713. MPlayerHouses.readlock(__FUNCTION__, __LINE__);
  1714. map<int32, map<int32, PlayerHouse*> >::iterator itr;
  1715. for (itr = m_playerHouses.begin(); itr != m_playerHouses.end(); itr++) {
  1716. map<int32, PlayerHouse*>::iterator itr2;
  1717. for (itr2 = itr->second.begin(); itr2 != itr->second.end(); itr2++) {
  1718. if (itr2->second->unique_id == unique_id) {
  1719. ret = itr2->second;
  1720. break;
  1721. }
  1722. }
  1723. if (ret)
  1724. break;
  1725. }
  1726. MPlayerHouses.releasereadlock(__FUNCTION__, __LINE__);
  1727. return ret;
  1728. }
  1729. PlayerHouse* World::GetPlayerHouseByInstanceID(int32 instance_id) {
  1730. PlayerHouse* ret = 0;
  1731. MPlayerHouses.readlock(__FUNCTION__, __LINE__);
  1732. map<int32, map<int32, PlayerHouse*> >::iterator itr;
  1733. for (itr = m_playerHouses.begin(); itr != m_playerHouses.end(); itr++) {
  1734. map<int32, PlayerHouse*>::iterator itr2;
  1735. for (itr2 = itr->second.begin(); itr2 != itr->second.end(); itr2++) {
  1736. if (itr2->second->instance_id == instance_id) {
  1737. ret = itr2->second;
  1738. break;
  1739. }
  1740. }
  1741. if (ret)
  1742. break;
  1743. }
  1744. MPlayerHouses.releasereadlock(__FUNCTION__, __LINE__);
  1745. return ret;
  1746. }
  1747. vector<PlayerHouse*> World::GetAllPlayerHouses(int32 char_id) {
  1748. vector<PlayerHouse*> ret;
  1749. MPlayerHouses.readlock(__FUNCTION__, __LINE__);
  1750. map<int32, map<int32, PlayerHouse*> >::iterator itr;
  1751. for (itr = m_playerHouses.begin(); itr != m_playerHouses.end(); itr++) {
  1752. if (itr->second.count(char_id) > 0)
  1753. ret.push_back(itr->second[char_id]);
  1754. }
  1755. MPlayerHouses.releasereadlock(__FUNCTION__, __LINE__);
  1756. return ret;
  1757. }
  1758. vector<PlayerHouse*> World::GetAllPlayerHousesByHouseID(int32 house_id) {
  1759. vector<PlayerHouse*> ret;
  1760. MPlayerHouses.readlock(__FUNCTION__, __LINE__);
  1761. if (m_houseZones.count(house_id) > 0) {
  1762. map<int32, PlayerHouse*>::iterator itr;
  1763. for (itr = m_playerHouses[house_id].begin(); itr != m_playerHouses[house_id].end(); itr++)
  1764. ret.push_back(itr->second);
  1765. }
  1766. MPlayerHouses.releasereadlock(__FUNCTION__, __LINE__);
  1767. return ret;
  1768. }
  1769. void World::PopulateTOVStatMap() {
  1770. //This function populates a map that converts changed CoE to ToV stats
  1771. tov_itemstat_conversion[0] = TOV_ITEM_STAT_HPREGEN;
  1772. tov_itemstat_conversion[1] = TOV_ITEM_STAT_MANAREGEN;
  1773. tov_itemstat_conversion[2] = TOV_ITEM_STAT_HPREGENPPT;
  1774. tov_itemstat_conversion[3] = TOV_ITEM_STAT_MPREGENPPT;
  1775. tov_itemstat_conversion[4] = TOV_ITEM_STAT_COMBATHPREGENPPT;
  1776. tov_itemstat_conversion[5] = TOV_ITEM_STAT_COMBATMPREGENPPT;
  1777. tov_itemstat_conversion[6] = TOV_ITEM_STAT_MAXHP;
  1778. tov_itemstat_conversion[7] = TOV_ITEM_STAT_MAXHPPERC;
  1779. tov_itemstat_conversion[8] = TOV_ITEM_STAT_MAXHPPERCFINAL;
  1780. tov_itemstat_conversion[9] = TOV_ITEM_STAT_SPEED;
  1781. tov_itemstat_conversion[10] = TOV_ITEM_STAT_SLOW;
  1782. tov_itemstat_conversion[11] = TOV_ITEM_STAT_MOUNTSPEED;
  1783. tov_itemstat_conversion[12] = TOV_ITEM_STAT_MOUNTAIRSPEED;
  1784. tov_itemstat_conversion[13] = TOV_ITEM_STAT_LEAPSPEED;
  1785. tov_itemstat_conversion[14] = TOV_ITEM_STAT_LEAPTIME;
  1786. tov_itemstat_conversion[15] = TOV_ITEM_STAT_GLIDEEFFICIENCY;
  1787. tov_itemstat_conversion[16] = TOV_ITEM_STAT_OFFENSIVESPEED;
  1788. tov_itemstat_conversion[17] = TOV_ITEM_STAT_ATTACKSPEED;
  1789. tov_itemstat_conversion[18] = 698;
  1790. tov_itemstat_conversion[19] = TOV_ITEM_STAT_MAXMANA;
  1791. tov_itemstat_conversion[20] = TOV_ITEM_STAT_MAXMANAPERC;
  1792. tov_itemstat_conversion[21] = TOV_ITEM_STAT_MAXATTPERC;
  1793. tov_itemstat_conversion[22] = TOV_ITEM_STAT_BLURVISION;
  1794. tov_itemstat_conversion[23] = TOV_ITEM_STAT_MAGICLEVELIMMUNITY;
  1795. tov_itemstat_conversion[24] = TOV_ITEM_STAT_HATEGAINMOD;
  1796. tov_itemstat_conversion[25] = TOV_ITEM_STAT_COMBATEXPMOD;
  1797. tov_itemstat_conversion[26] = TOV_ITEM_STAT_TRADESKILLEXPMOD;
  1798. tov_itemstat_conversion[27] = TOV_ITEM_STAT_ACHIEVEMENTEXPMOD;
  1799. tov_itemstat_conversion[28] = TOV_ITEM_STAT_SIZEMOD;
  1800. tov_itemstat_conversion[29] = TOV_ITEM_STAT_DPS;
  1801. tov_itemstat_conversion[30] = 698;
  1802. tov_itemstat_conversion[31] = TOV_ITEM_STAT_STEALTH;
  1803. tov_itemstat_conversion[32] = TOV_ITEM_STAT_INVIS;
  1804. tov_itemstat_conversion[33] = TOV_ITEM_STAT_SEESTEALTH;
  1805. tov_itemstat_conversion[34] = TOV_ITEM_STAT_SEEINVIS;
  1806. tov_itemstat_conversion[35] = TOV_ITEM_STAT_EFFECTIVELEVELMOD;
  1807. tov_itemstat_conversion[36] = TOV_ITEM_STAT_RIPOSTECHANCE;
  1808. tov_itemstat_conversion[37] = TOV_ITEM_STAT_PARRYCHANCE;
  1809. tov_itemstat_conversion[38] = TOV_ITEM_STAT_DODGECHANCE;
  1810. tov_itemstat_conversion[39] = TOV_ITEM_STAT_AEAUTOATTACKCHANCE;
  1811. tov_itemstat_conversion[40] = 698;
  1812. tov_itemstat_conversion[41] = TOV_ITEM_STAT_MULTIATTACKCHANCE;
  1813. tov_itemstat_conversion[42] = 698;
  1814. tov_itemstat_conversion[43] = 698;
  1815. tov_itemstat_conversion[44] = 698;
  1816. tov_itemstat_conversion[45] = TOV_ITEM_STAT_SPELLMULTIATTACKCHANCE;
  1817. tov_itemstat_conversion[46] = 698;
  1818. tov_itemstat_conversion[47] = TOV_ITEM_STAT_FLURRY;
  1819. tov_itemstat_conversion[48] = 698;
  1820. tov_itemstat_conversion[49] = TOV_ITEM_STAT_MELEEDAMAGEMULTIPLIER;
  1821. tov_itemstat_conversion[50] = TOV_ITEM_STAT_EXTRAHARVESTCHANCE;
  1822. tov_itemstat_conversion[51] = TOV_ITEM_STAT_EXTRASHIELDBLOCKCHANCE;
  1823. tov_itemstat_conversion[52] = TOV_ITEM_STAT_ITEMHPREGENPPT;
  1824. tov_itemstat_conversion[53] = TOV_ITEM_STAT_ITEMPPREGENPPT;
  1825. tov_itemstat_conversion[54] = TOV_ITEM_STAT_MELEECRITCHANCE;
  1826. tov_itemstat_conversion[55] = TOV_ITEM_STAT_CRITAVOIDANCE;
  1827. tov_itemstat_conversion[56] = TOV_ITEM_STAT_BENEFICIALCRITCHANCE;
  1828. tov_itemstat_conversion[57] = TOV_ITEM_STAT_CRITBONUS;
  1829. tov_itemstat_conversion[58] = 698;
  1830. tov_itemstat_conversion[59] = TOV_ITEM_STAT_POTENCY;
  1831. tov_itemstat_conversion[60] = 698;
  1832. tov_itemstat_conversion[61] = TOV_ITEM_STAT_UNCONSCIOUSHPMOD;
  1833. tov_itemstat_conversion[62] = TOV_ITEM_STAT_ABILITYREUSESPEED;
  1834. tov_itemstat_conversion[63] = TOV_ITEM_STAT_ABILITYRECOVERYSPEED;
  1835. tov_itemstat_conversion[64] = TOV_ITEM_STAT_ABILITYCASTINGSPEED;
  1836. tov_itemstat_conversion[65] = TOV_ITEM_STAT_SPELLREUSESPEED;
  1837. tov_itemstat_conversion[66] = TOV_ITEM_STAT_MELEEWEAPONRANGE;
  1838. tov_itemstat_conversion[67] = TOV_ITEM_STAT_RANGEDWEAPONRANGE;
  1839. tov_itemstat_conversion[68] = TOV_ITEM_STAT_FALLINGDAMAGEREDUCTION;
  1840. tov_itemstat_conversion[69] = TOV_ITEM_STAT_RIPOSTEDAMAGE;
  1841. tov_itemstat_conversion[70] = TOV_ITEM_STAT_MINIMUMDEFLECTIONCHANCE;
  1842. tov_itemstat_conversion[71] = TOV_ITEM_STAT_MOVEMENTWEAVE;
  1843. tov_itemstat_conversion[72] = TOV_ITEM_STAT_COMBATHPREGEN;
  1844. tov_itemstat_conversion[73] = TOV_ITEM_STAT_COMBATMANAREGEN;
  1845. tov_itemstat_conversion[74] = TOV_ITEM_STAT_CONTESTSPEEDBOOST;
  1846. tov_itemstat_conversion[75] = TOV_ITEM_STAT_TRACKINGAVOIDANCE;
  1847. tov_itemstat_conversion[76] = TOV_ITEM_STAT_STEALTHINVISSPEEDMOD;
  1848. tov_itemstat_conversion[77] = TOV_ITEM_STAT_LOOT_COIN;
  1849. tov_itemstat_conversion[78] = TOV_ITEM_STAT_ARMORMITIGATIONINCREASE;
  1850. tov_itemstat_conversion[79] = TOV_ITEM_STAT_AMMOCONSERVATION;
  1851. tov_itemstat_conversion[80] = TOV_ITEM_STAT_STRIKETHROUGH;
  1852. tov_itemstat_conversion[81] = TOV_ITEM_STAT_STATUSBONUS;
  1853. tov_itemstat_conversion[82] = TOV_ITEM_STAT_ACCURACY;
  1854. tov_itemstat_conversion[83] = TOV_ITEM_STAT_COUNTERSTRIKE;
  1855. tov_itemstat_conversion[84] = TOV_ITEM_STAT_SHIELDBASH;
  1856. tov_itemstat_conversion[85] = TOV_ITEM_STAT_WEAPONDAMAGEBONUS;
  1857. tov_itemstat_conversion[86] = 698;
  1858. tov_itemstat_conversion[87] = TOV_ITEM_STAT_WEAPONDAMAGEBONUSMELEEONLY;
  1859. tov_itemstat_conversion[88] = TOV_ITEM_STAT_ADDITIONALRIPOSTECHANCE;
  1860. tov_itemstat_conversion[89] = TOV_ITEM_STAT_PVPTOUGHNESS;
  1861. tov_itemstat_conversion[90] = TOV_ITEM_STAT_PVPLETHALITY;
  1862. tov_itemstat_conversion[91] = TOV_ITEM_STAT_STAMINABONUS;
  1863. tov_itemstat_conversion[92] = TOV_ITEM_STAT_WISDOMMITBONUS;
  1864. tov_itemstat_conversion[93] = TOV_ITEM_STAT_HEALRECEIVE;
  1865. tov_itemstat_conversion[94] = TOV_ITEM_STAT_HEALRECEIVEPERC;
  1866. tov_itemstat_conversion[95] = TOV_ITEM_STAT_PVPCRITICALMITIGATION;
  1867. tov_itemstat_conversion[96] = TOV_ITEM_STAT_BASEAVOIDANCEBONUS;
  1868. tov_itemstat_conversion[97] = TOV_ITEM_STAT_INCOMBATSAVAGERYREGEN;
  1869. tov_itemstat_conversion[98] = TOV_ITEM_STAT_OUTOFCOMBATSAVAGERYREGEN;
  1870. tov_itemstat_conversion[99] = TOV_ITEM_STAT_SAVAGERYREGEN;
  1871. tov_itemstat_conversion[100] = TOV_ITEM_STAT_SAVAGERYGAINMOD;
  1872. tov_itemstat_conversion[101] = TOV_ITEM_STAT_MAXSAVAGERYLEVEL;
  1873. }
  1874. int32 World::LoadItemBlueStats() {
  1875. Query query;
  1876. MYSQL_ROW row;
  1877. int32 count = 0;
  1878. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT version_range1,version_range2,emu_stat,name,stat from itemstats");
  1879. if (result && mysql_num_rows(result) > 0) {
  1880. while (result && (row = mysql_fetch_row(result))) {
  1881. count++;
  1882. if (atoi(row[0]) >= 63119) //KA
  1883. ka_itemstat_conversion[atoi(row[2])] = atoi(row[4]);
  1884. else if (atoi(row[0]) >= 57101) // ToV
  1885. tov_itemstat_conversion[atoi(row[2])] = atoi(row[4]);
  1886. else if (atoi(row[0]) >= 1193) // CoE
  1887. coe_itemstat_conversion[atoi(row[2])] = atoi(row[4]);
  1888. else if (atoi(row[0]) >= 1096) // DoV
  1889. dov_itemstat_conversion[atoi(row[2])] = atoi(row[4]);
  1890. }
  1891. }
  1892. return count;
  1893. }
  1894. sint16 World::GetItemStatTOVValue(sint16 subtype) {
  1895. return (tov_itemstat_conversion[subtype] - 600);
  1896. }
  1897. sint16 World::GetItemStatDOVValue(sint16 subtype) {
  1898. return (dov_itemstat_conversion[subtype] - 600);
  1899. }
  1900. sint16 World::GetItemStatCOEValue(sint16 subtype) {
  1901. return (coe_itemstat_conversion[subtype] - 600);
  1902. }
  1903. sint16 World::GetItemStatKAValue(sint16 subtype) {
  1904. return (ka_itemstat_conversion[subtype] - 600);
  1905. }
  1906. bool World::CheckTempBugCRC(char* msg)
  1907. {
  1908. MBugReport.writelock();
  1909. sint32 crc = GetItemNameCrc(std::string(msg));
  1910. if (bug_report_crc.count(crc) > 0)
  1911. {
  1912. MBugReport.releasewritelock();
  1913. return true;
  1914. }
  1915. else
  1916. bug_report_crc.insert(make_pair(crc, true));
  1917. MBugReport.releasewritelock();
  1918. return false;
  1919. }
  1920. #ifdef WIN32
  1921. ulong World::GetCurrentThreadID(){
  1922. return GetCurrentThreadId();
  1923. }
  1924. int64 World::GetThreadUsageCPUTime(){
  1925. HANDLE handle = GetCurrentThread();
  1926. int64 lpCreationTime;
  1927. int64 lpExitTime;
  1928. int64 lpKernelTime;
  1929. int64 lpUserTime;
  1930. if(GetThreadTimes(handle, (FILETIME*)&lpCreationTime, (FILETIME*)&lpExitTime, (FILETIME*)&lpKernelTime, (FILETIME*)&lpUserTime))
  1931. return lpKernelTime + lpUserTime;
  1932. return 0;
  1933. }
  1934. #else
  1935. #endif
  1936. void World::SyncCharacterAbilities(Client* client)
  1937. {
  1938. MStartingLists.readlock();
  1939. int8 baseClass = classes.GetBaseClass(client->GetPlayer()->GetAdventureClass());
  1940. int8 secondaryClass = classes.GetSecondaryBaseClass(client->GetPlayer()->GetAdventureClass());
  1941. int8 actualClass = client->GetPlayer()->GetAdventureClass();
  1942. int8 baseRace = client->GetPlayer()->GetRace();
  1943. bool reloadSpells = false;
  1944. multimap<int8, multimap<int8, StartingSkill>*>::iterator skill_itr = starting_skills.begin();
  1945. multimap<int8, multimap<int8, StartingSpell>*>::iterator spell_itr = starting_spells.begin();
  1946. bool isComplete = false;
  1947. int8 wait_iterations = 0; // wait 5 iterations and give up if db takes too long
  1948. do
  1949. {
  1950. bool isProcessing = false;
  1951. if (skill_itr != starting_skills.end())
  1952. {
  1953. isProcessing = true;
  1954. // race = 255 is wildcard all, otherwise needs to match the race id
  1955. if (skill_itr->first == 255 || skill_itr->first == baseRace)
  1956. {
  1957. multimap<int8, StartingSkill>::iterator child_itr;
  1958. for (child_itr = skill_itr->second->begin(); child_itr != skill_itr->second->end(); child_itr++)
  1959. {
  1960. // class = 255 is wildcard all, otherwise needs to match the class id
  1961. if (child_itr->first == 255 ||
  1962. child_itr->first == baseClass ||
  1963. child_itr->first == secondaryClass ||
  1964. child_itr->first == actualClass)
  1965. {
  1966. if (!client->GetPlayer()->skill_list.HasSkill(child_itr->second.skill_id))
  1967. {
  1968. Query query;
  1969. LogWrite(PLAYER__DEBUG, 0, "Player", "Adding skill %i for race: %i, class: %i for char_id: %u", child_itr->second.skill_id, baseRace, baseClass, client->GetCharacterID());
  1970. query.AddQueryAsync(client->GetCharacterID(), &database, Q_INSERT, "INSERT IGNORE INTO character_skills (char_id, skill_id, current_val, max_val) VALUES (%u, %u, %u, %u)",
  1971. client->GetCharacterID(), child_itr->second.skill_id, child_itr->second.current_val, child_itr->second.max_val);
  1972. client->GetPlayer()->AddSkill(child_itr->second.skill_id, child_itr->second.current_val, child_itr->second.max_val);
  1973. }
  1974. }
  1975. }
  1976. }
  1977. skill_itr++;
  1978. }
  1979. if (spell_itr != starting_spells.end())
  1980. {
  1981. isProcessing = true;
  1982. // race = 255 is wildcard all, otherwise needs to match the race id
  1983. if (spell_itr->first == 255 || spell_itr->first == baseRace)
  1984. {
  1985. multimap<int8, StartingSpell>::iterator child_itr;
  1986. for (child_itr = spell_itr->second->begin(); child_itr != spell_itr->second->end(); child_itr++)
  1987. {
  1988. // class = 255 is wildcard all, otherwise needs to match the class id
  1989. if (child_itr->first == 255 ||
  1990. child_itr->first == baseClass ||
  1991. child_itr->first == secondaryClass ||
  1992. child_itr->first == actualClass)
  1993. {
  1994. if (!client->GetPlayer()->HasSpell(child_itr->second.spell_id, child_itr->second.tier, true))
  1995. {
  1996. Query query;
  1997. LogWrite(PLAYER__DEBUG, 0, "Player", "Adding spell %i for race: %i, class: %i for char_id: %u", child_itr->second.spell_id, baseRace, baseClass, client->GetCharacterID());
  1998. // knowledge_slot is a signed int in the DB
  1999. query.AddQueryAsync(client->GetCharacterID(), &database, Q_INSERT, "INSERT IGNORE INTO character_spells (char_id, spell_id, tier, knowledge_slot) VALUES (%u, %u, %u, %i)",
  2000. client->GetCharacterID(), child_itr->second.spell_id, child_itr->second.tier, child_itr->second.knowledge_slot);
  2001. // reload spells, we don't know the spellbook or timer info
  2002. reloadSpells = true;
  2003. }
  2004. }
  2005. }
  2006. }
  2007. spell_itr++;
  2008. }
  2009. // poor mans async kill just in case, we don't want a client stuck here, they can get the spells on next zone
  2010. if (!isProcessing)
  2011. {
  2012. bool isDBActive = database.IsActiveQuery(client->GetCharacterID());
  2013. if (isDBActive && wait_iterations < 5)
  2014. {
  2015. wait_iterations++;
  2016. }
  2017. else
  2018. isComplete = true;
  2019. }
  2020. } while (!isComplete);
  2021. MStartingLists.releasereadlock();
  2022. if (reloadSpells)
  2023. database.LoadCharacterSpells(client->GetCharacterID(), client->GetPlayer());
  2024. }
  2025. void World::LoadStartingLists()
  2026. {
  2027. LogWrite(WORLD__DEBUG, 1, "World", "-Loading `starting_skills`...");
  2028. database.LoadStartingSkills(this);
  2029. LogWrite(WORLD__DEBUG, 1, "World", "-Loading `starting_spells`...");
  2030. database.LoadStartingSpells(this);
  2031. }
  2032. void World::PurgeStartingLists()
  2033. {
  2034. MStartingLists.writelock();
  2035. multimap<int8, multimap<int8, StartingSkill>*>::iterator skill_itr;
  2036. for (skill_itr = starting_skills.begin(); skill_itr != starting_skills.end(); skill_itr++)
  2037. {
  2038. multimap<int8, StartingSkill>* tmpMap = skill_itr->second;
  2039. safe_delete(tmpMap);
  2040. }
  2041. starting_skills.clear();
  2042. multimap<int8, multimap<int8, StartingSpell>*>::iterator spell_itr;
  2043. for (spell_itr = starting_spells.begin(); spell_itr != starting_spells.end(); spell_itr++)
  2044. {
  2045. multimap<int8, StartingSpell>* tmpMap = spell_itr->second;
  2046. safe_delete(tmpMap);
  2047. }
  2048. starting_spells.clear();
  2049. MStartingLists.releasewritelock();
  2050. }
  2051. void ZoneList::WatchdogHeartbeat()
  2052. {
  2053. list<ZoneServer*>::iterator zone_iter;
  2054. ZoneServer* tmp = 0;
  2055. MZoneList.writelock(__FUNCTION__, __LINE__);
  2056. bool match = false;
  2057. for (zone_iter = zlist.begin(); zone_iter != zlist.end(); zone_iter++)
  2058. {
  2059. tmp = *zone_iter;
  2060. if (tmp)
  2061. {
  2062. int32 curTime = Timer::GetCurrentTime2();
  2063. sint64 diff = (sint64)curTime - (sint64)tmp->GetWatchdogTime();
  2064. if (diff > 60000)
  2065. {
  2066. tmp->SetWatchdogTime(Timer::GetCurrentTime2()); // reset so we don't continuously flood this heartbeat
  2067. LogWrite(WORLD__ERROR, 1, "World", "Zone %s is hung for %i milliseconds.. attempting to cancel threads...", tmp->GetZoneName(), diff);
  2068. #ifndef WIN32
  2069. tmp->CancelThreads();
  2070. zlist.erase(zone_iter);
  2071. safe_delete(tmp);
  2072. #endif
  2073. MZoneList.releasewritelock(__FUNCTION__, __LINE__);
  2074. match = true;
  2075. break;
  2076. }
  2077. else if (diff > 30000 && !tmp->isZoneShuttingDown())
  2078. {
  2079. LogWrite(WORLD__ERROR, 1, "World", "Zone %s is hung for %i milliseconds.. attempting shutdown", tmp->GetZoneName(), diff);
  2080. tmp->Shutdown();
  2081. }
  2082. }
  2083. }
  2084. if(!match)
  2085. MZoneList.releasewritelock(__FUNCTION__, __LINE__);
  2086. }