Spawn.cpp 118 KB

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