12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214 |
- /*
- EQ2Emulator: Everquest II Server Emulator
- Copyright (C) 2007 EQ2EMulator Development Team (http://www.eq2emulator.net)
- This file is part of EQ2Emulator.
- EQ2Emulator is free software: you can redistribute it and/or modify
- it under the terms of the GNU General Public License as published by
- the Free Software Foundation, either version 3 of the License, or
- (at your option) any later version.
- EQ2Emulator is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
- You should have received a copy of the GNU General Public License
- along with EQ2Emulator. If not, see <http://www.gnu.org/licenses/>.
- */
- #include "Spawn.h"
- #include <stdio.h>
- #include "../common/timer.h"
- #include <time.h>
- #include <math.h>
- #include "Entity.h"
- #include "Widget.h"
- #include "Sign.h"
- #include "../common/MiscFunctions.h"
- #include "../common/Log.h"
- #include "Rules/Rules.h"
- #include "World.h"
- #include "LuaInterface.h"
- #include "Zone/SPGrid.h"
- #include "Bots/Bot.h"
- #include "Zone/raycast_mesh.h"
- extern ConfigReader configReader;
- extern RuleManager rule_manager;
- extern World world;
- extern ZoneList zone_list;
- Spawn::Spawn(){
- loot_coins = 0;
- trap_triggered = false;
- trap_state = 0;
- chest_drop_time = 0;
- trap_opened_time = 0;
- group_id = 0;
- size_offset = 0;
- merchant_id = 0;
- merchant_type = 0;
- merchant_min_level = 0;
- merchant_max_level = 0;
- memset(&appearance, 0, sizeof(AppearanceData));
- memset(&basic_info, 0, sizeof(BasicInfoStruct));
- appearance.pos.state = 0x4080;
- appearance.encounter_level =6;
- size = 32;
- appearance.pos.collision_radius = 32;
- id = Spawn::NextID();
- oversized_packet = 0xFF;
- zone = 0;
- spawn_location_id = 0;
- spawn_entry_id = 0;
- spawn_location_spawns_id = 0;
- respawn = 0;
- expire_time = 0;
- expire_offset = 0;
- x_offset = 0;
- y_offset = 0;
- z_offset = 0;
- database_id = 0;
- packet_num = 1;
- changed = false;
- vis_changed = false;
- position_changed = false;
- send_spawn_changes = true;
- info_changed = false;
- appearance.pos.Speed1 = 0;
- last_attacker = 0;
- faction_id = 0;
- running_to = 0;
- tmp_visual_state = -1;
- tmp_action_state = -1;
- transporter_id = 0;
- invulnerable = false;
- spawn_group_list = 0;
- MSpawnGroup = 0;
- movement_locations = 0;
- MMovementLocations = 0;
- target = 0;
- primary_command_list_id = 0;
- secondary_command_list_id = 0;
- is_pet = false;
- m_followTarget = 0;
- following = false;
- req_quests_continued_access = false;
- req_quests_override = 0;
- req_quests_private = false;
- m_illusionModel = 0;
- Cell_Info.CurrentCell = nullptr;
- Cell_Info.CellListIndex = -1;
- m_addedToWorldTimestamp = 0;
- m_spawnAnim = 0;
- m_spawnAnimLeeway = 0;
- m_Update.SetName("Spawn::m_Update");
- m_requiredHistory.SetName("Spawn::m_requiredHistory");
- m_requiredQuests.SetName("Spawn::m_requiredQuests");
- last_heading_angle = 0.0;
- last_grid_update = 0;
- last_location_update = 0.0;
- last_movement_update = Timer::GetCurrentTime2();
- forceMapCheck = false;
- m_followDistance = 0;
- MCommandMutex.SetName("Entity::MCommandMutex");
- has_spawn_proximities = false;
- pickup_item_id = 0;
- pickup_unique_item_id = 0;
- disable_sounds = false;
- has_quests_required = false;
- is_flying_creature = false;
- is_water_creature = false;
- region_map = nullptr;
- current_map = nullptr;
- RegionMutex.SetName("Spawn::RegionMutex");
- pause_timer.Disable();
- m_SpawnMutex.SetName("Spawn::SpawnMutex");
- appearance_equipment_list.SetAppearanceType(1);
- is_transport_spawn = false;
- rail_id = 0;
- is_omitted_by_db_flag = false;
- loot_tier = 0;
- }
- Spawn::~Spawn(){
- vector<Item*>::iterator itr;
- for (itr = loot_items.begin(); itr != loot_items.end(); itr++)
- safe_delete(*itr);
- loot_items.clear();
- RemovePrimaryCommands();
- for(int32 i=0;i<secondary_command_list.size();i++){
- safe_delete(secondary_command_list[i]);
- }
- secondary_command_list.clear();
- RemoveSpawnFromGroup();
- if (MMovementLocations)
- MMovementLocations->writelock(__FUNCTION__, __LINE__);
- if(movement_locations){
- while(movement_locations->size()){
- safe_delete(movement_locations->front());
- movement_locations->pop_front();
- }
- safe_delete(movement_locations);
- }
- if (MMovementLocations)
- MMovementLocations->releasewritelock(__FUNCTION__, __LINE__);
- safe_delete(MMovementLocations);
- MMovementLoop.lock();
- for (int32 i = 0; i < movement_loop.size(); i++)
- safe_delete(movement_loop.at(i));
- movement_loop.clear();
- MMovementLoop.unlock();
- m_requiredHistory.writelock(__FUNCTION__, __LINE__);
- map<int32, LUAHistory*>::iterator lua_itr;
- for (lua_itr = required_history.begin(); lua_itr != required_history.end(); lua_itr++) {
- safe_delete(lua_itr->second);
- }
- required_history.clear();
- m_requiredHistory.releasewritelock(__FUNCTION__, __LINE__);
- m_requiredQuests.writelock(__FUNCTION__, __LINE__);
- map<int32, vector<int16>* >::iterator rq_itr;
- for (rq_itr = required_quests.begin(); rq_itr != required_quests.end(); rq_itr++){
- safe_delete(rq_itr->second);
- }
- required_quests.clear();
- m_requiredQuests.releasewritelock(__FUNCTION__, __LINE__);
- // just in case to make sure data is destroyed
- RemoveSpawnProximities();
-
- Regions.clear();
- }
- void Spawn::RemovePrimaryCommands()
- {
- for (int32 i = 0; i < primary_command_list.size(); i++) {
- safe_delete(primary_command_list[i]);
- }
- primary_command_list.clear();
- }
- void Spawn::InitializeHeaderPacketData(Player* player, PacketStruct* header, int16 index) {
- header->setDataByName("index", index);
- if (GetSpawnAnim() > 0 && Timer::GetCurrentTime2() < (GetAddedToWorldTimestamp() + GetSpawnAnimLeeway())) {
- if (header->GetVersion() >= 57080)
- header->setDataByName("spawn_anim", GetSpawnAnim());
- else
- header->setDataByName("spawn_anim", (int16)GetSpawnAnim());
- }
- else {
- if (header->GetVersion() >= 57080)
- header->setDataByName("spawn_anim", 0xFFFFFFFF);
- else
- header->setDataByName("spawn_anim", 0xFFFF);
- }
- if (primary_command_list.size() > 0){
- if (primary_command_list.size() > 1) {
- header->setArrayLengthByName("command_list", primary_command_list.size());
- for (int32 i = 0; i < primary_command_list.size(); i++) {
- header->setArrayDataByName("command_list_name", primary_command_list[i]->name.c_str(), i);
- header->setArrayDataByName("command_list_max_distance", primary_command_list[i]->distance, i);
- header->setArrayDataByName("command_list_error", primary_command_list[i]->error_text.c_str(), i);
- header->setArrayDataByName("command_list_command", primary_command_list[i]->command.c_str(), i);
- }
- }
- if (header->GetVersion() <= 546) {
- header->setMediumStringByName("default_command", primary_command_list[0]->name.c_str());
- }
- else
- header->setMediumStringByName("default_command", primary_command_list[0]->command.c_str());
- header->setDataByName("max_distance", primary_command_list[0]->distance);
- }
- if (spawn_group_list && MSpawnGroup){
- MSpawnGroup->readlock(__FUNCTION__, __LINE__);
- header->setArrayLengthByName("group_size", spawn_group_list->size());
- vector<Spawn*>::iterator itr;
- int i = 0;
- for (itr = spawn_group_list->begin(); itr != spawn_group_list->end(); itr++, i++){
- int32 idx = 0;
- idx = player->GetIDWithPlayerSpawn((*itr));
- header->setArrayDataByName("group_spawn_id", idx, i);
- }
- MSpawnGroup->releasereadlock(__FUNCTION__, __LINE__);
- }
- header->setDataByName("spawn_id", player->GetIDWithPlayerSpawn(this));
- header->setDataByName("crc", 1);
- header->setDataByName("time_stamp", Timer::GetCurrentTime2());
- }
- void Spawn::InitializeVisPacketData(Player* player, PacketStruct* vis_packet) {
- int16 version = vis_packet->GetVersion();
- if (IsPlayer())
- appearance.pos.grid_id = 0xFFFFFFFF;
- if (IsPlayer())
- vis_packet->setDataByName("player", 1);
- if (version <= 546) {
- vis_packet->setDataByName("targetable", appearance.targetable);
- vis_packet->setDataByName("show_name", appearance.display_name);
- vis_packet->setDataByName("attackable", appearance.attackable);
- if(appearance.attackable == 1)
- vis_packet->setDataByName("attackable_icon", 1);
- if (IsPlayer()) {
- if (((Player*)this)->IsGroupMember(player))
- vis_packet->setDataByName("group_member", 1);
- }
- }
- if (appearance.targetable == 1 || appearance.show_level == 1 || appearance.display_name == 1) {
- if (!IsGroundSpawn()) {
- int8 arrow_color = ARROW_COLOR_WHITE;
- sint8 npc_con = player->GetFactions()->GetCon(faction_id);
- if (IsPlayer() && !((Player*)this)->CanSeeInvis(player))
- npc_con = 0;
- else if (!IsPlayer() && IsEntity() && !((Entity*)this)->CanSeeInvis(player))
- npc_con = 0;
- if (appearance.attackable == 1)
- arrow_color = player->GetArrowColor(GetLevel());
- if (version <= 283) {
- if (GetMerchantID() > 0)
- arrow_color += 7;
- else {
- if (primary_command_list.size() > 0) {
- int16 len = strlen(primary_command_list[0]->command.c_str());
- if(len >= 4 && strncmp(primary_command_list[0]->command.c_str(), "bank", 4) == 0)
- arrow_color += 14;
- else if (len >= 4 && strncmp(primary_command_list[0]->command.c_str(), "hail", 4) == 0)
- arrow_color += 21;
- else if (len >= 6 && strncmp(primary_command_list[0]->command.c_str(), "attack", 6) == 0) {
- if (arrow_color > 5)
- arrow_color = 34;
- else
- arrow_color += 29;
- }
- }
- }
- }
- vis_packet->setDataByName("arrow_color", arrow_color);
- if (appearance.attackable == 0)
- vis_packet->setDataByName("locked_no_loot", 1);
- else
- vis_packet->setDataByName("locked_no_loot", appearance.locked_no_loot);
- if (player->GetArrowColor(GetLevel()) == ARROW_COLOR_GRAY)
- if (npc_con == -4)
- npc_con = -3;
- vis_packet->setDataByName("npc_con", npc_con);
- if (appearance.attackable == 1 && IsNPC() && (player->GetFactions()->GetCon(faction_id) <= -4 || ((NPC*)this)->Brain()->GetHate(player) > 1)) {
- vis_packet->setDataByName("npc_hate", ((NPC*)this)->Brain()->GetHatePercentage(player));
- vis_packet->setDataByName("show_difficulty_arrows", 1);
- }
- int8 quest_flag = player->CheckQuestFlag(this);
- if (version < 1188 && quest_flag >= 16)
- quest_flag = 1;
- vis_packet->setDataByName("quest_flag", quest_flag);
- if (player->CheckQuestsKillUpdate(this, false)) {
- vis_packet->setDataByName("name_quest_icon", 1);
- }
- }
- }
- int8 vis_flags = 0;
- if (MeetsSpawnAccessRequirements(player)) {
- if (appearance.attackable == 1)
- vis_flags += 64; //attackable icon
- if (appearance.show_level == 1)
- vis_flags += 32;
- if (appearance.display_name == 1)
- vis_flags += 16;
- if (IsPlayer() || appearance.targetable == 1)
- vis_flags += 4;
- if (appearance.show_command_icon == 1)
- vis_flags += 2;
- if (this == player) {
- //if (version <= 283) {
- // vis_flags = 1;
- //}
- //else
- vis_flags += 1;
- }
- }
- else if (req_quests_override > 0)
- {
- //Check to see if there's an override value set
- vis_flags = req_quests_override & 0xFF;
- }
- if (player->HasGMVision())
- {
- if ((vis_flags & 16) == 0 && appearance.display_name == 0)
- vis_flags += 16;
- if ((vis_flags & 4) == 0)
- vis_flags += 4;
- }
- if (version <= 546 && (vis_flags > 1 || appearance.display_hand_icon > 0)) //interactable
- vis_flags = 1;
- vis_packet->setDataByName("vis_flags", vis_flags);
- if (MeetsSpawnAccessRequirements(player)) {
- vis_packet->setDataByName("hand_flag", appearance.display_hand_icon);
- }
- else {
- if ((req_quests_override & 256) > 0)
- vis_packet->setDataByName("hand_flag", 1);
- }
- if (version == 546 && GetMerchantID() > 0) {
- vis_packet->setDataByName("guild", "<Merchant>");
- }
- }
- void Spawn::InitializeFooterPacketData(Player* player, PacketStruct* footer) {
- if (IsWidget()){
- Widget* widget = (Widget*)this;
- if (widget->GetMultiFloorLift()) {
- footer->setDataByName("widget_x", widget->GetX());
- footer->setDataByName("widget_y", widget->GetY());
- footer->setDataByName("widget_z", widget->GetZ());
- }
- else {
- footer->setDataByName("widget_x", widget->GetWidgetX());
- footer->setDataByName("widget_y", widget->GetWidgetY());
- footer->setDataByName("widget_z", widget->GetWidgetZ());
- }
- footer->setDataByName("widget_id", widget->GetWidgetID());
- footer->setDataByName("unknown3c", 6);
- }
- else if (IsSign()){
- Sign* sign = (Sign*)this;
- footer->setDataByName("widget_id", sign->GetWidgetID());
- footer->setDataByName("widget_x", sign->GetWidgetX());
- footer->setDataByName("widget_y", sign->GetWidgetY());
- footer->setDataByName("widget_z", sign->GetWidgetZ());
- footer->setDataByName("unknown2b", 6);
- if (sign->GetSignTitle())
- footer->setMediumStringByName("title", sign->GetSignTitle());
- if (sign->GetSignDescription())
- footer->setMediumStringByName("description", sign->GetSignDescription());
- footer->setDataByName("sign_distance", sign->GetSignDistance());
- footer->setDataByName("show", 1);
- }
- if ( IsPlayer())
- footer->setDataByName("is_player", 1);
- if (strlen(appearance.name) < 1)
- strncpy(appearance.name,to_string(GetID()).c_str(),128);
- footer->setMediumStringByName("name", appearance.name);
- footer->setMediumStringByName("guild", appearance.sub_title);
- footer->setMediumStringByName("prefix", appearance.prefix_title);
- footer->setMediumStringByName("suffix", appearance.suffix_title);
- footer->setMediumStringByName("last_name", appearance.last_name);
- if (appearance.attackable == 0 && GetLevel() > 0)
- footer->setDataByName("spawn_type", 1);
- else if (appearance.attackable == 0)
- footer->setDataByName("spawn_type", 6);
- else
- footer->setDataByName("spawn_type", 3);
- }
- EQ2Packet* Spawn::spawn_serialize(Player* player, int16 version, int16 offset, int32 value, int16 offset2, int16 offset3, int16 offset4, int32 value2) {
- // 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
- if (IsNPC() && ((NPC*)this)->IsPet() && ((NPC*)this)->GetOwner()->IsPlayer() && player == ((NPC*)this)->GetOwner() && player->GetInfoStruct()->get_pet_id() == 0) {
- ((Player*)((NPC*)this)->GetOwner())->GetInfoStruct()->set_pet_id(player->spawn_id);
- player->SetCharSheetChanged(true);
- }
- m_Update.writelock(__FUNCTION__, __LINE__);
- int16 index = 0;
- if ((index = player->GetIndexForSpawn(this)) > 0) {
- player->SetSpawnMapIndex(this, index);
- }
- else {
- index = player->SetSpawnMapAndIndex(this);
- }
- // Jabantiz - [Bug] Client Crash on Revive
- if (player->GetIDWithPlayerSpawn(this) == 0) {
- player->SetSpawnMap(this);
- }
- PacketStruct* header = player->GetSpawnHeaderStruct();
- header->ResetData();
- InitializeHeaderPacketData(player, header, index);
- PacketStruct* footer = 0;
- if (IsWidget())
- footer = player->GetWidgetFooterStruct();
- else if (IsSign())
- footer = player->GetSignFooterStruct();
- else if (version > 546)
- footer = player->GetSpawnFooterStruct();
- if (footer) {
- footer->ResetData();
- InitializeFooterPacketData(player, footer);
- }
- PacketStruct* vis_struct = player->GetSpawnVisStruct();
- PacketStruct* info_struct = player->GetSpawnInfoStruct();
- PacketStruct* pos_struct = player->GetSpawnPosStruct();
- player->info_mutex.writelock(__FUNCTION__, __LINE__);
- player->vis_mutex.writelock(__FUNCTION__, __LINE__);
- player->pos_mutex.writelock(__FUNCTION__, __LINE__);
- info_struct->ResetData();
- InitializeInfoPacketData(player, info_struct);
-
- vis_struct->ResetData();
- InitializeVisPacketData(player, vis_struct);
- pos_struct->ResetData();
- InitializePosPacketData(player, pos_struct);
- if (version <= 283) {
- if (offset == 777) {
- info_struct->setDataByName("name", "This is a really long name\n");
- info_struct->setDataByName("last_name", "This is a really long LAST name\n");
- info_struct->setDataByName("name_suffix", "This is a really long SUFFIX\n");
- info_struct->setDataByName("name_prefix", "This is a really long PREFIX\n");
- info_struct->setDataByName("unknown", "This is a really long UNKNOWN\n");
- info_struct->setDataByName("second_suffix", "This is a really long 2nd SUFFIX\n");
- }
- //info_struct->setDataByName("unknown2", 3, 0); // level
- //info_struct->setDataByName("unknown2", 1, 1); //unknown, two down arrows
- //info_struct->setDataByName("unknown2", 1, 2); //unknown
- //info_struct->setDataByName("unknown2", 1, 3); //unknown
- //info_struct->setDataByName("unknown2", 1, 4); //solo fight
- //info_struct->setDataByName("unknown2", 1, 5); //unknown
- //info_struct->setDataByName("unknown2", 1, 6); //unknown
- //info_struct->setDataByName("unknown2", 1, 7); //merchant
- //info_struct->setDataByName("unknown2", 1, 8); //unknown
- //info_struct->setDataByName("unknown2", 1, 9); //unknown
- //info_struct->setDataByName("unknown2", 1, 10);
- //112: 00 00 00 01 02 03 04 05 - 06 07 08 09 0A 00 00 00 | ................ merchant, x4
- //112: 00 00 00 01 02 03 04 05 - 00 00 00 00 00 00 00 00 | ................ x4, epic, indifferent
- //info_struct->setDataByName("body_size", 42);
- //for(int i=0;i<8;i++)
- // info_struct->setDataByName("persistent_spell_visuals", 329, i);
- //info_struct->setDataByName("persistent_spell_levels", 20);
- }
- string* vis_data = vis_struct->serializeString();
- string* pos_data = pos_struct->serializeString();
- string* info_data = info_struct->serializeString();
- int16 part2_size = pos_data->length() + vis_data->length() + info_data->length();
- uchar* part2 = new uchar[part2_size];
- player->AddSpawnPosPacketForXOR(id, (uchar*)pos_data->c_str(), pos_data->length());
- player->AddSpawnVisPacketForXOR(id, (uchar*)vis_data->c_str(), vis_data->length());
- player->AddSpawnInfoPacketForXOR(id, (uchar*)info_data->c_str(), info_data->length());
- uchar* ptr = part2;
- memcpy(ptr, pos_data->c_str(), pos_data->length());
- ptr += pos_data->length();
- memcpy(ptr, vis_data->c_str(), vis_data->length());
- ptr += vis_data->length();
- memcpy(ptr, info_data->c_str(), info_data->length());
- player->pos_mutex.releasewritelock(__FUNCTION__, __LINE__);
- player->info_mutex.releasewritelock(__FUNCTION__, __LINE__);
- player->vis_mutex.releasewritelock(__FUNCTION__, __LINE__);
- string* part1 = header->serializeString();
- string* part3 = 0;
- if (footer)
- part3 = footer->serializeString();
- //uchar blah7[] = {0x01,0x01,0x00,0x00,0x01,0x01,0x00,0x01,0x01,0x00 };
- //uchar blah7[] = { 0x03,0x01,0x00,0x01,0x01,0x01,0x00,0x01,0x01,0x00 }; base
- //uchar blah7[] = { 0x03,0x00,0x00,0x01,0x01,0x01,0x00,0x01,0x01,0x00 }; //no change
- //uchar blah7[] = { 0x03,0x00,0x00,0x00,0x01,0x01,0x00,0x01,0x01,0x00 }; //blue instead of green
- //uchar blah7[] = { 0x03,0x00,0x00,0x00,0x00,0x01,0x00,0x01,0x01,0x00 }; //no change
- //uchar blah7[] = { 0x03,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x01,0x00 }; //not selectable
- //uchar blah7[] = { 0x03,0x00,0x00,0x00,0x00,0x01,0x00,0x00,0x01,0x00 }; //no change
- //uchar blah7[] = { 0x03,0x00,0x00,0x00,0x00,0x01,0x00,0x00,0x00,0x00 }; //no longer have the two down arrows
- //uchar blah7[] = { 0x01,0x00,0x00,0x00,0x00,0x01,0x00,0x00,0x00,0x00 }; //arrow color green instead of white/gray
- //memcpy(part2 + 77, blah7, sizeof(blah7));
- //DumpPacket(part2, 885);
- if (offset > 0) {
- if (offset4 > 0 && offset4 >= offset3) {
- uchar* ptr2 = (uchar*)part2;
- ptr2 += offset3;
- while (offset4 >= offset3) {
- int8 jumpsize = 1;
- if (value2 > 0xFFFF) {
- memcpy(ptr2, (uchar*)&value2, 4);
- jumpsize = 4;
- }
- else if (value2 > 0xFF) {
- memcpy(ptr2, (uchar*)&value2, 2);
- jumpsize = 2;
- }
- else {
- memcpy(ptr2, (uchar*)&value2, 1);
- jumpsize = 1;
- }
- ptr2 += jumpsize;
- offset4 -= jumpsize;
- }
- }
- if (offset2 > 0 && offset2 >= offset) {
- uchar* ptr2 = (uchar*)part2;
- ptr2 += offset;
- while (offset2 >= offset) {
- int8 jumpsize = 1;
- if (value > 0xFFFF) {
- memcpy(ptr2, (uchar*)&value, 4);
- jumpsize = 4;
- }
- else if (value > 0xFF) {
- memcpy(ptr2, (uchar*)&value, 2);
- jumpsize = 2;
- }
- else
- memcpy(ptr2, (uchar*)&value, 1);
- ptr2 += jumpsize;
- offset2 -= jumpsize;
- }
- }
- else {
- uchar* ptr2 = (uchar*)part2;
- ptr2 += offset;
- if (value > 0xFFFF)
- memcpy(ptr2, (uchar*)&value, 4);
- else if (value > 0xFF)
- memcpy(ptr2, (uchar*)&value, 2);
- else
- memcpy(ptr2, (uchar*)&value, 1);
- }
- cout << "setting offset: " << offset << " to: " << value << endl;
- }
- if (offset > 0)
- DumpPacket(part2, part2_size);
- uchar tmp[1100];
- bool reverse = (version > 283);
- part2_size = Pack(tmp, part2, part2_size, 1100, version, reverse);
- int32 total_size = part1->length() + part2_size + 3;
- if (part3)
- total_size += part3->length();
- int32 final_packet_size = total_size + 1;
- if (version > 283)
- final_packet_size += 3;
- else {
- if (final_packet_size >= 255) {
- final_packet_size += 2;
- }
- }
- uchar* final_packet = new uchar[final_packet_size];
- ptr = final_packet;
- if (version <= 283) {
- if ((final_packet_size - total_size) > 1) {
- memcpy(ptr, &oversized_packet, sizeof(oversized_packet));
- ptr += sizeof(oversized_packet);
- memcpy(ptr, &total_size, 2);
- ptr += 2;
- }
- else {
- memcpy(ptr, &total_size, 1);
- ptr += 1;
- }
- }
- else {
- memcpy(ptr, &total_size, sizeof(total_size));
- ptr += sizeof(total_size);
- }
- memcpy(ptr, &oversized_packet, sizeof(oversized_packet));
- ptr += sizeof(oversized_packet);
- int16 opcode = 0;
- if(IsWidget())
- opcode = EQOpcodeManager[GetOpcodeVersion(version)]->EmuToEQ(OP_EqCreateWidgetCmd);
- else if(IsSign())
- opcode = EQOpcodeManager[GetOpcodeVersion(version)]->EmuToEQ(OP_EqCreateSignWidgetCmd);
- else
- opcode = EQOpcodeManager[GetOpcodeVersion(version)]->EmuToEQ(OP_EqCreateGhostCmd);
- memcpy(ptr, &opcode, sizeof(opcode));
- ptr += sizeof(opcode);
- memcpy(ptr, part1->c_str(), part1->length());
- ptr += part1->length();
- memcpy(ptr, tmp, part2_size);
- ptr += part2_size;
- if (part3)
- memcpy(ptr, part3->c_str(), part3->length());
- delete[] part2;
- // printf("%s (%i): p1: %i, p2:% i (%i), p3:% i, ts: %i\n", GetName(), GetID(), part1->length(), part2_size, origPart2Size, part3->length(), total_size);
- EQ2Packet* ret = new EQ2Packet(OP_ClientCmdMsg, final_packet, final_packet_size);
- delete[] final_packet;
- m_Update.releasewritelock(__FUNCTION__, __LINE__);
- return ret;
- }
- uchar* Spawn::spawn_info_changes(Player* player, int16 version){
- int16 index = player->GetIndexForSpawn(this);
- PacketStruct* packet = player->GetSpawnInfoStruct();
- player->info_mutex.writelock(__FUNCTION__, __LINE__);
- packet->ResetData();
- InitializeInfoPacketData(player, packet);
- string* data = packet->serializeString();
- int32 size = data->length();
- uchar* xor_info_packet = player->GetTempInfoPacketForXOR();
- if (!xor_info_packet || size != player->GetTempInfoXorSize())
- {
- LogWrite(ZONE__DEBUG, 0, "Zone", "InstantiateInfoPacket: %i, %i", size, player->GetTempInfoXorSize());
- safe_delete(xor_info_packet);
- xor_info_packet = player->SetTempInfoPacketForXOR(size);
- }
- uchar* orig_packet = player->GetSpawnInfoPacketForXOR(id);
- if(orig_packet){
- memcpy(xor_info_packet, (uchar*)data->c_str(), size);
- Encode(xor_info_packet, orig_packet, size);
- }
- bool changed = false;
- for (int i = 0; i < size; ++i) {
- if (xor_info_packet[i]) {
- changed = true;
- break;
- }
- }
- if (!changed) {
- player->info_mutex.releasewritelock(__FUNCTION__, __LINE__);
- return nullptr;
- }
- uchar* tmp = new uchar[size + 10];
- size = Pack(tmp, xor_info_packet, size, size, version);
- player->info_mutex.releasewritelock(__FUNCTION__, __LINE__);
- int32 orig_size = size;
- size-=sizeof(int32);
- size+=CheckOverLoadSize(index);
- info_packet_size = size + CheckOverLoadSize(size);
- uchar* tmp2 = new uchar[info_packet_size];
- uchar* ptr = tmp2;
- ptr += DoOverLoad(size, ptr);
- ptr += DoOverLoad(index, ptr);
- memcpy(ptr, tmp+sizeof(int32), orig_size - sizeof(int32));
- delete[] tmp;
- return tmp2;
- }
- uchar* Spawn::spawn_vis_changes(Player* player, int16 version){
- PacketStruct* vis_struct = player->GetSpawnVisStruct();
- int16 index = player->GetIndexForSpawn(this);
- player->vis_mutex.writelock(__FUNCTION__, __LINE__);
- uchar* orig_packet = player->GetSpawnVisPacketForXOR(id);
- vis_struct->ResetData();
- InitializeVisPacketData(player, vis_struct);
- string* data = vis_struct->serializeString();
- int32 size = data->length();
- uchar* xor_vis_packet = player->GetTempVisPacketForXOR();
- if (!xor_vis_packet || size != player->GetTempVisXorSize())
- {
- LogWrite(ZONE__DEBUG, 0, "Zone", "InstantiateVisPacket: %i, %i", size, player->GetTempVisXorSize());
- safe_delete(xor_vis_packet);
- xor_vis_packet = player->SetTempVisPacketForXOR(size);
- }
- if(orig_packet){
- memcpy(xor_vis_packet, (uchar*)data->c_str(), size);
- Encode(xor_vis_packet, orig_packet, size);
- }
- bool changed = false;
- for (int i = 0; i < size; ++i) {
- if (xor_vis_packet[i]) {
- changed = true;
- break;
- }
- }
- if (!changed) {
- player->vis_mutex.releasewritelock(__FUNCTION__, __LINE__);
- return nullptr;
- }
- uchar* tmp = new uchar[size + 10];
- size = Pack(tmp, xor_vis_packet, size, size, version);
- player->vis_mutex.releasewritelock(__FUNCTION__, __LINE__);
- int32 orig_size = size;
- size-=sizeof(int32);
- size+=CheckOverLoadSize(index);
- vis_packet_size = size + CheckOverLoadSize(size);
- uchar* tmp2 = new uchar[vis_packet_size];
- uchar* ptr = tmp2;
- ptr += DoOverLoad(size, ptr);
- ptr += DoOverLoad(index, ptr);
- memcpy(ptr, tmp+sizeof(int32), orig_size - sizeof(int32));
- delete[] tmp;
- return tmp2;
- }
- uchar* Spawn::spawn_pos_changes(Player* player, int16 version) {
- int16 index = player->GetIndexForSpawn(this);
- PacketStruct* packet = player->GetSpawnPosStruct();
- player->pos_mutex.writelock(__FUNCTION__, __LINE__);
- uchar* orig_packet = player->GetSpawnPosPacketForXOR(id);
- packet->ResetData();
- InitializePosPacketData(player, packet, true);
- string* data = packet->serializeString();
- int32 size = data->length();
- uchar* xor_pos_packet = player->GetTempPosPacketForXOR();
- if (!xor_pos_packet || size != player->GetTempPosXorSize())
- {
- LogWrite(ZONE__DEBUG, 0, "Zone", "InstantiatePosPacket: %i, %i", size, player->GetTempPosXorSize());
- safe_delete(xor_pos_packet);
- xor_pos_packet = player->SetTempPosPacketForXOR(size);
- }
- if(orig_packet){
- memcpy(xor_pos_packet, (uchar*)data->c_str(), size);
- Encode(xor_pos_packet, orig_packet, size);
- }
- bool changed = false;
- for (int i = 0; i < size; ++i) {
- if (xor_pos_packet[i]) {
- changed = true;
- break;
- }
- }
- if (!changed) {
- player->pos_mutex.releasewritelock(__FUNCTION__, __LINE__);
- return nullptr;
- }
- uchar* tmp;
- if (IsPlayer() && version > 283)
- tmp = new uchar[size + 14];
- else
- tmp = new uchar[size + 10];
- size = Pack(tmp, xor_pos_packet, size, size, version);
- player->pos_mutex.releasewritelock(__FUNCTION__, __LINE__);
- int32 orig_size = size;
- // Needed for CoE+ clients
- if (version >= 1188)
- size += 1;
- if(IsPlayer() && version > 546)
- size += 4;
- size-=sizeof(int32);
- size+=CheckOverLoadSize(index);
- pos_packet_size = size + CheckOverLoadSize(size);
- uchar* tmp2 = new uchar[pos_packet_size];
- uchar* ptr = tmp2;
- ptr += DoOverLoad(size, ptr);
- ptr += DoOverLoad(index, ptr);
- // extra byte in coe+ clients, 0 for NPC's 1 for Players
- int8 x = 0;
- if (IsPlayer() && version > 546) {
- if (version >= 1188) {
- // set x to 1 and add it to the packet
- x = 1;
- memcpy(ptr, &x, sizeof(int8));
- ptr += sizeof(int8);
- }
- int32 now = Timer::GetCurrentTime2();
- memcpy(ptr, &now, sizeof(int32));
- ptr += sizeof(int32);
- }
- else if (version >= 1188) {
- // add x to packet
- memcpy(ptr, &x, sizeof(int8));
- ptr += sizeof(int8);
- }
- memcpy(ptr, tmp+sizeof(int32), orig_size - sizeof(int32));
- delete[] tmp;
- return tmp2;
- }
- EQ2Packet* Spawn::player_position_update_packet(Player* player, int16 version){
- if(!player || player->IsPlayer() == false){
- LogWrite(SPAWN__ERROR, 0, "Spawn", "Error: Called player_position_update_packet without player!");
- return 0;
- }
- else if(IsPlayer() == false){
- LogWrite(SPAWN__ERROR, 0, "Spawn", "Error: Called player_position_update_packet from spawn!");
- return 0;
- }
- static const int8 info_size = 1;
- static const int8 vis_size = 1;
- m_Update.writelock(__FUNCTION__, __LINE__);
- uchar* pos_changes = spawn_pos_changes(player, version);
- if (pos_changes == NULL )
- {
- m_Update.releasewritelock(__FUNCTION__, __LINE__);
- return NULL;
- }
- int32 tmp_pos_packet_size = pos_packet_size;
- m_Update.releasewritelock(__FUNCTION__, __LINE__);
- int32 size = info_size + tmp_pos_packet_size + vis_size + 8;
- if (version >= 284)
- size += 3;
- else if (version <= 283 && size >= 255) {//1 byte to 3 for overloaded val
- size += 2;
- }
- static const int8 oversized = 255;
- int16 opcode_val = EQOpcodeManager[GetOpcodeVersion(version)]->EmuToEQ(OP_EqUpdateGhostCmd);
- uchar* tmp = new uchar[size];
- memset(tmp, 0, size);
- uchar* ptr = tmp;
- if (version >= 284) {
- size -= 4;
- memcpy(ptr, &size, sizeof(int32));
- size += 4;
- ptr += sizeof(int32);
- }
- else {
- if (size >= 255) {
- memcpy(ptr, &oversized, sizeof(int8));
- ptr += sizeof(int8);
- size -= 3;
- memcpy(ptr, &size, sizeof(int16));
- size += 3;
- ptr += 3;
- }
- else {
- size -= 1;
- memcpy(ptr, &size, sizeof(int8));
- ptr += sizeof(int8);
- size += 1;
- ptr += 1;
- }
- }
- memcpy(ptr, &oversized, sizeof(int8));
- ptr += sizeof(int8);
- memcpy(ptr, &opcode_val, sizeof(int16));
- ptr += sizeof(int16);
- if (version <= 283) {
- int32 timestamp = Timer::GetCurrentTime2();
- memcpy(ptr, ×tamp, sizeof(int32));
- }
- ptr += sizeof(int32);
- ptr += info_size;
- memcpy(ptr, pos_changes, tmp_pos_packet_size);
- EQ2Packet* ret_packet = new EQ2Packet(OP_ClientCmdMsg, tmp, size);
- DumpPacket(ret_packet);
- delete[] tmp;
- delete[] pos_changes;
- return ret_packet;
- }
- EQ2Packet* Spawn::spawn_update_packet(Player* player, int16 version, bool override_changes, bool override_vis_changes){
- if(!player || player->IsPlayer() == false){
- LogWrite(SPAWN__ERROR, 0, "Spawn", "Error: Called spawn_update_packet without player!");
- return 0;
- }
- else if((IsPlayer() && info_changed == false && vis_changed == false) || (info_changed == false && vis_changed == false && position_changed == false)){
- if(!override_changes && !override_vis_changes)
- return 0;
- }
- static const uchar null_byte = 0;
- uchar* info_changes = 0;
- uchar* pos_changes = 0;
- uchar* vis_changes = 0;
- static const int8 oversized = 255;
- int16 opcode_val = EQOpcodeManager[GetOpcodeVersion(version)]->EmuToEQ(OP_EqUpdateGhostCmd);
- int16 tmp_info_packet_size;
- int16 tmp_vis_packet_size;
- int16 tmp_pos_packet_size;
- //We need to lock these variables up to make this thread safe
- m_Update.writelock(__FUNCTION__, __LINE__);
- //These variables are set in the spawn_info_changes, pos and vis changes functions
- info_packet_size = 1;
- pos_packet_size = 1;
- vis_packet_size = 1;
- if (info_changed || override_changes)
- info_changes = spawn_info_changes(player, version);
- if ((position_changed || override_changes) && IsPlayer() == false)
- pos_changes = spawn_pos_changes(player, version);
- if (vis_changed || override_changes || override_vis_changes)
- vis_changes = spawn_vis_changes(player, version);
- tmp_info_packet_size = info_packet_size;
- tmp_pos_packet_size = pos_packet_size;
- tmp_vis_packet_size = vis_packet_size;
- int32 size = info_packet_size + pos_packet_size + vis_packet_size + 8;
- if (version >= 284)
- size += 3;
- else if (version <= 283 && size >= 255) {//1 byte to 3 for overloaded val
- size += 2;
- }
- uchar* tmp = new uchar[size];
- memset(tmp, 0, size);
- uchar* ptr = tmp;
- if (version >= 284) {
- size -= 4;
- memcpy(ptr, &size, sizeof(int32));
- size += 4;
- ptr += sizeof(int32);
- }
- else {
- if (size >= 255) {
- memcpy(ptr, &oversized, sizeof(int8));
- ptr += sizeof(int8);
- size -= 3;
- memcpy(ptr, &size, sizeof(int16));
- size += 3;
- ptr += 3;
- }
- else {
- size -= 1;
- memcpy(ptr, &size, sizeof(int8));
- ptr += sizeof(int8);
- size += 1;
- }
- }
- memcpy(ptr, &oversized, sizeof(int8));
- ptr += sizeof(int8);
- memcpy(ptr, &opcode_val, sizeof(int16));
- ptr += sizeof(int16);
- if (IsPlayer() == false || version <= 546) { //this isnt sent for player updates, it is sent on position update
- //int32 time = Timer::GetCurrentTime2();
- packet_num = Timer::GetCurrentTime2();
- memcpy(ptr, &packet_num, sizeof(int32));
- }
- ptr += sizeof(int32);
- memcpy(ptr, info_changes ? info_changes : &null_byte, tmp_info_packet_size);
- ptr += info_packet_size;
- memcpy(ptr, pos_changes ? pos_changes : &null_byte, tmp_pos_packet_size);
- ptr += pos_packet_size;
- memcpy(ptr, vis_changes ? vis_changes : &null_byte, tmp_vis_packet_size);
- EQ2Packet* ret_packet = 0;
- if(info_packet_size + pos_packet_size + vis_packet_size > 0)
- ret_packet = new EQ2Packet(OP_ClientCmdMsg, tmp, size);
- delete[] tmp;
- safe_delete_array(info_changes);
- safe_delete_array(vis_changes);
- safe_delete_array(pos_changes);
- m_Update.releasewritelock(__FUNCTION__, __LINE__);
- return ret_packet;
- }
- uchar* Spawn::spawn_info_changes_ex(Player* player, int16 version) {
- int16 index = player->GetIndexForSpawn(this);
- PacketStruct* packet = player->GetSpawnInfoStruct();
- player->info_mutex.writelock(__FUNCTION__, __LINE__);
- packet->ResetData();
- InitializeInfoPacketData(player, packet);
- string* data = packet->serializeString();
- int32 size = data->length();
- uchar* xor_info_packet = player->GetTempInfoPacketForXOR();
- if (!xor_info_packet || size != player->GetTempInfoXorSize()) {
- LogWrite(ZONE__DEBUG, 0, "Zone", "InstantiateInfoExPacket: %i, %i", size, player->GetTempInfoXorSize());
- safe_delete(xor_info_packet);
- xor_info_packet = player->SetTempInfoPacketForXOR(size);
- }
- uchar* orig_packet = player->GetSpawnInfoPacketForXOR(id);
- if (orig_packet) {
- //if (!IsPlayer() && this->EngagedInCombat())
- //packet->PrintPacket();
- memcpy(xor_info_packet, (uchar*)data->c_str(), size);
- Encode(xor_info_packet, orig_packet, size);
- }
- bool changed = false;
- for (int i = 0; i < size; ++i) {
- if (xor_info_packet[i]) {
- changed = true;
- break;
- }
- }
- if (!changed) {
- player->info_mutex.releasewritelock(__FUNCTION__, __LINE__);
- return nullptr;
- }
- uchar* tmp = new uchar[size + 10];
- size = Pack(tmp, xor_info_packet, size, size, version);
- player->info_mutex.releasewritelock(__FUNCTION__, __LINE__);
- int32 orig_size = size;
- size -= sizeof(int32);
- size += CheckOverLoadSize(index);
- info_packet_size = size;
- uchar* tmp2 = new uchar[size];
- uchar* ptr = tmp2;
- ptr += DoOverLoad(index, ptr);
- memcpy(ptr, tmp + sizeof(int32), orig_size - sizeof(int32));
- delete[] tmp;
- return move(tmp2);
- }
- uchar* Spawn::spawn_vis_changes_ex(Player* player, int16 version) {
- PacketStruct* vis_struct = player->GetSpawnVisStruct();
- int16 index = player->GetIndexForSpawn(this);
- player->vis_mutex.writelock(__FUNCTION__, __LINE__);
- uchar* orig_packet = player->GetSpawnVisPacketForXOR(id);
- vis_struct->ResetData();
- InitializeVisPacketData(player, vis_struct);
- string* data = vis_struct->serializeString();
- int32 size = data->length();
- uchar* xor_vis_packet = player->GetTempVisPacketForXOR();
- if (!xor_vis_packet || size != player->GetTempVisXorSize()) {
- LogWrite(ZONE__DEBUG, 0, "Zone", "InstantiateVisExPacket: %i, %i", size, player->GetTempVisXorSize());
- safe_delete(xor_vis_packet);
- xor_vis_packet = player->SetTempVisPacketForXOR(size);
- }
- if (orig_packet) {
- //if (!IsPlayer() && this->EngagedInCombat())
- // vis_struct->PrintPacket();
- memcpy(xor_vis_packet, (uchar*)data->c_str(), size);
- Encode(xor_vis_packet, orig_packet, size);
- }
- bool changed = false;
- for (int i = 0; i < size; ++i) {
- if (xor_vis_packet[i]) {
- changed = true;
- break;
- }
- }
- if (!changed) {
- player->vis_mutex.releasewritelock(__FUNCTION__, __LINE__);
- return nullptr;
- }
- uchar* tmp = new uchar[size + 10];
- size = Pack(tmp, xor_vis_packet, size, size, version);
- player->vis_mutex.releasewritelock(__FUNCTION__, __LINE__);
- int32 orig_size = size;
- size -= sizeof(int32);
- size += CheckOverLoadSize(index);
- vis_packet_size = size;
- uchar* tmp2 = new uchar[size];
- uchar* ptr = tmp2;
- ptr += DoOverLoad(index, ptr);
- memcpy(ptr, tmp + sizeof(int32), orig_size - sizeof(int32));
- delete[] tmp;
- return move(tmp2);
- }
- uchar* Spawn::spawn_pos_changes_ex(Player* player, int16 version) {
- int16 index = player->GetIndexForSpawn(this);
- PacketStruct* packet = player->GetSpawnPosStruct();
- player->pos_mutex.writelock(__FUNCTION__, __LINE__);
- uchar* orig_packet = player->GetSpawnPosPacketForXOR(id);
- packet->ResetData();
- InitializePosPacketData(player, packet);
- string* data = packet->serializeString();
- int32 size = data->length();
- uchar* xor_pos_packet = player->GetTempPosPacketForXOR();
- if (!xor_pos_packet || size != player->GetTempPosXorSize()) {
- LogWrite(ZONE__DEBUG, 0, "Zone", "InstantiatePosExPacket: %i, %i", size, player->GetTempPosXorSize());
- safe_delete(xor_pos_packet);
- xor_pos_packet = player->SetTempPosPacketForXOR(size);
- }
- if (orig_packet) {
- //if (!IsPlayer() && this->EngagedInCombat())
- // packet->PrintPacket();
- memcpy(xor_pos_packet, (uchar*)data->c_str(), size);
- Encode(xor_pos_packet, orig_packet, size);
- }
- bool changed = false;
- for (int i = 0; i < size; ++i) {
- if (xor_pos_packet[i]) {
- changed = true;
- break;
- }
- }
- if (!changed) {
- player->pos_mutex.releasewritelock(__FUNCTION__, __LINE__);
- return nullptr;
- }
- uchar* tmp = new uchar[size + 10];
- size = Pack(tmp, xor_pos_packet, size, size, version);
- player->pos_mutex.releasewritelock(__FUNCTION__, __LINE__);
- int32 orig_size = size;
- if (version >= 1188) {
- size += 1;
- }
- if (IsPlayer() && version > 546) {
- size += 4;
- }
- size -= sizeof(int32);
- size += CheckOverLoadSize(index);
- pos_packet_size = size;
- uchar* tmp2 = new uchar[size];
- uchar* ptr = tmp2;
- ptr += DoOverLoad(index, ptr);
- // extra byte in coe+ clients, 0 for NPC's 1 for Players
- int8 x = 0;
- if (version > 546) {
- if (IsPlayer()) {
- if (version >= 1188) {
- x = 1;
- memcpy(ptr, &x, sizeof(int8));
- ptr += sizeof(int8);
- }
- int32 now = Timer::GetCurrentTime2();
- memcpy(ptr, &now, sizeof(int32));
- ptr += sizeof(int32);
- }
- else if (version >= 1188) {
- memcpy(ptr, &x, sizeof(int8));
- ptr += sizeof(int8);
- }
- }
- memcpy(ptr, tmp + sizeof(int32), orig_size - sizeof(int32));
- delete[] tmp;
- return move(tmp2);
- }
- EQ2Packet* Spawn::serialize(Player* player, int16 version){
- return 0;
- }
- Spawn* Spawn::GetTarget(){
- Spawn* ret = 0;
-
- // only attempt to get a spawn if we had a target stored
- if (target != 0)
- {
- ret = GetZone()->GetSpawnByID(target);
-
- if (!ret)
- target = 0;
- }
- return ret;
- }
- void Spawn::SetTarget(Spawn* spawn){
- SetInfo(&target, spawn ? spawn->GetID() : 0);
- }
- Spawn* Spawn::GetLastAttacker() {
- Spawn* ret = 0;
- ret = GetZone()->GetSpawnByID(last_attacker);
- if (!ret)
- last_attacker = 0;
- return ret;
- }
- void Spawn::SetLastAttacker(Spawn* spawn){
- last_attacker = spawn->GetID();
- }
- void Spawn::SetInvulnerable(bool val){
- invulnerable = val;
- }
- bool Spawn::GetInvulnerable(){
- return invulnerable;
- }
- bool Spawn::TakeDamage(int32 damage){
- if(invulnerable)
- return false;
- if (IsEntity()) {
- if (((Entity*)this)->IsMezzed())
- ((Entity*)this)->RemoveAllMezSpells();
- if (damage == 0)
- return true;
- }
- int32 hp = GetHP();
- if(damage >= hp) {
- SetHP(0);
- if (IsPlayer()) {
- ((Player*)this)->InCombat(false);
- ((Player*)this)->SetRangeAttack(false);
- GetZone()->TriggerCharSheetTimer(); // force char sheet updates now
- }
- }
- else {
- SetHP(hp - damage);
- // if player flag the char sheet as changed so the ui updates properly
- if (IsPlayer())
- ((Player*)this)->SetCharSheetChanged(true);
- }
- return true;
- }
- void Spawn::TakeDamage(Spawn* attacker, int32 damage){
- if (TakeDamage(damage))
- GetZone()->CallSpawnScript(this, SPAWN_SCRIPT_HEALTHCHANGED, attacker);
- SetLastAttacker(attacker);
- }
- ZoneServer* Spawn::GetZone(){
- return zone;
- }
- void Spawn::SetZone(ZoneServer* in_zone, int32 version){
- zone = in_zone;
-
- if(in_zone)
- {
- region_map = world.GetRegionMap(std::string(in_zone->GetZoneFile()), version);
- current_map = world.GetMap(std::string(in_zone->GetZoneFile()), version);
- }
- else
- {
- region_map = nullptr;
- current_map = nullptr;
- }
- }
- /*** HIT POINT ***/
- void Spawn::SetHP(sint32 new_val, bool setUpdateFlags){
- if(new_val == 0){
- ClearRunningLocations();
- CalculateRunningLocation(true);
- }
- if(new_val > basic_info.max_hp)
- SetInfo(&basic_info.max_hp, new_val, setUpdateFlags);
- SetInfo(&basic_info.cur_hp, new_val, setUpdateFlags);
- if(/*IsPlayer() &&*/ GetZone() && basic_info.cur_hp > 0 && basic_info.cur_hp < basic_info.max_hp)
- GetZone()->AddDamagedSpawn(this);
- if (IsEntity() && ((Entity*)this)->GetGroupMemberInfo()) {
- ((Entity*)this)->UpdateGroupMemberInfo();
- if (IsPlayer())
- world.GetGroupManager()->SendGroupUpdate(((Entity*)this)->GetGroupMemberInfo()->group_id, GetZone()->GetClientBySpawn(this));
- else
- world.GetGroupManager()->SendGroupUpdate(((Entity*)this)->GetGroupMemberInfo()->group_id);
- }
- if ( IsPlayer() && new_val == 0 ) // fixes on death not showing hp update for players
- ((Player*)this)->SetCharSheetChanged(true);
- if (IsNPC() && ((NPC*)this)->IsPet() && ((NPC*)this)->GetOwner()->IsPlayer()) {
- Player* player = (Player*)((NPC*)this)->GetOwner();
- if (player->GetPet() && player->GetCharmedPet()) {
- if (this == player->GetPet()) {
- player->GetInfoStruct()->set_pet_health_pct((float)basic_info.cur_hp / (float)basic_info.max_hp);
- player->SetCharSheetChanged(true);
- }
- }
- else {
- player->GetInfoStruct()->set_pet_health_pct((float)basic_info.cur_hp / (float)basic_info.max_hp);
- player->SetCharSheetChanged(true);
- }
- }
- }
- void Spawn::SetTotalHP(sint32 new_val){
- if(basic_info.hp_base == 0)
- SetTotalHPBase(new_val);
- SetInfo(&basic_info.max_hp, new_val);
- if(GetZone() && basic_info.cur_hp > 0 && basic_info.cur_hp < basic_info.max_hp)
- GetZone()->AddDamagedSpawn(this);
- if (IsEntity() && ((Entity*)this)->GetGroupMemberInfo()) {
- ((Entity*)this)->UpdateGroupMemberInfo();
- if (IsPlayer())
- world.GetGroupManager()->SendGroupUpdate(((Entity*)this)->GetGroupMemberInfo()->group_id, GetZone()->GetClientBySpawn(this));
- else
- world.GetGroupManager()->SendGroupUpdate(((Entity*)this)->GetGroupMemberInfo()->group_id);
- }
- if (IsNPC() && ((NPC*)this)->IsPet() && ((NPC*)this)->GetOwner()->IsPlayer()) {
- Player* player = (Player*)((NPC*)this)->GetOwner();
- if (player->GetPet() && player->GetCharmedPet()) {
- if (this == player->GetPet()) {
- player->GetInfoStruct()->set_pet_health_pct((float)basic_info.cur_hp / (float)basic_info.max_hp);
- player->SetCharSheetChanged(true);
- }
- }
- else {
- player->GetInfoStruct()->set_pet_health_pct((float)basic_info.cur_hp / (float)basic_info.max_hp);
- player->SetCharSheetChanged(true);
- }
- }
- }
- void Spawn::SetTotalHPBase(sint32 new_val)
- {
- SetInfo(&basic_info.hp_base, new_val);
- if(GetZone() && basic_info.cur_hp > 0 && basic_info.cur_hp < basic_info.max_hp)
- GetZone()->AddDamagedSpawn(this);
- if (IsEntity() && ((Entity*)this)->GetGroupMemberInfo()) {
- ((Entity*)this)->UpdateGroupMemberInfo();
- if (IsPlayer())
- world.GetGroupManager()->SendGroupUpdate(((Entity*)this)->GetGroupMemberInfo()->group_id, GetZone()->GetClientBySpawn(this));
- else
- world.GetGroupManager()->SendGroupUpdate(((Entity*)this)->GetGroupMemberInfo()->group_id);
- }
- }
- sint32 Spawn::GetHP()
- {
- return basic_info.cur_hp;
- }
- sint32 Spawn::GetTotalHP()
- {
- return basic_info.max_hp;
- }
- sint32 Spawn::GetTotalHPBase()
- {
- return basic_info.hp_base;
- }
- /*** POWER ***/
- void Spawn::SetPower(sint32 power, bool setUpdateFlags){
- if(power > basic_info.max_power)
- SetInfo(&basic_info.max_power, power, setUpdateFlags);
- SetInfo(&basic_info.cur_power, power, setUpdateFlags);
- if(/*IsPlayer() &&*/ GetZone() && basic_info.cur_power < basic_info.max_power)
- GetZone()->AddDamagedSpawn(this);
- if (IsEntity() && ((Entity*)this)->GetGroupMemberInfo()) {
- ((Entity*)this)->UpdateGroupMemberInfo();
- if (IsPlayer())
- world.GetGroupManager()->SendGroupUpdate(((Entity*)this)->GetGroupMemberInfo()->group_id, GetZone()->GetClientBySpawn(this));
- else
- world.GetGroupManager()->SendGroupUpdate(((Entity*)this)->GetGroupMemberInfo()->group_id);
- }
- if (IsNPC() && ((NPC*)this)->IsPet() && ((NPC*)this)->GetOwner()->IsPlayer()) {
- Player* player = (Player*)((NPC*)this)->GetOwner();
- if (player->GetPet() && player->GetCharmedPet()) {
- if (this == player->GetPet()) {
- player->GetInfoStruct()->set_pet_power_pct((float)basic_info.cur_power / (float)basic_info.max_power);
- player->SetCharSheetChanged(true);
- }
- }
- else {
- player->GetInfoStruct()->set_pet_power_pct((float)basic_info.cur_power / (float)basic_info.max_power);
- player->SetCharSheetChanged(true);
- }
- }
- }
- void Spawn::SetTotalPower(sint32 new_val)
- {
- if(basic_info.power_base == 0)
- SetTotalPowerBase(new_val);
- SetInfo(&basic_info.max_power, new_val);
- if(GetZone() && basic_info.cur_power < basic_info.max_power)
- GetZone()->AddDamagedSpawn(this);
- if (IsEntity() && ((Entity*)this)->GetGroupMemberInfo()) {
- ((Entity*)this)->UpdateGroupMemberInfo();
- if (IsPlayer())
- world.GetGroupManager()->SendGroupUpdate(((Entity*)this)->GetGroupMemberInfo()->group_id, GetZone()->GetClientBySpawn(this));
- else
- world.GetGroupManager()->SendGroupUpdate(((Entity*)this)->GetGroupMemberInfo()->group_id);
- }
- if (IsNPC() && ((NPC*)this)->IsPet() && ((NPC*)this)->GetOwner()->IsPlayer()) {
- Player* player = (Player*)((NPC*)this)->GetOwner();
- if (player->GetPet() && player->GetCharmedPet()) {
- if (this == player->GetPet()) {
- player->GetInfoStruct()->set_pet_power_pct((float)basic_info.cur_power / (float)basic_info.max_power);
- player->SetCharSheetChanged(true);
- }
- }
- else {
- player->GetInfoStruct()->set_pet_power_pct((float)basic_info.cur_power / (float)basic_info.max_power);
- player->SetCharSheetChanged(true);
- }
- }
- }
- void Spawn::SetTotalPowerBase(sint32 new_val)
- {
- SetInfo(&basic_info.power_base, new_val);
- if(GetZone() && basic_info.cur_power < basic_info.max_power)
- GetZone()->AddDamagedSpawn(this);
- if (IsEntity() && ((Entity*)this)->GetGroupMemberInfo()) {
- ((Entity*)this)->UpdateGroupMemberInfo();
- if (IsPlayer())
- world.GetGroupManager()->SendGroupUpdate(((Entity*)this)->GetGroupMemberInfo()->group_id, GetZone()->GetClientBySpawn(this));
- else
- world.GetGroupManager()->SendGroupUpdate(((Entity*)this)->GetGroupMemberInfo()->group_id);
- }
- }
- sint32 Spawn::GetPower()
- {
- return basic_info.cur_power;
- }
- sint32 Spawn::GetTotalPower(){
- return basic_info.max_power;
- }
- sint32 Spawn::GetTotalPowerBase()
- {
- return basic_info.power_base;
- }
- /*** SAVAGERY ***/
- void Spawn::SetSavagery(sint32 savagery, bool setUpdateFlags)
- {
- /* JA: extremely limited functionality until we better understand Savagery */
- if(savagery > basic_info.max_savagery)
- SetInfo(&basic_info.max_savagery, savagery, setUpdateFlags);
- SetInfo(&basic_info.cur_savagery, savagery, setUpdateFlags);
- }
- void Spawn::SetTotalSavagery(sint32 new_val)
- {
- /* JA: extremely limited functionality until we better understand Savagery */
- if(basic_info.savagery_base == 0)
- SetTotalSavageryBase(new_val);
- SetInfo(&basic_info.max_savagery, new_val);
- }
- void Spawn::SetTotalSavageryBase(sint32 new_val){
- SetInfo(&basic_info.savagery_base, new_val);
- if (IsEntity() && ((Entity*)this)->GetGroupMemberInfo()) {
- ((Entity*)this)->UpdateGroupMemberInfo();
- if (IsPlayer())
- world.GetGroupManager()->SendGroupUpdate(((Entity*)this)->GetGroupMemberInfo()->group_id, GetZone()->GetClientBySpawn(this));
- else
- world.GetGroupManager()->SendGroupUpdate(((Entity*)this)->GetGroupMemberInfo()->group_id);
- }
- }
- sint32 Spawn::GetTotalSavagery()
- {
- return basic_info.max_savagery;
- }
- sint32 Spawn::GetSavagery()
- {
- return basic_info.cur_savagery;
- }
- /*** DISSONANCE ***/
- void Spawn::SetDissonance(sint32 dissonance, bool setUpdateFlags)
- {
- /* JA: extremely limited functionality until we better understand Dissonance */
- if(dissonance > basic_info.max_dissonance)
- SetInfo(&basic_info.max_dissonance, dissonance, setUpdateFlags);
- SetInfo(&basic_info.cur_dissonance, dissonance, setUpdateFlags);
- }
- void Spawn::SetTotalDissonance(sint32 new_val)
- {
- /* JA: extremely limited functionality until we better understand Dissonance */
- if(basic_info.dissonance_base == 0)
- SetTotalDissonanceBase(new_val);
- SetInfo(&basic_info.max_dissonance, new_val);
- }
- void Spawn::SetTotalDissonanceBase(sint32 new_val)
- {
- SetInfo(&basic_info.dissonance_base, new_val);
- if (IsEntity() && ((Entity*)this)->GetGroupMemberInfo()) {
- ((Entity*)this)->UpdateGroupMemberInfo();
- if (IsPlayer())
- world.GetGroupManager()->SendGroupUpdate(((Entity*)this)->GetGroupMemberInfo()->group_id, GetZone()->GetClientBySpawn(this));
- else
- world.GetGroupManager()->SendGroupUpdate(((Entity*)this)->GetGroupMemberInfo()->group_id);
- }
- }
- sint32 Spawn::GetTotalDissonance()
- {
- return basic_info.max_dissonance;
- }
- sint32 Spawn::GetDissonance()
- {
- return basic_info.cur_dissonance;
- }
- /* --< Alternate Advancement Points >-- */
- void Spawn::SetAssignedAA(sint16 new_val)
- {
- SetInfo(&basic_info.assigned_aa, new_val);
- }
- void Spawn::SetUnassignedAA(sint16 new_val)
- {
- SetInfo(&basic_info.unassigned_aa, new_val);
- }
- void Spawn::SetTradeskillAA(sint16 new_val)
- {
- SetInfo(&basic_info.tradeskill_aa, new_val);
- }
- void Spawn::SetUnassignedTradeskillAA(sint16 new_val)
- {
- SetInfo(&basic_info.unassigned_tradeskill_aa, new_val);
- }
- void Spawn::SetPrestigeAA(sint16 new_val)
- {
- SetInfo(&basic_info.prestige_aa, new_val);
- }
- void Spawn::SetUnassignedPrestigeAA(sint16 new_val)
- {
- SetInfo(&basic_info.unassigned_prestige_aa, new_val);
- }
- void Spawn::SetTradeskillPrestigeAA(sint16 new_val)
- {
- SetInfo(&basic_info.tradeskill_prestige_aa, new_val);
- }
- void Spawn::SetUnassignedTradeskillPrestigeAA(sint16 new_val)
- {
- SetInfo(&basic_info.unassigned_tradeskill_prestige_aa, new_val);
- }
- void Spawn::SetAAXPRewards(int32 value)
- {
- SetInfo(&basic_info.aaxp_rewards, value, false);
- }
- sint16 Spawn::GetAssignedAA()
- {
- return basic_info.assigned_aa;
- }
- sint16 Spawn::GetUnassignedAA()
- {
- return basic_info.unassigned_aa;
- }
- sint16 Spawn::GetTradeskillAA()
- {
- return basic_info.tradeskill_aa;
- }
- sint16 Spawn::GetUnassignedTradeskillAA()
- {
- return basic_info.unassigned_tradeskill_aa;
- }
- sint16 Spawn::GetPrestigeAA()
- {
- return basic_info.prestige_aa;
- }
- sint16 Spawn::GetUnassignedPretigeAA()
- {
- return basic_info.unassigned_prestige_aa;
- }
- sint16 Spawn::GetTradeskillPrestigeAA()
- {
- return basic_info.tradeskill_prestige_aa;
- }
- sint16 Spawn::GetUnassignedTradeskillPrestigeAA()
- {
- return basic_info.unassigned_tradeskill_prestige_aa;
- }
- int32 Spawn::GetAAXPRewards()
- {
- return basic_info.aaxp_rewards;
- }
- float Spawn::GetDistance(float x1, float y1, float z1, float x2, float y2, float z2){
- x1 = x1 - x2;
- y1 = y1 - y2;
- z1 = z1 - z2;
- return sqrt(x1*x1 + y1*y1 + z1*z1);
- }
- float Spawn::GetDistance(float x, float y, float z, float radius, bool ignore_y) {
- if (ignore_y)
- return GetDistance(x, y, z, GetX(), y, GetZ()) - radius;
- else
- return GetDistance(x, y, z, GetX(), GetY(), GetZ()) - radius;
- }
- float Spawn::GetDistance(float x, float y, float z, bool ignore_y) {
- return GetDistance(x, y, z, 0.0f, ignore_y);
- }
- float Spawn::GetDistance(Spawn* spawn, bool ignore_y, bool includeRadius){
- float ret = 0;
- if (spawn)
- {
- float radius = 0.0f;
- if (includeRadius)
- radius = CalculateRadius(spawn);
- ret = GetDistance(spawn->GetX(), spawn->GetY(), spawn->GetZ(), radius, ignore_y);
- }
- // maybe distance against ourselves, in that case we want to nullify the radius check
- if (ret < 0)
- ret = 0.0f;
- return ret;
- }
- float Spawn::GetDistance(Spawn* spawn, float x1, float y1, float z1, bool includeRadius) {
- float ret = 0;
- if (spawn)
- {
- float radius = 0.0f;
- if (includeRadius)
- radius = CalculateRadius(spawn);
- ret = GetDistance(x1, y1, z1, spawn->GetX(), spawn->GetY(), spawn->GetZ()) - radius;
- }
- // maybe distance against ourselves, in that case we want to nullify the radius check
- if (ret < 0)
- ret = 0.0f;
- return ret;
- }
- float Spawn::CalculateRadius(Spawn* target)
- {
- float srcRadius = short_to_float(appearance.pos.collision_radius);
- if (target)
- {
- float targRadius = short_to_float(target->appearance.pos.collision_radius);
- return (targRadius / 32.0f) + (srcRadius / 32.0f);
- }
- else
- return (srcRadius / 32.0f);
- }
- int32 Spawn::GetRespawnTime(){
- return respawn;
- }
- void Spawn::SetRespawnTime(int32 time){
- respawn = time;
- }
- int32 Spawn::GetExpireOffsetTime(){
- return expire_offset;
- }
- void Spawn::SetExpireOffsetTime(int32 time){
- expire_offset = time;
- }
- int32 Spawn::GetSpawnLocationID(){
- return spawn_location_id;
- }
- void Spawn::SetSpawnLocationID(int32 id){
- spawn_location_id = id;
- }
- int32 Spawn::GetSpawnEntryID(){
- return spawn_entry_id;
- }
- void Spawn::SetSpawnEntryID(int32 id){
- spawn_entry_id = id;
- }
- int32 Spawn::GetSpawnLocationPlacementID(){
- return spawn_location_spawns_id;
- }
- void Spawn::SetSpawnLocationPlacementID(int32 id){
- spawn_location_spawns_id = id;
- }
- const char* Spawn::GetSpawnScript(){
- if(spawn_script.length() > 0)
- return spawn_script.c_str();
- else
- return 0;
- }
- void Spawn::SetSpawnScript(string name){
- spawn_script = name;
- }
- void Spawn::SetPrimaryCommand(const char* name, const char* command, float distance){
- EntityCommand* entity_command = CreateEntityCommand(name, distance, command, "", 0, 0);
- if(primary_command_list.size() > 0 && primary_command_list[0]){
- safe_delete(primary_command_list[0]);
- primary_command_list[0] = entity_command;
- }
- else
- primary_command_list.push_back(entity_command);
- }
- void Spawn::SetSecondaryCommands(vector<EntityCommand*>* commands){
- if(commands && commands->size() > 0){
- vector<EntityCommand*>::iterator itr;
- if(secondary_command_list.size() > 0){
- for(itr = secondary_command_list.begin(); itr != secondary_command_list.end(); itr++){
- safe_delete(*itr);
- }
- secondary_command_list.clear();
- }
- EntityCommand* command = 0;
- for(itr = commands->begin(); itr != commands->end(); itr++){
- command = CreateEntityCommand(*itr);
- secondary_command_list.push_back(command);
- }
- }
- }
- void Spawn::SetPrimaryCommands(vector<EntityCommand*>* commands){
- if(commands && commands->size() > 0){
- vector<EntityCommand*>::iterator itr;
- if(primary_command_list.size() > 0){
- for(itr = primary_command_list.begin(); itr != primary_command_list.end(); itr++){
- safe_delete(*itr);
- }
- primary_command_list.clear();
- }
- EntityCommand* command = 0;
- for(itr = commands->begin(); itr != commands->end(); itr++){
- command = CreateEntityCommand(*itr);
- primary_command_list.push_back(command);
- }
- }
- }
- EntityCommand* Spawn::FindEntityCommand(string command, bool primaryOnly) {
- EntityCommand* entity_command = 0;
- if (primary_command_list.size() > 0) {
- vector<EntityCommand*>::iterator itr;
- for (itr = primary_command_list.begin(); itr != primary_command_list.end(); itr++) {
- if ((*itr)->command.compare(command) == 0) {
- entity_command = *itr;
- break;
- }
- }
- }
- if (primaryOnly)
- return entity_command;
- if (!entity_command && secondary_command_list.size() > 0) {
- vector<EntityCommand*>::iterator itr;
- for (itr = secondary_command_list.begin(); itr != secondary_command_list.end(); itr++) {
- if ((*itr)->command == command) {
- entity_command = *itr;
- break;
- }
- }
- }
- return entity_command;
- }
- void Spawn::SetSizeOffset(int8 offset){
- size_offset = offset;
- }
- int8 Spawn::GetSizeOffset(){
- return size_offset;
- }
- void Spawn::SetMerchantID(int32 val){
- merchant_id = val;
- }
- int32 Spawn::GetMerchantID(){
- return merchant_id;
- }
- void Spawn::SetMerchantType(int8 val) {
- merchant_type = val;
- }
- int8 Spawn::GetMerchantType() {
- return merchant_type;
- }
- void Spawn::SetMerchantLevelRange(int32 minLvl, int32 maxLvl) {
- merchant_min_level = minLvl;
- merchant_max_level = maxLvl;
- }
- int32 Spawn::GetMerchantMinLevel() {
- return merchant_min_level;
- }
- int32 Spawn::GetMerchantMaxLevel() {
- return merchant_max_level;
- }
- bool Spawn::IsClientInMerchantLevelRange(Client* client, bool sendMessageIfDenied)
- {
- if (!client)
- return false;
- if (GetMerchantMinLevel() && client->GetPlayer()->GetLevel() < GetMerchantMinLevel())
- {
- client->Message(CHANNEL_COLOR_RED, "You are unable to interact with this merchant due to a minimum level %u allowed.", GetMerchantMinLevel());
- return false;
- }
- else if (GetMerchantMaxLevel() && client->GetPlayer()->GetLevel() > GetMerchantMaxLevel())
- {
- client->Message(CHANNEL_COLOR_RED, "You are unable to interact with this merchant due to a maximum level %u allowed.", GetMerchantMaxLevel());
- return false;
- }
-
- return true;
- }
- void Spawn::SetQuestsRequired(Spawn* new_spawn){
- m_requiredQuests.writelock(__FUNCTION__, __LINE__);
- if(required_quests.size() > 0){
- map<int32, vector<int16>* >::iterator itr;
- for(itr = required_quests.begin(); itr != required_quests.end(); itr++){
- vector<int16>* quest_steps = itr->second;
- for (int32 i = 0; i < quest_steps->size(); i++)
- new_spawn->SetQuestsRequired(itr->first, quest_steps->at(i));
- }
- }
- m_requiredQuests.releasewritelock(__FUNCTION__, __LINE__);
- }
- void Spawn::SetQuestsRequired(int32 quest_id, int16 quest_step){
- m_requiredQuests.writelock(__FUNCTION__, __LINE__);
- if (required_quests.count(quest_id) == 0)
- required_quests.insert(make_pair(quest_id, new vector<int16>));
- else{
- for (int32 i = 0; i < required_quests[quest_id]->size(); i++){
- if (required_quests[quest_id]->at(i) == quest_step){
- m_requiredQuests.releasewritelock(__FUNCTION__, __LINE__);
- return;
- }
- }
- }
- has_quests_required = true;
- required_quests[quest_id]->push_back(quest_step);
- m_requiredQuests.releasewritelock(__FUNCTION__, __LINE__);
- }
- bool Spawn::HasQuestsRequired()
- {
- return has_quests_required;
- }
- void Spawn::SetRequiredHistory(int32 event_id, int32 value1, int32 value2){
- LUAHistory* set_value = new LUAHistory();
- set_value->Value = value1;
- set_value->Value2 = value2;
- set_value->SaveNeeded = false;
- m_requiredHistory.writelock(__FUNCTION__, __LINE__);
- if (required_history.count(event_id) == 0)
- required_history.insert(make_pair(event_id, set_value));
- else
- {
- LUAHistory* tmp_value = required_history[event_id];
- required_history[event_id] = set_value;
- safe_delete(tmp_value);
- }
- m_requiredHistory.releasewritelock(__FUNCTION__, __LINE__);
- }
- void Spawn::SetTransporterID(int32 id){
- transporter_id = id;
- }
- int32 Spawn::GetTransporterID(){
- return transporter_id;
- }
- void Spawn::InitializePosPacketData(Player* player, PacketStruct* packet, bool bSpawnUpdate) {
- int16 version = packet->GetVersion();
- int32 new_grid_id = 0;
- if(player->GetMap() != nullptr && player->GetMap()->IsMapLoaded())
- {
- std::map<int32,TimedGridData>::iterator itr = established_grid_id.find(version);
- if ( itr == established_grid_id.end() || itr->second.timestamp <= (Timer::GetCurrentTime2()))
- {
- new_grid_id = player->GetMap()->GetGrid()->GetGridIDByLocation(GetX(),GetY(),GetZ());
- TimedGridData data;
- data.grid_id = new_grid_id;
- data.timestamp = Timer::GetCurrentTime2()+100;
- established_grid_id.insert(make_pair(packet->GetVersion(), data));
- }
- else
- new_grid_id = itr->second.grid_id;
- }
-
- packet->setDataByName("pos_grid_id", new_grid_id != 0 ? new_grid_id : appearance.pos.grid_id);
- bool include_heading = true;
- if (IsWidget() && ((Widget*)this)->GetIncludeHeading() == false)
- include_heading = false;
- else if (IsSign() && ((Sign*)this)->GetIncludeHeading() == false)
- include_heading = false;
- else if (IsGroundSpawn())
- include_heading = false;
- if (include_heading){
- packet->setDataByName("pos_heading1", appearance.pos.Dir1);
- packet->setDataByName("pos_heading2", appearance.pos.Dir2);
- }
- if (version <= 910) {
- packet->setDataByName("pos_collision_radius", appearance.pos.collision_radius > 0 ? appearance.pos.collision_radius : 32);
- packet->setDataByName("pos_size", size > 0 ? size : 32);
- packet->setDataByName("pos_size_multiplier", 32); //32 is normal
- }
- else {
- if (size == 0)
- size = 32;
- packet->setDataByName("pos_collision_radius", appearance.pos.collision_radius > 0 ? appearance.pos.collision_radius : 32);
- packet->setDataByName("pos_size", 1.0f);
- if (!IsPlayer())
- packet->setDataByName("pos_size", size > 0 ? (((float)size) / 32.0f) : 1.0f); // float not an integer
- else
- packet->setDataByName("pos_size", 1.0f);
- // please do not remove! This makes it so NPCs for example do not resize large/small when you are in combat with them!
- packet->setDataByName("pos_size_ratio", 1.0f);
- }
- packet->setDataByName("pos_state", appearance.pos.state);
- bool include_location = true;
- if (IsWidget() && ((Widget*)this)->GetIncludeLocation() == false)
- include_location = false;
- else if (IsSign() && ((Sign*)this)->GetIncludeLocation() == false)
- include_location = false;
- if (include_location){
- if (IsWidget() && ((Widget*)this)->GetMultiFloorLift()) {
- Widget* widget = (Widget*)this;
- float x = appearance.pos.X - widget->GetWidgetX();
- float y = appearance.pos.Y - widget->GetWidgetY();
- float z = appearance.pos.Z - widget->GetWidgetZ();
- packet->setDataByName("pos_x", x);
- packet->setDataByName("pos_y", y);
- packet->setDataByName("pos_z", z);
- }
- else {
- packet->setDataByName("pos_x", appearance.pos.X);
- packet->setDataByName("pos_y", appearance.pos.Y);
- packet->setDataByName("pos_z", appearance.pos.Z);
- }
- if (IsSign())
- packet->setDataByName("pos_unknown6", 3, 2);
- }
- if (IsPlayer()) {
- packet->setDataByName("pos_swim_speed_modifier", 20);
- packet->setDataByName("pos_x_velocity", TransformFromFloat(GetSpeedX(), 5));
- packet->setDataByName("pos_y_velocity", TransformFromFloat(GetSpeedY(), 5));
- packet->setDataByName("pos_z_velocity", TransformFromFloat(GetSpeedZ(), 5));
- }
- if (appearance.pos.X2 == 0 && appearance.pos.Y2 == 0 && appearance.pos.Z2 && (appearance.pos.X != 0 || appearance.pos.Y != 0 || appearance.pos.Z != 0)) {
- appearance.pos.X2 = appearance.pos.X;
- appearance.pos.Y2 = appearance.pos.Y;
- appearance.pos.Z2 = appearance.pos.Z;
- }
- if (appearance.pos.X3 == 0 && appearance.pos.Y3 == 0 && appearance.pos.Z3 && (appearance.pos.X != 0 || appearance.pos.Y != 0 || appearance.pos.Z != 0)) {
- appearance.pos.X3 = appearance.pos.X;
- appearance.pos.Y3 = appearance.pos.Y;
- appearance.pos.Z3 = appearance.pos.Z;
- }
- //Transform To/From Float bits (original client)
- //pos_loc_offset[3]: 5
- //pos_x_velocity: 5
- //pos_y_velocity: 5
- //pos_z_velocity: 5
- //pos_heading1: 6
- //pos_heading2: 6
- //pos_speed: 8
- //pos_dest_loc_offset[3]: 5
- //pos_dest_loc_offset2[3]: 5
- //pos_heading_speed: 5
- //pos_move_type: 5 (speed_modifier)
- //pos_swim_speed_modifier: 5
- //pos_side_speed: 8
- //pos_vert_speed: 8
- //pos_requested_pitch: 6
- //pos_requested_pitch_speed: 5
- //pos_pitch: 6
- //pos_collision_radius: 5
- //pos_size: 5
- //actor_stop_range: 5
- //this is for original box client, destinations used to be offsets
- if (appearance.pos.X2 != 0 || appearance.pos.Y2 != 0 || appearance.pos.Z2 != 0) {
- packet->setDataByName("pos_dest_loc_offset", TransformFromFloat(appearance.pos.X2 - appearance.pos.X, 5));
- packet->setDataByName("pos_dest_loc_offset", TransformFromFloat(appearance.pos.Y2 - appearance.pos.Y, 5), 1);
- packet->setDataByName("pos_dest_loc_offset", TransformFromFloat(appearance.pos.Z2 - appearance.pos.Z, 5), 2);
- }
- if (appearance.pos.X3 != 0 || appearance.pos.Y3 != 0 || appearance.pos.Z3 != 0) {
- packet->setDataByName("pos_dest_loc_offset2", TransformFromFloat(appearance.pos.X3 - appearance.pos.X, 5));
- packet->setDataByName("pos_dest_loc_offset2", TransformFromFloat(appearance.pos.Y3 - appearance.pos.Y, 5), 1);
- packet->setDataByName("pos_dest_loc_offset2", TransformFromFloat(appearance.pos.Z3 - appearance.pos.Z, 5), 2);
- }
- bool bSendSpeed = true;
- if (IsWidget() && ((Widget*)this)->GetMultiFloorLift()) {
- Widget* widget = (Widget*)this;
- float x;
- float y;
- float z;
- if (IsRunning()){
- x = appearance.pos.X2 - widget->GetWidgetX();
- y = appearance.pos.Y2 - widget->GetWidgetY();
- z = appearance.pos.Z2- widget->GetWidgetZ();
- }
- else {
- x = appearance.pos.X - widget->GetWidgetX();
- y = appearance.pos.Y - widget->GetWidgetY();
- z = appearance.pos.Z - widget->GetWidgetZ();
- }
- packet->setDataByName("pos_next_x", x);
- packet->setDataByName("pos_next_y", y);
- packet->setDataByName("pos_next_z", z);
- packet->setDataByName("pos_x3", x);
- packet->setDataByName("pos_y3", y);
- packet->setDataByName("pos_z3", z);
- }
- //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
- //This fixes the bug where spawns with movement scripts face south when initially spawning if they are at their target location.
- else if (bSpawnUpdate || memcmp(&appearance.pos.X, &appearance.pos.X2, sizeof(float) * 3) != 0) {
- packet->setDataByName("pos_next_x", appearance.pos.X2);
- packet->setDataByName("pos_next_y", appearance.pos.Y2);
- packet->setDataByName("pos_next_z", appearance.pos.Z2);
- packet->setDataByName("pos_x3", appearance.pos.X3);
- packet->setDataByName("pos_y3", appearance.pos.Y3);
- packet->setDataByName("pos_z3", appearance.pos.Z3);
- }
- else
- {
- bSendSpeed = false;
- }
- //packet->setDataByName("pos_unknown2", 4, 2);
- int16 speed_multiplier = rule_manager.GetGlobalRule(R_Spawn, SpeedMultiplier)->GetInt16(); // was 1280, 600 and now 300... investigating why
- if (IsPlayer()) {
- Player* player = static_cast<Player*>(this);
- packet->setDataByName("pos_speed", player->GetPosPacketSpeed() * speed_multiplier);
- packet->setDataByName("pos_side_speed", player->GetSideSpeed() * speed_multiplier);
- }
- else if (bSendSpeed) {
- packet->setDataByName("pos_speed", GetSpeed() * speed_multiplier);
- }
-
-
- if (IsNPC() || IsPlayer()) {
- packet->setDataByName("pos_move_type", 25);
- }
- else if (IsWidget() || IsSign()) {
- packet->setDataByName("pos_move_type", 11);
- }
- else if(IsGroundSpawn()) {
- packet->setDataByName("pos_move_type", 16);
- }
- if (!IsPlayer())
- packet->setDataByName("pos_movement_mode", 2);
-
- if(version <= 910)
- packet->setDataByName("pos_unknown10", 0xFFFF, 1);
- else if (version >= 1119)
- packet->setDataByName("face_actor_id", 0xFFFFFFFF);
- else
- packet->setDataByName("pos_unknown10", 0xFFFF);
- if(version <= 910)
- packet->setDataByName("pos_unknown10", 0xFFFF, 2);
- else if (version >= 1119)
- packet->setDataByName("face_actor_id", 0xFFFFFFFF);
- else
- packet->setDataByName("pos_unknown10", 0XFFFF, 1);
- packet->setDataByName("pos_pitch1", appearance.pos.Pitch1);
- packet->setDataByName("pos_pitch2", appearance.pos.Pitch2);
- packet->setDataByName("pos_roll", appearance.pos.Roll);
- }
- void Spawn::InitializeInfoPacketData(Player* spawn, PacketStruct* packet) {
- int16 version = packet->GetVersion();
- bool spawnHiddenFromClient = false;
- // radius of 0 is always seen, -1 is never seen (unless items/spells override), larger than 0 is a defined radius to restrict visibility
- sint32 radius = rule_manager.GetGlobalRule(R_PVP, InvisPlayerDiscoveryRange)->GetSInt32();
- if (radius != 0 && (Spawn*)spawn != this && this->IsPlayer() && !spawn->CanSeeInvis((Entity*)this))
- spawnHiddenFromClient = true;
- if (!spawnHiddenFromClient && (appearance.targetable == 1 || appearance.show_level == 1 || appearance.display_name == 1)) {
- appearance.locked_no_loot = 1; //for now
- if (!IsObject() && !IsGroundSpawn() && !IsWidget() && !IsSign()) {
- int8 percent = 0;
- if (GetHP() > 0)
- percent = (int8)(((float)GetHP() / GetTotalHP()) * 100);
- if (version >= 284) {
- if (percent < 100) {
- packet->setDataByName("hp_remaining", 100 ^ percent);
- }
- else
- packet->setDataByName("hp_remaining", 0);
- }
- else {
- if (percent > 100)
- percent = 100;
- packet->setDataByName("hp_remaining", percent);
- }
- if (GetTotalPower() > 0) {
- percent = (int8)(((float)GetPower() / GetTotalPower()) * 100);
- if (percent > 0)
- packet->setDataByName("power_percent", percent);
- else
- packet->setDataByName("power_percent", 0);
- }
- }
- }
- if (version <= 546) {
- packet->setDataByName("name", appearance.name);
- for (int8 i = 0; i < 8; i++)
- packet->setDataByName("unknown1", 0xFF, i);
- if (appearance.show_level == 0)
- packet->setDataByName("hide_health", 1);
- }
- if (GetHP() <= 0 && IsEntity()) {
- packet->setDataByName("corpse", 1);
- if(HasLoot())
- packet->setDataByName("loot_icon", 1);
- }
- if (!IsPlayer())
- packet->setDataByName("npc", 1);
- if (GetMerchantID() > 0)
- packet->setDataByName("merchant", 1);
-
- packet->setDataByName("effective_level", spawn->IsEntity() && ((Entity*)spawn)->GetInfoStruct()->get_effective_level() != 0 ? (int8)((Entity*)spawn)->GetInfoStruct()->get_effective_level() : (int8)GetLevel());
- packet->setDataByName("level", (int8)GetLevel());
- packet->setDataByName("unknown4", (int8)GetLevel());
- packet->setDataByName("difficulty", appearance.encounter_level); //6);
- packet->setDataByName("unknown6", 1);
- packet->setDataByName("heroic_flag", appearance.heroic_flag);
- if (IsNPC() && !IsPet())
- {
- if(((Entity*)this)->GetInfoStruct()->get_interaction_flag())
- packet->setDataByName("interaction_flag", ((Entity*)this)->GetInfoStruct()->get_interaction_flag()); //this makes NPCs head turn to look at you (12)
- else
- packet->setDataByName("interaction_flag", 12); //turn head since no other value is set
- }
- packet->setDataByName("class", appearance.adventure_class);
- int16 model_type = appearance.model_type;
- if (GetIllusionModel() != 0) {
- if (IsPlayer()) {
- if (((Player*)this)->get_character_flag(CF_SHOW_ILLUSION)) {
- model_type = GetIllusionModel();
- }
- }
- else
- model_type = GetIllusionModel();
- }
- int16 sogaModelType = appearance.soga_model_type;
- if (spawnHiddenFromClient)
- {
- model_type = 0;
- sogaModelType = 0;
- }
- packet->setDataByName("model_type", model_type);
- if (appearance.soga_model_type == 0)
- packet->setDataByName("soga_model_type", model_type);
- else
- packet->setDataByName("soga_model_type", sogaModelType);
- if (GetTempActionState() >= 0)
- packet->setDataByName("action_state", GetTempActionState());
- else
- packet->setDataByName("action_state", appearance.action_state);
- if (GetTempVisualState() >= 0)
- packet->setDataByName("visual_state", GetTempVisualState());
- else
- packet->setDataByName("visual_state", appearance.visual_state);
- packet->setDataByName("emote_state", appearance.emote_state);
- packet->setDataByName("mood_state", appearance.mood_state);
- packet->setDataByName("gender", appearance.gender);
- packet->setDataByName("race", appearance.race);
- packet->setDataByName("gender", appearance.gender);
- if (IsEntity()) {
- Entity* entity = ((Entity*)this);
- packet->setDataByName("combat_voice", entity->GetCombatVoice());
- packet->setDataByName("emote_voice", entity->GetEmoteVoice());
- for (int i = 0; i < 25; i++) {
- if (i == 2) { //don't send helm if hidden flag
- if (IsPlayer()) {
- if (((Player*)this)->get_character_flag(CF_HIDE_HELM)) {
- packet->setDataByName("equipment_types", 0, i);
- packet->setColorByName("equipment_colors", 0, i);
- packet->setColorByName("equipment_highlights", 0, i);
- continue;
- }
- }
- if (IsBot()) {
- if (!((Bot*)this)->ShowHelm) {
- packet->setDataByName("equipment_types", 0, i);
- packet->setColorByName("equipment_colors", 0, i);
- packet->setColorByName("equipment_highlights", 0, i);
- continue;
- }
- }
- }
- else if (i == 19) { //don't send cloak if hidden
- if (IsPlayer()) {
- if (!((Player*)this)->get_character_flag(CF_SHOW_CLOAK)) {
- packet->setDataByName("equipment_types", 0, i);
- packet->setColorByName("equipment_colors", 0, i);
- packet->setColorByName("equipment_highlights", 0, i);
- continue;
- }
- }
- if (IsBot()) {
- if (!((Bot*)this)->ShowCloak) {
- packet->setDataByName("equipment_types", 0, i);
- packet->setColorByName("equipment_colors", 0, i);
- packet->setColorByName("equipment_highlights", 0, i);
- continue;
- }
- }
- }
- entity->MEquipment.lock();
- packet->setDataByName("equipment_types", entity->equipment.equip_id[i], i);
- packet->setColorByName("equipment_colors", entity->equipment.color[i], i);
- packet->setColorByName("equipment_highlights", entity->equipment.highlight[i], i);
- entity->MEquipment.unlock();
-
- }
- packet->setDataByName("mount_type", entity->GetMount());
- // find the visual flags
- int8 vis_flag = 0;
- //Invis + crouch flag check
- if (entity->IsStealthed())
- vis_flag += (INFO_VIS_FLAG_INVIS + INFO_VIS_FLAG_CROUCH);
- //Invis flag check
- else if (entity->IsInvis())
- vis_flag += INFO_VIS_FLAG_INVIS;
- //Mount flag check
- if (entity->GetMount() > 0)
- vis_flag += INFO_VIS_FLAG_MOUNTED;
- //Hide hood check
- if ((IsPlayer() && ((Player*)this)->get_character_flag(CF_HIDE_HOOD)) || appearance.hide_hood)
- vis_flag += INFO_VIS_FLAG_HIDE_HOOD;
- packet->setDataByName("visual_flag", vis_flag);
- packet->setColorByName("mount_saddle_color", entity->GetMountSaddleColor());
- packet->setColorByName("mount_color", entity->GetMountColor());
- packet->setDataByName("hair_type_id", entity->features.hair_type);
- packet->setDataByName("chest_type_id", entity->features.chest_type);
- packet->setDataByName("wing_type_id", entity->features.wing_type);
- packet->setDataByName("legs_type_id", entity->features.legs_type);
- packet->setDataByName("soga_hair_type_id", entity->features.soga_hair_type);
- packet->setDataByName("facial_hair_type_id", entity->features.hair_face_type);
- packet->setDataByName("soga_facial_hair_type_id", entity->features.soga_hair_face_type);
- for (int i = 0; i < 3; i++) {
- packet->setDataByName("eye_type", entity->features.eye_type[i], i);
- packet->setDataByName("ear_type", entity->features.ear_type[i], i);
- packet->setDataByName("eye_brow_type", entity->features.eye_brow_type[i], i);
- packet->setDataByName("cheek_type", entity->features.cheek_type[i], i);
- packet->setDataByName("lip_type", entity->features.lip_type[i], i);
- packet->setDataByName("chin_type", entity->features.chin_type[i], i);
- packet->setDataByName("nose_type", entity->features.nose_type[i], i);
- packet->setDataByName("soga_eye_type", entity->features.soga_eye_type[i], i);
- packet->setDataByName("soga_ear_type", entity->features.soga_ear_type[i], i);
- packet->setDataByName("soga_eye_brow_type", entity->features.soga_eye_brow_type[i], i);
- packet->setDataByName("soga_cheek_type", entity->features.soga_cheek_type[i], i);
- packet->setDataByName("soga_lip_type", entity->features.soga_lip_type[i], i);
- packet->setDataByName("soga_chin_type", entity->features.soga_chin_type[i], i);
- packet->setDataByName("soga_nose_type", entity->features.soga_nose_type[i], i);
- }
- packet->setColorByName("skin_color", entity->features.skin_color);
- packet->setColorByName("model_color", entity->features.model_color);
- packet->setColorByName("eye_color", entity->features.eye_color);
- packet->setColorByName("hair_type_color", entity->features.hair_type_color);
- packet->setColorByName("hair_type_highlight_color", entity->features.hair_type_highlight_color);
- packet->setColorByName("hair_face_color", entity->features.hair_face_color);
- packet->setColorByName("hair_face_highlight_color", entity->features.hair_face_highlight_color);
- packet->setColorByName("hair_highlight", entity->features.hair_highlight_color);
- packet->setColorByName("wing_color1", entity->features.wing_color1);
- packet->setColorByName("wing_color2", entity->features.wing_color2);
- packet->setColorByName("hair_color1", entity->features.hair_color1);
- packet->setColorByName("hair_color2", entity->features.hair_color2);
- packet->setColorByName("soga_skin_color", entity->features.soga_skin_color);
- packet->setColorByName("soga_model_color", entity->features.soga_model_color);
- packet->setColorByName("soga_eye_color", entity->features.soga_eye_color);
- packet->setColorByName("soga_hair_color1", entity->features.soga_hair_color1);
- packet->setColorByName("soga_hair_color2", entity->features.soga_hair_color2);
- packet->setColorByName("soga_hair_type_color", entity->features.soga_hair_type_color);
- packet->setColorByName("soga_hair_type_highlight_color", entity->features.soga_hair_type_highlight_color);
- packet->setColorByName("soga_hair_face_color", entity->features.soga_hair_face_color);
- packet->setColorByName("soga_hair_face_highlight_color", entity->features.soga_hair_face_highlight_color);
- packet->setColorByName("soga_hair_highlight", entity->features.soga_hair_highlight_color);
- packet->setDataByName("body_size", entity->features.body_size);
- packet->setDataByName("body_age", entity->features.body_age);
- packet->setDataByName("soga_body_size", entity->features.soga_body_size);
- packet->setDataByName("soga_body_age", entity->features.soga_body_age);
- }
- else {
- EQ2_Color empty;
- empty.red = 255;
- empty.blue = 255;
- empty.green = 255;
- packet->setColorByName("skin_color", empty);
- packet->setColorByName("model_color", empty);
- packet->setColorByName("eye_color", empty);
- packet->setColorByName("soga_skin_color", empty);
- packet->setColorByName("soga_model_color", empty);
- packet->setColorByName("soga_eye_color", empty);
- }
- if (appearance.icon == 0) {
- if (appearance.attackable == 1)
- appearance.icon = 0;
- else if (appearance.encounter_level > 0)
- appearance.icon = 4;
- else
- appearance.icon = 6;
- }
- // If Coe+ clients modify the values before we send
- // if not then just send the value we have.
- int8 temp_icon = appearance.icon;
- //Check if we need to add the hand icon..
- if ((temp_icon & 6) != 6 && appearance.display_hand_icon) {
- temp_icon |= 6;
- }
- //Icon value 28 for boats, set this without modifying the value
- if (version >= 1188 && temp_icon != 28) {
- if ((temp_icon & 64) > 0) {
- temp_icon -= 64; // remove the DoV value;
- temp_icon += 128; // add the CoE value
- }
- if ((temp_icon & 32) > 0) {
- temp_icon -= 32; // remove the DoV value;
- temp_icon += 64; // add the CoE value
- }
- if ((temp_icon & 4) > 0) {
- temp_icon -= 4; // remove DoV value
- temp_icon += 8; // add the CoE icon
- }
- if ((temp_icon & 6) > 0) {
- temp_icon -= 10; // remove DoV value
- temp_icon += 12; // add the CoE icon
- }
- }
- packet->setDataByName("icon", temp_icon);//appearance.icon);
- int32 temp_activity_status = 0;
- if (!Alive() && GetTotalHP() > 0 && !IsObject() && !IsGroundSpawn())
- temp_activity_status = 1;
- temp_activity_status += (IsNPC() || IsObject() || IsGroundSpawn()) ? 1 << 1 : 0;
- if (version >= 1188) {
- // Fix widget or sign having 'Play Legends of Norrath' or 'Tell' options in right click (client hard-coded entity commands)
- if(IsWidget() || IsSign())
- temp_activity_status = 2;
- if (IsGroundSpawn() || GetShowHandIcon())
- temp_activity_status += ACTIVITY_STATUS_INTERACTABLE_1188;
- if ((appearance.activity_status & ACTIVITY_STATUS_ROLEPLAYING) > 0)
- temp_activity_status += ACTIVITY_STATUS_ROLEPLAYING_1188;
- if ((appearance.activity_status & ACTIVITY_STATUS_ANONYMOUS) > 0)
- temp_activity_status += ACTIVITY_STATUS_ANONYMOUS_1188;
- if ((appearance.activity_status & ACTIVITY_STATUS_LINKDEAD) > 0)
- temp_activity_status += ACTIVITY_STATUS_LINKDEAD_1188;
- if ((appearance.activity_status & ACTIVITY_STATUS_CAMPING) > 0)
- temp_activity_status += ACTIVITY_STATUS_CAMPING_1188;
- if ((appearance.activity_status & ACTIVITY_STATUS_LFG) > 0)
- temp_activity_status += ACTIVITY_STATUS_LFG_1188;
- if ((appearance.activity_status & ACTIVITY_STATUS_LFW) > 0)
- temp_activity_status += ACTIVITY_STATUS_LFW_1188;
- if ((appearance.activity_status & ACTIVITY_STATUS_SOLID) > 0)
- temp_activity_status += ACTIVITY_STATUS_SOLID_1188;
- if ((appearance.activity_status & ACTIVITY_STATUS_IMMUNITY_GAINED) > 0)
- temp_activity_status += ACTIVITY_STATUS_IMMUNITY_GAINED_1188;
- if ((appearance.activity_status & ACTIVITY_STATUS_IMMUNITY_REMAINING) > 0)
- temp_activity_status += ACTIVITY_STATUS_IMMUNITY_REMAINING_1188;
- if ((appearance.activity_status & ACTIVITY_STATUS_AFK) > 0)
- temp_activity_status += ACTIVITY_STATUS_AFK_1188;
- if (EngagedInCombat())
- temp_activity_status += ACTIVITY_STATUS_INCOMBAT_1188;
- // if this is either a boat or lift let the client be manipulated by the object
- // doesn't work for DoF client version 546
- if (appearance.icon == 28 || appearance.icon == 12 || IsTransportSpawn())
- {
- temp_activity_status += ACTIVITY_STATUS_ISTRANSPORT_1188;
- }
- }
- else
- {
- temp_activity_status = appearance.activity_status;
- if(IsNPC())
- temp_activity_status = 0xFF;
- // this only partially fixes lifts in classic 283 client if you move just as the lift starts to move
- if (appearance.icon == 28 || appearance.icon == 12)
- packet->setDataByName("is_transport", 1);
- if (MeetsSpawnAccessRequirements(spawn))
- packet->setDataByName("hand_icon", appearance.display_hand_icon);
- else {
- if ((req_quests_override & 256) > 0)
- packet->setDataByName("hand_icon", 1);
- }
- if (IsPlayer()) {
- if (((Player*)this)->get_character_flag(CF_AFK))
- packet->setDataByName("afk", 1);
- if ((appearance.activity_status & ACTIVITY_STATUS_ROLEPLAYING) > 0)
- packet->setDataByName("roleplaying", 1);
- if ((appearance.activity_status & ACTIVITY_STATUS_ANONYMOUS) > 0)
- packet->setDataByName("anonymous", 1);
- if ((appearance.activity_status & ACTIVITY_STATUS_LINKDEAD) > 0)
- packet->setDataByName("linkdead", 1);
- if ((appearance.activity_status & ACTIVITY_STATUS_CAMPING) > 0)
- packet->setDataByName("camping", 1);
- if ((appearance.activity_status & ACTIVITY_STATUS_LFG) > 0)
- packet->setDataByName("lfg", 1);
- }
- if ((appearance.activity_status & ACTIVITY_STATUS_SOLID) > 0)
- packet->setDataByName("solid_object", 1);
- }
- packet->setDataByName("activity_status", temp_activity_status); //appearance.activity_status);
- // If player and player has a follow target
- /* Jan 2021 Note!! Setting follow_target 0xFFFFFFFF has the result in causing strange behavior in swimming. Targetting a mob makes you focus down to its swim level, unable to swim above it.
- ** in the same respect the player will drop like a rock to the bottom of the ocean (seems to be when self set to that flag?)
- ** for now disabling this, if DoF needs it enabled for whatever reason then we need a version check added.
- */
- if (IsPlayer()) {
- if (((Player*)this)->GetFollowTarget())
- packet->setDataByName("follow_target", ((((Player*)this)->GetIDWithPlayerSpawn(((Player*)this)->GetFollowTarget()) * -1) - 1));
- //else
- // packet->setDataByName("follow_target", 0xFFFFFFFF);
- }
- //else if (!IsPet()) {
- // packet->setDataByName("follow_target", 0xFFFFFFFF);
- //}
-
- if (GetTarget() && GetTarget()->GetTargetable())
- packet->setDataByName("target_id", ((spawn->GetIDWithPlayerSpawn(GetTarget()) * -1) - 1));
- else
- packet->setDataByName("target_id", 0xFFFFFFFF);
- //Send spell effects for target window
- if(IsEntity()){
- InfoStruct* info = ((Entity*)this)->GetInfoStruct();
- int8 i = 0;
- int16 backdrop = 0;
- int16 spell_icon = 0;
- int32 spell_id = 0;
- LuaSpell* spell = 0;
- ((Entity*)this)->GetSpellEffectMutex()->readlock(__FUNCTION__, __LINE__);
- while(i < 30){
- //Change value of spell id for this packet if spell exists
- spell_id = info->spell_effects[i].spell_id;
- if(spell_id > 0)
- spell_id = 0xFFFFFFFF - spell_id;
- else
- spell_id = 0;
- packet->setSubstructDataByName("spell_effects", "spell_id", spell_id, i);
- //Change value of spell icon for this packet if spell exists
- spell_icon = info->spell_effects[i].icon;
- if(spell_icon > 0){
- if(!(spell_icon == 0xFFFF))
- spell_icon = 0xFFFF - spell_icon;
- }
- else
- spell_icon = 0;
- packet->setSubstructDataByName("spell_effects", "spell_icon", spell_icon, i);
- //Change backdrop values to match values in this packet
- backdrop = info->spell_effects[i].icon_backdrop;
- switch(backdrop){
- case 312:
- backdrop = 33080;
- break;
- case 313:
- backdrop = 33081;
- break;
- case 314:
- backdrop = 33082;
- break;
- case 315:
- backdrop = 33083;
- break;
- case 316:
- backdrop = 33084;
- break;
- case 317:
- backdrop = 33085;
- break;
- case (318 || 319):
- backdrop = 33086;
- break;
- default:
- break;
- }
- packet->setSubstructDataByName("spell_effects", "spell_icon_backdrop", backdrop, i);
- spell = info->spell_effects[i].spell;
- if (spell)
- packet->setSubstructDataByName("spell_effects", "spell_triggercount", spell->num_triggers, i);
- i++;
- }
- ((Entity*)this)->GetSpellEffectMutex()->releasereadlock(__FUNCTION__, __LINE__);
- }
- }
- void Spawn::MoveToLocation(Spawn* spawn, float distance, bool immediate, bool mapped){
- if(!spawn)
- return;
- SetRunningTo(spawn);
- FaceTarget(spawn);
- if (!IsPlayer() && distance > 0.0f)
- {
- if ((IsFlyingCreature() || IsWaterCreature()) && CheckLoS(spawn))
- {
- if (immediate)
- ClearRunningLocations();
- AddRunningLocation(spawn->GetX(), spawn->GetY(), spawn->GetZ(), GetSpeed(), distance, true, true, "", true);
- }
- else if (/*!mapped && */GetZone())
- {
- GetZone()->movementMgr->NavigateTo((Entity*)this, spawn->GetX(), spawn->GetY(), spawn->GetZ());
- last_grid_update = Timer::GetCurrentTime2();
- }
- else
- {
- if (immediate)
- ClearRunningLocations();
- AddRunningLocation(spawn->GetX(), spawn->GetY(), spawn->GetZ(), GetSpeed(), distance, true, true, "", mapped);
- }
- }
- }
- void Spawn::ProcessMovement(bool isSpawnListLocked){
- CheckProximities();
-
- if(IsPlayer()){
- //Check if player is riding a boat, if so update pos (boat's current location + XYZ offsets)
- Player* player = ((Player*)this);
- int32 boat_id = player->GetBoatSpawn();
- Spawn* boat = 0;
- if(boat_id > 0)
- boat = GetZone()->GetSpawnByID(boat_id, isSpawnListLocked);
- //TODO: MAYBE do this for real boats, not lifts... GetWidgetTypeNameByTypeID
- /*if(boat){
- SetX(boat->GetX() + player->GetBoatX());
- SetY(boat->GetY() + player->GetBoatY());
- SetZ(boat->GetZ() + player->GetBoatZ());
- }*/
- return;
- }
- if (forceMapCheck && GetZone() != nullptr && GetMap() != nullptr && GetMap()->IsMapLoaded())
- {
- FixZ(true);
- int32 newGrid = GetMap()->GetGrid()->GetGridID(this);
- if ((IsTransportSpawn() || !IsFlyingCreature()) && newGrid != 0 && newGrid != appearance.pos.grid_id)
- SetPos(&(appearance.pos.grid_id), newGrid);
- forceMapCheck = false;
- }
- if (GetHP() <= 0 && !IsWidget())
- return;
- if (EngagedInCombat())
- {
- int locations = 0;
- if (movement_locations && MMovementLocations)
- {
- MMovementLocations->readlock(__FUNCTION__, __LINE__);
- locations = movement_locations->size();
- MMovementLocations->releasereadlock(__FUNCTION__, __LINE__);
- }
- if (locations < 1 && GetZone() && ((Entity*)this)->IsFeared())
- {
- CalculateNewFearpoint();
- }
- }
- Spawn* followTarget = GetZone()->GetSpawnByID(m_followTarget, isSpawnListLocked);
- if (!followTarget && m_followTarget > 0)
- m_followTarget = 0;
- if (following && !IsPauseMovementTimerActive() && followTarget && !((Entity*)this)->IsFeared()) {
- // Need to clear m_followTarget before the zoneserver deletes it
- if (followTarget->GetHP() <= 0) {
- followTarget = 0;
- return;
- }
- if (!IsEntity() || (!((Entity*)this)->IsCasting() && !((Entity*)this)->IsMezzedOrStunned() && !((Entity*)this)->IsRooted())) {
- if (GetBaseSpeed() > 0) {
- CalculateRunningLocation();
- }
- else {
- float speed = 4.0f;
- if (IsEntity())
- speed = ((Entity*)this)->GetMaxSpeed();
- if (IsEntity())
- ((Entity*)this)->SetSpeed(speed);
-
- SetSpeed(speed);
- }
- MovementLocation* loc = GetCurrentRunningLocation();
- float dist = GetDistance(followTarget, true);
- if ((!EngagedInCombat() && m_followDistance > 0 && dist <= m_followDistance) ||
- (dist <= rule_manager.GetGlobalRule(R_Combat, MaxCombatRange)->GetFloat())) {
- ClearRunningLocations();
- CalculateRunningLocation(true);
- }
- else if (loc) {
- float distance = GetDistance(followTarget, loc->x, loc->y, loc->z);
- if ( (!EngagedInCombat() && m_followDistance > 0 && distance > m_followDistance) ||
- ( EngagedInCombat() && distance > rule_manager.GetGlobalRule(R_Combat, MaxCombatRange)->GetFloat())) {
- MoveToLocation(followTarget, rule_manager.GetGlobalRule(R_Combat, MaxCombatRange)->GetFloat(), true, loc->mapped);
- CalculateRunningLocation();
- }
- }
- else {
- MoveToLocation(followTarget, rule_manager.GetGlobalRule(R_Combat, MaxCombatRange)->GetFloat(), false);
- CalculateRunningLocation();
- }
- }
- }
- bool movementCase = false;
- // Movement loop is only for scripted paths
- if(!EngagedInCombat() && !IsPauseMovementTimerActive() && !NeedsToResumeMovement() && (!IsNPC() || !((NPC*)this)->m_runningBack)){
- MMovementLoop.writelock();
- if(movement_loop.size() > 0 && movement_index < movement_loop.size())
- {
- movementCase = true;
- // Get the target location
- MovementData* data = movement_loop[movement_index];
- // need to resume our movement
- if(resume_movement){
- if (movement_locations && MMovementLocations){
- MMovementLocations->writelock(__FUNCTION__, __LINE__);
- while (movement_locations->size()){
- safe_delete(movement_locations->front());
- movement_locations->pop_front();
- }
- movement_locations->clear();
- MMovementLocations->releasewritelock(__FUNCTION__, __LINE__);
- }
- data = movement_loop[movement_index];
-
- if(data)
- {
- ((Entity*)this)->SetSpeed(data->speed);
- SetSpeed(data->speed);
- if(data->use_movement_location_heading)
- SetHeading(data->heading);
- else if(!IsWidget())
- FaceTarget(data->x, data->z);
- // 0 delay at target location, need to set multiple locations
- if(data->delay == 0 && movement_loop.size() > 0) {
- int16 tmp_index = movement_index+1;
- MovementData* data2 = 0;
- if(tmp_index < movement_loop.size())
- data2 = movement_loop[tmp_index];
- else
- data2 = movement_loop[0];
- AddRunningLocation(data->x, data->y, data->z, data->speed, 0, true, false, "", true);
- AddRunningLocation(data2->x, data2->y, data2->z, data2->speed, 0, true, true, "", true);
- }
- // delay at target location, only need to set 1 location
- else
- AddRunningLocation(data->x, data->y, data->z, data->speed);
- }
- movement_start_time = 0;
- resume_movement = false;
- }
- // If we are not moving or we have arrived at our destination
- else if(!IsRunning() || (data && data->x == GetX() && data->y == GetY() && data->z == GetZ())){
- // If we were moving remove the last running location (the point we just arrived at)
- if(IsRunning())
- RemoveRunningLocation();
- // If this waypoint has a delay and we just arrived here (movement_start_time == 0)
- if(data && data->delay > 0 && movement_start_time == 0){
- // Set the current time
- movement_start_time = Timer::GetCurrentTime2();
- // If this waypoint had a lua function then call it
- if(data->lua_function.length() > 0)
- GetZone()->CallSpawnScript(this, SPAWN_SCRIPT_CUSTOM, 0, data->lua_function.c_str());
- int16 nextMove;
- if ((int16)(movement_index + 1) < movement_loop.size())
- nextMove = movement_index + 1;
- else
- nextMove = 0;
- // Get the next target location
- data = movement_loop[nextMove];
-
- //Go ahead and face the next location
- if(data)
- FaceTarget(data->x, data->z);
- }
- // If this waypoint has no delay or we have waited the required time (current time >= delay + movement_start_time)
- else if(data && data->delay == 0 || (data && data->delay > 0 && Timer::GetCurrentTime2() >= (data->delay+movement_start_time))) {
- // if no delay at this waypoint but a lua function for it then call the function
- if(data->delay == 0 && data->lua_function.length() > 0)
- GetZone()->CallSpawnScript(this, SPAWN_SCRIPT_CUSTOM, 0, data->lua_function.c_str());
- // since we ran a lua function make sure the movement loop is still alive and accurate
- if(movement_loop.size() > 0)
- {
- // Advance the current movement loop index
- if((int16)(movement_index+1) < movement_loop.size())
- movement_index++;
- else
- movement_index = 0;
- // Get the next target location
- data = movement_loop[movement_index];
- // set the speed for that location
- SetSpeed(data->speed);
- if(!IsWidget())
- // turn towards the location
- FaceTarget(data->x, data->z);
- // If 0 delay at location get and set data for the point after it
- if(data->delay == 0 && movement_loop.size() > 0){
- if(movement_locations && MMovementLocations)
- {
- MMovementLocations->writelock(__FUNCTION__, __LINE__);
- while (movement_locations->size()){
- safe_delete(movement_locations->front());
- movement_locations->pop_front();
- }
- // clear current target locations
- movement_locations->clear();
- MMovementLocations->releasewritelock(__FUNCTION__, __LINE__);
- }
- // get the data for the location after out new location
- int16 tmp_index = movement_index+1;
- MovementData* data2 = 0;
- if(tmp_index < movement_loop.size())
- data2 = movement_loop[tmp_index];
- else
- data2 = movement_loop[0];
- // set the first location (adds it to movement_locations that we just cleared)
- AddRunningLocation(data->x, data->y, data->z, data->speed, 0, true, false, "", true);
- // set the location after that
- AddRunningLocation(data2->x, data2->y, data2->z, data2->speed, 0, true, true, "", true);
- }
- // there is a delay at the next location so we only need to set it
- else
- AddRunningLocation(data->x, data->y, data->z, data->speed, 0, true, true, "", true);
- // reset this timer to 0 now that we are moving again
- movement_start_time = 0;
- }
- }
- }
- // moving and not at target location yet
- else if(GetBaseSpeed() > 0)
- CalculateRunningLocation();
- // not moving, have a target location but not at it yet
- else if (data) {
- SetSpeed(data->speed);
- AddRunningLocation(data->x, data->y, data->z, data->speed);
- }
- }
- MMovementLoop.releasewritelock();
- }
-
- if (!movementCase && IsRunning() && !IsPauseMovementTimerActive()) {
- CalculateRunningLocation();
- //last_movement_update = Timer::GetCurrentTime2();
- }
- else if(movementCase)
- {
- //last_movement_update = Timer::GetCurrentTime2();
- }
- /*else if (IsNPC() && !IsRunning() && !EngagedInCombat() && ((NPC*)this)->GetRunbackLocation()) {
- // Is an npc that is not moving and not engaged in combat but has a run back location set then clear the runback location
- LogWrite(NPC_AI__DEBUG, 7, "NPC_AI", "Clear runback location for %s", GetName());
- ((NPC*)this)->ClearRunback();
- resume_movement = true;
- NeedsToResumeMovement(false);
- }*/
- }
- void Spawn::ResetMovement(bool inFlight){
- if(!inFlight)
- MMovementLoop.writelock();
- vector<MovementData*>::iterator itr;
- for(itr = movement_loop.begin(); itr != movement_loop.end(); itr++){
- safe_delete(*itr);
- }
- movement_loop.clear();
- movement_index = 0;
- resume_movement = true;
- ClearRunningLocations();
- if(!inFlight)
- MMovementLoop.releasewritelock();
- }
- void Spawn::AddMovementLocation(float x, float y, float z, float speed, int16 delay, const char* lua_function, float heading, bool include_heading){
- LogWrite(LUA__DEBUG, 5, "LUA", "AddMovementLocation: x: %.2f, y: %.2f, z: %.2f, speed: %.2f, delay: %i, lua: %s",
- x, y, z, speed, delay, string(lua_function).c_str());
- MovementData* data = new MovementData;
- data->x = x;
- data->y = y;
- data->z = z;
- data->speed = speed;
- data->delay = delay*1000;
- if(lua_function)
- data->lua_function = string(lua_function);
-
- data->heading = heading;
- data->use_movement_location_heading = include_heading;
- MMovementLoop.lock();
- movement_loop.push_back(data);
- MMovementLoop.unlock();
- }
- bool Spawn::IsRunning(){
- bool movement = false;
- if(movement_locations && MMovementLocations)
- {
- MMovementLocations->readlock(__FUNCTION__, __LINE__);
- movement = movement_locations->size() > 0;
- MMovementLocations->releasereadlock(__FUNCTION__, __LINE__);
- if(movement)
- return true;
- }
- MMovementLoop.lock();
- movement = movement_loop.size() > 0;
- MMovementLoop.unlock();
- return movement;
- }
- void Spawn::RunToLocation(float x, float y, float z, float following_x, float following_y, float following_z){
- if(IsPauseMovementTimerActive())
- return;
-
- if(!IsWidget())
- FaceTarget(x, z);
- SetPos(&appearance.pos.X2, x, false);
- SetPos(&appearance.pos.Z2, z, false);
- SetPos(&appearance.pos.Y2, y, false);
- if(following_x == 0 && following_y == 0 && following_z == 0){
- SetPos(&appearance.pos.X3, x, false);
- SetPos(&appearance.pos.Z3, z, false);
- SetPos(&appearance.pos.Y3, y, false);
- }
- else{
- SetPos(&appearance.pos.X3, following_x, false);
- SetPos(&appearance.pos.Y3, following_y, false);
- SetPos(&appearance.pos.Z3, following_z, false);
- }
- position_changed = true;
- changed = true;
- GetZone()->AddChangedSpawn(this);
- }
- MovementLocation* Spawn::GetCurrentRunningLocation(){
- MovementLocation* ret = 0;
- if(movement_locations && movement_locations->size() > 0){
- MMovementLocations->readlock(__FUNCTION__, __LINE__);
- ret = movement_locations->front();
- MMovementLocations->releasereadlock(__FUNCTION__, __LINE__);
- }
- return ret;
- }
- MovementLocation* Spawn::GetLastRunningLocation(){
- MovementLocation* ret = 0;
- if(movement_locations && movement_locations->size() > 0){
- MMovementLocations->readlock(__FUNCTION__, __LINE__);
- ret = movement_locations->back();
- MMovementLocations->releasereadlock(__FUNCTION__, __LINE__);
- }
- return ret;
- }
- 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){
- if(speed == 0)
- return;
- if ( IsEntity() )
- ((Entity*)this)->SetSpeed(speed);
- else
- this->SetSpeed(speed);
- MovementLocation* current_location = 0;
- float distance = GetDistance(x, y, z, distance_away != 0);
- if(distance_away != 0){
- distance -= distance_away;
- x = x - (GetX() - x)*distance_away/distance;
- z = z - (GetZ() - z)*distance_away/distance;
- }
- if(!movement_locations){
- movement_locations = new deque<MovementLocation*>();
- MMovementLocations = new Mutex();
- }
- MovementLocation* data = new MovementLocation;
- data->mapped = isMapped;
- data->x = x;
- data->y = y;
- data->z = z;
- data->speed = speed;
- data->attackable = attackable;
- data->lua_function = lua_function;
- data->gridid = 0; // used for runback defaults
- MMovementLocations->writelock(__FUNCTION__, __LINE__);
- if(movement_locations->size() > 0)
- current_location = movement_locations->back();
- if(!current_location){
- SetSpawnOrigX(GetX());
- SetSpawnOrigY(GetY());
- SetSpawnOrigZ(GetZ());
- SetSpawnOrigHeading(GetHeading());
- }
- movement_locations->push_back(data);
- if(!IsPauseMovementTimerActive() && finished_adding_locations){
- current_location = movement_locations->front();
- SetSpeed(current_location->speed);
- if(movement_locations->size() > 1){
- data = movement_locations->at(1);
- RunToLocation(current_location->x, current_location->y, current_location->z, data->x, data->y, data->z);
- }
- else
- RunToLocation(current_location->x, current_location->y, current_location->z, 0, 0, 0);
- }
- MMovementLocations->releasewritelock(__FUNCTION__, __LINE__);
- }
- bool Spawn::RemoveRunningLocation(){
- bool ret = false;
- if(movement_locations){
- MMovementLocations->writelock(__FUNCTION__, __LINE__);
- if(movement_locations->size() > 0){
- delete movement_locations->front();
- movement_locations->pop_front();
- ret = true;
- }
- MMovementLocations->releasewritelock(__FUNCTION__, __LINE__);
- }
- return ret;
- }
- void Spawn::ClearRunningLocations(){
- while(RemoveRunningLocation()){}
- }
- void Spawn::NewWaypointChange(MovementLocation* data){
- if(data){
- if(NeedsToResumeMovement()){
- resume_movement = true;
- NeedsToResumeMovement(false);
- }
- if(!data->attackable)
- SetHeading(GetSpawnOrigHeading());
- }
-
- if (data && data->lua_function.length() > 0)
- GetZone()->CallSpawnScript(this, SPAWN_SCRIPT_CUSTOM, 0, data->lua_function.c_str());
- RemoveRunningLocation();
- }
- bool Spawn::CalculateChange(){
- bool remove_needed = false;
- MovementLocation* data = 0;
- if(movement_locations && MMovementLocations){
- MMovementLocations->readlock(__FUNCTION__, __LINE__);
- if(movement_locations->size() > 0){
- // Target location
- data = movement_locations->front();
- // If no target or we are at the target location need to remove this point
- if(!data || (data->x == GetX() && data->y == GetY() && data->z == GetZ()))
- remove_needed = true;
- }
- MMovementLocations->releasereadlock(__FUNCTION__, __LINE__);
- }
-
- if(remove_needed){
- NewWaypointChange(data);
- }
- else if(data){
- // Speed is per second so we need a time_step (amount of time since the last update) to modify movement by
- 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
- // Get current location
- float nx = GetX();
- float ny = GetY();
- float nz = GetZ();
-
- // Get Forward vecotr
- float tar_vx = data->x - nx;
- float tar_vy = data->y - ny;
- float tar_vz = data->z - nz;
- // Multiply speed by the time_step to get how much should have changed over the last tick
- float speed = GetSpeed() * time_step;
- // Normalize the forward vector and multiply by speed, this gives us our change in coords, just need to add them to our current coords
- float len = sqrtf(tar_vx * tar_vx + tar_vy * tar_vy + tar_vz * tar_vz);
- tar_vx = (tar_vx / len) * speed;
- tar_vy = (tar_vy / len) * speed;
- tar_vz = (tar_vz / len) * speed;
- // Distance less then 0.5 just set the npc to the target location
- if (GetDistance(data->x, data->y, data->z, IsWidget() ? false : true) <= speed) {
- SetX(data->x, false);
- SetZ(data->z, false);
- SetY(data->y, false, true);
- remove_needed = true;
- NewWaypointChange(data);
- }
- else {
- SetX(nx + tar_vx, false);
- SetZ(nz + tar_vz, false);
- if ( IsWidget() )
- SetY(ny + tar_vy, false, true);
- else
- SetY(ny + tar_vy, false);
- }
-
- if (GetMap() != nullptr) {
- Cell* newCell = GetMap()->GetGrid()->GetCell(GetX(), GetZ());
- int32 newGrid = GetMap()->GetGrid()->GetGridID(this);
- if ((!IsFlyingCreature() || IsTransportSpawn()) && newGrid != 0 && newGrid != appearance.pos.grid_id)
- SetPos(&(appearance.pos.grid_id), newGrid);
- }
- }
- return remove_needed;
- }
- void Spawn::CalculateRunningLocation(bool stop){
- bool pauseTimerEnabled = IsPauseMovementTimerActive();
- if (!pauseTimerEnabled && !stop && (last_location_update + 100) > Timer::GetCurrentTime2())
- return;
- else if (!pauseTimerEnabled && !stop)
- last_location_update = Timer::GetCurrentTime2();
- bool removed = CalculateChange();
- if (stop || pauseTimerEnabled) {
- //following = false;
- SetPos(&appearance.pos.X2, GetX(), false);
- SetPos(&appearance.pos.Y2, GetY(), false);
- SetPos(&appearance.pos.Z2, GetZ(), false);
- SetPos(&appearance.pos.X3, GetX(), false);
- SetPos(&appearance.pos.Y3, GetY(), false);
- SetPos(&appearance.pos.Z3, GetZ(), false);
- }
- else if (removed && movement_locations && movement_locations->size() > 0) {
- if (MMovementLocations)
- MMovementLocations->readlock(__FUNCTION__, __LINE__);
- MovementLocation* current_location = movement_locations->at(0);
- if (movement_locations->size() > 1) {
- MovementLocation* data = movement_locations->at(1);
- RunToLocation(current_location->x, current_location->y, current_location->z, data->x, data->y, data->z);
- }
- else
- RunToLocation(current_location->x, current_location->y, current_location->z, 0, 0, 0);
- if (MMovementLocations)
- MMovementLocations->releasereadlock(__FUNCTION__, __LINE__);
- }
- else if (GetZone() && GetTarget() != NULL && EngagedInCombat())
- {
- if (GetDistance(GetTarget()) > rule_manager.GetGlobalRule(R_Combat, MaxCombatRange)->GetFloat())
- {
- if ((IsFlyingCreature() || IsWaterCreature()) && CheckLoS(GetTarget()))
- AddRunningLocation(GetTarget()->GetX(), GetTarget()->GetY(), GetTarget()->GetZ(), GetSpeed(), 0, false);
- else
- GetZone()->movementMgr->NavigateTo((Entity*)this, GetTarget()->GetX(), GetTarget()->GetY(), GetTarget()->GetZ());
- }
- else
- ((Entity*)this)->HaltMovement();
- }
- else if (!following)
- {
- position_changed = true;
- changed = true;
- GetZone()->AddChangedSpawn(this);
- }
- }
- float Spawn::GetFaceTarget(float x, float z) {
- float angle;
- double diff_x = x - GetX();
- double diff_z = z - GetZ();
- //If we're very close to the same spot don't bother changing heading
- if (sqrt(diff_x * diff_x * diff_z * diff_z) < .1) {
- return GetHeading();
- }
- if (diff_z == 0) {
- if (diff_x > 0)
- angle = 90;
- else
- angle = 270;
- }
- else
- angle = ((atan(diff_x / diff_z)) * 180) / 3.14159265358979323846;
- if (angle < 0)
- angle = angle + 360;
- else
- angle = angle + 180;
- if (diff_x < 0)
- angle = angle + 180;
- if (last_heading_angle == angle) return angle;
- return angle;
- }
- void Spawn::FaceTarget(float x, float z){
- float angle;
- double diff_x = x - GetX();
- double diff_z = z - GetZ();
- //If we're very close to the same spot don't bother changing heading
- if (sqrt(diff_x * diff_x * diff_z * diff_z) < .1) {
- return;
- }
- if(diff_z==0){
- if(diff_x > 0)
- angle = 90;
- else
- angle = 270;
- }
- else
- angle = ((atan(diff_x / diff_z)) * 180) / 3.14159265358979323846;
- if(angle < 0)
- angle = angle + 360;
- else
- angle = angle + 180;
- if(diff_x < 0)
- angle = angle + 180;
- if (last_heading_angle == angle) return;
- SetHeading(angle);
- }
- void Spawn::FaceTarget(Spawn* target, bool disable_action_state){
- if(!target)
- return;
- FaceTarget(target->GetX(), target->GetZ());
- if(GetHP() > 0 && target->IsPlayer() && !EngagedInCombat()){
- GetZone()->AddHeadingTimer(this);
- if(disable_action_state)
- SetTempActionState(0);
- }
- }
- bool Spawn::MeetsSpawnAccessRequirements(Player* player){
- bool ret = false;
- Quest* quest = 0;
- //Check if we meet all quest requirements first..
- m_requiredQuests.readlock(__FUNCTION__, __LINE__);
- if (player && required_quests.size() > 0) {
- map<int32, vector<int16>* >::iterator itr;
- for (itr = required_quests.begin(); itr != required_quests.end(); itr++) {
- player->AddQuestRequiredSpawn(this, itr->first);
- vector<int16>* quest_steps = itr->second;
- for (int32 i = 0; i < quest_steps->size(); i++) {
- quest = player->GetQuest(itr->first);
- if (req_quests_continued_access) {
- if (quest) {
- if (quest->GetQuestStepCompleted(quest_steps->at(i))) {
- ret = true;
- break;
- }
- }
- else if (player->GetCompletedQuest(itr->first)) {
- ret = true;
- break;
- }
- }
- if (quest && quest->QuestStepIsActive(quest_steps->at(i))) {
- ret = true;
- break;
- }
- }
- }
- }
- else
- ret = true;
- m_requiredQuests.releasereadlock(__FUNCTION__, __LINE__);
- if (!ret)
- return ret;
- //Now check if the player meets all history requirements
- m_requiredHistory.readlock(__FUNCTION__, __LINE__);
- if (required_history.size() > 0){
- map<int32, LUAHistory*>::iterator itr;
- for (itr = required_history.begin(); itr != required_history.end(); itr++){
- player->AddHistoryRequiredSpawn(this, itr->first);
- LUAHistory* player_history = player->GetLUAHistory(itr->first);
- if (player_history){
- if (player_history->Value != itr->second->Value || player_history->Value2 != itr->second->Value2)
- ret = false;
- }
- else
- ret = false;
- if (!ret)
- break;
- }
- }
- m_requiredHistory.releasereadlock(__FUNCTION__, __LINE__);
- return ret;
- }
- vector<Spawn*>* Spawn::GetSpawnGroup(){
- vector<Spawn*>* ret_list = 0;
- if(spawn_group_list){
- ret_list = new vector<Spawn*>();
- if(MSpawnGroup)
- MSpawnGroup->readlock(__FUNCTION__, __LINE__);
- ret_list->insert(ret_list->begin(), spawn_group_list->begin(), spawn_group_list->end());
- if(MSpawnGroup)
- MSpawnGroup->releasereadlock(__FUNCTION__, __LINE__);
- }
- return ret_list;
- }
- bool Spawn::HasSpawnGroup() {
- return spawn_group_list && spawn_group_list->size() > 0;
- }
- bool Spawn::IsInSpawnGroup(Spawn* spawn) {
- bool ret = false;
- if (HasSpawnGroup() && spawn) {
- vector<Spawn*>::iterator itr;
- for (itr = spawn_group_list->begin(); itr != spawn_group_list->end(); itr++) {
- if ((*itr) == spawn) {
- ret = true;
- break;
- }
- }
- }
- return ret;
- }
- void Spawn::AddSpawnToGroup(Spawn* spawn){
- if(!spawn)
- return;
- if(!spawn_group_list){
- spawn_group_list = new vector<Spawn*>();
- spawn_group_list->push_back(this);
- safe_delete(MSpawnGroup);
- MSpawnGroup = new Mutex();
- MSpawnGroup->SetName("Spawn::MSpawnGroup");
- }
- vector<Spawn*>::iterator itr;
- MSpawnGroup->writelock(__FUNCTION__, __LINE__);
- for(itr = spawn_group_list->begin(); itr != spawn_group_list->end(); itr++){
- if((*itr) == spawn){
- MSpawnGroup->releasewritelock(__FUNCTION__, __LINE__);
- return;
- }
- }
- spawn_group_list->push_back(spawn);
- spawn->SetSpawnGroupList(spawn_group_list, MSpawnGroup);
- MSpawnGroup->releasewritelock(__FUNCTION__, __LINE__);
- }
- void Spawn::SetSpawnGroupList(vector<Spawn*>* list, Mutex* mutex){
- spawn_group_list = list;
- MSpawnGroup = mutex;
- }
- void Spawn::RemoveSpawnFromGroup(bool erase_all){
- SetSpawnGroupID(0);
- bool del = false;
- if(MSpawnGroup){
- MSpawnGroup->writelock(__FUNCTION__, __LINE__);
- if(spawn_group_list){
- vector<Spawn*>::iterator itr;
- Spawn* spawn = 0;
- if(spawn_group_list->size() == 1)
- erase_all = true;
- for(itr = spawn_group_list->begin(); itr != spawn_group_list->end(); itr++){
- spawn = *itr;
- if (spawn) {
- if(!erase_all){
- if(spawn == this){
- spawn_group_list->erase(itr);
- MSpawnGroup->releasewritelock(__FUNCTION__, __LINE__);
- spawn_group_list = 0;
- MSpawnGroup = 0;
- return;
- }
- }
- else{
- if (spawn != this)
- spawn->SetSpawnGroupList(0, 0);
- }
- }
- }
- if (erase_all)
- spawn_group_list->clear();
- del = (spawn_group_list->size() == 0);
- }
- MSpawnGroup->releasewritelock(__FUNCTION__, __LINE__);
- if (del){
- safe_delete(MSpawnGroup);
- safe_delete(spawn_group_list);
- }
- }
- }
- void Spawn::SetSpawnGroupID(int32 id){
- m_SpawnMutex.writelock();
- group_id = id;
- m_SpawnMutex.releasewritelock();
- }
- int32 Spawn::GetSpawnGroupID(){
- int32 groupid = 0;
- m_SpawnMutex.readlock();
- groupid = group_id;
- m_SpawnMutex.releasereadlock();
- return groupid;
- }
- void Spawn::AddChangedZoneSpawn(){
- if(send_spawn_changes && GetZone())
- GetZone()->AddChangedSpawn(this);
- }
- void Spawn::RemoveSpawnAccess(Spawn* spawn) {
- if (allowed_access.count(spawn->GetID()) > 0) {
- allowed_access.erase(spawn->GetID());
- GetZone()->HidePrivateSpawn(this);
- }
- }
- void Spawn::SetFollowTarget(Spawn* spawn, int32 follow_distance) {
- if (spawn && spawn != this) {
- m_followTarget = spawn->GetID();
- m_followDistance = follow_distance;
- }
- else {
- m_followTarget = 0;
- if (following)
- following = false;
- m_followDistance = 0;
- }
- }
- void Spawn::AddTempVariable(string var, string val) {
- m_tempVariableTypes[var] = 5;
- m_tempVariables[var] = val;
- }
- void Spawn::AddTempVariable(string var, Spawn* val) {
- m_tempVariableTypes[var] = 1;
- m_tempVariableSpawn[var] = val->GetID();
- }
- void Spawn::AddTempVariable(string var, ZoneServer* val) {
- m_tempVariableTypes[var] = 2;
- m_tempVariableZone[var] = val;
- }
- void Spawn::AddTempVariable(string var, Item* val) {
- m_tempVariableTypes[var] = 3;
- m_tempVariableItem[var] = val;
- }
- void Spawn::AddTempVariable(string var, Quest* val) {
- m_tempVariableTypes[var] = 4;
- m_tempVariableQuest[var] = val;
- }
- string Spawn::GetTempVariable(string var) {
- string ret = "";
- if (m_tempVariables.count(var) > 0)
- ret = m_tempVariables[var];
- return ret;
- }
- Spawn* Spawn::GetTempVariableSpawn(string var) {
- Spawn* ret = 0;
-
- if (m_tempVariableSpawn.count(var) > 0)
- ret = GetZone()->GetSpawnByID(m_tempVariableSpawn[var]);
- return ret;
- }
- ZoneServer* Spawn::GetTempVariableZone(string var) {
- ZoneServer* ret = 0;
- if (m_tempVariableZone.count(var) > 0)
- ret = m_tempVariableZone[var];
- return ret;
- }
- Item* Spawn::GetTempVariableItem(string var) {
- Item* ret = 0;
- if (m_tempVariableItem.count(var) > 0)
- ret = m_tempVariableItem[var];
- return ret;
- }
- Quest* Spawn::GetTempVariableQuest(string var) {
- Quest* ret = 0;
- if (m_tempVariableQuest.count(var) > 0)
- ret = m_tempVariableQuest[var];
- return ret;
- }
- int8 Spawn::GetTempVariableType(string var) {
- int8 ret = 0;
- if (m_tempVariableTypes.count(var) > 0)
- ret = m_tempVariableTypes[var];
- return ret;
- }
- void Spawn::DeleteTempVariable(string var) {
- int8 type = GetTempVariableType(var);
- switch (type) {
- case 1:
- m_tempVariableSpawn.erase(var);
- break;
- case 2:
- m_tempVariableZone.erase(var);
- break;
- case 3:
- m_tempVariableItem.erase(var);
- break;
- case 4:
- m_tempVariableQuest.erase(var);
- break;
- case 5:
- m_tempVariables.erase(var);
- break;
- }
- m_tempVariableTypes.erase(var);
- }
- Spawn* Spawn::GetRunningTo() {
- return GetZone()->GetSpawnByID(running_to);
- }
- Spawn* Spawn::GetFollowTarget() {
- return GetZone()->GetSpawnByID(m_followTarget);
- }
- void Spawn::CopySpawnAppearance(Spawn* spawn){
- if (!spawn)
- return;
- //This function copies the appearace of the provided spawn to this one
- if (spawn->IsEntity() && IsEntity()){
- memcpy(&((Entity*)this)->features, &((Entity*)spawn)->features, sizeof(CharFeatures));
- memcpy(&((Entity*)this)->equipment, &((Entity*)spawn)->equipment, sizeof(EQ2_Equipment));
- }
- SetSize(spawn->GetSize());
- SetModelType(spawn->GetModelType());
- }
- void Spawn::SetY(float y, bool updateFlags, bool disableYMapCheck)
- {
- SetPos(&appearance.pos.Y, y, updateFlags);
- if (!disableYMapCheck)
- FixZ();
- }
- float Spawn::FindDestGroundZ(glm::vec3 dest, float z_offset)
- {
- float best_z = BEST_Z_INVALID;
- if (GetZone() != nullptr && GetMap() != nullptr)
- {
- dest.z += z_offset;
- best_z = GetMap()->FindBestZ(dest, nullptr);
- }
- return best_z;
- }
- float Spawn::GetFixedZ(const glm::vec3& destination, int32 z_find_offset) {
- BenchTimer timer;
- timer.reset();
- float new_z = destination.z;
- if (GetZone() != nullptr && GetMap() != nullptr) {
- /* if (flymode == GravityBehavior::Flying)
- return new_z;
- */
- /* if (zone->HasWaterMap() && zone->watermap->InLiquid(glm::vec3(m_Position)))
- return new_z;
- */
- /*
- * Any more than 5 in the offset makes NPC's hop/snap to ceiling in small corridors
- */
- new_z = this->FindDestGroundZ(destination, z_find_offset);
- if (new_z != BEST_Z_INVALID) {
- if (new_z < -2000) {
- new_z = GetY();
- }
- }
- auto duration = timer.elapsed();
- LogWrite(MAP__DEBUG, 0, "Map", "Mob::GetFixedZ() ([{%s}]) returned [{%f}] at [{%f}], [{%f}], [{%f}] - Took [{%f}]",
- this->GetName(),
- new_z,
- destination.x,
- destination.y,
- destination.z,
- duration);
- }
- return new_z;
- }
- void Spawn::FixZ(bool forceUpdate) {
- if (IsPlayer() || IsFlyingCreature() || !GetZone() || (IsObject() && GetZone()->GetInstanceType() == Instance_Type::PERSONAL_HOUSE_INSTANCE)) {
- return;
- }
- /*
- if (flymode == GravityBehavior::Flying) {
- return;
- }*/
- /*
- if (zone->watermap && zone->watermap->InLiquid(m_Position)) {
- return;
- }*/
-
- // we do the inwater check here manually to avoid double calling for a Z coordinate
- glm::vec3 current_loc(GetX(), GetZ(), GetY());
- float new_z = GetFixedZ(current_loc, 1);
-
- if ( region_map != nullptr )
- {
- glm::vec3 targPos(GetX(), GetY(), GetZ());
-
- if(region_map->InWater(targPos, appearance.pos.grid_id))
- return;
- }
-
- if (new_z == GetY())
- return;
- if ((new_z > -2000) && new_z != BEST_Z_INVALID) {
- SetY(new_z, forceUpdate, true);
- }
- else {
- LogWrite(MAP__DEBUG, 0, "Map", "[{%s}] is failing to find Z [{%f}]", this->GetName(), std::abs(GetY() - new_z));
- }
- }
- bool Spawn::CheckLoS(Spawn* target)
- {
- float radiusSrc = 2.0f;
- float radiusTarg = 2.0f;
- glm::vec3 targpos(target->GetX(), target->GetZ(), target->GetY()+radiusTarg);
- glm::vec3 pos(GetX(), GetZ(), GetY()+radiusSrc);
- return CheckLoS(pos, targpos);
- }
- bool Spawn::CheckLoS(glm::vec3 myloc, glm::vec3 oloc)
- {
- ZoneServer* zone = GetZone();
- if (zone == NULL || GetMap() == NULL || !GetMap()->IsMapLoaded())
- return true;
- else
- return GetMap()->CheckLoS(myloc, oloc);
- return false;
- }
- void Spawn::CalculateNewFearpoint()
- {
- if (GetZone() && GetZone()->pathing) {
- auto Node = zone->pathing->GetRandomLocation(glm::vec3(GetX(), GetZ(), GetY()));
- if (Node.x != 0.0f || Node.y != 0.0f || Node.z != 0.0f) {
- AddRunningLocation(Node.x, Node.y, Node.z, GetSpeed(), 0, true, true, "", true);
- }
- }
- }
- Item* Spawn::LootItem(int32 id) {
- Item* ret = 0;
- vector<Item*>::iterator itr;
- MLootItems.lock();
- for (itr = loot_items.begin(); itr != loot_items.end(); itr++) {
- if ((*itr)->details.item_id == id) {
- ret = *itr;
- loot_items.erase(itr);
- break;
- }
- }
- MLootItems.unlock();
- return ret;
- }
- int32 Spawn::GetLootItemID() {
- int32 ret = 0;
- vector<Item*>::iterator itr;
- MLootItems.lock();
- for (itr = loot_items.begin(); itr != loot_items.end(); itr++) {
- ret = (*itr)->details.item_id;
- break;
- }
- MLootItems.unlock();
- return ret;
- }
- bool Spawn::HasLootItemID(int32 id) {
- bool ret = false;
- vector<Item*>::iterator itr;
- MLootItems.readlock(__FUNCTION__, __LINE__);
- for (itr = loot_items.begin(); itr != loot_items.end(); itr++) {
- if ((*itr)->details.item_id == id) {
- ret = true;
- break;
- }
- }
- MLootItems.releasereadlock(__FUNCTION__, __LINE__);
- return ret;
- }
- void Spawn::CheckProximities()
- {
- if (!has_spawn_proximities)
- return;
- if (spawn_proximities.size() > 0)
- {
- MutexList<SpawnProximity*>::iterator itr = spawn_proximities.begin();
- while (itr.Next()) {
- SpawnProximity* prox = itr.value;
- map<int32, bool>::iterator spawnsItr;
- for (spawnsItr = prox->spawns_in_proximity.begin(); spawnsItr != prox->spawns_in_proximity.end(); spawnsItr++) {
- Spawn* tmpSpawn = 0;
- if (spawnsItr->first &&
- ((prox->spawn_type == SPAWNPROXIMITY_DATABASE_ID && (tmpSpawn = GetZone()->GetSpawnByDatabaseID(spawnsItr->first)) != 0) ||
- (prox->spawn_type == SPAWNPROXIMITY_LOCATION_ID && (tmpSpawn = GetZone()->GetSpawnByLocationID(spawnsItr->first)) != 0)))
- {
- if (!spawnsItr->second && tmpSpawn->GetDistance(this) <= prox->distance)
- {
- if (prox->in_range_lua_function.size() > 0)
- GetZone()->CallSpawnScript(this, SPAWN_SCRIPT_CUSTOM, tmpSpawn, prox->in_range_lua_function.c_str());
- spawnsItr->second = true;
- }
- else if (spawnsItr->second && tmpSpawn->GetDistance(this) > prox->distance)
- {
- if (prox->leaving_range_lua_function.size() > 0)
- GetZone()->CallSpawnScript(this, SPAWN_SCRIPT_CUSTOM, tmpSpawn, prox->leaving_range_lua_function.c_str());
- spawnsItr->second = false;
- }
- }
- }
- }
- }
- }
- void Spawn::AddSpawnToProximity(int32 spawnValue, SpawnProximityType type)
- {
- if (!has_spawn_proximities)
- return;
- if (spawn_proximities.size() > 0)
- {
- MutexList<SpawnProximity*>::iterator itr = spawn_proximities.begin();
- while (itr.Next()) {
- SpawnProximity* prox = itr->value;
- if (prox->spawn_value == spawnValue && prox->spawn_type == type)
- prox->spawns_in_proximity.insert(make_pair(spawnValue, false));
- }
- }
- }
- void Spawn::RemoveSpawnFromProximity(int32 spawnValue, SpawnProximityType type)
- {
- if (!has_spawn_proximities)
- return;
- if (spawn_proximities.size() > 0)
- {
- MutexList<SpawnProximity*>::iterator itr = spawn_proximities.begin();
- while (itr.Next()) {
- SpawnProximity* prox = itr->value;
- if (prox->spawn_value == spawnValue && prox->spawn_type == type &&
- prox->spawns_in_proximity.count(spawnValue) > 0)
- prox->spawns_in_proximity.erase(spawnValue);
- }
- }
- }
- 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) {
- EntityCommand* cmd = FindEntityCommand(string(command), true);
- bool newCommand = false;
- if (!cmd)
- {
- newCommand = true;
- cmd = CreateEntityCommand(name, distance, command, error_text, cast_time, spell_visual, !defaultDenyList);
- }
- if (defaultDenyList)
- SetPermissionToEntityCommand(cmd, player, true);
- if (newCommand)
- primary_command_list.push_back(cmd);
- }
- void Spawn::RemovePrimaryEntityCommand(const char* command) {
- vector<EntityCommand*>::iterator itr;
- string tmpStr(command);
- for (itr = primary_command_list.begin(); itr != primary_command_list.end(); itr++) {
- EntityCommand* cmd = *itr;
- if (cmd->command.compare(tmpStr) == 0)
- {
- primary_command_list.erase(itr);
- delete cmd;
- break;
- }
- }
- }
- bool Spawn::SetPermissionToEntityCommand(EntityCommand* command, Player* player, bool permissionValue)
- {
- if(!player)
- return false;
-
- return SetPermissionToEntityCommandByCharID(command, player->GetCharacterID(), permissionValue);
- }
- bool Spawn::SetPermissionToEntityCommandByCharID(EntityCommand* command, int32 charID, bool permissionValue)
- {
- map<int32, bool>::iterator itr = command->allow_or_deny.find(charID);
- if (itr == command->allow_or_deny.end())
- command->allow_or_deny.insert(make_pair(charID, permissionValue));
- else if (itr->second != permissionValue)
- itr->second = permissionValue;
-
- return true;
- }
- void Spawn::RemoveSpawnFromPlayer(Player* player)
- {
- m_Update.writelock(__FUNCTION__, __LINE__);
- player->RemoveSpawn(this); // sets it as removed
- m_Update.releasewritelock(__FUNCTION__, __LINE__);
- }
- bool Spawn::InWater()
- {
- bool inWater = false;
- if ( region_map != nullptr )
- {
- glm::vec3 targPos(GetX(), GetY(), GetZ());
- if ( IsGroundSpawn() )
- targPos.y -= .5f;
-
- if(region_map->InWater(targPos, appearance.pos.grid_id))
- inWater = true;
- }
- return inWater;
- }
- bool Spawn::InLava()
- {
- bool inLava = false;
- if ( region_map != nullptr )
- {
- glm::vec3 targPos(GetX(), GetY(), GetZ());
- if ( IsGroundSpawn() )
- targPos.y -= .5f;
-
- if(region_map->InLava(targPos, appearance.pos.grid_id))
- inLava = true;
- }
- return inLava;
- }
- void Spawn::DeleteRegion(Region_Node* inNode, ZBSP_Node* rootNode)
- {
- map<map<Region_Node*, ZBSP_Node*>, Region_Status>::iterator testitr;
- for (testitr = Regions.begin(); testitr != Regions.end(); testitr++)
- {
- map<Region_Node*, ZBSP_Node*>::const_iterator actualItr = testitr->first.begin();
- Region_Node* node = actualItr->first;
- ZBSP_Node* BSP_Root = actualItr->second;
- if(inNode == node && rootNode == BSP_Root )
- {
- testitr = Regions.erase(testitr);
- break;
- }
- }
- }
- bool Spawn::InRegion(Region_Node* inNode, ZBSP_Node* rootNode)
- {
- map<map<Region_Node*, ZBSP_Node*>, Region_Status>::iterator testitr;
- for (testitr = Regions.begin(); testitr != Regions.end(); testitr++)
- {
- map<Region_Node*, ZBSP_Node*>::const_iterator actualItr = testitr->first.begin();
- Region_Node* node = actualItr->first;
- ZBSP_Node* BSP_Root = actualItr->second;
- if(inNode == node && rootNode == BSP_Root )
- {
- return testitr->second.inRegion;
- }
- }
-
- return false;
- }
- int32 Spawn::GetRegionType(Region_Node* inNode, ZBSP_Node* rootNode)
- {
- map<map<Region_Node*, ZBSP_Node*>, Region_Status>::iterator testitr;
- for (testitr = Regions.begin(); testitr != Regions.end(); testitr++)
- {
- map<Region_Node*, ZBSP_Node*>::const_iterator actualItr = testitr->first.begin();
- Region_Node* node = actualItr->first;
- ZBSP_Node* BSP_Root = actualItr->second;
- if(inNode == node && rootNode == BSP_Root )
- {
- return testitr->second.regionType;
- }
- }
-
- return false;
- }
- float Spawn::SpawnAngle(Spawn* target, float selfx, float selfz)
- {
- if (!target || target == this)
- return 0.0f;
- float angle, lengthb, vectorx, vectorz, dotp;
- float spx = (target->GetX()); // mob xloc (inverse because eq)
- float spz = -(target->GetZ()); // mob yloc
- float heading = target->GetHeading(); // mob heading
- if (heading < 270)
- heading += 90;
- else
- heading -= 270;
- heading = heading * 3.1415f / 180.0f; // convert to radians
- vectorx = spx + (10.0f * std::cos(heading)); // create a vector based on heading
- vectorz = spz + (10.0f * std::sin(heading)); // of spawn length 10
- // length of spawn to player vector
- lengthb = (float) std::sqrt(((selfx - spx) * (selfx - spx)) + ((-selfz - spz) * (-selfz - spz)));
- // calculate dot product to get angle
- // Handle acos domain errors due to floating point rounding errors
- dotp = ((vectorx - spx) * (selfx - spx) +
- (vectorz - spz) * (-selfz - spz)) / (10.0f * lengthb);
- if (dotp > 1)
- return 0.0f;
- else if (dotp < -1)
- return 180.0f;
- angle = std::acos(dotp);
- angle = angle * 180.0f / 3.1415f;
- return angle;
- }
- void Spawn::StopMovement()
- {
- ResetMovement(true);
- }
- bool Spawn::PauseMovement(int32 period_of_time_ms)
- {
- if(period_of_time_ms < 1)
- period_of_time_ms = 1;
-
- RunToLocation(GetX(),GetY(),GetZ());
- pause_timer.Start(period_of_time_ms, true);
- return true;
- }
- bool Spawn::IsPauseMovementTimerActive()
- {
- if(pause_timer.Check())
- pause_timer.Disable();
-
- return pause_timer.Enabled();
- }
- bool Spawn::IsFlyingCreature()
- {
- if(!IsEntity())
- return false;
- return ((Entity*)this)->GetInfoStruct()->get_flying_type();
- }
- bool Spawn::IsWaterCreature()
- {
- if(!IsEntity())
- return false;
- return ((Entity*)this)->GetInfoStruct()->get_water_type();
- }
- void Spawn::SetFlyingCreature() {
- if(!IsEntity() || !rule_manager.GetGlobalRule(R_Spawn, UseHardCodeFlyingModelType)->GetInt8())
- return;
- if(((Entity*)this)->GetInfoStruct()->get_flying_type() > 0) // DB spawn npc flag already set
- return;
- switch (GetModelType())
- {
- case 260:
- case 295:
- ((Entity*)this)->GetInfoStruct()->set_flying_type(1);
- is_flying_creature = true;
- break;
- default:
- ((Entity*)this)->GetInfoStruct()->set_flying_type(0);
- break;
- }
- }
-
- void Spawn::SetWaterCreature() {
- if(!IsEntity() || !rule_manager.GetGlobalRule(R_Spawn, UseHardCodeWaterModelType)->GetInt8())
- return;
- if(((Entity*)this)->GetInfoStruct()->get_water_type() > 0) // DB spawn npc flag already set
- return;
- switch (GetModelType())
- {
- case 194:
- case 204:
- case 210:
- case 241:
- case 242:
- case 254:
- case 10668:
- case 20828:
- ((Entity*)this)->GetInfoStruct()->set_water_type(1);
- break;
- default:
- ((Entity*)this)->GetInfoStruct()->set_water_type(0);
- break;
- }
- }
- void Spawn::AddRailPassenger(int32 char_id)
- {
- std::lock_guard<std::mutex> lk(m_RailMutex);
- rail_passengers.insert(make_pair(char_id,true));
- }
- void Spawn::RemoveRailPassenger(int32 char_id)
- {
- std::lock_guard<std::mutex> lk(m_RailMutex);
- std::map<int32, bool>::iterator itr = rail_passengers.find(char_id);
- if(itr != rail_passengers.end())
- rail_passengers.erase(itr);
- }
- vector<Spawn*> Spawn::GetPassengersOnRail() {
- vector<Spawn*> tmp_list;
- Spawn* spawn;
- m_RailMutex.lock();
- std::map<int32, bool>::iterator itr = rail_passengers.begin();
- while(itr != rail_passengers.end()){
- Client* client = zone_list.GetClientByCharID(itr->first);
- if(!client || !client->GetPlayer())
- continue;
- tmp_list.push_back(client->GetPlayer());
- itr++;
- }
- m_RailMutex.unlock();
- return tmp_list;
- }
|