Spawn.cpp 124 KB

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