9
3

World.cpp 93 KB

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