World.cpp 77 KB

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