LuaFunctions.cpp 307 KB

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