Spawn.cpp 123 KB

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