World.cpp 94 KB

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