World.cpp 80 KB

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