World.cpp 93 KB

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