LuaFunctions.cpp 265 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153
  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 "LuaFunctions.h"
  17. #include "Spawn.h"
  18. #include "WorldDatabase.h"
  19. #include "LuaInterface.h"
  20. #include "../common/ConfigReader.h"
  21. #include "client.h"
  22. #include "World.h"
  23. #include "Commands/Commands.h"
  24. #include "races.h"
  25. #include "classes.h"
  26. #include "Variables.h"
  27. #include "SpellProcess.h"
  28. #include "../common/Log.h"
  29. #include <math.h>
  30. #include "HeroicOp/HeroicOp.h"
  31. #include "RaceTypes/RaceTypes.h"
  32. #include "ClientPacketFunctions.h"
  33. #include "Transmute.h"
  34. extern WorldDatabase database;
  35. extern LuaInterface* lua_interface;
  36. extern ConfigReader configReader;
  37. extern MasterQuestList master_quest_list;
  38. extern MasterItemList master_item_list;
  39. extern MasterSpellList master_spell_list;
  40. extern World world;
  41. extern Commands commands;
  42. extern ZoneList zone_list;
  43. extern Races races;
  44. extern Classes classes;
  45. extern Variables variables;
  46. extern MasterSkillList master_skill_list;
  47. extern MasterHeroicOPList master_ho_list;
  48. extern MasterRaceTypeList race_types_list;
  49. extern MasterLanguagesList master_languages_list;
  50. int EQ2Emu_lua_PlayFlavor(lua_State* state){
  51. if(!lua_interface)
  52. return 0;
  53. Spawn* spawn = lua_interface->GetSpawn(state);
  54. string mp3_string = lua_interface->GetStringValue(state, 2);
  55. string text_string = lua_interface->GetStringValue(state, 3);
  56. string emote_string = lua_interface->GetStringValue(state, 4);
  57. int32 key1 = lua_interface->GetInt32Value(state, 5);
  58. int32 key2 = lua_interface->GetInt32Value(state, 6);
  59. Spawn* player = lua_interface->GetSpawn(state, 7);
  60. int8 language = lua_interface->GetInt8Value(state, 8);
  61. if(spawn){
  62. const char* mp3 = 0;
  63. const char* text = 0;
  64. const char* emote = 0;
  65. if(mp3_string.length() > 0)
  66. mp3 = mp3_string.c_str();
  67. if(text_string.length() > 0)
  68. text = text_string.c_str();
  69. if(emote_string.length() > 0)
  70. emote = emote_string.c_str();
  71. Client* client = 0;
  72. if(player && player->IsPlayer())
  73. client = spawn->GetZone()->GetClientBySpawn(player);
  74. if(client){
  75. if(((Player*)player)->WasSentSpawn(spawn->GetID()) && !((Player*)player)->WasSpawnRemoved(spawn))
  76. spawn->GetZone()->PlayFlavor(client, spawn, mp3, text, emote, key1, key2, language);
  77. }
  78. else
  79. spawn->GetZone()->PlayFlavor(spawn, mp3, text, emote, key1, key2, language);
  80. }
  81. return 0;
  82. }
  83. int EQ2Emu_lua_PlaySound(lua_State* state){
  84. if(!lua_interface)
  85. return 0;
  86. Spawn* spawn = lua_interface->GetSpawn(state);
  87. string sound_string = lua_interface->GetStringValue(state, 2);
  88. float x = lua_interface->GetFloatValue(state, 3);
  89. float y = lua_interface->GetFloatValue(state, 4);
  90. float z = lua_interface->GetFloatValue(state, 5);
  91. Spawn* player = lua_interface->GetSpawn(state, 6);
  92. if(spawn && sound_string.length() > 0){
  93. Client* client = 0;
  94. if(player && player->IsPlayer())
  95. client = spawn->GetZone()->GetClientBySpawn(player);
  96. if(client)
  97. spawn->GetZone()->PlaySoundFile(client, sound_string.c_str(), x, y, z);
  98. else
  99. spawn->GetZone()->PlaySoundFile(0, sound_string.c_str(), x, y, z);
  100. }
  101. return 0;
  102. }
  103. int EQ2Emu_lua_SetRequiredQuest(lua_State* state){
  104. if(!lua_interface)
  105. return 0;
  106. Spawn* spawn = lua_interface->GetSpawn(state);
  107. int32 quest_id = lua_interface->GetInt32Value(state, 2);
  108. int16 quest_step = lua_interface->GetInt16Value(state, 3);
  109. bool private_spawn = (lua_interface->GetInt8Value(state, 4) == 1);
  110. bool continued_access = (lua_interface->GetInt8Value(state, 5) == 1);
  111. int16 flag_override = lua_interface->GetInt16Value(state, 6);
  112. if (!spawn){
  113. lua_interface->LogError("LUA SetRequiredQuest command error: spawn is not valid");
  114. return 0;
  115. }
  116. if(quest_id > 0){
  117. //Add this quest to the list of required quests for this spawn
  118. spawn->SetQuestsRequired(quest_id, quest_step);
  119. //If private spawn value set
  120. if(private_spawn){
  121. //Set the spawn to be private when not granted access through this quest
  122. spawn->AddAllowAccessSpawn(spawn);
  123. spawn->SetPrivateQuestSpawn(true);
  124. }
  125. //This value allows access after a quest step, or the whole quest has been completed
  126. if(continued_access)
  127. spawn->SetQuestsRequiredContinuedAccess(true);
  128. //This value will override vis_flags in the vis packet
  129. if(flag_override > 0)
  130. spawn->SetQuestsRequiredOverride(flag_override);
  131. }
  132. return 0;
  133. }
  134. int EQ2Emu_lua_SpawnSetByDistance(lua_State* state){
  135. if(!lua_interface)
  136. return 0;
  137. Spawn* spawn = lua_interface->GetSpawn(state);
  138. float max_distance = lua_interface->GetFloatValue(state, 2);
  139. string variable = lua_interface->GetStringValue(state, 3);
  140. string value = lua_interface->GetStringValue(state, 4);
  141. if(max_distance > 0 && spawn && value.length() > 0 && variable.length() > 0 && spawn->GetZone())
  142. spawn->GetZone()->SpawnSetByDistance(spawn, max_distance, variable, value);
  143. return 0;
  144. }
  145. int EQ2Emu_lua_PerformCameraShake(lua_State* state) {
  146. if (!lua_interface)
  147. return 0;
  148. Client* client = 0;
  149. Spawn* player = lua_interface->GetSpawn(state);
  150. if (player->GetZone())
  151. client = player->GetZone()->GetClientBySpawn(player);
  152. if (!client) {
  153. lua_interface->LogError("LUA PerformCameraShake command error: could not find client");
  154. return 0;
  155. }
  156. int16 value1 = lua_interface->GetInt16Value(state, 2);
  157. int16 value2 = lua_interface->GetInt16Value(state, 3);
  158. PacketStruct* packet = configReader.getStruct("WS_PerformCameraShakeMsg", client->GetVersion());
  159. if (packet) {
  160. packet->setDataByName("unknown1", value1);
  161. packet->setDataByName("unknown2", value2);
  162. client->QueuePacket(packet->serialize());
  163. safe_delete(packet);
  164. }
  165. return 0;
  166. }
  167. int EQ2Emu_lua_KillSpawn(lua_State* state) {
  168. if(!lua_interface)
  169. return 0;
  170. Spawn* dead = lua_interface->GetSpawn(state);
  171. Spawn* killer = lua_interface->GetSpawn(state, 2);
  172. bool send_packet = (lua_interface->GetInt8Value(state, 3) == 1);
  173. if(dead && dead->Alive() && dead->GetZone())
  174. dead->GetZone()->KillSpawn(false, dead, killer, send_packet);
  175. return 0;
  176. }
  177. int EQ2Emu_lua_KillSpawnByDistance(lua_State* state){
  178. if(!lua_interface)
  179. return 0;
  180. Spawn* spawn = lua_interface->GetSpawn(state);
  181. float max_distance = lua_interface->GetFloatValue(state, 2);
  182. bool include_players = lua_interface->GetInt8Value(state, 3);
  183. bool send_packet = (lua_interface->GetInt8Value(state, 4) == 1);
  184. if(max_distance > 0 && spawn && spawn->GetZone())
  185. spawn->GetZone()->KillSpawnByDistance(spawn, max_distance, include_players, send_packet);
  186. return 0;
  187. }
  188. int EQ2Emu_lua_Despawn(lua_State* state){
  189. if(!lua_interface)
  190. return 0;
  191. Spawn* spawn = lua_interface->GetSpawn(state);
  192. int32 delay = lua_interface->GetInt32Value(state, 2);
  193. if(spawn && spawn->GetZone())
  194. spawn->GetZone()->Despawn(spawn, delay);
  195. return 0;
  196. }
  197. int EQ2Emu_lua_SpawnSet(lua_State* state){
  198. if(!lua_interface)
  199. return 0;
  200. Spawn* spawn = lua_interface->GetSpawn(state);
  201. string variable = lua_interface->GetStringValue(state, 2);
  202. string value = lua_interface->GetStringValue(state, 3);
  203. int32 type = commands.GetSpawnSetType(variable);
  204. if(type != 0xFFFFFFFF && value.length() > 0 && spawn)
  205. commands.SetSpawnCommand(0, spawn, type, value.c_str());
  206. return 0;
  207. }
  208. int EQ2Emu_lua_GetSpawn(lua_State* state){
  209. if(!lua_interface)
  210. return 0;
  211. Spawn* spawn = lua_interface->GetSpawn(state);
  212. int32 spawn_id = lua_interface->GetInt32Value(state, 2);
  213. if(spawn && spawn_id > 0){
  214. Spawn* closest_spawn = spawn->GetZone()->GetClosestSpawn(spawn, spawn_id);
  215. if(closest_spawn){
  216. lua_interface->SetSpawnValue(state, closest_spawn);
  217. return 1;
  218. }
  219. }
  220. return 0;
  221. }
  222. int EQ2Emu_lua_GetVariableValue(lua_State* state) {
  223. if (!lua_interface)
  224. return 0;
  225. string variable_name = lua_interface->GetStringValue(state);
  226. Variable* var = variables.FindVariable(variable_name);
  227. if (var) {
  228. lua_interface->SetStringValue(state, var->GetValue());
  229. return 1;
  230. }
  231. return 0;
  232. }
  233. int EQ2Emu_lua_GetCoinMessage(lua_State* state) {
  234. if (!lua_interface)
  235. return 0;
  236. int32 total_coins = lua_interface->GetInt32Value(state);
  237. if (total_coins == 0) {
  238. lua_interface->SetStringValue(state, "0 copper");
  239. return 1;
  240. }
  241. char tmp[64] = {0};
  242. string message = "";
  243. int32 val = 0;
  244. if (total_coins >= 1000000) {
  245. val = total_coins / 1000000;
  246. total_coins -= 1000000 * val;
  247. sprintf(tmp, " %u Platinum", val);
  248. message.append(tmp);
  249. memset(tmp, 0, 64);
  250. }
  251. if (total_coins >= 10000) {
  252. val = total_coins / 10000;
  253. total_coins -= 10000 * val;
  254. sprintf(tmp, " %u Gold", val);
  255. message.append(tmp);
  256. memset(tmp, 0, 64);
  257. }
  258. if (total_coins >= 100) {
  259. val = total_coins / 100;
  260. total_coins -= 100 * val;
  261. sprintf(tmp, " %u Silver", val);
  262. message.append(tmp);
  263. memset(tmp, 0, 64);
  264. }
  265. if (total_coins > 0) {
  266. sprintf(tmp, " %u Copper", (int32)total_coins);
  267. message.append(tmp);
  268. }
  269. lua_interface->SetStringValue(state, message.c_str());
  270. return 1;
  271. }
  272. int EQ2Emu_lua_GetSpawnByGroupID(lua_State* state) {
  273. ZoneServer* zone = lua_interface->GetZone(state);
  274. int32 group_id = lua_interface->GetInt32Value(state, 2);
  275. if (zone) {
  276. Spawn* spawn = zone->GetSpawnGroup(group_id);
  277. if (spawn) {
  278. lua_interface->SetSpawnValue(state, spawn);
  279. return 1;
  280. }
  281. }
  282. return 0;
  283. }
  284. int EQ2Emu_lua_GetSpawnByLocationID(lua_State* state) {
  285. ZoneServer* zone = lua_interface->GetZone(state);
  286. int32 location_id = lua_interface->GetInt32Value(state, 2);
  287. if (zone) {
  288. Spawn* spawn = zone->GetSpawnByLocationID(location_id);
  289. if (spawn) {
  290. lua_interface->SetSpawnValue(state, spawn);
  291. return 1;
  292. }
  293. }
  294. return 0;
  295. }
  296. int EQ2Emu_lua_GetSpawnID(lua_State* state) {
  297. Spawn* spawn = lua_interface->GetSpawn(state);
  298. if (spawn) {
  299. lua_interface->SetInt32Value(state, spawn->GetDatabaseID());
  300. return 1;
  301. }
  302. return 0;
  303. }
  304. int EQ2Emu_lua_GetSpawnGroupID(lua_State* state) {
  305. Spawn* spawn = lua_interface->GetSpawn(state);
  306. if (spawn) {
  307. lua_interface->SetInt32Value(state, spawn->GetSpawnGroupID());
  308. return 1;
  309. }
  310. return 0;
  311. }
  312. int EQ2Emu_lua_GetSpawnLocationID(lua_State* state) {
  313. Spawn* spawn = lua_interface->GetSpawn(state);
  314. if (spawn) {
  315. lua_interface->SetInt32Value(state, spawn->GetSpawnLocationID());
  316. return 1;
  317. }
  318. return 0;
  319. }
  320. int EQ2Emu_lua_GetSpawnLocationPlacementID(lua_State* state) {
  321. Spawn* spawn = lua_interface->GetSpawn(state);
  322. if (spawn) {
  323. lua_interface->SetInt32Value(state, spawn->GetSpawnLocationPlacementID());
  324. return 1;
  325. }
  326. return 0;
  327. }
  328. int EQ2Emu_lua_GetFactionAmount(lua_State* state) {
  329. Player* player = (Player*)lua_interface->GetSpawn(state);
  330. int32 faction_id = lua_interface->GetInt32Value(state, 2);
  331. if (player && player->IsPlayer() && faction_id > 0) {
  332. lua_interface->SetInt32Value(state, player->GetFactions()->GetFactionValue(faction_id));
  333. return 1;
  334. }
  335. return 0;
  336. }
  337. int EQ2Emu_lua_GetGender(lua_State* state) {
  338. Spawn* spawn = lua_interface->GetSpawn(state);
  339. if (spawn) {
  340. lua_interface->SetInt32Value(state, spawn->GetGender());
  341. return 1;
  342. }
  343. return 0;
  344. }
  345. int EQ2Emu_lua_GetTarget(lua_State* state) {
  346. Spawn* spawn = lua_interface->GetSpawn(state);
  347. if (spawn && spawn->IsEntity() && ((Entity*)spawn)->GetTarget()) {
  348. lua_interface->SetSpawnValue(state, ((Entity*)spawn)->GetTarget());
  349. return 1;
  350. }
  351. return 0;
  352. }
  353. int EQ2Emu_lua_PlayVoice(lua_State* state){
  354. if(!lua_interface)
  355. return 0;
  356. Spawn* spawn = lua_interface->GetSpawn(state);
  357. string mp3_string = lua_interface->GetStringValue(state, 2);
  358. int32 key1 = lua_interface->GetInt32Value(state, 3);
  359. int32 key2 = lua_interface->GetInt32Value(state, 4);
  360. Spawn* player = lua_interface->GetSpawn(state, 5);
  361. if(spawn && mp3_string.length() > 0){
  362. Client* client = 0;
  363. if(player && player->IsPlayer())
  364. client = spawn->GetZone()->GetClientBySpawn(player);
  365. if(client){
  366. if(((Player*)player)->WasSentSpawn(spawn->GetID()) && !((Player*)player)->WasSpawnRemoved(spawn))
  367. spawn->GetZone()->PlayVoice(client, spawn, mp3_string.c_str(), key1, key2);
  368. }
  369. else
  370. spawn->GetZone()->PlayVoice(spawn, mp3_string.c_str(), key1, key2);
  371. }
  372. return 0;
  373. }
  374. int EQ2Emu_lua_GetCurrentZoneSafeLocation(lua_State* state){
  375. if(!lua_interface)
  376. return 0;
  377. Spawn* spawn = lua_interface->GetSpawn(state);
  378. if(spawn){
  379. lua_interface->SetFloatValue(state, spawn->GetZone()->GetSafeX());
  380. lua_interface->SetFloatValue(state, spawn->GetZone()->GetSafeY());
  381. lua_interface->SetFloatValue(state, spawn->GetZone()->GetSafeZ());
  382. return 3;
  383. }
  384. return 0;
  385. }
  386. int EQ2Emu_lua_AddLootItem(lua_State* state){
  387. if(!lua_interface)
  388. return 0;
  389. Spawn* spawn = lua_interface->GetSpawn(state);
  390. if(spawn && spawn->IsEntity()){
  391. int32 item_id = lua_interface->GetInt32Value(state, 2);
  392. int16 charges = lua_interface->GetInt16Value(state, 3);
  393. if(charges == 0)
  394. charges = 1;
  395. ((Entity*)spawn)->AddLootItem(item_id, charges);
  396. }
  397. return 0;
  398. }
  399. int EQ2Emu_lua_RemoveLootItem(lua_State* state){
  400. if(!lua_interface)
  401. return 0;
  402. Spawn* spawn = lua_interface->GetSpawn(state);
  403. if(spawn && spawn->IsEntity()){
  404. int32 item_id = lua_interface->GetInt32Value(state, 2);
  405. ((Entity*)spawn)->LootItem(item_id);
  406. }
  407. return 0;
  408. }
  409. int EQ2Emu_lua_AddLootCoin(lua_State* state){
  410. if(!lua_interface)
  411. return 0;
  412. Spawn* spawn = lua_interface->GetSpawn(state);
  413. if(spawn && spawn->IsEntity()){
  414. int32 val = lua_interface->GetInt32Value(state, 2);
  415. ((Entity*)spawn)->AddLootCoins(val);
  416. }
  417. return 0;
  418. }
  419. int EQ2Emu_lua_GiveLoot(lua_State* state){
  420. if(!lua_interface)
  421. return 0;
  422. Spawn* entity = lua_interface->GetSpawn(state);
  423. Spawn* player = lua_interface->GetSpawn(state, 2);
  424. if(entity && entity->IsEntity() && player && player->IsPlayer()){
  425. int32 coins = lua_interface->GetInt32Value(state, 3);
  426. vector<Item*>* items = 0;
  427. int i=0;
  428. int32 item_id = 0;
  429. while((item_id = lua_interface->GetInt32Value(state, 4+i))){
  430. if(items == 0)
  431. items = new vector<Item*>;
  432. if(master_item_list.GetItem(item_id))
  433. items->push_back(master_item_list.GetItem(item_id));
  434. i++;
  435. }
  436. Client* client = 0;
  437. client = entity->GetZone()->GetClientBySpawn(player);
  438. if(client){
  439. ((Player*)player)->AddPendingLootItems(entity->GetID(), items);
  440. client->Loot(coins, ((Player*)player)->GetPendingLootItems(entity->GetID()), (Entity*)entity);
  441. }
  442. safe_delete(items);
  443. }
  444. return 0;
  445. }
  446. int EQ2Emu_lua_HasPendingLootItem(lua_State* state){
  447. if(!lua_interface)
  448. return 0;
  449. Spawn* entity = lua_interface->GetSpawn(state);
  450. Spawn* player = lua_interface->GetSpawn(state, 2);
  451. int32 item_id = lua_interface->GetInt32Value(state, 3);
  452. if(entity && entity->IsEntity() && player && player->IsPlayer() && item_id > 0){
  453. lua_interface->SetBooleanValue(state, ((Player*)player)->HasPendingLootItem(entity->GetID(), item_id));
  454. return 1;
  455. }
  456. return 0;
  457. }
  458. int EQ2Emu_lua_HasPendingLoot(lua_State* state){
  459. if(!lua_interface)
  460. return 0;
  461. Spawn* entity = lua_interface->GetSpawn(state);
  462. Spawn* player = lua_interface->GetSpawn(state, 2);
  463. if(entity && entity->IsEntity() && player && player->IsPlayer()){
  464. lua_interface->SetBooleanValue(state, ((Player*)player)->HasPendingLootItems(entity->GetID()));
  465. return 1;
  466. }
  467. return 0;
  468. }
  469. int EQ2Emu_lua_CreateConversation(lua_State* state){
  470. if(!lua_interface)
  471. return 0;
  472. vector<ConversationOption>* conversation = new vector<ConversationOption>();
  473. lua_interface->SetConversationValue(state, conversation);
  474. return 1;
  475. }
  476. int EQ2Emu_lua_AddConversationOption(lua_State* state){
  477. if(!lua_interface)
  478. return 0;
  479. vector<ConversationOption>* conversation = lua_interface->GetConversation(state);
  480. if(conversation){
  481. ConversationOption conv_option;
  482. conv_option.option = lua_interface->GetStringValue(state, 2);
  483. conv_option.function = lua_interface->GetStringValue(state, 3);
  484. if(conv_option.option.length() > 0)
  485. conversation->push_back(conv_option);
  486. }
  487. return 0;
  488. }
  489. int EQ2Emu_lua_CloseConversation(lua_State* state){
  490. if(!lua_interface)
  491. return 0;
  492. Spawn* npc = lua_interface->GetSpawn(state);
  493. Spawn* player = lua_interface->GetSpawn(state, 2);
  494. if(npc && player && player->IsPlayer() && player->GetZone()){
  495. Client* client = player->GetZone()->GetClientBySpawn(player);
  496. if(client){
  497. int32 conversation_id = client->GetConversationID(npc, 0);
  498. client->CloseDialog(conversation_id);
  499. }
  500. }
  501. return 0;
  502. }
  503. int EQ2Emu_lua_CloseItemConversation(lua_State* state){
  504. if(!lua_interface)
  505. return 0;
  506. Item* item = lua_interface->GetItem(state);
  507. Spawn* player = lua_interface->GetSpawn(state, 2);
  508. if(item && player && player->IsPlayer() && player->GetZone()){
  509. Client* client = player->GetZone()->GetClientBySpawn(player);
  510. if(client){
  511. int32 conversation_id = client->GetConversationID(0, item);
  512. client->CloseDialog(conversation_id);
  513. }
  514. }
  515. return 0;
  516. }
  517. int EQ2Emu_lua_StartDialogConversation(lua_State* state) {
  518. if (!lua_interface)
  519. return 0;
  520. vector<ConversationOption>* conversation = lua_interface->GetConversation(state);
  521. Spawn* spawn = 0;
  522. Item* item = 0;
  523. int8 type = lua_interface->GetInt8Value(state, 2);
  524. if (type == 1 || type == 3)
  525. spawn = lua_interface->GetSpawn(state, 3);
  526. else if (type == 2 || type == 4)
  527. item = lua_interface->GetItem(state, 3);
  528. Spawn* player = lua_interface->GetSpawn(state, 4);
  529. string text = lua_interface->GetStringValue(state, 5);
  530. string mp3 = lua_interface->GetStringValue(state, 6);
  531. int32 key1 = lua_interface->GetInt32Value(state, 7);
  532. int32 key2 = lua_interface->GetInt32Value(state, 8);
  533. if (conversation && text.length() > 0 && (spawn || item) && player && player->IsPlayer()) {
  534. Client* client = player->GetZone()->GetClientBySpawn(player);
  535. if (client) {
  536. if (spawn) {
  537. // Need to do this so the function works the same as it did before
  538. if (type == 1)
  539. type++;
  540. if (mp3.length() > 0)
  541. client->DisplayConversation((Entity*)spawn, type, conversation, const_cast<char*>(text.c_str()), mp3.c_str(), key1, key2);
  542. else
  543. client->DisplayConversation((Entity*)spawn, type, conversation, const_cast<char*>(text.c_str()));
  544. }
  545. else {
  546. if (mp3.length() > 0)
  547. client->DisplayConversation(item, conversation, const_cast<char*>(text.c_str()), type, mp3.c_str(), key1, key2);
  548. else
  549. client->DisplayConversation(item, conversation, const_cast<char*>(text.c_str()), type);
  550. }
  551. }
  552. }
  553. safe_delete(conversation);
  554. return 0;
  555. }
  556. /*int EQ2Emu_lua_StartItemConversation(lua_State* state){
  557. if(!lua_interface)
  558. return 0;
  559. vector<ConversationOption>* conversation = lua_interface->GetConversation(state);
  560. Item* item = lua_interface->GetItem(state, 2);
  561. Spawn* player = lua_interface->GetSpawn(state, 3);
  562. string text = lua_interface->GetStringValue(state, 4);
  563. string mp3 = lua_interface->GetStringValue(state, 5);
  564. int32 key1 = lua_interface->GetInt32Value(state, 6);
  565. int32 key2 = lua_interface->GetInt32Value(state, 7);
  566. if(conversation && text.length() > 0 && item && player && player->IsPlayer()){
  567. Client* client = player->GetZone()->GetClientBySpawn(player);
  568. if(client){
  569. if(mp3.length() > 0)
  570. client->DisplayConversation(item, conversation, (char*)text.c_str(), mp3.c_str(), key1, key2);
  571. else
  572. client->DisplayConversation(item, conversation, (char*)text.c_str());
  573. }
  574. safe_delete(conversation);
  575. }
  576. return 0;
  577. }*/
  578. int EQ2Emu_lua_StartConversation(lua_State* state){
  579. if(!lua_interface)
  580. return 0;
  581. vector<ConversationOption>* conversation = lua_interface->GetConversation(state);
  582. Spawn* npc = lua_interface->GetSpawn(state, 2);
  583. Spawn* player = lua_interface->GetSpawn(state, 3);
  584. string text = lua_interface->GetStringValue(state, 4);
  585. string mp3 = lua_interface->GetStringValue(state, 5);
  586. int32 key1 = lua_interface->GetInt32Value(state, 6);
  587. int32 key2 = lua_interface->GetInt32Value(state, 7);
  588. if(conversation && text.length() > 0 && npc && npc->IsEntity() && player && player->IsPlayer()){
  589. Client* client = npc->GetZone()->GetClientBySpawn(player);
  590. if(mp3.length() > 0)
  591. client->DisplayConversation((Entity*)npc, 1, conversation, text.c_str(), mp3.c_str(), key1, key2);
  592. else
  593. client->DisplayConversation((Entity*)npc, 1, conversation, text.c_str());
  594. safe_delete(conversation);
  595. }
  596. return 0;
  597. }
  598. int EQ2Emu_lua_SetPlayerProximityFunction(lua_State* state){
  599. if(!lua_interface)
  600. return 0;
  601. Spawn* spawn = lua_interface->GetSpawn(state);
  602. float distance = lua_interface->GetFloatValue(state, 2);
  603. string in_range_function = lua_interface->GetStringValue(state, 3);
  604. string leaving_range_function = lua_interface->GetStringValue(state, 4);
  605. if(spawn && !spawn->IsPlayer() && distance > 0 && in_range_function.length() > 0)
  606. spawn->GetZone()->AddPlayerProximity(spawn, distance, in_range_function, leaving_range_function);
  607. return 0;
  608. }
  609. int EQ2Emu_lua_SetLocationProximityFunction(lua_State* state) {
  610. ZoneServer* zone = lua_interface->GetZone(state);
  611. float x = lua_interface->GetFloatValue(state, 2);
  612. float y = lua_interface->GetFloatValue(state, 3);
  613. float z = lua_interface->GetFloatValue(state, 4);
  614. float max_variation = lua_interface->GetFloatValue(state, 5);
  615. string in_range_function = lua_interface->GetStringValue(state, 6);
  616. string leaving_range_function = lua_interface->GetStringValue(state, 7);
  617. if (zone && in_range_function.length() > 0)
  618. zone->AddLocationProximity(x, y, z, max_variation, in_range_function, leaving_range_function);
  619. return 0;
  620. }
  621. int EQ2Emu_lua_SetLootCoin(lua_State* state){
  622. if(!lua_interface)
  623. return 0;
  624. Spawn* spawn = lua_interface->GetSpawn(state);
  625. if(spawn && spawn->IsEntity()){
  626. int32 val = lua_interface->GetInt32Value(state, 2);
  627. ((Entity*)spawn)->SetLootCoins(val);
  628. }
  629. return 0;
  630. }
  631. int EQ2Emu_lua_GetLootCoin(lua_State* state){
  632. if(!lua_interface)
  633. return 0;
  634. Spawn* spawn = lua_interface->GetSpawn(state);
  635. if(spawn && spawn->IsEntity()){
  636. lua_interface->SetInt32Value(state, ((Entity*)spawn)->GetLootCoins());
  637. return 1;
  638. }
  639. return 0;
  640. }
  641. int EQ2Emu_lua_MovementLoopAdd(lua_State* state){
  642. if(!lua_interface)
  643. return 0;
  644. Spawn* spawn = lua_interface->GetSpawn(state);
  645. float x = lua_interface->GetFloatValue(state, 2);
  646. float y = lua_interface->GetFloatValue(state, 3);
  647. float z = lua_interface->GetFloatValue(state, 4);
  648. float speed = lua_interface->GetFloatValue(state, 5);
  649. int32 delay = lua_interface->GetInt32Value(state, 6); //this should be given as seconds, as it is converted to ms later
  650. string function = lua_interface->GetStringValue(state, 7);
  651. if(spawn){
  652. spawn->AddMovementLocation(x, y, z, speed, delay, function.c_str());
  653. spawn->GetZone()->AddMovementNPC(spawn);
  654. }
  655. lua_interface->ResetFunctionStack(state);
  656. return 0;
  657. }
  658. int EQ2Emu_lua_IsPlayer(lua_State* state){
  659. if(!lua_interface)
  660. return 0;
  661. Spawn* spawn = lua_interface->GetSpawn(state);
  662. if(spawn){
  663. lua_interface->SetBooleanValue(state, spawn->IsPlayer());
  664. return 1;
  665. }
  666. return 0;
  667. }
  668. int EQ2Emu_lua_FaceTarget(lua_State* state){
  669. if(!lua_interface)
  670. return 0;
  671. Spawn* spawn = lua_interface->GetSpawn(state);
  672. Spawn* target = lua_interface->GetSpawn(state, 2);
  673. if(spawn && target){
  674. if(spawn->IsEntity())
  675. // ((Entity*)spawn)->FaceTarget(target);
  676. static_cast<Entity*>(spawn)->FaceTarget(target);
  677. }
  678. lua_interface->ResetFunctionStack(state);
  679. return 0;
  680. }
  681. int EQ2Emu_lua_MoveToLocation(lua_State* state){
  682. if(!lua_interface)
  683. return 0;
  684. Spawn* spawn = lua_interface->GetSpawn(state);
  685. float x = lua_interface->GetFloatValue(state, 2);
  686. float y = lua_interface->GetFloatValue(state, 3);
  687. float z = lua_interface->GetFloatValue(state, 4);
  688. float speed = lua_interface->GetFloatValue(state, 5);
  689. string lua_function = lua_interface->GetStringValue(state, 6);
  690. bool more_points = lua_interface->GetBooleanValue(state, 7);
  691. if(spawn) {
  692. if (speed == 0)
  693. speed = spawn->GetSpeed();
  694. spawn->AddRunningLocation(x, y, z, speed, 0.0f, true, !more_points, lua_function);
  695. }
  696. lua_interface->ResetFunctionStack(state);
  697. return 0;
  698. }
  699. int EQ2Emu_lua_Say(lua_State* state){
  700. if(!lua_interface)
  701. return 0;
  702. Spawn* spawn = lua_interface->GetSpawn(state);
  703. string message = lua_interface->GetStringValue(state, 2);
  704. Spawn* player = lua_interface->GetSpawn(state, 3);
  705. int32 language = lua_interface->GetInt32Value(state, 4);
  706. if(spawn && message.length() > 0){
  707. Client* client = 0;
  708. if(player && player->IsPlayer())
  709. client = spawn->GetZone()->GetClientBySpawn(player);
  710. if(client)
  711. spawn->GetZone()->HandleChatMessage(client, spawn, 0, CHANNEL_SAY, message.c_str(), 30, 0, true, language);
  712. else
  713. spawn->GetZone()->HandleChatMessage(spawn, 0, CHANNEL_SAY, message.c_str(), 30, 0, true, language);
  714. }
  715. lua_interface->ResetFunctionStack(state);
  716. return 0;
  717. }
  718. int EQ2Emu_lua_Shout(lua_State* state){
  719. if(!lua_interface)
  720. return 0;
  721. Spawn* spawn = lua_interface->GetSpawn(state);
  722. string message = lua_interface->GetStringValue(state, 2);
  723. Spawn* player = lua_interface->GetSpawn(state, 3);
  724. if(spawn && message.length() > 0){
  725. Client* client = 0;
  726. if(player && player->IsPlayer())
  727. client = spawn->GetZone()->GetClientBySpawn(player);
  728. if(client)
  729. spawn->GetZone()->HandleChatMessage(client, spawn, 0, CHANNEL_SHOUT, message.c_str(), 30);
  730. else
  731. spawn->GetZone()->HandleChatMessage(spawn, 0, CHANNEL_SHOUT, message.c_str(), 30);
  732. }
  733. lua_interface->ResetFunctionStack(state);
  734. return 0;
  735. }
  736. int EQ2Emu_lua_SayOOC(lua_State* state){
  737. if(!lua_interface)
  738. return 0;
  739. Spawn* spawn = lua_interface->GetSpawn(state);
  740. string message = lua_interface->GetStringValue(state, 2);
  741. Spawn* player = lua_interface->GetSpawn(state, 3);
  742. if(spawn && message.length() > 0){
  743. Client* client = 0;
  744. if(player && player->IsPlayer())
  745. client = spawn->GetZone()->GetClientBySpawn(player);
  746. if(client)
  747. spawn->GetZone()->HandleChatMessage(client, spawn, 0, CHANNEL_OOC, message.c_str(), 30);
  748. else
  749. spawn->GetZone()->HandleChatMessage(spawn, 0, CHANNEL_OOC, message.c_str(), 30);
  750. }
  751. lua_interface->ResetFunctionStack(state);
  752. return 0;
  753. }
  754. int EQ2Emu_lua_Emote(lua_State* state){
  755. if(!lua_interface)
  756. return 0;
  757. Spawn* spawn = lua_interface->GetSpawn(state);
  758. string message = lua_interface->GetStringValue(state, 2);
  759. Spawn* spawn2 = lua_interface->GetSpawn(state, 3);
  760. Spawn* player = lua_interface->GetSpawn(state, 4);
  761. char* to = 0;
  762. if(spawn2)
  763. to = spawn2->GetName();
  764. if(spawn && message.length() > 0){
  765. Client* client = 0;
  766. if(player && player->IsPlayer())
  767. client = spawn->GetZone()->GetClientBySpawn(player);
  768. if(client)
  769. spawn->GetZone()->HandleChatMessage(client, spawn, to, CHANNEL_EMOTE, message.c_str(), 30);
  770. else
  771. spawn->GetZone()->HandleChatMessage(spawn, to, CHANNEL_EMOTE, message.c_str(), 30);
  772. }
  773. lua_interface->ResetFunctionStack(state);
  774. return 0;
  775. }
  776. int EQ2Emu_lua_SpellHeal(lua_State* state){
  777. if(!lua_interface)
  778. return 0;
  779. LuaSpell* luaspell = lua_interface->GetCurrentSpell(state);
  780. if(!luaspell)
  781. return 0;
  782. Spawn* caster = luaspell->caster;
  783. string heal_type = lua_interface->GetStringValue(state);//power, heal ect
  784. int32 min_heal = lua_interface->GetInt32Value(state, 2);
  785. int32 max_heal = lua_interface->GetInt32Value(state, 3);
  786. Spawn* target = lua_interface->GetSpawn(state, 4);
  787. int8 crit_mod = lua_interface->GetInt32Value(state, 5);
  788. bool no_calcs = lua_interface->GetInt32Value(state, 6) == 1;
  789. lua_interface->ResetFunctionStack(state);
  790. if(caster && caster->IsEntity()){
  791. bool success = false;
  792. luaspell->resisted = false;
  793. if (target) {
  794. float distance = caster->GetDistance(target, true);
  795. distance -= caster->appearance.pos.collision_radius / 10;
  796. distance -= target->appearance.pos.collision_radius / 10;
  797. if (((Entity*)caster)->SpellHeal(target, distance, luaspell, heal_type, min_heal, max_heal, crit_mod, no_calcs))
  798. success = true;
  799. }
  800. if (luaspell->targets.size() > 0) {
  801. Spawn* target = 0;
  802. ZoneServer* zone = luaspell->caster->GetZone();
  803. luaspell->MSpellTargets.readlock(__FUNCTION__, __LINE__);
  804. for (int32 i = 0; i < luaspell->targets.size(); i++) {
  805. if ((target = zone->GetSpawnByID(luaspell->targets[i]))) {
  806. float distance = caster->GetDistance(target, true);
  807. distance -= caster->appearance.pos.collision_radius/10;
  808. distance -= target->appearance.pos.collision_radius/10;
  809. ((Entity*)caster)->SpellHeal(target, distance, luaspell, heal_type, min_heal, max_heal, crit_mod, no_calcs);
  810. }
  811. }
  812. luaspell->MSpellTargets.releasereadlock(__FUNCTION__, __LINE__);
  813. success = true;
  814. }
  815. if (success) {
  816. if(caster->GetZone())
  817. caster->GetZone()->TriggerCharSheetTimer();
  818. }
  819. }
  820. return 0;
  821. }
  822. int EQ2Emu_lua_SummonItem(lua_State* state){
  823. if(!lua_interface)
  824. return 0;
  825. Spawn* spawn = lua_interface->GetSpawn(state);
  826. int32 item_id = lua_interface->GetInt32Value(state, 2);
  827. bool send_messages = (lua_interface->GetInt8Value(state, 3) == 1);
  828. string location = lua_interface->GetStringValue(state, 4);
  829. if (spawn && spawn->IsPlayer()){
  830. Client* client = spawn->GetZone()->GetClientBySpawn(spawn);
  831. if (client && item_id > 0){
  832. if (strncasecmp(location.c_str(), "bank", 4) == 0)
  833. lua_interface->SetBooleanValue(state, client->AddItemToBank(item_id, 1));
  834. else
  835. lua_interface->SetBooleanValue(state, client->AddItem(item_id, 1));
  836. if (send_messages) {
  837. Item* item = master_item_list.GetItem(item_id);
  838. if (item) {
  839. client->Message(CHANNEL_COLOR_YELLOW, "You receive \\aITEM %u 0:%s\\/a.", item->details.item_id, item->name.c_str());
  840. string popup_text = "You receive " + item->name;
  841. client->SendPopupMessage(10, popup_text.c_str(), "ui_harvested_normal", 3, 0xFF, 0xFF, 0xFF);
  842. }
  843. }
  844. return 1;
  845. }
  846. }
  847. lua_interface->SetBooleanValue(state, false);
  848. return 1;
  849. }
  850. int EQ2Emu_lua_RemoveItem(lua_State* state) {
  851. Spawn* spawn = lua_interface->GetSpawn(state);
  852. int32 item_id = lua_interface->GetInt32Value(state, 2);
  853. Client *client;
  854. Item *item;
  855. if (spawn && spawn->IsPlayer() && item_id > 0) {
  856. if ((client = spawn->GetZone()->GetClientBySpawn(spawn))) {
  857. if ((item = client->GetPlayer()->item_list.GetItemFromID(item_id))) {
  858. if (client->RemoveItem(item, 1)) {
  859. lua_interface->SetBooleanValue(state, true);
  860. return 1;
  861. }
  862. }
  863. }
  864. }
  865. lua_interface->SetBooleanValue(state, false);
  866. return 1;
  867. }
  868. int EQ2Emu_lua_HasItem(lua_State* state) {
  869. Spawn* player = lua_interface->GetSpawn(state);
  870. int32 item_id = lua_interface->GetInt32Value(state, 2);
  871. bool include_bank = lua_interface->GetInt8Value(state, 3);
  872. if (player && player->IsPlayer()) {
  873. bool hasItem = false;
  874. hasItem = ((Player*)player)->item_list.HasItem(item_id, include_bank);
  875. if (!hasItem)
  876. hasItem = ((Player*)player)->GetEquipmentList()->HasItem(item_id);
  877. lua_interface->SetBooleanValue(state, hasItem);
  878. return 1;
  879. }
  880. lua_interface->SetBooleanValue(state, false);
  881. return 1;
  882. }
  883. int EQ2Emu_lua_Spawn(lua_State* state){
  884. if(!lua_interface)
  885. return 0;
  886. ZoneServer* zone = lua_interface->GetZone(state);
  887. int32 spawn_id = lua_interface->GetInt32Value(state, 2);
  888. bool restricted_npc = (lua_interface->GetInt8Value(state, 3) == 1);
  889. float x = lua_interface->GetFloatValue(state, 4);
  890. float y = lua_interface->GetFloatValue(state, 5);
  891. float z = lua_interface->GetFloatValue(state, 6);
  892. float heading = lua_interface->GetFloatValue(state, 7);
  893. if(zone && spawn_id > 0 && (x != 0 || y != 0 || z != 0)){
  894. Spawn* spawn = zone->GetSpawn(spawn_id);
  895. if(!spawn)
  896. lua_interface->LogError("LUA Spawn command error: Could not find spawn with id of %u.", spawn_id);
  897. else{
  898. spawn->SetX(x);
  899. spawn->SetY(y);
  900. spawn->SetZ(z);
  901. spawn->SetLocation(zone->GetClosestLocation(spawn));
  902. spawn->SetHeading(heading);
  903. if(restricted_npc)
  904. spawn->AddAllowAccessSpawn(spawn);
  905. zone->AddSpawn(spawn);
  906. const char* spawn_script = world.GetSpawnScript(spawn_id);
  907. if(spawn_script && lua_interface->GetSpawnScript(spawn_script) != 0){
  908. spawn->SetSpawnScript(string(spawn_script));
  909. zone->CallSpawnScript(spawn, SPAWN_SCRIPT_SPAWN);
  910. }
  911. lua_interface->SetSpawnValue(state, spawn);
  912. return 1;
  913. }
  914. }
  915. else{
  916. string output = "Invalid paramaters to LUA Spawn command: \n";
  917. if(!zone)
  918. output = output.append("\t").append("Missing zone reference. \n");
  919. if(spawn_id == 0)
  920. output = output.append("\t").append("Missing spawn_id.");
  921. lua_interface->LogError("Error in EQ2Emu_lua_Zone - %s", output.c_str());
  922. }
  923. return 0;
  924. }
  925. int EQ2Emu_lua_GetZoneName(lua_State* state){
  926. if(!lua_interface)
  927. return 0;
  928. ZoneServer* zone = lua_interface->GetZone(state);
  929. if(zone){
  930. lua_interface->SetStringValue(state, zone->GetZoneName());
  931. return 1;
  932. }
  933. return 0;
  934. }
  935. int EQ2Emu_lua_GetZoneID(lua_State* state){
  936. if(!lua_interface)
  937. return 0;
  938. ZoneServer* zone = lua_interface->GetZone(state);
  939. if(zone){
  940. lua_interface->SetInt32Value(state, zone->GetZoneID());
  941. return 1;
  942. }
  943. return 0;
  944. }
  945. int EQ2Emu_lua_GetZone(lua_State* state){
  946. if(!lua_interface)
  947. return 0;
  948. int32 zone_id = lua_interface->GetInt32Value(state);
  949. ZoneServer* zone = 0;
  950. if(zone_id > 0)
  951. zone = zone_list.Get(zone_id);
  952. else{
  953. string zone_name = lua_interface->GetStringValue(state);
  954. if(zone_name.length() > 0){
  955. zone = zone_list.Get(zone_name.c_str());
  956. }
  957. else{
  958. Spawn* spawn = lua_interface->GetSpawn(state);
  959. if(spawn)
  960. zone = spawn->GetZone();
  961. }
  962. }
  963. if(zone){
  964. lua_interface->SetZoneValue(state, zone);
  965. return 1;
  966. }
  967. return 0;
  968. }
  969. int EQ2Emu_lua_AddHate(lua_State* state) {
  970. Spawn* entity = lua_interface->GetSpawn(state);
  971. Spawn* npc = lua_interface->GetSpawn(state, 2);
  972. sint32 amount = lua_interface->GetSInt32Value(state, 3);
  973. bool send_packet = lua_interface->GetInt8Value(state, 4) == 1 ? true : false;
  974. LuaSpell* luaspell = lua_interface->GetCurrentSpell(state);
  975. if (entity && entity->IsEntity() && amount != 0) {
  976. if (luaspell) {
  977. ZoneServer* zone = luaspell->caster->GetZone();
  978. luaspell->MSpellTargets.readlock(__FUNCTION__, __LINE__);
  979. for (int32 i = 0; i < luaspell->targets.size(); i++) {
  980. Spawn* spawn = zone->GetSpawnByID(luaspell->targets.at(i));
  981. if (spawn && spawn->IsNPC() && spawn->Alive() && spawn->GetZone()) {
  982. ((NPC*)spawn)->AddHate((Entity*)entity, amount);
  983. if (send_packet)
  984. entity->GetZone()->SendThreatPacket(entity, npc, amount, luaspell->spell->GetName());
  985. }
  986. }
  987. luaspell->MSpellTargets.releasereadlock(__FUNCTION__, __LINE__);
  988. }
  989. else if (npc && npc->IsNPC() && npc->GetZone())
  990. ((NPC*)npc)->AddHate((Entity*)entity, amount);
  991. }
  992. return 0;
  993. }
  994. int EQ2Emu_lua_Zone(lua_State* state){
  995. if(!lua_interface)
  996. return 0;
  997. ZoneServer* zone = lua_interface->GetZone(state);
  998. Spawn* player = lua_interface->GetSpawn(state, 2);
  999. Client* client = 0;
  1000. if(player && player->IsPlayer())
  1001. client = player->GetZone()->GetClientBySpawn(player);
  1002. float x = lua_interface->GetFloatValue(state, 3);
  1003. float y = lua_interface->GetFloatValue(state, 4);
  1004. float z = lua_interface->GetFloatValue(state, 5);
  1005. float heading = lua_interface->GetFloatValue(state, 6);
  1006. if(zone && client) {
  1007. LogWrite(LUA__DEBUG, 0, "LUA", "LUA Zone Request by Player: '%s' (%u)", player->GetName(), player->GetID());
  1008. LogWrite(LUA__DEBUG, 5, "LUA", "\tTo Zone: '%s' (%u)", zone->GetZoneName(), zone->GetZoneID());
  1009. if ( !client->CheckZoneAccess(zone->GetZoneName()) )
  1010. {
  1011. LogWrite(LUA__WARNING, 0, "LUA", "CheckZoneAccess() FAILED! LUA Zone Request Denied!");
  1012. return 0;
  1013. }
  1014. if(x != 0 || y != 0 || z != 0){
  1015. LogWrite(LUA__DEBUG, 5, "LUA", "\tTo Coordinates: %2f, %2f, %2f, %2f", x, y, z, heading);
  1016. player->SetX(x);
  1017. player->SetY(y);
  1018. player->SetZ(z);
  1019. player->SetHeading(heading);
  1020. client->Zone(zone->GetZoneName(), false);
  1021. }
  1022. else
  1023. client->Zone(zone->GetZoneName());
  1024. }
  1025. else
  1026. lua_interface->SimpleLogError("Error in EQ2Emu_lua_Zone: invalid zone or spawn input.");
  1027. return 0;
  1028. }
  1029. int EQ2Emu_lua_AddSpawnAccess(lua_State* state){
  1030. if(!lua_interface)
  1031. return 0;
  1032. Spawn* spawn = lua_interface->GetSpawn(state);
  1033. Spawn* spawn2 = lua_interface->GetSpawn(state, 2);
  1034. if(spawn && spawn2)
  1035. spawn->AddAllowAccessSpawn(spawn2);
  1036. return 0;
  1037. }
  1038. int EQ2Emu_lua_CastSpell(lua_State* state){
  1039. if(!lua_interface)
  1040. return 0;
  1041. Spawn* target = lua_interface->GetSpawn(state);
  1042. int32 spell_id = lua_interface->GetInt32Value(state, 2);
  1043. int8 spell_tier = lua_interface->GetInt8Value(state, 3);
  1044. Spawn* caster = lua_interface->GetSpawn(state, 4);
  1045. if (!target) {
  1046. lua_interface->LogError("LUA CastSpell command error: target is not a valid spawn");
  1047. return 0;
  1048. }
  1049. if (!target->IsEntity()) {
  1050. lua_interface->LogError("LUA CastSpell command error: target (%s) is not an entity", target->GetName());
  1051. return 0;
  1052. }
  1053. if (spell_id <= 0) {
  1054. lua_interface->LogError("LUA CastSpell command error: spell id is not valid");
  1055. return 0;
  1056. }
  1057. if (caster && !caster->IsEntity()) {
  1058. lua_interface->LogError("LUA CastSpell command error: caster (%s) is not an entity", caster->GetName());
  1059. return 0;
  1060. }
  1061. if (spell_tier == 0)
  1062. spell_tier = 1;
  1063. if (!caster)
  1064. caster = target;
  1065. target->GetZone()->ProcessSpell(master_spell_list.GetSpell(spell_id, spell_tier), (Entity*)caster, (Entity*)target);
  1066. return 0;
  1067. }
  1068. int EQ2Emu_lua_SpellDamage(lua_State* state){
  1069. if(!lua_interface)
  1070. return 0;
  1071. Spawn* target = lua_interface->GetSpawn(state);
  1072. int32 target_id = 0;
  1073. if (target)
  1074. target_id = target->GetID();
  1075. LuaSpell* luaspell = lua_interface->GetCurrentSpell(state);
  1076. if(!luaspell)
  1077. return 0;
  1078. Spawn* caster = luaspell->caster;
  1079. sint32 type = lua_interface->GetSInt32Value(state, 2);
  1080. int32 min_damage = lua_interface->GetInt32Value(state, 3);
  1081. int32 max_damage = lua_interface->GetInt32Value(state, 4);
  1082. int8 crit_mod = lua_interface->GetInt32Value(state, 5);
  1083. bool no_calcs = lua_interface->GetInt32Value(state, 6) == 1;
  1084. //lua_interface->ResetFunctionStack(state);
  1085. int32 class_id = lua_interface->GetInt32Value(state, 7);
  1086. vector<int16> faction_req;
  1087. vector<int16> race_req;
  1088. int32 class_req = 0;
  1089. int32 i = 0;
  1090. int8 f = 0;
  1091. int8 r = 0;
  1092. while ((class_id = lua_interface->GetInt32Value(state, 7 + i))) {
  1093. if (class_id < 100) {
  1094. class_req += pow(2.0, double(class_id - 1));
  1095. }
  1096. else if (class_id > 100 && class_id < 1000) {
  1097. race_req.push_back(class_id);
  1098. r++;
  1099. }
  1100. else {
  1101. faction_req.push_back(class_id);
  1102. f++;
  1103. }
  1104. i++;
  1105. }
  1106. if(caster && caster->IsEntity()){
  1107. bool race_match = false;
  1108. bool success = false;
  1109. luaspell->resisted = false;
  1110. if (luaspell->initial_target == target_id) {
  1111. int xxx = 0;
  1112. }
  1113. if (luaspell->targets.size() > 0) {
  1114. ZoneServer* zone = luaspell->caster->GetZone();
  1115. Spawn* target = 0;
  1116. luaspell->MSpellTargets.readlock(__FUNCTION__, __LINE__);
  1117. for (int32 i = 0; i < luaspell->targets.size(); i++) {
  1118. if ((target = zone->GetSpawnByID(luaspell->targets[i]))) {
  1119. if (race_req.size() > 0) {
  1120. for (int8 i=0; i < race_req.size(); i++) {
  1121. int32 xxx = target->GetLuaRaceId();
  1122. if (target->GetLuaRaceId() == race_req[i]) {
  1123. race_match = true;
  1124. }
  1125. }
  1126. }
  1127. else
  1128. race_match = true; // if the race_req.size = 0 then there is no race requirement and the race_match will be true
  1129. if (race_match == true) {
  1130. float distance = caster->GetDistance(target, true);
  1131. distance -= caster->appearance.pos.collision_radius / 10;
  1132. distance -= target->appearance.pos.collision_radius / 10;
  1133. ((Entity*)caster)->SpellAttack(target, distance, luaspell, type, min_damage, max_damage, crit_mod, no_calcs);
  1134. }
  1135. }
  1136. }
  1137. success = true;
  1138. luaspell->MSpellTargets.releasereadlock(__FUNCTION__, __LINE__);
  1139. }
  1140. else if (target) {
  1141. //check class and race/faction here
  1142. if (race_req.size() > 0) {
  1143. for (int8 i= 0; i < race_req.size(); i++) {
  1144. if (target->GetLuaRaceId() == race_req[i]) {
  1145. race_match = true;
  1146. }
  1147. }
  1148. }
  1149. else
  1150. race_match = true; // if the race_req.size = 0 then there is no race requirement and the race_match will be true
  1151. if (race_match == true) {
  1152. float distance = caster->GetDistance(target, true);
  1153. distance -= caster->appearance.pos.collision_radius / 10;
  1154. distance -= target->appearance.pos.collision_radius / 10;
  1155. if (((Entity*)caster)->SpellAttack(target, distance, luaspell, type, min_damage, max_damage, crit_mod, no_calcs))
  1156. success = true;
  1157. }
  1158. }
  1159. if (success) {
  1160. Spell* spell = luaspell->spell;
  1161. if(caster->IsPlayer() && spell && spell->GetSpellData()->target_type == 1 && spell->GetSpellData()->spell_book_type == 1){ //offense combat art
  1162. ((Player*)caster)->InCombat(true);
  1163. if(caster->GetZone())
  1164. caster->GetZone()->TriggerCharSheetTimer();
  1165. }
  1166. }
  1167. }
  1168. return 0;
  1169. }
  1170. int EQ2Emu_lua_ModifyPower(lua_State* state){
  1171. if(!lua_interface)
  1172. return 0;
  1173. Spawn* spawn = lua_interface->GetSpawn(state);
  1174. sint32 value = lua_interface->GetSInt32Value(state, 2);
  1175. lua_interface->ResetFunctionStack(state);
  1176. if(spawn && value != 0){
  1177. if (spawn->GetPower() + value > spawn->GetTotalPower())
  1178. spawn->SetPower(spawn->GetTotalPower());
  1179. else
  1180. spawn->SetPower(spawn->GetPower() + value);
  1181. }
  1182. return 0;
  1183. }
  1184. int EQ2Emu_lua_ModifyHP(lua_State* state){
  1185. if(!lua_interface)
  1186. return 0;
  1187. Spawn* spawn = lua_interface->GetSpawn(state);
  1188. sint32 value = lua_interface->GetSInt32Value(state, 2);
  1189. lua_interface->ResetFunctionStack(state);
  1190. if(spawn && value != 0){
  1191. if (spawn->GetHP() + value > spawn->GetTotalHP())
  1192. spawn->SetHP(spawn->GetTotalHP());
  1193. else
  1194. spawn->SetHP(spawn->GetHP() + value);
  1195. }
  1196. return 0;
  1197. }
  1198. int EQ2Emu_lua_ModifyMaxPower(lua_State* state){
  1199. if(!lua_interface)
  1200. return 0;
  1201. Spawn* spawn = lua_interface->GetSpawn(state);
  1202. sint32 value = lua_interface->GetSInt32Value(state, 2);
  1203. lua_interface->ResetFunctionStack(state);
  1204. if(spawn && value != 0){
  1205. spawn->SetPower(spawn->GetPower() + value);
  1206. if(value > spawn->GetTotalHPBase())
  1207. spawn->SetTotalPower(spawn->GetTotalPower() + value);
  1208. }
  1209. return 0;
  1210. }
  1211. int EQ2Emu_lua_ModifyMaxHP(lua_State* state){
  1212. if(!lua_interface)
  1213. return 0;
  1214. Spawn* spawn = lua_interface->GetSpawn(state);
  1215. sint32 value = lua_interface->GetSInt32Value(state, 2);
  1216. lua_interface->ResetFunctionStack(state);
  1217. if(spawn && value != 0){
  1218. spawn->SetHP(spawn->GetHP() + value);
  1219. if(value > spawn->GetTotalHPBase())
  1220. spawn->SetTotalHP(spawn->GetTotalHP() + value);
  1221. }
  1222. return 0;
  1223. }
  1224. int EQ2Emu_lua_SetCurrentHP(lua_State* state){
  1225. if(!lua_interface)
  1226. return 0;
  1227. Spawn* spawn = lua_interface->GetSpawn(state);
  1228. sint32 value = lua_interface->GetSInt32Value(state, 2);
  1229. lua_interface->ResetFunctionStack(state);
  1230. if(spawn && value > 0){
  1231. spawn->SetHP(value);
  1232. if(value > spawn->GetTotalHPBase())
  1233. spawn->SetTotalHP(value);
  1234. }
  1235. return 0;
  1236. }
  1237. int EQ2Emu_lua_SetMaxHP(lua_State* state){
  1238. if(!lua_interface)
  1239. return 0;
  1240. Spawn* spawn = lua_interface->GetSpawn(state);
  1241. float value = lua_interface->GetFloatValue(state, 2);
  1242. lua_interface->ResetFunctionStack(state);
  1243. if (spawn && spawn->IsEntity() && value > 0)
  1244. ((Entity*)spawn)->AddSpellBonus(0, ITEM_STAT_HEALTH, value - spawn->GetTotalHP());
  1245. if (spawn->IsPlayer())
  1246. ((Player*)spawn)->SetCharSheetChanged(true);
  1247. return 0;
  1248. }
  1249. int EQ2Emu_lua_SetMaxHPBase(lua_State* state){
  1250. if(!lua_interface)
  1251. return 0;
  1252. Spawn* spawn = lua_interface->GetSpawn(state);
  1253. sint32 value = lua_interface->GetSInt32Value(state, 2);
  1254. lua_interface->ResetFunctionStack(state);
  1255. if(spawn && spawn->IsEntity() && value > 0)
  1256. ((Entity*)spawn)->SetTotalHPBase(value);
  1257. return 0;
  1258. }
  1259. int EQ2Emu_lua_SetCurrentPower(lua_State* state){
  1260. if(!lua_interface)
  1261. return 0;
  1262. Spawn* spawn = lua_interface->GetSpawn(state);
  1263. sint32 value = lua_interface->GetSInt32Value(state, 2);
  1264. lua_interface->ResetFunctionStack(state);
  1265. if(spawn && value > 0){
  1266. spawn->SetPower(value);
  1267. if(value > spawn->GetTotalPowerBase())
  1268. spawn->SetTotalPower(value);
  1269. }
  1270. return 0;
  1271. }
  1272. int EQ2Emu_lua_SetMaxPower(lua_State* state){
  1273. if(!lua_interface)
  1274. return 0;
  1275. Spawn* spawn = lua_interface->GetSpawn(state);
  1276. sint32 value = lua_interface->GetSInt32Value(state, 2);
  1277. lua_interface->ResetFunctionStack(state);
  1278. if(spawn && spawn->IsEntity() && value > 0)
  1279. ((Entity*)spawn)->AddSpellBonus(0, ITEM_STAT_POWER, value - spawn->GetTotalPower());
  1280. return 0;
  1281. }
  1282. int EQ2Emu_lua_SetMaxPowerBase(lua_State* state){
  1283. if(!lua_interface)
  1284. return 0;
  1285. Spawn* spawn = lua_interface->GetSpawn(state);
  1286. sint32 value = lua_interface->GetSInt32Value(state, 2);
  1287. lua_interface->ResetFunctionStack(state);
  1288. if(spawn && spawn->IsEntity() && value > 0)
  1289. ((Entity*)spawn)->SetTotalPowerBase(value);
  1290. return 0;
  1291. }
  1292. int EQ2Emu_lua_SetPosition(lua_State* state){
  1293. if(!lua_interface)
  1294. return 0;
  1295. Spawn* spawn = lua_interface->GetSpawn(state);
  1296. float x = lua_interface->GetFloatValue(state, 2);
  1297. float y = lua_interface->GetFloatValue(state, 3);
  1298. float z = lua_interface->GetFloatValue(state, 4);
  1299. float heading = lua_interface->GetFloatValue(state, 5);
  1300. lua_interface->ResetFunctionStack(state);
  1301. if(spawn){
  1302. spawn->SetX(x);
  1303. spawn->SetY(y);
  1304. spawn->SetZ(z);
  1305. if (heading != 0)
  1306. spawn->SetHeading(heading);
  1307. spawn->SetSpawnOrigX(spawn->GetX());
  1308. spawn->SetSpawnOrigY(spawn->GetY());
  1309. spawn->SetSpawnOrigZ(spawn->GetZ());
  1310. spawn->SetSpawnOrigHeading(spawn->GetHeading());
  1311. if(spawn->IsPlayer()){
  1312. Client* client = spawn->GetZone()->GetClientBySpawn(spawn);
  1313. if(client){
  1314. EQ2Packet* packet = client->GetPlayer()->Move(x, y, z, client->GetVersion(), (heading == 0 ? -1.0f : (heading + 180.0f)));
  1315. client->QueuePacket(packet);
  1316. }
  1317. }
  1318. }
  1319. return 0;
  1320. }
  1321. int EQ2Emu_lua_SetHeading(lua_State* state){
  1322. if(!lua_interface)
  1323. return 0;
  1324. Spawn* spawn = lua_interface->GetSpawn(state);
  1325. float value = lua_interface->GetFloatValue(state, 2);
  1326. lua_interface->ResetFunctionStack(state);
  1327. if (spawn) {
  1328. spawn->SetHeading(value);
  1329. if (spawn->IsPlayer()) {
  1330. Client* client = spawn->GetZone()->GetClientBySpawn(spawn);
  1331. if (client) {
  1332. EQ2Packet* packet = client->GetPlayer()->Move(spawn->GetX(), spawn->GetY(), spawn->GetZ(), client->GetVersion(), value + 180.0f);
  1333. client->QueuePacket(packet);
  1334. }
  1335. }
  1336. }
  1337. return 0;
  1338. }
  1339. int EQ2Emu_lua_SetModelType(lua_State* state){
  1340. if(!lua_interface)
  1341. return 0;
  1342. Spawn* spawn = lua_interface->GetSpawn(state);
  1343. int16 value = lua_interface->GetInt16Value(state, 2);
  1344. lua_interface->ResetFunctionStack(state);
  1345. if(spawn)
  1346. spawn->SetModelType(value);
  1347. return 0;
  1348. }
  1349. int EQ2Emu_lua_SetAdventureClass(lua_State* state){
  1350. if(!lua_interface)
  1351. return 0;
  1352. Spawn* spawn = lua_interface->GetSpawn(state);
  1353. int8 value = lua_interface->GetInt8Value(state, 2);
  1354. lua_interface->ResetFunctionStack(state);
  1355. if(spawn){
  1356. if(spawn->IsPlayer())
  1357. ((Player*)spawn)->SetPlayerAdventureClass(value);
  1358. else
  1359. spawn->SetAdventureClass(value);
  1360. }
  1361. return 0;
  1362. }
  1363. int EQ2Emu_lua_SetTradeskillClass(lua_State* state){
  1364. if(!lua_interface)
  1365. return 0;
  1366. Spawn* spawn = lua_interface->GetSpawn(state);
  1367. int8 value = lua_interface->GetInt8Value(state, 2);
  1368. lua_interface->ResetFunctionStack(state);
  1369. if(spawn) {
  1370. spawn->SetTradeskillClass(value);
  1371. if (spawn->IsEntity()) {
  1372. ((Entity*)spawn)->GetInfoStruct()->tradeskill_class1 = classes.GetTSBaseClass(spawn->GetTradeskillClass());
  1373. ((Entity*)spawn)->GetInfoStruct()->tradeskill_class2 = classes.GetSecondaryTSBaseClass(spawn->GetTradeskillClass());
  1374. ((Entity*)spawn)->GetInfoStruct()->tradeskill_class3 = spawn->GetTradeskillClass();
  1375. }
  1376. if (spawn->IsPlayer())
  1377. ((Player*)spawn)->SetCharSheetChanged(true);
  1378. }
  1379. return 0;
  1380. }
  1381. int EQ2Emu_lua_SetMount(lua_State* state){
  1382. if(!lua_interface)
  1383. return 0;
  1384. Spawn* spawn = lua_interface->GetSpawn(state);
  1385. int16 value = lua_interface->GetInt16Value(state, 2);
  1386. if(spawn && spawn->IsEntity()){
  1387. ((Entity*)spawn)->SetMount(value);
  1388. EQ2_Color color;
  1389. color.red = 255;
  1390. color.green = 255;
  1391. color.blue = 255;
  1392. ((Entity*)spawn)->SetMountColor(&color);
  1393. ((Entity*)spawn)->SetMountSaddleColor(&color);
  1394. }
  1395. return 0;
  1396. }
  1397. int EQ2Emu_lua_SetMountColor(lua_State* state){
  1398. if(!lua_interface)
  1399. return 0;
  1400. Spawn* spawn = lua_interface->GetSpawn(state);
  1401. EQ2_Color mount_color;
  1402. EQ2_Color saddle_color;
  1403. mount_color.red = lua_interface->GetInt8Value(state, 2);
  1404. mount_color.green = lua_interface->GetInt8Value(state, 3);
  1405. mount_color.blue = lua_interface->GetInt8Value(state, 4);
  1406. saddle_color.red = lua_interface->GetInt8Value(state, 5);
  1407. saddle_color.green = lua_interface->GetInt8Value(state, 6);
  1408. saddle_color.blue = lua_interface->GetInt8Value(state, 7);
  1409. if(spawn && spawn->IsEntity()){
  1410. ((Entity*)spawn)->SetMountColor(&mount_color);
  1411. ((Entity*)spawn)->SetMountSaddleColor(&saddle_color);
  1412. }
  1413. return 0;
  1414. }
  1415. int EQ2Emu_lua_GetMount(lua_State* state){
  1416. if(!lua_interface)
  1417. return 0;
  1418. Spawn* spawn = lua_interface->GetSpawn(state);
  1419. if(spawn && spawn->IsEntity()){
  1420. lua_interface->SetInt32Value(state, ((Entity*)spawn)->GetMount());
  1421. return 1;
  1422. }
  1423. return 0;
  1424. }
  1425. int EQ2Emu_lua_GetRace(lua_State* state){
  1426. if(!lua_interface)
  1427. return 0;
  1428. Spawn* spawn = lua_interface->GetSpawn(state);
  1429. if(spawn)
  1430. {
  1431. LogWrite(LUA__DEBUG, 0, "LUA", "%s - Race: %i", __FUNCTION__, spawn->GetRace());
  1432. lua_interface->SetInt32Value(state, spawn->GetRace());
  1433. return 1;
  1434. }
  1435. return 0;
  1436. }
  1437. int EQ2Emu_lua_GetRaceName(lua_State* state){
  1438. if(!lua_interface)
  1439. return 0;
  1440. Spawn* spawn = lua_interface->GetSpawn(state);
  1441. if(spawn) {
  1442. lua_interface->SetStringValue(state, races.GetRaceName(spawn->GetRace()));
  1443. return 1;
  1444. }
  1445. return 0;
  1446. }
  1447. int EQ2Emu_lua_GetClass(lua_State* state) {
  1448. Spawn* spawn = lua_interface->GetSpawn(state);
  1449. if (spawn) {
  1450. lua_interface->SetInt32Value(state, spawn->GetAdventureClass());
  1451. return 1;
  1452. }
  1453. return 0;
  1454. }
  1455. int EQ2Emu_lua_GetClassName(lua_State* state) {
  1456. Spawn* spawn = lua_interface->GetSpawn(state);
  1457. if (spawn) {
  1458. lua_interface->SetStringValue(state, classes.GetClassName(spawn->GetAdventureClass()));
  1459. return 1;
  1460. }
  1461. return 0;
  1462. }
  1463. int EQ2Emu_lua_SetSpeed(lua_State* state){
  1464. if(!lua_interface)
  1465. return 0;
  1466. Spawn* spawn = lua_interface->GetSpawn(state);
  1467. float value = lua_interface->GetFloatValue(state, 2);
  1468. lua_interface->ResetFunctionStack(state);
  1469. if(spawn){
  1470. spawn->SetSpeed(value);
  1471. if(spawn->IsPlayer()){
  1472. Client* client = spawn->GetZone()->GetClientBySpawn(spawn);
  1473. if(client){
  1474. PacketStruct* packet = configReader.getStruct("WS_SetControlGhost", client->GetVersion());
  1475. if(packet){
  1476. packet->setDataByName("spawn_id", 0xFFFFFFFF);
  1477. packet->setDataByName("speed", value);
  1478. packet->setDataByName("size", 0.51);
  1479. EQ2Packet* app = packet->serialize();
  1480. client->QueuePacket(app);
  1481. safe_delete(packet);
  1482. }
  1483. }
  1484. }
  1485. }
  1486. return 0;
  1487. }
  1488. int EQ2Emu_lua_AddSpellBonus(lua_State* state){
  1489. if(!lua_interface)
  1490. return 0;
  1491. Spawn* spawn = lua_interface->GetSpawn(state);
  1492. const int16 type = lua_interface->GetInt16Value(state, 2);
  1493. const float value = lua_interface->GetFloatValue(state, 3);
  1494. LuaSpell* luaspell = lua_interface->GetCurrentSpell(state);
  1495. int64 class_req = 0;
  1496. int32 class_id = 0;
  1497. vector<int16> faction_req;
  1498. vector<int16> race_req;
  1499. int32 i = 0;
  1500. int8 f = 0;
  1501. int8 r = 0;
  1502. while((class_id = lua_interface->GetInt32Value(state, 4 + i))) {
  1503. if (class_id < 100) {
  1504. class_req += pow(2.0, double(class_id - 1));
  1505. }
  1506. else if (class_id > 100 && class_id < 1000) {
  1507. race_req.push_back(class_id);
  1508. r++;
  1509. }
  1510. else {
  1511. faction_req.push_back(class_id);
  1512. f++;
  1513. }
  1514. i++;
  1515. }
  1516. if (value != 0 && type >= 0) {
  1517. if (luaspell && luaspell->spell && luaspell->caster) {
  1518. ZoneServer* zone = luaspell->caster->GetZone();
  1519. luaspell->MSpellTargets.readlock(__FUNCTION__, __LINE__);
  1520. for (int32 i = 0; i < luaspell->targets.size(); i++) {
  1521. Spawn* target = zone->GetSpawnByID(luaspell->targets[i]);
  1522. if (target) {
  1523. if (target->IsPlayer()) {
  1524. ((Player*)target)->AddSpellBonus(luaspell, type, value, class_req, race_req, faction_req);
  1525. LogWrite(LUA__DEBUG, 0, "LUA", "Applying Spell Bonus to Player '%s'. Is a Group Member.", ((Player*)target)->GetName());
  1526. if (((Player*)target)->GetGroupMemberInfo())
  1527. ((Player*)target)->UpdateGroupMemberInfo();
  1528. ((Player*)target)->SetCharSheetChanged(true);
  1529. }
  1530. else if (target->IsNPC())
  1531. ((NPC*)target)->AddSpellBonus(luaspell, type, value, class_req,race_req, faction_req);
  1532. else
  1533. lua_interface->SimpleLogError("Error applying spell bonus on non entity.");
  1534. }
  1535. }
  1536. luaspell->MSpellTargets.releasereadlock(__FUNCTION__, __LINE__);
  1537. if (!(luaspell->effect_bitmask & EFFECT_FLAG_SPELLBONUS))
  1538. luaspell->effect_bitmask += EFFECT_FLAG_SPELLBONUS;
  1539. }
  1540. else if (spawn && spawn->IsEntity()) {
  1541. ((Entity*)spawn)->AddSpellBonus(luaspell, type, value, class_req, race_req, faction_req);
  1542. LogWrite(LUA__DEBUG, 0, "LUA", "Applying Spell Bonus to Entity '%s'. Is a Group Member.", ((Entity*)spawn)->GetName());
  1543. if(spawn->IsPlayer())
  1544. ((Player*)spawn)->SetCharSheetChanged(true);
  1545. }
  1546. else
  1547. lua_interface->SimpleLogError("Unable to apply spell bonus in AddSpellBonus.");
  1548. }
  1549. else
  1550. lua_interface->SimpleLogError("Invalid parameters for AddSpellBonus.");
  1551. return 0;
  1552. }
  1553. int EQ2Emu_lua_AddSpawnSpellBonus(lua_State* state){
  1554. if(!lua_interface)
  1555. return 0;
  1556. Spawn* spawn = lua_interface->GetSpawn(state);
  1557. int16 type = lua_interface->GetInt16Value(state, 2);
  1558. sint32 value = lua_interface->GetSInt32Value(state, 3);
  1559. LuaSpell* luaspell = lua_interface->GetCurrentSpell(state);
  1560. if (!spawn) {
  1561. lua_interface->LogError("LUA AddSpawnSpellBonus command error: spawn is not valid");
  1562. return 0;
  1563. }
  1564. if (!spawn->IsEntity()) {
  1565. lua_interface->LogError("LUA AddSpawnSpellBonus command error: spawn is not an entity");
  1566. return 0;
  1567. }
  1568. if (value == 0) {
  1569. lua_interface->LogError("LUA AddSpawnSpellBonus command error: value must be set");
  1570. return 0;
  1571. }
  1572. if (!luaspell || !luaspell->spell) {
  1573. lua_interface->LogError("LUA AddSpawnSpellBonus command error: can only be used in a spell script");
  1574. return 0;
  1575. }
  1576. int32 class_req = 0;
  1577. vector<int16> faction_req;
  1578. vector<int16> race_req;
  1579. int32 class_id = 0;
  1580. int32 i = 0;
  1581. int8 f = 0;
  1582. int8 r = 0;
  1583. while ((class_id = lua_interface->GetInt32Value(state, 4 + i))) {
  1584. if (class_id < 100) {
  1585. class_req += pow(2.0, double(class_id - 1));
  1586. }
  1587. else if (class_id > 100 && class_id < 1000) {
  1588. race_req.push_back(class_id);
  1589. r++;
  1590. }
  1591. else {
  1592. faction_req.push_back(class_id);
  1593. f++;
  1594. }
  1595. i++;
  1596. }
  1597. ((Entity*)spawn)->AddSpellBonus(luaspell, type, value, class_req,race_req,faction_req);
  1598. if (!(luaspell->effect_bitmask & EFFECT_FLAG_SPELLBONUS))
  1599. luaspell->effect_bitmask += EFFECT_FLAG_SPELLBONUS;
  1600. if(spawn->IsPlayer())
  1601. ((Player*)spawn)->SetCharSheetChanged(true);
  1602. return 0;
  1603. }
  1604. int EQ2Emu_lua_RemoveSpellBonus(lua_State* state){
  1605. if(!lua_interface)
  1606. return 0;
  1607. Spawn* spawn = lua_interface->GetSpawn(state);
  1608. LuaSpell* luaspell = lua_interface->GetCurrentSpell(state);
  1609. if(luaspell && luaspell->spell) {
  1610. ZoneServer* zone = luaspell->caster->GetZone();
  1611. Spawn* target = 0;
  1612. luaspell->MSpellTargets.readlock(__FUNCTION__, __LINE__);
  1613. for (int32 i = 0; i < luaspell->targets.size(); i++) {
  1614. target = zone->GetSpawnByID(luaspell->targets[i]);
  1615. if (target && target->IsEntity()) {
  1616. ((Entity*)target)->RemoveSpellBonus(luaspell);
  1617. if (target->IsPlayer())
  1618. ((Player*)target)->SetCharSheetChanged(true);
  1619. }
  1620. }
  1621. luaspell->MSpellTargets.releasereadlock(__FUNCTION__, __LINE__);
  1622. }
  1623. else if (spawn && spawn->IsEntity()) {
  1624. ((Entity*)spawn)->RemoveSpellBonus(luaspell);
  1625. if (spawn->IsPlayer())
  1626. ((Player*)spawn)->SetCharSheetChanged(true);
  1627. }
  1628. return 0;
  1629. }
  1630. int EQ2Emu_lua_AddSkillBonus(lua_State* state) {
  1631. if(!lua_interface)
  1632. return 0;
  1633. Spawn* spawn = lua_interface->GetSpawn(state);
  1634. int32 skill_id = lua_interface->GetInt32Value(state, 2);
  1635. float value = lua_interface->GetFloatValue(state, 3);
  1636. LuaSpell* luaspell = lua_interface->GetCurrentSpell(state);
  1637. if (value != 0) {
  1638. int32 spell_id = 0;
  1639. if (luaspell && luaspell->spell && luaspell->caster) {
  1640. spell_id = luaspell->spell->GetSpellID();
  1641. ZoneServer* zone = luaspell->caster->GetZone();
  1642. Spawn* target = 0;
  1643. luaspell->MSpellTargets.readlock(__FUNCTION__, __LINE__);
  1644. for (int32 i = 0; i < luaspell->targets.size(); i++) {
  1645. target = zone->GetSpawnByID(luaspell->targets[i]);
  1646. if (target && target->Alive()) {
  1647. if (target->IsPlayer()) {
  1648. ((Player*)target)->AddSkillBonus(spell_id, skill_id, value);
  1649. Client* client = target->GetZone()->GetClientBySpawn(target);
  1650. if (client) {
  1651. EQ2Packet* packet = ((Player*)target)->GetSkills()->GetSkillPacket(client->GetVersion());
  1652. if (packet)
  1653. client->QueuePacket(packet);
  1654. }
  1655. if (!(luaspell->effect_bitmask & EFFECT_FLAG_SKILLBONUS))
  1656. luaspell->effect_bitmask += EFFECT_FLAG_SKILLBONUS;
  1657. }
  1658. else if (target->IsNPC()){
  1659. ((NPC*)target)->AddSkillBonus(spell_id, skill_id, value);
  1660. if (!(luaspell->effect_bitmask & EFFECT_FLAG_SKILLBONUS))
  1661. luaspell->effect_bitmask += EFFECT_FLAG_SKILLBONUS;
  1662. }
  1663. else
  1664. LogWrite(LUA__ERROR, 0, "LUA", "Error applying bonus buff on '%s'. Not a NPC or player.", target->GetName());
  1665. }
  1666. }
  1667. luaspell->MSpellTargets.releasereadlock(__FUNCTION__, __LINE__);
  1668. }
  1669. else if (spawn) {
  1670. if (spawn->IsPlayer()) {
  1671. ((Player*)spawn)->AddSkillBonus(spell_id, skill_id, value);
  1672. Client* client = spawn->GetZone()->GetClientBySpawn(spawn);
  1673. if (client) {
  1674. EQ2Packet* packet = ((Player*)spawn)->GetSkills()->GetSkillPacket(client->GetVersion());
  1675. if (packet)
  1676. client->QueuePacket(packet);
  1677. }
  1678. }
  1679. else if (spawn->IsNPC())
  1680. ((NPC*)spawn)->AddSkillBonus(spell_id, skill_id, value);
  1681. else
  1682. LogWrite(LUA__ERROR, 0, "LUA", "Error applying skill bonus on '%s'. Not a NPC or player.", spawn->GetName());
  1683. }
  1684. }
  1685. else
  1686. lua_interface->SimpleLogError("Invalid parameters for AddSkillBonus.");
  1687. return 0;
  1688. }
  1689. int EQ2Emu_lua_RemoveSkillBonus(lua_State* state) {
  1690. if (!lua_interface)
  1691. return 0;
  1692. Spawn* spawn = lua_interface->GetSpawn(state);
  1693. LuaSpell* luaspell = lua_interface->GetCurrentSpell(state);
  1694. if (spawn && spawn->IsPlayer()) {
  1695. int32 spell_id = 0;
  1696. if(luaspell && luaspell->spell) {
  1697. spell_id = luaspell->spell->GetSpellID();
  1698. ZoneServer* zone = luaspell->caster->GetZone();
  1699. Spawn* target = 0;
  1700. luaspell->MSpellTargets.readlock(__FUNCTION__, __LINE__);
  1701. for (int32 i = 0; i < luaspell->targets.size(); i++) {
  1702. target = zone->GetSpawnByID(luaspell->targets[i]);
  1703. if (target) {
  1704. if (target->IsPlayer()) {
  1705. ((Player*)target)->RemoveSkillBonus(spell_id);
  1706. Client* client = target->GetZone()->GetClientBySpawn(target);
  1707. if (client) {
  1708. EQ2Packet* packet = ((Player*)target)->GetSkills()->GetSkillPacket(client->GetVersion());
  1709. if (packet)
  1710. client->QueuePacket(packet);
  1711. }
  1712. }
  1713. else if (target->IsNPC())
  1714. ((NPC*)target)->RemoveSkillBonus(spell_id);
  1715. else
  1716. LogWrite(LUA__ERROR, 0, "LUA", "Error removing skill bonus on '%s'. Not a NPC or player.", spawn->GetName());
  1717. }
  1718. }
  1719. luaspell->MSpellTargets.releasereadlock(__FUNCTION__, __LINE__);
  1720. }
  1721. else if (spawn) {
  1722. if (spawn->IsPlayer()) {
  1723. ((Player*)spawn)->RemoveSkillBonus(spell_id);
  1724. Client* client = spawn->GetZone()->GetClientBySpawn(spawn);
  1725. if (client) {
  1726. EQ2Packet* packet = ((Player*)spawn)->GetSkills()->GetSkillPacket(client->GetVersion());
  1727. if (packet)
  1728. client->QueuePacket(packet);
  1729. }
  1730. }
  1731. else if (spawn->IsNPC())
  1732. ((NPC*)spawn)->RemoveSkillBonus(spell_id);
  1733. else
  1734. LogWrite(LUA__ERROR, 0, "LUA", "Error removing skill bonus on '%s'. Not a NPC or player.", spawn->GetName());
  1735. }
  1736. }
  1737. return 0;
  1738. }
  1739. int EQ2Emu_lua_AddControlEffect(lua_State* state) {
  1740. if(!lua_interface)
  1741. return 0;
  1742. Spawn* spawn = lua_interface->GetSpawn(state);
  1743. int8 type = lua_interface->GetInt32Value(state, 2);
  1744. bool only_add_spawn = lua_interface->GetInt8Value(state, 3) == 1;
  1745. LuaSpell* luaspell = lua_interface->GetCurrentSpell(state);
  1746. if (!only_add_spawn && luaspell && luaspell->spell && luaspell->caster && type != 0) {
  1747. ZoneServer* zone = luaspell->caster->GetZone();
  1748. Spawn* target = 0;
  1749. luaspell->MSpellTargets.readlock(__FUNCTION__, __LINE__);
  1750. for (int32 i = 0; i < luaspell->targets.size(); i++) {
  1751. target = zone->GetSpawnByID(luaspell->targets[i]);
  1752. if (target && target->IsEntity()) {
  1753. if (type == CONTROL_EFFECT_TYPE_MEZ) {
  1754. ((Entity*)target)->AddMezSpell(luaspell);
  1755. if (!(luaspell->effect_bitmask & EFFECT_FLAG_MEZ))
  1756. luaspell->effect_bitmask += EFFECT_FLAG_MEZ;
  1757. if (target->IsNPC())
  1758. ((NPC*)target)->Brain()->AddHate(luaspell->caster, 5);
  1759. }
  1760. else if (type == CONTROL_EFFECT_TYPE_STIFLE) {
  1761. ((Entity*)target)->AddStifleSpell(luaspell);
  1762. if (!(luaspell->effect_bitmask & EFFECT_FLAG_STIFLE))
  1763. luaspell->effect_bitmask += EFFECT_FLAG_STIFLE;
  1764. if (target->IsNPC())
  1765. ((NPC*)target)->Brain()->AddHate(luaspell->caster, 5);
  1766. }
  1767. else if (type == CONTROL_EFFECT_TYPE_DAZE) {
  1768. ((Entity*)target)->AddDazeSpell(luaspell);
  1769. if (!(luaspell->effect_bitmask & EFFECT_FLAG_DAZE))
  1770. luaspell->effect_bitmask += EFFECT_FLAG_DAZE;
  1771. if (target->IsNPC())
  1772. ((NPC*)target)->Brain()->AddHate(luaspell->caster, 5);
  1773. }
  1774. else if (type == CONTROL_EFFECT_TYPE_STUN) {
  1775. if (!(luaspell->effect_bitmask & EFFECT_FLAG_STUN))
  1776. luaspell->effect_bitmask += EFFECT_FLAG_STUN;
  1777. ((Entity*)target)->AddStunSpell(luaspell);
  1778. if (target->IsNPC())
  1779. ((NPC*)target)->Brain()->AddHate(luaspell->caster, 5);
  1780. }
  1781. else if (type == CONTROL_EFFECT_TYPE_ROOT) {
  1782. if (!(luaspell->effect_bitmask & EFFECT_FLAG_ROOT))
  1783. luaspell->effect_bitmask += EFFECT_FLAG_ROOT;
  1784. ((Entity*)target)->AddRootSpell(luaspell);
  1785. if (target->IsNPC())
  1786. ((NPC*)target)->Brain()->AddHate(luaspell->caster, 5);
  1787. }
  1788. else if (type == CONTROL_EFFECT_TYPE_FEAR){
  1789. if (!(luaspell->effect_bitmask & EFFECT_FLAG_FEAR))
  1790. luaspell->effect_bitmask += EFFECT_FLAG_FEAR;
  1791. ((Entity*)target)->AddFearSpell(luaspell);
  1792. if (target->IsNPC())
  1793. ((NPC*)target)->Brain()->AddHate(luaspell->caster, 5);
  1794. }
  1795. else if (type == CONTROL_EFFECT_TYPE_WALKUNDERWATER){
  1796. ((Entity*)target)->AddWaterwalkSpell(luaspell);
  1797. if (!(luaspell->effect_bitmask & EFFECT_FLAG_WATERWALK))
  1798. luaspell->effect_bitmask += EFFECT_FLAG_WATERWALK;
  1799. }
  1800. else if (type == CONTROL_EFFECT_TYPE_JUMPUNDERWATER){
  1801. ((Entity*)target)->AddWaterjumpSpell(luaspell);
  1802. if (!(luaspell->effect_bitmask & EFFECT_FLAG_WATERJUMP))
  1803. luaspell->effect_bitmask += EFFECT_FLAG_WATERJUMP;
  1804. }
  1805. else if (type == CONTROL_EFFECT_TYPE_SNARE) {
  1806. ((Entity*)target)->AddSnareSpell(luaspell);
  1807. if (!(luaspell->effect_bitmask & EFFECT_FLAG_SNARE))
  1808. luaspell->effect_bitmask += EFFECT_FLAG_SNARE;
  1809. if (target->IsNPC())
  1810. ((NPC*)target)->Brain()->AddHate(luaspell->caster, 5);
  1811. }
  1812. else if (type == CONTROL_EFFECT_TYPE_FLIGHT){
  1813. ((Entity*)target)->AddFlightSpell(luaspell);
  1814. if (!(luaspell->effect_bitmask & EFFECT_FLAG_FLIGHT))
  1815. luaspell->effect_bitmask += EFFECT_FLAG_FLIGHT;
  1816. }
  1817. else if (type == CONTROL_EFFECT_TYPE_GLIDE){
  1818. ((Entity*)target)->AddGlideSpell(luaspell);
  1819. if (!(luaspell->effect_bitmask & EFFECT_FLAG_GLIDE))
  1820. luaspell->effect_bitmask += EFFECT_FLAG_GLIDE;
  1821. }
  1822. else if (type == CONTROL_EFFECT_TYPE_SAFEFALL){
  1823. ((Entity*)target)->AddSafefallSpell(luaspell);
  1824. if (!(luaspell->effect_bitmask & EFFECT_FLAG_SAFEFALL))
  1825. luaspell->effect_bitmask += EFFECT_FLAG_SAFEFALL;
  1826. }
  1827. else
  1828. lua_interface->LogError("Unhandled control effect type of %u.", type);
  1829. }
  1830. else
  1831. lua_interface->LogError("Error applying control effect on non entity '%s'.", target->GetName());
  1832. }
  1833. luaspell->MSpellTargets.releasereadlock(__FUNCTION__, __LINE__);
  1834. }
  1835. else if (only_add_spawn && spawn && spawn->IsEntity()) {
  1836. if (type == CONTROL_EFFECT_TYPE_MEZ){
  1837. ((Entity*)spawn)->AddMezSpell(luaspell);
  1838. if (!(luaspell->effect_bitmask & EFFECT_FLAG_MEZ))
  1839. luaspell->effect_bitmask += EFFECT_FLAG_MEZ;
  1840. }
  1841. else if (type == CONTROL_EFFECT_TYPE_STIFLE){
  1842. ((Entity*)spawn)->AddStifleSpell(luaspell);
  1843. if (!(luaspell->effect_bitmask & EFFECT_FLAG_STIFLE))
  1844. luaspell->effect_bitmask += EFFECT_FLAG_STIFLE;
  1845. }
  1846. else if (type == CONTROL_EFFECT_TYPE_DAZE){
  1847. ((Entity*)spawn)->AddDazeSpell(luaspell);
  1848. if (!(luaspell->effect_bitmask & EFFECT_FLAG_DAZE))
  1849. luaspell->effect_bitmask += EFFECT_FLAG_DAZE;
  1850. }
  1851. else if (type == CONTROL_EFFECT_TYPE_STUN){
  1852. ((Entity*)spawn)->AddStunSpell(luaspell);
  1853. if (!(luaspell->effect_bitmask & EFFECT_FLAG_STUN))
  1854. luaspell->effect_bitmask += EFFECT_FLAG_STUN;
  1855. }
  1856. else if (type == CONTROL_EFFECT_TYPE_ROOT){
  1857. ((Entity*)spawn)->AddRootSpell(luaspell);
  1858. if (!(luaspell->effect_bitmask & EFFECT_FLAG_ROOT))
  1859. luaspell->effect_bitmask += EFFECT_FLAG_ROOT;
  1860. }
  1861. else if (type == CONTROL_EFFECT_TYPE_FEAR){
  1862. ((Entity*)spawn)->AddFearSpell(luaspell);
  1863. if (!(luaspell->effect_bitmask & EFFECT_FLAG_FEAR))
  1864. luaspell->effect_bitmask += EFFECT_FLAG_FEAR;
  1865. }
  1866. else if (type == CONTROL_EFFECT_TYPE_WALKUNDERWATER){
  1867. ((Entity*)spawn)->AddWaterwalkSpell(luaspell);
  1868. if (!(luaspell->effect_bitmask & EFFECT_FLAG_WATERWALK))
  1869. luaspell->effect_bitmask += EFFECT_FLAG_WATERWALK;
  1870. }
  1871. else if (type == CONTROL_EFFECT_TYPE_JUMPUNDERWATER){
  1872. ((Entity*)spawn)->AddWaterjumpSpell(luaspell);
  1873. if (!(luaspell->effect_bitmask & EFFECT_FLAG_WATERJUMP))
  1874. luaspell->effect_bitmask += EFFECT_FLAG_WATERJUMP;
  1875. }
  1876. else if (type == CONTROL_EFFECT_TYPE_SNARE){
  1877. ((Entity*)spawn)->AddSnareSpell(luaspell);
  1878. if (!(luaspell->effect_bitmask & EFFECT_FLAG_SNARE))
  1879. luaspell->effect_bitmask += EFFECT_FLAG_SNARE;
  1880. }
  1881. else if (type == CONTROL_EFFECT_TYPE_FLIGHT){
  1882. ((Entity*)spawn)->AddFlightSpell(luaspell);
  1883. if (!(luaspell->effect_bitmask & EFFECT_FLAG_FLIGHT))
  1884. luaspell->effect_bitmask += EFFECT_FLAG_FLIGHT;
  1885. }
  1886. else if (type == CONTROL_EFFECT_TYPE_GLIDE){
  1887. ((Entity*)spawn)->AddGlideSpell(luaspell);
  1888. if (!(luaspell->effect_bitmask & EFFECT_FLAG_GLIDE))
  1889. luaspell->effect_bitmask += EFFECT_FLAG_GLIDE;
  1890. }
  1891. else if (type == CONTROL_EFFECT_TYPE_SAFEFALL){
  1892. ((Entity*)spawn)->AddSafefallSpell(luaspell);
  1893. if (!(luaspell->effect_bitmask & EFFECT_FLAG_SAFEFALL))
  1894. luaspell->effect_bitmask += EFFECT_FLAG_SAFEFALL;
  1895. }
  1896. else
  1897. lua_interface->LogError("Unhandled control effect type of %u.", type);
  1898. }
  1899. else
  1900. lua_interface->LogError("Error applying control effect on non entity '%s'.", spawn->GetName());
  1901. return 0;
  1902. }
  1903. int EQ2Emu_lua_RemoveControlEffect(lua_State* state) {
  1904. if(!lua_interface)
  1905. return 0;
  1906. Spawn* spawn = lua_interface->GetSpawn(state);
  1907. int8 type = lua_interface->GetInt8Value(state, 2);
  1908. bool only_remove_spawn = lua_interface->GetInt8Value(state, 3) == 1;
  1909. LuaSpell* luaspell = lua_interface->GetCurrentSpell(state);
  1910. if (spawn && spawn->IsEntity()) {
  1911. if (!only_remove_spawn && luaspell && luaspell->spell) {
  1912. ZoneServer* zone = luaspell->caster->GetZone();
  1913. Spawn* target = 0;
  1914. luaspell->MSpellTargets.readlock(__FUNCTION__, __LINE__);
  1915. for (int32 i = 0; i < luaspell->targets.size(); i++) {
  1916. target = zone->GetSpawnByID(luaspell->targets[i]);
  1917. if (target) {
  1918. if (type == CONTROL_EFFECT_TYPE_MEZ)
  1919. ((Entity*)target)->RemoveMezSpell(luaspell);
  1920. else if (type == CONTROL_EFFECT_TYPE_STIFLE)
  1921. ((Entity*)target)->RemoveStifleSpell(luaspell);
  1922. else if (type == CONTROL_EFFECT_TYPE_DAZE)
  1923. ((Entity*)target)->RemoveDazeSpell(luaspell);
  1924. else if (type == CONTROL_EFFECT_TYPE_STUN)
  1925. ((Entity*)target)->RemoveStunSpell(luaspell);
  1926. else if (type == CONTROL_EFFECT_TYPE_ROOT)
  1927. ((Entity*)target)->RemoveRootSpell(luaspell);
  1928. else if (type == CONTROL_EFFECT_TYPE_FEAR)
  1929. ((Entity*)target)->RemoveFearSpell(luaspell);
  1930. else if (type == CONTROL_EFFECT_TYPE_WALKUNDERWATER)
  1931. ((Entity*)target)->RemoveWaterwalkSpell(luaspell);
  1932. else if (type == CONTROL_EFFECT_TYPE_JUMPUNDERWATER)
  1933. ((Entity*)target)->RemoveWaterjumpSpell(luaspell);
  1934. else if (type == CONTROL_EFFECT_TYPE_SNARE)
  1935. ((Entity*)target)->RemoveSnareSpell(luaspell);
  1936. else if (type == CONTROL_EFFECT_TYPE_FLIGHT)
  1937. ((Entity*)target)->RemoveFlightSpell(luaspell);
  1938. else if (type == CONTROL_EFFECT_TYPE_GLIDE)
  1939. ((Entity*)target)->RemoveGlideSpell(luaspell);
  1940. else if (type == CONTROL_EFFECT_TYPE_SAFEFALL)
  1941. ((Entity*)target)->RemoveGlideSpell(luaspell);
  1942. else
  1943. lua_interface->LogError("Unhandled control effect type of %u.", type);
  1944. }
  1945. }
  1946. luaspell->MSpellTargets.releasereadlock(__FUNCTION__, __LINE__);
  1947. }
  1948. else if (only_remove_spawn) {
  1949. if (type == CONTROL_EFFECT_TYPE_MEZ)
  1950. ((Entity*)spawn)->RemoveMezSpell(luaspell);
  1951. else if (type == CONTROL_EFFECT_TYPE_STIFLE)
  1952. ((Entity*)spawn)->RemoveStifleSpell(luaspell);
  1953. else if (type == CONTROL_EFFECT_TYPE_DAZE)
  1954. ((Entity*)spawn)->RemoveDazeSpell(luaspell);
  1955. else if (type == CONTROL_EFFECT_TYPE_STUN)
  1956. ((Entity*)spawn)->RemoveStunSpell(luaspell);
  1957. else if (type == CONTROL_EFFECT_TYPE_ROOT)
  1958. ((Entity*)spawn)->RemoveRootSpell(luaspell);
  1959. else if (type == CONTROL_EFFECT_TYPE_FEAR)
  1960. ((Entity*)spawn)->RemoveFearSpell(luaspell);
  1961. else if (type == CONTROL_EFFECT_TYPE_WALKUNDERWATER)
  1962. ((Entity*)spawn)->RemoveWaterwalkSpell(luaspell);
  1963. else if (type == CONTROL_EFFECT_TYPE_JUMPUNDERWATER)
  1964. ((Entity*)spawn)->RemoveWaterjumpSpell(luaspell);
  1965. else if (type == CONTROL_EFFECT_TYPE_SNARE)
  1966. ((Entity*)spawn)->RemoveSnareSpell(luaspell);
  1967. else if (type == CONTROL_EFFECT_TYPE_FLIGHT)
  1968. ((Entity*)spawn)->RemoveFlightSpell(luaspell);
  1969. else if (type == CONTROL_EFFECT_TYPE_GLIDE)
  1970. ((Entity*)spawn)->RemoveGlideSpell(luaspell);
  1971. else if (type == CONTROL_EFFECT_TYPE_SAFEFALL)
  1972. ((Entity*)spawn)->RemoveSafefallSpell(luaspell);
  1973. else
  1974. lua_interface->LogError("Unhandled control effect type of %u.", type);
  1975. }
  1976. }
  1977. return 0;
  1978. }
  1979. int EQ2Emu_lua_SetIntBase(lua_State* state){
  1980. if(!lua_interface)
  1981. return 0;
  1982. Spawn* spawn = lua_interface->GetSpawn(state);
  1983. int16 value = lua_interface->GetInt16Value(state, 2);
  1984. if(spawn && spawn->IsEntity()){
  1985. ((Entity*)spawn)->GetInfoStruct()->intel_base = value;
  1986. if(spawn->IsPlayer())
  1987. ((Player*)spawn)->SetCharSheetChanged(true);
  1988. }
  1989. return 0;
  1990. }
  1991. int EQ2Emu_lua_SetAgiBase(lua_State* state){
  1992. if(!lua_interface)
  1993. return 0;
  1994. Spawn* spawn = lua_interface->GetSpawn(state);
  1995. int16 value = lua_interface->GetInt16Value(state, 2);
  1996. if(spawn && spawn->IsEntity()){
  1997. ((Entity*)spawn)->GetInfoStruct()->agi_base = value;
  1998. if(spawn->IsPlayer())
  1999. ((Player*)spawn)->SetCharSheetChanged(true);
  2000. }
  2001. return 0;
  2002. }
  2003. int EQ2Emu_lua_SetWisBase(lua_State* state){
  2004. if(!lua_interface)
  2005. return 0;
  2006. Spawn* spawn = lua_interface->GetSpawn(state);
  2007. int16 value = lua_interface->GetInt16Value(state, 2);
  2008. if(spawn && spawn->IsEntity()){
  2009. ((Entity*)spawn)->GetInfoStruct()->wis_base = value;
  2010. if(spawn->IsPlayer())
  2011. ((Player*)spawn)->SetCharSheetChanged(true);
  2012. }
  2013. return 0;
  2014. }
  2015. int EQ2Emu_lua_SetStaBase(lua_State* state){
  2016. if(!lua_interface)
  2017. return 0;
  2018. Spawn* spawn = lua_interface->GetSpawn(state);
  2019. int16 value = lua_interface->GetInt16Value(state, 2);
  2020. if(spawn && spawn->IsEntity()){
  2021. ((Entity*)spawn)->GetInfoStruct()->sta_base = value;
  2022. if(spawn->IsPlayer())
  2023. ((Player*)spawn)->SetCharSheetChanged(true);
  2024. }
  2025. return 0;
  2026. }
  2027. int EQ2Emu_lua_SetStrBase(lua_State* state){
  2028. if(!lua_interface)
  2029. return 0;
  2030. Spawn* spawn = lua_interface->GetSpawn(state);
  2031. int16 value = lua_interface->GetInt16Value(state, 2);
  2032. if(spawn && spawn->IsEntity()){
  2033. ((Entity*)spawn)->GetInfoStruct()->str_base = value;
  2034. if(spawn->IsPlayer())
  2035. ((Player*)spawn)->SetCharSheetChanged(true);
  2036. }
  2037. return 0;
  2038. }
  2039. int EQ2Emu_lua_SetInt(lua_State* state){
  2040. if(!lua_interface)
  2041. return 0;
  2042. Spawn* spawn = lua_interface->GetSpawn(state);
  2043. sint32 value = lua_interface->GetSInt32Value(state, 2);
  2044. if(spawn && spawn->IsEntity()){
  2045. ((Entity*)spawn)->AddSpellBonus(0, ITEM_STAT_INT, value);
  2046. if(spawn->IsPlayer())
  2047. ((Player*)spawn)->SetCharSheetChanged(true);
  2048. }
  2049. return 0;
  2050. }
  2051. int EQ2Emu_lua_SetWis(lua_State* state){
  2052. if(!lua_interface)
  2053. return 0;
  2054. Spawn* spawn = lua_interface->GetSpawn(state);
  2055. float value = lua_interface->GetFloatValue(state, 2);
  2056. if(spawn && spawn->IsEntity()){
  2057. ((Entity*)spawn)->AddSpellBonus(0, ITEM_STAT_WIS, value);
  2058. if(spawn->IsPlayer())
  2059. ((Player*)spawn)->SetCharSheetChanged(true);
  2060. }
  2061. return 0;
  2062. }
  2063. int EQ2Emu_lua_SetSta(lua_State* state){
  2064. if(!lua_interface)
  2065. return 0;
  2066. Spawn* spawn = lua_interface->GetSpawn(state);
  2067. float value = lua_interface->GetFloatValue(state, 2);
  2068. if(spawn && spawn->IsEntity()){
  2069. ((Entity*)spawn)->AddSpellBonus(0, ITEM_STAT_STA, value);
  2070. if(spawn->IsPlayer())
  2071. ((Player*)spawn)->SetCharSheetChanged(true);
  2072. }
  2073. return 0;
  2074. }
  2075. int EQ2Emu_lua_SetStr(lua_State* state){
  2076. if(!lua_interface)
  2077. return 0;
  2078. Spawn* spawn = lua_interface->GetSpawn(state);
  2079. float value = lua_interface->GetFloatValue(state, 2);
  2080. if(spawn && spawn->IsEntity()){
  2081. ((Entity*)spawn)->AddSpellBonus(0, ITEM_STAT_STR, value);
  2082. if(spawn->IsPlayer())
  2083. ((Player*)spawn)->SetCharSheetChanged(true);
  2084. }
  2085. return 0;
  2086. }
  2087. int EQ2Emu_lua_SetAgi(lua_State* state){
  2088. if(!lua_interface)
  2089. return 0;
  2090. Spawn* spawn = lua_interface->GetSpawn(state);
  2091. float value = lua_interface->GetFloatValue(state, 2);
  2092. if(spawn && spawn->IsEntity()){
  2093. ((Entity*)spawn)->AddSpellBonus(0, ITEM_STAT_AGI, value);
  2094. if(spawn->IsPlayer())
  2095. ((Player*)spawn)->SetCharSheetChanged(true);
  2096. }
  2097. return 0;
  2098. }
  2099. int EQ2Emu_lua_GetCurrentHP(lua_State* state){
  2100. if(!lua_interface)
  2101. return 0;
  2102. Spawn* spawn = lua_interface->GetSpawn(state);
  2103. if(spawn){
  2104. lua_interface->SetInt32Value(state, spawn->GetHP());
  2105. return 1;
  2106. }
  2107. return 0;
  2108. }
  2109. int EQ2Emu_lua_GetMaxHP(lua_State* state){
  2110. if(!lua_interface)
  2111. return 0;
  2112. Spawn* spawn = lua_interface->GetSpawn(state);
  2113. if(spawn){
  2114. lua_interface->SetInt32Value(state, spawn->GetTotalHP());
  2115. return 1;
  2116. }
  2117. return 0;
  2118. }
  2119. int EQ2Emu_lua_GetMaxHPBase(lua_State* state){
  2120. if(!lua_interface)
  2121. return 0;
  2122. Spawn* spawn = lua_interface->GetSpawn(state);
  2123. if(spawn){
  2124. lua_interface->SetInt32Value(state, spawn->GetTotalHPBase());
  2125. return 1;
  2126. }
  2127. return 0;
  2128. }
  2129. int EQ2Emu_lua_GetName(lua_State* state){
  2130. if(!lua_interface)
  2131. return 0;
  2132. Spawn* spawn = lua_interface->GetSpawn(state);
  2133. if(spawn){
  2134. lua_interface->SetStringValue(state, spawn->GetName());
  2135. return 1;
  2136. }
  2137. return 0;
  2138. }
  2139. int EQ2Emu_lua_GetLevel(lua_State* state) {
  2140. Spawn* spawn = lua_interface->GetSpawn(state);
  2141. if (spawn) {
  2142. lua_interface->SetInt32Value(state, spawn->GetLevel());
  2143. return 1;
  2144. }
  2145. return 0;
  2146. }
  2147. int EQ2Emu_lua_GetCurrentPower(lua_State* state){
  2148. if(!lua_interface)
  2149. return 0;
  2150. Spawn* spawn = lua_interface->GetSpawn(state);
  2151. if(spawn){
  2152. lua_interface->SetInt32Value(state, spawn->GetPower());
  2153. return 1;
  2154. }
  2155. return 0;
  2156. }
  2157. int EQ2Emu_lua_GetMaxPower(lua_State* state){
  2158. if(!lua_interface)
  2159. return 0;
  2160. Spawn* spawn = lua_interface->GetSpawn(state);
  2161. if(spawn){
  2162. lua_interface->SetInt32Value(state, spawn->GetTotalPower());
  2163. return 1;
  2164. }
  2165. return 0;
  2166. }
  2167. int EQ2Emu_lua_GetMaxPowerBase(lua_State* state){
  2168. if(!lua_interface)
  2169. return 0;
  2170. Spawn* spawn = lua_interface->GetSpawn(state);
  2171. if(spawn){
  2172. lua_interface->SetInt32Value(state, spawn->GetTotalPowerBase());
  2173. return 1;
  2174. }
  2175. return 0;
  2176. }
  2177. int EQ2Emu_lua_GetDistance(lua_State* state){
  2178. if(!lua_interface)
  2179. return 0;
  2180. Spawn* spawn = lua_interface->GetSpawn(state);
  2181. Spawn* spawn2 = lua_interface->GetSpawn(state, 2);
  2182. bool include_radius = lua_interface->GetInt8Value(state, 3) == 1;
  2183. if(spawn && spawn2){
  2184. float distance = spawn->GetDistance(spawn2);
  2185. if (include_radius) {
  2186. distance -= spawn->appearance.pos.collision_radius / 10;
  2187. distance -= spawn2->appearance.pos.collision_radius / 10;
  2188. }
  2189. lua_interface->SetFloatValue(state, distance);
  2190. return 1;
  2191. }
  2192. return 0;
  2193. }
  2194. int EQ2Emu_lua_GetX(lua_State* state){
  2195. if(!lua_interface)
  2196. return 0;
  2197. Spawn* spawn = lua_interface->GetSpawn(state);
  2198. if(spawn){
  2199. lua_interface->SetFloatValue(state, spawn->GetX());
  2200. return 1;
  2201. }
  2202. return 0;
  2203. }
  2204. int EQ2Emu_lua_GetY(lua_State* state){
  2205. if(!lua_interface)
  2206. return 0;
  2207. Spawn* spawn = lua_interface->GetSpawn(state);
  2208. if(spawn){
  2209. lua_interface->SetFloatValue(state, spawn->GetY());
  2210. return 1;
  2211. }
  2212. return 0;
  2213. }
  2214. int EQ2Emu_lua_GetZ(lua_State* state){
  2215. if(!lua_interface)
  2216. return 0;
  2217. Spawn* spawn = lua_interface->GetSpawn(state);
  2218. if(spawn){
  2219. lua_interface->SetFloatValue(state, spawn->GetZ());
  2220. return 1;
  2221. }
  2222. return 0;
  2223. }
  2224. int EQ2Emu_lua_GetHeading(lua_State* state){
  2225. if(!lua_interface)
  2226. return 0;
  2227. Spawn* spawn = lua_interface->GetSpawn(state);
  2228. if(spawn){
  2229. lua_interface->SetFloatValue(state, spawn->GetHeading());
  2230. return 1;
  2231. }
  2232. return 0;
  2233. }
  2234. int EQ2Emu_lua_GetModelType(lua_State* state){
  2235. if(!lua_interface)
  2236. return 0;
  2237. Spawn* spawn = lua_interface->GetSpawn(state);
  2238. if(spawn){
  2239. lua_interface->SetInt32Value(state, spawn->GetModelType());
  2240. return 1;
  2241. }
  2242. return 0;
  2243. }
  2244. int EQ2Emu_lua_GetSpeed(lua_State* state){
  2245. if(!lua_interface)
  2246. return 0;
  2247. Spawn* spawn = lua_interface->GetSpawn(state);
  2248. if(spawn){
  2249. lua_interface->SetFloatValue(state, spawn->GetSpeed());
  2250. return 1;
  2251. }
  2252. return 0;
  2253. }
  2254. int EQ2Emu_lua_HasMoved(lua_State* state){
  2255. if(!lua_interface)
  2256. return 0;
  2257. Spawn* spawn = lua_interface->GetSpawn(state);
  2258. if(spawn && spawn->IsEntity()){
  2259. lua_interface->SetBooleanValue(state, ((Entity*)spawn)->HasMoved(false));
  2260. return 1;
  2261. }
  2262. return 0;
  2263. }
  2264. int EQ2Emu_lua_GetInt(lua_State* state){
  2265. if(!lua_interface)
  2266. return 0;
  2267. Spawn* spawn = lua_interface->GetSpawn(state);
  2268. if(spawn && spawn->IsEntity()){
  2269. lua_interface->SetInt32Value(state, ((Entity*)spawn)->GetInt());
  2270. return 1;
  2271. }
  2272. return 0;
  2273. }
  2274. int EQ2Emu_lua_GetWis(lua_State* state){
  2275. if(!lua_interface)
  2276. return 0;
  2277. Spawn* spawn = lua_interface->GetSpawn(state);
  2278. if(spawn && spawn->IsEntity()){
  2279. lua_interface->SetInt32Value(state, ((Entity*)spawn)->GetWis());
  2280. return 1;
  2281. }
  2282. return 0;
  2283. }
  2284. int EQ2Emu_lua_GetSta(lua_State* state){
  2285. if(!lua_interface)
  2286. return 0;
  2287. Spawn* spawn = lua_interface->GetSpawn(state);
  2288. if(spawn && spawn->IsEntity()){
  2289. lua_interface->SetInt32Value(state, ((Entity*)spawn)->GetSta());
  2290. return 1;
  2291. }
  2292. return 0;
  2293. }
  2294. int EQ2Emu_lua_GetStr(lua_State* state){
  2295. if(!lua_interface)
  2296. return 0;
  2297. Spawn* spawn = lua_interface->GetSpawn(state);
  2298. if(spawn && spawn->IsEntity()){
  2299. lua_interface->SetInt32Value(state, ((Entity*)spawn)->GetStr());
  2300. return 1;
  2301. }
  2302. return 0;
  2303. }
  2304. int EQ2Emu_lua_GetAgi(lua_State* state){
  2305. if(!lua_interface)
  2306. return 0;
  2307. Spawn* spawn = lua_interface->GetSpawn(state);
  2308. if(spawn && spawn->IsEntity()){
  2309. lua_interface->SetInt32Value(state, ((Entity*)spawn)->GetAgi());
  2310. return 1;
  2311. }
  2312. return 0;
  2313. }
  2314. int EQ2Emu_lua_GetIntBase(lua_State* state){
  2315. if(!lua_interface)
  2316. return 0;
  2317. Spawn* spawn = lua_interface->GetSpawn(state);
  2318. if(spawn && spawn->IsEntity()){
  2319. lua_interface->SetInt32Value(state, ((Entity*)spawn)->GetIntBase());
  2320. return 1;
  2321. }
  2322. return 0;
  2323. }
  2324. int EQ2Emu_lua_GetWisBase(lua_State* state){
  2325. if(!lua_interface)
  2326. return 0;
  2327. Spawn* spawn = lua_interface->GetSpawn(state);
  2328. if(spawn && spawn->IsEntity()){
  2329. lua_interface->SetInt32Value(state, ((Entity*)spawn)->GetWisBase());
  2330. return 1;
  2331. }
  2332. return 0;
  2333. }
  2334. int EQ2Emu_lua_GetStaBase(lua_State* state){
  2335. if(!lua_interface)
  2336. return 0;
  2337. Spawn* spawn = lua_interface->GetSpawn(state);
  2338. if(spawn && spawn->IsEntity()){
  2339. lua_interface->SetInt32Value(state, ((Entity*)spawn)->GetStaBase());
  2340. return 1;
  2341. }
  2342. return 0;
  2343. }
  2344. int EQ2Emu_lua_GetStrBase(lua_State* state){
  2345. if(!lua_interface)
  2346. return 0;
  2347. Spawn* spawn = lua_interface->GetSpawn(state);
  2348. if(spawn && spawn->IsEntity()){
  2349. lua_interface->SetInt32Value(state, ((Entity*)spawn)->GetStrBase());
  2350. return 1;
  2351. }
  2352. return 0;
  2353. }
  2354. int EQ2Emu_lua_GetAgiBase(lua_State* state){
  2355. if(!lua_interface)
  2356. return 0;
  2357. Spawn* spawn = lua_interface->GetSpawn(state);
  2358. if(spawn && spawn->IsEntity()){
  2359. lua_interface->SetInt32Value(state, ((Entity*)spawn)->GetAgiBase());
  2360. return 1;
  2361. }
  2362. return 0;
  2363. }
  2364. int EQ2Emu_lua_SetStepComplete(lua_State* state){
  2365. if(!lua_interface)
  2366. return 0;
  2367. Spawn* player = lua_interface->GetSpawn(state);
  2368. int32 quest_id = lua_interface->GetInt32Value(state, 2);
  2369. int32 step = lua_interface->GetInt32Value(state, 3);
  2370. if(player && player->IsPlayer() && quest_id > 0 && step > 0 && (((Player*)player)->player_quests.count(quest_id) > 0)){
  2371. Client* client = player->GetZone()->GetClientBySpawn(player);
  2372. if(client)
  2373. client->AddPendingQuestUpdate(quest_id, step);
  2374. }
  2375. return 0;
  2376. }
  2377. int EQ2Emu_lua_AddStepProgress(lua_State* state) {
  2378. Spawn* player = lua_interface->GetSpawn(state);
  2379. int32 quest_id = lua_interface->GetInt32Value(state, 2);
  2380. int32 step = lua_interface->GetInt32Value(state, 3);
  2381. int32 progress = lua_interface->GetInt32Value(state, 4);
  2382. if (player && player->IsPlayer() && quest_id > 0 && step > 0 && progress > 0 && (((Player*)player)->player_quests.count(quest_id) > 0)) {
  2383. Client* client = player->GetZone()->GetClientBySpawn(player);
  2384. if (client)
  2385. client->AddPendingQuestUpdate(quest_id, step, progress);
  2386. }
  2387. return 0;
  2388. }
  2389. int EQ2Emu_lua_GetTaskGroupStep(lua_State* state){
  2390. if(!lua_interface)
  2391. return 0;
  2392. Spawn* player = lua_interface->GetSpawn(state);
  2393. int32 quest_id = lua_interface->GetInt32Value(state, 2);
  2394. if(player && player->IsPlayer() && quest_id > 0 ){
  2395. lua_interface->SetInt32Value(state, ((Player*)player)->GetTaskGroupStep(quest_id));
  2396. return 1;
  2397. }
  2398. return 0;
  2399. }
  2400. int EQ2Emu_lua_QuestStepIsComplete(lua_State* state){
  2401. if(!lua_interface)
  2402. return 0;
  2403. Spawn* player = lua_interface->GetSpawn(state);
  2404. int32 quest_id = lua_interface->GetInt32Value(state, 2);
  2405. int32 step_id = lua_interface->GetInt32Value(state, 3);
  2406. if(player && player->IsPlayer() && quest_id > 0 ){
  2407. lua_interface->SetBooleanValue(state, ((Player*)player)->GetQuestStepComplete(quest_id, step_id));
  2408. return 1;
  2409. }
  2410. return 0;
  2411. }
  2412. int EQ2Emu_lua_GetQuestStep(lua_State* state){
  2413. if(!lua_interface)
  2414. return 0;
  2415. Spawn* player = lua_interface->GetSpawn(state);
  2416. int32 quest_id = lua_interface->GetInt32Value(state, 2);
  2417. if(player && player->IsPlayer() && quest_id > 0 ){
  2418. lua_interface->SetInt32Value(state, ((Player*)player)->GetQuestStep(quest_id));
  2419. return 1;
  2420. }
  2421. return 0;
  2422. }
  2423. int EQ2Emu_lua_RegisterQuest(lua_State* state){
  2424. if(!lua_interface)
  2425. return 0;
  2426. Quest* quest = lua_interface->GetQuest(state);
  2427. string name = lua_interface->GetStringValue(state, 2);
  2428. string type = lua_interface->GetStringValue(state, 3);
  2429. string zone = lua_interface->GetStringValue(state, 4);
  2430. int16 level = lua_interface->GetInt16Value(state, 5);
  2431. string description = lua_interface->GetStringValue(state, 6);
  2432. bool load = true;
  2433. if(!quest){
  2434. lua_interface->SimpleLogError("Quest not given in RegisterQuest!");
  2435. load = false;
  2436. }
  2437. if(load && name.length() == 0){
  2438. lua_interface->SimpleLogError("Name not given in RegisterQuest!");
  2439. load = false;
  2440. }
  2441. if(load && type.length() == 0){
  2442. lua_interface->LogError("Type not given in RegisterQuest for '%s'!", name.c_str());
  2443. load = false;
  2444. }
  2445. if(load && zone.length() == 0){
  2446. lua_interface->LogError("Zone not given in RegisterQuest for '%s'!", name.c_str());
  2447. load = false;
  2448. }
  2449. if(load && description.length() == 0){
  2450. lua_interface->LogError("Description not given in RegisterQuest for '%s'!", name.c_str());
  2451. load = false;
  2452. }
  2453. if(load && level == 0){
  2454. lua_interface->LogError("Level not given in RegisterQuest for '%s'!", name.c_str());
  2455. load = false;
  2456. }
  2457. if(load)
  2458. quest->RegisterQuest(name, type, zone, level, description);
  2459. return 0;
  2460. }
  2461. int EQ2Emu_lua_SetQuestPrereqLevel(lua_State* state){
  2462. if(!lua_interface)
  2463. return 0;
  2464. Quest* quest = lua_interface->GetQuest(state);
  2465. if(quest){
  2466. int8 level = lua_interface->GetInt16Value(state, 2);
  2467. quest->SetPrereqLevel(level);
  2468. }
  2469. return 0;
  2470. }
  2471. int EQ2Emu_lua_AddQuestPrereqQuest(lua_State* state){
  2472. if(!lua_interface)
  2473. return 0;
  2474. Quest* quest = lua_interface->GetQuest(state);
  2475. if(quest){
  2476. int32 quest_id = lua_interface->GetInt32Value(state, 2);
  2477. quest->AddPrereqQuest(quest_id);
  2478. }
  2479. return 0;
  2480. }
  2481. int EQ2Emu_lua_AddQuestPrereqItem(lua_State* state){
  2482. if(!lua_interface)
  2483. return 0;
  2484. Quest* quest = lua_interface->GetQuest(state);
  2485. if(quest){
  2486. int32 item_id = lua_interface->GetInt32Value(state, 2);
  2487. int8 quantity = lua_interface->GetInt32Value(state, 3);
  2488. if(quantity == 0)
  2489. quantity = 1;
  2490. Item* master_item = master_item_list.GetItem(item_id);
  2491. if(master_item){
  2492. Item* item = new Item(master_item);
  2493. item->details.count = quantity;
  2494. quest->AddPrereqItem(item);
  2495. }
  2496. }
  2497. return 0;
  2498. }
  2499. int EQ2Emu_lua_HasQuest(lua_State* state){
  2500. if(!lua_interface)
  2501. return 0;
  2502. Spawn* player = lua_interface->GetSpawn(state);
  2503. int32 quest_id = lua_interface->GetInt32Value(state, 2);
  2504. if(player && player->IsPlayer() && quest_id > 0){
  2505. lua_interface->SetBooleanValue(state, (((Player*)player)->player_quests.count(quest_id) > 0));
  2506. return 1;
  2507. }
  2508. return 0;
  2509. }
  2510. int EQ2Emu_lua_QuestReturnNPC(lua_State* state){
  2511. if(!lua_interface)
  2512. return 0;
  2513. Quest* quest = lua_interface->GetQuest(state);
  2514. int32 spawn_id = lua_interface->GetInt32Value(state, 2);
  2515. if(quest && spawn_id > 0)
  2516. quest->SetQuestReturnNPC(spawn_id);
  2517. return 0;
  2518. }
  2519. int EQ2Emu_lua_AddTimer(lua_State* state){
  2520. if(!lua_interface)
  2521. return 0;
  2522. Spawn* spawn = lua_interface->GetSpawn(state);
  2523. if (!spawn) {
  2524. lua_interface->LogError("LUA AddTimer command error: spawn is not valid");
  2525. return 0;
  2526. }
  2527. int32 time = lua_interface->GetInt32Value(state, 2);
  2528. if (time <= 0) {
  2529. lua_interface->LogError("LUA AddTimer command error: time is not set");
  2530. return 0;
  2531. }
  2532. string function = lua_interface->GetStringValue(state, 3);
  2533. if (function.length() == 0) {
  2534. lua_interface->LogError("LUA AddTimer command error: function is not set");
  2535. return 0;
  2536. }
  2537. int32 max_count = lua_interface->GetInt32Value(state, 4);
  2538. Spawn* player = lua_interface->GetSpawn(state, 5);
  2539. SpawnScriptTimer* timer = new SpawnScriptTimer;
  2540. timer->timer = Timer::GetCurrentTime2() + time;
  2541. timer->function = function;
  2542. timer->spawn = spawn->GetID();
  2543. timer->player = player ? player->GetID() : 0;
  2544. if(max_count == 0)
  2545. max_count = 1;
  2546. timer->max_count = max_count;
  2547. timer->current_count = 0;
  2548. spawn->GetZone()->AddSpawnScriptTimer(timer);
  2549. return 0;
  2550. }
  2551. int EQ2Emu_lua_GetQuest(lua_State* state){
  2552. if(!lua_interface)
  2553. return 0;
  2554. Spawn* player = lua_interface->GetSpawn(state);
  2555. int32 quest_id = lua_interface->GetInt32Value(state, 2);
  2556. if(player && player->IsPlayer() && quest_id > 0){
  2557. lua_interface->SetQuestValue(state, ((Player*)player)->player_quests[quest_id]);
  2558. return 1;
  2559. }
  2560. return 0;
  2561. }
  2562. int EQ2Emu_lua_QuestIsComplete(lua_State* state){
  2563. if(!lua_interface)
  2564. return 0;
  2565. Spawn* player = lua_interface->GetSpawn(state);
  2566. int32 quest_id = lua_interface->GetInt32Value(state, 2);
  2567. if(player && player->IsPlayer() && quest_id > 0 && (((Player*)player)->player_quests.count(quest_id) > 0)){
  2568. Quest* quest = ((Player*)player)->player_quests[quest_id];
  2569. if(quest)
  2570. lua_interface->SetBooleanValue(state, quest->GetCompleted());
  2571. return 1;
  2572. }
  2573. return 0;
  2574. }
  2575. int EQ2Emu_lua_HasCompletedQuest(lua_State* state){
  2576. if(!lua_interface)
  2577. return 0;
  2578. Spawn* player = lua_interface->GetSpawn(state);
  2579. int32 quest_id = lua_interface->GetInt32Value(state, 2);
  2580. if(player && player->IsPlayer() && quest_id > 0){
  2581. lua_interface->SetBooleanValue(state, (((Player*)player)->GetCompletedQuest(quest_id) != 0));
  2582. return 1;
  2583. }
  2584. return 0;
  2585. }
  2586. int EQ2Emu_lua_ProvidesQuest(lua_State* state){
  2587. if(!lua_interface)
  2588. return 0;
  2589. Spawn* npc = lua_interface->GetSpawn(state);
  2590. int32 quest_id = lua_interface->GetInt32Value(state, 2);
  2591. if(npc && !npc->IsPlayer() && quest_id > 0)
  2592. npc->AddProvidedQuest(quest_id);
  2593. return 0;
  2594. }
  2595. int EQ2Emu_lua_OfferQuest(lua_State* state){
  2596. if(!lua_interface)
  2597. return 0;
  2598. Spawn* npc = lua_interface->GetSpawn(state);
  2599. Spawn* player = lua_interface->GetSpawn(state, 2);
  2600. int32 quest_id = lua_interface->GetInt32Value(state, 3);
  2601. /* NPC is allowed to be null */
  2602. if (player && player->IsPlayer() && quest_id > 0){
  2603. Quest* master_quest = master_quest_list.GetQuest(quest_id);
  2604. if(master_quest){
  2605. Client* client = player->GetZone()->GetClientBySpawn(player);
  2606. Quest* quest = new Quest(master_quest);
  2607. if(client && quest){
  2608. client->AddPendingQuest(quest);
  2609. if (npc)
  2610. quest->SetQuestGiver(npc->GetDatabaseID());
  2611. else
  2612. quest->SetQuestGiver(0);
  2613. }
  2614. }
  2615. }
  2616. return 0;
  2617. }
  2618. int EQ2Emu_lua_AddQuestPrereqClass(lua_State* state){
  2619. if(!lua_interface)
  2620. return 0;
  2621. Quest* quest = lua_interface->GetQuest(state);
  2622. if(quest){
  2623. int8 class_id = lua_interface->GetInt8Value(state, 2);
  2624. quest->AddPrereqClass(class_id);
  2625. }
  2626. return 0;
  2627. }
  2628. int EQ2Emu_lua_AddQuestPrereqRace(lua_State* state){
  2629. if(!lua_interface)
  2630. return 0;
  2631. Quest* quest = lua_interface->GetQuest(state);
  2632. if(quest){
  2633. int8 race = lua_interface->GetInt8Value(state, 2);
  2634. quest->AddPrereqRace(race);
  2635. }
  2636. return 0;
  2637. }
  2638. int EQ2Emu_lua_AddQuestPrereqModelType(lua_State* state){
  2639. if(!lua_interface)
  2640. return 0;
  2641. Quest* quest = lua_interface->GetQuest(state);
  2642. if(quest){
  2643. int16 model_type = lua_interface->GetInt16Value(state, 2);
  2644. quest->AddPrereqModelType(model_type);
  2645. }
  2646. return 0;
  2647. }
  2648. int EQ2Emu_lua_AddQuestPrereqTradeskillLevel(lua_State* state){
  2649. if(!lua_interface)
  2650. return 0;
  2651. Quest* quest = lua_interface->GetQuest(state);
  2652. if (!quest) {
  2653. lua_interface->LogError("LUA AddQuestPrereqTradeskillLevel command error: quest is not valid");
  2654. return 0;
  2655. }
  2656. int8 level = lua_interface->GetInt8Value(state, 2);
  2657. quest->SetPrereqTSLevel(level);
  2658. return 0;
  2659. }
  2660. int EQ2Emu_lua_AddQuestPrereqTradeskillClass(lua_State* state){
  2661. if(!lua_interface)
  2662. return 0;
  2663. Quest* quest = lua_interface->GetQuest(state);
  2664. if (!quest) {
  2665. lua_interface->LogError("LUA AddQuestPrereqTradeskillClass command error: quest is not valid");
  2666. return 0;
  2667. }
  2668. int8 class_id = lua_interface->GetInt8Value(state, 2);
  2669. quest->AddPrereqTradeskillClass(class_id);
  2670. return 0;
  2671. }
  2672. int EQ2Emu_lua_AddQuestPrereqFaction(lua_State* state){
  2673. if(!lua_interface)
  2674. return 0;
  2675. Quest* quest = lua_interface->GetQuest(state);
  2676. if(quest){
  2677. int32 faction_id = lua_interface->GetInt32Value(state, 2);
  2678. sint32 min = lua_interface->GetSInt32Value(state, 3);
  2679. sint32 max = lua_interface->GetSInt32Value(state, 4);
  2680. quest->AddPrereqFaction(faction_id, min, max);
  2681. }
  2682. return 0;
  2683. }
  2684. int EQ2Emu_lua_AddQuestSelectableRewardItem(lua_State* state){
  2685. if(!lua_interface)
  2686. return 0;
  2687. Quest* quest = lua_interface->GetQuest(state);
  2688. if(quest){
  2689. int32 item_id = lua_interface->GetInt32Value(state, 2);
  2690. int8 quantity = lua_interface->GetInt8Value(state, 3);
  2691. if(quantity == 0)
  2692. quantity = 1;
  2693. Item* master_item = master_item_list.GetItem(item_id);
  2694. if(master_item){
  2695. Item* item = new Item(master_item);
  2696. item->details.count = quantity;
  2697. quest->AddSelectableRewardItem(item);
  2698. }
  2699. }
  2700. return 0;
  2701. }
  2702. int EQ2Emu_lua_AddQuestRewardItem(lua_State* state){
  2703. if(!lua_interface)
  2704. return 0;
  2705. Quest* quest = lua_interface->GetQuest(state);
  2706. if(quest){
  2707. int32 item_id = lua_interface->GetInt32Value(state, 2);
  2708. int8 quantity = lua_interface->GetInt32Value(state, 3);
  2709. if(quantity == 0)
  2710. quantity = 1;
  2711. Item* master_item = master_item_list.GetItem(item_id);
  2712. if(master_item){
  2713. Item* item = new Item(master_item);
  2714. item->details.count = quantity;
  2715. quest->AddRewardItem(item);
  2716. }
  2717. }
  2718. return 0;
  2719. }
  2720. int EQ2Emu_lua_AddQuestRewardCoin(lua_State* state){
  2721. if(!lua_interface)
  2722. return 0;
  2723. Quest* quest = lua_interface->GetQuest(state);
  2724. if(quest){
  2725. int32 copper = lua_interface->GetInt32Value(state, 2);
  2726. int32 silver = lua_interface->GetInt32Value(state, 3);
  2727. int32 gold = lua_interface->GetInt32Value(state, 4);
  2728. int32 plat = lua_interface->GetInt32Value(state, 5);
  2729. quest->AddRewardCoins(copper, silver, gold, plat);
  2730. }
  2731. return 0;
  2732. }
  2733. int EQ2Emu_lua_AddQuestRewardFaction(lua_State* state){
  2734. if(!lua_interface)
  2735. return 0;
  2736. Quest* quest = lua_interface->GetQuest(state);
  2737. int32 faction_id = lua_interface->GetInt32Value(state, 2);
  2738. sint32 amount = lua_interface->GetSInt32Value(state, 3);
  2739. if (quest && faction_id > 0 && amount != 0)
  2740. quest->AddRewardFaction(faction_id, amount);
  2741. return 0;
  2742. }
  2743. int EQ2Emu_lua_SetQuestRewardStatus(lua_State* state){
  2744. if(!lua_interface)
  2745. return 0;
  2746. Quest* quest = lua_interface->GetQuest(state);
  2747. if(quest){
  2748. int32 status = lua_interface->GetInt32Value(state, 2);
  2749. quest->SetRewardStatus(status);
  2750. }
  2751. return 0;
  2752. }
  2753. int EQ2Emu_lua_SetQuestRewardComment(lua_State* state){
  2754. if(!lua_interface)
  2755. return 0;
  2756. Quest* quest = lua_interface->GetQuest(state);
  2757. if(quest){
  2758. string comment = lua_interface->GetStringValue(state, 2);
  2759. quest->SetRewardComment(comment);
  2760. }
  2761. return 0;
  2762. }
  2763. int EQ2Emu_lua_SetQuestRewardExp(lua_State* state){
  2764. if(!lua_interface)
  2765. return 0;
  2766. Quest* quest = lua_interface->GetQuest(state);
  2767. if(quest){
  2768. int32 exp = lua_interface->GetInt32Value(state, 2);
  2769. quest->SetRewardXP(exp);
  2770. }
  2771. return 0;
  2772. }
  2773. int EQ2Emu_lua_AddQuestStep(lua_State* state) {
  2774. Quest* quest = lua_interface->GetQuest(state);
  2775. if (quest) {
  2776. int32 step = lua_interface->GetInt32Value(state, 2);
  2777. string description = lua_interface->GetStringValue(state, 3);
  2778. int32 quantity = lua_interface->GetInt32Value(state, 4);
  2779. float percentage = lua_interface->GetFloatValue(state, 5);
  2780. string str_taskgroup = lua_interface->GetStringValue(state, 6);
  2781. int16 icon = lua_interface->GetInt16Value(state, 7);
  2782. int32 usableitemid = lua_interface->GetInt32Value(state, 8);
  2783. const char* taskgroup = 0;
  2784. if(str_taskgroup.length() > 0)
  2785. taskgroup = str_taskgroup.c_str();
  2786. QuestStep* quest_step = quest->AddQuestStep(step, QUEST_STEP_TYPE_NORMAL, description, 0, quantity, taskgroup, 0, 0, percentage, usableitemid);
  2787. if (quest_step && icon && quantity > 0)
  2788. quest_step->SetIcon(icon);
  2789. }
  2790. return 0;
  2791. }
  2792. int EQ2Emu_lua_AddQuestStepKill(lua_State* state){
  2793. if(!lua_interface)
  2794. return 0;
  2795. Quest* quest = lua_interface->GetQuest(state);
  2796. if(quest){
  2797. int32 step = lua_interface->GetInt32Value(state, 2);
  2798. string description = lua_interface->GetStringValue(state, 3);
  2799. int32 quantity = lua_interface->GetInt32Value(state, 4);
  2800. float percentage = lua_interface->GetFloatValue(state, 5);
  2801. string str_taskgroup = lua_interface->GetStringValue(state, 6);
  2802. int16 icon = lua_interface->GetInt16Value(state, 7);
  2803. const char* taskgroup = 0;
  2804. if(str_taskgroup.length() > 0)
  2805. taskgroup = str_taskgroup.c_str();
  2806. int32 npc_id = 0;
  2807. vector<int32>* ids = 0;
  2808. Spawn* spawn = nullptr;
  2809. int i=0;
  2810. while((npc_id = lua_interface->GetInt32Value(state, 8+i))){
  2811. if(ids == 0)
  2812. ids = new vector<int32>;
  2813. ids->push_back(npc_id);
  2814. i++;
  2815. }
  2816. QuestStep* quest_step = quest->AddQuestStep(step, QUEST_STEP_TYPE_KILL, description, ids, quantity, taskgroup, 0, 0, percentage,0);
  2817. if(quest_step && icon > 0 && quantity > 0)
  2818. quest_step->SetIcon(icon);
  2819. }
  2820. return 0;
  2821. }
  2822. int EQ2Emu_lua_AddQuestStepChat(lua_State* state){
  2823. if(!lua_interface)
  2824. return 0;
  2825. Quest* quest = lua_interface->GetQuest(state);
  2826. if(quest){
  2827. int32 step = lua_interface->GetInt32Value(state, 2);
  2828. string description = lua_interface->GetStringValue(state, 3);
  2829. int32 quantity = lua_interface->GetInt32Value(state, 4);
  2830. string str_taskgroup = lua_interface->GetStringValue(state, 5);
  2831. int16 icon = lua_interface->GetInt16Value(state, 6);
  2832. const char* taskgroup = 0;
  2833. if(str_taskgroup.length() > 0)
  2834. taskgroup = str_taskgroup.c_str();
  2835. int32 npc_id = 0;
  2836. vector<int32>* ids = 0;
  2837. int i=0;
  2838. while((npc_id = lua_interface->GetInt32Value(state, 7+i))){
  2839. if(ids == 0)
  2840. ids = new vector<int32>;
  2841. ids->push_back(npc_id);
  2842. i++;
  2843. }
  2844. QuestStep* quest_step = quest->AddQuestStep(step, QUEST_STEP_TYPE_CHAT, description, ids, quantity, taskgroup);
  2845. if (quest_step && icon > 0)
  2846. quest_step->SetIcon(icon);
  2847. if(quest->GetPlayer()){
  2848. Client* client = quest->GetPlayer()->GetZone()->GetClientBySpawn(quest->GetPlayer());
  2849. quest->GetPlayer()->GetZone()->SendQuestUpdates(client);
  2850. }
  2851. }
  2852. return 0;
  2853. }
  2854. int EQ2Emu_lua_AddQuestStepObtainItem(lua_State* state){
  2855. if(!lua_interface)
  2856. return 0;
  2857. Quest* quest = lua_interface->GetQuest(state);
  2858. if(quest){
  2859. int32 step = lua_interface->GetInt32Value(state, 2);
  2860. string description = lua_interface->GetStringValue(state, 3);
  2861. int32 quantity = lua_interface->GetInt32Value(state, 4);
  2862. float percentage = lua_interface->GetFloatValue(state, 5);
  2863. string str_taskgroup = lua_interface->GetStringValue(state, 6);
  2864. int16 icon = lua_interface->GetInt16Value(state, 7);
  2865. const char* taskgroup = 0;
  2866. if(str_taskgroup.length() > 0)
  2867. taskgroup = str_taskgroup.c_str();
  2868. int32 item_id = 0;
  2869. vector<int32>* ids = 0;
  2870. int i=0;
  2871. while((item_id = lua_interface->GetInt32Value(state, 8+i))){
  2872. if(ids == 0)
  2873. ids = new vector<int32>;
  2874. ids->push_back(item_id);
  2875. i++;
  2876. }
  2877. QuestStep* quest_step = quest->AddQuestStep(step, QUEST_STEP_TYPE_OBTAIN_ITEM, description, ids, quantity, taskgroup, 0, 0, percentage,0);
  2878. if (quest_step && icon > 0 && quantity > 0)
  2879. quest_step->SetIcon(icon);
  2880. }
  2881. return 0;
  2882. }
  2883. int EQ2Emu_lua_AddQuestStepLocation(lua_State* state){
  2884. if(!lua_interface)
  2885. return 0;
  2886. Quest* quest = lua_interface->GetQuest(state);
  2887. if(quest){
  2888. int32 step = lua_interface->GetInt32Value(state, 2);
  2889. string description = lua_interface->GetStringValue(state, 3);
  2890. float max_variation = lua_interface->GetFloatValue(state, 4);
  2891. string str_taskgroup = lua_interface->GetStringValue(state, 5);
  2892. int16 icon = lua_interface->GetInt16Value(state, 6);
  2893. const char* taskgroup = 0;
  2894. if(str_taskgroup.length() > 0)
  2895. taskgroup = str_taskgroup.c_str();
  2896. vector<Location>* locations = 0;
  2897. int i = 7;
  2898. while(true) {
  2899. Location loc;
  2900. loc.x = lua_interface->GetFloatValue(state, i);
  2901. loc.y = lua_interface->GetFloatValue(state, i + 1);
  2902. loc.z = lua_interface->GetFloatValue(state, i + 2);
  2903. if(loc.x == 0 && loc.y == 0 && loc.z == 0)
  2904. break;
  2905. if(locations == 0)
  2906. locations = new vector<Location>;
  2907. locations->push_back(loc);
  2908. i += 3;
  2909. }
  2910. QuestStep* quest_step = quest->AddQuestStep(step, QUEST_STEP_TYPE_LOCATION, description, 0, 1, taskgroup, locations, max_variation);
  2911. if (quest_step && icon > 0)
  2912. quest_step->SetIcon(icon);
  2913. }
  2914. return 0;
  2915. }
  2916. int EQ2Emu_lua_AddQuestUsableItem(lua_State* state){
  2917. if (!lua_interface)
  2918. return 0;
  2919. Quest* quest = lua_interface->GetQuest(state);
  2920. if (quest){
  2921. int32 step = lua_interface->GetInt32Value(state, 2);
  2922. string description = lua_interface->GetStringValue(state, 3);
  2923. float max_variation = lua_interface->GetFloatValue(state, 4);
  2924. string str_taskgroup = lua_interface->GetStringValue(state, 5);
  2925. int16 icon = lua_interface->GetInt16Value(state, 6);
  2926. const char* taskgroup = 0;
  2927. if (str_taskgroup.length() > 0)
  2928. taskgroup = str_taskgroup.c_str();
  2929. vector<Location>* locations = 0;
  2930. int i = 7;
  2931. while (true) {
  2932. Location loc;
  2933. loc.x = lua_interface->GetFloatValue(state, i);
  2934. loc.y = lua_interface->GetFloatValue(state, i + 1);
  2935. loc.z = lua_interface->GetFloatValue(state, i + 2);
  2936. if (loc.x == 0 && loc.y == 0 && loc.z == 0)
  2937. break;
  2938. if (locations == 0)
  2939. locations = new vector<Location>;
  2940. locations->push_back(loc);
  2941. i += 3;
  2942. }
  2943. QuestStep* quest_step = quest->AddQuestStep(step, QUEST_STEP_TYPE_LOCATION, description, 0, 1, taskgroup, locations, max_variation);
  2944. if (quest_step && icon > 0)
  2945. quest_step->SetIcon(icon);
  2946. }
  2947. return 0;
  2948. }
  2949. int EQ2Emu_lua_AddQuestStepSpell(lua_State* state) {
  2950. Quest* quest = lua_interface->GetQuest(state);
  2951. if(quest) {
  2952. int32 step = lua_interface->GetInt32Value(state, 2);
  2953. string description = lua_interface->GetStringValue(state, 3);
  2954. int32 quantity = lua_interface->GetInt32Value(state, 4);
  2955. float percentage = lua_interface->GetFloatValue(state, 5);
  2956. string str_taskgroup = lua_interface->GetStringValue(state, 6);
  2957. int16 icon = lua_interface->GetInt16Value(state, 7);
  2958. const char* taskgroup = 0;
  2959. if (str_taskgroup.length() > 0)
  2960. taskgroup = str_taskgroup.c_str();
  2961. int32 spell_id = 0;
  2962. vector<int32>* ids = 0;
  2963. int i = 0;
  2964. while ((spell_id = lua_interface->GetInt32Value(state, 8 + i))) {
  2965. if (ids == 0)
  2966. ids = new vector<int32>;
  2967. ids->push_back(spell_id);
  2968. i++;
  2969. }
  2970. QuestStep* quest_step = quest->AddQuestStep(step, QUEST_STEP_TYPE_SPELL, description, ids, quantity, taskgroup, 0, 0, percentage,0);
  2971. if(quest_step && icon > 0 && quantity > 0)
  2972. quest_step->SetIcon(icon);
  2973. }
  2974. return 0;
  2975. }
  2976. int EQ2Emu_lua_AddQuestStepCraft(lua_State* state){
  2977. if(!lua_interface)
  2978. return 0;
  2979. Quest* quest = lua_interface->GetQuest(state);
  2980. if(quest){
  2981. int32 step = lua_interface->GetInt32Value(state, 2);
  2982. string description = lua_interface->GetStringValue(state, 3);
  2983. int32 quantity = lua_interface->GetInt32Value(state, 4);
  2984. float percentage = lua_interface->GetFloatValue(state, 5);
  2985. string str_taskgroup = lua_interface->GetStringValue(state, 6);
  2986. int16 icon = lua_interface->GetInt16Value(state, 7);
  2987. const char* taskgroup = 0;
  2988. if(str_taskgroup.length() > 0)
  2989. taskgroup = str_taskgroup.c_str();
  2990. int32 item_id = 0;
  2991. vector<int32>* ids = 0;
  2992. int i=0;
  2993. while((item_id = lua_interface->GetInt32Value(state, 8+i))){
  2994. if(ids == 0)
  2995. ids = new vector<int32>;
  2996. ids->push_back(item_id);
  2997. i++;
  2998. }
  2999. QuestStep* quest_step = quest->AddQuestStep(step, QUEST_STEP_TYPE_CRAFT, description, ids, quantity, taskgroup, 0, 0, percentage,0);
  3000. if (quest_step && icon > 0 && quantity > 0)
  3001. quest_step->SetIcon(icon);
  3002. }
  3003. return 0;
  3004. }
  3005. int EQ2Emu_lua_AddQuestStepHarvest(lua_State* state){
  3006. if(!lua_interface)
  3007. return 0;
  3008. Quest* quest = lua_interface->GetQuest(state);
  3009. if(quest){
  3010. int32 step = lua_interface->GetInt32Value(state, 2);
  3011. string description = lua_interface->GetStringValue(state, 3);
  3012. int32 quantity = lua_interface->GetInt32Value(state, 4);
  3013. float percentage = lua_interface->GetFloatValue(state, 5);
  3014. string str_taskgroup = lua_interface->GetStringValue(state, 6);
  3015. int16 icon = lua_interface->GetInt16Value(state, 7);
  3016. const char* taskgroup = 0;
  3017. if(str_taskgroup.length() > 0)
  3018. taskgroup = str_taskgroup.c_str();
  3019. int32 item_id = 0;
  3020. vector<int32>* ids = 0;
  3021. int i=0;
  3022. while((item_id = lua_interface->GetInt32Value(state, 8+i))){
  3023. if(ids == 0)
  3024. ids = new vector<int32>;
  3025. ids->push_back(item_id);
  3026. i++;
  3027. }
  3028. QuestStep* quest_step = quest->AddQuestStep(step, QUEST_STEP_TYPE_HARVEST, description, ids, quantity, taskgroup, 0, 0, percentage,0);
  3029. if (quest_step && icon > 0 && quantity > 0)
  3030. quest_step->SetIcon(icon);
  3031. }
  3032. return 0;
  3033. }
  3034. int EQ2Emu_lua_SetQuestCompleteAction(lua_State* state){
  3035. if(!lua_interface)
  3036. return 0;
  3037. Quest* quest = lua_interface->GetQuest(state);
  3038. if(quest){
  3039. string action = lua_interface->GetStringValue(state, 2);
  3040. if(action.length() > 0)
  3041. quest->SetCompleteAction(action);
  3042. }
  3043. return 0;
  3044. }
  3045. int EQ2Emu_lua_AddQuestStepCompleteAction(lua_State* state){
  3046. if(!lua_interface)
  3047. return 0;
  3048. Quest* quest = lua_interface->GetQuest(state);
  3049. if(quest){
  3050. int32 step = lua_interface->GetInt32Value(state, 2);
  3051. string action = lua_interface->GetStringValue(state, 3);
  3052. if(step > 0 && action.length() > 0)
  3053. quest->AddCompleteAction(step, action);
  3054. }
  3055. return 0;
  3056. }
  3057. int EQ2Emu_lua_AddQuestStepProgressAction(lua_State* state){
  3058. if(!lua_interface)
  3059. return 0;
  3060. Quest* quest = lua_interface->GetQuest(state);
  3061. if(quest){
  3062. int32 step = lua_interface->GetInt32Value(state, 2);
  3063. string action = lua_interface->GetStringValue(state, 3);
  3064. if(step > 0 && action.length() > 0)
  3065. quest->AddProgressAction(step, action);
  3066. }
  3067. return 0;
  3068. }
  3069. int EQ2Emu_lua_UpdateQuestDescription(lua_State* state){
  3070. if(!lua_interface)
  3071. return 0;
  3072. Quest* quest = lua_interface->GetQuest(state);
  3073. string description = lua_interface->GetStringValue(state, 2);
  3074. if(quest && description.length() > 0)
  3075. quest->SetDescription(description);
  3076. return 0;
  3077. }
  3078. int EQ2Emu_lua_SetCompletedDescription(lua_State* state){
  3079. if(!lua_interface)
  3080. return 0;
  3081. Quest* quest = lua_interface->GetQuest(state);
  3082. string description = lua_interface->GetStringValue(state, 2);
  3083. if(quest && description.length() > 0)
  3084. quest->SetCompletedDescription(description);
  3085. return 0;
  3086. }
  3087. int EQ2Emu_lua_UpdateQuestTaskGroupDescription(lua_State* state){
  3088. if(!lua_interface)
  3089. return 0;
  3090. Quest* quest = lua_interface->GetQuest(state);
  3091. int32 step = lua_interface->GetInt32Value(state, 2);
  3092. string description = lua_interface->GetStringValue(state, 3);
  3093. bool display_bullets = (lua_interface->GetInt8Value(state, 4) == 1);
  3094. if(quest && step > 0 && description.length() > 0){
  3095. quest->SetTaskGroupDescription(step, description, display_bullets);
  3096. if(quest->GetPlayer()){
  3097. Client* client = quest->GetPlayer()->GetZone()->GetClientBySpawn(quest->GetPlayer());
  3098. if(client)
  3099. client->SendQuestUpdateStep(quest, step, false);
  3100. }
  3101. }
  3102. return 0;
  3103. }
  3104. int EQ2Emu_lua_UpdateQuestStepDescription(lua_State* state){
  3105. if(!lua_interface)
  3106. return 0;
  3107. Quest* quest = lua_interface->GetQuest(state);
  3108. int32 step = lua_interface->GetInt32Value(state, 2);
  3109. string description = lua_interface->GetStringValue(state, 3);
  3110. if(quest && step > 0 && description.length() > 0){
  3111. quest->SetStepDescription(step, description);
  3112. if(quest->GetPlayer()){
  3113. Client* client = quest->GetPlayer()->GetZone()->GetClientBySpawn(quest->GetPlayer());
  3114. if(client)
  3115. client->SendQuestUpdateStep(quest, step);
  3116. }
  3117. }
  3118. return 0;
  3119. }
  3120. int EQ2Emu_lua_UpdateQuestZone(lua_State* state) {
  3121. Quest* quest = lua_interface->GetQuest(state);
  3122. string zone = lua_interface->GetStringValue(state, 2);
  3123. if (quest && zone.length() > 0)
  3124. quest->SetZone(zone);
  3125. return 0;
  3126. }
  3127. int EQ2Emu_lua_GiveQuestReward(lua_State* state){
  3128. if(!lua_interface)
  3129. return 0;
  3130. Quest* quest = lua_interface->GetQuest(state);
  3131. Spawn* spawn = lua_interface->GetSpawn(state, 2);
  3132. if(quest && spawn){
  3133. if(spawn->IsPlayer()){
  3134. Client* client = spawn->GetZone()->GetClientBySpawn(spawn);
  3135. if(client)
  3136. client->AddPendingQuestReward(quest);
  3137. }
  3138. }
  3139. return 0;
  3140. }
  3141. int EQ2Emu_lua_Harvest(lua_State* state){
  3142. if(!lua_interface)
  3143. return 0;
  3144. Spawn* player = lua_interface->GetSpawn(state);
  3145. Spawn* node = lua_interface->GetSpawn(state, 2);
  3146. if(player && node && player->IsPlayer() && node->IsGroundSpawn()){
  3147. Client* client = player->GetZone()->GetClientBySpawn(player);
  3148. if(client){
  3149. LogWrite(MISC__TODO, 1, "TODO", "Cancel harvest if skill insufficient; Func: %s, Line: %i", __FUNCTION__, __LINE__);
  3150. ((GroundSpawn*)node)->ProcessHarvest(client);
  3151. if(((GroundSpawn*)node)->GetNumberHarvests() == 0)
  3152. player->GetZone()->RemoveSpawn(true, node, true);
  3153. }
  3154. }
  3155. else if(player && player->IsPlayer()){
  3156. Client* client = player->GetZone()->GetClientBySpawn(player);
  3157. if(client)
  3158. client->Message(CHANNEL_COLOR_RED, "Invalid target for this spell.");
  3159. }
  3160. return 0;
  3161. }
  3162. int EQ2Emu_lua_Bind(lua_State* state){
  3163. if(!lua_interface)
  3164. return 0;
  3165. Spawn* spawn = lua_interface->GetSpawn(state);
  3166. int32 zone_id = lua_interface->GetInt32Value(state, 2);
  3167. float x = lua_interface->GetFloatValue(state, 3);
  3168. float y = lua_interface->GetFloatValue(state, 4);
  3169. float z = lua_interface->GetFloatValue(state, 5);
  3170. float h = lua_interface->GetFloatValue(state, 6);
  3171. if (!spawn) {
  3172. lua_interface->LogError("LUA Bind command error: spawn is not valid");
  3173. return 0;
  3174. }
  3175. if (!spawn->IsPlayer()) {
  3176. lua_interface->LogError("LUA Bind command error: spawn is not a player");
  3177. return 0;
  3178. }
  3179. if (zone_id == 0) {
  3180. Client* client = spawn->GetZone()->GetClientBySpawn(spawn);
  3181. if (!client) {
  3182. lua_interface->LogError("LUA Bind command error: unable to get client from spawn");
  3183. return 0;
  3184. }
  3185. if (!client->Bind())
  3186. client->SimpleMessage(CHANNEL_COLOR_RED, "Unable to set bind point.");
  3187. }
  3188. else {
  3189. Player* player = (Player*)spawn;
  3190. player->GetPlayerInfo()->SetBindZone(zone_id);
  3191. player->GetPlayerInfo()->SetBindX(x);
  3192. player->GetPlayerInfo()->SetBindY(y);
  3193. player->GetPlayerInfo()->SetBindZ(z);
  3194. player->GetPlayerInfo()->SetBindHeading(h);
  3195. }
  3196. return 0;
  3197. }
  3198. int EQ2Emu_lua_Gate(lua_State* state){
  3199. if(!lua_interface)
  3200. return 0;
  3201. Spawn* spawn = lua_interface->GetSpawn(state);
  3202. if(spawn){
  3203. if(spawn->IsPlayer()){
  3204. Client* client = spawn->GetZone()->GetClientBySpawn(spawn);
  3205. if(client){
  3206. if(!client->Gate())
  3207. client->SimpleMessage(CHANNEL_COLOR_RED, "Unable to gate.");
  3208. }
  3209. }
  3210. }
  3211. return 0;
  3212. }
  3213. int EQ2Emu_lua_IsBindAllowed(lua_State* state){
  3214. if(!lua_interface)
  3215. return 0;
  3216. bool ret = false;
  3217. Spawn* spawn = lua_interface->GetSpawn(state);
  3218. if(spawn){
  3219. if(spawn->IsPlayer()){
  3220. Client* client = spawn->GetZone()->GetClientBySpawn(spawn);
  3221. if(client)
  3222. ret = client->BindAllowed();
  3223. }
  3224. }
  3225. lua_interface->SetBooleanValue(state, ret);
  3226. return 1;
  3227. }
  3228. int EQ2Emu_lua_IsGateAllowed(lua_State* state){
  3229. if(!lua_interface)
  3230. return 0;
  3231. bool ret = false;
  3232. Spawn* spawn = lua_interface->GetSpawn(state);
  3233. if(spawn){
  3234. if(spawn->IsPlayer()){
  3235. Client* client = spawn->GetZone()->GetClientBySpawn(spawn);
  3236. if(client)
  3237. ret = client->GateAllowed();
  3238. }
  3239. }
  3240. lua_interface->SetBooleanValue(state, ret);
  3241. return 1;
  3242. }
  3243. int EQ2Emu_lua_IsAlive(lua_State* state) {
  3244. Spawn* spawn = lua_interface->GetSpawn(state);
  3245. if (spawn) {
  3246. lua_interface->SetBooleanValue(state, spawn->Alive());
  3247. return 1;
  3248. }
  3249. return 0;
  3250. }
  3251. int EQ2Emu_lua_IsInCombat(lua_State* state) {
  3252. if (!lua_interface)
  3253. return 0;
  3254. Spawn* spawn = lua_interface->GetSpawn(state);
  3255. if (spawn && spawn->IsEntity()) {
  3256. lua_interface->SetBooleanValue(state, ((Entity*)spawn)->EngagedInCombat());
  3257. return 1;
  3258. }
  3259. return 0;
  3260. }
  3261. int EQ2Emu_lua_SendMessage(lua_State* state) {
  3262. Spawn* spawn = lua_interface->GetSpawn(state);
  3263. string message = lua_interface->GetStringValue(state, 2);
  3264. string color_str = lua_interface->GetStringValue(state, 3);
  3265. int8 color = CHANNEL_COLOR_WHITE;
  3266. if (spawn && spawn->IsPlayer() && message.length() > 0) {
  3267. Client* client = spawn->GetZone()->GetClientBySpawn(spawn);
  3268. if (client) {
  3269. if (color_str.length() > 0) {
  3270. if (strncasecmp(color_str.c_str(), "red", 3) == 0)
  3271. color = CHANNEL_COLOR_RED;
  3272. else if (strncasecmp(color_str.c_str(), "yellow", 6) == 0)
  3273. color = CHANNEL_COLOR_YELLOW;
  3274. }
  3275. client->SimpleMessage(color, message.c_str());
  3276. }
  3277. }
  3278. return 0;
  3279. }
  3280. int EQ2Emu_lua_SendPopUpMessage(lua_State* state) {
  3281. Spawn* spawn = lua_interface->GetSpawn(state);
  3282. string message = lua_interface->GetStringValue(state, 2);
  3283. int8 red = lua_interface->GetInt8Value(state, 3);
  3284. int8 green = lua_interface->GetInt8Value(state, 4);
  3285. int8 blue = lua_interface->GetInt8Value(state, 5);
  3286. if (!spawn) {
  3287. lua_interface->LogError("LUA SendPopUpMessage command error: Spawn is not valid.");
  3288. return 0;
  3289. }
  3290. int32 words = ::CountWordsInString(message.c_str());
  3291. if (words < 5)
  3292. words = 5;
  3293. Client* client = spawn->GetZone()->GetClientBySpawn(spawn);
  3294. if (client)
  3295. client->SendPopupMessage(10, message.c_str(), "ui_harvested_normal", words, red, green, blue);
  3296. return 0;
  3297. }
  3298. int EQ2Emu_lua_SetServerControlFlag(lua_State* state) {
  3299. Spawn* spawn = lua_interface->GetSpawn(state);
  3300. int8 param = lua_interface->GetInt8Value(state, 2);
  3301. int8 param_value = lua_interface->GetInt8Value(state, 3);
  3302. int8 value = lua_interface->GetInt8Value(state, 4);
  3303. if (spawn && spawn->IsPlayer() && (param >= 1 && param <= 5)) {
  3304. Client* client = spawn->GetZone()->GetClientBySpawn(spawn);
  3305. if (client) {
  3306. PacketStruct* packet = configReader.getStruct("WS_ServerControlFlags", client->GetVersion());
  3307. switch (param) {
  3308. case 1: {
  3309. packet->setDataByName("parameter1", param_value);
  3310. break;
  3311. }
  3312. case 2: {
  3313. packet->setDataByName("parameter2", param_value);
  3314. break;
  3315. }
  3316. case 3: {
  3317. packet->setDataByName("parameter3", param_value);
  3318. break;
  3319. }
  3320. case 4: {
  3321. packet->setDataByName("parameter4", param_value);
  3322. break;
  3323. }
  3324. case 5: {
  3325. packet->setDataByName("parameter5", param_value);
  3326. break;
  3327. }
  3328. }
  3329. packet->setDataByName("value", value);
  3330. client->QueuePacket(packet->serialize());
  3331. safe_delete(packet);
  3332. }
  3333. }
  3334. return 0;
  3335. }
  3336. int EQ2Emu_lua_ToggleTracking(lua_State* state) {
  3337. Spawn* spawn = lua_interface->GetSpawn(state);
  3338. if (spawn && spawn->IsPlayer()) {
  3339. if (((Player*)spawn)->GetIsTracking())
  3340. spawn->GetZone()->AddPlayerTracking((Player*)spawn);
  3341. else
  3342. spawn->GetZone()->RemovePlayerTracking((Player*)spawn, TRACKING_STOP);
  3343. }
  3344. return 0;
  3345. }
  3346. int EQ2Emu_lua_AddPrimaryEntityCommand(lua_State* state) {
  3347. Spawn* player = lua_interface->GetSpawn(state);
  3348. Spawn* spawn = lua_interface->GetSpawn(state, 2);
  3349. string name = lua_interface->GetStringValue(state, 3);
  3350. float distance = lua_interface->GetFloatValue(state, 4);
  3351. string command = lua_interface->GetStringValue(state, 5);
  3352. string error_text = lua_interface->GetStringValue(state, 6);
  3353. int16 cast_time = lua_interface->GetInt16Value(state, 7);
  3354. int32 spell_visual = lua_interface->GetInt32Value(state, 8);
  3355. if (spawn && player && player->IsPlayer() && name.length() > 0) {
  3356. if (distance == 0)
  3357. distance = 10.0f;
  3358. if (command.length() == 0)
  3359. command = name;
  3360. spawn->AddPrimaryEntityCommand(name.c_str(), distance, command.c_str(), error_text.c_str(), cast_time, spell_visual);
  3361. player->GetZone()->SendUpdateDefaultCommand(spawn, command.c_str(), distance);
  3362. }
  3363. return 0;
  3364. }
  3365. int EQ2Emu_lua_AddSpellBookEntry(lua_State* state) {
  3366. if(!lua_interface)
  3367. return 0;
  3368. Spawn* player = lua_interface->GetSpawn(state);
  3369. int32 spellid = lua_interface->GetInt32Value(state, 2);
  3370. int16 tier = lua_interface->GetInt16Value(state, 3);
  3371. Spell* spell = master_spell_list.GetSpell(spellid, tier);
  3372. if (player && spell && player->IsPlayer()) {
  3373. Client* client = player->GetZone()->GetClientBySpawn(player);
  3374. if (client) {
  3375. if (!client->GetPlayer()->HasSpell(spellid, tier -1, true))
  3376. {
  3377. Spell* spell = master_spell_list.GetSpell(spellid, tier);
  3378. client->GetPlayer()->AddSpellBookEntry(spellid, 1, client->GetPlayer()->GetFreeSpellBookSlot(spell->GetSpellData()->spell_book_type), spell->GetSpellData()->spell_book_type, spell->GetSpellData()->linked_timer, true);
  3379. client->GetPlayer()->UnlockSpell(spell);
  3380. client->SendSpellUpdate(spell);
  3381. }
  3382. else
  3383. {
  3384. Spell* spell = master_spell_list.GetSpell(spellid, tier) ;
  3385. int8 old_slot = client->GetPlayer()->GetSpellSlot(spell->GetSpellID());
  3386. client->GetPlayer()->RemoveSpellBookEntry(spell->GetSpellID());
  3387. client->GetPlayer()->AddSpellBookEntry(spell->GetSpellID(), spell->GetSpellTier(), old_slot, spell->GetSpellData()->spell_book_type, spell->GetSpellData()->linked_timer, true);
  3388. client->GetPlayer()->UnlockSpell(spell);
  3389. client->SendSpellUpdate(spell);
  3390. }
  3391. //if (client ) {
  3392. // ((Player*)player)->AddSpellBookEntry(spell->GetSpellID(), spell->GetSpellTier(), ((Player*)player)->GetFreeSpellBookSlot(spell->GetSpellData()->spell_book_type), spell->GetSpellData()->spell_book_type, spell->GetSpellData()->linked_timer, true);
  3393. EQ2Packet* outapp = ((Player*)player)->GetSpellBookUpdatePacket(client->GetVersion());
  3394. if (outapp)
  3395. client->QueuePacket(outapp);
  3396. }
  3397. }
  3398. return 0;
  3399. }
  3400. int EQ2Emu_lua_HasFreeSlot(lua_State* state) {
  3401. if(!lua_interface)
  3402. return 0;
  3403. Spawn* player = lua_interface->GetSpawn(state);
  3404. if(player && player->IsPlayer()) {
  3405. lua_interface->SetBooleanValue(state, ((Player*)player)->item_list.HasFreeSlot());
  3406. return 1;
  3407. }
  3408. return 0;
  3409. }
  3410. int EQ2Emu_lua_Attack(lua_State* state) {
  3411. if (lua_interface) {
  3412. Spawn* npc = lua_interface->GetSpawn(state);
  3413. Spawn* player = lua_interface->GetSpawn(state, 2);
  3414. if (npc && player && npc->IsNPC() && player->IsPlayer())
  3415. ((NPC*)npc)->AddHate((Entity*)player, 100);
  3416. }
  3417. return 0;
  3418. }
  3419. int EQ2Emu_lua_ApplySpellVisual(lua_State* state) {
  3420. if (lua_interface) {
  3421. Spawn* target = lua_interface->GetSpawn(state);
  3422. int32 spell_visual = lua_interface->GetInt32Value(state, 2);
  3423. if (target && target->GetZone())
  3424. target->GetZone()->SendCastSpellPacket(spell_visual, target);
  3425. }
  3426. return 0;
  3427. }
  3428. int EQ2Emu_lua_HasCollectionsToHandIn(lua_State *state) {
  3429. Spawn *player;
  3430. if (lua_interface) {
  3431. player = lua_interface->GetSpawn(state);
  3432. if (player && player->IsPlayer()) {
  3433. lua_interface->SetBooleanValue(state, ((Player *)player)->GetCollectionList()->HasCollectionsToHandIn());
  3434. return 1;
  3435. }
  3436. }
  3437. return 0;
  3438. }
  3439. int EQ2Emu_lua_HandInCollections(lua_State *state) {
  3440. Spawn *player;
  3441. Client *client;
  3442. if (lua_interface) {
  3443. player = lua_interface->GetSpawn(state);
  3444. if (player && ((Player *)player)->IsPlayer() && ((Player *)player)->GetCollectionList()->HasCollectionsToHandIn())
  3445. if ((client = player->GetZone()->GetClientBySpawn(player)))
  3446. client->HandInCollections();
  3447. }
  3448. return 0;
  3449. }
  3450. int EQ2Emu_lua_UseWidget(lua_State* state) {
  3451. Spawn* widget;
  3452. if (lua_interface) {
  3453. widget = lua_interface->GetSpawn(state);
  3454. if (widget && widget->IsWidget())
  3455. ((Widget*)widget)->HandleUse(NULL, "");
  3456. }
  3457. return 0;
  3458. }
  3459. int EQ2Emu_lua_SetSpellList(lua_State* state) {
  3460. Spawn* spawn = 0;
  3461. int32 primary_list = 0;
  3462. int32 secondary_list = 0;
  3463. if (lua_interface) {
  3464. spawn = lua_interface->GetSpawn(state);
  3465. primary_list = lua_interface->GetInt32Value(state, 2);
  3466. secondary_list = lua_interface->GetInt32Value(state, 3);
  3467. if (!spawn->IsNPC()) {
  3468. lua_interface->LogError("LUA SetSpellList command error: Spawn was not a valid NPC");
  3469. return 0;
  3470. }
  3471. NPC* npc = (NPC*)spawn;
  3472. npc->SetPrimarySpellList(primary_list);
  3473. npc->SetSecondarySpellList(secondary_list);
  3474. npc->SetSpells(npc->GetZone()->GetNPCSpells(npc->GetPrimarySpellList(), npc->GetSecondarySpellList()));
  3475. }
  3476. return 0;
  3477. }
  3478. int EQ2Emu_lua_GetPet(lua_State* state){
  3479. if(!lua_interface)
  3480. return 0;
  3481. Spawn* spawn = lua_interface->GetSpawn(state);
  3482. if(spawn) {
  3483. if (spawn->IsEntity() && ((Entity*)spawn)->GetPet()) {
  3484. lua_interface->SetSpawnValue(state, ((Entity*)spawn)->GetPet());
  3485. return 1;
  3486. }
  3487. }
  3488. return 0;
  3489. }
  3490. int EQ2Emu_lua_GetCharmedPet(lua_State* state){
  3491. if(!lua_interface)
  3492. return 0;
  3493. Spawn* spawn = lua_interface->GetSpawn(state);
  3494. if(spawn) {
  3495. if (spawn->IsEntity() && ((Entity*)spawn)->GetCharmedPet()) {
  3496. lua_interface->SetSpawnValue(state, ((Entity*)spawn)->GetCharmedPet());
  3497. return 1;
  3498. }
  3499. }
  3500. return 0;
  3501. }
  3502. int EQ2Emu_lua_GetDeityPet(lua_State* state){
  3503. if(!lua_interface)
  3504. return 0;
  3505. Spawn* spawn = lua_interface->GetSpawn(state);
  3506. if(spawn) {
  3507. if (spawn->IsEntity() && ((Entity*)spawn)->GetDeityPet()) {
  3508. lua_interface->SetSpawnValue(state, ((Entity*)spawn)->GetDeityPet());
  3509. return 1;
  3510. }
  3511. }
  3512. return 0;
  3513. }
  3514. int EQ2Emu_lua_GetCosmeticPet(lua_State* state){
  3515. if(!lua_interface)
  3516. return 0;
  3517. Spawn* spawn = lua_interface->GetSpawn(state);
  3518. if(spawn) {
  3519. if (spawn->IsEntity() && ((Entity*)spawn)->GetCosmeticPet()) {
  3520. lua_interface->SetSpawnValue(state, ((Entity*)spawn)->GetCosmeticPet());
  3521. return 1;
  3522. }
  3523. }
  3524. return 0;
  3525. }
  3526. int EQ2Emu_lua_Charm(lua_State* state){
  3527. if (!lua_interface)
  3528. return 0;
  3529. Spawn* owner = lua_interface->GetSpawn(state);
  3530. Spawn* pet = lua_interface->GetSpawn(state, 2);
  3531. LuaSpell* luaspell = lua_interface->GetCurrentSpell(state);
  3532. if (!luaspell) {
  3533. lua_interface->LogError("LUA Charm command error: Spell is not valid, charm can only be used in spell scripts.");
  3534. return 0;
  3535. }
  3536. if (owner && pet && owner->IsEntity() && pet->IsNPC()) {
  3537. ((Entity*)owner)->SetCharmedPet((Entity*)pet);
  3538. pet->SetPet(true);
  3539. ((NPC*)pet)->SetPetType(PET_TYPE_CHARMED);
  3540. ((NPC*)pet)->SetOwner((Entity*)owner);
  3541. // If owner is player and player does not have a summoned pet set the players charsheet
  3542. if (owner->IsPlayer() && !((Entity*)owner)->GetPet()) {
  3543. Player* player = (Player*)owner;
  3544. player->GetInfoStruct()->pet_id = player->GetIDWithPlayerSpawn(pet);
  3545. strcpy(player->GetInfoStruct()->pet_name, pet->GetName());
  3546. player->GetInfoStruct()->pet_movement = 2;
  3547. player->GetInfoStruct()->pet_behavior = 3;
  3548. player->GetInfoStruct()->pet_health_pct = 1.0f;
  3549. player->GetInfoStruct()->pet_power_pct = 1.0f;
  3550. // Make sure the values get sent to the client
  3551. player->SetCharSheetChanged(true);
  3552. }
  3553. // Clear the spawns script so the charmed mob doesn't try to do anything like random walks
  3554. pet->SetSpawnScript("");
  3555. // Set faction to the same as the owner
  3556. pet->SetFactionID(owner->GetFactionID());
  3557. ((NPC*)pet)->SetPetSpellID(luaspell->spell->GetSpellData()->id);
  3558. ((NPC*)pet)->SetPetSpellTier(luaspell->spell->GetSpellData()->tier);
  3559. // Clear hate list
  3560. ((NPC*)pet)->Brain()->ClearHate();
  3561. // Set the brain to a pet brain
  3562. ((NPC*)pet)->SetBrain(new CombatPetBrain((NPC*)pet));
  3563. }
  3564. return 0;
  3565. }
  3566. int EQ2Emu_lua_GetGroup(lua_State* state) {
  3567. if (!lua_interface)
  3568. return 0;
  3569. Spawn* spawn = lua_interface->GetSpawn(state);
  3570. if (!spawn) {
  3571. lua_interface->LogError("LUA GetGroup command error: spawn is not valid");
  3572. return 0;
  3573. }
  3574. vector<Spawn*> groupMembers;
  3575. if (!spawn->IsPlayer() && spawn->HasSpawnGroup()) {
  3576. groupMembers = *spawn->GetSpawnGroup();
  3577. }
  3578. else if (spawn->IsPlayer() && ((Player*)spawn)->GetGroupMemberInfo()) {
  3579. world.GetGroupManager()->GroupLock(__FUNCTION__, __LINE__);
  3580. deque<GroupMemberInfo*>::iterator itr;
  3581. deque<GroupMemberInfo*>* members = world.GetGroupManager()->GetGroupMembers(((Player*)spawn)->GetGroupMemberInfo()->group_id);
  3582. GroupMemberInfo* info = 0;
  3583. for(itr = members->begin(); itr != members->end(); itr++){
  3584. info = *itr;
  3585. if(info->client)
  3586. groupMembers.push_back(info->client->GetPlayer());
  3587. }
  3588. world.GetGroupManager()->ReleaseGroupLock(__FUNCTION__, __LINE__);
  3589. }
  3590. else
  3591. return 0;
  3592. lua_createtable(state, groupMembers.size(), 0);
  3593. int newTable = lua_gettop(state);
  3594. for(int32 i=0; i < groupMembers.size(); i++) {
  3595. lua_interface->SetSpawnValue(state, groupMembers.at(i));
  3596. lua_rawseti(state, newTable, i + 1);
  3597. }
  3598. return 1;
  3599. }
  3600. int EQ2Emu_lua_CreateOptionWindow(lua_State* state) {
  3601. if (!lua_interface)
  3602. return 0;
  3603. vector<OptionWindowOption>* option_window = new vector<OptionWindowOption>();
  3604. lua_interface->SetOptionWindowValue(state, option_window);
  3605. return 1;
  3606. }
  3607. int EQ2Emu_lua_AddOptionWindowOption(lua_State* state) {
  3608. if (!lua_interface)
  3609. return 0;
  3610. vector<OptionWindowOption>* option_window = lua_interface->GetOptionWindow(state);
  3611. if (option_window) {
  3612. OptionWindowOption option_window_option;
  3613. option_window_option.optionName = lua_interface->GetStringValue(state, 2);
  3614. option_window_option.optionDescription = lua_interface->GetStringValue(state, 3);
  3615. option_window_option.optionIconSheet = lua_interface->GetInt32Value(state, 4);
  3616. option_window_option.optionIconID = lua_interface->GetInt16Value(state, 5);
  3617. option_window_option.optionCommand = lua_interface->GetStringValue(state, 6);
  3618. option_window_option.optionConfirmTitle = lua_interface->GetStringValue(state, 7);
  3619. if (option_window_option.optionName.length() > 0 && option_window_option.optionDescription.length() > 0)
  3620. option_window->push_back(option_window_option);
  3621. }
  3622. return 0;
  3623. }
  3624. int EQ2Emu_lua_SendOptionWindow(lua_State* state) {
  3625. if (!lua_interface)
  3626. return 0;
  3627. vector<OptionWindowOption>* option_window = lua_interface->GetOptionWindow(state);
  3628. Spawn* player = lua_interface->GetSpawn(state, 2);
  3629. string window_title = lua_interface->GetStringValue(state, 3);
  3630. string cancel_command = lua_interface->GetStringValue(state, 4);
  3631. Client* client = player->GetZone()->GetClientBySpawn(player);
  3632. if (option_window && window_title.length() > 0 && client) {
  3633. PacketStruct* packet = configReader.getStruct("WS_SelectTradeskill", client->GetVersion());
  3634. if (!packet)
  3635. return 0;
  3636. packet->setDataByName("title_text", window_title.c_str());
  3637. if (cancel_command.length() > 0)
  3638. packet->setDataByName("command_text_cancel", cancel_command.c_str());
  3639. packet->setArrayLengthByName("num_selections", option_window->size());
  3640. vector<OptionWindowOption>::iterator itr;
  3641. int8 i = 0;
  3642. for (itr = option_window->begin(); itr != option_window->end(); itr++) {
  3643. OptionWindowOption opt = *itr;
  3644. packet->setArrayDataByName("tradeskill_name", opt.optionName.c_str(), i);
  3645. packet->setArrayDataByName("tradeskill_description", opt.optionDescription.c_str(), i);
  3646. packet->setArrayDataByName("icon_sheet", opt.optionIconSheet, i);
  3647. packet->setArrayDataByName("icon_id", opt.optionIconID, i);
  3648. if (opt.optionCommand.length() > 0)
  3649. packet->setArrayDataByName("command_text", opt.optionCommand.c_str(), i);
  3650. if (opt.optionConfirmTitle.length() > 0)
  3651. packet->setArrayDataByName("confirm_window_title", opt.optionConfirmTitle.c_str(), i);
  3652. i++;
  3653. }
  3654. client->QueuePacket(packet->serialize());
  3655. safe_delete(option_window);
  3656. safe_delete(packet);
  3657. }
  3658. return 0;
  3659. }
  3660. int EQ2Emu_lua_GetTradeskillClass(lua_State* state) {
  3661. if (!lua_interface)
  3662. return 0;
  3663. Spawn* spawn = lua_interface->GetSpawn(state);
  3664. if (spawn) {
  3665. lua_interface->SetInt32Value(state, spawn->GetTradeskillClass());
  3666. return 1;
  3667. }
  3668. else
  3669. lua_interface->LogError("LUA GetTradeskillClass command error: Spawn was not valid");
  3670. return 0;
  3671. }
  3672. int EQ2Emu_lua_GetTradeskillLevel(lua_State* state) {
  3673. if (!lua_interface)
  3674. return 0;
  3675. Spawn* spawn = lua_interface->GetSpawn(state);
  3676. if (spawn) {
  3677. lua_interface->SetInt32Value(state, spawn->GetTSLevel());
  3678. return 1;
  3679. }
  3680. else
  3681. lua_interface->LogError("LUA GetTradeskillLevel command error: Spawns was not valid");
  3682. return 0;
  3683. }
  3684. int EQ2Emu_lua_GetTradeskillClassName(lua_State* state) {
  3685. if (!lua_interface)
  3686. return 0;
  3687. Spawn* spawn = lua_interface->GetSpawn(state);
  3688. if (spawn) {
  3689. int8 class_id = spawn->GetTradeskillClass();
  3690. // Need to add 42 for the offset in the array
  3691. class_id += 44;
  3692. lua_interface->SetStringValue(state, classes.GetClassNameCase(class_id).c_str());
  3693. return 1;
  3694. }
  3695. else
  3696. lua_interface->LogError("LUA GetTradeskillClassName command error: Spawn was not valid");
  3697. return 0;
  3698. }
  3699. int EQ2Emu_lua_SetTradeskillLevel(lua_State* state) {
  3700. if (!lua_interface)
  3701. return 0;
  3702. Spawn* spawn = lua_interface->GetSpawn(state);
  3703. int16 level = lua_interface->GetInt8Value(state, 2);
  3704. if (spawn) {
  3705. if (spawn->IsPlayer())
  3706. spawn->GetZone()->GetClientBySpawn(spawn)->ChangeTSLevel(spawn->GetTSLevel(), level);
  3707. else
  3708. spawn->SetTSLevel(level);
  3709. }
  3710. else
  3711. lua_interface->LogError("LUA SetTradeskillLevel command error: Spawn was not valid");
  3712. return 0;
  3713. }
  3714. int EQ2Emu_lua_SummonPet(lua_State* state) {
  3715. // Check to see if we have a valid lua_interface
  3716. if(!lua_interface)
  3717. return 0;
  3718. // Get the spawn that is getting the pet
  3719. Spawn* spawn = lua_interface->GetSpawn(state);
  3720. // Get the DB ID of the pet
  3721. int32 pet_id = lua_interface->GetInt32Value(state, 2);
  3722. // The max level the pet can gain
  3723. int8 max_level = lua_interface->GetInt8Value(state, 3);
  3724. // Get the spell that this command was called from
  3725. LuaSpell* luaspell = lua_interface->GetCurrentSpell(state);
  3726. // Check to make sure the spawn pointer is valid
  3727. if (!spawn) {
  3728. lua_interface->LogError("LUA SummonPet command error: Spawn is not valid");
  3729. return 0;
  3730. }
  3731. // Check to make sure the spawn is an entity
  3732. if (!spawn->IsEntity()) {
  3733. lua_interface->LogError("LUA SummonPet command error: Spawn is not an entity");
  3734. return 0;
  3735. }
  3736. // Check to make sure the spawn doesn't already have a pet of this type
  3737. if (((Entity*)spawn)->GetPet()) {
  3738. if (spawn->IsPlayer()) {
  3739. Client* client = spawn->GetZone()->GetClientBySpawn(spawn);
  3740. if (client)
  3741. client->SimpleMessage(CHANNEL_COLOR_YELLOW, "You already have a pet.");
  3742. }
  3743. lua_interface->LogError("LUA SummonPet command error: spawn already has a pet of this type");
  3744. return 0;
  3745. }
  3746. // Check to see if the DB ID for the pet is set
  3747. if (pet_id == 0) {
  3748. lua_interface->LogError("LUA SummonPet command error: pet_id can not be set to 0");
  3749. return 0;
  3750. }
  3751. // Check to see if the pointer to the spell is valid
  3752. if (!luaspell) {
  3753. lua_interface->LogError("LUA SummonPet command error: valid spell not found, SummonPet can only be used in spell scripts");
  3754. return 0;
  3755. }
  3756. // Get a pointer to a spawn with the given DB ID and check if the pointer is valid
  3757. Spawn* pet = spawn->GetZone()->GetSpawn(pet_id);
  3758. if(!pet) {
  3759. lua_interface->LogError("LUA SummonPet command error: Could not find spawn with id of %u.", pet_id);
  3760. return 0;
  3761. }
  3762. // Check to make sure the pet is an npc
  3763. if (!pet->IsNPC()) {
  3764. lua_interface->LogError("LUA SummonPet command error: id (%u) did not point to a npc", pet_id);
  3765. return 0;
  3766. }
  3767. // Spawn the pet at the same location as the owner
  3768. pet->SetX(spawn->GetX());
  3769. pet->SetY(spawn->GetY());
  3770. pet->SetZ(spawn->GetZ());
  3771. pet->SetLocation(spawn->GetLocation());
  3772. pet->SetHeading(spawn->GetHeading());
  3773. spawn->GetZone()->AddSpawn(pet);
  3774. /*
  3775. const char* spawn_script = world.GetSpawnScript(pet_id);
  3776. if(spawn_script && lua_interface->GetSpawnScript(spawn_script) != 0){
  3777. spawn->SetSpawnScript(string(spawn_script));
  3778. zone->CallSpawnScript(spawn, SPAWN_SCRIPT_SPAWN);
  3779. }*/
  3780. // Get a random pet name
  3781. string random_pet_name;
  3782. int16 rand_index = MakeRandomInt(0, spawn->GetZone()->pet_names.size() - 1);
  3783. random_pet_name = spawn->GetZone()->pet_names.at(rand_index);
  3784. LogWrite(PET__DEBUG, 0, "Pets", "Randomize Pet Name: '%s' (rand: %i)", random_pet_name.c_str(), rand_index);
  3785. // If player set various values for the char sheet (pet window)
  3786. if (spawn->IsPlayer()) {
  3787. Player* player = (Player*)spawn;
  3788. player->GetInfoStruct()->pet_id = player->GetIDWithPlayerSpawn(pet);
  3789. strcpy(player->GetInfoStruct()->pet_name, random_pet_name.c_str());
  3790. player->GetInfoStruct()->pet_movement = 2;
  3791. player->GetInfoStruct()->pet_behavior = 3;
  3792. player->GetInfoStruct()->pet_health_pct = 1.0f;
  3793. player->GetInfoStruct()->pet_power_pct = 1.0f;
  3794. // Make sure the values get sent to the client
  3795. player->SetCharSheetChanged(true);
  3796. }
  3797. // Set the pets name
  3798. pet->SetName(random_pet_name.c_str());
  3799. // Set the level of the pet to the owners level or max level(if set) if owners level is greater
  3800. if (max_level > 0)
  3801. pet->SetLevel(spawn->GetLevel() >= max_level ? max_level : spawn->GetLevel());
  3802. else
  3803. pet->SetLevel(spawn->GetLevel());
  3804. // Set the max level this pet can reach
  3805. ((NPC*)pet)->SetMaxPetLevel(max_level);
  3806. // Set the faction of the pet to the same faction as the owner
  3807. pet->SetFactionID(spawn->GetFactionID());
  3808. // Set the spawn as a pet
  3809. pet->SetPet(true);
  3810. // Give a pointer of the owner to the pet
  3811. ((NPC*)pet)->SetOwner((Entity*)spawn);
  3812. // Give a pointer of the pet to the owner
  3813. ((Entity*)spawn)->SetCombatPet((Entity*)pet);
  3814. // Set the pet type
  3815. ((NPC*)pet)->SetPetType(PET_TYPE_COMBAT);
  3816. // Set the spell id used to create this pet
  3817. ((NPC*)pet)->SetPetSpellID(luaspell->spell->GetSpellData()->id);
  3818. // Set the spell tier used to create this pet
  3819. ((NPC*)pet)->SetPetSpellTier(luaspell->spell->GetSpellData()->tier);
  3820. // Set the pets spawn type to 6
  3821. pet->SetSpawnType(6);
  3822. // Set the pets brain
  3823. ((NPC*)pet)->SetBrain(new CombatPetBrain((NPC*)pet));
  3824. // Check to see if the pet has a subtitle
  3825. if (strlen(pet->GetSubTitle()) > 0) {
  3826. // Add the players name to the front of the sub title
  3827. string pet_subtitle;
  3828. pet_subtitle.append(spawn->GetName()).append("'s ").append(pet->GetSubTitle());
  3829. LogWrite(PET__DEBUG, 0, "Pets", "Pet Subtitle: '%s'", pet_subtitle.c_str());
  3830. // Set the pets subtitle to the new one
  3831. pet->SetSubTitle(pet_subtitle.c_str());
  3832. }
  3833. // Add the "Pet Options" entity command to the pet
  3834. pet->AddSecondaryEntityCommand("Pet Options", 10.0f, "petoptions", "", 0, 0);
  3835. // Set the pet as the return value for this function
  3836. lua_interface->SetSpawnValue(state, pet);
  3837. return 1;
  3838. }
  3839. int EQ2Emu_lua_SummonDeityPet(lua_State* state){
  3840. if(!lua_interface)
  3841. return 0;
  3842. Spawn* spawn = lua_interface->GetSpawn(state);
  3843. int32 pet_id = lua_interface->GetInt32Value(state, 2);
  3844. LuaSpell* luaspell = lua_interface->GetCurrentSpell(state);
  3845. if (!spawn) {
  3846. lua_interface->LogError("LUA SummonDeityPet command error: Spawn is not valid");
  3847. return 0;
  3848. }
  3849. if (!spawn->IsEntity()) {
  3850. lua_interface->LogError("LUA SummonDeityPet command error: Spawn is not an entity");
  3851. return 0;
  3852. }
  3853. if (((Entity*)spawn)->GetDeityPet()) {
  3854. if (spawn->IsPlayer()) {
  3855. Client* client = spawn->GetZone()->GetClientBySpawn(spawn);
  3856. if (client)
  3857. client->SimpleMessage(CHANNEL_COLOR_YELLOW, "You already have a deity pet.");
  3858. }
  3859. lua_interface->LogError("LUA SummonDeityPet command error: spawn already has a pet of this type");
  3860. return 0;
  3861. }
  3862. if (pet_id == 0) {
  3863. lua_interface->LogError("LUA SummonDeityPet command error: pet_id can not be set to 0");
  3864. return 0;
  3865. }
  3866. if (!luaspell) {
  3867. lua_interface->LogError("LUA SummonDeityPet command error: valid spell not found, SummonDeityPet can only be used in spell scripts");
  3868. return 0;
  3869. }
  3870. Spawn* pet = spawn->GetZone()->GetSpawn(pet_id);
  3871. if(!pet) {
  3872. lua_interface->LogError("LUA SummonDeityPet command error: Could not find spawn with id of %u.", pet_id);
  3873. return 0;
  3874. }
  3875. if (!pet->IsNPC()) {
  3876. lua_interface->LogError("LUA SummonDeityPet command error: spawn with id of %u is not a npc", pet_id);
  3877. return 0;
  3878. }
  3879. pet->SetX(spawn->GetX());
  3880. pet->SetY(spawn->GetY());
  3881. pet->SetZ(spawn->GetZ());
  3882. pet->SetLocation(spawn->GetLocation());
  3883. pet->SetHeading(spawn->GetHeading());
  3884. spawn->GetZone()->AddSpawn(pet);
  3885. string random_pet_name;
  3886. int16 rand_index = MakeRandomInt(0, spawn->GetZone()->pet_names.size() - 1);
  3887. random_pet_name = spawn->GetZone()->pet_names.at(rand_index);
  3888. LogWrite(PET__DEBUG, 0, "Pets", "Randomize Pet Name: '%s' (rand: %i)", random_pet_name.c_str(), rand_index);
  3889. pet->SetName(random_pet_name.c_str());
  3890. pet->SetLevel(spawn->GetLevel());
  3891. pet->SetFactionID(spawn->GetFactionID());
  3892. pet->SetPet(true);
  3893. ((NPC*)pet)->SetPetType(PET_TYPE_DEITY);
  3894. ((NPC*)pet)->SetOwner((Entity*)spawn);
  3895. ((Entity*)spawn)->SetDeityPet((Entity*)pet);
  3896. pet->SetSpawnType(6);
  3897. ((NPC*)pet)->SetBrain(new NonCombatPetBrain((NPC*)pet));
  3898. ((NPC*)pet)->SetPetSpellID(luaspell->spell->GetSpellData()->id);
  3899. ((NPC*)pet)->SetPetSpellTier(luaspell->spell->GetSpellData()->tier);
  3900. if (strlen(pet->GetSubTitle()) > 0) {
  3901. string pet_subtitle;
  3902. pet_subtitle.append(spawn->GetName()).append("'s ").append(pet->GetSubTitle());
  3903. LogWrite(PET__DEBUG, 0, "Pets", "Pet Subtitle: '%s'", pet_subtitle.c_str());
  3904. pet->SetSubTitle(pet_subtitle.c_str());
  3905. }
  3906. // deity and cosmetic pets are not attackable
  3907. pet->SetAttackable(false);
  3908. pet->AddSecondaryEntityCommand("Pet Options", 10.0f, "petoptions", "", 0, 0);
  3909. lua_interface->SetSpawnValue(state, pet);
  3910. return 1;
  3911. }
  3912. int EQ2Emu_lua_SummonCosmeticPet(lua_State* state){
  3913. if(!lua_interface)
  3914. return 0;
  3915. Spawn* spawn = lua_interface->GetSpawn(state);
  3916. int32 pet_id = lua_interface->GetInt32Value(state, 2);
  3917. LuaSpell* luaspell = lua_interface->GetCurrentSpell(state);
  3918. if (!spawn) {
  3919. lua_interface->LogError("LUA SummonCosmeticPet command error: Spawn is not valid");
  3920. return 0;
  3921. }
  3922. if (!spawn->IsEntity()) {
  3923. lua_interface->LogError("LUA SummonCosmeticPet command error: Spawn is not an entity");
  3924. return 0;
  3925. }
  3926. if (((Entity*)spawn)->GetCosmeticPet()) {
  3927. if (spawn->IsPlayer()) {
  3928. Client* client = spawn->GetZone()->GetClientBySpawn(spawn);
  3929. if (client)
  3930. client->SimpleMessage(CHANNEL_COLOR_YELLOW, "You already have a cosmetic pet.");
  3931. }
  3932. lua_interface->LogError("LUA SummonCosmeticPet command error: spawn already has a pet of this type");
  3933. return 0;
  3934. }
  3935. if (pet_id == 0) {
  3936. lua_interface->LogError("LUA SummonCosmeticPet command error: pet_id can not be set to 0");
  3937. return 0;
  3938. }
  3939. if (!luaspell) {
  3940. lua_interface->LogError("LUA SummonCosmeticPet command error: valid spell not found, SummonCosmeticPet can only be used in spell scripts");
  3941. return 0;
  3942. }
  3943. Spawn* pet = spawn->GetZone()->GetSpawn(pet_id);
  3944. if(!pet) {
  3945. lua_interface->LogError("LUA SummonCosmeticPet command error: Could not find spawn with id of %u.", pet_id);
  3946. return 0;
  3947. }
  3948. if (!pet->IsNPC()) {
  3949. lua_interface->LogError("LUA SummonCosmeticPet command error: spawn with id of %u is not a npc", pet_id);
  3950. return 0;
  3951. }
  3952. pet->SetX(spawn->GetX());
  3953. pet->SetY(spawn->GetY());
  3954. pet->SetZ(spawn->GetZ());
  3955. pet->SetLocation(spawn->GetLocation());
  3956. pet->SetHeading(spawn->GetHeading());
  3957. spawn->GetZone()->AddSpawn(pet);
  3958. string random_pet_name;
  3959. int16 rand_index = MakeRandomInt(0, spawn->GetZone()->pet_names.size() - 1);
  3960. random_pet_name = spawn->GetZone()->pet_names.at(rand_index);
  3961. LogWrite(PET__DEBUG, 0, "Pets", "Randomize Pet Name: '%s' (rand: %i)", random_pet_name.c_str(), rand_index);
  3962. pet->SetName(random_pet_name.c_str());
  3963. pet->SetLevel(spawn->GetLevel());
  3964. pet->SetFactionID(spawn->GetFactionID());
  3965. pet->SetPet(true);
  3966. ((NPC*)pet)->SetPetType(PET_TYPE_COSMETIC);
  3967. ((NPC*)pet)->SetOwner((Entity*)spawn);
  3968. ((Entity*)spawn)->SetCosmeticPet((Entity*)pet);
  3969. pet->SetSpawnType(6);
  3970. ((NPC*)pet)->SetBrain(new NonCombatPetBrain((NPC*)pet));
  3971. ((NPC*)pet)->SetPetSpellID(luaspell->spell->GetSpellData()->id);
  3972. ((NPC*)pet)->SetPetSpellTier(luaspell->spell->GetSpellData()->tier);
  3973. if (strlen(pet->GetSubTitle()) > 0) {
  3974. string pet_subtitle;
  3975. pet_subtitle.append(spawn->GetName()).append("'s ").append(pet->GetSubTitle());
  3976. LogWrite(PET__DEBUG, 0, "Pets", "Pet Subtitle: '%s'", pet_subtitle.c_str());
  3977. pet->SetSubTitle(pet_subtitle.c_str());
  3978. }
  3979. pet->SetAttackable(false);
  3980. pet->AddSecondaryEntityCommand("Pet Options", 10.0f, "petoptions", "", 0, 0);
  3981. lua_interface->SetSpawnValue(state, pet);
  3982. return 1;
  3983. }
  3984. int EQ2Emu_lua_DismissPet(lua_State* state) {
  3985. if (!lua_interface)
  3986. return 0;
  3987. Spawn* spawn = lua_interface->GetSpawn(state);
  3988. if (!spawn) {
  3989. lua_interface->LogError("LUA DismissPet command error: spawn is not valid");
  3990. return 0;
  3991. }
  3992. if (!spawn->IsPet()) {
  3993. lua_interface->LogError("LUA DismissPet command error: spawn is not a pet");
  3994. return 0;
  3995. }
  3996. if (!((NPC*)spawn)->IsDismissing())
  3997. ((NPC*)spawn)->GetOwner()->DismissPet((NPC*)spawn);
  3998. return 0;
  3999. }
  4000. int EQ2Emu_lua_SetQuestFeatherColor(lua_State* state){
  4001. if(!lua_interface)
  4002. return 0;
  4003. Quest* quest = lua_interface->GetQuest(state);
  4004. if (!quest) {
  4005. lua_interface->LogError("LUA SetQuestFeatherColor command error: valid quest not found, SetQuestFeatherColor can only be called from a quest script");
  4006. return 0;
  4007. }
  4008. int8 feather_color = lua_interface->GetInt8Value(state, 2);
  4009. if(feather_color > 0)
  4010. quest->SetFeatherColor(feather_color);
  4011. return 0;
  4012. }
  4013. int EQ2Emu_lua_RemoveSpawnAccess(lua_State* state){
  4014. if(!lua_interface)
  4015. return 0;
  4016. Spawn* spawn = lua_interface->GetSpawn(state);
  4017. Spawn* spawn2 = lua_interface->GetSpawn(state, 2);
  4018. if (!spawn) {
  4019. lua_interface->LogError("LUA RemoveSpawnAccess command error: first spawn is not valid");
  4020. return 0;
  4021. }
  4022. if (!spawn2) {
  4023. lua_interface->LogError("LUA RemoveSpawnAccess command error: second spawn is not valid");
  4024. return 0;
  4025. }
  4026. spawn->RemoveSpawnAccess(spawn2);
  4027. return 0;
  4028. }
  4029. int EQ2Emu_lua_SpawnByLocationID(lua_State* state) {
  4030. if (!lua_interface)
  4031. return 0;
  4032. ZoneServer* zone = lua_interface->GetZone(state);
  4033. int32 location_id = lua_interface->GetInt32Value(state, 2);
  4034. if (!zone) {
  4035. lua_interface->LogError("LUA SpawnByLocationID command error: zone is not valid");
  4036. return 0;
  4037. }
  4038. if (location_id == 0) {
  4039. lua_interface->LogError("LUA SpawnByLocationID command error: location id can not be 0");
  4040. return 0;
  4041. }
  4042. SpawnLocation* location = zone->GetSpawnLocation(location_id);
  4043. if (!location) {
  4044. lua_interface->LogError("LUA SpawnByLocationID command error: no location found for the given ID (%u)", location_id);
  4045. return 0;
  4046. }
  4047. Spawn* spawn = 0;
  4048. if (location->entities[0]) {
  4049. if(location->entities[0]->spawn_type == SPAWN_ENTRY_TYPE_NPC)
  4050. spawn = zone->AddNPCSpawn(location, location->entities[0]);
  4051. else if(location->entities[0]->spawn_type == SPAWN_ENTRY_TYPE_GROUNDSPAWN)
  4052. spawn = zone->AddGroundSpawn(location, location->entities[0]);
  4053. else if(location->entities[0]->spawn_type == SPAWN_ENTRY_TYPE_OBJECT)
  4054. spawn = zone->AddObjectSpawn(location, location->entities[0]);
  4055. else if(location->entities[0]->spawn_type == SPAWN_ENTRY_TYPE_WIDGET)
  4056. spawn = zone->AddWidgetSpawn(location, location->entities[0]);
  4057. else if(location->entities[0]->spawn_type == SPAWN_ENTRY_TYPE_SIGN)
  4058. spawn = zone->AddSignSpawn(location, location->entities[0]);
  4059. if (spawn) {
  4060. const char* script = 0;
  4061. for (int x = 0; x < 3; x++) {
  4062. switch (x) {
  4063. case 0:
  4064. script = world.GetSpawnEntryScript(location->entities[0]->spawn_entry_id);
  4065. break;
  4066. case 1:
  4067. script = world.GetSpawnLocationScript(location->entities[0]->spawn_location_id);
  4068. break;
  4069. case 2:
  4070. script = world.GetSpawnScript(location->entities[0]->spawn_id);
  4071. break;
  4072. }
  4073. if (script && lua_interface->GetSpawnScript(script) != 0) {
  4074. spawn->SetSpawnScript(string(script));
  4075. break;
  4076. }
  4077. }
  4078. zone->CallSpawnScript(spawn, SPAWN_SCRIPT_SPAWN);
  4079. lua_interface->SetSpawnValue(state, spawn);
  4080. return 1;
  4081. }
  4082. else {
  4083. LogWrite(ZONE__ERROR, 0, "Zone", "Error adding spawn to zone");
  4084. safe_delete(spawn);
  4085. }
  4086. }
  4087. return 0;
  4088. }
  4089. int EQ2Emu_lua_CastEntityCommand(lua_State* state) {
  4090. if (!lua_interface)
  4091. return 0;
  4092. Spawn* caster = lua_interface->GetSpawn(state);
  4093. Spawn* target = lua_interface->GetSpawn(state, 2);
  4094. int32 id = lua_interface->GetInt32Value(state, 3);
  4095. string command = lua_interface->GetStringValue(state, 4);
  4096. if (!caster) {
  4097. lua_interface->LogError("LUA CastEntityCommand command error: caster is not valid");
  4098. return 0;
  4099. }
  4100. if (!target) {
  4101. lua_interface->LogError("LUA CastEntityCommand command error: target is not valid");
  4102. return 0;
  4103. }
  4104. if (!caster->IsPlayer()) {
  4105. lua_interface->LogError("LUA CastEntityCommand command error: caster is not a player");
  4106. return 0;
  4107. }
  4108. EntityCommand* entity_command = caster->GetZone()->GetEntityCommand(id, command);
  4109. if (!entity_command) {
  4110. lua_interface->LogError("LUA CastEntityCommand command error: unable to get a valid EntityCommand with the given ID (%u) and name (%s)", id, command.c_str());
  4111. return 0;
  4112. }
  4113. Client* client = caster->GetZone()->GetClientBySpawn(caster);
  4114. if (!client) {
  4115. lua_interface->LogError("LUA CastEntityCommand command error: unable to get a valid client for the given caster");
  4116. return 0;
  4117. }
  4118. client->GetCurrentZone()->ProcessEntityCommand(entity_command, (Player*)caster, target);
  4119. return 0;
  4120. }
  4121. int EQ2Emu_lua_SetLuaBrain(lua_State* state) {
  4122. if (!lua_interface)
  4123. return 0;
  4124. Spawn* spawn = lua_interface->GetSpawn(state);
  4125. if (!spawn) {
  4126. lua_interface->LogError("LUA SetLuaBrain command error: spawn is not valid");
  4127. return 0;
  4128. }
  4129. if (!spawn->IsNPC()) {
  4130. lua_interface->LogError("LUA SetLuaBrain command error: spawn is not a npc");
  4131. return 0;
  4132. }
  4133. ((NPC*)spawn)->SetBrain(new LuaBrain((NPC*)spawn));
  4134. return 0;
  4135. }
  4136. int EQ2Emu_lua_SetBrainTick(lua_State* state) {
  4137. if (!lua_interface)
  4138. return 0;
  4139. Spawn* spawn = lua_interface->GetSpawn(state);
  4140. int16 tick = lua_interface->GetInt16Value(state, 2);
  4141. if (!spawn) {
  4142. lua_interface->LogError("LUA SetBrainTick command error: spawn is not valid");
  4143. return 0;
  4144. }
  4145. if (!spawn->IsNPC()) {
  4146. lua_interface->LogError("LUA SetBrainTick command error: spawn is not a valid npc");
  4147. return 0;
  4148. }
  4149. if (tick < 20) {
  4150. lua_interface->LogError("LUA SetBrainTick command error: tick can not be set below 20 milliseconds");
  4151. return 0;
  4152. }
  4153. ((NPC*)spawn)->Brain()->SetTick(tick);
  4154. return 0;
  4155. }
  4156. int EQ2Emu_lua_SetFollowTarget(lua_State* state) {
  4157. if (!lua_interface)
  4158. return 0;
  4159. Spawn* spawn = lua_interface->GetSpawn(state);
  4160. Spawn* target = lua_interface->GetSpawn(state, 2);
  4161. if (!spawn) {
  4162. lua_interface->LogError("LUA SetFollowTarget command error: spawn is not valid");
  4163. return 0;
  4164. }
  4165. // Target can be null, setting follow target to 0 clears it and will cancel follow, so no need to check it
  4166. spawn->SetFollowTarget(target);
  4167. return 0;
  4168. }
  4169. int EQ2Emu_lua_GetFollowTarget(lua_State* state) {
  4170. if (!lua_interface)
  4171. return 0;
  4172. Spawn* spawn = lua_interface->GetSpawn(state);
  4173. if (!spawn) {
  4174. lua_interface->LogError("LUA GetFollowTarget command error: spawn is not valid");
  4175. return 0;
  4176. }
  4177. Spawn* target = spawn->GetFollowTarget();
  4178. if (target) {
  4179. lua_interface->SetSpawnValue(state, target);
  4180. return 1;
  4181. }
  4182. return 0;
  4183. }
  4184. int EQ2Emu_lua_ToggleFollow(lua_State* state) {
  4185. if (!lua_interface)
  4186. return 0;
  4187. Spawn* spawn = lua_interface->GetSpawn(state);
  4188. if (!spawn) {
  4189. lua_interface->LogError("LUA ToggleFollow command error: spawn is not valid");
  4190. return 0;
  4191. }
  4192. if (spawn->following)
  4193. spawn->following = false;
  4194. else
  4195. spawn->following = true;
  4196. return 0;
  4197. }
  4198. int EQ2Emu_lua_IsFollowing(lua_State* state) {
  4199. if (!lua_interface)
  4200. return 0;
  4201. Spawn* spawn = lua_interface->GetSpawn(state);
  4202. if (!spawn) {
  4203. lua_interface->LogError("LUA IsFollowing command error: spawn is not valid");
  4204. return 0;
  4205. }
  4206. lua_interface->SetBooleanValue(state, spawn->following);
  4207. return 1;
  4208. }
  4209. int EQ2Emu_lua_SetTempVariable(lua_State* state) {
  4210. // As this is unique among the rest of our lua functions as the 3rd param can be of multiple types
  4211. // I will attempt to explain how this function works for future refrence
  4212. // Fist lets make sure lua_interface is valid, if not return out
  4213. if (!lua_interface)
  4214. return 0;
  4215. // Next we grab the first 2 params same as we usually would
  4216. Spawn* spawn = lua_interface->GetSpawn(state);
  4217. string var = lua_interface->GetStringValue(state, 2);
  4218. // DataType will let us know the value type so we can handle it correctly, we set these ourself so the values I used are made up
  4219. // 1 = Spawn
  4220. // 2 = Zone
  4221. // 3 = Item
  4222. // 4 = Quest
  4223. // 5 = String
  4224. // 6 = nil (null)
  4225. int8 dataType = 0;
  4226. // Define pointers for each potential type
  4227. Spawn* spawnVal = 0;
  4228. ZoneServer* zone = 0;
  4229. Item* item = 0;
  4230. Quest* quest = 0;
  4231. string val;
  4232. // Finally we get to grabbing the third param, we will first check to see if it is light user data
  4233. // which is custom data types, in this case it can be Spawn, Zone, Item, or Quest. Conversation and
  4234. // options window are also light user data be we do not handle those.
  4235. // We check with lua_islightuserdata(lua_State*, index)
  4236. if (lua_islightuserdata(state, 3)) {
  4237. // It is light user data so we will grab the param with lua_touserdata(lua_State*, index)
  4238. // and convert it to LUAUserData*
  4239. LUAUserData* data = (LUAUserData*)lua_touserdata(state, 3);
  4240. // Check to make sure the data we got is valid, if not give an error
  4241. if(!data || !data->IsCorrectlyInitialized()){
  4242. lua_interface->LogError("LUA SetTempVariable command error while processing %s", lua_tostring(state, -1));
  4243. }
  4244. // Check if data is a Spawn, if so set our Spawn pointer and the dataType variable
  4245. else if(data->IsSpawn()) {
  4246. spawnVal = data->spawn;
  4247. dataType = 1;
  4248. }
  4249. // Check if data is a Zone, if so set our Zone pointer and the dataType variable
  4250. else if (data->IsZone()) {
  4251. zone = data->zone;
  4252. dataType = 2;
  4253. }
  4254. // Check if data is a Item, if so set our Item pointer and the dataType variable
  4255. else if (data->IsItem()) {
  4256. item = data->item;
  4257. dataType = 3;
  4258. }
  4259. // Check if data is a Ques, if so set our Quest pointer and the dataType variable
  4260. else if (data->IsQuest()) {
  4261. quest = data->quest;
  4262. dataType = 4;
  4263. }
  4264. }
  4265. // Wasn't light user data, check if it is nil(null)
  4266. else if (lua_isnil(state, 3)) {
  4267. // It is nil (null) set the dataType variable, no need to set a pointer in this case
  4268. dataType = 6;
  4269. }
  4270. // Wasn't light user data or nil (null), must be a string
  4271. else {
  4272. // Set the string and dataType variable
  4273. val = lua_interface->GetStringValue(state, 3);
  4274. dataType = 5;
  4275. }
  4276. // We now have all the params, lets check to make sure they are valid
  4277. if (!spawn) {
  4278. lua_interface->LogError("LUA SetTempVariable command error: spawn is not valid");
  4279. return 0;
  4280. }
  4281. if (var.length() == 0) {
  4282. lua_interface->LogError("LUA SetTempVariable command error: var must be set");
  4283. return 0;
  4284. }
  4285. if (dataType == 0) {
  4286. lua_interface->LogError("LUA SetTempVariable command error: unknown data type");
  4287. return 0;
  4288. }
  4289. // All params are valid, lets set the spawns temp variable, this is where dataType variable comes in.
  4290. // AddTempVariable has overloads for all the types of data we support, we need to make sure the third
  4291. // param gets sent to the correct list so we check the value of dataType to know where it should go.
  4292. switch (dataType) {
  4293. case 1:
  4294. // 1 = Spawn
  4295. spawn->AddTempVariable(var, spawnVal);
  4296. break;
  4297. case 2:
  4298. // 2 = Zone
  4299. spawn->AddTempVariable(var, zone);
  4300. break;
  4301. case 3:
  4302. // 3 = Item
  4303. spawn->AddTempVariable(var, item);
  4304. break;
  4305. case 4:
  4306. // 4 = Quest
  4307. spawn->AddTempVariable(var, quest);
  4308. break;
  4309. case 5:
  4310. // 5 = String
  4311. spawn->AddTempVariable(var, val);
  4312. break;
  4313. case 6:
  4314. // 6 = nil (null) so the variable is no longer set, lets remove it from the spawn
  4315. spawn->DeleteTempVariable(var);
  4316. break;
  4317. }
  4318. // And we are done so return out
  4319. return 0;
  4320. }
  4321. int EQ2Emu_lua_GetTempVariable(lua_State* state) {
  4322. if (!lua_interface)
  4323. return 0;
  4324. Spawn* spawn = lua_interface->GetSpawn(state);
  4325. string var = lua_interface->GetStringValue(state, 2);
  4326. if (!spawn) {
  4327. lua_interface->LogError("LUA GetTempVariable command error: spawn is not valid");
  4328. return 0;
  4329. }
  4330. if (var.length() == 0) {
  4331. lua_interface->LogError("LUA GetTempVariable command error: var must be set");
  4332. return 0;
  4333. }
  4334. // This will tell us the type of data this variable contains, uses the same values as the previous function
  4335. int8 type = spawn->GetTempVariableType(var);
  4336. Spawn* spawn2 = 0;
  4337. ZoneServer* zone = 0;
  4338. Item* item = 0;
  4339. Quest* quest = 0;
  4340. // Set the lua function return value based on the type of data the variable contains
  4341. switch(type) {
  4342. case 1:
  4343. spawn2 = spawn->GetTempVariableSpawn(var);
  4344. if (!spawn2)
  4345. return 0;
  4346. lua_interface->SetSpawnValue(state, spawn2);
  4347. break;
  4348. case 2:
  4349. zone = spawn->GetTempVariableZone(var);
  4350. if (!zone)
  4351. return 0;
  4352. lua_interface->SetZoneValue(state, zone);
  4353. break;
  4354. case 3:
  4355. item = spawn->GetTempVariableItem(var);
  4356. if (!item)
  4357. return 0;
  4358. lua_interface->SetItemValue(state, item);
  4359. break;
  4360. case 4:
  4361. quest = spawn->GetTempVariableQuest(var);
  4362. if (!quest)
  4363. return 0;
  4364. lua_interface->SetQuestValue(state, quest);
  4365. break;
  4366. case 5:
  4367. lua_interface->SetStringValue(state, spawn->GetTempVariable(var).c_str());
  4368. break;
  4369. default:
  4370. // Not a valid type then the variable was not set so return out
  4371. return 0;
  4372. }
  4373. // Return value was set so return out
  4374. return 1;
  4375. }
  4376. int EQ2Emu_lua_GiveQuestItem(lua_State* state)
  4377. {
  4378. if (!lua_interface)
  4379. return 0;
  4380. Quest* quest = lua_interface->GetQuest(state);
  4381. Spawn* spawn = lua_interface->GetSpawn(state, 2);
  4382. string description = lua_interface->GetStringValue(state, 3);
  4383. int32 item_id = lua_interface->GetInt32Value(state, 4);
  4384. if (!quest) {
  4385. lua_interface->LogError("LUA GiveQuestItem command error: quest is not valid");
  4386. lua_interface->SetBooleanValue(state, false);
  4387. return 1;
  4388. }
  4389. if (!spawn) {
  4390. lua_interface->LogError("LUA GiveQuestItem command error: spawn is not valid");
  4391. lua_interface->SetBooleanValue(state, false);
  4392. return 1;
  4393. }
  4394. if (!spawn->IsPlayer()) {
  4395. lua_interface->LogError("LUA GiveQuestItem command error: spawn must be a player");
  4396. lua_interface->SetBooleanValue(state, false);
  4397. return 1;
  4398. }
  4399. if (item_id == 0) {
  4400. lua_interface->LogError("LUA GiveQuestItem command error: item_id is not valid");
  4401. lua_interface->SetBooleanValue(state, false);
  4402. return 1;
  4403. }
  4404. Client* client = spawn->GetZone()->GetClientBySpawn(spawn);
  4405. if (!client) {
  4406. lua_interface->LogError("LUA GiveQuestItem command error: unable to get a valid client from the given spawn");
  4407. lua_interface->SetBooleanValue(state, false);
  4408. return 1;
  4409. }
  4410. Item* item = master_item_list.GetItem(item_id);
  4411. if (!item) {
  4412. lua_interface->LogError("LUA GiveQuestItem command error: unable to get an item from the given id (%u)", item_id);
  4413. lua_interface->SetBooleanValue(state, false);
  4414. return 1;
  4415. }
  4416. PacketStruct* packet = configReader.getStruct("WS_QuestComplete", client->GetVersion());
  4417. if (packet) {
  4418. packet->setDataByName("title", "Quest Reward!");
  4419. packet->setDataByName("name", quest->GetName());
  4420. packet->setDataByName("description", description.c_str());
  4421. packet->setDataByName("level", quest->GetLevel());
  4422. packet->setArrayLengthByName("num_rewards", 1);
  4423. packet->setArrayDataByName("reward_id", item->details.item_id);
  4424. if (client->GetVersion() < 860)
  4425. packet->setItemArrayDataByName("item", item, (Player*)spawn, 0, 0, -1);
  4426. else if (client->GetVersion() < 1193)
  4427. packet->setItemArrayDataByName("item", item, (Player*)spawn);
  4428. else
  4429. packet->setItemArrayDataByName("item", item, (Player*)spawn, 0, 0, 2);
  4430. client->QueuePacket(packet->serialize());
  4431. safe_delete(packet);
  4432. lua_interface->SetBooleanValue(state, client->AddItem(item_id, 1));
  4433. client->Message(CHANNEL_COLOR_YELLOW, "You receive \\aITEM %u 0:%s\\/a.", item->details.item_id, item->name.c_str());
  4434. return 1;
  4435. }
  4436. lua_interface->SetBooleanValue(state, false);
  4437. return 1;
  4438. }
  4439. int EQ2Emu_lua_SetQuestRepeatable(lua_State* state) {
  4440. if (!lua_interface)
  4441. return 0;
  4442. Quest* quest = lua_interface->GetQuest(state);
  4443. if (!quest) {
  4444. lua_interface->LogError("LUA SetQuestRepeatable command error: quest is not valid");
  4445. return 0;
  4446. }
  4447. quest->SetRepeatable(true);
  4448. return 0;
  4449. }
  4450. int EQ2Emu_lua_GetArchetypeName(lua_State* state) {
  4451. if (!lua_interface)
  4452. return 0;
  4453. Spawn* spawn = lua_interface->GetSpawn(state);
  4454. if (!spawn) {
  4455. lua_interface->LogError("LUA GetArchetypeName command error: spawn is not valid");
  4456. return 0;
  4457. }
  4458. int8 base_class = classes.GetBaseClass(spawn->GetAdventureClass());
  4459. string ret = classes.GetClassNameCase(base_class);
  4460. if(ret.length() > 0){
  4461. lua_interface->SetStringValue(state, ret.c_str());
  4462. return 1;
  4463. }
  4464. return 0;
  4465. }
  4466. int EQ2Emu_lua_AddWard(lua_State* state) {
  4467. if (!lua_interface)
  4468. return 0;
  4469. int32 damage = lua_interface->GetInt32Value(state);
  4470. bool keepWard = (lua_interface->GetInt8Value(state, 2) == 1);
  4471. int8 wardType = lua_interface->GetInt8Value(state, 3);
  4472. int8 damageTypes = lua_interface->GetInt8Value(state, 4);
  4473. LuaSpell* spell = lua_interface->GetCurrentSpell(state);
  4474. bool ward_was_added = false;
  4475. ZoneServer* zone = spell->caster->GetZone();
  4476. spell->MSpellTargets.readlock(__FUNCTION__, __LINE__);
  4477. for (int32 i = 0; i < spell->targets.size(); i++) {
  4478. Spawn* target = zone->GetSpawnByID(spell->targets.at(i));
  4479. if (!target)
  4480. continue;
  4481. if (target->IsEntity()) {
  4482. // If the ward is already active remove it
  4483. if (((Entity*)target)->GetWard(spell->spell->GetSpellID()))
  4484. ((Entity*)target)->RemoveWard(spell->spell->GetSpellID());
  4485. // Create new ward info
  4486. WardInfo* ward = new WardInfo;
  4487. ward->Spell = spell;
  4488. ward->BaseDamage = damage;
  4489. ward->DamageLeft = damage;
  4490. ward->keepWard = keepWard;
  4491. ward->WardType = wardType;
  4492. if (wardType == WARD_TYPE_MAGICAL)
  4493. ward->DamageType = damageTypes;
  4494. // Add the ward to the entity
  4495. ((Entity*)target)->AddWard(spell->spell->GetSpellID(), ward);
  4496. ward_was_added = true;
  4497. }
  4498. }
  4499. spell->MSpellTargets.releasereadlock(__FUNCTION__, __LINE__);
  4500. if (ward_was_added && spell->caster->IsPlayer()){
  4501. spell->had_dmg_remaining = true;
  4502. ClientPacketFunctions::SendMaintainedExamineUpdate(spell->caster->GetZone()->GetClientBySpawn(spell->caster), spell->slot_pos, damage, 1);
  4503. }
  4504. return 0;
  4505. }
  4506. int EQ2Emu_lua_AddToWard(lua_State* state) {
  4507. if (!lua_interface)
  4508. return 0;
  4509. int32 amount = lua_interface->GetInt32Value(state);
  4510. LuaSpell* spell = lua_interface->GetCurrentSpell(state);
  4511. WardInfo* ward = 0;
  4512. ZoneServer* zone = spell->caster->GetZone();
  4513. spell->MSpellTargets.readlock(__FUNCTION__, __LINE__);
  4514. if (zone->GetSpawnByID(spell->targets.at(0))->IsEntity()) {
  4515. Entity* target = (Entity*)zone->GetSpawnByID(spell->targets.at(0));
  4516. ward = target->GetWard(spell->spell->GetSpellID());
  4517. if (ward) {
  4518. ward->DamageLeft += amount;
  4519. if (ward->DamageLeft > ward->BaseDamage)
  4520. ward->DamageLeft = ward->BaseDamage;
  4521. for (int32 i = 0; i < spell->targets.size(); i++) {
  4522. if (Spawn* spawn = zone->GetSpawnByID(spell->targets.at(i)))
  4523. zone->SendHealPacket(ward->Spell->caster, spawn, HEAL_PACKET_TYPE_REGEN_ABSORB, amount, ward->Spell->spell->GetName());
  4524. }
  4525. }
  4526. }
  4527. spell->MSpellTargets.releasereadlock(__FUNCTION__, __LINE__);
  4528. if (ward && spell->caster->IsPlayer())
  4529. ClientPacketFunctions::SendMaintainedExamineUpdate(spell->caster->GetZone()->GetClientBySpawn(spell->caster), spell->slot_pos, ward->DamageLeft, 1);
  4530. return 0;
  4531. }
  4532. int EQ2Emu_lua_GetWardAmountLeft(lua_State* state) {
  4533. if (!lua_interface)
  4534. return 0;
  4535. LuaSpell* spell = lua_interface->GetCurrentSpell(state);
  4536. if (!spell) {
  4537. lua_interface->LogError("LUA GetWardAmountLeft command error: this command can only be used in a spell script");
  4538. return 0;
  4539. }
  4540. if (spell->caster->GetZone()->GetSpawnByID(spell->targets.at(0))->IsEntity()) {
  4541. Entity* target = (Entity*)spell->caster->GetZone()->GetSpawnByID(spell->targets.at(0));
  4542. WardInfo* ward = target->GetWard(spell->spell->GetSpellID());
  4543. if (ward) {
  4544. lua_interface->SetInt32Value(state, ward->DamageLeft);
  4545. return 1;
  4546. }
  4547. }
  4548. return 0;
  4549. }
  4550. int EQ2Emu_lua_RemoveWard(lua_State* state) {
  4551. if (!lua_interface)
  4552. return 0;
  4553. LuaSpell* spell = lua_interface->GetCurrentSpell(state);
  4554. ZoneServer* zone = spell->caster->GetZone();
  4555. Spawn * target = 0;
  4556. spell->MSpellTargets.readlock(__FUNCTION__, __LINE__);
  4557. for (int32 i = 0; i < spell->targets.size(); i++) {
  4558. target = zone->GetSpawnByID(spell->targets.at(i));
  4559. if (target && target->IsEntity()) {
  4560. ((Entity*)target)->RemoveWard(spell->spell->GetSpellID());
  4561. }
  4562. }
  4563. spell->MSpellTargets.releasereadlock(__FUNCTION__, __LINE__);
  4564. return 0;
  4565. }
  4566. int EQ2Emu_lua_Interrupt(lua_State* state)
  4567. {
  4568. if (!lua_interface)
  4569. return 0;
  4570. Spawn* caster = lua_interface->GetSpawn(state); // Second param in lua_interface->get functions defaults to 1
  4571. Spawn* target = lua_interface->GetSpawn(state, 2);
  4572. LuaSpell* spell = lua_interface->GetCurrentSpell(state);
  4573. if (!caster)
  4574. {
  4575. lua_interface->LogError("LUA Interrupt command error: caster is not a valid spawn");
  4576. return 0;
  4577. }
  4578. if(!target)
  4579. {
  4580. lua_interface->LogError("LUA Interrupt command error: target is not a valid spawn");
  4581. return 0;
  4582. }
  4583. if (!spell) {
  4584. lua_interface->LogError("LUA Interrupt command error: spell is not a valid spawn");
  4585. return 0;
  4586. }
  4587. if (!target->IsEntity() && !spell)
  4588. {
  4589. lua_interface->LogError("LUA Interrupt command error: Target is not an entity");
  4590. return 0;
  4591. }
  4592. if (!target && spell){
  4593. spell->MSpellTargets.readlock(__FUNCTION__, __LINE__);
  4594. for (int8 i = 0; i < spell->targets.size(); i++){
  4595. target = caster->GetZone()->GetSpawnByID(spell->targets.at(i));
  4596. if (!target || !target->IsEntity())
  4597. continue;
  4598. ((Entity*)target)->GetZone()->GetSpellProcess()->Interrupted((Entity*)target, caster, SPELL_ERROR_INTERRUPTED);
  4599. }
  4600. spell->MSpellTargets.releasereadlock(__FUNCTION__, __LINE__);
  4601. }
  4602. else
  4603. caster->GetZone()->GetSpellProcess()->Interrupted((Entity*)target, caster, SPELL_ERROR_INTERRUPTED);
  4604. return 0;
  4605. }
  4606. int EQ2Emu_lua_Stealth(lua_State* state){
  4607. if(!lua_interface)
  4608. return 0;
  4609. int8 type = lua_interface->GetInt8Value(state);
  4610. Spawn* spawn = lua_interface->GetSpawn(state, 2);
  4611. LuaSpell* spell = lua_interface->GetCurrentSpell(state);
  4612. if (!spell){
  4613. lua_interface->LogError("LUA Stealth command error: must be used from spell script");
  4614. return 0;
  4615. }
  4616. ZoneServer* zone = spell->caster->GetZone();
  4617. if (spawn){
  4618. if (spawn->IsEntity()){
  4619. if (type == 1){
  4620. ((Entity*)spawn)->AddStealthSpell(spell);
  4621. if (!(spell->effect_bitmask & EFFECT_FLAG_STEALTH))
  4622. spell->effect_bitmask += EFFECT_FLAG_STEALTH;
  4623. }
  4624. else if (type == 2){
  4625. ((Entity*)spawn)->AddInvisSpell(spell);
  4626. if (!(spell->effect_bitmask & EFFECT_FLAG_INVIS))
  4627. spell->effect_bitmask += EFFECT_FLAG_INVIS;
  4628. }
  4629. return 0;
  4630. }
  4631. else{
  4632. lua_interface->LogError("LUA Stealth command error: target override is not Entity");
  4633. return 0;
  4634. }
  4635. }
  4636. else{
  4637. spell->MSpellTargets.readlock(__FUNCTION__, __LINE__);
  4638. for (int32 i = 0; i < spell->targets.size(); i++){
  4639. spawn = zone->GetSpawnByID(spell->targets.at(i));
  4640. if (!spawn || !spawn->IsEntity())
  4641. continue;
  4642. if (type == 1){
  4643. ((Entity*)spawn)->AddStealthSpell(spell);
  4644. if (!(spell->effect_bitmask & EFFECT_FLAG_STEALTH))
  4645. spell->effect_bitmask += EFFECT_FLAG_STEALTH;
  4646. }
  4647. else if (type == 2){
  4648. ((Entity*)spawn)->AddInvisSpell(spell);
  4649. if (!(spell->effect_bitmask & EFFECT_FLAG_INVIS))
  4650. spell->effect_bitmask += EFFECT_FLAG_INVIS;
  4651. }
  4652. else{
  4653. lua_interface->LogError("LUA Stealth command error: invalid stealth type given");
  4654. break;
  4655. }
  4656. }
  4657. spell->MSpellTargets.releasereadlock(__FUNCTION__, __LINE__);
  4658. }
  4659. return 0;
  4660. }
  4661. int EQ2Emu_lua_IsStealthed(lua_State* state){
  4662. if(!lua_interface)
  4663. return 0;
  4664. Spawn* spawn = lua_interface->GetSpawn(state);
  4665. if (!spawn){
  4666. lua_interface->LogError("LUA IsStealthed command error: spawn is not valid");
  4667. return 0;}
  4668. if(spawn->IsEntity()){
  4669. lua_interface->SetBooleanValue(state, ((Entity*)spawn)->IsStealthed());
  4670. return 1;
  4671. }
  4672. else
  4673. lua_interface->LogError("LUA IsStealthed command error: spawn is not entity");
  4674. return 0;
  4675. }
  4676. int EQ2Emu_lua_IsInvis(lua_State* state){
  4677. if(!lua_interface)
  4678. return 0;
  4679. Spawn* spawn = lua_interface->GetSpawn(state);
  4680. if (!spawn){
  4681. lua_interface->LogError("LUA IsInvis command error: spawn is not valid");
  4682. return 0;}
  4683. if(spawn->IsEntity()){
  4684. lua_interface->SetBooleanValue(state, ((Entity*)spawn)->IsInvis());
  4685. return 1;
  4686. }
  4687. else
  4688. lua_interface->LogError("LUA IsInvis command error: spawn is not entity");
  4689. return 0;
  4690. }
  4691. int EQ2Emu_lua_HasItemEquipped(lua_State* state){
  4692. if(!lua_interface)
  4693. return 0;
  4694. Spawn* player = lua_interface->GetSpawn(state);
  4695. int32 item_id = lua_interface->GetInt32Value(state, 2);
  4696. if (!player->IsPlayer()){
  4697. lua_interface->LogError("LUA HasItemEquipped command error: spawn is not player");
  4698. return 0;
  4699. }
  4700. lua_interface->SetBooleanValue(state, ((Player*)player)->GetEquipmentList()->HasItem(item_id));
  4701. return 1;
  4702. }
  4703. int EQ2Emu_lua_GetEquippedItemBySlot(lua_State* state){
  4704. if(!lua_interface)
  4705. return 0;
  4706. Spawn* player = lua_interface->GetSpawn(state);
  4707. int8 slot = lua_interface->GetInt8Value(state, 2);
  4708. if(!player){
  4709. lua_interface->LogError("LUA GetEquippedItemBySlot command error: spawn is not valid");
  4710. return 0;
  4711. }
  4712. if(!player->IsPlayer()){
  4713. lua_interface->LogError("LUA GetEquippedItemBySlot command error: spawn is not player");
  4714. return 0;
  4715. }
  4716. Item* item = ((Player*)player)->GetEquipmentList()->GetItem(slot);
  4717. if(!item){
  4718. lua_interface->LogError("LUA GetEquippedItemBySlot command error: item was not found in slot");
  4719. return 0;
  4720. }
  4721. lua_interface->SetItemValue(state, item);
  4722. return 1;
  4723. }
  4724. int EQ2Emu_lua_GetEquippedItemByID(lua_State* state){
  4725. if(!lua_interface)
  4726. return 0;
  4727. Spawn* player = lua_interface->GetSpawn(state);
  4728. int32 id = lua_interface->GetInt32Value(state, 2);
  4729. if(!player){
  4730. lua_interface->LogError("LUA GetEquippedItemByID command error: spawn is not valid");
  4731. return 0;
  4732. }
  4733. if(!player->IsPlayer()){
  4734. lua_interface->LogError("LUA GetEquippedItemByID command error: spawn is not player");
  4735. return 0;
  4736. }
  4737. Item* item = ((Player*)player)->GetEquipmentList()->GetItemFromItemID(id);
  4738. if(!item){
  4739. lua_interface->LogError("LUA GetEquippedItemByID command error: equipped item with used id not found");
  4740. return 0;
  4741. }
  4742. lua_interface->SetItemValue(state, item);
  4743. return 1;
  4744. }
  4745. int EQ2Emu_lua_GetItemByID(lua_State* state){
  4746. if(!lua_interface)
  4747. return 0;
  4748. Spawn* player = lua_interface->GetSpawn(state);
  4749. int32 id = lua_interface->GetInt32Value(state, 2);
  4750. int8 count = lua_interface->GetInt8Value(state, 3);
  4751. bool include_bank = lua_interface->GetInt8Value(state, 4);
  4752. if(!player){
  4753. lua_interface->LogError("LUA GetItemByID command error: spawn is not valid");
  4754. return 0;
  4755. }
  4756. if(!player->IsPlayer()){
  4757. lua_interface->LogError("LUA GetItemByID command error: spawn is not player");
  4758. return 0;
  4759. }
  4760. if(!count)
  4761. count = 1;
  4762. Item* item = ((Player*)player)->GetPlayerItemList()->GetItemFromID(id, count, include_bank);
  4763. if(!item){
  4764. lua_interface->LogError("LUA GetItemByID command error: item with used id not found");
  4765. return 0;
  4766. }
  4767. lua_interface->SetItemValue(state, item);
  4768. return 1;
  4769. }
  4770. int EQ2Emu_lua_PlayAnimation(lua_State* state){
  4771. if(!lua_interface)
  4772. return 0;
  4773. Spawn* spawn = lua_interface->GetSpawn(state);
  4774. int32 anim = lua_interface->GetInt32Value(state, 2);
  4775. Spawn* spawn2 = lua_interface->GetSpawn(state, 3);
  4776. int8 type = lua_interface->GetInt8Value(state, 4);
  4777. if(!spawn){
  4778. lua_interface->LogError("LUA PlayAnimation command error: spawn is not valid");
  4779. return 0;
  4780. }
  4781. if(spawn2){
  4782. if (spawn2->IsPlayer()){
  4783. if(type != 1 && type != 2)
  4784. spawn->GetZone()->PlayAnimation(spawn, anim, spawn2);
  4785. else
  4786. spawn->GetZone()->PlayAnimation(spawn, anim, spawn2, type);
  4787. return 0;
  4788. }
  4789. else{
  4790. lua_interface->LogError("LUA PlayAnimation command error: second spawn not a player");
  4791. return 0;
  4792. }
  4793. }
  4794. else
  4795. spawn->GetZone()->PlayAnimation(spawn, anim);
  4796. return 0;
  4797. }
  4798. int EQ2Emu_lua_IsPet(lua_State* state){
  4799. if(!lua_interface)
  4800. return 0;
  4801. Spawn* spawn = lua_interface->GetSpawn(state);
  4802. if(!spawn){
  4803. lua_interface->LogError("LUA IsPet command error: spawn is not valid");
  4804. return 0;
  4805. }
  4806. lua_interface->SetBooleanValue(state, spawn->IsPet());
  4807. return 1;
  4808. }
  4809. int EQ2Emu_lua_GetOwner(lua_State* state){
  4810. if(!lua_interface)
  4811. return 0;
  4812. Spawn* spawn = lua_interface->GetSpawn(state);
  4813. if(!spawn){
  4814. lua_interface->LogError("LUA GetOwner command error: spawn is not valid");
  4815. return 0;
  4816. }
  4817. if(!spawn->IsNPC()){
  4818. lua_interface->LogError("LUA GetOwner command error: spawn is not a NPC");
  4819. return 0;
  4820. }
  4821. if(((NPC*)spawn)->GetOwner()){
  4822. lua_interface->SetSpawnValue(state, ((NPC*)spawn)->GetOwner());
  4823. return 1;
  4824. }
  4825. return 0;
  4826. }
  4827. int EQ2Emu_lua_SetTarget(lua_State* state){
  4828. if(!lua_interface)
  4829. return 0;
  4830. Spawn* spawn = lua_interface->GetSpawn(state);
  4831. Spawn* target = lua_interface->GetSpawn(state, 2);
  4832. if(!spawn){
  4833. lua_interface->LogError("LUA SetTarget command error: spawn is not valid");
  4834. return 0;
  4835. }
  4836. if(!spawn){
  4837. lua_interface->LogError("LUA SetTarget command error: target is not valid");
  4838. return 0;
  4839. }
  4840. spawn->SetTarget(target);
  4841. return 0;
  4842. }
  4843. int EQ2Emu_lua_SetInCombat(lua_State* state){
  4844. if(!lua_interface)
  4845. return 0;
  4846. Spawn* spawn = lua_interface->GetSpawn(state);
  4847. bool val = lua_interface->GetBooleanValue(state, 2);
  4848. if(!spawn){
  4849. lua_interface->LogError("LUA SetInCombat command error: spawn is not valid");
  4850. return 0;
  4851. }
  4852. if(!spawn->IsEntity()){
  4853. lua_interface->LogError("LUA SetInCombat command error: spawn is not an entity");
  4854. return 0;
  4855. }
  4856. ((Entity*)spawn)->InCombat(val);
  4857. if (val) {
  4858. spawn->ClearRunningLocations();
  4859. spawn->CalculateRunningLocation(true);
  4860. }
  4861. return 0;
  4862. }
  4863. int EQ2Emu_lua_CompareSpawns(lua_State* state){
  4864. if(!lua_interface)
  4865. return 0;
  4866. Spawn* spawn1 = lua_interface->GetSpawn(state);
  4867. Spawn* spawn2 = lua_interface->GetSpawn(state, 2);
  4868. if(!spawn1){
  4869. lua_interface->LogError("LUA CompareSpawns command error: first spawn is not valid");
  4870. return 0;
  4871. }
  4872. if(!spawn2){
  4873. lua_interface->LogError("LUA CompareSpawns command error: second spawn is not valid");
  4874. return 0;
  4875. }
  4876. lua_interface->SetBooleanValue(state, (spawn1 == spawn2));
  4877. return 1;
  4878. }
  4879. int EQ2Emu_lua_Runback(lua_State* state){
  4880. if(!lua_interface)
  4881. return 0;
  4882. Spawn* spawn = lua_interface->GetSpawn(state);
  4883. if(!spawn){
  4884. lua_interface->LogError("LUA Runback command error: spawn is not valid");
  4885. return 0;
  4886. }
  4887. if(!spawn->IsNPC()){
  4888. lua_interface->LogError("LUA Runback command error: spawn is not an NPC");
  4889. return 0;
  4890. }
  4891. ((NPC*)spawn)->Runback();
  4892. return 0;
  4893. }
  4894. int EQ2Emu_lua_GetRunbackDistance(lua_State* state){
  4895. if(!lua_interface)
  4896. return 0;
  4897. Spawn* spawn = lua_interface->GetSpawn(state);
  4898. if(!spawn){
  4899. lua_interface->LogError("LUA GetRunbackDistance command error: spawn is not valid");
  4900. return 0;
  4901. }
  4902. if(!spawn->IsNPC()){
  4903. lua_interface->LogError("LUA GetRunbackDistance command error: spawn is not an NPC");
  4904. return 0;
  4905. }
  4906. lua_interface->SetFloatValue(state, ((NPC*)spawn)->GetRunbackDistance());
  4907. return 1;
  4908. }
  4909. int EQ2Emu_lua_IsCasting(lua_State* state){
  4910. if(!lua_interface)
  4911. return 0;
  4912. Spawn* spawn = lua_interface->GetSpawn(state);
  4913. if(!spawn){
  4914. lua_interface->LogError("LUA IsCasting command error: spawn is not valid");
  4915. return 0;
  4916. }
  4917. if(!spawn->IsEntity()){
  4918. lua_interface->LogError("LUA IsCasting command error: spawn is not an entity");
  4919. return 0;
  4920. }
  4921. lua_interface->SetBooleanValue(state, ((Entity*)spawn)->IsCasting());
  4922. return 1;
  4923. }
  4924. int EQ2Emu_lua_IsMezzed(lua_State* state){
  4925. if(!lua_interface)
  4926. return 0;
  4927. Spawn* spawn = lua_interface->GetSpawn(state);
  4928. if(!spawn){
  4929. lua_interface->LogError("LUA IsMezzed command error: spawn is not valid");
  4930. return 0;
  4931. }
  4932. if(!spawn->IsEntity()){
  4933. lua_interface->LogError("LUA IsMezzed command error: spawn is not an entity");
  4934. return 0;
  4935. }
  4936. lua_interface->SetBooleanValue(state, ((Entity*)spawn)->IsMezzed());
  4937. return 1;
  4938. }
  4939. int EQ2Emu_lua_IsStunned(lua_State* state){
  4940. if(!lua_interface)
  4941. return 0;
  4942. Spawn* spawn = lua_interface->GetSpawn(state);
  4943. if(!spawn){
  4944. lua_interface->LogError("LUA IsStunned command error: spawn is not valid");
  4945. return 0;
  4946. }
  4947. if(!spawn->IsEntity()){
  4948. lua_interface->LogError("LUA IsStunned command error: spawn is not an entity");
  4949. return 0;
  4950. }
  4951. lua_interface->SetBooleanValue(state, ((Entity*)spawn)->IsStunned());
  4952. return 1;
  4953. }
  4954. int EQ2Emu_lua_IsMezzedOrStunned(lua_State* state){
  4955. if(!lua_interface)
  4956. return 0;
  4957. Spawn* spawn = lua_interface->GetSpawn(state);
  4958. if(!spawn){
  4959. lua_interface->LogError("LUA IsMezzedOrStunned command error: spawn is not valid");
  4960. return 0;
  4961. }
  4962. if(!spawn->IsEntity()){
  4963. lua_interface->LogError("LUA IsMezzedOrStunned command error: spawn is not an entity");
  4964. return 0;
  4965. }
  4966. lua_interface->SetBooleanValue(state, ((Entity*)spawn)->IsMezzedOrStunned());
  4967. return 1;
  4968. }
  4969. int EQ2Emu_lua_ProcessSpell(lua_State* state){
  4970. if(!lua_interface)
  4971. return 0;
  4972. Spawn* spawn = lua_interface->GetSpawn(state);
  4973. Spawn* target = lua_interface->GetSpawn(state, 2);
  4974. float distance = lua_interface->GetFloatValue(state, 3);
  4975. if(!spawn){
  4976. lua_interface->LogError("LUA ProcessSpell command error: spawn is not valid");
  4977. return 0;
  4978. }
  4979. if(!target){
  4980. lua_interface->LogError("LUA ProcessSpell command error: spawn is not an entity");
  4981. return 0;
  4982. }
  4983. if(!spawn->IsNPC()){
  4984. lua_interface->LogError("LUA ProcessSpell command error: spawn is not an NPC");
  4985. return 0;
  4986. }
  4987. if(!target->IsEntity()){
  4988. lua_interface->LogError("LUA ProcessSpell command error: target is not an entity");
  4989. return 0;
  4990. }
  4991. lua_interface->SetBooleanValue(state, ((NPC*)spawn)->Brain()->ProcessSpell(((Entity*)target), distance));
  4992. return 1;
  4993. }
  4994. int EQ2Emu_lua_ProcessMelee(lua_State* state){
  4995. if(!lua_interface)
  4996. return 0;
  4997. Spawn* spawn = lua_interface->GetSpawn(state);
  4998. Spawn* target = lua_interface->GetSpawn(state, 2);
  4999. float distance = lua_interface->GetFloatValue(state, 3);
  5000. if(!spawn){
  5001. lua_interface->LogError("LUA ProcessMelee command error: spawn is not valid");
  5002. return 0;
  5003. }
  5004. if(!target){
  5005. lua_interface->LogError("LUA ProcessMelee command error: target is not valid");
  5006. return 0;
  5007. }
  5008. if(!spawn->IsNPC()){
  5009. lua_interface->LogError("LUA ProcessMelee command error: spawn is not an NPC");
  5010. return 0;
  5011. }
  5012. if(!target->IsEntity()){
  5013. lua_interface->LogError("LUA ProcessMelee command error: target is not an entity");
  5014. return 0;
  5015. }
  5016. ((NPC*)spawn)->Brain()->ProcessMelee(((Entity*)target), distance);
  5017. return 0;
  5018. }
  5019. int EQ2Emu_lua_HasRecovered(lua_State* state){
  5020. if(!lua_interface)
  5021. return 0;
  5022. Spawn* spawn = lua_interface->GetSpawn(state);
  5023. if(!spawn){
  5024. lua_interface->LogError("LUA HasRecovered command error: spawn is not valid");
  5025. return 0;
  5026. }
  5027. if(!spawn->IsNPC()){
  5028. lua_interface->LogError("LUA HasRecovered command error: spawn is not an NPC");
  5029. return 0;
  5030. }
  5031. lua_interface->SetBooleanValue(state, ((NPC*)spawn)->Brain()->HasRecovered());
  5032. return 1;
  5033. }
  5034. int EQ2Emu_lua_GetEncounterSize(lua_State* state){
  5035. if(!lua_interface)
  5036. return 0;
  5037. Spawn* spawn = lua_interface->GetSpawn(state);
  5038. if(!spawn){
  5039. lua_interface->LogError("LUA GetEncounterSize command error: spawn is not valid");
  5040. return 0;
  5041. }
  5042. if(!spawn->IsNPC()){
  5043. lua_interface->LogError("LUA GetEncounterSize command error: spawn is not an NPC");
  5044. return 0;
  5045. }
  5046. lua_interface->SetInt32Value(state, ((NPC*)spawn)->Brain()->GetEncounterSize());
  5047. return 1;
  5048. }
  5049. int EQ2Emu_lua_GetMostHated(lua_State* state){
  5050. if(!lua_interface)
  5051. return 0;
  5052. Spawn* spawn = lua_interface->GetSpawn(state);
  5053. if(!spawn){
  5054. lua_interface->LogError("LUA GetMostHated command error: spawn is not valid");
  5055. return 0;
  5056. }
  5057. if(!spawn->IsNPC()){
  5058. lua_interface->LogError("LUA GetMostHated command error: spawn is not an NPC");
  5059. return 0;
  5060. }
  5061. Entity* hated = ((NPC*)spawn)->Brain()->GetMostHated();
  5062. if (hated) {
  5063. lua_interface->SetSpawnValue(state, hated);
  5064. return 1;
  5065. }
  5066. return 0;
  5067. }
  5068. int EQ2Emu_lua_ClearHate(lua_State* state){
  5069. if(!lua_interface)
  5070. return 0;
  5071. Spawn* spawn = lua_interface->GetSpawn(state);
  5072. Spawn* hated = lua_interface->GetSpawn(state, 2);
  5073. if(!spawn){
  5074. lua_interface->LogError("LUA ClearHate command error: spawn is not valid");
  5075. return 0;
  5076. }
  5077. if(!spawn->IsNPC()){
  5078. lua_interface->LogError("LUA ClearHate command error: spawn is not NPC");
  5079. return 0;
  5080. }
  5081. if(!hated){
  5082. ((NPC*)spawn)->Brain()->ClearHate();
  5083. return 0;
  5084. }
  5085. else
  5086. {
  5087. if(!hated->IsEntity()){
  5088. lua_interface->LogError("LUA ClearHate command error: second param is not entity");
  5089. return 0;
  5090. }
  5091. ((NPC*)spawn)->Brain()->ClearHate(((Entity*)hated));
  5092. return 0;
  5093. }
  5094. return 0;
  5095. }
  5096. int EQ2Emu_lua_ClearEncounter(lua_State* state){
  5097. if(!lua_interface)
  5098. return 0;
  5099. Spawn* spawn = lua_interface->GetSpawn(state);
  5100. if(!spawn){
  5101. lua_interface->LogError("LUA ClearEncounter command error: spawn is not valid");
  5102. return 0;
  5103. }
  5104. if(!spawn->IsNPC()){
  5105. lua_interface->LogError("LUA ClearEncounter command error: spawn is not an NPC");
  5106. return 0;
  5107. }
  5108. ((NPC*)spawn)->Brain()->ClearEncounter();
  5109. return 0;
  5110. }
  5111. int EQ2Emu_lua_GetEncounter(lua_State* state) {
  5112. if (!lua_interface)
  5113. return 0;
  5114. Spawn* spawn = lua_interface->GetSpawn(state);
  5115. if (!spawn) {
  5116. lua_interface->LogError("LUA GetEncounter command error: Spawn is not valid");
  5117. return 0;
  5118. }
  5119. if (!spawn->IsNPC()) {
  5120. lua_interface->LogError("LUA GetEncounter command error: spawn is not a NPC");
  5121. return 0;
  5122. }
  5123. // Temp list to store hate list
  5124. vector<int32>* encounterList = ((NPC*)spawn)->Brain()->GetEncounter();
  5125. if (encounterList->size() == 0) {
  5126. safe_delete(encounterList);
  5127. return 0;
  5128. }
  5129. lua_createtable(state, encounterList->size(), 0);
  5130. int newTable = lua_gettop(state);
  5131. for(int32 i = 0; i < encounterList->size(); i++) {
  5132. Spawn* temp = spawn->GetZone()->GetSpawnByID(encounterList->at(i));
  5133. if (temp)
  5134. lua_interface->SetSpawnValue(state, temp);
  5135. lua_rawseti(state, newTable, i + 1);
  5136. }
  5137. safe_delete(encounterList);
  5138. return 1;
  5139. }
  5140. int EQ2Emu_lua_GetHateList(lua_State* state) {
  5141. if (!lua_interface)
  5142. return 0;
  5143. Spawn* spawn = lua_interface->GetSpawn(state);
  5144. if (!spawn) {
  5145. lua_interface->LogError("LUA GetHateList command error: spawn is not valid");
  5146. return 0;
  5147. }
  5148. if (!spawn->IsNPC()) {
  5149. lua_interface->LogError("LUA GetHateList command error: spawn is not a NPC");
  5150. return 0;
  5151. }
  5152. // Temp list to store hate list
  5153. vector<Entity*>* hateList = ((NPC*)spawn)->Brain()->GetHateList();
  5154. if (hateList->size() == 0) {
  5155. safe_delete(hateList);
  5156. return 0;
  5157. }
  5158. lua_createtable(state, hateList->size(), 0);
  5159. int newTable = lua_gettop(state);
  5160. for(int32 i = 0; i < hateList->size(); i++) {
  5161. lua_interface->SetSpawnValue(state, hateList->at(i));
  5162. lua_rawseti(state, newTable, i + 1);
  5163. }
  5164. safe_delete(hateList);
  5165. return 1;
  5166. }
  5167. int EQ2Emu_lua_HasGroup(lua_State* state) {
  5168. if (!lua_interface)
  5169. return 0;
  5170. Spawn* spawn = lua_interface->GetSpawn(state);
  5171. if (!spawn) {
  5172. lua_interface->LogError("LUA HasGroup command error: spawn is not valid");
  5173. return 0;
  5174. }
  5175. if (spawn->IsPlayer()) {
  5176. if (((Player*)spawn)->GetGroupMemberInfo() && world.GetGroupManager()->GetGroupSize(((Player*)spawn)->GetGroupMemberInfo()->group_id) > 1)
  5177. lua_interface->SetBooleanValue(state, true);
  5178. else
  5179. lua_interface->SetBooleanValue(state, false);
  5180. return 1;
  5181. }
  5182. else {
  5183. lua_interface->SetBooleanValue(state, spawn->HasSpawnGroup());
  5184. return 1;
  5185. }
  5186. }
  5187. int EQ2Emu_lua_SetCompleteFlag(lua_State* state) {
  5188. if (!lua_interface)
  5189. return 0;
  5190. Quest* quest = lua_interface->GetQuest(state);
  5191. if (!quest) {
  5192. lua_interface->LogError("LUA SetCompleteFlag command error: quest is not valid");
  5193. return 0;
  5194. }
  5195. quest->SetCompletedFlag(true);
  5196. return 0;
  5197. }
  5198. int EQ2Emu_lua_HasSpellEffect(lua_State* state) {
  5199. if (!lua_interface)
  5200. return 0;
  5201. Spawn* spawn = lua_interface->GetSpawn(state);
  5202. int32 spellID = lua_interface->GetInt32Value(state, 2);
  5203. int8 tier = lua_interface->GetInt8Value(state, 3);
  5204. if (!spawn) {
  5205. lua_interface->LogError("LUA HasSpellEffect command error: spawn is not valid");
  5206. return 0;
  5207. }
  5208. if (!spawn->IsEntity()) {
  5209. lua_interface->LogError("LUA HasSpellEffect command error: spawn is not an entity");
  5210. return 0;
  5211. }
  5212. if (spellID == 0) {
  5213. lua_interface->LogError("LUA HasSpellEffect command error: spell id is not valid");
  5214. return 0;
  5215. }
  5216. SpellEffects* effect = ((Entity*)spawn)->GetSpellEffect(spellID);
  5217. if (effect) {
  5218. if (tier > 0) {
  5219. // If a tier was passed chec to see if it is the same as the effect
  5220. if (tier == effect->tier)
  5221. lua_interface->SetBooleanValue(state, true);
  5222. else
  5223. lua_interface->SetBooleanValue(state, false);
  5224. return 1;
  5225. }
  5226. else {
  5227. // Have an effect but no tier was passed so return true
  5228. lua_interface->SetBooleanValue(state, true);
  5229. }
  5230. return 1;
  5231. }
  5232. // no effect so return false
  5233. lua_interface->SetBooleanValue(state, false);
  5234. return 1;
  5235. }
  5236. int EQ2Emu_lua_AddSpawnIDAccess(lua_State* state){
  5237. if (!lua_interface)
  5238. return 0;
  5239. Spawn* spawn = lua_interface->GetSpawn(state);
  5240. int32 id = lua_interface->GetInt32Value(state, 2);
  5241. ZoneServer* zone = lua_interface->GetZone(state, 3);
  5242. Spawn* spawn2 = 0;
  5243. vector<Spawn*> list;
  5244. if(!spawn){
  5245. lua_interface->LogError("LUA AddSpawnIDAccess command error: spawn is not valid");
  5246. return 0;
  5247. }
  5248. //If zone not provided, use spawn's zone
  5249. if(!zone)
  5250. zone = spawn->GetZone();
  5251. list = zone->GetSpawnsByID(id);
  5252. if(list.size() == 0){
  5253. lua_interface->LogError("LUA AddSpawnIDAccess command error: GetSpawnsByID returned no spawns");
  5254. return 0;
  5255. }
  5256. vector<Spawn*>::iterator itr = list.begin();
  5257. for (int8 i=0; i < list.size(); i++){
  5258. spawn2 = itr[i];
  5259. if(spawn2)
  5260. spawn2->AddAllowAccessSpawn(spawn);
  5261. }
  5262. return 0;
  5263. }
  5264. int EQ2Emu_lua_RemoveSpawnIDAccess(lua_State* state){
  5265. if (!lua_interface)
  5266. return 0;
  5267. Spawn* spawn = lua_interface->GetSpawn(state);
  5268. int32 id = lua_interface->GetInt32Value(state, 2);
  5269. ZoneServer* zone = lua_interface->GetZone(state, 3);
  5270. Spawn* spawn2 = 0;
  5271. if(!spawn){
  5272. lua_interface->LogError("LUA RemoveSpawnIDAccess command error: spawn is not valid");
  5273. return 0;
  5274. }
  5275. //If zone not provided, use spawn's zone
  5276. if(!zone)
  5277. zone = spawn->GetZone();
  5278. vector<Spawn*> list = zone->GetSpawnsByID(id);
  5279. vector<Spawn*>::iterator itr = list.begin();
  5280. if(list.size() == 0){
  5281. lua_interface->LogError("LUA RemoveSpawnIDAccess command error: GetSpawnsByID returned no spawns");
  5282. return 0;
  5283. }
  5284. for (int8 i=0; i < list.size(); i++){
  5285. spawn2 = itr[i];
  5286. if(spawn2)
  5287. spawn2->RemoveSpawnAccess(spawn);
  5288. }
  5289. return 0;
  5290. }
  5291. int EQ2Emu_lua_SetQuestYellow(lua_State* state){
  5292. if (!lua_interface)
  5293. return 0;
  5294. Quest* quest = lua_interface->GetQuest(state);
  5295. if (!quest){
  5296. lua_interface->LogError("LUA SetQuestYellow command error: quest is not valid");
  5297. return 0;
  5298. }
  5299. quest->SetYellowName(true);
  5300. return 0;
  5301. }
  5302. int EQ2Emu_lua_CanReceiveQuest(lua_State* state){
  5303. if (!lua_interface)
  5304. return 0;
  5305. Spawn* spawn = lua_interface->GetSpawn(state);
  5306. int32 quest_id = lua_interface->GetInt32Value(state, 2);
  5307. if (!spawn){
  5308. lua_interface->LogError("LUA CanReceieveQuest command error: spawn is not valid");
  5309. return 0;
  5310. }
  5311. if (!spawn->IsPlayer()){
  5312. lua_interface->LogError("LUA CanReceieveQuest command error: spawn is not player");
  5313. return 0;
  5314. }
  5315. lua_interface->SetBooleanValue(state, ((Player*)spawn)->CanReceiveQuest(quest_id));
  5316. return 1;
  5317. }
  5318. int EQ2Emu_lua_SetSuccessTimer(lua_State* state) {
  5319. if (!lua_interface)
  5320. return 0;
  5321. Spawn* spawn = lua_interface->GetSpawn(state);
  5322. if (!spawn) {
  5323. lua_interface->LogError("LUA SetSuccessTimer command error: spawn is not valid");
  5324. return 0;
  5325. }
  5326. if (!spawn->IsPlayer()) {
  5327. lua_interface->LogError("LUA SetSuccessTimer command error: spawn is not a player");
  5328. return 0;
  5329. }
  5330. ZoneServer* zone = spawn->GetZone();
  5331. if (!zone) {
  5332. lua_interface->LogError("LUA SetSuccessTimer command error: unable to get a valid zone for the given spawn");
  5333. return 0;
  5334. }
  5335. Instance_Type iType = zone->GetInstanceType();
  5336. if (iType == SOLO_LOCKOUT_INSTANCE ||
  5337. iType == GROUP_LOCKOUT_INSTANCE ||
  5338. iType == RAID_LOCKOUT_INSTANCE ||
  5339. iType == SOLO_PERSIST_INSTANCE ||
  5340. iType == GROUP_PERSIST_INSTANCE ||
  5341. iType == RAID_PERSIST_INSTANCE) {
  5342. InstanceData* data = ((Player*)spawn)->GetCharacterInstances()->FindInstanceByZoneID(spawn->GetZone()->GetZoneID());
  5343. if (data) {
  5344. // Check to see if the timer has already been set, if it has return out.
  5345. if (Timer::GetUnixTimeStamp() <= data->last_success_timestamp + data->success_lockout_time)
  5346. return 0;
  5347. database.UpdateCharacterInstance(((Player*)spawn)->GetCharacterID(), string(spawn->GetZone()->GetZoneName()), spawn->GetZone()->GetInstanceID(), 1, Timer::GetUnixTimeStamp());
  5348. data->last_success_timestamp = Timer::GetUnixTimeStamp();
  5349. Client* client = spawn->GetZone()->GetClientBySpawn(spawn);
  5350. if (client) {
  5351. string time_msg = "";
  5352. int32 time = data->success_lockout_time;
  5353. int16 hour;
  5354. int8 min;
  5355. int8 sec;
  5356. hour = time/3600;
  5357. time = time%3600;
  5358. min = time/60;
  5359. time = time%60;
  5360. sec = time;
  5361. if (hour > 0) {
  5362. char temp[10];
  5363. sprintf(temp, " %i", hour);
  5364. time_msg.append(temp);
  5365. time_msg.append(" hour");
  5366. time_msg.append((hour > 1) ? "s" : "");
  5367. }
  5368. if (min > 0) {
  5369. char temp[5];
  5370. sprintf(temp, " %i", min);
  5371. time_msg.append(temp);
  5372. time_msg.append(" minute");
  5373. time_msg.append((min > 1) ? "s" : "");
  5374. }
  5375. // Only add seconds if minutes and hours are 0
  5376. if (hour == 0 && min == 0 && sec > 0) {
  5377. char temp[5];
  5378. sprintf(temp, " %i", sec);
  5379. time_msg.append(temp);
  5380. time_msg.append(" second");
  5381. time_msg.append((sec > 1) ? "s" : "");
  5382. }
  5383. client->Message(CHANNEL_COLOR_YELLOW, "The success zone reuse timer for %s has been set. You can return in%s.", data->zone_name.c_str(), time_msg.c_str());
  5384. }
  5385. }
  5386. else
  5387. lua_interface->LogError("LUA SetSuccessTimer command error: unable to get instance data for instance %u for player %s", spawn->GetZone()->GetInstanceID(), spawn->GetName());
  5388. }
  5389. else
  5390. lua_interface->LogError("LUA SetSuccessTimer command error: current zone for given spawn is not a lockout or persistent instance");
  5391. return 0;
  5392. }
  5393. int EQ2Emu_lua_SetFailureTimer(lua_State* state) {
  5394. if (!lua_interface)
  5395. return 0;
  5396. Spawn* spawn = lua_interface->GetSpawn(state);
  5397. if (!spawn) {
  5398. lua_interface->LogError("LUA SetFailureTimer command error: spawn is not valid");
  5399. return 0;
  5400. }
  5401. if (!spawn->IsPlayer()) {
  5402. lua_interface->LogError("LUA SetFailureTimer command error: spawn is not a player");
  5403. return 0;
  5404. }
  5405. ZoneServer* zone = spawn->GetZone();
  5406. if (!zone) {
  5407. lua_interface->LogError("LUA SetFailureTimer command error: unable to get a valid zone for the given spawn");
  5408. return 0;
  5409. }
  5410. Instance_Type iType = zone->GetInstanceType();
  5411. if (iType == SOLO_LOCKOUT_INSTANCE ||
  5412. iType == GROUP_LOCKOUT_INSTANCE ||
  5413. iType == RAID_LOCKOUT_INSTANCE ||
  5414. iType == SOLO_PERSIST_INSTANCE ||
  5415. iType == GROUP_PERSIST_INSTANCE ||
  5416. iType == RAID_PERSIST_INSTANCE) {
  5417. InstanceData* data = ((Player*)spawn)->GetCharacterInstances()->FindInstanceByZoneID(spawn->GetZone()->GetZoneID());
  5418. if (data) {
  5419. // Check to see if the timer has already been set, if it has return out.
  5420. if (Timer::GetUnixTimeStamp() <= data->last_failure_timestamp + data->failure_lockout_time)
  5421. return 0;
  5422. database.UpdateCharacterInstance(((Player*)spawn)->GetCharacterID(), string(spawn->GetZone()->GetZoneName()), spawn->GetZone()->GetInstanceID(), 2, Timer::GetUnixTimeStamp());
  5423. data->last_failure_timestamp = Timer::GetUnixTimeStamp();
  5424. Client* client = spawn->GetZone()->GetClientBySpawn(spawn);
  5425. if (client) {
  5426. string time_msg = "";
  5427. int32 time = data->failure_lockout_time;
  5428. int16 hour;
  5429. int8 min;
  5430. int8 sec;
  5431. hour = time / 3600;
  5432. time = time % 3600;
  5433. min = time / 60;
  5434. time = time % 60;
  5435. sec = time;
  5436. if (hour > 0) {
  5437. char temp[10];
  5438. sprintf(temp, " %i", hour);
  5439. time_msg.append(temp);
  5440. time_msg.append(" hour");
  5441. time_msg.append((hour > 1) ? "s" : "");
  5442. }
  5443. if (min > 0) {
  5444. char temp[5];
  5445. sprintf(temp, " %i", min);
  5446. time_msg.append(temp);
  5447. time_msg.append(" minute");
  5448. time_msg.append((min > 1) ? "s" : "");
  5449. }
  5450. // Only add seconds if minutes and hours are 0
  5451. if (hour == 0 && min == 0 && sec > 0) {
  5452. char temp[5];
  5453. sprintf(temp, " %i", sec);
  5454. time_msg.append(temp);
  5455. time_msg.append(" second");
  5456. time_msg.append((sec > 1) ? "s" : "");
  5457. }
  5458. client->Message(CHANNEL_COLOR_YELLOW, "The failure zone reuse timer for %s has been set. You can return in%s", data->zone_name.c_str(), time_msg.c_str());
  5459. }
  5460. }
  5461. else
  5462. lua_interface->LogError("LUA SetFailureTimer command error: unable to get instance data for instance %u for player %s", spawn->GetZone()->GetInstanceID(), spawn->GetName());
  5463. }
  5464. else
  5465. lua_interface->LogError("LUA SetFailureTimer command error: current zone for given spawn is not a lockout or persistent instance");
  5466. return 0;
  5467. }
  5468. int EQ2Emu_lua_IsGroundSpawn(lua_State* state) {
  5469. if (!lua_interface)
  5470. return 0;
  5471. Spawn* spawn = lua_interface->GetSpawn(state);
  5472. if (!spawn) {
  5473. lua_interface->LogError("LUA IsGroundSpawn command error: not a valid spawn");
  5474. return 0;
  5475. }
  5476. lua_interface->SetBooleanValue(state, spawn->IsGroundSpawn());
  5477. return 1;
  5478. }
  5479. int EQ2Emu_lua_CanHarvest(lua_State* state) {
  5480. if (!lua_interface)
  5481. return 0;
  5482. Spawn* player = lua_interface->GetSpawn(state);
  5483. Spawn* ground = lua_interface->GetSpawn(state, 2);
  5484. if (!player) {
  5485. lua_interface->LogError("LUA CanHarvest command error: not a valid spawn");
  5486. return 0;
  5487. }
  5488. if (!player->IsPlayer()) {
  5489. lua_interface->LogError("LUA CanHarvest command error: spawn is not a player");
  5490. return 0;
  5491. }
  5492. if (!ground) {
  5493. lua_interface->LogError("LUA CanHarvest command error: not a valid spawn");
  5494. return 0;
  5495. }
  5496. if (!ground->IsGroundSpawn()) {
  5497. lua_interface->LogError("LUA CanHarvest command error: spawn is not a ground spawn");
  5498. return 0;
  5499. }
  5500. vector<GroundSpawnEntry*>* groundspawn_entries = player->GetZone()->GetGroundSpawnEntries(((GroundSpawn*)ground)->GetGroundSpawnEntryID());
  5501. if(!groundspawn_entries) {
  5502. lua_interface->LogError("LUA CanHarvest command error: No groundspawn entries assigned to groundspawn id: %u", ((GroundSpawn*)ground)->GetGroundSpawnEntryID());
  5503. return 0;
  5504. }
  5505. Skill* skill = 0;
  5506. string collection_skill = string(((GroundSpawn*)ground)->GetCollectionSkill());
  5507. if (collection_skill == "Collecting")
  5508. skill = ((Player*)player)->GetSkillByName("Gathering");
  5509. else
  5510. skill = ((Player*)player)->GetSkillByName(collection_skill.c_str());
  5511. if(!skill) {
  5512. lua_interface->LogError("LUA CanHarvest command error: Player '%s' lacks the skill: '%s'", player->GetName(), collection_skill.c_str());
  5513. return 0;
  5514. }
  5515. vector<GroundSpawnEntry*>::iterator itr;
  5516. GroundSpawnEntry* entry = 0;
  5517. bool can_harvest = false;
  5518. sint32 min_skill = -1;
  5519. // first, iterate through groundspawn_entries, discard tables player cannot use
  5520. for(itr = groundspawn_entries->begin(); itr != groundspawn_entries->end(); itr++)
  5521. {
  5522. entry = *itr;
  5523. if (min_skill == -1 || entry->min_skill_level < min_skill)
  5524. min_skill = entry->min_skill_level;
  5525. // if player lacks skill, skip table
  5526. if( entry->min_skill_level > skill->current_val )
  5527. continue;
  5528. // if bonus, but player lacks level, skip table
  5529. if(entry->bonus_table && (player->GetLevel() < entry->min_adventure_level))
  5530. continue;
  5531. can_harvest = true;
  5532. break;
  5533. }
  5534. lua_interface->SetBooleanValue(state, can_harvest);
  5535. // If false, send the message to the client
  5536. if (!can_harvest) {
  5537. Client* client = player->GetZone()->GetClientBySpawn(player);
  5538. if (client) {
  5539. string msg = "You do not have enough skill to ";
  5540. if(collection_skill == "Gathering" || collection_skill == "Collecting")
  5541. msg.append("gather");
  5542. else if(collection_skill == "Mining")
  5543. msg.append("mine");
  5544. else if(collection_skill == "Trapping")
  5545. msg.append("trap");
  5546. else if(collection_skill == "Foresting")
  5547. msg.append("forest");
  5548. else if(collection_skill == "Fishing")
  5549. msg.append("catch");
  5550. msg.append(" the %s. It requires %i %s skill, and your skill is %i.");
  5551. client->Message(CHANNEL_HARVESTING_WARNINGS, msg.c_str(), ground->GetName(), min_skill, skill->name.data.c_str(), skill->current_val);
  5552. // You do not have enough skill to catch the band of fish. It requires 20 Fishing skill, and your skill is 12.
  5553. }
  5554. }
  5555. return 1;
  5556. }
  5557. int EQ2Emu_lua_HasRecipeBook(lua_State* state){
  5558. if (!lua_interface)
  5559. return 0;
  5560. Spawn* player = lua_interface->GetSpawn(state);
  5561. int32 recipe_id = lua_interface->GetInt32Value(state, 2);
  5562. if(!player){
  5563. lua_interface->LogError("LUA HasRecipeBook command error, Spawn is not valid");
  5564. return 0;
  5565. }
  5566. if(!player->IsPlayer()){
  5567. lua_interface->LogError("LUA HasRecipeBook command error, Spawn is not a player");
  5568. return 0;
  5569. }
  5570. bool ret = ((Player*)player)->HasRecipeBook(recipe_id);
  5571. lua_interface->SetBooleanValue(state, ret);
  5572. return 1;
  5573. }
  5574. int EQ2Emu_lua_SummonDumbFirePet(lua_State* state) {
  5575. // Check to see if we have a valid lua_interface
  5576. if(!lua_interface)
  5577. return 0;
  5578. // Get the spawn that is getting the pet
  5579. Spawn* spawn = lua_interface->GetSpawn(state);
  5580. Spawn* target = lua_interface->GetSpawn(state, 2);
  5581. // Get the DB ID of the pet
  5582. int32 pet_id = lua_interface->GetInt32Value(state, 3);
  5583. float x = lua_interface->GetFloatValue(state, 4);
  5584. float y = lua_interface->GetFloatValue(state, 5);
  5585. float z = lua_interface->GetFloatValue(state, 6);
  5586. // Get the spell that this command was called from
  5587. LuaSpell* luaspell = lua_interface->GetCurrentSpell(state);
  5588. // Check to make sure the spawn pointer is valid
  5589. if (!spawn) {
  5590. lua_interface->LogError("LUA SummonDumbFirePet command error: Spawn is not valid");
  5591. return 0;
  5592. }
  5593. // Check to make sure the spawn is an entity
  5594. if (!spawn->IsEntity()) {
  5595. lua_interface->LogError("LUA SummonDumbFirePet command error: Spawn is not an entity");
  5596. return 0;
  5597. }
  5598. if (!target) {
  5599. lua_interface->LogError("LUA SummonDumbFirePet command error: target is not valid");
  5600. return 0;
  5601. }
  5602. if (!target->IsEntity()) {
  5603. lua_interface->LogError("LUA SummonDumbFirePet command error: target is not an entity");
  5604. return 0;
  5605. }
  5606. // Check to see if the DB ID for the pet is set
  5607. if (pet_id == 0) {
  5608. lua_interface->LogError("LUA SummonDumbFirePet command error: pet_id can not be set to 0");
  5609. return 0;
  5610. }
  5611. // Check to see if the pointer to the spell is valid
  5612. if (!luaspell) {
  5613. lua_interface->LogError("LUA SummonDumbFirePet command error: valid spell not found, SummonPet can only be used in spell scripts");
  5614. return 0;
  5615. }
  5616. // Get a pointer to a spawn with the given DB ID and check if the pointer is valid
  5617. Spawn* pet = spawn->GetZone()->GetSpawn(pet_id);
  5618. if(!pet) {
  5619. lua_interface->LogError("LUA SummonDumbFirePet command error: Could not find spawn with id of %u.", pet_id);
  5620. return 0;
  5621. }
  5622. // Check to make sure the pet is an npc
  5623. if (!pet->IsNPC()) {
  5624. lua_interface->LogError("LUA SummonDumbFirePet command error: id (%u) did not point to a npc", pet_id);
  5625. return 0;
  5626. }
  5627. if (x == 0)
  5628. x = spawn->GetX();
  5629. if (y == 0)
  5630. y = spawn->GetY();
  5631. if (z == 0)
  5632. z = spawn->GetZ();
  5633. // Spawn the pet at the same location as the owner
  5634. pet->SetX(x);
  5635. pet->SetY(y);
  5636. pet->SetZ(z);
  5637. pet->SetLocation(spawn->GetLocation());
  5638. pet->SetHeading(spawn->GetHeading());
  5639. spawn->GetZone()->AddSpawn(pet);
  5640. /*
  5641. const char* spawn_script = world.GetSpawnScript(pet_id);
  5642. if(spawn_script && lua_interface->GetSpawnScript(spawn_script) != 0){
  5643. spawn->SetSpawnScript(string(spawn_script));
  5644. zone->CallSpawnScript(spawn, SPAWN_SCRIPT_SPAWN);
  5645. }*/
  5646. // Get a random pet name
  5647. string random_pet_name;
  5648. int16 rand_index = MakeRandomInt(0, spawn->GetZone()->pet_names.size() - 1);
  5649. random_pet_name = spawn->GetZone()->pet_names.at(rand_index);
  5650. LogWrite(PET__DEBUG, 0, "Pets", "Randomize Pet Name: '%s' (rand: %i)", random_pet_name.c_str(), rand_index);
  5651. // Set the pets name
  5652. pet->SetName(random_pet_name.c_str());
  5653. // Set the level of the pet to the owners level
  5654. pet->SetLevel(spawn->GetLevel());
  5655. // Set the faction of the pet to the same faction as the owner
  5656. pet->SetFactionID(spawn->GetFactionID());
  5657. // Set the spawn as a pet
  5658. pet->SetPet(true);
  5659. // Give a pointer of the owner to the pet
  5660. ((NPC*)pet)->SetOwner((Entity*)spawn);
  5661. // Set the pet type
  5662. ((NPC*)pet)->SetPetType(PET_TYPE_DUMBFIRE);
  5663. // Set the spell id used to create this pet
  5664. ((NPC*)pet)->SetPetSpellID(luaspell->spell->GetSpellData()->id);
  5665. // Set the spell tier used to create this pet
  5666. ((NPC*)pet)->SetPetSpellTier(luaspell->spell->GetSpellData()->tier);
  5667. // Set the pets spawn type to 6
  5668. pet->SetSpawnType(6);
  5669. // Set the pets brain
  5670. ((NPC*)pet)->SetBrain(new DumbFirePetBrain((NPC*)pet, (Entity*)target, luaspell->spell->GetSpellDuration() * 100));
  5671. // Check to see if the pet has a subtitle
  5672. if (strlen(pet->GetSubTitle()) > 0) {
  5673. // Add the players name to the front of the sub title
  5674. string pet_subtitle;
  5675. pet_subtitle.append(spawn->GetName()).append("'s ").append(pet->GetSubTitle());
  5676. LogWrite(PET__DEBUG, 0, "Pets", "Pet Subtitle: '%s'", pet_subtitle.c_str());
  5677. // Set the pets subtitle to the new one
  5678. pet->SetSubTitle(pet_subtitle.c_str());
  5679. }
  5680. // Set the pet as the return value for this function
  5681. lua_interface->SetSpawnValue(state, pet);
  5682. return 1;
  5683. }
  5684. int EQ2Emu_lua_SpawnMove(lua_State* state){
  5685. if(!lua_interface)
  5686. return 0;
  5687. Spawn* spawn = lua_interface->GetSpawn(state);
  5688. Spawn* player = lua_interface->GetSpawn(state, 2);
  5689. float max_distance = lua_interface->GetFloatValue(state, 3);
  5690. string type = lua_interface->GetStringValue(state, 4);
  5691. if(!spawn || (spawn && spawn->IsPlayer())){
  5692. lua_interface->LogError("LUA SpawnMove command error: first param spawn is not valid or is player");
  5693. return 0;
  5694. }
  5695. if(!player || (player && !player->IsPlayer())){
  5696. lua_interface->LogError("LUA SpawnMove command error: second param is not player");
  5697. return 0;
  5698. }
  5699. Client* client = 0;
  5700. if(player->GetZone())
  5701. client = player->GetZone()->GetClientBySpawn(player);
  5702. if(!client){
  5703. lua_interface->LogError("LUA SpawnMove command error: could not find client");
  5704. return 0;
  5705. }
  5706. //Set max_distance to default if not set or not proper value
  5707. if(max_distance <= 0)
  5708. max_distance = 500;
  5709. PacketStruct* packet = configReader.getStruct("WS_MoveObjectMode", client->GetVersion());
  5710. if(packet){
  5711. float unknown2_3 = 0;
  5712. int8 placement_mode = 0;
  5713. if(type == "wall"){
  5714. placement_mode = 2;
  5715. unknown2_3 = 150;
  5716. }
  5717. else if(type == "ceiling")
  5718. placement_mode = 1;
  5719. packet->setDataByName("placement_mode", placement_mode);
  5720. packet->setDataByName("spawn_id", ((Player*)player)->GetIDWithPlayerSpawn(spawn));
  5721. packet->setDataByName("model_type", spawn->GetModelType());
  5722. packet->setDataByName("unknown", 1); //size
  5723. packet->setDataByName("unknown2", 1); //size 2
  5724. packet->setDataByName("unknown2", .5, 1); //size 3
  5725. packet->setDataByName("unknown2", 3, 2);
  5726. packet->setDataByName("unknown2", unknown2_3, 3);
  5727. packet->setDataByName("max_distance", max_distance);
  5728. packet->setDataByName("CoEunknown", 0xFFFFFFFF);
  5729. client->QueuePacket(packet->serialize());
  5730. safe_delete(packet);
  5731. }
  5732. return 0;
  5733. }
  5734. int EQ2Emu_lua_GetItemType(lua_State* state){
  5735. if(!lua_interface)
  5736. return 0;
  5737. Item* item = lua_interface->GetItem(state);
  5738. if(!item){
  5739. lua_interface->LogError("LUA GetItemType command error: item pointer is not valid");
  5740. return 0;
  5741. }
  5742. lua_interface->SetInt32Value(state, item->generic_info.item_type);
  5743. return 1;
  5744. }
  5745. int EQ2Emu_lua_AddTransportSpawn(lua_State* state){
  5746. if(!lua_interface)
  5747. return 0;
  5748. Spawn* spawn = lua_interface->GetSpawn(state);
  5749. if(!spawn){
  5750. lua_interface->LogError("LUA AddTransportSpawn command error: spawn is not valid");
  5751. return 0;
  5752. }
  5753. if(spawn->GetZone())
  5754. spawn->GetZone()->AddTransportSpawn(spawn);
  5755. return 0;
  5756. }
  5757. int EQ2Emu_lua_GetSkillValue(lua_State* state) {
  5758. if (!lua_interface)
  5759. return 0;
  5760. Skill* skill = lua_interface->GetSkill(state);
  5761. if (!skill) {
  5762. lua_interface->LogError("LUA GetSkillValue command error: skill is not valid");
  5763. return 0;
  5764. }
  5765. lua_interface->SetInt32Value(state, skill->current_val);
  5766. return 1;
  5767. }
  5768. int EQ2Emu_lua_GetSkillMaxValue(lua_State* state) {
  5769. if (!lua_interface)
  5770. return 0;
  5771. Skill* skill = lua_interface->GetSkill(state);
  5772. if (!skill) {
  5773. lua_interface->LogError("LUA GetSkillMaxValue command error: skill is not valid");
  5774. return 0;
  5775. }
  5776. lua_interface->SetInt32Value(state, skill->max_val);
  5777. return 1;
  5778. }
  5779. int EQ2Emu_lua_GetSkillName(lua_State* state) {
  5780. if (!lua_interface)
  5781. return 0;
  5782. Skill* skill = lua_interface->GetSkill(state);
  5783. if (!skill) {
  5784. lua_interface->LogError("LUA GetSkillName command error: skill is not valid");
  5785. return 0;
  5786. }
  5787. lua_interface->SetStringValue(state, skill->name.data.c_str());
  5788. return 1;
  5789. }
  5790. int EQ2Emu_lua_SetSkillMaxValue(lua_State* state) {
  5791. if (!lua_interface)
  5792. return 0;
  5793. Skill* skill = lua_interface->GetSkill(state);
  5794. int16 value = lua_interface->GetInt16Value(state, 2);
  5795. if (!skill) {
  5796. lua_interface->LogError("LUA SetSkillMaxValue command error: skill is not valid");
  5797. return 0;
  5798. }
  5799. skill->max_val = value;
  5800. if (skill->max_val < skill->current_val)
  5801. skill->current_val = skill->max_val;
  5802. return 0;
  5803. }
  5804. int EQ2Emu_lua_SetSkillValue(lua_State* state) {
  5805. if (!lua_interface)
  5806. return 0;
  5807. Skill* skill = lua_interface->GetSkill(state);
  5808. int16 value = lua_interface->GetInt16Value(state, 2);
  5809. if (!skill) {
  5810. lua_interface->LogError("LUA SetSkillValue command error: skill is not valid");
  5811. return 0;
  5812. }
  5813. if (value > skill->max_val)
  5814. skill->current_val = skill->max_val;
  5815. else
  5816. skill->current_val = value;
  5817. return 0;
  5818. }
  5819. int EQ2Emu_lua_GetSkill(lua_State* state) {
  5820. if (!lua_interface)
  5821. return 0;
  5822. Spawn* spawn = lua_interface->GetSpawn(state);
  5823. string name = lua_interface->GetStringValue(state, 2);
  5824. if (!spawn) {
  5825. lua_interface->LogError("LUA GetSkill command error: spawn is not valid");
  5826. return 0;
  5827. }
  5828. if (!spawn->IsEntity()) {
  5829. lua_interface->LogError("LUA GetSkill command error: spawn is not a valid entity");
  5830. return 0;
  5831. }
  5832. Skill* skill = ((Entity*)spawn)->GetSkillByName(name.c_str());
  5833. if (skill) {
  5834. lua_interface->SetSkillValue(state, skill);
  5835. return 1;
  5836. }
  5837. return 0;
  5838. }
  5839. int EQ2Emu_lua_AddProc(lua_State* state) {
  5840. if (!lua_interface)
  5841. return 0;
  5842. Spawn* spawn = lua_interface->GetSpawn(state);
  5843. int8 type = lua_interface->GetInt8Value(state, 2);
  5844. float chance = lua_interface->GetFloatValue(state, 3);
  5845. Item* item = lua_interface->GetItem(state, 4);
  5846. bool use_all_spelltargets = (lua_interface->GetInt8Value(state, 5) == 1);
  5847. LuaSpell* spell = 0;
  5848. if (!spawn && (!spell || !use_all_spelltargets)) {
  5849. lua_interface->LogError("LUA AddProc command error: spawn is not valid");
  5850. return 0;
  5851. }
  5852. if ((!spell || use_all_spelltargets) && spawn && !spawn->IsEntity()) {
  5853. lua_interface->LogError("LUA AddProc command error: spawn is not a valid entity");
  5854. return 0;
  5855. }
  5856. if (!item)
  5857. spell = lua_interface->GetCurrentSpell(state);
  5858. if (!item && !spell) {
  5859. lua_interface->LogError("LUA AddProc command error: can only use with an item provided or inside a spell script");
  5860. return 0;
  5861. }
  5862. if (spell && use_all_spelltargets){
  5863. Spawn* target;
  5864. spell->MSpellTargets.readlock(__FUNCTION__, __LINE__);
  5865. for (int8 i = 0; i < spell->targets.size(); i++){
  5866. target = spell->caster->GetZone()->GetSpawnByID(spell->targets.at(i));
  5867. if (!target || !target->IsEntity())
  5868. continue;
  5869. ((Entity*)target)->AddProc(type, chance, item, spell);
  5870. }
  5871. spell->MSpellTargets.releasereadlock(__FUNCTION__, __LINE__);
  5872. }
  5873. else
  5874. ((Entity*)spawn)->AddProc(type, chance, item, spell);
  5875. return 0;
  5876. }
  5877. int EQ2Emu_lua_RemoveProc(lua_State* state) {
  5878. if (!lua_interface)
  5879. return 0;
  5880. Spawn* spawn = lua_interface->GetSpawn(state);
  5881. Item* item = lua_interface->GetItem(state, 2);
  5882. LuaSpell* spell = 0;
  5883. if (!spawn) {
  5884. lua_interface->LogError("LUA RemoveProc command error: spawn is not valid");
  5885. return 0;
  5886. }
  5887. if (!spawn->IsEntity()) {
  5888. lua_interface->LogError("LUA RemoveProc command error: spawn is not a valid entity");
  5889. return 0;
  5890. }
  5891. if (!item)
  5892. spell = lua_interface->GetCurrentSpell(state);
  5893. if (!item && !spell) {
  5894. lua_interface->LogError("LUA RemoveProc command error: can only use with an item provided or inside a spell script");
  5895. return 0;
  5896. }
  5897. if (spell){
  5898. Spawn* target;
  5899. spell->MSpellTargets.readlock(__FUNCTION__, __LINE__);
  5900. for (int8 i = 0; i < spell->targets.size(); i++){
  5901. target = spell->caster->GetZone()->GetSpawnByID(spell->targets.at(i));
  5902. if (!target || !target->IsEntity())
  5903. continue;
  5904. ((Entity*)target)->RemoveProc(item, spell);
  5905. }
  5906. spell->MSpellTargets.releasereadlock(__FUNCTION__, __LINE__);
  5907. spell->caster->RemoveProc(item, spell);
  5908. }
  5909. else
  5910. ((Entity*)spawn)->RemoveProc(item, spell);
  5911. return 0;
  5912. }
  5913. int EQ2Emu_lua_Knockback(lua_State* state) {
  5914. if (!lua_interface)
  5915. return 0;
  5916. Spawn* target_spawn = lua_interface->GetSpawn(state);
  5917. Spawn* spawn = lua_interface->GetSpawn(state, 2);
  5918. int32 duration = lua_interface->GetInt32Value(state, 3);
  5919. float vertical = lua_interface->GetFloatValue(state, 4);
  5920. float horizontal = lua_interface->GetFloatValue(state, 5);
  5921. bool use_heading = lua_interface->GetInt8Value(state, 6) == 1 ? true : false;
  5922. LuaSpell* spell = lua_interface->GetCurrentSpell(state);
  5923. if (!target_spawn) {
  5924. lua_interface->LogError("LUA Knockback command error: target_spawn is not valid");
  5925. return 0;
  5926. }
  5927. if (!spawn) {
  5928. lua_interface->LogError("LUA Knockback command error: spawn is not valid");
  5929. return 0;
  5930. }
  5931. if (spawn->IsPlayer() && (vertical != 0 || horizontal != 0)) {
  5932. Client* client = spawn->GetZone()->GetClientBySpawn(spawn);
  5933. PacketStruct* packet = configReader.getStruct("WS_PlayerKnockback", client->GetVersion());
  5934. if (packet) {
  5935. packet->setDataByName("target_x", target_spawn->GetX());
  5936. packet->setDataByName("target_y", target_spawn->GetY());
  5937. packet->setDataByName("target_z", target_spawn->GetZ());
  5938. packet->setDataByName("vertical_movement", vertical);
  5939. packet->setDataByName("horizontal_movement", horizontal);
  5940. if (use_heading)
  5941. packet->setDataByName("use_player_heading", 1);
  5942. client->QueuePacket(packet->serialize());
  5943. }
  5944. safe_delete(packet);
  5945. }
  5946. return 0;
  5947. }
  5948. int EQ2Emu_lua_IsEpic(lua_State* state) {
  5949. if (!lua_interface)
  5950. return 0;
  5951. Spawn* spawn = lua_interface->GetSpawn(state);
  5952. if (!spawn) {
  5953. lua_interface->LogError("LUA IsEpic command error: spawn is not valid");
  5954. return 0;
  5955. }
  5956. lua_interface->SetBooleanValue(state, (spawn->GetHeroic() >= 2));
  5957. return 1;
  5958. }
  5959. int EQ2Emu_lua_ProcDamage(lua_State* state) {
  5960. if (!lua_interface)
  5961. return 0;
  5962. Spawn* caster = lua_interface->GetSpawn(state);
  5963. Spawn* target = lua_interface->GetSpawn(state, 2);
  5964. string name = lua_interface->GetStringValue(state, 3);
  5965. int8 dmg_type = lua_interface->GetInt8Value(state, 4);
  5966. int32 low_damage = lua_interface->GetInt32Value(state, 5);
  5967. int32 high_damage = lua_interface->GetInt32Value(state, 6);
  5968. string success_msg = lua_interface->GetStringValue(state, 7);
  5969. string effect_msg = lua_interface->GetStringValue(state, 8);
  5970. if (!caster) {
  5971. lua_interface->LogError("LUA ProcDamage command error: caster is not a valid spawn");
  5972. return 0;
  5973. }
  5974. if (!caster->IsEntity()) {
  5975. lua_interface->LogError("LUA ProcDamage command error: caster is not an entity");
  5976. return 0;
  5977. }
  5978. if (!target) {
  5979. lua_interface->LogError("LUA ProcDamage command error: target is not a valid spawn");
  5980. return 0;
  5981. }
  5982. if (!target->IsEntity()) {
  5983. lua_interface->LogError("LUA ProcDamage command error: target is not an entity");
  5984. return 0;
  5985. }
  5986. if (name.length() == 0) {
  5987. lua_interface->LogError("LUA ProcDamage command error: name is empty");
  5988. return 0;
  5989. }
  5990. ((Entity*)caster)->ProcAttack(target, dmg_type, low_damage, high_damage, name, success_msg, effect_msg);
  5991. return 0;
  5992. }
  5993. int EQ2Emu_lua_GetSkillIDByName(lua_State* state) {
  5994. if(!lua_interface)
  5995. return 0;
  5996. string name = lua_interface->GetStringValue(state);
  5997. if(name.length() == 0){
  5998. lua_interface->LogError("LUA GetSkillIDByName command error: name param was not set");
  5999. return 0;
  6000. }
  6001. Skill* skill = master_skill_list.GetSkillByName(name.c_str());
  6002. if(!skill){
  6003. lua_interface->LogError("LUA GetSkillIDByName command error: skill with name of %s not found", name.c_str());
  6004. return 0;
  6005. }
  6006. lua_interface->SetInt32Value(state, skill->skill_id);
  6007. return 1;
  6008. }
  6009. int EQ2Emu_lua_IsHeroic(lua_State* state) {
  6010. if(!lua_interface)
  6011. return 0;
  6012. Spawn* spawn = lua_interface->GetSpawn(state);
  6013. if(!spawn){
  6014. lua_interface->LogError("LUA IsHeroic command error: spawn is not valid");
  6015. return 0;
  6016. }
  6017. lua_interface->SetBooleanValue(state, spawn->GetHeroic() == 1);
  6018. return 1;
  6019. }
  6020. int EQ2Emu_lua_LastSpellAttackHit(lua_State* state) {
  6021. if(!lua_interface)
  6022. return 0;
  6023. LuaSpell* luaspell = lua_interface->GetCurrentSpell(state);
  6024. if(!luaspell){
  6025. lua_interface->LogError("LUA LastSpellAttackHit command error: this must be called from a spellscript");
  6026. return 0;
  6027. }
  6028. lua_interface->SetBooleanValue(state, luaspell->last_spellattack_hit);
  6029. return 1;
  6030. }
  6031. int EQ2Emu_lua_IsBehind(lua_State* state) {
  6032. if (!lua_interface)
  6033. return 0;
  6034. Spawn* spawn = lua_interface->GetSpawn(state);
  6035. Spawn* target = lua_interface->GetSpawn(state, 2);
  6036. if (!spawn) {
  6037. lua_interface->LogError("LUA IsBehind command error: spawn is not valid");
  6038. return 0;
  6039. }
  6040. if (!spawn->IsEntity()) {
  6041. lua_interface->LogError("LUA IsBehind command error: spawn is not an entity");
  6042. return 0;
  6043. }
  6044. if (!target) {
  6045. lua_interface->LogError("LUA IsBehind command error: target is not valid");
  6046. return 0;
  6047. }
  6048. lua_interface->SetBooleanValue(state, ((Entity*)spawn)->BehindTarget(target));
  6049. return 1;
  6050. }
  6051. int EQ2Emu_lua_IsFlanking(lua_State* state) {
  6052. if (!lua_interface)
  6053. return 0;
  6054. Spawn* spawn = lua_interface->GetSpawn(state);
  6055. Spawn* target = lua_interface->GetSpawn(state, 2);
  6056. if (!spawn) {
  6057. lua_interface->LogError("LUA IsFlanking command error: spawn is not valid");
  6058. return 0;
  6059. }
  6060. if (!spawn->IsEntity()) {
  6061. lua_interface->LogError("LUA IsFlanking command error: spawn is not an entity");
  6062. return 0;
  6063. }
  6064. if (!target) {
  6065. lua_interface->LogError("LUA IsFlanking command error: target is not valid");
  6066. return 0;
  6067. }
  6068. lua_interface->SetBooleanValue(state, ((Entity*)spawn)->FlankingTarget(target));
  6069. return 1;
  6070. }
  6071. int EQ2Emu_lua_GetItemCount(lua_State* state) {
  6072. if(!lua_interface)
  6073. return 0;
  6074. Item* item = lua_interface->GetItem(state);
  6075. if(!item){
  6076. lua_interface->LogError("LUA GetItemCount command error: item not valid");
  6077. return 0;
  6078. }
  6079. lua_interface->SetInt32Value(state, item->details.count);
  6080. return 1;
  6081. }
  6082. int EQ2Emu_lua_SetItemCount(lua_State* state) {
  6083. if(!lua_interface)
  6084. return 0;
  6085. Item* item = lua_interface->GetItem(state);
  6086. Spawn* owner = lua_interface->GetSpawn(state, 2);
  6087. int16 new_count = lua_interface->GetInt32Value(state, 3);
  6088. if(!item){
  6089. lua_interface->LogError("LUA SetItemCount command error: item not valid");
  6090. return 0;
  6091. }
  6092. if(!owner){
  6093. lua_interface->LogError("LUA SetItemCount command error: spawn not valid");
  6094. return 0;
  6095. }
  6096. if(!owner->IsPlayer()){
  6097. lua_interface->LogError("LUA SetItemCount command error: spawn is not a player");
  6098. return 0;
  6099. }
  6100. if(item->stack_count < new_count){
  6101. lua_interface->LogError("LUA SetItemCount command error: new item count cannot be more than max stack count");
  6102. return 0;
  6103. }
  6104. if(new_count > 0){
  6105. item->details.count = new_count;
  6106. item->save_needed = true;
  6107. }
  6108. else if(((Player*)owner)->GetEquipmentList()->GetItem(item->details.slot_id) == item)
  6109. ((Player*)owner)->GetEquipmentList()->RemoveItem(item->details.slot_id, true);
  6110. else if(((Player*)owner)->GetPlayerItemList()->GetItemFromUniqueID(item->details.unique_id) == item)
  6111. ((Player*)owner)->GetPlayerItemList()->RemoveItem(item, true);
  6112. else
  6113. {
  6114. lua_interface->LogError("LUA SetItemCount command error: could not remove item from player");
  6115. return 0;
  6116. }
  6117. Client* client = owner->GetZone()->GetClientBySpawn(owner);
  6118. if(!client)
  6119. return 0;
  6120. ((Player*)owner)->SendInventoryUpdate(client->GetVersion());
  6121. EQ2Packet* app = ((Player*)owner)->GetEquipmentList()->serialize(client->GetVersion());
  6122. if(app)
  6123. client->QueuePacket(app);
  6124. return 0;
  6125. }
  6126. int EQ2Emu_lua_AddSpellTimer(lua_State* state) {
  6127. if(!lua_interface)
  6128. return 0;
  6129. int32 time = lua_interface->GetInt32Value(state);
  6130. string function = lua_interface->GetStringValue(state, 2);
  6131. Spawn* caster = lua_interface->GetSpawn(state, 3);
  6132. Spawn* target = lua_interface->GetSpawn(state, 4);
  6133. LuaSpell* spell = lua_interface->GetCurrentSpell(state);
  6134. if (time == 0) {
  6135. lua_interface->LogError("LUA AddSpellTimer command error: time must be set");
  6136. return 0;
  6137. }
  6138. if (function.length() == 0) {
  6139. lua_interface->LogError("LUA AddSpellTimer command error: function name must be set");
  6140. return 0;
  6141. }
  6142. if (!spell) {
  6143. lua_interface->LogError("LUA AddSpellTimer command error: spell not found, AddSpellTimer must be used in a spell script");
  6144. return 0;
  6145. }
  6146. SpellScriptTimer* timer = new SpellScriptTimer;
  6147. /* //Google tells me memsetting a string is bad, manually setting just in case - Foof
  6148. #ifdef WIN32
  6149. ZeroMemory(timer, sizeof(SpellScriptTimer));
  6150. #else
  6151. bzero(timer, sizeof(SpellScriptTimer));
  6152. #endif*/
  6153. timer->caster = 0;
  6154. timer->deleteWhenDone = false;
  6155. timer->target = 0;
  6156. timer->time = Timer::GetCurrentTime2() + time;
  6157. timer->customFunction = function;
  6158. timer->spell = spell;
  6159. if (caster)
  6160. timer->caster = caster->GetID();
  6161. if (target)
  6162. timer->target = target->GetID();
  6163. spell->caster->GetZone()->GetSpellProcess()->AddSpellScriptTimer(timer);
  6164. return 0;
  6165. }
  6166. int EQ2Emu_lua_Resurrect(lua_State* state) {
  6167. if(!lua_interface)
  6168. return 0;
  6169. float hp_perc = lua_interface->GetFloatValue(state);
  6170. float power_perc = lua_interface->GetFloatValue(state, 2);
  6171. bool send_window = lua_interface->GetInt32Value(state, 3) == 1;
  6172. Spawn* target = lua_interface->GetSpawn(state, 4);
  6173. string heal_name = lua_interface->GetStringValue(state, 5);
  6174. int8 crit_mod = lua_interface->GetInt32Value(state, 6);
  6175. bool no_calcs = lua_interface->GetInt32Value(state, 7) == 1;
  6176. LuaSpell* spell = lua_interface->GetCurrentSpell(state);
  6177. if(!spell){
  6178. lua_interface->LogError("LUA command error: this function must be used in a spellscript");
  6179. return 0;
  6180. }
  6181. Entity* caster = spell->caster;
  6182. if(!caster){
  6183. lua_interface->LogError("LUA command error: could not find caster");
  6184. return 0;
  6185. }
  6186. Client* client = 0;
  6187. PendingResurrection* rez = 0;
  6188. ZoneServer* zone = spell->caster->GetZone();
  6189. if(!target){
  6190. spell->MSpellTargets.readlock(__FUNCTION__, __LINE__);
  6191. if(spell->targets.size() > 0){
  6192. vector<int32> spell_targets = spell->targets;
  6193. for(int8 i=0; i < spell_targets.size(); i++){
  6194. target = zone->GetSpawnByID(spell_targets.at(i));
  6195. if(!target)
  6196. continue;
  6197. if(!target->IsPlayer())
  6198. continue;
  6199. client = target->GetZone()->GetClientBySpawn(target);
  6200. if(!client)
  6201. continue;
  6202. rez = client->GetCurrentRez();
  6203. if(rez->active)
  6204. continue;
  6205. client->GetResurrectMutex()->writelock(__FUNCTION__, __LINE__);
  6206. rez->active = true;
  6207. rez->caster = caster;
  6208. rez->expire_timer = new Timer;
  6209. int32 duration = spell->spell->GetSpellDuration();
  6210. rez->expire_timer->Start(duration * 100);
  6211. rez->hp_perc = hp_perc;
  6212. rez->mp_perc = power_perc;
  6213. rez->range = spell->spell->GetSpellData()->range;
  6214. rez->spell_name = spell->spell->GetName();
  6215. if(heal_name.length() > 0)
  6216. rez->heal_name = heal_name;
  6217. else
  6218. rez->heal_name = rez->spell_name;
  6219. rez->no_calcs = no_calcs;
  6220. rez->crit_mod = crit_mod;
  6221. rez->spell_visual = spell->spell->GetSpellData()->spell_visual;
  6222. if(send_window)
  6223. client->SendResurrectionWindow();
  6224. else{
  6225. target->GetZone()->ResurrectSpawn(target, client);
  6226. rez->should_delete = true;
  6227. }
  6228. client->GetResurrectMutex()->releasewritelock(__FUNCTION__, __LINE__);
  6229. }
  6230. }
  6231. spell->MSpellTargets.releasereadlock(__FUNCTION__, __LINE__);
  6232. }
  6233. else {
  6234. client = target->GetZone()->GetClientBySpawn(target);
  6235. if(!client)
  6236. return 0;
  6237. rez = client->GetCurrentRez();
  6238. if(rez->active)
  6239. return 0;
  6240. client->GetResurrectMutex()->writelock(__FUNCTION__, __LINE__);
  6241. rez->active = true;
  6242. rez->caster = caster;
  6243. rez->expire_timer = new Timer;
  6244. int32 duration = spell->spell->GetSpellDuration();
  6245. rez->expire_timer->Start(duration * 100);
  6246. rez->hp_perc = hp_perc;
  6247. rez->mp_perc = power_perc;
  6248. rez->range = spell->spell->GetSpellData()->range;
  6249. rez->spell_name = spell->spell->GetName();
  6250. if(heal_name.length() > 0)
  6251. rez->heal_name = heal_name;
  6252. else
  6253. rez->heal_name = rez->spell_name;
  6254. rez->no_calcs = no_calcs;
  6255. rez->crit_mod = crit_mod;
  6256. rez->spell_visual = spell->spell->GetSpellData()->spell_visual;
  6257. if(send_window)
  6258. client->SendResurrectionWindow();
  6259. else{
  6260. target->GetZone()->ResurrectSpawn(target, client);
  6261. rez->should_delete = true;
  6262. }
  6263. client->GetResurrectMutex()->releasewritelock(__FUNCTION__, __LINE__);
  6264. }
  6265. return 0;
  6266. }
  6267. int EQ2Emu_lua_SetVision(lua_State* state) {
  6268. if (!lua_interface)
  6269. return 0;
  6270. Spawn* spawn = lua_interface->GetSpawn(state);
  6271. int8 vision = lua_interface->GetInt8Value(state, 2);
  6272. LuaSpell* spell = lua_interface->GetCurrentSpell(state);
  6273. if (!spawn) {
  6274. lua_interface->LogError("LUA SetVision command error: spawn is not valid");
  6275. return 0;
  6276. }
  6277. if (!spawn->IsEntity()) {
  6278. lua_interface->LogError("LUA SetVision command error: spawn is not an entity");
  6279. return 0;
  6280. }
  6281. if (spell && spell->targets.size() > 0) {
  6282. ZoneServer* zone = spell->caster->GetZone();
  6283. for (int8 i = 0; i < spell->targets.size(); i++) {
  6284. Spawn* target = zone->GetSpawnByID(spell->targets.at(i));
  6285. if (target->IsEntity()) {
  6286. ((Entity*)target)->GetInfoStruct()->vision = vision;
  6287. if (target->IsPlayer())
  6288. ((Player*)target)->SetCharSheetChanged(true);
  6289. }
  6290. }
  6291. }
  6292. else {
  6293. ((Entity*)spawn)->GetInfoStruct()->vision = vision;
  6294. if (spawn->IsPlayer())
  6295. ((Player*)spawn)->SetCharSheetChanged(true);
  6296. }
  6297. return 0;
  6298. }
  6299. int EQ2Emu_lua_BlurVision(lua_State* state) {
  6300. if (!lua_interface)
  6301. return 0;
  6302. Spawn* spawn = lua_interface->GetSpawn(state);
  6303. float intensity = lua_interface->GetFloatValue(state, 2);
  6304. LuaSpell* spell = lua_interface->GetCurrentSpell(state);
  6305. if (!spawn) {
  6306. lua_interface->LogError("LUA BlurVision command error: spawn is not valid");
  6307. return 0;
  6308. }
  6309. if (!spawn->IsEntity()) {
  6310. lua_interface->LogError("LUA BlurVision command error: spawn is not an entity");
  6311. return 0;
  6312. }
  6313. if (spell && spell->targets.size() > 0) {
  6314. ZoneServer* zone = spell->caster->GetZone();
  6315. for (int8 i = 0; i < spell->targets.size(); i++) {
  6316. Spawn* target = zone->GetSpawnByID(spell->targets.at(i));
  6317. if (target && target->IsEntity()) {
  6318. ((Entity*)target)->GetInfoStruct()->drunk = intensity;
  6319. if (target->IsPlayer())
  6320. ((Player*)target)->SetCharSheetChanged(true);
  6321. }
  6322. }
  6323. }
  6324. else {
  6325. ((Entity*)spawn)->GetInfoStruct()->drunk = intensity;
  6326. if (spawn->IsPlayer())
  6327. ((Player*)spawn)->SetCharSheetChanged(true);
  6328. }
  6329. return 0;
  6330. }
  6331. int EQ2Emu_lua_BreatheUnderwater(lua_State* state) {
  6332. if (!lua_interface)
  6333. return 0;
  6334. Spawn* spawn = lua_interface->GetSpawn(state);
  6335. bool breatheUnderwater = lua_interface->GetBooleanValue(state, 2);
  6336. LuaSpell* spell = lua_interface->GetCurrentSpell(state);
  6337. if (!spawn) {
  6338. lua_interface->LogError("LUA BreathUnderwater command error: spawn is not valid");
  6339. return 0;
  6340. }
  6341. if (!spawn->IsEntity()) {
  6342. lua_interface->LogError("LUA BreathUnderwater command error: spawn is not en entity");
  6343. return 0;
  6344. }
  6345. if (spell && spell->targets.size() > 0) {
  6346. ZoneServer* zone = spell->caster->GetZone();
  6347. for (int8 i = 0; i < spell->targets.size(); i++) {
  6348. Spawn* target = zone->GetSpawnByID(spell->targets.at(i));
  6349. if (target->IsEntity()) {
  6350. ((Entity*)target)->GetInfoStruct()->breathe_underwater = breatheUnderwater;
  6351. if (target->IsPlayer())
  6352. ((Player*)target)->SetCharSheetChanged(true);
  6353. }
  6354. }
  6355. }
  6356. else {
  6357. ((Entity*)spawn)->GetInfoStruct()->breathe_underwater = breatheUnderwater;
  6358. if (spawn->IsPlayer())
  6359. ((Player*)spawn)->SetCharSheetChanged(true);
  6360. }
  6361. return 0;
  6362. }
  6363. int EQ2Emu_lua_GetItemSkillReq(lua_State* state) {
  6364. if(!lua_interface)
  6365. return 0;
  6366. Item* item = lua_interface->GetItem(state);
  6367. int8 type = lua_interface->GetInt32Value(state, 2);
  6368. if(!item){
  6369. lua_interface->LogError("LUA GetItemSkillReq command error: item not valid");
  6370. return 0;
  6371. }
  6372. if(type == 1)
  6373. lua_interface->SetInt32Value(state, item->generic_info.skill_req1);
  6374. else if (type == 2)
  6375. lua_interface->SetInt32Value(state, item->generic_info.skill_req2);
  6376. return 1;
  6377. }
  6378. int EQ2Emu_lua_SetSpeeedMultiplier(lua_State* state) {
  6379. if (!lua_interface)
  6380. return 0;
  6381. Spawn* target = lua_interface->GetSpawn(state);
  6382. float val = lua_interface->GetFloatValue(state, 2);
  6383. LuaSpell* spell = lua_interface->GetCurrentSpell(state);
  6384. // Added from Gangrenous post
  6385. if (spell && spell->resisted)
  6386. return 0;
  6387. if (spell && spell->spell && spell->targets.size() > 0) {
  6388. ZoneServer* zone = spell->caster->GetZone();
  6389. for (int32 i = 0; i != spell->targets.size(); i++) {
  6390. Spawn* spawn = zone->GetSpawnByID(spell->targets.at(i));
  6391. if (spawn && spawn->IsEntity()) {
  6392. ((Entity*)spawn)->SetSpeedMultiplier(val);
  6393. if (spawn->IsPlayer())
  6394. ((Player*)spawn)->SetCharSheetChanged(true);
  6395. }
  6396. }
  6397. }
  6398. else {
  6399. if (target && target->IsEntity()) {
  6400. ((Entity*)target)->SetSpeedMultiplier(val);
  6401. if (target->IsPlayer())
  6402. ((Player*)target)->SetCharSheetChanged(true);
  6403. }
  6404. }
  6405. return 0;
  6406. }
  6407. int EQ2Emu_lua_SetIllusion(lua_State* state) {
  6408. if (!lua_interface)
  6409. return 0;
  6410. Spawn* spawn = lua_interface->GetSpawn(state);
  6411. int16 model = lua_interface->GetInt16Value(state, 2);
  6412. LuaSpell* spell = lua_interface->GetCurrentSpell(state);
  6413. if (spell && spell->spell && spell->targets.size() > 0) {
  6414. ZoneServer* zone = spell->caster->GetZone();
  6415. for (int32 i = 0; i < spell->targets.size(); i++) {
  6416. Spawn* target = zone->GetSpawnByID(spell->targets.at(i));
  6417. if (target)
  6418. target->SetIllusionModel(model);
  6419. }
  6420. }
  6421. else {
  6422. if (!spawn) {
  6423. lua_interface->LogError("LUA SetIllusion command error: spawn is not valid");
  6424. return 0;
  6425. }
  6426. spawn->SetIllusionModel(model);
  6427. }
  6428. return 0;
  6429. }
  6430. int EQ2Emu_lua_ResetIllusion(lua_State* state) {
  6431. if (!lua_interface)
  6432. return 0;
  6433. Spawn* spawn = lua_interface->GetSpawn(state);
  6434. LuaSpell* spell = lua_interface->GetCurrentSpell(state);
  6435. if (spell && spell->spell && spell->targets.size() > 0) {
  6436. ZoneServer* zone = spell->caster->GetZone();
  6437. for (int32 i = 0; i < spell->targets.size(); i++) {
  6438. Spawn* target = zone->GetSpawnByID(spell->targets.at(i));
  6439. if (target)
  6440. target->SetIllusionModel(0);
  6441. }
  6442. }
  6443. else {
  6444. if (!spawn) {
  6445. lua_interface->LogError("LUA ResetIllusion command error: spawn is not valid");
  6446. return 0;
  6447. }
  6448. spawn->SetIllusionModel(0);
  6449. }
  6450. return 0;
  6451. }
  6452. int EQ2Emu_lua_AddThreatTransfer(lua_State* state) {
  6453. if (!lua_interface)
  6454. return 0;
  6455. Spawn* caster = lua_interface->GetSpawn(state);
  6456. Spawn* target = lua_interface->GetSpawn(state, 2);
  6457. float chance = lua_interface->GetFloatValue(state, 3);
  6458. LuaSpell* spell = lua_interface->GetCurrentSpell(state);
  6459. if (!caster) {
  6460. lua_interface->LogError("LUA AddThreatTransfer command error: caster is not a valid spawn");
  6461. return 0;
  6462. }
  6463. if (!caster->IsEntity()) {
  6464. lua_interface->LogError("LUA AddThreatTransfer command error: caster is not an entity");
  6465. return 0;
  6466. }
  6467. if (!target) {
  6468. lua_interface->LogError("LUA AddThreatTransfer command error: target is not a valid spawn");
  6469. return 0;
  6470. }
  6471. if (!target->IsEntity()) {
  6472. lua_interface->LogError("LUA AddThreatTransfer command error: target is not an entity");
  6473. return 0;
  6474. }
  6475. if (chance <= 0) {
  6476. lua_interface->LogError("LUA AddThreatTransfer command error: chance must be greater then 0%");
  6477. return 0;
  6478. }
  6479. if (!spell) {
  6480. lua_interface->LogError("LUA AddThreatTransfer command error: can only be used in a spell script");
  6481. return 0;
  6482. }
  6483. if (((Entity*)caster)->GetThreatTransfer()) {
  6484. return 0;
  6485. }
  6486. ThreatTransfer* transfer = new ThreatTransfer;
  6487. transfer->Target = target->GetID();
  6488. transfer->Amount = chance;
  6489. transfer->Spell = spell;
  6490. ((Entity*)caster)->SetThreatTransfer(transfer);
  6491. return 0;
  6492. }
  6493. int EQ2Emu_lua_RemoveThreatTransfer(lua_State* state) {
  6494. if (!lua_interface)
  6495. return 0;
  6496. Spawn* spawn = lua_interface->GetSpawn(state);
  6497. LuaSpell* spell = lua_interface->GetCurrentSpell(state);
  6498. if (!spawn) {
  6499. lua_interface->LogError("LUA RemoveThreatTransfer command error: spawn is not valid");
  6500. return 0;
  6501. }
  6502. if (!spell) {
  6503. lua_interface->LogError("LUA RemoveThreatTransfer command error: can only be used in a spell script");
  6504. return 0;
  6505. }
  6506. if (((Entity*)spawn)->GetThreatTransfer() && ((Entity*)spawn)->GetThreatTransfer()->Spell == spell) {
  6507. ThreatTransfer* transfer = ((Entity*)spawn)->GetThreatTransfer();
  6508. safe_delete(transfer);
  6509. ((Entity*)spawn)->SetThreatTransfer(0);
  6510. }
  6511. return 0;
  6512. }
  6513. int EQ2Emu_lua_CureByType(lua_State* state) {
  6514. if(!lua_interface)
  6515. return 0;
  6516. LuaSpell* spell = lua_interface->GetCurrentSpell(state);
  6517. if(!spell){
  6518. lua_interface->LogError("LUA CureByType command error: can only be used in a spell script");
  6519. return 0;
  6520. }
  6521. int8 cure_count = lua_interface->GetInt8Value(state);
  6522. int8 cure_type = lua_interface->GetInt8Value(state, 2);
  6523. string cure_name = lua_interface->GetStringValue(state, 3);
  6524. int8 cure_level = lua_interface->GetInt8Value(state, 4);
  6525. Spawn* target = lua_interface->GetSpawn(state, 5);
  6526. if(target){
  6527. if(!target->IsEntity()){
  6528. lua_interface->LogError("LUA CureByType command error: spawn override must be entity if used");
  6529. return 0;
  6530. }
  6531. if(((Entity*)target)->GetDetTypeCount(cure_type) > 0)
  6532. ((Entity*)target)->CureDetrimentByType(cure_count, cure_type, cure_name.length() > 0 ? cure_name : (string)spell->spell->GetName(), spell->caster, cure_level);
  6533. }
  6534. else {
  6535. ZoneServer* zone = spell->caster->GetZone();
  6536. vector<int32> targets = spell->targets;
  6537. spell->MSpellTargets.readlock(__FUNCTION__, __LINE__);
  6538. for(int8 i=0; i<targets.size(); i++){
  6539. target = zone->GetSpawnByID(targets.at(i));
  6540. if(!target || !target->IsEntity())
  6541. continue;
  6542. if(((Entity*)target)->GetDetTypeCount(cure_type) > 0)
  6543. ((Entity*)target)->CureDetrimentByType(cure_count, cure_type, cure_name.length() > 0 ? cure_name : (string)spell->spell->GetName(), spell->caster, cure_level);
  6544. }
  6545. spell->MSpellTargets.releasereadlock(__FUNCTION__, __LINE__);
  6546. }
  6547. return 0;
  6548. }
  6549. int EQ2Emu_lua_CureByControlEffect(lua_State* state) {
  6550. if(!lua_interface)
  6551. return 0;
  6552. LuaSpell* spell = lua_interface->GetCurrentSpell(state);
  6553. if(!spell){
  6554. lua_interface->LogError("LUA CureByControlEffect command error: can only be used in a spell script");
  6555. return 0;
  6556. }
  6557. int8 cure_count = lua_interface->GetInt8Value(state);
  6558. int8 cure_type = lua_interface->GetInt8Value(state, 2);
  6559. string cure_name = lua_interface->GetStringValue(state, 3);
  6560. int8 cure_level = lua_interface->GetInt8Value(state, 4);
  6561. Spawn* target = lua_interface->GetSpawn(state, 5);
  6562. if(target){
  6563. if(!target->IsEntity()){
  6564. lua_interface->LogError("LUA CureByControlEffect command error: spawn override must be entity if used");
  6565. return 0;
  6566. }
  6567. if(((Entity*)target)->GetDetCount() > 0)
  6568. ((Entity*)target)->CureDetrimentByControlEffect(cure_count, cure_type, cure_name.length() > 0 ? cure_name : (string)spell->spell->GetName(), spell->caster, cure_level);
  6569. }
  6570. else {
  6571. ZoneServer* zone = spell->caster->GetZone();
  6572. vector<int32> targets = spell->targets;
  6573. spell->MSpellTargets.readlock(__FUNCTION__, __LINE__);
  6574. for(int8 i=0; i<targets.size(); i++){
  6575. target = zone->GetSpawnByID(targets.at(i));
  6576. if(!target || !target->IsEntity())
  6577. continue;
  6578. if(((Entity*)target)->GetDetCount() > 0)
  6579. ((Entity*)target)->CureDetrimentByControlEffect(cure_count, cure_type, cure_name.length() > 0 ? cure_name : (string)spell->spell->GetName(), spell->caster, cure_level);
  6580. }
  6581. spell->MSpellTargets.releasereadlock(__FUNCTION__, __LINE__);
  6582. }
  6583. return 0;
  6584. }
  6585. int EQ2Emu_lua_CancelSpell(lua_State* state) {
  6586. if (!lua_interface)
  6587. return 0;
  6588. LuaSpell* spell = lua_interface->GetCurrentSpell(state);
  6589. if (!spell) {
  6590. lua_interface->LogError("LUA CancelSpell command error: can only be use in a spell script");
  6591. return 0;
  6592. }
  6593. if (!spell->caster) {
  6594. lua_interface->LogError("LUA CancelSpell command error: unable to get the caster of the spell");
  6595. return 0;
  6596. }
  6597. if (!spell->caster->GetZone()) {
  6598. lua_interface->LogError("LUA CancelSpell command error: unable to get the zone of the caster");
  6599. return 0;
  6600. }
  6601. spell->caster->GetZone()->GetSpellProcess()->AddSpellCancel(spell);
  6602. return 0;
  6603. }
  6604. int EQ2Emu_lua_RemoveStealth(lua_State* state) {
  6605. if (!lua_interface)
  6606. return 0;
  6607. Spawn* spawn = lua_interface->GetSpawn(state);
  6608. LuaSpell* spell = lua_interface->GetCurrentSpell(state);
  6609. if (!spell){
  6610. lua_interface->LogError("LUA RemoveStealth command error: must be used from spell script");
  6611. return 0;
  6612. }
  6613. if (spawn && spawn->IsEntity())
  6614. ((Entity*)spawn)->RemoveStealthSpell(spell);
  6615. else{
  6616. ZoneServer* zone = spell->caster->GetZone();
  6617. spell->MSpellTargets.readlock(__FUNCTION__, __LINE__);
  6618. for (int32 i = 0; i < spell->targets.size(); i++){
  6619. spawn = zone->GetSpawnByID(spell->targets.at(i));
  6620. if (!spawn || !spawn->IsEntity())
  6621. continue;
  6622. ((Entity*)spawn)->RemoveStealthSpell(spell);
  6623. }
  6624. spell->MSpellTargets.releasereadlock(__FUNCTION__, __LINE__);
  6625. }
  6626. return 0;
  6627. }
  6628. int EQ2Emu_lua_RemoveInvis(lua_State* state) {
  6629. if (!lua_interface)
  6630. return 0;
  6631. Spawn* spawn = lua_interface->GetSpawn(state);
  6632. LuaSpell* spell = lua_interface->GetCurrentSpell(state);
  6633. if (!spell){
  6634. lua_interface->LogError("LUA RemoveInvis command error: must be used from spell script");
  6635. return 0;
  6636. }
  6637. if (spawn && spawn->IsEntity())
  6638. ((Entity*)spawn)->RemoveInvisSpell(spell);
  6639. else{
  6640. ZoneServer* zone = spell->caster->GetZone();
  6641. spell->MSpellTargets.readlock(__FUNCTION__, __LINE__);
  6642. for (int32 i = 0; i < spell->targets.size(); i++){
  6643. spawn = zone->GetSpawnByID(spell->targets.at(i));
  6644. if (!spawn || !spawn->IsEntity())
  6645. continue;
  6646. ((Entity*)spawn)->RemoveInvisSpell(spell);
  6647. }
  6648. spell->MSpellTargets.releasereadlock(__FUNCTION__, __LINE__);
  6649. }
  6650. return 0;
  6651. }
  6652. int EQ2Emu_lua_StartHeroicOpportunity(lua_State* state) {
  6653. if (!lua_interface)
  6654. return 0;
  6655. Spawn* caster = lua_interface->GetSpawn(state);
  6656. int8 class_id = lua_interface->GetInt8Value(state, 2);
  6657. if (!caster) {
  6658. lua_interface->LogError("LUA StartHeroicOpportunity command error: caster is not valid");
  6659. return 0;
  6660. }
  6661. if (!caster->IsPlayer()) {
  6662. lua_interface->LogError("LUA StartHeroicOpportunity command error: caster must be a player");
  6663. return 0;
  6664. }
  6665. Spawn* target = caster->GetTarget();
  6666. if (!target) {
  6667. lua_interface->LogError("LUA StartHeroicOpportunity command error: target is not valid");
  6668. return 0;
  6669. }
  6670. Client* client = caster->GetZone()->GetClientBySpawn(caster);
  6671. if (!client) {
  6672. lua_interface->LogError("LUA StartHeroicOpportunity command error: unable to get a client for the given caster");
  6673. return 0;
  6674. }
  6675. HeroicOP* ho = master_ho_list.GetHeroicOP(class_id);
  6676. if (ho) {
  6677. ho->SetTarget(target->GetID());
  6678. LogWrite(SPELL__ERROR, 0, "HO", "caster: %u", caster->GetID());
  6679. LogWrite(SPELL__ERROR, 0, "HO", "target: %u", target->GetID());
  6680. if (((Entity*)caster)->GetGroupMemberInfo()) {
  6681. if (caster->GetZone()->GetSpellProcess()->AddHO(client, ho)) {
  6682. world.GetGroupManager()->GroupLock(__FUNCTION__, __LINE__);
  6683. deque<GroupMemberInfo*>::iterator itr;
  6684. deque<GroupMemberInfo*>* members = world.GetGroupManager()->GetGroupMembers(((Entity*)caster)->GetGroupMemberInfo()->group_id);
  6685. for (itr = members->begin(); itr != members->end(); itr++) {
  6686. if ((*itr)->client)
  6687. ClientPacketFunctions::SendHeroicOPUpdate((*itr)->client, ho);
  6688. }
  6689. world.GetGroupManager()->ReleaseGroupLock(__FUNCTION__, __LINE__);
  6690. }
  6691. else
  6692. safe_delete(ho);
  6693. }
  6694. else {
  6695. if (caster->GetZone()->GetSpellProcess()->AddHO(client, ho)) {
  6696. ClientPacketFunctions::SendHeroicOPUpdate(client, ho);
  6697. }
  6698. else
  6699. safe_delete(ho);
  6700. }
  6701. }
  6702. return 0;
  6703. }
  6704. int EQ2Emu_lua_SetSpellTriggerCount(lua_State* state){
  6705. if (!lua_interface)
  6706. return 0;
  6707. LuaSpell* spell = lua_interface->GetCurrentSpell(state);
  6708. if (!spell){
  6709. lua_interface->LogError("LUA SetSpellTriggerCount command error: you must use this function in a spellscript!");
  6710. return 0;
  6711. }
  6712. int16 triggerCount = lua_interface->GetInt16Value(state);
  6713. bool cancel_after_triggers = (lua_interface->GetInt8Value(state, 2) == 1);
  6714. if (!triggerCount){
  6715. lua_interface->LogError("LUA SetSpellTriggerCount command error: used trigger value equals zero!");
  6716. return 0;
  6717. }
  6718. spell->num_triggers = triggerCount;
  6719. spell->had_triggers = true;
  6720. spell->cancel_after_all_triggers = cancel_after_triggers;
  6721. return 0;
  6722. }
  6723. int EQ2Emu_lua_GetSpellTriggerCount(lua_State* state){
  6724. if (!lua_interface)
  6725. return 0;
  6726. LuaSpell* spell = lua_interface->GetCurrentSpell(state);
  6727. if (!spell){
  6728. lua_interface->LogError("LUA GetSpellTriggerCount command error: you must use this function in a spellscript!");
  6729. return 0;
  6730. }
  6731. lua_interface->SetInt32Value(state, spell->num_triggers);
  6732. return 1;
  6733. }
  6734. int EQ2Emu_lua_RemoveTriggerFromSpell(lua_State* state){
  6735. if (!lua_interface)
  6736. return 0;
  6737. LuaSpell* spell = lua_interface->GetCurrentSpell(state);
  6738. if (!spell){
  6739. lua_interface->LogError("LUA RemoveTriggerFromSpell command error: you must use this function in a spellscript!");
  6740. return 0;
  6741. }
  6742. int16 remove_count = lua_interface->GetInt16Value(state);
  6743. if (!remove_count)
  6744. remove_count = 1;
  6745. if (remove_count >= spell->num_triggers){
  6746. spell->num_triggers = 0;
  6747. if (spell->cancel_after_all_triggers)
  6748. spell->caster->GetZone()->GetSpellProcess()->AddSpellCancel(spell);
  6749. }
  6750. else{
  6751. spell->num_triggers -= remove_count;
  6752. ClientPacketFunctions::SendMaintainedExamineUpdate(spell->caster->GetZone()->GetClientBySpawn(spell->caster), spell->slot_pos, spell->num_triggers, 0);
  6753. }
  6754. return 0;
  6755. }
  6756. int EQ2Emu_lua_CopySpawnAppearance(lua_State* state){
  6757. if (!lua_interface)
  6758. return 0;
  6759. Spawn* spawn = lua_interface->GetSpawn(state);
  6760. Spawn* copy_spawn = lua_interface->GetSpawn(state, 2);
  6761. if (!spawn){
  6762. lua_interface->LogError("LUA CopySpawnAppearance command error: the first spawn used was not valid!");
  6763. return 0;
  6764. }
  6765. if (!copy_spawn){
  6766. lua_interface->LogError("LUA CopySpawnAppearance command error: the second spawn used was not valid!");
  6767. return 0;
  6768. }
  6769. spawn->CopySpawnAppearance(copy_spawn);
  6770. return 0;
  6771. }
  6772. int EQ2Emu_lua_AddImmunitySpell(lua_State* state){
  6773. if (!lua_interface)
  6774. return 0;
  6775. LuaSpell* spell = lua_interface->GetCurrentSpell(state);
  6776. int8 type = lua_interface->GetInt8Value(state);
  6777. Spawn* spawn = lua_interface->GetSpawn(state, 2);
  6778. if (!spell){
  6779. lua_interface->LogError("LUA AddImmunitySpell command error: This must be used in a spellscript");
  6780. return 0;
  6781. }
  6782. if (spawn){
  6783. if (!spawn->IsEntity()){
  6784. lua_interface->LogError("LUA AddImmunitySpell command error: The spawn provided is not an entity");
  6785. return 0;
  6786. }
  6787. Entity* entity = ((Entity*)spawn);
  6788. switch (type){
  6789. case IMMUNITY_TYPE_AOE:
  6790. entity->AddAOEImmunity(spell);
  6791. if (!(spell->effect_bitmask & EFFECT_FLAG_AOE_IMMUNE))
  6792. spell->effect_bitmask += EFFECT_FLAG_AOE_IMMUNE;
  6793. break;
  6794. case IMMUNITY_TYPE_STUN:
  6795. entity->AddStunImmunity(spell);
  6796. if (!(spell->effect_bitmask & EFFECT_FLAG_STUN_IMMUNE))
  6797. spell->effect_bitmask += EFFECT_FLAG_STUN_IMMUNE;
  6798. break;
  6799. case IMMUNITY_TYPE_ROOT:
  6800. entity->AddRootImmunity(spell);
  6801. if (!(spell->effect_bitmask & EFFECT_FLAG_ROOT_IMMUNE))
  6802. spell->effect_bitmask += EFFECT_FLAG_ROOT_IMMUNE;
  6803. break;
  6804. case IMMUNITY_TYPE_DAZE:
  6805. entity->AddDazeImmunity(spell);
  6806. if (!(spell->effect_bitmask & EFFECT_FLAG_DAZE_IMMUNE))
  6807. spell->effect_bitmask += EFFECT_FLAG_DAZE_IMMUNE;
  6808. break;
  6809. case IMMUNITY_TYPE_FEAR:
  6810. entity->AddFearImmunity(spell);
  6811. if (!(spell->effect_bitmask & EFFECT_FLAG_FEAR_IMMUNE))
  6812. spell->effect_bitmask += EFFECT_FLAG_FEAR_IMMUNE;
  6813. break;
  6814. case IMMUNITY_TYPE_MEZ:
  6815. entity->AddMezImmunity(spell);
  6816. if (!(spell->effect_bitmask & EFFECT_FLAG_MEZ_IMMUNE))
  6817. spell->effect_bitmask += EFFECT_FLAG_MEZ_IMMUNE;
  6818. break;
  6819. case IMMUNITY_TYPE_STIFLE:
  6820. entity->AddStifleImmunity(spell);
  6821. if (!(spell->effect_bitmask & EFFECT_FLAG_STIFLE_IMMUNE))
  6822. spell->effect_bitmask += EFFECT_FLAG_STIFLE_IMMUNE;
  6823. break;
  6824. default:
  6825. lua_interface->LogError("LUA AddImmunitySpell command error: invalid immunity type");
  6826. }
  6827. }
  6828. else {
  6829. bool should_break = false;
  6830. spell->MSpellTargets.readlock(__FUNCTION__, __LINE__);
  6831. for (int8 i = 0; i < spell->targets.size(); i++){
  6832. spawn = spell->caster->GetZone()->GetSpawnByID(spell->targets.at(i));
  6833. if (!spawn || !spawn->IsEntity())
  6834. continue;
  6835. Entity* entity = ((Entity*)spawn);
  6836. switch (type){
  6837. case IMMUNITY_TYPE_AOE:
  6838. entity->AddAOEImmunity(spell);
  6839. if (!(spell->effect_bitmask & EFFECT_FLAG_AOE_IMMUNE))
  6840. spell->effect_bitmask += EFFECT_FLAG_AOE_IMMUNE;
  6841. break;
  6842. case IMMUNITY_TYPE_STUN:
  6843. entity->AddStunImmunity(spell);
  6844. if (!(spell->effect_bitmask & EFFECT_FLAG_STUN_IMMUNE))
  6845. spell->effect_bitmask += EFFECT_FLAG_STUN_IMMUNE;
  6846. break;
  6847. case IMMUNITY_TYPE_ROOT:
  6848. entity->AddRootImmunity(spell);
  6849. if (!(spell->effect_bitmask & EFFECT_FLAG_ROOT_IMMUNE))
  6850. spell->effect_bitmask += EFFECT_FLAG_ROOT_IMMUNE;
  6851. break;
  6852. case IMMUNITY_TYPE_DAZE:
  6853. entity->AddDazeImmunity(spell);
  6854. if (!(spell->effect_bitmask & EFFECT_FLAG_DAZE_IMMUNE))
  6855. spell->effect_bitmask += EFFECT_FLAG_DAZE_IMMUNE;
  6856. break;
  6857. case IMMUNITY_TYPE_FEAR:
  6858. entity->AddFearImmunity(spell);
  6859. if (!(spell->effect_bitmask & EFFECT_FLAG_FEAR_IMMUNE))
  6860. spell->effect_bitmask += EFFECT_FLAG_FEAR_IMMUNE;
  6861. break;
  6862. case IMMUNITY_TYPE_MEZ:
  6863. entity->AddMezImmunity(spell);
  6864. if (!(spell->effect_bitmask & EFFECT_FLAG_MEZ_IMMUNE))
  6865. spell->effect_bitmask += EFFECT_FLAG_MEZ_IMMUNE;
  6866. break;
  6867. case IMMUNITY_TYPE_STIFLE:
  6868. entity->AddStifleImmunity(spell);
  6869. if (!(spell->effect_bitmask & EFFECT_FLAG_STIFLE_IMMUNE))
  6870. spell->effect_bitmask += EFFECT_FLAG_STIFLE_IMMUNE;
  6871. break;
  6872. default:
  6873. lua_interface->LogError("LUA AddImmunitySpell command error: invalid immunity type");
  6874. should_break = true;
  6875. }
  6876. if (should_break)
  6877. break;
  6878. }
  6879. spell->MSpellTargets.releasereadlock(__FUNCTION__, __LINE__);
  6880. }
  6881. return 0;
  6882. }
  6883. int EQ2Emu_lua_RemoveImmunitySpell(lua_State* state){
  6884. if (!lua_interface)
  6885. return 0;
  6886. LuaSpell* spell = lua_interface->GetCurrentSpell(state);
  6887. int8 type = lua_interface->GetInt8Value(state);
  6888. Spawn* spawn = lua_interface->GetSpawn(state, 2);
  6889. if (!spell){
  6890. lua_interface->LogError("LUA RemoveImmunitySpell command error: This must be used in a spellscript");
  6891. return 0;
  6892. }
  6893. if (spawn){
  6894. if (!spawn->IsEntity()){
  6895. lua_interface->LogError("LUA RemoveImmunitySpell command error: The spawn provided is not an entity");
  6896. return 0;
  6897. }
  6898. Entity* entity = ((Entity*)spawn);
  6899. switch (type){
  6900. case IMMUNITY_TYPE_AOE:
  6901. entity->RemoveAOEImmunity(spell);
  6902. break;
  6903. case IMMUNITY_TYPE_STUN:
  6904. entity->RemoveStunImmunity(spell);
  6905. break;
  6906. case IMMUNITY_TYPE_ROOT:
  6907. entity->RemoveRootImmunity(spell);
  6908. break;
  6909. case IMMUNITY_TYPE_DAZE:
  6910. entity->RemoveDazeImmunity(spell);
  6911. break;
  6912. case IMMUNITY_TYPE_FEAR:
  6913. entity->RemoveFearImmunity(spell);
  6914. break;
  6915. case IMMUNITY_TYPE_MEZ:
  6916. entity->RemoveMezImmunity(spell);
  6917. break;
  6918. case IMMUNITY_TYPE_STIFLE:
  6919. entity->RemoveStifleImmunity(spell);
  6920. break;
  6921. default:
  6922. lua_interface->LogError("LUA RemoveImmunitySpell command error: invalid immunity type");
  6923. }
  6924. }
  6925. else {
  6926. bool should_break = false;
  6927. spell->MSpellTargets.readlock(__FUNCTION__, __LINE__);
  6928. for (int8 i = 0; i < spell->targets.size(); i++){
  6929. spawn = spell->caster->GetZone()->GetSpawnByID(spell->targets.at(i));
  6930. if (!spawn || !spawn->IsEntity())
  6931. continue;
  6932. Entity* entity = ((Entity*)spawn);
  6933. switch (type){
  6934. case IMMUNITY_TYPE_AOE:
  6935. entity->RemoveAOEImmunity(spell);
  6936. break;
  6937. case IMMUNITY_TYPE_STUN:
  6938. entity->RemoveStunImmunity(spell);
  6939. break;
  6940. case IMMUNITY_TYPE_ROOT:
  6941. entity->RemoveRootImmunity(spell);
  6942. break;
  6943. case IMMUNITY_TYPE_DAZE:
  6944. entity->RemoveDazeImmunity(spell);
  6945. break;
  6946. case IMMUNITY_TYPE_FEAR:
  6947. entity->RemoveFearImmunity(spell);
  6948. break;
  6949. case IMMUNITY_TYPE_MEZ:
  6950. entity->RemoveMezImmunity(spell);
  6951. break;
  6952. case IMMUNITY_TYPE_STIFLE:
  6953. entity->RemoveStifleImmunity(spell);
  6954. break;
  6955. default:
  6956. lua_interface->LogError("LUA RemoveImmunitySpell command error: invalid immunity type");
  6957. should_break = true;
  6958. }
  6959. if (should_break)
  6960. break;
  6961. }
  6962. spell->MSpellTargets.releasereadlock(__FUNCTION__, __LINE__);
  6963. }
  6964. return 0;
  6965. }
  6966. int EQ2Emu_lua_SetSpellSnareValue(lua_State* state) {
  6967. if (!lua_interface)
  6968. return 0;
  6969. LuaSpell* spell = lua_interface->GetCurrentSpell(state);
  6970. if (!spell) {
  6971. lua_interface->LogError("LUA SetSpellSnareValue command error: This can only be used in a spell script!");
  6972. return 0;
  6973. }
  6974. float snare = lua_interface->GetFloatValue(state);
  6975. Spawn* spawn = lua_interface->GetSpawn(state, 2);
  6976. // convert the val to the speed multipler value (100 - val)
  6977. float val = 100.0 - snare;
  6978. val /= 100.0;
  6979. if (spawn) {
  6980. if (!spawn->IsEntity()) {
  6981. lua_interface->LogError("LUA SetSpellSnareValue command error: spawn must be an entity.");
  6982. return 0;
  6983. }
  6984. ((Entity*)spawn)->SetSnareValue(spell, val);
  6985. }
  6986. else {
  6987. spell->MSpellTargets.readlock(__FUNCTION__, __LINE__);
  6988. for (int8 i = 0; i < spell->targets.size(); i++){
  6989. spawn = spell->caster->GetZone()->GetSpawnByID(spell->targets.at(i));
  6990. if (!spawn || !spawn->IsEntity())
  6991. continue;
  6992. ((Entity*)spawn)->SetSnareValue(spell, val);
  6993. }
  6994. spell->MSpellTargets.releasereadlock(__FUNCTION__, __LINE__);
  6995. }
  6996. return 0;
  6997. }
  6998. int EQ2Emu_lua_CheckRaceType(lua_State* state) {
  6999. if (!lua_interface)
  7000. return 0;
  7001. Spawn* spawn = lua_interface->GetSpawn(state);
  7002. int16 race_id = lua_interface->GetInt16Value(state, 2);
  7003. if (!spawn) {
  7004. lua_interface->LogError("LUA CheckRaceType command error: spawn is not valid");
  7005. return 0;
  7006. }
  7007. if (race_id == 0) {
  7008. lua_interface->LogError("LUA CheckRaceType command error: race id must be set");
  7009. return 0;
  7010. }
  7011. lua_interface->SetBooleanValue(state, (race_id == race_types_list.GetRaceType(spawn->GetModelType()) || race_id == race_types_list.GetRaceBaseType(spawn->GetModelType())));
  7012. return 1;
  7013. }
  7014. int EQ2Emu_lua_GetRaceType(lua_State* state) {
  7015. if (!lua_interface)
  7016. return 0;
  7017. Spawn* spawn = lua_interface->GetSpawn(state);
  7018. if (!spawn) {
  7019. lua_interface->LogError("LUA GetRaceType command error: spawn is not valid");
  7020. return 0;
  7021. }
  7022. lua_interface->SetInt32Value(state, race_types_list.GetRaceType(spawn->GetModelType()));
  7023. return 1;
  7024. }
  7025. int EQ2Emu_lua_GetRaceBaseType(lua_State* state) {
  7026. if (!lua_interface)
  7027. return 0;
  7028. Spawn* spawn = lua_interface->GetSpawn(state);
  7029. if (!spawn) {
  7030. lua_interface->LogError("LUA GetRaceBaseType command error: spawn is not valid");
  7031. return 0;
  7032. }
  7033. lua_interface->SetInt32Value(state, race_types_list.GetRaceBaseType(spawn->GetModelType()));
  7034. return 1;
  7035. }
  7036. int EQ2Emu_lua_GetSpellName(lua_State* state) {
  7037. if (!lua_interface)
  7038. return 0;
  7039. LuaSpell* spell = lua_interface->GetCurrentSpell(state);
  7040. if (!spell){
  7041. lua_interface->LogError("LUA GetSpellName command error: this function must be used from a spell script!");
  7042. return 0;
  7043. }
  7044. lua_interface->SetStringValue(state, spell->spell->GetName());
  7045. return 1;
  7046. }
  7047. int EQ2Emu_lua_GetQuestFlags(lua_State* state) {
  7048. if (!lua_interface)
  7049. return 0;
  7050. Quest* quest = lua_interface->GetQuest(state);
  7051. if (!quest) {
  7052. lua_interface->LogError("LUA GetQuestFlags command error: quest is not valid.");
  7053. return 0;
  7054. }
  7055. lua_interface->SetInt32Value(state, quest->GetQuestFlags());
  7056. return 1;
  7057. }
  7058. int EQ2Emu_lua_SetQuestFlags(lua_State* state) {
  7059. if (!lua_interface)
  7060. return 0;
  7061. Quest* quest = lua_interface->GetQuest(state);
  7062. int32 flags = lua_interface->GetInt32Value(state, 2);
  7063. if (!quest) {
  7064. lua_interface->LogError("LUA SetQuestFlags command error: quest is not valid.");
  7065. return 0;
  7066. }
  7067. quest->SetQuestFlags(flags);
  7068. return 0;
  7069. }
  7070. int EQ2Emu_lua_SetQuestTimer(lua_State* state) {
  7071. if (!lua_interface)
  7072. return 0;
  7073. Quest* quest = lua_interface->GetQuest(state);
  7074. Spawn* player = lua_interface->GetSpawn(state, 2);
  7075. int32 step = lua_interface->GetInt32Value(state, 3);
  7076. int32 duration = lua_interface->GetInt32Value(state, 4);
  7077. string action = lua_interface->GetStringValue(state, 5);
  7078. if (!quest) {
  7079. lua_interface->LogError("LUA SetQuestTimer command error: quest is not valid.");
  7080. return 0;
  7081. }
  7082. if (!player) {
  7083. lua_interface->LogError("LUA SetQuestTimer command error: player is not a valid spawn.");
  7084. return 0;
  7085. }
  7086. if (!player->IsPlayer()) {
  7087. lua_interface->LogError("LUA SetQuestTimer command error: player is not a valid player.");
  7088. return 0;
  7089. }
  7090. if (step == 0) {
  7091. lua_interface->LogError("LUA SetQuestTimer command error: step must be set.");
  7092. return 0;
  7093. }
  7094. if (duration == 0) {
  7095. lua_interface->LogError("LUA SetQuestTimer command error: duration must be set.");
  7096. return 0;
  7097. }
  7098. if (action.length() == 0) {
  7099. lua_interface->LogError("LUA SetQuestTimer command error: failed action must be set.");
  7100. return 0;
  7101. }
  7102. Client* client = player->GetZone()->GetClientBySpawn(player);
  7103. if (!client) {
  7104. lua_interface->LogError("LUA SetQuestTimer command error: failed to get a valid client pointer for the given player");
  7105. return 0;
  7106. }
  7107. quest->SetTimerStep(step);
  7108. quest->AddFailedAction(step, action);
  7109. quest->SetStepTimer(duration);
  7110. client->AddQuestTimer(quest->GetQuestID());
  7111. return 0;
  7112. }
  7113. int EQ2Emu_lua_SetQuestTimerComplete(lua_State* state) {
  7114. if (!lua_interface)
  7115. return 0;
  7116. Quest* quest = lua_interface->GetQuest(state);
  7117. Spawn* player = lua_interface->GetSpawn(state, 2);
  7118. if (!quest) {
  7119. lua_interface->LogError("LUA SetQuestTimerComplete command error: quest is not valid.");
  7120. return 0;
  7121. }
  7122. if (!player) {
  7123. lua_interface->LogError("LUA SetQuestTimerComplete command error: player is not a valid spawn.");
  7124. return 0;
  7125. }
  7126. if (!player->IsPlayer()) {
  7127. lua_interface->LogError("LUA SetQuestTimerComplete command error: player is not a valid player.");
  7128. return 0;
  7129. }
  7130. Client* client = player->GetZone()->GetClientBySpawn(player);
  7131. if (!client) {
  7132. lua_interface->LogError("LUA SetQuestTimerComplete command error: failed to get a valid client pointer for the given player");
  7133. return 0;
  7134. }
  7135. quest->SetTimerStep(0);
  7136. quest->SetStepTimer(0);
  7137. client->RemoveQuestTimer(quest->GetQuestID());
  7138. return 0;
  7139. }
  7140. int EQ2Emu_lua_RemoveQuestStep(lua_State* state) {
  7141. if (!lua_interface)
  7142. return 0;
  7143. Spawn* player = lua_interface->GetSpawn(state);
  7144. Quest* quest = lua_interface->GetQuest(state, 2);
  7145. int32 step = lua_interface->GetInt32Value(state, 3);
  7146. if (!player) {
  7147. lua_interface->LogError("LUA RemoveQuestStep command error: player is not a valid spawn.");
  7148. return 0;
  7149. }
  7150. if (!player->IsPlayer()) {
  7151. lua_interface->LogError("LUA RemoveQuestStep command error: player is not a valid player.");
  7152. return 0;
  7153. }
  7154. if (!quest) {
  7155. lua_interface->LogError("LUA RemoveQuestStep command error: quest is not valid.");
  7156. return 0;
  7157. }
  7158. if (step == 0) {
  7159. lua_interface->LogError("LUA RemoveQuestStep command error: step must be set.");
  7160. return 0;
  7161. }
  7162. Client* client = player->GetZone()->GetClientBySpawn(player);
  7163. if (!client) {
  7164. lua_interface->LogError("LUA RemoveQuestStep command error: unable to get a valid client pointer from the given player.");
  7165. return 0;
  7166. }
  7167. if (quest->RemoveQuestStep(step, client)) {
  7168. client->QueuePacket(quest->QuestJournalReply(client->GetVersion(), client->GetNameCRC(), (Player*)player, 0, 0, 0, true));
  7169. client->GetCurrentZone()->SendQuestUpdates(client);
  7170. }
  7171. else
  7172. lua_interface->LogError("LUA RemoveQuestStep command error: unable to remove the step (%u) from the quest (%s).", step, quest->GetName());
  7173. return 0;
  7174. }
  7175. int EQ2Emu_lua_ResetQuestStep(lua_State* state) {
  7176. if (!lua_interface)
  7177. return 0;
  7178. Quest* quest = lua_interface->GetQuest(state, 1);
  7179. int32 step = lua_interface->GetInt32Value(state, 2);
  7180. string desc = lua_interface->GetStringValue(state, 3);
  7181. string task_group = lua_interface->GetStringValue(state, 4);
  7182. if (!quest) {
  7183. lua_interface->LogError("LUA ResetQuestStep command error: quest is not valid.");
  7184. return 0;
  7185. }
  7186. if (step == 0) {
  7187. lua_interface->LogError("LUA ResetQuestStep command error: step must be set.");
  7188. return 0;
  7189. }
  7190. QuestStep* quest_step = quest->GetQuestStep(step);
  7191. if (!quest_step) {
  7192. lua_interface->LogError("LUA ResetQuestStep command error: unable to get a valid quest step.");
  7193. return 0;
  7194. }
  7195. quest_step->SetStepProgress(0);
  7196. quest_step->SetTaskGroup(task_group);
  7197. quest_step->SetDescription(desc);
  7198. return 0;
  7199. }
  7200. int EQ2Emu_lua_AddQuestStepFailureAction(lua_State* state) {
  7201. if (!lua_interface)
  7202. return 0;
  7203. Quest* quest = lua_interface->GetQuest(state);
  7204. int32 step = lua_interface->GetInt32Value(state, 2);
  7205. string action = lua_interface->GetStringValue(state, 3);
  7206. if (!quest) {
  7207. lua_interface->LogError("LUA AddQuestStepFailureAction command error: quest is not valid.");
  7208. return 0;
  7209. }
  7210. if (step == 0) {
  7211. lua_interface->LogError("LUA AddQuestStepFailureAction command error: step must be set.");
  7212. return 0;
  7213. }
  7214. if (action.length() == 0) {
  7215. lua_interface->LogError("LUA AddQuestStepFailureAction command error: action must be set.");
  7216. return 0;
  7217. }
  7218. quest->AddFailedAction(step, action);
  7219. return 0;
  7220. }
  7221. int EQ2Emu_lua_SetStepFailed(lua_State* state) {
  7222. if (!lua_interface)
  7223. return 0;
  7224. Spawn* player = lua_interface->GetSpawn(state);
  7225. int32 quest_id = lua_interface->GetInt32Value(state, 2);
  7226. int32 step = lua_interface->GetInt32Value(state, 3);
  7227. if (!player) {
  7228. lua_interface->LogError("LUA SetStepFailed command error: player is not a valid spawn.");
  7229. return 0;
  7230. }
  7231. if (!player->IsPlayer()) {
  7232. lua_interface->LogError("LUA SetStepFailed command error: player is not a valid player.");
  7233. return 0;
  7234. }
  7235. if (quest_id == 0) {
  7236. lua_interface->LogError("LUA SetStepFailed command error: quest_id must be set.");
  7237. return 0;
  7238. }
  7239. if (step == 0) {
  7240. lua_interface->LogError("LUA SetStepFailed command error: step must be set.");
  7241. return 0;
  7242. }
  7243. Quest* quest = ((Player*)player)->GetQuest(quest_id);
  7244. if (!quest) {
  7245. lua_interface->LogError("LUA SetStepFailed command error: unable to get a valid quest from the given id (%u).", quest_id);
  7246. return 0;
  7247. }
  7248. quest->StepFailed(step);
  7249. return 0;
  7250. }
  7251. int EQ2Emu_lua_GetQuestCompleteCount(lua_State* state) {
  7252. if (!lua_interface)
  7253. return 0;
  7254. Spawn* player = lua_interface->GetSpawn(state);
  7255. int32 quest_id = lua_interface->GetInt32Value(state, 2);
  7256. if (!player) {
  7257. lua_interface->LogError("LUA GetQuestCompleteCount command error: player is not a valid spawn");
  7258. return 0;
  7259. }
  7260. if (!player->IsPlayer()) {
  7261. lua_interface->LogError("LUA GetQuestCompleteCount command error: player is not a valid player");
  7262. return 0;
  7263. }
  7264. if (quest_id == 0) {
  7265. lua_interface->LogError("LUA GetQuestCompleteCount command error: quest id is not valid");
  7266. return 0;
  7267. }
  7268. Quest* quest = ((Player*)player)->GetCompletedQuest(quest_id);
  7269. if (!quest) {
  7270. lua_interface->SetInt32Value(state, 0);
  7271. return 1;
  7272. }
  7273. lua_interface->SetInt32Value(state, quest->GetCompleteCount());
  7274. return 1;
  7275. }
  7276. int EQ2Emu_lua_SetServerVariable(lua_State* state) {
  7277. if (!lua_interface)
  7278. return 0;
  7279. string name = lua_interface->GetStringValue(state);
  7280. string value = lua_interface->GetStringValue(state, 2);
  7281. string comment = lua_interface->GetStringValue(state, 3);
  7282. if (name.length() == 0) {
  7283. lua_interface->LogError("LUA SetServerVariable command error: name is not valid");
  7284. return 0;
  7285. }
  7286. if (value.length() == 0) {
  7287. lua_interface->LogError("LUA SetServerVariable command error: value is not valid");
  7288. return 0;
  7289. }
  7290. string varname = string("lua_").append(name);
  7291. Variable* var = variables.FindVariable(varname);
  7292. if (var)
  7293. var->SetValue(value.c_str());
  7294. else {
  7295. var = new Variable(varname.c_str(), value.c_str(), comment.c_str());
  7296. variables.AddVariable(var);
  7297. }
  7298. database.SaveVariable(var->GetName(), var->GetValue(), var->GetComment());
  7299. return 0;
  7300. }
  7301. int EQ2Emu_lua_GetServerVariable(lua_State* state) {
  7302. if (!lua_interface)
  7303. return 0;
  7304. string name = lua_interface->GetStringValue(state);
  7305. if (name.length() == 0) {
  7306. lua_interface->LogError("LUA GetServerVariable command error: name is not valid");
  7307. return 0;
  7308. }
  7309. string varname = string("lua_").append(name);
  7310. Variable* var = variables.FindVariable(varname);
  7311. if (var)
  7312. lua_interface->SetStringValue(state, var->GetValue());
  7313. else
  7314. lua_interface->SetStringValue(state, "NULL");
  7315. return 1;
  7316. }
  7317. int EQ2Emu_lua_HasLanguage(lua_State* state) {
  7318. if (!lua_interface)
  7319. return 0;
  7320. Spawn* player = lua_interface->GetSpawn(state);
  7321. int32 language_id = lua_interface->GetInt32Value(state, 2);
  7322. if (!player) {
  7323. lua_interface->LogError("LUA HasLanguage command error: player is not valid");
  7324. return 0;
  7325. }
  7326. if (!player->IsPlayer()) {
  7327. lua_interface->LogError("LUA HasLanguage command error: player is not a valid player");
  7328. return 0;
  7329. }
  7330. lua_interface->SetBooleanValue(state, ((Player*)player)->HasLanguage(language_id));
  7331. return 1;
  7332. }
  7333. int EQ2Emu_lua_AddLanguage(lua_State* state) {
  7334. if (!lua_interface)
  7335. return 0;
  7336. Spawn* player = lua_interface->GetSpawn(state);
  7337. int32 language_id = lua_interface->GetInt32Value(state, 2);
  7338. if (!player) {
  7339. lua_interface->LogError("LUA AddLanguage command error: player is not valid");
  7340. return 0;
  7341. }
  7342. if (!player->IsPlayer()) {
  7343. lua_interface->LogError("LUA AddLanguage command error: player is not a valid player");
  7344. return 0;
  7345. }
  7346. Language* language = master_languages_list.GetLanguage(language_id);
  7347. if (language)
  7348. ((Player*)player)->AddLanguage(language->GetID(), language->GetName(), true);
  7349. return 0;
  7350. }
  7351. int EQ2Emu_lua_IsNight(lua_State* state) {
  7352. if (!lua_interface)
  7353. return 0;
  7354. ZoneServer* zone = lua_interface->GetZone(state);
  7355. if (!zone) {
  7356. lua_interface->LogError("LUA IsNight command error: zone is not valid");
  7357. return 0;
  7358. }
  7359. lua_interface->SetBooleanValue(state, zone->IsDusk());
  7360. return 1;
  7361. }
  7362. int EQ2Emu_lua_AddMultiFloorLift(lua_State* state) {
  7363. if (!lua_interface)
  7364. return 0;
  7365. Spawn* spawn = lua_interface->GetSpawn(state);
  7366. if (!spawn) {
  7367. lua_interface->LogError("LUA AddMultiFloorLift command error: spawn is not valid");
  7368. return 0;
  7369. }
  7370. if (!spawn->IsWidget()) {
  7371. lua_interface->LogError("LUA AddMultiFloorLift command error: spawn is not a widget");
  7372. return 0;
  7373. }
  7374. ((Widget*)spawn)->SetMultiFloorLift(true);
  7375. if (spawn->GetZone())
  7376. spawn->GetZone()->AddTransportSpawn(spawn);
  7377. return 0;
  7378. }
  7379. int EQ2Emu_lua_StartAutoMount(lua_State* state) {
  7380. if (!lua_interface)
  7381. return 0;
  7382. Spawn* player = lua_interface->GetSpawn(state);
  7383. int32 path = lua_interface->GetInt32Value(state, 2);
  7384. if (!player) {
  7385. lua_interface->LogError("LUA StartAutoMount command error: spawn is not valid");
  7386. return 0;
  7387. }
  7388. if (!player->IsPlayer()) {
  7389. lua_interface->LogError("LUA StartAutoMount command error: spawn is not a player");
  7390. return 0;
  7391. }
  7392. if (path == 0) {
  7393. lua_interface->LogError("LUA StartAutoMount command error: path must be greater then zero");
  7394. return 0;
  7395. }
  7396. Client* client = player->GetZone()->GetClientBySpawn(player);
  7397. if (!client) {
  7398. lua_interface->LogError("LUA StartAutoMount command error: unable to get a client from the given player");
  7399. return 0;
  7400. }
  7401. client->SetPendingFlightPath(path);
  7402. ((Player*)player)->SetTempMount(((Entity*)player)->GetMount());
  7403. ((Player*)player)->SetTempMountColor(((Entity*)player)->GetMountColor());
  7404. ((Player*)player)->SetTempMountSaddleColor(((Entity*)player)->GetMountSaddleColor());
  7405. PacketStruct* packet = configReader.getStruct("WS_ReadyForTakeOff", client->GetVersion());
  7406. if (packet) {
  7407. client->QueuePacket(packet->serialize());
  7408. safe_delete(packet);
  7409. }
  7410. return 0;
  7411. }
  7412. int EQ2Emu_lua_EndAutoMount(lua_State* state) {
  7413. if (!lua_interface)
  7414. return 0;
  7415. Spawn* player = lua_interface->GetSpawn(state);
  7416. if (!player) {
  7417. lua_interface->LogError("LUA EndAutoMount command error: spawn is not valid");
  7418. return 0;
  7419. }
  7420. if (!player->IsPlayer()) {
  7421. lua_interface->LogError("LUA EndAutoMount command error: spawn is not a player");
  7422. return 0;
  7423. }
  7424. Client* client = player->GetZone()->GetClientBySpawn(player);
  7425. if (!client) {
  7426. lua_interface->LogError("LUA EndAutoMount command error: unable to get a client from the given player");
  7427. return 0;
  7428. }
  7429. client->EndAutoMount();
  7430. return 0;
  7431. }
  7432. int EQ2Emu_lua_IsOnAutoMount(lua_State* state) {
  7433. if (!lua_interface)
  7434. return 0;
  7435. Spawn* player = lua_interface->GetSpawn(state);
  7436. if (!player) {
  7437. lua_interface->LogError("LUA IsOnAutoMount command error: spawn in not valid");
  7438. return 0;
  7439. }
  7440. if (!player->IsPlayer()) {
  7441. lua_interface->LogError("LUA IsOnAutoMount command error: spawn is not a player");
  7442. return 0;
  7443. }
  7444. Client* client = player->GetZone()->GetClientBySpawn(player);
  7445. if (!client) {
  7446. lua_interface->LogError("LUA IsOnAutoMount command error: unable to get a client from the given player");
  7447. return 0;
  7448. }
  7449. lua_interface->SetBooleanValue(state, client->GetOnAutoMount());
  7450. return 1;
  7451. }
  7452. int EQ2Emu_lua_SetPlayerHistory(lua_State* state) {
  7453. if (!lua_interface)
  7454. return 0;
  7455. Spawn* player = lua_interface->GetSpawn(state);
  7456. int32 event_id = lua_interface->GetInt32Value(state, 2);
  7457. int32 value = lua_interface->GetInt32Value(state, 3);
  7458. int32 value2 = lua_interface->GetInt32Value(state, 4);
  7459. if (!player) {
  7460. lua_interface->LogError("LUA SetPlayerHistory command error: spawn is not valid");
  7461. return 0;
  7462. }
  7463. if (!player->IsPlayer()) {
  7464. lua_interface->LogError("LUA SetPlayerHistory command error: spawn is not a player");
  7465. return 0;
  7466. }
  7467. ((Player*)player)->UpdateLUAHistory(event_id, value, value2);
  7468. return 0;
  7469. }
  7470. int EQ2Emu_lua_GetPlayerHistory(lua_State* state) {
  7471. if (!lua_interface)
  7472. return 0;
  7473. Spawn* player = lua_interface->GetSpawn(state);
  7474. int32 event_id = lua_interface->GetInt32Value(state, 2);
  7475. if (!player) {
  7476. lua_interface->LogError("LUA GetPlayerHistory command error: spawn is not valid");
  7477. return 0;
  7478. }
  7479. if (!player->IsPlayer()) {
  7480. lua_interface->LogError("LUA GetPlayerHistory command error: spawn is not a player");
  7481. return 0;
  7482. }
  7483. LUAHistory* hd = ((Player*)player)->GetLUAHistory(event_id);
  7484. if (!hd)
  7485. return 0;
  7486. lua_interface->SetInt32Value(state, hd->Value);
  7487. lua_interface->SetInt32Value(state, hd->Value2);
  7488. return 2;
  7489. }
  7490. int EQ2Emu_lua_SetGridID(lua_State* state) {
  7491. if (!lua_interface)
  7492. return 0;
  7493. Spawn* spawn = lua_interface->GetSpawn(state);
  7494. int32 grid = lua_interface->GetInt32Value(state, 2);
  7495. if (!spawn) {
  7496. lua_interface->LogError("LUA SetGridID command error: spawn is not valid");
  7497. return 0;
  7498. }
  7499. if (grid == 0) {
  7500. lua_interface->LogError("LUA SetGridID command error: grid is not valid");
  7501. return 0;
  7502. }
  7503. spawn->SetPos(&(spawn->appearance.pos.grid_id), grid);
  7504. return 0;
  7505. }
  7506. int EQ2Emu_lua_SetRequiredHistory(lua_State* state){
  7507. if (!lua_interface)
  7508. return 0;
  7509. Spawn* spawn = lua_interface->GetSpawn(state);
  7510. int32 event_id = lua_interface->GetInt32Value(state, 2);
  7511. int32 value1 = lua_interface->GetInt32Value(state, 3);
  7512. int32 value2 = lua_interface->GetInt32Value(state, 4);
  7513. bool private_spawn = (lua_interface->GetInt8Value(state, 5) == 1);
  7514. int16 flag_override = lua_interface->GetInt16Value(state, 6);
  7515. if (!spawn){
  7516. lua_interface->LogError("LUA SetRequiredHistory command error: spawn is not valid");
  7517. return 0;
  7518. }
  7519. //Add this quest to the list of required quests for this spawn
  7520. spawn->SetRequiredHistory(event_id, value1, value2);
  7521. //If private spawn value set
  7522. if (private_spawn){
  7523. //Set the spawn to be private when not granted access via history
  7524. spawn->AddAllowAccessSpawn(spawn);
  7525. spawn->SetPrivateQuestSpawn(true);
  7526. }
  7527. //This value will override vis_flags in the vis packet
  7528. if (flag_override > 0)
  7529. spawn->SetQuestsRequiredOverride(flag_override);
  7530. return 0;
  7531. }
  7532. int EQ2Emu_lua_GetQuestStepProgress(lua_State* state) {
  7533. if (!lua_interface)
  7534. return 0;
  7535. Spawn* player = lua_interface->GetSpawn(state);
  7536. int32 quest_id = lua_interface->GetInt32Value(state, 2);
  7537. int32 step_id = lua_interface->GetInt32Value(state, 3);
  7538. if (!player) {
  7539. lua_interface->LogError("LUA GetQuestStepProgress command error: spawn is not valid");
  7540. return 0;
  7541. }
  7542. if (!player->IsPlayer()) {
  7543. lua_interface->LogError("LUA GetQuestStepProgress command error: spawn is not a player");
  7544. return 0;
  7545. }
  7546. lua_interface->SetInt32Value(state, ((Player*)player)->GetStepProgress(quest_id, step_id));
  7547. return 1;
  7548. }
  7549. int EQ2Emu_lua_SetPlayerLevel(lua_State* state) {
  7550. if (!lua_interface)
  7551. return 0;
  7552. Spawn* player = lua_interface->GetSpawn(state);
  7553. int8 level = lua_interface->GetInt8Value(state, 2);
  7554. if (!player) {
  7555. lua_interface->LogError("LUA SetPlayerLevel command error: spawn is not valid");
  7556. return 0;
  7557. }
  7558. if (!player->IsPlayer()) {
  7559. lua_interface->LogError("LUA SetPlayerLevel command error: spawn is not a player");
  7560. return 0;
  7561. }
  7562. if (level == 0) {
  7563. lua_interface->LogError("LUA SetPlayerLevel command error: new level can't be 0");
  7564. return 0;
  7565. }
  7566. Client* client = player->GetZone()->GetClientBySpawn(player);
  7567. if (!client) {
  7568. lua_interface->LogError("LUA SetPlayerLevel command error: unable to get a client from the given spawn");
  7569. return 0;
  7570. }
  7571. client->ChangeLevel(client->GetPlayer()->GetLevel(), level);
  7572. client->GetPlayer()->SetXP(1);
  7573. client->GetPlayer()->SetNeededXP();
  7574. return 0;
  7575. }
  7576. int EQ2Emu_lua_AddCoin(lua_State* state) {
  7577. if (!lua_interface)
  7578. return 0;
  7579. Spawn* player = lua_interface->GetSpawn(state);
  7580. int32 amount = lua_interface->GetInt32Value(state, 2);
  7581. if (!player) {
  7582. lua_interface->LogError("LUA AddCoin command error: spawn is not valid");
  7583. return 0;
  7584. }
  7585. if (!player->IsPlayer()){
  7586. lua_interface->LogError("LUA AddCoin command error: spawn is not a player");
  7587. return 0;
  7588. }
  7589. if (amount == 0) {
  7590. lua_interface->LogError("LUA AddCoin command error: amount must be greater then 0");
  7591. return 0;
  7592. }
  7593. ((Player*)player)->AddCoins(amount);
  7594. return 0;
  7595. }
  7596. int EQ2Emu_lua_RemoveCoin(lua_State* state) {
  7597. if (!lua_interface)
  7598. return 0;
  7599. Spawn* player = lua_interface->GetSpawn(state);
  7600. int32 amount = lua_interface->GetInt32Value(state, 2);
  7601. if (!player) {
  7602. lua_interface->LogError("LUA RemoveCoin command error: spawn is not valid");
  7603. return 0;
  7604. }
  7605. if (!player->IsPlayer()){
  7606. lua_interface->LogError("LUA RemoveCoin command error: spawn is not a player");
  7607. return 0;
  7608. }
  7609. if (amount == 0) {
  7610. lua_interface->LogError("LUA RemoveCoin command error: amount must be greater then 0");
  7611. return 0;
  7612. }
  7613. lua_interface->SetBooleanValue(state, ((Player*)player)->RemoveCoins(amount));
  7614. return 1;
  7615. }
  7616. int EQ2Emu_lua_GetPlayersInZone(lua_State* state) {
  7617. if (!lua_interface)
  7618. return 0;
  7619. ZoneServer* zone = lua_interface->GetZone(state);
  7620. if (!zone) {
  7621. lua_interface->LogError("LUA GetPlayersInZone command error: zone is not valid");
  7622. return 0;
  7623. }
  7624. vector<Entity*> players = zone->GetPlayers();
  7625. if (players.size() == 0)
  7626. return 0;
  7627. lua_createtable(state, players.size(), 0);
  7628. int newTable = lua_gettop(state);
  7629. for (int32 i = 0; i < players.size(); i++) {
  7630. lua_interface->SetSpawnValue(state, players.at(i));
  7631. lua_rawseti(state, newTable, i + 1);
  7632. }
  7633. return 1;
  7634. }
  7635. int EQ2Emu_lua_SpawnGroupByID(lua_State* state){
  7636. if (!lua_interface)
  7637. return 0;
  7638. ZoneServer* zone = lua_interface->GetZone(state, 1);
  7639. if (!zone) {
  7640. lua_interface->LogError("LUA GetPlayersInZone command error: zone is not valid");
  7641. return 0;
  7642. }
  7643. int32 group_id = lua_interface->GetInt32Value(state, 2);
  7644. //Map of <placement_id, location_id>
  7645. map<int32, int32>* locs = zone->GetSpawnLocationsByGroup(group_id);
  7646. map<int32, int32>::iterator itr;
  7647. vector<Spawn*> group;
  7648. for (itr = locs->begin(); itr != locs->end(); itr++){
  7649. SpawnLocation* location = zone->GetSpawnLocation(itr->second);
  7650. if (!location) {
  7651. lua_interface->LogError("LUA SpawnByLocationID command error: no location found for the given ID (%u)", itr->second);
  7652. return 0;
  7653. }
  7654. Spawn* spawn = 0;
  7655. if (location->entities[0]) {
  7656. if (location->entities[0]->spawn_type == SPAWN_ENTRY_TYPE_NPC)
  7657. spawn = zone->AddNPCSpawn(location, location->entities[0]);
  7658. else if (location->entities[0]->spawn_type == SPAWN_ENTRY_TYPE_GROUNDSPAWN)
  7659. spawn = zone->AddGroundSpawn(location, location->entities[0]);
  7660. else if (location->entities[0]->spawn_type == SPAWN_ENTRY_TYPE_OBJECT)
  7661. spawn = zone->AddObjectSpawn(location, location->entities[0]);
  7662. else if (location->entities[0]->spawn_type == SPAWN_ENTRY_TYPE_WIDGET)
  7663. spawn = zone->AddWidgetSpawn(location, location->entities[0]);
  7664. else if (location->entities[0]->spawn_type == SPAWN_ENTRY_TYPE_SIGN)
  7665. spawn = zone->AddSignSpawn(location, location->entities[0]);
  7666. if (spawn) {
  7667. const char* script = 0;
  7668. for (int x = 0; x < 3; x++) {
  7669. switch (x) {
  7670. case 0:
  7671. script = world.GetSpawnEntryScript(location->entities[0]->spawn_entry_id);
  7672. break;
  7673. case 1:
  7674. script = world.GetSpawnLocationScript(location->entities[0]->spawn_location_id);
  7675. break;
  7676. case 2:
  7677. script = world.GetSpawnScript(location->entities[0]->spawn_id);
  7678. break;
  7679. }
  7680. if (script && lua_interface->GetSpawnScript(script) != 0) {
  7681. spawn->SetSpawnScript(string(script));
  7682. break;
  7683. }
  7684. }
  7685. zone->CallSpawnScript(spawn, SPAWN_SCRIPT_SPAWN);
  7686. lua_interface->SetSpawnValue(state, spawn);
  7687. group.push_back(spawn);
  7688. }
  7689. else {
  7690. LogWrite(ZONE__ERROR, 0, "Zone", "Error adding spawn to zone");
  7691. safe_delete(spawn);
  7692. }
  7693. }
  7694. }
  7695. if (!group.empty()){
  7696. lua_createtable(state, group.size(), 0);
  7697. int newTable = lua_gettop(state);
  7698. for (int32 i = 0; i < group.size(); i++) {
  7699. lua_interface->SetSpawnValue(state, group[i]);
  7700. lua_rawseti(state, newTable, i + 1);
  7701. }
  7702. }
  7703. else
  7704. lua_pushnil(state);
  7705. return 1;
  7706. }
  7707. int EQ2Emu_lua_SetSpawnAnimation(lua_State* state) {
  7708. if (!lua_interface)
  7709. return 0;
  7710. Spawn* spawn = lua_interface->GetSpawn(state, 1);
  7711. int32 anim_id = lua_interface->GetInt32Value(state, 2);
  7712. int16 leeway = lua_interface->GetInt16Value(state, 3);
  7713. if (!spawn) {
  7714. lua_interface->LogError("LUA SetSpawnAnimation command error: spawn is not valid");
  7715. return 0;
  7716. }
  7717. if (anim_id == 0) {
  7718. lua_interface->LogError("LUA SetSpawnAnimation command error: anim_id is not valid");
  7719. return 0;
  7720. }
  7721. if (leeway == 0)
  7722. leeway = 5000;
  7723. spawn->SetSpawnAnim(anim_id);
  7724. spawn->SetSpawnAnimLeeway(leeway);
  7725. return 0;
  7726. }
  7727. int EQ2Emu_lua_GetClientVersion(lua_State* state) {
  7728. if (!lua_interface)
  7729. return 0;
  7730. Spawn* player = lua_interface->GetSpawn(state);
  7731. if (!player) {
  7732. return 0;
  7733. }
  7734. Client* client = player->GetZone()->GetClientBySpawn(player);
  7735. if (!client) {
  7736. return 0;
  7737. }
  7738. lua_interface->SetInt32Value(state, client->GetVersion());
  7739. return 1;
  7740. }
  7741. int EQ2Emu_lua_GetItemID(lua_State* state) {
  7742. if (!lua_interface)
  7743. return 0;
  7744. Item* item = lua_interface->GetItem(state);
  7745. if (!item) {
  7746. lua_interface->LogError("LUA GetItemID command error: item is not valid");
  7747. return 0;
  7748. }
  7749. lua_interface->SetInt32Value(state, item->details.item_id);
  7750. return 1;
  7751. }
  7752. int EQ2Emu_lua_IsEntity(lua_State* state) {
  7753. if (!lua_interface)
  7754. return 0;
  7755. Spawn* spawn = lua_interface->GetSpawn(state);
  7756. if (!spawn) {
  7757. lua_interface->LogError("LUA IsEntity command error: spawn is not valid");
  7758. return 0;
  7759. }
  7760. lua_interface->SetBooleanValue(state, spawn->IsEntity());
  7761. return 1;
  7762. }
  7763. int EQ2Emu_lua_GetOrigX(lua_State* state) {
  7764. if (!lua_interface)
  7765. return 0;
  7766. Spawn* spawn = lua_interface->GetSpawn(state);
  7767. if (!spawn) {
  7768. lua_interface->LogError("LUA GetOrigX command error: spawn is not valid");
  7769. return 0;
  7770. }
  7771. lua_interface->SetFloatValue(state, spawn->GetSpawnOrigX());
  7772. return 1;
  7773. }
  7774. int EQ2Emu_lua_GetOrigY(lua_State* state) {
  7775. if (!lua_interface)
  7776. return 0;
  7777. Spawn* spawn = lua_interface->GetSpawn(state);
  7778. if (!spawn) {
  7779. lua_interface->LogError("LUA GetOrigY command error: spawn is not valid");
  7780. return 0;
  7781. }
  7782. lua_interface->SetFloatValue(state, spawn->GetSpawnOrigY());
  7783. return 1;
  7784. }
  7785. int EQ2Emu_lua_GetOrigZ(lua_State* state) {
  7786. if (!lua_interface)
  7787. return 0;
  7788. Spawn* spawn = lua_interface->GetSpawn(state);
  7789. if (!spawn) {
  7790. lua_interface->LogError("LUA GetOrigZ command error: spawn is not valid");
  7791. return 0;
  7792. }
  7793. lua_interface->SetFloatValue(state, spawn->GetSpawnOrigZ());
  7794. return 1;
  7795. }
  7796. int EQ2Emu_lua_GetPCTOfHP(lua_State* state) {
  7797. if (!lua_interface)
  7798. return 0;
  7799. Spawn* spawn = lua_interface->GetSpawn(state);
  7800. float pct = lua_interface->GetFloatValue(state, 2);
  7801. if (!spawn) {
  7802. lua_interface->LogError("LUA GetPCTOfHP command error: spawn is not valid");
  7803. return 0;
  7804. }
  7805. if (pct == 0) {
  7806. lua_interface->LogError("LUA GetPCTOfHP command error: pct is not valid");
  7807. return 0;
  7808. }
  7809. int32 amount = std::round(spawn->GetTotalHP() * (pct / 100));
  7810. lua_interface->SetInt32Value(state, amount);
  7811. return 1;
  7812. }
  7813. int EQ2Emu_lua_GetPCTOfPower(lua_State* state) {
  7814. if (!lua_interface)
  7815. return 0;
  7816. Spawn* spawn = lua_interface->GetSpawn(state);
  7817. float pct = lua_interface->GetFloatValue(state, 2);
  7818. if (!spawn) {
  7819. lua_interface->LogError("LUA GetPCTOfPower command error: spawn is not valid");
  7820. return 0;
  7821. }
  7822. if (pct == 0) {
  7823. lua_interface->LogError("LUA GetPCTOfPower command error: pct is not valid");
  7824. return 0;
  7825. }
  7826. int32 amount = std::round(spawn->GetTotalPower() * (pct / 100));
  7827. lua_interface->SetInt32Value(state, amount);
  7828. return 1;
  7829. }
  7830. int EQ2Emu_lua_GetBoundZoneID(lua_State* state) {
  7831. if (!lua_interface)
  7832. return 0;
  7833. Spawn* spawn = lua_interface->GetSpawn(state);
  7834. if (!spawn) {
  7835. lua_interface->LogError("LUA GetBoundZoneID command error: spawn is not valid");
  7836. return 0;
  7837. }
  7838. if (!spawn->IsPlayer()) {
  7839. lua_interface->LogError("LUA GetBoundZoneID command error: spawn is not a player");
  7840. return 0;
  7841. }
  7842. lua_interface->SetInt32Value(state, ((Player*)spawn)->GetPlayerInfo()->GetBindZoneID());
  7843. return 1;
  7844. }
  7845. int EQ2Emu_lua_Evac(lua_State* state) {
  7846. if (!lua_interface)
  7847. return 0;
  7848. Spawn* target = lua_interface->GetSpawn(state);
  7849. if (target) {
  7850. float x = target->GetZone()->GetSafeX();
  7851. float y = target->GetZone()->GetSafeY();
  7852. float z = target->GetZone()->GetSafeZ();
  7853. float h = target->GetZone()->GetSafeHeading();
  7854. target->SetX(x);
  7855. target->SetY(y);
  7856. target->SetZ(z);
  7857. target->SetHeading(h);
  7858. target->SetSpawnOrigX(target->GetX());
  7859. target->SetSpawnOrigY(target->GetY());
  7860. target->SetSpawnOrigZ(target->GetZ());
  7861. target->SetSpawnOrigHeading(target->GetHeading());
  7862. if (target->IsPlayer()) {
  7863. Client* client = target->GetZone()->GetClientBySpawn(target);
  7864. if (client) {
  7865. PacketStruct* packet = configReader.getStruct("WS_TeleportWithinZone", client->GetVersion());
  7866. if (packet)
  7867. {
  7868. packet->setDataByName("x", x);
  7869. packet->setDataByName("y", y);
  7870. packet->setDataByName("z", z);
  7871. client->QueuePacket(packet->serialize());
  7872. safe_delete(packet);
  7873. }
  7874. }
  7875. }
  7876. }
  7877. else {
  7878. LuaSpell* spell = lua_interface->GetCurrentSpell(state);
  7879. ZoneServer* zone = spell->caster->GetZone();
  7880. float x = spell->caster->GetZone()->GetSafeX();
  7881. float y = spell->caster->GetZone()->GetSafeY();
  7882. float z = spell->caster->GetZone()->GetSafeZ();
  7883. float h = spell->caster->GetZone()->GetSafeHeading();
  7884. spell->MSpellTargets.readlock(__FUNCTION__, __LINE__);
  7885. for (int32 i = 0; i < spell->targets.size(); i++) {
  7886. Spawn* target2 = zone->GetSpawnByID(spell->targets.at(i));
  7887. if (!target2)
  7888. continue;
  7889. target2->SetX(x);
  7890. target2->SetY(y);
  7891. target2->SetZ(z);
  7892. target2->SetHeading(h);
  7893. target2->SetSpawnOrigX(target2->GetX());
  7894. target2->SetSpawnOrigY(target2->GetY());
  7895. target2->SetSpawnOrigZ(target2->GetZ());
  7896. target2->SetSpawnOrigHeading(target2->GetHeading());
  7897. if (target2->IsPlayer()) {
  7898. Client* client = target2->GetZone()->GetClientBySpawn(target2);
  7899. if (client) {
  7900. PacketStruct* packet = configReader.getStruct("WS_TeleportWithinZone", client->GetVersion());
  7901. if (packet)
  7902. {
  7903. packet->setDataByName("x", x);
  7904. packet->setDataByName("y", y);
  7905. packet->setDataByName("z", z);
  7906. client->QueuePacket(packet->serialize());
  7907. safe_delete(packet);
  7908. }
  7909. }
  7910. }
  7911. }
  7912. spell->MSpellTargets.releasereadlock(__FUNCTION__, __LINE__);
  7913. }
  7914. return 0;
  7915. }
  7916. int EQ2Emu_lua_GetSpellTier(lua_State* state) {
  7917. if (!lua_interface)
  7918. return 0;
  7919. LuaSpell* luaspell = lua_interface->GetCurrentSpell(state);
  7920. if (!luaspell) {
  7921. lua_interface->LogError("LUA GetSpellTier command error: must be used in a spell script");
  7922. return 0;
  7923. }
  7924. int8 tier = luaspell->spell->GetSpellTier();
  7925. lua_interface->SetInt32Value(state, tier);
  7926. return 1;
  7927. }
  7928. int EQ2Emu_lua_GetSpellID(lua_State* state) {
  7929. if (!lua_interface)
  7930. return 0;
  7931. LuaSpell* luaspell = lua_interface->GetCurrentSpell(state);
  7932. if (!luaspell) {
  7933. lua_interface->LogError("LUA GetSpellID command error: must be used in a spell script");
  7934. return 0;
  7935. }
  7936. int32 spell_id = luaspell->spell->GetSpellID();
  7937. lua_interface->SetInt32Value(state, spell_id);
  7938. return 1;
  7939. }
  7940. int EQ2Emu_lua_StartTransmute(lua_State* state) {
  7941. if (!lua_interface)
  7942. return 0;
  7943. Spawn* spawn = lua_interface->GetSpawn(state, 1);
  7944. if (!spawn) {
  7945. lua_interface->LogError("Lua StartTransmute command error: no spawn");
  7946. return 0;
  7947. }
  7948. if (!spawn->IsPlayer()) {
  7949. lua_interface->LogError("Lua StartTransmute command error: spawn is not a player");
  7950. return 0;
  7951. }
  7952. ZoneServer* zone = spawn->GetZone();
  7953. if (!zone) {
  7954. return 0;
  7955. }
  7956. Client* client = zone->GetClientBySpawn(spawn);
  7957. if (!client) {
  7958. return 0;
  7959. }
  7960. Transmute::CreateItemRequest(client, static_cast<Player*>(spawn));
  7961. return 0;
  7962. }
  7963. int EQ2Emu_lua_CompleteTransmute(lua_State* state) {
  7964. if (!lua_interface)
  7965. return 0;
  7966. Spawn* spawn = lua_interface->GetSpawn(state, 1);
  7967. if (!spawn) {
  7968. lua_interface->LogError("Lua CompleteTransmute command error: no spawn");
  7969. return 0;
  7970. }
  7971. if (!spawn->IsPlayer()) {
  7972. lua_interface->LogError("Lua CompleteTransmute command error: spawn is not a player");
  7973. return 0;
  7974. }
  7975. ZoneServer* zone = spawn->GetZone();
  7976. if (!zone) {
  7977. return 0;
  7978. }
  7979. Client* client = zone->GetClientBySpawn(spawn);
  7980. if (!client) {
  7981. return 0;
  7982. }
  7983. Transmute::CompleteTransmutation(client, static_cast<Player*>(spawn));
  7984. return 0;
  7985. }
  7986. int EQ2Emu_lua_ProcHate(lua_State* state) {
  7987. if (!lua_interface)
  7988. return 0;
  7989. Spawn* caster = lua_interface->GetSpawn(state);
  7990. Spawn* target = lua_interface->GetSpawn(state, 2);
  7991. int32 threat_amt = lua_interface->GetInt32Value(state, 3);
  7992. string spell_name = lua_interface->GetStringValue(state, 4);
  7993. if (!caster) {
  7994. lua_interface->LogError("LUA ProcHate command error: caster is not a valid spawn");
  7995. return 0;
  7996. }
  7997. if (!caster->IsEntity()) {
  7998. lua_interface->LogError("LUA ProcHate command error: caster is not an entity");
  7999. return 0;
  8000. }
  8001. if (!target) {
  8002. lua_interface->LogError("LUA ProcHate command error: target is not a valid spawn");
  8003. return 0;
  8004. }
  8005. if (!target->IsEntity()) {
  8006. lua_interface->LogError("LUA ProcHate command error: target is not an entity");
  8007. return 0;
  8008. }
  8009. static_cast<Entity*>(target)->AddHate(static_cast<Entity*>(caster), threat_amt);
  8010. caster->GetZone()->SendThreatPacket(static_cast<Entity*>(caster), target, threat_amt, spell_name.c_str());
  8011. return 0;
  8012. }