Spawn.cpp 113 KB

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