Spawn.cpp 111 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743
  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 "Spawn.h"
  17. #include <stdio.h>
  18. #include "../common/timer.h"
  19. #include <time.h>
  20. #include <math.h>
  21. #include "Entity.h"
  22. #include "Widget.h"
  23. #include "Sign.h"
  24. #include "../common/MiscFunctions.h"
  25. #include "../common/Log.h"
  26. #include "Rules/Rules.h"
  27. #include "World.h"
  28. #include "LuaInterface.h"
  29. #include "Zone/SPGrid.h"
  30. #include "Bots/Bot.h"
  31. #include "Zone/raycast_mesh.h"
  32. extern ConfigReader configReader;
  33. extern RuleManager rule_manager;
  34. extern World world;
  35. Spawn::Spawn(){
  36. loot_coins = 0;
  37. trap_triggered = false;
  38. group_id = 0;
  39. size_offset = 0;
  40. merchant_id = 0;
  41. merchant_type = 0;
  42. merchant_min_level = 0;
  43. merchant_max_level = 0;
  44. memset(&appearance, 0, sizeof(AppearanceData));
  45. memset(&basic_info, 0, sizeof(BasicInfoStruct));
  46. appearance.pos.state = 0x4080;
  47. appearance.encounter_level =6;
  48. size = 32;
  49. appearance.pos.collision_radius = 32;
  50. id = Spawn::NextID();
  51. oversized_packet = 0xFF;
  52. zone = 0;
  53. spawn_location_id = 0;
  54. spawn_entry_id = 0;
  55. spawn_location_spawns_id = 0;
  56. respawn = 0;
  57. expire_time = 0;
  58. expire_offset = 0;
  59. x_offset = 0;
  60. y_offset = 0;
  61. z_offset = 0;
  62. database_id = 0;
  63. packet_num = 1;
  64. changed = false;
  65. vis_changed = false;
  66. position_changed = false;
  67. send_spawn_changes = true;
  68. info_changed = false;
  69. appearance.pos.Speed1 = 0;
  70. last_attacker = 0;
  71. faction_id = 0;
  72. running_to = 0;
  73. tmp_visual_state = -1;
  74. tmp_action_state = -1;
  75. transporter_id = 0;
  76. invulnerable = false;
  77. spawn_group_list = 0;
  78. MSpawnGroup = 0;
  79. movement_locations = 0;
  80. MMovementLocations = 0;
  81. target = 0;
  82. primary_command_list_id = 0;
  83. secondary_command_list_id = 0;
  84. is_pet = false;
  85. m_followTarget = 0;
  86. following = false;
  87. req_quests_continued_access = false;
  88. req_quests_override = 0;
  89. req_quests_private = false;
  90. m_illusionModel = 0;
  91. Cell_Info.CurrentCell = nullptr;
  92. Cell_Info.CellListIndex = -1;
  93. m_addedToWorldTimestamp = 0;
  94. m_spawnAnim = 0;
  95. m_spawnAnimLeeway = 0;
  96. m_Update.SetName("Spawn::m_Update");
  97. m_requiredHistory.SetName("Spawn::m_requiredHistory");
  98. m_requiredQuests.SetName("Spawn::m_requiredQuests");
  99. last_heading_angle = 0.0;
  100. last_grid_update = 0;
  101. last_location_update = 0.0;
  102. last_movement_update = Timer::GetCurrentTime2();
  103. forceMapCheck = false;
  104. m_followDistance = 0;
  105. MCommandMutex.SetName("Entity::MCommandMutex");
  106. has_spawn_proximities = false;
  107. pickup_item_id = 0;
  108. pickup_unique_item_id = 0;
  109. disable_sounds = false;
  110. }
  111. Spawn::~Spawn(){
  112. vector<Item*>::iterator itr;
  113. for (itr = loot_items.begin(); itr != loot_items.end(); itr++)
  114. safe_delete(*itr);
  115. RemovePrimaryCommands();
  116. for(int32 i=0;i<secondary_command_list.size();i++){
  117. safe_delete(secondary_command_list[i]);
  118. }
  119. RemoveSpawnFromGroup();
  120. if (MMovementLocations)
  121. MMovementLocations->writelock(__FUNCTION__, __LINE__);
  122. if(movement_locations){
  123. while(movement_locations->size()){
  124. safe_delete(movement_locations->front());
  125. movement_locations->pop_front();
  126. }
  127. safe_delete(movement_locations);
  128. }
  129. if (MMovementLocations)
  130. MMovementLocations->releasewritelock(__FUNCTION__, __LINE__);
  131. safe_delete(MMovementLocations);
  132. MMovementLoop.lock();
  133. for (int32 i = 0; i < movement_loop.size(); i++)
  134. safe_delete(movement_loop.at(i));
  135. MMovementLoop.unlock();
  136. map<int32, vector<int16>* >::iterator rq_itr;
  137. m_requiredQuests.writelock(__FUNCTION__, __LINE__);
  138. for (rq_itr = required_quests.begin(); rq_itr != required_quests.end(); rq_itr++){
  139. safe_delete(rq_itr->second);
  140. }
  141. m_requiredQuests.releasewritelock(__FUNCTION__, __LINE__);
  142. // just in case to make sure data is destroyed
  143. RemoveSpawnProximities();
  144. }
  145. void Spawn::RemovePrimaryCommands()
  146. {
  147. for (int32 i = 0; i < primary_command_list.size(); i++) {
  148. safe_delete(primary_command_list[i]);
  149. }
  150. primary_command_list.clear();
  151. }
  152. void Spawn::InitializeHeaderPacketData(Player* player, PacketStruct* header, int16 index) {
  153. header->setDataByName("index", index);
  154. if (GetSpawnAnim() > 0 && Timer::GetCurrentTime2() < (GetAddedToWorldTimestamp() + GetSpawnAnimLeeway())) {
  155. if (header->GetVersion() >= 57080)
  156. header->setDataByName("spawn_anim", GetSpawnAnim());
  157. else
  158. header->setDataByName("spawn_anim", (int16)GetSpawnAnim());
  159. }
  160. else {
  161. if (header->GetVersion() >= 57080)
  162. header->setDataByName("spawn_anim", 0xFFFFFFFF);
  163. else
  164. header->setDataByName("spawn_anim", 0xFFFF);
  165. }
  166. if (primary_command_list.size() > 0){
  167. if (primary_command_list.size() > 1) {
  168. header->setArrayLengthByName("command_list", primary_command_list.size());
  169. for (int32 i = 0; i < primary_command_list.size(); i++) {
  170. header->setArrayDataByName("command_list_name", primary_command_list[i]->name.c_str(), i);
  171. header->setArrayDataByName("command_list_max_distance", primary_command_list[i]->distance, i);
  172. header->setArrayDataByName("command_list_error", primary_command_list[i]->error_text.c_str(), i);
  173. header->setArrayDataByName("command_list_command", primary_command_list[i]->command.c_str(), i);
  174. }
  175. }
  176. header->setMediumStringByName("default_command", primary_command_list[0]->command.c_str());
  177. header->setDataByName("max_distance", primary_command_list[0]->distance);
  178. }
  179. if (spawn_group_list && MSpawnGroup){
  180. MSpawnGroup->readlock(__FUNCTION__, __LINE__);
  181. header->setArrayLengthByName("group_size", spawn_group_list->size());
  182. vector<Spawn*>::iterator itr;
  183. int i = 0;
  184. for (itr = spawn_group_list->begin(); itr != spawn_group_list->end(); itr++, i++){
  185. header->setArrayDataByName("group_spawn_id", player->GetIDWithPlayerSpawn((*itr)), i);
  186. }
  187. MSpawnGroup->releasereadlock(__FUNCTION__, __LINE__);
  188. }
  189. header->setDataByName("spawn_id", player->GetIDWithPlayerSpawn(this));
  190. header->setDataByName("crc", 1);
  191. header->setDataByName("time_stamp", Timer::GetCurrentTime2());
  192. }
  193. void Spawn::InitializeVisPacketData(Player* player, PacketStruct* vis_packet) {
  194. int16 version = vis_packet->GetVersion();
  195. if (IsPlayer())
  196. appearance.pos.grid_id = 0xFFFFFFFF;
  197. if (IsPlayer())
  198. vis_packet->setDataByName("player", 1);
  199. if (version <= 546) {
  200. vis_packet->setDataByName("targetable", appearance.targetable);
  201. vis_packet->setDataByName("show_name", appearance.display_name);
  202. vis_packet->setDataByName("attackable", appearance.attackable);
  203. if (IsPlayer()) {
  204. if (((Player*)this)->IsGroupMember(player))
  205. vis_packet->setDataByName("group_member", 1);
  206. }
  207. }
  208. if (appearance.targetable == 1 || appearance.show_level == 1 || appearance.display_name == 1) {
  209. if (!IsGroundSpawn()) {
  210. int8 arrow_color = ARROW_COLOR_WHITE;
  211. sint8 npc_con = player->GetFactions()->GetCon(faction_id);
  212. if (IsPlayer() && !((Player*)this)->CanSeeInvis(player))
  213. npc_con = 0;
  214. else if (!IsPlayer() && IsEntity() && !((Entity*)this)->CanSeeInvis(player))
  215. npc_con = 0;
  216. if (appearance.attackable == 1)
  217. arrow_color = player->GetArrowColor(GetLevel());
  218. /*if (version <= 283) {
  219. if (GetMerchantID() > 0)
  220. arrow_color += 7;
  221. else {
  222. if (primary_command_list.size() > 0) {
  223. int16 len = strlen(primary_command_list[0]->command.c_str());
  224. if(len >= 4 && strncmp(primary_command_list[0]->command.c_str(), "bank", 4) == 0)
  225. arrow_color += 14;
  226. else if (len >= 4 && strncmp(primary_command_list[0]->command.c_str(), "hail", 4) == 0)
  227. arrow_color += 21;
  228. else if (len >= 6 && strncmp(primary_command_list[0]->command.c_str(), "attack", 6) == 0) {
  229. if (arrow_color > 5)
  230. arrow_color = 34;
  231. else
  232. arrow_color += 29;
  233. }
  234. }
  235. }
  236. }*/
  237. vis_packet->setDataByName("arrow_color", arrow_color);
  238. vis_packet->setDataByName("locked_no_loot", appearance.locked_no_loot);
  239. if (player->GetArrowColor(GetLevel()) == ARROW_COLOR_GRAY)
  240. if (npc_con == -4)
  241. npc_con = -3;
  242. vis_packet->setDataByName("npc_con", npc_con);
  243. if (appearance.attackable == 1 && IsNPC() && (player->GetFactions()->GetCon(faction_id) <= -4 || ((NPC*)this)->Brain()->GetHate(player) > 1)) {
  244. vis_packet->setDataByName("npc_hate", ((NPC*)this)->Brain()->GetHatePercentage(player));
  245. vis_packet->setDataByName("show_difficulty_arrows", 1);
  246. }
  247. int8 quest_flag = player->CheckQuestFlag(this);
  248. if (version < 1188 && quest_flag >= 16)
  249. quest_flag = 1;
  250. vis_packet->setDataByName("quest_flag", quest_flag);
  251. if (player->CheckQuestsKillUpdate(this, false)) {
  252. vis_packet->setDataByName("name_quest_icon", 1);
  253. }
  254. }
  255. }
  256. int8 vis_flags = 0;
  257. if (MeetsSpawnAccessRequirements(player)) {
  258. if (appearance.attackable == 1)
  259. vis_flags += 64; //attackable icon
  260. if (appearance.show_level == 1)
  261. vis_flags += 32;
  262. if (appearance.display_name == 1)
  263. vis_flags += 16;
  264. if (IsPlayer() || appearance.targetable == 1)
  265. vis_flags += 4;
  266. if (appearance.show_command_icon == 1)
  267. vis_flags += 2;
  268. if (this == player) {
  269. //if (version <= 283) {
  270. // vis_flags = 1;
  271. //}
  272. //else
  273. vis_flags += 1;
  274. }
  275. }
  276. else if (req_quests_override > 0)
  277. {
  278. //Check to see if there's an override value set
  279. vis_flags = req_quests_override & 0xFF;
  280. }
  281. if (player->HasGMVision())
  282. {
  283. if ((vis_flags & 16) == 0 && appearance.display_name == 0)
  284. vis_flags += 16;
  285. if ((vis_flags & 4) == 0)
  286. vis_flags += 4;
  287. }
  288. if (version <= 546 && vis_flags > 0)
  289. vis_flags = 1;
  290. vis_packet->setDataByName("vis_flags", vis_flags);
  291. if (MeetsSpawnAccessRequirements(player))
  292. vis_packet->setDataByName("hand_flag", appearance.display_hand_icon);
  293. else {
  294. if ((req_quests_override & 256) > 0)
  295. vis_packet->setDataByName("hand_flag", 1);
  296. }
  297. }
  298. void Spawn::InitializeFooterPacketData(Player* player, PacketStruct* footer) {
  299. if (IsWidget()){
  300. Widget* widget = (Widget*)this;
  301. if (widget->GetMultiFloorLift()) {
  302. footer->setDataByName("widget_x", widget->GetX());
  303. footer->setDataByName("widget_y", widget->GetY());
  304. footer->setDataByName("widget_z", widget->GetZ());
  305. }
  306. else {
  307. footer->setDataByName("widget_x", widget->GetWidgetX());
  308. footer->setDataByName("widget_y", widget->GetWidgetY());
  309. footer->setDataByName("widget_z", widget->GetWidgetZ());
  310. }
  311. footer->setDataByName("widget_id", widget->GetWidgetID());
  312. footer->setDataByName("unknown3c", 6);
  313. }
  314. else if (IsSign()){
  315. Sign* sign = (Sign*)this;
  316. footer->setDataByName("widget_id", sign->GetWidgetID());
  317. footer->setDataByName("widget_x", sign->GetWidgetX());
  318. footer->setDataByName("widget_y", sign->GetWidgetY());
  319. footer->setDataByName("widget_z", sign->GetWidgetZ());
  320. footer->setDataByName("unknown2b", 6);
  321. if (sign->GetSignTitle())
  322. footer->setMediumStringByName("title", sign->GetSignTitle());
  323. if (sign->GetSignDescription())
  324. footer->setMediumStringByName("description", sign->GetSignDescription());
  325. footer->setDataByName("sign_distance", sign->GetSignDistance());
  326. footer->setDataByName("show", 1);
  327. }
  328. if ( IsPlayer())
  329. footer->setDataByName("is_player", 1);
  330. if (strlen(appearance.name) < 1)
  331. strncpy(appearance.name,to_string(GetID()).c_str(),128);
  332. footer->setMediumStringByName("name", appearance.name);
  333. footer->setMediumStringByName("guild", appearance.sub_title);
  334. footer->setMediumStringByName("prefix", appearance.prefix_title);
  335. footer->setMediumStringByName("suffix", appearance.suffix_title);
  336. footer->setMediumStringByName("last_name", appearance.last_name);
  337. if (appearance.attackable == 0 && GetLevel() > 0)
  338. footer->setDataByName("spawn_type", 1);
  339. else if (appearance.attackable == 0)
  340. footer->setDataByName("spawn_type", 6);
  341. else
  342. footer->setDataByName("spawn_type", 3);
  343. }
  344. EQ2Packet* Spawn::spawn_serialize(Player* player, int16 version, int16 offset, int32 value, int16 offset2, int16 offset3, int16 offset4, int32 value2) {
  345. // If spawn is NPC AND is pet && owner is a player && owner is the player passed to this function && player's char sheet pet id is 0
  346. if (IsNPC() && ((NPC*)this)->IsPet() && ((NPC*)this)->GetOwner()->IsPlayer() && player == ((NPC*)this)->GetOwner() && player->GetInfoStruct()->pet_id == 0) {
  347. ((Player*)((NPC*)this)->GetOwner())->GetInfoStruct()->pet_id = player->spawn_id;
  348. player->SetCharSheetChanged(true);
  349. }
  350. int16 index;
  351. if (player->player_spawn_index_map.count(this) > 0) {
  352. index = player->player_spawn_index_map[this];
  353. player->player_spawn_map[index] = this;
  354. }
  355. else {
  356. player->spawn_index++;
  357. if (player->spawn_index == 255)
  358. player->spawn_index++; //just so we dont have to worry about overloading
  359. index = player->spawn_index;
  360. player->player_spawn_index_map[this] = index;
  361. player->player_spawn_map[index] = this;
  362. }
  363. // Jabantiz - [Bug] Client Crash on Revive
  364. if (player->player_spawn_reverse_id_map.count(this) == 0) {
  365. int32 spawn_id = ++player->spawn_id;
  366. player->player_spawn_id_map[spawn_id] = this;
  367. player->player_spawn_reverse_id_map[this] = spawn_id;
  368. }
  369. m_Update.writelock(__FUNCTION__, __LINE__);
  370. PacketStruct* header = player->GetSpawnHeaderStruct();
  371. header->ResetData();
  372. InitializeHeaderPacketData(player, header, index);
  373. PacketStruct* footer = 0;
  374. if (IsWidget())
  375. footer = player->GetWidgetFooterStruct();
  376. else if (IsSign())
  377. footer = player->GetSignFooterStruct();
  378. else if (version > 546)
  379. footer = player->GetSpawnFooterStruct();
  380. if (footer) {
  381. footer->ResetData();
  382. InitializeFooterPacketData(player, footer);
  383. }
  384. PacketStruct* vis_struct = player->GetSpawnVisStruct();
  385. PacketStruct* info_struct = player->GetSpawnInfoStruct();
  386. PacketStruct* pos_struct = player->GetSpawnPosStruct();
  387. player->vis_mutex.writelock(__FUNCTION__, __LINE__);
  388. vis_struct->ResetData();
  389. InitializeVisPacketData(player, vis_struct);
  390. player->info_mutex.writelock(__FUNCTION__, __LINE__);
  391. info_struct->ResetData();
  392. InitializeInfoPacketData(player, info_struct);
  393. player->pos_mutex.writelock(__FUNCTION__, __LINE__);
  394. pos_struct->ResetData();
  395. InitializePosPacketData(player, pos_struct);
  396. if (version <= 283) {
  397. if (offset == 777) {
  398. info_struct->setDataByName("name", "This is a really long name\n");
  399. info_struct->setDataByName("last_name", "This is a really long LAST name\n");
  400. info_struct->setDataByName("name_suffix", "This is a really long SUFFIX\n");
  401. info_struct->setDataByName("name_prefix", "This is a really long PREFIX\n");
  402. info_struct->setDataByName("unknown", "This is a really long UNKNOWN\n");
  403. info_struct->setDataByName("second_suffix", "This is a really long 2nd SUFFIX\n");
  404. }
  405. //info_struct->setDataByName("unknown2", 3, 0); // level
  406. //info_struct->setDataByName("unknown2", 1, 1); //unknown, two down arrows
  407. //info_struct->setDataByName("unknown2", 1, 2); //unknown
  408. //info_struct->setDataByName("unknown2", 1, 3); //unknown
  409. //info_struct->setDataByName("unknown2", 1, 4); //solo fight
  410. //info_struct->setDataByName("unknown2", 1, 5); //unknown
  411. //info_struct->setDataByName("unknown2", 1, 6); //unknown
  412. //info_struct->setDataByName("unknown2", 1, 7); //merchant
  413. //info_struct->setDataByName("unknown2", 1, 8); //unknown
  414. //info_struct->setDataByName("unknown2", 1, 9); //unknown
  415. //info_struct->setDataByName("unknown2", 1, 10);
  416. //112: 00 00 00 01 02 03 04 05 - 06 07 08 09 0A 00 00 00 | ................ merchant, x4
  417. //112: 00 00 00 01 02 03 04 05 - 00 00 00 00 00 00 00 00 | ................ x4, epic, indifferent
  418. //info_struct->setDataByName("body_size", 42);
  419. //for(int i=0;i<8;i++)
  420. // info_struct->setDataByName("persistent_spell_visuals", 329, i);
  421. //info_struct->setDataByName("persistent_spell_levels", 20);
  422. }
  423. string* vis_data = vis_struct->serializeString();
  424. string* pos_data = pos_struct->serializeString();
  425. string* info_data = info_struct->serializeString();
  426. int16 part2_size = pos_data->length() + vis_data->length() + info_data->length();
  427. uchar* part2 = new uchar[part2_size];
  428. player->AddSpawnPosPacketForXOR(id, (uchar*)pos_data->c_str(), pos_data->length());
  429. player->AddSpawnVisPacketForXOR(id, (uchar*)vis_data->c_str(), vis_data->length());
  430. player->AddSpawnInfoPacketForXOR(id, (uchar*)info_data->c_str(), info_data->length());
  431. uchar* ptr = part2;
  432. memcpy(ptr, pos_data->c_str(), pos_data->length());
  433. ptr += pos_data->length();
  434. memcpy(ptr, vis_data->c_str(), vis_data->length());
  435. ptr += vis_data->length();
  436. memcpy(ptr, info_data->c_str(), info_data->length());
  437. player->pos_mutex.releasewritelock(__FUNCTION__, __LINE__);
  438. player->info_mutex.releasewritelock(__FUNCTION__, __LINE__);
  439. player->vis_mutex.releasewritelock(__FUNCTION__, __LINE__);
  440. string* part1 = header->serializeString();
  441. string* part3 = 0;
  442. if (footer)
  443. part3 = footer->serializeString();
  444. //uchar blah7[] = {0x01,0x01,0x00,0x00,0x01,0x01,0x00,0x01,0x01,0x00 };
  445. //uchar blah7[] = { 0x03,0x01,0x00,0x01,0x01,0x01,0x00,0x01,0x01,0x00 }; base
  446. //uchar blah7[] = { 0x03,0x00,0x00,0x01,0x01,0x01,0x00,0x01,0x01,0x00 }; //no change
  447. //uchar blah7[] = { 0x03,0x00,0x00,0x00,0x01,0x01,0x00,0x01,0x01,0x00 }; //blue instead of green
  448. //uchar blah7[] = { 0x03,0x00,0x00,0x00,0x00,0x01,0x00,0x01,0x01,0x00 }; //no change
  449. //uchar blah7[] = { 0x03,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x01,0x00 }; //not selectable
  450. //uchar blah7[] = { 0x03,0x00,0x00,0x00,0x00,0x01,0x00,0x00,0x01,0x00 }; //no change
  451. //uchar blah7[] = { 0x03,0x00,0x00,0x00,0x00,0x01,0x00,0x00,0x00,0x00 }; //no longer have the two down arrows
  452. //uchar blah7[] = { 0x01,0x00,0x00,0x00,0x00,0x01,0x00,0x00,0x00,0x00 }; //arrow color green instead of white/gray
  453. //memcpy(part2 + 77, blah7, sizeof(blah7));
  454. //DumpPacket(part2, 885);
  455. if (offset > 0) {
  456. if (offset4 > 0 && offset4 >= offset3) {
  457. uchar* ptr2 = (uchar*)part2;
  458. ptr2 += offset3;
  459. while (offset4 >= offset3) {
  460. int8 jumpsize = 1;
  461. if (value2 > 0xFFFF) {
  462. memcpy(ptr2, (uchar*)&value2, 4);
  463. jumpsize = 4;
  464. }
  465. else if (value2 > 0xFF) {
  466. memcpy(ptr2, (uchar*)&value2, 2);
  467. jumpsize = 2;
  468. }
  469. else {
  470. memcpy(ptr2, (uchar*)&value2, 1);
  471. jumpsize = 1;
  472. }
  473. ptr2 += jumpsize;
  474. offset4 -= jumpsize;
  475. }
  476. }
  477. if (offset2 > 0 && offset2 >= offset) {
  478. uchar* ptr2 = (uchar*)part2;
  479. ptr2 += offset;
  480. while (offset2 >= offset) {
  481. int8 jumpsize = 1;
  482. if (value > 0xFFFF) {
  483. memcpy(ptr2, (uchar*)&value, 4);
  484. jumpsize = 4;
  485. }
  486. else if (value > 0xFF) {
  487. memcpy(ptr2, (uchar*)&value, 2);
  488. jumpsize = 2;
  489. }
  490. else
  491. memcpy(ptr2, (uchar*)&value, 1);
  492. ptr2 += jumpsize;
  493. offset2 -= jumpsize;
  494. }
  495. }
  496. else {
  497. uchar* ptr2 = (uchar*)part2;
  498. ptr2 += offset;
  499. if (value > 0xFFFF)
  500. memcpy(ptr2, (uchar*)&value, 4);
  501. else if (value > 0xFF)
  502. memcpy(ptr2, (uchar*)&value, 2);
  503. else
  504. memcpy(ptr2, (uchar*)&value, 1);
  505. }
  506. cout << "setting offset: " << offset << " to: " << value << endl;
  507. }
  508. if (offset > 0)
  509. DumpPacket(part2, part2_size);
  510. uchar tmp[1100];
  511. bool reverse = (version > 283);
  512. part2_size = Pack(tmp, part2, part2_size, 1100, version, reverse);
  513. int32 total_size = part1->length() + part2_size + 3;
  514. if (part3)
  515. total_size += part3->length();
  516. int32 final_packet_size = total_size + 1;
  517. if (version > 283)
  518. final_packet_size += 3;
  519. else {
  520. if (final_packet_size >= 255) {
  521. final_packet_size += 2;
  522. }
  523. }
  524. uchar* final_packet = new uchar[final_packet_size];
  525. ptr = final_packet;
  526. if (version <= 283) {
  527. if ((final_packet_size - total_size) > 1) {
  528. memcpy(ptr, &oversized_packet, sizeof(oversized_packet));
  529. ptr += sizeof(oversized_packet);
  530. memcpy(ptr, &total_size, 2);
  531. ptr += 2;
  532. }
  533. else {
  534. memcpy(ptr, &total_size, 1);
  535. ptr += 1;
  536. }
  537. }
  538. else {
  539. memcpy(ptr, &total_size, sizeof(total_size));
  540. ptr += sizeof(total_size);
  541. }
  542. memcpy(ptr, &oversized_packet, sizeof(oversized_packet));
  543. ptr += sizeof(oversized_packet);
  544. if (opcode == 0) {
  545. opcode = EQOpcodeManager[GetOpcodeVersion(version)]->EmuToEQ(OP_EqCreateGhostCmd);
  546. }
  547. memcpy(ptr, &opcode, sizeof(opcode));
  548. ptr += sizeof(opcode);
  549. memcpy(ptr, part1->c_str(), part1->length());
  550. ptr += part1->length();
  551. memcpy(ptr, tmp, part2_size);
  552. ptr += part2_size;
  553. if (part3)
  554. memcpy(ptr, part3->c_str(), part3->length());
  555. delete[] part2;
  556. // printf("%s (%i): p1: %i, p2:% i (%i), p3:% i, ts: %i\n", GetName(), GetID(), part1->length(), part2_size, origPart2Size, part3->length(), total_size);
  557. EQ2Packet* ret = new EQ2Packet(OP_ClientCmdMsg, final_packet, final_packet_size);
  558. delete[] final_packet;
  559. m_Update.releasewritelock(__FUNCTION__, __LINE__);
  560. return ret;
  561. }
  562. uchar* Spawn::spawn_info_changes(Player* player, int16 version){
  563. int16 index = player->player_spawn_index_map[this];
  564. PacketStruct* packet = player->GetSpawnInfoStruct();
  565. player->info_mutex.writelock(__FUNCTION__, __LINE__);
  566. packet->ResetData();
  567. InitializeInfoPacketData(player, packet);
  568. string* data = packet->serializeString();
  569. int32 size = data->length();
  570. uchar* xor_info_packet = player->GetTempInfoPacketForXOR();
  571. if (!xor_info_packet || size != player->GetTempInfoXorSize())
  572. {
  573. LogWrite(ZONE__DEBUG, 0, "Zone", "InstantiateInfoPacket: %i, %i", size, player->GetTempInfoXorSize());
  574. safe_delete(xor_info_packet);
  575. xor_info_packet = player->SetTempInfoPacketForXOR(size);
  576. }
  577. uchar* orig_packet = player->GetSpawnInfoPacketForXOR(id);
  578. if(orig_packet){
  579. memcpy(xor_info_packet, (uchar*)data->c_str(), size);
  580. Encode(xor_info_packet, orig_packet, size);
  581. }
  582. bool changed = false;
  583. for (int i = 0; i < size; ++i) {
  584. if (xor_info_packet[i]) {
  585. changed = true;
  586. break;
  587. }
  588. }
  589. if (!changed) {
  590. player->info_mutex.releasewritelock(__FUNCTION__, __LINE__);
  591. return nullptr;
  592. }
  593. uchar* tmp = new uchar[size + 10];
  594. size = Pack(tmp, xor_info_packet, size, size, version);
  595. player->info_mutex.releasewritelock(__FUNCTION__, __LINE__);
  596. int32 orig_size = size;
  597. size-=sizeof(int32);
  598. size+=CheckOverLoadSize(index);
  599. info_packet_size = size + CheckOverLoadSize(size);
  600. uchar* tmp2 = new uchar[info_packet_size];
  601. uchar* ptr = tmp2;
  602. ptr += DoOverLoad(size, ptr);
  603. ptr += DoOverLoad(index, ptr);
  604. memcpy(ptr, tmp+sizeof(int32), orig_size - sizeof(int32));
  605. delete[] tmp;
  606. return tmp2;
  607. }
  608. uchar* Spawn::spawn_vis_changes(Player* player, int16 version){
  609. PacketStruct* vis_struct = player->GetSpawnVisStruct();
  610. int16 index = player->player_spawn_index_map[this];
  611. player->vis_mutex.writelock(__FUNCTION__, __LINE__);
  612. uchar* orig_packet = player->GetSpawnVisPacketForXOR(id);
  613. vis_struct->ResetData();
  614. InitializeVisPacketData(player, vis_struct);
  615. string* data = vis_struct->serializeString();
  616. int32 size = data->length();
  617. uchar* xor_vis_packet = player->GetTempVisPacketForXOR();
  618. if (!xor_vis_packet || size != player->GetTempVisXorSize())
  619. {
  620. LogWrite(ZONE__DEBUG, 0, "Zone", "InstantiateVisPacket: %i, %i", size, player->GetTempVisXorSize());
  621. safe_delete(xor_vis_packet);
  622. xor_vis_packet = player->SetTempVisPacketForXOR(size);
  623. }
  624. if(orig_packet){
  625. memcpy(xor_vis_packet, (uchar*)data->c_str(), size);
  626. Encode(xor_vis_packet, orig_packet, size);
  627. }
  628. bool changed = false;
  629. for (int i = 0; i < size; ++i) {
  630. if (xor_vis_packet[i]) {
  631. changed = true;
  632. break;
  633. }
  634. }
  635. if (!changed) {
  636. player->vis_mutex.releasewritelock(__FUNCTION__, __LINE__);
  637. return nullptr;
  638. }
  639. uchar* tmp = new uchar[size + 10];
  640. size = Pack(tmp, xor_vis_packet, size, size, version);
  641. player->vis_mutex.releasewritelock(__FUNCTION__, __LINE__);
  642. int32 orig_size = size;
  643. size-=sizeof(int32);
  644. size+=CheckOverLoadSize(index);
  645. vis_packet_size = size + CheckOverLoadSize(size);
  646. uchar* tmp2 = new uchar[vis_packet_size];
  647. uchar* ptr = tmp2;
  648. ptr += DoOverLoad(size, ptr);
  649. ptr += DoOverLoad(index, ptr);
  650. memcpy(ptr, tmp+sizeof(int32), orig_size - sizeof(int32));
  651. delete[] tmp;
  652. return tmp2;
  653. }
  654. uchar* Spawn::spawn_pos_changes(Player* player, int16 version) {
  655. int16 index = player->GetIndexForSpawn(this);
  656. PacketStruct* packet = player->GetSpawnPosStruct();
  657. player->pos_mutex.writelock(__FUNCTION__, __LINE__);
  658. uchar* orig_packet = player->GetSpawnPosPacketForXOR(id);
  659. packet->ResetData();
  660. InitializePosPacketData(player, packet, true);
  661. string* data = packet->serializeString();
  662. int32 size = data->length();
  663. uchar* xor_pos_packet = player->GetTempPosPacketForXOR();
  664. if (!xor_pos_packet || size != player->GetTempPosXorSize())
  665. {
  666. LogWrite(ZONE__DEBUG, 0, "Zone", "InstantiatePosPacket: %i, %i", size, player->GetTempPosXorSize());
  667. safe_delete(xor_pos_packet);
  668. xor_pos_packet = player->SetTempPosPacketForXOR(size);
  669. }
  670. if(orig_packet){
  671. memcpy(xor_pos_packet, (uchar*)data->c_str(), size);
  672. Encode(xor_pos_packet, orig_packet, size);
  673. }
  674. bool changed = false;
  675. for (int i = 0; i < size; ++i) {
  676. if (xor_pos_packet[i]) {
  677. changed = true;
  678. break;
  679. }
  680. }
  681. if (!changed) {
  682. player->pos_mutex.releasewritelock(__FUNCTION__, __LINE__);
  683. return nullptr;
  684. }
  685. uchar* tmp;
  686. if (IsPlayer() && version > 283)
  687. tmp = new uchar[size + 14];
  688. else
  689. tmp = new uchar[size + 10];
  690. size = Pack(tmp, xor_pos_packet, size, size, version);
  691. player->pos_mutex.releasewritelock(__FUNCTION__, __LINE__);
  692. int32 orig_size = size;
  693. // Needed for CoE+ clients
  694. if (version >= 1188)
  695. size += 1;
  696. if(IsPlayer() && version > 546)
  697. size += 4;
  698. size-=sizeof(int32);
  699. size+=CheckOverLoadSize(index);
  700. pos_packet_size = size + CheckOverLoadSize(size);
  701. uchar* tmp2 = new uchar[pos_packet_size];
  702. uchar* ptr = tmp2;
  703. ptr += DoOverLoad(size, ptr);
  704. ptr += DoOverLoad(index, ptr);
  705. // extra byte in coe+ clients, 0 for NPC's 1 for Players
  706. int8 x = 0;
  707. if (IsPlayer() && version > 546) {
  708. if (version >= 1188) {
  709. // set x to 1 and add it to the packet
  710. x = 1;
  711. memcpy(ptr, &x, sizeof(int8));
  712. ptr += sizeof(int8);
  713. }
  714. int32 now = Timer::GetCurrentTime2();
  715. memcpy(ptr, &now, sizeof(int32));
  716. ptr += sizeof(int32);
  717. }
  718. else if (version >= 1188) {
  719. // add x to packet
  720. memcpy(ptr, &x, sizeof(int8));
  721. ptr += sizeof(int8);
  722. }
  723. memcpy(ptr, tmp+sizeof(int32), orig_size - sizeof(int32));
  724. delete[] tmp;
  725. return tmp2;
  726. }
  727. EQ2Packet* Spawn::player_position_update_packet(Player* player, int16 version){
  728. if(!player || player->IsPlayer() == false){
  729. LogWrite(SPAWN__ERROR, 0, "Spawn", "Error: Called player_position_update_packet without player!");
  730. return 0;
  731. }
  732. else if(IsPlayer() == false){
  733. LogWrite(SPAWN__ERROR, 0, "Spawn", "Error: Called player_position_update_packet from spawn!");
  734. return 0;
  735. }
  736. static const int8 info_size = 1;
  737. static const int8 vis_size = 1;
  738. m_Update.writelock(__FUNCTION__, __LINE__);
  739. uchar* pos_changes = spawn_pos_changes(player, version);
  740. if (pos_changes == NULL )
  741. {
  742. m_Update.releasewritelock(__FUNCTION__, __LINE__);
  743. return NULL;
  744. }
  745. int32 tmp_pos_packet_size = pos_packet_size;
  746. m_Update.releasewritelock(__FUNCTION__, __LINE__);
  747. int32 size = info_size + tmp_pos_packet_size + vis_size + 8;
  748. if (version >= 284)
  749. size += 3;
  750. else if (version <= 283 && size >= 255) {//1 byte to 3 for overloaded val
  751. size += 2;
  752. }
  753. static const int8 oversized = 255;
  754. int16 opcode_val = EQOpcodeManager[GetOpcodeVersion(version)]->EmuToEQ(OP_EqUpdateGhostCmd);
  755. uchar* tmp = new uchar[size];
  756. memset(tmp, 0, size);
  757. uchar* ptr = tmp;
  758. if (version >= 284) {
  759. size -= 4;
  760. memcpy(ptr, &size, sizeof(int32));
  761. size += 4;
  762. ptr += sizeof(int32);
  763. }
  764. else {
  765. if (size >= 255) {
  766. memcpy(ptr, &oversized, sizeof(int8));
  767. ptr += sizeof(int8);
  768. size -= 3;
  769. memcpy(ptr, &size, sizeof(int16));
  770. size += 3;
  771. ptr += 3;
  772. }
  773. else {
  774. size -= 1;
  775. memcpy(ptr, &size, sizeof(int8));
  776. ptr += sizeof(int8);
  777. size += 1;
  778. ptr += 1;
  779. }
  780. }
  781. memcpy(ptr, &oversized, sizeof(int8));
  782. ptr += sizeof(int8);
  783. memcpy(ptr, &opcode_val, sizeof(int16));
  784. ptr += sizeof(int16);
  785. ptr += sizeof(int32);
  786. ptr += info_size;
  787. memcpy(ptr, pos_changes, tmp_pos_packet_size);
  788. EQ2Packet* ret_packet = new EQ2Packet(OP_ClientCmdMsg, tmp, size);
  789. DumpPacket(ret_packet);
  790. delete[] tmp;
  791. delete[] pos_changes;
  792. return ret_packet;
  793. }
  794. EQ2Packet* Spawn::spawn_update_packet(Player* player, int16 version, bool override_changes, bool override_vis_changes){
  795. if(!player || player->IsPlayer() == false){
  796. LogWrite(SPAWN__ERROR, 0, "Spawn", "Error: Called spawn_update_packet without player!");
  797. return 0;
  798. }
  799. else if((IsPlayer() && info_changed == false && vis_changed == false) || (info_changed == false && vis_changed == false && position_changed == false)){
  800. if(!override_changes && !override_vis_changes)
  801. return 0;
  802. }
  803. static const uchar null_byte = 0;
  804. uchar* info_changes = 0;
  805. uchar* pos_changes = 0;
  806. uchar* vis_changes = 0;
  807. static const int8 oversized = 255;
  808. int16 opcode_val = EQOpcodeManager[GetOpcodeVersion(version)]->EmuToEQ(OP_EqUpdateGhostCmd);
  809. int16 tmp_info_packet_size;
  810. int16 tmp_vis_packet_size;
  811. int16 tmp_pos_packet_size;
  812. //We need to lock these variables up to make this thread safe
  813. m_Update.writelock(__FUNCTION__, __LINE__);
  814. //These variables are set in the spawn_info_changes, pos and vis changes functions
  815. info_packet_size = 1;
  816. pos_packet_size = 1;
  817. vis_packet_size = 1;
  818. if (info_changed || override_changes)
  819. info_changes = spawn_info_changes(player, version);
  820. if ((position_changed || override_changes) && IsPlayer() == false)
  821. pos_changes = spawn_pos_changes(player, version);
  822. if (vis_changed || override_changes || override_vis_changes)
  823. vis_changes = spawn_vis_changes(player, version);
  824. tmp_info_packet_size = info_packet_size;
  825. tmp_pos_packet_size = pos_packet_size;
  826. tmp_vis_packet_size = vis_packet_size;
  827. int32 size = info_packet_size + pos_packet_size + vis_packet_size + 8;
  828. if (version >= 284)
  829. size += 3;
  830. else if (version <= 283 && size >= 255) {//1 byte to 3 for overloaded val
  831. size += 2;
  832. }
  833. uchar* tmp = new uchar[size];
  834. memset(tmp, 0, size);
  835. uchar* ptr = tmp;
  836. if (version >= 284) {
  837. size -= 4;
  838. memcpy(ptr, &size, sizeof(int32));
  839. size += 4;
  840. ptr += sizeof(int32);
  841. }
  842. else {
  843. if (size >= 255) {
  844. memcpy(ptr, &oversized, sizeof(int8));
  845. ptr += sizeof(int8);
  846. size -= 3;
  847. memcpy(ptr, &size, sizeof(int16));
  848. size += 3;
  849. ptr += 3;
  850. }
  851. else {
  852. size -= 1;
  853. memcpy(ptr, &size, sizeof(int8));
  854. ptr += sizeof(int8);
  855. size += 1;
  856. }
  857. }
  858. memcpy(ptr, &oversized, sizeof(int8));
  859. ptr += sizeof(int8);
  860. memcpy(ptr, &opcode_val, sizeof(int16));
  861. ptr += sizeof(int16);
  862. if (IsPlayer() == false || version <= 546) { //this isnt sent for player updates, it is sent on position update
  863. //int32 time = Timer::GetCurrentTime2();
  864. packet_num = Timer::GetCurrentTime2();
  865. memcpy(ptr, &packet_num, sizeof(int32));
  866. }
  867. ptr += sizeof(int32);
  868. memcpy(ptr, info_changes ? info_changes : &null_byte, tmp_info_packet_size);
  869. ptr += info_packet_size;
  870. memcpy(ptr, pos_changes ? pos_changes : &null_byte, tmp_pos_packet_size);
  871. ptr += pos_packet_size;
  872. memcpy(ptr, vis_changes ? vis_changes : &null_byte, tmp_vis_packet_size);
  873. EQ2Packet* ret_packet = new EQ2Packet(OP_ClientCmdMsg, tmp, size);
  874. delete[] tmp;
  875. safe_delete_array(info_changes);
  876. safe_delete_array(vis_changes);
  877. safe_delete_array(pos_changes);
  878. m_Update.releasewritelock(__FUNCTION__, __LINE__);
  879. return ret_packet;
  880. }
  881. uchar* Spawn::spawn_info_changes_ex(Player* player, int16 version) {
  882. int16 index = player->player_spawn_index_map[this];
  883. PacketStruct* packet = player->GetSpawnInfoStruct();
  884. player->info_mutex.writelock(__FUNCTION__, __LINE__);
  885. packet->ResetData();
  886. InitializeInfoPacketData(player, packet);
  887. string* data = packet->serializeString();
  888. int32 size = data->length();
  889. uchar* xor_info_packet = player->GetTempInfoPacketForXOR();
  890. if (!xor_info_packet || size != player->GetTempInfoXorSize()) {
  891. LogWrite(ZONE__DEBUG, 0, "Zone", "InstantiateInfoExPacket: %i, %i", size, player->GetTempInfoXorSize());
  892. safe_delete(xor_info_packet);
  893. xor_info_packet = player->SetTempInfoPacketForXOR(size);
  894. }
  895. uchar* orig_packet = player->GetSpawnInfoPacketForXOR(id);
  896. if (orig_packet) {
  897. memcpy(xor_info_packet, (uchar*)data->c_str(), size);
  898. Encode(xor_info_packet, orig_packet, size);
  899. }
  900. bool changed = false;
  901. for (int i = 0; i < size; ++i) {
  902. if (xor_info_packet[i]) {
  903. changed = true;
  904. break;
  905. }
  906. }
  907. if (!changed) {
  908. player->info_mutex.releasewritelock(__FUNCTION__, __LINE__);
  909. return nullptr;
  910. }
  911. uchar* tmp = new uchar[size + 10];
  912. size = Pack(tmp, xor_info_packet, size, size, version);
  913. player->info_mutex.releasewritelock(__FUNCTION__, __LINE__);
  914. int32 orig_size = size;
  915. size -= sizeof(int32);
  916. size += CheckOverLoadSize(index);
  917. info_packet_size = size;
  918. uchar* tmp2 = new uchar[size];
  919. uchar* ptr = tmp2;
  920. ptr += DoOverLoad(index, ptr);
  921. memcpy(ptr, tmp + sizeof(int32), orig_size - sizeof(int32));
  922. delete[] tmp;
  923. return move(tmp2);
  924. }
  925. uchar* Spawn::spawn_vis_changes_ex(Player* player, int16 version) {
  926. PacketStruct* vis_struct = player->GetSpawnVisStruct();
  927. int16 index = player->player_spawn_index_map[this];
  928. player->vis_mutex.writelock(__FUNCTION__, __LINE__);
  929. uchar* orig_packet = player->GetSpawnVisPacketForXOR(id);
  930. vis_struct->ResetData();
  931. InitializeVisPacketData(player, vis_struct);
  932. string* data = vis_struct->serializeString();
  933. int32 size = data->length();
  934. uchar* xor_vis_packet = player->GetTempVisPacketForXOR();
  935. if (!xor_vis_packet || size != player->GetTempVisXorSize()) {
  936. LogWrite(ZONE__DEBUG, 0, "Zone", "InstantiateVisExPacket: %i, %i", size, player->GetTempVisXorSize());
  937. safe_delete(xor_vis_packet);
  938. xor_vis_packet = player->SetTempVisPacketForXOR(size);
  939. }
  940. if (orig_packet) {
  941. memcpy(xor_vis_packet, (uchar*)data->c_str(), size);
  942. Encode(xor_vis_packet, orig_packet, size);
  943. }
  944. bool changed = false;
  945. for (int i = 0; i < size; ++i) {
  946. if (xor_vis_packet[i]) {
  947. changed = true;
  948. break;
  949. }
  950. }
  951. if (!changed) {
  952. player->vis_mutex.releasewritelock(__FUNCTION__, __LINE__);
  953. return nullptr;
  954. }
  955. uchar* tmp = new uchar[size + 10];
  956. size = Pack(tmp, xor_vis_packet, size, size, version);
  957. player->vis_mutex.releasewritelock(__FUNCTION__, __LINE__);
  958. int32 orig_size = size;
  959. size -= sizeof(int32);
  960. size += CheckOverLoadSize(index);
  961. vis_packet_size = size;
  962. uchar* tmp2 = new uchar[size];
  963. uchar* ptr = tmp2;
  964. ptr += DoOverLoad(index, ptr);
  965. memcpy(ptr, tmp + sizeof(int32), orig_size - sizeof(int32));
  966. delete[] tmp;
  967. return move(tmp2);
  968. }
  969. uchar* Spawn::spawn_pos_changes_ex(Player* player, int16 version) {
  970. int16 index = player->GetIndexForSpawn(this);
  971. PacketStruct* packet = player->GetSpawnPosStruct();
  972. player->pos_mutex.writelock(__FUNCTION__, __LINE__);
  973. uchar* orig_packet = player->GetSpawnPosPacketForXOR(id);
  974. packet->ResetData();
  975. InitializePosPacketData(player, packet);
  976. string* data = packet->serializeString();
  977. int32 size = data->length();
  978. uchar* xor_pos_packet = player->GetTempPosPacketForXOR();
  979. if (!xor_pos_packet || size != player->GetTempPosXorSize()) {
  980. LogWrite(ZONE__DEBUG, 0, "Zone", "InstantiatePosExPacket: %i, %i", size, player->GetTempPosXorSize());
  981. safe_delete(xor_pos_packet);
  982. xor_pos_packet = player->SetTempPosPacketForXOR(size);
  983. }
  984. if (orig_packet) {
  985. memcpy(xor_pos_packet, (uchar*)data->c_str(), size);
  986. Encode(xor_pos_packet, orig_packet, size);
  987. }
  988. bool changed = false;
  989. for (int i = 0; i < size; ++i) {
  990. if (xor_pos_packet[i]) {
  991. changed = true;
  992. break;
  993. }
  994. }
  995. if (!changed) {
  996. player->pos_mutex.releasewritelock(__FUNCTION__, __LINE__);
  997. return nullptr;
  998. }
  999. uchar* tmp = new uchar[size + 10];
  1000. size = Pack(tmp, xor_pos_packet, size, size, version);
  1001. player->pos_mutex.releasewritelock(__FUNCTION__, __LINE__);
  1002. int32 orig_size = size;
  1003. if (version >= 1188) {
  1004. size += 1;
  1005. }
  1006. if (IsPlayer() && version > 546) {
  1007. size += 4;
  1008. }
  1009. size -= sizeof(int32);
  1010. size += CheckOverLoadSize(index);
  1011. pos_packet_size = size;
  1012. uchar* tmp2 = new uchar[size];
  1013. uchar* ptr = tmp2;
  1014. ptr += DoOverLoad(index, ptr);
  1015. // extra byte in coe+ clients, 0 for NPC's 1 for Players
  1016. int8 x = 0;
  1017. if (version > 546) {
  1018. if (IsPlayer()) {
  1019. if (version >= 1188) {
  1020. x = 1;
  1021. memcpy(ptr, &x, sizeof(int8));
  1022. ptr += sizeof(int8);
  1023. }
  1024. int32 now = Timer::GetCurrentTime2();
  1025. memcpy(ptr, &now, sizeof(int32));
  1026. ptr += sizeof(int32);
  1027. }
  1028. else if (version >= 1188) {
  1029. memcpy(ptr, &x, sizeof(int8));
  1030. ptr += sizeof(int8);
  1031. }
  1032. }
  1033. memcpy(ptr, tmp + sizeof(int32), orig_size - sizeof(int32));
  1034. delete[] tmp;
  1035. return move(tmp2);
  1036. }
  1037. EQ2Packet* Spawn::serialize(Player* player, int16 version){
  1038. return 0;
  1039. }
  1040. Spawn* Spawn::GetTarget(){
  1041. Spawn* ret = 0;
  1042. // only attempt to get a spawn if we had a target stored
  1043. if (target != 0)
  1044. {
  1045. ret = GetZone()->GetSpawnByID(target);
  1046. if (!ret)
  1047. target = 0;
  1048. }
  1049. return ret;
  1050. }
  1051. void Spawn::SetTarget(Spawn* spawn){
  1052. SetInfo(&target, spawn ? spawn->GetID() : 0);
  1053. }
  1054. Spawn* Spawn::GetLastAttacker() {
  1055. Spawn* ret = 0;
  1056. ret = GetZone()->GetSpawnByID(last_attacker);
  1057. if (!ret)
  1058. last_attacker = 0;
  1059. return ret;
  1060. }
  1061. void Spawn::SetLastAttacker(Spawn* spawn){
  1062. last_attacker = spawn->GetID();
  1063. }
  1064. void Spawn::SetInvulnerable(bool val){
  1065. invulnerable = val;
  1066. }
  1067. bool Spawn::GetInvulnerable(){
  1068. return invulnerable;
  1069. }
  1070. bool Spawn::TakeDamage(int32 damage){
  1071. if(invulnerable)
  1072. return false;
  1073. if (IsEntity()) {
  1074. if (((Entity*)this)->IsMezzed())
  1075. ((Entity*)this)->RemoveAllMezSpells();
  1076. if (damage == 0)
  1077. return true;
  1078. }
  1079. int32 hp = GetHP();
  1080. if(damage >= hp) {
  1081. SetHP(0);
  1082. if (IsPlayer()) {
  1083. ((Player*)this)->InCombat(false);
  1084. ((Player*)this)->SetRangeAttack(false);
  1085. GetZone()->TriggerCharSheetTimer(); // force char sheet updates now
  1086. }
  1087. }
  1088. else {
  1089. SetHP(hp - damage);
  1090. // if player flag the char sheet as changed so the ui updates properly
  1091. if (IsPlayer())
  1092. ((Player*)this)->SetCharSheetChanged(true);
  1093. }
  1094. return true;
  1095. }
  1096. void Spawn::TakeDamage(Spawn* attacker, int32 damage){
  1097. if (TakeDamage(damage))
  1098. GetZone()->CallSpawnScript(this, SPAWN_SCRIPT_HEALTHCHANGED, attacker);
  1099. SetLastAttacker(attacker);
  1100. }
  1101. ZoneServer* Spawn::GetZone(){
  1102. return zone;
  1103. }
  1104. void Spawn::SetZone(ZoneServer* in_zone){
  1105. zone = in_zone;
  1106. }
  1107. /*** HIT POINT ***/
  1108. void Spawn::SetHP(sint32 new_val, bool setUpdateFlags){
  1109. if(new_val == 0){
  1110. ClearRunningLocations();
  1111. CalculateRunningLocation(true);
  1112. }
  1113. if(new_val > basic_info.max_hp)
  1114. SetInfo(&basic_info.max_hp, new_val, setUpdateFlags);
  1115. SetInfo(&basic_info.cur_hp, new_val, setUpdateFlags);
  1116. if(/*IsPlayer() &&*/ GetZone() && basic_info.cur_hp > 0 && basic_info.cur_hp < basic_info.max_hp)
  1117. GetZone()->AddDamagedSpawn(this);
  1118. if (IsEntity() && ((Entity*)this)->GetGroupMemberInfo()) {
  1119. ((Entity*)this)->UpdateGroupMemberInfo();
  1120. if (IsPlayer())
  1121. world.GetGroupManager()->SendGroupUpdate(((Entity*)this)->GetGroupMemberInfo()->group_id, GetZone()->GetClientBySpawn(this));
  1122. else
  1123. world.GetGroupManager()->SendGroupUpdate(((Entity*)this)->GetGroupMemberInfo()->group_id);
  1124. }
  1125. if (IsNPC() && ((NPC*)this)->IsPet() && ((NPC*)this)->GetOwner()->IsPlayer()) {
  1126. Player* player = (Player*)((NPC*)this)->GetOwner();
  1127. if (player->GetPet() && player->GetCharmedPet()) {
  1128. if (this == player->GetPet()) {
  1129. player->GetInfoStruct()->pet_health_pct = (float)basic_info.cur_hp / (float)basic_info.max_hp;
  1130. player->SetCharSheetChanged(true);
  1131. }
  1132. }
  1133. else {
  1134. player->GetInfoStruct()->pet_health_pct = (float)basic_info.cur_hp / (float)basic_info.max_hp;
  1135. player->SetCharSheetChanged(true);
  1136. }
  1137. }
  1138. }
  1139. void Spawn::SetTotalHP(sint32 new_val){
  1140. if(basic_info.hp_base == 0)
  1141. SetTotalHPBase(new_val);
  1142. SetInfo(&basic_info.max_hp, new_val);
  1143. if(GetZone() && basic_info.cur_hp > 0 && basic_info.cur_hp < basic_info.max_hp)
  1144. GetZone()->AddDamagedSpawn(this);
  1145. if (IsEntity() && ((Entity*)this)->GetGroupMemberInfo()) {
  1146. ((Entity*)this)->UpdateGroupMemberInfo();
  1147. if (IsPlayer())
  1148. world.GetGroupManager()->SendGroupUpdate(((Entity*)this)->GetGroupMemberInfo()->group_id, GetZone()->GetClientBySpawn(this));
  1149. else
  1150. world.GetGroupManager()->SendGroupUpdate(((Entity*)this)->GetGroupMemberInfo()->group_id);
  1151. }
  1152. if (IsNPC() && ((NPC*)this)->IsPet() && ((NPC*)this)->GetOwner()->IsPlayer()) {
  1153. Player* player = (Player*)((NPC*)this)->GetOwner();
  1154. if (player->GetPet() && player->GetCharmedPet()) {
  1155. if (this == player->GetPet()) {
  1156. player->GetInfoStruct()->pet_health_pct = (float)basic_info.cur_hp / (float)basic_info.max_hp;
  1157. player->SetCharSheetChanged(true);
  1158. }
  1159. }
  1160. else {
  1161. player->GetInfoStruct()->pet_health_pct = (float)basic_info.cur_hp / (float)basic_info.max_hp;
  1162. player->SetCharSheetChanged(true);
  1163. }
  1164. }
  1165. }
  1166. void Spawn::SetTotalHPBase(sint32 new_val)
  1167. {
  1168. SetInfo(&basic_info.hp_base, new_val);
  1169. if(GetZone() && basic_info.cur_hp > 0 && basic_info.cur_hp < basic_info.max_hp)
  1170. GetZone()->AddDamagedSpawn(this);
  1171. if (IsEntity() && ((Entity*)this)->GetGroupMemberInfo()) {
  1172. ((Entity*)this)->UpdateGroupMemberInfo();
  1173. if (IsPlayer())
  1174. world.GetGroupManager()->SendGroupUpdate(((Entity*)this)->GetGroupMemberInfo()->group_id, GetZone()->GetClientBySpawn(this));
  1175. else
  1176. world.GetGroupManager()->SendGroupUpdate(((Entity*)this)->GetGroupMemberInfo()->group_id);
  1177. }
  1178. }
  1179. sint32 Spawn::GetHP()
  1180. {
  1181. return basic_info.cur_hp;
  1182. }
  1183. sint32 Spawn::GetTotalHP()
  1184. {
  1185. return basic_info.max_hp;
  1186. }
  1187. sint32 Spawn::GetTotalHPBase()
  1188. {
  1189. return basic_info.hp_base;
  1190. }
  1191. /*** POWER ***/
  1192. void Spawn::SetPower(sint32 power, bool setUpdateFlags){
  1193. if(power > basic_info.max_power)
  1194. SetInfo(&basic_info.max_power, power, setUpdateFlags);
  1195. SetInfo(&basic_info.cur_power, power, setUpdateFlags);
  1196. if(/*IsPlayer() &&*/ GetZone() && basic_info.cur_power < basic_info.max_power)
  1197. GetZone()->AddDamagedSpawn(this);
  1198. if (IsEntity() && ((Entity*)this)->GetGroupMemberInfo()) {
  1199. ((Entity*)this)->UpdateGroupMemberInfo();
  1200. if (IsPlayer())
  1201. world.GetGroupManager()->SendGroupUpdate(((Entity*)this)->GetGroupMemberInfo()->group_id, GetZone()->GetClientBySpawn(this));
  1202. else
  1203. world.GetGroupManager()->SendGroupUpdate(((Entity*)this)->GetGroupMemberInfo()->group_id);
  1204. }
  1205. if (IsNPC() && ((NPC*)this)->IsPet() && ((NPC*)this)->GetOwner()->IsPlayer()) {
  1206. Player* player = (Player*)((NPC*)this)->GetOwner();
  1207. if (player->GetPet() && player->GetCharmedPet()) {
  1208. if (this == player->GetPet()) {
  1209. player->GetInfoStruct()->pet_power_pct = (float)basic_info.cur_power / (float)basic_info.max_power;
  1210. player->SetCharSheetChanged(true);
  1211. }
  1212. }
  1213. else {
  1214. player->GetInfoStruct()->pet_power_pct = (float)basic_info.cur_power / (float)basic_info.max_power;
  1215. player->SetCharSheetChanged(true);
  1216. }
  1217. }
  1218. }
  1219. void Spawn::SetTotalPower(sint32 new_val)
  1220. {
  1221. if(basic_info.power_base == 0)
  1222. SetTotalPowerBase(new_val);
  1223. SetInfo(&basic_info.max_power, new_val);
  1224. if(GetZone() && basic_info.cur_power < basic_info.max_power)
  1225. GetZone()->AddDamagedSpawn(this);
  1226. if (IsEntity() && ((Entity*)this)->GetGroupMemberInfo()) {
  1227. ((Entity*)this)->UpdateGroupMemberInfo();
  1228. if (IsPlayer())
  1229. world.GetGroupManager()->SendGroupUpdate(((Entity*)this)->GetGroupMemberInfo()->group_id, GetZone()->GetClientBySpawn(this));
  1230. else
  1231. world.GetGroupManager()->SendGroupUpdate(((Entity*)this)->GetGroupMemberInfo()->group_id);
  1232. }
  1233. if (IsNPC() && ((NPC*)this)->IsPet() && ((NPC*)this)->GetOwner()->IsPlayer()) {
  1234. Player* player = (Player*)((NPC*)this)->GetOwner();
  1235. if (player->GetPet() && player->GetCharmedPet()) {
  1236. if (this == player->GetPet()) {
  1237. player->GetInfoStruct()->pet_power_pct = (float)basic_info.cur_power / (float)basic_info.max_power;
  1238. player->SetCharSheetChanged(true);
  1239. }
  1240. }
  1241. else {
  1242. player->GetInfoStruct()->pet_power_pct = (float)basic_info.cur_power / (float)basic_info.max_power;
  1243. player->SetCharSheetChanged(true);
  1244. }
  1245. }
  1246. }
  1247. void Spawn::SetTotalPowerBase(sint32 new_val)
  1248. {
  1249. SetInfo(&basic_info.power_base, new_val);
  1250. if(GetZone() && basic_info.cur_power < basic_info.max_power)
  1251. GetZone()->AddDamagedSpawn(this);
  1252. if (IsEntity() && ((Entity*)this)->GetGroupMemberInfo()) {
  1253. ((Entity*)this)->UpdateGroupMemberInfo();
  1254. if (IsPlayer())
  1255. world.GetGroupManager()->SendGroupUpdate(((Entity*)this)->GetGroupMemberInfo()->group_id, GetZone()->GetClientBySpawn(this));
  1256. else
  1257. world.GetGroupManager()->SendGroupUpdate(((Entity*)this)->GetGroupMemberInfo()->group_id);
  1258. }
  1259. }
  1260. sint32 Spawn::GetPower()
  1261. {
  1262. return basic_info.cur_power;
  1263. }
  1264. sint32 Spawn::GetTotalPower(){
  1265. return basic_info.max_power;
  1266. }
  1267. sint32 Spawn::GetTotalPowerBase()
  1268. {
  1269. return basic_info.power_base;
  1270. }
  1271. /*** SAVAGERY ***/
  1272. void Spawn::SetSavagery(sint32 savagery, bool setUpdateFlags)
  1273. {
  1274. /* JA: extremely limited functionality until we better understand Savagery */
  1275. if(savagery > basic_info.max_savagery)
  1276. SetInfo(&basic_info.max_savagery, savagery, setUpdateFlags);
  1277. SetInfo(&basic_info.cur_savagery, savagery, setUpdateFlags);
  1278. }
  1279. void Spawn::SetTotalSavagery(sint32 new_val)
  1280. {
  1281. /* JA: extremely limited functionality until we better understand Savagery */
  1282. if(basic_info.savagery_base == 0)
  1283. SetTotalSavageryBase(new_val);
  1284. SetInfo(&basic_info.max_savagery, new_val);
  1285. }
  1286. void Spawn::SetTotalSavageryBase(sint32 new_val){
  1287. SetInfo(&basic_info.savagery_base, new_val);
  1288. if (IsEntity() && ((Entity*)this)->GetGroupMemberInfo()) {
  1289. ((Entity*)this)->UpdateGroupMemberInfo();
  1290. if (IsPlayer())
  1291. world.GetGroupManager()->SendGroupUpdate(((Entity*)this)->GetGroupMemberInfo()->group_id, GetZone()->GetClientBySpawn(this));
  1292. else
  1293. world.GetGroupManager()->SendGroupUpdate(((Entity*)this)->GetGroupMemberInfo()->group_id);
  1294. }
  1295. }
  1296. sint32 Spawn::GetTotalSavagery()
  1297. {
  1298. return basic_info.max_savagery;
  1299. }
  1300. sint32 Spawn::GetSavagery()
  1301. {
  1302. return basic_info.cur_savagery;
  1303. }
  1304. /*** DISSONANCE ***/
  1305. void Spawn::SetDissonance(sint32 dissonance, bool setUpdateFlags)
  1306. {
  1307. /* JA: extremely limited functionality until we better understand Dissonance */
  1308. if(dissonance > basic_info.max_dissonance)
  1309. SetInfo(&basic_info.max_dissonance, dissonance, setUpdateFlags);
  1310. SetInfo(&basic_info.cur_dissonance, dissonance, setUpdateFlags);
  1311. }
  1312. void Spawn::SetTotalDissonance(sint32 new_val)
  1313. {
  1314. /* JA: extremely limited functionality until we better understand Dissonance */
  1315. if(basic_info.dissonance_base == 0)
  1316. SetTotalDissonanceBase(new_val);
  1317. SetInfo(&basic_info.max_dissonance, new_val);
  1318. }
  1319. void Spawn::SetTotalDissonanceBase(sint32 new_val)
  1320. {
  1321. SetInfo(&basic_info.dissonance_base, new_val);
  1322. if (IsEntity() && ((Entity*)this)->GetGroupMemberInfo()) {
  1323. ((Entity*)this)->UpdateGroupMemberInfo();
  1324. if (IsPlayer())
  1325. world.GetGroupManager()->SendGroupUpdate(((Entity*)this)->GetGroupMemberInfo()->group_id, GetZone()->GetClientBySpawn(this));
  1326. else
  1327. world.GetGroupManager()->SendGroupUpdate(((Entity*)this)->GetGroupMemberInfo()->group_id);
  1328. }
  1329. }
  1330. sint32 Spawn::GetTotalDissonance()
  1331. {
  1332. return basic_info.max_dissonance;
  1333. }
  1334. sint32 Spawn::GetDissonance()
  1335. {
  1336. return basic_info.cur_dissonance;
  1337. }
  1338. /* --< Alternate Advancement Points >-- */
  1339. void Spawn::SetAssignedAA(sint16 new_val)
  1340. {
  1341. SetInfo(&basic_info.assigned_aa, new_val);
  1342. }
  1343. void Spawn::SetUnassignedAA(sint16 new_val)
  1344. {
  1345. SetInfo(&basic_info.unassigned_aa, new_val);
  1346. }
  1347. void Spawn::SetTradeskillAA(sint16 new_val)
  1348. {
  1349. SetInfo(&basic_info.tradeskill_aa, new_val);
  1350. }
  1351. void Spawn::SetUnassignedTradeskillAA(sint16 new_val)
  1352. {
  1353. SetInfo(&basic_info.unassigned_tradeskill_aa, new_val);
  1354. }
  1355. void Spawn::SetPrestigeAA(sint16 new_val)
  1356. {
  1357. SetInfo(&basic_info.prestige_aa, new_val);
  1358. }
  1359. void Spawn::SetUnassignedPrestigeAA(sint16 new_val)
  1360. {
  1361. SetInfo(&basic_info.unassigned_prestige_aa, new_val);
  1362. }
  1363. void Spawn::SetTradeskillPrestigeAA(sint16 new_val)
  1364. {
  1365. SetInfo(&basic_info.tradeskill_prestige_aa, new_val);
  1366. }
  1367. void Spawn::SetUnassignedTradeskillPrestigeAA(sint16 new_val)
  1368. {
  1369. SetInfo(&basic_info.unassigned_tradeskill_prestige_aa, new_val);
  1370. }
  1371. sint16 Spawn::GetAssignedAA()
  1372. {
  1373. return basic_info.assigned_aa;
  1374. }
  1375. sint16 Spawn::GetUnassignedAA()
  1376. {
  1377. return basic_info.unassigned_aa;
  1378. }
  1379. sint16 Spawn::GetTradeskillAA()
  1380. {
  1381. return basic_info.tradeskill_aa;
  1382. }
  1383. sint16 Spawn::GetUnassignedTradeskillAA()
  1384. {
  1385. return basic_info.unassigned_tradeskill_aa;
  1386. }
  1387. sint16 Spawn::GetPrestigeAA()
  1388. {
  1389. return basic_info.prestige_aa;
  1390. }
  1391. sint16 Spawn::GetUnassignedPretigeAA()
  1392. {
  1393. return basic_info.unassigned_prestige_aa;
  1394. }
  1395. sint16 Spawn::GetTradeskillPrestigeAA()
  1396. {
  1397. return basic_info.tradeskill_prestige_aa;
  1398. }
  1399. sint16 Spawn::GetUnassignedTradeskillPrestigeAA()
  1400. {
  1401. return basic_info.unassigned_tradeskill_prestige_aa;
  1402. }
  1403. float Spawn::GetDistance(float x1, float y1, float z1, float x2, float y2, float z2){
  1404. x1 = x1 - x2;
  1405. y1 = y1 - y2;
  1406. z1 = z1 - z2;
  1407. return sqrt(x1*x1 + y1*y1 + z1*z1);
  1408. }
  1409. float Spawn::GetDistance(float x, float y, float z, float radius, bool ignore_y) {
  1410. if (ignore_y)
  1411. return GetDistance(x, y, z, GetX(), y, GetZ()) - radius;
  1412. else
  1413. return GetDistance(x, y, z, GetX(), GetY(), GetZ()) - radius;
  1414. }
  1415. float Spawn::GetDistance(float x, float y, float z, bool ignore_y) {
  1416. return GetDistance(x, y, z, 0.0f, ignore_y);
  1417. }
  1418. float Spawn::GetDistance(Spawn* spawn, bool ignore_y, bool includeRadius){
  1419. float ret = 0;
  1420. if (spawn)
  1421. {
  1422. float radius = 0.0f;
  1423. if (includeRadius)
  1424. radius = CalculateRadius(spawn);
  1425. ret = GetDistance(spawn->GetX(), spawn->GetY(), spawn->GetZ(), radius, ignore_y);
  1426. }
  1427. // maybe distance against ourselves, in that case we want to nullify the radius check
  1428. if (ret < 0)
  1429. ret = 0.0f;
  1430. return ret;
  1431. }
  1432. float Spawn::GetDistance(Spawn* spawn, float x1, float y1, float z1, bool includeRadius) {
  1433. float ret = 0;
  1434. if (spawn)
  1435. {
  1436. float radius = 0.0f;
  1437. if (includeRadius)
  1438. radius = CalculateRadius(spawn);
  1439. ret = GetDistance(x1, y1, z1, spawn->GetX(), spawn->GetY(), spawn->GetZ()) - radius;
  1440. }
  1441. // maybe distance against ourselves, in that case we want to nullify the radius check
  1442. if (ret < 0)
  1443. ret = 0.0f;
  1444. return ret;
  1445. }
  1446. float Spawn::CalculateRadius(Spawn* target)
  1447. {
  1448. float srcRadius = short_to_float(appearance.pos.collision_radius);
  1449. if (target)
  1450. {
  1451. float targRadius = short_to_float(target->appearance.pos.collision_radius);
  1452. return (targRadius / 32.0f) + (srcRadius / 32.0f);
  1453. }
  1454. else
  1455. return (srcRadius / 32.0f);
  1456. }
  1457. int32 Spawn::GetRespawnTime(){
  1458. return respawn;
  1459. }
  1460. void Spawn::SetRespawnTime(int32 time){
  1461. respawn = time;
  1462. }
  1463. int32 Spawn::GetExpireOffsetTime(){
  1464. return expire_offset;
  1465. }
  1466. void Spawn::SetExpireOffsetTime(int32 time){
  1467. expire_offset = time;
  1468. }
  1469. int32 Spawn::GetSpawnLocationID(){
  1470. return spawn_location_id;
  1471. }
  1472. void Spawn::SetSpawnLocationID(int32 id){
  1473. spawn_location_id = id;
  1474. }
  1475. int32 Spawn::GetSpawnEntryID(){
  1476. return spawn_entry_id;
  1477. }
  1478. void Spawn::SetSpawnEntryID(int32 id){
  1479. spawn_entry_id = id;
  1480. }
  1481. int32 Spawn::GetSpawnLocationPlacementID(){
  1482. return spawn_location_spawns_id;
  1483. }
  1484. void Spawn::SetSpawnLocationPlacementID(int32 id){
  1485. spawn_location_spawns_id = id;
  1486. }
  1487. const char* Spawn::GetSpawnScript(){
  1488. if(spawn_script.length() > 0)
  1489. return spawn_script.c_str();
  1490. else
  1491. return 0;
  1492. }
  1493. void Spawn::SetSpawnScript(string name){
  1494. spawn_script = name;
  1495. }
  1496. void Spawn::SetPrimaryCommand(const char* name, const char* command, float distance){
  1497. EntityCommand* entity_command = CreateEntityCommand(name, distance, command, "", 0, 0);
  1498. if(primary_command_list.size() > 0 && primary_command_list[0]){
  1499. safe_delete(primary_command_list[0]);
  1500. primary_command_list[0] = entity_command;
  1501. }
  1502. else
  1503. primary_command_list.push_back(entity_command);
  1504. }
  1505. void Spawn::SetSecondaryCommands(vector<EntityCommand*>* commands){
  1506. if(commands && commands->size() > 0){
  1507. vector<EntityCommand*>::iterator itr;
  1508. if(secondary_command_list.size() > 0){
  1509. for(itr = secondary_command_list.begin(); itr != secondary_command_list.end(); itr++){
  1510. safe_delete(*itr);
  1511. }
  1512. secondary_command_list.clear();
  1513. }
  1514. EntityCommand* command = 0;
  1515. for(itr = commands->begin(); itr != commands->end(); itr++){
  1516. command = CreateEntityCommand(*itr);
  1517. secondary_command_list.push_back(command);
  1518. }
  1519. }
  1520. }
  1521. void Spawn::SetPrimaryCommands(vector<EntityCommand*>* commands){
  1522. if(commands && commands->size() > 0){
  1523. vector<EntityCommand*>::iterator itr;
  1524. if(primary_command_list.size() > 0){
  1525. for(itr = primary_command_list.begin(); itr != primary_command_list.end(); itr++){
  1526. safe_delete(*itr);
  1527. }
  1528. primary_command_list.clear();
  1529. }
  1530. EntityCommand* command = 0;
  1531. for(itr = commands->begin(); itr != commands->end(); itr++){
  1532. command = CreateEntityCommand(*itr);
  1533. primary_command_list.push_back(command);
  1534. }
  1535. }
  1536. }
  1537. EntityCommand* Spawn::FindEntityCommand(string command, bool primaryOnly) {
  1538. EntityCommand* entity_command = 0;
  1539. if (primary_command_list.size() > 0) {
  1540. vector<EntityCommand*>::iterator itr;
  1541. for (itr = primary_command_list.begin(); itr != primary_command_list.end(); itr++) {
  1542. if ((*itr)->command.compare(command) == 0) {
  1543. entity_command = *itr;
  1544. break;
  1545. }
  1546. }
  1547. }
  1548. if (primaryOnly)
  1549. return entity_command;
  1550. if (!entity_command && secondary_command_list.size() > 0) {
  1551. vector<EntityCommand*>::iterator itr;
  1552. for (itr = secondary_command_list.begin(); itr != secondary_command_list.end(); itr++) {
  1553. if ((*itr)->command == command) {
  1554. entity_command = *itr;
  1555. break;
  1556. }
  1557. }
  1558. }
  1559. return entity_command;
  1560. }
  1561. void Spawn::SetSizeOffset(int8 offset){
  1562. size_offset = offset;
  1563. }
  1564. int8 Spawn::GetSizeOffset(){
  1565. return size_offset;
  1566. }
  1567. void Spawn::SetMerchantID(int32 val){
  1568. merchant_id = val;
  1569. }
  1570. int32 Spawn::GetMerchantID(){
  1571. return merchant_id;
  1572. }
  1573. void Spawn::SetMerchantType(int8 val) {
  1574. merchant_type = val;
  1575. }
  1576. int8 Spawn::GetMerchantType() {
  1577. return merchant_type;
  1578. }
  1579. void Spawn::SetMerchantLevelRange(int32 minLvl, int32 maxLvl) {
  1580. merchant_min_level = minLvl;
  1581. merchant_max_level = maxLvl;
  1582. }
  1583. int32 Spawn::GetMerchantMinLevel() {
  1584. return merchant_min_level;
  1585. }
  1586. int32 Spawn::GetMerchantMaxLevel() {
  1587. return merchant_max_level;
  1588. }
  1589. bool Spawn::IsClientInMerchantLevelRange(Client* client, bool sendMessageIfDenied)
  1590. {
  1591. if (!client)
  1592. return false;
  1593. if (GetMerchantMinLevel() && client->GetPlayer()->GetLevel() < GetMerchantMinLevel())
  1594. {
  1595. client->Message(CHANNEL_COLOR_RED, "You are unable to interact with this merchant due to a minimum level %u allowed.", GetMerchantMinLevel());
  1596. return false;
  1597. }
  1598. else if (GetMerchantMaxLevel() && client->GetPlayer()->GetLevel() > GetMerchantMaxLevel())
  1599. {
  1600. client->Message(CHANNEL_COLOR_RED, "You are unable to interact with this merchant due to a maximum level %u allowed.", GetMerchantMaxLevel());
  1601. return false;
  1602. }
  1603. return true;
  1604. }
  1605. void Spawn::SetQuestsRequired(map<int32, vector<int16>* >* quests){
  1606. if(quests){
  1607. map<int32, vector<int16>* >::iterator itr;
  1608. for(itr = quests->begin(); itr != quests->end(); itr++){
  1609. vector<int16>* quest_steps = itr->second;
  1610. for (int32 i = 0; i < quest_steps->size(); i++)
  1611. SetQuestsRequired(itr->first, quest_steps->at(i));
  1612. }
  1613. }
  1614. }
  1615. void Spawn::SetQuestsRequired(int32 quest_id, int16 quest_step){
  1616. m_requiredQuests.writelock(__FUNCTION__, __LINE__);
  1617. if (required_quests.count(quest_id) == 0)
  1618. required_quests[quest_id] = new vector<int16>;
  1619. else{
  1620. for (int32 i = 0; i < required_quests[quest_id]->size(); i++){
  1621. if (required_quests[quest_id]->at(i) == quest_step){
  1622. m_requiredQuests.releasewritelock(__FUNCTION__, __LINE__);
  1623. return;
  1624. }
  1625. }
  1626. }
  1627. required_quests[quest_id]->push_back(quest_step);
  1628. m_requiredQuests.releasewritelock(__FUNCTION__, __LINE__);
  1629. }
  1630. void Spawn::SetRequiredHistory(int32 event_id, int32 value1, int32 value2){
  1631. LUAHistory set_value;
  1632. set_value.Value = value1;
  1633. set_value.Value2 = value2;
  1634. set_value.SaveNeeded = false;
  1635. m_requiredHistory.writelock(__FUNCTION__, __LINE__);
  1636. required_history[event_id] = set_value;
  1637. m_requiredHistory.releasewritelock(__FUNCTION__, __LINE__);
  1638. }
  1639. map<int32, vector<int16>* >* Spawn::GetQuestsRequired(){
  1640. return &required_quests;
  1641. }
  1642. void Spawn::SetTransporterID(int32 id){
  1643. transporter_id = id;
  1644. }
  1645. int32 Spawn::GetTransporterID(){
  1646. return transporter_id;
  1647. }
  1648. void Spawn::InitializePosPacketData(Player* player, PacketStruct* packet, bool bSpawnUpdate) {
  1649. int16 version = packet->GetVersion();
  1650. packet->setDataByName("pos_grid_id", appearance.pos.grid_id);
  1651. bool include_heading = true;
  1652. if (IsWidget() && ((Widget*)this)->GetIncludeHeading() == false)
  1653. include_heading = false;
  1654. else if (IsSign() && ((Sign*)this)->GetIncludeHeading() == false)
  1655. include_heading = false;
  1656. else if (IsGroundSpawn())
  1657. include_heading = false;
  1658. if (include_heading){
  1659. packet->setDataByName("pos_heading1", appearance.pos.Dir1);
  1660. packet->setDataByName("pos_heading2", appearance.pos.Dir2);
  1661. }
  1662. if (version <= 910) {
  1663. packet->setDataByName("pos_collision_radius", appearance.pos.collision_radius > 0 ? appearance.pos.collision_radius : 32);
  1664. packet->setDataByName("pos_size", size > 0 ? size : 32);
  1665. packet->setDataByName("pos_size_multiplier", 32); //32 is normal
  1666. }
  1667. else {
  1668. if (size == 0)
  1669. size = 32;
  1670. packet->setDataByName("pos_collision_radius", appearance.pos.collision_radius > 0 ? appearance.pos.collision_radius : 32);
  1671. packet->setDataByName("pos_size", 1.0f);
  1672. if (!IsPlayer())
  1673. packet->setDataByName("pos_size_ratio", size > 0 ? (((float)size) / 32) : 1);
  1674. else
  1675. packet->setDataByName("pos_size_ratio", 1.0f);
  1676. }
  1677. packet->setDataByName("pos_state", appearance.pos.state);
  1678. bool include_location = true;
  1679. if (IsWidget() && ((Widget*)this)->GetIncludeLocation() == false)
  1680. include_location = false;
  1681. else if (IsSign() && ((Sign*)this)->GetIncludeLocation() == false)
  1682. include_location = false;
  1683. if (include_location){
  1684. if (IsWidget() && ((Widget*)this)->GetMultiFloorLift()) {
  1685. Widget* widget = (Widget*)this;
  1686. float x = appearance.pos.X - widget->GetWidgetX();
  1687. float y = appearance.pos.Y - widget->GetWidgetY();
  1688. float z = appearance.pos.Z - widget->GetWidgetZ();
  1689. packet->setDataByName("pos_x", x);
  1690. packet->setDataByName("pos_y", y);
  1691. packet->setDataByName("pos_z", z);
  1692. }
  1693. else {
  1694. packet->setDataByName("pos_x", appearance.pos.X);
  1695. packet->setDataByName("pos_y", appearance.pos.Y);
  1696. packet->setDataByName("pos_z", appearance.pos.Z);
  1697. }
  1698. if (IsSign())
  1699. packet->setDataByName("pos_unknown6", 3, 2);
  1700. }
  1701. if (IsPlayer()) {
  1702. packet->setDataByName("pos_swim_speed_modifier", 20);
  1703. packet->setDataByName("pos_x_velocity", TransformFromFloat(GetSpeedX(), 5));
  1704. packet->setDataByName("pos_y_velocity", TransformFromFloat(GetSpeedY(), 5));
  1705. packet->setDataByName("pos_z_velocity", TransformFromFloat(GetSpeedZ(), 5));
  1706. }
  1707. if (appearance.pos.X2 == 0 && appearance.pos.Y2 == 0 && appearance.pos.Z2 && (appearance.pos.X != 0 || appearance.pos.Y != 0 || appearance.pos.Z != 0)) {
  1708. appearance.pos.X2 = appearance.pos.X;
  1709. appearance.pos.Y2 = appearance.pos.Y;
  1710. appearance.pos.Z2 = appearance.pos.Z;
  1711. }
  1712. if (appearance.pos.X3 == 0 && appearance.pos.Y3 == 0 && appearance.pos.Z3 && (appearance.pos.X != 0 || appearance.pos.Y != 0 || appearance.pos.Z != 0)) {
  1713. appearance.pos.X3 = appearance.pos.X;
  1714. appearance.pos.Y3 = appearance.pos.Y;
  1715. appearance.pos.Z3 = appearance.pos.Z;
  1716. }
  1717. //Transform To/From Float bits (original client)
  1718. //pos_loc_offset[3]: 5
  1719. //pos_x_velocity: 5
  1720. //pos_y_velocity: 5
  1721. //pos_z_velocity: 5
  1722. //pos_heading1: 6
  1723. //pos_heading2: 6
  1724. //pos_speed: 8
  1725. //pos_dest_loc_offset[3]: 5
  1726. //pos_dest_loc_offset2[3]: 5
  1727. //pos_heading_speed: 5
  1728. //pos_move_type: 5 (speed_modifier)
  1729. //pos_swim_speed_modifier: 5
  1730. //pos_side_speed: 8
  1731. //pos_vert_speed: 8
  1732. //pos_requested_pitch: 6
  1733. //pos_requested_pitch_speed: 5
  1734. //pos_pitch: 6
  1735. //pos_collision_radius: 5
  1736. //pos_size: 5
  1737. //actor_stop_range: 5
  1738. //this is for original box client, destinations used to be offsets
  1739. if (appearance.pos.X2 != 0 || appearance.pos.Y2 != 0 || appearance.pos.Z2 != 0) {
  1740. packet->setDataByName("pos_dest_loc_offset", TransformFromFloat(appearance.pos.X2 - appearance.pos.X, 5));
  1741. packet->setDataByName("pos_dest_loc_offset", TransformFromFloat(appearance.pos.Y2 - appearance.pos.Y, 5), 1);
  1742. packet->setDataByName("pos_dest_loc_offset", TransformFromFloat(appearance.pos.Z2 - appearance.pos.Z, 5), 2);
  1743. }
  1744. if (appearance.pos.X3 != 0 || appearance.pos.Y3 != 0 || appearance.pos.Z3 != 0) {
  1745. packet->setDataByName("pos_dest_loc_offset2", TransformFromFloat(appearance.pos.X3 - appearance.pos.X, 5));
  1746. packet->setDataByName("pos_dest_loc_offset2", TransformFromFloat(appearance.pos.Y3 - appearance.pos.Y, 5), 1);
  1747. packet->setDataByName("pos_dest_loc_offset2", TransformFromFloat(appearance.pos.Z3 - appearance.pos.Z, 5), 2);
  1748. }
  1749. bool bSendSpeed = true;
  1750. if (IsWidget() && ((Widget*)this)->GetMultiFloorLift()) {
  1751. Widget* widget = (Widget*)this;
  1752. float x;
  1753. float y;
  1754. float z;
  1755. if (IsRunning()){
  1756. x = appearance.pos.X2 - widget->GetWidgetX();
  1757. y = appearance.pos.Y2 - widget->GetWidgetY();
  1758. z = appearance.pos.Z2- widget->GetWidgetZ();
  1759. }
  1760. else {
  1761. x = appearance.pos.X - widget->GetWidgetX();
  1762. y = appearance.pos.Y - widget->GetWidgetY();
  1763. z = appearance.pos.Z - widget->GetWidgetZ();
  1764. }
  1765. packet->setDataByName("pos_next_x", x);
  1766. packet->setDataByName("pos_next_y", y);
  1767. packet->setDataByName("pos_next_z", z);
  1768. packet->setDataByName("pos_x3", x);
  1769. packet->setDataByName("pos_y3", y);
  1770. packet->setDataByName("pos_z3", z);
  1771. }
  1772. //If this is a spawn update or this spawn is currently moving we can send these values, otherwise set speed and next_xyz to 0
  1773. //This fixes the bug where spawns with movement scripts face south when initially spawning if they are at their target location.
  1774. else if (bSpawnUpdate || memcmp(&appearance.pos.X, &appearance.pos.X2, sizeof(float) * 3) != 0) {
  1775. packet->setDataByName("pos_next_x", appearance.pos.X2);
  1776. packet->setDataByName("pos_next_y", appearance.pos.Y2);
  1777. packet->setDataByName("pos_next_z", appearance.pos.Z2);
  1778. packet->setDataByName("pos_x3", appearance.pos.X3);
  1779. packet->setDataByName("pos_y3", appearance.pos.Y3);
  1780. packet->setDataByName("pos_z3", appearance.pos.Z3);
  1781. }
  1782. else
  1783. {
  1784. bSendSpeed = false;
  1785. }
  1786. //packet->setDataByName("pos_unknown2", 4, 2);
  1787. int16 speed_multiplier = rule_manager.GetGlobalRule(R_Spawn, SpeedMultiplier)->GetInt16(); // was 1280, 600 and now 300... investigating why
  1788. if (IsPlayer()) {
  1789. Player* player = static_cast<Player*>(this);
  1790. packet->setDataByName("pos_speed", player->GetPosPacketSpeed() * speed_multiplier);
  1791. packet->setDataByName("pos_side_speed", player->GetSideSpeed() * speed_multiplier);
  1792. }
  1793. else if (bSendSpeed) {
  1794. packet->setDataByName("pos_speed", GetSpeed() * speed_multiplier);
  1795. }
  1796. if (IsNPC() || IsPlayer()) {
  1797. packet->setDataByName("pos_move_type", 25);
  1798. }
  1799. else if (IsWidget() || IsSign()) {
  1800. packet->setDataByName("pos_move_type", 11);
  1801. }
  1802. else if(IsGroundSpawn()) {
  1803. packet->setDataByName("pos_move_type", 16);
  1804. }
  1805. if (!IsPlayer())
  1806. packet->setDataByName("pos_movement_mode", 2);
  1807. if(version <= 910)
  1808. packet->setDataByName("pos_unknown10", 0xFFFF, 1);
  1809. else if (version >= 1119)
  1810. packet->setDataByName("face_actor_id", 0xFFFFFFFF);
  1811. else
  1812. packet->setDataByName("pos_unknown10", 0xFFFF);
  1813. if(version <= 910)
  1814. packet->setDataByName("pos_unknown10", 0xFFFF, 2);
  1815. else if (version >= 1119)
  1816. packet->setDataByName("face_actor_id", 0xFFFFFFFF);
  1817. else
  1818. packet->setDataByName("pos_unknown10", 0XFFFF, 1);
  1819. packet->setDataByName("pos_pitch1", appearance.pos.Pitch1);
  1820. packet->setDataByName("pos_pitch2", appearance.pos.Pitch2);
  1821. packet->setDataByName("pos_roll", appearance.pos.Roll);
  1822. }
  1823. void Spawn::InitializeInfoPacketData(Player* spawn, PacketStruct* packet) {
  1824. int16 version = packet->GetVersion();
  1825. bool spawnHiddenFromClient = false;
  1826. // radius of 0 is always seen, -1 is never seen (unless items/spells override), larger than 0 is a defined radius to restrict visibility
  1827. sint32 radius = rule_manager.GetGlobalRule(R_PVP, InvisPlayerDiscoveryRange)->GetSInt32();
  1828. if (radius != 0 && (Spawn*)spawn != this && this->IsPlayer() && !spawn->CanSeeInvis((Entity*)this))
  1829. spawnHiddenFromClient = true;
  1830. if (!spawnHiddenFromClient && (appearance.targetable == 1 || appearance.show_level == 1 || appearance.display_name == 1)) {
  1831. appearance.locked_no_loot = 1; //for now
  1832. if (!IsObject() && !IsGroundSpawn() && !IsWidget() && !IsSign()) {
  1833. int8 percent = 0;
  1834. if (GetHP() > 0)
  1835. percent = (int8)(((float)GetHP() / GetTotalHP()) * 100);
  1836. if (version >= 284) {
  1837. if (percent < 100) {
  1838. packet->setDataByName("hp_remaining", 100 ^ percent);
  1839. }
  1840. else
  1841. packet->setDataByName("hp_remaining", 0);
  1842. }
  1843. else {
  1844. if (percent > 100)
  1845. percent = 100;
  1846. packet->setDataByName("hp_remaining", percent);
  1847. }
  1848. if (GetTotalPower() > 0) {
  1849. percent = (int8)(((float)GetPower() / GetTotalPower()) * 100);
  1850. if (percent > 0)
  1851. packet->setDataByName("power_percent", percent);
  1852. else
  1853. packet->setDataByName("power_percent", 0);
  1854. }
  1855. }
  1856. }
  1857. if (version <= 546) {
  1858. packet->setDataByName("name", appearance.name);
  1859. for (int8 i = 0; i < 8; i++)
  1860. packet->setDataByName("unknown1", 0xFF, i);
  1861. if (appearance.show_level == 0)
  1862. packet->setDataByName("hide_health", 1);
  1863. }
  1864. if (GetHP() <= 0 && IsEntity())
  1865. packet->setDataByName("corpse", 1);
  1866. if (!IsPlayer())
  1867. packet->setDataByName("npc", 1);
  1868. if (GetMerchantID() > 0)
  1869. packet->setDataByName("merchant", 1);
  1870. if (EngagedInCombat())
  1871. packet->setDataByName("in_combat", 1);
  1872. packet->setDataByName("level", (int8)GetLevel());
  1873. packet->setDataByName("unknown4", (int8)GetLevel());
  1874. packet->setDataByName("difficulty", appearance.encounter_level); //6);
  1875. packet->setDataByName("heroic_flag", appearance.heroic_flag);
  1876. if (!IsObject() && !IsGroundSpawn() && !IsWidget() && !IsSign())
  1877. packet->setDataByName("interaction_flag", 12); //this makes NPCs head turn to look at you
  1878. packet->setDataByName("class", appearance.adventure_class);
  1879. int16 model_type = appearance.model_type;
  1880. if (GetIllusionModel() != 0) {
  1881. if (IsPlayer()) {
  1882. if (((Player*)this)->get_character_flag(CF_SHOW_ILLUSION)) {
  1883. model_type = GetIllusionModel();
  1884. }
  1885. }
  1886. else
  1887. model_type = GetIllusionModel();
  1888. }
  1889. int16 sogaModelType = appearance.soga_model_type;
  1890. if (spawnHiddenFromClient)
  1891. {
  1892. model_type = 0;
  1893. sogaModelType = 0;
  1894. }
  1895. packet->setDataByName("model_type", model_type);
  1896. if (appearance.soga_model_type == 0)
  1897. packet->setDataByName("soga_model_type", model_type);
  1898. else
  1899. packet->setDataByName("soga_model_type", sogaModelType);
  1900. if (GetTempActionState() >= 0)
  1901. packet->setDataByName("action_state", GetTempActionState());
  1902. else
  1903. packet->setDataByName("action_state", appearance.action_state);
  1904. if (GetTempVisualState() >= 0)
  1905. packet->setDataByName("visual_state", GetTempVisualState());
  1906. else
  1907. packet->setDataByName("visual_state", appearance.visual_state);
  1908. packet->setDataByName("emote_state", appearance.emote_state);
  1909. packet->setDataByName("mood_state", appearance.mood_state);
  1910. packet->setDataByName("gender", appearance.gender);
  1911. packet->setDataByName("race", appearance.race);
  1912. packet->setDataByName("gender", appearance.gender);
  1913. if (IsEntity()) {
  1914. Entity* entity = ((Entity*)this);
  1915. packet->setDataByName("combat_voice", entity->GetCombatVoice());
  1916. packet->setDataByName("emote_voice", entity->GetEmoteVoice());
  1917. for (int i = 0; i < 25; i++) {
  1918. if (i == 2) { //don't send helm if hidden flag
  1919. if (IsPlayer()) {
  1920. if (((Player*)this)->get_character_flag(CF_HIDE_HELM)) {
  1921. packet->setDataByName("equipment_types", 0, i);
  1922. packet->setColorByName("equipment_colors", 0, i);
  1923. packet->setColorByName("equipment_highlights", 0, i);
  1924. continue;
  1925. }
  1926. }
  1927. if (IsBot()) {
  1928. if (!((Bot*)this)->ShowHelm) {
  1929. packet->setDataByName("equipment_types", 0, i);
  1930. packet->setColorByName("equipment_colors", 0, i);
  1931. packet->setColorByName("equipment_highlights", 0, i);
  1932. continue;
  1933. }
  1934. }
  1935. }
  1936. else if (i == 19) { //don't send cloak if hidden
  1937. if (IsPlayer()) {
  1938. if (!((Player*)this)->get_character_flag(CF_SHOW_CLOAK)) {
  1939. packet->setDataByName("equipment_types", 0, i);
  1940. packet->setColorByName("equipment_colors", 0, i);
  1941. packet->setColorByName("equipment_highlights", 0, i);
  1942. continue;
  1943. }
  1944. }
  1945. if (IsBot()) {
  1946. if (!((Bot*)this)->ShowCloak) {
  1947. packet->setDataByName("equipment_types", 0, i);
  1948. packet->setColorByName("equipment_colors", 0, i);
  1949. packet->setColorByName("equipment_highlights", 0, i);
  1950. continue;
  1951. }
  1952. }
  1953. }
  1954. packet->setDataByName("equipment_types", entity->equipment.equip_id[i], i);
  1955. packet->setColorByName("equipment_colors", entity->equipment.color[i], i);
  1956. packet->setColorByName("equipment_highlights", entity->equipment.highlight[i], i);
  1957. }
  1958. packet->setDataByName("mount_type", entity->GetMount());
  1959. // find the visual flags
  1960. int8 vis_flag = 0;
  1961. //Invis + crouch flag check
  1962. if (entity->IsStealthed())
  1963. vis_flag += (INFO_VIS_FLAG_INVIS + INFO_VIS_FLAG_CROUCH);
  1964. //Invis flag check
  1965. else if (entity->IsInvis())
  1966. vis_flag += INFO_VIS_FLAG_INVIS;
  1967. //Mount flag check
  1968. if (entity->GetMount() > 0)
  1969. vis_flag += INFO_VIS_FLAG_MOUNTED;
  1970. //Hide hood check
  1971. if ((IsPlayer() && ((Player*)this)->get_character_flag(CF_HIDE_HOOD)) || appearance.hide_hood)
  1972. vis_flag += INFO_VIS_FLAG_HIDE_HOOD;
  1973. packet->setDataByName("visual_flag", vis_flag);
  1974. packet->setColorByName("mount_saddle_color", entity->GetMountSaddleColor());
  1975. packet->setColorByName("mount_color", entity->GetMountColor());
  1976. packet->setDataByName("hair_type_id", entity->features.hair_type);
  1977. packet->setDataByName("chest_type_id", entity->features.chest_type);
  1978. packet->setDataByName("wing_type_id", entity->features.wing_type);
  1979. packet->setDataByName("legs_type_id", entity->features.legs_type);
  1980. packet->setDataByName("soga_hair_type_id", entity->features.soga_hair_type);
  1981. packet->setDataByName("facial_hair_type_id", entity->features.hair_face_type);
  1982. packet->setDataByName("soga_facial_hair_type_id", entity->features.soga_hair_face_type);
  1983. for (int i = 0; i < 3; i++) {
  1984. packet->setDataByName("eye_type", entity->features.eye_type[i], i);
  1985. packet->setDataByName("ear_type", entity->features.ear_type[i], i);
  1986. packet->setDataByName("eye_brow_type", entity->features.eye_brow_type[i], i);
  1987. packet->setDataByName("cheek_type", entity->features.cheek_type[i], i);
  1988. packet->setDataByName("lip_type", entity->features.lip_type[i], i);
  1989. packet->setDataByName("chin_type", entity->features.chin_type[i], i);
  1990. packet->setDataByName("nose_type", entity->features.nose_type[i], i);
  1991. packet->setDataByName("soga_eye_type", entity->features.soga_eye_type[i], i);
  1992. packet->setDataByName("soga_ear_type", entity->features.soga_ear_type[i], i);
  1993. packet->setDataByName("soga_eye_brow_type", entity->features.soga_eye_brow_type[i], i);
  1994. packet->setDataByName("soga_cheek_type", entity->features.soga_cheek_type[i], i);
  1995. packet->setDataByName("soga_lip_type", entity->features.soga_lip_type[i], i);
  1996. packet->setDataByName("soga_chin_type", entity->features.soga_chin_type[i], i);
  1997. packet->setDataByName("soga_nose_type", entity->features.soga_nose_type[i], i);
  1998. }
  1999. packet->setColorByName("skin_color", entity->features.skin_color);
  2000. packet->setColorByName("eye_color", entity->features.eye_color);
  2001. packet->setColorByName("hair_type_color", entity->features.hair_type_color);
  2002. packet->setColorByName("hair_type_highlight_color", entity->features.hair_type_highlight_color);
  2003. packet->setColorByName("hair_face_color", entity->features.hair_face_color);
  2004. packet->setColorByName("hair_face_highlight_color", entity->features.hair_face_highlight_color);
  2005. packet->setColorByName("hair_highlight", entity->features.hair_highlight_color);
  2006. packet->setColorByName("wing_color1", entity->features.wing_color1);
  2007. packet->setColorByName("wing_color2", entity->features.wing_color2);
  2008. packet->setColorByName("hair_color1", entity->features.hair_color1);
  2009. packet->setColorByName("hair_color2", entity->features.hair_color2);
  2010. packet->setColorByName("soga_skin_color", entity->features.soga_skin_color);
  2011. packet->setColorByName("soga_eye_color", entity->features.soga_eye_color);
  2012. packet->setColorByName("soga_hair_color1", entity->features.soga_hair_color1);
  2013. packet->setColorByName("soga_hair_color2", entity->features.soga_hair_color2);
  2014. packet->setColorByName("soga_hair_type_color", entity->features.soga_hair_type_color);
  2015. packet->setColorByName("soga_hair_type_highlight_color", entity->features.soga_hair_type_highlight_color);
  2016. packet->setColorByName("soga_hair_face_color", entity->features.soga_hair_face_color);
  2017. packet->setColorByName("soga_hair_face_highlight_color", entity->features.soga_hair_face_highlight_color);
  2018. packet->setColorByName("soga_hair_highlight", entity->features.soga_hair_highlight_color);
  2019. packet->setDataByName("body_age", entity->features.body_age);
  2020. }
  2021. else {
  2022. EQ2_Color empty;
  2023. empty.red = 255;
  2024. empty.blue = 255;
  2025. empty.green = 255;
  2026. packet->setColorByName("skin_color", empty);
  2027. packet->setColorByName("eye_color", empty);
  2028. packet->setColorByName("soga_skin_color", empty);
  2029. packet->setColorByName("soga_eye_color", empty);
  2030. }
  2031. if (appearance.icon == 0) {
  2032. if (appearance.attackable == 1)
  2033. appearance.icon = 0;
  2034. else if (appearance.encounter_level > 0)
  2035. appearance.icon = 4;
  2036. else
  2037. appearance.icon = 6;
  2038. }
  2039. // If Coe+ clients modify the values before we send
  2040. // if not then just send the value we have.
  2041. int8 temp_icon = appearance.icon;
  2042. //Check if we need to add the hand icon..
  2043. if ((temp_icon & 6) != 6 && appearance.display_hand_icon) {
  2044. temp_icon |= 6;
  2045. }
  2046. //Icon value 28 for boats, set this without modifying the value
  2047. if (version >= 1188 && temp_icon != 28) {
  2048. if ((temp_icon & 64) > 0) {
  2049. temp_icon -= 64; // remove the DoV value;
  2050. temp_icon += 128; // add the CoE value
  2051. }
  2052. if ((temp_icon & 32) > 0) {
  2053. temp_icon -= 32; // remove the DoV value;
  2054. temp_icon += 64; // add the CoE value
  2055. }
  2056. if ((temp_icon & 4) > 0) {
  2057. temp_icon -= 4; // remove DoV value
  2058. temp_icon += 8; // add the CoE icon
  2059. }
  2060. if ((temp_icon & 6) > 0) {
  2061. temp_icon -= 10; // remove DoV value
  2062. temp_icon += 12; // add the CoE icon
  2063. }
  2064. }
  2065. packet->setDataByName("icon", temp_icon);//appearance.icon);
  2066. int32 temp_activity_status = 0;
  2067. if (!Alive() && GetTotalHP() > 0 && !IsObject() && !IsGroundSpawn())
  2068. temp_activity_status = 1;
  2069. temp_activity_status += (IsNPC() || IsObject() || IsGroundSpawn()) ? 1 << 1 : 0;
  2070. if ( version >= 546)
  2071. {
  2072. if (IsGroundSpawn() || GetShowHandIcon())
  2073. temp_activity_status += ACTIVITY_STATUS_INTERACTABLE_1188;
  2074. if ((appearance.activity_status & ACTIVITY_STATUS_ROLEPLAYING) > 0)
  2075. temp_activity_status += ACTIVITY_STATUS_ROLEPLAYING_1188;
  2076. if ((appearance.activity_status & ACTIVITY_STATUS_ANONYMOUS) > 0)
  2077. temp_activity_status += ACTIVITY_STATUS_ANONYMOUS_1188;
  2078. if ((appearance.activity_status & ACTIVITY_STATUS_LINKDEAD) > 0)
  2079. temp_activity_status += ACTIVITY_STATUS_LINKDEAD_1188;
  2080. if ((appearance.activity_status & ACTIVITY_STATUS_CAMPING) > 0)
  2081. temp_activity_status += ACTIVITY_STATUS_CAMPING_1188;
  2082. if ((appearance.activity_status & ACTIVITY_STATUS_LFG) > 0)
  2083. temp_activity_status += ACTIVITY_STATUS_LFG_1188;
  2084. if ((appearance.activity_status & ACTIVITY_STATUS_LFW) > 0)
  2085. temp_activity_status += ACTIVITY_STATUS_LFW_1188;
  2086. if ((appearance.activity_status & ACTIVITY_STATUS_SOLID) > 0)
  2087. temp_activity_status += ACTIVITY_STATUS_SOLID_1188;
  2088. if ((appearance.activity_status & ACTIVITY_STATUS_IMMUNITY_GAINED) > 0)
  2089. temp_activity_status += ACTIVITY_STATUS_IMMUNITY_GAINED_1188;
  2090. if ((appearance.activity_status & ACTIVITY_STATUS_IMMUNITY_REMAINING) > 0)
  2091. temp_activity_status += ACTIVITY_STATUS_IMMUNITY_REMAINING_1188;
  2092. if ((appearance.activity_status & ACTIVITY_STATUS_AFK) > 0)
  2093. temp_activity_status += ACTIVITY_STATUS_AFK_1188;
  2094. if (EngagedInCombat())
  2095. temp_activity_status += ACTIVITY_STATUS_INCOMBAT_1188;
  2096. // if this is either a boat or lift let the client be manipulated by the object
  2097. // doesn't work for DoF client version 546
  2098. if (appearance.icon == 28 || appearance.icon == 12)
  2099. {
  2100. temp_activity_status += ACTIVITY_STATUS_ISTRANSPORT_1188;
  2101. if ( version == 546 )
  2102. packet->setDataByName("is_transport", 1);
  2103. }
  2104. // for some reason Spawns are using different flags?? all NPCs were getting LFG before this was here
  2105. // ground spawns seem to behave the same way, LFG ground spawn anyone??
  2106. if ((IsEntity() || IsGroundSpawn()) && version <= 546)
  2107. temp_activity_status = 0;
  2108. }
  2109. else
  2110. {
  2111. temp_activity_status = appearance.activity_status;
  2112. temp_activity_status = 0xFF;
  2113. // this only partially fixes lifts in classic 283 client if you move just as the lift starts to move
  2114. if (appearance.icon == 28 || appearance.icon == 12)
  2115. packet->setDataByName("is_transport", 1);
  2116. if (MeetsSpawnAccessRequirements(spawn))
  2117. packet->setDataByName("hand_icon", appearance.display_hand_icon);
  2118. else {
  2119. if ((req_quests_override & 256) > 0)
  2120. packet->setDataByName("hand_icon", 1);
  2121. }
  2122. if (IsPlayer()) {
  2123. if (((Player*)this)->get_character_flag(CF_AFK))
  2124. packet->setDataByName("afk", 1);
  2125. if ((appearance.activity_status & ACTIVITY_STATUS_ROLEPLAYING) > 0)
  2126. packet->setDataByName("roleplaying", 1);
  2127. if ((appearance.activity_status & ACTIVITY_STATUS_ANONYMOUS) > 0)
  2128. packet->setDataByName("anonymous", 1);
  2129. if ((appearance.activity_status & ACTIVITY_STATUS_LINKDEAD) > 0)
  2130. packet->setDataByName("linkdead", 1);
  2131. if ((appearance.activity_status & ACTIVITY_STATUS_CAMPING) > 0)
  2132. packet->setDataByName("camping", 1);
  2133. if ((appearance.activity_status & ACTIVITY_STATUS_LFG) > 0)
  2134. packet->setDataByName("lfg", 1);
  2135. }
  2136. if ((appearance.activity_status & ACTIVITY_STATUS_SOLID) > 0)
  2137. packet->setDataByName("solid_object", 1);
  2138. }
  2139. packet->setDataByName("activity_status", temp_activity_status); //appearance.activity_status);
  2140. // If player and player has a follow target
  2141. if (IsPlayer()) {
  2142. if (((Player*)this)->GetFollowTarget())
  2143. packet->setDataByName("follow_target", ((((Player*)this)->GetIDWithPlayerSpawn(((Player*)this)->GetFollowTarget()) * -1) - 1));
  2144. else
  2145. packet->setDataByName("follow_target", 0xFFFFFFFF);
  2146. }
  2147. if (GetTarget() && GetTarget()->GetTargetable())
  2148. packet->setDataByName("target_id", ((spawn->GetIDWithPlayerSpawn(GetTarget()) * -1) - 1));
  2149. else
  2150. packet->setDataByName("target_id", 0xFFFFFFFF);
  2151. //Send spell effects for target window
  2152. if(IsEntity()){
  2153. InfoStruct* info = ((Entity*)this)->GetInfoStruct();
  2154. int8 i = 0;
  2155. int16 backdrop = 0;
  2156. int16 spell_icon = 0;
  2157. int32 spell_id = 0;
  2158. LuaSpell* spell = 0;
  2159. ((Entity*)this)->GetSpellEffectMutex()->readlock(__FUNCTION__, __LINE__);
  2160. while(i < 30){
  2161. //Change value of spell id for this packet if spell exists
  2162. spell_id = info->spell_effects[i].spell_id;
  2163. if(spell_id > 0)
  2164. spell_id = 0xFFFFFFFF - spell_id;
  2165. else
  2166. spell_id = 0;
  2167. packet->setSubstructDataByName("spell_effects", "spell_id", spell_id, i);
  2168. //Change value of spell icon for this packet if spell exists
  2169. spell_icon = info->spell_effects[i].icon;
  2170. if(spell_icon > 0){
  2171. if(!(spell_icon == 0xFFFF))
  2172. spell_icon = 0xFFFF - spell_icon;
  2173. }
  2174. else
  2175. spell_icon = 0;
  2176. packet->setSubstructDataByName("spell_effects", "spell_icon", spell_icon, i);
  2177. //Change backdrop values to match values in this packet
  2178. backdrop = info->spell_effects[i].icon_backdrop;
  2179. switch(backdrop){
  2180. case 312:
  2181. backdrop = 33080;
  2182. break;
  2183. case 313:
  2184. backdrop = 33081;
  2185. break;
  2186. case 314:
  2187. backdrop = 33082;
  2188. break;
  2189. case 315:
  2190. backdrop = 33083;
  2191. break;
  2192. case 316:
  2193. backdrop = 33084;
  2194. break;
  2195. case 317:
  2196. backdrop = 33085;
  2197. break;
  2198. case (318 || 319):
  2199. backdrop = 33086;
  2200. break;
  2201. default:
  2202. break;
  2203. }
  2204. packet->setSubstructDataByName("spell_effects", "spell_icon_backdrop", backdrop, i);
  2205. spell = info->spell_effects[i].spell;
  2206. if (spell)
  2207. packet->setSubstructDataByName("spell_effects", "spell_triggercount", spell->num_triggers, i);
  2208. i++;
  2209. }
  2210. ((Entity*)this)->GetSpellEffectMutex()->releasereadlock(__FUNCTION__, __LINE__);
  2211. }
  2212. }
  2213. void Spawn::MoveToLocation(Spawn* spawn, float distance, bool immediate, bool mapped){
  2214. if(!spawn)
  2215. return;
  2216. SetRunningTo(spawn);
  2217. FaceTarget(spawn);
  2218. if (!IsPlayer() && distance > 0.0f)
  2219. {
  2220. if (IsFlying() && CheckLoS(spawn))
  2221. {
  2222. if (immediate)
  2223. ClearRunningLocations();
  2224. AddRunningLocation(spawn->GetX(), spawn->GetY(), spawn->GetZ(), GetSpeed(), distance, true, true, "", true);
  2225. }
  2226. else if (/*!mapped && */GetZone())
  2227. {
  2228. GetZone()->movementMgr->NavigateTo((Entity*)this, spawn->GetX(), spawn->GetY(), spawn->GetZ());
  2229. last_grid_update = Timer::GetCurrentTime2();
  2230. }
  2231. else
  2232. {
  2233. if (immediate)
  2234. ClearRunningLocations();
  2235. AddRunningLocation(spawn->GetX(), spawn->GetY(), spawn->GetZ(), GetSpeed(), distance, true, true, "", mapped);
  2236. }
  2237. }
  2238. }
  2239. void Spawn::ProcessMovement(bool isSpawnListLocked){
  2240. CheckProximities();
  2241. if(IsPlayer()){
  2242. //Check if player is riding a boat, if so update pos (boat's current location + XYZ offsets)
  2243. Player* player = ((Player*)this);
  2244. int32 boat_id = player->GetBoatSpawn();
  2245. Spawn* boat = 0;
  2246. if(boat_id > 0)
  2247. boat = GetZone()->GetSpawnByID(boat_id, isSpawnListLocked);
  2248. if(boat){
  2249. SetX(boat->GetX() + player->GetBoatX());
  2250. SetY(boat->GetY() + player->GetBoatY());
  2251. SetZ(boat->GetZ() + player->GetBoatZ());
  2252. }
  2253. return;
  2254. }
  2255. if (forceMapCheck && GetZone() != nullptr && zone->zonemap != nullptr && zone->zonemap->IsMapLoaded())
  2256. {
  2257. FixZ(true);
  2258. int32 newGrid = GetZone()->Grid->GetGridID(this);
  2259. if (!IsFlying() && newGrid != 0 && newGrid != appearance.pos.grid_id)
  2260. SetPos(&(appearance.pos.grid_id), newGrid);
  2261. forceMapCheck = false;
  2262. }
  2263. if (GetHP() <= 0 && !IsWidget())
  2264. return;
  2265. if (EngagedInCombat())
  2266. {
  2267. int locations = 0;
  2268. if (movement_locations && MMovementLocations)
  2269. {
  2270. MMovementLocations->readlock(__FUNCTION__, __LINE__);
  2271. locations = movement_locations->size();
  2272. MMovementLocations->releasereadlock(__FUNCTION__, __LINE__);
  2273. }
  2274. if (locations < 1 && GetZone() && ((Entity*)this)->IsFeared())
  2275. {
  2276. CalculateNewFearpoint();
  2277. }
  2278. }
  2279. MMovementLoop.lock();
  2280. Spawn* followTarget = GetZone()->GetSpawnByID(m_followTarget, isSpawnListLocked);
  2281. if (!followTarget && m_followTarget > 0)
  2282. m_followTarget = 0;
  2283. if (following && followTarget && !((Entity*)this)->IsFeared()) {
  2284. // Need to clear m_followTarget before the zoneserver deletes it
  2285. if (followTarget->GetHP() <= 0) {
  2286. followTarget = 0;
  2287. MMovementLoop.unlock();
  2288. return;
  2289. }
  2290. if (!IsEntity() || (!((Entity*)this)->IsCasting() && !((Entity*)this)->IsMezzedOrStunned() && !((Entity*)this)->IsRooted())) {
  2291. if (GetBaseSpeed() > 0) {
  2292. CalculateRunningLocation();
  2293. }
  2294. else {
  2295. float speed = 4.0f;
  2296. if (IsEntity())
  2297. speed = ((Entity*)this)->GetMaxSpeed();
  2298. SetSpeed(speed);
  2299. }
  2300. MovementLocation* loc = GetCurrentRunningLocation();
  2301. float dist = GetDistance(followTarget, true);
  2302. if ((!EngagedInCombat() && m_followDistance > 0 && dist <= m_followDistance) ||
  2303. (dist <= rule_manager.GetGlobalRule(R_Combat, MaxCombatRange)->GetFloat())) {
  2304. ClearRunningLocations();
  2305. CalculateRunningLocation(true);
  2306. }
  2307. else if (loc) {
  2308. float distance = GetDistance(followTarget, loc->x, loc->y, loc->z);
  2309. if ( (!EngagedInCombat() && m_followDistance > 0 && distance > m_followDistance) ||
  2310. ( EngagedInCombat() && distance > rule_manager.GetGlobalRule(R_Combat, MaxCombatRange)->GetFloat())) {
  2311. MoveToLocation(followTarget, rule_manager.GetGlobalRule(R_Combat, MaxCombatRange)->GetFloat(), true, loc->mapped);
  2312. CalculateRunningLocation();
  2313. }
  2314. }
  2315. else {
  2316. MoveToLocation(followTarget, rule_manager.GetGlobalRule(R_Combat, MaxCombatRange)->GetFloat());
  2317. CalculateRunningLocation();
  2318. }
  2319. }
  2320. }
  2321. // Movement loop is only for scripted paths
  2322. else if(!EngagedInCombat() && !NeedsToResumeMovement() && movement_loop.size() > 0 && movement_index < movement_loop.size() && (!IsNPC() || !((NPC*)this)->m_runningBack)){
  2323. // Get the target location
  2324. MovementData* data = movement_loop[movement_index];
  2325. // need to resume our movement
  2326. if(resume_movement){
  2327. if (movement_locations){
  2328. while (movement_locations->size()){
  2329. safe_delete(movement_locations->front());
  2330. movement_locations->pop_front();
  2331. }
  2332. movement_locations->clear();
  2333. }
  2334. data = movement_loop[movement_index];
  2335. ((Entity*)this)->SetSpeed(data->speed);
  2336. SetSpeed(data->speed);
  2337. if(!IsWidget())
  2338. FaceTarget(data->x, data->z);
  2339. // 0 delay at target location, need to set multiple locations
  2340. if(data->delay == 0 && movement_loop.size() > 0) {
  2341. int16 tmp_index = movement_index+1;
  2342. MovementData* data2 = 0;
  2343. if(tmp_index < movement_loop.size())
  2344. data2 = movement_loop[tmp_index];
  2345. else
  2346. data2 = movement_loop[0];
  2347. AddRunningLocation(data->x, data->y, data->z, data->speed, 0, true, false, "", true);
  2348. AddRunningLocation(data2->x, data2->y, data2->z, data2->speed, 0, true, true, "", true);
  2349. }
  2350. // delay at target location, only need to set 1 location
  2351. else
  2352. AddRunningLocation(data->x, data->y, data->z, data->speed);
  2353. movement_start_time = 0;
  2354. resume_movement = false;
  2355. }
  2356. // If we are not moving or we have arrived at our destination
  2357. else if(!IsRunning() || (data && data->x == GetX() && data->y == GetY() && data->z == GetZ())){
  2358. // If we were moving remove the last running location (the point we just arrived at)
  2359. if(IsRunning())
  2360. RemoveRunningLocation();
  2361. // If this waypoint has a delay and we just arrived here (movement_start_time == 0)
  2362. if(data->delay > 0 && movement_start_time == 0){
  2363. // Set the current time
  2364. movement_start_time = Timer::GetCurrentTime2();
  2365. // If this waypoint had a lua function then call it
  2366. if(data->lua_function.length() > 0)
  2367. GetZone()->CallSpawnScript(this, SPAWN_SCRIPT_CUSTOM, 0, data->lua_function.c_str());
  2368. int16 nextMove;
  2369. if ((int16)(movement_index + 1) < movement_loop.size())
  2370. nextMove = movement_index + 1;
  2371. else
  2372. nextMove = 0;
  2373. // Get the next target location
  2374. data = movement_loop[nextMove];
  2375. //Go ahead and face the next location
  2376. FaceTarget(data->x, data->z);
  2377. }
  2378. // If this waypoint has no delay or we have waited the required time (current time >= delay + movement_start_time)
  2379. else if(data->delay == 0 || (data->delay > 0 && Timer::GetCurrentTime2() >= (data->delay+movement_start_time))) {
  2380. // if no delay at this waypoint but a lua function for it then call the function
  2381. if(data->delay == 0 && data->lua_function.length() > 0)
  2382. GetZone()->CallSpawnScript(this, SPAWN_SCRIPT_CUSTOM, 0, data->lua_function.c_str());
  2383. // Advance the current movement loop index
  2384. if((int16)(movement_index+1) < movement_loop.size())
  2385. movement_index++;
  2386. else
  2387. movement_index = 0;
  2388. // Get the next target location
  2389. data = movement_loop[movement_index];
  2390. // set the speed for that location
  2391. SetSpeed(data->speed);
  2392. if(!IsWidget())
  2393. // turn towards the location
  2394. FaceTarget(data->x, data->z);
  2395. // If 0 delay at location get and set data for the point after it
  2396. if(data->delay == 0 && movement_loop.size() > 0){
  2397. while (movement_locations->size()){
  2398. safe_delete(movement_locations->front());
  2399. movement_locations->pop_front();
  2400. }
  2401. // clear current target locations
  2402. movement_locations->clear();
  2403. // get the data for the location after out new location
  2404. int16 tmp_index = movement_index+1;
  2405. MovementData* data2 = 0;
  2406. if(tmp_index < movement_loop.size())
  2407. data2 = movement_loop[tmp_index];
  2408. else
  2409. data2 = movement_loop[0];
  2410. // set the first location (adds it to movement_locations that we just cleared)
  2411. AddRunningLocation(data->x, data->y, data->z, data->speed, 0, true, false, "", true);
  2412. // set the location after that
  2413. AddRunningLocation(data2->x, data2->y, data2->z, data2->speed, 0, true, true, "", true);
  2414. }
  2415. // there is a delay at the next location so we only need to set it
  2416. else
  2417. AddRunningLocation(data->x, data->y, data->z, data->speed);
  2418. // reset this timer to 0 now that we are moving again
  2419. movement_start_time = 0;
  2420. }
  2421. }
  2422. // moving and not at target location yet
  2423. else if(GetBaseSpeed() > 0)
  2424. CalculateRunningLocation();
  2425. // not moving, have a target location but not at it yet
  2426. else if (data) {
  2427. SetSpeed(data->speed);
  2428. AddRunningLocation(data->x, data->y, data->z, data->speed);
  2429. }
  2430. }
  2431. else if (IsRunning()) {
  2432. CalculateRunningLocation();
  2433. }
  2434. /*else if (IsNPC() && !IsRunning() && !EngagedInCombat() && ((NPC*)this)->GetRunbackLocation()) {
  2435. // Is an npc that is not moving and not engaged in combat but has a run back location set then clear the runback location
  2436. LogWrite(NPC_AI__DEBUG, 7, "NPC_AI", "Clear runback location for %s", GetName());
  2437. ((NPC*)this)->ClearRunback();
  2438. resume_movement = true;
  2439. NeedsToResumeMovement(false);
  2440. }*/
  2441. MMovementLoop.unlock();
  2442. }
  2443. void Spawn::ResetMovement(){
  2444. MMovementLoop.lock();
  2445. vector<MovementData*>::iterator itr;
  2446. for(itr = movement_loop.begin(); itr != movement_loop.end(); itr++){
  2447. safe_delete(*itr);
  2448. }
  2449. MMovementLoop.unlock();
  2450. resume_movement = true;
  2451. movement_index = 0;
  2452. }
  2453. void Spawn::AddMovementLocation(float x, float y, float z, float speed, int16 delay, const char* lua_function){
  2454. LogWrite(LUA__DEBUG, 5, "LUA", "AddMovementLocation: x: %.2f, y: %.2f, z: %.2f, speed: %.2f, delay: %i, lua: %s",
  2455. x, y, z, speed, delay, string(lua_function).c_str());
  2456. MovementData* data = new MovementData;
  2457. data->x = x;
  2458. data->y = y;
  2459. data->z = z;
  2460. data->speed = speed;
  2461. data->delay = delay*1000;
  2462. if(lua_function)
  2463. data->lua_function = string(lua_function);
  2464. MMovementLoop.lock();
  2465. movement_loop.push_back(data);
  2466. MMovementLoop.unlock();
  2467. }
  2468. bool Spawn::IsRunning(){
  2469. if(movement_locations && movement_locations->size() > 0)
  2470. return true;
  2471. else
  2472. return false;
  2473. }
  2474. void Spawn::RunToLocation(float x, float y, float z, float following_x, float following_y, float following_z){
  2475. if(!IsWidget())
  2476. FaceTarget(x, z);
  2477. SetPos(&appearance.pos.X2, x, false);
  2478. SetPos(&appearance.pos.Z2, z, false);
  2479. SetPos(&appearance.pos.Y2, y, false);
  2480. if(following_x == 0 && following_y == 0 && following_z == 0){
  2481. SetPos(&appearance.pos.X3, x, false);
  2482. SetPos(&appearance.pos.Z3, z, false);
  2483. SetPos(&appearance.pos.Y3, y, false);
  2484. }
  2485. else{
  2486. SetPos(&appearance.pos.X3, following_x, false);
  2487. SetPos(&appearance.pos.Y3, following_y, false);
  2488. SetPos(&appearance.pos.Z3, following_z, false);
  2489. }
  2490. position_changed = true;
  2491. changed = true;
  2492. GetZone()->AddChangedSpawn(this);
  2493. }
  2494. MovementLocation* Spawn::GetCurrentRunningLocation(){
  2495. MovementLocation* ret = 0;
  2496. if(movement_locations && movement_locations->size() > 0){
  2497. MMovementLocations->readlock(__FUNCTION__, __LINE__);
  2498. ret = movement_locations->front();
  2499. MMovementLocations->releasereadlock(__FUNCTION__, __LINE__);
  2500. }
  2501. return ret;
  2502. }
  2503. MovementLocation* Spawn::GetLastRunningLocation(){
  2504. MovementLocation* ret = 0;
  2505. if(movement_locations && movement_locations->size() > 0){
  2506. MMovementLocations->readlock(__FUNCTION__, __LINE__);
  2507. ret = movement_locations->back();
  2508. MMovementLocations->releasereadlock(__FUNCTION__, __LINE__);
  2509. }
  2510. return ret;
  2511. }
  2512. void Spawn::AddRunningLocation(float x, float y, float z, float speed, float distance_away, bool attackable, bool finished_adding_locations, string lua_function, bool isMapped){
  2513. if(speed == 0)
  2514. return;
  2515. ((Entity*)this)->SetSpeed(speed);
  2516. MovementLocation* current_location = 0;
  2517. float distance = GetDistance(x, y, z, distance_away != 0);
  2518. if(distance_away != 0){
  2519. distance -= distance_away;
  2520. x = x - (GetX() - x)*distance_away/distance;
  2521. z = z - (GetZ() - z)*distance_away/distance;
  2522. }
  2523. if(!movement_locations){
  2524. movement_locations = new deque<MovementLocation*>();
  2525. MMovementLocations = new Mutex();
  2526. }
  2527. MovementLocation* data = new MovementLocation;
  2528. data->mapped = isMapped;
  2529. data->x = x;
  2530. data->y = y;
  2531. data->z = z;
  2532. data->speed = speed;
  2533. data->attackable = attackable;
  2534. data->lua_function = lua_function;
  2535. data->gridid = 0; // used for runback defaults
  2536. MMovementLocations->writelock(__FUNCTION__, __LINE__);
  2537. if(movement_locations->size() > 0)
  2538. current_location = movement_locations->back();
  2539. if(!current_location){
  2540. SetSpawnOrigX(GetX());
  2541. SetSpawnOrigY(GetY());
  2542. SetSpawnOrigZ(GetZ());
  2543. SetSpawnOrigHeading(GetHeading());
  2544. }
  2545. movement_locations->push_back(data);
  2546. if(finished_adding_locations){
  2547. current_location = movement_locations->front();
  2548. SetSpeed(current_location->speed);
  2549. if(movement_locations->size() > 1){
  2550. data = movement_locations->at(1);
  2551. RunToLocation(current_location->x, current_location->y, current_location->z, data->x, data->y, data->z);
  2552. }
  2553. else
  2554. RunToLocation(current_location->x, current_location->y, current_location->z, 0, 0, 0);
  2555. }
  2556. MMovementLocations->releasewritelock(__FUNCTION__, __LINE__);
  2557. }
  2558. bool Spawn::RemoveRunningLocation(){
  2559. bool ret = false;
  2560. if(movement_locations){
  2561. MMovementLocations->writelock(__FUNCTION__, __LINE__);
  2562. if(movement_locations->size() > 0){
  2563. delete movement_locations->front();
  2564. movement_locations->pop_front();
  2565. ret = true;
  2566. }
  2567. MMovementLocations->releasewritelock(__FUNCTION__, __LINE__);
  2568. }
  2569. return ret;
  2570. }
  2571. void Spawn::ClearRunningLocations(){
  2572. while(RemoveRunningLocation()){}
  2573. }
  2574. bool Spawn::CalculateChange(){
  2575. bool remove_needed = false;
  2576. if(movement_locations && MMovementLocations){
  2577. MovementLocation* data = 0;
  2578. MMovementLocations->readlock(__FUNCTION__, __LINE__);
  2579. if(movement_locations->size() > 0){
  2580. // Target location
  2581. data = movement_locations->front();
  2582. // If no target or we are at the target location need to remove this point
  2583. if(!data || (data->x == GetX() && data->y == GetY() && data->z == GetZ()))
  2584. remove_needed = true;
  2585. if(data){
  2586. if(NeedsToResumeMovement()){
  2587. resume_movement = true;
  2588. NeedsToResumeMovement(false);
  2589. }
  2590. if(!data->attackable)
  2591. SetHeading(GetSpawnOrigHeading());
  2592. }
  2593. }
  2594. MMovementLocations->releasereadlock(__FUNCTION__, __LINE__);
  2595. if(remove_needed) {
  2596. if (data && data->lua_function.length() > 0)
  2597. GetZone()->CallSpawnScript(this, SPAWN_SCRIPT_CUSTOM, 0, data->lua_function.c_str());
  2598. RemoveRunningLocation();
  2599. //CalculateChange();
  2600. }
  2601. else if(data){
  2602. // Speed is per second so we need a time_step (amount of time since the last update) to modify movement by
  2603. float time_step = (Timer::GetCurrentTime2() - last_movement_update) * 0.001; // * 0.001 is the same as / 1000, float muliplications is suppose to be faster though
  2604. // Get current location
  2605. float nx = GetX();
  2606. float ny = GetY();
  2607. float nz = GetZ();
  2608. // Get Forward vecotr
  2609. float tar_vx = data->x - nx;
  2610. float tar_vy = data->y - ny;
  2611. float tar_vz = data->z - nz;
  2612. // Multiply speed by the time_step to get how much should have changed over the last tick
  2613. float speed = GetSpeed() * time_step;
  2614. // Normalize the forward vector and multiply by speed, this gives us our change in coords, just need to add them to our current coords
  2615. float len = sqrtf(tar_vx * tar_vx + tar_vy * tar_vy + tar_vz * tar_vz);
  2616. tar_vx = (tar_vx / len) * speed;
  2617. tar_vy = (tar_vy / len) * speed;
  2618. tar_vz = (tar_vz / len) * speed;
  2619. // Distance less then 0.5 just set the npc to the target location
  2620. if (GetDistance(data->x, data->y, data->z, IsWidget() ? false : true) <= 0.5f) {
  2621. SetX(data->x, false);
  2622. SetZ(data->z, false);
  2623. SetY(data->y, false, true);
  2624. }
  2625. else {
  2626. SetX(nx + tar_vx, false);
  2627. SetZ(nz + tar_vz, false);
  2628. if ( IsWidget() )
  2629. SetY(ny + tar_vy, false, true);
  2630. else
  2631. SetY(ny + tar_vy, false);
  2632. }
  2633. if (GetZone()->Grid != nullptr) {
  2634. Cell* newCell = GetZone()->Grid->GetCell(GetX(), GetZ());
  2635. if (newCell != Cell_Info.CurrentCell) {
  2636. GetZone()->Grid->RemoveSpawnFromCell(this);
  2637. GetZone()->Grid->AddSpawn(this, newCell);
  2638. }
  2639. int32 newGrid = GetZone()->Grid->GetGridID(this);
  2640. if (!IsFlying() && newGrid != 0 && newGrid != appearance.pos.grid_id)
  2641. SetPos(&(appearance.pos.grid_id), newGrid);
  2642. }
  2643. }
  2644. }
  2645. return remove_needed;
  2646. }
  2647. void Spawn::CalculateRunningLocation(bool stop){
  2648. if (!stop && (last_location_update + 100) > Timer::GetCurrentTime2())
  2649. return;
  2650. else if (!stop)
  2651. last_location_update = Timer::GetCurrentTime2();
  2652. bool removed = CalculateChange();
  2653. if (stop) {
  2654. //following = false;
  2655. SetPos(&appearance.pos.X2, GetX(), false);
  2656. SetPos(&appearance.pos.Y2, GetY(), false);
  2657. SetPos(&appearance.pos.Z2, GetZ(), false);
  2658. SetPos(&appearance.pos.X3, GetX(), false);
  2659. SetPos(&appearance.pos.Y3, GetY(), false);
  2660. SetPos(&appearance.pos.Z3, GetZ(), false);
  2661. }
  2662. else if (removed && movement_locations && movement_locations->size() > 0) {
  2663. MovementLocation* current_location = movement_locations->at(0);
  2664. if (movement_locations->size() > 1) {
  2665. MovementLocation* data = movement_locations->at(1);
  2666. RunToLocation(current_location->x, current_location->y, current_location->z, data->x, data->y, data->z);
  2667. }
  2668. else
  2669. RunToLocation(current_location->x, current_location->y, current_location->z, 0, 0, 0);
  2670. }
  2671. else if (GetZone() && GetTarget() != NULL && EngagedInCombat())
  2672. {
  2673. if (GetDistance(GetTarget()) > rule_manager.GetGlobalRule(R_Combat, MaxCombatRange)->GetFloat())
  2674. {
  2675. if (IsFlying() && CheckLoS(GetTarget()))
  2676. AddRunningLocation(GetTarget()->GetX(), GetTarget()->GetY(), GetTarget()->GetZ(), GetSpeed(), 0, false);
  2677. else
  2678. GetZone()->movementMgr->NavigateTo((Entity*)this, GetTarget()->GetX(), GetTarget()->GetY(), GetTarget()->GetZ());
  2679. }
  2680. else
  2681. ((Entity*)this)->HaltMovement();
  2682. }
  2683. }
  2684. float Spawn::GetFaceTarget(float x, float z) {
  2685. float angle;
  2686. double diff_x = x - GetX();
  2687. double diff_z = z - GetZ();
  2688. //If we're very close to the same spot don't bother changing heading
  2689. if (sqrt(diff_x * diff_x * diff_z * diff_z) < .1) {
  2690. return GetHeading();
  2691. }
  2692. if (diff_z == 0) {
  2693. if (diff_x > 0)
  2694. angle = 90;
  2695. else
  2696. angle = 270;
  2697. }
  2698. else
  2699. angle = ((atan(diff_x / diff_z)) * 180) / 3.14159265358979323846;
  2700. if (angle < 0)
  2701. angle = angle + 360;
  2702. else
  2703. angle = angle + 180;
  2704. if (diff_x < 0)
  2705. angle = angle + 180;
  2706. if (last_heading_angle == angle) return angle;
  2707. return angle;
  2708. }
  2709. void Spawn::FaceTarget(float x, float z){
  2710. float angle;
  2711. double diff_x = x - GetX();
  2712. double diff_z = z - GetZ();
  2713. //If we're very close to the same spot don't bother changing heading
  2714. if (sqrt(diff_x * diff_x * diff_z * diff_z) < .1) {
  2715. return;
  2716. }
  2717. if(diff_z==0){
  2718. if(diff_x > 0)
  2719. angle = 90;
  2720. else
  2721. angle = 270;
  2722. }
  2723. else
  2724. angle = ((atan(diff_x / diff_z)) * 180) / 3.14159265358979323846;
  2725. if(angle < 0)
  2726. angle = angle + 360;
  2727. else
  2728. angle = angle + 180;
  2729. if(diff_x < 0)
  2730. angle = angle + 180;
  2731. if (last_heading_angle == angle) return;
  2732. SetHeading(angle);
  2733. }
  2734. void Spawn::FaceTarget(Spawn* target){
  2735. if(!target)
  2736. return;
  2737. FaceTarget(target->GetX(), target->GetZ());
  2738. if(GetHP() > 0 && target->IsPlayer() && !EngagedInCombat()){
  2739. GetZone()->AddHeadingTimer(this);
  2740. SetTempActionState(0);
  2741. }
  2742. }
  2743. bool Spawn::MeetsSpawnAccessRequirements(Player* player){
  2744. bool ret = false;
  2745. Quest* quest = 0;
  2746. //Check if we meet all quest requirements first..
  2747. m_requiredQuests.readlock(__FUNCTION__, __LINE__);
  2748. if (player && required_quests.size() > 0) {
  2749. map<int32, vector<int16>* >::iterator itr;
  2750. for (itr = required_quests.begin(); itr != required_quests.end(); itr++) {
  2751. player->AddQuestRequiredSpawn(this, itr->first);
  2752. vector<int16>* quest_steps = itr->second;
  2753. for (int32 i = 0; i < quest_steps->size(); i++) {
  2754. quest = player->GetQuest(itr->first);
  2755. if (req_quests_continued_access) {
  2756. if (quest) {
  2757. if (quest->GetQuestStepCompleted(quest_steps->at(i))) {
  2758. ret = true;
  2759. break;
  2760. }
  2761. }
  2762. else if (player->GetCompletedQuest(itr->first)) {
  2763. ret = true;
  2764. break;
  2765. }
  2766. }
  2767. if (quest && quest->QuestStepIsActive(quest_steps->at(i))) {
  2768. ret = true;
  2769. break;
  2770. }
  2771. }
  2772. }
  2773. }
  2774. else
  2775. ret = true;
  2776. m_requiredQuests.releasereadlock(__FUNCTION__, __LINE__);
  2777. if (!ret)
  2778. return ret;
  2779. //Now check if the player meets all history requirements
  2780. m_requiredHistory.readlock(__FUNCTION__, __LINE__);
  2781. if (required_history.size() > 0){
  2782. map<int32, LUAHistory>::iterator itr;
  2783. for (itr = required_history.begin(); itr != required_history.end(); itr++){
  2784. player->AddHistoryRequiredSpawn(this, itr->first);
  2785. LUAHistory* player_history = player->GetLUAHistory(itr->first);
  2786. if (player_history){
  2787. if (player_history->Value != itr->second.Value || player_history->Value2 != itr->second.Value2)
  2788. ret = false;
  2789. }
  2790. else
  2791. ret = false;
  2792. if (!ret)
  2793. break;
  2794. }
  2795. }
  2796. m_requiredHistory.releasereadlock(__FUNCTION__, __LINE__);
  2797. return ret;
  2798. }
  2799. vector<Spawn*>* Spawn::GetSpawnGroup(){
  2800. vector<Spawn*>* ret_list = 0;
  2801. if(spawn_group_list){
  2802. ret_list = new vector<Spawn*>();
  2803. if(MSpawnGroup)
  2804. MSpawnGroup->readlock(__FUNCTION__, __LINE__);
  2805. ret_list->insert(ret_list->begin(), spawn_group_list->begin(), spawn_group_list->end());
  2806. if(MSpawnGroup)
  2807. MSpawnGroup->releasereadlock(__FUNCTION__, __LINE__);
  2808. }
  2809. return ret_list;
  2810. }
  2811. bool Spawn::HasSpawnGroup() {
  2812. return spawn_group_list && spawn_group_list->size() > 0;
  2813. }
  2814. bool Spawn::IsInSpawnGroup(Spawn* spawn) {
  2815. bool ret = false;
  2816. if (HasSpawnGroup() && spawn) {
  2817. vector<Spawn*>::iterator itr;
  2818. for (itr = spawn_group_list->begin(); itr != spawn_group_list->end(); itr++) {
  2819. if ((*itr) == spawn) {
  2820. ret = true;
  2821. break;
  2822. }
  2823. }
  2824. }
  2825. return ret;
  2826. }
  2827. void Spawn::AddSpawnToGroup(Spawn* spawn){
  2828. if(!spawn)
  2829. return;
  2830. if(!spawn_group_list){
  2831. spawn_group_list = new vector<Spawn*>();
  2832. spawn_group_list->push_back(this);
  2833. safe_delete(MSpawnGroup);
  2834. MSpawnGroup = new Mutex();
  2835. MSpawnGroup->SetName("Spawn::MSpawnGroup");
  2836. }
  2837. vector<Spawn*>::iterator itr;
  2838. MSpawnGroup->writelock(__FUNCTION__, __LINE__);
  2839. for(itr = spawn_group_list->begin(); itr != spawn_group_list->end(); itr++){
  2840. if((*itr) == spawn){
  2841. MSpawnGroup->releasewritelock(__FUNCTION__, __LINE__);
  2842. return;
  2843. }
  2844. }
  2845. spawn_group_list->push_back(spawn);
  2846. spawn->SetSpawnGroupList(spawn_group_list, MSpawnGroup);
  2847. MSpawnGroup->releasewritelock(__FUNCTION__, __LINE__);
  2848. }
  2849. void Spawn::SetSpawnGroupList(vector<Spawn*>* list, Mutex* mutex){
  2850. spawn_group_list = list;
  2851. MSpawnGroup = mutex;
  2852. }
  2853. void Spawn::RemoveSpawnFromGroup(bool erase_all){
  2854. SetSpawnGroupID(0);
  2855. bool del = false;
  2856. if(MSpawnGroup){
  2857. MSpawnGroup->writelock(__FUNCTION__, __LINE__);
  2858. if(spawn_group_list){
  2859. vector<Spawn*>::iterator itr;
  2860. Spawn* spawn = 0;
  2861. if(spawn_group_list->size() == 1)
  2862. erase_all = true;
  2863. for(itr = spawn_group_list->begin(); itr != spawn_group_list->end(); itr++){
  2864. spawn = *itr;
  2865. if (spawn) {
  2866. if(!erase_all){
  2867. if(spawn == this){
  2868. spawn_group_list->erase(itr);
  2869. MSpawnGroup->releasewritelock(__FUNCTION__, __LINE__);
  2870. spawn_group_list = 0;
  2871. MSpawnGroup = 0;
  2872. return;
  2873. }
  2874. }
  2875. else{
  2876. if (spawn != this)
  2877. spawn->SetSpawnGroupList(0, 0);
  2878. }
  2879. }
  2880. }
  2881. if (erase_all)
  2882. spawn_group_list->clear();
  2883. del = (spawn_group_list->size() == 0);
  2884. }
  2885. MSpawnGroup->releasewritelock(__FUNCTION__, __LINE__);
  2886. if (del){
  2887. safe_delete(MSpawnGroup);
  2888. safe_delete(spawn_group_list);
  2889. }
  2890. }
  2891. }
  2892. void Spawn::SetSpawnGroupID(int32 id){
  2893. group_id = id;
  2894. }
  2895. int32 Spawn::GetSpawnGroupID(){
  2896. return group_id;
  2897. }
  2898. void Spawn::AddChangedZoneSpawn(){
  2899. if(send_spawn_changes && GetZone())
  2900. GetZone()->AddChangedSpawn(this);
  2901. }
  2902. void Spawn::RemoveSpawnAccess(Spawn* spawn) {
  2903. if (allowed_access.count(spawn->GetID()) > 0) {
  2904. allowed_access.erase(spawn->GetID());
  2905. GetZone()->HidePrivateSpawn(this);
  2906. }
  2907. }
  2908. void Spawn::SetFollowTarget(Spawn* spawn, int32 follow_distance) {
  2909. if (spawn && spawn != this) {
  2910. m_followTarget = spawn->GetID();
  2911. m_followDistance = follow_distance;
  2912. }
  2913. else {
  2914. m_followTarget = 0;
  2915. if (following)
  2916. following = false;
  2917. m_followDistance = 0;
  2918. }
  2919. }
  2920. void Spawn::AddTempVariable(string var, string val) {
  2921. m_tempVariableTypes[var] = 5;
  2922. m_tempVariables[var] = val;
  2923. }
  2924. void Spawn::AddTempVariable(string var, Spawn* val) {
  2925. m_tempVariableTypes[var] = 1;
  2926. m_tempVariableSpawn[var] = val->GetID();
  2927. }
  2928. void Spawn::AddTempVariable(string var, ZoneServer* val) {
  2929. m_tempVariableTypes[var] = 2;
  2930. m_tempVariableZone[var] = val;
  2931. }
  2932. void Spawn::AddTempVariable(string var, Item* val) {
  2933. m_tempVariableTypes[var] = 3;
  2934. m_tempVariableItem[var] = val;
  2935. }
  2936. void Spawn::AddTempVariable(string var, Quest* val) {
  2937. m_tempVariableTypes[var] = 4;
  2938. m_tempVariableQuest[var] = val;
  2939. }
  2940. string Spawn::GetTempVariable(string var) {
  2941. string ret = "";
  2942. if (m_tempVariables.count(var) > 0)
  2943. ret = m_tempVariables[var];
  2944. return ret;
  2945. }
  2946. Spawn* Spawn::GetTempVariableSpawn(string var) {
  2947. Spawn* ret = 0;
  2948. if (m_tempVariableSpawn.count(var) > 0)
  2949. ret = GetZone()->GetSpawnByID(m_tempVariableSpawn[var]);
  2950. return ret;
  2951. }
  2952. ZoneServer* Spawn::GetTempVariableZone(string var) {
  2953. ZoneServer* ret = 0;
  2954. if (m_tempVariableZone.count(var) > 0)
  2955. ret = m_tempVariableZone[var];
  2956. return ret;
  2957. }
  2958. Item* Spawn::GetTempVariableItem(string var) {
  2959. Item* ret = 0;
  2960. if (m_tempVariableItem.count(var) > 0)
  2961. ret = m_tempVariableItem[var];
  2962. return ret;
  2963. }
  2964. Quest* Spawn::GetTempVariableQuest(string var) {
  2965. Quest* ret = 0;
  2966. if (m_tempVariableQuest.count(var) > 0)
  2967. ret = m_tempVariableQuest[var];
  2968. return ret;
  2969. }
  2970. int8 Spawn::GetTempVariableType(string var) {
  2971. int8 ret = 0;
  2972. if (m_tempVariableTypes.count(var) > 0)
  2973. ret = m_tempVariableTypes[var];
  2974. return ret;
  2975. }
  2976. void Spawn::DeleteTempVariable(string var) {
  2977. int8 type = GetTempVariableType(var);
  2978. switch (type) {
  2979. case 1:
  2980. m_tempVariableSpawn.erase(var);
  2981. break;
  2982. case 2:
  2983. m_tempVariableZone.erase(var);
  2984. break;
  2985. case 3:
  2986. m_tempVariableItem.erase(var);
  2987. break;
  2988. case 4:
  2989. m_tempVariableQuest.erase(var);
  2990. break;
  2991. case 5:
  2992. m_tempVariables.erase(var);
  2993. break;
  2994. }
  2995. m_tempVariableTypes.erase(var);
  2996. }
  2997. Spawn* Spawn::GetRunningTo() {
  2998. return GetZone()->GetSpawnByID(running_to);
  2999. }
  3000. Spawn* Spawn::GetFollowTarget() {
  3001. return GetZone()->GetSpawnByID(m_followTarget);
  3002. }
  3003. void Spawn::CopySpawnAppearance(Spawn* spawn){
  3004. if (!spawn)
  3005. return;
  3006. //This function copies the appearace of the provided spawn to this one
  3007. if (spawn->IsEntity() && IsEntity()){
  3008. memcpy(&((Entity*)this)->features, &((Entity*)spawn)->features, sizeof(CharFeatures));
  3009. memcpy(&((Entity*)this)->equipment, &((Entity*)spawn)->equipment, sizeof(EQ2_Equipment));
  3010. }
  3011. SetSize(spawn->GetSize());
  3012. SetModelType(spawn->GetModelType());
  3013. }
  3014. void Spawn::SetY(float y, bool updateFlags, bool disableYMapCheck)
  3015. {
  3016. SetPos(&appearance.pos.Y, y, updateFlags);
  3017. if (!disableYMapCheck)
  3018. FixZ();
  3019. }
  3020. float Spawn::FindDestGroundZ(glm::vec3 dest, float z_offset)
  3021. {
  3022. float best_z = BEST_Z_INVALID;
  3023. if (GetZone() != nullptr && GetZone()->zonemap != nullptr)
  3024. {
  3025. dest.z += z_offset;
  3026. best_z = zone->zonemap->FindBestZ(dest, nullptr);
  3027. }
  3028. return best_z;
  3029. }
  3030. float Spawn::GetFixedZ(const glm::vec3& destination, int32 z_find_offset) {
  3031. BenchTimer timer;
  3032. timer.reset();
  3033. float new_z = destination.z;
  3034. if (GetZone() != nullptr && zone->zonemap != nullptr) {
  3035. /* if (flymode == GravityBehavior::Flying)
  3036. return new_z;
  3037. */
  3038. /* if (zone->HasWaterMap() && zone->watermap->InLiquid(glm::vec3(m_Position)))
  3039. return new_z;
  3040. */
  3041. /*
  3042. * Any more than 5 in the offset makes NPC's hop/snap to ceiling in small corridors
  3043. */
  3044. new_z = this->FindDestGroundZ(destination, z_find_offset);
  3045. if (new_z != BEST_Z_INVALID) {
  3046. if (new_z < -2000) {
  3047. new_z = GetY();
  3048. }
  3049. }
  3050. auto duration = timer.elapsed();
  3051. LogWrite(MAP__DEBUG, 0, "Map", "Mob::GetFixedZ() ([{%s}]) returned [{%f}] at [{%f}], [{%f}], [{%f}] - Took [{%f}]",
  3052. this->GetName(),
  3053. new_z,
  3054. destination.x,
  3055. destination.y,
  3056. destination.z,
  3057. duration);
  3058. }
  3059. return new_z;
  3060. }
  3061. void Spawn::FixZ(bool forceUpdate) {
  3062. if (IsPlayer() || IsFlying() || !GetZone() || (IsObject() && GetZone()->GetInstanceType() == Instance_Type::PERSONAL_HOUSE_INSTANCE)) {
  3063. return;
  3064. }
  3065. /*
  3066. if (flymode == GravityBehavior::Flying) {
  3067. return;
  3068. }*/
  3069. /*
  3070. if (zone->watermap && zone->watermap->InLiquid(m_Position)) {
  3071. return;
  3072. }*/
  3073. glm::vec3 current_loc(GetX(), GetZ(), GetY());
  3074. float new_z = GetFixedZ(current_loc, 1);
  3075. if (new_z == GetY())
  3076. return;
  3077. if ((new_z > -2000) && new_z != BEST_Z_INVALID) {
  3078. SetY(new_z, forceUpdate, true);
  3079. }
  3080. else {
  3081. LogWrite(MAP__DEBUG, 0, "Map", "[{%s}] is failing to find Z [{%f}]", this->GetName(), std::abs(GetY() - new_z));
  3082. }
  3083. }
  3084. bool Spawn::CheckLoS(Spawn* target)
  3085. {
  3086. glm::vec3 targpos(target->GetX(), target->GetZ(), target->GetY()+1.0f);
  3087. glm::vec3 pos(GetX(), GetZ(), GetY()+1.0f);
  3088. return CheckLoS(pos, targpos);
  3089. }
  3090. bool Spawn::CheckLoS(glm::vec3 myloc, glm::vec3 oloc)
  3091. {
  3092. ZoneServer* zone = GetZone();
  3093. if (zone == NULL || zone->zonemap == NULL || !zone->zonemap->IsMapLoaded())
  3094. return true;
  3095. else
  3096. return zone->zonemap->CheckLoS(myloc, oloc);
  3097. return false;
  3098. }
  3099. void Spawn::CalculateNewFearpoint()
  3100. {
  3101. if (GetZone() && GetZone()->pathing) {
  3102. auto Node = zone->pathing->GetRandomLocation(glm::vec3(GetX(), GetZ(), GetY()));
  3103. if (Node.x != 0.0f || Node.y != 0.0f || Node.z != 0.0f) {
  3104. AddRunningLocation(Node.x, Node.y, Node.z, GetSpeed(), 0, true, true, "", true);
  3105. }
  3106. }
  3107. }
  3108. Item* Spawn::LootItem(int32 id) {
  3109. Item* ret = 0;
  3110. vector<Item*>::iterator itr;
  3111. MLootItems.lock();
  3112. for (itr = loot_items.begin(); itr != loot_items.end(); itr++) {
  3113. if ((*itr)->details.item_id == id) {
  3114. ret = *itr;
  3115. loot_items.erase(itr);
  3116. break;
  3117. }
  3118. }
  3119. MLootItems.unlock();
  3120. return ret;
  3121. }
  3122. int32 Spawn::GetLootItemID() {
  3123. int32 ret = 0;
  3124. vector<Item*>::iterator itr;
  3125. MLootItems.lock();
  3126. for (itr = loot_items.begin(); itr != loot_items.end(); itr++) {
  3127. ret = (*itr)->details.item_id;
  3128. break;
  3129. }
  3130. MLootItems.unlock();
  3131. return ret;
  3132. }
  3133. bool Spawn::HasLootItemID(int32 id) {
  3134. bool ret = false;
  3135. vector<Item*>::iterator itr;
  3136. MLootItems.readlock(__FUNCTION__, __LINE__);
  3137. for (itr = loot_items.begin(); itr != loot_items.end(); itr++) {
  3138. if ((*itr)->details.item_id == id) {
  3139. ret = true;
  3140. break;
  3141. }
  3142. }
  3143. MLootItems.releasereadlock(__FUNCTION__, __LINE__);
  3144. return ret;
  3145. }
  3146. void Spawn::CheckProximities()
  3147. {
  3148. if (!has_spawn_proximities)
  3149. return;
  3150. if (spawn_proximities.size() > 0)
  3151. {
  3152. MutexList<SpawnProximity*>::iterator itr = spawn_proximities.begin();
  3153. while (itr.Next()) {
  3154. SpawnProximity* prox = itr.value;
  3155. map<int32, bool>::iterator spawnsItr;
  3156. for (spawnsItr = prox->spawns_in_proximity.begin(); spawnsItr != prox->spawns_in_proximity.end(); spawnsItr++) {
  3157. Spawn* tmpSpawn = 0;
  3158. if (spawnsItr->first &&
  3159. ((prox->spawn_type == SPAWNPROXIMITY_DATABASE_ID && (tmpSpawn = GetZone()->GetSpawnByDatabaseID(spawnsItr->first)) != 0) ||
  3160. (prox->spawn_type == SPAWNPROXIMITY_LOCATION_ID && (tmpSpawn = GetZone()->GetSpawnByLocationID(spawnsItr->first)) != 0)))
  3161. {
  3162. if (!spawnsItr->second && tmpSpawn->GetDistance(this) <= prox->distance)
  3163. {
  3164. if (prox->in_range_lua_function.size() > 0)
  3165. GetZone()->CallSpawnScript(this, SPAWN_SCRIPT_CUSTOM, tmpSpawn, prox->in_range_lua_function.c_str());
  3166. spawnsItr->second = true;
  3167. }
  3168. else if (spawnsItr->second && tmpSpawn->GetDistance(this) > prox->distance)
  3169. {
  3170. if (prox->leaving_range_lua_function.size() > 0)
  3171. GetZone()->CallSpawnScript(this, SPAWN_SCRIPT_CUSTOM, tmpSpawn, prox->leaving_range_lua_function.c_str());
  3172. spawnsItr->second = false;
  3173. }
  3174. }
  3175. }
  3176. }
  3177. }
  3178. }
  3179. void Spawn::AddSpawnToProximity(int32 spawnValue, SpawnProximityType type)
  3180. {
  3181. if (!has_spawn_proximities)
  3182. return;
  3183. if (spawn_proximities.size() > 0)
  3184. {
  3185. MutexList<SpawnProximity*>::iterator itr = spawn_proximities.begin();
  3186. while (itr.Next()) {
  3187. SpawnProximity* prox = itr->value;
  3188. if (prox->spawn_value == spawnValue && prox->spawn_type == type)
  3189. prox->spawns_in_proximity.insert(make_pair(spawnValue, false));
  3190. }
  3191. }
  3192. }
  3193. void Spawn::RemoveSpawnFromProximity(int32 spawnValue, SpawnProximityType type)
  3194. {
  3195. if (!has_spawn_proximities)
  3196. return;
  3197. if (spawn_proximities.size() > 0)
  3198. {
  3199. MutexList<SpawnProximity*>::iterator itr = spawn_proximities.begin();
  3200. while (itr.Next()) {
  3201. SpawnProximity* prox = itr->value;
  3202. if (prox->spawn_value == spawnValue && prox->spawn_type == type &&
  3203. prox->spawns_in_proximity.count(spawnValue) > 0)
  3204. prox->spawns_in_proximity.erase(spawnValue);
  3205. }
  3206. }
  3207. }
  3208. void Spawn::AddPrimaryEntityCommand(const char* name, float distance, const char* command, const char* error_text, int16 cast_time, int32 spell_visual, bool defaultDenyList, Player* player) {
  3209. EntityCommand* cmd = FindEntityCommand(string(command), true);
  3210. bool newCommand = false;
  3211. if (!cmd)
  3212. {
  3213. newCommand = true;
  3214. cmd = CreateEntityCommand(name, distance, command, error_text, cast_time, spell_visual, !defaultDenyList);
  3215. }
  3216. if (defaultDenyList)
  3217. SetPermissionToEntityCommand(cmd, player, true);
  3218. if (newCommand)
  3219. primary_command_list.push_back(cmd);
  3220. }
  3221. void Spawn::RemovePrimaryEntityCommand(const char* command) {
  3222. vector<EntityCommand*>::iterator itr;
  3223. string tmpStr(command);
  3224. for (itr = primary_command_list.begin(); itr != primary_command_list.end(); itr++) {
  3225. EntityCommand* cmd = *itr;
  3226. if (cmd->command.compare(tmpStr) == 0)
  3227. {
  3228. primary_command_list.erase(itr);
  3229. delete cmd;
  3230. break;
  3231. }
  3232. }
  3233. }
  3234. bool Spawn::SetPermissionToEntityCommand(EntityCommand* command, Player* player, bool permissionValue)
  3235. {
  3236. if (player != NULL)
  3237. {
  3238. map<int32, bool>::iterator itr = command->allow_or_deny.find(player->GetCharacterID());
  3239. if (itr == command->allow_or_deny.end())
  3240. command->allow_or_deny.insert(make_pair(player->GetCharacterID(), permissionValue));
  3241. else if (itr->second != permissionValue)
  3242. itr->second = permissionValue;
  3243. return true;
  3244. }
  3245. return false;
  3246. }