LuaFunctions.cpp 305 KB

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