Spawn.cpp 111 KB

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