Spawn.cpp 121 KB

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