World.cpp 87 KB

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