9
3

Player.cpp 265 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691
  1. /*
  2. EQ2Emulator: Everquest II Server Emulator
  3. Copyright (C) 2007 EQ2EMulator Development Team (http://www.eq2emulator.net)
  4. This file is part of EQ2Emulator.
  5. EQ2Emulator is free software: you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation, either version 3 of the License, or
  8. (at your option) any later version.
  9. EQ2Emulator is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with EQ2Emulator. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #include "Player.h"
  17. #include "../common/MiscFunctions.h"
  18. #include "World.h"
  19. #include "WorldDatabase.h"
  20. #include <math.h>
  21. #include "classes.h"
  22. #include "LuaInterface.h"
  23. #include "../common/Log.h"
  24. #include "Rules/Rules.h"
  25. #include "Titles.h"
  26. #include "Languages.h"
  27. #include "SpellProcess.h"
  28. #include <algorithm>
  29. #include <regex>
  30. #include "ClientPacketFunctions.h"
  31. extern Classes classes;
  32. extern WorldDatabase database;
  33. extern World world;
  34. extern ConfigReader configReader;
  35. extern MasterSkillList master_skill_list;
  36. extern MasterSpellList master_spell_list;
  37. extern MasterQuestList master_quest_list;
  38. extern Variables variables;
  39. extern LuaInterface* lua_interface;
  40. extern MasterItemList master_item_list;
  41. extern RuleManager rule_manager;
  42. extern MasterTitlesList master_titles_list;
  43. extern MasterLanguagesList master_languages_list;
  44. Player::Player(){
  45. tutorial_step = 0;
  46. char_id = 0;
  47. group = 0;
  48. appearance.pos.grid_id = 0;
  49. spawn_index = 1;
  50. info = 0;
  51. movement_packet = 0;
  52. last_movement_activity = 0;
  53. //speed = 0;
  54. packet_num = 0;
  55. range_attack = false;
  56. old_movement_packet = 0;
  57. charsheet_changed = false;
  58. quickbar_updated = false;
  59. custNPC = false;
  60. spawn_tmp_vis_xor_packet = 0;
  61. spawn_tmp_pos_xor_packet = 0;
  62. spawn_tmp_info_xor_packet = 0;
  63. pending_collection_reward = 0;
  64. pos_packet_speed = 0;
  65. appearance.display_name = 1;
  66. appearance.show_command_icon = 1;
  67. appearance.player_flag = 1;
  68. appearance.targetable = 1;
  69. appearance.show_level = 1;
  70. spell_count = 0;
  71. spell_orig_packet = 0;
  72. spell_xor_packet = 0;
  73. raid_orig_packet = nullptr;
  74. raid_xor_packet = nullptr;
  75. resurrecting = false;
  76. spawn_id = 1;
  77. spawn_type = 4;
  78. player_spawn_id_map[1] = this;
  79. player_spawn_reverse_id_map[this] = 1;
  80. MPlayerQuests.SetName("Player::MPlayerQuests");
  81. test_time = 0;
  82. returning_from_ld = false;
  83. away_message = "Sorry, I am A.F.K. (Away From Keyboard)";
  84. AddSecondaryEntityCommand("Inspect", 10000, "inspect_player", "", 0, 0);
  85. AddSecondaryEntityCommand("Who", 10000, "who", "", 0, 0);
  86. // commented out commands a player canNOT use on themselves... move these to Client::HandleVerbRequest()?
  87. //AddSecondaryEntityCommand("Assist", 10, "assist", "", 0, 0);
  88. //AddSecondaryEntityCommand("Duel", 10, "duel", "", 0, 0);
  89. //AddSecondaryEntityCommand("Duel Bet", 10, "duelbet", "", 0, 0);
  90. //AddSecondaryEntityCommand("Trade", 10, "trade", "", 0, 0);
  91. is_tracking = false;
  92. guild = 0;
  93. following = false;
  94. combat_target = 0;
  95. //InitXPTable();
  96. pending_deletion = false;
  97. spawn_vis_struct = 0;
  98. spawn_pos_struct = 0;
  99. spawn_info_struct = 0;
  100. spawn_header_struct = 0;
  101. spawn_footer_struct = 0;
  102. widget_footer_struct = 0;
  103. sign_footer_struct = 0;
  104. pos_xor_size = 0;
  105. info_xor_size = 0;
  106. vis_xor_size = 0;
  107. pos_mutex.SetName("Player::pos_mutex");
  108. vis_mutex.SetName("Player::vis_mutex");
  109. info_mutex.SetName("Player::info_mutex");
  110. index_mutex.SetName("Player::index_mutex");
  111. spawn_mutex.SetName("Player::spawn_mutex");
  112. m_playerSpawnQuestsRequired.SetName("Player::player_spawn_quests_required");
  113. m_playerSpawnHistoryRequired.SetName("Player::player_spawn_history_required");
  114. gm_vision = false;
  115. SetSaveSpellEffects(true);
  116. reset_mentorship = false;
  117. all_spells_locked = false;
  118. current_language_id = 0;
  119. active_reward = false;
  120. SortedTraitList = new map <int8, map <int8, vector<TraitData*> > >;
  121. ClassTraining = new map <int8, vector<TraitData*> >;
  122. RaceTraits = new map <int8, vector<TraitData*> >;
  123. InnateRaceTraits = new map <int8, vector<TraitData*> >;
  124. FocusEffects = new map <int8, vector<TraitData*> >;
  125. need_trait_update = true;
  126. active_food_unique_id = 0;
  127. active_drink_unique_id = 0;
  128. raidsheet_changed = false;
  129. hassent_raid = false;
  130. }
  131. Player::~Player(){
  132. SetSaveSpellEffects(true);
  133. for(int32 i=0;i<spells.size();i++){
  134. safe_delete(spells[i]);
  135. }
  136. for(int32 i=0;i<quickbar_items.size();i++){
  137. safe_delete(quickbar_items[i]);
  138. }
  139. map<int32, vector<int32>*>::iterator itr;
  140. for (itr = player_spawn_quests_required.begin(); itr != player_spawn_quests_required.end(); itr++){
  141. safe_delete(itr->second);
  142. }
  143. player_spawn_quests_required.clear();
  144. for (itr = player_spawn_history_required.begin(); itr != player_spawn_history_required.end(); itr++){
  145. safe_delete(itr->second);
  146. }
  147. player_spawn_history_required.clear();
  148. map<int8, map<int8, vector<HistoryData*> > >::iterator itr1;
  149. map<int8, vector<HistoryData*> >::iterator itr2;
  150. vector<HistoryData*>::iterator itr3;
  151. // Type
  152. for (itr1 = m_characterHistory.begin(); itr1 != m_characterHistory.end(); itr1++) {
  153. // Sub type
  154. for (itr2 = itr1->second.begin(); itr2 != itr1->second.end(); itr2++) {
  155. // vector of data
  156. for (itr3 = itr2->second.begin(); itr3 != itr2->second.end(); itr3++) {
  157. safe_delete(*itr3);
  158. }
  159. }
  160. }
  161. m_characterHistory.clear();
  162. mLUAHistory.writelock();
  163. map<int32, LUAHistory*>::iterator itr4;
  164. for (itr4 = m_charLuaHistory.begin(); itr4 != m_charLuaHistory.end(); itr4++) {
  165. safe_delete(itr4->second);
  166. }
  167. m_charLuaHistory.clear();
  168. mLUAHistory.releasewritelock();
  169. safe_delete_array(movement_packet);
  170. safe_delete_array(old_movement_packet);
  171. safe_delete_array(spawn_tmp_info_xor_packet);
  172. safe_delete_array(spawn_tmp_vis_xor_packet);
  173. safe_delete_array(spawn_tmp_pos_xor_packet);
  174. safe_delete_array(spell_xor_packet);
  175. safe_delete_array(spell_orig_packet);
  176. safe_delete_array(raid_orig_packet);
  177. safe_delete_array(raid_xor_packet);
  178. DestroyQuests();
  179. WritePlayerStatistics();
  180. RemovePlayerStatistics();
  181. DeleteMail();
  182. world.RemoveLottoPlayer(GetCharacterID());
  183. safe_delete(info);
  184. index_mutex.writelock(__FUNCTION__, __LINE__);
  185. player_spawn_reverse_id_map.clear();
  186. player_spawn_id_map.clear();
  187. index_mutex.releasewritelock(__FUNCTION__, __LINE__);
  188. info_mutex.writelock(__FUNCTION__, __LINE__);
  189. spawn_info_packet_list.clear();
  190. info_mutex.releasewritelock(__FUNCTION__, __LINE__);
  191. vis_mutex.writelock(__FUNCTION__, __LINE__);
  192. spawn_vis_packet_list.clear();
  193. vis_mutex.releasewritelock(__FUNCTION__, __LINE__);
  194. pos_mutex.writelock(__FUNCTION__, __LINE__);
  195. spawn_pos_packet_list.clear();
  196. pos_mutex.releasewritelock(__FUNCTION__, __LINE__);
  197. safe_delete(spawn_header_struct);
  198. safe_delete(spawn_footer_struct);
  199. safe_delete(sign_footer_struct);
  200. safe_delete(widget_footer_struct);
  201. safe_delete(spawn_info_struct);
  202. safe_delete(spawn_vis_struct);
  203. safe_delete(spawn_pos_struct);
  204. ClearPendingSelectableItemRewards(0, true);
  205. ClearPendingItemRewards();
  206. ClearEverything();
  207. safe_delete(SortedTraitList);
  208. safe_delete(ClassTraining);
  209. safe_delete(RaceTraits);
  210. safe_delete(InnateRaceTraits);
  211. safe_delete(FocusEffects);
  212. // leak fix on Language* pointer from Player::AddLanguage
  213. player_languages_list.Clear();
  214. }
  215. EQ2Packet* Player::serialize(Player* player, int16 version){
  216. return spawn_serialize(player, version);
  217. }
  218. EQ2Packet* Player::Move(float x, float y, float z, int16 version, float heading){
  219. PacketStruct* packet = configReader.getStruct("WS_MoveClient", version);
  220. if(packet){
  221. packet->setDataByName("x", x);
  222. packet->setDataByName("y", y);
  223. packet->setDataByName("z", z);
  224. packet->setDataByName("unknown", 1); // 1 seems to force the client to re-render the zone at the new location
  225. packet->setDataByName("location", 0xFFFFFFFF); //added in 869
  226. if (heading != -1.0f)
  227. packet->setDataByName("heading", heading);
  228. EQ2Packet* outapp = packet->serialize();
  229. safe_delete(packet);
  230. return outapp;
  231. }
  232. return 0;
  233. }
  234. void Player::DestroyQuests(){
  235. MPlayerQuests.writelock(__FUNCTION__, __LINE__);
  236. map<int32, Quest*>::iterator itr;
  237. for(itr = completed_quests.begin(); itr != completed_quests.end(); itr++){
  238. if(itr->second) {
  239. safe_delete(itr->second);
  240. }
  241. }
  242. completed_quests.clear();
  243. for(itr = player_quests.begin(); itr != player_quests.end(); itr++){
  244. if(itr->second) {
  245. safe_delete(itr->second);
  246. }
  247. }
  248. player_quests.clear();
  249. for(itr = pending_quests.begin(); itr != pending_quests.end(); itr++){
  250. if(itr->second) {
  251. safe_delete(itr->second);
  252. }
  253. }
  254. pending_quests.clear();
  255. MPlayerQuests.releasewritelock(__FUNCTION__, __LINE__);
  256. }
  257. PlayerInfo* Player::GetPlayerInfo(){
  258. if(info == 0)
  259. info = new PlayerInfo(this);
  260. return info;
  261. }
  262. void PlayerInfo::CalculateXPPercentages(){
  263. int32 xp_needed = info_struct->get_xp_needed();
  264. if(xp_needed > 0){
  265. double div_percent = ((double)info_struct->get_xp() / xp_needed) * 100.0;
  266. int16 percentage = (int16)(div_percent) * 10;
  267. double whole, fractional = 0.0;
  268. fractional = std::modf(div_percent, &whole);
  269. info_struct->set_xp_yellow(percentage);
  270. info_struct->set_xp_blue((int16)(fractional * 1000));
  271. // vitality bars probably need a revisit
  272. info_struct->set_xp_blue_vitality_bar(0);
  273. info_struct->set_xp_yellow_vitality_bar(0);
  274. if(player->GetXPVitality() > 0){
  275. float vitality_total = player->GetXPVitality()*10 + percentage;
  276. vitality_total -= ((int)(percentage/100)*100);
  277. if(vitality_total < 100){ //10%
  278. info_struct->set_xp_blue_vitality_bar(info_struct->get_xp_blue() + (int16)(player->GetXPVitality() *10));
  279. }
  280. else
  281. info_struct->set_xp_yellow_vitality_bar(info_struct->get_xp_yellow() + (int16)(player->GetXPVitality() *10));
  282. }
  283. }
  284. }
  285. void PlayerInfo::CalculateTSXPPercentages(){
  286. int32 ts_xp_needed = info_struct->get_ts_xp_needed();
  287. if(ts_xp_needed > 0){
  288. float percentage = ((double)info_struct->get_ts_xp() / ts_xp_needed) * 1000;
  289. info_struct->set_tradeskill_exp_yellow((int16)percentage);
  290. info_struct->set_tradeskill_exp_blue((int16)((percentage - info_struct->get_tradeskill_exp_yellow()) * 1000));
  291. /*info_struct->xp_blue_vitality_bar = 0;
  292. info_struct->xp_yellow_vitality_bar = 0;
  293. if(player->GetXPVitality() > 0){
  294. float vitality_total = player->GetXPVitality()*10 + percentage;
  295. vitality_total -= ((int)(percentage/100)*100);
  296. if(vitality_total < 100){ //10%
  297. info_struct->xp_blue_vitality_bar = info_struct->xp_blue + (int16)(player->GetXPVitality() *10);
  298. }
  299. else
  300. info_struct->xp_yellow_vitality_bar = info_struct->xp_yellow + (int16)(player->GetXPVitality() *10);
  301. }*/
  302. }
  303. }
  304. void PlayerInfo::SetHouseZone(int32 id){
  305. house_zone_id = id;
  306. }
  307. void PlayerInfo::SetBindZone(int32 id){
  308. bind_zone_id = id;
  309. }
  310. void PlayerInfo::SetBindX(float x){
  311. bind_x = x;
  312. }
  313. void PlayerInfo::SetBindY(float y){
  314. bind_y = y;
  315. }
  316. void PlayerInfo::SetBindZ(float z){
  317. bind_z = z;
  318. }
  319. void PlayerInfo::SetBindHeading(float heading){
  320. bind_heading = heading;
  321. }
  322. int32 PlayerInfo::GetHouseZoneID(){
  323. return house_zone_id;
  324. }
  325. int32 PlayerInfo::GetBindZoneID(){
  326. return bind_zone_id;
  327. }
  328. float PlayerInfo::GetBindZoneX(){
  329. return bind_x;
  330. }
  331. float PlayerInfo::GetBindZoneY(){
  332. return bind_y;
  333. }
  334. float PlayerInfo::GetBindZoneZ(){
  335. return bind_z;
  336. }
  337. float PlayerInfo::GetBindZoneHeading(){
  338. return bind_heading;
  339. }
  340. PacketStruct* PlayerInfo::serialize2(int16 version){
  341. PacketStruct* packet = configReader.getStruct("WS_CharacterSheet", version);
  342. if(packet){
  343. //TODO: 2021 FIX THIS CASTING
  344. char deity[32];
  345. strncpy(deity, info_struct->get_deity().c_str(), 32);
  346. packet->setDataByName("deity", deity);
  347. char name[40];
  348. strncpy(name, info_struct->get_name().c_str(), 40);
  349. packet->setDataByName("character_name", name);
  350. packet->setDataByName("race", info_struct->get_race());
  351. packet->setDataByName("gender", info_struct->get_gender());
  352. packet->setDataByName("class1", info_struct->get_class1());
  353. packet->setDataByName("class2", info_struct->get_class2());
  354. packet->setDataByName("class3", info_struct->get_class3());
  355. packet->setDataByName("tradeskill_class1", info_struct->get_tradeskill_class1());
  356. packet->setDataByName("tradeskill_class2", info_struct->get_tradeskill_class2());
  357. packet->setDataByName("tradeskill_class3", info_struct->get_tradeskill_class3());
  358. packet->setDataByName("level", info_struct->get_level());
  359. packet->setDataByName("effective_level", info_struct->get_effective_level() != 0 ? info_struct->get_effective_level() : info_struct->get_level());
  360. packet->setDataByName("tradeskill_level", info_struct->get_tradeskill_level());
  361. packet->setDataByName("account_age_base", info_struct->get_account_age_base());
  362. // for(int8 i=0;i<19;i++)
  363. // {
  364. // packet->setDataByName("account_age_bonus", info_struct->get_account_age_bonus(i));
  365. // }
  366. //
  367. packet->setDataByName("current_hp", player->GetHP());
  368. packet->setDataByName("max_hp",player-> GetTotalHP());
  369. packet->setDataByName("base_hp", player->GetTotalHPBase());
  370. float bonus_health = floor( (float)(info_struct->get_sta() * player->CalculateBonusMod()));
  371. packet->setDataByName("bonus_health", bonus_health);
  372. packet->setDataByName("stat_bonus_health", player->CalculateBonusMod());
  373. packet->setDataByName("current_power", player->GetPower());
  374. packet->setDataByName("max_power", player->GetTotalPower());
  375. packet->setDataByName("base_power", player->GetTotalPowerBase());
  376. packet->setDataByName("bonus_power", floor( (float)(player->GetPrimaryStat() * player->CalculateBonusMod())));
  377. packet->setDataByName("stat_bonus_power", player->CalculateBonusMod());
  378. packet->setDataByName("conc_used", info_struct->get_cur_concentration());
  379. packet->setDataByName("conc_max", info_struct->get_max_concentration());
  380. packet->setDataByName("attack", info_struct->get_cur_attack());
  381. packet->setDataByName("attack_base", info_struct->get_attack_base());
  382. packet->setDataByName("absorb", info_struct->get_absorb());
  383. packet->setDataByName("mitigation_skill1", info_struct->get_mitigation_skill1());
  384. packet->setDataByName("mitigation_skill2", info_struct->get_mitigation_skill2());
  385. packet->setDataByName("mitigation_skill3", info_struct->get_mitigation_skill3());
  386. CalculateXPPercentages();
  387. packet->setDataByName("exp_yellow", info_struct->get_xp_yellow());
  388. packet->setDataByName("exp_blue", info_struct->get_xp_blue());
  389. packet->setDataByName("tradeskill_exp_yellow", info_struct->get_tradeskill_exp_yellow());
  390. packet->setDataByName("tradeskill_exp_blue", info_struct->get_tradeskill_exp_blue());
  391. packet->setDataByName("flags", info_struct->get_flags());
  392. packet->setDataByName("flags2", info_struct->get_flags2());
  393. packet->setDataByName("avoidance_pct", (int16)info_struct->get_avoidance_display()*10.0f);//avoidance_pct 192 = 19.2% // confirmed DoV
  394. packet->setDataByName("avoidance_base", (int16)info_struct->get_avoidance_base()*10.0f); // confirmed DoV
  395. packet->setDataByName("avoidance", info_struct->get_cur_avoidance());
  396. packet->setDataByName("base_avoidance_pct", info_struct->get_base_avoidance_pct());// confirmed DoV
  397. float parry_pct = info_struct->get_parry(); // client works off of int16, but we use floats to track the actual x/100%
  398. packet->setDataByName("parry",(int16)(parry_pct*10.0f));// confirmed DoV
  399. float block_pct = info_struct->get_block()*10.0f;
  400. packet->setDataByName("block", (int16)block_pct);// confirmed DoV
  401. packet->setDataByName("uncontested_block", info_struct->get_uncontested_block());// confirmed DoV
  402. packet->setDataByName("str", info_struct->get_str());
  403. packet->setDataByName("sta", info_struct->get_sta());
  404. packet->setDataByName("agi", info_struct->get_agi());
  405. packet->setDataByName("wis", info_struct->get_wis());
  406. packet->setDataByName("int", info_struct->get_intel());
  407. packet->setDataByName("str_base", info_struct->get_str_base());
  408. packet->setDataByName("sta_base", info_struct->get_sta_base());
  409. packet->setDataByName("agi_base", info_struct->get_agi_base());
  410. packet->setDataByName("wis_base", info_struct->get_wis_base());
  411. packet->setDataByName("int_base", info_struct->get_intel_base());
  412. packet->setDataByName("mitigation_cur", info_struct->get_cur_mitigation());
  413. packet->setDataByName("mitigation_max", info_struct->get_max_mitigation());
  414. packet->setDataByName("mitigation_base", info_struct->get_mitigation_base());
  415. packet->setDataByName("heat", info_struct->get_heat());
  416. packet->setDataByName("cold", info_struct->get_cold());
  417. packet->setDataByName("magic", info_struct->get_magic());
  418. packet->setDataByName("mental", info_struct->get_mental());
  419. packet->setDataByName("divine", info_struct->get_divine());
  420. packet->setDataByName("disease", info_struct->get_disease());
  421. packet->setDataByName("poison", info_struct->get_poison());
  422. packet->setDataByName("heat_base", info_struct->get_heat_base());
  423. packet->setDataByName("cold_base", info_struct->get_cold_base());
  424. packet->setDataByName("magic_base", info_struct->get_magic_base());
  425. packet->setDataByName("mental_base", info_struct->get_mental_base());
  426. packet->setDataByName("divine_base", info_struct->get_divine_base());
  427. packet->setDataByName("disease_base", info_struct->get_disease_base());
  428. packet->setDataByName("poison_base", info_struct->get_poison_base());
  429. packet->setDataByName("mitigation_cur2", info_struct->get_cur_mitigation());
  430. packet->setDataByName("mitigation_max2", info_struct->get_max_mitigation());
  431. packet->setDataByName("mitigation_base2", info_struct->get_mitigation_base());
  432. packet->setDataByName("coins_copper", info_struct->get_coin_copper());
  433. packet->setDataByName("coins_silver", info_struct->get_coin_silver());
  434. packet->setDataByName("coins_gold", info_struct->get_coin_gold());
  435. packet->setDataByName("coins_plat", info_struct->get_coin_plat());
  436. packet->setDataByName("weight", info_struct->get_weight());
  437. packet->setDataByName("max_weight", info_struct->get_max_weight());
  438. if(info_struct->get_pet_id() != 0xFFFFFFFF) {
  439. char pet_name[32];
  440. strncpy(pet_name, info_struct->get_pet_name().c_str(), version <= 373 ? 16 : 32);
  441. packet->setDataByName("pet_name", pet_name);
  442. }
  443. else {
  444. packet->setDataByName("pet_name", "No Pet");
  445. }
  446. packet->setDataByName("pet_health_pct", info_struct->get_pet_health_pct());
  447. packet->setDataByName("pet_power_pct", info_struct->get_pet_power_pct());
  448. packet->setDataByName("pet_movement", info_struct->get_pet_movement());
  449. packet->setDataByName("pet_behavior", info_struct->get_pet_behavior());
  450. packet->setDataByName("status_points", info_struct->get_status_points());
  451. if(bind_zone_id > 0){
  452. string bind_name = database.GetZoneName(bind_zone_id);
  453. if (bind_name.length() > 0)
  454. packet->setDataByName("bind_zone", bind_name.c_str());
  455. }
  456. else
  457. packet->setDataByName("bind_zone", "None");
  458. if(house_zone_id > 0){
  459. string house_name = database.GetZoneName(house_zone_id);
  460. if (house_name.length() > 0)
  461. packet->setDataByName("house_zone", house_name.c_str());
  462. }
  463. else
  464. packet->setDataByName("house_zone", "None");
  465. //packet->setDataByName("account_age_base", 14);
  466. packet->setDataByName("hp_regen", info_struct->get_hp_regen());
  467. packet->setDataByName("power_regen", info_struct->get_power_regen());
  468. /*packet->setDataByName("unknown11", -1, 0);
  469. packet->setDataByName("unknown11", -1, 1);
  470. packet->setDataByName("unknown13", 201, 0);
  471. packet->setDataByName("unknown13", 201, 1);
  472. packet->setDataByName("unknown13", 234, 2);
  473. packet->setDataByName("unknown13", 201, 3);
  474. packet->setDataByName("unknown13", 214, 4);
  475. packet->setDataByName("unknown13", 234, 5);
  476. packet->setDataByName("unknown13", 234, 6);
  477. packet->setDataByName("unknown14", 78);
  478. */
  479. packet->setDataByName("adventure_exp_vitality", (int16)(player->GetXPVitality() *10));
  480. //packet->setDataByName("unknown15b", 9911);
  481. packet->setDataByName("unknown15a", 78);
  482. packet->setDataByName("xp_yellow_vitality_bar", info_struct->get_xp_yellow_vitality_bar());
  483. packet->setDataByName("xp_blue_vitality_bar", info_struct->get_xp_blue_vitality_bar());
  484. packet->setDataByName("tradeskill_exp_vitality", 100);
  485. packet->setDataByName("unknown15c", 200);
  486. //packet->setDataByName("unknown15", 100, 10);
  487. packet->setDataByName("unknown18", 16880, 1);
  488. /*packet->setDataByName("unknown19", 1);
  489. packet->setDataByName("unknown19", 3, 1);
  490. packet->setDataByName("unknown19", 1074301064, 2);
  491. packet->setDataByName("unknown19", 1, 3);
  492. packet->setDataByName("unknown19", 3, 4);
  493. packet->setDataByName("unknown19", 1074301064, 5);
  494. packet->setDataByName("unknown19", 6, 6);
  495. packet->setDataByName("unknown19", 14, 7);
  496. packet->setDataByName("unknown19", 1083179008, 8);*/
  497. player->SetGroupInformation(packet);
  498. packet->setDataByName("unknown20", 1, 107);
  499. packet->setDataByName("unknown20", 1, 108);
  500. packet->setDataByName("unknown20", 1, 109);
  501. packet->setDataByName("unknown20", 1, 110);
  502. packet->setDataByName("unknown20", 1, 111);
  503. //packet->setDataByName("unknown20b", 255);
  504. //packet->setDataByName("unknown20b", 255, 1);
  505. //packet->setDataByName("unknown20b", 255, 2);
  506. packet->setDataByName("unknown11", 123);
  507. packet->setDataByName("unknown11", 234, 1);
  508. //packet->setDataByName("in_combat", 32768);
  509. //make name flash red
  510. /*packet->setDataByName("unknown20", 8);
  511. packet->setDataByName("unknown20", 38, 70);
  512. packet->setDataByName("unknown20", 17, 77);
  513. packet->setDataByName("unknown20", 1, 112); //melee stats and such
  514. packet->setDataByName("unknown20", 1, 113);
  515. packet->setDataByName("unknown20", 1, 114);
  516. packet->setDataByName("unknown20", 1, 115);
  517. packet->setDataByName("unknown20", 4294967295, 309);
  518. packet->setDataByName("unknown22", 2, 4);
  519. packet->setDataByName("unknown23", 2, 29);
  520. */
  521. //packet->setDataByName("unknown20b", 1, i); // pet bar in here
  522. // for(int i=0;i<19;i++)
  523. // packet->setDataByName("unknown7", 257, i);
  524. //packet->setDataByName("unknown21", info_struct->rain, 2);
  525. packet->setDataByName("rain", info_struct->get_rain());
  526. packet->setDataByName("rain2", info_struct->get_wind()); //-102.24);
  527. /*packet->setDataByName("unknown22", 3, 4);
  528. packet->setDataByName("unknown23", 3, 161);
  529. packet->setDataByName("unknown20", 103);
  530. packet->setDataByName("unknown20", 1280, 70);
  531. packet->setDataByName("unknown20", 9, 71);
  532. packet->setDataByName("unknown20", 5, 72);
  533. packet->setDataByName("unknown20", 4294967271, 73);
  534. packet->setDataByName("unknown20", 5, 75);
  535. packet->setDataByName("unknown20", 1051, 77);
  536. packet->setDataByName("unknown20", 3, 78);
  537. packet->setDataByName("unknown20", 6, 104);
  538. packet->setDataByName("unknown20", 1, 105);
  539. packet->setDataByName("unknown20", 20, 106);
  540. packet->setDataByName("unknown20", 3, 107);
  541. packet->setDataByName("unknown20", 1, 108);
  542. packet->setDataByName("unknown20", 1, 109);
  543. packet->setDataByName("unknown20", 4278190080, 494);
  544. packet->setDataByName("unknown20b", 255);
  545. packet->setDataByName("unknown20b", 255, 1);
  546. packet->setDataByName("unknown20b", 255, 2);
  547. packet->setDataByName("unknown20", 50, 75);
  548. */
  549. //packet->setDataByName("rain2", -102.24);
  550. for(int i=0;i<45;i++){
  551. if(i < 30){
  552. packet->setSubstructDataByName("maintained_effects", "name", info_struct->maintained_effects[i].name, i, 0);
  553. packet->setSubstructDataByName("maintained_effects", "target", info_struct->maintained_effects[i].target, i, 0);
  554. packet->setSubstructDataByName("maintained_effects", "spell_id", info_struct->maintained_effects[i].spell_id, i, 0);
  555. packet->setSubstructDataByName("maintained_effects", "slot_pos", info_struct->maintained_effects[i].slot_pos, i, 0);
  556. packet->setSubstructDataByName("maintained_effects", "icon", info_struct->maintained_effects[i].icon, i, 0);
  557. packet->setSubstructDataByName("maintained_effects", "icon_type", info_struct->maintained_effects[i].icon_backdrop, i, 0);
  558. packet->setSubstructDataByName("maintained_effects", "conc_used", info_struct->maintained_effects[i].conc_used, i, 0);
  559. packet->setSubstructDataByName("maintained_effects", "unknown3", 1, i, 0);
  560. packet->setSubstructDataByName("maintained_effects", "total_time", info_struct->maintained_effects[i].total_time, i, 0);
  561. packet->setSubstructDataByName("maintained_effects", "expire_timestamp", info_struct->maintained_effects[i].expire_timestamp, i, 0);
  562. }
  563. else if(version < 942)//version 942 added 15 additional spell effect slots
  564. break;
  565. packet->setSubstructDataByName("spell_effects", "spell_id", info_struct->spell_effects[i].spell_id, i, 0);
  566. if(info_struct->spell_effects[i].spell_id > 0 && info_struct->spell_effects[i].spell_id < 0xFFFFFFFF)
  567. packet->setSubstructDataByName("spell_effects", "unknown2", 514, i, 0);
  568. packet->setSubstructDataByName("spell_effects", "total_time", info_struct->spell_effects[i].total_time, i, 0);
  569. packet->setSubstructDataByName("spell_effects", "expire_timestamp", info_struct->spell_effects[i].expire_timestamp, i, 0);
  570. packet->setSubstructDataByName("spell_effects", "icon", info_struct->spell_effects[i].icon, i, 0);
  571. packet->setSubstructDataByName("spell_effects", "icon_type", info_struct->spell_effects[i].icon_backdrop, i, 0);
  572. }
  573. return packet;
  574. }
  575. return 0;
  576. }
  577. EQ2Packet* PlayerInfo::serialize3(PacketStruct* packet, int16 version){
  578. if(packet){
  579. string* data = packet->serializeString();
  580. int32 size = data->length();
  581. //DumpPacket((uchar*)data->c_str(), size);
  582. uchar* tmp = new uchar[size];
  583. if(!changes){
  584. orig_packet = new uchar[size];
  585. changes = new uchar[size];
  586. memcpy(orig_packet, (uchar*)data->c_str(), size);
  587. size = Pack(tmp, (uchar*)data->c_str(), size, size, version);
  588. }
  589. else{
  590. memcpy(changes, (uchar*)data->c_str(), size);
  591. Encode(changes, orig_packet, size);
  592. size = Pack(tmp, changes, size, size, version);
  593. //cout << "INFO HERE:\n";
  594. //DumpPacket(tmp, size);
  595. }
  596. EQ2Packet* ret_packet = new EQ2Packet(OP_UpdateCharacterSheetMsg, tmp, size+4);
  597. safe_delete_array(tmp);
  598. safe_delete(packet);
  599. return ret_packet;
  600. }
  601. return 0;
  602. }
  603. void PlayerInfo::SetAccountAge(int32 age){
  604. info_struct->set_account_age_base(age);
  605. }
  606. EQ2Packet* PlayerInfo::serialize(int16 version, int16 modifyPos, int32 modifyValue) {
  607. PacketStruct* packet = configReader.getStruct("WS_CharacterSheet", version);
  608. //0-69, locked screen movement
  609. //30-69 normal movement
  610. //10-30 normal movement
  611. if (packet) {
  612. char name[40];
  613. strncpy(name,info_struct->get_name().c_str(),40);
  614. packet->setDataByName("character_name", name);
  615. packet->setDataByName("race", info_struct->get_race());
  616. packet->setDataByName("gender", info_struct->get_gender());
  617. packet->setDataByName("exiled", 0); // need exiled data
  618. packet->setDataByName("class1", info_struct->get_class1());
  619. packet->setDataByName("class2", info_struct->get_class2());
  620. packet->setDataByName("class3", info_struct->get_class3());
  621. packet->setDataByName("tradeskill_class1", info_struct->get_tradeskill_class1());
  622. packet->setDataByName("tradeskill_class2", info_struct->get_tradeskill_class2());
  623. packet->setDataByName("tradeskill_class3", info_struct->get_tradeskill_class3());
  624. packet->setDataByName("level", info_struct->get_level());
  625. packet->setDataByName("effective_level", info_struct->get_effective_level() != 0 ? info_struct->get_effective_level() : info_struct->get_level());
  626. packet->setDataByName("tradeskill_level", info_struct->get_tradeskill_level());
  627. packet->setDataByName("account_age_base", info_struct->get_account_age_base());
  628. //TODO: 2021 FIX THIS CASTING
  629. for (int8 i = 0; i < 19; i++)
  630. packet->setDataByName("account_age_bonus", 0);
  631. //TODO: 2021 FIX THIS CASTING
  632. char deity[32];
  633. strncpy(deity, info_struct->get_deity().c_str(), 32);
  634. packet->setDataByName("deity", deity);
  635. packet->setDataByName("last_name", player->GetLastName());
  636. packet->setDataByName("current_hp", player->GetHP());
  637. packet->setDataByName("max_hp", player->GetTotalHP());
  638. packet->setDataByName("base_hp", player->GetTotalHPBase());
  639. packet->setDataByName("current_power", player->GetPower());
  640. packet->setDataByName("max_power", player->GetTotalPower());
  641. packet->setDataByName("base_power", player->GetTotalPowerBase());
  642. packet->setDataByName("conc_used", info_struct->get_cur_concentration());
  643. packet->setDataByName("conc_max", info_struct->get_max_concentration());
  644. packet->setDataByName("hp_regen", player->GetInfoStruct()->get_hp_regen());
  645. packet->setDataByName("power_regen", player->GetInfoStruct()->get_power_regen());
  646. packet->setDataByName("stat_bonus_health", player->CalculateBonusMod());//bonus health and bonus power getting same value?
  647. packet->setDataByName("stat_bonus_power", player->CalculateBonusMod());//bonus health and bonus power getting same value?
  648. float bonus_health = floor((float)(info_struct->get_sta() * player->CalculateBonusMod()));
  649. packet->setDataByName("bonus_health", bonus_health);
  650. packet->setDataByName("bonus_power", floor((float)(player->GetPrimaryStat() * player->CalculateBonusMod())));
  651. packet->setDataByName("stat_bonus_damage", 95); //stat_bonus_damage
  652. packet->setDataByName("mitigation_cur", info_struct->get_cur_mitigation());// confirmed DoV
  653. packet->setDataByName("mitigation_base", info_struct->get_mitigation_base());// confirmed DoV
  654. packet->setDataByName("mitigation_pct_pve", info_struct->get_mitigation_pve()); // % calculation Mitigation % vs PvE 392 = 39.2%// confirmed DoV
  655. packet->setDataByName("mitigation_pct_pvp", info_struct->get_mitigation_pvp()); // % calculation Mitigation % vs PvP 559 = 55.9%// confirmed DoV
  656. packet->setDataByName("toughness", 0);//toughness// confirmed DoV
  657. packet->setDataByName("toughness_resist_dmg_pvp", 0);//toughness_resist_dmg_pvp 73 = 7300% // confirmed DoV
  658. packet->setDataByName("avoidance_pct", (int16)info_struct->get_avoidance_display()*10.0f);//avoidance_pct 192 = 19.2% // confirmed DoV
  659. packet->setDataByName("avoidance_base", (int16)info_struct->get_avoidance_base()*10.0f); // confirmed DoV
  660. packet->setDataByName("avoidance", info_struct->get_cur_avoidance());
  661. packet->setDataByName("base_avoidance_pct", info_struct->get_base_avoidance_pct());// confirmed DoV
  662. float parry_pct = info_struct->get_parry(); // client works off of int16, but we use floats to track the actual x/100%
  663. packet->setDataByName("parry",(int16)(parry_pct*10.0f));// confirmed DoV
  664. float block_pct = info_struct->get_block()*10.0f;
  665. packet->setDataByName("block", (int16)block_pct);// confirmed DoV
  666. packet->setDataByName("uncontested_block", info_struct->get_uncontested_block());// confirmed DoV
  667. packet->setDataByName("str", info_struct->get_str());// confirmed DoV
  668. packet->setDataByName("sta", info_struct->get_sta());// confirmed DoV
  669. packet->setDataByName("agi", info_struct->get_agi());// confirmed DoV
  670. packet->setDataByName("wis", info_struct->get_wis());// confirmed DoV
  671. packet->setDataByName("int", info_struct->get_intel());// confirmed DoV
  672. packet->setDataByName("str_base", info_struct->get_str_base()); // confirmed DoV
  673. packet->setDataByName("sta_base", info_struct->get_sta_base());// confirmed DoV
  674. packet->setDataByName("agi_base", info_struct->get_agi_base());// confirmed DoV
  675. packet->setDataByName("wis_base", info_struct->get_wis_base());// confirmed DoV
  676. packet->setDataByName("int_base", info_struct->get_intel_base());// confirmed DoV
  677. if (version <= 996) {
  678. packet->setDataByName("heat", info_struct->get_heat());
  679. packet->setDataByName("cold", info_struct->get_cold());
  680. packet->setDataByName("magic", info_struct->get_magic());
  681. packet->setDataByName("mental", info_struct->get_mental());
  682. packet->setDataByName("divine", info_struct->get_divine());
  683. packet->setDataByName("disease", info_struct->get_disease());
  684. packet->setDataByName("poison", info_struct->get_poison());
  685. packet->setDataByName("heat_base", info_struct->get_heat_base());
  686. packet->setDataByName("cold_base", info_struct->get_cold_base());
  687. packet->setDataByName("magic_base", info_struct->get_magic_base());
  688. packet->setDataByName("mental_base", info_struct->get_mental_base());
  689. packet->setDataByName("divine_base", info_struct->get_divine_base());
  690. packet->setDataByName("disease_base", info_struct->get_disease_base());
  691. packet->setDataByName("poison_base", info_struct->get_poison_base());
  692. }
  693. else {
  694. packet->setDataByName("elemental", info_struct->get_heat());// confirmed DoV
  695. packet->setDataByName("noxious", info_struct->get_poison());// confirmed DoV
  696. packet->setDataByName("arcane", info_struct->get_magic());// confirmed DoV
  697. packet->setDataByName("elemental_base", info_struct->get_elemental_base());// confirmed DoV
  698. packet->setDataByName("noxious_base", info_struct->get_noxious_base());// confirmed DoV
  699. packet->setDataByName("arcane_base", info_struct->get_arcane_base());// confirmed DoV
  700. }
  701. packet->setDataByName("elemental_absorb_pve", 0); //210 = 21.0% confirmed DoV
  702. packet->setDataByName("noxious_absorb_pve", 0);//210 = 21.0% confirmed DoV
  703. packet->setDataByName("arcane_absorb_pve", 0);//210 = 21.0% confirmed DoV
  704. packet->setDataByName("elemental_absorb_pvp", 0);//210 = 21.0% confirmed DoV
  705. packet->setDataByName("noxious_absorb_pvp", 0);//210 = 21.0% confirmed DoV
  706. packet->setDataByName("arcane_absorb_pvp", 0);//210 = 21.0% confirmed DoV
  707. packet->setDataByName("elemental_dmg_reduction", 0);// confirmed DoV
  708. packet->setDataByName("noxious_dmg_reduction", 0);// confirmed DoV
  709. packet->setDataByName("arcane_dmg_reduction", 0);// confirmed DoV
  710. packet->setDataByName("elemental_dmg_reduction_pct", 0);//210 = 21.0% confirmed DoV
  711. packet->setDataByName("noxious_dmg_reduction_pct", 0);//210 = 21.0% confirmed DoV
  712. packet->setDataByName("arcane_dmg_reduction_pct", 0);//210 = 21.0% confirmed DoV
  713. CalculateXPPercentages();
  714. packet->setDataByName("current_adv_xp", info_struct->get_xp()); // confirmed DoV
  715. packet->setDataByName("needed_adv_xp", info_struct->get_xp_needed());// confirmed DoV
  716. if(version >= 60114)
  717. {
  718. // AoM ends up the debt_adv_xp field is the percentage of xp to the next level needed to advance out of debt (WHYY CANT THIS JUST BE A PERCENTAGE LIKE DOV!)
  719. float currentPctOfLevel = (float)info_struct->get_xp() / (float)info_struct->get_xp_needed();
  720. float neededPctAdvanceOutOfDebt = currentPctOfLevel + (info_struct->get_xp_debt() / 100.0f);
  721. packet->setDataByName("debt_adv_xp", neededPctAdvanceOutOfDebt);
  722. }
  723. else
  724. {
  725. double currentPctOfLevel = (double)info_struct->get_xp() / (double)info_struct->get_xp_needed();
  726. double neededPctAdvanceOutOfDebt = (currentPctOfLevel + ((double)info_struct->get_xp_debt() / 100.0)) * 1000.0;
  727. packet->setDataByName("exp_debt", (int16)(neededPctAdvanceOutOfDebt));//95= 9500% //confirmed DoV
  728. }
  729. packet->setDataByName("current_trade_xp", info_struct->get_ts_xp());// confirmed DoV
  730. packet->setDataByName("needed_trade_xp", info_struct->get_ts_xp_needed());// confirmed DoV
  731. packet->setDataByName("debt_trade_xp", 0);//95= 9500% //confirmed DoV
  732. packet->setDataByName("server_bonus", 0);//confirmed DoV
  733. packet->setDataByName("adventure_vet_bonus", 145);//confirmed DoV
  734. packet->setDataByName("tradeskill_vet_bonus", 123);//confirmed DoV
  735. packet->setDataByName("recruit_friend", 110);// 110 = 11000% //confirmed DoV
  736. packet->setDataByName("recruit_friend_bonus", 0);//confirmed DoV
  737. packet->setDataByName("adventure_vitality", (int16)(player->GetXPVitality() * 10)); // a %%
  738. packet->setDataByName("adventure_vitality_yellow_arrow", info_struct->get_xp_yellow_vitality_bar()); //change info_struct to match struct
  739. packet->setDataByName("adventure_vitality_blue_arrow", info_struct->get_xp_blue_vitality_bar()); //change info_struct to match struct
  740. packet->setDataByName("tradeskill_vitality", 300); //300 = 30%
  741. packet->setDataByName("tradeskill_vitality_purple_arrow", 0);// dov confirmed
  742. packet->setDataByName("tradeskill_vitality_blue_arrow", 0);// dov confirmed
  743. packet->setDataByName("mentor_bonus", 50);//mentor_bonus //this converts wrong says mentor bonus enabled but earning 0
  744. packet->setDataByName("assigned_aa", player->GetAssignedAA());
  745. packet->setDataByName("max_aa", rule_manager.GetGlobalRule(R_Player, MaxAA)->GetInt16());
  746. packet->setDataByName("unassigned_aa", player->GetUnassignedAA()); // dov confirmed
  747. packet->setDataByName("aa_green_bar", 0);// dov confirmed
  748. packet->setDataByName("adv_xp_to_aa_xp_slider", 0); // aa slider max // dov confirmed
  749. packet->setDataByName("adv_xp_to_aa_xp_max", 100); // aa slider position // dov confirmed
  750. packet->setDataByName("aa_blue_bar", 0);// dov confirmed
  751. packet->setDataByName("bonus_achievement_xp", 0); // dov confirmed
  752. packet->setDataByName("level_events", 32);// dov confirmed
  753. packet->setDataByName("items_found", 62);// dov confirmed
  754. packet->setDataByName("named_npcs_killed", 192);// dov confirmed
  755. packet->setDataByName("quests_completed", 670);// dov confirmed
  756. packet->setDataByName("exploration_events", 435);// dov confirmed
  757. packet->setDataByName("completed_collections", 144);// dov confirmed
  758. packet->setDataByName("unknown_1096_13_MJ", 80);//unknown_1096_13_MJ
  759. packet->setDataByName("unknown_1096_14_MJ", 50);//unknown_1096_14_MJ
  760. packet->setDataByName("coins_copper", info_struct->get_coin_copper());// dov confirmed
  761. packet->setDataByName("coins_silver", info_struct->get_coin_silver());// dov confirmed
  762. packet->setDataByName("coins_gold", info_struct->get_coin_gold());// dov confirmed
  763. packet->setDataByName("coins_plat", info_struct->get_coin_plat());// dov confirmed
  764. Skill* skill = player->GetSkillByName("Swimming", false);
  765. float breath_modifier = rule_manager.GetZoneRule(player->GetZoneID(), R_Player, SwimmingSkillMinBreathLength)->GetFloat();
  766. if(skill) {
  767. int32 max_val = 450;
  768. if(skill->max_val > 0)
  769. max_val = skill->max_val;
  770. float diff = (float)(skill->current_val + player->GetStat(ITEM_STAT_SWIMMING)) / (float)max_val;
  771. float max_breath_mod = rule_manager.GetZoneRule(player->GetZoneID(), R_Player, SwimmingSkillMaxBreathLength)->GetFloat();
  772. float diff_mod = max_breath_mod * diff;
  773. if(diff_mod > max_breath_mod)
  774. breath_modifier = max_breath_mod;
  775. else if(diff_mod > breath_modifier)
  776. breath_modifier = diff_mod;
  777. }
  778. packet->setDataByName("breath", breath_modifier);
  779. packet->setDataByName("melee_pri_dmg_min", player->GetPrimaryWeaponMinDamage());// dov confirmed
  780. packet->setDataByName("melee_pri_dmg_max", player->GetPrimaryWeaponMaxDamage());// dov confirmed
  781. packet->setDataByName("melee_sec_dmg_min", player->GetSecondaryWeaponMinDamage());// dov confirmed
  782. packet->setDataByName("melee_sec_dmg_max", player->GetSecondaryWeaponMaxDamage());// dov confirmed // this is off when using 2 handed weapon
  783. packet->setDataByName("ranged_dmg_min", player->GetRangedWeaponMinDamage());// dov confirmed
  784. packet->setDataByName("ranged_dmg_max", player->GetRangedWeaponMaxDamage());// dov confirmed
  785. if (info_struct->get_attackspeed() > 0) {
  786. packet->setDataByName("melee_pri_delay", (((float)player->GetPrimaryWeaponDelay() * 1.33) / player->CalculateAttackSpeedMod()) * .001);// dov confirmed
  787. packet->setDataByName("melee_sec_delay", (((float)player->GetSecondaryWeaponDelay() * 1.33) / player->CalculateAttackSpeedMod()) * .001);// dov confirmed
  788. packet->setDataByName("ranged_delay", (((float)player->GetRangeWeaponDelay() * 1.33) / player->CalculateAttackSpeedMod()) * .001);// dov confirmed
  789. }
  790. else {
  791. packet->setDataByName("melee_pri_delay", (float)player->GetPrimaryWeaponDelay() * .001);// dov confirmed
  792. packet->setDataByName("melee_sec_delay", (float)player->GetSecondaryWeaponDelay() * .001);// dov confirmed
  793. packet->setDataByName("ranged_delay", (float)player->GetRangeWeaponDelay() * .001);// dov confirmed
  794. }
  795. packet->setDataByName("ability_mod_pve", info_struct->get_ability_modifier());// dov confirmed
  796. packet->setDataByName("base_melee_crit", 85);//85 = 8500% dov confirmed
  797. packet->setDataByName("base_spell_crit", 84);// dov confirmed
  798. packet->setDataByName("base_taunt_crit", 83);// dov confirmed
  799. packet->setDataByName("base_heal_crit", 82);// dov confirmed
  800. packet->setDataByName("flags", info_struct->get_flags());
  801. packet->setDataByName("flags2", info_struct->get_flags2());
  802. if (version == 546) {
  803. if (player->get_character_flag(CF_ANONYMOUS))
  804. packet->setDataByName("flags_anonymous", 1);
  805. if (player->get_character_flag(CF_ROLEPLAYING))
  806. packet->setDataByName("flags_roleplaying", 1);
  807. if (player->get_character_flag(CF_AFK))
  808. packet->setDataByName("flags_afk", 1);
  809. if (player->get_character_flag(CF_LFG))
  810. packet->setDataByName("flags_lfg", 1);
  811. if (player->get_character_flag(CF_LFW))
  812. packet->setDataByName("flags_lfw", 1);
  813. if (!player->get_character_flag(CF_HIDE_HOOD) && !player->get_character_flag(CF_HIDE_HELM))
  814. packet->setDataByName("flags_show_hood", 1);
  815. if (player->get_character_flag(CF_SHOW_ILLUSION))
  816. packet->setDataByName("flags_show_illusion_form", 1);
  817. if (player->get_character_flag(CF_ALLOW_DUEL_INVITES))
  818. packet->setDataByName("flags_show_duel_invites", 1);
  819. if (player->get_character_flag(CF_ALLOW_TRADE_INVITES))
  820. packet->setDataByName("flags_show_trade_invites", 1);
  821. if (player->get_character_flag(CF_ALLOW_GROUP_INVITES))
  822. packet->setDataByName("flags_show_group_invites", 1);
  823. if (player->get_character_flag(CF_ALLOW_RAID_INVITES))
  824. packet->setDataByName("flags_show_raid_invites", 1);
  825. if (player->get_character_flag(CF_ALLOW_GUILD_INVITES))
  826. packet->setDataByName("flags_show_guild_invites", 1);
  827. }
  828. packet->setDataByName("haste", info_struct->get_haste());// dov confirmed
  829. packet->setDataByName("drunk", info_struct->get_drunk());// dov confirmed
  830. packet->setDataByName("hate_mod", info_struct->get_hate_mod());// dov confirmed
  831. packet->setDataByName("adventure_effects_bonus", 55);// NEED an adventure_effects_bonus// dov confirmed
  832. packet->setDataByName("tradeskill_effects_bonus", 56);// NEED an tradeskill_effects_bonus// dov confirmed
  833. packet->setDataByName("dps", info_struct->get_dps());// dov confirmed
  834. packet->setDataByName("melee_ae", info_struct->get_melee_ae());// dov confirmed
  835. packet->setDataByName("multi_attack", info_struct->get_multi_attack());// dov confirmed
  836. packet->setDataByName("spell_multi_attack", info_struct->get_spell_multi_attack());// dov confirmed
  837. packet->setDataByName("block_chance", info_struct->get_block_chance());// dov confirmed
  838. packet->setDataByName("crit_chance", info_struct->get_crit_chance());// dov confirmed
  839. packet->setDataByName("crit_bonus", info_struct->get_crit_bonus());// dov confirmed
  840. packet->setDataByName("potency", info_struct->get_potency());//info_struct->get_potency);// dov confirmed
  841. packet->setDataByName("reuse_speed", info_struct->get_reuse_speed());// dov confirmed
  842. packet->setDataByName("recovery_speed", info_struct->get_recovery_speed());// dov confirmed
  843. packet->setDataByName("casting_speed", info_struct->get_casting_speed());// dov confirmed
  844. packet->setDataByName("spell_reuse_speed", info_struct->get_spell_reuse_speed());// dov confirmed
  845. packet->setDataByName("strikethrough", info_struct->get_strikethrough());//dov confirmed
  846. packet->setDataByName("accuracy", info_struct->get_accuracy());//dov confirmed
  847. packet->setDataByName("critical_mit", info_struct->get_critical_mitigation());//dov /confirmed
  848. ((Entity*)player)->MStats.lock();
  849. packet->setDataByName("durability_mod", player->stats[ITEM_STAT_DURABILITY_MOD]);// dov confirmed
  850. packet->setDataByName("durability_add", player->stats[ITEM_STAT_DURABILITY_ADD]);// dov confirmed
  851. packet->setDataByName("progress_mod", player->stats[ITEM_STAT_PROGRESS_MOD]);// dov confirmed
  852. packet->setDataByName("progress_add", player->stats[ITEM_STAT_PROGRESS_ADD]);// dov confirmed
  853. packet->setDataByName("success_mod", player->stats[ITEM_STAT_SUCCESS_MOD]);// dov confirmed
  854. packet->setDataByName("crit_success_mod", player->stats[ITEM_STAT_CRIT_SUCCESS_MOD]);// dov confirmed
  855. ((Entity*)player)->MStats.unlock();
  856. if (version <= 373 && info_struct->get_pet_id() == 0xFFFFFFFF)
  857. packet->setDataByName("pet_id", 0);
  858. else {
  859. packet->setDataByName("pet_id", info_struct->get_pet_id());
  860. char pet_name[32];
  861. strncpy(pet_name, info_struct->get_pet_name().c_str(), version <= 373 ? 16 : 32);
  862. packet->setDataByName("pet_name", pet_name);
  863. }
  864. packet->setDataByName("pet_health_pct", info_struct->get_pet_health_pct());
  865. packet->setDataByName("pet_power_pct", info_struct->get_pet_power_pct());
  866. packet->setDataByName("pet_movement", info_struct->get_pet_movement());
  867. packet->setDataByName("pet_behavior", info_struct->get_pet_behavior());
  868. packet->setDataByName("rain", info_struct->get_rain());
  869. packet->setDataByName("rain2", info_struct->get_wind()); //-102.24);
  870. packet->setDataByName("status_points", info_struct->get_status_points());
  871. packet->setDataByName("guild_status", 888888);
  872. if (house_zone_id > 0){
  873. string house_name = database.GetZoneName(house_zone_id);
  874. if(house_name.length() > 0)
  875. packet->setDataByName("house_zone", house_name.c_str());
  876. }
  877. else
  878. packet->setDataByName("house_zone", "None");
  879. if (bind_zone_id > 0){
  880. string bind_name = database.GetZoneName(bind_zone_id);
  881. if(bind_name.length() > 0)
  882. packet->setDataByName("bind_zone", bind_name.c_str());
  883. }
  884. else
  885. packet->setDataByName("bind_zone", "None");
  886. ((Entity*)player)->MStats.lock();
  887. packet->setDataByName("rare_harvest_chance", player->stats[ITEM_STAT_RARE_HARVEST_CHANCE]);
  888. packet->setDataByName("max_crafting", player->stats[ITEM_STAT_MAX_CRAFTING]);
  889. packet->setDataByName("component_refund", player->stats[ITEM_STAT_COMPONENT_REFUND]);
  890. packet->setDataByName("ex_durability_mod", player->stats[ITEM_STAT_EX_DURABILITY_MOD]);
  891. packet->setDataByName("ex_durability_add", player->stats[ITEM_STAT_EX_DURABILITY_ADD]);
  892. packet->setDataByName("ex_crit_success_mod", player->stats[ITEM_STAT_EX_CRIT_SUCCESS_MOD]);
  893. packet->setDataByName("ex_crit_failure_mod", player->stats[ITEM_STAT_EX_CRIT_FAILURE_MOD]);
  894. packet->setDataByName("ex_progress_mod", player->stats[ITEM_STAT_EX_PROGRESS_MOD]);
  895. packet->setDataByName("ex_progress_add", player->stats[ITEM_STAT_EX_PROGRESS_ADD]);
  896. packet->setDataByName("ex_success_mod", player->stats[ITEM_STAT_EX_SUCCESS_MOD]);
  897. ((Entity*)player)->MStats.unlock();
  898. packet->setDataByName("flurry", info_struct->get_flurry());
  899. packet->setDataByName("unknown153", 153);
  900. packet->setDataByName("bountiful_harvest", 0); // need bountiful harvest
  901. packet->setDataByName("unknown156", 156);
  902. packet->setDataByName("unknown157", 157);
  903. packet->setDataByName("unknown159", 159);
  904. packet->setDataByName("unknown160", 160);
  905. packet->setDataByName("unknown163", 163);
  906. packet->setDataByName("unknown168", 168);
  907. packet->setDataByName("decrease_falling_dmg", 169);
  908. if (version <= 561) {
  909. packet->setDataByName("exp_yellow", info_struct->get_xp_yellow() / 10);
  910. packet->setDataByName("exp_blue", info_struct->get_xp_blue()/10);
  911. }
  912. else {
  913. packet->setDataByName("exp_yellow", info_struct->get_xp_yellow());
  914. packet->setDataByName("exp_blue", info_struct->get_xp_blue());
  915. }
  916. if (version <= 561) {
  917. packet->setDataByName("tradeskill_exp_yellow", info_struct->get_tradeskill_exp_yellow() / 10);
  918. packet->setDataByName("tradeskill_exp_blue", info_struct->get_tradeskill_exp_blue() / 10);
  919. }
  920. else {
  921. packet->setDataByName("tradeskill_exp_yellow", info_struct->get_tradeskill_exp_yellow());
  922. packet->setDataByName("tradeskill_exp_blue", info_struct->get_tradeskill_exp_blue());
  923. }
  924. packet->setDataByName("attack", info_struct->get_cur_attack());
  925. packet->setDataByName("attack_base", info_struct->get_attack_base());
  926. packet->setDataByName("absorb", info_struct->get_absorb());
  927. packet->setDataByName("mitigation_skill1", info_struct->get_mitigation_skill1());
  928. packet->setDataByName("mitigation_skill2", info_struct->get_mitigation_skill2());
  929. packet->setDataByName("mitigation_skill3", info_struct->get_mitigation_skill3());
  930. packet->setDataByName("mitigation_max", info_struct->get_max_mitigation());
  931. packet->setDataByName("savagery", 250);
  932. packet->setDataByName("max_savagery", 500);
  933. packet->setDataByName("savagery_level", 1);
  934. packet->setDataByName("max_savagery_level", 5);
  935. packet->setDataByName("dissonance", 5000);
  936. packet->setDataByName("max_dissonance", 10000);
  937. packet->setDataByName("mitigation_cur2", info_struct->get_cur_mitigation());
  938. packet->setDataByName("mitigation_max2", info_struct->get_max_mitigation());
  939. packet->setDataByName("mitigation_base2", info_struct->get_mitigation_base());
  940. packet->setDataByName("weight", info_struct->get_weight());
  941. packet->setDataByName("max_weight", info_struct->get_max_weight());
  942. packet->setDataByName("unknownint32a", 777777);
  943. packet->setDataByName("unknownint32b", 666666);
  944. packet->setDataByName("mitigation2_cur", 2367);
  945. packet->setDataByName("uncontested_riposte", info_struct->get_uncontested_riposte());
  946. packet->setDataByName("uncontested_dodge", info_struct->get_uncontested_dodge());
  947. packet->setDataByName("uncontested_parry", info_struct->get_uncontested_parry()); //????
  948. packet->setDataByName("uncontested_riposte_pve", 0); //????
  949. packet->setDataByName("uncontested_parry_pve", 0); //????
  950. packet->setDataByName("total_prestige_points", player->GetPrestigeAA());
  951. packet->setDataByName("unassigned_prestige_points", player->GetUnassignedPretigeAA());
  952. packet->setDataByName("total_tradeskill_points", player->GetTradeskillAA());
  953. packet->setDataByName("unassigned_tradeskill_points", player->GetUnassignedTradeskillAA());
  954. packet->setDataByName("total_tradeskill_prestige_points", player->GetTradeskillPrestigeAA());
  955. packet->setDataByName("unassigned_tradeskill_prestige_points", player->GetUnassignedTradeskillPrestigeAA());
  956. // unknown14c = percent aa exp to next level
  957. packet->setDataByName("unknown14d", 100, 0);
  958. packet->setDataByName("unknown20", 1084227584, 72);
  959. packet->setDataByName("unknown15c", 200);
  960. player->SetGroupInformation(packet);
  961. packet->setDataByName("in_combat_movement_speed", 125);
  962. packet->setDataByName("increase_max_power", 127);
  963. packet->setDataByName("increase_max_power2", 128);
  964. packet->setDataByName("vision", info_struct->get_vision());
  965. packet->setDataByName("breathe_underwater", info_struct->get_breathe_underwater());
  966. int32 expireTimestamp = 0;
  967. Spawn* maintained_target = 0;
  968. player->GetSpellEffectMutex()->readlock(__FUNCTION__, __LINE__);
  969. player->GetMaintainedMutex()->readlock(__FUNCTION__, __LINE__);
  970. for (int i = 0; i < 45; i++) {
  971. if (i < 30) {
  972. maintained_target = player->GetZone() ? player->GetZone()->GetSpawnByID(info_struct->maintained_effects[i].target) : nullptr;
  973. packet->setSubstructDataByName("maintained_effects", "name", info_struct->maintained_effects[i].name, i, 0);
  974. if (maintained_target)
  975. packet->setSubstructDataByName("maintained_effects", "target", player->GetIDWithPlayerSpawn(maintained_target), i, 0);
  976. packet->setSubstructDataByName("maintained_effects", "target_type", info_struct->maintained_effects[i].target_type, i, 0);
  977. packet->setSubstructDataByName("maintained_effects", "spell_id", info_struct->maintained_effects[i].spell_id, i, 0);
  978. packet->setSubstructDataByName("maintained_effects", "slot_pos", info_struct->maintained_effects[i].slot_pos, i, 0);
  979. packet->setSubstructDataByName("maintained_effects", "icon", info_struct->maintained_effects[i].icon, i, 0);
  980. packet->setSubstructDataByName("maintained_effects", "icon_type", info_struct->maintained_effects[i].icon_backdrop, i, 0);
  981. packet->setSubstructDataByName("maintained_effects", "conc_used", info_struct->maintained_effects[i].conc_used, i, 0);
  982. packet->setSubstructDataByName("maintained_effects", "unknown3", 1, i, 0);
  983. packet->setSubstructDataByName("maintained_effects", "total_time", info_struct->maintained_effects[i].total_time, i, 0);
  984. expireTimestamp = info_struct->maintained_effects[i].expire_timestamp;
  985. if (expireTimestamp == 0xFFFFFFFF)
  986. expireTimestamp = 0;
  987. packet->setSubstructDataByName("maintained_effects", "expire_timestamp", expireTimestamp, i, 0);
  988. }
  989. else if (version < 942)//version 942 added 15 additional spell effect slots
  990. break;
  991. packet->setSubstructDataByName("spell_effects", "spell_id", info_struct->spell_effects[i].spell_id, i, 0);
  992. packet->setSubstructDataByName("spell_effects", "total_time", info_struct->spell_effects[i].total_time, i, 0);
  993. expireTimestamp = info_struct->spell_effects[i].expire_timestamp;
  994. if (expireTimestamp == 0xFFFFFFFF)
  995. expireTimestamp = 0;
  996. packet->setSubstructDataByName("spell_effects", "expire_timestamp", expireTimestamp, i, 0);
  997. packet->setSubstructDataByName("spell_effects", "icon", info_struct->spell_effects[i].icon, i, 0);
  998. packet->setSubstructDataByName("spell_effects", "icon_type", info_struct->spell_effects[i].icon_backdrop, i, 0);
  999. if(info_struct->spell_effects[i].spell && info_struct->spell_effects[i].spell->spell && info_struct->spell_effects[i].spell->spell->GetSpellData()->friendly_spell == 1)
  1000. packet->setSubstructDataByName("spell_effects", "cancellable", 1, i);
  1001. }
  1002. player->GetMaintainedMutex()->releasereadlock(__FUNCTION__, __LINE__);
  1003. player->GetSpellEffectMutex()->releasereadlock(__FUNCTION__, __LINE__);
  1004. int8 det_count = 0;
  1005. //Send detriment counts as 255 if all dets of that type are incurable
  1006. det_count = player->GetTraumaCount();
  1007. if (det_count > 0) {
  1008. if (!player->HasCurableDetrimentType(DET_TYPE_TRAUMA))
  1009. det_count = 255;
  1010. }
  1011. packet->setDataByName("trauma_count", det_count);
  1012. det_count = player->GetArcaneCount();
  1013. if (det_count > 0) {
  1014. if (!player->HasCurableDetrimentType(DET_TYPE_ARCANE))
  1015. det_count = 255;
  1016. }
  1017. packet->setDataByName("arcane_count", det_count);
  1018. det_count = player->GetNoxiousCount();
  1019. if (det_count > 0) {
  1020. if (!player->HasCurableDetrimentType(DET_TYPE_NOXIOUS))
  1021. det_count = 255;
  1022. }
  1023. packet->setDataByName("noxious_count", det_count);
  1024. det_count = player->GetElementalCount();
  1025. if (det_count > 0) {
  1026. if (!player->HasCurableDetrimentType(DET_TYPE_ELEMENTAL))
  1027. det_count = 255;
  1028. }
  1029. packet->setDataByName("elemental_count", det_count);
  1030. det_count = player->GetCurseCount();
  1031. if (det_count > 0) {
  1032. if (!player->HasCurableDetrimentType(DET_TYPE_CURSE))
  1033. det_count = 255;
  1034. }
  1035. packet->setDataByName("curse_count", det_count);
  1036. player->GetDetrimentMutex()->readlock(__FUNCTION__, __LINE__);
  1037. vector<DetrimentalEffects>* det_list = player->GetDetrimentalSpellEffects();
  1038. DetrimentalEffects det;
  1039. int32 i = 0;
  1040. for (i = 0; i < det_list->size(); i++) {
  1041. det = det_list->at(i);
  1042. packet->setSubstructDataByName("detrimental_spell_effects", "spell_id", det.spell_id, i);
  1043. packet->setSubstructDataByName("detrimental_spell_effects", "total_time", det.total_time, i);
  1044. packet->setSubstructDataByName("detrimental_spell_effects", "icon", det.icon, i);
  1045. packet->setSubstructDataByName("detrimental_spell_effects", "icon_type", det.icon_backdrop, i);
  1046. expireTimestamp = det.expire_timestamp;
  1047. if (expireTimestamp == 0xFFFFFFFF)
  1048. expireTimestamp = 0;
  1049. packet->setSubstructDataByName("detrimental_spell_effects", "expire_timestamp", expireTimestamp, i);
  1050. packet->setSubstructDataByName("detrimental_spell_effects", "unknown2", 2, i);
  1051. if (i == 30) {
  1052. if (version < 942)
  1053. break;
  1054. }
  1055. else if (i == 45)
  1056. break;
  1057. }
  1058. if (version < 942) {
  1059. while (i < 30) {
  1060. packet->setSubstructDataByName("detrimental_spell_effects", "spell_id", 0xFFFFFFFF, i);
  1061. i++;
  1062. }
  1063. }
  1064. else {
  1065. while (i < 45) {
  1066. packet->setSubstructDataByName("detrimental_spell_effects", "spell_id", 0xFFFFFFFF, i);
  1067. i++;
  1068. }
  1069. }
  1070. player->GetDetrimentMutex()->releasereadlock(__FUNCTION__, __LINE__);
  1071. // disabling as not in use right now
  1072. //packet->setDataByName("spirit_rank", 2);
  1073. //packet->setDataByName("spirit", 1);
  1074. //packet->setDataByName("spirit_progress", .67);
  1075. packet->setDataByName("combat_exp_enabled", 1);
  1076. string* data = packet->serializeString();
  1077. int32 size = data->length();
  1078. //printf("CharSheet size: %u for version %u\n", size, version);
  1079. //DumpPacket((uchar*)data->c_str(), data->size());
  1080. //packet->PrintPacket();
  1081. uchar* tmp = new uchar[size];
  1082. bool reverse = version > 373;
  1083. if (!changes) {
  1084. orig_packet = new uchar[size];
  1085. changes = new uchar[size];
  1086. memcpy(orig_packet, (uchar*)data->c_str(), size);
  1087. size = Pack(tmp, orig_packet, size, size, version, reverse);
  1088. }
  1089. else {
  1090. memcpy(changes, (uchar*)data->c_str(), size);
  1091. if (modifyPos > 0) {
  1092. uchar* ptr2 = (uchar*)changes;
  1093. ptr2 += modifyPos - 1;
  1094. if (modifyValue > 0xFFFF) {
  1095. memcpy(ptr2, (uchar*)&modifyValue, 4);
  1096. }
  1097. else if (modifyValue > 0xFF) {
  1098. memcpy(ptr2, (uchar*)&modifyValue, 2);
  1099. }
  1100. else
  1101. memcpy(ptr2, (uchar*)&modifyValue, 1);
  1102. }
  1103. Encode(changes, orig_packet, size);
  1104. if (modifyPos > 0) {
  1105. uchar* ptr2 = (uchar*)orig_packet;
  1106. if (modifyPos > 64)
  1107. ptr2 += modifyPos - 64;
  1108. int16 tmpsize = modifyPos + 128;
  1109. if (tmpsize > size)
  1110. tmpsize = size;
  1111. }
  1112. size = Pack(tmp, changes, size, size, version, reverse);
  1113. }
  1114. if (version >= 546 && player->GetClient()) {
  1115. player->GetClient()->SendControlGhost();
  1116. }
  1117. EQ2Packet* ret_packet = new EQ2Packet(OP_UpdateCharacterSheetMsg, tmp, size);
  1118. safe_delete(packet);
  1119. safe_delete_array(tmp);
  1120. return ret_packet;
  1121. }
  1122. return 0;
  1123. }
  1124. EQ2Packet* PlayerInfo::serializePet(int16 version) {
  1125. PacketStruct* packet = configReader.getStruct("WS_CharacterPet", version);
  1126. if(packet) {
  1127. Spawn* pet = 0;
  1128. pet = player->GetPet();
  1129. if (!pet)
  1130. pet = player->GetCharmedPet();
  1131. if (pet) {
  1132. packet->setDataByName("current_hp", pet->GetHP());
  1133. packet->setDataByName("max_hp", pet->GetTotalHP());
  1134. packet->setDataByName("base_hp", pet->GetTotalHPBase());
  1135. packet->setDataByName("current_power", pet->GetPower());
  1136. packet->setDataByName("max_power", pet->GetTotalPower());
  1137. packet->setDataByName("base_power", pet->GetTotalPowerBase());
  1138. packet->setDataByName("spawn_id", info_struct->get_pet_id());
  1139. packet->setDataByName("spawn_id2", info_struct->get_pet_id());
  1140. if(info_struct->get_pet_id() != 0xFFFFFFFF) {
  1141. packet->setDataByName("pet_id", info_struct->get_pet_id());
  1142. char pet_name[32];
  1143. strncpy(pet_name, info_struct->get_pet_name().c_str(), 32);
  1144. packet->setDataByName("name", pet_name);
  1145. }
  1146. else {
  1147. packet->setDataByName("name", "No Pet");
  1148. packet->setDataByName("no_pet", "No Pet");
  1149. }
  1150. if (version >= 57000) {
  1151. packet->setDataByName("current_power3", pet->GetPower());
  1152. packet->setDataByName("max_power3", pet->GetTotalPower());
  1153. packet->setDataByName("health_pct_tooltip", (double)info_struct->get_pet_health_pct());
  1154. packet->setDataByName("health_pct_bar", (double)info_struct->get_pet_health_pct());
  1155. }
  1156. else {
  1157. packet->setDataByName("health_pct_tooltip", info_struct->get_pet_health_pct());
  1158. packet->setDataByName("health_pct_bar", info_struct->get_pet_health_pct());
  1159. }
  1160. packet->setDataByName("power_pct_tooltip", info_struct->get_pet_power_pct());
  1161. packet->setDataByName("power_pct_bar", info_struct->get_pet_power_pct());
  1162. packet->setDataByName("unknown5", 255); // Hate % maybe
  1163. packet->setDataByName("movement", info_struct->get_pet_movement());
  1164. packet->setDataByName("behavior", info_struct->get_pet_behavior());
  1165. }
  1166. else {
  1167. packet->setDataByName("current_hp", 0);
  1168. packet->setDataByName("max_hp", 0);
  1169. packet->setDataByName("base_hp", 0);
  1170. packet->setDataByName("current_power", 0);
  1171. packet->setDataByName("max_power", 0);
  1172. packet->setDataByName("base_power", 0);
  1173. packet->setDataByName("spawn_id", 0);
  1174. packet->setDataByName("spawn_id2", 0xFFFFFFFF);
  1175. packet->setDataByName("name", "");
  1176. packet->setDataByName("no_pet", "No Pet");
  1177. packet->setDataByName("health_pct_tooltip", 0);
  1178. packet->setDataByName("health_pct_bar", 0);
  1179. packet->setDataByName("power_pct_tooltip", 0);
  1180. packet->setDataByName("power_pct_bar", 0);
  1181. packet->setDataByName("unknown5", 0);
  1182. packet->setDataByName("movement", 0);
  1183. packet->setDataByName("behavior", 0);
  1184. }
  1185. string* data = packet->serializeString();
  1186. int32 size = data->length();
  1187. uchar* tmp = new uchar[size];
  1188. // if this is the first time sending this packet create the buffers
  1189. if(!pet_changes){
  1190. pet_orig_packet = new uchar[size];
  1191. pet_changes = new uchar[size];
  1192. // copy the packet into the pet_orig_packet so we can xor against it in the future
  1193. memcpy(pet_orig_packet, (uchar*)data->c_str(), size);
  1194. // pack the packet, result ends up in tmp
  1195. size = Pack(tmp, (uchar*)data->c_str(), size, size, version);
  1196. }
  1197. else{
  1198. // copy the packet into pet_changes
  1199. memcpy(pet_changes, (uchar*)data->c_str(), size);
  1200. // XOR's the packet to the original, stores the new packet in the orig packet (will xor against that for the next update)
  1201. // puts the xor packet into pet_changes.
  1202. Encode(pet_changes, pet_orig_packet, size);
  1203. // Pack the pet_changes packet, will put the packed size at the start, result ends up in tmp
  1204. size = Pack(tmp, pet_changes, size, size, version);
  1205. }
  1206. // Create the packet that we will send
  1207. EQ2Packet* ret_packet = new EQ2Packet(OP_CharacterPet, tmp, size+4);
  1208. // Clean up
  1209. safe_delete_array(tmp);
  1210. safe_delete(packet);
  1211. // Return the packet that will be sent to the client
  1212. return ret_packet;
  1213. }
  1214. return 0;
  1215. }
  1216. bool Player::DamageEquippedItems(int8 amount, Client* client) {
  1217. bool ret = false;
  1218. int8 item_type;
  1219. Item* item = 0;
  1220. equipment_list.MEquipmentItems.readlock(__FUNCTION__, __LINE__);
  1221. for(int8 i=0;i<NUM_SLOTS;i++){
  1222. item = equipment_list.items[i];
  1223. if(item) {
  1224. item_type = item->generic_info.item_type;
  1225. if (item->details.item_id > 0 && item_type != ITEM_TYPE_FOOD && item_type != ITEM_TYPE_BAUBLE && item_type != ITEM_TYPE_THROWN &&
  1226. !item->CheckFlag2(INDESTRUCTABLE)){
  1227. ret = true;
  1228. if((item->generic_info.condition - amount) > 0)
  1229. item->generic_info.condition -= amount;
  1230. else
  1231. item->generic_info.condition = 0;
  1232. item->save_needed = true;
  1233. if (client)
  1234. client->QueuePacket(item->serialize(client->GetVersion(), false, this));
  1235. }
  1236. }
  1237. }
  1238. equipment_list.MEquipmentItems.releasereadlock(__FUNCTION__, __LINE__);
  1239. return ret;
  1240. }
  1241. int16 Player::ConvertSlotToClient(int8 slot, int16 version) {
  1242. if (version <= 373) {
  1243. if (slot == EQ2_FOOD_SLOT)
  1244. slot = EQ2_ORIG_FOOD_SLOT;
  1245. else if (slot == EQ2_DRINK_SLOT)
  1246. slot = EQ2_ORIG_DRINK_SLOT;
  1247. else if (slot > EQ2_EARS_SLOT_1 && slot <= EQ2_WAIST_SLOT)
  1248. slot -= 1;
  1249. }
  1250. else if (version <= 561) {
  1251. if (slot == EQ2_FOOD_SLOT)
  1252. slot = EQ2_DOF_FOOD_SLOT;
  1253. else if (slot == EQ2_DRINK_SLOT)
  1254. slot = EQ2_DOF_DRINK_SLOT;
  1255. else if (slot > EQ2_EARS_SLOT_1 && slot <= EQ2_WAIST_SLOT)
  1256. slot -= 1;
  1257. }
  1258. return slot;
  1259. }
  1260. int16 Player::ConvertSlotFromClient(int8 slot, int16 version) {
  1261. if (version <= 373) {
  1262. if (slot == EQ2_ORIG_FOOD_SLOT)
  1263. slot = EQ2_FOOD_SLOT;
  1264. else if (slot == EQ2_ORIG_DRINK_SLOT)
  1265. slot = EQ2_DRINK_SLOT;
  1266. else if (slot > EQ2_EARS_SLOT_1 && slot <= EQ2_WAIST_SLOT)
  1267. slot += 1;
  1268. }
  1269. else if (version <= 561) {
  1270. if (slot == EQ2_DOF_FOOD_SLOT)
  1271. slot = EQ2_FOOD_SLOT;
  1272. else if (slot == EQ2_DOF_DRINK_SLOT)
  1273. slot = EQ2_DRINK_SLOT;
  1274. else if (slot > EQ2_EARS_SLOT_1 && slot <= EQ2_WAIST_SLOT)
  1275. slot += 1;
  1276. }
  1277. return slot;
  1278. }
  1279. int16 Player::GetNumSlotsEquip(int16 version) {
  1280. if(version <= 561) {
  1281. return CLASSIC_NUM_SLOTS;
  1282. }
  1283. return NUM_SLOTS;
  1284. }
  1285. int8 Player::GetMaxBagSlots(int16 version) {
  1286. if(version <= 373) {
  1287. return CLASSIC_EQ_MAX_BAG_SLOTS;
  1288. }
  1289. else if(version <= 561) {
  1290. return DOF_EQ_MAX_BAG_SLOTS;
  1291. }
  1292. return 255;
  1293. }
  1294. vector<EQ2Packet*> Player::UnequipItem(int16 index, sint32 bag_id, int8 slot, int16 version, int8 appearance_type, bool send_item_updates) {
  1295. vector<EQ2Packet*> packets;
  1296. EquipmentItemList* equipList = &equipment_list;
  1297. if(appearance_type)
  1298. equipList = &appearance_equipment_list;
  1299. if(index >= NUM_SLOTS) {
  1300. LogWrite(PLAYER__ERROR, 0, "Player", "%u index is out of range for equip items, bag_id: %i, slot: %u, version: %u, appearance: %u", index, bag_id, slot, version, appearance_type);
  1301. return packets;
  1302. }
  1303. equipList->MEquipmentItems.readlock(__FUNCTION__, __LINE__);
  1304. Item* item = equipList->items[index];
  1305. if(item && !IsAllowedCombatEquip(item->details.slot_id, true)) {
  1306. LogWrite(PLAYER__ERROR, 0, "Player", "Attempt to unequip item %s (%u) FAILED in combat!", item->name.c_str(), item->details.item_id);
  1307. equipList->MEquipmentItems.releasereadlock(__FUNCTION__, __LINE__);
  1308. return packets;
  1309. }
  1310. equipList->MEquipmentItems.releasereadlock(__FUNCTION__, __LINE__);
  1311. if (item && bag_id == -999) {
  1312. int8 old_slot = item->details.slot_id;
  1313. if(item->details.equip_slot_id) {
  1314. if (item->GetItemScript() && lua_interface)
  1315. lua_interface->RunItemScript(item->GetItemScript(), "unequipped", item, this);
  1316. const char* zone_script = world.GetZoneScript(GetZone()->GetZoneID());
  1317. if (zone_script && lua_interface)
  1318. lua_interface->RunZoneScript(zone_script, "item_unequipped", GetZone(), this, item->details.item_id, item->name.c_str(), 0, item->details.unique_id);
  1319. item->save_needed = true;
  1320. EQ2Packet* outapp = item_list.serialize(this, version);
  1321. if (outapp) {
  1322. packets.push_back(outapp);
  1323. packets.push_back(item->serialize(version, false));
  1324. EQ2Packet* bag_packet = SendBagUpdate(item->details.inv_slot_id, version);
  1325. if (bag_packet)
  1326. packets.push_back(bag_packet);
  1327. }
  1328. sint16 equip_slot_id = item->details.equip_slot_id;
  1329. item->details.equip_slot_id = 0;
  1330. equipList->RemoveItem(index);
  1331. SetEquippedItemAppearances();
  1332. packets.push_back(equipList->serialize(version, this));
  1333. SetCharSheetChanged(true);
  1334. SetEquipment(0, equip_slot_id ? equip_slot_id : old_slot);
  1335. }
  1336. else if (item_list.AssignItemToFreeSlot(item)) {
  1337. if(appearance_type)
  1338. database.DeleteItem(GetCharacterID(), item, "APPEARANCE");
  1339. else
  1340. database.DeleteItem(GetCharacterID(), item, "EQUIPPED");
  1341. if (item->GetItemScript() && lua_interface)
  1342. lua_interface->RunItemScript(item->GetItemScript(), "unequipped", item, this);
  1343. const char* zone_script = world.GetZoneScript(GetZone()->GetZoneID());
  1344. if (zone_script && lua_interface)
  1345. lua_interface->RunZoneScript(zone_script, "item_unequipped", GetZone(), this, item->details.item_id, item->name.c_str(), 0, item->details.unique_id);
  1346. item->save_needed = true;
  1347. EQ2Packet* outapp = item_list.serialize(this, version);
  1348. if (outapp) {
  1349. packets.push_back(outapp);
  1350. packets.push_back(item->serialize(version, false));
  1351. EQ2Packet* bag_packet = SendBagUpdate(item->details.inv_slot_id, version);
  1352. if (bag_packet)
  1353. packets.push_back(bag_packet);
  1354. }
  1355. equipList->RemoveItem(index);
  1356. SetEquippedItemAppearances();
  1357. packets.push_back(equipList->serialize(version, this));
  1358. SetCharSheetChanged(true);
  1359. SetEquipment(0, old_slot);
  1360. }
  1361. else {
  1362. PacketStruct* packet = configReader.getStruct("WS_DisplayText", version);
  1363. if (packet) {
  1364. packet->setDataByName("color", CHANNEL_COLOR_YELLOW);
  1365. packet->setMediumStringByName("text", "Unable to unequip item: no free inventory locations.");
  1366. packet->setDataByName("unknown02", 0x00ff);
  1367. packets.push_back(packet->serialize());
  1368. safe_delete(packet);
  1369. }
  1370. }
  1371. }
  1372. else if (item) {
  1373. Item* to_item = 0;
  1374. if(appearance_type && slot == 255)
  1375. {
  1376. sint16 tmpSlot = 0;
  1377. item_list.GetFirstFreeSlot(&bag_id, &tmpSlot);
  1378. if(tmpSlot >= 0 && tmpSlot < 255)
  1379. slot = tmpSlot;
  1380. else
  1381. bag_id = 0;
  1382. }
  1383. item_list.MPlayerItems.readlock(__FUNCTION__, __LINE__);
  1384. if (item_list.items.count(bag_id) > 0 && item_list.items[bag_id][BASE_EQUIPMENT].count(slot) > 0)
  1385. to_item = item_list.items[bag_id][BASE_EQUIPMENT][slot];
  1386. bool canEquipToSlot = false;
  1387. if (to_item && equipList->CanItemBeEquippedInSlot(to_item, item->details.slot_id)) {
  1388. canEquipToSlot = true;
  1389. }
  1390. item_list.MPlayerItems.releasereadlock(__FUNCTION__, __LINE__);
  1391. if (canEquipToSlot) {
  1392. equipList->RemoveItem(index);
  1393. if(item->details.appearance_type)
  1394. database.DeleteItem(GetCharacterID(), item, "APPEARANCE");
  1395. else
  1396. database.DeleteItem(GetCharacterID(), item, "EQUIPPED");
  1397. database.DeleteItem(GetCharacterID(), to_item, "NOT-EQUIPPED");
  1398. if (item->GetItemScript() && lua_interface)
  1399. lua_interface->RunItemScript(item->GetItemScript(), "unequipped", item, this);
  1400. if (to_item->GetItemScript() && lua_interface)
  1401. lua_interface->RunItemScript(to_item->GetItemScript(), "equipped", to_item, this);
  1402. if(item->IsBag() && ( item->details.inv_slot_id != bag_id || item->details.slot_id != slot)) {
  1403. item_list.EraseItem(item);
  1404. }
  1405. item_list.RemoveItem(to_item);
  1406. equipList->SetItem(item->details.slot_id, to_item);
  1407. to_item->save_needed = true;
  1408. packets.push_back(to_item->serialize(version, false));
  1409. SetEquipment(to_item);
  1410. item->details.inv_slot_id = bag_id;
  1411. item->details.slot_id = slot;
  1412. item->details.appearance_type = 0;
  1413. item->details.equip_slot_id = 0;
  1414. if(!item->IsBag() && item_list.AddItem(item)) { // bags are omitted because they are equipped while remaining in inventory
  1415. item->save_needed = true;
  1416. SetEquippedItemAppearances();
  1417. // SerializeItemPackets serves item and equipList in opposite order is why we don't use that function here..
  1418. packets.push_back(item->serialize(version, false));
  1419. packets.push_back(equipList->serialize(version, this));
  1420. packets.push_back(item_list.serialize(this, version));
  1421. }
  1422. else if(item->IsBag()) {
  1423. // already in inventory
  1424. }
  1425. else {
  1426. LogWrite(PLAYER__ERROR, 0, "Player", "failed to add item to item_list during UnequipItem, index %u, bag id %i, slot %u, version %u, appearance type %u", index, bag_id, slot, version, appearance_type);
  1427. }
  1428. }
  1429. else if (to_item && to_item->IsBag() && to_item->details.num_slots > 0) {
  1430. bool free_slot = false;
  1431. for (int8 i = 0; i < to_item->details.num_slots; i++) {
  1432. item_list.MPlayerItems.readlock(__FUNCTION__, __LINE__);
  1433. int32 count = item_list.items[to_item->details.bag_id][appearance_type].count(i);
  1434. item_list.MPlayerItems.releasereadlock(__FUNCTION__, __LINE__);
  1435. if (count == 0) {
  1436. SetEquipment(0, item->details.equip_slot_id ? item->details.equip_slot_id : item->details.slot_id);
  1437. if(item->details.appearance_type)
  1438. database.DeleteItem(GetCharacterID(), item, "APPEARANCE");
  1439. else
  1440. database.DeleteItem(GetCharacterID(), item, "EQUIPPED");
  1441. if (item->GetItemScript() && lua_interface)
  1442. lua_interface->RunItemScript(item->GetItemScript(), "unequipped", item, this);
  1443. if(item->IsBag() && item != to_item) {
  1444. item_list.EraseItem(item);
  1445. }
  1446. equipList->RemoveItem(index);
  1447. if(!item->IsBag()) {
  1448. item->details.inv_slot_id = to_item->details.bag_id;
  1449. item->details.slot_id = i;
  1450. item->details.appearance_type = to_item->details.appearance_type;
  1451. }
  1452. else {
  1453. item->details.appearance_type = 0;
  1454. }
  1455. item->details.equip_slot_id = 0;
  1456. SerializeItemPackets(equipList, &packets, item, version, to_item);
  1457. free_slot = true;
  1458. break;
  1459. }
  1460. }
  1461. if (!free_slot) {
  1462. PacketStruct* packet = configReader.getStruct("WS_DisplayText", version);
  1463. if (packet) {
  1464. packet->setDataByName("color", CHANNEL_COLOR_YELLOW);
  1465. packet->setMediumStringByName("text", "Unable to unequip item: no free space in the bag.");
  1466. packet->setDataByName("unknown02", 0x00ff);
  1467. packets.push_back(packet->serialize());
  1468. safe_delete(packet);
  1469. }
  1470. }
  1471. }
  1472. else if (to_item) {
  1473. PacketStruct* packet = configReader.getStruct("WS_DisplayText", version);
  1474. if (packet) {
  1475. packet->setDataByName("color", CHANNEL_COLOR_YELLOW);
  1476. packet->setMediumStringByName("text", "Unable to swap items: that item cannot be equipped there.");
  1477. packet->setDataByName("unknown02", 0x00ff);
  1478. packets.push_back(packet->serialize());
  1479. safe_delete(packet);
  1480. }
  1481. }
  1482. else {
  1483. if ((bag_id == 0 && slot < NUM_INV_SLOTS) || (bag_id == -3 && slot < NUM_BANK_SLOTS) || (bag_id == -4 && slot < NUM_SHARED_BANK_SLOTS)) {
  1484. if (bag_id == -4 && item->CheckFlag(NO_TRADE)) {
  1485. PacketStruct* packet = configReader.getStruct("WS_DisplayText", version);
  1486. if (packet) {
  1487. packet->setDataByName("color", CHANNEL_COLOR_YELLOW);
  1488. packet->setMediumStringByName("text", "Unable to unequip item: that item cannot be traded.");
  1489. packet->setDataByName("unknown02", 0x00ff);
  1490. packets.push_back(packet->serialize());
  1491. safe_delete(packet);
  1492. }
  1493. }
  1494. else {
  1495. // need to check if appearance slot vs equipped
  1496. SetEquipment(0, item->details.equip_slot_id ? item->details.equip_slot_id : item->details.slot_id);
  1497. if(item->details.appearance_type)
  1498. database.DeleteItem(GetCharacterID(), item, "APPEARANCE");
  1499. else
  1500. database.DeleteItem(GetCharacterID(), item, "EQUIPPED");
  1501. if (item->GetItemScript() && lua_interface)
  1502. lua_interface->RunItemScript(item->GetItemScript(), "unequipped", item, this);
  1503. if(item->IsBag() && (item->details.inv_slot_id != bag_id || item->details.slot_id != slot)) {
  1504. item_list.EraseItem(item);
  1505. }
  1506. equipList->RemoveItem(index);
  1507. item->details.inv_slot_id = bag_id;
  1508. item->details.slot_id = slot;
  1509. item->details.appearance_type = 0;
  1510. item->details.equip_slot_id = 0;
  1511. SerializeItemPackets(equipList, &packets, item, version);
  1512. }
  1513. }
  1514. else {
  1515. Item* bag = item_list.GetItemFromUniqueID(bag_id, true);
  1516. if (bag && bag->IsBag() && slot < bag->details.num_slots) {
  1517. SetEquipment(0, item->details.equip_slot_id ? item->details.equip_slot_id : item->details.slot_id);
  1518. if(item->details.appearance_type)
  1519. database.DeleteItem(GetCharacterID(), item, "APPEARANCE");
  1520. else
  1521. database.DeleteItem(GetCharacterID(), item, "EQUIPPED");
  1522. if (item->GetItemScript() && lua_interface)
  1523. lua_interface->RunItemScript(item->GetItemScript(), "unequipped", item, this);
  1524. if(item->IsBag() && ( item->details.inv_slot_id != bag_id || item->details.slot_id != slot)) {
  1525. item_list.EraseItem(item);
  1526. }
  1527. equipList->RemoveItem(index);
  1528. item->details.inv_slot_id = bag_id;
  1529. item->details.slot_id = slot;
  1530. item->details.appearance_type = 0;
  1531. item->details.equip_slot_id = 0;
  1532. SerializeItemPackets(equipList, &packets, item, version);
  1533. }
  1534. }
  1535. }
  1536. Item* bag = item_list.GetItemFromUniqueID(bag_id, true);
  1537. if (bag && bag->IsBag())
  1538. packets.push_back(bag->serialize(version, false, this));
  1539. }
  1540. if(send_item_updates && GetClient())
  1541. {
  1542. GetClient()->UpdateSentSpellList();
  1543. GetClient()->ClearSentSpellList();
  1544. }
  1545. return packets;
  1546. }
  1547. map<int32, Item*>* Player::GetItemList(){
  1548. return item_list.GetAllItems();
  1549. }
  1550. vector<Item*>* Player::GetEquippedItemList(){
  1551. return equipment_list.GetAllEquippedItems();
  1552. }
  1553. vector<Item*>* Player::GetAppearanceEquippedItemList(){
  1554. return appearance_equipment_list.GetAllEquippedItems();
  1555. }
  1556. EQ2Packet* Player::SendBagUpdate(int32 bag_unique_id, int16 version){
  1557. Item* bag = 0;
  1558. if(bag_unique_id > 0)
  1559. bag = item_list.GetItemFromUniqueID(bag_unique_id, true);
  1560. if(bag && bag->IsBag())
  1561. return bag->serialize(version, false, this);
  1562. return 0;
  1563. }
  1564. void Player::SetEquippedItemAppearances(){
  1565. vector<Item*>* items = GetEquipmentList()->GetAllEquippedItems();
  1566. vector<Item*>* appearance_items = GetAppearanceEquipmentList()->GetAllEquippedItems();
  1567. if(items){
  1568. for(int32 i=0;i<items->size();i++)
  1569. SetEquipment(items->at(i));
  1570. // just have appearance items brute force replace the slots after the fact
  1571. for(int32 i=0;i<appearance_items->size();i++)
  1572. SetEquipment(appearance_items->at(i));
  1573. }
  1574. safe_delete(items);
  1575. safe_delete(appearance_items);
  1576. info_changed = true;
  1577. GetZone()->SendSpawnChanges(this);
  1578. }
  1579. EQ2Packet* Player::SwapEquippedItems(int8 slot1, int8 slot2, int16 version, int16 equip_type){
  1580. EquipmentItemList* equipList = &equipment_list;
  1581. // right now client seems to pass 3 for this? Not sure why when other fields has appearance equipment as type 1
  1582. if(equip_type == 3)
  1583. equipList = &appearance_equipment_list;
  1584. equipList->MEquipmentItems.readlock(__FUNCTION__, __LINE__);
  1585. Item* item_from = equipList->items[slot1];
  1586. Item* item_to = equipList->items[slot2];
  1587. equipList->MEquipmentItems.releasereadlock(__FUNCTION__, __LINE__);
  1588. if(item_from && equipList->CanItemBeEquippedInSlot(item_from, slot2)){
  1589. if(item_to){
  1590. if(!equipList->CanItemBeEquippedInSlot(item_to, slot1))
  1591. return 0;
  1592. }
  1593. equipList->MEquipmentItems.writelock(__FUNCTION__, __LINE__);
  1594. equipList->items[slot1] = nullptr;
  1595. equipList->MEquipmentItems.releasewritelock(__FUNCTION__, __LINE__);
  1596. equipList->SetItem(slot2, item_from);
  1597. if(item_to)
  1598. {
  1599. equipList->SetItem(slot1, item_to);
  1600. item_to->save_needed = true;
  1601. }
  1602. item_from->save_needed = true;
  1603. if (GetClient())
  1604. {
  1605. //EquipmentItemList* equipList = &equipment_list;
  1606. //if(appearance_type)
  1607. // equipList = &appearance_equipment_list;
  1608. if(item_to)
  1609. GetClient()->QueuePacket(item_to->serialize(version, false, this));
  1610. GetClient()->QueuePacket(item_from->serialize(version, false, this));
  1611. GetClient()->QueuePacket(item_list.serialize(this, version));
  1612. }
  1613. return equipList->serialize(version, this);
  1614. }
  1615. return 0;
  1616. }
  1617. bool Player::CanEquipItem(Item* item, int8 slot) {
  1618. if(client && client->GetVersion() <= 561 && slot == EQ2_EARS_SLOT_2)
  1619. return false;
  1620. if (item) {
  1621. Client* client = GetClient();
  1622. if (client) {
  1623. if (item->IsWeapon() && slot == 1) {
  1624. bool dwable = item->IsDualWieldAble(client, item, slot);
  1625. if (dwable == 0) {
  1626. return false;
  1627. }
  1628. }
  1629. if (item->CheckFlag(EVIL_ONLY) && GetAlignment() != ALIGNMENT_EVIL) {
  1630. client->Message(0, "%s requires an evil race.", item->name.c_str());
  1631. }
  1632. else if (item->CheckFlag(GOOD_ONLY) && GetAlignment() != ALIGNMENT_GOOD) {
  1633. client->Message(0, "%s requires a good race.", item->name.c_str());
  1634. }
  1635. else if (item->IsArmor() || item->IsWeapon() || item->IsFood() || item->IsRanged() || item->IsShield() || item->IsBauble() || item->IsAmmo() || item->IsThrown()) {
  1636. if (((item->generic_info.skill_req1 == 0 || item->generic_info.skill_req1 == 0xFFFFFFFF || skill_list.HasSkill(item->generic_info.skill_req1)) && (item->generic_info.skill_req2 == 0 || item->generic_info.skill_req2 == 0xFFFFFFFF || skill_list.HasSkill(item->generic_info.skill_req2)))) {
  1637. int16 override_level = item->GetOverrideLevel(GetAdventureClass(), GetTradeskillClass());
  1638. if (override_level > 0 && override_level <= GetLevel())
  1639. return true;
  1640. if (item->CheckClass(GetAdventureClass(), GetTradeskillClass()))
  1641. if (item->CheckLevel(GetAdventureClass(), GetTradeskillClass(), GetLevel()))
  1642. return true;
  1643. else
  1644. client->Message(CHANNEL_COLOR_RED, "You must be at least level %u to equip %s.", item->generic_info.adventure_default_level, item->CreateItemLink(client->GetVersion()).c_str());
  1645. else
  1646. client->Message(CHANNEL_COLOR_RED, "Your class may not equip %s.", item->CreateItemLink(client->GetVersion()).c_str());
  1647. }
  1648. else {
  1649. Skill* firstSkill = master_skill_list.GetSkill(item->generic_info.skill_req1);
  1650. Skill* secondSkill = master_skill_list.GetSkill(item->generic_info.skill_req2);
  1651. std::string msg("");
  1652. if(GetClient()->GetAdminStatus() >= 200) {
  1653. if(firstSkill && !skill_list.HasSkill(item->generic_info.skill_req1)) {
  1654. msg += "(" + std::string(firstSkill->name.data.c_str());
  1655. }
  1656. if(secondSkill && !skill_list.HasSkill(item->generic_info.skill_req2)) {
  1657. if(msg.length() > 0) {
  1658. msg += ", ";
  1659. }
  1660. else {
  1661. msg = "(";
  1662. }
  1663. msg += std::string(secondSkill->name.data.c_str());
  1664. }
  1665. if(msg.length() > 0) {
  1666. msg += ") ";
  1667. }
  1668. }
  1669. client->Message(0, "You lack the skill %srequired to equip this item.",msg.c_str());
  1670. }
  1671. }
  1672. else
  1673. client->Message(0, "Item %s isn't equipable.", item->name.c_str());
  1674. }
  1675. }
  1676. return false;
  1677. }
  1678. vector<EQ2Packet*> Player::EquipItem(int16 index, int16 version, int8 appearance_type, int8 slot_id) {
  1679. EquipmentItemList* equipList = &equipment_list;
  1680. if(appearance_type)
  1681. equipList = &appearance_equipment_list;
  1682. vector<EQ2Packet*> packets;
  1683. item_list.MPlayerItems.readlock(__FUNCTION__, __LINE__);
  1684. if (item_list.indexed_items.count(index) == 0) {
  1685. item_list.MPlayerItems.releasereadlock(__FUNCTION__, __LINE__);
  1686. return packets;
  1687. }
  1688. Item* item = item_list.indexed_items[index];
  1689. int8 orig_slot_id = slot_id;
  1690. int8 slot = 255;
  1691. if (item) {
  1692. if(orig_slot_id == 255 && item->CheckFlag2(APPEARANCE_ONLY)) {
  1693. appearance_type = 1;
  1694. equipList = &appearance_equipment_list;
  1695. }
  1696. if (slot_id != 255 && !item->HasSlot(slot_id)) {
  1697. item_list.MPlayerItems.releasereadlock(__FUNCTION__, __LINE__);
  1698. return packets;
  1699. }
  1700. slot = equipList->GetFreeSlot(item, slot_id, version);
  1701. bool canEquip = CanEquipItem(item,slot);
  1702. int32 conflictSlot = 0;
  1703. if(canEquip && !appearance_type && item->CheckFlag2(APPEARANCE_ONLY))
  1704. {
  1705. item_list.MPlayerItems.releasereadlock(__FUNCTION__, __LINE__);
  1706. if(GetClient()) {
  1707. GetClient()->SimpleMessage(CHANNEL_COLOR_RED, "This item is for appearance slots only.");
  1708. }
  1709. return packets;
  1710. }
  1711. else if(canEquip && (conflictSlot = equipList->CheckSlotConflict(item)) > 0) {
  1712. bool abort = true;
  1713. switch(conflictSlot) {
  1714. case LORE:
  1715. if(GetClient())
  1716. GetClient()->SimpleMessage(CHANNEL_COLOR_RED, "Lore conflict, cannot equip this item.");
  1717. break;
  1718. case LORE_EQUIP:
  1719. if(GetClient())
  1720. GetClient()->SimpleMessage(CHANNEL_COLOR_RED, "You already have this item equipped, you cannot equip another.");
  1721. break;
  1722. case STACK_LORE:
  1723. if(GetClient())
  1724. GetClient()->SimpleMessage(CHANNEL_COLOR_RED, "Cannot equip as it exceeds lore stack.");
  1725. break;
  1726. default:
  1727. abort = false;
  1728. break;
  1729. }
  1730. if(abort) {
  1731. item_list.MPlayerItems.releasereadlock(__FUNCTION__, __LINE__);
  1732. return packets;
  1733. }
  1734. }
  1735. else if (canEquip && item->CheckFlag(ATTUNEABLE)) {
  1736. PacketStruct* packet = configReader.getStruct("WS_ChoiceWindow", version);
  1737. char text[255];
  1738. sprintf(text, "%s must be attuned before it can be equipped. Would you like to attune it now?", item->name.c_str());
  1739. char accept_command[25];
  1740. sprintf(accept_command, "attune_inv %i 1 0 -1", index);
  1741. packet->setDataByName("text", text);
  1742. packet->setDataByName("accept_text", "Attune");
  1743. packet->setDataByName("accept_command", accept_command);
  1744. packet->setDataByName("cancel_text", "Cancel");
  1745. // No clue if we even need the following 2 unknowns, just added them so the packet matches what live sends
  1746. packet->setDataByName("max_length", 50);
  1747. packet->setDataByName("unknown4", 1);
  1748. packets.push_back(packet->serialize());
  1749. safe_delete(packet);
  1750. item_list.MPlayerItems.releasereadlock(__FUNCTION__, __LINE__);
  1751. return packets;
  1752. }
  1753. if (canEquip && slot == 255)
  1754. {
  1755. if (slot_id == 255) {
  1756. if(item->slot_data.size() > 0) {
  1757. slot = item->slot_data.at(0);
  1758. if(!IsAllowedCombatEquip(slot, true)) {
  1759. LogWrite(PLAYER__ERROR, 0, "Player", "Attempt to equip item %s (%u) with FAILED in combat!", item->name.c_str(), item->details.item_id);
  1760. item_list.MPlayerItems.releasereadlock(__FUNCTION__, __LINE__);
  1761. return packets;
  1762. }
  1763. }
  1764. else {
  1765. LogWrite(PLAYER__ERROR, 0, "Player", "Attempt to equip item %s (%u) with auto equip FAILED, no slot_data exists! Check items table, 'slots' column value should not be 0.", item->name.c_str(), item->details.item_id);
  1766. item_list.MPlayerItems.releasereadlock(__FUNCTION__, __LINE__);
  1767. return packets;
  1768. }
  1769. }
  1770. else
  1771. slot = slot_id;
  1772. item_list.MPlayerItems.releasereadlock(__FUNCTION__, __LINE__);
  1773. packets = UnequipItem(slot, item->details.inv_slot_id, item->details.slot_id, version, appearance_type, false);
  1774. // grab player items lock again and assure item still present
  1775. item_list.MPlayerItems.readlock(__FUNCTION__, __LINE__);
  1776. if (item_list.indexed_items.count(index) == 0) {
  1777. item_list.MPlayerItems.releasereadlock(__FUNCTION__, __LINE__);
  1778. return packets;
  1779. }
  1780. // If item is a 2handed weapon and something is in the secondary, unequip the secondary
  1781. if (item->IsWeapon() && item->weapon_info->wield_type == ITEM_WIELD_TYPE_TWO_HAND && equipList->GetItem(EQ2_SECONDARY_SLOT) != 0) {
  1782. item_list.MPlayerItems.releasereadlock(__FUNCTION__, __LINE__);
  1783. vector<EQ2Packet*> tmp_packets = UnequipItem(EQ2_SECONDARY_SLOT, -999, 0, version, appearance_type, false);
  1784. //packets.reserve(packets.size() + tmp_packets.size());
  1785. packets.insert(packets.end(), tmp_packets.begin(), tmp_packets.end());
  1786. }
  1787. else {
  1788. // release for delete item / scripting etc
  1789. item_list.MPlayerItems.releasereadlock(__FUNCTION__, __LINE__);
  1790. }
  1791. }
  1792. else if (canEquip && slot < 255) {
  1793. if(!IsAllowedCombatEquip(slot, true)) {
  1794. LogWrite(PLAYER__ERROR, 0, "Player", "Attempt to equip item %s (%u) with auto equip FAILED in combat!", item->name.c_str(), item->details.item_id);
  1795. item_list.MPlayerItems.releasereadlock(__FUNCTION__, __LINE__);
  1796. return packets;
  1797. }
  1798. // If item is a 2handed weapon and something is in the secondary, unequip the secondary
  1799. if (item->IsWeapon() && item->weapon_info->wield_type == ITEM_WIELD_TYPE_TWO_HAND && equipList->GetItem(EQ2_SECONDARY_SLOT) != 0) {
  1800. item_list.MPlayerItems.releasereadlock(__FUNCTION__, __LINE__);
  1801. vector<EQ2Packet*> tmp_packets = UnequipItem(EQ2_SECONDARY_SLOT, -999, 0, version, appearance_type, false);
  1802. //packets.reserve(packets.size() + tmp_packets.size());
  1803. packets.insert(packets.end(), tmp_packets.begin(), tmp_packets.end());
  1804. }
  1805. else {
  1806. // release for delete item / scripting etc
  1807. item_list.MPlayerItems.releasereadlock(__FUNCTION__, __LINE__);
  1808. }
  1809. database.DeleteItem(GetCharacterID(), item, "NOT-EQUIPPED");
  1810. if (item->GetItemScript() && lua_interface)
  1811. lua_interface->RunItemScript(item->GetItemScript(), "equipped", item, this);
  1812. if(!item->IsBag()) {
  1813. item_list.RemoveItem(item);
  1814. }
  1815. equipList->SetItem(slot, item);
  1816. item->save_needed = true;
  1817. packets.push_back(item->serialize(version, false));
  1818. SetEquipment(item);
  1819. const char* zone_script = world.GetZoneScript(GetZone()->GetZoneID());
  1820. if (zone_script && lua_interface)
  1821. lua_interface->RunZoneScript(zone_script, "item_equipped", GetZone(), this, item->details.item_id, item->name.c_str(), 0, item->details.unique_id);
  1822. int32 bag_id = item->details.inv_slot_id;
  1823. if (item->generic_info.condition == 0) {
  1824. Client* client = GetClient();
  1825. if (client) {
  1826. string popup_text = "Your ";
  1827. string popup_item = item->CreateItemLink(client->GetVersion(), true).c_str();
  1828. string popup_textcont = " is worn out and will not be effective until repaired.";
  1829. popup_text.append(popup_item);
  1830. popup_text.append(popup_textcont);
  1831. //devn00b: decided to use "crimson" for the color. (220,20,60 rgb)
  1832. client->SendPopupMessage(10, popup_text.c_str(), "", 5, 0xDC, 0x14, 0x3C);
  1833. client->Message(CHANNEL_COLOR_RED, "Your %s is worn out and will not be effective until repaired.", item->CreateItemLink(client->GetVersion(), true).c_str());
  1834. }
  1835. }
  1836. SetEquippedItemAppearances();
  1837. packets.push_back(equipList->serialize(version, this));
  1838. EQ2Packet* outapp = item_list.serialize(this, version);
  1839. if (outapp) {
  1840. packets.push_back(outapp);
  1841. EQ2Packet* bag_packet = SendBagUpdate(bag_id, version);
  1842. if (bag_packet)
  1843. packets.push_back(bag_packet);
  1844. }
  1845. SetCharSheetChanged(true);
  1846. }
  1847. else {
  1848. // clear items lock
  1849. item_list.MPlayerItems.releasereadlock(__FUNCTION__, __LINE__);
  1850. }
  1851. }
  1852. else {
  1853. // clear items lock
  1854. item_list.MPlayerItems.releasereadlock(__FUNCTION__, __LINE__);
  1855. }
  1856. if(slot < 255) {
  1857. if (slot == EQ2_FOOD_SLOT && item->IsFoodFood() && get_character_flag(CF_FOOD_AUTO_CONSUME)) {
  1858. Item* item = GetEquipmentList()->GetItem(EQ2_FOOD_SLOT);
  1859. if(item && GetClient() && GetClient()->CheckConsumptionAllowed(slot, false))
  1860. GetClient()->ConsumeFoodDrink(item, EQ2_FOOD_SLOT);
  1861. if(item)
  1862. SetActiveFoodUniqueID(item->details.unique_id);
  1863. }
  1864. else if (slot == EQ2_DRINK_SLOT && item->IsFoodDrink() && get_character_flag(CF_DRINK_AUTO_CONSUME)) {
  1865. Item* item = GetEquipmentList()->GetItem(EQ2_DRINK_SLOT);
  1866. if(item && GetClient() && GetClient()->CheckConsumptionAllowed(slot, false))
  1867. GetClient()->ConsumeFoodDrink(item, EQ2_DRINK_SLOT);
  1868. if(item)
  1869. SetActiveDrinkUniqueID(item->details.unique_id);
  1870. }
  1871. }
  1872. client->UpdateSentSpellList();
  1873. client->ClearSentSpellList();
  1874. return packets;
  1875. }
  1876. bool Player::AddItem(Item* item, AddItemType type) {
  1877. int32 conflictItemList = 0, conflictequipmentList = 0, conflictAppearanceEquipmentList = 0;
  1878. int16 lore_stack_count = 0;
  1879. if (item && item->details.item_id > 0) {
  1880. if( ((conflictItemList = item_list.CheckSlotConflict(item, true, true, &lore_stack_count)) == LORE ||
  1881. (conflictequipmentList = equipment_list.CheckSlotConflict(item, true, &lore_stack_count)) == LORE ||
  1882. (conflictAppearanceEquipmentList = appearance_equipment_list.CheckSlotConflict(item, true, &lore_stack_count)) == LORE) && !item->CheckFlag(STACK_LORE)) {
  1883. switch(type)
  1884. {
  1885. case AddItemType::BUY_FROM_BROKER:
  1886. client->Message(CHANNEL_COLOR_CHAT_RELATIONSHIP, "You already own this item and cannot have another.");
  1887. break;
  1888. default:
  1889. client->Message(CHANNEL_COLOR_CHAT_RELATIONSHIP, "You cannot obtain %s due to lore conflict.", item->name.c_str());
  1890. break;
  1891. }
  1892. safe_delete(item);
  1893. return false;
  1894. }
  1895. else if(conflictItemList == STACK_LORE || conflictequipmentList == STACK_LORE ||
  1896. conflictAppearanceEquipmentList == STACK_LORE) {
  1897. switch(type)
  1898. {
  1899. default:
  1900. client->Message(CHANNEL_COLOR_CHAT_RELATIONSHIP, "You already have one stack of the LORE item: %s.", item->name.c_str());
  1901. break;
  1902. }
  1903. safe_delete(item);
  1904. return false;
  1905. }
  1906. else if (item_list.AssignItemToFreeSlot(item)) {
  1907. item->save_needed = true;
  1908. CalculateApplyWeight();
  1909. return true;
  1910. }
  1911. else if (item_list.AddOverflowItem(item)) {
  1912. CalculateApplyWeight();
  1913. return true;
  1914. }
  1915. }
  1916. return false;
  1917. }
  1918. bool Player::AddItemToBank(Item* item) {
  1919. if (item && item->details.item_id > 0) {
  1920. sint32 bag = -3;
  1921. sint16 slot = -1;
  1922. if (item_list.GetFirstFreeBankSlot(&bag, &slot)) {
  1923. item->details.inv_slot_id = bag;
  1924. item->details.slot_id = slot;
  1925. item->save_needed = true;
  1926. return item_list.AddItem(item);
  1927. }
  1928. else if (item_list.AddOverflowItem(item))
  1929. return true;
  1930. }
  1931. return false;
  1932. }
  1933. EQ2Packet* Player::SendInventoryUpdate(int16 version) {
  1934. // assure any inventory updates are reflected in sell window
  1935. if(GetClient() && GetClient()->GetMerchantTransaction())
  1936. GetClient()->SendSellMerchantList();
  1937. return item_list.serialize(this, version);
  1938. }
  1939. void Player::UpdateInventory(int32 bag_id) {
  1940. EQ2Packet* outapp = client->GetPlayer()->SendInventoryUpdate(client->GetVersion());
  1941. client->QueuePacket(outapp);
  1942. outapp = client->GetPlayer()->SendBagUpdate(bag_id, client->GetVersion());
  1943. if (outapp)
  1944. client->QueuePacket(outapp);
  1945. }
  1946. EQ2Packet* Player::MoveInventoryItem(sint32 to_bag_id, int16 from_index, int8 new_slot, int8 charges, int8 appearance_type, bool* item_deleted, int16 version) {
  1947. Item* item = item_list.GetItemFromIndex(from_index);
  1948. int8 result = item_list.MoveItem(to_bag_id, from_index, new_slot, appearance_type, charges);
  1949. if (result == 1) {
  1950. if (item) {
  1951. if (!item->needs_deletion)
  1952. item->save_needed = true;
  1953. else if (item->needs_deletion) {
  1954. database.DeleteItem(GetCharacterID(), item, 0);
  1955. client->GetPlayer()->item_list.DestroyItem(from_index);
  1956. client->GetPlayer()->UpdateInventory(to_bag_id);
  1957. if(item_deleted)
  1958. *item_deleted = true;
  1959. }
  1960. }
  1961. return item_list.serialize(this, version);
  1962. }
  1963. else {
  1964. PacketStruct* packet = configReader.getStruct("WS_DisplayText", version);
  1965. if (packet) {
  1966. packet->setDataByName("color", CHANNEL_COLOR_YELLOW);
  1967. packet->setMediumStringByName("text", "Could not move item to that location.");
  1968. packet->setDataByName("unknown02", 0x00ff);
  1969. EQ2Packet* outapp = packet->serialize();
  1970. safe_delete(packet);
  1971. return outapp;
  1972. }
  1973. }
  1974. return 0;
  1975. }
  1976. int32 Player::GetCoinsCopper(){
  1977. return GetInfoStruct()->get_coin_copper();
  1978. }
  1979. int32 Player::GetCoinsSilver(){
  1980. return GetInfoStruct()->get_coin_silver();
  1981. }
  1982. int32 Player::GetCoinsGold(){
  1983. return GetInfoStruct()->get_coin_gold();
  1984. }
  1985. int32 Player::GetCoinsPlat(){
  1986. return GetInfoStruct()->get_coin_plat();
  1987. }
  1988. int32 Player::GetBankCoinsCopper(){
  1989. return GetInfoStruct()->get_bank_coin_copper();
  1990. }
  1991. int32 Player::GetBankCoinsSilver(){
  1992. return GetInfoStruct()->get_bank_coin_silver();
  1993. }
  1994. int32 Player::GetBankCoinsGold(){
  1995. return GetInfoStruct()->get_bank_coin_gold();
  1996. }
  1997. int32 Player::GetBankCoinsPlat(){
  1998. return GetInfoStruct()->get_bank_coin_plat();
  1999. }
  2000. int32 Player::GetStatusPoints(){
  2001. return GetInfoStruct()->get_status_points();
  2002. }
  2003. vector<QuickBarItem*>* Player::GetQuickbar(){
  2004. return &quickbar_items;
  2005. }
  2006. bool Player::UpdateQuickbarNeeded(){
  2007. return quickbar_updated;
  2008. }
  2009. void Player::ResetQuickbarNeeded(){
  2010. quickbar_updated = false;
  2011. }
  2012. void Player::AddQuickbarItem(int32 bar, int32 slot, int32 type, int16 icon, int16 icon_type, int32 id, int8 tier, int32 unique_id, const char* text, bool update){
  2013. RemoveQuickbarItem(bar, slot, false);
  2014. QuickBarItem* ability = new QuickBarItem;
  2015. ability->deleted = false;
  2016. ability->hotbar = bar;
  2017. ability->slot = slot;
  2018. ability->type = type;
  2019. ability->icon = icon;
  2020. ability->tier = tier;
  2021. ability->icon_type = icon_type;
  2022. ability->id = id;
  2023. if(unique_id == 0)
  2024. unique_id = database.NextUniqueHotbarID();
  2025. ability->unique_id = unique_id;
  2026. if(type == QUICKBAR_TEXT_CMD && text){
  2027. ability->text.data = string(text);
  2028. ability->text.size = ability->text.data.length();
  2029. }
  2030. else
  2031. ability->text.size = 0;
  2032. quickbar_items.push_back(ability);
  2033. if(update)
  2034. quickbar_updated = true;
  2035. }
  2036. void Player::RemoveQuickbarItem(int32 bar, int32 slot, bool update){
  2037. vector<QuickBarItem*>::iterator itr;
  2038. QuickBarItem* qbi = 0;
  2039. for(itr=quickbar_items.begin();itr!=quickbar_items.end();itr++){
  2040. qbi = *itr;
  2041. if(qbi && qbi->deleted == false && qbi->hotbar == bar && qbi->slot == slot){
  2042. qbi->deleted = true;
  2043. break;
  2044. }
  2045. }
  2046. if(update)
  2047. quickbar_updated = true;
  2048. }
  2049. void Player::ClearQuickbarItems(){
  2050. quickbar_items.clear();
  2051. }
  2052. EQ2Packet* Player::GetQuickbarPacket(int16 version){
  2053. PacketStruct* packet = configReader.getStruct("WS_QuickBarInit", version);
  2054. if(packet){
  2055. vector<QuickBarItem*>::iterator itr;
  2056. packet->setArrayLengthByName("num_abilities", quickbar_items.size());
  2057. int16 i=0;
  2058. for(itr=quickbar_items.begin();itr != quickbar_items.end(); itr++){
  2059. QuickBarItem* ability = *itr;
  2060. if(!ability || ability->deleted)
  2061. continue;
  2062. packet->setArrayDataByName("hotbar", ability->hotbar, i);
  2063. packet->setArrayDataByName("slot", ability->slot, i);
  2064. packet->setArrayDataByName("type", ability->type, i);
  2065. packet->setArrayDataByName("icon", ability->icon, i);
  2066. packet->setArrayDataByName("icon_type", ability->icon_type, i);
  2067. packet->setArrayDataByName("id", ability->id, i);
  2068. packet->setArrayDataByName("unique_id", ability->tier, i);
  2069. packet->setArrayDataByName("text", &ability->text, i);
  2070. i++;
  2071. }
  2072. EQ2Packet* app = packet->serialize();
  2073. safe_delete(packet);
  2074. return app;
  2075. }
  2076. return 0;
  2077. }
  2078. void Player::AddSpellBookEntry(int32 spell_id, int8 tier, sint32 slot, int32 type, int32 timer, bool save_needed){
  2079. SpellBookEntry* spell = new SpellBookEntry;
  2080. spell->status = 169;
  2081. spell->slot = slot;
  2082. spell->spell_id = spell_id;
  2083. spell->type = type;
  2084. spell->tier = tier;
  2085. spell->timer = timer;
  2086. spell->save_needed = save_needed;
  2087. spell->recast = 0;
  2088. spell->recast_available = 0;
  2089. spell->player = this;
  2090. spell->visible = true;
  2091. spell->in_use = false;
  2092. spell->in_remiss = false;
  2093. MSpellsBook.lock();
  2094. spells.push_back(spell);
  2095. MSpellsBook.unlock();
  2096. if (type == SPELL_BOOK_TYPE_NOT_SHOWN)
  2097. AddPassiveSpell(spell_id, tier);
  2098. }
  2099. void Player::DeleteSpellBook(int8 type_selection){
  2100. MSpellsBook.lock();
  2101. vector<SpellBookEntry*>::iterator itr;
  2102. SpellBookEntry* spell = 0;
  2103. for(itr = spells.begin(); itr != spells.end();){
  2104. spell = *itr;
  2105. if((type_selection & DELETE_TRADESKILLS) == 0 && spell->type == SPELL_BOOK_TYPE_TRADESKILL) {
  2106. itr++;
  2107. continue;
  2108. }
  2109. else if((type_selection & DELETE_SPELLS) == 0 && spell->type == SPELL_BOOK_TYPE_SPELL) {
  2110. itr++;
  2111. continue;
  2112. }
  2113. else if((type_selection & DELETE_COMBAT_ART) == 0 && spell->type == SPELL_BOOK_TYPE_COMBAT_ART) {
  2114. itr++;
  2115. continue;
  2116. }
  2117. else if((type_selection & DELETE_ABILITY) == 0 && spell->type == SPELL_BOOK_TYPE_ABILITY) {
  2118. itr++;
  2119. continue;
  2120. }
  2121. else if((type_selection & DELETE_NOT_SHOWN) == 0 && spell->type == SPELL_BOOK_TYPE_NOT_SHOWN) {
  2122. itr++;
  2123. continue;
  2124. }
  2125. database.DeleteCharacterSpell(GetCharacterID(), spell->spell_id);
  2126. if (spell->type == SPELL_BOOK_TYPE_NOT_SHOWN)
  2127. RemovePassive(spell->spell_id, spell->tier, true);
  2128. itr = spells.erase(itr);
  2129. }
  2130. MSpellsBook.unlock();
  2131. }
  2132. void Player::RemoveSpellBookEntry(int32 spell_id, bool remove_passives_from_list){
  2133. MSpellsBook.lock();
  2134. vector<SpellBookEntry*>::iterator itr;
  2135. SpellBookEntry* spell = 0;
  2136. for(itr = spells.begin(); itr != spells.end(); itr++){
  2137. spell = *itr;
  2138. if(spell->spell_id == spell_id){
  2139. if (spell->type == SPELL_BOOK_TYPE_NOT_SHOWN)
  2140. RemovePassive(spell->spell_id, spell->tier, remove_passives_from_list);
  2141. spells.erase(itr);
  2142. break;
  2143. }
  2144. }
  2145. MSpellsBook.unlock();
  2146. }
  2147. void Player::ResortSpellBook(int32 sort_by, int32 order, int32 pattern, int32 maxlvl_only, int32 book_type)
  2148. {
  2149. //sort_by : 0 - alpha, 1 - level, 2 - category
  2150. //order : 0 - ascending, 1 - descending
  2151. //pattern : 0 - zigzag, 1 - down, 2 - across
  2152. MSpellsBook.lock();
  2153. std::vector<SpellBookEntry*> sort_spells(spells);
  2154. if (!maxlvl_only)
  2155. {
  2156. switch (sort_by)
  2157. {
  2158. case 0:
  2159. if (!order)
  2160. stable_sort(sort_spells.begin(), sort_spells.end(), SortSpellEntryByName);
  2161. else
  2162. stable_sort(sort_spells.begin(), sort_spells.end(), SortSpellEntryByNameReverse);
  2163. break;
  2164. case 1:
  2165. if (!order)
  2166. stable_sort(sort_spells.begin(), sort_spells.end(), SortSpellEntryByLevel);
  2167. else
  2168. stable_sort(sort_spells.begin(), sort_spells.end(), SortSpellEntryByLevelReverse);
  2169. break;
  2170. case 2:
  2171. if (!order)
  2172. stable_sort(sort_spells.begin(), sort_spells.end(), SortSpellEntryByCategory);
  2173. else
  2174. stable_sort(sort_spells.begin(), sort_spells.end(), SortSpellEntryByCategoryReverse);
  2175. break;
  2176. }
  2177. }
  2178. vector<SpellBookEntry*>::iterator itr;
  2179. SpellBookEntry* spell = 0;
  2180. map<string, SpellBookEntry*> tmpSpells;
  2181. vector<SpellBookEntry*> resultSpells;
  2182. int32 i = 0;
  2183. int8 page_book_count = 0;
  2184. int32 last_start_point = 0;
  2185. for (itr = sort_spells.begin(); itr != sort_spells.end(); itr++) {
  2186. spell = *itr;
  2187. if (spell->type != book_type)
  2188. continue;
  2189. if (maxlvl_only)
  2190. {
  2191. Spell* actual_spell = 0;
  2192. actual_spell = master_spell_list.GetSpell(spell->spell_id, spell->tier);
  2193. if(!actual_spell) {
  2194. // we have a spell that doesn't exist here!
  2195. continue;
  2196. }
  2197. std::regex re("^(.*?)(\\s(I{1,}[VX]{0,}|V{1,}[IVX]{0,})|X{1,}[IVX]{0,})$");
  2198. std::string output = std::regex_replace(string(actual_spell->GetName()), re, "$1", std::regex_constants::format_no_copy);
  2199. if ( output.size() < 1 )
  2200. output = string(actual_spell->GetName());
  2201. map<string, SpellBookEntry*>::iterator tmpItr = tmpSpells.find(output);
  2202. if (tmpItr != tmpSpells.end())
  2203. {
  2204. Spell* tmpSpell = master_spell_list.GetSpell(tmpItr->second->spell_id, tmpItr->second->tier);
  2205. if (actual_spell->GetLevelRequired(this) > tmpSpell->GetLevelRequired(this))
  2206. {
  2207. tmpItr->second->visible = false;
  2208. tmpItr->second->slot = 0xFFFF;
  2209. std::vector<SpellBookEntry*>::iterator it;
  2210. it = find(resultSpells.begin(), resultSpells.end(), (SpellBookEntry*)tmpItr->second);
  2211. if (it != resultSpells.end())
  2212. resultSpells.erase(it);
  2213. tmpSpells.erase(tmpItr);
  2214. }
  2215. else
  2216. continue; // leave as-is we have the newer spell
  2217. }
  2218. spell->visible = true;
  2219. tmpSpells.insert(make_pair(output, spell));
  2220. resultSpells.push_back(spell);
  2221. }
  2222. spell->slot = i;
  2223. GetSpellBookSlotSort(pattern, &i, &page_book_count, &last_start_point);
  2224. } // end for loop for setting slots
  2225. if (maxlvl_only)
  2226. {
  2227. switch (sort_by)
  2228. {
  2229. case 0:
  2230. if (!order)
  2231. stable_sort(resultSpells.begin(), resultSpells.end(), SortSpellEntryByName);
  2232. else
  2233. stable_sort(resultSpells.begin(), resultSpells.end(), SortSpellEntryByNameReverse);
  2234. break;
  2235. case 1:
  2236. if (!order)
  2237. stable_sort(resultSpells.begin(), resultSpells.end(), SortSpellEntryByLevel);
  2238. else
  2239. stable_sort(resultSpells.begin(), resultSpells.end(), SortSpellEntryByLevelReverse);
  2240. break;
  2241. case 2:
  2242. if (!order)
  2243. stable_sort(resultSpells.begin(), resultSpells.end(), SortSpellEntryByCategory);
  2244. else
  2245. stable_sort(resultSpells.begin(), resultSpells.end(), SortSpellEntryByCategoryReverse);
  2246. break;
  2247. }
  2248. i = 0;
  2249. page_book_count = 0;
  2250. last_start_point = 0;
  2251. vector<SpellBookEntry*>::iterator tmpItr;
  2252. for (tmpItr = resultSpells.begin(); tmpItr != resultSpells.end(); tmpItr++) {
  2253. ((SpellBookEntry*)*tmpItr)->slot = i;
  2254. GetSpellBookSlotSort(pattern, &i, &page_book_count, &last_start_point);
  2255. }
  2256. }
  2257. MSpellsBook.unlock();
  2258. }
  2259. bool Player::SortSpellEntryByName(SpellBookEntry* s1, SpellBookEntry* s2)
  2260. {
  2261. Spell* spell1 = master_spell_list.GetSpell(s1->spell_id, s1->tier);
  2262. Spell* spell2 = master_spell_list.GetSpell(s2->spell_id, s2->tier);
  2263. if (!spell1 || !spell2)
  2264. return false;
  2265. return (string(spell1->GetName()) < string(spell2->GetName()));
  2266. }
  2267. bool Player::SortSpellEntryByCategory(SpellBookEntry* s1, SpellBookEntry* s2)
  2268. {
  2269. Spell* spell1 = master_spell_list.GetSpell(s1->spell_id, s1->tier);
  2270. Spell* spell2 = master_spell_list.GetSpell(s2->spell_id, s2->tier);
  2271. if (!spell1 || !spell2)
  2272. return false;
  2273. return (spell1->GetSpellIconBackdrop() < spell2->GetSpellIconBackdrop());
  2274. }
  2275. bool Player::SortSpellEntryByLevel(SpellBookEntry* s1, SpellBookEntry* s2)
  2276. {
  2277. Spell* spell1 = master_spell_list.GetSpell(s1->spell_id, s1->tier);
  2278. Spell* spell2 = master_spell_list.GetSpell(s2->spell_id, s2->tier);
  2279. if (!spell1 || !spell2)
  2280. return false;
  2281. int16 lvl1 = spell1->GetLevelRequired(s1->player);
  2282. int16 lvl2 = spell2->GetLevelRequired(s2->player);
  2283. if (lvl1 == 0xFFFF)
  2284. lvl1 = 0;
  2285. if (lvl2 == 0xFFFF)
  2286. lvl2 = 0;
  2287. return (lvl1 < lvl2);
  2288. }
  2289. bool Player::SortSpellEntryByNameReverse(SpellBookEntry* s1, SpellBookEntry* s2)
  2290. {
  2291. Spell* spell1 = master_spell_list.GetSpell(s1->spell_id, s1->tier);
  2292. Spell* spell2 = master_spell_list.GetSpell(s2->spell_id, s2->tier);
  2293. if (!spell1 || !spell2)
  2294. return false;
  2295. return (string(spell2->GetName()) < string(spell1->GetName()));
  2296. }
  2297. bool Player::SortSpellEntryByCategoryReverse(SpellBookEntry* s1, SpellBookEntry* s2)
  2298. {
  2299. Spell* spell1 = master_spell_list.GetSpell(s1->spell_id, s1->tier);
  2300. Spell* spell2 = master_spell_list.GetSpell(s2->spell_id, s2->tier);
  2301. if (!spell1 || !spell2)
  2302. return false;
  2303. return (spell2->GetSpellIconBackdrop() < spell1->GetSpellIconBackdrop());
  2304. }
  2305. bool Player::SortSpellEntryByLevelReverse(SpellBookEntry* s1, SpellBookEntry* s2)
  2306. {
  2307. Spell* spell1 = master_spell_list.GetSpell(s1->spell_id, s1->tier);
  2308. Spell* spell2 = master_spell_list.GetSpell(s2->spell_id, s2->tier);
  2309. if (!spell1 || !spell2)
  2310. return false;
  2311. int16 lvl1 = spell1->GetLevelRequired(s1->player);
  2312. int16 lvl2 = spell2->GetLevelRequired(s2->player);
  2313. if (lvl1 == 0xFFFF)
  2314. lvl1 = 0;
  2315. if (lvl2 == 0xFFFF)
  2316. lvl2 = 0;
  2317. return (lvl2 < lvl1);
  2318. }
  2319. int8 Player::GetSpellSlot(int32 spell_id){
  2320. MSpellsBook.lock();
  2321. vector<SpellBookEntry*>::iterator itr;
  2322. SpellBookEntry* spell = 0;
  2323. for(itr = spells.begin(); itr != spells.end(); itr++){
  2324. spell = *itr;
  2325. if(spell->spell_id == spell_id)
  2326. {
  2327. int8 slot = spell->slot;
  2328. MSpellsBook.unlock();
  2329. return slot;
  2330. }
  2331. }
  2332. MSpellsBook.unlock();
  2333. return 0;
  2334. }
  2335. void Player::AddSkill(int32 skill_id, int16 current_val, int16 max_val, bool save_needed){
  2336. Skill* master_skill = master_skill_list.GetSkill(skill_id);
  2337. if (master_skill) {
  2338. Skill* skill = new Skill(master_skill);
  2339. skill->current_val = current_val;
  2340. skill->previous_val = current_val;
  2341. skill->max_val = max_val;
  2342. if (save_needed)
  2343. skill->save_needed = true;
  2344. skill_list.AddSkill(skill);
  2345. }
  2346. }
  2347. void Player::RemovePlayerSkill(int32 skill_id, bool save) {
  2348. Skill* skill = skill_list.GetSkill(skill_id);
  2349. if (skill)
  2350. RemoveSkillFromDB(skill, save);
  2351. }
  2352. void Player::RemoveSkillFromDB(Skill* skill, bool save) {
  2353. skill_list.RemoveSkill(skill);
  2354. if (save)
  2355. database.DeleteCharacterSkill(GetCharacterID(), skill);
  2356. }
  2357. int16 Player::GetSpellSlotMappingCount(){
  2358. int16 ret = 0;
  2359. MSpellsBook.lock();
  2360. for(int32 i=0;i<spells.size();i++){
  2361. SpellBookEntry* spell = (SpellBookEntry*)spells[i];
  2362. if(spell->slot >= 0 && spell->spell_id > 0 && spell->type != SPELL_BOOK_TYPE_NOT_SHOWN)
  2363. ret++;
  2364. }
  2365. MSpellsBook.unlock();
  2366. return ret;
  2367. }
  2368. int8 Player::GetSpellTier(int32 id){
  2369. int8 ret = 0;
  2370. MSpellsBook.lock();
  2371. for(int32 i=0;i<spells.size();i++){
  2372. SpellBookEntry* spell = (SpellBookEntry*)spells[i];
  2373. if(spell->spell_id == id){
  2374. ret = spell->tier;
  2375. break;
  2376. }
  2377. }
  2378. MSpellsBook.unlock();
  2379. return ret;
  2380. }
  2381. int16 Player::GetSpellPacketCount(){
  2382. int16 ret = 0;
  2383. MSpellsBook.lock();
  2384. for(int32 i=0;i<spells.size();i++){
  2385. SpellBookEntry* spell = (SpellBookEntry*)spells[i];
  2386. if(spell->spell_id > 0 && spell->type != SPELL_BOOK_TYPE_NOT_SHOWN)
  2387. ret++;
  2388. }
  2389. MSpellsBook.unlock();
  2390. return ret;
  2391. }
  2392. void Player::LockAllSpells() {
  2393. vector<SpellBookEntry*>::iterator itr;
  2394. MSpellsBook.writelock(__FUNCTION__, __LINE__);
  2395. for (itr = spells.begin(); itr != spells.end(); itr++) {
  2396. if ((*itr)->type != SPELL_BOOK_TYPE_TRADESKILL)
  2397. RemoveSpellStatus((*itr), SPELL_STATUS_LOCK, false);
  2398. }
  2399. all_spells_locked = true;
  2400. MSpellsBook.releasewritelock(__FUNCTION__, __LINE__);
  2401. }
  2402. void Player::UnlockAllSpells(bool modify_recast, Spell* exception) {
  2403. vector<SpellBookEntry*>::iterator itr;
  2404. int32 exception_spell_id = 0;
  2405. if (exception)
  2406. exception_spell_id = exception->GetSpellID();
  2407. MSpellsBook.writelock(__FUNCTION__, __LINE__);
  2408. for (itr = spells.begin(); itr != spells.end(); itr++) {
  2409. MaintainedEffects* effect = 0;
  2410. if((effect = GetMaintainedSpell((*itr)->spell_id)) && effect->spell->spell->GetSpellData()->duration_until_cancel)
  2411. continue;
  2412. if ((*itr)->in_use == false &&
  2413. (((*itr)->spell_id != exception_spell_id ||
  2414. (*itr)->timer > 0 && (*itr)->timer != exception->GetSpellData()->linked_timer)
  2415. && (*itr)->type != SPELL_BOOK_TYPE_TRADESKILL)) {
  2416. AddSpellStatus((*itr), SPELL_STATUS_LOCK, modify_recast);
  2417. (*itr)->recast_available = 0;
  2418. }
  2419. else if((*itr)->in_remiss)
  2420. {
  2421. AddSpellStatus((*itr), SPELL_STATUS_LOCK);
  2422. (*itr)->recast_available = 0;
  2423. (*itr)->in_remiss = false;
  2424. }
  2425. }
  2426. all_spells_locked = false;
  2427. MSpellsBook.releasewritelock(__FUNCTION__, __LINE__);
  2428. }
  2429. void Player::LockSpell(Spell* spell, int16 recast) {
  2430. vector<SpellBookEntry*>::iterator itr;
  2431. SpellBookEntry* spell2;
  2432. MSpellsBook.writelock(__FUNCTION__, __LINE__);
  2433. for (itr = spells.begin(); itr != spells.end(); itr++) {
  2434. spell2 = *itr;
  2435. if (spell2->spell_id == spell->GetSpellID() || (spell->GetSpellData()->linked_timer > 0 && spell->GetSpellData()->linked_timer == spell2->timer))
  2436. {
  2437. spell2->in_use = true;
  2438. RemoveSpellStatus(spell2, SPELL_STATUS_LOCK, true, recast);
  2439. }
  2440. else if(spell2->in_use)
  2441. RemoveSpellStatus(spell2, SPELL_STATUS_LOCK, false, 0);
  2442. }
  2443. MSpellsBook.releasewritelock(__FUNCTION__, __LINE__);
  2444. }
  2445. void Player::UnlockSpell(Spell* spell) {
  2446. if (spell->GetStayLocked())
  2447. return;
  2448. vector<SpellBookEntry*>::iterator itr;
  2449. SpellBookEntry* spell2;
  2450. MSpellsBook.writelock(__FUNCTION__, __LINE__);
  2451. for (itr = spells.begin(); itr != spells.end(); itr++) {
  2452. spell2 = *itr;
  2453. if (spell2->spell_id == spell->GetSpellID() || (spell->GetSpellData()->linked_timer > 0 && spell->GetSpellData()->linked_timer == spell2->timer))
  2454. {
  2455. spell2->in_use = false;
  2456. spell2->recast_available = 0;
  2457. if(all_spells_locked)
  2458. spell2->in_remiss = true;
  2459. else
  2460. AddSpellStatus(spell2, SPELL_STATUS_LOCK, false);
  2461. }
  2462. }
  2463. MSpellsBook.releasewritelock(__FUNCTION__, __LINE__);
  2464. }
  2465. void Player::LockTSSpells() {
  2466. vector<SpellBookEntry*>::iterator itr;
  2467. MSpellsBook.writelock(__FUNCTION__, __LINE__);
  2468. for (itr = spells.begin(); itr != spells.end(); itr++) {
  2469. if ((*itr)->type == SPELL_BOOK_TYPE_TRADESKILL)
  2470. RemoveSpellStatus(*itr, SPELL_STATUS_LOCK);
  2471. }
  2472. MSpellsBook.releasewritelock(__FUNCTION__, __LINE__);
  2473. // Unlock all other types
  2474. UnlockAllSpells();
  2475. }
  2476. void Player::UnlockTSSpells() {
  2477. vector<SpellBookEntry*>::iterator itr;
  2478. MSpellsBook.writelock(__FUNCTION__, __LINE__);
  2479. for (itr = spells.begin(); itr != spells.end(); itr++) {
  2480. if ((*itr)->type == SPELL_BOOK_TYPE_TRADESKILL)
  2481. AddSpellStatus(*itr, SPELL_STATUS_LOCK);
  2482. }
  2483. MSpellsBook.releasewritelock(__FUNCTION__, __LINE__);
  2484. // Lock all other types
  2485. LockAllSpells();
  2486. }
  2487. void Player::QueueSpell(Spell* spell) {
  2488. vector<SpellBookEntry*>::iterator itr;
  2489. SpellBookEntry* spell2;
  2490. MSpellsBook.writelock(__FUNCTION__, __LINE__);
  2491. for (itr = spells.begin(); itr != spells.end(); itr++) {
  2492. spell2 = *itr;
  2493. if (spell2->spell_id == spell->GetSpellID())
  2494. AddSpellStatus(spell2, SPELL_STATUS_QUEUE, false);
  2495. }
  2496. MSpellsBook.releasewritelock(__FUNCTION__, __LINE__);
  2497. }
  2498. void Player::UnQueueSpell(Spell* spell) {
  2499. vector<SpellBookEntry*>::iterator itr;
  2500. SpellBookEntry* spell2;
  2501. MSpellsBook.writelock(__FUNCTION__, __LINE__);
  2502. for (itr = spells.begin(); itr != spells.end(); itr++) {
  2503. spell2 = *itr;
  2504. if (spell2->spell_id == spell->GetSpellID())
  2505. RemoveSpellStatus(spell2, SPELL_STATUS_QUEUE, false);
  2506. }
  2507. MSpellsBook.releasewritelock(__FUNCTION__, __LINE__);
  2508. }
  2509. vector<Spell*> Player::GetSpellBookSpellsByTimer(Spell* spell, int32 timerID) {
  2510. vector<Spell*> ret;
  2511. vector<SpellBookEntry*>::iterator itr;
  2512. MSpellsBook.readlock(__FUNCTION__, __LINE__);
  2513. for (itr = spells.begin(); itr != spells.end(); itr++) {
  2514. if ((*itr)->timer == timerID && spell->GetSpellID() != (*itr)->spell_id)
  2515. ret.push_back(master_spell_list.GetSpell((*itr)->spell_id, (*itr)->tier));
  2516. }
  2517. MSpellsBook.releasereadlock(__FUNCTION__, __LINE__);
  2518. return ret;
  2519. }
  2520. void Player::ModifySpellStatus(SpellBookEntry* spell, sint16 value, bool modify_recast, int16 recast) {
  2521. SetSpellEntryRecast(spell, modify_recast, recast);
  2522. if (modify_recast || spell->recast_available <= Timer::GetCurrentTime2() || value == 4) {
  2523. spell->status += value; // use set/remove spell status now
  2524. }
  2525. }
  2526. void Player::AddSpellStatus(SpellBookEntry* spell, sint16 value, bool modify_recast, int16 recast) {
  2527. SetSpellEntryRecast(spell, modify_recast, recast);
  2528. if (modify_recast || spell->recast_available <= Timer::GetCurrentTime2() || value == 4) {
  2529. spell->status = spell->status | value;
  2530. }
  2531. }
  2532. void Player::RemoveSpellStatus(SpellBookEntry* spell, sint16 value, bool modify_recast, int16 recast) {
  2533. SetSpellEntryRecast(spell, modify_recast, recast);
  2534. if (modify_recast || spell->recast_available <= Timer::GetCurrentTime2() || value == 4) {
  2535. spell->status = spell->status & ~value;
  2536. }
  2537. }
  2538. void Player::SetSpellStatus(Spell* spell, int8 status){
  2539. MSpellsBook.lock();
  2540. vector<SpellBookEntry*>::iterator itr;
  2541. SpellBookEntry* spell2 = 0;
  2542. for(itr = spells.begin(); itr != spells.end(); itr++){
  2543. spell2 = *itr;
  2544. if(spell2->spell_id == spell->GetSpellData()->id){
  2545. spell2->status = spell2->status | status;
  2546. break;
  2547. }
  2548. }
  2549. MSpellsBook.unlock();
  2550. }
  2551. void Player::SetSpellEntryRecast(SpellBookEntry* spell, bool modify_recast, int16 recast) {
  2552. if (modify_recast) {
  2553. spell->recast = recast / 100;
  2554. Spell* spell_ = master_spell_list.GetSpell(spell->spell_id, spell->tier);
  2555. if(spell_) {
  2556. float override_recast = 0.0f;
  2557. if(recast > 0) {
  2558. override_recast = static_cast<float>(recast);
  2559. }
  2560. int32 recast_time = spell_->CalculateRecastTimer(this, override_recast);
  2561. spell->recast = recast_time / 100;
  2562. spell->recast_available = Timer::GetCurrentTime2() + recast_time;
  2563. }
  2564. else {
  2565. spell->recast_available = Timer::GetCurrentTime2() + recast;
  2566. }
  2567. }
  2568. }
  2569. vector<SpellBookEntry*>* Player::GetSpellsSaveNeeded(){
  2570. vector<SpellBookEntry*>* ret = 0;
  2571. vector<SpellBookEntry*>::iterator itr;
  2572. MSpellsBook.lock();
  2573. SpellBookEntry* spell = 0;
  2574. for(itr = spells.begin(); itr != spells.end(); itr++){
  2575. spell = *itr;
  2576. if(spell->save_needed){
  2577. if(!ret)
  2578. ret = new vector<SpellBookEntry*>;
  2579. ret->push_back(spell);
  2580. }
  2581. }
  2582. MSpellsBook.unlock();
  2583. return ret;
  2584. }
  2585. int16 Player::GetTierUp(int16 tier)
  2586. {
  2587. switch(tier)
  2588. {
  2589. case 0:
  2590. break;
  2591. case 7:
  2592. case 9:
  2593. tier -= 2;
  2594. break;
  2595. default:
  2596. tier -= 1;
  2597. break;
  2598. }
  2599. return tier;
  2600. }
  2601. bool Player::HasSpell(int32 spell_id, int8 tier, bool include_higher_tiers, bool include_possible_scribe){
  2602. bool ret = false;
  2603. vector<SpellBookEntry*>::iterator itr;
  2604. MSpellsBook.lock();
  2605. SpellBookEntry* spell = 0;
  2606. for(itr = spells.begin(); itr != spells.end(); itr++){
  2607. spell = *itr;
  2608. if(spell->spell_id == spell_id && (tier == 255 || spell->tier == tier || (include_higher_tiers && spell->tier > tier) || (include_possible_scribe && tier <= spell->tier))){
  2609. ret = true;
  2610. break;
  2611. }
  2612. }
  2613. MSpellsBook.unlock();
  2614. return ret;
  2615. }
  2616. sint32 Player::GetFreeSpellBookSlot(int32 type){
  2617. sint32 ret = 0;
  2618. MSpellsBook.lock();
  2619. vector<SpellBookEntry*>::iterator itr;
  2620. SpellBookEntry* spell = 0;
  2621. for(itr = spells.begin(); itr != spells.end(); itr++){
  2622. spell = *itr;
  2623. if(spell->type == type && spell->slot > ret) //get last slot (add 1 to it on return)
  2624. ret = spell->slot;
  2625. }
  2626. MSpellsBook.unlock();
  2627. return ret+1;
  2628. }
  2629. SpellBookEntry* Player::GetSpellBookSpell(int32 spell_id){
  2630. MSpellsBook.lock();
  2631. vector<SpellBookEntry*>::iterator itr;
  2632. SpellBookEntry* ret = 0;
  2633. SpellBookEntry* spell = 0;
  2634. for(itr = spells.begin(); itr != spells.end(); itr++){
  2635. spell = *itr;
  2636. if(spell->spell_id == spell_id){
  2637. ret = spell;
  2638. break;
  2639. }
  2640. }
  2641. MSpellsBook.unlock();
  2642. return ret;
  2643. }
  2644. vector<int32> Player::GetSpellBookSpellIDBySkill(int32 skill_id) {
  2645. vector<int32> ret;
  2646. MSpellsBook.readlock(__FUNCTION__, __LINE__);
  2647. vector<SpellBookEntry*>::iterator itr;
  2648. Spell* spell = 0;
  2649. for(itr = spells.begin(); itr != spells.end(); itr++){
  2650. spell = master_spell_list.GetSpell((*itr)->spell_id, (*itr)->tier);
  2651. if(spell && spell->GetSpellData()->mastery_skill == skill_id)
  2652. ret.push_back(spell->GetSpellData()->id);
  2653. }
  2654. MSpellsBook.releasereadlock(__FUNCTION__, __LINE__);
  2655. return ret;
  2656. }
  2657. EQ2Packet* Player::GetSpellSlotMappingPacket(int16 version){
  2658. PacketStruct* packet = configReader.getStruct("WS_SpellSlotMapping", version);
  2659. if(packet){
  2660. int16 count = GetSpellSlotMappingCount();
  2661. int16 ptr = 0;
  2662. if(count > 0){
  2663. packet->setArrayLengthByName("spell_count", count);
  2664. MSpellsBook.lock();
  2665. for(int32 i=0;i<spells.size();i++){
  2666. SpellBookEntry* spell = (SpellBookEntry*)spells[i];
  2667. if(spell->type == SPELL_BOOK_TYPE_NOT_SHOWN || spell->slot < 0 || spell->spell_id == 0)
  2668. continue;
  2669. packet->setArrayDataByName("spell_id", spell->spell_id, ptr);
  2670. packet->setArrayDataByName("slot_id", (int16)spell->slot, ptr);
  2671. ptr++;
  2672. }
  2673. MSpellsBook.unlock();
  2674. EQ2Packet* ret = packet->serialize();
  2675. safe_delete(packet);
  2676. return ret;
  2677. }
  2678. safe_delete(packet);
  2679. }
  2680. return 0;
  2681. }
  2682. EQ2Packet* Player::GetSpellBookUpdatePacket(int16 version) {
  2683. std::unique_lock lock(spell_packet_update_mutex);
  2684. PacketStruct* packet = configReader.getStruct("WS_UpdateSpellBook", version);
  2685. EQ2Packet* ret = 0;
  2686. if (packet) {
  2687. Spell* spell = 0;
  2688. SpellBookEntry* spell_entry = 0;
  2689. int16 count = GetSpellPacketCount();
  2690. int16 ptr = 0;
  2691. // Get the packet size
  2692. PacketStruct* packet2 = configReader.getStruct("SubStruct_UpdateSpellBook", version);
  2693. int32 total_bytes = packet2->GetTotalPacketSize();
  2694. safe_delete(packet2);
  2695. packet->setArrayLengthByName("spell_count", count);
  2696. LogWrite(PLAYER__DEBUG, 5, "Player", "%s: GetSpellBookUpdatePacket Spell Count: %u, Spell Entry Book Size: %u", GetName(), count, total_bytes);
  2697. if (count > 0) {
  2698. if (count > spell_count) {
  2699. uchar* tmp = 0;
  2700. if (spell_orig_packet) {
  2701. tmp = new uchar[count * total_bytes];
  2702. memset(tmp, 0, total_bytes * count);
  2703. memcpy(tmp, spell_orig_packet, spell_count * total_bytes);
  2704. safe_delete_array(spell_orig_packet);
  2705. safe_delete_array(spell_xor_packet);
  2706. spell_orig_packet = tmp;
  2707. }
  2708. else {
  2709. spell_orig_packet = new uchar[count * total_bytes];
  2710. memset(spell_orig_packet, 0, total_bytes * count);
  2711. }
  2712. spell_xor_packet = new uchar[count * total_bytes];
  2713. memset(spell_xor_packet, 0, count * total_bytes);
  2714. }
  2715. spell_count = count;
  2716. MSpellsBook.lock();
  2717. for (int32 i = 0; i < spells.size(); i++) {
  2718. spell_entry = (SpellBookEntry*)spells[i];
  2719. if (spell_entry->spell_id == 0 || spell_entry->type == SPELL_BOOK_TYPE_NOT_SHOWN)
  2720. continue;
  2721. spell = master_spell_list.GetSpell(spell_entry->spell_id, spell_entry->tier);
  2722. if (spell) {
  2723. if (spell_entry->recast_available == 0 || Timer::GetCurrentTime2() > spell_entry->recast_available) {
  2724. packet->setSubstructArrayDataByName("spells", "available", 1, 0, ptr);
  2725. }
  2726. LogWrite(PLAYER__DEBUG, 9, "Player", "%s: GetSpellBookUpdatePacket Send Spell %u in position %u\n",GetName(), spell_entry->spell_id, ptr);
  2727. packet->setSubstructArrayDataByName("spells", "spell_id", spell_entry->spell_id, 0, ptr);
  2728. packet->setSubstructArrayDataByName("spells", "type", spell_entry->type, 0, ptr);
  2729. packet->setSubstructArrayDataByName("spells", "recast_available", spell_entry->recast_available, 0, ptr);
  2730. packet->setSubstructArrayDataByName("spells", "recast_time", spell_entry->recast, 0, ptr);
  2731. packet->setSubstructArrayDataByName("spells", "status", spell_entry->status, 0, ptr);
  2732. packet->setSubstructArrayDataByName("spells", "icon", (spell->TranslateClientSpellIcon(version) * -1) - 1, 0, ptr);
  2733. packet->setSubstructArrayDataByName("spells", "icon_type", spell->GetSpellIconBackdrop(), 0, ptr);
  2734. packet->setSubstructArrayDataByName("spells", "icon2", spell->GetSpellIconHeroicOp(), 0, ptr);
  2735. packet->setSubstructArrayDataByName("spells", "unique_id", (spell_entry->tier + 1) * -1, 0, ptr); //this is actually GetSpellNameCrc(spell->GetName()), but hijacking it for spell tier
  2736. packet->setSubstructArrayDataByName("spells", "charges", 255, 0, ptr);
  2737. // Beastlord and Channeler spell support
  2738. if (spell->GetSpellData()->savage_bar == 1)
  2739. packet->setSubstructArrayDataByName("spells", "unknown6", 32, 0, ptr); // advantages
  2740. else if (spell->GetSpellData()->savage_bar == 2)
  2741. packet->setSubstructArrayDataByName("spells", "unknown6", 64, 0, ptr); // primal
  2742. else if (spell->GetSpellData()->savage_bar == 3) {
  2743. packet->setSubstructArrayDataByName("spells", "unknown6", 6, 1, ptr); // 6 = channeler
  2744. // Slot req for channelers
  2745. // bitmask for slots 1 = slot 1, 2 = slot 2, 4 = slot 3, 8 = slot 4, 16 = slot 5, 32 = slot 6, 64 = slot 7, 128 = slot 8
  2746. packet->setSubstructArrayDataByName("spells", "savage_bar_slot", spell->GetSpellData()->savage_bar_slot, 0, ptr);
  2747. }
  2748. ptr++;
  2749. }
  2750. }
  2751. MSpellsBook.unlock();
  2752. }
  2753. ret = packet->serializeCountPacket(version, 0, spell_orig_packet, spell_xor_packet);
  2754. //packet->PrintPacket();
  2755. //DumpPacket(ret);
  2756. safe_delete(packet);
  2757. }
  2758. return ret;
  2759. }
  2760. EQ2Packet* Player::GetRaidUpdatePacket(int16 version) {
  2761. std::unique_lock lock(raid_update_mutex);
  2762. std::vector<int32> raidGroups;
  2763. PacketStruct* packet = configReader.getStruct("WS_RaidUpdate", version);
  2764. EQ2Packet* ret = 0;
  2765. Entity* member = 0;
  2766. int8 det_count = 0;
  2767. int8 total_groups = 0;
  2768. if (packet) {
  2769. int16 ptr = 0;
  2770. // Get the packet size
  2771. PacketStruct* packet2 = configReader.getStruct("Substruct_RaidMember", version);
  2772. int32 total_bytes = packet2->GetTotalPacketSize();
  2773. safe_delete(packet2);
  2774. world.GetGroupManager()->GroupLock(__FUNCTION__, __LINE__);
  2775. if (GetGroupMemberInfo()) {
  2776. PlayerGroup* group = world.GetGroupManager()->GetGroup(GetGroupMemberInfo()->group_id);
  2777. if (group)
  2778. {
  2779. group->GetRaidGroups(&raidGroups);
  2780. std::vector<int32>::iterator raid_itr;
  2781. int32 group_pos = 0;
  2782. for(raid_itr = raidGroups.begin(); raid_itr != raidGroups.end(); raid_itr++) {
  2783. group = world.GetGroupManager()->GetGroup((*raid_itr));
  2784. if(!group)
  2785. continue;
  2786. total_groups++;
  2787. group->MGroupMembers.readlock(__FUNCTION__, __LINE__);
  2788. deque<GroupMemberInfo*>* members = group->GetMembers();
  2789. deque<GroupMemberInfo*>::iterator itr;
  2790. GroupMemberInfo* info = 0;
  2791. int x = 1;
  2792. int lastpos = 1;
  2793. bool gotleader = false;
  2794. for (itr = members->begin(); itr != members->end(); itr++) {
  2795. info = *itr;
  2796. if(!info)
  2797. continue;
  2798. member = info->member;
  2799. std::string prop_name("group_member");
  2800. if(!gotleader && info->leader) {
  2801. lastpos = x;
  2802. x = 0;
  2803. gotleader = true;
  2804. }
  2805. else if(lastpos) {
  2806. x = lastpos;
  2807. lastpos = 0;
  2808. }
  2809. prop_name.append(std::to_string(x) + "_" + std::to_string(group_pos));
  2810. x++;
  2811. if (member && member->GetZone() == GetZone()) {
  2812. packet->setSubstructDataByName(prop_name.c_str(), "spawn_id", GetIDWithPlayerSpawn(member), 0);
  2813. if (member->HasPet()) {
  2814. if (member->GetPet())
  2815. packet->setSubstructDataByName(prop_name.c_str(), "pet_id", GetIDWithPlayerSpawn(member->GetPet()), 0);
  2816. else
  2817. packet->setSubstructDataByName(prop_name.c_str(), "pet_id", GetIDWithPlayerSpawn(member->GetCharmedPet()), 0);
  2818. }
  2819. else
  2820. packet->setSubstructDataByName(prop_name.c_str(), "pet_id", 0xFFFFFFFF, 0);
  2821. //Send detriment counts as 255 if all dets of that type are incurable
  2822. det_count = member->GetTraumaCount();
  2823. if (det_count > 0) {
  2824. if (!member->HasCurableDetrimentType(DET_TYPE_TRAUMA))
  2825. det_count = 255;
  2826. }
  2827. packet->setSubstructDataByName(prop_name.c_str(), "trauma_count", det_count, 0);
  2828. det_count = member->GetArcaneCount();
  2829. if (det_count > 0) {
  2830. if (!member->HasCurableDetrimentType(DET_TYPE_ARCANE))
  2831. det_count = 255;
  2832. }
  2833. packet->setSubstructDataByName(prop_name.c_str(), "arcane_count", det_count, 0);
  2834. det_count = member->GetNoxiousCount();
  2835. if (det_count > 0) {
  2836. if (!member->HasCurableDetrimentType(DET_TYPE_NOXIOUS))
  2837. det_count = 255;
  2838. }
  2839. packet->setSubstructDataByName(prop_name.c_str(), "noxious_count", det_count, 0);
  2840. det_count = member->GetElementalCount();
  2841. if (det_count > 0) {
  2842. if (!member->HasCurableDetrimentType(DET_TYPE_ELEMENTAL))
  2843. det_count = 255;
  2844. }
  2845. packet->setSubstructDataByName(prop_name.c_str(), "elemental_count", det_count, 0);
  2846. det_count = member->GetCurseCount();
  2847. if (det_count > 0) {
  2848. if (!member->HasCurableDetrimentType(DET_TYPE_CURSE))
  2849. det_count = 255;
  2850. }
  2851. packet->setSubstructDataByName(prop_name.c_str(), "curse_count", det_count, 0);
  2852. packet->setSubstructDataByName(prop_name.c_str(), "zone_status", 1, 0);
  2853. }
  2854. else {
  2855. packet->setSubstructDataByName(prop_name.c_str(), "pet_id", 0xFFFFFFFF, 0);
  2856. //packet->setSubstructDataByName(prop_name.c_str(), "unknown5", 1, 0, 1); // unknown5 > 1 = name is blue
  2857. packet->setSubstructDataByName(prop_name.c_str(), "zone_status", 2, 0);
  2858. }
  2859. packet->setSubstructDataByName(prop_name.c_str(), "name", info->name.c_str(), 0);
  2860. packet->setSubstructDataByName(prop_name.c_str(), "hp_current", info->hp_current, 0);
  2861. packet->setSubstructDataByName(prop_name.c_str(), "hp_max", info->hp_max, 0);
  2862. packet->setSubstructDataByName(prop_name.c_str(), "hp_current2", info->hp_current, 0);
  2863. packet->setSubstructDataByName(prop_name.c_str(), "power_current", info->power_current, 0);
  2864. packet->setSubstructDataByName(prop_name.c_str(), "power_max", info->power_max, 0);
  2865. packet->setSubstructDataByName(prop_name.c_str(), "level_current", info->level_current, 0);
  2866. packet->setSubstructDataByName(prop_name.c_str(), "level_max", info->level_max, 0);
  2867. packet->setSubstructDataByName(prop_name.c_str(), "zone", info->zone.c_str(), 0);
  2868. packet->setSubstructDataByName(prop_name.c_str(), "race_id", info->race_id, 0);
  2869. packet->setSubstructDataByName(prop_name.c_str(), "class_id", info->class_id, 0);
  2870. }
  2871. group->MGroupMembers.releasereadlock(__FUNCTION__, __LINE__);
  2872. group_pos += 1;
  2873. }
  2874. }
  2875. }
  2876. world.GetGroupManager()->ReleaseGroupLock(__FUNCTION__, __LINE__);
  2877. //packet->PrintPacket();
  2878. hassent_raid = true;
  2879. string* data = packet->serializeString();
  2880. int32 size = data->length();
  2881. uchar* tmp = new uchar[size];
  2882. if(!raid_xor_packet){
  2883. raid_orig_packet = new uchar[size];
  2884. raid_xor_packet = new uchar[size];
  2885. memcpy(raid_orig_packet, (uchar*)data->c_str(), size);
  2886. size = Pack(tmp, (uchar*)data->c_str(), size, size, version);
  2887. }
  2888. else{
  2889. memcpy(raid_xor_packet, (uchar*)data->c_str(), size);
  2890. Encode(raid_xor_packet, raid_orig_packet, size);
  2891. size = Pack(tmp, raid_xor_packet, size, size, version);
  2892. }
  2893. ret = new EQ2Packet(OP_UpdateRaidMsg, tmp, size);
  2894. safe_delete_array(tmp);
  2895. safe_delete(packet);
  2896. //DumpPacket(ret);
  2897. }
  2898. return ret;
  2899. }
  2900. PlayerInfo::~PlayerInfo(){
  2901. RemoveOldPackets();
  2902. }
  2903. PlayerInfo::PlayerInfo(Player* in_player){
  2904. orig_packet = 0;
  2905. changes = 0;
  2906. pet_orig_packet = 0;
  2907. pet_changes = 0;
  2908. player = in_player;
  2909. info_struct = player->GetInfoStruct();
  2910. info_struct->set_name(std::string(player->GetName()));
  2911. info_struct->set_deity(std::string("None"));
  2912. info_struct->set_class1(classes.GetBaseClass(player->GetAdventureClass()));
  2913. info_struct->set_class2(classes.GetSecondaryBaseClass(player->GetAdventureClass()));
  2914. info_struct->set_class3(player->GetAdventureClass());
  2915. info_struct->set_race(player->GetRace());
  2916. info_struct->set_gender(player->GetGender());
  2917. info_struct->set_level(player->GetLevel());
  2918. info_struct->set_tradeskill_level(player->GetTSLevel());
  2919. info_struct->set_tradeskill_class1(classes.GetTSBaseClass(player->GetTradeskillClass()));
  2920. info_struct->set_tradeskill_class2(classes.GetSecondaryTSBaseClass(player->GetTradeskillClass()));
  2921. info_struct->set_tradeskill_class3(player->GetTradeskillClass());
  2922. for(int i=0;i<45;i++){
  2923. if(i<30){
  2924. info_struct->maintained_effects[i].spell_id = 0xFFFFFFFF;
  2925. info_struct->maintained_effects[i].icon = 0xFFFF;
  2926. info_struct->maintained_effects[i].spell = nullptr;
  2927. }
  2928. info_struct->spell_effects[i].spell_id = 0xFFFFFFFF;
  2929. info_struct->spell_effects[i].spell = nullptr;
  2930. }
  2931. house_zone_id = 0;
  2932. bind_zone_id = 0;
  2933. bind_x = 0;
  2934. bind_y = 0;
  2935. bind_z = 0;
  2936. bind_heading = 0;
  2937. boat_x_offset = 0;
  2938. boat_y_offset = 0;
  2939. boat_z_offset = 0;
  2940. boat_spawn = 0;
  2941. }
  2942. MaintainedEffects* Player::GetFreeMaintainedSpellSlot(){
  2943. MaintainedEffects* ret = 0;
  2944. InfoStruct* info = GetInfoStruct();
  2945. GetMaintainedMutex()->readlock(__FUNCTION__, __LINE__);
  2946. for(int i=0;i<NUM_MAINTAINED_EFFECTS;i++){
  2947. if(info->maintained_effects[i].spell_id == 0xFFFFFFFF){
  2948. ret = &info->maintained_effects[i];
  2949. ret->spell_id = 0;
  2950. ret->slot_pos = i;
  2951. break;
  2952. }
  2953. }
  2954. GetMaintainedMutex()->releasereadlock(__FUNCTION__, __LINE__);
  2955. return ret;
  2956. }
  2957. MaintainedEffects* Player::GetMaintainedSpell(int32 id){
  2958. MaintainedEffects* ret = 0;
  2959. InfoStruct* info = GetInfoStruct();
  2960. GetMaintainedMutex()->readlock(__FUNCTION__, __LINE__);
  2961. for(int i=0;i<NUM_MAINTAINED_EFFECTS;i++){
  2962. if(info->maintained_effects[i].spell_id == id){
  2963. ret = &info->maintained_effects[i];
  2964. break;
  2965. }
  2966. }
  2967. GetMaintainedMutex()->releasereadlock(__FUNCTION__, __LINE__);
  2968. return ret;
  2969. }
  2970. MaintainedEffects* Player::GetMaintainedSpellBySlot(int8 slot){
  2971. MaintainedEffects* ret = 0;
  2972. InfoStruct* info = GetInfoStruct();
  2973. GetMaintainedMutex()->readlock(__FUNCTION__, __LINE__);
  2974. for(int i=0;i<NUM_MAINTAINED_EFFECTS;i++){
  2975. if(info->maintained_effects[i].slot_pos == slot){
  2976. ret = &info->maintained_effects[i];
  2977. break;
  2978. }
  2979. }
  2980. GetMaintainedMutex()->releasereadlock(__FUNCTION__, __LINE__);
  2981. return ret;
  2982. }
  2983. MaintainedEffects* Player::GetMaintainedSpells() {
  2984. return GetInfoStruct()->maintained_effects;
  2985. }
  2986. SpellEffects* Player::GetFreeSpellEffectSlot(){
  2987. SpellEffects* ret = 0;
  2988. InfoStruct* info = GetInfoStruct();
  2989. GetSpellEffectMutex()->readlock(__FUNCTION__, __LINE__);
  2990. for(int i=0;i<45;i++){
  2991. if(info->spell_effects[i].spell_id == 0xFFFFFFFF){
  2992. ret = &info->spell_effects[i];
  2993. ret->spell_id = 0;
  2994. break;
  2995. }
  2996. }
  2997. GetSpellEffectMutex()->releasereadlock(__FUNCTION__, __LINE__);
  2998. return ret;
  2999. }
  3000. SpellEffects* Player::GetSpellEffects() {
  3001. return GetInfoStruct()->spell_effects;
  3002. }
  3003. // call inside info_mutex
  3004. void Player::ClearRemovalTimers(){
  3005. map<int32, SpawnQueueState*>::iterator itr;
  3006. for(itr = spawn_state_list.begin(); itr != spawn_state_list.end();) {
  3007. SpawnQueueState* sr = itr->second;
  3008. itr = spawn_state_list.erase(itr);
  3009. safe_delete(sr);
  3010. }
  3011. }
  3012. void Player::ClearEverything(){
  3013. index_mutex.writelock(__FUNCTION__, __LINE__);
  3014. player_spawn_id_map.clear();
  3015. player_spawn_reverse_id_map.clear();
  3016. index_mutex.releasewritelock(__FUNCTION__, __LINE__);
  3017. map<int32, vector<int32>*>::iterator itr;
  3018. m_playerSpawnQuestsRequired.writelock(__FUNCTION__, __LINE__);
  3019. for (itr = player_spawn_quests_required.begin(); itr != player_spawn_quests_required.end(); itr++){
  3020. safe_delete(itr->second);
  3021. }
  3022. player_spawn_quests_required.clear();
  3023. m_playerSpawnQuestsRequired.releasewritelock(__FUNCTION__, __LINE__);
  3024. m_playerSpawnHistoryRequired.writelock(__FUNCTION__, __LINE__);
  3025. for (itr = player_spawn_history_required.begin(); itr != player_spawn_history_required.end(); itr++){
  3026. safe_delete(itr->second);
  3027. }
  3028. player_spawn_history_required.clear();
  3029. m_playerSpawnHistoryRequired.releasewritelock(__FUNCTION__, __LINE__);
  3030. spawn_mutex.writelock(__FUNCTION__, __LINE__);
  3031. ClearRemovalTimers();
  3032. spawn_packet_sent.clear();
  3033. spawn_mutex.releasewritelock(__FUNCTION__, __LINE__);
  3034. info_mutex.writelock(__FUNCTION__, __LINE__);
  3035. spawn_info_packet_list.clear();
  3036. info_mutex.releasewritelock(__FUNCTION__, __LINE__);
  3037. vis_mutex.writelock(__FUNCTION__, __LINE__);
  3038. spawn_vis_packet_list.clear();
  3039. vis_mutex.releasewritelock(__FUNCTION__, __LINE__);
  3040. pos_mutex.writelock(__FUNCTION__, __LINE__);
  3041. spawn_pos_packet_list.clear();
  3042. pos_mutex.releasewritelock(__FUNCTION__, __LINE__);
  3043. }
  3044. bool Player::IsResurrecting(){
  3045. return resurrecting;
  3046. }
  3047. void Player::SetResurrecting(bool val){
  3048. resurrecting = val;
  3049. }
  3050. void Player::AddMaintainedSpell(LuaSpell* luaspell){
  3051. if(!luaspell)
  3052. return;
  3053. Spell* spell = luaspell->spell;
  3054. MaintainedEffects* effect = GetFreeMaintainedSpellSlot();
  3055. int32 target_type = 0;
  3056. Spawn* spawn = 0;
  3057. if(effect && luaspell->caster && luaspell->caster->GetZone()){
  3058. GetMaintainedMutex()->writelock(__FUNCTION__, __LINE__);
  3059. strcpy(effect->name, spell->GetSpellData()->name.data.c_str());
  3060. effect->target = luaspell->initial_target;
  3061. spawn = luaspell->caster->GetZone()->GetSpawnByID(luaspell->initial_target);
  3062. if (spawn){
  3063. if (spawn == this)
  3064. target_type = 0;
  3065. else if (GetPet() == spawn || GetCharmedPet() == spawn)
  3066. target_type = 1;
  3067. else
  3068. target_type = 2;
  3069. }
  3070. effect->target_type = target_type;
  3071. effect->spell = luaspell;
  3072. if(!luaspell->slot_pos)
  3073. luaspell->slot_pos = effect->slot_pos;
  3074. effect->spell_id = spell->GetSpellData()->id;
  3075. LogWrite(PLAYER__DEBUG, 5, "Player", "AddMaintainedSpell Spell ID: %u, req concentration: %u", spell->GetSpellData()->id, spell->GetSpellData()->req_concentration);
  3076. effect->icon = spell->GetSpellData()->icon;
  3077. effect->icon_backdrop = spell->GetSpellData()->icon_backdrop;
  3078. effect->conc_used = spell->GetSpellData()->req_concentration;
  3079. effect->total_time = spell->GetSpellDuration()/10;
  3080. effect->tier = spell->GetSpellData()->tier;
  3081. if (spell->GetSpellData()->duration_until_cancel)
  3082. effect->expire_timestamp = 0xFFFFFFFF;
  3083. else
  3084. effect->expire_timestamp = Timer::GetCurrentTime2() + (spell->GetSpellDuration()*100);
  3085. GetMaintainedMutex()->releasewritelock(__FUNCTION__, __LINE__);
  3086. charsheet_changed = true;
  3087. }
  3088. }
  3089. void Player::AddSpellEffect(LuaSpell* luaspell, int32 override_expire_time){
  3090. if(!luaspell || !luaspell->caster)
  3091. return;
  3092. Spell* spell = luaspell->spell;
  3093. SpellEffects* old_effect = GetSpellEffect(spell->GetSpellID(), luaspell->caster);
  3094. SpellEffects* effect = 0;
  3095. if (old_effect){
  3096. GetZone()->RemoveTargetFromSpell(old_effect->spell, this);
  3097. RemoveSpellEffect(old_effect->spell);
  3098. }
  3099. LogWrite(SPELL__DEBUG, 0, "Spell", "%s AddSpellEffect %s (%u).", spell->GetName(), GetName(), GetID());
  3100. effect = GetFreeSpellEffectSlot();
  3101. if(effect){
  3102. GetSpellEffectMutex()->writelock(__FUNCTION__, __LINE__);
  3103. effect->spell = luaspell;
  3104. effect->spell_id = spell->GetSpellData()->id;
  3105. effect->caster = luaspell->caster;
  3106. effect->total_time = spell->GetSpellDuration()/10;
  3107. if (spell->GetSpellData()->duration_until_cancel)
  3108. effect->expire_timestamp = 0xFFFFFFFF;
  3109. else if(override_expire_time)
  3110. effect->expire_timestamp = Timer::GetCurrentTime2() + override_expire_time;
  3111. else
  3112. effect->expire_timestamp = Timer::GetCurrentTime2() + (spell->GetSpellDuration()*100);
  3113. effect->icon = spell->GetSpellData()->icon;
  3114. effect->icon_backdrop = spell->GetSpellData()->icon_backdrop;
  3115. effect->tier = spell->GetSpellTier();
  3116. GetSpellEffectMutex()->releasewritelock(__FUNCTION__, __LINE__);
  3117. charsheet_changed = true;
  3118. if(luaspell->caster && luaspell->caster->IsPlayer() && luaspell->caster != this)
  3119. {
  3120. if(GetClient()) {
  3121. GetClient()->TriggerSpellSave();
  3122. }
  3123. if(((Player*)luaspell->caster)->GetClient()) {
  3124. ((Player*)luaspell->caster)->GetClient()->TriggerSpellSave();
  3125. }
  3126. }
  3127. }
  3128. }
  3129. void Player::RemoveMaintainedSpell(LuaSpell* luaspell){
  3130. if(!luaspell)
  3131. return;
  3132. bool found = false;
  3133. Client* client = GetClient();
  3134. LuaSpell* old_spell = 0;
  3135. LuaSpell* current_spell = 0;
  3136. GetMaintainedMutex()->writelock(__FUNCTION__, __LINE__);
  3137. for(int i=0;i<30;i++){
  3138. // If we already found the spell then we are bumping all other up one so there are no gaps in the ui
  3139. // This check needs to be first so found can never be true on the first iteration (i = 0)
  3140. if (found) {
  3141. old_spell = GetInfoStruct()->maintained_effects[i - 1].spell;
  3142. current_spell = GetInfoStruct()->maintained_effects[i].spell;
  3143. //Update the maintained window uses_remaining and damage_remaining values
  3144. if (current_spell && current_spell->num_triggers > 0)
  3145. ClientPacketFunctions::SendMaintainedExamineUpdate(client, i - 1, current_spell->num_triggers, 0);
  3146. else if (current_spell && current_spell->damage_remaining > 0)
  3147. ClientPacketFunctions::SendMaintainedExamineUpdate(client, i - 1, current_spell->damage_remaining, 1);
  3148. else if (old_spell && old_spell->had_triggers)
  3149. ClientPacketFunctions::SendMaintainedExamineUpdate(client, i - 1, 0, 0);
  3150. else if (old_spell && old_spell->had_dmg_remaining)
  3151. ClientPacketFunctions::SendMaintainedExamineUpdate(client, i - 1, 0, 1);
  3152. GetInfoStruct()->maintained_effects[i].slot_pos = i - 1;
  3153. GetInfoStruct()->maintained_effects[i - 1] = GetInfoStruct()->maintained_effects[i];
  3154. if (current_spell)
  3155. current_spell->slot_pos = i - 1;
  3156. }
  3157. // Compare spells, if we found a match set the found flag
  3158. if(GetInfoStruct()->maintained_effects[i].spell == luaspell)
  3159. found = true;
  3160. }
  3161. // if we found the spell in the array then we need to flag the char sheet as changed and set the last element to empty
  3162. if (found) {
  3163. memset(&GetInfoStruct()->maintained_effects[29], 0, sizeof(MaintainedEffects));
  3164. GetInfoStruct()->maintained_effects[29].spell_id = 0xFFFFFFFF;
  3165. GetInfoStruct()->maintained_effects[29].icon = 0xFFFF;
  3166. GetInfoStruct()->maintained_effects[29].spell = nullptr;
  3167. charsheet_changed = true;
  3168. }
  3169. GetMaintainedMutex()->releasewritelock(__FUNCTION__, __LINE__);
  3170. }
  3171. void Player::RemoveSpellEffect(LuaSpell* spell){
  3172. bool found = false;
  3173. GetSpellEffectMutex()->writelock(__FUNCTION__, __LINE__);
  3174. for(int i=0;i<45;i++){
  3175. if (found) {
  3176. GetInfoStruct()->spell_effects[i-1] = GetInfoStruct()->spell_effects[i];
  3177. }
  3178. if(GetInfoStruct()->spell_effects[i].spell == spell)
  3179. found = true;
  3180. }
  3181. if (found) {
  3182. memset(&GetInfoStruct()->spell_effects[44], 0, sizeof(SpellEffects));
  3183. GetInfoStruct()->spell_effects[44].spell_id = 0xFFFFFFFF;
  3184. changed = true;
  3185. info_changed = true;
  3186. AddChangedZoneSpawn();
  3187. charsheet_changed = true;
  3188. }
  3189. GetSpellEffectMutex()->releasewritelock(__FUNCTION__, __LINE__);
  3190. }
  3191. void Player::PrepareIncomingMovementPacket(int32 len, uchar* data, int16 version)
  3192. {
  3193. if(GetClient() && GetClient()->IsReloadingZone())
  3194. return;
  3195. LogWrite(PLAYER__DEBUG, 7, "Player", "Enter: %s", __FUNCTION__); // trace
  3196. // XML structs may be to slow to use in this portion of the code as a single
  3197. // client sends a LOT of these packets when they are moving. I have commented
  3198. // out all the code for xml structs, to switch to it just uncomment
  3199. // the code and comment the 2 if/else if/else blocks, both have a comment
  3200. // above them to let you know wich ones they are.
  3201. //PacketStruct* update = configReader.getStruct("WS_PlayerPosUpdate", version);
  3202. int16 total_bytes; // = update->GetTotalPacketSize();
  3203. // Comment out this if/else if/else block if you switch to xml structs
  3204. if (version >= 1144)
  3205. total_bytes = sizeof(Player_Update1144);
  3206. else if (version >= 1096)
  3207. total_bytes = sizeof(Player_Update1096);
  3208. else if (version <= 373)
  3209. total_bytes = sizeof(Player_Update283);
  3210. else
  3211. total_bytes = sizeof(Player_Update);
  3212. if (!movement_packet)
  3213. movement_packet = new uchar[total_bytes];
  3214. else if (!old_movement_packet)
  3215. old_movement_packet = new uchar[total_bytes];
  3216. if (movement_packet && old_movement_packet)
  3217. memcpy(old_movement_packet, movement_packet, total_bytes);
  3218. bool reverse = version > 373;
  3219. Unpack(len, data, movement_packet, total_bytes, 0, reverse);
  3220. if (!movement_packet || !old_movement_packet)
  3221. return;
  3222. Decode(movement_packet, old_movement_packet, total_bytes);
  3223. //update->LoadPacketData(movement_packet, total_bytes);
  3224. int32 activity; // = update->getType_int32_ByName("activity");
  3225. int32 grid_id; // = update->getType_int32_ByName("grid_location");
  3226. float direction1; // = update->getType_float_ByName("direction1");
  3227. float direction2; // = update->getType_float_ByName("direction2");;
  3228. float speed; // = update->getType_float_ByName("speed");;
  3229. float side_speed;
  3230. float vert_speed;
  3231. float x; // = update->getType_float_ByName("x");;
  3232. float y; // = update->getType_float_ByName("y");;
  3233. float z; // = update->getType_float_ByName("z");;
  3234. float x_speed;
  3235. float y_speed;
  3236. float z_speed;
  3237. float client_pitch;
  3238. // comment out this if/else if/else block if you use xml structs
  3239. if (version >= 1144) {
  3240. Player_Update1144* update = (Player_Update1144*)movement_packet;
  3241. activity = update->activity;
  3242. grid_id = update->grid_location;
  3243. direction1 = update->direction1;
  3244. direction2 = update->direction2;
  3245. speed = update->speed;
  3246. side_speed = update->side_speed;
  3247. vert_speed = update->vert_speed;
  3248. x = update->x;
  3249. y = update->y;
  3250. z = update->z;
  3251. x_speed = update->speed_x;
  3252. y_speed = update->speed_y;
  3253. z_speed = update->speed_z;
  3254. client_pitch = update->pitch;
  3255. SetPitch(180 + update->pitch);
  3256. }
  3257. else if (version >= 1096) {
  3258. Player_Update1096* update = (Player_Update1096*)movement_packet;
  3259. activity = update->activity;
  3260. grid_id = update->grid_location;
  3261. direction1 = update->direction1;
  3262. direction2 = update->direction2;
  3263. speed = update->speed;
  3264. side_speed = update->side_speed;
  3265. vert_speed = update->vert_speed;
  3266. x = update->x;
  3267. y = update->y;
  3268. z = update->z;
  3269. x_speed = update->speed_x;
  3270. y_speed = update->speed_y;
  3271. z_speed = update->speed_z;
  3272. client_pitch = update->pitch;
  3273. SetPitch(180 + update->pitch);
  3274. }
  3275. else if (version <= 373) {
  3276. Player_Update283* update = (Player_Update283*)movement_packet;
  3277. activity = update->activity;
  3278. grid_id = update->grid_location;
  3279. direction1 = update->direction1;
  3280. direction2 = update->direction2;
  3281. speed = update->speed;
  3282. side_speed = update->side_speed;
  3283. vert_speed = update->vert_speed;
  3284. client_pitch = update->pitch;
  3285. x = update->x;
  3286. y = update->y;
  3287. z = update->z;
  3288. x_speed = update->speed_x;
  3289. y_speed = update->speed_y;
  3290. z_speed = update->speed_z;
  3291. appearance.pos.X2 = update->orig_x;
  3292. appearance.pos.Y2 = update->orig_y;
  3293. appearance.pos.Z2 = update->orig_z;
  3294. appearance.pos.X3 = update->orig_x2;
  3295. appearance.pos.Y3 = update->orig_y2;
  3296. appearance.pos.Z3 = update->orig_z2;
  3297. if (update->pitch != 0)
  3298. SetPitch(180 + update->pitch);
  3299. }
  3300. else {
  3301. Player_Update* update = (Player_Update*)movement_packet;
  3302. activity = update->activity;
  3303. grid_id = update->grid_location;
  3304. direction1 = update->direction1;
  3305. direction2 = update->direction2;
  3306. speed = update->speed;
  3307. side_speed = update->side_speed;
  3308. vert_speed = update->vert_speed;
  3309. x = update->x;
  3310. y = update->y;
  3311. z = update->z;
  3312. x_speed = update->speed_x;
  3313. y_speed = update->speed_y;
  3314. z_speed = update->speed_z;
  3315. appearance.pos.X2 = update->orig_x;
  3316. appearance.pos.Y2 = update->orig_y;
  3317. appearance.pos.Z2 = update->orig_z;
  3318. appearance.pos.X3 = update->orig_x2;
  3319. appearance.pos.Y3 = update->orig_y2;
  3320. appearance.pos.Z3 = update->orig_z2;
  3321. client_pitch = update->pitch;
  3322. SetPitch(180 + update->pitch);
  3323. }
  3324. SetHeading((sint16)(direction1 * 64), (sint16)(direction2 * 64));
  3325. if (activity != last_movement_activity) {
  3326. switch(activity) {
  3327. case UPDATE_ACTIVITY_RUNNING:
  3328. case UPDATE_ACTIVITY_RUNNING_AOM:
  3329. case UPDATE_ACTIVITY_IN_WATER_ABOVE:
  3330. case UPDATE_ACTIVITY_IN_WATER_BELOW:
  3331. case UPDATE_ACTIVITY_MOVE_WATER_ABOVE_AOM:
  3332. case UPDATE_ACTIVITY_MOVE_WATER_BELOW_AOM: {
  3333. if(GetZone() && GetZone()->GetDrowningVictim(this))
  3334. GetZone()->RemoveDrowningVictim(this);
  3335. break;
  3336. }
  3337. case UPDATE_ACTIVITY_DROWNING:
  3338. case UPDATE_ACTIVITY_DROWNING2:
  3339. case UPDATE_ACTIVITY_DROWNING_AOM:
  3340. case UPDATE_ACTIVITY_DROWNING2_AOM: {
  3341. if(GetZone() && !GetInvulnerable()) {
  3342. GetZone()->AddDrowningVictim(this);
  3343. }
  3344. break;
  3345. }
  3346. case UPDATE_ACTIVITY_JUMPING:
  3347. case UPDATE_ACTIVITY_JUMPING_AOM:
  3348. case UPDATE_ACTIVITY_FALLING:
  3349. case UPDATE_ACTIVITY_FALLING_AOM: {
  3350. if(IsCasting()) {
  3351. GetZone()->Interrupted(this, 0, SPELL_ERROR_INTERRUPTED, false, true);
  3352. }
  3353. if(GetInitialState() != 1024) {
  3354. SetInitialState(1024);
  3355. }
  3356. else if(GetInitialState() == 1024) {
  3357. if(activity == UPDATE_ACTIVITY_JUMPING_AOM) {
  3358. SetInitialState(UPDATE_ACTIVITY_JUMPING_AOM);
  3359. }
  3360. else {
  3361. SetInitialState(16512);
  3362. }
  3363. }
  3364. break;
  3365. }
  3366. }
  3367. last_movement_activity = activity;
  3368. }
  3369. //Player is riding a lift, update lift XYZ offsets and the lift's spawn pointer
  3370. if (activity & UPDATE_ACTIVITY_RIDING_BOAT) {
  3371. Spawn* boat = 0;
  3372. float boat_x = x;
  3373. float boat_y = y;
  3374. float boat_z = z;
  3375. if (GetBoatSpawn() == 0 && GetZone()) {
  3376. boat = GetZone()->GetClosestTransportSpawn(GetX(), GetY(), GetZ());
  3377. SetBoatSpawn(boat);
  3378. if(boat)
  3379. {
  3380. LogWrite(PLAYER__DEBUG, 0, "Player", "Set Player %s (%u) on Boat: %s",
  3381. GetName(), GetCharacterID(), boat ? boat->GetName() : "notset");
  3382. boat->AddRailPassenger(GetCharacterID());
  3383. GetZone()->CallSpawnScript(boat, SPAWN_SCRIPT_BOARD, this);
  3384. }
  3385. }
  3386. if (boat || (GetBoatSpawn() && GetZone())) {
  3387. if (!boat)
  3388. boat = GetZone()->GetSpawnByID(GetBoatSpawn());
  3389. if (boat && boat->IsWidget() && ((Widget*)boat)->GetMultiFloorLift()) {
  3390. boat_x -= boat->GetX();
  3391. boat_y -= boat->GetY();
  3392. boat_z -= boat->GetZ();
  3393. }
  3394. }
  3395. SetBoatX(boat_x);
  3396. SetBoatY(boat_y);
  3397. SetBoatZ(boat_z);
  3398. pos_packet_speed = speed;
  3399. grid_id = GetLocation();
  3400. }
  3401. else if (GetBoatSpawn() > 0 && !lift_cooldown.Enabled())
  3402. {
  3403. lift_cooldown.Start(100, true);
  3404. }
  3405. else if(lift_cooldown.Check())
  3406. {
  3407. if(GetBoatSpawn())
  3408. {
  3409. Spawn* boat = GetZone()->GetSpawnByID(GetBoatSpawn());
  3410. if(boat)
  3411. {
  3412. LogWrite(PLAYER__DEBUG, 0, "Player", "Remove Player %s (%u) from Boat: %s",
  3413. GetName(), GetCharacterID(), boat ? boat->GetName() : "notset");
  3414. boat->RemoveRailPassenger(GetCharacterID());
  3415. GetZone()->CallSpawnScript(boat, SPAWN_SCRIPT_DEBOARD, this);
  3416. }
  3417. }
  3418. SetBoatSpawn(0);
  3419. lift_cooldown.Disable();
  3420. }
  3421. if (!IsResurrecting() && !GetBoatSpawn())
  3422. {
  3423. if (!IsRooted() && !IsMezzedOrStunned()) {
  3424. SetX(x);
  3425. SetY(y, true, true);
  3426. SetZ(z);
  3427. SetSpeedX(x_speed);
  3428. SetSpeedY(y_speed);
  3429. SetSpeedZ(z_speed);
  3430. SetSideSpeed(side_speed);
  3431. SetVertSpeed(vert_speed);
  3432. SetClientHeading1(direction1);
  3433. SetClientHeading2(direction2);
  3434. SetClientPitch(client_pitch);
  3435. if(version > 373) {
  3436. pos_packet_speed = speed;
  3437. }
  3438. }
  3439. else {
  3440. SetSpeedX(0.0f);
  3441. SetSpeedY(0.0f);
  3442. SetSpeedZ(0.0f);
  3443. SetSideSpeed(0.0f);
  3444. SetVertSpeed(0.0f);
  3445. SetClientHeading1(direction1);
  3446. SetClientHeading2(direction2);
  3447. SetClientPitch(client_pitch);
  3448. pos_packet_speed = 0;
  3449. }
  3450. }
  3451. if (GetLocation() != grid_id)
  3452. {
  3453. LogWrite(PLAYER__DEBUG, 0, "Player", "%s left grid %u and entered grid %u", appearance.name, GetLocation(), grid_id);
  3454. const char* zone_script = world.GetZoneScript(GetZone()->GetZoneID());
  3455. if (zone_script && lua_interface) {
  3456. lua_interface->RunZoneScript(zone_script, "leave_location", GetZone(), this, GetLocation());
  3457. }
  3458. SetLocation(grid_id);
  3459. if (zone_script && lua_interface) {
  3460. lua_interface->RunZoneScript(zone_script, "enter_location", GetZone(), this, grid_id);
  3461. }
  3462. }
  3463. if (activity == UPDATE_ACTIVITY_IN_WATER_ABOVE || activity == UPDATE_ACTIVITY_IN_WATER_BELOW ||
  3464. activity == UPDATE_ACTIVITY_MOVE_WATER_BELOW_AOM || activity == UPDATE_ACTIVITY_MOVE_WATER_ABOVE_AOM) {
  3465. if (MakeRandomFloat(0, 100) < 25 && InWater())
  3466. GetSkillByName("Swimming", true);
  3467. }
  3468. // don't have to uncomment the print packet but you MUST uncomment the safe_delete() for xml structs
  3469. //update->PrintPacket();
  3470. //safe_delete(update);
  3471. LogWrite(PLAYER__DEBUG, 7, "Player", "Exit: %s", __FUNCTION__); // trace
  3472. }
  3473. int16 Player::GetLastMovementActivity(){
  3474. return last_movement_activity;
  3475. }
  3476. void Player::AddSpawnInfoPacketForXOR(int32 spawn_id, uchar* packet, int16 packet_size){
  3477. spawn_info_packet_list[spawn_id] = string((char*)packet, packet_size);
  3478. }
  3479. void Player::AddSpawnPosPacketForXOR(int32 spawn_id, uchar* packet, int16 packet_size){
  3480. spawn_pos_packet_list[spawn_id] = string((char*)packet, packet_size);
  3481. }
  3482. uchar* Player::GetSpawnPosPacketForXOR(int32 spawn_id){
  3483. uchar* ret = 0;
  3484. if(spawn_pos_packet_list.count(spawn_id) == 1)
  3485. ret = (uchar*)spawn_pos_packet_list[spawn_id].c_str();
  3486. return ret;
  3487. }
  3488. uchar* Player::GetSpawnInfoPacketForXOR(int32 spawn_id){
  3489. uchar* ret = 0;
  3490. if(spawn_info_packet_list.count(spawn_id) == 1)
  3491. ret = (uchar*)spawn_info_packet_list[spawn_id].c_str();
  3492. return ret;
  3493. }
  3494. void Player::AddSpawnVisPacketForXOR(int32 spawn_id, uchar* packet, int16 packet_size){
  3495. spawn_vis_packet_list[spawn_id] = string((char*)packet, packet_size);
  3496. }
  3497. uchar* Player::GetSpawnVisPacketForXOR(int32 spawn_id){
  3498. uchar* ret = 0;
  3499. if(spawn_vis_packet_list.count(spawn_id) == 1)
  3500. ret = (uchar*)spawn_vis_packet_list[spawn_id].c_str();
  3501. return ret;
  3502. }
  3503. uchar* Player::GetTempInfoPacketForXOR(){
  3504. return spawn_tmp_info_xor_packet;
  3505. }
  3506. uchar* Player::GetTempVisPacketForXOR(){
  3507. return spawn_tmp_vis_xor_packet;
  3508. }
  3509. uchar* Player::GetTempPosPacketForXOR(){
  3510. return spawn_tmp_pos_xor_packet;
  3511. }
  3512. uchar* Player::SetTempInfoPacketForXOR(int16 size){
  3513. spawn_tmp_info_xor_packet = new uchar[size];
  3514. info_xor_size = size;
  3515. return spawn_tmp_info_xor_packet;
  3516. }
  3517. uchar* Player::SetTempVisPacketForXOR(int16 size){
  3518. spawn_tmp_vis_xor_packet = new uchar[size];
  3519. vis_xor_size = size;
  3520. return spawn_tmp_vis_xor_packet;
  3521. }
  3522. uchar* Player::SetTempPosPacketForXOR(int16 size){
  3523. spawn_tmp_pos_xor_packet = new uchar[size];
  3524. pos_xor_size = size;
  3525. return spawn_tmp_pos_xor_packet;
  3526. }
  3527. bool Player::CheckPlayerInfo(){
  3528. return info != 0;
  3529. }
  3530. bool Player::SetSpawnSentState(Spawn* spawn, SpawnState state) {
  3531. bool val = true;
  3532. spawn_mutex.writelock(__FUNCTION__, __LINE__);
  3533. int16 index = GetIndexForSpawn(spawn);
  3534. if(index > 0 && (state == SpawnState::SPAWN_STATE_SENDING)) {
  3535. LogWrite(PLAYER__WARNING, 0, "Player", "Spawn ALREADY INDEXED for Player %s (%u). Spawn %s (index %u) attempted to state %u.",
  3536. GetName(), GetCharacterID(), spawn->GetName(), index, state);
  3537. if(GetClient() && GetClient()->IsReloadingZone()) {
  3538. spawn_packet_sent.insert(make_pair(spawn->GetID(), state));
  3539. val = false;
  3540. }
  3541. // we don't do anything this spawn is already populated by the player
  3542. }
  3543. else {
  3544. LogWrite(PLAYER__DEBUG, 0, "Player", "Spawn for Player %s (%u). Spawn %s (index %u) in state %u.",
  3545. GetName(), GetCharacterID(), spawn->GetName(), index, state);
  3546. map<int32,int8>::iterator itr = spawn_packet_sent.find(spawn->GetID());
  3547. if(itr != spawn_packet_sent.end())
  3548. itr->second = state;
  3549. else
  3550. spawn_packet_sent.insert(make_pair(spawn->GetID(), state));
  3551. if(state == SPAWN_STATE_SENT_WAIT) {
  3552. map<int32,SpawnQueueState*>::iterator state_itr;
  3553. if((state_itr = spawn_state_list.find(spawn->GetID())) != spawn_state_list.end()) {
  3554. safe_delete(state_itr->second);
  3555. spawn_state_list.erase(state_itr);
  3556. }
  3557. SpawnQueueState* removal = new SpawnQueueState;
  3558. removal->index_id = index;
  3559. removal->spawn_state_timer = Timer(500, true);
  3560. removal->spawn_state_timer.Start();
  3561. spawn_state_list.insert(make_pair(spawn->GetID(),removal));
  3562. }
  3563. else if(state == SpawnState::SPAWN_STATE_REMOVING &&
  3564. spawn_state_list.count(spawn->GetID()) == 0) {
  3565. SpawnQueueState* removal = new SpawnQueueState;
  3566. removal->index_id = index;
  3567. removal->spawn_state_timer = Timer(1000, true);
  3568. removal->spawn_state_timer.Start();
  3569. spawn_state_list.insert(make_pair(spawn->GetID(),removal));
  3570. }
  3571. }
  3572. spawn_mutex.releasewritelock(__FUNCTION__, __LINE__);
  3573. return val;
  3574. }
  3575. void Player::CheckSpawnStateQueue() {
  3576. if(!GetClient() || !GetClient()->IsReadyForUpdates())
  3577. return;
  3578. spawn_mutex.writelock(__FUNCTION__, __LINE__);
  3579. map<int32, SpawnQueueState*>::iterator itr;
  3580. for(itr = spawn_state_list.begin(); itr != spawn_state_list.end();) {
  3581. if(itr->second->spawn_state_timer.Check()) {
  3582. map<int32, int8>::iterator sent_itr = spawn_packet_sent.find(itr->first);
  3583. LogWrite(PLAYER__DEBUG, 0, "Player", "Spawn for Player %s (%u). Spawn index %u in state %u.",
  3584. GetName(), GetCharacterID(), itr->second->index_id, sent_itr->second);
  3585. switch(sent_itr->second) {
  3586. case SpawnState::SPAWN_STATE_SENT_WAIT: {
  3587. sent_itr->second = SpawnState::SPAWN_STATE_SENT;
  3588. SpawnQueueState* sr = itr->second;
  3589. itr = spawn_state_list.erase(itr);
  3590. safe_delete(sr);
  3591. break;
  3592. }
  3593. case SpawnState::SPAWN_STATE_REMOVING: {
  3594. if(itr->first == GetID() && GetClient()->IsReloadingZone()) {
  3595. itr->second->spawn_state_timer.Disable();
  3596. continue;
  3597. }
  3598. if(itr->second->index_id) {
  3599. PacketStruct* packet = packet = configReader.getStruct("WS_DestroyGhostCmd", GetClient()->GetVersion());
  3600. packet->setDataByName("spawn_index", itr->second->index_id);
  3601. packet->setDataByName("delete", 1);
  3602. GetClient()->QueuePacket(packet->serialize());
  3603. safe_delete(packet);
  3604. }
  3605. sent_itr->second = SpawnState::SPAWN_STATE_REMOVING_SLEEP;
  3606. itr++;
  3607. break;
  3608. }
  3609. case SpawnState::SPAWN_STATE_REMOVING_SLEEP: {
  3610. map<int32, int8>::iterator sent_itr = spawn_packet_sent.find(itr->first);
  3611. sent_itr->second = SpawnState::SPAWN_STATE_REMOVED;
  3612. SpawnQueueState* sr = itr->second;
  3613. itr = spawn_state_list.erase(itr);
  3614. safe_delete(sr);
  3615. break;
  3616. }
  3617. default: {
  3618. // reset
  3619. itr->second->spawn_state_timer.Disable();
  3620. break;
  3621. }
  3622. }
  3623. }
  3624. else
  3625. itr++;
  3626. }
  3627. spawn_mutex.releasewritelock(__FUNCTION__, __LINE__);
  3628. }
  3629. bool Player::WasSentSpawn(int32 spawn_id){
  3630. if(GetID() == spawn_id)
  3631. return true;
  3632. bool ret = false;
  3633. spawn_mutex.readlock(__FUNCTION__, __LINE__);
  3634. map<int32, int8>::iterator itr = spawn_packet_sent.find(spawn_id);
  3635. if(itr != spawn_packet_sent.end() && itr->second == SpawnState::SPAWN_STATE_SENT) {
  3636. ret = true;
  3637. }
  3638. spawn_mutex.releasereadlock(__FUNCTION__, __LINE__);
  3639. return ret;
  3640. }
  3641. bool Player::IsSendingSpawn(int32 spawn_id){
  3642. bool ret = false;
  3643. spawn_mutex.readlock(__FUNCTION__, __LINE__);
  3644. map<int32, int8>::iterator itr = spawn_packet_sent.find(spawn_id);
  3645. if(itr != spawn_packet_sent.end() && (itr->second == SpawnState::SPAWN_STATE_SENDING || itr->second == SPAWN_STATE_SENT_WAIT)) {
  3646. ret = true;
  3647. }
  3648. spawn_mutex.releasereadlock(__FUNCTION__, __LINE__);
  3649. return ret;
  3650. }
  3651. bool Player::IsRemovingSpawn(int32 spawn_id){
  3652. bool ret = false;
  3653. spawn_mutex.readlock(__FUNCTION__, __LINE__);
  3654. map<int32, int8>::iterator itr = spawn_packet_sent.find(spawn_id);
  3655. if(itr != spawn_packet_sent.end() &&
  3656. (itr->second == SpawnState::SPAWN_STATE_REMOVING || itr->second == SpawnState::SPAWN_STATE_REMOVING_SLEEP)) {
  3657. ret = true;
  3658. }
  3659. spawn_mutex.releasereadlock(__FUNCTION__, __LINE__);
  3660. return ret;
  3661. }
  3662. PlayerSkillList* Player::GetSkills(){
  3663. return &skill_list;
  3664. }
  3665. void Player::InCombat(bool val, bool range) {
  3666. if (val)
  3667. GetInfoStruct()->set_flags(GetInfoStruct()->get_flags() | (1 << (range?CF_RANGED_AUTO_ATTACK:CF_AUTO_ATTACK)));
  3668. else
  3669. GetInfoStruct()->set_flags(GetInfoStruct()->get_flags() & ~(1 << (range?CF_RANGED_AUTO_ATTACK:CF_AUTO_ATTACK)));
  3670. bool changeCombatState = false;
  3671. if((in_combat && !val) || (!in_combat && val))
  3672. changeCombatState = true;
  3673. in_combat = val;
  3674. if(in_combat)
  3675. AddIconValue(64);
  3676. else
  3677. RemoveIconValue(64);
  3678. bool update_regen = false;
  3679. if(GetInfoStruct()->get_engaged_encounter()) {
  3680. if(!IsAggroed() || !IsEngagedInEncounter()) {
  3681. GetInfoStruct()->set_engaged_encounter(0);
  3682. update_regen = true;
  3683. }
  3684. }
  3685. if(changeCombatState || update_regen)
  3686. SetRegenValues((GetInfoStruct()->get_effective_level() > 0) ? GetInfoStruct()->get_effective_level() : GetLevel());
  3687. charsheet_changed = true;
  3688. info_changed = true;
  3689. }
  3690. void Player::SetCharSheetChanged(bool val){
  3691. charsheet_changed = val;
  3692. }
  3693. bool Player::GetCharSheetChanged(){
  3694. return charsheet_changed;
  3695. }
  3696. void Player::SetRaidSheetChanged(bool val){
  3697. raidsheet_changed = val;
  3698. }
  3699. bool Player::GetRaidSheetChanged(){
  3700. return raidsheet_changed;
  3701. }
  3702. bool Player::AdventureXPEnabled(){
  3703. return (GetInfoStruct()->get_flags() & (1 << CF_COMBAT_EXPERIENCE_ENABLED));
  3704. }
  3705. bool Player::TradeskillXPEnabled() {
  3706. // TODO: need to identify the flag to togle tradeskill xp
  3707. return true;
  3708. }
  3709. void Player::set_character_flag(int flag){
  3710. LogWrite(PLAYER__DEBUG, 0, "Player", "Flag: %u", flag);
  3711. LogWrite(PLAYER__DEBUG, 0, "Player", "Flags before: %u, Flags2: %u", GetInfoStruct()->get_flags(), GetInfoStruct()->get_flags2());
  3712. if (flag > CF_MAXIMUM_FLAG) return;
  3713. if (flag < 32) GetInfoStruct()->set_flags(GetInfoStruct()->get_flags() | (1 << flag));
  3714. else GetInfoStruct()->set_flags2(GetInfoStruct()->get_flags2() | (1 << (flag - 32)));
  3715. charsheet_changed = true;
  3716. info_changed = true;
  3717. LogWrite(PLAYER__DEBUG, 0, "Player", "Flags after: %u, Flags2: %u", GetInfoStruct()->get_flags(), GetInfoStruct()->get_flags2());
  3718. }
  3719. void Player::reset_character_flag(int flag){
  3720. LogWrite(PLAYER__DEBUG, 0, "Player", "Flag: %u", flag);
  3721. LogWrite(PLAYER__DEBUG, 0, "Player", "Flags before: %u, Flags2: %u", GetInfoStruct()->get_flags(), GetInfoStruct()->get_flags2());
  3722. if (flag > CF_MAXIMUM_FLAG) return;
  3723. if (flag < 32)
  3724. {
  3725. int8 origflag = GetInfoStruct()->get_flags();
  3726. GetInfoStruct()->set_flags(origflag &= ~(1 << flag));
  3727. }
  3728. else
  3729. {
  3730. int8 flag2 = GetInfoStruct()->get_flags2();
  3731. GetInfoStruct()->set_flags2(flag2 &= ~(1 << (flag - 32)));
  3732. }
  3733. charsheet_changed = true;
  3734. info_changed = true;
  3735. LogWrite(PLAYER__DEBUG, 0, "Player", "Flags after: %u, Flags2: %u", GetInfoStruct()->get_flags(), GetInfoStruct()->get_flags2());
  3736. }
  3737. void Player::toggle_character_flag(int flag){
  3738. LogWrite(PLAYER__DEBUG, 0, "Player", "Flag: %u", flag);
  3739. LogWrite(PLAYER__DEBUG, 0, "Player", "Flags before: %u, Flags2: %u", GetInfoStruct()->get_flags(), GetInfoStruct()->get_flags2());
  3740. if (flag > CF_MAXIMUM_FLAG) return;
  3741. if (flag < 32)
  3742. {
  3743. int32 origflag = GetInfoStruct()->get_flags();
  3744. GetInfoStruct()->set_flags(origflag ^= (1 << flag));
  3745. }
  3746. else
  3747. {
  3748. int32 flag2 = GetInfoStruct()->get_flags2();
  3749. GetInfoStruct()->set_flags2(flag2 ^= (1 << (flag - 32)));
  3750. }
  3751. charsheet_changed = true;
  3752. info_changed = true;
  3753. LogWrite(PLAYER__DEBUG, 0, "Player", "Flags after: %u, Flags2: %u", GetInfoStruct()->get_flags(), GetInfoStruct()->get_flags2());
  3754. }
  3755. bool Player::get_character_flag(int flag){
  3756. bool ret = false;
  3757. if (flag > CF_MAXIMUM_FLAG){
  3758. LogWrite(PLAYER__DEBUG, 0, "Player", "Player::get_character_flag error: attempted to check flag %i", flag);
  3759. return ret;
  3760. }
  3761. if (flag < 32) ret = ((GetInfoStruct()->get_flags()) >> flag & 1);
  3762. else ret = ((GetInfoStruct()->get_flags2()) >> (flag - 32) & 1);
  3763. return ret;
  3764. }
  3765. float Player::GetXPVitality(){
  3766. return GetInfoStruct()->get_xp_vitality();
  3767. }
  3768. float Player::GetTSXPVitality() {
  3769. return GetInfoStruct()->get_tradeskill_xp_vitality();
  3770. }
  3771. bool Player::DoubleXPEnabled(){
  3772. return GetInfoStruct()->get_xp_vitality() > 0;
  3773. }
  3774. void Player::SetCharacterID(int32 new_id){
  3775. char_id = new_id;
  3776. }
  3777. int32 Player::GetCharacterID(){
  3778. return char_id;
  3779. }
  3780. float Player::CalculateXP(Spawn* victim){
  3781. if(AdventureXPEnabled() == false || !victim)
  3782. return 0;
  3783. float multiplier = 0;
  3784. float zone_xp_modifier = 1; // let's be safe!!
  3785. if( GetZone()->GetXPModifier() != 0 ) {
  3786. zone_xp_modifier = GetZone()->GetXPModifier();
  3787. LogWrite(PLAYER__DEBUG, 5, "XP", "Zone XP Modifier = %.2f", zone_xp_modifier);
  3788. }
  3789. switch(GetArrowColor(victim->GetLevel())){
  3790. case ARROW_COLOR_GRAY:
  3791. LogWrite(PLAYER__DEBUG, 5, "XP", "Gray Arrow = No XP");
  3792. return 0.0f;
  3793. break;
  3794. case ARROW_COLOR_GREEN:
  3795. multiplier = 3.25;
  3796. LogWrite(PLAYER__DEBUG, 5, "XP", "Green Arrow Multiplier = %.2f", multiplier);
  3797. break;
  3798. case ARROW_COLOR_BLUE:
  3799. multiplier = 3.5;
  3800. LogWrite(PLAYER__DEBUG, 5, "XP", "Blue Arrow Multiplier = %.2f", multiplier);
  3801. break;
  3802. case ARROW_COLOR_WHITE:
  3803. multiplier = 4;
  3804. LogWrite(PLAYER__DEBUG, 5, "XP", "White Arrow Multiplier = %.2f", multiplier);
  3805. break;
  3806. case ARROW_COLOR_YELLOW:
  3807. multiplier = 4.25;
  3808. LogWrite(PLAYER__DEBUG, 5, "XP", "Yellow Arrow Multiplier = %.2f", multiplier);
  3809. break;
  3810. case ARROW_COLOR_ORANGE:
  3811. multiplier = 4.5;
  3812. LogWrite(PLAYER__DEBUG, 5, "XP", "Orange Arrow Multiplier = %.2f", multiplier);
  3813. break;
  3814. case ARROW_COLOR_RED:
  3815. multiplier = 6;
  3816. LogWrite(PLAYER__DEBUG, 5, "XP", "Red Arrow Multiplier = %.2f", multiplier);
  3817. break;
  3818. }
  3819. float total = multiplier * 8;
  3820. LogWrite(PLAYER__DEBUG, 5, "XP", "Multiplier * 8 = %.2f", total);
  3821. if(victim->GetDifficulty() > 6) { // no need to multiply by 1 if this is a normal mob
  3822. total *= (victim->GetDifficulty() - 5);
  3823. LogWrite(PLAYER__DEBUG, 5, "XP", "Encounter > 6, total = %.2f", total);
  3824. }
  3825. else if(victim->GetDifficulty() <= 5) {
  3826. total /= (7 - victim->GetDifficulty()); //1 down mobs are worth half credit, 2 down worth .25, etc
  3827. LogWrite(PLAYER__DEBUG, 5, "XP", "Encounter <= 5, total = %.2f", total);
  3828. }
  3829. if(victim->GetHeroic() > 1) {
  3830. total *= victim->GetHeroic();
  3831. LogWrite(PLAYER__DEBUG, 5, "XP", "Heroic, total = %.2f", total);
  3832. }
  3833. if(DoubleXPEnabled()) {
  3834. LogWrite(PLAYER__DEBUG, 5, "XP", "Calculating Double XP!");
  3835. float percent = (((float)(total))/GetNeededXP()) *100;
  3836. LogWrite(PLAYER__DEBUG, 5, "XP", "Percent of total / XP Needed * 100, percent = %.2f", percent);
  3837. float xp_vitality = GetXPVitality();
  3838. if(xp_vitality >= percent) {
  3839. GetInfoStruct()->set_xp_vitality(xp_vitality - percent);
  3840. total *= 2;
  3841. LogWrite(PLAYER__DEBUG, 5, "XP", "Vitality >= Percent, total = %.2f", total);
  3842. }
  3843. else {
  3844. total += ((GetXPVitality() / percent) *2)*total;
  3845. GetInfoStruct()->set_xp_vitality(0);
  3846. LogWrite(PLAYER__DEBUG, 5, "XP", "Vitality < Percent, total = %.2f", total);
  3847. }
  3848. }
  3849. LogWrite(PLAYER__DEBUG, 5, "XP", "Final total = %.2f", (total * world.GetXPRate() * zone_xp_modifier));
  3850. return total * world.GetXPRate() * zone_xp_modifier;
  3851. }
  3852. float Player::CalculateTSXP(int8 level){
  3853. if(TradeskillXPEnabled() == false)
  3854. return 0;
  3855. float multiplier = 0;
  3856. float zone_xp_modifier = 1; // let's be safe!!
  3857. if( GetZone()->GetXPModifier() != 0 ) {
  3858. zone_xp_modifier = GetZone()->GetXPModifier();
  3859. LogWrite(PLAYER__DEBUG, 5, "XP", "Zone XP Modifier = %.2f", zone_xp_modifier);
  3860. }
  3861. sint16 diff = level - GetTSLevel();
  3862. if(GetTSLevel() < 10)
  3863. diff *= 3;
  3864. else if(GetTSLevel() <= 20)
  3865. diff *= 2;
  3866. if(diff >= 9)
  3867. multiplier = 6;
  3868. else if(diff >= 5)
  3869. multiplier = 4.5;
  3870. else if(diff >= 1)
  3871. multiplier = 4.25;
  3872. else if(diff == 0)
  3873. multiplier = 4;
  3874. else if(diff <= -11)
  3875. multiplier = 0;
  3876. else if(diff <= -6)
  3877. multiplier = 3.25;
  3878. else //if(diff < 0)
  3879. multiplier = 3.5;
  3880. float total = multiplier * 8;
  3881. LogWrite(PLAYER__DEBUG, 5, "XP", "Multiplier * 8 = %.2f", total);
  3882. if(DoubleXPEnabled()) {
  3883. LogWrite(PLAYER__DEBUG, 5, "XP", "Calculating Double XP!");
  3884. float percent = (((float)(total))/GetNeededTSXP()) *100;
  3885. LogWrite(PLAYER__DEBUG, 5, "XP", "Percent of total / XP Needed * 100, percent = %.2f", percent);
  3886. float ts_xp_vitality = GetTSXPVitality();
  3887. if(ts_xp_vitality >= percent) {
  3888. GetInfoStruct()->set_tradeskill_xp_vitality(ts_xp_vitality - percent);
  3889. total *= 2;
  3890. LogWrite(PLAYER__DEBUG, 5, "XP", "Vitality >= Percent, total = %.2f", total);
  3891. }
  3892. else {
  3893. total += ((GetTSXPVitality() / percent) *2)*total;
  3894. GetInfoStruct()->set_tradeskill_xp_vitality(0);
  3895. LogWrite(PLAYER__DEBUG, 5, "XP", "Vitality < Percent, total = %.2f", total);
  3896. }
  3897. }
  3898. LogWrite(PLAYER__DEBUG, 5, "XP", "Final total = %.2f", (total * world.GetXPRate() * zone_xp_modifier));
  3899. return total * world.GetXPRate() * zone_xp_modifier;
  3900. }
  3901. void Player::CalculateOfflineDebtRecovery(int32 unix_timestamp)
  3902. {
  3903. float xpDebt = GetXPDebt();
  3904. // not a real timestamp to work with
  3905. if(unix_timestamp < 1 || xpDebt == 0.0f)
  3906. return;
  3907. uint32 diff = (Timer::GetUnixTimeStamp() - unix_timestamp)/1000;
  3908. float recoveryDebtPercentage = rule_manager.GetGlobalRule(R_Combat, ExperienceDebtRecoveryPercent)->GetFloat()/100.0f;
  3909. int32 recoveryPeriodSeconds = rule_manager.GetGlobalRule(R_Combat, ExperienceDebtRecoveryPeriod)->GetInt32();
  3910. if(recoveryDebtPercentage == 0.0f || recoveryPeriodSeconds < 1)
  3911. return;
  3912. float periodsPassed = (float)diff/(float)recoveryPeriodSeconds;
  3913. // not enough time passed to calculate debt xp recovered
  3914. if(periodsPassed < 1.0f)
  3915. return;
  3916. float debtToSubtract = xpDebt * ((recoveryDebtPercentage*periodsPassed)/100.0f);
  3917. if(debtToSubtract >= xpDebt)
  3918. GetInfoStruct()->set_xp_debt(0.0f);
  3919. else
  3920. GetInfoStruct()->set_xp_debt(xpDebt - debtToSubtract);
  3921. }
  3922. void Player::SetNeededXP(int32 val){
  3923. GetInfoStruct()->set_xp_needed(val);
  3924. }
  3925. void Player::SetNeededXP(){
  3926. //GetInfoStruct()->xp_needed = GetLevel() * 100;
  3927. // Get xp needed to get to the next level
  3928. int16 level = GetLevel() + 1;
  3929. // If next level is beyond what we have in the map multiply the last value we have by how many levels we are over plus one
  3930. if (level > 95)
  3931. SetNeededXP(database.GetMysqlExpCurve(95)* ((level - 95) + 1));
  3932. else
  3933. SetNeededXP(database.GetMysqlExpCurve(level));
  3934. }
  3935. void Player::SetXP(int32 val){
  3936. GetInfoStruct()->set_xp(val);
  3937. }
  3938. void Player::SetNeededTSXP(int32 val) {
  3939. GetInfoStruct()->set_ts_xp_needed(val);
  3940. }
  3941. void Player::SetNeededTSXP() {
  3942. GetInfoStruct()->set_ts_xp_needed(GetTSLevel() * 100);
  3943. }
  3944. void Player::SetTSXP(int32 val) {
  3945. GetInfoStruct()->set_ts_xp(val);
  3946. }
  3947. float Player::GetXPDebt(){
  3948. return GetInfoStruct()->get_xp_debt();
  3949. }
  3950. int32 Player::GetNeededXP(){
  3951. return GetInfoStruct()->get_xp_needed();
  3952. }
  3953. int32 Player::GetXP(){
  3954. return GetInfoStruct()->get_xp();
  3955. }
  3956. int32 Player::GetNeededTSXP() {
  3957. return GetInfoStruct()->get_ts_xp_needed();
  3958. }
  3959. int32 Player::GetTSXP() {
  3960. return GetInfoStruct()->get_ts_xp();
  3961. }
  3962. bool Player::AddXP(int32 xp_amount){
  3963. if(!GetClient()) // potential linkdead player
  3964. return false;
  3965. MStats.lock();
  3966. xp_amount += (int32)(((float)xp_amount) * stats[ITEM_STAT_COMBATEXPMOD]) / 100;
  3967. MStats.unlock();
  3968. if(GetInfoStruct()->get_xp_debt())
  3969. {
  3970. float expRatioToDebt = rule_manager.GetGlobalRule(R_Combat, ExperienceToDebt)->GetFloat()/100.0f;
  3971. int32 amountToTakeFromDebt = (int32)((float)expRatioToDebt * (float)xp_amount);
  3972. int32 amountRequiredClearDebt = (GetInfoStruct()->get_xp_debt()/100.0f) * xp_amount;
  3973. if(amountToTakeFromDebt > amountRequiredClearDebt)
  3974. {
  3975. GetInfoStruct()->set_xp_debt(0.0f);
  3976. if(amountRequiredClearDebt > xp_amount)
  3977. xp_amount = 0;
  3978. else
  3979. xp_amount -= amountRequiredClearDebt;
  3980. }
  3981. else
  3982. {
  3983. float amountRemovedPct = ((float)amountToTakeFromDebt/(float)amountRequiredClearDebt);
  3984. GetInfoStruct()->set_xp_debt(GetInfoStruct()->get_xp_debt()-amountRemovedPct);
  3985. if(amountToTakeFromDebt > xp_amount)
  3986. xp_amount = 0;
  3987. else
  3988. xp_amount -= amountToTakeFromDebt;
  3989. }
  3990. }
  3991. // used up in xp debt
  3992. if(!xp_amount) {
  3993. SetCharSheetChanged(true);
  3994. return true;
  3995. }
  3996. int32 prev_level = GetLevel();
  3997. float current_xp_percent = ((float)GetXP()/(float)GetNeededXP())*100;
  3998. float miniding_min_percent = ((int)(current_xp_percent/10)+1)*10;
  3999. while((xp_amount + GetXP()) >= GetNeededXP()){
  4000. if (!CheckLevelStatus(GetLevel() + 1)) {
  4001. if(GetClient()) {
  4002. GetClient()->SimpleMessage(CHANNEL_COLOR_RED, "You do not have the required status to level up anymore!");
  4003. }
  4004. SetCharSheetChanged(true);
  4005. return false;
  4006. }
  4007. xp_amount -= GetNeededXP() - GetXP();
  4008. SetLevel(GetLevel() + 1);
  4009. }
  4010. SetXP(GetXP() + xp_amount);
  4011. GetPlayerInfo()->CalculateXPPercentages();
  4012. current_xp_percent = ((float)GetXP()/(float)GetNeededXP())*100;
  4013. if(current_xp_percent >= miniding_min_percent){
  4014. SetHP(GetTotalHP());
  4015. SetPower(GetTotalPower());
  4016. GetZone()->SendCastSpellPacket(332, this, this); //send mini level up spell effect
  4017. }
  4018. if(GetClient()) {
  4019. GetClient()->Message(CHANNEL_REWARD, "You gain %u experience!", (int32)xp_amount);
  4020. if (prev_level != GetLevel())
  4021. GetClient()->ChangeLevel(prev_level, GetLevel());
  4022. }
  4023. SetCharSheetChanged(true);
  4024. return true;
  4025. }
  4026. bool Player::AddTSXP(int32 xp_amount){
  4027. MStats.lock();
  4028. xp_amount += ((xp_amount)*stats[ITEM_STAT_TRADESKILLEXPMOD]) / 100;
  4029. MStats.unlock();
  4030. float current_xp_percent = ((float)GetTSXP()/(float)GetNeededTSXP())*100;
  4031. float miniding_min_percent = ((int)(current_xp_percent/10)+1)*10;
  4032. while((xp_amount + GetTSXP()) >= GetNeededTSXP()){
  4033. if (!CheckLevelStatus(GetTSLevel() + 1)) {
  4034. if(GetClient()) {
  4035. GetClient()->SimpleMessage(CHANNEL_COLOR_RED, "You do not have the required status to level up anymore!");
  4036. }
  4037. return false;
  4038. }
  4039. xp_amount -= GetNeededTSXP() - GetTSXP();
  4040. SetTSLevel(GetTSLevel() + 1);
  4041. SetTSXP(0);
  4042. SetNeededTSXP();
  4043. }
  4044. SetTSXP(GetTSXP() + xp_amount);
  4045. GetPlayerInfo()->CalculateXPPercentages();
  4046. current_xp_percent = ((float)GetTSXP()/(float)GetNeededTSXP())*100;
  4047. if(current_xp_percent >= miniding_min_percent){
  4048. SetHP(GetTotalHP());
  4049. SetPower(GetTotalPower());
  4050. }
  4051. if (GetTradeskillClass() == 0){
  4052. SetTradeskillClass(1);
  4053. GetInfoStruct()->set_tradeskill_class1(1);
  4054. GetInfoStruct()->set_tradeskill_class2(1);
  4055. GetInfoStruct()->set_tradeskill_class3(1);
  4056. }
  4057. return true;
  4058. }
  4059. void Player::CalculateLocation(){
  4060. if(GetSpeed() > 0 ){
  4061. if(GetHeading() >= 270 && GetHeading() <= 360){
  4062. SetX(GetX() + (GetSpeed()*.5)*((360-GetHeading())/90));
  4063. SetZ(GetZ() - (GetSpeed()*.5)*((GetHeading()-270)/90));
  4064. }
  4065. else if(GetHeading() >= 180 && GetHeading() < 270){
  4066. SetX(GetX() + (GetSpeed()*.5)*((GetHeading()-180)/90));
  4067. SetZ(GetZ() + (GetSpeed()*.5)*((270-GetHeading())/90));
  4068. }
  4069. else if(GetHeading() >= 90 && GetHeading() < 180){
  4070. SetX(GetX() - (GetSpeed()*.5)*((180-GetHeading())/90));
  4071. SetZ(GetZ() + (GetSpeed()*.5)*((GetHeading()-90)/90));
  4072. }
  4073. else if(GetHeading() >= 0 && GetHeading() < 90){
  4074. SetX(GetX() - (GetSpeed()*.5)*(GetHeading()/90));
  4075. SetZ(GetZ() - (GetSpeed()*.5)*((90-GetHeading())/90));
  4076. }
  4077. }
  4078. }
  4079. Spawn* Player::GetSpawnByIndex(int16 index){
  4080. Spawn* spawn = 0;
  4081. index_mutex.readlock(__FUNCTION__, __LINE__);
  4082. if(player_spawn_id_map.count(index) > 0)
  4083. spawn = player_spawn_id_map[index];
  4084. index_mutex.releasereadlock(__FUNCTION__, __LINE__);
  4085. return spawn;
  4086. }
  4087. int16 Player::GetIndexForSpawn(Spawn* spawn) {
  4088. int16 val = 0;
  4089. index_mutex.readlock(__FUNCTION__, __LINE__);
  4090. if(player_spawn_reverse_id_map.count(spawn) > 0)
  4091. val = player_spawn_reverse_id_map[spawn];
  4092. index_mutex.releasereadlock(__FUNCTION__, __LINE__);
  4093. return val;
  4094. }
  4095. bool Player::WasSpawnRemoved(Spawn* spawn){
  4096. bool wasRemoved = false;
  4097. if(IsRemovingSpawn(spawn->GetID()))
  4098. return false;
  4099. spawn_mutex.readlock(__FUNCTION__, __LINE__);
  4100. map<int32, int8>::iterator itr = spawn_packet_sent.find(spawn_id);
  4101. if(itr != spawn_packet_sent.end() && itr->second == SpawnState::SPAWN_STATE_REMOVED) {
  4102. wasRemoved = true;
  4103. }
  4104. spawn_mutex.releasereadlock(__FUNCTION__, __LINE__);
  4105. return wasRemoved;
  4106. }
  4107. void Player::RemoveSpawn(Spawn* spawn, bool delete_spawn)
  4108. {
  4109. LogWrite(PLAYER__DEBUG, 3, "Player", "Remove Spawn '%s' (%u)", spawn->GetName(), spawn->GetID());
  4110. SetSpawnSentState(spawn, delete_spawn ? SpawnState::SPAWN_STATE_REMOVING : SpawnState::SPAWN_STATE_REMOVING_SLEEP);
  4111. info_mutex.writelock(__FUNCTION__, __LINE__);
  4112. vis_mutex.writelock(__FUNCTION__, __LINE__);
  4113. pos_mutex.writelock(__FUNCTION__, __LINE__);
  4114. index_mutex.writelock(__FUNCTION__, __LINE__);
  4115. if (player_spawn_reverse_id_map[spawn] && player_spawn_id_map.count(player_spawn_reverse_id_map[spawn]) > 0)
  4116. player_spawn_id_map.erase(player_spawn_reverse_id_map[spawn]);
  4117. if (player_spawn_reverse_id_map.count(spawn) > 0)
  4118. player_spawn_reverse_id_map.erase(spawn);
  4119. if (player_spawn_id_map.count(spawn->GetID()) && player_spawn_id_map[spawn->GetID()] == spawn)
  4120. player_spawn_id_map.erase(spawn->GetID());
  4121. int32 id = spawn->GetID();
  4122. if (spawn_info_packet_list.count(id))
  4123. spawn_info_packet_list.erase(id);
  4124. if (spawn_pos_packet_list.count(id))
  4125. spawn_pos_packet_list.erase(id);
  4126. if (spawn_vis_packet_list.count(id))
  4127. spawn_vis_packet_list.erase(id);
  4128. index_mutex.releasewritelock(__FUNCTION__, __LINE__);
  4129. info_mutex.releasewritelock(__FUNCTION__, __LINE__);
  4130. pos_mutex.releasewritelock(__FUNCTION__, __LINE__);
  4131. vis_mutex.releasewritelock(__FUNCTION__, __LINE__);
  4132. }
  4133. vector<int32> Player::GetQuestIDs(){
  4134. vector<int32> ret;
  4135. map<int32, Quest*>::iterator itr;
  4136. MPlayerQuests.readlock(__FUNCTION__, __LINE__);
  4137. for(itr = player_quests.begin(); itr != player_quests.end(); itr++){
  4138. if(itr->second)
  4139. ret.push_back(itr->second->GetQuestID());
  4140. }
  4141. MPlayerQuests.releasereadlock(__FUNCTION__, __LINE__);
  4142. return ret;
  4143. }
  4144. vector<Quest*>* Player::CheckQuestsItemUpdate(Item* item){
  4145. vector<Quest*>* quest_updates = 0;
  4146. map<int32, Quest*>::iterator itr;
  4147. MPlayerQuests.readlock(__FUNCTION__, __LINE__);
  4148. for(itr = player_quests.begin(); itr != player_quests.end(); itr++){
  4149. if(itr->second && itr->second->CheckQuestItemUpdate(item->details.item_id, item->details.count)){
  4150. if(!quest_updates)
  4151. quest_updates = new vector<Quest*>();
  4152. quest_updates->push_back(itr->second);
  4153. }
  4154. }
  4155. MPlayerQuests.releasereadlock(__FUNCTION__, __LINE__);
  4156. return quest_updates;
  4157. }
  4158. void Player::CheckQuestsCraftUpdate(Item* item, int32 qty){
  4159. map<int32, Quest*>::iterator itr;
  4160. vector<Quest*>* update_list = new vector<Quest*>;
  4161. MPlayerQuests.readlock(__FUNCTION__, __LINE__);
  4162. for(itr = player_quests.begin(); itr != player_quests.end(); itr++){
  4163. if(itr->second){
  4164. if(item && qty > 0){
  4165. if(itr->second->CheckQuestRefIDUpdate(item->details.item_id, qty)){
  4166. update_list->push_back(itr->second);
  4167. }
  4168. }
  4169. }
  4170. }
  4171. MPlayerQuests.releasereadlock(__FUNCTION__, __LINE__);
  4172. if(update_list && update_list->size() > 0){
  4173. Client* client = GetClient();
  4174. if(client){
  4175. for(int8 i=0;i<update_list->size(); i++){
  4176. client->SendQuestUpdate(update_list->at(i));
  4177. client->SendQuestFailure(update_list->at(i));
  4178. }
  4179. }
  4180. }
  4181. update_list->clear();
  4182. safe_delete(update_list);
  4183. }
  4184. void Player::CheckQuestsHarvestUpdate(Item* item, int32 qty){
  4185. map<int32, Quest*>::iterator itr;
  4186. vector<Quest*>* update_list = new vector<Quest*>;
  4187. MPlayerQuests.readlock(__FUNCTION__, __LINE__);
  4188. for(itr = player_quests.begin(); itr != player_quests.end(); itr++){
  4189. if(itr->second){
  4190. if(item && qty > 0){
  4191. if(itr->second->CheckQuestRefIDUpdate(item->details.item_id, qty)){
  4192. update_list->push_back(itr->second);
  4193. }
  4194. }
  4195. }
  4196. }
  4197. MPlayerQuests.releasereadlock(__FUNCTION__, __LINE__);
  4198. if(update_list && update_list->size() > 0){
  4199. Client* client = GetClient();
  4200. if(client){
  4201. for(int8 i=0;i<update_list->size(); i++){
  4202. client->SendQuestUpdate(update_list->at(i));
  4203. client->SendQuestFailure(update_list->at(i));
  4204. }
  4205. }
  4206. }
  4207. update_list->clear();
  4208. safe_delete(update_list);
  4209. }
  4210. vector<Quest*>* Player::CheckQuestsSpellUpdate(Spell* spell) {
  4211. vector<Quest*>* quest_updates = 0;
  4212. map<int32, Quest*>::iterator itr;
  4213. MPlayerQuests.readlock(__FUNCTION__, __LINE__);
  4214. for (itr = player_quests.begin(); itr != player_quests.end(); itr++){
  4215. if (itr->second && itr->second->CheckQuestSpellUpdate(spell)) {
  4216. if (!quest_updates)
  4217. quest_updates = new vector<Quest*>();
  4218. quest_updates->push_back(itr->second);
  4219. }
  4220. }
  4221. MPlayerQuests.releasereadlock(__FUNCTION__, __LINE__);
  4222. return quest_updates;
  4223. }
  4224. PacketStruct* Player::GetQuestJournalPacket(bool all_quests, int16 version, int32 crc, int32 current_quest_id, bool updated){
  4225. PacketStruct* packet = configReader.getStruct("WS_QuestJournalUpdate", version);
  4226. Quest* quest = 0;
  4227. if(packet){
  4228. int16 total_quests_num = 0;
  4229. int16 total_completed_quests = 0;
  4230. MPlayerQuests.readlock(__FUNCTION__, __LINE__);
  4231. map<int32, Quest*> total_quests = player_quests;
  4232. if(all_quests && completed_quests.size() > 0)
  4233. total_quests.insert(completed_quests.begin(), completed_quests.end());
  4234. if(total_quests.size() > 0){
  4235. map<string, int16> quest_types;
  4236. map<int32, Quest*>::iterator itr;
  4237. int16 zone_id = 0;
  4238. for(itr = total_quests.begin(); itr != total_quests.end(); itr++){
  4239. if(itr->first && itr->second){
  4240. if(current_quest_id == 0 && itr->second->GetTurnedIn() == false)
  4241. current_quest_id = itr->first;
  4242. if(itr->second->GetTurnedIn())
  4243. total_completed_quests++;
  4244. if(itr->second->GetType()){
  4245. if(quest_types.count(itr->second->GetType()) == 0){
  4246. quest_types[itr->second->GetType()] = zone_id;
  4247. zone_id++;
  4248. }
  4249. }
  4250. if(itr->second->GetZone()){
  4251. if(quest_types.count(itr->second->GetZone()) == 0){
  4252. quest_types[itr->second->GetZone()] = zone_id; // Fix #490 - incorrect ordering of quests in journal
  4253. zone_id++;
  4254. }
  4255. }
  4256. total_quests_num++;
  4257. }
  4258. else
  4259. continue;
  4260. }
  4261. packet->setArrayLengthByName("num_quests", total_quests_num);
  4262. int16 i = 0;
  4263. for(itr = total_quests.begin(); itr != total_quests.end(); itr++){
  4264. if(i == 0 && quest_types.size() > 0){
  4265. packet->setArrayLengthByName("num_quest_zones", quest_types.size());
  4266. map<string, int16>::iterator type_itr;
  4267. int16 x = 0;
  4268. for(type_itr = quest_types.begin(); type_itr != quest_types.end(); type_itr++){
  4269. packet->setArrayDataByName("quest_zones_zone", type_itr->first.c_str(), x);
  4270. packet->setArrayDataByName("quest_zones_zone_id", type_itr->second, x);
  4271. x++;
  4272. }
  4273. }
  4274. if(itr->first == 0 || !itr->second)
  4275. continue;
  4276. if(!all_quests && !itr->second->GetUpdateRequired())
  4277. continue;
  4278. quest = itr->second;
  4279. if(!quest->GetDeleted())
  4280. packet->setArrayDataByName("active", 1, i);
  4281. packet->setArrayDataByName("name", quest->GetName(), i);
  4282. packet->setArrayDataByName("quest_type", quest->GetType(), i);
  4283. packet->setArrayDataByName("quest_zone", quest->GetZone(), i);
  4284. int8 display_status = QUEST_DISPLAY_STATUS_SHOW;
  4285. if(itr->second->GetCompleted())
  4286. packet->setArrayDataByName("completed", 1, i);
  4287. if(itr->second->GetTurnedIn()){
  4288. packet->setArrayDataByName("turned_in", 1, i);
  4289. packet->setArrayDataByName("completed", 1, i);
  4290. packet->setArrayDataByName("visible", 1, i);
  4291. packet->setArrayDataByName("unknown3", 1, i);
  4292. display_status += QUEST_DISPLAY_STATUS_COMPLETED;
  4293. }
  4294. if (updated) {
  4295. packet->setArrayDataByName("quest_updated", 1, i);
  4296. packet->setArrayDataByName("journal_updated", 1, i);
  4297. }
  4298. packet->setArrayDataByName("quest_id", quest->GetQuestID(), i);
  4299. packet->setArrayDataByName("day", quest->GetDay(), i);
  4300. packet->setArrayDataByName("month", quest->GetMonth(), i);
  4301. packet->setArrayDataByName("year", quest->GetYear(), i);
  4302. packet->setArrayDataByName("level", quest->GetQuestLevel(), i);
  4303. int8 difficulty = 0;
  4304. string category = quest->GetType();
  4305. if(category == "Tradeskill")
  4306. difficulty = GetTSArrowColor(quest->GetQuestLevel());
  4307. else
  4308. difficulty = GetArrowColor(quest->GetQuestLevel());
  4309. packet->setArrayDataByName("difficulty", difficulty, i);
  4310. if (itr->second->GetEncounterLevel() > 4)
  4311. packet->setArrayDataByName("encounter_level", quest->GetEncounterLevel(), i);
  4312. else
  4313. packet->setArrayDataByName("encounter_level", 4, i);
  4314. if(version >= 931 && quest_types.count(quest->GetType()) > 0)
  4315. packet->setArrayDataByName("zonetype_id", quest_types[quest->GetType()], i);
  4316. if(version >= 931 && quest_types.count(quest->GetZone()) > 0)
  4317. packet->setArrayDataByName("zone_id", quest_types[quest->GetZone()], i);
  4318. if(version >= 931 && quest->GetVisible()){
  4319. if (quest->GetCompletedFlag())
  4320. display_status += QUEST_DISPLAY_STATUS_COMPLETE_FLAG;
  4321. else if (quest->IsRepeatable())
  4322. display_status += QUEST_DISPLAY_STATUS_REPEATABLE;
  4323. if (quest->GetYellowName() || quest->CheckCategoryYellow())
  4324. display_status += QUEST_DISPLAY_STATUS_YELLOW;
  4325. if (quest->IsTracked())
  4326. display_status += QUEST_DISPLAY_STATUS_CHECK;
  4327. else
  4328. display_status += QUEST_DISPLAY_STATUS_NO_CHECK;
  4329. if (quest->IsHidden() && !quest->GetTurnedIn()) {
  4330. display_status += QUEST_DISPLAY_STATUS_HIDDEN;
  4331. display_status -= QUEST_DISPLAY_STATUS_SHOW;
  4332. }
  4333. if(quest->CanShareQuestCriteria(GetClient(),false)) {
  4334. display_status += QUEST_DISPLAY_STATUS_CAN_SHARE;
  4335. }
  4336. }
  4337. else
  4338. packet->setArrayDataByName("visible", quest->GetVisible(), i);
  4339. if (itr->second->IsRepeatable())
  4340. packet->setArrayDataByName("repeatable", 1, i);
  4341. packet->setArrayDataByName("display_status", display_status, i);
  4342. i++;
  4343. }
  4344. //packet->setDataByName("unknown4", 0);
  4345. packet->setDataByName("visible_quest_id", current_quest_id);
  4346. }
  4347. MPlayerQuests.releasereadlock(__FUNCTION__, __LINE__);
  4348. packet->setDataByName("player_crc", crc);
  4349. packet->setDataByName("player_name", GetName());
  4350. packet->setDataByName("used_quests", total_quests_num - total_completed_quests);
  4351. packet->setDataByName("max_quests", 75);
  4352. LogWrite(PLAYER__PACKET, 0, "Player", "Dump/Print Packet in func: %s, line: %i", __FUNCTION__, __LINE__);
  4353. #if EQDEBUG >= 9
  4354. packet->PrintPacket();
  4355. #endif
  4356. }
  4357. return packet;
  4358. }
  4359. PacketStruct* Player::GetQuestJournalPacket(Quest* quest, int16 version, int32 crc, bool updated) {
  4360. if (!quest)
  4361. return 0;
  4362. PacketStruct* packet = configReader.getStruct("WS_QuestJournalUpdate", version);
  4363. if (packet) {
  4364. packet->setArrayLengthByName("num_quests", 1);
  4365. packet->setArrayLengthByName("num_quest_zones", 1);
  4366. packet->setArrayDataByName("quest_zones_zone", quest->GetType());
  4367. packet->setArrayDataByName("quest_zones_zone_id", 0);
  4368. if(!quest->GetDeleted() && !quest->GetCompleted())
  4369. packet->setArrayDataByName("active", 1);
  4370. packet->setArrayDataByName("name", quest->GetName());
  4371. // don't see these two in the struct
  4372. packet->setArrayDataByName("quest_type", quest->GetType());
  4373. packet->setArrayDataByName("quest_zone", quest->GetZone());
  4374. int8 display_status = QUEST_DISPLAY_STATUS_SHOW;
  4375. if(quest->GetCompleted())
  4376. packet->setArrayDataByName("completed", 1);
  4377. if(quest->GetTurnedIn()) {
  4378. packet->setArrayDataByName("turned_in", 1);
  4379. packet->setArrayDataByName("completed", 1);
  4380. packet->setArrayDataByName("visible", 1);
  4381. display_status += QUEST_DISPLAY_STATUS_COMPLETED;
  4382. }
  4383. packet->setArrayDataByName("quest_id", quest->GetQuestID());
  4384. packet->setArrayDataByName("day", quest->GetDay());
  4385. packet->setArrayDataByName("month", quest->GetMonth());
  4386. packet->setArrayDataByName("year", quest->GetYear());
  4387. packet->setArrayDataByName("level", quest->GetQuestLevel());
  4388. int8 difficulty = 0;
  4389. string category = quest->GetType();
  4390. if(category == "Tradeskill")
  4391. difficulty = GetTSArrowColor(quest->GetQuestLevel());
  4392. else
  4393. difficulty = GetArrowColor(quest->GetQuestLevel());
  4394. packet->setArrayDataByName("difficulty", difficulty);
  4395. if (quest->GetEncounterLevel() > 4)
  4396. packet->setArrayDataByName("encounter_level", quest->GetEncounterLevel());
  4397. else
  4398. packet->setArrayDataByName("encounter_level", 4);
  4399. if (version >= 931) {
  4400. packet->setArrayDataByName("zonetype_id", 0);
  4401. packet->setArrayDataByName("zone_id", 0);
  4402. }
  4403. if(version >= 931 && quest->GetVisible()){
  4404. if (quest->GetCompletedFlag())
  4405. display_status += QUEST_DISPLAY_STATUS_COMPLETE_FLAG;
  4406. else if (quest->IsRepeatable())
  4407. display_status += QUEST_DISPLAY_STATUS_REPEATABLE;
  4408. if (quest->GetYellowName() || quest->CheckCategoryYellow())
  4409. display_status += QUEST_DISPLAY_STATUS_YELLOW;
  4410. if (quest->IsTracked())
  4411. display_status += QUEST_DISPLAY_STATUS_CHECK;
  4412. else
  4413. display_status += QUEST_DISPLAY_STATUS_NO_CHECK;
  4414. if (quest->IsHidden() && !quest->GetTurnedIn()) {
  4415. display_status += QUEST_DISPLAY_STATUS_HIDDEN;
  4416. display_status -= QUEST_DISPLAY_STATUS_SHOW;
  4417. }
  4418. if(quest->CanShareQuestCriteria(GetClient(),false)) {
  4419. display_status += QUEST_DISPLAY_STATUS_CAN_SHARE;
  4420. }
  4421. }
  4422. else
  4423. packet->setArrayDataByName("visible", quest->GetVisible());
  4424. if (quest->IsRepeatable())
  4425. packet->setArrayDataByName("repeatable", 1);
  4426. packet->setArrayDataByName("display_status", display_status);
  4427. if (updated) {
  4428. packet->setArrayDataByName("quest_updated", 1);
  4429. packet->setArrayDataByName("journal_updated", 1);
  4430. }
  4431. if(version >= 546)
  4432. packet->setDataByName("unknown3", 1);
  4433. packet->setDataByName("visible_quest_id", quest->GetQuestID());
  4434. packet->setDataByName("player_crc", crc);
  4435. packet->setDataByName("player_name", GetName());
  4436. packet->setDataByName("used_quests", player_quests.size());
  4437. packet->setDataByName("unknown4a", 1);
  4438. packet->setDataByName("max_quests", 75);
  4439. }
  4440. return packet;
  4441. }
  4442. Quest* Player::SetStepComplete(int32 id, int32 step){
  4443. Quest* ret = 0;
  4444. MPlayerQuests.readlock(__FUNCTION__, __LINE__);
  4445. if(player_quests.count(id) > 0){
  4446. if(player_quests[id] && player_quests[id]->SetStepComplete(step))
  4447. ret = player_quests[id];
  4448. }
  4449. MPlayerQuests.releasereadlock(__FUNCTION__, __LINE__);
  4450. return ret;
  4451. }
  4452. Quest* Player::AddStepProgress(int32 quest_id, int32 step, int32 progress) {
  4453. Quest* ret = 0;
  4454. MPlayerQuests.readlock(__FUNCTION__, __LINE__);
  4455. if (player_quests.count(quest_id) > 0) {
  4456. if (player_quests[quest_id] && player_quests[quest_id]->AddStepProgress(step, progress))
  4457. ret = player_quests[quest_id];
  4458. }
  4459. MPlayerQuests.releasereadlock(__FUNCTION__, __LINE__);
  4460. return ret;
  4461. }
  4462. int32 Player::GetStepProgress(int32 quest_id, int32 step_id) {
  4463. int32 ret = 0;
  4464. MPlayerQuests.readlock(__FUNCTION__, __LINE__);
  4465. if (player_quests.count(quest_id) > 0 && player_quests[quest_id])
  4466. ret = player_quests[quest_id]->GetStepProgress(step_id);
  4467. MPlayerQuests.releasereadlock(__FUNCTION__, __LINE__);
  4468. return ret;
  4469. }
  4470. void Player::RemoveQuest(int32 id, bool delete_quest){
  4471. MPlayerQuests.writelock(__FUNCTION__, __LINE__);
  4472. map<int32, Quest*>::iterator itr = player_quests.find(id);
  4473. if(itr != player_quests.end()) {
  4474. player_quests.erase(itr);
  4475. }
  4476. if(delete_quest){
  4477. safe_delete(player_quests[id]);
  4478. }
  4479. MPlayerQuests.releasewritelock(__FUNCTION__, __LINE__);
  4480. SendQuestRequiredSpawns(id);
  4481. }
  4482. vector<Quest*>* Player::CheckQuestsLocationUpdate(){
  4483. vector<Quest*>* quest_updates = 0;
  4484. map<int32, Quest*>::iterator itr;
  4485. MPlayerQuests.readlock(__FUNCTION__, __LINE__);
  4486. for(itr = player_quests.begin(); itr != player_quests.end(); itr++){
  4487. if(itr->second && itr->second->CheckQuestLocationUpdate(GetX(), GetY(), GetZ(), (GetZoneID()))){
  4488. if(!quest_updates)
  4489. quest_updates = new vector<Quest*>();
  4490. quest_updates->push_back(itr->second);
  4491. }
  4492. }
  4493. MPlayerQuests.releasereadlock(__FUNCTION__, __LINE__);
  4494. return quest_updates;
  4495. }
  4496. vector<Quest*>* Player::CheckQuestsFailures(){
  4497. vector<Quest*>* quest_failures = 0;
  4498. map<int32, Quest*>::iterator itr;
  4499. MPlayerQuests.readlock(__FUNCTION__, __LINE__);
  4500. for(itr = player_quests.begin(); itr != player_quests.end(); itr++){
  4501. if(itr->second && itr->second->GetQuestFailures()->size() > 0){
  4502. if(!quest_failures)
  4503. quest_failures = new vector<Quest*>();
  4504. quest_failures->push_back(itr->second);
  4505. }
  4506. }
  4507. MPlayerQuests.releasereadlock(__FUNCTION__, __LINE__);
  4508. return quest_failures;
  4509. }
  4510. vector<Quest*>* Player::CheckQuestsKillUpdate(Spawn* spawn, bool update){
  4511. vector<Quest*>* quest_updates = 0;
  4512. map<int32, Quest*>::iterator itr;
  4513. MPlayerQuests.readlock(__FUNCTION__, __LINE__);
  4514. for(itr = player_quests.begin(); itr != player_quests.end(); itr++){
  4515. if(itr->second && itr->second->CheckQuestKillUpdate(spawn, update)){
  4516. if(!quest_updates)
  4517. quest_updates = new vector<Quest*>();
  4518. quest_updates->push_back(itr->second);
  4519. }
  4520. }
  4521. MPlayerQuests.releasereadlock(__FUNCTION__, __LINE__);
  4522. return quest_updates;
  4523. }
  4524. bool Player::HasQuestUpdateRequirement(Spawn* spawn){
  4525. bool reqMet = false;
  4526. map<int32, Quest*>::iterator itr;
  4527. MPlayerQuests.readlock(__FUNCTION__, __LINE__);
  4528. for(itr = player_quests.begin(); itr != player_quests.end(); itr++){
  4529. if(itr->second && itr->second->CheckQuestReferencedSpawns(spawn)){
  4530. reqMet = true;
  4531. break;
  4532. }
  4533. }
  4534. MPlayerQuests.releasereadlock(__FUNCTION__, __LINE__);
  4535. return reqMet;
  4536. }
  4537. vector<Quest*>* Player::CheckQuestsChatUpdate(Spawn* spawn){
  4538. vector<Quest*>* quest_updates = 0;
  4539. map<int32, Quest*>::iterator itr;
  4540. MPlayerQuests.readlock(__FUNCTION__, __LINE__);
  4541. for(itr = player_quests.begin(); itr != player_quests.end(); itr++){
  4542. if(itr->second && itr->second->CheckQuestChatUpdate(spawn->GetDatabaseID())){
  4543. if(!quest_updates)
  4544. quest_updates = new vector<Quest*>();
  4545. quest_updates->push_back(itr->second);
  4546. }
  4547. }
  4548. MPlayerQuests.releasereadlock(__FUNCTION__, __LINE__);
  4549. return quest_updates;
  4550. }
  4551. int16 Player::GetTaskGroupStep(int32 quest_id){
  4552. Quest* quest = 0;
  4553. int16 step = 0;
  4554. MPlayerQuests.readlock(__FUNCTION__, __LINE__);
  4555. if(player_quests.count(quest_id) > 0){
  4556. quest = player_quests[quest_id];
  4557. if(quest) {
  4558. step = quest->GetTaskGroupStep();
  4559. }
  4560. }
  4561. MPlayerQuests.releasereadlock(__FUNCTION__, __LINE__);
  4562. return step;
  4563. }
  4564. bool Player::GetQuestStepComplete(int32 quest_id, int32 step_id){
  4565. bool ret = false;
  4566. MPlayerQuests.readlock(__FUNCTION__, __LINE__);
  4567. if(player_quests.count(quest_id) > 0){
  4568. Quest* quest = player_quests[quest_id];
  4569. if ( quest != NULL )
  4570. ret = quest->GetQuestStepCompleted(step_id);
  4571. }
  4572. MPlayerQuests.releasereadlock(__FUNCTION__, __LINE__);
  4573. return ret;
  4574. }
  4575. int16 Player::GetQuestStep(int32 quest_id){
  4576. Quest* quest = 0;
  4577. int16 step = 0;
  4578. MPlayerQuests.readlock(__FUNCTION__, __LINE__);
  4579. if(player_quests.count(quest_id) > 0){
  4580. quest = player_quests[quest_id];
  4581. if(quest) {
  4582. step = quest->GetQuestStep();
  4583. }
  4584. }
  4585. MPlayerQuests.releasereadlock(__FUNCTION__, __LINE__);
  4586. return step;
  4587. }
  4588. map<int32, Quest*>* Player::GetPlayerQuests(){
  4589. return &player_quests;
  4590. }
  4591. map<int32, Quest*>* Player::GetCompletedPlayerQuests(){
  4592. return &completed_quests;
  4593. }
  4594. Quest* Player::GetAnyQuest(int32 quest_id) {
  4595. if(player_quests.count(quest_id) > 0)
  4596. return player_quests[quest_id];
  4597. if(completed_quests.count(quest_id) > 0)
  4598. return completed_quests[quest_id];
  4599. return 0;
  4600. }
  4601. Quest* Player::GetCompletedQuest(int32 quest_id){
  4602. if(completed_quests.count(quest_id) > 0)
  4603. return completed_quests[quest_id];
  4604. return 0;
  4605. }
  4606. bool Player::HasQuestBeenCompleted(int32 quest_id){
  4607. bool ret = false;
  4608. MPlayerQuests.readlock(__FUNCTION__, __LINE__);
  4609. if(completed_quests.count(quest_id) > 0 && completed_quests[quest_id])
  4610. ret = true;
  4611. MPlayerQuests.releasereadlock(__FUNCTION__, __LINE__);
  4612. return ret;
  4613. }
  4614. bool Player::HasActiveQuest(int32 quest_id){
  4615. bool ret = false;
  4616. MPlayerQuests.readlock(__FUNCTION__, __LINE__);
  4617. if(player_quests.count(quest_id) > 0 && player_quests[quest_id])
  4618. ret = true;
  4619. MPlayerQuests.releasereadlock(__FUNCTION__, __LINE__);
  4620. return ret;
  4621. }
  4622. bool Player::HasAnyQuest(int32 quest_id){
  4623. bool ret = false;
  4624. MPlayerQuests.readlock(__FUNCTION__, __LINE__);
  4625. if(player_quests.count(quest_id) > 0)
  4626. ret = true;
  4627. if(completed_quests.count(quest_id) > 0)
  4628. ret = true;
  4629. MPlayerQuests.releasereadlock(__FUNCTION__, __LINE__);
  4630. return ret;
  4631. }
  4632. int32 Player::GetQuestCompletedCount(int32 quest_id) {
  4633. int32 count = 0;
  4634. MPlayerQuests.readlock(__FUNCTION__, __LINE__);
  4635. Quest* quest = GetCompletedQuest(quest_id);
  4636. if(quest) {
  4637. count = quest->GetCompleteCount();
  4638. }
  4639. MPlayerQuests.releasereadlock(__FUNCTION__, __LINE__);
  4640. return count;
  4641. }
  4642. Quest* Player::GetQuest(int32 quest_id){
  4643. if(player_quests.count(quest_id) > 0)
  4644. return player_quests[quest_id];
  4645. return 0;
  4646. }
  4647. void Player::AddCompletedQuest(Quest* quest){
  4648. Quest* existing = GetCompletedQuest(quest->GetQuestID());
  4649. MPlayerQuests.writelock(__FUNCTION__, __LINE__);
  4650. completed_quests[quest->GetQuestID()] = quest;
  4651. if(existing && existing != quest) {
  4652. safe_delete(existing);
  4653. }
  4654. quest->SetSaveNeeded(true);
  4655. quest->SetTurnedIn(true);
  4656. if(quest->GetCompletedDescription())
  4657. quest->SetDescription(string(quest->GetCompletedDescription()));
  4658. quest->SetUpdateRequired(true);
  4659. MPlayerQuests.releasewritelock(__FUNCTION__, __LINE__);
  4660. }
  4661. bool Player::CheckQuestRemoveFlag(Spawn* spawn){
  4662. if(current_quest_flagged.count(spawn) > 0){
  4663. current_quest_flagged.erase(spawn);
  4664. return true;
  4665. }
  4666. return false;
  4667. }
  4668. bool Player::CheckQuestRequired(Spawn* spawn){
  4669. if(spawn)
  4670. return spawn->MeetsSpawnAccessRequirements(this);
  4671. return false;
  4672. }
  4673. int8 Player::CheckQuestFlag(Spawn* spawn){
  4674. int8 ret = 0;
  4675. if (!spawn) {
  4676. LogWrite(PLAYER__ERROR, 0, "Player", "CheckQuestFlag() called with an invalid spawn");
  4677. return ret;
  4678. }
  4679. if(spawn->HasProvidedQuests()){
  4680. vector<int32>* quests = spawn->GetProvidedQuests();
  4681. Quest* quest = 0;
  4682. for(int32 i=0;i<quests->size();i++){
  4683. MPlayerQuests.readlock(__FUNCTION__, __LINE__);
  4684. if(player_quests.count(quests->at(i)) > 0){
  4685. if(player_quests[quests->at(i)] && player_quests[quests->at(i)]->GetCompleted() && player_quests[quests->at(i)]->GetQuestReturnNPC() == spawn->GetDatabaseID()){
  4686. ret = 2;
  4687. MPlayerQuests.releasereadlock(__FUNCTION__, __LINE__);
  4688. break;
  4689. }
  4690. }
  4691. MPlayerQuests.releasereadlock(__FUNCTION__, __LINE__);
  4692. int8 flag = 0;
  4693. if (CanReceiveQuest(quests->at(i), &flag)){
  4694. if(flag) {
  4695. ret = flag;
  4696. break;
  4697. }
  4698. master_quest_list.LockQuests();
  4699. quest = master_quest_list.GetQuest(quests->at(i), false);
  4700. master_quest_list.UnlockQuests();
  4701. if(quest){
  4702. int8 color = quest->GetFeatherColor();
  4703. // purple
  4704. if (color == 1)
  4705. ret = 16;
  4706. // green
  4707. else if (color == 2)
  4708. ret = 32;
  4709. // blue
  4710. else if (color == 3)
  4711. ret = 64;
  4712. // normal
  4713. else
  4714. ret = 1;
  4715. break;
  4716. }
  4717. }
  4718. }
  4719. }
  4720. map<int32, Quest*>::iterator itr;
  4721. MPlayerQuests.readlock(__FUNCTION__, __LINE__);
  4722. for(itr = player_quests.begin(); itr != player_quests.end(); itr++){
  4723. // must make sure the quest ptr is alive or nullptr
  4724. if(itr->second && itr->second->CheckQuestChatUpdate(spawn->GetDatabaseID(), false))
  4725. ret = 2;
  4726. }
  4727. MPlayerQuests.releasereadlock(__FUNCTION__, __LINE__);
  4728. if(ret > 0)
  4729. current_quest_flagged[spawn] = true;
  4730. return ret;
  4731. }
  4732. bool Player::CanReceiveQuest(int32 quest_id, int8* ret){
  4733. bool passed = true;
  4734. int32 x;
  4735. master_quest_list.LockQuests();
  4736. Quest* quest = master_quest_list.GetQuest(quest_id, false);
  4737. master_quest_list.UnlockQuests();
  4738. if (!quest)
  4739. passed = false;
  4740. //check if quest is already completed, and not repeatable
  4741. else if (HasQuestBeenCompleted(quest_id) && !quest->IsRepeatable())
  4742. passed = false;
  4743. //check if the player already has this quest
  4744. else if (player_quests.count(quest_id) > 0)
  4745. passed = false;
  4746. //Check Prereq Adv Levels
  4747. else if (quest->GetPrereqLevel() > GetLevel())
  4748. passed = false;
  4749. else if (quest->GetPrereqMaxLevel() > 0){
  4750. if (GetLevel() > quest->GetPrereqMaxLevel())
  4751. passed = false;
  4752. }
  4753. //Check Prereq TS Levels
  4754. else if (quest->GetPrereqTSLevel() > GetTSLevel())
  4755. passed = false;
  4756. else if (quest->GetPrereqMaxTSLevel() > 0){
  4757. if (GetTSLevel() > quest->GetPrereqMaxLevel())
  4758. passed = false;
  4759. }
  4760. // Check quest pre req
  4761. MPlayerQuests.readlock(__FUNCTION__, __LINE__);
  4762. vector<int32>* prereq_quests = quest->GetPrereqQuests();
  4763. if(passed && prereq_quests && prereq_quests->size() > 0){
  4764. for(int32 x=0;x<prereq_quests->size();x++){
  4765. if(completed_quests.count(prereq_quests->at(x)) == 0){
  4766. passed = false;
  4767. break;
  4768. }
  4769. }
  4770. }
  4771. MPlayerQuests.releasereadlock(__FUNCTION__, __LINE__);
  4772. //Check Prereq Classes
  4773. vector<int8>* prereq_classes = quest->GetPrereqClasses();
  4774. if(passed && prereq_classes && prereq_classes->size() > 0){
  4775. for(int32 x=0;x<prereq_classes->size();x++){
  4776. if(prereq_classes->at(x) == GetAdventureClass()){
  4777. passed = true;
  4778. break;
  4779. }
  4780. else
  4781. passed = false;
  4782. }
  4783. }
  4784. //Check Prereq TS Classes
  4785. vector<int8>* prereq_tsclasses = quest->GetPrereqTradeskillClasses();
  4786. if(passed && prereq_tsclasses && prereq_tsclasses->size() > 0){
  4787. for( x=0;x<prereq_tsclasses->size();x++){
  4788. if(prereq_tsclasses->at(x) == GetTradeskillClass()){
  4789. passed = true;
  4790. break;
  4791. }
  4792. else
  4793. passed = false;
  4794. }
  4795. }
  4796. // Check model prereq
  4797. vector<int16>* prereq_model_types = quest->GetPrereqModelTypes();
  4798. if(passed && prereq_model_types && prereq_model_types->size() > 0){
  4799. for(x=0;x<prereq_model_types->size();x++){
  4800. if(prereq_model_types->at(x) == GetModelType()){
  4801. passed = true;
  4802. break;
  4803. }
  4804. else
  4805. passed = false;
  4806. }
  4807. }
  4808. // Check faction pre req
  4809. vector<QuestFactionPrereq>* prereq_factions = quest->GetPrereqFactions();
  4810. if(passed && prereq_factions && prereq_factions->size() > 0){
  4811. sint32 val = 0;
  4812. for(x=0;x<prereq_factions->size();x++){
  4813. val = GetFactions()->GetFactionValue(prereq_factions->at(x).faction_id);
  4814. if(val >= prereq_factions->at(x).min && (prereq_factions->at(x).max == 0 || val <= prereq_factions->at(x).max)){
  4815. passed = true;
  4816. break;
  4817. }
  4818. else
  4819. passed = false;
  4820. }
  4821. }
  4822. LogWrite(MISC__TODO, 1, "TODO", "Check prereq items\n\t(%s, function: %s, line #: %i)", __FILE__, __FUNCTION__, __LINE__);
  4823. // Check race pre req
  4824. vector<int8>* prereq_races = quest->GetPrereqRaces();
  4825. if(passed && prereq_races && prereq_races->size() > 0){
  4826. for(x=0;x<prereq_races->size();x++){
  4827. if(prereq_races->at(x) == GetRace()){
  4828. passed = true;
  4829. break;
  4830. }
  4831. else
  4832. passed = false;
  4833. }
  4834. }
  4835. int32 flag = 0;
  4836. if(lua_interface->CallQuestFunction(quest, "ReceiveQuestCriteria", this, 0xFFFFFFFF, &flag)) {
  4837. if(ret)
  4838. *ret = flag;
  4839. if(!flag) {
  4840. passed = false;
  4841. }
  4842. else {
  4843. passed = true;
  4844. }
  4845. }
  4846. return passed;
  4847. }
  4848. bool Player::UpdateQuestReward(int32 quest_id, QuestRewardData* qrd) {
  4849. if(!GetClient())
  4850. return false;
  4851. MPlayerQuests.readlock(__FUNCTION__, __LINE__);
  4852. Quest* quest = GetAnyQuest(quest_id);
  4853. if(!quest) {
  4854. MPlayerQuests.releasereadlock(__FUNCTION__, __LINE__);
  4855. return false;
  4856. }
  4857. quest->SetQuestTemporaryState(qrd->is_temporary, qrd->description);
  4858. if(qrd->is_temporary) {
  4859. quest->SetStatusTmpReward(qrd->tmp_status);
  4860. quest->SetCoinTmpReward(qrd->tmp_coin);
  4861. }
  4862. MPlayerQuests.releasereadlock(__FUNCTION__, __LINE__);
  4863. GetClient()->GiveQuestReward(quest, qrd->has_displayed);
  4864. SetActiveReward(true);
  4865. return true;
  4866. }
  4867. Quest* Player::PendingQuestAcceptance(int32 quest_id, int32 item_id, bool* quest_exists) {
  4868. vector<Item*>* items = 0;
  4869. bool ret = false;
  4870. MPlayerQuests.readlock(__FUNCTION__, __LINE__);
  4871. Quest* quest = GetAnyQuest(quest_id);
  4872. if(!quest) {
  4873. if(quest_exists) {
  4874. *quest_exists = false;
  4875. }
  4876. MPlayerQuests.releasereadlock(__FUNCTION__, __LINE__);
  4877. return nullptr;
  4878. }
  4879. if(quest_exists) {
  4880. *quest_exists = true;
  4881. }
  4882. if(quest->GetQuestTemporaryState())
  4883. items = quest->GetTmpRewardItems();
  4884. else
  4885. items = quest->GetRewardItems();
  4886. if (item_id == 0) {
  4887. ret = true;
  4888. }
  4889. else {
  4890. items = quest->GetSelectableRewardItems();
  4891. if (items && items->size() > 0) {
  4892. for (int32 i = 0; i < items->size(); i++) {
  4893. if (items->at(i)->details.item_id == item_id) {
  4894. ret = true;
  4895. break;
  4896. }
  4897. }
  4898. }
  4899. }
  4900. MPlayerQuests.releasereadlock(__FUNCTION__, __LINE__);
  4901. return quest;
  4902. }
  4903. bool Player::AcceptQuestReward(int32 item_id, int32 selectable_item_id) {
  4904. if(!GetClient()) {
  4905. return false;
  4906. }
  4907. Collection *collection = 0;
  4908. MPlayerQuests.readlock(__FUNCTION__, __LINE__);
  4909. Quest* quest = client->GetPendingQuestAcceptance(item_id);
  4910. if(quest){
  4911. GetClient()->AcceptQuestReward(quest, item_id);
  4912. MPlayerQuests.releasereadlock(__FUNCTION__, __LINE__);
  4913. return true;
  4914. }
  4915. bool collectedItems = false;
  4916. if (client->GetPlayer()->HasPendingItemRewards()) {
  4917. vector<Item*> items = client->GetPlayer()->GetPendingItemRewards();
  4918. if (items.size() > 0) {
  4919. collectedItems = true;
  4920. for (int i = 0; i < items.size(); i++) {
  4921. client->GetPlayer()->AddItem(new Item(items[i]));
  4922. }
  4923. client->GetPlayer()->ClearPendingItemRewards();
  4924. client->GetPlayer()->SetActiveReward(false);
  4925. }
  4926. map<int32, Item*> selectable_item = client->GetPlayer()->GetPendingSelectableItemReward(item_id);
  4927. if (selectable_item.size() > 0) {
  4928. collectedItems = true;
  4929. map<int32, Item*>::iterator itr;
  4930. for (itr = selectable_item.begin(); itr != selectable_item.end(); itr++) {
  4931. client->GetPlayer()->AddItem(new Item(itr->second));
  4932. client->GetPlayer()->ClearPendingSelectableItemRewards(itr->first);
  4933. }
  4934. client->GetPlayer()->SetActiveReward(false);
  4935. }
  4936. }
  4937. else if (collection = GetPendingCollectionReward())
  4938. {
  4939. client->AcceptCollectionRewards(collection, (selectable_item_id > 0) ? selectable_item_id : item_id);
  4940. collectedItems = true;
  4941. }
  4942. MPlayerQuests.releasereadlock(__FUNCTION__, __LINE__);
  4943. return collectedItems;
  4944. }
  4945. bool Player::SendQuestStepUpdate(int32 quest_id, int32 quest_step_id, bool display_quest_helper) {
  4946. MPlayerQuests.readlock(__FUNCTION__, __LINE__);
  4947. Quest* quest = GetAnyQuest(quest_id);
  4948. if(!quest) {
  4949. MPlayerQuests.releasereadlock(__FUNCTION__, __LINE__);
  4950. return false;
  4951. }
  4952. QuestStep* quest_step = quest->GetQuestStep(quest_step_id);
  4953. if (quest_step) {
  4954. if(GetClient()) {
  4955. GetClient()->QueuePacket(quest->QuestJournalReply(GetClient()->GetVersion(), GetClient()->GetNameCRC(), this, quest_step, 1, false, false, display_quest_helper));
  4956. }
  4957. quest_step->WasUpdated(false);
  4958. }
  4959. bool turnedIn = quest->GetTurnedIn();
  4960. MPlayerQuests.releasereadlock(__FUNCTION__, __LINE__);
  4961. if(turnedIn && GetClient()) //update the journal so the old quest isn't the one displayed in the client's quest helper
  4962. GetClient()->SendQuestJournal();
  4963. return true;
  4964. }
  4965. void Player::SendQuest(int32 quest_id) {
  4966. if(!GetClient()) {
  4967. return;
  4968. }
  4969. MPlayerQuests.readlock(__FUNCTION__, __LINE__);
  4970. Quest* quest = GetQuest(quest_id);
  4971. if (quest)
  4972. GetClient()->QueuePacket(quest->QuestJournalReply(GetClient()->GetVersion(), GetClient()->GetNameCRC(), this));
  4973. else {
  4974. quest = GetCompletedQuest(quest_id);
  4975. if (quest)
  4976. GetClient()->QueuePacket(quest->QuestJournalReply(GetClient()->GetVersion(), GetClient()->GetNameCRC(), this, 0, 1, true));
  4977. }
  4978. MPlayerQuests.releasereadlock(__FUNCTION__, __LINE__);
  4979. }
  4980. void Player::UpdateQuestCompleteCount(int32 quest_id) {
  4981. if(!GetClient()) {
  4982. return;
  4983. }
  4984. MPlayerQuests.readlock(__FUNCTION__, __LINE__);
  4985. // If character has already completed this quest once update the given date in the database
  4986. Quest* quest = GetQuest(id);
  4987. Quest* quest2 = GetCompletedQuest(id);
  4988. if (quest && quest2) {
  4989. quest->SetCompleteCount(quest2->GetCompleteCount());
  4990. database.SaveCharRepeatableQuest(GetClient(), id, quest->GetCompleteCount());
  4991. }
  4992. MPlayerQuests.releasereadlock(__FUNCTION__, __LINE__);
  4993. }
  4994. void Player::GetQuestTemporaryRewards(int32 quest_id, std::vector<Item*>* items) {
  4995. MPlayerQuests.readlock(__FUNCTION__, __LINE__);
  4996. Quest* quest = GetAnyQuest(quest_id);
  4997. if(quest) {
  4998. quest->GetTmpRewardItemsByID(items);
  4999. }
  5000. MPlayerQuests.releasereadlock(__FUNCTION__, __LINE__);
  5001. }
  5002. void Player::AddQuestTemporaryReward(int32 quest_id, int32 item_id, int16 item_count) {
  5003. MPlayerQuests.readlock(__FUNCTION__, __LINE__);
  5004. Quest* quest = GetAnyQuest(quest_id);
  5005. if(quest) {
  5006. Item* item = master_item_list.GetItem(item_id);
  5007. if(item) {
  5008. Item* tmpItem = new Item(item);
  5009. tmpItem->details.count = item_count;
  5010. quest->AddTmpRewardItem(tmpItem);
  5011. }
  5012. }
  5013. MPlayerQuests.releasereadlock(__FUNCTION__, __LINE__);
  5014. }
  5015. bool Player::ShouldSendSpawn(Spawn* spawn){
  5016. if(spawn->IsDeletedSpawn() || IsSendingSpawn(spawn->GetID()) || IsRemovingSpawn(spawn->GetID()))
  5017. return false;
  5018. else if((WasSentSpawn(spawn->GetID()) == false) && (!spawn->IsPrivateSpawn() || spawn->AllowedAccess(this)))
  5019. return true;
  5020. return false;
  5021. }
  5022. int8 Player::GetTSArrowColor(int8 level){
  5023. int8 color = 0;
  5024. sint16 diff = level - GetTSLevel();
  5025. if(GetLevel() < 10)
  5026. diff *= 3;
  5027. else if(GetLevel() <= 20)
  5028. diff *= 2;
  5029. if(diff >= 9)
  5030. color = ARROW_COLOR_RED;
  5031. else if(diff >= 5)
  5032. color = ARROW_COLOR_ORANGE;
  5033. else if(diff >= 1)
  5034. color = ARROW_COLOR_YELLOW;
  5035. else if(diff == 0)
  5036. color = ARROW_COLOR_WHITE;
  5037. else if(diff <= -11)
  5038. color = ARROW_COLOR_GRAY;
  5039. else if(diff <= -6)
  5040. color = ARROW_COLOR_GREEN;
  5041. else //if(diff < 0)
  5042. color = ARROW_COLOR_BLUE;
  5043. return color;
  5044. }
  5045. void Player::AddCoins(int64 val){
  5046. int32 tmp = 0;
  5047. UpdatePlayerStatistic(STAT_PLAYER_TOTAL_WEALTH, (GetCoinsCopper() + (GetCoinsSilver() * 100) + (GetCoinsGold() * 10000) + (GetCoinsPlat() * 1000000)) + val, true);
  5048. if(val >= 1000000){
  5049. tmp = val / 1000000;
  5050. val -= tmp*1000000;
  5051. GetInfoStruct()->add_coin_plat(tmp);
  5052. }
  5053. if(val >= 10000){
  5054. tmp = val / 10000;
  5055. val -= tmp*10000;
  5056. GetInfoStruct()->add_coin_gold(tmp);
  5057. }
  5058. if(val >= 100){
  5059. tmp = val / 100;
  5060. val -= tmp*100;
  5061. GetInfoStruct()->add_coin_silver(tmp);
  5062. }
  5063. GetInfoStruct()->add_coin_copper(val);
  5064. int32 new_copper_value = GetInfoStruct()->get_coin_copper();
  5065. if(new_copper_value >= 100){
  5066. tmp = new_copper_value/100;
  5067. GetInfoStruct()->set_coin_copper(new_copper_value - (100 * tmp));
  5068. GetInfoStruct()->add_coin_silver(tmp);
  5069. }
  5070. int32 new_silver_value = GetInfoStruct()->get_coin_silver();
  5071. if(new_silver_value >= 100){
  5072. tmp = new_silver_value/100;
  5073. GetInfoStruct()->set_coin_silver(new_silver_value - (100 * tmp));
  5074. GetInfoStruct()->add_coin_gold(tmp);
  5075. }
  5076. int32 new_gold_value = GetInfoStruct()->get_coin_gold();
  5077. if(new_gold_value >= 100){
  5078. tmp = new_gold_value/100;
  5079. GetInfoStruct()->set_coin_gold(new_gold_value - (100 * tmp));
  5080. GetInfoStruct()->add_coin_plat(tmp);
  5081. }
  5082. charsheet_changed = true;
  5083. }
  5084. bool Player::RemoveCoins(int64 val){
  5085. int64 total_coins = GetInfoStruct()->get_coin_plat()*1000000;
  5086. total_coins += GetInfoStruct()->get_coin_gold()*10000;
  5087. total_coins += GetInfoStruct()->get_coin_silver()*100;
  5088. total_coins += GetInfoStruct()->get_coin_copper();
  5089. if(total_coins >= val){
  5090. total_coins -= val;
  5091. GetInfoStruct()->set_coin_plat(0);
  5092. GetInfoStruct()->set_coin_gold(0);
  5093. GetInfoStruct()->set_coin_silver(0);
  5094. GetInfoStruct()->set_coin_copper(0);
  5095. AddCoins(total_coins);
  5096. return true;
  5097. }
  5098. return false;
  5099. }
  5100. bool Player::HasCoins(int64 val) {
  5101. int64 total_coins = GetInfoStruct()->get_coin_plat()*1000000;
  5102. total_coins += GetInfoStruct()->get_coin_gold()*10000;
  5103. total_coins += GetInfoStruct()->get_coin_silver()*100;
  5104. total_coins += GetInfoStruct()->get_coin_copper();
  5105. if(total_coins >= val)
  5106. return true;
  5107. return false;
  5108. }
  5109. bool Player::HasPendingLootItems(int32 id){
  5110. return (pending_loot_items.count(id) > 0 && pending_loot_items[id].size() > 0);
  5111. }
  5112. bool Player::HasPendingLootItem(int32 id, int32 item_id){
  5113. return (pending_loot_items.count(id) > 0 && pending_loot_items[id].count(item_id) > 0);
  5114. }
  5115. vector<Item*>* Player::GetPendingLootItems(int32 id){
  5116. vector<Item*>* ret = 0;
  5117. if(pending_loot_items.count(id) > 0){
  5118. ret = new vector<Item*>();
  5119. map<int32, bool>::iterator itr;
  5120. for(itr = pending_loot_items[id].begin(); itr != pending_loot_items[id].end(); itr++){
  5121. if(master_item_list.GetItem(itr->first))
  5122. ret->push_back(master_item_list.GetItem(itr->first));
  5123. }
  5124. }
  5125. return ret;
  5126. }
  5127. void Player::RemovePendingLootItem(int32 id, int32 item_id){
  5128. if(pending_loot_items.count(id) > 0){
  5129. pending_loot_items[id].erase(item_id);
  5130. if(pending_loot_items[id].size() == 0)
  5131. pending_loot_items.erase(id);
  5132. }
  5133. }
  5134. void Player::RemovePendingLootItems(int32 id){
  5135. if(pending_loot_items.count(id) > 0)
  5136. pending_loot_items.erase(id);
  5137. }
  5138. void Player::AddPendingLootItems(int32 id, vector<Item*>* items){
  5139. if(items){
  5140. Item* item = 0;
  5141. for(int32 i=0;i<items->size();i++){
  5142. item = items->at(i);
  5143. if(item)
  5144. pending_loot_items[id][item->details.item_id] = true;
  5145. }
  5146. }
  5147. }
  5148. void Player::AddPlayerStatistic(int32 stat_id, sint32 stat_value, int32 stat_date) {
  5149. if (statistics.count(stat_id) == 0) {
  5150. Statistic* stat = new Statistic;
  5151. stat->stat_id = stat_id;
  5152. stat->stat_value = stat_value;
  5153. stat->stat_date = stat_date;
  5154. stat->save_needed = false;
  5155. statistics[stat_id] = stat;
  5156. }
  5157. }
  5158. void Player::UpdatePlayerStatistic(int32 stat_id, sint32 stat_value, bool overwrite) {
  5159. if (statistics.count(stat_id) == 0)
  5160. AddPlayerStatistic(stat_id, 0, 0);
  5161. Statistic* stat = statistics[stat_id];
  5162. overwrite == true ? stat->stat_value = stat_value : stat->stat_value += stat_value;
  5163. stat->stat_date = Timer::GetUnixTimeStamp();
  5164. stat->save_needed = true;
  5165. }
  5166. void Player::WritePlayerStatistics() {
  5167. map<int32, Statistic*>::iterator stat_itr;
  5168. for (stat_itr = statistics.begin(); stat_itr != statistics.end(); stat_itr++) {
  5169. Statistic* stat = stat_itr->second;
  5170. if (stat->save_needed) {
  5171. stat->save_needed = false;
  5172. database.WritePlayerStatistic(this, stat);
  5173. }
  5174. }
  5175. }
  5176. sint64 Player::GetPlayerStatisticValue(int32 stat_id) {
  5177. if (stat_id >= 0 && statistics.count(stat_id) > 0)
  5178. return statistics[stat_id]->stat_value;
  5179. return 0;
  5180. }
  5181. void Player::RemovePlayerStatistics() {
  5182. map<int32, Statistic*>::iterator stat_itr;
  5183. for (stat_itr = statistics.begin(); stat_itr != statistics.end(); stat_itr++)
  5184. safe_delete(stat_itr->second);
  5185. statistics.clear();
  5186. }
  5187. void Player::SetGroup(PlayerGroup* new_group){
  5188. group = new_group;
  5189. }
  5190. /*PlayerGroup* Player::GetGroup(){
  5191. return group;
  5192. }*/
  5193. bool Player::IsGroupMember(Entity* player) {
  5194. bool ret = false;
  5195. if (GetGroupMemberInfo() && player) {
  5196. //world.GetGroupManager()->GroupLock(__FUNCTION__, __LINE__);
  5197. ret = world.GetGroupManager()->IsInGroup(GetGroupMemberInfo()->group_id, player);
  5198. /*deque<GroupMemberInfo*>::iterator itr;
  5199. deque<GroupMemberInfo*>* members = world.GetGroupManager()->GetGroupMembers(GetGroupMemberInfo()->group_id);
  5200. for (itr = members->begin(); itr != members->end(); itr++) {
  5201. GroupMemberInfo* gmi = *itr;
  5202. if (gmi->client && gmi->client->GetPlayer() == player) {
  5203. ret = true;
  5204. break;
  5205. }
  5206. }
  5207. world.GetGroupManager()->ReleaseGroupLock(__FUNCTION__, __LINE__);*/
  5208. }
  5209. return ret;
  5210. }
  5211. void Player::SetGroupInformation(PacketStruct* packet){
  5212. int8 det_count = 0;
  5213. Entity* member = 0;
  5214. world.GetGroupManager()->GroupLock(__FUNCTION__, __LINE__);
  5215. if (GetGroupMemberInfo()) {
  5216. PlayerGroup* group = world.GetGroupManager()->GetGroup(GetGroupMemberInfo()->group_id);
  5217. if (group)
  5218. {
  5219. group->MGroupMembers.readlock(__FUNCTION__, __LINE__);
  5220. deque<GroupMemberInfo*>* members = group->GetMembers();
  5221. deque<GroupMemberInfo*>::iterator itr;
  5222. GroupMemberInfo* info = 0;
  5223. int x = 0;
  5224. for (itr = members->begin(); itr != members->end(); itr++) {
  5225. info = *itr;
  5226. if (info == GetGroupMemberInfo()) {
  5227. if (info->leader)
  5228. packet->setDataByName("group_leader_id", 0xFFFFFFFF); // If this player is the group leader then fill this element with FF FF FF FF
  5229. continue;
  5230. }
  5231. else {
  5232. if (info->leader)
  5233. packet->setDataByName("group_leader_id", x); // If leader is some one else then fill with the slot number they are in
  5234. }
  5235. member = info->member;
  5236. if (member && member->GetZone() == GetZone()) {
  5237. packet->setSubstructDataByName("group_members", "spawn_id", GetIDWithPlayerSpawn(member), x);
  5238. if (member->HasPet()) {
  5239. if (member->GetPet())
  5240. packet->setSubstructDataByName("group_members", "pet_id", GetIDWithPlayerSpawn(member->GetPet()), x);
  5241. else
  5242. packet->setSubstructDataByName("group_members", "pet_id", GetIDWithPlayerSpawn(member->GetCharmedPet()), x);
  5243. }
  5244. else
  5245. packet->setSubstructDataByName("group_members", "pet_id", 0xFFFFFFFF, x);
  5246. //Send detriment counts as 255 if all dets of that type are incurable
  5247. det_count = member->GetTraumaCount();
  5248. if (det_count > 0) {
  5249. if (!member->HasCurableDetrimentType(DET_TYPE_TRAUMA))
  5250. det_count = 255;
  5251. }
  5252. packet->setSubstructDataByName("group_members", "trauma_count", det_count, x);
  5253. det_count = member->GetArcaneCount();
  5254. if (det_count > 0) {
  5255. if (!member->HasCurableDetrimentType(DET_TYPE_ARCANE))
  5256. det_count = 255;
  5257. }
  5258. packet->setSubstructDataByName("group_members", "arcane_count", det_count, x);
  5259. det_count = member->GetNoxiousCount();
  5260. if (det_count > 0) {
  5261. if (!member->HasCurableDetrimentType(DET_TYPE_NOXIOUS))
  5262. det_count = 255;
  5263. }
  5264. packet->setSubstructDataByName("group_members", "noxious_count", det_count, x);
  5265. det_count = member->GetElementalCount();
  5266. if (det_count > 0) {
  5267. if (!member->HasCurableDetrimentType(DET_TYPE_ELEMENTAL))
  5268. det_count = 255;
  5269. }
  5270. packet->setSubstructDataByName("group_members", "elemental_count", det_count, x);
  5271. det_count = member->GetCurseCount();
  5272. if (det_count > 0) {
  5273. if (!member->HasCurableDetrimentType(DET_TYPE_CURSE))
  5274. det_count = 255;
  5275. }
  5276. packet->setSubstructDataByName("group_members", "curse_count", det_count, x);
  5277. packet->setSubstructDataByName("group_members", "zone_status", 1, x);
  5278. }
  5279. else {
  5280. packet->setSubstructDataByName("group_members", "pet_id", 0xFFFFFFFF, x);
  5281. //packet->setSubstructDataByName("group_members", "unknown5", 1, x, 1); // unknown5 > 1 = name is blue
  5282. packet->setSubstructDataByName("group_members", "zone_status", 2, x);
  5283. }
  5284. packet->setSubstructDataByName("group_members", "name", info->name.c_str(), x);
  5285. packet->setSubstructDataByName("group_members", "hp_current", info->hp_current, x);
  5286. packet->setSubstructDataByName("group_members", "hp_max", info->hp_max, x);
  5287. packet->setSubstructDataByName("group_members", "power_current", info->power_current, x);
  5288. packet->setSubstructDataByName("group_members", "power_max", info->power_max, x);
  5289. packet->setSubstructDataByName("group_members", "level_current", info->level_current, x);
  5290. packet->setSubstructDataByName("group_members", "level_max", info->level_max, x);
  5291. packet->setSubstructDataByName("group_members", "zone", info->zone.c_str(), x);
  5292. packet->setSubstructDataByName("group_members", "race_id", info->race_id, x);
  5293. packet->setSubstructDataByName("group_members", "class_id", info->class_id, x);
  5294. x++;
  5295. }
  5296. }
  5297. group->MGroupMembers.releasereadlock(__FUNCTION__, __LINE__);
  5298. }
  5299. //packet->PrintPacket();
  5300. world.GetGroupManager()->ReleaseGroupLock(__FUNCTION__, __LINE__);
  5301. }
  5302. PlayerItemList* Player::GetPlayerItemList(){
  5303. return &item_list;
  5304. }
  5305. void Player::ResetSavedSpawns(){
  5306. spawn_mutex.writelock(__FUNCTION__, __LINE__);
  5307. ClearRemovalTimers();
  5308. spawn_packet_sent.clear();
  5309. spawn_mutex.releasewritelock(__FUNCTION__, __LINE__);
  5310. info_mutex.writelock(__FUNCTION__, __LINE__);
  5311. spawn_info_packet_list.clear();
  5312. info_mutex.releasewritelock(__FUNCTION__, __LINE__);
  5313. vis_mutex.writelock(__FUNCTION__, __LINE__);
  5314. spawn_vis_packet_list.clear();
  5315. vis_mutex.releasewritelock(__FUNCTION__, __LINE__);
  5316. pos_mutex.writelock(__FUNCTION__, __LINE__);
  5317. spawn_pos_packet_list.clear();
  5318. pos_mutex.releasewritelock(__FUNCTION__, __LINE__);
  5319. index_mutex.writelock(__FUNCTION__, __LINE__);
  5320. player_spawn_reverse_id_map.clear();
  5321. player_spawn_id_map.clear();
  5322. player_spawn_id_map[1] = this;
  5323. player_spawn_reverse_id_map[this] = 1;
  5324. spawn_index = 1;
  5325. index_mutex.releasewritelock(__FUNCTION__, __LINE__);
  5326. m_playerSpawnQuestsRequired.writelock(__FUNCTION__, __LINE__);
  5327. player_spawn_quests_required.clear();
  5328. m_playerSpawnQuestsRequired.releasewritelock(__FUNCTION__, __LINE__);
  5329. if(info)
  5330. info->RemoveOldPackets();
  5331. safe_delete_array(movement_packet);
  5332. safe_delete_array(old_movement_packet);
  5333. }
  5334. void Player::SetReturningFromLD(bool val){
  5335. std::unique_lock lock(spell_packet_update_mutex);
  5336. if(val && val != returning_from_ld)
  5337. {
  5338. if(GetPlayerItemList())
  5339. GetPlayerItemList()->ResetPackets();
  5340. GetEquipmentList()->ResetPackets();
  5341. GetAppearanceEquipmentList()->ResetPackets();
  5342. skill_list.ResetPackets();
  5343. safe_delete_array(spell_orig_packet);
  5344. safe_delete_array(spell_xor_packet);
  5345. spell_orig_packet=0;
  5346. spell_xor_packet=0;
  5347. spell_count = 0;
  5348. safe_delete_array(raid_orig_packet);
  5349. safe_delete_array(raid_xor_packet);
  5350. raid_orig_packet=0;
  5351. raid_xor_packet=0;
  5352. reset_character_flag(CF_IS_SITTING);
  5353. if (GetActivityStatus() & ACTIVITY_STATUS_CAMPING)
  5354. SetActivityStatus(GetActivityStatus() - ACTIVITY_STATUS_CAMPING);
  5355. if (GetActivityStatus() & ACTIVITY_STATUS_LINKDEAD)
  5356. SetActivityStatus(GetActivityStatus() - ACTIVITY_STATUS_LINKDEAD);
  5357. SetTempVisualState(0);
  5358. safe_delete_array(spawn_tmp_info_xor_packet);
  5359. safe_delete_array(spawn_tmp_vis_xor_packet);
  5360. safe_delete_array(spawn_tmp_pos_xor_packet);
  5361. spawn_tmp_info_xor_packet = 0;
  5362. spawn_tmp_vis_xor_packet = 0;
  5363. spawn_tmp_pos_xor_packet = 0;
  5364. pos_xor_size = 0;
  5365. info_xor_size = 0;
  5366. vis_xor_size = 0;
  5367. index_mutex.writelock(__FUNCTION__, __LINE__);
  5368. player_spawn_id_map[1] = this;
  5369. player_spawn_reverse_id_map[this] = 1;
  5370. spawn_index = 1;
  5371. index_mutex.releasewritelock(__FUNCTION__, __LINE__);
  5372. }
  5373. returning_from_ld = val;
  5374. }
  5375. bool Player::IsReturningFromLD(){
  5376. return returning_from_ld;
  5377. }
  5378. void Player::AddFriend(const char* name, bool save){
  5379. if(name){
  5380. if(save)
  5381. friend_list[name] = 1;
  5382. else
  5383. friend_list[name] = 0;
  5384. }
  5385. }
  5386. bool Player::IsFriend(const char* name){
  5387. if(name && friend_list.count(name) > 0 && friend_list[name] < 2)
  5388. return true;
  5389. return false;
  5390. }
  5391. void Player::RemoveFriend(const char* name){
  5392. if(friend_list.count(name) > 0)
  5393. friend_list[name] = 2;
  5394. }
  5395. map<string, int8>* Player::GetFriends(){
  5396. return &friend_list;
  5397. }
  5398. void Player::AddIgnore(const char* name, bool save){
  5399. if(name){
  5400. if(save)
  5401. ignore_list[name] = 1;
  5402. else
  5403. ignore_list[name] = 0;
  5404. }
  5405. }
  5406. bool Player::IsIgnored(const char* name){
  5407. if(name && ignore_list.count(name) > 0 && ignore_list[name] < 2)
  5408. return true;
  5409. return false;
  5410. }
  5411. void Player::RemoveIgnore(const char* name){
  5412. if(name && ignore_list.count(name) > 0)
  5413. ignore_list[name] = 2;
  5414. }
  5415. map<string, int8>* Player::GetIgnoredPlayers(){
  5416. return &ignore_list;
  5417. }
  5418. bool Player::CheckLevelStatus(int16 new_level) {
  5419. int16 LevelCap = rule_manager.GetGlobalRule(R_Player, MaxLevel)->GetInt16();
  5420. int16 LevelCapOverrideStatus = rule_manager.GetGlobalRule(R_Player, MaxLevelOverrideStatus)->GetInt16();
  5421. if ( GetClient() && (LevelCap < new_level) && (LevelCapOverrideStatus > GetClient()->GetAdminStatus()) )
  5422. return false;
  5423. return true;
  5424. }
  5425. Skill* Player::GetSkillByName(const char* name, bool check_update){
  5426. Skill* ret = skill_list.GetSkillByName(name);
  5427. if(check_update)
  5428. {
  5429. if(skill_list.CheckSkillIncrease(ret))
  5430. CalculateBonuses();
  5431. }
  5432. return ret;
  5433. }
  5434. Skill* Player::GetSkillByID(int32 id, bool check_update){
  5435. Skill* ret = skill_list.GetSkill(id);
  5436. if(check_update)
  5437. {
  5438. if(skill_list.CheckSkillIncrease(ret))
  5439. CalculateBonuses();
  5440. }
  5441. return ret;
  5442. }
  5443. void Player::SetRangeAttack(bool val){
  5444. range_attack = val;
  5445. }
  5446. bool Player::GetRangeAttack(){
  5447. return range_attack;
  5448. }
  5449. bool Player::AddMail(Mail* mail) {
  5450. bool ret = false;
  5451. if (mail) {
  5452. mail_list.Put(mail->mail_id, mail);
  5453. ret = true;
  5454. }
  5455. return ret;
  5456. }
  5457. MutexMap<int32, Mail*>* Player::GetMail() {
  5458. return &mail_list;
  5459. }
  5460. Mail* Player::GetMail(int32 mail_id) {
  5461. Mail* mail = 0;
  5462. if (mail_list.count(mail_id) > 0)
  5463. mail = mail_list.Get(mail_id);
  5464. return mail;
  5465. }
  5466. void Player::DeleteMail(bool from_database) {
  5467. MutexMap<int32, Mail*>::iterator itr = mail_list.begin();
  5468. while (itr.Next())
  5469. DeleteMail(itr->first, from_database);
  5470. mail_list.clear();
  5471. }
  5472. void Player::DeleteMail(int32 mail_id, bool from_database) {
  5473. if (mail_list.count(mail_id) > 0) {
  5474. if (from_database)
  5475. database.DeletePlayerMail(mail_list.Get(mail_id));
  5476. mail_list.erase(mail_id, true, true); // need to delete the mail ptr
  5477. }
  5478. }
  5479. /* CharacterInstances */
  5480. CharacterInstances::CharacterInstances() {
  5481. m_instanceList.SetName("Mutex::m_instanceList");
  5482. }
  5483. CharacterInstances::~CharacterInstances() {
  5484. RemoveInstances();
  5485. }
  5486. void CharacterInstances::AddInstance(int32 db_id, int32 instance_id, int32 last_success_timestamp, int32 last_failure_timestamp, int32 success_lockout_time, int32 failure_lockout_time, int32 zone_id, int8 zone_instancetype, string zone_name) {
  5487. InstanceData data;
  5488. data.db_id = db_id;
  5489. data.instance_id = instance_id;
  5490. data.zone_id = zone_id;
  5491. data.zone_instance_type = zone_instancetype;
  5492. data.zone_name = zone_name;
  5493. data.last_success_timestamp = last_success_timestamp;
  5494. data.last_failure_timestamp = last_failure_timestamp;
  5495. data.success_lockout_time = success_lockout_time;
  5496. data.failure_lockout_time = failure_lockout_time;
  5497. instanceList.push_back(data);
  5498. }
  5499. void CharacterInstances::RemoveInstances() {
  5500. instanceList.clear();
  5501. }
  5502. bool CharacterInstances::RemoveInstanceByZoneID(int32 zone_id) {
  5503. vector<InstanceData>::iterator itr;
  5504. m_instanceList.writelock(__FUNCTION__, __LINE__);
  5505. for(itr = instanceList.begin(); itr != instanceList.end(); itr++) {
  5506. InstanceData* data = &(*itr);
  5507. if (data->zone_id == zone_id) {
  5508. instanceList.erase(itr);
  5509. m_instanceList.releasewritelock(__FUNCTION__, __LINE__);
  5510. return true;
  5511. }
  5512. }
  5513. m_instanceList.releasewritelock(__FUNCTION__, __LINE__);
  5514. return false;
  5515. }
  5516. bool CharacterInstances::RemoveInstanceByInstanceID(int32 instance_id) {
  5517. vector<InstanceData>::iterator itr;
  5518. m_instanceList.writelock(__FUNCTION__, __LINE__);
  5519. for(itr = instanceList.begin(); itr != instanceList.end(); itr++) {
  5520. InstanceData* data = &(*itr);
  5521. if (data->instance_id == instance_id) {
  5522. instanceList.erase(itr);
  5523. m_instanceList.releasewritelock(__FUNCTION__, __LINE__);
  5524. return true;
  5525. }
  5526. }
  5527. m_instanceList.releasewritelock(__FUNCTION__, __LINE__);
  5528. return false;
  5529. }
  5530. InstanceData* CharacterInstances::FindInstanceByZoneID(int32 zone_id) {
  5531. m_instanceList.readlock(__FUNCTION__, __LINE__);
  5532. for(int32 i = 0; i < instanceList.size(); i++) {
  5533. InstanceData* data = &instanceList.at(i);
  5534. if (data->zone_id == zone_id) {
  5535. m_instanceList.releasereadlock(__FUNCTION__, __LINE__);
  5536. return data;
  5537. }
  5538. }
  5539. m_instanceList.releasereadlock(__FUNCTION__, __LINE__);
  5540. return 0;
  5541. }
  5542. InstanceData* CharacterInstances::FindInstanceByDBID(int32 db_id) {
  5543. m_instanceList.readlock(__FUNCTION__, __LINE__);
  5544. for(int32 i = 0; i < instanceList.size(); i++) {
  5545. InstanceData* data = &instanceList.at(i);
  5546. if (data->db_id == db_id) {
  5547. m_instanceList.releasereadlock(__FUNCTION__, __LINE__);
  5548. return data;
  5549. }
  5550. }
  5551. m_instanceList.releasereadlock(__FUNCTION__, __LINE__);
  5552. return 0;
  5553. }
  5554. InstanceData* CharacterInstances::FindInstanceByInstanceID(int32 instance_id) {
  5555. m_instanceList.readlock(__FUNCTION__, __LINE__);
  5556. for(int32 i = 0; i < instanceList.size(); i++) {
  5557. InstanceData* data = &instanceList.at(i);
  5558. if (data->instance_id == instance_id) {
  5559. m_instanceList.releasereadlock(__FUNCTION__, __LINE__);
  5560. return data;
  5561. }
  5562. }
  5563. m_instanceList.releasereadlock(__FUNCTION__, __LINE__);
  5564. return 0;
  5565. }
  5566. vector<InstanceData> CharacterInstances::GetLockoutInstances() {
  5567. vector<InstanceData> ret;
  5568. m_instanceList.readlock(__FUNCTION__, __LINE__);
  5569. for (int32 i = 0; i < instanceList.size(); i++) {
  5570. InstanceData* data = &instanceList.at(i);
  5571. if (data->zone_instance_type == SOLO_LOCKOUT_INSTANCE || data->zone_instance_type == GROUP_LOCKOUT_INSTANCE || data->zone_instance_type == RAID_LOCKOUT_INSTANCE)
  5572. ret.push_back(*data);
  5573. }
  5574. m_instanceList.releasereadlock(__FUNCTION__, __LINE__);
  5575. return ret;
  5576. }
  5577. vector<InstanceData> CharacterInstances::GetPersistentInstances() {
  5578. vector<InstanceData> ret;
  5579. m_instanceList.readlock(__FUNCTION__, __LINE__);
  5580. for (int32 i = 0; i < instanceList.size(); i++) {
  5581. InstanceData* data = &instanceList.at(i);
  5582. if (data->zone_instance_type == SOLO_PERSIST_INSTANCE || data->zone_instance_type == GROUP_PERSIST_INSTANCE || data->zone_instance_type == RAID_PERSIST_INSTANCE)
  5583. ret.push_back(*data);
  5584. }
  5585. m_instanceList.releasereadlock(__FUNCTION__, __LINE__);
  5586. return ret;
  5587. }
  5588. void CharacterInstances::ProcessInstanceTimers(Player* player) {
  5589. // Only need to check persistent instances here, lockout should be handled by zone shutting down
  5590. // Check instance id, if > 0 check timers, if timers expired set instance id to 0 and update the db `character_instance` to instance id 0,
  5591. // delete instance from `instances` if no more characters assigned to it
  5592. m_instanceList.readlock(__FUNCTION__, __LINE__);
  5593. for (int32 i = 0; i < instanceList.size(); i++) {
  5594. InstanceData* data = &instanceList.at(i);
  5595. // Check to see if we have a valid instance and if it is persistant
  5596. if (data->instance_id > 0) {
  5597. if (data->zone_instance_type == SOLO_PERSIST_INSTANCE || data->zone_instance_type == GROUP_PERSIST_INSTANCE || data->zone_instance_type == RAID_PERSIST_INSTANCE) {
  5598. // Check max duration (last success + success time)
  5599. // if the zone does not have a success lockout time, we should not apply this logic
  5600. if (data->success_lockout_time > 0 && (Timer::GetUnixTimeStamp() >= (data->last_success_timestamp + data->success_lockout_time))) {
  5601. // Max duration has passed, instance has expired lets remove the player from it,
  5602. // this will also delete the instace if all players have been removed from it
  5603. database.DeleteCharacterFromInstance(player->GetCharacterID(), data->instance_id);
  5604. data->instance_id = 0;
  5605. }
  5606. }
  5607. if (data->zone_instance_type == SOLO_LOCKOUT_INSTANCE || data->zone_instance_type == GROUP_LOCKOUT_INSTANCE || data->zone_instance_type == RAID_LOCKOUT_INSTANCE) {
  5608. // Need to check lockout instance ids to ensure they are still valid.
  5609. if (!database.VerifyInstanceID(player->GetCharacterID(), data->instance_id))
  5610. data->instance_id = 0;
  5611. }
  5612. }
  5613. }
  5614. m_instanceList.releasereadlock(__FUNCTION__, __LINE__);
  5615. /*for(int32 i=0;i<instanceList->size();i++)
  5616. {
  5617. bool valuesUpdated = false;
  5618. InstanceData data = instanceList->at(i);
  5619. if ( data.grant_reenter_time_left > 0 )
  5620. {
  5621. if ( ( data.grant_reenter_time_left - msDiff ) < 1 )
  5622. data.grant_reenter_time_left = 0;
  5623. else
  5624. data.grant_reenter_time_left -= msDiff;
  5625. valuesUpdated = true;
  5626. }
  5627. if ( data.grant_reset_time_left > 0 )
  5628. {
  5629. if ( ( data.grant_reset_time_left - msDiff ) < 1 )
  5630. data.grant_reset_time_left = 0;
  5631. else
  5632. data.grant_reset_time_left -= msDiff;
  5633. valuesUpdated = true;
  5634. }
  5635. if ( data.lockout_time > 0 )
  5636. {
  5637. if ( ( data.lockout_time - msDiff ) < 1 )
  5638. {
  5639. data.lockout_time = 0;
  5640. // this means that their timer ran out and we need to clear it from db and player
  5641. RemoveInstanceByInstanceID(data.instance_id);
  5642. database.DeleteCharacterFromInstance(player->GetCharacterID(),data.instance_id);
  5643. }
  5644. else
  5645. data.lockout_time -= msDiff;
  5646. valuesUpdated = true;
  5647. }
  5648. if ( valuesUpdated )
  5649. database.UpdateCharacterInstanceTimers(player->GetCharacterID(),data.instance_id,data.lockout_time,data.grant_reset_time_left,data.grant_reenter_time_left);
  5650. }*/
  5651. }
  5652. int32 CharacterInstances::GetInstanceCount() {
  5653. return instanceList.size();
  5654. }
  5655. void Player::SetPlayerAdventureClass(int8 new_class){
  5656. SetAdventureClass(new_class);
  5657. GetInfoStruct()->set_class1(classes.GetBaseClass(new_class));
  5658. GetInfoStruct()->set_class2(classes.GetSecondaryBaseClass(new_class));
  5659. GetInfoStruct()->set_class3(new_class);
  5660. charsheet_changed = true;
  5661. if(GetZone())
  5662. GetZone()->TriggerCharSheetTimer();
  5663. }
  5664. void Player::AddSkillBonus(int32 spell_id, int32 skill_id, float value) {
  5665. GetSkills()->AddSkillBonus(spell_id, skill_id, value);
  5666. }
  5667. SkillBonus* Player::GetSkillBonus(int32 spell_id) {
  5668. return GetSkills()->GetSkillBonus(spell_id);
  5669. }
  5670. void Player::RemoveSkillBonus(int32 spell_id) {
  5671. GetSkills()->RemoveSkillBonus(spell_id);
  5672. }
  5673. bool Player::HasFreeBankSlot() {
  5674. return item_list.HasFreeBankSlot();
  5675. }
  5676. int8 Player::FindFreeBankSlot() {
  5677. return item_list.FindFreeBankSlot();
  5678. }
  5679. void Player::AddTitle(sint32 title_id, const char *name, int8 prefix, bool save_needed){
  5680. Title* new_title = new Title;
  5681. new_title->SetID(title_id);
  5682. new_title->SetName(name);
  5683. new_title->SetPrefix(prefix);
  5684. new_title->SetSaveNeeded(save_needed);
  5685. player_titles_list.Add(new_title);
  5686. }
  5687. void Player::AddAAEntry(int16 template_id, int8 tab_id, int32 aa_id, int16 order,int8 treeid) {
  5688. }
  5689. void Player::AddLanguage(int32 id, const char *name, bool save_needed){
  5690. Skill* skill = master_skill_list.GetSkillByName(name);
  5691. if(skill && !GetSkills()->HasSkill(skill->skill_id)) {
  5692. AddSkill(skill->skill_id, 1, skill->max_val, true);
  5693. }
  5694. // Check to see if the player already has the language
  5695. if (HasLanguage(id))
  5696. return;
  5697. // Doesn't already have the language so add it
  5698. Language* new_language = new Language;
  5699. new_language->SetID(id);
  5700. new_language->SetName(name);
  5701. player_languages_list.Add(new_language);
  5702. if (save_needed)
  5703. database.SaveCharacterLang(GetCharacterID(), id);
  5704. }
  5705. bool Player::HasLanguage(int32 id){
  5706. list<Language*>* languages = player_languages_list.GetAllLanguages();
  5707. list<Language*>::iterator itr;
  5708. Language* language = 0;
  5709. bool ret = false;
  5710. for(itr = languages->begin(); itr != languages->end(); itr++){
  5711. language = *itr;
  5712. if(language->GetID() == id){
  5713. ret = true;
  5714. break;
  5715. }
  5716. }
  5717. return ret;
  5718. }
  5719. bool Player::HasLanguage(const char* name){
  5720. list<Language*>* languages = player_languages_list.GetAllLanguages();
  5721. list<Language*>::iterator itr;
  5722. Language* language = 0;
  5723. bool ret = false;
  5724. for(itr = languages->begin(); itr != languages->end(); itr++){
  5725. language = *itr;
  5726. if(!strncmp(language->GetName(), name, strlen(name))){
  5727. ret = true;
  5728. break;
  5729. }
  5730. }
  5731. return ret;
  5732. }
  5733. void Player::AddPassiveSpell(int32 id, int8 tier)
  5734. {
  5735. // Add the spell to the list of passives this player currently has
  5736. // list is used for quickly going over only the passive spells the
  5737. // player has instead of going through all their spells.
  5738. passive_spells.push_back(id);
  5739. Client* client = GetClient();
  5740. // Don not apply passives if the client is null, zoning, or reviving
  5741. if (client == NULL || client->IsZoning() || IsResurrecting())
  5742. return;
  5743. Spell* spell = 0;
  5744. spell = master_spell_list.GetSpell(id, tier);
  5745. if (spell) {
  5746. SpellProcess* spellProcess = 0;
  5747. // Get the current zones spell process
  5748. spellProcess = GetZone()->GetSpellProcess();
  5749. // Cast the spell, CastPassives() bypasses the spell queue
  5750. spellProcess->CastPassives(spell, this);
  5751. }
  5752. }
  5753. void Player::ApplyPassiveSpells()
  5754. {
  5755. Spell* spell = 0;
  5756. SpellBookEntry* spell2 = 0;
  5757. vector<int32>::iterator itr;
  5758. SpellProcess* spellProcess = 0;
  5759. spellProcess = GetZone()->GetSpellProcess();
  5760. for (itr = passive_spells.begin(); itr != passive_spells.end(); itr++)
  5761. {
  5762. spell2 = GetSpellBookSpell((*itr));
  5763. spell = master_spell_list.GetSpell(spell2->spell_id, spell2->tier);
  5764. if (spell) {
  5765. spellProcess->CastPassives(spell, this);
  5766. }
  5767. }
  5768. }
  5769. void Player::RemovePassive(int32 id, int8 tier, bool remove_from_list)
  5770. {
  5771. Spell* spell = 0;
  5772. spell = master_spell_list.GetSpell(id, tier);
  5773. if (spell) {
  5774. SpellProcess* spellProcess = 0;
  5775. spellProcess = GetZone()->GetSpellProcess();
  5776. spellProcess->CastPassives(spell, this, true);
  5777. if (remove_from_list) {
  5778. vector<int32>::iterator remove;
  5779. remove = find(passive_spells.begin(), passive_spells.end(), id);
  5780. if (remove != passive_spells.end())
  5781. passive_spells.erase(remove);
  5782. }
  5783. database.DeleteCharacterSpell(GetCharacterID(), spell->GetSpellID());
  5784. }
  5785. }
  5786. void Player::RemoveAllPassives()
  5787. {
  5788. vector<int32>::iterator itr;
  5789. for (itr = passive_spells.begin(); itr != passive_spells.end(); itr++)
  5790. RemoveSpellBookEntry((*itr), false);
  5791. passive_spells.clear();
  5792. }
  5793. void Player::ResetPetInfo() {
  5794. GetInfoStruct()->set_pet_id(0xFFFFFFFF);
  5795. GetInfoStruct()->set_pet_movement(0);
  5796. GetInfoStruct()->set_pet_behavior(0);
  5797. GetInfoStruct()->set_pet_health_pct(0.0f);
  5798. GetInfoStruct()->set_pet_power_pct(0.0f);
  5799. // Make sure the values get sent to the client
  5800. SetCharSheetChanged(true);
  5801. }
  5802. bool Player::HasRecipeBook(int32 recipe_id){
  5803. return recipebook_list.HasRecipeBook(recipe_id);
  5804. }
  5805. bool Player::DiscoveredLocation(int32 locationID) {
  5806. bool ret = false;
  5807. // No discovery type entry then return false
  5808. if (m_characterHistory.count(HISTORY_TYPE_DISCOVERY) == 0)
  5809. return false;
  5810. // Is a discovery type entry but not a location subtype return false
  5811. if (m_characterHistory[HISTORY_TYPE_DISCOVERY].count(HISTORY_SUBTYPE_LOCATION) == 0)
  5812. return false;
  5813. vector<HistoryData*>::iterator itr;
  5814. for (itr = m_characterHistory[HISTORY_TYPE_DISCOVERY][HISTORY_SUBTYPE_LOCATION].begin(); itr != m_characterHistory[HISTORY_TYPE_DISCOVERY][HISTORY_SUBTYPE_LOCATION].end(); itr++) {
  5815. if ((*itr)->Value == locationID) {
  5816. ret = true;
  5817. break;
  5818. }
  5819. }
  5820. return ret;
  5821. }
  5822. void Player::UpdatePlayerHistory(int8 type, int8 subtype, int32 value, int32 value2) {
  5823. switch (type) {
  5824. case HISTORY_TYPE_NONE:
  5825. HandleHistoryNone(subtype, value, value2);
  5826. break;
  5827. case HISTORY_TYPE_DEATH:
  5828. HandleHistoryDeath(subtype, value, value2);
  5829. break;
  5830. case HISTORY_TYPE_DISCOVERY:
  5831. HandleHistoryDiscovery(subtype, value, value2);
  5832. break;
  5833. case HISTORY_TYPE_XP:
  5834. HandleHistoryXP(subtype, value, value2);
  5835. break;
  5836. default:
  5837. // Not a valid history event so return out before trying to save into the db
  5838. return;
  5839. }
  5840. }
  5841. void Player::HandleHistoryNone(int8 subtype, int32 value, int32 value2) {
  5842. }
  5843. void Player::HandleHistoryDeath(int8 subtype, int32 value, int32 value2) {
  5844. }
  5845. void Player::HandleHistoryDiscovery(int8 subtype, int32 value, int32 value2) {
  5846. switch (subtype) {
  5847. case HISTORY_SUBTYPE_NONE:
  5848. break;
  5849. case HISTORY_SUBTYPE_ADVENTURE:
  5850. break;
  5851. case HISTORY_SUBTYPE_TRADESKILL:
  5852. break;
  5853. case HISTORY_SUBTYPE_QUEST:
  5854. break;
  5855. case HISTORY_SUBTYPE_AA:
  5856. break;
  5857. case HISTORY_SUBTYPE_ITEM:
  5858. break;
  5859. case HISTORY_SUBTYPE_LOCATION: {
  5860. HistoryData* hd = new HistoryData;
  5861. hd->Value = value;
  5862. hd->Value2 = value2;
  5863. hd->EventDate = Timer::GetUnixTimeStamp();
  5864. strcpy(hd->Location, GetZone()->GetZoneName());
  5865. hd->needs_save = true;
  5866. m_characterHistory[HISTORY_TYPE_DISCOVERY][HISTORY_SUBTYPE_LOCATION].push_back(hd);
  5867. break;
  5868. }
  5869. default:
  5870. // Invalid subtype, default to NONE
  5871. break;
  5872. }
  5873. }
  5874. void Player::HandleHistoryXP(int8 subtype, int32 value, int32 value2) {
  5875. switch (subtype) {
  5876. case HISTORY_SUBTYPE_NONE:
  5877. break;
  5878. case HISTORY_SUBTYPE_ADVENTURE: {
  5879. HistoryData* hd = new HistoryData;
  5880. hd->Value = value;
  5881. hd->Value2 = value2;
  5882. hd->EventDate = Timer::GetUnixTimeStamp();
  5883. strcpy(hd->Location, GetZone()->GetZoneName());
  5884. hd->needs_save = true;
  5885. m_characterHistory[HISTORY_TYPE_XP][HISTORY_SUBTYPE_ADVENTURE].push_back(hd);
  5886. }
  5887. break;
  5888. case HISTORY_SUBTYPE_TRADESKILL:
  5889. break;
  5890. case HISTORY_SUBTYPE_QUEST:
  5891. break;
  5892. case HISTORY_SUBTYPE_AA:
  5893. break;
  5894. case HISTORY_SUBTYPE_ITEM:
  5895. break;
  5896. case HISTORY_SUBTYPE_LOCATION:
  5897. break;
  5898. default:
  5899. // Invalid subtype, default to NONE
  5900. break;
  5901. }
  5902. }
  5903. void Player::LoadPlayerHistory(int8 type, int8 subtype, HistoryData* hd) {
  5904. m_characterHistory[type][subtype].push_back(hd);
  5905. }
  5906. void Player::SaveHistory() {
  5907. LogWrite(PLAYER__DEBUG, 0, "Player", "Saving History for Player: '%s'", GetName());
  5908. map<int8, map<int8, vector<HistoryData*> > >::iterator itr;
  5909. map<int8, vector<HistoryData*> >::iterator itr2;
  5910. vector<HistoryData*>::iterator itr3;
  5911. for (itr = m_characterHistory.begin(); itr != m_characterHistory.end(); itr++) {
  5912. for (itr2 = itr->second.begin(); itr2 != itr->second.end(); itr2++) {
  5913. for (itr3 = itr2->second.begin(); itr3 != itr2->second.end(); itr3++) {
  5914. if((*itr3)->needs_save) {
  5915. database.SaveCharacterHistory(this, itr->first, itr2->first, (*itr3)->Value, (*itr3)->Value2, (*itr3)->Location, (*itr3)->EventDate);
  5916. (*itr3)->needs_save = false;
  5917. }
  5918. }
  5919. }
  5920. }
  5921. }
  5922. void Player::InitXPTable() {
  5923. int i = 2;
  5924. while (i >= 2 && i <= 95) {
  5925. m_levelXPReq[i] = database.GetMysqlExpCurve(i);
  5926. i++;
  5927. }
  5928. }
  5929. void Player::SendQuestRequiredSpawns(int32 quest_id){
  5930. bool locked = true;
  5931. m_playerSpawnQuestsRequired.readlock(__FUNCTION__, __LINE__);
  5932. if (player_spawn_quests_required.size() > 0 ) {
  5933. ZoneServer* zone = GetZone();
  5934. Client* client = GetClient();
  5935. if (!client){
  5936. m_playerSpawnQuestsRequired.releasereadlock(__FUNCTION__, __LINE__);
  5937. return;
  5938. }
  5939. int xxx = player_spawn_quests_required.count(quest_id);
  5940. if (player_spawn_quests_required.count(quest_id) > 0){
  5941. vector<int32> spawns = *player_spawn_quests_required[quest_id];
  5942. m_playerSpawnQuestsRequired.releasereadlock(__FUNCTION__, __LINE__);
  5943. Spawn* spawn = 0;
  5944. vector<int32>::iterator itr;
  5945. for (itr = spawns.begin(); itr != spawns.end();){
  5946. spawn = zone->GetSpawnByID(*itr);
  5947. if (spawn)
  5948. zone->SendSpawnChanges(spawn, client, false, true);
  5949. else {
  5950. itr = spawns.erase(itr);
  5951. continue;
  5952. }
  5953. itr++;
  5954. }
  5955. locked = false;
  5956. }
  5957. }
  5958. if (locked)
  5959. m_playerSpawnQuestsRequired.releasereadlock(__FUNCTION__, __LINE__);
  5960. }
  5961. void Player::SendHistoryRequiredSpawns(int32 event_id){
  5962. bool locked = true;
  5963. m_playerSpawnHistoryRequired.readlock(__FUNCTION__, __LINE__);
  5964. if (player_spawn_history_required.size() > 0) {
  5965. ZoneServer* zone = GetZone();
  5966. Client* client = GetClient();
  5967. if (!client){
  5968. m_playerSpawnHistoryRequired.releasereadlock(__FUNCTION__, __LINE__);
  5969. return;
  5970. }
  5971. if (player_spawn_history_required.count(event_id) > 0){
  5972. vector<int32> spawns = *player_spawn_history_required[event_id];
  5973. m_playerSpawnHistoryRequired.releasereadlock(__FUNCTION__, __LINE__);
  5974. Spawn* spawn = 0;
  5975. vector<int32>::iterator itr;
  5976. for (itr = spawns.begin(); itr != spawns.end();){
  5977. spawn = zone->GetSpawnByID(*itr);
  5978. if (spawn)
  5979. zone->SendSpawnChanges(spawn, client, false, true);
  5980. else {
  5981. itr = spawns.erase(itr);
  5982. continue;
  5983. }
  5984. itr++;
  5985. }
  5986. locked = false;
  5987. }
  5988. }
  5989. if (locked)
  5990. m_playerSpawnHistoryRequired.releasereadlock(__FUNCTION__, __LINE__);
  5991. }
  5992. void Player::AddQuestRequiredSpawn(Spawn* spawn, int32 quest_id){
  5993. if(!spawn || !quest_id)
  5994. return;
  5995. m_playerSpawnQuestsRequired.writelock(__FUNCTION__, __LINE__);
  5996. if(player_spawn_quests_required.count(quest_id) == 0)
  5997. player_spawn_quests_required[quest_id] = new vector<int32>;
  5998. vector<int32>* quest_spawns = player_spawn_quests_required[quest_id];
  5999. int32 current_spawn = 0;
  6000. for(int32 i=0;i<quest_spawns->size();i++){
  6001. current_spawn = quest_spawns->at(i);
  6002. if (current_spawn == spawn->GetID()){
  6003. m_playerSpawnQuestsRequired.releasewritelock(__FUNCTION__, __LINE__);
  6004. return;
  6005. }
  6006. }
  6007. player_spawn_quests_required[quest_id]->push_back(spawn->GetID());
  6008. m_playerSpawnQuestsRequired.releasewritelock(__FUNCTION__, __LINE__);
  6009. }
  6010. void Player::AddHistoryRequiredSpawn(Spawn* spawn, int32 event_id){
  6011. if (!spawn || !event_id)
  6012. return;
  6013. m_playerSpawnHistoryRequired.writelock(__FUNCTION__, __LINE__);
  6014. if (player_spawn_history_required.count(event_id) == 0)
  6015. player_spawn_history_required[event_id] = new vector<int32>;
  6016. vector<int32>* history_spawns = player_spawn_history_required[event_id];
  6017. vector<int32>::iterator itr = find(history_spawns->begin(), history_spawns->end(), spawn->GetID());
  6018. if (itr == history_spawns->end())
  6019. history_spawns->push_back(spawn->GetID());
  6020. m_playerSpawnHistoryRequired.releasewritelock(__FUNCTION__, __LINE__);
  6021. }
  6022. int32 PlayerInfo::GetBoatSpawn(){
  6023. return boat_spawn;
  6024. }
  6025. void PlayerInfo::SetBoatSpawn(Spawn* spawn){
  6026. if(spawn)
  6027. boat_spawn = spawn->GetID();
  6028. else
  6029. boat_spawn = 0;
  6030. }
  6031. void PlayerInfo::RemoveOldPackets(){
  6032. safe_delete_array(changes);
  6033. safe_delete_array(orig_packet);
  6034. safe_delete_array(pet_changes);
  6035. safe_delete_array(pet_orig_packet);
  6036. changes = 0;
  6037. orig_packet = 0;
  6038. pet_changes = 0;
  6039. pet_orig_packet = 0;
  6040. }
  6041. PlayerControlFlags::PlayerControlFlags(){
  6042. MControlFlags.SetName("PlayerControlFlags::MControlFlags");
  6043. MFlagChanges.SetName("PlayerControlFlags::MFlagChanges");
  6044. flags_changed = false;
  6045. flag_changes.clear();
  6046. current_flags.clear();
  6047. }
  6048. PlayerControlFlags::~PlayerControlFlags(){
  6049. flag_changes.clear();
  6050. current_flags.clear();
  6051. }
  6052. bool PlayerControlFlags::ControlFlagsChanged(){
  6053. bool ret;
  6054. MFlagChanges.writelock(__FUNCTION__, __LINE__);
  6055. ret = flags_changed;
  6056. MFlagChanges.releasewritelock(__FUNCTION__, __LINE__);
  6057. return ret;
  6058. }
  6059. void PlayerControlFlags::SetPlayerControlFlag(int8 param, int8 param_value, bool is_active){
  6060. if (!param || !param_value)
  6061. return;
  6062. bool active_changed = false;
  6063. MControlFlags.writelock(__FUNCTION__, __LINE__);
  6064. active_changed = (current_flags[param][param_value] != is_active);
  6065. if (active_changed){
  6066. current_flags[param][param_value] = is_active;
  6067. MFlagChanges.writelock(__FUNCTION__, __LINE__);
  6068. flag_changes[param][param_value] = is_active ? 1 : 0;
  6069. flags_changed = true;
  6070. MFlagChanges.releasewritelock(__FUNCTION__, __LINE__);
  6071. }
  6072. MControlFlags.releasewritelock(__FUNCTION__, __LINE__);
  6073. }
  6074. void PlayerControlFlags::SendControlFlagUpdates(Client* client){
  6075. if (!client)
  6076. return;
  6077. map<int8, int8>* ptr = 0;
  6078. map<int8, map<int8, int8> >::iterator itr;
  6079. map<int8, int8>::iterator itr2;
  6080. MFlagChanges.writelock(__FUNCTION__, __LINE__);
  6081. for (itr = flag_changes.begin(); itr != flag_changes.end(); itr++){
  6082. ptr = &itr->second;
  6083. for (itr2 = ptr->begin(); itr2 != ptr->end(); itr2++){
  6084. int32 param = itr2->first;
  6085. if(client->GetVersion() <= 561) {
  6086. if(itr->first == 1) { // first set of flags DoF only supports these
  6087. bool skip = false;
  6088. switch(itr2->first) {
  6089. case 1: // flymode for DoF
  6090. case 2: // no clip mode for DoF
  6091. case 4: // we don't know
  6092. case 32: { // safe fall (DoF is low gravity for this parameter)
  6093. skip = true;
  6094. break;
  6095. }
  6096. }
  6097. if(skip) {
  6098. continue;
  6099. }
  6100. }
  6101. bool bypassFlag = true;
  6102. // remap control effects to old DoF from AoM
  6103. switch(itr->first) {
  6104. case 4: {
  6105. if(itr2->first == 64) { // stun
  6106. ClientPacketFunctions::SendServerControlFlagsClassic(client, 8, itr2->second);
  6107. param = 16;
  6108. bypassFlag = false;
  6109. }
  6110. break;
  6111. }
  6112. }
  6113. if(itr->first > 1 && bypassFlag) {
  6114. continue; // we don't support flag sets higher than 1 for DoF
  6115. }
  6116. ClientPacketFunctions::SendServerControlFlagsClassic(client, param, itr2->second);
  6117. }
  6118. else {
  6119. ClientPacketFunctions::SendServerControlFlags(client, itr->first, itr2->first, itr2->second);
  6120. }
  6121. }
  6122. }
  6123. flag_changes.clear();
  6124. flags_changed = false;
  6125. MFlagChanges.releasewritelock(__FUNCTION__, __LINE__);
  6126. }
  6127. bool Player::ControlFlagsChanged(){
  6128. return control_flags.ControlFlagsChanged();
  6129. }
  6130. void Player::SetPlayerControlFlag(int8 param, int8 param_value, bool is_active){
  6131. control_flags.SetPlayerControlFlag(param, param_value, is_active);
  6132. }
  6133. void Player::SendControlFlagUpdates(Client* client){
  6134. control_flags.SendControlFlagUpdates(client);
  6135. }
  6136. void Player::LoadLUAHistory(int32 event_id, LUAHistory* history) {
  6137. mLUAHistory.writelock();
  6138. if (m_charLuaHistory.count(event_id) > 0) {
  6139. LogWrite(PLAYER__ERROR, 0, "Player", "Attempted to added a dupicate event (%u) to character LUA history", event_id);
  6140. safe_delete(history);
  6141. mLUAHistory.releasewritelock();
  6142. return;
  6143. }
  6144. m_charLuaHistory.insert(make_pair(event_id,history));
  6145. mLUAHistory.releasewritelock();
  6146. }
  6147. void Player::SaveLUAHistory() {
  6148. mLUAHistory.readlock();
  6149. LogWrite(PLAYER__DEBUG, 0, "Player", "Saving LUA History for Player: '%s'", GetName());
  6150. map<int32, LUAHistory*>::iterator itr;
  6151. for (itr = m_charLuaHistory.begin(); itr != m_charLuaHistory.end(); itr++) {
  6152. if (itr->second->SaveNeeded) {
  6153. database.SaveCharacterLUAHistory(this, itr->first, itr->second->Value, itr->second->Value2);
  6154. itr->second->SaveNeeded = false;
  6155. }
  6156. }
  6157. mLUAHistory.releasereadlock();
  6158. }
  6159. void Player::UpdateLUAHistory(int32 event_id, int32 value, int32 value2) {
  6160. mLUAHistory.writelock();
  6161. LUAHistory* hd = 0;
  6162. if (m_charLuaHistory.count(event_id) > 0)
  6163. hd = m_charLuaHistory[event_id];
  6164. else {
  6165. hd = new LUAHistory;
  6166. m_charLuaHistory.insert(make_pair(event_id,hd));
  6167. }
  6168. hd->Value = value;
  6169. hd->Value2 = value2;
  6170. hd->SaveNeeded = true;
  6171. mLUAHistory.releasewritelock();
  6172. // release the mLUAHistory lock, we will maintain a readlock to avoid any further writes until we complete SendHistoryRequiredSpawns
  6173. // through Spawn::SendSpawnChanges -> Spawn::InitializeVisPacketData -> Spawn::MeetsSpawnAccessRequirements-> Player::GetLUAHistory (this was causing a deadlock)
  6174. mLUAHistory.readlock();
  6175. SendHistoryRequiredSpawns(event_id);
  6176. mLUAHistory.releasereadlock();
  6177. }
  6178. LUAHistory* Player::GetLUAHistory(int32 event_id) {
  6179. LUAHistory* ret = 0;
  6180. mLUAHistory.readlock();
  6181. if (m_charLuaHistory.count(event_id) > 0)
  6182. ret = m_charLuaHistory[event_id];
  6183. mLUAHistory.releasereadlock();
  6184. return ret;
  6185. }
  6186. bool Player::CanSeeInvis(Entity* target)
  6187. {
  6188. if (!target->IsStealthed() && !target->IsInvis())
  6189. return true;
  6190. if (target->IsStealthed() && HasSeeHideSpell())
  6191. return true;
  6192. else if (target->IsInvis() && HasSeeInvisSpell())
  6193. return true;
  6194. sint32 radius = rule_manager.GetZoneRule(GetZoneID(), R_PVP, InvisPlayerDiscoveryRange)->GetSInt32();
  6195. if (radius == 0) // radius of 0 is always seen
  6196. return true;
  6197. // radius of -1 is never seen except through items/spells, radius > -1 means we will show the player if they get into the inner radius
  6198. else if (radius > -1 && this->GetDistance((Spawn*)target) < (float)radius)
  6199. return true;
  6200. // TODO: Implement See Invis Spells! http://cutpon.com:3000/devn00b/EQ2EMu/issues/43
  6201. Item* item = 0;
  6202. vector<Item*>* equipped_list = GetEquippedItemList();
  6203. bool seeInvis = false;
  6204. bool seeStealth = false;
  6205. for (int32 i = 0; i < equipped_list->size(); i++)
  6206. {
  6207. item = equipped_list->at(i);
  6208. seeInvis = item->HasStat(ITEM_STAT_SEEINVIS);
  6209. seeStealth = item->HasStat(ITEM_STAT_SEESTEALTH);
  6210. if (target->IsStealthed() && seeStealth)
  6211. return true;
  6212. else if (target->IsInvis() && seeInvis)
  6213. return true;
  6214. }
  6215. return false;
  6216. }
  6217. // returns true if we need to update target info due to see invis status change
  6218. bool Player::CheckChangeInvisHistory(Entity* target)
  6219. {
  6220. std::map<int32, bool>::iterator it;
  6221. it = target_invis_history.find(target->GetID());
  6222. if (it != target_invis_history.end())
  6223. {
  6224. //canSeeStatus
  6225. if (it->second)
  6226. {
  6227. if (!this->CanSeeInvis(target))
  6228. {
  6229. UpdateTargetInvisHistory(target->GetID(), false);
  6230. return true;
  6231. }
  6232. else
  6233. return false;
  6234. }
  6235. else
  6236. {
  6237. if (this->CanSeeInvis(target))
  6238. {
  6239. UpdateTargetInvisHistory(target->GetID(), true);
  6240. return true;
  6241. }
  6242. else
  6243. return false;
  6244. }
  6245. }
  6246. if (!this->CanSeeInvis(target))
  6247. UpdateTargetInvisHistory(target->GetID(), false);
  6248. else
  6249. UpdateTargetInvisHistory(target->GetID(), true);
  6250. return true;
  6251. }
  6252. void Player::UpdateTargetInvisHistory(int32 targetID, bool canSeeStatus)
  6253. {
  6254. target_invis_history[targetID] = canSeeStatus;
  6255. }
  6256. void Player::RemoveTargetInvisHistory(int32 targetID)
  6257. {
  6258. target_invis_history.erase(targetID);
  6259. }
  6260. int16 Player::GetNextSpawnIndex(Spawn* spawn, bool set_lock)
  6261. {
  6262. if(set_lock)
  6263. index_mutex.writelock(__FUNCTION__, __LINE__);
  6264. int16 next_index = 0;
  6265. int16 max_count = 0;
  6266. bool not_found = true;
  6267. do {
  6268. next_index = (spawn_index++);
  6269. max_count++;
  6270. if(max_count > 0xFFFE) {
  6271. LogWrite(PLAYER__ERROR, 0, "Player", "%s: This is bad we ran out of spawn indexes!", GetName());
  6272. break;
  6273. }
  6274. if(next_index == 1 && spawn != this) { // only self can occupy index 1
  6275. continue;
  6276. }
  6277. if(next_index == 0 || next_index == 255) { // avoid 0 and overloads (255)
  6278. continue;
  6279. }
  6280. Spawn* tmp_spawn = nullptr;
  6281. if(player_spawn_id_map.count(next_index) > 0)
  6282. tmp_spawn = player_spawn_id_map[next_index];
  6283. if(tmp_spawn && tmp_spawn != spawn) { // spawn index already taken and it is not this spawn
  6284. continue;
  6285. }
  6286. not_found = false;
  6287. }
  6288. while(not_found);
  6289. if(set_lock)
  6290. index_mutex.releasewritelock(__FUNCTION__, __LINE__);
  6291. return next_index;
  6292. }
  6293. bool Player::SetSpawnMap(Spawn* spawn)
  6294. {
  6295. if(!client->GetPlayer()->SetSpawnSentState(spawn, SpawnState::SPAWN_STATE_SENDING)) {
  6296. return false;
  6297. }
  6298. index_mutex.writelock(__FUNCTION__, __LINE__);
  6299. int32 tmp_id = GetNextSpawnIndex(spawn, false);
  6300. player_spawn_id_map[tmp_id] = spawn;
  6301. if(player_spawn_reverse_id_map.count(spawn))
  6302. player_spawn_reverse_id_map.erase(spawn);
  6303. player_spawn_reverse_id_map.insert(make_pair(spawn,tmp_id));
  6304. index_mutex.releasewritelock(__FUNCTION__, __LINE__);
  6305. return true;
  6306. }
  6307. int16 Player::SetSpawnMapAndIndex(Spawn* spawn)
  6308. {
  6309. index_mutex.writelock(__FUNCTION__, __LINE__);
  6310. int32 new_index = GetNextSpawnIndex(spawn, false);
  6311. player_spawn_id_map[new_index] = spawn;
  6312. player_spawn_reverse_id_map[spawn] = new_index;
  6313. index_mutex.releasewritelock(__FUNCTION__, __LINE__);
  6314. return new_index;
  6315. }
  6316. NPC* Player::InstantiateSpiritShard(float origX, float origY, float origZ, float origHeading, int32 origGridID, ZoneServer* origZone)
  6317. {
  6318. NPC* npc = new NPC();
  6319. string newName(GetName());
  6320. newName.append("'s spirit shard");
  6321. strcpy(npc->appearance.name, newName.c_str());
  6322. /*vector<EntityCommand*>* primary_command_list = zone->GetEntityCommandList(result.GetInt32(9));
  6323. vector<EntityCommand*>* secondary_command_list = zone->GetEntityCommandList(result.GetInt32(10));
  6324. if(primary_command_list){
  6325. npc->SetPrimaryCommands(primary_command_list);
  6326. npc->primary_command_list_id = result.GetInt32(9);
  6327. }
  6328. if(secondary_command_list){
  6329. npc->SetSecondaryCommands(secondary_command_list);
  6330. npc->secondary_command_list_id = result.GetInt32(10);
  6331. }*/
  6332. npc->appearance.level = GetLevel();
  6333. npc->appearance.race = GetRace();
  6334. npc->appearance.gender = GetGender();
  6335. npc->appearance.adventure_class = GetAdventureClass();
  6336. npc->appearance.model_type = GetModelType();
  6337. npc->appearance.soga_model_type = GetSogaModelType();
  6338. npc->appearance.display_name = 1;
  6339. npc->features.hair_type = GetHairType();
  6340. npc->features.hair_face_type = GetFacialHairType();
  6341. npc->features.wing_type = GetWingType();
  6342. npc->features.chest_type = GetChestType();
  6343. npc->features.legs_type = GetLegsType();
  6344. npc->features.soga_hair_type = GetSogaHairType();
  6345. npc->features.soga_hair_face_type = GetSogaFacialHairType();
  6346. npc->appearance.attackable = 0;
  6347. npc->appearance.show_level = 1;
  6348. npc->appearance.targetable = 1;
  6349. npc->appearance.show_command_icon = 1;
  6350. npc->appearance.display_hand_icon = 0;
  6351. npc->appearance.hide_hood = GetHideHood();
  6352. npc->size = GetSize();
  6353. npc->appearance.pos.collision_radius = appearance.pos.collision_radius;
  6354. npc->appearance.action_state = appearance.action_state;
  6355. npc->appearance.visual_state = 6193; // ghostly look
  6356. npc->appearance.mood_state = appearance.mood_state;
  6357. npc->appearance.emote_state = appearance.emote_state;
  6358. npc->appearance.pos.state = appearance.pos.state;
  6359. npc->appearance.activity_status = appearance.activity_status;
  6360. strncpy(npc->appearance.sub_title, appearance.sub_title, sizeof(npc->appearance.sub_title));
  6361. npc->SetPrefixTitle(GetPrefixTitle());
  6362. npc->SetSuffixTitle(GetSuffixTitle());
  6363. npc->SetLastName(GetLastName());
  6364. npc->SetX(origX);
  6365. npc->SetY(origY);
  6366. npc->SetZ(origZ);
  6367. npc->SetHeading(origHeading);
  6368. npc->SetSpawnOrigX(origX);
  6369. npc->SetSpawnOrigY(origY);
  6370. npc->SetSpawnOrigZ(origZ);
  6371. npc->SetSpawnOrigHeading(origHeading);
  6372. npc->SetLocation(origGridID);
  6373. npc->SetAlive(false);
  6374. const char* script = rule_manager.GetGlobalRule(R_Combat, SpiritShardSpawnScript)->GetString();
  6375. int32 dbid = database.CreateSpiritShard(newName.c_str(), GetLevel(), GetRace(), GetGender(), GetAdventureClass(), GetModelType(), GetSogaModelType(),
  6376. GetHairType(), GetFacialHairType(), GetWingType(), GetChestType(), GetLegsType(), GetSogaHairType(), GetSogaFacialHairType(), GetHideHood(),
  6377. GetSize(), npc->appearance.pos.collision_radius, npc->appearance.action_state, npc->appearance.visual_state, npc->appearance.mood_state,
  6378. npc->appearance.emote_state, npc->appearance.pos.state, npc->appearance.activity_status, npc->appearance.sub_title, GetPrefixTitle(), GetSuffixTitle(),
  6379. GetLastName(), origX, origY, origZ, origHeading, origGridID, GetCharacterID(), origZone->GetZoneID(), origZone->GetInstanceID());
  6380. npc->SetShardID(dbid);
  6381. npc->SetShardCharID(GetCharacterID());
  6382. npc->SetShardCreatedTimestamp(Timer::GetCurrentTime2());
  6383. if(script)
  6384. npc->SetSpawnScript(script);
  6385. return npc;
  6386. }
  6387. void Player::SaveSpellEffects()
  6388. {
  6389. if(stop_save_spell_effects)
  6390. {
  6391. LogWrite(PLAYER__WARNING, 0, "Player", "SaveSpellEffects called while player constructing / deconstructing!");
  6392. return;
  6393. }
  6394. SpellProcess* spellProcess = 0;
  6395. // Get the current zones spell process
  6396. spellProcess = GetZone()->GetSpellProcess();
  6397. Query savedEffects;
  6398. savedEffects.AddQueryAsync(GetCharacterID(), &database, Q_DELETE, "delete from character_spell_effects where charid=%u", GetCharacterID());
  6399. savedEffects.AddQueryAsync(GetCharacterID(), &database, Q_DELETE, "delete from character_spell_effect_targets where caster_char_id=%u", GetCharacterID());
  6400. InfoStruct* info = GetInfoStruct();
  6401. MSpellEffects.readlock(__FUNCTION__, __LINE__);
  6402. MMaintainedSpells.readlock(__FUNCTION__, __LINE__);
  6403. for(int i = 0; i < 45; i++) {
  6404. if(info->spell_effects[i].spell_id != 0xFFFFFFFF)
  6405. {
  6406. Spawn* spawn = nullptr;
  6407. int32 target_char_id = 0;
  6408. if(info->spell_effects[i].spell->initial_target_char_id != 0)
  6409. target_char_id = info->spell_effects[i].spell->initial_target_char_id;
  6410. else if((spawn = GetZone()->GetSpawnByID(info->spell_effects[i].spell->initial_target)) != nullptr && spawn->IsPlayer())
  6411. target_char_id = ((Player*)spawn)->GetCharacterID();
  6412. int32 timestamp = 0xFFFFFFFF;
  6413. if(info->spell_effects[i].spell->spell->GetSpellData() && !info->spell_effects[i].spell->spell->GetSpellData()->duration_until_cancel)
  6414. timestamp = info->spell_effects[i].expire_timestamp - Timer::GetCurrentTime2();
  6415. int32 caster_char_id = info->spell_effects[i].spell->initial_caster_char_id;
  6416. if(caster_char_id == 0)
  6417. continue;
  6418. savedEffects.AddQueryAsync(GetCharacterID(), &database, Q_INSERT,
  6419. "insert into character_spell_effects (name, caster_char_id, target_char_id, target_type, db_effect_type, spell_id, effect_slot, slot_pos, icon, icon_backdrop, conc_used, tier, total_time, expire_timestamp, lua_file, custom_spell, charid, damage_remaining, effect_bitmask, num_triggers, had_triggers, cancel_after_triggers, crit, last_spellattack_hit, interrupted, resisted, has_damaged, custom_function) values ('%s', %u, %u, %u, %u, %u, %u, %u, %u, %u, %u, %u, %f, %u, '%s', %u, %u, %u, %u, %u, %u, %u, %u, %u, %u, %u, %u, '%s')",
  6420. database.getSafeEscapeString(info->spell_effects[i].spell->spell->GetName()).c_str(), caster_char_id,
  6421. target_char_id, 0 /*no target_type for spell_effects*/, DB_TYPE_SPELLEFFECTS /* db_effect_type for spell_effects */, info->spell_effects[i].spell->spell->IsCopiedSpell() ? info->spell_effects[i].spell->spell->GetSpellData()->inherited_spell_id : info->spell_effects[i].spell_id, i, info->spell_effects[i].spell->slot_pos,
  6422. info->spell_effects[i].icon, info->spell_effects[i].icon_backdrop, 0 /* no conc_used for spell_effects */, info->spell_effects[i].tier,
  6423. info->spell_effects[i].total_time, timestamp, database.getSafeEscapeString(info->spell_effects[i].spell->file_name.c_str()).c_str(), info->spell_effects[i].spell->spell->IsCopiedSpell(), GetCharacterID(),
  6424. info->spell_effects[i].spell->damage_remaining, info->spell_effects[i].spell->effect_bitmask, info->spell_effects[i].spell->num_triggers, info->spell_effects[i].spell->had_triggers, info->spell_effects[i].spell->cancel_after_all_triggers,
  6425. info->spell_effects[i].spell->crit, info->spell_effects[i].spell->last_spellattack_hit, info->spell_effects[i].spell->interrupted, info->spell_effects[i].spell->resisted, info->spell_effects[i].spell->has_damaged, (info->maintained_effects[i].expire_timestamp) == 0xFFFFFFFF ? "" : database.getSafeEscapeString(spellProcess->SpellScriptTimerCustomFunction(info->spell_effects[i].spell).c_str()).c_str());
  6426. }
  6427. if (i < NUM_MAINTAINED_EFFECTS && info->maintained_effects[i].spell_id != 0xFFFFFFFF){
  6428. Spawn* spawn = GetZone()->GetSpawnByID(info->maintained_effects[i].spell->initial_target);
  6429. int32 target_char_id = 0;
  6430. if(info->maintained_effects[i].spell->initial_target_char_id != 0)
  6431. target_char_id = info->maintained_effects[i].spell->initial_target_char_id;
  6432. else if(!info->maintained_effects[i].spell->initial_target)
  6433. target_char_id = GetCharacterID();
  6434. else if(spawn && spawn->IsPlayer())
  6435. target_char_id = ((Player*)spawn)->GetCharacterID();
  6436. else if (spawn && spawn->IsPet() && ((Entity*)spawn)->GetOwner() == (Entity*)this)
  6437. target_char_id = 0xFFFFFFFF;
  6438. int32 caster_char_id = info->maintained_effects[i].spell->initial_caster_char_id;
  6439. int32 timestamp = 0xFFFFFFFF;
  6440. if(info->maintained_effects[i].spell->spell->GetSpellData() && !info->maintained_effects[i].spell->spell->GetSpellData()->duration_until_cancel)
  6441. timestamp = info->maintained_effects[i].expire_timestamp - Timer::GetCurrentTime2();
  6442. savedEffects.AddQueryAsync(GetCharacterID(), &database, Q_INSERT,
  6443. "insert into character_spell_effects (name, caster_char_id, target_char_id, target_type, db_effect_type, spell_id, effect_slot, slot_pos, icon, icon_backdrop, conc_used, tier, total_time, expire_timestamp, lua_file, custom_spell, charid, damage_remaining, effect_bitmask, num_triggers, had_triggers, cancel_after_triggers, crit, last_spellattack_hit, interrupted, resisted, has_damaged, custom_function) values ('%s', %u, %u, %u, %u, %u, %u, %u, %u, %u, %u, %u, %f, %u, '%s', %u, %u, %u, %u, %u, %u, %u, %u, %u, %u, %u, %u, '%s')",
  6444. database.getSafeEscapeString(info->maintained_effects[i].name).c_str(), caster_char_id, target_char_id, info->maintained_effects[i].target_type, DB_TYPE_MAINTAINEDEFFECTS /* db_effect_type for maintained_effects */, info->maintained_effects[i].spell->spell->IsCopiedSpell() ? info->maintained_effects[i].spell->spell->GetSpellData()->inherited_spell_id : info->maintained_effects[i].spell_id, i, info->maintained_effects[i].slot_pos,
  6445. info->maintained_effects[i].icon, info->maintained_effects[i].icon_backdrop, info->maintained_effects[i].conc_used, info->maintained_effects[i].tier,
  6446. info->maintained_effects[i].total_time, timestamp, database.getSafeEscapeString(info->maintained_effects[i].spell->file_name.c_str()).c_str(), info->maintained_effects[i].spell->spell->IsCopiedSpell(), GetCharacterID(),
  6447. info->maintained_effects[i].spell->damage_remaining, info->maintained_effects[i].spell->effect_bitmask, info->maintained_effects[i].spell->num_triggers, info->maintained_effects[i].spell->had_triggers, info->maintained_effects[i].spell->cancel_after_all_triggers,
  6448. info->maintained_effects[i].spell->crit, info->maintained_effects[i].spell->last_spellattack_hit, info->maintained_effects[i].spell->interrupted, info->maintained_effects[i].spell->resisted, info->maintained_effects[i].spell->has_damaged, (info->maintained_effects[i].expire_timestamp) == 0xFFFFFFFF ? "" : database.getSafeEscapeString(spellProcess->SpellScriptTimerCustomFunction(info->maintained_effects[i].spell).c_str()).c_str());
  6449. info->maintained_effects[i].spell->MSpellTargets.readlock(__FUNCTION__, __LINE__);
  6450. std::string insertTargets = string("insert into character_spell_effect_targets (caster_char_id, target_char_id, target_type, db_effect_type, spell_id, effect_slot, slot_pos) values ");
  6451. bool firstTarget = true;
  6452. map<Spawn*, int8> targetsInserted;
  6453. for (int8 t = 0; t < info->maintained_effects[i].spell->targets.size(); t++) {
  6454. int32 spawn_id = info->maintained_effects[i].spell->targets.at(t);
  6455. Spawn* spawn = GetZone()->GetSpawnByID(spawn_id);
  6456. LogWrite(SPELL__DEBUG, 0, "Spell", "%s has target %u to identify for spell %s", GetName(), spawn_id, info->maintained_effects[i].spell->spell->GetName());
  6457. if(spawn && (spawn->IsPlayer() || spawn->IsPet()))
  6458. {
  6459. int32 tmpCharID = 0;
  6460. int8 type = 0;
  6461. if(targetsInserted.find(spawn) != targetsInserted.end())
  6462. continue;
  6463. if(spawn->IsPlayer())
  6464. tmpCharID = ((Player*)spawn)->GetCharacterID();
  6465. else if (spawn->IsPet() && ((Entity*)spawn)->GetOwner() == (Entity*)this)
  6466. {
  6467. tmpCharID = 0xFFFFFFFF;
  6468. }
  6469. else if(spawn->IsPet() && ((Entity*)spawn)->GetOwner() &&
  6470. ((Entity*)spawn)->GetOwner()->IsPlayer())
  6471. {
  6472. type = ((Entity*)spawn)->GetPetType();
  6473. Player* petOwner = (Player*)((Entity*)spawn)->GetOwner();
  6474. tmpCharID = petOwner->GetCharacterID();
  6475. }
  6476. if(!firstTarget)
  6477. insertTargets.append(", ");
  6478. targetsInserted.insert(make_pair(spawn, true));
  6479. LogWrite(SPELL__DEBUG, 0, "Spell", "%s has target %s (%u) added to spell %s", GetName(), spawn ? spawn->GetName() : "NA", tmpCharID, info->maintained_effects[i].spell->spell->GetName());
  6480. insertTargets.append("(" + std::to_string(caster_char_id) + ", " + std::to_string(tmpCharID) + ", " + std::to_string(type) + ", " +
  6481. std::to_string(DB_TYPE_MAINTAINEDEFFECTS) + ", " + std::to_string(info->maintained_effects[i].spell_id) + ", " + std::to_string(i) +
  6482. ", " + std::to_string(info->maintained_effects[i].slot_pos) + ")");
  6483. firstTarget = false;
  6484. }
  6485. }
  6486. multimap<int32,int8>::iterator entries;
  6487. for(entries = info->maintained_effects[i].spell->char_id_targets.begin(); entries != info->maintained_effects[i].spell->char_id_targets.end(); entries++)
  6488. {
  6489. if(!firstTarget)
  6490. insertTargets.append(", ");
  6491. LogWrite(SPELL__DEBUG, 0, "Spell", "%s has target %s (%u) added to spell %s", GetName(), spawn ? spawn->GetName() : "NA", entries->first, info->maintained_effects[i].spell->spell->GetName());
  6492. insertTargets.append("(" + std::to_string(caster_char_id) + ", " + std::to_string(entries->first) + ", " + std::to_string(entries->second) + ", " +
  6493. std::to_string(DB_TYPE_MAINTAINEDEFFECTS) + ", " + std::to_string(info->maintained_effects[i].spell_id) + ", " + std::to_string(i) +
  6494. ", " + std::to_string(info->maintained_effects[i].slot_pos) + ")");
  6495. firstTarget = false;
  6496. }
  6497. info->maintained_effects[i].spell->MSpellTargets.releasereadlock(__FUNCTION__, __LINE__);
  6498. if(!firstTarget) {
  6499. savedEffects.AddQueryAsync(GetCharacterID(), &database, Q_INSERT, insertTargets.c_str());
  6500. }
  6501. }
  6502. }
  6503. MMaintainedSpells.releasereadlock(__FUNCTION__, __LINE__);
  6504. MSpellEffects.releasereadlock(__FUNCTION__, __LINE__);
  6505. }
  6506. void Player::MentorTarget()
  6507. {
  6508. if(client->GetPlayer()->GetGroupMemberInfo() && client->GetPlayer()->GetGroupMemberInfo()->mentor_target_char_id)
  6509. {
  6510. client->GetPlayer()->GetGroupMemberInfo()->mentor_target_char_id = 0;
  6511. reset_mentorship = true;
  6512. client->Message(CHANNEL_COMMAND_TEXT, "You stop mentoring, and return to level %u.", client->GetPlayer()->GetLevel());
  6513. }
  6514. else if(!reset_mentorship && client->GetPlayer()->GetTarget())
  6515. {
  6516. if(client->GetPlayer()->GetTarget()->IsPlayer())
  6517. {
  6518. Player* tmpPlayer = (Player*)client->GetPlayer()->GetTarget();
  6519. if(tmpPlayer->GetGroupMemberInfo() && tmpPlayer->GetGroupMemberInfo()->mentor_target_char_id)
  6520. {
  6521. client->Message(CHANNEL_COMMAND_TEXT, "You cannot mentor %s at this time.",tmpPlayer->GetName());
  6522. return;
  6523. }
  6524. if(client->GetPlayer()->group_id > 0 && client->GetPlayer()->GetTarget()->group_id == client->GetPlayer()->group_id)
  6525. {
  6526. if(client->GetPlayer()->GetGroupMemberInfo() && !client->GetPlayer()->GetGroupMemberInfo()->mentor_target_char_id && client->GetPlayer()->GetZone() == client->GetPlayer()->GetTarget()->GetZone() && client->GetPlayer()->GetTarget()->GetName() != client->GetPlayer()->GetName())
  6527. {
  6528. SetMentorStats(client->GetPlayer()->GetTarget()->GetLevel(), tmpPlayer->GetCharacterID());
  6529. client->Message(CHANNEL_COMMAND_TEXT, "You are now mentoring %s, reducing your effective level to %u.",client->GetPlayer()->GetTarget()->GetName(), client->GetPlayer()->GetTarget()->GetLevel());
  6530. }
  6531. if(client->GetPlayer()->GetTarget()->GetName() == client->GetPlayer()->GetName()) {
  6532. client->Message(CHANNEL_COMMAND_TEXT, "You cannot mentor yourself.");
  6533. }
  6534. }
  6535. }
  6536. }
  6537. }
  6538. void Player::SetMentorStats(int32 effective_level, int32 target_char_id, bool update_stats)
  6539. {
  6540. if(update_stats) {
  6541. RemoveSpells();
  6542. }
  6543. if(client->GetPlayer()->GetGroupMemberInfo())
  6544. client->GetPlayer()->GetGroupMemberInfo()->mentor_target_char_id = target_char_id;
  6545. InfoStruct* info = GetInfoStruct();
  6546. info->set_effective_level(effective_level);
  6547. CalculatePlayerHPPower(effective_level);
  6548. client->GetPlayer()->CalculateBonuses();
  6549. if(update_stats) {
  6550. client->GetPlayer()->SetHP(GetTotalHP());
  6551. client->GetPlayer()->SetPower(GetTotalPower());
  6552. }
  6553. /*info->set_agi_base(effective_level * 2 + 15);
  6554. info->set_intel_base(effective_level * 2 + 15);
  6555. info->set_wis_base(effective_level * 2 + 15);
  6556. info->set_str_base(effective_level * 2 + 15);
  6557. info->set_sta_base(effective_level * 2 + 15);
  6558. info->set_cold_base((int16)(effective_level * 1.5 + 10));
  6559. info->set_heat_base((int16)(effective_level * 1.5 + 10));
  6560. info->set_disease_base((int16)(effective_level * 1.5 + 10));
  6561. info->set_mental_base((int16)(effective_level * 1.5 + 10));
  6562. info->set_magic_base((int16)(effective_level * 1.5 + 10));
  6563. info->set_divine_base((int16)(effective_level * 1.5 + 10));
  6564. info->set_poison_base((int16)(effective_level * 1.5 + 10));*/
  6565. GetClient()->ClearSentItemDetails();
  6566. if(GetClient())
  6567. {
  6568. EQ2Packet* app = GetEquipmentList()->serialize(GetClient()->GetVersion(), this);
  6569. if (app) {
  6570. GetClient()->QueuePacket(app);
  6571. }
  6572. }
  6573. GetEquipmentList()->SendEquippedItems(this);
  6574. }
  6575. void Player::SetLevel(int16 level, bool setUpdateFlags) {
  6576. if(!GetGroupMemberInfo() || GetGroupMemberInfo()->mentor_target_char_id == 0) {
  6577. GetInfoStruct()->set_effective_level(level);
  6578. }
  6579. SetInfo(&appearance.level, level, setUpdateFlags);
  6580. SetXP(0);
  6581. SetNeededXP();
  6582. }
  6583. bool Player::SerializeItemPackets(EquipmentItemList* equipList, vector<EQ2Packet*>* packets, Item* item, int16 version, Item* to_item) {
  6584. if(item_list.AddItem(item)) {
  6585. item->save_needed = true;
  6586. SetEquippedItemAppearances();
  6587. packets->push_back(equipList->serialize(version, this));
  6588. packets->push_back(item->serialize(version, false));
  6589. if(to_item)
  6590. packets->push_back(to_item->serialize(version, false, this));
  6591. packets->push_back(item_list.serialize(this, version));
  6592. return true;
  6593. }
  6594. else {
  6595. LogWrite(PLAYER__ERROR, 0, "Player", "failed to add item to item_list");
  6596. }
  6597. return false;
  6598. }
  6599. void Player::AddGMVisualFilter(int32 filter_type, int32 filter_value, char* filter_search_str, int16 visual_tag) {
  6600. if(MatchGMVisualFilter(filter_type, filter_value, filter_search_str) > 0)
  6601. return;
  6602. vis_mutex.writelock(__FUNCTION__, __LINE__);
  6603. GMTagFilter filter;
  6604. filter.filter_type = filter_type;
  6605. filter.filter_value = filter_value;
  6606. memset(filter.filter_search_criteria, 0, sizeof(filter.filter_search_criteria));
  6607. if(filter_search_str)
  6608. memcpy(&filter.filter_search_criteria, filter_search_str, strnlen(filter_search_str, sizeof(filter.filter_search_criteria)));
  6609. filter.visual_tag = visual_tag;
  6610. gm_visual_filters.push_back(filter);
  6611. vis_mutex.releasewritelock(__FUNCTION__, __LINE__);
  6612. }
  6613. int16 Player::MatchGMVisualFilter(int32 filter_type, int32 filter_value, char* filter_search_str, bool in_vismutex_lock) {
  6614. if(!in_vismutex_lock)
  6615. vis_mutex.readlock(__FUNCTION__, __LINE__);
  6616. int16 tag_id = 0;
  6617. vector<GMTagFilter>::iterator itr = gm_visual_filters.begin();
  6618. for(;itr != gm_visual_filters.end();itr++) {
  6619. if(itr->filter_type == filter_type && itr->filter_value == filter_value) {
  6620. if(filter_search_str && !strcasecmp(filter_search_str, itr->filter_search_criteria)) {
  6621. tag_id = itr->visual_tag;
  6622. break;
  6623. }
  6624. }
  6625. }
  6626. if(!in_vismutex_lock)
  6627. vis_mutex.releasereadlock(__FUNCTION__, __LINE__);
  6628. return tag_id;
  6629. }
  6630. void Player::ClearGMVisualFilters() {
  6631. vis_mutex.writelock(__FUNCTION__, __LINE__);
  6632. gm_visual_filters.clear();
  6633. vis_mutex.releasewritelock(__FUNCTION__, __LINE__);
  6634. }
  6635. int Player::GetPVPAlignment(){
  6636. int bind_zone = GetPlayerInfo()->GetBindZoneID();
  6637. int alignment = 0;
  6638. if(bind_zone && bind_zone != 0){
  6639. //0 is good.
  6640. //1 is evil.
  6641. //2 is neutral aka haven players.
  6642. switch(bind_zone){
  6643. //good zones
  6644. case 114: //Gfay
  6645. case 221: //Qeynos Harbor
  6646. case 222: //North Qeynos
  6647. case 231: //South Qeynos
  6648. case 233: //Nettleville
  6649. case 234: //Starcrest
  6650. case 235: //Graystone
  6651. case 236: //CastleView
  6652. case 237: //Willowood
  6653. case 238: //Baubbleshire
  6654. case 470: //Frostfang
  6655. case 589: //Qeynos Combined 1
  6656. case 660: //Qeynos Combined 2
  6657. alignment = 0; //good
  6658. break;
  6659. //evil zones
  6660. case 128: //East Freeport
  6661. case 134: //Big Bend
  6662. case 135: //Stonestair
  6663. case 136: //Temple St.
  6664. case 137: //Beggars Ct.
  6665. case 138: //Longshadow
  6666. case 139: //Scale Yard
  6667. case 144: //North Freeport
  6668. case 166: //South Freeport
  6669. case 168: //West Freeport
  6670. case 184: //Neriak
  6671. case 644: //BigBend2
  6672. case 645: //Stonestair2
  6673. case 646: //Temple St2
  6674. case 647: //Beggars Ct2
  6675. case 648: //LongShadow2
  6676. case 649: //Scale Yard2
  6677. alignment = 1; //evil
  6678. break;
  6679. //Neutral (MajDul?)
  6680. case 45: //haven
  6681. case 46: //MajDul
  6682. alignment = 2;
  6683. break;
  6684. default:
  6685. alignment = -1; //error
  6686. }
  6687. //return -1 (error), 0 (good), 1 (evil), or 2 (Neutral)
  6688. return alignment;
  6689. }
  6690. return -1; //error
  6691. }
  6692. void Player::GetSpellBookSlotSort(int32 pattern, int32* i, int8* page_book_count, int32* last_start_point) {
  6693. switch(pattern) {
  6694. case 1: { // down
  6695. *i = (*i) + 2;
  6696. (*page_book_count)++;
  6697. if(*page_book_count > 3) {
  6698. if(((*i) % 2) == 0) {
  6699. (*i) = (*last_start_point) + 1;
  6700. }
  6701. else {
  6702. (*last_start_point) = (*last_start_point) + 8;
  6703. (*i) = (*last_start_point);
  6704. }
  6705. (*page_book_count) = 0;
  6706. }
  6707. break;
  6708. }
  6709. case 2: { // across
  6710. (*page_book_count)++;
  6711. switch(*page_book_count) {
  6712. case 1:
  6713. case 3: {
  6714. (*i)++;
  6715. break;
  6716. }
  6717. case 2: {
  6718. (*i) = (*i) + 7;
  6719. break;
  6720. }
  6721. case 4: {
  6722. (*last_start_point) = (*last_start_point) + 2;
  6723. (*i) = (*last_start_point);
  6724. (*page_book_count) = 0;
  6725. break;
  6726. }
  6727. }
  6728. break;
  6729. }
  6730. default: { // zig-zag
  6731. (*i)++;
  6732. break;
  6733. }
  6734. }
  6735. }
  6736. bool Player::IsSpawnInRangeList(int32 spawn_id) {
  6737. std::shared_lock lock(spawn_aggro_range_mutex);
  6738. map<int32, bool>::iterator spawn_itr = player_aggro_range_spawns.find(spawn_id);
  6739. if(spawn_itr != player_aggro_range_spawns.end()) {
  6740. return spawn_itr->second;
  6741. }
  6742. return false;
  6743. }
  6744. void Player::SetSpawnInRangeList(int32 spawn_id, bool in_range) {
  6745. std::unique_lock lock(spawn_aggro_range_mutex);
  6746. player_aggro_range_spawns[spawn_id] = in_range;
  6747. }
  6748. void Player::ProcessSpawnRangeUpdates() {
  6749. std::unique_lock lock(spawn_aggro_range_mutex);
  6750. if(GetClient()->GetCurrentZone() == nullptr) {
  6751. return;
  6752. }
  6753. map<int32, bool>::iterator spawn_itr;
  6754. for(spawn_itr = player_aggro_range_spawns.begin(); spawn_itr != player_aggro_range_spawns.end();) {
  6755. if(spawn_itr->second) {
  6756. Spawn* spawn = GetClient()->GetCurrentZone()->GetSpawnByID(spawn_itr->first);
  6757. if(spawn && spawn->IsNPC() && (GetDistance(spawn)) > ((NPC*)spawn)->GetAggroRadius()) {
  6758. GetClient()->GetCurrentZone()->SendSpawnChanges((NPC*)spawn, GetClient(), true, true);
  6759. spawn_itr->second = false;
  6760. spawn_itr = player_aggro_range_spawns.erase(spawn_itr);
  6761. continue;
  6762. }
  6763. }
  6764. spawn_itr++;
  6765. }
  6766. }
  6767. void Player::CalculatePlayerHPPower(int16 new_level) {
  6768. if(IsPlayer()) {
  6769. int16 effective_level = GetInfoStruct()->get_effective_level() != 0 ? GetInfoStruct()->get_effective_level() : GetLevel();
  6770. if(new_level < 1) {
  6771. new_level = effective_level;
  6772. }
  6773. float hp_rule_mod = rule_manager.GetGlobalRule(R_Player, StartHPLevelMod)->GetFloat();
  6774. float power_rule_mod = rule_manager.GetGlobalRule(R_Player, StartPowerLevelMod)->GetFloat();
  6775. sint32 base_hp = rule_manager.GetGlobalRule(R_Player, StartHPBase)->GetFloat();
  6776. sint32 base_power = rule_manager.GetGlobalRule(R_Player, StartPowerBase)->GetSInt32();
  6777. sint32 new_hp = (sint32)((float)new_level * (float)new_level * hp_rule_mod + base_hp);
  6778. sint32 new_power = (sint32)((float)new_level * (float)new_level * power_rule_mod + base_power);
  6779. if(new_hp < 1) {
  6780. LogWrite(PLAYER__WARNING, 0, "Player", "Player HP Calculation for %s too low at level %u due to ruleset, StartPowerLevelMod %f, BasePower %i", GetName(), new_level, hp_rule_mod, base_hp);
  6781. new_hp = 1;
  6782. }
  6783. if(new_power < 1) {
  6784. LogWrite(PLAYER__WARNING, 0, "Player", "Player Power Calculations for %s too low at level %u due to ruleset, StartPowerLevelMod %f, BasePower %i", GetName(), new_level, power_rule_mod, base_power);
  6785. new_power = 1;
  6786. }
  6787. SetTotalHPBase(new_hp);
  6788. SetTotalHPBaseInstance(new_hp); // we need the hp base to override the instance as the new default
  6789. SetTotalPowerBase(new_power);
  6790. SetTotalPowerBaseInstance(new_power); // we need the hp base to override the instance as the new default
  6791. LogWrite(PLAYER__INFO, 0, "Player", "Player %s: Level %u, Set Base HP %i, Set Base Power: %i", GetName(), new_level, new_hp, new_power);
  6792. }
  6793. }
  6794. bool Player::IsAllowedCombatEquip(int8 slot, bool send_message) {
  6795. bool rule_pass = true;
  6796. if(EngagedInCombat() && rule_manager.GetZoneRule(GetZoneID(), R_Player, AllowPlayerEquipCombat)->GetInt8() == 0) {
  6797. switch(slot) {
  6798. case EQ2_PRIMARY_SLOT:
  6799. case EQ2_SECONDARY_SLOT:
  6800. case EQ2_RANGE_SLOT:
  6801. case EQ2_AMMO_SLOT: {
  6802. // good to go!
  6803. break;
  6804. }
  6805. default: {
  6806. if(send_message && GetClient()) {
  6807. GetClient()->SimpleMessage(CHANNEL_COLOR_RED, "You may not unequip/equip items while in combat.");
  6808. }
  6809. rule_pass = false;
  6810. break;
  6811. }
  6812. }
  6813. }
  6814. return rule_pass;
  6815. }
  6816. void Player::SetActiveFoodUniqueID(int32 unique_id, bool update_db) {
  6817. active_food_unique_id = unique_id;
  6818. if(update_db) {
  6819. database.insertCharacterProperty(client, CHAR_PROPERTY_SETACTIVEFOOD, (char*)std::to_string(unique_id).c_str());
  6820. }
  6821. }
  6822. void Player::SetActiveDrinkUniqueID(int32 unique_id, bool update_db) {
  6823. active_drink_unique_id = unique_id;
  6824. if(update_db) {
  6825. database.insertCharacterProperty(client, CHAR_PROPERTY_SETACTIVEDRINK, (char*)std::to_string(unique_id).c_str());
  6826. }
  6827. }