World.cpp 93 KB

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