World.cpp 79 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535
  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. bool World::RejoinGroup(Client* client, int32 group_id){
  1291. if (!group_id) // no need if no group id!
  1292. return false;
  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 false;
  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. return match;
  1335. }
  1336. void World::AddBonuses(ItemStatsValues* values, int16 type, sint32 value, Entity* entity){
  1337. if(values){
  1338. switch(type){
  1339. case ITEM_STAT_STR:{
  1340. values->str += value;
  1341. break;
  1342. }
  1343. case ITEM_STAT_STA:{
  1344. values->sta += value;
  1345. break;
  1346. }
  1347. case ITEM_STAT_AGI:{
  1348. values->agi += value;
  1349. break;
  1350. }
  1351. case ITEM_STAT_WIS:{
  1352. values->wis += value;
  1353. break;
  1354. }
  1355. case ITEM_STAT_INT:{
  1356. values->int_ += value;
  1357. break;
  1358. }
  1359. case ITEM_STAT_VS_SLASH:{
  1360. values->vs_slash += value;
  1361. break;
  1362. }
  1363. case ITEM_STAT_VS_CRUSH:{
  1364. values->vs_crush += value;
  1365. break;
  1366. }
  1367. case ITEM_STAT_VS_PIERCE:{
  1368. values->vs_pierce += value;
  1369. break;
  1370. }
  1371. case ITEM_STAT_VS_HEAT:{
  1372. values->vs_heat += value;
  1373. break;
  1374. }
  1375. case ITEM_STAT_VS_COLD:{
  1376. values->vs_cold += value;
  1377. break;
  1378. }
  1379. case ITEM_STAT_VS_MAGIC:{
  1380. values->vs_magic += value;
  1381. break;
  1382. }
  1383. case ITEM_STAT_VS_MENTAL:{
  1384. values->vs_mental += value;
  1385. break;
  1386. }
  1387. case ITEM_STAT_VS_DIVINE:{
  1388. values->vs_divine += value;
  1389. break;
  1390. }
  1391. case ITEM_STAT_VS_DISEASE:{
  1392. values->vs_disease += value;
  1393. break;
  1394. }
  1395. case ITEM_STAT_VS_POISON:{
  1396. values->vs_poison += value;
  1397. break;
  1398. }
  1399. case ITEM_STAT_HEALTH:{
  1400. values->health += value;
  1401. break;
  1402. }
  1403. case ITEM_STAT_POWER:{
  1404. values->power += value;
  1405. break;
  1406. }
  1407. case ITEM_STAT_CONCENTRATION:{
  1408. values->concentration += value;
  1409. break;
  1410. }
  1411. case ITEM_STAT_ABILITY_MODIFIER:{
  1412. values->ability_modifier += value;
  1413. break;
  1414. }
  1415. case ITEM_STAT_CRITICALMITIGATION:{
  1416. values->criticalmitigation += value;
  1417. break;
  1418. }
  1419. case ITEM_STAT_EXTRASHIELDBLOCKCHANCE:{
  1420. values->extrashieldblockchance += value;
  1421. break;
  1422. }
  1423. case ITEM_STAT_BENEFICIALCRITCHANCE:{
  1424. values->beneficialcritchance += value;
  1425. break;
  1426. }
  1427. case ITEM_STAT_CRITBONUS:{
  1428. values->critbonus += value;
  1429. break;
  1430. }
  1431. /*case ITEM_STAT_POTENCY:{
  1432. values->potency += value;
  1433. break;
  1434. }*/
  1435. case ITEM_STAT_HATEGAINMOD:{
  1436. values->hategainmod += value;
  1437. break;
  1438. }
  1439. case ITEM_STAT_ABILITYREUSESPEED:{
  1440. values->abilityreusespeed += value;
  1441. break;
  1442. }
  1443. case ITEM_STAT_ABILITYCASTINGSPEED:{
  1444. values->abilitycastingspeed += value;
  1445. break;
  1446. }
  1447. case ITEM_STAT_ABILITYRECOVERYSPEED:{
  1448. values->abilityrecoveryspeed += value;
  1449. break;
  1450. }
  1451. case ITEM_STAT_SPELLREUSESPEED:{
  1452. values->spellreusespeed += value;
  1453. break;
  1454. }
  1455. case ITEM_STAT_SPELLMULTIATTACKCHANCE:{
  1456. values->spellmultiattackchance += value;
  1457. break;
  1458. }
  1459. case ITEM_STAT_DPS:{
  1460. values->dps += value;
  1461. break;
  1462. }
  1463. case ITEM_STAT_ATTACKSPEED:{
  1464. values->attackspeed += value;
  1465. break;
  1466. }
  1467. case ITEM_STAT_MULTIATTACKCHANCE:{
  1468. values->multiattackchance += value;
  1469. break;
  1470. }
  1471. case ITEM_STAT_AEAUTOATTACKCHANCE:{
  1472. values->aeautoattackchance += value;
  1473. break;
  1474. }
  1475. case ITEM_STAT_STRIKETHROUGH:{
  1476. values->strikethrough += value;
  1477. break;
  1478. }
  1479. case ITEM_STAT_ACCURACY:{
  1480. values->accuracy += value;
  1481. break;
  1482. }
  1483. /*case ITEM_STAT_OFFENSIVESPEED:{
  1484. values->offensivespeed += value;
  1485. break;
  1486. }*/
  1487. default: {
  1488. if (entity)
  1489. entity->stats[type] += value;
  1490. break;
  1491. }
  1492. }
  1493. }
  1494. }
  1495. void World::CreateGuild(const char* guild_name, Client* leader, int32 group_id) {
  1496. deque<GroupMemberInfo*>::iterator itr;
  1497. GroupMemberInfo* gmi;
  1498. Guild *guild;
  1499. assert(guild_name);
  1500. guild = new Guild();
  1501. guild->SetName(guild_name);
  1502. guild->SetFormedDate(Timer::GetUnixTimeStamp());
  1503. database.LoadGuildDefaultRanks(guild);
  1504. database.LoadGuildDefaultEventFilters(guild);
  1505. database.SaveGuild(guild, true);
  1506. database.SaveGuildEvents(guild);
  1507. database.SaveGuildRanks(guild);
  1508. database.SaveGuildEventFilters(guild);
  1509. database.SaveGuildRecruiting(guild);
  1510. guild_list.AddGuild(guild);
  1511. if (leader && !leader->GetPlayer()->GetGuild())
  1512. guild->AddNewGuildMember(leader, 0, GUILD_RANK_LEADER);
  1513. database.SaveGuildMembers(guild);
  1514. if (leader && group_id > 0) {
  1515. GetGroupManager()->GroupLock(__FUNCTION__, __LINE__);
  1516. PlayerGroup* group = world.GetGroupManager()->GetGroup(group_id);
  1517. if (group)
  1518. {
  1519. group->MGroupMembers.readlock(__FUNCTION__, __LINE__);
  1520. deque<GroupMemberInfo*>* members = group->GetMembers();
  1521. for (itr = members->begin(); itr != members->end(); itr++) {
  1522. gmi = *itr;
  1523. if (gmi->client && gmi->client != leader && !gmi->client->GetPlayer()->GetGuild())
  1524. guild->InvitePlayer(gmi->client, leader->GetPlayer()->GetName());
  1525. }
  1526. group->MGroupMembers.releasereadlock(__FUNCTION__, __LINE__);
  1527. }
  1528. GetGroupManager()->ReleaseGroupLock(__FUNCTION__, __LINE__);
  1529. }
  1530. }
  1531. void World::SaveGuilds() {
  1532. MutexMap<int32, Guild*>* guilds = guild_list.GetGuilds();
  1533. MutexMap<int32, Guild*>::iterator itr = guilds->begin();
  1534. while (itr.Next()) {
  1535. Guild* guild = itr.second;
  1536. if (guild->GetSaveNeeded())
  1537. database.SaveGuild(guild);
  1538. if (guild->GetMemberSaveNeeded())
  1539. database.SaveGuildMembers(guild);
  1540. if (guild->GetEventsSaveNeeded())
  1541. database.SaveGuildEvents(guild);
  1542. if (guild->GetRanksSaveNeeded())
  1543. database.SaveGuildRanks(guild);
  1544. if (guild->GetEventFiltersSaveNeeded())
  1545. database.SaveGuildEventFilters(guild);
  1546. if (guild->GetPointsHistorySaveNeeded())
  1547. database.SaveGuildPointsHistory(guild);
  1548. if (guild->GetRecruitingSaveNeeded())
  1549. database.SaveGuildRecruiting(guild);
  1550. }
  1551. }
  1552. void World::PickRandomLottoDigits(int32* digits) {
  1553. if (digits) {
  1554. for (int32 i = 0; i < 6; i++) {
  1555. bool found = true;
  1556. int32 num = 0;
  1557. while (found) {
  1558. num = ((int32)rand() % 36) + 1;
  1559. for (int32 j = 0; j < 6; j++) {
  1560. if (digits[j] == num)
  1561. break;
  1562. if (j == 5)
  1563. found = false;
  1564. }
  1565. }
  1566. digits[i] = num;
  1567. }
  1568. }
  1569. }
  1570. void World::AddLottoPlayer(int32 character_id, int32 end_time) {
  1571. LottoPlayer* lp;
  1572. if (lotto_players.count(character_id) == 0) {
  1573. lp = new LottoPlayer;
  1574. lotto_players.Put(character_id, lp);
  1575. }
  1576. else
  1577. lp = lotto_players.Get(character_id);
  1578. lp->end_time = end_time;
  1579. lp->num_matches = 0;
  1580. lp->set = false;
  1581. }
  1582. void World::RemoveLottoPlayer(int32 character_id) {
  1583. if (lotto_players.count(character_id) > 0)
  1584. lotto_players.erase(character_id, false, true);
  1585. }
  1586. void World::SetLottoPlayerNumMatches(int32 character_id, int8 num_matches) {
  1587. if (lotto_players.count(character_id) > 0) {
  1588. lotto_players.Get(character_id)->num_matches = num_matches;
  1589. lotto_players.Get(character_id)->set = true;
  1590. }
  1591. }
  1592. void World::CheckLottoPlayers() {
  1593. MutexMap<int32, LottoPlayer*>::iterator itr = lotto_players.begin();
  1594. while (itr.Next()) {
  1595. LottoPlayer* lp = itr->second;
  1596. if (Timer::GetCurrentTime2() >= lp->end_time && lp->set) {
  1597. int8 num_matches = lp->num_matches;
  1598. LogWrite(PLAYER__DEBUG, 0, "Player", "Num matches: %u", lp->num_matches);
  1599. Client* client = zone_list.GetClientByCharID(itr->first);
  1600. if (client && num_matches >= 2) {
  1601. if (num_matches == 2) {
  1602. client->SimpleMessage(CHANNEL_COLOR_YELLOW, "You receive 10 silver.");
  1603. client->SendPopupMessage(0, "Congratulations! You have won 10 silver!", "", 2, 0xFF, 0xFF, 0x99);
  1604. client->GetPlayer()->AddCoins(1000);
  1605. client->GetPlayer()->GetZone()->SendCastSpellPacket(869, client->GetPlayer());
  1606. }
  1607. else if (num_matches == 3) {
  1608. client->SimpleMessage(CHANNEL_COLOR_YELLOW, "You receive 50 silver.");
  1609. client->SendPopupMessage(0, "Congratulations! You have won 50 silver!", "", 2, 0xFF, 0xFF, 0x99);
  1610. client->GetPlayer()->AddCoins(5000);
  1611. client->GetPlayer()->GetZone()->SendCastSpellPacket(870, client->GetPlayer());
  1612. }
  1613. else if (num_matches == 4) {
  1614. client->SimpleMessage(CHANNEL_COLOR_YELLOW, "You receive 2 gold 50 silver.");
  1615. client->SendPopupMessage(0, "Congratulations! You have won 2 gold 50 silver!", "", 2, 0xFF, 0xFF, 0x99);
  1616. client->GetPlayer()->AddCoins(25000);
  1617. client->GetPlayer()->GetZone()->SendCastSpellPacket(871, client->GetPlayer());
  1618. }
  1619. else if (num_matches == 5) {
  1620. client->SimpleMessage(CHANNEL_COLOR_YELLOW, "You receive 25 gold.");
  1621. client->SendPopupMessage(0, "Congratulations! You have won 25 gold!", "", 2, 0xFF, 0xFF, 0x99);
  1622. client->GetPlayer()->AddCoins(250000);
  1623. client->GetPlayer()->GetZone()->SendCastSpellPacket(872, client->GetPlayer());
  1624. }
  1625. else if (num_matches == 6) {
  1626. Variable* var = variables.FindVariable("gambling_current_jackpot");
  1627. if (var) {
  1628. int64 jackpot = 0;
  1629. try {
  1630. jackpot = atoul(var->GetValue());
  1631. }
  1632. catch (...) {
  1633. jackpot = 10000;
  1634. }
  1635. char coin_message[128];
  1636. char message[512];
  1637. char announcement[512];
  1638. memset(coin_message, 0, sizeof(coin_message));
  1639. memset(message, 0, sizeof(message));
  1640. memset(announcement, 0, sizeof(announcement));
  1641. sprintf(coin_message, "%s", client->GetCoinMessage(jackpot).c_str());
  1642. sprintf(message, "Congratulations! You have won %s!", coin_message);
  1643. sprintf(announcement, "%s as won the jackpot containing a total of %s!", client->GetPlayer()->GetName(), coin_message);
  1644. client->Message(CHANNEL_COLOR_YELLOW, "You receive %s.", coin_message);
  1645. client->SendPopupMessage(0, message, "", 2, 0xFF, 0xFF, 0x99);
  1646. zone_list.HandleGlobalAnnouncement(announcement);
  1647. client->GetPlayer()->AddCoins(jackpot);
  1648. client->GetPlayer()->GetZone()->SendCastSpellPacket(843, client->GetPlayer());
  1649. client->GetPlayer()->GetZone()->SendCastSpellPacket(1413, client->GetPlayer());
  1650. }
  1651. }
  1652. }
  1653. RemoveLottoPlayer(itr->first);
  1654. }
  1655. }
  1656. }
  1657. 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) {
  1658. MHouseZones.writelock(__FUNCTION__, __LINE__);
  1659. if (m_houseZones.count(id) == 0) {
  1660. HouseZone* hz = new HouseZone;
  1661. //ZeroMemory(hz, sizeof(HouseZone));
  1662. hz->id = id;
  1663. hz->name = name;
  1664. hz->cost_coin = cost_coins;
  1665. hz->cost_status = cost_status;
  1666. hz->upkeep_coin = upkeep_coins;
  1667. hz->upkeep_status = upkeep_status;
  1668. hz->vault_slots = vault_slots;
  1669. hz->alignment = alignment;
  1670. hz->guild_level = guild_level;
  1671. hz->zone_id = zone_id;
  1672. hz->exit_zone_id = exit_zone_id;
  1673. hz->exit_x = exit_x;
  1674. hz->exit_y = exit_y;
  1675. hz->exit_z = exit_z;
  1676. hz->exit_heading = exit_heading;
  1677. m_houseZones[id] = hz;
  1678. }
  1679. else {
  1680. LogWrite(WORLD__ERROR, 0, "Housing", "Duplicate house id (%u) for %s", id, name.c_str());
  1681. }
  1682. MHouseZones.releasewritelock(__FUNCTION__, __LINE__);
  1683. }
  1684. HouseZone* World::GetHouseZone(int32 id) {
  1685. HouseZone* ret = 0;
  1686. MHouseZones.readlock(__FUNCTION__, __LINE__);
  1687. if (m_houseZones.count(id) > 0)
  1688. ret = m_houseZones[id];
  1689. MHouseZones.releasereadlock(__FUNCTION__, __LINE__);
  1690. return ret;
  1691. }
  1692. 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) {
  1693. MPlayerHouses.writelock(__FUNCTION__, __LINE__);
  1694. if (m_playerHouses.count(house_id) == 0 || m_playerHouses[house_id].count(char_id) == 0) {
  1695. PlayerHouse* ph = new PlayerHouse;
  1696. ph->house_id = house_id;
  1697. ph->unique_id = unique_id;
  1698. ph->instance_id = instance_id;
  1699. ph->escrow_coins = escrow_coins;
  1700. ph->escrow_status = escrow_status;
  1701. ph->upkeep_due = upkeep_due;
  1702. ph->player_name = player_name;
  1703. ReloadHouseData(ph);
  1704. m_playerHouses[house_id][char_id] = ph;
  1705. }
  1706. MPlayerHouses.releasewritelock(__FUNCTION__, __LINE__);
  1707. }
  1708. void World::ReloadHouseData(PlayerHouse* ph)
  1709. {
  1710. database.LoadDeposits(ph);
  1711. database.LoadHistory(ph);
  1712. }
  1713. PlayerHouse* World::GetPlayerHouseByHouseID(int32 char_id, int32 house_id) {
  1714. PlayerHouse* ret = 0;
  1715. MPlayerHouses.readlock(__FUNCTION__, __LINE__);
  1716. if (m_playerHouses.count(house_id) > 0 && m_playerHouses[house_id].count(char_id) > 0)
  1717. ret = m_playerHouses[house_id][char_id];
  1718. MPlayerHouses.releasereadlock(__FUNCTION__, __LINE__);
  1719. return ret;
  1720. }
  1721. PlayerHouse* World::GetPlayerHouseByUniqueID(int64 unique_id) {
  1722. PlayerHouse* ret = 0;
  1723. MPlayerHouses.readlock(__FUNCTION__, __LINE__);
  1724. map<int32, map<int32, PlayerHouse*> >::iterator itr;
  1725. for (itr = m_playerHouses.begin(); itr != m_playerHouses.end(); itr++) {
  1726. map<int32, PlayerHouse*>::iterator itr2;
  1727. for (itr2 = itr->second.begin(); itr2 != itr->second.end(); itr2++) {
  1728. if (itr2->second->unique_id == unique_id) {
  1729. ret = itr2->second;
  1730. break;
  1731. }
  1732. }
  1733. if (ret)
  1734. break;
  1735. }
  1736. MPlayerHouses.releasereadlock(__FUNCTION__, __LINE__);
  1737. return ret;
  1738. }
  1739. PlayerHouse* World::GetPlayerHouseByInstanceID(int32 instance_id) {
  1740. PlayerHouse* ret = 0;
  1741. MPlayerHouses.readlock(__FUNCTION__, __LINE__);
  1742. map<int32, map<int32, PlayerHouse*> >::iterator itr;
  1743. for (itr = m_playerHouses.begin(); itr != m_playerHouses.end(); itr++) {
  1744. map<int32, PlayerHouse*>::iterator itr2;
  1745. for (itr2 = itr->second.begin(); itr2 != itr->second.end(); itr2++) {
  1746. if (itr2->second->instance_id == instance_id) {
  1747. ret = itr2->second;
  1748. break;
  1749. }
  1750. }
  1751. if (ret)
  1752. break;
  1753. }
  1754. MPlayerHouses.releasereadlock(__FUNCTION__, __LINE__);
  1755. return ret;
  1756. }
  1757. vector<PlayerHouse*> World::GetAllPlayerHouses(int32 char_id) {
  1758. vector<PlayerHouse*> ret;
  1759. MPlayerHouses.readlock(__FUNCTION__, __LINE__);
  1760. map<int32, map<int32, PlayerHouse*> >::iterator itr;
  1761. for (itr = m_playerHouses.begin(); itr != m_playerHouses.end(); itr++) {
  1762. if (itr->second.count(char_id) > 0)
  1763. ret.push_back(itr->second[char_id]);
  1764. }
  1765. MPlayerHouses.releasereadlock(__FUNCTION__, __LINE__);
  1766. return ret;
  1767. }
  1768. vector<PlayerHouse*> World::GetAllPlayerHousesByHouseID(int32 house_id) {
  1769. vector<PlayerHouse*> ret;
  1770. MPlayerHouses.readlock(__FUNCTION__, __LINE__);
  1771. if (m_houseZones.count(house_id) > 0) {
  1772. map<int32, PlayerHouse*>::iterator itr;
  1773. for (itr = m_playerHouses[house_id].begin(); itr != m_playerHouses[house_id].end(); itr++)
  1774. ret.push_back(itr->second);
  1775. }
  1776. MPlayerHouses.releasereadlock(__FUNCTION__, __LINE__);
  1777. return ret;
  1778. }
  1779. void World::PopulateTOVStatMap() {
  1780. //This function populates a map that converts changed CoE to ToV stats
  1781. tov_itemstat_conversion[0] = TOV_ITEM_STAT_HPREGEN;
  1782. tov_itemstat_conversion[1] = TOV_ITEM_STAT_MANAREGEN;
  1783. tov_itemstat_conversion[2] = TOV_ITEM_STAT_HPREGENPPT;
  1784. tov_itemstat_conversion[3] = TOV_ITEM_STAT_MPREGENPPT;
  1785. tov_itemstat_conversion[4] = TOV_ITEM_STAT_COMBATHPREGENPPT;
  1786. tov_itemstat_conversion[5] = TOV_ITEM_STAT_COMBATMPREGENPPT;
  1787. tov_itemstat_conversion[6] = TOV_ITEM_STAT_MAXHP;
  1788. tov_itemstat_conversion[7] = TOV_ITEM_STAT_MAXHPPERC;
  1789. tov_itemstat_conversion[8] = TOV_ITEM_STAT_MAXHPPERCFINAL;
  1790. tov_itemstat_conversion[9] = TOV_ITEM_STAT_SPEED;
  1791. tov_itemstat_conversion[10] = TOV_ITEM_STAT_SLOW;
  1792. tov_itemstat_conversion[11] = TOV_ITEM_STAT_MOUNTSPEED;
  1793. tov_itemstat_conversion[12] = TOV_ITEM_STAT_MOUNTAIRSPEED;
  1794. tov_itemstat_conversion[13] = TOV_ITEM_STAT_LEAPSPEED;
  1795. tov_itemstat_conversion[14] = TOV_ITEM_STAT_LEAPTIME;
  1796. tov_itemstat_conversion[15] = TOV_ITEM_STAT_GLIDEEFFICIENCY;
  1797. tov_itemstat_conversion[16] = TOV_ITEM_STAT_OFFENSIVESPEED;
  1798. tov_itemstat_conversion[17] = TOV_ITEM_STAT_ATTACKSPEED;
  1799. tov_itemstat_conversion[18] = 698;
  1800. tov_itemstat_conversion[19] = TOV_ITEM_STAT_MAXMANA;
  1801. tov_itemstat_conversion[20] = TOV_ITEM_STAT_MAXMANAPERC;
  1802. tov_itemstat_conversion[21] = TOV_ITEM_STAT_MAXATTPERC;
  1803. tov_itemstat_conversion[22] = TOV_ITEM_STAT_BLURVISION;
  1804. tov_itemstat_conversion[23] = TOV_ITEM_STAT_MAGICLEVELIMMUNITY;
  1805. tov_itemstat_conversion[24] = TOV_ITEM_STAT_HATEGAINMOD;
  1806. tov_itemstat_conversion[25] = TOV_ITEM_STAT_COMBATEXPMOD;
  1807. tov_itemstat_conversion[26] = TOV_ITEM_STAT_TRADESKILLEXPMOD;
  1808. tov_itemstat_conversion[27] = TOV_ITEM_STAT_ACHIEVEMENTEXPMOD;
  1809. tov_itemstat_conversion[28] = TOV_ITEM_STAT_SIZEMOD;
  1810. tov_itemstat_conversion[29] = TOV_ITEM_STAT_DPS;
  1811. tov_itemstat_conversion[30] = 698;
  1812. tov_itemstat_conversion[31] = TOV_ITEM_STAT_STEALTH;
  1813. tov_itemstat_conversion[32] = TOV_ITEM_STAT_INVIS;
  1814. tov_itemstat_conversion[33] = TOV_ITEM_STAT_SEESTEALTH;
  1815. tov_itemstat_conversion[34] = TOV_ITEM_STAT_SEEINVIS;
  1816. tov_itemstat_conversion[35] = TOV_ITEM_STAT_EFFECTIVELEVELMOD;
  1817. tov_itemstat_conversion[36] = TOV_ITEM_STAT_RIPOSTECHANCE;
  1818. tov_itemstat_conversion[37] = TOV_ITEM_STAT_PARRYCHANCE;
  1819. tov_itemstat_conversion[38] = TOV_ITEM_STAT_DODGECHANCE;
  1820. tov_itemstat_conversion[39] = TOV_ITEM_STAT_AEAUTOATTACKCHANCE;
  1821. tov_itemstat_conversion[40] = 698;
  1822. tov_itemstat_conversion[41] = TOV_ITEM_STAT_MULTIATTACKCHANCE;
  1823. tov_itemstat_conversion[42] = 698;
  1824. tov_itemstat_conversion[43] = 698;
  1825. tov_itemstat_conversion[44] = 698;
  1826. tov_itemstat_conversion[45] = TOV_ITEM_STAT_SPELLMULTIATTACKCHANCE;
  1827. tov_itemstat_conversion[46] = 698;
  1828. tov_itemstat_conversion[47] = TOV_ITEM_STAT_FLURRY;
  1829. tov_itemstat_conversion[48] = 698;
  1830. tov_itemstat_conversion[49] = TOV_ITEM_STAT_MELEEDAMAGEMULTIPLIER;
  1831. tov_itemstat_conversion[50] = TOV_ITEM_STAT_EXTRAHARVESTCHANCE;
  1832. tov_itemstat_conversion[51] = TOV_ITEM_STAT_EXTRASHIELDBLOCKCHANCE;
  1833. tov_itemstat_conversion[52] = TOV_ITEM_STAT_ITEMHPREGENPPT;
  1834. tov_itemstat_conversion[53] = TOV_ITEM_STAT_ITEMPPREGENPPT;
  1835. tov_itemstat_conversion[54] = TOV_ITEM_STAT_MELEECRITCHANCE;
  1836. tov_itemstat_conversion[55] = TOV_ITEM_STAT_CRITAVOIDANCE;
  1837. tov_itemstat_conversion[56] = TOV_ITEM_STAT_BENEFICIALCRITCHANCE;
  1838. tov_itemstat_conversion[57] = TOV_ITEM_STAT_CRITBONUS;
  1839. tov_itemstat_conversion[58] = 698;
  1840. tov_itemstat_conversion[59] = TOV_ITEM_STAT_POTENCY;
  1841. tov_itemstat_conversion[60] = 698;
  1842. tov_itemstat_conversion[61] = TOV_ITEM_STAT_UNCONSCIOUSHPMOD;
  1843. tov_itemstat_conversion[62] = TOV_ITEM_STAT_ABILITYREUSESPEED;
  1844. tov_itemstat_conversion[63] = TOV_ITEM_STAT_ABILITYRECOVERYSPEED;
  1845. tov_itemstat_conversion[64] = TOV_ITEM_STAT_ABILITYCASTINGSPEED;
  1846. tov_itemstat_conversion[65] = TOV_ITEM_STAT_SPELLREUSESPEED;
  1847. tov_itemstat_conversion[66] = TOV_ITEM_STAT_MELEEWEAPONRANGE;
  1848. tov_itemstat_conversion[67] = TOV_ITEM_STAT_RANGEDWEAPONRANGE;
  1849. tov_itemstat_conversion[68] = TOV_ITEM_STAT_FALLINGDAMAGEREDUCTION;
  1850. tov_itemstat_conversion[69] = TOV_ITEM_STAT_RIPOSTEDAMAGE;
  1851. tov_itemstat_conversion[70] = TOV_ITEM_STAT_MINIMUMDEFLECTIONCHANCE;
  1852. tov_itemstat_conversion[71] = TOV_ITEM_STAT_MOVEMENTWEAVE;
  1853. tov_itemstat_conversion[72] = TOV_ITEM_STAT_COMBATHPREGEN;
  1854. tov_itemstat_conversion[73] = TOV_ITEM_STAT_COMBATMANAREGEN;
  1855. tov_itemstat_conversion[74] = TOV_ITEM_STAT_CONTESTSPEEDBOOST;
  1856. tov_itemstat_conversion[75] = TOV_ITEM_STAT_TRACKINGAVOIDANCE;
  1857. tov_itemstat_conversion[76] = TOV_ITEM_STAT_STEALTHINVISSPEEDMOD;
  1858. tov_itemstat_conversion[77] = TOV_ITEM_STAT_LOOT_COIN;
  1859. tov_itemstat_conversion[78] = TOV_ITEM_STAT_ARMORMITIGATIONINCREASE;
  1860. tov_itemstat_conversion[79] = TOV_ITEM_STAT_AMMOCONSERVATION;
  1861. tov_itemstat_conversion[80] = TOV_ITEM_STAT_STRIKETHROUGH;
  1862. tov_itemstat_conversion[81] = TOV_ITEM_STAT_STATUSBONUS;
  1863. tov_itemstat_conversion[82] = TOV_ITEM_STAT_ACCURACY;
  1864. tov_itemstat_conversion[83] = TOV_ITEM_STAT_COUNTERSTRIKE;
  1865. tov_itemstat_conversion[84] = TOV_ITEM_STAT_SHIELDBASH;
  1866. tov_itemstat_conversion[85] = TOV_ITEM_STAT_WEAPONDAMAGEBONUS;
  1867. tov_itemstat_conversion[86] = 698;
  1868. tov_itemstat_conversion[87] = TOV_ITEM_STAT_WEAPONDAMAGEBONUSMELEEONLY;
  1869. tov_itemstat_conversion[88] = TOV_ITEM_STAT_ADDITIONALRIPOSTECHANCE;
  1870. tov_itemstat_conversion[89] = TOV_ITEM_STAT_PVPTOUGHNESS;
  1871. tov_itemstat_conversion[90] = TOV_ITEM_STAT_PVPLETHALITY;
  1872. tov_itemstat_conversion[91] = TOV_ITEM_STAT_STAMINABONUS;
  1873. tov_itemstat_conversion[92] = TOV_ITEM_STAT_WISDOMMITBONUS;
  1874. tov_itemstat_conversion[93] = TOV_ITEM_STAT_HEALRECEIVE;
  1875. tov_itemstat_conversion[94] = TOV_ITEM_STAT_HEALRECEIVEPERC;
  1876. tov_itemstat_conversion[95] = TOV_ITEM_STAT_PVPCRITICALMITIGATION;
  1877. tov_itemstat_conversion[96] = TOV_ITEM_STAT_BASEAVOIDANCEBONUS;
  1878. tov_itemstat_conversion[97] = TOV_ITEM_STAT_INCOMBATSAVAGERYREGEN;
  1879. tov_itemstat_conversion[98] = TOV_ITEM_STAT_OUTOFCOMBATSAVAGERYREGEN;
  1880. tov_itemstat_conversion[99] = TOV_ITEM_STAT_SAVAGERYREGEN;
  1881. tov_itemstat_conversion[100] = TOV_ITEM_STAT_SAVAGERYGAINMOD;
  1882. tov_itemstat_conversion[101] = TOV_ITEM_STAT_MAXSAVAGERYLEVEL;
  1883. }
  1884. int32 World::LoadItemBlueStats() {
  1885. Query query;
  1886. MYSQL_ROW row;
  1887. int32 count = 0;
  1888. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT version_range1,version_range2,emu_stat,name,stat from itemstats");
  1889. if (result && mysql_num_rows(result) > 0) {
  1890. while (result && (row = mysql_fetch_row(result))) {
  1891. count++;
  1892. if (atoi(row[0]) >= 63119) //KA
  1893. ka_itemstat_conversion[atoi(row[2])] = atoi(row[4]);
  1894. else if (atoi(row[0]) >= 57101) // ToV
  1895. tov_itemstat_conversion[atoi(row[2])] = atoi(row[4]);
  1896. else if (atoi(row[0]) >= 1193) // CoE
  1897. coe_itemstat_conversion[atoi(row[2])] = atoi(row[4]);
  1898. else if (atoi(row[0]) >= 1096) // DoV
  1899. dov_itemstat_conversion[atoi(row[2])] = atoi(row[4]);
  1900. }
  1901. }
  1902. return count;
  1903. }
  1904. sint16 World::newValue = 27;
  1905. sint16 World::GetItemStatAOMValue(sint16 subtype) {
  1906. sint16 tmp_subtype = subtype;
  1907. // this is ugly for now cause I didn't want to map it all out, see a better way later but a lot of these are just slightly shifted
  1908. if(subtype > 39)
  1909. {
  1910. // 88 needs to be something else (crit mitigation)
  1911. // 19 needs to be something else (ability reuse speed) which is 62
  1912. if(subtype == 21) // ITEM_STAT_MAXATTPERC
  1913. tmp_subtype = 20;
  1914. else if(subtype == 41) // flurry
  1915. tmp_subtype = 39;
  1916. else if(subtype == 47) // flurry
  1917. tmp_subtype = 41;
  1918. else if(subtype == 49) // flurry
  1919. tmp_subtype = 42;
  1920. else if(subtype == 51) // ITEM_STAT_EXTRASHIELDBLOCKCHANCE
  1921. tmp_subtype = 44;
  1922. //tmp_subtype = 43 is bountiful harvest
  1923. else if(subtype == 54 && subtype <= 57) // ITEM_STAT_MELEECRITCHANCE
  1924. tmp_subtype = subtype - 7;
  1925. else if(subtype == 59) // ITEM_STAT_POTENCY
  1926. tmp_subtype = 51;
  1927. else if(subtype >= 61 && subtype <= 85) // ITEM_STAT_RANGEDWEAPONRANGE
  1928. tmp_subtype = subtype - 9; //
  1929. else if(subtype >= 86 && subtype <= 101) // ITEM_STAT_WEAPONDAMAGEBONUSMELEEONLY
  1930. tmp_subtype = subtype - 8; //
  1931. else if(subtype == 102) // ITEM_STAT_SPELLWEAPONDAMAGEBONUS
  1932. tmp_subtype = 77; //
  1933. else if(subtype >= 103 && subtype <= 110)
  1934. tmp_subtype = subtype - 9;
  1935. else if(subtype == 122) // ITEM_STAT_ABILITYDOUBLECASTCHANCE
  1936. tmp_subtype = 40; //
  1937. else if(subtype == 124) // ITEM_STAT_STATUSEARNED
  1938. tmp_subtype = 27; //
  1939. else
  1940. tmp_subtype += 1;
  1941. // 80 serves as ranged weapon range increase, but so does 58?
  1942. }
  1943. else if((subtype > 18 && subtype < 28) || subtype > 30) // max mana was 18
  1944. tmp_subtype = subtype - 1;
  1945. else if(subtype == 5)
  1946. tmp_subtype = 46;
  1947. else if(subtype == 4)
  1948. tmp_subtype = 45;
  1949. LogWrite(PLAYER__DEBUG, 0, "Player", "Convert type: %i -> %i", subtype, tmp_subtype);
  1950. return tmp_subtype;
  1951. }
  1952. sint16 World::GetItemStatTOVValue(sint16 subtype) {
  1953. return (tov_itemstat_conversion[subtype] - 600);
  1954. }
  1955. sint16 World::GetItemStatDOVValue(sint16 subtype) {
  1956. return (dov_itemstat_conversion[subtype] - 600);
  1957. }
  1958. sint16 World::GetItemStatCOEValue(sint16 subtype) {
  1959. return (coe_itemstat_conversion[subtype] - 600);
  1960. }
  1961. sint16 World::GetItemStatKAValue(sint16 subtype) {
  1962. return (ka_itemstat_conversion[subtype] - 600);
  1963. }
  1964. bool World::CheckTempBugCRC(char* msg)
  1965. {
  1966. MBugReport.writelock();
  1967. sint32 crc = GetItemNameCrc(std::string(msg));
  1968. if (bug_report_crc.count(crc) > 0)
  1969. {
  1970. MBugReport.releasewritelock();
  1971. return true;
  1972. }
  1973. else
  1974. bug_report_crc.insert(make_pair(crc, true));
  1975. MBugReport.releasewritelock();
  1976. return false;
  1977. }
  1978. #ifdef WIN32
  1979. ulong World::GetCurrentThreadID(){
  1980. return GetCurrentThreadId();
  1981. }
  1982. int64 World::GetThreadUsageCPUTime(){
  1983. HANDLE handle = GetCurrentThread();
  1984. int64 lpCreationTime;
  1985. int64 lpExitTime;
  1986. int64 lpKernelTime;
  1987. int64 lpUserTime;
  1988. if(GetThreadTimes(handle, (FILETIME*)&lpCreationTime, (FILETIME*)&lpExitTime, (FILETIME*)&lpKernelTime, (FILETIME*)&lpUserTime))
  1989. return lpKernelTime + lpUserTime;
  1990. return 0;
  1991. }
  1992. #else
  1993. #endif
  1994. void World::SyncCharacterAbilities(Client* client)
  1995. {
  1996. MStartingLists.readlock();
  1997. int8 baseClass = classes.GetBaseClass(client->GetPlayer()->GetAdventureClass());
  1998. int8 secondaryClass = classes.GetSecondaryBaseClass(client->GetPlayer()->GetAdventureClass());
  1999. int8 actualClass = client->GetPlayer()->GetAdventureClass();
  2000. int8 baseRace = client->GetPlayer()->GetRace();
  2001. bool reloadSpells = false;
  2002. multimap<int8, multimap<int8, StartingSkill>*>::iterator skill_itr = starting_skills.begin();
  2003. multimap<int8, multimap<int8, StartingSpell>*>::iterator spell_itr = starting_spells.begin();
  2004. bool isComplete = false;
  2005. int8 wait_iterations = 0; // wait 5 iterations and give up if db takes too long
  2006. do
  2007. {
  2008. bool isProcessing = false;
  2009. if (skill_itr != starting_skills.end())
  2010. {
  2011. isProcessing = true;
  2012. // race = 255 is wildcard all, otherwise needs to match the race id
  2013. if (skill_itr->first == 255 || skill_itr->first == baseRace)
  2014. {
  2015. multimap<int8, StartingSkill>::iterator child_itr;
  2016. for (child_itr = skill_itr->second->begin(); child_itr != skill_itr->second->end(); child_itr++)
  2017. {
  2018. // class = 255 is wildcard all, otherwise needs to match the class id
  2019. if (child_itr->first == 255 ||
  2020. child_itr->first == baseClass ||
  2021. child_itr->first == secondaryClass ||
  2022. child_itr->first == actualClass)
  2023. {
  2024. if (!client->GetPlayer()->skill_list.HasSkill(child_itr->second.skill_id))
  2025. {
  2026. Query query;
  2027. 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());
  2028. 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)",
  2029. client->GetCharacterID(), child_itr->second.skill_id, child_itr->second.current_val, child_itr->second.max_val);
  2030. client->GetPlayer()->AddSkill(child_itr->second.skill_id, child_itr->second.current_val, child_itr->second.max_val);
  2031. }
  2032. }
  2033. }
  2034. }
  2035. skill_itr++;
  2036. }
  2037. if (spell_itr != starting_spells.end())
  2038. {
  2039. isProcessing = true;
  2040. // race = 255 is wildcard all, otherwise needs to match the race id
  2041. if (spell_itr->first == 255 || spell_itr->first == baseRace)
  2042. {
  2043. multimap<int8, StartingSpell>::iterator child_itr;
  2044. for (child_itr = spell_itr->second->begin(); child_itr != spell_itr->second->end(); child_itr++)
  2045. {
  2046. // class = 255 is wildcard all, otherwise needs to match the class id
  2047. if (child_itr->first == 255 ||
  2048. child_itr->first == baseClass ||
  2049. child_itr->first == secondaryClass ||
  2050. child_itr->first == actualClass)
  2051. {
  2052. if (!client->GetPlayer()->HasSpell(child_itr->second.spell_id, child_itr->second.tier, true))
  2053. {
  2054. Query query;
  2055. 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());
  2056. // knowledge_slot is a signed int in the DB
  2057. query.AddQueryAsync(client->GetCharacterID(), &database, Q_INSERT, "INSERT IGNORE INTO character_spells (char_id, spell_id, tier, knowledge_slot) VALUES (%u, %u, %u, %i)",
  2058. client->GetCharacterID(), child_itr->second.spell_id, child_itr->second.tier, child_itr->second.knowledge_slot);
  2059. // reload spells, we don't know the spellbook or timer info
  2060. reloadSpells = true;
  2061. }
  2062. }
  2063. }
  2064. }
  2065. spell_itr++;
  2066. }
  2067. // poor mans async kill just in case, we don't want a client stuck here, they can get the spells on next zone
  2068. if (!isProcessing)
  2069. {
  2070. bool isDBActive = database.IsActiveQuery(client->GetCharacterID());
  2071. if (isDBActive && wait_iterations < 5)
  2072. {
  2073. wait_iterations++;
  2074. }
  2075. else
  2076. isComplete = true;
  2077. }
  2078. } while (!isComplete);
  2079. MStartingLists.releasereadlock();
  2080. if (reloadSpells)
  2081. database.LoadCharacterSpells(client->GetCharacterID(), client->GetPlayer());
  2082. }
  2083. void World::LoadStartingLists()
  2084. {
  2085. LogWrite(WORLD__DEBUG, 1, "World", "-Loading `starting_skills`...");
  2086. database.LoadStartingSkills(this);
  2087. LogWrite(WORLD__DEBUG, 1, "World", "-Loading `starting_spells`...");
  2088. database.LoadStartingSpells(this);
  2089. }
  2090. void World::PurgeStartingLists()
  2091. {
  2092. MStartingLists.writelock();
  2093. multimap<int8, multimap<int8, StartingSkill>*>::iterator skill_itr;
  2094. for (skill_itr = starting_skills.begin(); skill_itr != starting_skills.end(); skill_itr++)
  2095. {
  2096. multimap<int8, StartingSkill>* tmpMap = skill_itr->second;
  2097. safe_delete(tmpMap);
  2098. }
  2099. starting_skills.clear();
  2100. multimap<int8, multimap<int8, StartingSpell>*>::iterator spell_itr;
  2101. for (spell_itr = starting_spells.begin(); spell_itr != starting_spells.end(); spell_itr++)
  2102. {
  2103. multimap<int8, StartingSpell>* tmpMap = spell_itr->second;
  2104. safe_delete(tmpMap);
  2105. }
  2106. starting_spells.clear();
  2107. MStartingLists.releasewritelock();
  2108. }
  2109. void World::SetReloadingSubsystem(string subsystem) {
  2110. MReloadingSubsystems.lock();
  2111. reloading_subsystems[subsystem] = Timer::GetCurrentTime2();
  2112. MReloadingSubsystems.unlock();
  2113. }
  2114. void World::RemoveReloadingSubSystem(string subsystem) {
  2115. MReloadingSubsystems.lock();
  2116. if (reloading_subsystems.count(subsystem) > 0)
  2117. reloading_subsystems.erase(subsystem);
  2118. MReloadingSubsystems.unlock();
  2119. }
  2120. bool World::IsReloadingSubsystems() {
  2121. bool result = false;
  2122. MReloadingSubsystems.lock();
  2123. result = reloading_subsystems.size() > 0;
  2124. MReloadingSubsystems.unlock();
  2125. return result;
  2126. }
  2127. map<string, int32> World::GetOldestReloadingSubsystem() {
  2128. map<string, int32> result;
  2129. MReloadingSubsystems.lock();
  2130. int32 current_time = Timer::GetCurrentTime2();
  2131. map<string, int32>::iterator itr;
  2132. int32 oldest = current_time;
  2133. string oldestname = "";
  2134. for (itr = reloading_subsystems.begin(); itr != reloading_subsystems.end(); itr++) {
  2135. if (itr->second < oldest)
  2136. oldestname = itr->first;
  2137. }
  2138. result[oldestname] = oldest;
  2139. MReloadingSubsystems.unlock();
  2140. return result;
  2141. }
  2142. void ZoneList::WatchdogHeartbeat()
  2143. {
  2144. list<ZoneServer*>::iterator zone_iter;
  2145. ZoneServer* tmp = 0;
  2146. MZoneList.writelock(__FUNCTION__, __LINE__);
  2147. bool match = false;
  2148. for (zone_iter = zlist.begin(); zone_iter != zlist.end(); zone_iter++)
  2149. {
  2150. tmp = *zone_iter;
  2151. if (tmp)
  2152. {
  2153. int32 curTime = Timer::GetCurrentTime2();
  2154. sint64 diff = (sint64)curTime - (sint64)tmp->GetWatchdogTime();
  2155. if (diff > 120000)
  2156. {
  2157. LogWrite(WORLD__ERROR, 1, "World", "Zone %s is hung for %i milliseconds.. attempting to cancel threads...", tmp->GetZoneName(), diff);
  2158. #ifndef WIN32
  2159. tmp->CancelThreads();
  2160. zlist.erase(zone_iter);
  2161. safe_delete(tmp);
  2162. #endif
  2163. MZoneList.releasewritelock(__FUNCTION__, __LINE__);
  2164. match = true;
  2165. break;
  2166. }
  2167. else if (diff > 90000 && !tmp->isZoneShuttingDown())
  2168. {
  2169. tmp->SetWatchdogTime(Timer::GetCurrentTime2()); // reset so we don't continuously flood this heartbeat
  2170. map<string, int32> oldest_process = world.GetOldestReloadingSubsystem();
  2171. if (oldest_process.size() > 0) {
  2172. map<string, int32>::iterator itr = oldest_process.begin();
  2173. if(itr != oldest_process.end())
  2174. 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);
  2175. else
  2176. LogWrite(WORLD__ERROR, 1, "World", "Zone %s is hung for %i milliseconds... attempting shutdown", tmp->GetZoneName(), diff);
  2177. }
  2178. else
  2179. LogWrite(WORLD__ERROR, 1, "World", "Zone %s is hung for %i milliseconds.. attempting shutdown", tmp->GetZoneName(), diff);
  2180. tmp->Shutdown();
  2181. }
  2182. else if (diff > 30000)
  2183. {
  2184. if (world.IsReloadingSubsystems()) {
  2185. if (world.GetSuppressedWarningTime() == 0) {
  2186. world.SetSuppressedWarning();
  2187. map<string, int32> oldest_process = world.GetOldestReloadingSubsystem();
  2188. if (oldest_process.size() > 0) {
  2189. map<string, int32>::iterator itr = oldest_process.begin();
  2190. if(itr != oldest_process.end())
  2191. LogWrite(WORLD__ERROR, 1, "World", "Zone %s is hung for %i milliseconds.. while waiting for %s to reload...", tmp->GetZoneName(), diff, itr->first);
  2192. else
  2193. LogWrite(WORLD__ERROR, 1, "World", "Zone %s is hung for %i milliseconds...", tmp->GetZoneName(), diff);
  2194. }
  2195. }
  2196. continue;
  2197. }
  2198. }
  2199. else if (diff > 60000 && !tmp->isZoneShuttingDown())
  2200. {
  2201. if (world.IsReloadingSubsystems())
  2202. continue;
  2203. LogWrite(WORLD__ERROR, 1, "World", "Zone %s is hung for %i milliseconds.. attempting shutdown", tmp->GetZoneName(), diff);
  2204. tmp->Shutdown();
  2205. }
  2206. }
  2207. }
  2208. if(!match)
  2209. MZoneList.releasewritelock(__FUNCTION__, __LINE__);
  2210. }
  2211. void World::LoadRegionMaps(std::string zoneFile)
  2212. {
  2213. string zoneToLower(zoneFile);
  2214. boost::algorithm::to_lower(zoneToLower);
  2215. MWorldRegionMaps.writelock();
  2216. std::map<std::string, RegionMapRange*>::iterator itr;
  2217. itr = region_maps.find(zoneToLower);
  2218. if (itr == region_maps.end())
  2219. {
  2220. RegionMapRange* newRange = new RegionMapRange();
  2221. newRange->AddVersionRange(zoneFile);
  2222. region_maps.insert(make_pair(zoneToLower, newRange));
  2223. }
  2224. MWorldRegionMaps.releasewritelock();
  2225. }
  2226. RegionMap* World::GetRegionMap(std::string zoneFile, int32 client_version)
  2227. {
  2228. string zoneToLower(zoneFile);
  2229. boost::algorithm::to_lower(zoneToLower);
  2230. MWorldRegionMaps.readlock();
  2231. std::map<std::string, RegionMapRange*>::iterator itr;
  2232. itr = region_maps.find(zoneToLower);
  2233. if ( itr != region_maps.end())
  2234. {
  2235. std::map<VersionRange*, RegionMap*>::iterator rmitr;
  2236. rmitr = itr->second->FindRegionByVersion(client_version);
  2237. if ( rmitr != itr->second->GetRangeEnd())
  2238. {
  2239. MWorldRegionMaps.releasereadlock();
  2240. return rmitr->second;
  2241. }
  2242. }
  2243. MWorldRegionMaps.releasereadlock();
  2244. return nullptr;
  2245. }
  2246. void World::LoadMaps(std::string zoneFile)
  2247. {
  2248. string zoneToLower(zoneFile);
  2249. boost::algorithm::to_lower(zoneToLower);
  2250. MWorldMaps.writelock();
  2251. std::map<std::string, MapRange*>::iterator itr;
  2252. itr = maps.find(zoneToLower);
  2253. if (itr == maps.end())
  2254. {
  2255. MapRange* newRange = new MapRange();
  2256. newRange->AddVersionRange(zoneFile);
  2257. maps.insert(make_pair(zoneToLower, newRange));
  2258. }
  2259. MWorldMaps.releasewritelock();
  2260. }
  2261. Map* World::GetMap(std::string zoneFile, int32 client_version)
  2262. {
  2263. string zoneToLower(zoneFile);
  2264. boost::algorithm::to_lower(zoneToLower);
  2265. MWorldMaps.readlock();
  2266. std::map<std::string, MapRange*>::iterator itr;
  2267. itr = maps.find(zoneToLower);
  2268. if ( itr != maps.end())
  2269. {
  2270. std::map<VersionRange*, Map*>::iterator rmitr;
  2271. rmitr = itr->second->FindMapByVersion(client_version);
  2272. if ( rmitr != itr->second->GetRangeEnd())
  2273. {
  2274. MWorldMaps.releasereadlock();
  2275. return rmitr->second;
  2276. }
  2277. }
  2278. MWorldMaps.releasereadlock();
  2279. return nullptr;
  2280. }