Spawn.cpp 121 KB

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