Spawn.cpp 121 KB

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