9
3

World.cpp 89 KB

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