World.cpp 87 KB

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