9
3

Spawn.cpp 99 KB

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