World.cpp 91 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898
  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 "../common/Log.h"
  39. #include "Traits/Traits.h"
  40. #include "Chat/Chat.h"
  41. #include "Tradeskills/Tradeskills.h"
  42. #include "AltAdvancement/AltAdvancement.h"
  43. #include "LuaInterface.h"
  44. #include "HeroicOp/HeroicOp.h"
  45. #include "RaceTypes/RaceTypes.h"
  46. #include <boost/algorithm/string.hpp>
  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. Chat chat;
  78. extern ConfigReader configReader;
  79. extern LoginServer loginserver;
  80. extern World world;
  81. extern RuleManager rule_manager;
  82. 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) {
  83. save_time_timer.Start();
  84. time_tick_timer.Start();
  85. vitality_timer.Start();
  86. player_stats_timer.Start();
  87. server_stats_timer.Start();
  88. //remove_grouped_player.Start();
  89. guilds_timer.Start();
  90. lotto_players_timer.Start();
  91. watchdog_timer.Start();
  92. xp_rate = -1;
  93. ts_xp_rate = -1;
  94. vitality_frequency = 0xFFFFFFFF;
  95. vitality_amount = -1;
  96. last_checked_time = 0;
  97. items_loaded = false;
  98. spells_loaded = false;
  99. achievments_loaded = false;
  100. merchant_inventory_items.clear();
  101. MHouseZones.SetName("World::m_houseZones");
  102. MPlayerHouses.SetName("World::m_playerHouses");
  103. MWorldMaps.SetName("World::MWorldMaps");
  104. MWorldRegionMaps.SetName("World::MWorldRegionMaps");
  105. }
  106. World::~World(){
  107. // 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
  108. DeleteSpawns();
  109. if(database.GetStatus() == database.Connected)
  110. WriteServerStatistics();
  111. RemoveServerStatistics();
  112. DeleteMerchantsInfo();
  113. MutexMap<int32, LottoPlayer*>::iterator itr = lotto_players.begin();
  114. while (itr.Next())
  115. safe_delete(itr->second);
  116. map<int32, HouseZone*>::iterator itr2;
  117. for (itr2 = m_houseZones.begin(); itr2 != m_houseZones.end(); itr2++)
  118. safe_delete(itr2->second);
  119. m_houseZones.clear();
  120. tov_itemstat_conversion.clear();
  121. PurgeStartingLists();
  122. PurgeVoiceOvers();
  123. map<std::string, RegionMapRange*>::iterator itr3;
  124. for (itr3 = region_maps.begin(); itr3 != region_maps.end(); itr3++)
  125. safe_delete(itr3->second);
  126. map<std::string, MapRange*>::iterator itr4;
  127. for (itr4 = maps.begin(); itr4 != maps.end(); itr4++)
  128. safe_delete(itr4->second);
  129. PurgeNPCSpells();
  130. }
  131. void World::init(){
  132. WorldDatabase::next_id = database.GetMaxHotBarID();
  133. LogWrite(COMMAND__DEBUG, 1, "Command", "-Loading Commands...");
  134. database.LoadCommandList();
  135. LogWrite(COMMAND__DEBUG, 1, "Command", "-Load Commands complete!");
  136. LogWrite(FACTION__DEBUG, 1, "Faction", "-Loading Factions...");
  137. database.LoadFactionList();
  138. LogWrite(FACTION__DEBUG, 1, "Faction", "-Load Factions complete!...");
  139. LogWrite(SKILL__DEBUG, 1, "Skill", "-Loading Skills...");
  140. database.LoadSkills();
  141. LogWrite(SKILL__DEBUG, 1, "Skill", "-Load Skills complete...");
  142. LogWrite(WORLD__DEBUG, 1, "World", "-Loading `variables`...");
  143. database.LoadGlobalVariables();
  144. LogWrite(WORLD__DEBUG, 1, "World", "-Load `variables` complete!");
  145. LogWrite(WORLD__DEBUG, 1, "World", "-Loading `appearances`...");
  146. database.LoadAppearanceMasterList();
  147. LogWrite(WORLD__DEBUG, 1, "World", "-Load `appearances` complete!");
  148. LogWrite(WORLD__DEBUG, 1, "World", "-Loading `visual_states`...");
  149. database.LoadVisualStates();
  150. LogWrite(WORLD__DEBUG, 1, "World", "-Load `visual states` complete!");
  151. LoadStartingLists();
  152. LoadVoiceOvers();
  153. LogWrite(WORLD__DEBUG, 1, "World", "-Setting system parameters...");
  154. Variable* var = variables.FindVariable("gametime");
  155. const char* time_string = 0;
  156. char default_time[] = "0/0/3800 8:30";
  157. if(var)
  158. time_string = var->GetValue();
  159. if(!time_string)
  160. time_string = default_time;
  161. int year, month, day, hour, minute;
  162. sscanf (time_string, "%d/%d/%d %d:%d", &month, &day, &year, &hour, &minute);
  163. LogWrite(WORLD__DEBUG, 3, "World", "--Setting World Time to %s...", time_string);
  164. world_time.month = month;
  165. world_time.day = day;
  166. world_time.year = year;
  167. world_time.hour = hour;
  168. world_time.minute = minute;
  169. LogWrite(WORLD__DEBUG, 3, "World", "--Loading Vitality Information...");
  170. LoadVitalityInformation();
  171. LogWrite(WORLD__DEBUG, 3, "World", "--Loading Server Statistics...");
  172. database.LoadServerStatistics();
  173. LogWrite(WORLD__DEBUG, 3, "World", "--Setting Server Start Time...");
  174. UpdateServerStatistic(STAT_SERVER_START_TIME, Timer::GetUnixTimeStamp(), true);
  175. LogWrite(WORLD__DEBUG, 3, "World", "--Resetting Accepted Connections to 0...");
  176. UpdateServerStatistic(STAT_SERVER_ACCEPTED_CONNECTION, 0, true);
  177. LogWrite(WORLD__DEBUG, 3, "World", "--Resetting Active Zones to 0...");
  178. UpdateServerStatistic(STAT_SERVER_NUM_ACTIVE_ZONES, 0, true);
  179. // Clear all online players at server startup
  180. LogWrite(WORLD__DEBUG, 3, "World", "--Resetting characters online flags...");
  181. database.ToggleCharacterOnline();
  182. LogWrite(WORLD__DEBUG, 1, "World", "-Set system parameters complete!");
  183. LogWrite(RULESYS__DEBUG, 1, "Rules", "-Loading Rule Sets...");
  184. database.LoadRuleSets();
  185. LogWrite(RULESYS__DEBUG, 1, "Rules", "-Load Rule Sets complete!");
  186. LoadItemBlueStats();
  187. //PopulateTOVStatMap();
  188. group_buff_updates.Start(rule_manager.GetGlobalRule(R_Client, GroupSpellsTimer)->GetInt32());
  189. }
  190. PacketStruct* World::GetWorldTime(int16 version){
  191. MWorldTime.readlock(__FUNCTION__, __LINE__);
  192. PacketStruct* packet = configReader.getStruct("WS_GameWorldTime", version);
  193. if(packet){
  194. packet->setDataByName("year", world_time.year);
  195. packet->setDataByName("month", world_time.month);
  196. packet->setDataByName("day", world_time.day);
  197. packet->setDataByName("hour", world_time.hour);
  198. packet->setDataByName("minute", world_time.minute);
  199. packet->setDataByName("unknown", 250);
  200. packet->setDataByName("unix_time", Timer::GetUnixTimeStamp());
  201. packet->setDataByName("unknown2", 1);
  202. }
  203. MWorldTime.releasereadlock(__FUNCTION__, __LINE__);
  204. return packet;
  205. }
  206. float World::GetXPRate(){
  207. xp_rate = rule_manager.GetGlobalRule(R_Player, XPMultiplier)->GetFloat();
  208. LogWrite(WORLD__DEBUG, 0, "World", "Setting Global XP Rate to: %.2f", xp_rate);
  209. return xp_rate;
  210. }
  211. float World::GetTSXPRate(){
  212. ts_xp_rate = rule_manager.GetGlobalRule(R_Player, TSXPMultiplier)->GetFloat();
  213. LogWrite(WORLD__DEBUG, 0, "World", "Setting Global Tradeskill XP Rate to: %.2f", ts_xp_rate);
  214. return ts_xp_rate;
  215. }
  216. void World::Process(){
  217. if(last_checked_time > Timer::GetCurrentTime2())
  218. return;
  219. last_checked_time = Timer::GetCurrentTime2() + 1000;
  220. if(save_time_timer.Check())
  221. {
  222. MWorldTime.readlock(__FUNCTION__, __LINE__);
  223. database.SaveWorldTime(&world_time);
  224. MWorldTime.releasereadlock(__FUNCTION__, __LINE__);
  225. }
  226. if(time_tick_timer.Check())
  227. {
  228. MWorldTime.writelock(__FUNCTION__, __LINE__);
  229. WorldTimeTick();
  230. MWorldTime.releasewritelock(__FUNCTION__, __LINE__);
  231. }
  232. if(vitality_timer.Check())
  233. UpdateVitality();
  234. if (player_stats_timer.Check())
  235. WritePlayerStatistics();
  236. if (server_stats_timer.Check())
  237. WriteServerStatistics();
  238. /*if(remove_grouped_player.Check())
  239. CheckRemoveGroupedPlayer();*/
  240. if (group_buff_updates.Check())
  241. GetGroupManager()->UpdateGroupBuffs();
  242. if (guilds_timer.Check())
  243. SaveGuilds();
  244. if (lotto_players_timer.Check())
  245. CheckLottoPlayers();
  246. if(watchdog_timer.Check())
  247. zone_list.WatchdogHeartbeat();
  248. }
  249. vector<Variable*>* World::GetClientVariables(){
  250. return variables.GetVariables("cl_");
  251. }
  252. void World::LoadVitalityInformation()
  253. {
  254. int32 timestamp = Timer::GetUnixTimeStamp();
  255. int32 diff = 0;
  256. // fetch vitalitytimer value from `variables` table
  257. Variable* timer_var = variables.FindVariable("vitalitytimer");
  258. if(timer_var)
  259. {
  260. try
  261. {
  262. diff = timestamp - atoul(timer_var->GetValue());
  263. diff *= 1000; //convert seconds to milliseconds
  264. }
  265. catch(...)
  266. {
  267. LogWrite(WORLD__ERROR, 0, "World", "Error parsing vitalitytimer, value: %s", timer_var->GetValue());
  268. }
  269. }
  270. // Now using Rules System to set vitality parameters
  271. vitality_amount = rule_manager.GetGlobalRule(R_Player, VitalityAmount)->GetFloat();
  272. vitality_frequency = rule_manager.GetGlobalRule(R_Player, VitalityFrequency)->GetInt32();
  273. vitality_frequency *= 1000; //convert seconds to milliseconds
  274. if(diff >= vitality_frequency)
  275. UpdateVitality(); //update now
  276. else
  277. vitality_timer.SetTimer(vitality_frequency - diff);
  278. }
  279. void World::UpdateVitality()
  280. {
  281. // push new vitalitytimer to `variables` table
  282. database.UpdateVitality(Timer::GetUnixTimeStamp(), vitality_amount);
  283. if(vitality_timer.GetDuration() != vitality_frequency)
  284. vitality_timer.SetTimer(vitality_frequency);
  285. zone_list.UpdateVitality(vitality_amount);
  286. }
  287. void ZoneList::UpdateVitality(float amount)
  288. {
  289. list<ZoneServer*>::iterator zone_iter;
  290. ZoneServer* tmp = 0;
  291. MZoneList.readlock(__FUNCTION__, __LINE__);
  292. for(zone_iter=zlist.begin(); zone_iter!=zlist.end(); zone_iter++)
  293. {
  294. tmp = *zone_iter;
  295. if(tmp)
  296. tmp->UpdateVitality(amount);
  297. }
  298. MZoneList.releasereadlock(__FUNCTION__, __LINE__);
  299. }
  300. void ZoneList::SendTimeUpdate()
  301. {
  302. list<ZoneServer*>::iterator zone_iter;
  303. ZoneServer* tmp = 0;
  304. MZoneList.readlock(__FUNCTION__, __LINE__);
  305. for(zone_iter=zlist.begin(); zone_iter!=zlist.end(); zone_iter++)
  306. {
  307. tmp = *zone_iter;
  308. if(tmp && !tmp->isZoneShuttingDown())
  309. tmp->WorldTimeUpdateTrigger();
  310. }
  311. MZoneList.releasereadlock(__FUNCTION__, __LINE__);
  312. }
  313. // should already be ran inside MWorldTime
  314. void World::WorldTimeTick(){
  315. world_time.minute++;
  316. //I know it looks complicated, but the nested ifs are to avoid checking all of them every 3 seconds
  317. if(world_time.minute >= 60){ // >= in case of bad time from db
  318. world_time.minute = 0;
  319. world_time.hour++;
  320. if(world_time.hour >= 24){
  321. world_time.hour = 0;
  322. world_time.day++;
  323. if(world_time.day>=30){
  324. world_time.day = 0;
  325. world_time.month++;
  326. if(world_time.month >= 12){
  327. world_time.month = 0;
  328. world_time.year++;
  329. }
  330. }
  331. }
  332. }
  333. }
  334. ZoneList::ZoneList() {
  335. MZoneList.SetName("ZoneList::MZoneList");
  336. }
  337. ZoneList::~ZoneList() {
  338. list<ZoneServer*>::iterator zone_iter;
  339. ZoneServer* zs = 0;
  340. for(zone_iter=zlist.begin(); zone_iter!=zlist.end();){
  341. zs = *zone_iter;
  342. zone_iter = zlist.erase(zone_iter);
  343. safe_delete(zs);
  344. }
  345. }
  346. void ZoneList::CheckFriendList(Client* client) {
  347. LogWrite(WORLD__DEBUG, 0, "World", "Sending FriendList...");
  348. MClientList.lock();
  349. map<string,Client*>::iterator itr;
  350. for(itr = client_map.begin(); itr != client_map.end(); itr++){
  351. if(itr->second != client && itr->second){
  352. if(itr->second->GetPlayer()->IsFriend(client->GetPlayer()->GetName())){
  353. itr->second->SendFriendList();
  354. itr->second->Message(CHANNEL_COLOR_CHAT_RELATIONSHIP, "Friend: %s has logged in.", client->GetPlayer()->GetName());
  355. }
  356. }
  357. }
  358. MClientList.unlock();
  359. }
  360. void ZoneList::CheckFriendZoned(Client* client){
  361. MClientList.lock();
  362. map<string,Client*>::iterator itr;
  363. for(itr = client_map.begin(); itr != client_map.end(); itr++){
  364. if(itr->second != client && itr->second){
  365. if(itr->second->GetPlayer()->IsFriend(client->GetPlayer()->GetName())){
  366. itr->second->SendFriendList();
  367. }
  368. }
  369. }
  370. MClientList.unlock();
  371. }
  372. bool ZoneList::HandleGlobalChatMessage(Client* from, char* to, int16 channel, const char* message, const char* channel_name, int32 current_language_id){
  373. if (!from) {
  374. LogWrite(WORLD__ERROR, 0, "World", "HandleGlobalChatMessage() called with an invalid client");
  375. return false;
  376. }
  377. if(channel == CHANNEL_PRIVATE_TELL){
  378. Client* find_client = zone_list.GetClientByCharName(to);
  379. if(!find_client || find_client->GetPlayer()->IsIgnored(from->GetPlayer()->GetName()))
  380. return false;
  381. else if(find_client == from)
  382. {
  383. from->Message(CHANNEL_COLOR_RED,"You must be very lonely...(ERROR: Cannot send tell to self)");
  384. }
  385. else
  386. {
  387. const char* whoto = find_client->GetPlayer()->GetName();
  388. find_client->HandleTellMessage(from, message, whoto, from->GetPlayer()->GetCurrentLanguage());
  389. from->HandleTellMessage(from, message, whoto, from->GetPlayer()->GetCurrentLanguage());
  390. if (find_client->GetPlayer()->get_character_flag(CF_AFK)) {
  391. find_client->HandleTellMessage(find_client, find_client->GetPlayer()->GetAwayMessage().c_str(),whoto, from->GetPlayer()->GetCurrentLanguage());
  392. from->HandleTellMessage(find_client, find_client->GetPlayer()->GetAwayMessage().c_str(),whoto, from->GetPlayer()->GetCurrentLanguage());
  393. }
  394. }
  395. }
  396. else if(channel == CHANNEL_GROUP_SAY) {
  397. GroupMemberInfo* gmi = from->GetPlayer()->GetGroupMemberInfo();
  398. if(gmi)
  399. world.GetGroupManager()->GroupMessage(gmi->group_id, message);
  400. }
  401. else{
  402. list<ZoneServer*>::iterator zone_iter;
  403. ZoneServer* zs = 0;
  404. MZoneList.readlock(__FUNCTION__, __LINE__);
  405. for(zone_iter=zlist.begin(); zone_iter!=zlist.end();zone_iter++){
  406. zs = *zone_iter;
  407. if(zs)
  408. zs->HandleChatMessage(from->GetPlayer(), to, channel, message, 0, channel_name, true, current_language_id);
  409. }
  410. MZoneList.releasereadlock(__FUNCTION__, __LINE__);
  411. }
  412. return true;
  413. }
  414. void ZoneList::LoadSpellProcess(){
  415. list<ZoneServer*>::iterator zone_iter;
  416. ZoneServer* zone = 0;
  417. MZoneList.readlock(__FUNCTION__, __LINE__);
  418. for (zone_iter=zlist.begin(); zone_iter!=zlist.end();zone_iter++){
  419. zone = *zone_iter;
  420. if (zone)
  421. zone->LoadSpellProcess();
  422. }
  423. MZoneList.releasereadlock(__FUNCTION__, __LINE__);
  424. }
  425. void ZoneList::DeleteSpellProcess(){
  426. list<ZoneServer*>::iterator zone_iter;
  427. ZoneServer* zone = 0;
  428. MZoneList.readlock(__FUNCTION__, __LINE__);
  429. for (zone_iter=zlist.begin(); zone_iter!=zlist.end();zone_iter++){
  430. zone = *zone_iter;
  431. if (zone)
  432. zone->DeleteSpellProcess();
  433. }
  434. MZoneList.releasereadlock(__FUNCTION__, __LINE__);
  435. }
  436. void ZoneList::HandleGlobalBroadcast(const char* message) {
  437. list<ZoneServer*>::iterator zone_iter;
  438. ZoneServer* zone = 0;
  439. MZoneList.readlock(__FUNCTION__, __LINE__);
  440. for (zone_iter=zlist.begin(); zone_iter!=zlist.end();zone_iter++){
  441. zone = *zone_iter;
  442. if (zone)
  443. zone->HandleBroadcast(message);
  444. }
  445. MZoneList.releasereadlock(__FUNCTION__, __LINE__);
  446. }
  447. void ZoneList::HandleGlobalAnnouncement(const char* message) {
  448. list<ZoneServer*>::iterator zone_iter;
  449. ZoneServer* zone = 0;
  450. MZoneList.readlock(__FUNCTION__, __LINE__);
  451. for (zone_iter=zlist.begin(); zone_iter!=zlist.end();zone_iter++){
  452. zone = *zone_iter;
  453. if (zone)
  454. zone->HandleAnnouncement(message);
  455. }
  456. MZoneList.releasereadlock(__FUNCTION__, __LINE__);
  457. }
  458. int32 ZoneList::Count(){
  459. return zlist.size();
  460. }
  461. void ZoneList::Add(ZoneServer* zone) {
  462. MZoneList.writelock(__FUNCTION__, __LINE__);
  463. zlist.push_back(zone);
  464. MZoneList.releasewritelock(__FUNCTION__, __LINE__);
  465. }
  466. void ZoneList::Remove(ZoneServer* zone) {
  467. const char* zoneName = zone->GetZoneName();
  468. MZoneList.writelock(__FUNCTION__, __LINE__);
  469. zlist.remove(zone);
  470. MZoneList.releasewritelock(__FUNCTION__, __LINE__);
  471. ZoneServer* alternativeZone = Get(zoneName, false, false);
  472. if(!alternativeZone && !rule_manager.GetGlobalRule(R_World, MemoryCacheZoneMaps)->GetBool()) {
  473. world.RemoveMaps(std::string(zoneName));
  474. }
  475. }
  476. ZoneServer* ZoneList::Get(const char* zone_name, bool loadZone, bool skip_existing_zones) {
  477. list<ZoneServer*>::iterator zone_iter;
  478. ZoneServer* tmp = 0;
  479. ZoneServer* ret = 0;
  480. if(!skip_existing_zones) {
  481. MZoneList.readlock(__FUNCTION__, __LINE__);
  482. for(zone_iter=zlist.begin(); zone_iter!=zlist.end(); zone_iter++){
  483. tmp = *zone_iter;
  484. if (!tmp->isZoneShuttingDown() && !tmp->IsInstanceZone() && strlen(zone_name) == strlen(tmp->GetZoneName()) &&
  485. strncasecmp(tmp->GetZoneName(), zone_name, strlen(zone_name))==0){
  486. if(tmp->NumPlayers() < 30 || tmp->IsCityZone())
  487. ret = tmp;
  488. break;
  489. }
  490. }
  491. MZoneList.releasereadlock(__FUNCTION__, __LINE__);
  492. }
  493. if(!ret )
  494. {
  495. if ( loadZone )
  496. {
  497. ret = new ZoneServer(zone_name, true);
  498. database.LoadZoneInfo(ret);
  499. ret->Init();
  500. }
  501. }
  502. else if(ret && loadZone) {
  503. ret->IncrementIncomingClients();
  504. }
  505. return ret;
  506. }
  507. ZoneServer* ZoneList::Get(int32 id, bool loadZone, bool skip_existing_zones) {
  508. list<ZoneServer*>::iterator zone_iter;
  509. ZoneServer* tmp = 0;
  510. ZoneServer* ret = 0;
  511. if(!skip_existing_zones) {
  512. MZoneList.readlock(__FUNCTION__, __LINE__);
  513. for(zone_iter=zlist.begin(); zone_iter!=zlist.end(); zone_iter++){
  514. tmp = *zone_iter;
  515. if(!tmp->isZoneShuttingDown() && !tmp->IsInstanceZone() && tmp->GetZoneID() == id){
  516. if(tmp->NumPlayers() < 30 || tmp->IsCityZone())
  517. ret = tmp;
  518. break;
  519. }
  520. }
  521. MZoneList.releasereadlock(__FUNCTION__, __LINE__);
  522. }
  523. if(ret)
  524. tmp = ret;
  525. else if (loadZone) {
  526. string zonename = database.GetZoneName(id);
  527. if(zonename.length() >0){
  528. tmp = new ZoneServer(zonename.c_str());
  529. database.LoadZoneInfo(tmp);
  530. tmp->Init();
  531. }
  532. }
  533. return tmp;
  534. }
  535. void ZoneList::SendZoneList(Client* client) {
  536. list<ZoneServer*>::iterator zone_iter;
  537. ZoneServer* tmp = 0;
  538. MZoneList.readlock(__FUNCTION__, __LINE__);
  539. int zonesListed = 0;
  540. for(zone_iter=zlist.begin(); zone_iter!=zlist.end(); zone_iter++){
  541. tmp = *zone_iter;
  542. if ( zonesListed > 20 )
  543. {
  544. client->Message(CHANNEL_COLOR_YELLOW,"Reached max zone list of 20.");
  545. break;
  546. }
  547. zonesListed++;
  548. client->Message(CHANNEL_COLOR_YELLOW,"Zone(ID): %s(%i), Instance ID: %i, Description: %s.",tmp->GetZoneName(),tmp->GetZoneID(),
  549. tmp->GetInstanceID(),tmp->GetZoneDescription());
  550. }
  551. MZoneList.releasereadlock(__FUNCTION__, __LINE__);
  552. }
  553. ZoneServer* ZoneList::GetByInstanceID(int32 id, int32 zone_id, bool skip_existing_zones) {
  554. list<ZoneServer*>::iterator zone_iter;
  555. ZoneServer* tmp = 0;
  556. ZoneServer* ret = 0;
  557. if(!skip_existing_zones) {
  558. MZoneList.readlock(__FUNCTION__, __LINE__);
  559. if ( id > 0 )
  560. {
  561. for(zone_iter=zlist.begin(); zone_iter!=zlist.end(); zone_iter++){
  562. tmp = *zone_iter;
  563. if(tmp->GetInstanceID() == id){
  564. ret = tmp;
  565. break;
  566. }
  567. }
  568. }
  569. MZoneList.releasereadlock(__FUNCTION__, __LINE__);
  570. }
  571. if(ret)
  572. tmp = ret;
  573. else if ( zone_id > 0 ){
  574. string zonename = database.GetZoneName(zone_id);
  575. if(zonename.length() > 0){
  576. tmp = new ZoneServer(zonename.c_str());
  577. // the player is trying to preload an already existing instance but it isn't loaded
  578. if ( id > 0 )
  579. tmp->SetupInstance(id);
  580. database.LoadZoneInfo(tmp);
  581. tmp->Init();
  582. }
  583. }
  584. return tmp;
  585. }
  586. ZoneServer* ZoneList::GetByLowestPopulation(int32 zone_id) {
  587. ZoneServer* ret = 0;
  588. ZoneServer* zone = 0;
  589. int32 clients = 0;
  590. list<ZoneServer*>::iterator itr;
  591. MZoneList.readlock(__FUNCTION__, __LINE__);
  592. if (zone_id) {
  593. for (itr = zlist.begin(); itr != zlist.end(); itr++) {
  594. zone = *itr;
  595. if (zone) {
  596. // check the zone id's
  597. if (zone->GetZoneID() == zone_id) {
  598. // check this zones client count
  599. if (clients == 0 || zone->GetClientCount() < clients) {
  600. ret = zone;
  601. clients = zone->GetClientCount();
  602. }
  603. }
  604. }
  605. }
  606. }
  607. MZoneList.releasereadlock(__FUNCTION__, __LINE__);
  608. return ret;
  609. }
  610. bool ZoneList::ClientConnected(int32 account_id){
  611. bool ret = false;
  612. map<string, Client*>::iterator itr;
  613. MClientList.lock();
  614. for(itr=client_map.begin(); itr != client_map.end(); itr++){
  615. if(itr->second && itr->second->GetAccountID() == account_id && itr->second->getConnection() && itr->second->getConnection()->GetState() != CLOSING && itr->second->getConnection()->GetState() != CLOSED && (itr->second->GetPlayer()->GetActivityStatus() & ACTIVITY_STATUS_LINKDEAD) == 0){
  616. ret = true;
  617. break;
  618. }
  619. else if(!itr->second){
  620. client_map.erase(itr);
  621. if(client_map.size() > 0){
  622. itr=client_map.begin();
  623. if(itr == client_map.end()){
  624. if(itr->second && itr->second->GetAccountID() == account_id && (itr->second->GetPlayer()->GetActivityStatus() & ACTIVITY_STATUS_LINKDEAD) == 0)
  625. ret = true;
  626. break;
  627. }
  628. }
  629. else
  630. break;
  631. }
  632. }
  633. MClientList.unlock();
  634. return ret;
  635. }
  636. void ZoneList::RemoveClientZoneReference(ZoneServer* zone){
  637. map<string, Client*>::iterator itr;
  638. MClientList.lock();
  639. for(itr=client_map.begin(); itr != client_map.end(); itr++){
  640. if(itr->second) {
  641. if(itr->second->GetCurrentZone() == zone) {
  642. itr->second->SetCurrentZone(nullptr);
  643. }
  644. if(itr->second->GetZoningDestination() == zone) {
  645. itr->second->SetZoningDestination(nullptr);
  646. }
  647. }
  648. }
  649. MClientList.unlock();
  650. list<ZoneServer*>::iterator zone_iter;
  651. ZoneServer* tmp = 0;
  652. MZoneList.readlock(__FUNCTION__, __LINE__);
  653. for(zone_iter=zlist.begin(); zone_iter!=zlist.end(); zone_iter++){
  654. tmp = *zone_iter;
  655. if(tmp)
  656. tmp->RemoveClientsFromZone(zone);
  657. }
  658. MZoneList.releasereadlock(__FUNCTION__, __LINE__);
  659. }
  660. void ZoneList::ReloadClientQuests(){
  661. list<ZoneServer*>::iterator zone_iter;
  662. ZoneServer* tmp = 0;
  663. MZoneList.readlock(__FUNCTION__, __LINE__);
  664. for(zone_iter=zlist.begin(); zone_iter!=zlist.end(); zone_iter++){
  665. tmp = *zone_iter;
  666. if(tmp)
  667. tmp->ReloadClientQuests();
  668. }
  669. MZoneList.releasereadlock(__FUNCTION__, __LINE__);
  670. }
  671. void ZoneList::ProcessWhoQuery(vector<string>* queries, ZoneServer* zone, vector<Entity*>* players, bool isGM){
  672. Entity* player = 0;
  673. bool add_player = true;
  674. bool found_match = false;
  675. int8 lower = 0;
  676. int8 upper = 0;
  677. vector<Entity*> tmpPlayers;
  678. vector<Entity*>::iterator spawn_iter;
  679. if(!zone->isZoneShuttingDown()){
  680. tmpPlayers = zone->GetPlayers();
  681. for(spawn_iter = tmpPlayers.begin(); spawn_iter!=tmpPlayers.end(); spawn_iter++){
  682. player = *spawn_iter;
  683. add_player = true;
  684. Client* find_client = zone_list.GetClientByCharName(player->GetName());
  685. if (find_client == NULL) continue;
  686. int flags = find_client->GetPlayer()->GetInfoStruct()->get_flags();
  687. int flags2 = find_client->GetPlayer()->GetInfoStruct()->get_flags2();
  688. for(int32 i=0;add_player && queries && i<queries->size();i++){
  689. found_match = false;
  690. if(queries->at(i) == "ALL")
  691. continue;
  692. if(queries->at(i).length() > 3 && classes.GetClassID(queries->at(i).c_str()) > 0){
  693. if(player->GetAdventureClass() != classes.GetClassID(queries->at(i).c_str()))
  694. add_player = false;
  695. found_match = true;
  696. }
  697. else if(queries->at(i).length() > 2 && races.GetRaceID(queries->at(i).c_str()) > 0){
  698. if(player->GetRace() != races.GetRaceID(queries->at(i).c_str()))
  699. add_player = false;
  700. found_match = true;
  701. }
  702. if(!found_match && queries->at(i) == "GOOD"){
  703. if(player->GetDeity() != 1)
  704. add_player = false;
  705. found_match = true;
  706. }
  707. else if(!found_match && queries->at(i) == "EVIL"){
  708. if(player->GetDeity() == 1)
  709. add_player = false;
  710. found_match = true;
  711. }
  712. if((queries->at(i) == "GUIDE") && (find_client->GetAdminStatus() > 0) && ((find_client->GetAdminStatus() >> 4) < 5))
  713. found_match = true;
  714. else if((queries->at(i) == "GM") && ((find_client->GetAdminStatus() >> 4) > 4))
  715. found_match = true;
  716. else if((queries->at(i) == "LFG") && (flags & (1 << CF_LFG)))
  717. found_match = true;
  718. else if((queries->at(i) == "LFW") && (flags & (1 << CF_LFW)))
  719. found_match = true;
  720. else if((queries->at(i) == "ROLEPLAYING") && (flags & (1 << CF_ROLEPLAYING)))
  721. found_match = true;
  722. else if(strspn(queries->at(i).c_str(),"0123456789") == queries->at(i).length()){
  723. try{
  724. if(lower == 0)
  725. lower = atoi(queries->at(i).c_str());
  726. else
  727. upper = atoi(queries->at(i).c_str());
  728. }
  729. catch(...){}
  730. found_match = true;
  731. }
  732. if(!found_match){
  733. string name = string(player->GetName());
  734. name = ToUpper(name);
  735. if(name.find(queries->at(i)) == name.npos)
  736. add_player = false;
  737. }
  738. }
  739. if(lower > 0 && upper > 0){
  740. if(player->GetLevel() < lower || player->GetLevel() > upper)
  741. add_player = false;
  742. }
  743. else if(lower > 0){
  744. if(player->GetLevel() != lower)
  745. add_player = false;
  746. }
  747. if((flags2 & (1 << (CF_GM_HIDDEN - 32))) && !isGM) {
  748. add_player = false;
  749. found_match = true;
  750. }
  751. if(add_player)
  752. players->push_back(player);
  753. }
  754. }
  755. }
  756. void ZoneList::ProcessWhoQuery(const char* query, Client* client){
  757. list<ZoneServer*>::iterator zone_iter;
  758. vector<Entity*> players;
  759. vector<Entity*>::iterator spawn_iter;
  760. Entity* player = 0;
  761. //for now display all clients
  762. bool all = false;
  763. vector<string>* queries = 0;
  764. bool isGM = ((client->GetAdminStatus() >> 4) > 4);
  765. if(query){
  766. string query_string = string(query);
  767. query_string = ToUpper(query_string);
  768. queries = SplitString(query_string, ' ');
  769. }
  770. if(queries && queries->size() > 0 && queries->at(0) == "ALL")
  771. all = true;
  772. if(all){
  773. MZoneList.readlock(__FUNCTION__, __LINE__);
  774. for(zone_iter=zlist.begin(); zone_iter!=zlist.end(); zone_iter++){
  775. ZoneServer* tmp = *zone_iter;
  776. ProcessWhoQuery(queries, tmp, &players, isGM);
  777. }
  778. MZoneList.releasereadlock(__FUNCTION__, __LINE__);
  779. }
  780. else{
  781. ProcessWhoQuery(queries, client->GetCurrentZone(), &players, isGM);
  782. }
  783. PacketStruct* packet = configReader.getStruct("WS_WhoQueryReply", client->GetVersion());
  784. if(packet){
  785. packet->setDataByName("account_id", client->GetAccountID());
  786. packet->setDataByName("unknown", 0xFFFFFFFF);
  787. int8 num_characters = players.size();
  788. int8 max_who_results = 10;
  789. int8 max_who_results_status_override = 100;
  790. Variable* var = variables.FindVariable("max_who_results_status_override");
  791. if ( var ){
  792. max_who_results_status_override = atoi(var->GetValue());
  793. }
  794. // AdnaeDMorte
  795. if ( client->GetAdminStatus() >= max_who_results_status_override ){
  796. client->Message(CHANNEL_COLOR_RED, "** ADMIN-MODE ** ");
  797. }
  798. Variable* var1 = variables.FindVariable("max_who_results");
  799. if ( var1 ){
  800. max_who_results = atoi(var1->GetValue());
  801. }
  802. if(num_characters > max_who_results && client->GetAdminStatus() < max_who_results_status_override){
  803. num_characters = max_who_results;
  804. packet->setDataByName("response", 3); //response 1 = error message, 3 == capped
  805. }
  806. else
  807. packet->setDataByName("response", 2);
  808. packet->setArrayLengthByName("num_characters", num_characters);
  809. packet->setDataByName("unknown10", 1);
  810. int i=0;
  811. for(spawn_iter = players.begin(); spawn_iter!=players.end(); spawn_iter++, i++){
  812. if(i == num_characters)
  813. break;
  814. player = *spawn_iter;
  815. Client* find_client = zone_list.GetClientByCharName(player->GetName());
  816. int flags = find_client->GetPlayer()->GetInfoStruct()->get_flags();
  817. int flags2 = find_client->GetPlayer()->GetInfoStruct()->get_flags2();
  818. packet->setArrayDataByName("char_name", player->GetName(), i);
  819. packet->setArrayDataByName("level", player->GetLevel(), i);
  820. packet->setArrayDataByName("admin_level", ((flags2 & (1 << (CF_HIDE_STATUS - 32))) && !isGM)?0:(find_client->GetAdminStatus() >> 4), i);
  821. packet->setArrayDataByName("class", player->GetAdventureClass(), i);
  822. packet->setArrayDataByName("unknown4", 0xFF, i); //probably tradeskill class
  823. packet->setArrayDataByName("flags", (((flags >> CF_ANONYMOUS) & 1) << 0 ) |
  824. (((flags >> CF_LFG) & 1) << 1 ) |
  825. (((flags >> CF_ANONYMOUS) & 1) << 2 ) |
  826. /*(((flags >> CF_HIDDEN) & 1) << 3 ) |*/
  827. (((flags >> CF_ROLEPLAYING) & 1) << 4 ) |
  828. (((flags >> CF_AFK) & 1) << 5 ) |
  829. (((flags >> CF_LFW) & 1) << 6 ) /*|
  830. (((flags >> CF_NOTA) & 1) << 7 )*/, i);
  831. packet->setArrayDataByName("race", player->GetRace(), i);
  832. if(player->GetZone() && player->GetZone()->GetZoneDescription())
  833. packet->setArrayDataByName("zone", player->GetZone()->GetZoneDescription(), i);
  834. if(player->appearance.sub_title) {
  835. int32 sub_title_length = strlen(player->appearance.sub_title);
  836. char tmp_title[255];
  837. int32 index = 0;
  838. int32 index_tmp = 0;
  839. while (index < sub_title_length) {
  840. if (player->appearance.sub_title[index] != '<' && player->appearance.sub_title[index] != '>') {
  841. memcpy(tmp_title + index_tmp, player->appearance.sub_title + index, 1);
  842. index_tmp++;
  843. }
  844. index++;
  845. }
  846. tmp_title[index_tmp] = '\0';
  847. packet->setArrayDataByName("guild", tmp_title, i);
  848. }
  849. }
  850. client->QueuePacket(packet->serialize());
  851. safe_delete(packet);
  852. }
  853. }
  854. bool ZoneList::DepopFinished(){
  855. list<ZoneServer*>::iterator zone_iter;
  856. MZoneList.readlock(__FUNCTION__, __LINE__);
  857. bool finished_depop = true;
  858. for(zone_iter=zlist.begin(); zone_iter!=zlist.end(); zone_iter++){
  859. if(!(*zone_iter)->FinishedDepop())
  860. finished_depop = false;
  861. }
  862. MZoneList.releasereadlock(__FUNCTION__, __LINE__);
  863. return finished_depop;
  864. }
  865. void ZoneList::Depop(){
  866. list<ZoneServer*>::iterator zone_iter;
  867. MZoneList.readlock(__FUNCTION__, __LINE__);
  868. for(zone_iter=zlist.begin(); zone_iter!=zlist.end(); zone_iter++){
  869. (*zone_iter)->Depop();
  870. }
  871. MZoneList.releasereadlock(__FUNCTION__, __LINE__);
  872. }
  873. void ZoneList::Repop(){
  874. list<ZoneServer*>::iterator zone_iter;
  875. MZoneList.readlock(__FUNCTION__, __LINE__);
  876. for(zone_iter=zlist.begin(); zone_iter!=zlist.end(); zone_iter++){
  877. (*zone_iter)->Depop(false, true);
  878. }
  879. MZoneList.releasereadlock(__FUNCTION__, __LINE__);
  880. }
  881. void ZoneList::ReloadSpawns() {
  882. MZoneList.readlock(__FUNCTION__, __LINE__);
  883. list<ZoneServer*>::iterator itr;
  884. for (itr = zlist.begin(); itr != zlist.end(); itr++)
  885. (*itr)->ReloadSpawns();
  886. MZoneList.releasereadlock(__FUNCTION__, __LINE__);
  887. }
  888. bool World::ReportBug(string data, char* player_name, int32 account_id, const char* spawn_name, int32 spawn_id, int32 zone_id){
  889. //loginserver
  890. vector<string> list;
  891. int32 offset = 0;
  892. int32 old_offset = 0;
  893. while((offset = data.find(7, old_offset+1)) < 0xFFFFFFFF){
  894. if(old_offset > 0)
  895. list.push_back(data.substr(old_offset+1, offset-old_offset-1));
  896. else
  897. list.push_back(data.substr(old_offset, offset));
  898. old_offset = offset;
  899. }
  900. if(list.size() > 0 && list.size() < 7){
  901. string output = "Invalid bug list:\n";
  902. for(int32 i=0;i<list.size();i++)
  903. output = output.append("\t").append(list[i]).append("\n");
  904. LogWrite(WORLD__ERROR, 0, "World", "%s", output.c_str());
  905. return false;
  906. }
  907. ServerPacket* outpack = new ServerPacket(ServerOP_BugReport, sizeof(BugReport));
  908. BugReport* report = (BugReport*)outpack->pBuffer;
  909. if (list.size() < 7) {
  910. strncpy(report->category, "AutoBug", 7);
  911. strncpy(report->subcategory, "AutoGenerate", 12);
  912. strncpy(report->causes_crash, "N", 1);
  913. strncpy(report->reproducible, "Y", 1);
  914. strncpy(report->summary, data.c_str(), data.length() > 127 ? 127 : data.length());
  915. strncpy(report->description, data.c_str(), data.length() > 1999 ? 1999 : data.length());
  916. strncpy(report->version, "CUR", 3);
  917. }
  918. else
  919. {
  920. strncpy(report->category, list[0].c_str(), list[0].length() > 63 ? 63 : list[0].length());
  921. strncpy(report->subcategory, list[1].c_str(), list[1].length() > 63 ? 63 : list[1].length());
  922. strncpy(report->causes_crash, list[2].c_str(), list[2].length() > 63 ? 63 : list[2].length());
  923. strncpy(report->reproducible, list[3].c_str(), list[3].length() > 63 ? 63 : list[3].length());
  924. strncpy(report->summary, list[4].c_str(), list[4].length() > 127 ? 127 : list[4].length());
  925. strncpy(report->description, list[5].c_str(), list[5].length() > 1999 ? 1999 : list[5].length());
  926. strncpy(report->version, list[6].c_str(), list[6].length() > 31 ? 31 : list[6].length());
  927. }
  928. strncpy(report->player, player_name, strlen(player_name) > 63 ? 63 : strlen(player_name));
  929. strncpy(report->spawn_name, spawn_name, strlen(spawn_name) > 63 ? 63 : strlen(spawn_name));
  930. report->spawn_id = spawn_id;
  931. report->account_id = account_id;
  932. report->zone_id = zone_id;
  933. loginserver.SendPacket(outpack);
  934. 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);
  935. safe_delete(outpack);
  936. return true;
  937. }
  938. void ZoneList::WritePlayerStatistics() {
  939. list<ZoneServer*>::iterator zone_itr;
  940. MZoneList.readlock(__FUNCTION__, __LINE__);
  941. for (zone_itr = zlist.begin(); zone_itr != zlist.end(); zone_itr++)
  942. (*zone_itr)->WritePlayerStatistics();
  943. MZoneList.releasereadlock(__FUNCTION__, __LINE__);
  944. }
  945. void ZoneList::ShutDownZones(){
  946. LogWrite(WORLD__INFO, 0, "World", "Shutting down all zones, please wait...");
  947. list<ZoneServer*>::iterator zone_itr;
  948. int32 size = 0;
  949. MZoneList.readlock(__FUNCTION__, __LINE__);
  950. for (zone_itr = zlist.begin(); zone_itr != zlist.end(); zone_itr++){
  951. (*zone_itr)->Shutdown();
  952. }
  953. size = zlist.size();
  954. MZoneList.releasereadlock(__FUNCTION__, __LINE__);
  955. while(size > 0){
  956. Sleep(10);
  957. MZoneList.readlock(__FUNCTION__, __LINE__);
  958. size = zlist.size();
  959. MZoneList.releasereadlock(__FUNCTION__, __LINE__);
  960. }
  961. LogWrite(WORLD__INFO, 0, "World", "Zone shutdown complete");
  962. }
  963. void ZoneList::ReloadMail() {
  964. map<string, Client*>::iterator itr;
  965. MClientList.writelock(__FUNCTION__, __LINE__);
  966. for (itr = client_map.begin(); itr != client_map.end(); itr++) {
  967. itr->second->GetPlayer()->DeleteMail();
  968. database.LoadPlayerMail(itr->second);
  969. }
  970. MClientList.releasewritelock(__FUNCTION__, __LINE__);
  971. }
  972. void World::AddSpawnScript(int32 id, const char* name){
  973. MSpawnScripts.lock();
  974. if(name)
  975. spawn_scripts[id] = string(name);
  976. MSpawnScripts.unlock();
  977. }
  978. void World::AddSpawnEntryScript(int32 id, const char* name){
  979. MSpawnScripts.lock();
  980. if(name)
  981. spawnentry_scripts[id] = string(name);
  982. MSpawnScripts.unlock();
  983. }
  984. void World::AddSpawnLocationScript(int32 id, const char* name){
  985. MSpawnScripts.lock();
  986. if(name)
  987. spawnlocation_scripts[id] = string(name);
  988. MSpawnScripts.unlock();
  989. }
  990. void World::AddZoneScript(int32 id, const char* name) {
  991. MZoneScripts.lock();
  992. if (name)
  993. zone_scripts[id] = string(name);
  994. MZoneScripts.unlock();
  995. }
  996. const char* World::GetSpawnScript(int32 id){
  997. LogWrite(SPAWN__TRACE, 1, "Spawn", "Enter %s", __FUNCTION__);
  998. const char* ret = 0;
  999. MSpawnScripts.lock();
  1000. if(spawn_scripts.count(id) > 0)
  1001. ret = spawn_scripts[id].c_str();
  1002. MSpawnScripts.unlock();
  1003. LogWrite(SPAWN__TRACE, 1, "Spawn", "Exit %s", __FUNCTION__);
  1004. return ret;
  1005. }
  1006. const char* World::GetSpawnEntryScript(int32 id){
  1007. LogWrite(SPAWN__TRACE, 1, "Spawn", "Enter %s", __FUNCTION__);
  1008. const char* ret = 0;
  1009. MSpawnScripts.lock();
  1010. if(spawnentry_scripts.count(id) > 0)
  1011. ret = spawnentry_scripts[id].c_str();
  1012. MSpawnScripts.unlock();
  1013. LogWrite(SPAWN__TRACE, 1, "Spawn", "Exit %s", __FUNCTION__);
  1014. return ret;
  1015. }
  1016. const char* World::GetSpawnLocationScript(int32 id){
  1017. LogWrite(SPAWN__TRACE, 1, "Spawn", "Enter %s", __FUNCTION__);
  1018. const char* ret = 0;
  1019. MSpawnScripts.lock();
  1020. if(spawnlocation_scripts.count(id) > 0)
  1021. ret = spawnlocation_scripts[id].c_str();
  1022. MSpawnScripts.unlock();
  1023. LogWrite(SPAWN__TRACE, 1, "Spawn", "Exit %s", __FUNCTION__);
  1024. return ret;
  1025. }
  1026. const char* World::GetZoneScript(int32 id) {
  1027. const char* ret = 0;
  1028. MZoneScripts.lock();
  1029. if (zone_scripts.count(id) > 0)
  1030. ret = zone_scripts[id].c_str();
  1031. MZoneScripts.unlock();
  1032. return ret;
  1033. }
  1034. void World::ResetSpawnScripts(){
  1035. MSpawnScripts.lock();
  1036. spawn_scripts.clear();
  1037. spawnentry_scripts.clear();
  1038. spawnlocation_scripts.clear();
  1039. MSpawnScripts.unlock();
  1040. }
  1041. void World::ResetZoneScripts() {
  1042. MZoneScripts.lock();
  1043. zone_scripts.clear();
  1044. MZoneScripts.unlock();
  1045. }
  1046. vector<MerchantItemInfo>* World::GetMerchantItemList(int32 merchant_id, int8 merchant_type, Player* player)
  1047. {
  1048. vector<MerchantItemInfo>* ret = 0;
  1049. MMerchantList.lock();
  1050. if(merchant_info.count(merchant_id) > 0)
  1051. {
  1052. MerchantInfo* info = merchant_info[merchant_id];
  1053. vector<MerchantItemInfo>::iterator itr;
  1054. int32 inventory_id = 0;
  1055. Item* item = 0;
  1056. for(int i=info->inventory_ids.size()-1;i>=0;i--)
  1057. {
  1058. inventory_id = info->inventory_ids[i];
  1059. if(merchant_inventory_items.count(inventory_id) > 0)
  1060. {
  1061. for(itr = merchant_inventory_items[inventory_id].begin(); itr != merchant_inventory_items[inventory_id].end(); itr++)
  1062. {
  1063. if(!ret)
  1064. ret = new vector<MerchantItemInfo>;
  1065. item = master_item_list.GetItem((*itr).item_id);
  1066. // if NOT spell merchant, OR
  1067. // skill req is any skill, OR player has the skill, AND
  1068. // skill req2 is any skill, OR player has the skill2
  1069. 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)) ) ) )
  1070. (*ret).push_back(*itr);
  1071. }
  1072. }
  1073. }
  1074. }
  1075. MMerchantList.unlock();
  1076. return ret;
  1077. }
  1078. vector<MerchantItemInfo>* World::GetMerchantList(int32 merchant_id){
  1079. vector<MerchantItemInfo>* ret = 0;
  1080. MMerchantList.lock();
  1081. if(merchant_info.count(merchant_id) > 0){
  1082. MerchantInfo* info = merchant_info[merchant_id];
  1083. map<int32, int16>::iterator itr;
  1084. int32 inventory_id = 0;
  1085. for(int i=info->inventory_ids.size()-1;i>=0;i--){
  1086. inventory_id = info->inventory_ids[i];
  1087. if(merchant_inventory_items.count(inventory_id) > 0){
  1088. ret = &merchant_inventory_items[inventory_id];
  1089. }
  1090. }
  1091. }
  1092. MMerchantList.unlock();
  1093. return ret;
  1094. }
  1095. void World::AddMerchantItem(int32 inventory_id, MerchantItemInfo ItemInfo){
  1096. MMerchantList.lock();
  1097. merchant_inventory_items[inventory_id].push_back(ItemInfo);
  1098. MMerchantList.unlock();
  1099. }
  1100. void World::DeleteMerchantItems(){
  1101. MMerchantList.lock();
  1102. merchant_inventory_items.clear();
  1103. MMerchantList.unlock();
  1104. }
  1105. void World::RemoveMerchantItem(int32 inventory_id, int32 item_id){
  1106. MMerchantList.lock();
  1107. if(merchant_inventory_items.count(inventory_id) > 0) {
  1108. vector<MerchantItemInfo>::iterator itr;
  1109. for(itr = merchant_inventory_items[inventory_id].begin(); itr != merchant_inventory_items[inventory_id].end(); itr++){
  1110. if ((*itr).item_id == item_id) {
  1111. merchant_inventory_items[inventory_id].erase(itr);
  1112. break;
  1113. }
  1114. }
  1115. }
  1116. MMerchantList.unlock();
  1117. }
  1118. int16 World::GetMerchantItemQuantity(int32 merchant_id, int32 item_id){
  1119. int16 amount = 0;
  1120. int32 inventory_id = GetInventoryID(merchant_id, item_id);
  1121. if(inventory_id > 0){
  1122. MMerchantList.lock();
  1123. vector<MerchantItemInfo>::iterator itr;
  1124. for(itr = merchant_inventory_items[inventory_id].begin(); itr != merchant_inventory_items[inventory_id].end(); itr++){
  1125. if ((*itr).item_id == item_id)
  1126. amount = (*itr).quantity;
  1127. }
  1128. MMerchantList.unlock();
  1129. }
  1130. return amount;
  1131. }
  1132. int32 World::GetInventoryID(int32 merchant_id, int32 item_id){
  1133. int32 ret = 0;
  1134. MMerchantList.lock();
  1135. if(merchant_info.count(merchant_id) > 0){
  1136. MerchantInfo* info = merchant_info[merchant_id];
  1137. vector<MerchantItemInfo>::iterator itr;
  1138. int32 inventory_id = 0;
  1139. for(int i=info->inventory_ids.size()-1;i>=0;i--){
  1140. inventory_id = info->inventory_ids[i];
  1141. if(merchant_inventory_items.count(inventory_id) > 0){
  1142. for(itr = merchant_inventory_items[inventory_id].begin(); itr != merchant_inventory_items[inventory_id].end(); itr++){
  1143. if((*itr).item_id == item_id){
  1144. ret = inventory_id;
  1145. break;
  1146. }
  1147. }
  1148. if(ret > 0)
  1149. break;
  1150. }
  1151. }
  1152. }
  1153. MMerchantList.unlock();
  1154. return ret;
  1155. }
  1156. void World::DecreaseMerchantQuantity(int32 merchant_id, int32 item_id, int16 amount){
  1157. int16 total_left = GetMerchantItemQuantity(merchant_id, item_id);
  1158. if(total_left > 0 && total_left < 0xFF){
  1159. int32 inventory_id = GetInventoryID(merchant_id, item_id);
  1160. if(inventory_id > 0){
  1161. MMerchantList.lock();
  1162. vector<MerchantItemInfo>::iterator itr;
  1163. for(itr = merchant_inventory_items[inventory_id].begin(); itr != merchant_inventory_items[inventory_id].end(); itr++){
  1164. if ((*itr).item_id == item_id) {
  1165. if(total_left <= amount) {
  1166. merchant_inventory_items[inventory_id].erase(itr);
  1167. amount = 0;
  1168. break;
  1169. }
  1170. else
  1171. (*itr).quantity -= amount;
  1172. amount = (*itr).quantity;
  1173. }
  1174. }
  1175. MMerchantList.unlock();
  1176. }
  1177. }
  1178. }
  1179. MerchantInfo* World::GetMerchantInfo(int32 merchant_id){
  1180. MerchantInfo* ret = 0;
  1181. MMerchantList.lock();
  1182. if(merchant_info.count(merchant_id) > 0)
  1183. ret = merchant_info[merchant_id];
  1184. MMerchantList.unlock();
  1185. return ret;
  1186. }
  1187. void World::AddMerchantInfo(int32 merchant_id, MerchantInfo* info){
  1188. MMerchantList.lock();
  1189. if(merchant_info.count(merchant_id) > 0){
  1190. safe_delete(merchant_info[merchant_id]);
  1191. }
  1192. merchant_info[merchant_id] = info;
  1193. MMerchantList.unlock();
  1194. }
  1195. map<int32, MerchantInfo*>* World::GetMerchantInfo() {
  1196. return &merchant_info;
  1197. }
  1198. void World::DeleteMerchantsInfo(){
  1199. MMerchantList.lock();
  1200. map<int32, MerchantInfo*>::iterator itr;
  1201. for(itr = merchant_info.begin(); itr != merchant_info.end(); itr++){
  1202. safe_delete(itr->second);
  1203. }
  1204. merchant_info.clear();
  1205. MMerchantList.unlock();
  1206. }
  1207. void World::DeleteSpawns(){
  1208. //reloading = true;
  1209. //ClearLootTables();
  1210. /*
  1211. map<int32, NPC*>::iterator npc_list_iter;
  1212. for(npc_list_iter=npc_list.begin();npc_list_iter!=npc_list.end();npc_list_iter++) {
  1213. safe_delete(npc_list_iter->second);
  1214. }
  1215. npc_list.clear();
  1216. map<int32, Object*>::iterator object_list_iter;
  1217. for(object_list_iter=object_list.begin();object_list_iter!=object_list.end();object_list_iter++) {
  1218. safe_delete(object_list_iter->second);
  1219. }
  1220. object_list.clear();
  1221. map<int32, GroundSpawn*>::iterator groundspawn_list_iter;
  1222. for(groundspawn_list_iter=groundspawn_list.begin();groundspawn_list_iter!=groundspawn_list.end();groundspawn_list_iter++) {
  1223. safe_delete(groundspawn_list_iter->second);
  1224. }
  1225. groundspawn_list.clear();
  1226. map<int32, Widget*>::iterator widget_list_iter;
  1227. for(widget_list_iter=widget_list.begin();widget_list_iter!=widget_list.end();widget_list_iter++) {
  1228. safe_delete(widget_list_iter->second);
  1229. }
  1230. widget_list.clear();
  1231. map<int32, Sign*>::iterator sign_list_iter;
  1232. for(sign_list_iter=sign_list.begin();sign_list_iter!=sign_list.end();sign_list_iter++) {
  1233. safe_delete(sign_list_iter->second);
  1234. }
  1235. sign_list.clear();*/
  1236. map<int32, AppearanceData*>::iterator appearance_list_iter;
  1237. for(appearance_list_iter=npc_appearance_list.begin();appearance_list_iter!=npc_appearance_list.end();appearance_list_iter++) {
  1238. safe_delete(appearance_list_iter->second);
  1239. }
  1240. npc_appearance_list.clear();
  1241. /*
  1242. map<int32, vector<EntityCommand*>* >::iterator command_list_iter;
  1243. for(command_list_iter=entity_command_list.begin();command_list_iter!=entity_command_list.end();command_list_iter++) {
  1244. vector<EntityCommand*>* v = command_list_iter->second;
  1245. if(v){
  1246. for(int32 i=0;i<v->size();i++){
  1247. safe_delete(v->at(i));
  1248. }
  1249. safe_delete(v);
  1250. }
  1251. }
  1252. entity_command_list.clear();
  1253. */
  1254. //DeleteGroundSpawnItems();
  1255. //DeleteTransporters();
  1256. //DeleteTransporterMaps();
  1257. }
  1258. void World::ReloadGuilds() {
  1259. guild_list.GetGuilds()->clear(true);
  1260. database.LoadGuilds();
  1261. }
  1262. int8 World::GetClassID(const char* name){
  1263. return classes.GetClassID(name);
  1264. }
  1265. void World::WritePlayerStatistics() {
  1266. zone_list.WritePlayerStatistics();
  1267. }
  1268. void World::WriteServerStatistics() {
  1269. map<int32, Statistic*>::iterator itr;
  1270. Statistic* stat = 0;
  1271. for (itr = server_statistics.begin(); itr != server_statistics.end(); itr++) {
  1272. stat = itr->second;
  1273. if (stat->save_needed) {
  1274. stat->save_needed = false;
  1275. database.WriteServerStatistic(stat);
  1276. }
  1277. }
  1278. database.WriteServerStatisticsNeededQueries();
  1279. }
  1280. void World::AddServerStatistic(int32 stat_id, sint32 stat_value, int32 stat_date) {
  1281. if (server_statistics.count(stat_id) == 0) {
  1282. Statistic* stat = new Statistic;
  1283. stat->stat_id = stat_id;
  1284. stat->stat_value = stat_value;
  1285. stat->stat_date = stat_date;
  1286. stat->save_needed = false;
  1287. server_statistics[stat_id] = stat;
  1288. }
  1289. }
  1290. void World::UpdateServerStatistic(int32 stat_id, sint32 stat_value, bool overwrite) {
  1291. if (server_statistics.count(stat_id) == 0)
  1292. AddServerStatistic(stat_id, stat_value, 0);
  1293. Statistic* stat = server_statistics[stat_id];
  1294. overwrite == true ? stat->stat_value = stat_value : stat->stat_value += stat_value;
  1295. stat->stat_date = Timer::GetUnixTimeStamp();
  1296. stat->save_needed = true;
  1297. }
  1298. sint32 World::GetServerStatisticValue(int32 stat_id) {
  1299. if (server_statistics.count(stat_id) > 0)
  1300. return server_statistics[stat_id]->stat_value;
  1301. return 0;
  1302. }
  1303. void World::RemoveServerStatistics() {
  1304. map<int32, Statistic*>::iterator stat_itr;
  1305. for (stat_itr = server_statistics.begin(); stat_itr != server_statistics.end(); stat_itr++)
  1306. safe_delete(stat_itr->second);
  1307. server_statistics.clear();
  1308. }
  1309. void World::SendGroupQuests(PlayerGroup* group, Client* client){
  1310. return;
  1311. /*if(!group)
  1312. return;
  1313. GroupMemberInfo* info = 0;
  1314. MGroups.readlock(__FUNCTION__, __LINE__);
  1315. deque<GroupMemberInfo*>::iterator itr;
  1316. for(itr = group->members.begin(); itr != group->members.end(); itr++){
  1317. info = *itr;
  1318. if(info->client){
  1319. LogWrite(PLAYER__DEBUG, 0, "Player", "Send Quest Journal...");
  1320. info->client->SendQuestJournal(false, client);
  1321. client->SendQuestJournal(false, info->client);
  1322. }
  1323. }
  1324. MGroups.releasereadlock(__FUNCTION__, __LINE__);*/
  1325. }
  1326. /*void World::CheckRemoveGroupedPlayer(){
  1327. map<GroupMemberInfo*, int32>::iterator itr;
  1328. GroupMemberInfo* found = 0;
  1329. MGroups.readlock(__FUNCTION__, __LINE__);
  1330. for(itr = group_removal_pending.begin(); itr != group_removal_pending.end(); itr++){
  1331. if(itr->second < Timer::GetCurrentTime2()){
  1332. found = itr->first;
  1333. break;
  1334. }
  1335. }
  1336. MGroups.releasereadlock(__FUNCTION__, __LINE__);
  1337. if(found){
  1338. if(!found->client || (found->client && found->client->IsConnected() == false))
  1339. DeleteGroupMember(found);
  1340. else{
  1341. MGroups.writelock(__FUNCTION__, __LINE__);
  1342. group_removal_pending.erase(found);
  1343. MGroups.releasewritelock(__FUNCTION__, __LINE__);
  1344. }
  1345. }
  1346. }*/
  1347. bool World::RejoinGroup(Client* client, int32 group_id){
  1348. if (!group_id) // no need if no group id!
  1349. return false;
  1350. world.GetGroupManager()->GroupLock(__FUNCTION__, __LINE__);
  1351. PlayerGroup* group = world.GetGroupManager()->GetGroup(group_id);
  1352. deque<GroupMemberInfo*>* members = 0;
  1353. if (group)
  1354. members = group->GetMembers();
  1355. string name = string(client->GetPlayer()->GetName());
  1356. if (!members)
  1357. {
  1358. // group does not exist!
  1359. Query query;
  1360. query.AddQueryAsync(client->GetCharacterID(), &database, Q_INSERT, "UPDATE characters set group_id = 0 where id = %u",
  1361. client->GetCharacterID());
  1362. 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);
  1363. world.GetGroupManager()->ReleaseGroupLock(__FUNCTION__, __LINE__);
  1364. return false;
  1365. }
  1366. deque<GroupMemberInfo*>::iterator itr;
  1367. GroupMemberInfo* info = 0;
  1368. bool match = false;
  1369. group->MGroupMembers.writelock();
  1370. for (itr = members->begin(); itr != members->end(); itr++) {
  1371. info = *itr;
  1372. if (info && info->name == name)
  1373. {
  1374. info->client = client;
  1375. info->member = client->GetPlayer();
  1376. client->GetPlayer()->SetGroup(group);
  1377. client->GetPlayer()->SetGroupMemberInfo(info);
  1378. client->GetPlayer()->UpdateGroupMemberInfo(true, true);
  1379. LogWrite(PLAYER__DEBUG, 0, "Player", "Identified group match for player %s to group id %u", name.c_str(), group_id);
  1380. match = true;
  1381. break;
  1382. }
  1383. }
  1384. group->MGroupMembers.releasewritelock();
  1385. // must be done after cause it needs a readlock
  1386. if (match)
  1387. group->SendGroupUpdate();
  1388. if (!match)
  1389. 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);
  1390. world.GetGroupManager()->ReleaseGroupLock(__FUNCTION__, __LINE__);
  1391. return match;
  1392. }
  1393. void World::AddBonuses(Item* item, ItemStatsValues* values, int16 type, sint32 value, Entity* entity){
  1394. if(values){
  1395. if(item && entity && entity->IsPlayer())
  1396. {
  1397. int32 effective_level = entity->GetInfoStructUInt("effective_level");
  1398. if(effective_level && effective_level < entity->GetLevel() && item->details.recommended_level > effective_level)
  1399. {
  1400. int32 diff = item->details.recommended_level - effective_level;
  1401. float tmpValue = (float)value;
  1402. value = (sint32)(float)(tmpValue / (1.0f + ((float)diff * .05f)));
  1403. }
  1404. }
  1405. switch(type){
  1406. case ITEM_STAT_STR:{
  1407. values->str += value;
  1408. break;
  1409. }
  1410. case ITEM_STAT_STA:{
  1411. values->sta += value;
  1412. break;
  1413. }
  1414. case ITEM_STAT_AGI:{
  1415. values->agi += value;
  1416. break;
  1417. }
  1418. case ITEM_STAT_WIS:{
  1419. values->wis += value;
  1420. break;
  1421. }
  1422. case ITEM_STAT_INT:{
  1423. values->int_ += value;
  1424. break;
  1425. }
  1426. case ITEM_STAT_VS_SLASH:{
  1427. values->vs_slash += value;
  1428. break;
  1429. }
  1430. case ITEM_STAT_VS_CRUSH:{
  1431. values->vs_crush += value;
  1432. break;
  1433. }
  1434. case ITEM_STAT_VS_PIERCE:{
  1435. values->vs_pierce += value;
  1436. break;
  1437. }
  1438. case ITEM_STAT_VS_HEAT:{
  1439. values->vs_heat += value;
  1440. break;
  1441. }
  1442. case ITEM_STAT_VS_COLD:{
  1443. values->vs_cold += value;
  1444. break;
  1445. }
  1446. case ITEM_STAT_VS_MAGIC:{
  1447. values->vs_magic += value;
  1448. break;
  1449. }
  1450. case ITEM_STAT_VS_MENTAL:{
  1451. values->vs_mental += value;
  1452. break;
  1453. }
  1454. case ITEM_STAT_VS_DIVINE:{
  1455. values->vs_divine += value;
  1456. break;
  1457. }
  1458. case ITEM_STAT_VS_DISEASE:{
  1459. values->vs_disease += value;
  1460. break;
  1461. }
  1462. case ITEM_STAT_VS_POISON:{
  1463. values->vs_poison += value;
  1464. break;
  1465. }
  1466. case ITEM_STAT_HEALTH:{
  1467. values->health += value;
  1468. break;
  1469. }
  1470. case ITEM_STAT_POWER:{
  1471. values->power += value;
  1472. break;
  1473. }
  1474. case ITEM_STAT_CONCENTRATION:{
  1475. values->concentration += value;
  1476. break;
  1477. }
  1478. case ITEM_STAT_ABILITY_MODIFIER:{
  1479. values->ability_modifier += value;
  1480. break;
  1481. }
  1482. case ITEM_STAT_CRITICALMITIGATION:{
  1483. values->criticalmitigation += value;
  1484. break;
  1485. }
  1486. case ITEM_STAT_EXTRASHIELDBLOCKCHANCE:{
  1487. values->extrashieldblockchance += value;
  1488. break;
  1489. }
  1490. case ITEM_STAT_BENEFICIALCRITCHANCE:{
  1491. values->beneficialcritchance += value;
  1492. break;
  1493. }
  1494. case ITEM_STAT_CRITBONUS:{
  1495. values->critbonus += value;
  1496. break;
  1497. }
  1498. case ITEM_STAT_POTENCY:{
  1499. values->potency += value;
  1500. break;
  1501. }
  1502. case ITEM_STAT_HATEGAINMOD:{
  1503. values->hategainmod += value;
  1504. break;
  1505. }
  1506. case ITEM_STAT_ABILITYREUSESPEED:{
  1507. values->abilityreusespeed += value;
  1508. break;
  1509. }
  1510. case ITEM_STAT_ABILITYCASTINGSPEED:{
  1511. values->abilitycastingspeed += value;
  1512. break;
  1513. }
  1514. case ITEM_STAT_ABILITYRECOVERYSPEED:{
  1515. values->abilityrecoveryspeed += value;
  1516. break;
  1517. }
  1518. case ITEM_STAT_SPELLREUSESPEED:{
  1519. values->spellreusespeed += value;
  1520. break;
  1521. }
  1522. case ITEM_STAT_SPELLMULTIATTACKCHANCE:{
  1523. values->spellmultiattackchance += value;
  1524. break;
  1525. }
  1526. case ITEM_STAT_DPS:{
  1527. values->dps += value;
  1528. break;
  1529. }
  1530. case ITEM_STAT_ATTACKSPEED:{
  1531. values->attackspeed += value;
  1532. break;
  1533. }
  1534. case ITEM_STAT_MULTIATTACKCHANCE:{
  1535. values->multiattackchance += value;
  1536. break;
  1537. }
  1538. case ITEM_STAT_AEAUTOATTACKCHANCE:{
  1539. values->aeautoattackchance += value;
  1540. break;
  1541. }
  1542. case ITEM_STAT_STRIKETHROUGH:{
  1543. values->strikethrough += value;
  1544. break;
  1545. }
  1546. case ITEM_STAT_ACCURACY:{
  1547. values->accuracy += value;
  1548. break;
  1549. }
  1550. /*case ITEM_STAT_OFFENSIVESPEED:{
  1551. values->offensivespeed += value;
  1552. break;
  1553. }*/
  1554. default: {
  1555. if (entity) {
  1556. entity->MStats.lock();
  1557. entity->stats[type] += value;
  1558. entity->MStats.unlock();
  1559. }
  1560. break;
  1561. }
  1562. }
  1563. }
  1564. }
  1565. void World::CreateGuild(const char* guild_name, Client* leader, int32 group_id) {
  1566. deque<GroupMemberInfo*>::iterator itr;
  1567. GroupMemberInfo* gmi;
  1568. Guild *guild;
  1569. assert(guild_name);
  1570. guild = new Guild();
  1571. guild->SetName(guild_name);
  1572. guild->SetFormedDate(Timer::GetUnixTimeStamp());
  1573. database.LoadGuildDefaultRanks(guild);
  1574. database.LoadGuildDefaultEventFilters(guild);
  1575. database.SaveGuild(guild, true);
  1576. database.SaveGuildEvents(guild);
  1577. database.SaveGuildRanks(guild);
  1578. database.SaveGuildEventFilters(guild);
  1579. database.SaveGuildRecruiting(guild);
  1580. guild_list.AddGuild(guild);
  1581. if (leader && !leader->GetPlayer()->GetGuild())
  1582. guild->AddNewGuildMember(leader, 0, GUILD_RANK_LEADER);
  1583. database.SaveGuildMembers(guild);
  1584. if (leader && group_id > 0) {
  1585. GetGroupManager()->GroupLock(__FUNCTION__, __LINE__);
  1586. PlayerGroup* group = world.GetGroupManager()->GetGroup(group_id);
  1587. if (group)
  1588. {
  1589. group->MGroupMembers.readlock(__FUNCTION__, __LINE__);
  1590. deque<GroupMemberInfo*>* members = group->GetMembers();
  1591. for (itr = members->begin(); itr != members->end(); itr++) {
  1592. gmi = *itr;
  1593. if (gmi->client && gmi->client != leader && !gmi->client->GetPlayer()->GetGuild())
  1594. guild->InvitePlayer(gmi->client, leader->GetPlayer()->GetName());
  1595. }
  1596. group->MGroupMembers.releasereadlock(__FUNCTION__, __LINE__);
  1597. }
  1598. GetGroupManager()->ReleaseGroupLock(__FUNCTION__, __LINE__);
  1599. }
  1600. }
  1601. void World::SaveGuilds() {
  1602. MutexMap<int32, Guild*>* guilds = guild_list.GetGuilds();
  1603. MutexMap<int32, Guild*>::iterator itr = guilds->begin();
  1604. while (itr.Next()) {
  1605. Guild* guild = itr.second;
  1606. if (guild->GetSaveNeeded())
  1607. database.SaveGuild(guild);
  1608. if (guild->GetMemberSaveNeeded())
  1609. database.SaveGuildMembers(guild);
  1610. if (guild->GetEventsSaveNeeded())
  1611. database.SaveGuildEvents(guild);
  1612. if (guild->GetRanksSaveNeeded())
  1613. database.SaveGuildRanks(guild);
  1614. if (guild->GetEventFiltersSaveNeeded())
  1615. database.SaveGuildEventFilters(guild);
  1616. if (guild->GetPointsHistorySaveNeeded())
  1617. database.SaveGuildPointsHistory(guild);
  1618. if (guild->GetRecruitingSaveNeeded())
  1619. database.SaveGuildRecruiting(guild);
  1620. }
  1621. }
  1622. void World::PickRandomLottoDigits(int32* digits) {
  1623. if (digits) {
  1624. for (int32 i = 0; i < 6; i++) {
  1625. bool found = true;
  1626. int32 num = 0;
  1627. while (found) {
  1628. num = ((int32)rand() % 36) + 1;
  1629. for (int32 j = 0; j < 6; j++) {
  1630. if (digits[j] == num)
  1631. break;
  1632. if (j == 5)
  1633. found = false;
  1634. }
  1635. }
  1636. digits[i] = num;
  1637. }
  1638. }
  1639. }
  1640. void World::AddLottoPlayer(int32 character_id, int32 end_time) {
  1641. LottoPlayer* lp;
  1642. if (lotto_players.count(character_id) == 0) {
  1643. lp = new LottoPlayer;
  1644. lotto_players.Put(character_id, lp);
  1645. }
  1646. else
  1647. lp = lotto_players.Get(character_id);
  1648. lp->end_time = end_time;
  1649. lp->num_matches = 0;
  1650. lp->set = false;
  1651. }
  1652. void World::RemoveLottoPlayer(int32 character_id) {
  1653. if (lotto_players.count(character_id) > 0)
  1654. lotto_players.erase(character_id, false, true);
  1655. }
  1656. void World::SetLottoPlayerNumMatches(int32 character_id, int8 num_matches) {
  1657. if (lotto_players.count(character_id) > 0) {
  1658. lotto_players.Get(character_id)->num_matches = num_matches;
  1659. lotto_players.Get(character_id)->set = true;
  1660. }
  1661. }
  1662. void World::CheckLottoPlayers() {
  1663. MutexMap<int32, LottoPlayer*>::iterator itr = lotto_players.begin();
  1664. while (itr.Next()) {
  1665. LottoPlayer* lp = itr->second;
  1666. if (Timer::GetCurrentTime2() >= lp->end_time && lp->set) {
  1667. int8 num_matches = lp->num_matches;
  1668. LogWrite(PLAYER__DEBUG, 0, "Player", "Num matches: %u", lp->num_matches);
  1669. Client* client = zone_list.GetClientByCharID(itr->first);
  1670. if (client && num_matches >= 2) {
  1671. if (num_matches == 2) {
  1672. client->SimpleMessage(CHANNEL_COLOR_YELLOW, "You receive 10 silver.");
  1673. client->SendPopupMessage(0, "Congratulations! You have won 10 silver!", "", 2, 0xFF, 0xFF, 0x99);
  1674. client->GetPlayer()->AddCoins(1000);
  1675. client->GetPlayer()->GetZone()->SendCastSpellPacket(869, client->GetPlayer());
  1676. }
  1677. else if (num_matches == 3) {
  1678. client->SimpleMessage(CHANNEL_COLOR_YELLOW, "You receive 50 silver.");
  1679. client->SendPopupMessage(0, "Congratulations! You have won 50 silver!", "", 2, 0xFF, 0xFF, 0x99);
  1680. client->GetPlayer()->AddCoins(5000);
  1681. client->GetPlayer()->GetZone()->SendCastSpellPacket(870, client->GetPlayer());
  1682. }
  1683. else if (num_matches == 4) {
  1684. client->SimpleMessage(CHANNEL_COLOR_YELLOW, "You receive 2 gold 50 silver.");
  1685. client->SendPopupMessage(0, "Congratulations! You have won 2 gold 50 silver!", "", 2, 0xFF, 0xFF, 0x99);
  1686. client->GetPlayer()->AddCoins(25000);
  1687. client->GetPlayer()->GetZone()->SendCastSpellPacket(871, client->GetPlayer());
  1688. }
  1689. else if (num_matches == 5) {
  1690. client->SimpleMessage(CHANNEL_COLOR_YELLOW, "You receive 25 gold.");
  1691. client->SendPopupMessage(0, "Congratulations! You have won 25 gold!", "", 2, 0xFF, 0xFF, 0x99);
  1692. client->GetPlayer()->AddCoins(250000);
  1693. client->GetPlayer()->GetZone()->SendCastSpellPacket(872, client->GetPlayer());
  1694. }
  1695. else if (num_matches == 6) {
  1696. Variable* var = variables.FindVariable("gambling_current_jackpot");
  1697. if (var) {
  1698. int64 jackpot = 0;
  1699. try {
  1700. jackpot = atoul(var->GetValue());
  1701. }
  1702. catch (...) {
  1703. jackpot = 10000;
  1704. }
  1705. char coin_message[128];
  1706. char message[512];
  1707. char announcement[512];
  1708. memset(coin_message, 0, sizeof(coin_message));
  1709. memset(message, 0, sizeof(message));
  1710. memset(announcement, 0, sizeof(announcement));
  1711. sprintf(coin_message, "%s", client->GetCoinMessage(jackpot).c_str());
  1712. sprintf(message, "Congratulations! You have won %s!", coin_message);
  1713. sprintf(announcement, "%s as won the jackpot containing a total of %s!", client->GetPlayer()->GetName(), coin_message);
  1714. client->Message(CHANNEL_COLOR_YELLOW, "You receive %s.", coin_message);
  1715. client->SendPopupMessage(0, message, "", 2, 0xFF, 0xFF, 0x99);
  1716. zone_list.HandleGlobalAnnouncement(announcement);
  1717. client->GetPlayer()->AddCoins(jackpot);
  1718. client->GetPlayer()->GetZone()->SendCastSpellPacket(843, client->GetPlayer());
  1719. client->GetPlayer()->GetZone()->SendCastSpellPacket(1413, client->GetPlayer());
  1720. }
  1721. }
  1722. }
  1723. RemoveLottoPlayer(itr->first);
  1724. }
  1725. }
  1726. }
  1727. 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) {
  1728. MHouseZones.writelock(__FUNCTION__, __LINE__);
  1729. if (m_houseZones.count(id) == 0) {
  1730. HouseZone* hz = new HouseZone;
  1731. //ZeroMemory(hz, sizeof(HouseZone));
  1732. hz->id = id;
  1733. hz->name = name;
  1734. hz->cost_coin = cost_coins;
  1735. hz->cost_status = cost_status;
  1736. hz->upkeep_coin = upkeep_coins;
  1737. hz->upkeep_status = upkeep_status;
  1738. hz->vault_slots = vault_slots;
  1739. hz->alignment = alignment;
  1740. hz->guild_level = guild_level;
  1741. hz->zone_id = zone_id;
  1742. hz->exit_zone_id = exit_zone_id;
  1743. hz->exit_x = exit_x;
  1744. hz->exit_y = exit_y;
  1745. hz->exit_z = exit_z;
  1746. hz->exit_heading = exit_heading;
  1747. m_houseZones[id] = hz;
  1748. }
  1749. else {
  1750. LogWrite(WORLD__ERROR, 0, "Housing", "Duplicate house id (%u) for %s", id, name.c_str());
  1751. }
  1752. MHouseZones.releasewritelock(__FUNCTION__, __LINE__);
  1753. }
  1754. HouseZone* World::GetHouseZone(int32 id) {
  1755. HouseZone* ret = 0;
  1756. MHouseZones.readlock(__FUNCTION__, __LINE__);
  1757. if (m_houseZones.count(id) > 0)
  1758. ret = m_houseZones[id];
  1759. MHouseZones.releasereadlock(__FUNCTION__, __LINE__);
  1760. return ret;
  1761. }
  1762. 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) {
  1763. MPlayerHouses.writelock(__FUNCTION__, __LINE__);
  1764. if (m_playerHouses.count(house_id) == 0 || m_playerHouses[house_id].count(char_id) == 0) {
  1765. PlayerHouse* ph = new PlayerHouse;
  1766. ph->house_id = house_id;
  1767. ph->unique_id = unique_id;
  1768. ph->instance_id = instance_id;
  1769. ph->escrow_coins = escrow_coins;
  1770. ph->escrow_status = escrow_status;
  1771. ph->upkeep_due = upkeep_due;
  1772. ph->player_name = player_name;
  1773. ReloadHouseData(ph);
  1774. m_playerHouses[house_id][char_id] = ph;
  1775. }
  1776. MPlayerHouses.releasewritelock(__FUNCTION__, __LINE__);
  1777. }
  1778. void World::ReloadHouseData(PlayerHouse* ph)
  1779. {
  1780. database.LoadDeposits(ph);
  1781. database.LoadHistory(ph);
  1782. }
  1783. PlayerHouse* World::GetPlayerHouseByHouseID(int32 char_id, int32 house_id) {
  1784. PlayerHouse* ret = 0;
  1785. MPlayerHouses.readlock(__FUNCTION__, __LINE__);
  1786. if (m_playerHouses.count(house_id) > 0 && m_playerHouses[house_id].count(char_id) > 0)
  1787. ret = m_playerHouses[house_id][char_id];
  1788. MPlayerHouses.releasereadlock(__FUNCTION__, __LINE__);
  1789. return ret;
  1790. }
  1791. PlayerHouse* World::GetPlayerHouseByUniqueID(int64 unique_id) {
  1792. PlayerHouse* ret = 0;
  1793. MPlayerHouses.readlock(__FUNCTION__, __LINE__);
  1794. map<int32, map<int32, PlayerHouse*> >::iterator itr;
  1795. for (itr = m_playerHouses.begin(); itr != m_playerHouses.end(); itr++) {
  1796. map<int32, PlayerHouse*>::iterator itr2;
  1797. for (itr2 = itr->second.begin(); itr2 != itr->second.end(); itr2++) {
  1798. if (itr2->second->unique_id == unique_id) {
  1799. ret = itr2->second;
  1800. break;
  1801. }
  1802. }
  1803. if (ret)
  1804. break;
  1805. }
  1806. MPlayerHouses.releasereadlock(__FUNCTION__, __LINE__);
  1807. return ret;
  1808. }
  1809. PlayerHouse* World::GetPlayerHouseByInstanceID(int32 instance_id) {
  1810. PlayerHouse* ret = 0;
  1811. MPlayerHouses.readlock(__FUNCTION__, __LINE__);
  1812. map<int32, map<int32, PlayerHouse*> >::iterator itr;
  1813. for (itr = m_playerHouses.begin(); itr != m_playerHouses.end(); itr++) {
  1814. map<int32, PlayerHouse*>::iterator itr2;
  1815. for (itr2 = itr->second.begin(); itr2 != itr->second.end(); itr2++) {
  1816. if (itr2->second->instance_id == instance_id) {
  1817. ret = itr2->second;
  1818. break;
  1819. }
  1820. }
  1821. if (ret)
  1822. break;
  1823. }
  1824. MPlayerHouses.releasereadlock(__FUNCTION__, __LINE__);
  1825. return ret;
  1826. }
  1827. vector<PlayerHouse*> World::GetAllPlayerHouses(int32 char_id) {
  1828. vector<PlayerHouse*> ret;
  1829. MPlayerHouses.readlock(__FUNCTION__, __LINE__);
  1830. map<int32, map<int32, PlayerHouse*> >::iterator itr;
  1831. for (itr = m_playerHouses.begin(); itr != m_playerHouses.end(); itr++) {
  1832. if (itr->second.count(char_id) > 0)
  1833. ret.push_back(itr->second[char_id]);
  1834. }
  1835. MPlayerHouses.releasereadlock(__FUNCTION__, __LINE__);
  1836. return ret;
  1837. }
  1838. vector<PlayerHouse*> World::GetAllPlayerHousesByHouseID(int32 house_id) {
  1839. vector<PlayerHouse*> ret;
  1840. MPlayerHouses.readlock(__FUNCTION__, __LINE__);
  1841. if (m_houseZones.count(house_id) > 0) {
  1842. map<int32, PlayerHouse*>::iterator itr;
  1843. for (itr = m_playerHouses[house_id].begin(); itr != m_playerHouses[house_id].end(); itr++)
  1844. ret.push_back(itr->second);
  1845. }
  1846. MPlayerHouses.releasereadlock(__FUNCTION__, __LINE__);
  1847. return ret;
  1848. }
  1849. void World::PopulateTOVStatMap() {
  1850. //This function populates a map that converts changed CoE to ToV stats
  1851. tov_itemstat_conversion[0] = TOV_ITEM_STAT_HPREGEN;
  1852. tov_itemstat_conversion[1] = TOV_ITEM_STAT_MANAREGEN;
  1853. tov_itemstat_conversion[2] = TOV_ITEM_STAT_HPREGENPPT;
  1854. tov_itemstat_conversion[3] = TOV_ITEM_STAT_MPREGENPPT;
  1855. tov_itemstat_conversion[4] = TOV_ITEM_STAT_COMBATHPREGENPPT;
  1856. tov_itemstat_conversion[5] = TOV_ITEM_STAT_COMBATMPREGENPPT;
  1857. tov_itemstat_conversion[6] = TOV_ITEM_STAT_MAXHP;
  1858. tov_itemstat_conversion[7] = TOV_ITEM_STAT_MAXHPPERC;
  1859. tov_itemstat_conversion[8] = TOV_ITEM_STAT_MAXHPPERCFINAL;
  1860. tov_itemstat_conversion[9] = TOV_ITEM_STAT_SPEED;
  1861. tov_itemstat_conversion[10] = TOV_ITEM_STAT_SLOW;
  1862. tov_itemstat_conversion[11] = TOV_ITEM_STAT_MOUNTSPEED;
  1863. tov_itemstat_conversion[12] = TOV_ITEM_STAT_MOUNTAIRSPEED;
  1864. tov_itemstat_conversion[13] = TOV_ITEM_STAT_LEAPSPEED;
  1865. tov_itemstat_conversion[14] = TOV_ITEM_STAT_LEAPTIME;
  1866. tov_itemstat_conversion[15] = TOV_ITEM_STAT_GLIDEEFFICIENCY;
  1867. tov_itemstat_conversion[16] = TOV_ITEM_STAT_OFFENSIVESPEED;
  1868. tov_itemstat_conversion[17] = TOV_ITEM_STAT_ATTACKSPEED;
  1869. tov_itemstat_conversion[18] = 698;
  1870. tov_itemstat_conversion[19] = TOV_ITEM_STAT_MAXMANA;
  1871. tov_itemstat_conversion[20] = TOV_ITEM_STAT_MAXMANAPERC;
  1872. tov_itemstat_conversion[21] = TOV_ITEM_STAT_MAXATTPERC;
  1873. tov_itemstat_conversion[22] = TOV_ITEM_STAT_BLURVISION;
  1874. tov_itemstat_conversion[23] = TOV_ITEM_STAT_MAGICLEVELIMMUNITY;
  1875. tov_itemstat_conversion[24] = TOV_ITEM_STAT_HATEGAINMOD;
  1876. tov_itemstat_conversion[25] = TOV_ITEM_STAT_COMBATEXPMOD;
  1877. tov_itemstat_conversion[26] = TOV_ITEM_STAT_TRADESKILLEXPMOD;
  1878. tov_itemstat_conversion[27] = TOV_ITEM_STAT_ACHIEVEMENTEXPMOD;
  1879. tov_itemstat_conversion[28] = TOV_ITEM_STAT_SIZEMOD;
  1880. tov_itemstat_conversion[29] = TOV_ITEM_STAT_DPS;
  1881. tov_itemstat_conversion[30] = 698;
  1882. tov_itemstat_conversion[31] = TOV_ITEM_STAT_STEALTH;
  1883. tov_itemstat_conversion[32] = TOV_ITEM_STAT_INVIS;
  1884. tov_itemstat_conversion[33] = TOV_ITEM_STAT_SEESTEALTH;
  1885. tov_itemstat_conversion[34] = TOV_ITEM_STAT_SEEINVIS;
  1886. tov_itemstat_conversion[35] = TOV_ITEM_STAT_EFFECTIVELEVELMOD;
  1887. tov_itemstat_conversion[36] = TOV_ITEM_STAT_RIPOSTECHANCE;
  1888. tov_itemstat_conversion[37] = TOV_ITEM_STAT_PARRYCHANCE;
  1889. tov_itemstat_conversion[38] = TOV_ITEM_STAT_DODGECHANCE;
  1890. tov_itemstat_conversion[39] = TOV_ITEM_STAT_AEAUTOATTACKCHANCE;
  1891. tov_itemstat_conversion[40] = 698;
  1892. tov_itemstat_conversion[41] = TOV_ITEM_STAT_MULTIATTACKCHANCE;
  1893. tov_itemstat_conversion[42] = 698;
  1894. tov_itemstat_conversion[43] = 698;
  1895. tov_itemstat_conversion[44] = 698;
  1896. tov_itemstat_conversion[45] = TOV_ITEM_STAT_SPELLMULTIATTACKCHANCE;
  1897. tov_itemstat_conversion[46] = 698;
  1898. tov_itemstat_conversion[47] = TOV_ITEM_STAT_FLURRY;
  1899. tov_itemstat_conversion[48] = 698;
  1900. tov_itemstat_conversion[49] = TOV_ITEM_STAT_MELEEDAMAGEMULTIPLIER;
  1901. tov_itemstat_conversion[50] = TOV_ITEM_STAT_EXTRAHARVESTCHANCE;
  1902. tov_itemstat_conversion[51] = TOV_ITEM_STAT_EXTRASHIELDBLOCKCHANCE;
  1903. tov_itemstat_conversion[52] = TOV_ITEM_STAT_ITEMHPREGENPPT;
  1904. tov_itemstat_conversion[53] = TOV_ITEM_STAT_ITEMPPREGENPPT;
  1905. tov_itemstat_conversion[54] = TOV_ITEM_STAT_MELEECRITCHANCE;
  1906. tov_itemstat_conversion[55] = TOV_ITEM_STAT_CRITAVOIDANCE;
  1907. tov_itemstat_conversion[56] = TOV_ITEM_STAT_BENEFICIALCRITCHANCE;
  1908. tov_itemstat_conversion[57] = TOV_ITEM_STAT_CRITBONUS;
  1909. tov_itemstat_conversion[58] = 698;
  1910. tov_itemstat_conversion[59] = TOV_ITEM_STAT_POTENCY;
  1911. tov_itemstat_conversion[60] = 698;
  1912. tov_itemstat_conversion[61] = TOV_ITEM_STAT_UNCONSCIOUSHPMOD;
  1913. tov_itemstat_conversion[62] = TOV_ITEM_STAT_ABILITYREUSESPEED;
  1914. tov_itemstat_conversion[63] = TOV_ITEM_STAT_ABILITYRECOVERYSPEED;
  1915. tov_itemstat_conversion[64] = TOV_ITEM_STAT_ABILITYCASTINGSPEED;
  1916. tov_itemstat_conversion[65] = TOV_ITEM_STAT_SPELLREUSESPEED;
  1917. tov_itemstat_conversion[66] = TOV_ITEM_STAT_MELEEWEAPONRANGE;
  1918. tov_itemstat_conversion[67] = TOV_ITEM_STAT_RANGEDWEAPONRANGE;
  1919. tov_itemstat_conversion[68] = TOV_ITEM_STAT_FALLINGDAMAGEREDUCTION;
  1920. tov_itemstat_conversion[69] = TOV_ITEM_STAT_RIPOSTEDAMAGE;
  1921. tov_itemstat_conversion[70] = TOV_ITEM_STAT_MINIMUMDEFLECTIONCHANCE;
  1922. tov_itemstat_conversion[71] = TOV_ITEM_STAT_MOVEMENTWEAVE;
  1923. tov_itemstat_conversion[72] = TOV_ITEM_STAT_COMBATHPREGEN;
  1924. tov_itemstat_conversion[73] = TOV_ITEM_STAT_COMBATMANAREGEN;
  1925. tov_itemstat_conversion[74] = TOV_ITEM_STAT_CONTESTSPEEDBOOST;
  1926. tov_itemstat_conversion[75] = TOV_ITEM_STAT_TRACKINGAVOIDANCE;
  1927. tov_itemstat_conversion[76] = TOV_ITEM_STAT_STEALTHINVISSPEEDMOD;
  1928. tov_itemstat_conversion[77] = TOV_ITEM_STAT_LOOT_COIN;
  1929. tov_itemstat_conversion[78] = TOV_ITEM_STAT_ARMORMITIGATIONINCREASE;
  1930. tov_itemstat_conversion[79] = TOV_ITEM_STAT_AMMOCONSERVATION;
  1931. tov_itemstat_conversion[80] = TOV_ITEM_STAT_STRIKETHROUGH;
  1932. tov_itemstat_conversion[81] = TOV_ITEM_STAT_STATUSBONUS;
  1933. tov_itemstat_conversion[82] = TOV_ITEM_STAT_ACCURACY;
  1934. tov_itemstat_conversion[83] = TOV_ITEM_STAT_COUNTERSTRIKE;
  1935. tov_itemstat_conversion[84] = TOV_ITEM_STAT_SHIELDBASH;
  1936. tov_itemstat_conversion[85] = TOV_ITEM_STAT_WEAPONDAMAGEBONUS;
  1937. tov_itemstat_conversion[86] = 698;
  1938. tov_itemstat_conversion[87] = TOV_ITEM_STAT_WEAPONDAMAGEBONUSMELEEONLY;
  1939. tov_itemstat_conversion[88] = TOV_ITEM_STAT_ADDITIONALRIPOSTECHANCE;
  1940. tov_itemstat_conversion[89] = TOV_ITEM_STAT_PVPTOUGHNESS;
  1941. tov_itemstat_conversion[90] = TOV_ITEM_STAT_PVPLETHALITY;
  1942. tov_itemstat_conversion[91] = TOV_ITEM_STAT_STAMINABONUS;
  1943. tov_itemstat_conversion[92] = TOV_ITEM_STAT_WISDOMMITBONUS;
  1944. tov_itemstat_conversion[93] = TOV_ITEM_STAT_HEALRECEIVE;
  1945. tov_itemstat_conversion[94] = TOV_ITEM_STAT_HEALRECEIVEPERC;
  1946. tov_itemstat_conversion[95] = TOV_ITEM_STAT_PVPCRITICALMITIGATION;
  1947. tov_itemstat_conversion[96] = TOV_ITEM_STAT_BASEAVOIDANCEBONUS;
  1948. tov_itemstat_conversion[97] = TOV_ITEM_STAT_INCOMBATSAVAGERYREGEN;
  1949. tov_itemstat_conversion[98] = TOV_ITEM_STAT_OUTOFCOMBATSAVAGERYREGEN;
  1950. tov_itemstat_conversion[99] = TOV_ITEM_STAT_SAVAGERYREGEN;
  1951. tov_itemstat_conversion[100] = TOV_ITEM_STAT_SAVAGERYGAINMOD;
  1952. tov_itemstat_conversion[101] = TOV_ITEM_STAT_MAXSAVAGERYLEVEL;
  1953. }
  1954. int32 World::LoadItemBlueStats() {
  1955. Query query;
  1956. MYSQL_ROW row;
  1957. int32 count = 0;
  1958. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT version_range1,version_range2,emu_stat,name,stat from itemstats");
  1959. if (result && mysql_num_rows(result) > 0) {
  1960. while (result && (row = mysql_fetch_row(result))) {
  1961. count++;
  1962. if (atoi(row[0]) >= 63119) //KA
  1963. ka_itemstat_conversion[atoi(row[2])] = atoi(row[4]);
  1964. else if (atoi(row[0]) >= 57101) // ToV
  1965. tov_itemstat_conversion[atoi(row[2])] = atoi(row[4]);
  1966. else if (atoi(row[0]) >= 1193) // CoE
  1967. coe_itemstat_conversion[atoi(row[2])] = atoi(row[4]);
  1968. else if (atoi(row[0]) >= 1096) // DoV
  1969. dov_itemstat_conversion[atoi(row[2])] = atoi(row[4]);
  1970. }
  1971. }
  1972. return count;
  1973. }
  1974. sint64 World::newValue = 0;
  1975. sint16 World::GetItemStatAOMValue(sint16 subtype) {
  1976. sint16 tmp_subtype = subtype;
  1977. // this is ugly for now cause I didn't want to map it all out, see a better way later but a lot of these are just slightly shifted
  1978. if(subtype > 39)
  1979. {
  1980. // 88 needs to be something else (crit mitigation)
  1981. // 19 needs to be something else (ability reuse speed) which is 62
  1982. if(subtype == 21) // ITEM_STAT_MAXATTPERC
  1983. tmp_subtype = 20;
  1984. else if(subtype == 41) // flurry
  1985. tmp_subtype = 39;
  1986. else if(subtype == 47) // flurry
  1987. tmp_subtype = 41;
  1988. else if(subtype == 49) // flurry
  1989. tmp_subtype = 42;
  1990. else if(subtype == 51) // ITEM_STAT_EXTRASHIELDBLOCKCHANCE
  1991. tmp_subtype = 44;
  1992. //tmp_subtype = 43 is bountiful harvest
  1993. else if(subtype == 54 && subtype <= 57) // ITEM_STAT_MELEECRITCHANCE
  1994. tmp_subtype = subtype - 7;
  1995. else if(subtype == 59) // ITEM_STAT_POTENCY
  1996. tmp_subtype = 51;
  1997. else if(subtype >= 61 && subtype <= 85) // ITEM_STAT_RANGEDWEAPONRANGE
  1998. tmp_subtype = subtype - 9; //
  1999. else if(subtype >= 86 && subtype <= 101) // ITEM_STAT_WEAPONDAMAGEBONUSMELEEONLY
  2000. tmp_subtype = subtype - 8; //
  2001. else if(subtype == 102) // ITEM_STAT_SPELLWEAPONDAMAGEBONUS
  2002. tmp_subtype = 77; //
  2003. else if(subtype >= 103 && subtype <= 110)
  2004. tmp_subtype = subtype - 9;
  2005. else if(subtype == 122) // ITEM_STAT_ABILITYDOUBLECASTCHANCE
  2006. tmp_subtype = 40; //
  2007. else if(subtype == 124) // ITEM_STAT_STATUSEARNED
  2008. tmp_subtype = 27; //
  2009. else
  2010. tmp_subtype += 1;
  2011. // 80 serves as ranged weapon range increase, but so does 58?
  2012. }
  2013. else if((subtype > 18 && subtype < 28) || subtype > 30) // max mana was 18
  2014. tmp_subtype = subtype - 1;
  2015. else if(subtype == 5)
  2016. tmp_subtype = 46;
  2017. else if(subtype == 4)
  2018. tmp_subtype = 45;
  2019. LogWrite(PLAYER__DEBUG, 0, "Player", "Convert type: %i -> %i", subtype, tmp_subtype);
  2020. return tmp_subtype;
  2021. }
  2022. sint16 World::GetItemStatTOVValue(sint16 subtype) {
  2023. return (tov_itemstat_conversion[subtype] - 600);
  2024. }
  2025. sint16 World::GetItemStatDOVValue(sint16 subtype) {
  2026. return (dov_itemstat_conversion[subtype] - 600);
  2027. }
  2028. sint16 World::GetItemStatCOEValue(sint16 subtype) {
  2029. return (coe_itemstat_conversion[subtype] - 600);
  2030. }
  2031. sint16 World::GetItemStatKAValue(sint16 subtype) {
  2032. return (ka_itemstat_conversion[subtype] - 600);
  2033. }
  2034. int8 World::TranslateSlotSubTypeToClient(Client* client, int8 stat_type, sint16 sub_type) {
  2035. int8 new_subtype = (int8)sub_type;
  2036. switch(stat_type) {
  2037. case 2: {
  2038. if(client->GetVersion() <= 546) {
  2039. if(sub_type == (ITEM_STAT_VS_POISON-200)) // poison
  2040. new_subtype = 9;
  2041. else if(sub_type == (ITEM_STAT_VS_DISEASE-200)) // disease
  2042. new_subtype = 8;
  2043. else if(sub_type == (ITEM_STAT_VS_COLD-200)) // cold
  2044. new_subtype = 4;
  2045. else if(sub_type == (ITEM_STAT_VS_HEAT-200) || sub_type == (ITEM_STAT_VS_MAGIC-200))
  2046. new_subtype += 2;
  2047. else if(sub_type == (ITEM_STAT_VS_MENTAL-200) || sub_type == (ITEM_STAT_VS_DIVINE-200))
  2048. new_subtype -= 2;
  2049. }
  2050. else if(client->GetVersion() >= 60085) { // AoM era since its the client we support
  2051. if(sub_type == (ITEM_STAT_VS_MENTAL-200) || sub_type == (ITEM_STAT_VS_DIVINE-200) || sub_type == (ITEM_STAT_VS_COLD-200)) {
  2052. new_subtype = 255; // omit client cannot properly display
  2053. }
  2054. }
  2055. break;
  2056. }
  2057. case 6:
  2058. case 7: {
  2059. if(stat_type == 7){
  2060. new_subtype = sub_type;
  2061. }
  2062. else if ((client->GetVersion() >= 63119) || client->GetVersion() == 61331){ //KA
  2063. new_subtype = world.GetItemStatKAValue(sub_type);
  2064. }
  2065. else if(client->GetVersion() >= 60085 ) {
  2066. new_subtype = world.GetItemStatAOMValue(sub_type);
  2067. }
  2068. else if (client->GetVersion() >= 57107){ //TOV
  2069. new_subtype = world.GetItemStatTOVValue(sub_type);
  2070. }
  2071. else if (client->GetVersion() >= 1193){ //COE
  2072. new_subtype = world.GetItemStatCOEValue(sub_type);
  2073. //tmp_subtype = stat->stat_subtype;
  2074. }
  2075. else if (client->GetVersion() >= 1096){ //DOV
  2076. new_subtype = world.GetItemStatDOVValue(sub_type); //comment out for testing
  2077. //tmp_subtype = stat->stat_subtype; //comment for normal use
  2078. }
  2079. break;
  2080. }
  2081. }
  2082. return new_subtype;
  2083. }
  2084. bool World::CheckTempBugCRC(char* msg)
  2085. {
  2086. MBugReport.writelock();
  2087. sint32 crc = GetItemNameCrc(std::string(msg));
  2088. if (bug_report_crc.count(crc) > 0)
  2089. {
  2090. MBugReport.releasewritelock();
  2091. return true;
  2092. }
  2093. else
  2094. bug_report_crc.insert(make_pair(crc, true));
  2095. MBugReport.releasewritelock();
  2096. return false;
  2097. }
  2098. #ifdef WIN32
  2099. ulong World::GetCurrentThreadID(){
  2100. return GetCurrentThreadId();
  2101. }
  2102. int64 World::GetThreadUsageCPUTime(){
  2103. HANDLE handle = GetCurrentThread();
  2104. int64 lpCreationTime;
  2105. int64 lpExitTime;
  2106. int64 lpKernelTime;
  2107. int64 lpUserTime;
  2108. if(GetThreadTimes(handle, (FILETIME*)&lpCreationTime, (FILETIME*)&lpExitTime, (FILETIME*)&lpKernelTime, (FILETIME*)&lpUserTime))
  2109. return lpKernelTime + lpUserTime;
  2110. return 0;
  2111. }
  2112. #else
  2113. #endif
  2114. void World::SyncCharacterAbilities(Client* client)
  2115. {
  2116. MStartingLists.readlock();
  2117. int8 baseClass = classes.GetBaseClass(client->GetPlayer()->GetAdventureClass());
  2118. int8 secondaryClass = classes.GetSecondaryBaseClass(client->GetPlayer()->GetAdventureClass());
  2119. int8 actualClass = client->GetPlayer()->GetAdventureClass();
  2120. int8 baseRace = client->GetPlayer()->GetRace();
  2121. multimap<int8, multimap<int8, StartingSkill>*>::iterator skill_itr = starting_skills.begin();
  2122. multimap<int8, multimap<int8, StartingSpell>*>::iterator spell_itr = starting_spells.begin();
  2123. bool isProcessing = false;
  2124. int8 wait_iterations = 0; // wait 5 iterations and give up if db takes too long
  2125. do
  2126. {
  2127. isProcessing = false;
  2128. if (skill_itr != starting_skills.end())
  2129. {
  2130. isProcessing = true;
  2131. // race = 255 is wildcard all, otherwise needs to match the race id
  2132. if (skill_itr->first == 255 || skill_itr->first == baseRace)
  2133. {
  2134. multimap<int8, StartingSkill>::iterator child_itr;
  2135. for (child_itr = skill_itr->second->begin(); child_itr != skill_itr->second->end(); child_itr++)
  2136. {
  2137. // class = 255 is wildcard all, otherwise needs to match the class id
  2138. if (child_itr->first == 255 ||
  2139. child_itr->first == baseClass ||
  2140. child_itr->first == secondaryClass ||
  2141. child_itr->first == actualClass)
  2142. {
  2143. if (!client->GetPlayer()->skill_list.HasSkill(child_itr->second.skill_id))
  2144. {
  2145. Query query;
  2146. 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());
  2147. 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)",
  2148. client->GetCharacterID(), child_itr->second.skill_id, child_itr->second.current_val, child_itr->second.max_val);
  2149. client->GetPlayer()->AddSkill(child_itr->second.skill_id, child_itr->second.current_val, child_itr->second.max_val);
  2150. }
  2151. }
  2152. }
  2153. }
  2154. skill_itr++;
  2155. }
  2156. if (spell_itr != starting_spells.end())
  2157. {
  2158. isProcessing = true;
  2159. // race = 255 is wildcard all, otherwise needs to match the race id
  2160. if (spell_itr->first == 255 || spell_itr->first == baseRace)
  2161. {
  2162. multimap<int8, StartingSpell>::iterator child_itr;
  2163. for (child_itr = spell_itr->second->begin(); child_itr != spell_itr->second->end(); child_itr++)
  2164. {
  2165. // class = 255 is wildcard all, otherwise needs to match the class id
  2166. if (child_itr->first == 255 ||
  2167. child_itr->first == baseClass ||
  2168. child_itr->first == secondaryClass ||
  2169. child_itr->first == actualClass)
  2170. {
  2171. if (!client->GetPlayer()->HasSpell(child_itr->second.spell_id, child_itr->second.tier, true))
  2172. {
  2173. Query query;
  2174. 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());
  2175. // knowledge_slot is a signed int in the DB
  2176. query.AddQueryAsync(client->GetCharacterID(), &database, Q_INSERT, "INSERT IGNORE INTO character_spells (char_id, spell_id, tier, knowledge_slot) VALUES (%u, %u, %u, %i)",
  2177. client->GetCharacterID(), child_itr->second.spell_id, child_itr->second.tier, child_itr->second.knowledge_slot);
  2178. // reload spells, we don't know the spellbook or timer info
  2179. client->GetPlayer()->GetInfoStruct()->set_reload_player_spells(1);
  2180. }
  2181. }
  2182. }
  2183. }
  2184. spell_itr++;
  2185. }
  2186. } while (isProcessing);
  2187. MStartingLists.releasereadlock();
  2188. }
  2189. void World::LoadStartingLists()
  2190. {
  2191. LogWrite(WORLD__DEBUG, 1, "World", "-Loading `starting_skills`...");
  2192. database.LoadStartingSkills(this);
  2193. LogWrite(WORLD__DEBUG, 1, "World", "-Loading `starting_spells`...");
  2194. database.LoadStartingSpells(this);
  2195. }
  2196. void World::PurgeStartingLists()
  2197. {
  2198. MStartingLists.writelock();
  2199. multimap<int8, multimap<int8, StartingSkill>*>::iterator skill_itr;
  2200. for (skill_itr = starting_skills.begin(); skill_itr != starting_skills.end(); skill_itr++)
  2201. {
  2202. multimap<int8, StartingSkill>* tmpMap = skill_itr->second;
  2203. safe_delete(tmpMap);
  2204. }
  2205. starting_skills.clear();
  2206. multimap<int8, multimap<int8, StartingSpell>*>::iterator spell_itr;
  2207. for (spell_itr = starting_spells.begin(); spell_itr != starting_spells.end(); spell_itr++)
  2208. {
  2209. multimap<int8, StartingSpell>* tmpMap = spell_itr->second;
  2210. safe_delete(tmpMap);
  2211. }
  2212. starting_spells.clear();
  2213. for(int type=0;type<3;type++) {
  2214. multimap<int32, multimap<int16, VoiceOverStruct>*>::iterator vos_itr;
  2215. for (vos_itr = voiceover_map[type].begin(); vos_itr != voiceover_map[type].end(); vos_itr++)
  2216. {
  2217. multimap<int16, VoiceOverStruct>* tmpMap = vos_itr->second;
  2218. safe_delete(tmpMap);
  2219. }
  2220. voiceover_map[type].clear();
  2221. }
  2222. MStartingLists.releasewritelock();
  2223. }
  2224. void World::SetReloadingSubsystem(string subsystem) {
  2225. MReloadingSubsystems.lock();
  2226. reloading_subsystems[subsystem] = Timer::GetCurrentTime2();
  2227. MReloadingSubsystems.unlock();
  2228. }
  2229. void World::RemoveReloadingSubSystem(string subsystem) {
  2230. MReloadingSubsystems.lock();
  2231. if (reloading_subsystems.count(subsystem) > 0)
  2232. reloading_subsystems.erase(subsystem);
  2233. MReloadingSubsystems.unlock();
  2234. }
  2235. bool World::IsReloadingSubsystems() {
  2236. bool result = false;
  2237. MReloadingSubsystems.lock();
  2238. result = reloading_subsystems.size() > 0;
  2239. MReloadingSubsystems.unlock();
  2240. return result;
  2241. }
  2242. map<string, int32> World::GetOldestReloadingSubsystem() {
  2243. map<string, int32> result;
  2244. MReloadingSubsystems.lock();
  2245. int32 current_time = Timer::GetCurrentTime2();
  2246. map<string, int32>::iterator itr;
  2247. int32 oldest = current_time;
  2248. string oldestname = "";
  2249. for (itr = reloading_subsystems.begin(); itr != reloading_subsystems.end(); itr++) {
  2250. if (itr->second < oldest) {
  2251. oldestname = itr->first;
  2252. result.clear();
  2253. result[oldestname] = oldest;
  2254. }
  2255. }
  2256. MReloadingSubsystems.unlock();
  2257. return result;
  2258. }
  2259. void ZoneList::WatchdogHeartbeat()
  2260. {
  2261. list<ZoneServer*>::iterator zone_iter;
  2262. ZoneServer* tmp = 0;
  2263. MZoneList.writelock(__FUNCTION__, __LINE__);
  2264. bool match = false;
  2265. for (zone_iter = zlist.begin(); zone_iter != zlist.end(); zone_iter++)
  2266. {
  2267. tmp = *zone_iter;
  2268. if (tmp)
  2269. {
  2270. int32 curTime = Timer::GetCurrentTime2();
  2271. sint64 diff = (sint64)curTime - (sint64)tmp->GetWatchdogTime();
  2272. if (diff > 120000)
  2273. {
  2274. LogWrite(WORLD__ERROR, 1, "World", "Zone %s is hung for %i milliseconds.. attempting to cancel threads...", tmp->GetZoneName(), diff);
  2275. #ifndef WIN32
  2276. tmp->CancelThreads();
  2277. zlist.erase(zone_iter);
  2278. safe_delete(tmp);
  2279. #endif
  2280. MZoneList.releasewritelock(__FUNCTION__, __LINE__);
  2281. match = true;
  2282. break;
  2283. }
  2284. else if (diff > 90000 && !tmp->isZoneShuttingDown())
  2285. {
  2286. tmp->SetWatchdogTime(Timer::GetCurrentTime2()); // reset so we don't continuously flood this heartbeat
  2287. map<string, int32> oldest_process = world.GetOldestReloadingSubsystem();
  2288. if (oldest_process.size() > 0) {
  2289. map<string, int32>::iterator itr = oldest_process.begin();
  2290. if(itr != oldest_process.end())
  2291. LogWrite(WORLD__ERROR, 1, "World", "Zone %s is hung for %i milliseconds.. while waiting for %s to reload...attempting shutdown", tmp->GetZoneName(), diff, itr->first);
  2292. else
  2293. LogWrite(WORLD__ERROR, 1, "World", "Zone %s is hung for %i milliseconds... attempting shutdown", tmp->GetZoneName(), diff);
  2294. }
  2295. else
  2296. LogWrite(WORLD__ERROR, 1, "World", "Zone %s is hung for %i milliseconds.. attempting shutdown", tmp->GetZoneName(), diff);
  2297. tmp->Shutdown();
  2298. }
  2299. else if (diff > 30000)
  2300. {
  2301. if (world.IsReloadingSubsystems()) {
  2302. if (world.GetSuppressedWarningTime() == 0) {
  2303. world.SetSuppressedWarning();
  2304. map<string, int32> oldest_process = world.GetOldestReloadingSubsystem();
  2305. if (oldest_process.size() > 0) {
  2306. map<string, int32>::iterator itr = oldest_process.begin();
  2307. if(itr != oldest_process.end())
  2308. LogWrite(WORLD__ERROR, 1, "World", "Zone %s is hung for %i milliseconds.. while waiting for %s to reload...", tmp->GetZoneName(), diff, itr->first);
  2309. else
  2310. LogWrite(WORLD__ERROR, 1, "World", "Zone %s is hung for %i milliseconds...", tmp->GetZoneName(), diff);
  2311. }
  2312. }
  2313. continue;
  2314. }
  2315. }
  2316. else if (diff > 60000 && !tmp->isZoneShuttingDown())
  2317. {
  2318. if (world.IsReloadingSubsystems())
  2319. continue;
  2320. LogWrite(WORLD__ERROR, 1, "World", "Zone %s is hung for %i milliseconds.. attempting shutdown", tmp->GetZoneName(), diff);
  2321. tmp->Shutdown();
  2322. }
  2323. }
  2324. }
  2325. if(!match)
  2326. MZoneList.releasewritelock(__FUNCTION__, __LINE__);
  2327. }
  2328. void World::LoadRegionMaps(std::string zoneFile)
  2329. {
  2330. string zoneToLower(zoneFile);
  2331. boost::algorithm::to_lower(zoneToLower);
  2332. MWorldRegionMaps.writelock();
  2333. std::map<std::string, RegionMapRange*>::iterator itr;
  2334. itr = region_maps.find(zoneToLower);
  2335. if (itr == region_maps.end())
  2336. {
  2337. RegionMapRange* newRange = new RegionMapRange();
  2338. newRange->AddVersionRange(zoneFile);
  2339. region_maps.insert(make_pair(zoneToLower, newRange));
  2340. }
  2341. MWorldRegionMaps.releasewritelock();
  2342. }
  2343. RegionMap* World::GetRegionMap(std::string zoneFile, int32 client_version)
  2344. {
  2345. string zoneToLower(zoneFile);
  2346. boost::algorithm::to_lower(zoneToLower);
  2347. MWorldRegionMaps.readlock();
  2348. std::map<std::string, RegionMapRange*>::iterator itr;
  2349. itr = region_maps.find(zoneToLower);
  2350. if ( itr != region_maps.end())
  2351. {
  2352. std::map<VersionRange*, RegionMap*>::iterator rmitr;
  2353. rmitr = itr->second->FindRegionByVersion(client_version);
  2354. if ( rmitr != itr->second->GetRangeEnd())
  2355. {
  2356. MWorldRegionMaps.releasereadlock();
  2357. return rmitr->second;
  2358. }
  2359. }
  2360. MWorldRegionMaps.releasereadlock();
  2361. return nullptr;
  2362. }
  2363. void World::LoadMaps(std::string zoneFile)
  2364. {
  2365. string zoneToLower(zoneFile);
  2366. boost::algorithm::to_lower(zoneToLower);
  2367. MWorldMaps.writelock();
  2368. std::map<std::string, MapRange*>::iterator itr;
  2369. itr = maps.find(zoneToLower);
  2370. if (itr == maps.end())
  2371. {
  2372. MapRange* newRange = new MapRange();
  2373. newRange->AddVersionRange(zoneFile);
  2374. maps.insert(make_pair(zoneToLower, newRange));
  2375. }
  2376. MWorldMaps.releasewritelock();
  2377. }
  2378. void World::RemoveMaps(std::string zoneFile)
  2379. {
  2380. string zoneToLower(zoneFile);
  2381. boost::algorithm::to_lower(zoneToLower);
  2382. MWorldMaps.writelock();
  2383. std::map<std::string, MapRange*>::iterator itr;
  2384. itr = maps.find(zoneToLower);
  2385. if (itr != maps.end())
  2386. {
  2387. MapRange* range = itr->second;
  2388. maps.erase(itr);
  2389. MWorldMaps.releasewritelock();
  2390. safe_delete(range);
  2391. }
  2392. else {
  2393. MWorldMaps.releasewritelock();
  2394. }
  2395. }
  2396. Map* World::GetMap(std::string zoneFile, int32 client_version)
  2397. {
  2398. string zoneToLower(zoneFile);
  2399. boost::algorithm::to_lower(zoneToLower);
  2400. MWorldMaps.readlock();
  2401. std::map<std::string, MapRange*>::iterator itr;
  2402. itr = maps.find(zoneToLower);
  2403. if ( itr != maps.end())
  2404. {
  2405. std::map<VersionRange*, Map*>::iterator rmitr;
  2406. rmitr = itr->second->FindMapByVersion(client_version);
  2407. if ( rmitr != itr->second->GetRangeEnd())
  2408. {
  2409. MWorldMaps.releasereadlock();
  2410. return rmitr->second;
  2411. }
  2412. }
  2413. MWorldMaps.releasereadlock();
  2414. return nullptr;
  2415. }
  2416. void World::SendTimeUpdate()
  2417. {
  2418. zone_list.SendTimeUpdate();
  2419. }
  2420. void World::LoadVoiceOvers()
  2421. {
  2422. LogWrite(WORLD__DEBUG, 1, "World", "-Loading `voiceovers`...");
  2423. database.LoadVoiceOvers(this);
  2424. }
  2425. void World::PurgeVoiceOvers()
  2426. {
  2427. MVoiceOvers.writelock();
  2428. for(int type=0;type<MAX_VOICEOVER_TYPE+1;type++) {
  2429. multimap<int32, multimap<int16, VoiceOverStruct>*>::iterator vos_itr;
  2430. for (vos_itr = voiceover_map[type].begin(); vos_itr != voiceover_map[type].end(); vos_itr++)
  2431. {
  2432. multimap<int16, VoiceOverStruct>* tmpMap = vos_itr->second;
  2433. safe_delete(tmpMap);
  2434. }
  2435. voiceover_map[type].clear();
  2436. }
  2437. MVoiceOvers.releasewritelock();
  2438. }
  2439. bool World::FindVoiceOver(int8 type, int32 id, int16 index, VoiceOverStruct* struct_, bool* find_garbled, VoiceOverStruct* garble_struct_) {
  2440. // if we complete both requirements, based on struct_ and garble_struct_ being passed when required by ptr not being null
  2441. bool succeed = false;
  2442. if(type > MAX_VOICEOVER_TYPE) {
  2443. LogWrite(WORLD__ERROR, 0, "World", "Voice over %u out of range, max voiceover type is %u...", type, MAX_VOICEOVER_TYPE);
  2444. return succeed;
  2445. }
  2446. MVoiceOvers.readlock();
  2447. multimap<int32, multimap<int16, VoiceOverStruct>*>::iterator itr = voiceover_map[type].find(id);
  2448. if(itr != voiceover_map[type].end()) {
  2449. std::pair<VOMapIterator, VOMapIterator> result = itr->second->equal_range(index);
  2450. int count = std::distance(result.first, result.second);
  2451. bool tries_attempt = true; // abort out the while loop
  2452. bool non_garble_found = false;
  2453. int rand = 0; // use to randomize the voiceover selection
  2454. int pos = 0;
  2455. int tries = 0;
  2456. bool has_ungarbled = false;
  2457. bool has_garbled = false;
  2458. int8 garble_link_id = 0; // used to match ungarbled to garbled when the link id is set in the DB
  2459. while(tries_attempt) {
  2460. pos = 0;
  2461. rand = MakeRandomInt(0, count);
  2462. if ( tries == 3 || non_garble_found || (find_garbled && (*find_garbled)))
  2463. rand = 0; // override, too many tries, or we otherwise found one garbled/ungarbled lets try to link it
  2464. for (VOMapIterator it = result.first; it != result.second; it++) {
  2465. if(!it->second.is_garbled) {
  2466. has_ungarbled = true;
  2467. }
  2468. else {
  2469. has_garbled = true;
  2470. }
  2471. pos++;
  2472. // if there is only 1 entry in the voiceover list we aren't going to bother skipping
  2473. if(count > 1 && pos < rand) {
  2474. continue;
  2475. }
  2476. if(!it->second.is_garbled && (garble_link_id == 0 || it->second.garble_link_id == garble_link_id)) {
  2477. garble_link_id = it->second.garble_link_id;
  2478. non_garble_found = true;
  2479. if(struct_) {
  2480. CopyVoiceOver(struct_, &it->second);
  2481. }
  2482. if(!find_garbled || ((*find_garbled))) {
  2483. if(find_garbled)
  2484. *find_garbled = true;
  2485. tries_attempt = false;
  2486. succeed = true;
  2487. break;
  2488. }
  2489. }
  2490. else if(find_garbled && !(*find_garbled) && it->second.is_garbled && (garble_link_id == 0 || it->second.garble_link_id == garble_link_id)) {
  2491. *find_garbled = true;
  2492. garble_link_id = it->second.garble_link_id;
  2493. if(garble_struct_) {
  2494. CopyVoiceOver(garble_struct_, &it->second);
  2495. if(!struct_ || non_garble_found) {
  2496. tries_attempt = false;
  2497. succeed = true;
  2498. break;
  2499. }
  2500. }
  2501. }
  2502. }
  2503. tries++;
  2504. if(!tries_attempt || (tries > 0 && !has_ungarbled && (!find_garbled || *find_garbled == true || !has_garbled)) || tries > 3)
  2505. break;
  2506. }
  2507. }
  2508. MVoiceOvers.releasereadlock();
  2509. return succeed;
  2510. }
  2511. void World::AddVoiceOver(int8 type, int32 id, int16 index, VoiceOverStruct* struct_) {
  2512. if(type > MAX_VOICEOVER_TYPE) {
  2513. LogWrite(WORLD__ERROR, 0, "World", "Voice over %u out of range, max voiceover type is %u...", type, MAX_VOICEOVER_TYPE);
  2514. return;
  2515. }
  2516. VoiceOverStruct tmpStruct;
  2517. tmpStruct.mp3_string = std::string(struct_->mp3_string);
  2518. tmpStruct.text_string = std::string(struct_->text_string);
  2519. tmpStruct.emote_string = std::string(struct_->emote_string);
  2520. tmpStruct.key1 = struct_->key1;
  2521. tmpStruct.key2 = struct_->key2;
  2522. tmpStruct.is_garbled = struct_->is_garbled;
  2523. MVoiceOvers.writelock();
  2524. if(!voiceover_map[type].count(id))
  2525. {
  2526. multimap<int16, VoiceOverStruct>* vo_struct = new multimap<int16, VoiceOverStruct>();
  2527. vo_struct->insert(make_pair(index, tmpStruct));
  2528. voiceover_map[type].insert(make_pair(id, vo_struct));
  2529. }
  2530. else
  2531. {
  2532. multimap<int32, multimap<int16, VoiceOverStruct>*>::const_iterator itr = voiceover_map[type].find(id);
  2533. itr->second->insert(make_pair(index, tmpStruct));
  2534. }
  2535. MVoiceOvers.releasewritelock();
  2536. }
  2537. void World::CopyVoiceOver(VoiceOverStruct* struct1, VoiceOverStruct* struct2) {
  2538. if(!struct1 || !struct2)
  2539. return;
  2540. struct1->mp3_string = std::string(struct2->mp3_string);
  2541. struct1->text_string = std::string(struct2->text_string);
  2542. struct1->emote_string = std::string(struct2->emote_string);
  2543. struct1->key1 = struct2->key1;
  2544. struct1->key2 = struct2->key2;
  2545. struct1->is_garbled = struct2->is_garbled;
  2546. struct1->garble_link_id = struct2->garble_link_id;
  2547. }
  2548. void World::AddNPCSpell(int32 list_id, int32 spell_id, int8 tier, bool spawn_cast, bool aggro_cast, sint8 req_hp_ratio){
  2549. std::unique_lock lock(MNPCSpells);
  2550. NPCSpell* npc_spell_struct = new NPCSpell;
  2551. npc_spell_struct->list_id = list_id;
  2552. npc_spell_struct->spell_id = spell_id;
  2553. npc_spell_struct->tier = tier;
  2554. npc_spell_struct->cast_on_spawn = spawn_cast;
  2555. npc_spell_struct->cast_on_initial_aggro = aggro_cast;
  2556. npc_spell_struct->required_hp_ratio = req_hp_ratio;
  2557. if(npc_spell_list.count(list_id) && npc_spell_list[list_id].count(spell_id)) {
  2558. map<int32, NPCSpell*>::iterator spell_itr = npc_spell_list[list_id].find(spell_id);
  2559. if(spell_itr != npc_spell_list[list_id].end()) {
  2560. safe_delete(spell_itr->second);
  2561. npc_spell_list[list_id].erase(spell_itr);
  2562. }
  2563. }
  2564. npc_spell_list[list_id].insert(make_pair(spell_id, npc_spell_struct));
  2565. }
  2566. vector<NPCSpell*>* World::GetNPCSpells(int32 primary_list, int32 secondary_list){
  2567. std::shared_lock lock(MNPCSpells);
  2568. vector<NPCSpell*>* ret = 0;
  2569. if(npc_spell_list.count(primary_list) > 0){
  2570. ret = new vector<NPCSpell*>();
  2571. map<int32, NPCSpell*>::iterator itr;
  2572. Spell* tmpSpell = 0;
  2573. for(itr = npc_spell_list[primary_list].begin(); itr != npc_spell_list[primary_list].end(); itr++){
  2574. tmpSpell = master_spell_list.GetSpell(itr->first, itr->second->tier);
  2575. if(tmpSpell) {
  2576. NPCSpell* addedSpell = new NPCSpell(itr->second);
  2577. ret->push_back(addedSpell);
  2578. }
  2579. }
  2580. }
  2581. if(npc_spell_list.count(secondary_list) > 0){
  2582. if(!ret)
  2583. ret = new vector<NPCSpell*>();
  2584. map<int32, NPCSpell*>::iterator itr;
  2585. Spell* tmpSpell = 0;
  2586. for(itr = npc_spell_list[secondary_list].begin(); itr != npc_spell_list[secondary_list].end(); itr++){
  2587. tmpSpell = master_spell_list.GetSpell(itr->first, itr->second->tier);
  2588. if(tmpSpell) {
  2589. NPCSpell* addedSpell = new NPCSpell(itr->second);
  2590. ret->push_back(addedSpell);
  2591. }
  2592. }
  2593. }
  2594. if(ret && ret->size() == 0){
  2595. safe_delete(ret);
  2596. ret = 0;
  2597. }
  2598. return ret;
  2599. }
  2600. void World::PurgeNPCSpells() {
  2601. std::unique_lock lock(MNPCSpells);
  2602. map<int32, map<int32, NPCSpell*> >::iterator list_itr;
  2603. map<int32, NPCSpell*>::iterator spell_itr;
  2604. Spell* tmpSpell = 0;
  2605. for(list_itr = npc_spell_list.begin(); list_itr != npc_spell_list.end(); list_itr++) {
  2606. for(spell_itr = npc_spell_list[list_itr->first].begin(); spell_itr != npc_spell_list[list_itr->first].end(); spell_itr++){
  2607. safe_delete(spell_itr->second);
  2608. }
  2609. }
  2610. npc_spell_list.clear();
  2611. }