World.cpp 73 KB

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