World.cpp 77 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483
  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(ItemStatsValues* values, int16 type, sint32 value, Entity* entity){
  1337. if(values){
  1338. switch(type){
  1339. case ITEM_STAT_STR:{
  1340. values->str += value;
  1341. break;
  1342. }
  1343. case ITEM_STAT_STA:{
  1344. values->sta += value;
  1345. break;
  1346. }
  1347. case ITEM_STAT_AGI:{
  1348. values->agi += value;
  1349. break;
  1350. }
  1351. case ITEM_STAT_WIS:{
  1352. values->wis += value;
  1353. break;
  1354. }
  1355. case ITEM_STAT_INT:{
  1356. values->int_ += value;
  1357. break;
  1358. }
  1359. case ITEM_STAT_VS_SLASH:{
  1360. values->vs_slash += value;
  1361. break;
  1362. }
  1363. case ITEM_STAT_VS_CRUSH:{
  1364. values->vs_crush += value;
  1365. break;
  1366. }
  1367. case ITEM_STAT_VS_PIERCE:{
  1368. values->vs_pierce += value;
  1369. break;
  1370. }
  1371. case ITEM_STAT_VS_HEAT:{
  1372. values->vs_heat += value;
  1373. break;
  1374. }
  1375. case ITEM_STAT_VS_COLD:{
  1376. values->vs_cold += value;
  1377. break;
  1378. }
  1379. case ITEM_STAT_VS_MAGIC:{
  1380. values->vs_magic += value;
  1381. break;
  1382. }
  1383. case ITEM_STAT_VS_MENTAL:{
  1384. values->vs_mental += value;
  1385. break;
  1386. }
  1387. case ITEM_STAT_VS_DIVINE:{
  1388. values->vs_divine += value;
  1389. break;
  1390. }
  1391. case ITEM_STAT_VS_DISEASE:{
  1392. values->vs_disease += value;
  1393. break;
  1394. }
  1395. case ITEM_STAT_VS_POISON:{
  1396. values->vs_poison += value;
  1397. break;
  1398. }
  1399. case ITEM_STAT_HEALTH:{
  1400. values->health += value;
  1401. break;
  1402. }
  1403. case ITEM_STAT_POWER:{
  1404. values->power += value;
  1405. break;
  1406. }
  1407. case ITEM_STAT_CONCENTRATION:{
  1408. values->concentration += value;
  1409. break;
  1410. }
  1411. case ITEM_STAT_ABILITY_MODIFIER:{
  1412. values->ability_modifier += value;
  1413. break;
  1414. }
  1415. case ITEM_STAT_CRITICALMITIGATION:{
  1416. values->criticalmitigation += value;
  1417. break;
  1418. }
  1419. case ITEM_STAT_EXTRASHIELDBLOCKCHANCE:{
  1420. values->extrashieldblockchance += value;
  1421. break;
  1422. }
  1423. case ITEM_STAT_BENEFICIALCRITCHANCE:{
  1424. values->beneficialcritchance += value;
  1425. break;
  1426. }
  1427. case ITEM_STAT_CRITBONUS:{
  1428. values->critbonus += value;
  1429. break;
  1430. }
  1431. /*case ITEM_STAT_POTENCY:{
  1432. values->potency += value;
  1433. break;
  1434. }*/
  1435. case ITEM_STAT_HATEGAINMOD:{
  1436. values->hategainmod += value;
  1437. break;
  1438. }
  1439. case ITEM_STAT_ABILITYREUSESPEED:{
  1440. values->abilityreusespeed += value;
  1441. break;
  1442. }
  1443. case ITEM_STAT_ABILITYCASTINGSPEED:{
  1444. values->abilitycastingspeed += value;
  1445. break;
  1446. }
  1447. case ITEM_STAT_ABILITYRECOVERYSPEED:{
  1448. values->abilityrecoveryspeed += value;
  1449. break;
  1450. }
  1451. case ITEM_STAT_SPELLREUSESPEED:{
  1452. values->spellreusespeed += value;
  1453. break;
  1454. }
  1455. case ITEM_STAT_SPELLMULTIATTACKCHANCE:{
  1456. values->spellmultiattackchance += value;
  1457. break;
  1458. }
  1459. case ITEM_STAT_DPS:{
  1460. values->dps += value;
  1461. break;
  1462. }
  1463. case ITEM_STAT_ATTACKSPEED:{
  1464. values->attackspeed += value;
  1465. break;
  1466. }
  1467. case ITEM_STAT_MULTIATTACKCHANCE:{
  1468. values->multiattackchance += value;
  1469. break;
  1470. }
  1471. case ITEM_STAT_AEAUTOATTACKCHANCE:{
  1472. values->aeautoattackchance += value;
  1473. break;
  1474. }
  1475. case ITEM_STAT_STRIKETHROUGH:{
  1476. values->strikethrough += value;
  1477. break;
  1478. }
  1479. case ITEM_STAT_ACCURACY:{
  1480. values->accuracy += value;
  1481. break;
  1482. }
  1483. /*case ITEM_STAT_OFFENSIVESPEED:{
  1484. values->offensivespeed += value;
  1485. break;
  1486. }*/
  1487. default: {
  1488. if (entity)
  1489. entity->stats[type] += value;
  1490. break;
  1491. }
  1492. }
  1493. }
  1494. }
  1495. void World::CreateGuild(const char* guild_name, Client* leader, int32 group_id) {
  1496. deque<GroupMemberInfo*>::iterator itr;
  1497. GroupMemberInfo* gmi;
  1498. Guild *guild;
  1499. assert(guild_name);
  1500. guild = new Guild();
  1501. guild->SetName(guild_name);
  1502. guild->SetFormedDate(Timer::GetUnixTimeStamp());
  1503. database.LoadGuildDefaultRanks(guild);
  1504. database.LoadGuildDefaultEventFilters(guild);
  1505. database.SaveGuild(guild, true);
  1506. database.SaveGuildEvents(guild);
  1507. database.SaveGuildRanks(guild);
  1508. database.SaveGuildEventFilters(guild);
  1509. database.SaveGuildRecruiting(guild);
  1510. guild_list.AddGuild(guild);
  1511. if (leader && !leader->GetPlayer()->GetGuild())
  1512. guild->AddNewGuildMember(leader, 0, GUILD_RANK_LEADER);
  1513. database.SaveGuildMembers(guild);
  1514. if (leader && group_id > 0) {
  1515. GetGroupManager()->GroupLock(__FUNCTION__, __LINE__);
  1516. PlayerGroup* group = world.GetGroupManager()->GetGroup(group_id);
  1517. if (group)
  1518. {
  1519. group->MGroupMembers.readlock(__FUNCTION__, __LINE__);
  1520. deque<GroupMemberInfo*>* members = group->GetMembers();
  1521. for (itr = members->begin(); itr != members->end(); itr++) {
  1522. gmi = *itr;
  1523. if (gmi->client && gmi->client != leader && !gmi->client->GetPlayer()->GetGuild())
  1524. guild->InvitePlayer(gmi->client, leader->GetPlayer()->GetName());
  1525. }
  1526. group->MGroupMembers.releasereadlock(__FUNCTION__, __LINE__);
  1527. }
  1528. GetGroupManager()->ReleaseGroupLock(__FUNCTION__, __LINE__);
  1529. }
  1530. }
  1531. void World::SaveGuilds() {
  1532. MutexMap<int32, Guild*>* guilds = guild_list.GetGuilds();
  1533. MutexMap<int32, Guild*>::iterator itr = guilds->begin();
  1534. while (itr.Next()) {
  1535. Guild* guild = itr.second;
  1536. if (guild->GetSaveNeeded())
  1537. database.SaveGuild(guild);
  1538. if (guild->GetMemberSaveNeeded())
  1539. database.SaveGuildMembers(guild);
  1540. if (guild->GetEventsSaveNeeded())
  1541. database.SaveGuildEvents(guild);
  1542. if (guild->GetRanksSaveNeeded())
  1543. database.SaveGuildRanks(guild);
  1544. if (guild->GetEventFiltersSaveNeeded())
  1545. database.SaveGuildEventFilters(guild);
  1546. if (guild->GetPointsHistorySaveNeeded())
  1547. database.SaveGuildPointsHistory(guild);
  1548. if (guild->GetRecruitingSaveNeeded())
  1549. database.SaveGuildRecruiting(guild);
  1550. }
  1551. }
  1552. void World::PickRandomLottoDigits(int32* digits) {
  1553. if (digits) {
  1554. for (int32 i = 0; i < 6; i++) {
  1555. bool found = true;
  1556. int32 num = 0;
  1557. while (found) {
  1558. num = ((int32)rand() % 36) + 1;
  1559. for (int32 j = 0; j < 6; j++) {
  1560. if (digits[j] == num)
  1561. break;
  1562. if (j == 5)
  1563. found = false;
  1564. }
  1565. }
  1566. digits[i] = num;
  1567. }
  1568. }
  1569. }
  1570. void World::AddLottoPlayer(int32 character_id, int32 end_time) {
  1571. LottoPlayer* lp;
  1572. if (lotto_players.count(character_id) == 0) {
  1573. lp = new LottoPlayer;
  1574. lotto_players.Put(character_id, lp);
  1575. }
  1576. else
  1577. lp = lotto_players.Get(character_id);
  1578. lp->end_time = end_time;
  1579. lp->num_matches = 0;
  1580. lp->set = false;
  1581. }
  1582. void World::RemoveLottoPlayer(int32 character_id) {
  1583. if (lotto_players.count(character_id) > 0)
  1584. lotto_players.erase(character_id, false, true);
  1585. }
  1586. void World::SetLottoPlayerNumMatches(int32 character_id, int8 num_matches) {
  1587. if (lotto_players.count(character_id) > 0) {
  1588. lotto_players.Get(character_id)->num_matches = num_matches;
  1589. lotto_players.Get(character_id)->set = true;
  1590. }
  1591. }
  1592. void World::CheckLottoPlayers() {
  1593. MutexMap<int32, LottoPlayer*>::iterator itr = lotto_players.begin();
  1594. while (itr.Next()) {
  1595. LottoPlayer* lp = itr->second;
  1596. if (Timer::GetCurrentTime2() >= lp->end_time && lp->set) {
  1597. int8 num_matches = lp->num_matches;
  1598. LogWrite(PLAYER__DEBUG, 0, "Player", "Num matches: %u", lp->num_matches);
  1599. Client* client = zone_list.GetClientByCharID(itr->first);
  1600. if (client && num_matches >= 2) {
  1601. if (num_matches == 2) {
  1602. client->SimpleMessage(CHANNEL_COLOR_YELLOW, "You receive 10 silver.");
  1603. client->SendPopupMessage(0, "Congratulations! You have won 10 silver!", "", 2, 0xFF, 0xFF, 0x99);
  1604. client->GetPlayer()->AddCoins(1000);
  1605. client->GetPlayer()->GetZone()->SendCastSpellPacket(869, client->GetPlayer());
  1606. }
  1607. else if (num_matches == 3) {
  1608. client->SimpleMessage(CHANNEL_COLOR_YELLOW, "You receive 50 silver.");
  1609. client->SendPopupMessage(0, "Congratulations! You have won 50 silver!", "", 2, 0xFF, 0xFF, 0x99);
  1610. client->GetPlayer()->AddCoins(5000);
  1611. client->GetPlayer()->GetZone()->SendCastSpellPacket(870, client->GetPlayer());
  1612. }
  1613. else if (num_matches == 4) {
  1614. client->SimpleMessage(CHANNEL_COLOR_YELLOW, "You receive 2 gold 50 silver.");
  1615. client->SendPopupMessage(0, "Congratulations! You have won 2 gold 50 silver!", "", 2, 0xFF, 0xFF, 0x99);
  1616. client->GetPlayer()->AddCoins(25000);
  1617. client->GetPlayer()->GetZone()->SendCastSpellPacket(871, client->GetPlayer());
  1618. }
  1619. else if (num_matches == 5) {
  1620. client->SimpleMessage(CHANNEL_COLOR_YELLOW, "You receive 25 gold.");
  1621. client->SendPopupMessage(0, "Congratulations! You have won 25 gold!", "", 2, 0xFF, 0xFF, 0x99);
  1622. client->GetPlayer()->AddCoins(250000);
  1623. client->GetPlayer()->GetZone()->SendCastSpellPacket(872, client->GetPlayer());
  1624. }
  1625. else if (num_matches == 6) {
  1626. Variable* var = variables.FindVariable("gambling_current_jackpot");
  1627. if (var) {
  1628. int64 jackpot = 0;
  1629. try {
  1630. jackpot = atoul(var->GetValue());
  1631. }
  1632. catch (...) {
  1633. jackpot = 10000;
  1634. }
  1635. char coin_message[128];
  1636. char message[512];
  1637. char announcement[512];
  1638. memset(coin_message, 0, sizeof(coin_message));
  1639. memset(message, 0, sizeof(message));
  1640. memset(announcement, 0, sizeof(announcement));
  1641. sprintf(coin_message, "%s", client->GetCoinMessage(jackpot).c_str());
  1642. sprintf(message, "Congratulations! You have won %s!", coin_message);
  1643. sprintf(announcement, "%s as won the jackpot containing a total of %s!", client->GetPlayer()->GetName(), coin_message);
  1644. client->Message(CHANNEL_COLOR_YELLOW, "You receive %s.", coin_message);
  1645. client->SendPopupMessage(0, message, "", 2, 0xFF, 0xFF, 0x99);
  1646. zone_list.HandleGlobalAnnouncement(announcement);
  1647. client->GetPlayer()->AddCoins(jackpot);
  1648. client->GetPlayer()->GetZone()->SendCastSpellPacket(843, client->GetPlayer());
  1649. client->GetPlayer()->GetZone()->SendCastSpellPacket(1413, client->GetPlayer());
  1650. }
  1651. }
  1652. }
  1653. RemoveLottoPlayer(itr->first);
  1654. }
  1655. }
  1656. }
  1657. 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) {
  1658. MHouseZones.writelock(__FUNCTION__, __LINE__);
  1659. if (m_houseZones.count(id) == 0) {
  1660. HouseZone* hz = new HouseZone;
  1661. //ZeroMemory(hz, sizeof(HouseZone));
  1662. hz->id = id;
  1663. hz->name = name;
  1664. hz->cost_coin = cost_coins;
  1665. hz->cost_status = cost_status;
  1666. hz->upkeep_coin = upkeep_coins;
  1667. hz->upkeep_status = upkeep_status;
  1668. hz->vault_slots = vault_slots;
  1669. hz->alignment = alignment;
  1670. hz->guild_level = guild_level;
  1671. hz->zone_id = zone_id;
  1672. hz->exit_zone_id = exit_zone_id;
  1673. hz->exit_x = exit_x;
  1674. hz->exit_y = exit_y;
  1675. hz->exit_z = exit_z;
  1676. hz->exit_heading = exit_heading;
  1677. m_houseZones[id] = hz;
  1678. }
  1679. else {
  1680. LogWrite(WORLD__ERROR, 0, "Housing", "Duplicate house id (%u) for %s", id, name.c_str());
  1681. }
  1682. MHouseZones.releasewritelock(__FUNCTION__, __LINE__);
  1683. }
  1684. HouseZone* World::GetHouseZone(int32 id) {
  1685. HouseZone* ret = 0;
  1686. MHouseZones.readlock(__FUNCTION__, __LINE__);
  1687. if (m_houseZones.count(id) > 0)
  1688. ret = m_houseZones[id];
  1689. MHouseZones.releasereadlock(__FUNCTION__, __LINE__);
  1690. return ret;
  1691. }
  1692. 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) {
  1693. MPlayerHouses.writelock(__FUNCTION__, __LINE__);
  1694. if (m_playerHouses.count(house_id) == 0 || m_playerHouses[house_id].count(char_id) == 0) {
  1695. PlayerHouse* ph = new PlayerHouse;
  1696. ph->house_id = house_id;
  1697. ph->unique_id = unique_id;
  1698. ph->instance_id = instance_id;
  1699. ph->escrow_coins = escrow_coins;
  1700. ph->escrow_status = escrow_status;
  1701. ph->upkeep_due = upkeep_due;
  1702. ph->player_name = player_name;
  1703. ReloadHouseData(ph);
  1704. m_playerHouses[house_id][char_id] = ph;
  1705. }
  1706. MPlayerHouses.releasewritelock(__FUNCTION__, __LINE__);
  1707. }
  1708. void World::ReloadHouseData(PlayerHouse* ph)
  1709. {
  1710. database.LoadDeposits(ph);
  1711. database.LoadHistory(ph);
  1712. }
  1713. PlayerHouse* World::GetPlayerHouseByHouseID(int32 char_id, int32 house_id) {
  1714. PlayerHouse* ret = 0;
  1715. MPlayerHouses.readlock(__FUNCTION__, __LINE__);
  1716. if (m_playerHouses.count(house_id) > 0 && m_playerHouses[house_id].count(char_id) > 0)
  1717. ret = m_playerHouses[house_id][char_id];
  1718. MPlayerHouses.releasereadlock(__FUNCTION__, __LINE__);
  1719. return ret;
  1720. }
  1721. PlayerHouse* World::GetPlayerHouseByUniqueID(int64 unique_id) {
  1722. PlayerHouse* ret = 0;
  1723. MPlayerHouses.readlock(__FUNCTION__, __LINE__);
  1724. map<int32, map<int32, PlayerHouse*> >::iterator itr;
  1725. for (itr = m_playerHouses.begin(); itr != m_playerHouses.end(); itr++) {
  1726. map<int32, PlayerHouse*>::iterator itr2;
  1727. for (itr2 = itr->second.begin(); itr2 != itr->second.end(); itr2++) {
  1728. if (itr2->second->unique_id == unique_id) {
  1729. ret = itr2->second;
  1730. break;
  1731. }
  1732. }
  1733. if (ret)
  1734. break;
  1735. }
  1736. MPlayerHouses.releasereadlock(__FUNCTION__, __LINE__);
  1737. return ret;
  1738. }
  1739. PlayerHouse* World::GetPlayerHouseByInstanceID(int32 instance_id) {
  1740. PlayerHouse* ret = 0;
  1741. MPlayerHouses.readlock(__FUNCTION__, __LINE__);
  1742. map<int32, map<int32, PlayerHouse*> >::iterator itr;
  1743. for (itr = m_playerHouses.begin(); itr != m_playerHouses.end(); itr++) {
  1744. map<int32, PlayerHouse*>::iterator itr2;
  1745. for (itr2 = itr->second.begin(); itr2 != itr->second.end(); itr2++) {
  1746. if (itr2->second->instance_id == instance_id) {
  1747. ret = itr2->second;
  1748. break;
  1749. }
  1750. }
  1751. if (ret)
  1752. break;
  1753. }
  1754. MPlayerHouses.releasereadlock(__FUNCTION__, __LINE__);
  1755. return ret;
  1756. }
  1757. vector<PlayerHouse*> World::GetAllPlayerHouses(int32 char_id) {
  1758. vector<PlayerHouse*> ret;
  1759. MPlayerHouses.readlock(__FUNCTION__, __LINE__);
  1760. map<int32, map<int32, PlayerHouse*> >::iterator itr;
  1761. for (itr = m_playerHouses.begin(); itr != m_playerHouses.end(); itr++) {
  1762. if (itr->second.count(char_id) > 0)
  1763. ret.push_back(itr->second[char_id]);
  1764. }
  1765. MPlayerHouses.releasereadlock(__FUNCTION__, __LINE__);
  1766. return ret;
  1767. }
  1768. vector<PlayerHouse*> World::GetAllPlayerHousesByHouseID(int32 house_id) {
  1769. vector<PlayerHouse*> ret;
  1770. MPlayerHouses.readlock(__FUNCTION__, __LINE__);
  1771. if (m_houseZones.count(house_id) > 0) {
  1772. map<int32, PlayerHouse*>::iterator itr;
  1773. for (itr = m_playerHouses[house_id].begin(); itr != m_playerHouses[house_id].end(); itr++)
  1774. ret.push_back(itr->second);
  1775. }
  1776. MPlayerHouses.releasereadlock(__FUNCTION__, __LINE__);
  1777. return ret;
  1778. }
  1779. void World::PopulateTOVStatMap() {
  1780. //This function populates a map that converts changed CoE to ToV stats
  1781. tov_itemstat_conversion[0] = TOV_ITEM_STAT_HPREGEN;
  1782. tov_itemstat_conversion[1] = TOV_ITEM_STAT_MANAREGEN;
  1783. tov_itemstat_conversion[2] = TOV_ITEM_STAT_HPREGENPPT;
  1784. tov_itemstat_conversion[3] = TOV_ITEM_STAT_MPREGENPPT;
  1785. tov_itemstat_conversion[4] = TOV_ITEM_STAT_COMBATHPREGENPPT;
  1786. tov_itemstat_conversion[5] = TOV_ITEM_STAT_COMBATMPREGENPPT;
  1787. tov_itemstat_conversion[6] = TOV_ITEM_STAT_MAXHP;
  1788. tov_itemstat_conversion[7] = TOV_ITEM_STAT_MAXHPPERC;
  1789. tov_itemstat_conversion[8] = TOV_ITEM_STAT_MAXHPPERCFINAL;
  1790. tov_itemstat_conversion[9] = TOV_ITEM_STAT_SPEED;
  1791. tov_itemstat_conversion[10] = TOV_ITEM_STAT_SLOW;
  1792. tov_itemstat_conversion[11] = TOV_ITEM_STAT_MOUNTSPEED;
  1793. tov_itemstat_conversion[12] = TOV_ITEM_STAT_MOUNTAIRSPEED;
  1794. tov_itemstat_conversion[13] = TOV_ITEM_STAT_LEAPSPEED;
  1795. tov_itemstat_conversion[14] = TOV_ITEM_STAT_LEAPTIME;
  1796. tov_itemstat_conversion[15] = TOV_ITEM_STAT_GLIDEEFFICIENCY;
  1797. tov_itemstat_conversion[16] = TOV_ITEM_STAT_OFFENSIVESPEED;
  1798. tov_itemstat_conversion[17] = TOV_ITEM_STAT_ATTACKSPEED;
  1799. tov_itemstat_conversion[18] = 698;
  1800. tov_itemstat_conversion[19] = TOV_ITEM_STAT_MAXMANA;
  1801. tov_itemstat_conversion[20] = TOV_ITEM_STAT_MAXMANAPERC;
  1802. tov_itemstat_conversion[21] = TOV_ITEM_STAT_MAXATTPERC;
  1803. tov_itemstat_conversion[22] = TOV_ITEM_STAT_BLURVISION;
  1804. tov_itemstat_conversion[23] = TOV_ITEM_STAT_MAGICLEVELIMMUNITY;
  1805. tov_itemstat_conversion[24] = TOV_ITEM_STAT_HATEGAINMOD;
  1806. tov_itemstat_conversion[25] = TOV_ITEM_STAT_COMBATEXPMOD;
  1807. tov_itemstat_conversion[26] = TOV_ITEM_STAT_TRADESKILLEXPMOD;
  1808. tov_itemstat_conversion[27] = TOV_ITEM_STAT_ACHIEVEMENTEXPMOD;
  1809. tov_itemstat_conversion[28] = TOV_ITEM_STAT_SIZEMOD;
  1810. tov_itemstat_conversion[29] = TOV_ITEM_STAT_DPS;
  1811. tov_itemstat_conversion[30] = 698;
  1812. tov_itemstat_conversion[31] = TOV_ITEM_STAT_STEALTH;
  1813. tov_itemstat_conversion[32] = TOV_ITEM_STAT_INVIS;
  1814. tov_itemstat_conversion[33] = TOV_ITEM_STAT_SEESTEALTH;
  1815. tov_itemstat_conversion[34] = TOV_ITEM_STAT_SEEINVIS;
  1816. tov_itemstat_conversion[35] = TOV_ITEM_STAT_EFFECTIVELEVELMOD;
  1817. tov_itemstat_conversion[36] = TOV_ITEM_STAT_RIPOSTECHANCE;
  1818. tov_itemstat_conversion[37] = TOV_ITEM_STAT_PARRYCHANCE;
  1819. tov_itemstat_conversion[38] = TOV_ITEM_STAT_DODGECHANCE;
  1820. tov_itemstat_conversion[39] = TOV_ITEM_STAT_AEAUTOATTACKCHANCE;
  1821. tov_itemstat_conversion[40] = 698;
  1822. tov_itemstat_conversion[41] = TOV_ITEM_STAT_MULTIATTACKCHANCE;
  1823. tov_itemstat_conversion[42] = 698;
  1824. tov_itemstat_conversion[43] = 698;
  1825. tov_itemstat_conversion[44] = 698;
  1826. tov_itemstat_conversion[45] = TOV_ITEM_STAT_SPELLMULTIATTACKCHANCE;
  1827. tov_itemstat_conversion[46] = 698;
  1828. tov_itemstat_conversion[47] = TOV_ITEM_STAT_FLURRY;
  1829. tov_itemstat_conversion[48] = 698;
  1830. tov_itemstat_conversion[49] = TOV_ITEM_STAT_MELEEDAMAGEMULTIPLIER;
  1831. tov_itemstat_conversion[50] = TOV_ITEM_STAT_EXTRAHARVESTCHANCE;
  1832. tov_itemstat_conversion[51] = TOV_ITEM_STAT_EXTRASHIELDBLOCKCHANCE;
  1833. tov_itemstat_conversion[52] = TOV_ITEM_STAT_ITEMHPREGENPPT;
  1834. tov_itemstat_conversion[53] = TOV_ITEM_STAT_ITEMPPREGENPPT;
  1835. tov_itemstat_conversion[54] = TOV_ITEM_STAT_MELEECRITCHANCE;
  1836. tov_itemstat_conversion[55] = TOV_ITEM_STAT_CRITAVOIDANCE;
  1837. tov_itemstat_conversion[56] = TOV_ITEM_STAT_BENEFICIALCRITCHANCE;
  1838. tov_itemstat_conversion[57] = TOV_ITEM_STAT_CRITBONUS;
  1839. tov_itemstat_conversion[58] = 698;
  1840. tov_itemstat_conversion[59] = TOV_ITEM_STAT_POTENCY;
  1841. tov_itemstat_conversion[60] = 698;
  1842. tov_itemstat_conversion[61] = TOV_ITEM_STAT_UNCONSCIOUSHPMOD;
  1843. tov_itemstat_conversion[62] = TOV_ITEM_STAT_ABILITYREUSESPEED;
  1844. tov_itemstat_conversion[63] = TOV_ITEM_STAT_ABILITYRECOVERYSPEED;
  1845. tov_itemstat_conversion[64] = TOV_ITEM_STAT_ABILITYCASTINGSPEED;
  1846. tov_itemstat_conversion[65] = TOV_ITEM_STAT_SPELLREUSESPEED;
  1847. tov_itemstat_conversion[66] = TOV_ITEM_STAT_MELEEWEAPONRANGE;
  1848. tov_itemstat_conversion[67] = TOV_ITEM_STAT_RANGEDWEAPONRANGE;
  1849. tov_itemstat_conversion[68] = TOV_ITEM_STAT_FALLINGDAMAGEREDUCTION;
  1850. tov_itemstat_conversion[69] = TOV_ITEM_STAT_RIPOSTEDAMAGE;
  1851. tov_itemstat_conversion[70] = TOV_ITEM_STAT_MINIMUMDEFLECTIONCHANCE;
  1852. tov_itemstat_conversion[71] = TOV_ITEM_STAT_MOVEMENTWEAVE;
  1853. tov_itemstat_conversion[72] = TOV_ITEM_STAT_COMBATHPREGEN;
  1854. tov_itemstat_conversion[73] = TOV_ITEM_STAT_COMBATMANAREGEN;
  1855. tov_itemstat_conversion[74] = TOV_ITEM_STAT_CONTESTSPEEDBOOST;
  1856. tov_itemstat_conversion[75] = TOV_ITEM_STAT_TRACKINGAVOIDANCE;
  1857. tov_itemstat_conversion[76] = TOV_ITEM_STAT_STEALTHINVISSPEEDMOD;
  1858. tov_itemstat_conversion[77] = TOV_ITEM_STAT_LOOT_COIN;
  1859. tov_itemstat_conversion[78] = TOV_ITEM_STAT_ARMORMITIGATIONINCREASE;
  1860. tov_itemstat_conversion[79] = TOV_ITEM_STAT_AMMOCONSERVATION;
  1861. tov_itemstat_conversion[80] = TOV_ITEM_STAT_STRIKETHROUGH;
  1862. tov_itemstat_conversion[81] = TOV_ITEM_STAT_STATUSBONUS;
  1863. tov_itemstat_conversion[82] = TOV_ITEM_STAT_ACCURACY;
  1864. tov_itemstat_conversion[83] = TOV_ITEM_STAT_COUNTERSTRIKE;
  1865. tov_itemstat_conversion[84] = TOV_ITEM_STAT_SHIELDBASH;
  1866. tov_itemstat_conversion[85] = TOV_ITEM_STAT_WEAPONDAMAGEBONUS;
  1867. tov_itemstat_conversion[86] = 698;
  1868. tov_itemstat_conversion[87] = TOV_ITEM_STAT_WEAPONDAMAGEBONUSMELEEONLY;
  1869. tov_itemstat_conversion[88] = TOV_ITEM_STAT_ADDITIONALRIPOSTECHANCE;
  1870. tov_itemstat_conversion[89] = TOV_ITEM_STAT_PVPTOUGHNESS;
  1871. tov_itemstat_conversion[90] = TOV_ITEM_STAT_PVPLETHALITY;
  1872. tov_itemstat_conversion[91] = TOV_ITEM_STAT_STAMINABONUS;
  1873. tov_itemstat_conversion[92] = TOV_ITEM_STAT_WISDOMMITBONUS;
  1874. tov_itemstat_conversion[93] = TOV_ITEM_STAT_HEALRECEIVE;
  1875. tov_itemstat_conversion[94] = TOV_ITEM_STAT_HEALRECEIVEPERC;
  1876. tov_itemstat_conversion[95] = TOV_ITEM_STAT_PVPCRITICALMITIGATION;
  1877. tov_itemstat_conversion[96] = TOV_ITEM_STAT_BASEAVOIDANCEBONUS;
  1878. tov_itemstat_conversion[97] = TOV_ITEM_STAT_INCOMBATSAVAGERYREGEN;
  1879. tov_itemstat_conversion[98] = TOV_ITEM_STAT_OUTOFCOMBATSAVAGERYREGEN;
  1880. tov_itemstat_conversion[99] = TOV_ITEM_STAT_SAVAGERYREGEN;
  1881. tov_itemstat_conversion[100] = TOV_ITEM_STAT_SAVAGERYGAINMOD;
  1882. tov_itemstat_conversion[101] = TOV_ITEM_STAT_MAXSAVAGERYLEVEL;
  1883. }
  1884. int32 World::LoadItemBlueStats() {
  1885. Query query;
  1886. MYSQL_ROW row;
  1887. int32 count = 0;
  1888. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT version_range1,version_range2,emu_stat,name,stat from itemstats");
  1889. if (result && mysql_num_rows(result) > 0) {
  1890. while (result && (row = mysql_fetch_row(result))) {
  1891. count++;
  1892. if (atoi(row[0]) >= 63119) //KA
  1893. ka_itemstat_conversion[atoi(row[2])] = atoi(row[4]);
  1894. else if (atoi(row[0]) >= 57101) // ToV
  1895. tov_itemstat_conversion[atoi(row[2])] = atoi(row[4]);
  1896. else if (atoi(row[0]) >= 1193) // CoE
  1897. coe_itemstat_conversion[atoi(row[2])] = atoi(row[4]);
  1898. else if (atoi(row[0]) >= 1096) // DoV
  1899. dov_itemstat_conversion[atoi(row[2])] = atoi(row[4]);
  1900. }
  1901. }
  1902. return count;
  1903. }
  1904. sint16 World::GetItemStatTOVValue(sint16 subtype) {
  1905. return (tov_itemstat_conversion[subtype] - 600);
  1906. }
  1907. sint16 World::GetItemStatDOVValue(sint16 subtype) {
  1908. return (dov_itemstat_conversion[subtype] - 600);
  1909. }
  1910. sint16 World::GetItemStatCOEValue(sint16 subtype) {
  1911. return (coe_itemstat_conversion[subtype] - 600);
  1912. }
  1913. sint16 World::GetItemStatKAValue(sint16 subtype) {
  1914. return (ka_itemstat_conversion[subtype] - 600);
  1915. }
  1916. bool World::CheckTempBugCRC(char* msg)
  1917. {
  1918. MBugReport.writelock();
  1919. sint32 crc = GetItemNameCrc(std::string(msg));
  1920. if (bug_report_crc.count(crc) > 0)
  1921. {
  1922. MBugReport.releasewritelock();
  1923. return true;
  1924. }
  1925. else
  1926. bug_report_crc.insert(make_pair(crc, true));
  1927. MBugReport.releasewritelock();
  1928. return false;
  1929. }
  1930. #ifdef WIN32
  1931. ulong World::GetCurrentThreadID(){
  1932. return GetCurrentThreadId();
  1933. }
  1934. int64 World::GetThreadUsageCPUTime(){
  1935. HANDLE handle = GetCurrentThread();
  1936. int64 lpCreationTime;
  1937. int64 lpExitTime;
  1938. int64 lpKernelTime;
  1939. int64 lpUserTime;
  1940. if(GetThreadTimes(handle, (FILETIME*)&lpCreationTime, (FILETIME*)&lpExitTime, (FILETIME*)&lpKernelTime, (FILETIME*)&lpUserTime))
  1941. return lpKernelTime + lpUserTime;
  1942. return 0;
  1943. }
  1944. #else
  1945. #endif
  1946. void World::SyncCharacterAbilities(Client* client)
  1947. {
  1948. MStartingLists.readlock();
  1949. int8 baseClass = classes.GetBaseClass(client->GetPlayer()->GetAdventureClass());
  1950. int8 secondaryClass = classes.GetSecondaryBaseClass(client->GetPlayer()->GetAdventureClass());
  1951. int8 actualClass = client->GetPlayer()->GetAdventureClass();
  1952. int8 baseRace = client->GetPlayer()->GetRace();
  1953. bool reloadSpells = false;
  1954. multimap<int8, multimap<int8, StartingSkill>*>::iterator skill_itr = starting_skills.begin();
  1955. multimap<int8, multimap<int8, StartingSpell>*>::iterator spell_itr = starting_spells.begin();
  1956. bool isComplete = false;
  1957. int8 wait_iterations = 0; // wait 5 iterations and give up if db takes too long
  1958. do
  1959. {
  1960. bool isProcessing = false;
  1961. if (skill_itr != starting_skills.end())
  1962. {
  1963. isProcessing = true;
  1964. // race = 255 is wildcard all, otherwise needs to match the race id
  1965. if (skill_itr->first == 255 || skill_itr->first == baseRace)
  1966. {
  1967. multimap<int8, StartingSkill>::iterator child_itr;
  1968. for (child_itr = skill_itr->second->begin(); child_itr != skill_itr->second->end(); child_itr++)
  1969. {
  1970. // class = 255 is wildcard all, otherwise needs to match the class id
  1971. if (child_itr->first == 255 ||
  1972. child_itr->first == baseClass ||
  1973. child_itr->first == secondaryClass ||
  1974. child_itr->first == actualClass)
  1975. {
  1976. if (!client->GetPlayer()->skill_list.HasSkill(child_itr->second.skill_id))
  1977. {
  1978. Query query;
  1979. 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());
  1980. 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)",
  1981. client->GetCharacterID(), child_itr->second.skill_id, child_itr->second.current_val, child_itr->second.max_val);
  1982. client->GetPlayer()->AddSkill(child_itr->second.skill_id, child_itr->second.current_val, child_itr->second.max_val);
  1983. }
  1984. }
  1985. }
  1986. }
  1987. skill_itr++;
  1988. }
  1989. if (spell_itr != starting_spells.end())
  1990. {
  1991. isProcessing = true;
  1992. // race = 255 is wildcard all, otherwise needs to match the race id
  1993. if (spell_itr->first == 255 || spell_itr->first == baseRace)
  1994. {
  1995. multimap<int8, StartingSpell>::iterator child_itr;
  1996. for (child_itr = spell_itr->second->begin(); child_itr != spell_itr->second->end(); child_itr++)
  1997. {
  1998. // class = 255 is wildcard all, otherwise needs to match the class id
  1999. if (child_itr->first == 255 ||
  2000. child_itr->first == baseClass ||
  2001. child_itr->first == secondaryClass ||
  2002. child_itr->first == actualClass)
  2003. {
  2004. if (!client->GetPlayer()->HasSpell(child_itr->second.spell_id, child_itr->second.tier, true))
  2005. {
  2006. Query query;
  2007. 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());
  2008. // knowledge_slot is a signed int in the DB
  2009. query.AddQueryAsync(client->GetCharacterID(), &database, Q_INSERT, "INSERT IGNORE INTO character_spells (char_id, spell_id, tier, knowledge_slot) VALUES (%u, %u, %u, %i)",
  2010. client->GetCharacterID(), child_itr->second.spell_id, child_itr->second.tier, child_itr->second.knowledge_slot);
  2011. // reload spells, we don't know the spellbook or timer info
  2012. reloadSpells = true;
  2013. }
  2014. }
  2015. }
  2016. }
  2017. spell_itr++;
  2018. }
  2019. // poor mans async kill just in case, we don't want a client stuck here, they can get the spells on next zone
  2020. if (!isProcessing)
  2021. {
  2022. bool isDBActive = database.IsActiveQuery(client->GetCharacterID());
  2023. if (isDBActive && wait_iterations < 5)
  2024. {
  2025. wait_iterations++;
  2026. }
  2027. else
  2028. isComplete = true;
  2029. }
  2030. } while (!isComplete);
  2031. MStartingLists.releasereadlock();
  2032. if (reloadSpells)
  2033. database.LoadCharacterSpells(client->GetCharacterID(), client->GetPlayer());
  2034. }
  2035. void World::LoadStartingLists()
  2036. {
  2037. LogWrite(WORLD__DEBUG, 1, "World", "-Loading `starting_skills`...");
  2038. database.LoadStartingSkills(this);
  2039. LogWrite(WORLD__DEBUG, 1, "World", "-Loading `starting_spells`...");
  2040. database.LoadStartingSpells(this);
  2041. }
  2042. void World::PurgeStartingLists()
  2043. {
  2044. MStartingLists.writelock();
  2045. multimap<int8, multimap<int8, StartingSkill>*>::iterator skill_itr;
  2046. for (skill_itr = starting_skills.begin(); skill_itr != starting_skills.end(); skill_itr++)
  2047. {
  2048. multimap<int8, StartingSkill>* tmpMap = skill_itr->second;
  2049. safe_delete(tmpMap);
  2050. }
  2051. starting_skills.clear();
  2052. multimap<int8, multimap<int8, StartingSpell>*>::iterator spell_itr;
  2053. for (spell_itr = starting_spells.begin(); spell_itr != starting_spells.end(); spell_itr++)
  2054. {
  2055. multimap<int8, StartingSpell>* tmpMap = spell_itr->second;
  2056. safe_delete(tmpMap);
  2057. }
  2058. starting_spells.clear();
  2059. MStartingLists.releasewritelock();
  2060. }
  2061. void World::SetReloadingSubsystem(string subsystem) {
  2062. MReloadingSubsystems.lock();
  2063. reloading_subsystems[subsystem] = Timer::GetCurrentTime2();
  2064. MReloadingSubsystems.unlock();
  2065. }
  2066. void World::RemoveReloadingSubSystem(string subsystem) {
  2067. MReloadingSubsystems.lock();
  2068. if (reloading_subsystems.count(subsystem) > 0)
  2069. reloading_subsystems.erase(subsystem);
  2070. MReloadingSubsystems.unlock();
  2071. }
  2072. bool World::IsReloadingSubsystems() {
  2073. bool result = false;
  2074. MReloadingSubsystems.lock();
  2075. result = reloading_subsystems.size() > 0;
  2076. MReloadingSubsystems.unlock();
  2077. return result;
  2078. }
  2079. map<string, int32> World::GetOldestReloadingSubsystem() {
  2080. map<string, int32> result;
  2081. MReloadingSubsystems.lock();
  2082. int32 current_time = Timer::GetCurrentTime2();
  2083. map<string, int32>::iterator itr;
  2084. int32 oldest = current_time;
  2085. string oldestname = "";
  2086. for (itr = reloading_subsystems.begin(); itr != reloading_subsystems.end(); itr++) {
  2087. if (itr->second < oldest)
  2088. oldestname = itr->first;
  2089. }
  2090. result[oldestname] = oldest;
  2091. MReloadingSubsystems.unlock();
  2092. return result;
  2093. }
  2094. void ZoneList::WatchdogHeartbeat()
  2095. {
  2096. list<ZoneServer*>::iterator zone_iter;
  2097. ZoneServer* tmp = 0;
  2098. MZoneList.writelock(__FUNCTION__, __LINE__);
  2099. bool match = false;
  2100. for (zone_iter = zlist.begin(); zone_iter != zlist.end(); zone_iter++)
  2101. {
  2102. tmp = *zone_iter;
  2103. if (tmp)
  2104. {
  2105. int32 curTime = Timer::GetCurrentTime2();
  2106. sint64 diff = (sint64)curTime - (sint64)tmp->GetWatchdogTime();
  2107. if (diff > 120000)
  2108. {
  2109. LogWrite(WORLD__ERROR, 1, "World", "Zone %s is hung for %i milliseconds.. attempting to cancel threads...", tmp->GetZoneName(), diff);
  2110. #ifndef WIN32
  2111. tmp->CancelThreads();
  2112. zlist.erase(zone_iter);
  2113. safe_delete(tmp);
  2114. #endif
  2115. MZoneList.releasewritelock(__FUNCTION__, __LINE__);
  2116. match = true;
  2117. break;
  2118. }
  2119. else if (diff > 90000 && !tmp->isZoneShuttingDown())
  2120. {
  2121. tmp->SetWatchdogTime(Timer::GetCurrentTime2()); // reset so we don't continuously flood this heartbeat
  2122. map<string, int32> oldest_process = world.GetOldestReloadingSubsystem();
  2123. if (oldest_process.size() > 0) {
  2124. map<string, int32>::iterator itr = oldest_process.begin();
  2125. if(itr != oldest_process.end())
  2126. 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);
  2127. else
  2128. LogWrite(WORLD__ERROR, 1, "World", "Zone %s is hung for %i milliseconds... attempting shutdown", tmp->GetZoneName(), diff);
  2129. }
  2130. else
  2131. LogWrite(WORLD__ERROR, 1, "World", "Zone %s is hung for %i milliseconds.. attempting shutdown", tmp->GetZoneName(), diff);
  2132. tmp->Shutdown();
  2133. }
  2134. else if (diff > 30000)
  2135. {
  2136. if (world.IsReloadingSubsystems()) {
  2137. if (world.GetSuppressedWarningTime() == 0) {
  2138. world.SetSuppressedWarning();
  2139. map<string, int32> oldest_process = world.GetOldestReloadingSubsystem();
  2140. if (oldest_process.size() > 0) {
  2141. map<string, int32>::iterator itr = oldest_process.begin();
  2142. if(itr != oldest_process.end())
  2143. LogWrite(WORLD__ERROR, 1, "World", "Zone %s is hung for %i milliseconds.. while waiting for %s to reload...", tmp->GetZoneName(), diff, itr->first);
  2144. else
  2145. LogWrite(WORLD__ERROR, 1, "World", "Zone %s is hung for %i milliseconds...", tmp->GetZoneName(), diff);
  2146. }
  2147. }
  2148. continue;
  2149. }
  2150. }
  2151. else if (diff > 60000 && !tmp->isZoneShuttingDown())
  2152. {
  2153. if (world.IsReloadingSubsystems())
  2154. continue;
  2155. LogWrite(WORLD__ERROR, 1, "World", "Zone %s is hung for %i milliseconds.. attempting shutdown", tmp->GetZoneName(), diff);
  2156. tmp->Shutdown();
  2157. }
  2158. }
  2159. }
  2160. if(!match)
  2161. MZoneList.releasewritelock(__FUNCTION__, __LINE__);
  2162. }
  2163. void World::LoadRegionMaps(std::string zoneFile)
  2164. {
  2165. string zoneToLower(zoneFile);
  2166. boost::algorithm::to_lower(zoneToLower);
  2167. MWorldRegionMaps.writelock();
  2168. std::map<std::string, RegionMapRange*>::iterator itr;
  2169. itr = region_maps.find(zoneToLower);
  2170. if (itr == region_maps.end())
  2171. {
  2172. RegionMapRange* newRange = new RegionMapRange();
  2173. newRange->AddVersionRange(zoneFile);
  2174. region_maps.insert(make_pair(zoneToLower, newRange));
  2175. }
  2176. MWorldRegionMaps.releasewritelock();
  2177. }
  2178. RegionMap* World::GetRegionMap(std::string zoneFile, int32 client_version)
  2179. {
  2180. string zoneToLower(zoneFile);
  2181. boost::algorithm::to_lower(zoneToLower);
  2182. MWorldRegionMaps.readlock();
  2183. std::map<std::string, RegionMapRange*>::iterator itr;
  2184. itr = region_maps.find(zoneToLower);
  2185. if ( itr != region_maps.end())
  2186. {
  2187. std::map<VersionRange*, RegionMap*>::iterator rmitr;
  2188. rmitr = itr->second->FindRegionByVersion(client_version);
  2189. if ( rmitr != itr->second->GetRangeEnd())
  2190. {
  2191. MWorldRegionMaps.releasereadlock();
  2192. return rmitr->second;
  2193. }
  2194. }
  2195. MWorldRegionMaps.releasereadlock();
  2196. return nullptr;
  2197. }
  2198. void World::LoadMaps(std::string zoneFile)
  2199. {
  2200. string zoneToLower(zoneFile);
  2201. boost::algorithm::to_lower(zoneToLower);
  2202. MWorldMaps.writelock();
  2203. std::map<std::string, MapRange*>::iterator itr;
  2204. itr = maps.find(zoneToLower);
  2205. if (itr == maps.end())
  2206. {
  2207. MapRange* newRange = new MapRange();
  2208. newRange->AddVersionRange(zoneFile);
  2209. maps.insert(make_pair(zoneToLower, newRange));
  2210. }
  2211. MWorldMaps.releasewritelock();
  2212. }
  2213. Map* World::GetMap(std::string zoneFile, int32 client_version)
  2214. {
  2215. string zoneToLower(zoneFile);
  2216. boost::algorithm::to_lower(zoneToLower);
  2217. MWorldMaps.readlock();
  2218. std::map<std::string, MapRange*>::iterator itr;
  2219. itr = maps.find(zoneToLower);
  2220. if ( itr != maps.end())
  2221. {
  2222. std::map<VersionRange*, Map*>::iterator rmitr;
  2223. rmitr = itr->second->FindMapByVersion(client_version);
  2224. if ( rmitr != itr->second->GetRangeEnd())
  2225. {
  2226. MWorldMaps.releasereadlock();
  2227. return rmitr->second;
  2228. }
  2229. }
  2230. MWorldMaps.releasereadlock();
  2231. return nullptr;
  2232. }