Spawn.cpp 111 KB

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