World.cpp 90 KB

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