World.cpp 94 KB

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