LuaFunctions.cpp 329 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707
  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. #include <sstream>
  36. #include <boost/algorithm/string.hpp>
  37. extern MasterFactionList master_faction_list;
  38. extern WorldDatabase database;
  39. extern LuaInterface* lua_interface;
  40. extern ConfigReader configReader;
  41. extern MasterQuestList master_quest_list;
  42. extern MasterItemList master_item_list;
  43. extern MasterSpellList master_spell_list;
  44. extern World world;
  45. extern Commands commands;
  46. extern ZoneList zone_list;
  47. extern Races races;
  48. extern Classes classes;
  49. extern Variables variables;
  50. extern MasterSkillList master_skill_list;
  51. extern MasterHeroicOPList master_ho_list;
  52. extern MasterRaceTypeList race_types_list;
  53. extern MasterLanguagesList master_languages_list;
  54. vector<string> ParseString(string strVal, char delim) {
  55. stringstream ss(strVal);
  56. vector<string> ret;
  57. while (ss.good())
  58. {
  59. string substr;
  60. getline(ss, substr, delim);
  61. ret.push_back(substr);
  62. }
  63. return ret;
  64. }
  65. vector<unsigned int> ParseStringToInt32(string strVal, char delim) {
  66. stringstream ss(strVal);
  67. vector<unsigned int> ret;
  68. while (ss.good())
  69. {
  70. string substr;
  71. getline(ss, substr, delim);
  72. stringstream valss(substr);
  73. unsigned int val = 0;
  74. valss >> val;
  75. ret.push_back(val);
  76. }
  77. return ret;
  78. }
  79. map<string, signed int> ParseStringMap(string strVal, char delim) {
  80. vector<string> pairs = ParseString(strVal, delim);
  81. vector<string>::iterator itr;
  82. map<string, signed int> ret;
  83. for (itr = pairs.begin(); itr != pairs.end(); itr++) {
  84. vector<string> keyvaluepair = ParseString(*itr, ':');
  85. if (keyvaluepair.size() == 2) {
  86. stringstream valss(keyvaluepair[1]);
  87. int32 val = 0;
  88. valss >> val;
  89. ret[keyvaluepair[0]] = val;
  90. }
  91. }
  92. return ret;
  93. }
  94. map<unsigned int, unsigned short> ParseIntMap(string strVal, char delim) {
  95. vector<string> pairs = ParseString(strVal, delim);
  96. vector<string>::iterator itr;
  97. map<unsigned int, unsigned short> ret;
  98. for (itr = pairs.begin(); itr != pairs.end(); itr++) {
  99. vector<string> keyvaluepair = ParseString(*itr, ':');
  100. int32 key = 0;
  101. if (keyvaluepair.size() > 0) {
  102. stringstream keyss(keyvaluepair[0]);
  103. keyss >> key;
  104. }
  105. if (keyvaluepair.size() == 1) {
  106. ret[key] = 1;
  107. }
  108. else if (keyvaluepair.size() == 2) {
  109. stringstream valss(keyvaluepair[1]);
  110. unsigned short val = 0;
  111. valss >> val;
  112. ret[key] = val;
  113. }
  114. }
  115. return ret;
  116. }
  117. map<unsigned int, signed int> ParseSInt32Map(string strVal, char delim) {
  118. vector<string> pairs = ParseString(strVal, delim);
  119. vector<string>::iterator itr;
  120. map<unsigned int, signed int> ret;
  121. for (itr = pairs.begin(); itr != pairs.end(); itr++) {
  122. vector<string> keyvaluepair = ParseString(*itr, ':');
  123. int32 key = 0;
  124. if (keyvaluepair.size() > 0) {
  125. stringstream keyss(keyvaluepair[0]);
  126. keyss >> key;
  127. }
  128. if (keyvaluepair.size() == 1) {
  129. ret[key] = 1;
  130. }
  131. else if (keyvaluepair.size() == 2) {
  132. stringstream valss(keyvaluepair[1]);
  133. signed int val = 0;
  134. valss >> val;
  135. ret[key] = val;
  136. }
  137. }
  138. return ret;
  139. }
  140. int EQ2Emu_lua_PlayFlavor(lua_State* state) {
  141. if (!lua_interface)
  142. return 0;
  143. Spawn* spawn = lua_interface->GetSpawn(state);
  144. string mp3_string = lua_interface->GetStringValue(state, 2);
  145. string text_string = lua_interface->GetStringValue(state, 3);
  146. string emote_string = lua_interface->GetStringValue(state, 4);
  147. int32 key1 = lua_interface->GetInt32Value(state, 5);
  148. int32 key2 = lua_interface->GetInt32Value(state, 6);
  149. Spawn* player = lua_interface->GetSpawn(state, 7);
  150. int8 language = lua_interface->GetInt8Value(state, 8);
  151. if (spawn) {
  152. const char* mp3 = 0;
  153. const char* text = 0;
  154. const char* emote = 0;
  155. if (mp3_string.length() > 0)
  156. mp3 = mp3_string.c_str();
  157. if (text_string.length() > 0)
  158. text = text_string.c_str();
  159. if (emote_string.length() > 0)
  160. emote = emote_string.c_str();
  161. Client* client = 0;
  162. if (player && player->IsPlayer())
  163. client = spawn->GetZone()->GetClientBySpawn(player);
  164. if (client) {
  165. if (((Player*)player)->WasSentSpawn(spawn->GetID()) && !((Player*)player)->WasSpawnRemoved(spawn))
  166. spawn->GetZone()->PlayFlavor(client, spawn, mp3, text, emote, key1, key2, language);
  167. }
  168. else
  169. spawn->GetZone()->PlayFlavor(spawn, mp3, text, emote, key1, key2, language);
  170. }
  171. return 0;
  172. }
  173. int EQ2Emu_lua_PlaySound(lua_State* state) {
  174. if (!lua_interface)
  175. return 0;
  176. Spawn* spawn = lua_interface->GetSpawn(state);
  177. string sound_string = lua_interface->GetStringValue(state, 2);
  178. float x = lua_interface->GetFloatValue(state, 3);
  179. float y = lua_interface->GetFloatValue(state, 4);
  180. float z = lua_interface->GetFloatValue(state, 5);
  181. Spawn* player = lua_interface->GetSpawn(state, 6);
  182. if (spawn && sound_string.length() > 0) {
  183. Client* client = 0;
  184. if (player && player->IsPlayer())
  185. client = spawn->GetZone()->GetClientBySpawn(player);
  186. if (client)
  187. spawn->GetZone()->PlaySoundFile(client, sound_string.c_str(), x, y, z);
  188. else
  189. spawn->GetZone()->PlaySoundFile(0, sound_string.c_str(), x, y, z);
  190. }
  191. return 0;
  192. }
  193. int EQ2Emu_lua_SetRequiredQuest(lua_State* state) {
  194. if (!lua_interface)
  195. return 0;
  196. Spawn* spawn = lua_interface->GetSpawn(state);
  197. int32 quest_id = lua_interface->GetInt32Value(state, 2);
  198. int16 quest_step = lua_interface->GetInt16Value(state, 3);
  199. bool private_spawn = (lua_interface->GetInt8Value(state, 4) == 1);
  200. bool continued_access = (lua_interface->GetInt8Value(state, 5) == 1);
  201. int16 flag_override = lua_interface->GetInt16Value(state, 6);
  202. if (!spawn) {
  203. lua_interface->LogError("%s: LUA SetRequiredQuest command error: spawn is not valid", lua_interface->GetScriptName(state));
  204. return 0;
  205. }
  206. if (quest_id > 0) {
  207. //Add this quest to the list of required quests for this spawn
  208. spawn->SetQuestsRequired(quest_id, quest_step);
  209. //If private spawn value set
  210. if (private_spawn) {
  211. //Set the spawn to be private when not granted access through this quest
  212. spawn->AddAllowAccessSpawn(spawn);
  213. spawn->SetPrivateQuestSpawn(true);
  214. }
  215. //This value allows access after a quest step, or the whole quest has been completed
  216. if (continued_access)
  217. spawn->SetQuestsRequiredContinuedAccess(true);
  218. //This value will override vis_flags in the vis packet
  219. if (flag_override > 0)
  220. spawn->SetQuestsRequiredOverride(flag_override);
  221. }
  222. return 0;
  223. }
  224. int EQ2Emu_lua_SpawnSetByDistance(lua_State* state) {
  225. if (!lua_interface)
  226. return 0;
  227. Spawn* spawn = lua_interface->GetSpawn(state);
  228. float max_distance = lua_interface->GetFloatValue(state, 2);
  229. string variable = lua_interface->GetStringValue(state, 3);
  230. string value = lua_interface->GetStringValue(state, 4);
  231. if (max_distance > 0 && spawn && value.length() > 0 && variable.length() > 0 && spawn->GetZone())
  232. spawn->GetZone()->SpawnSetByDistance(spawn, max_distance, variable, value);
  233. return 0;
  234. }
  235. int EQ2Emu_lua_PerformCameraShake(lua_State* state) {
  236. if (!lua_interface)
  237. return 0;
  238. Client* client = 0;
  239. Spawn* player = lua_interface->GetSpawn(state);
  240. if (!player) {
  241. lua_interface->LogError("LUA PerformCameraShake command error: spawn is not valid");
  242. return 0;
  243. }
  244. if (!player->IsPlayer()) {
  245. lua_interface->LogError("LUA PerformCameraShake command error: spawn is not a player");
  246. return 0;
  247. }
  248. if (player->GetZone())
  249. client = player->GetZone()->GetClientBySpawn(player);
  250. if (!client) {
  251. lua_interface->LogError("LUA PerformCameraShake command error: could not find client");
  252. return 0;
  253. }
  254. float intensity = lua_interface->GetFloatValue(state, 2);
  255. int8 direction = lua_interface->GetInt8Value(state, 3);
  256. PacketStruct* packet = configReader.getStruct("WS_PerformCameraShakeMsg", client->GetVersion());
  257. if (packet) {
  258. /* Client Intensity Logic (does not restrict service side, but expect .01 - 1.0 range)
  259. v1 = *(float *)(a1 + 4);
  260. if ( v1 > 0.0 )
  261. v2 = fminf(v1, 1.0);
  262. else
  263. v2 = 0.1;
  264. */
  265. packet->setDataByName("intensity", intensity);
  266. if ( client->GetVersion() > 546 )
  267. packet->setDataByName("direction", direction);
  268. client->QueuePacket(packet->serialize());
  269. safe_delete(packet);
  270. }
  271. return 0;
  272. }
  273. int EQ2Emu_lua_KillSpawn(lua_State* state) {
  274. if (!lua_interface)
  275. return 0;
  276. Spawn* dead = lua_interface->GetSpawn(state);
  277. Spawn* killer = lua_interface->GetSpawn(state, 2);
  278. bool send_packet = (lua_interface->GetInt8Value(state, 3) == 1);
  279. if (dead && dead->Alive() && dead->GetZone())
  280. dead->GetZone()->KillSpawn(false, dead, killer, send_packet);
  281. return 0;
  282. }
  283. int EQ2Emu_lua_KillSpawnByDistance(lua_State* state) {
  284. if (!lua_interface)
  285. return 0;
  286. Spawn* spawn = lua_interface->GetSpawn(state);
  287. float max_distance = lua_interface->GetFloatValue(state, 2);
  288. bool include_players = lua_interface->GetInt8Value(state, 3);
  289. bool send_packet = (lua_interface->GetInt8Value(state, 4) == 1);
  290. if (max_distance > 0 && spawn && spawn->GetZone())
  291. spawn->GetZone()->KillSpawnByDistance(spawn, max_distance, include_players, send_packet);
  292. return 0;
  293. }
  294. int EQ2Emu_lua_Despawn(lua_State* state) {
  295. if (!lua_interface)
  296. return 0;
  297. Spawn* spawn = lua_interface->GetSpawn(state);
  298. int32 delay = lua_interface->GetInt32Value(state, 2);
  299. if (spawn && spawn->GetZone())
  300. spawn->GetZone()->Despawn(spawn, delay);
  301. return 0;
  302. }
  303. int EQ2Emu_lua_ChangeHandIcon(lua_State* state) {
  304. if (!lua_interface)
  305. return 0;
  306. Spawn* spawn = lua_interface->GetSpawn(state);
  307. int8 displayHandIcon = lua_interface->GetInt8Value(state, 2);
  308. if (spawn) {
  309. spawn->info_changed = true;
  310. spawn->SetShowHandIcon(displayHandIcon);
  311. }
  312. return 0;
  313. }
  314. int EQ2Emu_lua_SendStateCommand(lua_State* state) {
  315. if (!lua_interface)
  316. return 0;
  317. Spawn* spawn = lua_interface->GetSpawn(state);
  318. int32 new_state = lua_interface->GetInt32Value(state, 2);
  319. if (spawn) {
  320. spawn->GetZone()->SendStateCommand(spawn, new_state);
  321. }
  322. return 0;
  323. }
  324. int EQ2Emu_lua_SpawnSet(lua_State* state) {
  325. if (!lua_interface)
  326. return 0;
  327. Spawn* spawn = lua_interface->GetSpawn(state);
  328. string variable = lua_interface->GetStringValue(state, 2);
  329. string value = lua_interface->GetStringValue(state, 3);
  330. bool no_update = lua_interface->GetBooleanValue(state, 4); // send update is true by default in SetSpawnCommand, so allow user to specify 'true' to disable send update.
  331. bool temporary_flag = lua_interface->GetBooleanValue(state, 5); // default false as originally designed, allow user to set temporary_flag true to not update DB
  332. int32 type = commands.GetSpawnSetType(variable);
  333. if (type != 0xFFFFFFFF && value.length() > 0 && spawn)
  334. commands.SetSpawnCommand(0, spawn, type, value.c_str(), !no_update, temporary_flag);
  335. return 0;
  336. }
  337. int EQ2Emu_lua_GetSpawn(lua_State* state) {
  338. if (!lua_interface)
  339. return 0;
  340. Spawn* spawn = lua_interface->GetSpawn(state);
  341. int32 spawn_id = lua_interface->GetInt32Value(state, 2);
  342. if (spawn && spawn_id > 0) {
  343. Spawn* closest_spawn = spawn->GetZone()->GetClosestSpawn(spawn, spawn_id);
  344. if (closest_spawn) {
  345. lua_interface->SetSpawnValue(state, closest_spawn);
  346. return 1;
  347. }
  348. }
  349. return 0;
  350. }
  351. int EQ2Emu_lua_GetSpawnFromList(lua_State* state) {
  352. if (!lua_interface)
  353. return 0;
  354. vector<Spawn*>* spawnList = lua_interface->GetSpawnList(state);
  355. int32 position = lua_interface->GetInt32Value(state, 2);
  356. if (spawnList) {
  357. if (spawnList->size() > position) {
  358. lua_interface->SetSpawnValue(state, spawnList->at(position));
  359. return 1;
  360. }
  361. else {
  362. return 0;
  363. }
  364. return spawnList->size();
  365. }
  366. return 0;
  367. }
  368. int EQ2Emu_lua_GetSpawnListSize(lua_State* state) {
  369. if (!lua_interface)
  370. return 0;
  371. vector<Spawn*>* spawnList = lua_interface->GetSpawnList(state);
  372. if (spawnList) {
  373. return spawnList->size();
  374. }
  375. return 0;
  376. }
  377. int EQ2Emu_lua_CreateSpawnList(lua_State* state) {
  378. if (!lua_interface)
  379. return 0;
  380. vector<Spawn*>* spawnList = new vector<Spawn*>();
  381. lua_interface->SetSpawnListValue(state, spawnList);
  382. return 1;
  383. }
  384. int EQ2Emu_lua_AddSpawnToSpawnList(lua_State* state) {
  385. if (!lua_interface)
  386. return 0;
  387. vector<Spawn*>* spawnList = lua_interface->GetSpawnList(state);
  388. Spawn* spawn = lua_interface->GetSpawn(state, 2);
  389. if (spawnList) {
  390. auto it = std::find(spawnList->begin(), spawnList->end(), spawn);
  391. if (it == spawnList->end())
  392. spawnList->push_back(spawn);
  393. }
  394. return 0;
  395. }
  396. int EQ2Emu_lua_RemoveSpawnFromSpawnList(lua_State* state) {
  397. if (!lua_interface)
  398. return 0;
  399. vector<Spawn*>* spawnList = lua_interface->GetSpawnList(state);
  400. Spawn* spawn = lua_interface->GetSpawn(state, 2);
  401. if (spawnList) {
  402. auto it = std::find(spawnList->begin(), spawnList->end(), spawn);
  403. if(it != spawnList->end())
  404. spawnList->erase(it);
  405. }
  406. return 0;
  407. }
  408. int EQ2Emu_lua_GetSpawnListBySpawnID(lua_State* state) {
  409. if (!lua_interface)
  410. return 0;
  411. Spawn* spawn = lua_interface->GetSpawn(state);
  412. int32 spawn_id = lua_interface->GetInt32Value(state, 2);
  413. if (spawn) {
  414. vector<Spawn*> spawns = spawn->GetZone()->GetSpawnsByID(spawn_id);
  415. if (spawns.size() > 0) {
  416. vector<Spawn*>* spawnList = new vector<Spawn*>();
  417. vector<Spawn*>::iterator itr;
  418. for (itr = spawns.begin(); itr != spawns.end(); itr++) {
  419. spawnList->push_back(*itr);
  420. }
  421. lua_interface->SetSpawnListValue(state, spawnList);
  422. return 1;
  423. }
  424. }
  425. return 0;
  426. }
  427. int EQ2Emu_lua_GetVariableValue(lua_State* state) {
  428. if (!lua_interface)
  429. return 0;
  430. string variable_name = lua_interface->GetStringValue(state);
  431. Variable* var = variables.FindVariable(variable_name);
  432. if (var) {
  433. lua_interface->SetStringValue(state, var->GetValue());
  434. return 1;
  435. }
  436. return 0;
  437. }
  438. int EQ2Emu_lua_GetCoinMessage(lua_State* state) {
  439. if (!lua_interface)
  440. return 0;
  441. int32 total_coins = lua_interface->GetInt32Value(state);
  442. if (total_coins == 0) {
  443. lua_interface->SetStringValue(state, "0 copper");
  444. return 1;
  445. }
  446. char tmp[64] = { 0 };
  447. string message = "";
  448. int32 val = 0;
  449. if (total_coins >= 1000000) {
  450. val = total_coins / 1000000;
  451. total_coins -= 1000000 * val;
  452. sprintf(tmp, " %u Platinum", val);
  453. message.append(tmp);
  454. memset(tmp, 0, 64);
  455. }
  456. if (total_coins >= 10000) {
  457. val = total_coins / 10000;
  458. total_coins -= 10000 * val;
  459. sprintf(tmp, " %u Gold", val);
  460. message.append(tmp);
  461. memset(tmp, 0, 64);
  462. }
  463. if (total_coins >= 100) {
  464. val = total_coins / 100;
  465. total_coins -= 100 * val;
  466. sprintf(tmp, " %u Silver", val);
  467. message.append(tmp);
  468. memset(tmp, 0, 64);
  469. }
  470. if (total_coins > 0) {
  471. sprintf(tmp, " %u Copper", (int32)total_coins);
  472. message.append(tmp);
  473. }
  474. lua_interface->SetStringValue(state, message.c_str());
  475. return 1;
  476. }
  477. int EQ2Emu_lua_GetSpawnByGroupID(lua_State* state) {
  478. ZoneServer* zone = lua_interface->GetZone(state);
  479. int32 group_id = lua_interface->GetInt32Value(state, 2);
  480. if (zone) {
  481. Spawn* spawn = zone->GetSpawnGroup(group_id);
  482. if (spawn) {
  483. lua_interface->SetSpawnValue(state, spawn);
  484. return 1;
  485. }
  486. }
  487. return 0;
  488. }
  489. int EQ2Emu_lua_GetSpawnByLocationID(lua_State* state) {
  490. ZoneServer* zone = lua_interface->GetZone(state);
  491. int32 location_id = lua_interface->GetInt32Value(state, 2);
  492. if (zone) {
  493. Spawn* spawn = zone->GetSpawnByLocationID(location_id);
  494. if (spawn) {
  495. lua_interface->SetSpawnValue(state, spawn);
  496. return 1;
  497. }
  498. }
  499. return 0;
  500. }
  501. int EQ2Emu_lua_GetSpawnID(lua_State* state) {
  502. Spawn* spawn = lua_interface->GetSpawn(state);
  503. if (spawn) {
  504. lua_interface->SetInt32Value(state, spawn->GetDatabaseID());
  505. return 1;
  506. }
  507. return 0;
  508. }
  509. int EQ2Emu_lua_GetSpawnGroupID(lua_State* state) {
  510. Spawn* spawn = lua_interface->GetSpawn(state);
  511. if (spawn) {
  512. lua_interface->SetInt32Value(state, spawn->GetSpawnGroupID());
  513. return 1;
  514. }
  515. return 0;
  516. }
  517. int EQ2Emu_lua_GetSpawnLocationID(lua_State* state) {
  518. Spawn* spawn = lua_interface->GetSpawn(state);
  519. if (spawn) {
  520. lua_interface->SetInt32Value(state, spawn->GetSpawnLocationID());
  521. return 1;
  522. }
  523. return 0;
  524. }
  525. int EQ2Emu_lua_GetSpawnLocationPlacementID(lua_State* state) {
  526. Spawn* spawn = lua_interface->GetSpawn(state);
  527. if (spawn) {
  528. lua_interface->SetInt32Value(state, spawn->GetSpawnLocationPlacementID());
  529. return 1;
  530. }
  531. return 0;
  532. }
  533. int EQ2Emu_lua_GetFactionAmount(lua_State* state) {
  534. Player* player = (Player*)lua_interface->GetSpawn(state);
  535. int32 faction_id = lua_interface->GetInt32Value(state, 2);
  536. if (player && player->IsPlayer() && faction_id > 0) {
  537. lua_interface->SetInt32Value(state, player->GetFactions()->GetFactionValue(faction_id));
  538. return 1;
  539. }
  540. return 0;
  541. }
  542. int EQ2Emu_lua_SetFactionID(lua_State* state) {
  543. if (!lua_interface)
  544. return 0;
  545. Spawn* spawn = lua_interface->GetSpawn(state);
  546. int32 value = lua_interface->GetInt32Value(state, 2);
  547. if (spawn) {
  548. spawn->SetFactionID(value);
  549. }
  550. return 0;
  551. }
  552. int EQ2Emu_lua_GetFactionID(lua_State* state) {
  553. Spawn* spawn = lua_interface->GetSpawn(state);
  554. if (spawn) {
  555. lua_interface->SetInt32Value(state, spawn->GetFactionID());
  556. return 1;
  557. }
  558. return 0;
  559. }
  560. int EQ2Emu_lua_GetGender(lua_State* state) {
  561. Spawn* spawn = lua_interface->GetSpawn(state);
  562. if (spawn) {
  563. lua_interface->SetInt32Value(state, spawn->GetGender());
  564. return 1;
  565. }
  566. return 0;
  567. }
  568. int EQ2Emu_lua_GetTarget(lua_State* state) {
  569. Spawn* spawn = lua_interface->GetSpawn(state);
  570. if (spawn && spawn->IsEntity() && ((Entity*)spawn)->GetTarget()) {
  571. lua_interface->SetSpawnValue(state, ((Entity*)spawn)->GetTarget());
  572. return 1;
  573. }
  574. return 0;
  575. }
  576. int EQ2Emu_lua_PlayVoice(lua_State* state) {
  577. if (!lua_interface)
  578. return 0;
  579. Spawn* spawn = lua_interface->GetSpawn(state);
  580. string mp3_string = lua_interface->GetStringValue(state, 2);
  581. int32 key1 = lua_interface->GetInt32Value(state, 3);
  582. int32 key2 = lua_interface->GetInt32Value(state, 4);
  583. Spawn* player = lua_interface->GetSpawn(state, 5);
  584. if (spawn && mp3_string.length() > 0) {
  585. Client* client = 0;
  586. if (player && player->IsPlayer())
  587. client = spawn->GetZone()->GetClientBySpawn(player);
  588. if (client) {
  589. if (((Player*)player)->WasSentSpawn(spawn->GetID()) && !((Player*)player)->WasSpawnRemoved(spawn))
  590. spawn->GetZone()->PlayVoice(client, spawn, mp3_string.c_str(), key1, key2);
  591. }
  592. else
  593. spawn->GetZone()->PlayVoice(spawn, mp3_string.c_str(), key1, key2);
  594. }
  595. return 0;
  596. }
  597. int EQ2Emu_lua_GetCurrentZoneSafeLocation(lua_State* state) {
  598. if (!lua_interface)
  599. return 0;
  600. Spawn* spawn = lua_interface->GetSpawn(state);
  601. if (spawn) {
  602. lua_interface->SetFloatValue(state, spawn->GetZone()->GetSafeX());
  603. lua_interface->SetFloatValue(state, spawn->GetZone()->GetSafeY());
  604. lua_interface->SetFloatValue(state, spawn->GetZone()->GetSafeZ());
  605. return 3;
  606. }
  607. return 0;
  608. }
  609. int EQ2Emu_lua_HasLootItem(lua_State* state) {
  610. if (!lua_interface)
  611. return 0;
  612. Spawn* spawn = lua_interface->GetSpawn(state);
  613. if (spawn) {
  614. int32 item_id = lua_interface->GetInt32Value(state, 2);
  615. lua_interface->SetBooleanValue(state, spawn->HasLootItemID(item_id));
  616. return 1;
  617. }
  618. return 0;
  619. }
  620. int EQ2Emu_lua_AddLootItem(lua_State* state) {
  621. if (!lua_interface)
  622. return 0;
  623. Spawn* spawn = lua_interface->GetSpawn(state);
  624. if (spawn && spawn->IsEntity()) {
  625. int32 item_id = lua_interface->GetInt32Value(state, 2);
  626. int16 charges = lua_interface->GetInt16Value(state, 3);
  627. if (charges == 0)
  628. charges = 1;
  629. ((Entity*)spawn)->AddLootItem(item_id, charges);
  630. }
  631. return 0;
  632. }
  633. int EQ2Emu_lua_RemoveLootItem(lua_State* state) {
  634. if (!lua_interface)
  635. return 0;
  636. Spawn* spawn = lua_interface->GetSpawn(state);
  637. if (spawn && spawn->IsEntity()) {
  638. int32 item_id = lua_interface->GetInt32Value(state, 2);
  639. spawn->LootItem(item_id);
  640. }
  641. return 0;
  642. }
  643. int EQ2Emu_lua_AddLootCoin(lua_State* state) {
  644. if (!lua_interface)
  645. return 0;
  646. Spawn* spawn = lua_interface->GetSpawn(state);
  647. if (spawn) {
  648. int32 val = lua_interface->GetInt32Value(state, 2);
  649. spawn->AddLootCoins(val);
  650. }
  651. return 0;
  652. }
  653. int EQ2Emu_lua_GiveLoot(lua_State* state) {
  654. if (!lua_interface)
  655. return 0;
  656. Spawn* entity = lua_interface->GetSpawn(state);
  657. Spawn* player = lua_interface->GetSpawn(state, 2);
  658. if (entity && player && player->IsPlayer()) {
  659. int32 coins = lua_interface->GetInt32Value(state, 3);
  660. vector<Item*>* items = 0;
  661. int i = 0;
  662. int32 item_id = 0;
  663. while ((item_id = lua_interface->GetInt32Value(state, 4 + i))) {
  664. if (items == 0)
  665. items = new vector<Item*>;
  666. if (master_item_list.GetItem(item_id))
  667. items->push_back(master_item_list.GetItem(item_id));
  668. i++;
  669. }
  670. Client* client = 0;
  671. client = player->GetZone()->GetClientBySpawn(player);
  672. if (client)
  673. ((Player*)player)->AddPendingLootItems(entity->GetID(), items);
  674. if(coins > 0)
  675. entity->AddLootCoins(coins);
  676. safe_delete(items);
  677. }
  678. return 0;
  679. }
  680. int EQ2Emu_lua_HasPendingLootItem(lua_State* state) {
  681. if (!lua_interface)
  682. return 0;
  683. Spawn* entity = lua_interface->GetSpawn(state);
  684. Spawn* player = lua_interface->GetSpawn(state, 2);
  685. int32 item_id = lua_interface->GetInt32Value(state, 3);
  686. if (entity && entity->IsEntity() && player && player->IsPlayer() && item_id > 0) {
  687. lua_interface->SetBooleanValue(state, ((Player*)player)->HasPendingLootItem(entity->GetID(), item_id));
  688. return 1;
  689. }
  690. return 0;
  691. }
  692. int EQ2Emu_lua_HasPendingLoot(lua_State* state) {
  693. if (!lua_interface)
  694. return 0;
  695. Spawn* entity = lua_interface->GetSpawn(state);
  696. Spawn* player = lua_interface->GetSpawn(state, 2);
  697. if (entity && player && player->IsPlayer()) {
  698. lua_interface->SetBooleanValue(state, ((Player*)player)->HasPendingLootItems(entity->GetID()));
  699. return 1;
  700. }
  701. return 0;
  702. }
  703. int EQ2Emu_lua_CreateConversation(lua_State* state) {
  704. if (!lua_interface)
  705. return 0;
  706. vector<ConversationOption>* conversation = lua_interface->GetConversation(state);
  707. safe_delete(conversation);
  708. conversation = new vector<ConversationOption>();
  709. lua_interface->SetConversationValue(state, conversation);
  710. return 1;
  711. }
  712. int EQ2Emu_lua_AddConversationOption(lua_State* state) {
  713. if (!lua_interface)
  714. return 0;
  715. vector<ConversationOption>* conversation = lua_interface->GetConversation(state);
  716. if (conversation) {
  717. ConversationOption conv_option;
  718. conv_option.option = lua_interface->GetStringValue(state, 2);
  719. conv_option.function = lua_interface->GetStringValue(state, 3);
  720. if (conv_option.option.length() > 0)
  721. conversation->push_back(conv_option);
  722. }
  723. return 0;
  724. }
  725. int EQ2Emu_lua_CloseConversation(lua_State* state) {
  726. if (!lua_interface)
  727. return 0;
  728. Spawn* npc = lua_interface->GetSpawn(state);
  729. Spawn* player = lua_interface->GetSpawn(state, 2);
  730. if (npc && player && player->IsPlayer() && player->GetZone()) {
  731. Client* client = player->GetZone()->GetClientBySpawn(player);
  732. if (client) {
  733. int32 conversation_id = client->GetConversationID(npc, 0);
  734. client->CloseDialog(conversation_id);
  735. }
  736. }
  737. return 0;
  738. }
  739. int EQ2Emu_lua_CloseItemConversation(lua_State* state) {
  740. if (!lua_interface)
  741. return 0;
  742. Item* item = lua_interface->GetItem(state);
  743. Spawn* player = lua_interface->GetSpawn(state, 2);
  744. if (item && player && player->IsPlayer() && player->GetZone()) {
  745. Client* client = player->GetZone()->GetClientBySpawn(player);
  746. if (client) {
  747. int32 conversation_id = client->GetConversationID(0, item);
  748. client->CloseDialog(conversation_id);
  749. }
  750. }
  751. return 0;
  752. }
  753. int EQ2Emu_lua_StartDialogConversation(lua_State* state) {
  754. if (!lua_interface)
  755. return 0;
  756. vector<ConversationOption>* conversation = lua_interface->GetConversation(state);
  757. Spawn* spawn = 0;
  758. Item* item = 0;
  759. int8 type = lua_interface->GetInt8Value(state, 2);
  760. if (type == 1 || type == 3)
  761. spawn = lua_interface->GetSpawn(state, 3);
  762. else if (type == 2 || type == 4)
  763. item = lua_interface->GetItem(state, 3);
  764. Spawn* player = lua_interface->GetSpawn(state, 4);
  765. string text = lua_interface->GetStringValue(state, 5);
  766. string mp3 = lua_interface->GetStringValue(state, 6);
  767. int32 key1 = lua_interface->GetInt32Value(state, 7);
  768. int32 key2 = lua_interface->GetInt32Value(state, 8);
  769. if (conversation && text.length() > 0 && (spawn || item) && player && player->IsPlayer()) {
  770. Client* client = player->GetZone()->GetClientBySpawn(player);
  771. if (client) {
  772. if (spawn) {
  773. // Need to do this so the function works the same as it did before
  774. if (type == 1)
  775. type++;
  776. if (mp3.length() > 0)
  777. client->DisplayConversation((Entity*)spawn, type, conversation, const_cast<char*>(text.c_str()), mp3.c_str(), key1, key2);
  778. else
  779. client->DisplayConversation((Entity*)spawn, type, conversation, const_cast<char*>(text.c_str()));
  780. }
  781. else {
  782. if (mp3.length() > 0)
  783. client->DisplayConversation(item, conversation, const_cast<char*>(text.c_str()), type, mp3.c_str(), key1, key2);
  784. else
  785. client->DisplayConversation(item, conversation, const_cast<char*>(text.c_str()), type);
  786. }
  787. }
  788. }
  789. safe_delete(conversation);
  790. lua_interface->SetConversationValue(state, NULL);
  791. return 0;
  792. }
  793. /*int EQ2Emu_lua_StartItemConversation(lua_State* state){
  794. if(!lua_interface)
  795. return 0;
  796. vector<ConversationOption>* conversation = lua_interface->GetConversation(state);
  797. Item* item = lua_interface->GetItem(state, 2);
  798. Spawn* player = lua_interface->GetSpawn(state, 3);
  799. string text = lua_interface->GetStringValue(state, 4);
  800. string mp3 = lua_interface->GetStringValue(state, 5);
  801. int32 key1 = lua_interface->GetInt32Value(state, 6);
  802. int32 key2 = lua_interface->GetInt32Value(state, 7);
  803. if(conversation && text.length() > 0 && item && player && player->IsPlayer()){
  804. Client* client = player->GetZone()->GetClientBySpawn(player);
  805. if(client){
  806. if(mp3.length() > 0)
  807. client->DisplayConversation(item, conversation, (char*)text.c_str(), mp3.c_str(), key1, key2);
  808. else
  809. client->DisplayConversation(item, conversation, (char*)text.c_str());
  810. }
  811. safe_delete(conversation);
  812. }
  813. return 0;
  814. }*/
  815. int EQ2Emu_lua_StartConversation(lua_State* state) {
  816. if (!lua_interface)
  817. return 0;
  818. vector<ConversationOption>* conversation = lua_interface->GetConversation(state);
  819. Spawn* npc = lua_interface->GetSpawn(state, 2);
  820. Spawn* player = lua_interface->GetSpawn(state, 3);
  821. string text = lua_interface->GetStringValue(state, 4);
  822. string mp3 = lua_interface->GetStringValue(state, 5);
  823. int32 key1 = lua_interface->GetInt32Value(state, 6);
  824. int32 key2 = lua_interface->GetInt32Value(state, 7);
  825. if (conversation && conversation->size() > 0 && text.length() > 0 && npc && npc->IsEntity() && player && player->IsPlayer()) {
  826. Client* client = npc->GetZone()->GetClientBySpawn(player);
  827. if (mp3.length() > 0)
  828. client->DisplayConversation((Entity*)npc, 1, conversation, text.c_str(), mp3.c_str(), key1, key2);
  829. else
  830. client->DisplayConversation((Entity*)npc, 1, conversation, text.c_str());
  831. safe_delete(conversation);
  832. lua_interface->SetConversationValue(state, NULL);
  833. }
  834. else
  835. 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);
  836. return 0;
  837. }
  838. int EQ2Emu_lua_SetPlayerProximityFunction(lua_State* state) {
  839. if (!lua_interface)
  840. return 0;
  841. Spawn* spawn = lua_interface->GetSpawn(state);
  842. float distance = lua_interface->GetFloatValue(state, 2);
  843. string in_range_function = lua_interface->GetStringValue(state, 3);
  844. string leaving_range_function = lua_interface->GetStringValue(state, 4);
  845. if (spawn && !spawn->IsPlayer() && distance > 0 && in_range_function.length() > 0)
  846. spawn->GetZone()->AddPlayerProximity(spawn, distance, in_range_function, leaving_range_function);
  847. return 0;
  848. }
  849. int EQ2Emu_lua_SetLocationProximityFunction(lua_State* state) {
  850. ZoneServer* zone = lua_interface->GetZone(state);
  851. float x = lua_interface->GetFloatValue(state, 2);
  852. float y = lua_interface->GetFloatValue(state, 3);
  853. float z = lua_interface->GetFloatValue(state, 4);
  854. float max_variation = lua_interface->GetFloatValue(state, 5);
  855. string in_range_function = lua_interface->GetStringValue(state, 6);
  856. string leaving_range_function = lua_interface->GetStringValue(state, 7);
  857. if (zone && in_range_function.length() > 0)
  858. zone->AddLocationProximity(x, y, z, max_variation, in_range_function, leaving_range_function);
  859. return 0;
  860. }
  861. int EQ2Emu_lua_SetLootCoin(lua_State* state) {
  862. if (!lua_interface)
  863. return 0;
  864. Spawn* spawn = lua_interface->GetSpawn(state);
  865. if (spawn && spawn->IsEntity()) {
  866. int32 val = lua_interface->GetInt32Value(state, 2);
  867. ((Entity*)spawn)->SetLootCoins(val);
  868. }
  869. return 0;
  870. }
  871. int EQ2Emu_lua_GetLootCoin(lua_State* state) {
  872. if (!lua_interface)
  873. return 0;
  874. Spawn* spawn = lua_interface->GetSpawn(state);
  875. if (spawn && spawn->IsEntity()) {
  876. lua_interface->SetInt32Value(state, ((Entity*)spawn)->GetLootCoins());
  877. return 1;
  878. }
  879. return 0;
  880. }
  881. int EQ2Emu_lua_MovementLoopAdd(lua_State* state) {
  882. if (!lua_interface)
  883. return 0;
  884. Spawn* spawn = lua_interface->GetSpawn(state);
  885. float x = lua_interface->GetFloatValue(state, 2);
  886. float y = lua_interface->GetFloatValue(state, 3);
  887. float z = lua_interface->GetFloatValue(state, 4);
  888. float speed = lua_interface->GetFloatValue(state, 5);
  889. int32 delay = lua_interface->GetInt32Value(state, 6); //this should be given as seconds, as it is converted to ms later
  890. string function = lua_interface->GetStringValue(state, 7);
  891. if (spawn) {
  892. spawn->AddMovementLocation(x, y, z, speed, delay, function.c_str());
  893. spawn->GetZone()->AddMovementNPC(spawn);
  894. }
  895. lua_interface->ResetFunctionStack(state);
  896. return 0;
  897. }
  898. int EQ2Emu_lua_IsPlayer(lua_State* state) {
  899. if (!lua_interface)
  900. return 0;
  901. Spawn* spawn = lua_interface->GetSpawn(state);
  902. if (spawn) {
  903. lua_interface->SetBooleanValue(state, spawn->IsPlayer());
  904. return 1;
  905. }
  906. return 0;
  907. }
  908. int EQ2Emu_lua_FaceTarget(lua_State* state) {
  909. if (!lua_interface)
  910. return 0;
  911. Spawn* spawn = lua_interface->GetSpawn(state);
  912. Spawn* target = lua_interface->GetSpawn(state, 2);
  913. if (spawn && target) {
  914. if (spawn->IsEntity())
  915. // ((Entity*)spawn)->FaceTarget(target);
  916. static_cast<Entity*>(spawn)->FaceTarget(target);
  917. }
  918. lua_interface->ResetFunctionStack(state);
  919. return 0;
  920. }
  921. int EQ2Emu_lua_MoveToLocation(lua_State* state) {
  922. if (!lua_interface)
  923. return 0;
  924. Spawn* spawn = lua_interface->GetSpawn(state);
  925. float x = lua_interface->GetFloatValue(state, 2);
  926. float y = lua_interface->GetFloatValue(state, 3);
  927. float z = lua_interface->GetFloatValue(state, 4);
  928. float speed = lua_interface->GetFloatValue(state, 5);
  929. string lua_function = lua_interface->GetStringValue(state, 6);
  930. bool more_points = lua_interface->GetBooleanValue(state, 7);
  931. if (spawn) {
  932. if (speed == 0)
  933. speed = spawn->GetSpeed();
  934. spawn->AddRunningLocation(x, y, z, speed, 0.0f, true, !more_points, lua_function);
  935. }
  936. lua_interface->ResetFunctionStack(state);
  937. return 0;
  938. }
  939. int EQ2Emu_lua_ClearRunningLocations(lua_State* state) {
  940. if (!lua_interface)
  941. return 0;
  942. Spawn* spawn = lua_interface->GetSpawn(state);
  943. if (spawn) {
  944. spawn->ClearRunningLocations();
  945. }
  946. return 0;
  947. }
  948. int EQ2Emu_lua_Say(lua_State* state) {
  949. if (!lua_interface)
  950. return 0;
  951. Spawn* spawn = lua_interface->GetSpawn(state);
  952. string message = lua_interface->GetStringValue(state, 2);
  953. Spawn* player = lua_interface->GetSpawn(state, 3);
  954. int32 language = lua_interface->GetInt32Value(state, 4);
  955. if (spawn && message.length() > 0) {
  956. Client* client = 0;
  957. if (player && player->IsPlayer())
  958. client = spawn->GetZone()->GetClientBySpawn(player);
  959. if (client)
  960. spawn->GetZone()->HandleChatMessage(client, spawn, 0, CHANNEL_SAY, message.c_str(), 30, 0, true, language);
  961. else
  962. spawn->GetZone()->HandleChatMessage(spawn, 0, CHANNEL_SAY, message.c_str(), 30, 0, true, language);
  963. }
  964. lua_interface->ResetFunctionStack(state);
  965. return 0;
  966. }
  967. int EQ2Emu_lua_Shout(lua_State* state) {
  968. if (!lua_interface)
  969. return 0;
  970. Spawn* spawn = lua_interface->GetSpawn(state);
  971. string message = lua_interface->GetStringValue(state, 2);
  972. Spawn* player = lua_interface->GetSpawn(state, 3);
  973. if (spawn && message.length() > 0) {
  974. Client* client = 0;
  975. if (player && player->IsPlayer())
  976. client = spawn->GetZone()->GetClientBySpawn(player);
  977. if (client)
  978. spawn->GetZone()->HandleChatMessage(client, spawn, 0, CHANNEL_SHOUT, message.c_str(), 30);
  979. else
  980. spawn->GetZone()->HandleChatMessage(spawn, 0, CHANNEL_SHOUT, message.c_str(), 30);
  981. }
  982. lua_interface->ResetFunctionStack(state);
  983. return 0;
  984. }
  985. int EQ2Emu_lua_SayOOC(lua_State* state) {
  986. if (!lua_interface)
  987. return 0;
  988. Spawn* spawn = lua_interface->GetSpawn(state);
  989. string message = lua_interface->GetStringValue(state, 2);
  990. Spawn* player = lua_interface->GetSpawn(state, 3);
  991. if (spawn && message.length() > 0) {
  992. Client* client = 0;
  993. if (player && player->IsPlayer())
  994. client = spawn->GetZone()->GetClientBySpawn(player);
  995. if (client)
  996. spawn->GetZone()->HandleChatMessage(client, spawn, 0, CHANNEL_OUT_OF_CHARACTER, message.c_str(), 30);
  997. else
  998. spawn->GetZone()->HandleChatMessage(spawn, 0, CHANNEL_OUT_OF_CHARACTER, message.c_str(), 30);
  999. }
  1000. lua_interface->ResetFunctionStack(state);
  1001. return 0;
  1002. }
  1003. int EQ2Emu_lua_Emote(lua_State* state) {
  1004. if (!lua_interface)
  1005. return 0;
  1006. Spawn* spawn = lua_interface->GetSpawn(state);
  1007. string message = lua_interface->GetStringValue(state, 2);
  1008. Spawn* spawn2 = lua_interface->GetSpawn(state, 3);
  1009. Spawn* player = lua_interface->GetSpawn(state, 4);
  1010. char* to = 0;
  1011. if (spawn2)
  1012. to = spawn2->GetName();
  1013. if (spawn && message.length() > 0) {
  1014. Client* client = 0;
  1015. if (player && player->IsPlayer())
  1016. client = spawn->GetZone()->GetClientBySpawn(player);
  1017. if (client)
  1018. spawn->GetZone()->HandleChatMessage(client, spawn, to, CHANNEL_EMOTE, message.c_str(), 30);
  1019. else
  1020. spawn->GetZone()->HandleChatMessage(spawn, to, CHANNEL_EMOTE, message.c_str(), 30);
  1021. }
  1022. lua_interface->ResetFunctionStack(state);
  1023. return 0;
  1024. }
  1025. int EQ2Emu_lua_SpellHeal(lua_State* state) {
  1026. if (!lua_interface)
  1027. return 0;
  1028. LuaSpell* luaspell = lua_interface->GetCurrentSpell(state);
  1029. if (!luaspell)
  1030. return 0;
  1031. Spawn* caster = luaspell->caster;
  1032. string heal_type = lua_interface->GetStringValue(state);//power, heal ect
  1033. int32 min_heal = lua_interface->GetInt32Value(state, 2);
  1034. int32 max_heal = lua_interface->GetInt32Value(state, 3);
  1035. Spawn* target = lua_interface->GetSpawn(state, 4);
  1036. int8 crit_mod = lua_interface->GetInt32Value(state, 5);
  1037. bool no_calcs = lua_interface->GetInt32Value(state, 6) == 1;
  1038. string custom_spell_name = lua_interface->GetStringValue(state, 7);//custom spell name
  1039. lua_interface->ResetFunctionStack(state);
  1040. if (caster && caster->IsEntity()) {
  1041. bool success = false;
  1042. luaspell->resisted = false;
  1043. if (target) {
  1044. float distance = caster->GetDistance(target, true);
  1045. if (((Entity*)caster)->SpellHeal(target, distance, luaspell, heal_type, min_heal, max_heal, crit_mod, no_calcs, custom_spell_name))
  1046. success = true;
  1047. }
  1048. if (luaspell->targets.size() > 0) {
  1049. Spawn* target = 0;
  1050. ZoneServer* zone = luaspell->caster->GetZone();
  1051. luaspell->MSpellTargets.readlock(__FUNCTION__, __LINE__);
  1052. for (int32 i = 0; i < luaspell->targets.size(); i++) {
  1053. if ((target = zone->GetSpawnByID(luaspell->targets[i]))) {
  1054. float distance = caster->GetDistance(target, true);
  1055. ((Entity*)caster)->SpellHeal(target, distance, luaspell, heal_type, min_heal, max_heal, crit_mod, no_calcs, custom_spell_name);
  1056. }
  1057. }
  1058. luaspell->MSpellTargets.releasereadlock(__FUNCTION__, __LINE__);
  1059. success = true;
  1060. }
  1061. if (success) {
  1062. if (caster->GetZone())
  1063. caster->GetZone()->TriggerCharSheetTimer();
  1064. }
  1065. }
  1066. return 0;
  1067. }
  1068. int EQ2Emu_lua_AddItem(lua_State* state) {
  1069. if (!lua_interface)
  1070. return 0;
  1071. Spawn* spawn = lua_interface->GetSpawn(state);
  1072. int32 item_id = lua_interface->GetInt32Value(state, 2);
  1073. int16 quantity = lua_interface->GetInt32Value(state, 3);
  1074. // default of 1 quantity to add
  1075. if (quantity == 0)
  1076. quantity = 1;
  1077. if (spawn && spawn->IsPlayer()) {
  1078. Client* client = spawn->GetZone()->GetClientBySpawn(spawn);
  1079. if (client && item_id > 0) {
  1080. lua_interface->SetBooleanValue(state, client->AddItem(item_id, quantity));
  1081. return 1;
  1082. }
  1083. }
  1084. lua_interface->SetBooleanValue(state, false);
  1085. return 1;
  1086. }
  1087. int EQ2Emu_lua_SummonItem(lua_State* state) {
  1088. if (!lua_interface)
  1089. return 0;
  1090. Spawn* spawn = lua_interface->GetSpawn(state);
  1091. int32 item_id = lua_interface->GetInt32Value(state, 2);
  1092. bool send_messages = (lua_interface->GetInt8Value(state, 3) == 1);
  1093. string location = lua_interface->GetStringValue(state, 4);
  1094. if (spawn && spawn->IsPlayer()) {
  1095. Client* client = spawn->GetZone()->GetClientBySpawn(spawn);
  1096. if (client && item_id > 0) {
  1097. if (strncasecmp(location.c_str(), "bank", 4) == 0)
  1098. lua_interface->SetBooleanValue(state, client->AddItemToBank(item_id, 1));
  1099. else
  1100. lua_interface->SetBooleanValue(state, client->AddItem(item_id, 1));
  1101. if (send_messages) {
  1102. Item* item = master_item_list.GetItem(item_id);
  1103. if (item) {
  1104. client->Message(CHANNEL_COLOR_YELLOW, "You receive %s.", item->CreateItemLink(client->GetVersion()).c_str());
  1105. string popup_text = "You receive " + item->name;
  1106. client->SendPopupMessage(10, popup_text.c_str(), "ui_harvested_normal", 3, 0xFF, 0xFF, 0xFF);
  1107. }
  1108. }
  1109. return 1;
  1110. }
  1111. }
  1112. lua_interface->SetBooleanValue(state, false);
  1113. return 1;
  1114. }
  1115. int EQ2Emu_lua_RemoveItem(lua_State* state) {
  1116. Spawn* spawn = lua_interface->GetSpawn(state);
  1117. int32 item_id = lua_interface->GetInt32Value(state, 2);
  1118. int16 quantity = lua_interface->GetInt16Value(state, 3);
  1119. // default of 1 to remove
  1120. if (quantity == 0)
  1121. quantity = 1;
  1122. Client* client;
  1123. Item* item;
  1124. if (spawn && spawn->IsPlayer() && item_id > 0) {
  1125. if ((client = spawn->GetZone()->GetClientBySpawn(spawn))) {
  1126. if ((item = client->GetPlayer()->item_list.GetItemFromID(item_id))) {
  1127. if (client->RemoveItem(item, quantity)) {
  1128. lua_interface->SetBooleanValue(state, true);
  1129. return 1;
  1130. }
  1131. }
  1132. }
  1133. }
  1134. lua_interface->SetBooleanValue(state, false);
  1135. return 1;
  1136. }
  1137. int EQ2Emu_lua_HasItem(lua_State* state) {
  1138. Spawn* player = lua_interface->GetSpawn(state);
  1139. int32 item_id = lua_interface->GetInt32Value(state, 2);
  1140. bool include_bank = lua_interface->GetInt8Value(state, 3);
  1141. if (player && player->IsPlayer()) {
  1142. bool hasItem = false;
  1143. hasItem = ((Player*)player)->item_list.HasItem(item_id, include_bank);
  1144. if (!hasItem)
  1145. hasItem = ((Player*)player)->GetEquipmentList()->HasItem(item_id);
  1146. lua_interface->SetBooleanValue(state, hasItem);
  1147. return 1;
  1148. }
  1149. lua_interface->SetBooleanValue(state, false);
  1150. return 1;
  1151. }
  1152. int EQ2Emu_lua_Spawn(lua_State* state) {
  1153. if (!lua_interface)
  1154. return 0;
  1155. ZoneServer* zone = lua_interface->GetZone(state);
  1156. int32 spawn_id = lua_interface->GetInt32Value(state, 2);
  1157. bool restricted_npc = (lua_interface->GetInt8Value(state, 3) == 1);
  1158. float x = lua_interface->GetFloatValue(state, 4);
  1159. float y = lua_interface->GetFloatValue(state, 5);
  1160. float z = lua_interface->GetFloatValue(state, 6);
  1161. float heading = lua_interface->GetFloatValue(state, 7);
  1162. if (zone && spawn_id > 0 && (x != 0 || y != 0 || z != 0)) {
  1163. Spawn* spawn = zone->GetSpawn(spawn_id);
  1164. if (!spawn)
  1165. lua_interface->LogError("%s: LUA Spawn command error: Could not find spawn with id of %u.", lua_interface->GetScriptName(state), spawn_id);
  1166. else {
  1167. spawn->SetX(x);
  1168. spawn->SetZ(z);
  1169. spawn->SetY(y,true,true);
  1170. spawn->SetLocation(zone->GetClosestLocation(spawn));
  1171. spawn->SetHeading(heading);
  1172. if (restricted_npc)
  1173. spawn->AddAllowAccessSpawn(spawn);
  1174. const char* spawn_script = world.GetSpawnScript(spawn_id);
  1175. bool scriptActive = false;
  1176. if (spawn_script && lua_interface->GetSpawnScript(spawn_script) != 0) {
  1177. scriptActive = true;
  1178. spawn->SetSpawnScript(string(spawn_script));
  1179. }
  1180. zone->CallSpawnScript(spawn, SPAWN_SCRIPT_PRESPAWN);
  1181. zone->AddSpawn(spawn);
  1182. if (scriptActive) {
  1183. zone->CallSpawnScript(spawn, SPAWN_SCRIPT_SPAWN);
  1184. }
  1185. lua_interface->SetSpawnValue(state, spawn);
  1186. return 1;
  1187. }
  1188. }
  1189. else {
  1190. string output = "Invalid paramaters to LUA Spawn command: \n";
  1191. if (!zone)
  1192. output = output.append("\t").append("Missing zone reference. \n");
  1193. if (spawn_id == 0)
  1194. output = output.append("\t").append("Missing spawn_id.");
  1195. lua_interface->LogError("%s: Error in EQ2Emu_lua_Spawn - %s", lua_interface->GetScriptName(state), output.c_str());
  1196. }
  1197. return 0;
  1198. }
  1199. int EQ2Emu_lua_GetZoneName(lua_State* state) {
  1200. if (!lua_interface)
  1201. return 0;
  1202. ZoneServer* zone = lua_interface->GetZone(state);
  1203. if (zone) {
  1204. lua_interface->SetStringValue(state, zone->GetZoneName());
  1205. return 1;
  1206. }
  1207. return 0;
  1208. }
  1209. int EQ2Emu_lua_GetZoneID(lua_State* state) {
  1210. if (!lua_interface)
  1211. return 0;
  1212. ZoneServer* zone = lua_interface->GetZone(state);
  1213. if (zone) {
  1214. lua_interface->SetInt32Value(state, zone->GetZoneID());
  1215. return 1;
  1216. }
  1217. return 0;
  1218. }
  1219. int EQ2Emu_lua_GetZone(lua_State* state) {
  1220. if (!lua_interface)
  1221. return 0;
  1222. int32 zone_id = lua_interface->GetInt32Value(state);
  1223. ZoneServer* zone = 0;
  1224. if (zone_id > 0)
  1225. zone = zone_list.Get(zone_id);
  1226. else {
  1227. string zone_name = lua_interface->GetStringValue(state);
  1228. if (zone_name.length() > 0) {
  1229. zone = zone_list.Get(zone_name.c_str());
  1230. }
  1231. else {
  1232. Spawn* spawn = lua_interface->GetSpawn(state);
  1233. if (spawn)
  1234. zone = spawn->GetZone();
  1235. }
  1236. }
  1237. if (zone) {
  1238. lua_interface->SetZoneValue(state, zone);
  1239. return 1;
  1240. }
  1241. return 0;
  1242. }
  1243. int EQ2Emu_lua_AddHate(lua_State* state) {
  1244. Spawn* entity = lua_interface->GetSpawn(state);
  1245. Spawn* npc = lua_interface->GetSpawn(state, 2);
  1246. sint32 amount = lua_interface->GetSInt32Value(state, 3);
  1247. bool send_packet = lua_interface->GetInt8Value(state, 4) == 1 ? true : false;
  1248. LuaSpell* luaspell = lua_interface->GetCurrentSpell(state);
  1249. if (entity && entity->IsEntity() && amount != 0) {
  1250. if (luaspell) {
  1251. ZoneServer* zone = luaspell->caster->GetZone();
  1252. luaspell->MSpellTargets.readlock(__FUNCTION__, __LINE__);
  1253. for (int32 i = 0; i < luaspell->targets.size(); i++) {
  1254. Spawn* spawn = zone->GetSpawnByID(luaspell->targets.at(i));
  1255. if (spawn && spawn->IsNPC() && spawn->Alive() && spawn->GetZone()) {
  1256. ((NPC*)spawn)->AddHate((Entity*)entity, amount);
  1257. if (send_packet)
  1258. entity->GetZone()->SendThreatPacket(entity, npc, amount, luaspell->spell->GetName());
  1259. }
  1260. }
  1261. luaspell->MSpellTargets.releasereadlock(__FUNCTION__, __LINE__);
  1262. }
  1263. else if (npc && npc->IsNPC() && npc->GetZone())
  1264. ((NPC*)npc)->AddHate((Entity*)entity, amount);
  1265. }
  1266. return 0;
  1267. }
  1268. int EQ2Emu_lua_Zone(lua_State* state) {
  1269. if (!lua_interface)
  1270. return 0;
  1271. ZoneServer* zone = lua_interface->GetZone(state);
  1272. Spawn* player = lua_interface->GetSpawn(state, 2);
  1273. Client* client = 0;
  1274. if (player && player->IsPlayer())
  1275. client = player->GetZone()->GetClientBySpawn(player);
  1276. float x = lua_interface->GetFloatValue(state, 3);
  1277. float y = lua_interface->GetFloatValue(state, 4);
  1278. float z = lua_interface->GetFloatValue(state, 5);
  1279. float heading = lua_interface->GetFloatValue(state, 6);
  1280. if (zone && client) {
  1281. LogWrite(LUA__DEBUG, 0, "LUA", "LUA Zone Request by Player: '%s' (%u)", player->GetName(), player->GetID());
  1282. LogWrite(LUA__DEBUG, 5, "LUA", "\tTo Zone: '%s' (%u)", zone->GetZoneName(), zone->GetZoneID());
  1283. if (!client->CheckZoneAccess(zone->GetZoneName()))
  1284. {
  1285. LogWrite(LUA__WARNING, 0, "LUA", "CheckZoneAccess() FAILED! LUA Zone Request Denied!");
  1286. return 0;
  1287. }
  1288. if (x != 0 || y != 0 || z != 0) {
  1289. LogWrite(LUA__DEBUG, 5, "LUA", "\tTo Coordinates: %2f, %2f, %2f, %2f", x, y, z, heading);
  1290. player->SetX(x);
  1291. player->SetY(y);
  1292. player->SetZ(z);
  1293. player->SetHeading(heading);
  1294. client->Zone(zone->GetZoneName(), false);
  1295. }
  1296. else
  1297. client->Zone(zone->GetZoneName());
  1298. }
  1299. else
  1300. lua_interface->LogError("%s: Error in EQ2Emu_lua_Zone: invalid zone or spawn input.", lua_interface->GetScriptName(state));
  1301. return 0;
  1302. }
  1303. int EQ2Emu_lua_AddSpawnAccess(lua_State* state) {
  1304. if (!lua_interface)
  1305. return 0;
  1306. Spawn* spawn = lua_interface->GetSpawn(state);
  1307. Spawn* spawn2 = lua_interface->GetSpawn(state, 2);
  1308. if (spawn && spawn2)
  1309. spawn->AddAllowAccessSpawn(spawn2);
  1310. return 0;
  1311. }
  1312. int EQ2Emu_lua_CastSpell(lua_State* state) {
  1313. if (!lua_interface)
  1314. return 0;
  1315. Spawn* target = lua_interface->GetSpawn(state);
  1316. int32 spell_id = lua_interface->GetInt32Value(state, 2);
  1317. int8 spell_tier = lua_interface->GetInt8Value(state, 3);
  1318. Spawn* caster = lua_interface->GetSpawn(state, 4);
  1319. int16 custom_cast_time = lua_interface->GetInt16Value(state, 5);
  1320. if (!target) {
  1321. lua_interface->LogError("%s: LUA CastSpell command error: target is not a valid spawn", lua_interface->GetScriptName(state));
  1322. return 0;
  1323. }
  1324. if (!target->IsEntity()) {
  1325. lua_interface->LogError("%s: LUA CastSpell command error: target (%s) is not an entity", lua_interface->GetScriptName(state), target->GetName());
  1326. return 0;
  1327. }
  1328. if (spell_id <= 0) {
  1329. lua_interface->LogError("%s: LUA CastSpell command error: spell id is not valid", lua_interface->GetScriptName(state));
  1330. return 0;
  1331. }
  1332. if (caster && !caster->IsEntity()) {
  1333. lua_interface->LogError("%s: LUA CastSpell command error: caster (%s) is not an entity", lua_interface->GetScriptName(state), caster->GetName());
  1334. return 0;
  1335. }
  1336. if (spell_tier == 0)
  1337. spell_tier = 1;
  1338. if (!caster)
  1339. caster = target;
  1340. target->GetZone()->ProcessSpell(master_spell_list.GetSpell(spell_id, spell_tier), (Entity*)caster, (Entity*)target, true, false, NULL, custom_cast_time);
  1341. return 0;
  1342. }
  1343. int EQ2Emu_lua_SpellDamage(lua_State* state) {
  1344. if (!lua_interface)
  1345. return 0;
  1346. Spawn* target = lua_interface->GetSpawn(state);
  1347. int32 target_id = 0;
  1348. if (target)
  1349. target_id = target->GetID();
  1350. LuaSpell* luaspell = lua_interface->GetCurrentSpell(state);
  1351. if (!luaspell)
  1352. return 0;
  1353. Spawn* caster = luaspell->caster;
  1354. sint32 type = lua_interface->GetSInt32Value(state, 2);
  1355. int32 min_damage = lua_interface->GetInt32Value(state, 3);
  1356. int32 max_damage = lua_interface->GetInt32Value(state, 4);
  1357. int8 crit_mod = lua_interface->GetInt32Value(state, 5);
  1358. bool no_calcs = lua_interface->GetInt32Value(state, 6) == 1;
  1359. //lua_interface->ResetFunctionStack(state);
  1360. int32 class_id = lua_interface->GetInt32Value(state, 7);
  1361. vector<int16> faction_req;
  1362. vector<int16> race_req;
  1363. int32 class_req = 0;
  1364. int32 i = 0;
  1365. int8 f = 0;
  1366. int8 r = 0;
  1367. while ((class_id = lua_interface->GetInt32Value(state, 7 + i))) {
  1368. if (class_id < 100) {
  1369. class_req += pow(2.0, double(class_id - 1));
  1370. }
  1371. else if (class_id > 100 && class_id < 1000) {
  1372. race_req.push_back(class_id);
  1373. r++;
  1374. }
  1375. else {
  1376. faction_req.push_back(class_id);
  1377. f++;
  1378. }
  1379. i++;
  1380. }
  1381. if (caster && caster->IsEntity()) {
  1382. bool race_match = false;
  1383. bool success = false;
  1384. luaspell->resisted = false;
  1385. if (luaspell->initial_target == target_id) {
  1386. int xxx = 0;
  1387. }
  1388. if (luaspell->targets.size() > 0) {
  1389. ZoneServer* zone = luaspell->caster->GetZone();
  1390. Spawn* target = 0;
  1391. luaspell->MSpellTargets.readlock(__FUNCTION__, __LINE__);
  1392. for (int32 i = 0; i < luaspell->targets.size(); i++) {
  1393. if ((target = zone->GetSpawnByID(luaspell->targets[i]))) {
  1394. if (race_req.size() > 0) {
  1395. for (int8 i = 0; i < race_req.size(); i++) {
  1396. int32 xxx = target->GetLuaRaceId();
  1397. if (target->GetLuaRaceId() == race_req[i]) {
  1398. race_match = true;
  1399. }
  1400. }
  1401. }
  1402. else
  1403. race_match = true; // if the race_req.size = 0 then there is no race requirement and the race_match will be true
  1404. if (race_match == true) {
  1405. float distance = caster->GetDistance(target, true);
  1406. ((Entity*)caster)->SpellAttack(target, distance, luaspell, type, min_damage, max_damage, crit_mod, no_calcs);
  1407. }
  1408. }
  1409. }
  1410. success = true;
  1411. luaspell->MSpellTargets.releasereadlock(__FUNCTION__, __LINE__);
  1412. }
  1413. else if (target) {
  1414. //check class and race/faction here
  1415. if (race_req.size() > 0) {
  1416. for (int8 i = 0; i < race_req.size(); i++) {
  1417. if (target->GetLuaRaceId() == race_req[i]) {
  1418. race_match = true;
  1419. }
  1420. }
  1421. }
  1422. else
  1423. race_match = true; // if the race_req.size = 0 then there is no race requirement and the race_match will be true
  1424. if (race_match == true) {
  1425. float distance = caster->GetDistance(target, true);
  1426. if (((Entity*)caster)->SpellAttack(target, distance, luaspell, type, min_damage, max_damage, crit_mod, no_calcs))
  1427. success = true;
  1428. }
  1429. }
  1430. if (success) {
  1431. Spell* spell = luaspell->spell;
  1432. if (caster->IsPlayer() && spell && spell->GetSpellData()->target_type == 1 && spell->GetSpellData()->spell_book_type == 1) { //offense combat art
  1433. ((Player*)caster)->InCombat(true);
  1434. if (caster->GetZone())
  1435. caster->GetZone()->TriggerCharSheetTimer();
  1436. }
  1437. }
  1438. }
  1439. return 0;
  1440. }
  1441. int EQ2Emu_lua_ModifyPower(lua_State* state) {
  1442. if (!lua_interface)
  1443. return 0;
  1444. Spawn* spawn = lua_interface->GetSpawn(state);
  1445. sint32 value = lua_interface->GetSInt32Value(state, 2);
  1446. lua_interface->ResetFunctionStack(state);
  1447. if (spawn && value != 0) {
  1448. if (spawn->GetPower() + value > spawn->GetTotalPower())
  1449. spawn->SetPower(spawn->GetTotalPower());
  1450. else
  1451. spawn->SetPower(spawn->GetPower() + value);
  1452. }
  1453. return 0;
  1454. }
  1455. int EQ2Emu_lua_ModifyHP(lua_State* state) {
  1456. if (!lua_interface)
  1457. return 0;
  1458. Spawn* spawn = lua_interface->GetSpawn(state);
  1459. sint32 value = lua_interface->GetSInt32Value(state, 2);
  1460. lua_interface->ResetFunctionStack(state);
  1461. if (spawn && value != 0) {
  1462. if (spawn->GetHP() + value > spawn->GetTotalHP())
  1463. spawn->SetHP(spawn->GetTotalHP());
  1464. else
  1465. spawn->SetHP(spawn->GetHP() + value);
  1466. }
  1467. return 0;
  1468. }
  1469. int EQ2Emu_lua_ModifyMaxPower(lua_State* state) {
  1470. if (!lua_interface)
  1471. return 0;
  1472. Spawn* spawn = lua_interface->GetSpawn(state);
  1473. sint32 value = lua_interface->GetSInt32Value(state, 2);
  1474. lua_interface->ResetFunctionStack(state);
  1475. if (spawn && value != 0) {
  1476. spawn->SetPower(spawn->GetPower() + value);
  1477. if (value > spawn->GetTotalHPBase())
  1478. spawn->SetTotalPower(spawn->GetTotalPower() + value);
  1479. }
  1480. return 0;
  1481. }
  1482. int EQ2Emu_lua_ModifyMaxHP(lua_State* state) {
  1483. if (!lua_interface)
  1484. return 0;
  1485. Spawn* spawn = lua_interface->GetSpawn(state);
  1486. sint32 value = lua_interface->GetSInt32Value(state, 2);
  1487. lua_interface->ResetFunctionStack(state);
  1488. if (spawn && value != 0) {
  1489. spawn->SetHP(spawn->GetHP() + value);
  1490. if (value > spawn->GetTotalHPBase())
  1491. spawn->SetTotalHP(spawn->GetTotalHP() + value);
  1492. }
  1493. return 0;
  1494. }
  1495. int EQ2Emu_lua_SetCurrentHP(lua_State* state) {
  1496. if (!lua_interface)
  1497. return 0;
  1498. Spawn* spawn = lua_interface->GetSpawn(state);
  1499. sint32 value = lua_interface->GetSInt32Value(state, 2);
  1500. lua_interface->ResetFunctionStack(state);
  1501. if (spawn && value > 0) {
  1502. spawn->SetHP(value);
  1503. if (value > spawn->GetTotalHPBase())
  1504. spawn->SetTotalHP(value);
  1505. }
  1506. return 0;
  1507. }
  1508. int EQ2Emu_lua_SetMaxHP(lua_State* state) {
  1509. if (!lua_interface)
  1510. return 0;
  1511. Spawn* spawn = lua_interface->GetSpawn(state);
  1512. float value = lua_interface->GetFloatValue(state, 2);
  1513. lua_interface->ResetFunctionStack(state);
  1514. if (spawn && spawn->IsEntity() && value > 0)
  1515. ((Entity*)spawn)->AddSpellBonus(0, ITEM_STAT_HEALTH, value - spawn->GetTotalHP());
  1516. if (spawn->IsPlayer())
  1517. ((Player*)spawn)->SetCharSheetChanged(true);
  1518. return 0;
  1519. }
  1520. int EQ2Emu_lua_SetMaxHPBase(lua_State* state) {
  1521. if (!lua_interface)
  1522. return 0;
  1523. Spawn* spawn = lua_interface->GetSpawn(state);
  1524. sint32 value = lua_interface->GetSInt32Value(state, 2);
  1525. lua_interface->ResetFunctionStack(state);
  1526. if (spawn && spawn->IsEntity() && value > 0)
  1527. ((Entity*)spawn)->SetTotalHPBase(value);
  1528. return 0;
  1529. }
  1530. int EQ2Emu_lua_SetCurrentPower(lua_State* state) {
  1531. if (!lua_interface)
  1532. return 0;
  1533. Spawn* spawn = lua_interface->GetSpawn(state);
  1534. sint32 value = lua_interface->GetSInt32Value(state, 2);
  1535. lua_interface->ResetFunctionStack(state);
  1536. if (spawn && value > 0) {
  1537. spawn->SetPower(value);
  1538. if (value > spawn->GetTotalPowerBase())
  1539. spawn->SetTotalPower(value);
  1540. }
  1541. return 0;
  1542. }
  1543. int EQ2Emu_lua_SetMaxPower(lua_State* state) {
  1544. if (!lua_interface)
  1545. return 0;
  1546. Spawn* spawn = lua_interface->GetSpawn(state);
  1547. sint32 value = lua_interface->GetSInt32Value(state, 2);
  1548. lua_interface->ResetFunctionStack(state);
  1549. if (spawn && spawn->IsEntity() && value > 0)
  1550. ((Entity*)spawn)->AddSpellBonus(0, ITEM_STAT_POWER, value - spawn->GetTotalPower());
  1551. return 0;
  1552. }
  1553. int EQ2Emu_lua_SetMaxPowerBase(lua_State* state) {
  1554. if (!lua_interface)
  1555. return 0;
  1556. Spawn* spawn = lua_interface->GetSpawn(state);
  1557. sint32 value = lua_interface->GetSInt32Value(state, 2);
  1558. lua_interface->ResetFunctionStack(state);
  1559. if (spawn && spawn->IsEntity() && value > 0)
  1560. ((Entity*)spawn)->SetTotalPowerBase(value);
  1561. return 0;
  1562. }
  1563. int EQ2Emu_lua_SetPosition(lua_State* state) {
  1564. if (!lua_interface)
  1565. return 0;
  1566. Spawn* spawn = lua_interface->GetSpawn(state);
  1567. float x = lua_interface->GetFloatValue(state, 2);
  1568. float y = lua_interface->GetFloatValue(state, 3);
  1569. float z = lua_interface->GetFloatValue(state, 4);
  1570. float heading = lua_interface->GetFloatValue(state, 5);
  1571. lua_interface->ResetFunctionStack(state);
  1572. if (spawn) {
  1573. spawn->SetX(x);
  1574. spawn->SetY(y);
  1575. spawn->SetZ(z);
  1576. if (heading != 0)
  1577. spawn->SetHeading(heading);
  1578. spawn->SetSpawnOrigX(spawn->GetX());
  1579. spawn->SetSpawnOrigY(spawn->GetY());
  1580. spawn->SetSpawnOrigZ(spawn->GetZ());
  1581. spawn->SetSpawnOrigHeading(spawn->GetHeading());
  1582. if (spawn->IsPlayer()) {
  1583. Client* client = spawn->GetZone()->GetClientBySpawn(spawn);
  1584. if (client) {
  1585. EQ2Packet* packet = client->GetPlayer()->Move(x, y, z, client->GetVersion(), (heading == 0 ? -1.0f : (heading + 180.0f)));
  1586. client->QueuePacket(packet);
  1587. }
  1588. }
  1589. }
  1590. return 0;
  1591. }
  1592. int EQ2Emu_lua_SetHeading(lua_State* state) {
  1593. if (!lua_interface)
  1594. return 0;
  1595. Spawn* spawn = lua_interface->GetSpawn(state);
  1596. float value = lua_interface->GetFloatValue(state, 2);
  1597. lua_interface->ResetFunctionStack(state);
  1598. if (spawn) {
  1599. spawn->SetHeading(value);
  1600. if (spawn->IsPlayer()) {
  1601. Client* client = spawn->GetZone()->GetClientBySpawn(spawn);
  1602. if (client) {
  1603. EQ2Packet* packet = client->GetPlayer()->Move(spawn->GetX(), spawn->GetY(), spawn->GetZ(), client->GetVersion(), value + 180.0f);
  1604. client->QueuePacket(packet);
  1605. }
  1606. }
  1607. }
  1608. return 0;
  1609. }
  1610. int EQ2Emu_lua_SetModelType(lua_State* state) {
  1611. if (!lua_interface)
  1612. return 0;
  1613. Spawn* spawn = lua_interface->GetSpawn(state);
  1614. int16 value = lua_interface->GetInt16Value(state, 2);
  1615. lua_interface->ResetFunctionStack(state);
  1616. if (spawn)
  1617. spawn->SetModelType(value);
  1618. return 0;
  1619. }
  1620. int EQ2Emu_lua_SetAdventureClass(lua_State* state) {
  1621. if (!lua_interface)
  1622. return 0;
  1623. Spawn* spawn = lua_interface->GetSpawn(state);
  1624. int8 value = lua_interface->GetInt8Value(state, 2);
  1625. lua_interface->ResetFunctionStack(state);
  1626. if (spawn) {
  1627. if (spawn->IsPlayer())
  1628. ((Player*)spawn)->SetPlayerAdventureClass(value);
  1629. else
  1630. spawn->SetAdventureClass(value);
  1631. }
  1632. return 0;
  1633. }
  1634. int EQ2Emu_lua_SetTradeskillClass(lua_State* state) {
  1635. if (!lua_interface)
  1636. return 0;
  1637. Spawn* spawn = lua_interface->GetSpawn(state);
  1638. int8 value = lua_interface->GetInt8Value(state, 2);
  1639. lua_interface->ResetFunctionStack(state);
  1640. if (spawn) {
  1641. spawn->SetTradeskillClass(value);
  1642. if (spawn->IsEntity()) {
  1643. ((Entity*)spawn)->GetInfoStruct()->tradeskill_class1 = classes.GetTSBaseClass(spawn->GetTradeskillClass());
  1644. ((Entity*)spawn)->GetInfoStruct()->tradeskill_class2 = classes.GetSecondaryTSBaseClass(spawn->GetTradeskillClass());
  1645. ((Entity*)spawn)->GetInfoStruct()->tradeskill_class3 = spawn->GetTradeskillClass();
  1646. }
  1647. if (spawn->IsPlayer())
  1648. ((Player*)spawn)->SetCharSheetChanged(true);
  1649. }
  1650. return 0;
  1651. }
  1652. int EQ2Emu_lua_SetMount(lua_State* state) {
  1653. if (!lua_interface)
  1654. return 0;
  1655. Spawn* spawn = lua_interface->GetSpawn(state);
  1656. int16 value = lua_interface->GetInt16Value(state, 2);
  1657. if (spawn && spawn->IsEntity()) {
  1658. ((Entity*)spawn)->SetMount(value);
  1659. EQ2_Color color;
  1660. color.red = 255;
  1661. color.green = 255;
  1662. color.blue = 255;
  1663. ((Entity*)spawn)->SetMountColor(&color);
  1664. ((Entity*)spawn)->SetMountSaddleColor(&color);
  1665. }
  1666. return 0;
  1667. }
  1668. int EQ2Emu_lua_SetMountColor(lua_State* state) {
  1669. if (!lua_interface)
  1670. return 0;
  1671. Spawn* spawn = lua_interface->GetSpawn(state);
  1672. EQ2_Color mount_color;
  1673. EQ2_Color saddle_color;
  1674. mount_color.red = lua_interface->GetInt8Value(state, 2);
  1675. mount_color.green = lua_interface->GetInt8Value(state, 3);
  1676. mount_color.blue = lua_interface->GetInt8Value(state, 4);
  1677. saddle_color.red = lua_interface->GetInt8Value(state, 5);
  1678. saddle_color.green = lua_interface->GetInt8Value(state, 6);
  1679. saddle_color.blue = lua_interface->GetInt8Value(state, 7);
  1680. if (spawn && spawn->IsEntity()) {
  1681. ((Entity*)spawn)->SetMountColor(&mount_color);
  1682. ((Entity*)spawn)->SetMountSaddleColor(&saddle_color);
  1683. }
  1684. return 0;
  1685. }
  1686. int EQ2Emu_lua_GetMount(lua_State* state) {
  1687. if (!lua_interface)
  1688. return 0;
  1689. Spawn* spawn = lua_interface->GetSpawn(state);
  1690. if (spawn && spawn->IsEntity()) {
  1691. lua_interface->SetInt32Value(state, ((Entity*)spawn)->GetMount());
  1692. return 1;
  1693. }
  1694. return 0;
  1695. }
  1696. int EQ2Emu_lua_GetRace(lua_State* state) {
  1697. if (!lua_interface)
  1698. return 0;
  1699. Spawn* spawn = lua_interface->GetSpawn(state);
  1700. if (spawn)
  1701. {
  1702. LogWrite(LUA__DEBUG, 0, "LUA", "%s - Race: %i", __FUNCTION__, spawn->GetRace());
  1703. lua_interface->SetInt32Value(state, spawn->GetRace());
  1704. return 1;
  1705. }
  1706. return 0;
  1707. }
  1708. int EQ2Emu_lua_GetRaceName(lua_State* state) {
  1709. if (!lua_interface)
  1710. return 0;
  1711. Spawn* spawn = lua_interface->GetSpawn(state);
  1712. if (spawn) {
  1713. lua_interface->SetStringValue(state, races.GetRaceName(spawn->GetRace()));
  1714. return 1;
  1715. }
  1716. return 0;
  1717. }
  1718. int EQ2Emu_lua_GetClass(lua_State* state) {
  1719. Spawn* spawn = lua_interface->GetSpawn(state);
  1720. if (spawn) {
  1721. lua_interface->SetInt32Value(state, spawn->GetAdventureClass());
  1722. return 1;
  1723. }
  1724. return 0;
  1725. }
  1726. int EQ2Emu_lua_GetClassName(lua_State* state) {
  1727. Spawn* spawn = lua_interface->GetSpawn(state);
  1728. if (spawn) {
  1729. lua_interface->SetStringValue(state, classes.GetClassName(spawn->GetAdventureClass()));
  1730. return 1;
  1731. }
  1732. return 0;
  1733. }
  1734. int EQ2Emu_lua_SetSpeed(lua_State* state) {
  1735. if (!lua_interface)
  1736. return 0;
  1737. Spawn* spawn = lua_interface->GetSpawn(state);
  1738. float value = lua_interface->GetFloatValue(state, 2);
  1739. lua_interface->ResetFunctionStack(state);
  1740. if (spawn) {
  1741. spawn->SetSpeed(value);
  1742. ((Entity*)spawn)->SetSpeed(value);
  1743. if (spawn->IsPlayer()) {
  1744. Client* client = spawn->GetZone()->GetClientBySpawn(spawn);
  1745. if (client) {
  1746. PacketStruct* packet = configReader.getStruct("WS_SetControlGhost", client->GetVersion());
  1747. if (packet) {
  1748. packet->setDataByName("spawn_id", 0xFFFFFFFF);
  1749. packet->setDataByName("speed", value);
  1750. packet->setDataByName("size", 0.51);
  1751. EQ2Packet* app = packet->serialize();
  1752. client->QueuePacket(app);
  1753. safe_delete(packet);
  1754. }
  1755. }
  1756. }
  1757. }
  1758. return 0;
  1759. }
  1760. int EQ2Emu_lua_AddSpellBonus(lua_State* state) {
  1761. if (!lua_interface)
  1762. return 0;
  1763. Spawn* spawn = lua_interface->GetSpawn(state);
  1764. const int16 type = lua_interface->GetInt16Value(state, 2);
  1765. const float value = lua_interface->GetFloatValue(state, 3);
  1766. LuaSpell* luaspell = lua_interface->GetCurrentSpell(state);
  1767. int64 class_req = 0;
  1768. int32 class_id = 0;
  1769. vector<int16> faction_req;
  1770. vector<int16> race_req;
  1771. int32 i = 0;
  1772. int8 f = 0;
  1773. int8 r = 0;
  1774. while ((class_id = lua_interface->GetInt32Value(state, 4 + i))) {
  1775. if (class_id < 100) {
  1776. class_req += pow(2.0, double(class_id - 1));
  1777. }
  1778. else if (class_id > 100 && class_id < 1000) {
  1779. race_req.push_back(class_id);
  1780. r++;
  1781. }
  1782. else {
  1783. faction_req.push_back(class_id);
  1784. f++;
  1785. }
  1786. i++;
  1787. }
  1788. if (value != 0 && type >= 0) {
  1789. if (luaspell && luaspell->spell && luaspell->caster) {
  1790. ZoneServer* zone = luaspell->caster->GetZone();
  1791. luaspell->MSpellTargets.readlock(__FUNCTION__, __LINE__);
  1792. for (int32 i = 0; i < luaspell->targets.size(); i++) {
  1793. Spawn* target = zone->GetSpawnByID(luaspell->targets[i]);
  1794. if (target) {
  1795. if (target->IsPlayer()) {
  1796. ((Player*)target)->AddSpellBonus(luaspell, type, value, class_req, race_req, faction_req);
  1797. LogWrite(LUA__DEBUG, 0, "LUA", "Applying Spell Bonus to Player '%s'. Is a Group Member.", ((Player*)target)->GetName());
  1798. if (((Player*)target)->GetGroupMemberInfo())
  1799. ((Player*)target)->UpdateGroupMemberInfo();
  1800. ((Player*)target)->SetCharSheetChanged(true);
  1801. }
  1802. else if (target->IsNPC())
  1803. ((NPC*)target)->AddSpellBonus(luaspell, type, value, class_req, race_req, faction_req);
  1804. else
  1805. lua_interface->LogError("%s: Error applying spell bonus on non entity.", lua_interface->GetScriptName(state));
  1806. }
  1807. }
  1808. luaspell->MSpellTargets.releasereadlock(__FUNCTION__, __LINE__);
  1809. if (!(luaspell->effect_bitmask & EFFECT_FLAG_SPELLBONUS))
  1810. luaspell->effect_bitmask += EFFECT_FLAG_SPELLBONUS;
  1811. }
  1812. else if (spawn && spawn->IsEntity()) {
  1813. ((Entity*)spawn)->AddSpellBonus(luaspell, type, value, class_req, race_req, faction_req);
  1814. LogWrite(LUA__DEBUG, 0, "LUA", "Applying Spell Bonus to Entity '%s'. Is a Group Member.", ((Entity*)spawn)->GetName());
  1815. if (spawn->IsPlayer())
  1816. ((Player*)spawn)->SetCharSheetChanged(true);
  1817. }
  1818. else
  1819. lua_interface->LogError("%s: Unable to apply spell bonus in AddSpellBonus.", lua_interface->GetScriptName(state));
  1820. }
  1821. else
  1822. lua_interface->LogError("%s: Invalid parameters for AddSpellBonus.", lua_interface->GetScriptName(state));
  1823. return 0;
  1824. }
  1825. int EQ2Emu_lua_AddSpawnSpellBonus(lua_State* state) {
  1826. if (!lua_interface)
  1827. return 0;
  1828. Spawn* spawn = lua_interface->GetSpawn(state);
  1829. int16 type = lua_interface->GetInt16Value(state, 2);
  1830. sint32 value = lua_interface->GetSInt32Value(state, 3);
  1831. LuaSpell* luaspell = lua_interface->GetCurrentSpell(state);
  1832. if (!spawn) {
  1833. lua_interface->LogError("%s: LUA AddSpawnSpellBonus command error: spawn is not valid", lua_interface->GetScriptName(state));
  1834. return 0;
  1835. }
  1836. if (!spawn->IsEntity()) {
  1837. lua_interface->LogError("%s: LUA AddSpawnSpellBonus command error: spawn is not an entity", lua_interface->GetScriptName(state));
  1838. return 0;
  1839. }
  1840. if (value == 0) {
  1841. lua_interface->LogError("%s: LUA AddSpawnSpellBonus command error: value must be set", lua_interface->GetScriptName(state));
  1842. return 0;
  1843. }
  1844. if (!luaspell || !luaspell->spell) {
  1845. lua_interface->LogError("%s: LUA AddSpawnSpellBonus command error: can only be used in a spell script", lua_interface->GetScriptName(state));
  1846. return 0;
  1847. }
  1848. int32 class_req = 0;
  1849. vector<int16> faction_req;
  1850. vector<int16> race_req;
  1851. int32 class_id = 0;
  1852. int32 i = 0;
  1853. int8 f = 0;
  1854. int8 r = 0;
  1855. while ((class_id = lua_interface->GetInt32Value(state, 4 + i))) {
  1856. if (class_id < 100) {
  1857. class_req += pow(2.0, double(class_id - 1));
  1858. }
  1859. else if (class_id > 100 && class_id < 1000) {
  1860. race_req.push_back(class_id);
  1861. r++;
  1862. }
  1863. else {
  1864. faction_req.push_back(class_id);
  1865. f++;
  1866. }
  1867. i++;
  1868. }
  1869. ((Entity*)spawn)->AddSpellBonus(luaspell, type, value, class_req, race_req, faction_req);
  1870. if (!(luaspell->effect_bitmask & EFFECT_FLAG_SPELLBONUS))
  1871. luaspell->effect_bitmask += EFFECT_FLAG_SPELLBONUS;
  1872. if (spawn->IsPlayer())
  1873. ((Player*)spawn)->SetCharSheetChanged(true);
  1874. return 0;
  1875. }
  1876. int EQ2Emu_lua_RemoveSpellBonus(lua_State* state) {
  1877. if (!lua_interface)
  1878. return 0;
  1879. Spawn* spawn = lua_interface->GetSpawn(state);
  1880. LuaSpell* luaspell = lua_interface->GetCurrentSpell(state);
  1881. if (luaspell && luaspell->spell) {
  1882. ZoneServer* zone = luaspell->caster->GetZone();
  1883. Spawn* target = 0;
  1884. luaspell->MSpellTargets.readlock(__FUNCTION__, __LINE__);
  1885. for (int32 i = 0; i < luaspell->targets.size(); i++) {
  1886. target = zone->GetSpawnByID(luaspell->targets[i]);
  1887. if (target && target->IsEntity()) {
  1888. ((Entity*)target)->RemoveSpellBonus(luaspell);
  1889. if (target->IsPlayer())
  1890. ((Player*)target)->SetCharSheetChanged(true);
  1891. }
  1892. }
  1893. luaspell->MSpellTargets.releasereadlock(__FUNCTION__, __LINE__);
  1894. }
  1895. else if (spawn && spawn->IsEntity()) {
  1896. ((Entity*)spawn)->RemoveSpellBonus(luaspell);
  1897. if (spawn->IsPlayer())
  1898. ((Player*)spawn)->SetCharSheetChanged(true);
  1899. }
  1900. return 0;
  1901. }
  1902. int EQ2Emu_lua_AddSkillBonus(lua_State* state) {
  1903. if (!lua_interface)
  1904. return 0;
  1905. Spawn* spawn = lua_interface->GetSpawn(state);
  1906. int32 skill_id = lua_interface->GetInt32Value(state, 2);
  1907. float value = lua_interface->GetFloatValue(state, 3);
  1908. LuaSpell* luaspell = lua_interface->GetCurrentSpell(state);
  1909. if (value != 0) {
  1910. int32 spell_id = 0;
  1911. if (luaspell && luaspell->spell && luaspell->caster) {
  1912. spell_id = luaspell->spell->GetSpellID();
  1913. ZoneServer* zone = luaspell->caster->GetZone();
  1914. Spawn* target = 0;
  1915. luaspell->MSpellTargets.readlock(__FUNCTION__, __LINE__);
  1916. for (int32 i = 0; i < luaspell->targets.size(); i++) {
  1917. target = zone->GetSpawnByID(luaspell->targets[i]);
  1918. if (target && target->Alive()) {
  1919. if (target->IsPlayer()) {
  1920. ((Player*)target)->AddSkillBonus(spell_id, skill_id, value);
  1921. Client* client = target->GetZone()->GetClientBySpawn(target);
  1922. if (client) {
  1923. EQ2Packet* packet = ((Player*)target)->GetSkills()->GetSkillPacket(client->GetVersion());
  1924. if (packet)
  1925. client->QueuePacket(packet);
  1926. }
  1927. if (!(luaspell->effect_bitmask & EFFECT_FLAG_SKILLBONUS))
  1928. luaspell->effect_bitmask += EFFECT_FLAG_SKILLBONUS;
  1929. }
  1930. else if (target->IsNPC()) {
  1931. ((NPC*)target)->AddSkillBonus(spell_id, skill_id, value);
  1932. if (!(luaspell->effect_bitmask & EFFECT_FLAG_SKILLBONUS))
  1933. luaspell->effect_bitmask += EFFECT_FLAG_SKILLBONUS;
  1934. }
  1935. else
  1936. LogWrite(LUA__ERROR, 0, "LUA", "Error applying bonus buff on '%s'. Not a NPC or player.", target->GetName());
  1937. }
  1938. }
  1939. luaspell->MSpellTargets.releasereadlock(__FUNCTION__, __LINE__);
  1940. }
  1941. else if (spawn) {
  1942. if (spawn->IsPlayer()) {
  1943. ((Player*)spawn)->AddSkillBonus(spell_id, skill_id, value);
  1944. Client* client = spawn->GetZone()->GetClientBySpawn(spawn);
  1945. if (client) {
  1946. EQ2Packet* packet = ((Player*)spawn)->GetSkills()->GetSkillPacket(client->GetVersion());
  1947. if (packet)
  1948. client->QueuePacket(packet);
  1949. }
  1950. }
  1951. else if (spawn->IsNPC())
  1952. ((NPC*)spawn)->AddSkillBonus(spell_id, skill_id, value);
  1953. else
  1954. LogWrite(LUA__ERROR, 0, "LUA", "Error applying skill bonus on '%s'. Not a NPC or player.", spawn->GetName());
  1955. }
  1956. }
  1957. else
  1958. lua_interface->LogError("%s: Invalid parameters for AddSkillBonus.", lua_interface->GetScriptName(state));
  1959. return 0;
  1960. }
  1961. int EQ2Emu_lua_RemoveSkillBonus(lua_State* state) {
  1962. if (!lua_interface)
  1963. return 0;
  1964. Spawn* spawn = lua_interface->GetSpawn(state);
  1965. LuaSpell* luaspell = lua_interface->GetCurrentSpell(state);
  1966. if (spawn && spawn->IsPlayer()) {
  1967. int32 spell_id = 0;
  1968. if (luaspell && luaspell->spell) {
  1969. spell_id = luaspell->spell->GetSpellID();
  1970. ZoneServer* zone = luaspell->caster->GetZone();
  1971. Spawn* target = 0;
  1972. luaspell->MSpellTargets.readlock(__FUNCTION__, __LINE__);
  1973. for (int32 i = 0; i < luaspell->targets.size(); i++) {
  1974. target = zone->GetSpawnByID(luaspell->targets[i]);
  1975. if (target) {
  1976. if (target->IsPlayer()) {
  1977. ((Player*)target)->RemoveSkillBonus(spell_id);
  1978. Client* client = target->GetZone()->GetClientBySpawn(target);
  1979. if (client) {
  1980. EQ2Packet* packet = ((Player*)target)->GetSkills()->GetSkillPacket(client->GetVersion());
  1981. if (packet)
  1982. client->QueuePacket(packet);
  1983. }
  1984. }
  1985. else if (target->IsNPC())
  1986. ((NPC*)target)->RemoveSkillBonus(spell_id);
  1987. else
  1988. LogWrite(LUA__ERROR, 0, "LUA", "Error removing skill bonus on '%s'. Not a NPC or player.", spawn->GetName());
  1989. }
  1990. }
  1991. luaspell->MSpellTargets.releasereadlock(__FUNCTION__, __LINE__);
  1992. }
  1993. else if (spawn) {
  1994. if (spawn->IsPlayer()) {
  1995. ((Player*)spawn)->RemoveSkillBonus(spell_id);
  1996. Client* client = spawn->GetZone()->GetClientBySpawn(spawn);
  1997. if (client) {
  1998. EQ2Packet* packet = ((Player*)spawn)->GetSkills()->GetSkillPacket(client->GetVersion());
  1999. if (packet)
  2000. client->QueuePacket(packet);
  2001. }
  2002. }
  2003. else if (spawn->IsNPC())
  2004. ((NPC*)spawn)->RemoveSkillBonus(spell_id);
  2005. else
  2006. LogWrite(LUA__ERROR, 0, "LUA", "Error removing skill bonus on '%s'. Not a NPC or player.", spawn->GetName());
  2007. }
  2008. }
  2009. return 0;
  2010. }
  2011. int EQ2Emu_lua_AddControlEffect(lua_State* state) {
  2012. if (!lua_interface)
  2013. return 0;
  2014. Spawn* spawn = lua_interface->GetSpawn(state);
  2015. int8 type = lua_interface->GetInt32Value(state, 2);
  2016. bool only_add_spawn = lua_interface->GetInt8Value(state, 3) == 1;
  2017. LuaSpell* luaspell = lua_interface->GetCurrentSpell(state);
  2018. if (!only_add_spawn && luaspell && luaspell->spell && luaspell->caster && type != 0) {
  2019. ZoneServer* zone = luaspell->caster->GetZone();
  2020. Spawn* target = 0;
  2021. luaspell->MSpellTargets.readlock(__FUNCTION__, __LINE__);
  2022. for (int32 i = 0; i < luaspell->targets.size(); i++) {
  2023. target = zone->GetSpawnByID(luaspell->targets[i]);
  2024. if (target && target->IsEntity()) {
  2025. if (type == CONTROL_EFFECT_TYPE_MEZ) {
  2026. ((Entity*)target)->AddMezSpell(luaspell);
  2027. if (!(luaspell->effect_bitmask & EFFECT_FLAG_MEZ))
  2028. luaspell->effect_bitmask += EFFECT_FLAG_MEZ;
  2029. if (target->IsNPC())
  2030. ((NPC*)target)->Brain()->AddHate(luaspell->caster, 5);
  2031. }
  2032. else if (type == CONTROL_EFFECT_TYPE_STIFLE) {
  2033. ((Entity*)target)->AddStifleSpell(luaspell);
  2034. if (!(luaspell->effect_bitmask & EFFECT_FLAG_STIFLE))
  2035. luaspell->effect_bitmask += EFFECT_FLAG_STIFLE;
  2036. if (target->IsNPC())
  2037. ((NPC*)target)->Brain()->AddHate(luaspell->caster, 5);
  2038. }
  2039. else if (type == CONTROL_EFFECT_TYPE_DAZE) {
  2040. ((Entity*)target)->AddDazeSpell(luaspell);
  2041. if (!(luaspell->effect_bitmask & EFFECT_FLAG_DAZE))
  2042. luaspell->effect_bitmask += EFFECT_FLAG_DAZE;
  2043. if (target->IsNPC())
  2044. ((NPC*)target)->Brain()->AddHate(luaspell->caster, 5);
  2045. }
  2046. else if (type == CONTROL_EFFECT_TYPE_STUN) {
  2047. if (!(luaspell->effect_bitmask & EFFECT_FLAG_STUN))
  2048. luaspell->effect_bitmask += EFFECT_FLAG_STUN;
  2049. ((Entity*)target)->AddStunSpell(luaspell);
  2050. if (target->IsNPC())
  2051. ((NPC*)target)->Brain()->AddHate(luaspell->caster, 5);
  2052. }
  2053. else if (type == CONTROL_EFFECT_TYPE_ROOT) {
  2054. if (!(luaspell->effect_bitmask & EFFECT_FLAG_ROOT))
  2055. luaspell->effect_bitmask += EFFECT_FLAG_ROOT;
  2056. ((Entity*)target)->AddRootSpell(luaspell);
  2057. if (target->IsNPC())
  2058. ((NPC*)target)->Brain()->AddHate(luaspell->caster, 5);
  2059. }
  2060. else if (type == CONTROL_EFFECT_TYPE_FEAR) {
  2061. if (!(luaspell->effect_bitmask & EFFECT_FLAG_FEAR))
  2062. luaspell->effect_bitmask += EFFECT_FLAG_FEAR;
  2063. ((Entity*)target)->AddFearSpell(luaspell);
  2064. if (target->IsNPC())
  2065. ((NPC*)target)->Brain()->AddHate(luaspell->caster, 5);
  2066. }
  2067. else if (type == CONTROL_EFFECT_TYPE_WALKUNDERWATER) {
  2068. ((Entity*)target)->AddWaterwalkSpell(luaspell);
  2069. if (!(luaspell->effect_bitmask & EFFECT_FLAG_WATERWALK))
  2070. luaspell->effect_bitmask += EFFECT_FLAG_WATERWALK;
  2071. }
  2072. else if (type == CONTROL_EFFECT_TYPE_JUMPUNDERWATER) {
  2073. ((Entity*)target)->AddWaterjumpSpell(luaspell);
  2074. if (!(luaspell->effect_bitmask & EFFECT_FLAG_WATERJUMP))
  2075. luaspell->effect_bitmask += EFFECT_FLAG_WATERJUMP;
  2076. }
  2077. else if (type == CONTROL_EFFECT_TYPE_SNARE) {
  2078. ((Entity*)target)->AddSnareSpell(luaspell);
  2079. if (!(luaspell->effect_bitmask & EFFECT_FLAG_SNARE))
  2080. luaspell->effect_bitmask += EFFECT_FLAG_SNARE;
  2081. if (target->IsNPC())
  2082. ((NPC*)target)->Brain()->AddHate(luaspell->caster, 5);
  2083. }
  2084. else if (type == CONTROL_EFFECT_TYPE_FLIGHT) {
  2085. ((Entity*)target)->AddFlightSpell(luaspell);
  2086. if (!(luaspell->effect_bitmask & EFFECT_FLAG_FLIGHT))
  2087. luaspell->effect_bitmask += EFFECT_FLAG_FLIGHT;
  2088. }
  2089. else if (type == CONTROL_EFFECT_TYPE_GLIDE) {
  2090. ((Entity*)target)->AddGlideSpell(luaspell);
  2091. if (!(luaspell->effect_bitmask & EFFECT_FLAG_GLIDE))
  2092. luaspell->effect_bitmask += EFFECT_FLAG_GLIDE;
  2093. }
  2094. else if (type == CONTROL_EFFECT_TYPE_SAFEFALL) {
  2095. ((Entity*)target)->AddSafefallSpell(luaspell);
  2096. if (!(luaspell->effect_bitmask & EFFECT_FLAG_SAFEFALL))
  2097. luaspell->effect_bitmask += EFFECT_FLAG_SAFEFALL;
  2098. }
  2099. else
  2100. lua_interface->LogError("%s: Unhandled control effect type of %u.", lua_interface->GetScriptName(state), type);
  2101. }
  2102. else
  2103. lua_interface->LogError("%s: Error applying control effect on non entity '%s'.", lua_interface->GetScriptName(state), target->GetName());
  2104. }
  2105. luaspell->MSpellTargets.releasereadlock(__FUNCTION__, __LINE__);
  2106. }
  2107. else if (only_add_spawn && spawn && spawn->IsEntity()) {
  2108. if (type == CONTROL_EFFECT_TYPE_MEZ) {
  2109. ((Entity*)spawn)->AddMezSpell(luaspell);
  2110. if (!(luaspell->effect_bitmask & EFFECT_FLAG_MEZ))
  2111. luaspell->effect_bitmask += EFFECT_FLAG_MEZ;
  2112. }
  2113. else if (type == CONTROL_EFFECT_TYPE_STIFLE) {
  2114. ((Entity*)spawn)->AddStifleSpell(luaspell);
  2115. if (!(luaspell->effect_bitmask & EFFECT_FLAG_STIFLE))
  2116. luaspell->effect_bitmask += EFFECT_FLAG_STIFLE;
  2117. }
  2118. else if (type == CONTROL_EFFECT_TYPE_DAZE) {
  2119. ((Entity*)spawn)->AddDazeSpell(luaspell);
  2120. if (!(luaspell->effect_bitmask & EFFECT_FLAG_DAZE))
  2121. luaspell->effect_bitmask += EFFECT_FLAG_DAZE;
  2122. }
  2123. else if (type == CONTROL_EFFECT_TYPE_STUN) {
  2124. ((Entity*)spawn)->AddStunSpell(luaspell);
  2125. if (!(luaspell->effect_bitmask & EFFECT_FLAG_STUN))
  2126. luaspell->effect_bitmask += EFFECT_FLAG_STUN;
  2127. }
  2128. else if (type == CONTROL_EFFECT_TYPE_ROOT) {
  2129. ((Entity*)spawn)->AddRootSpell(luaspell);
  2130. if (!(luaspell->effect_bitmask & EFFECT_FLAG_ROOT))
  2131. luaspell->effect_bitmask += EFFECT_FLAG_ROOT;
  2132. }
  2133. else if (type == CONTROL_EFFECT_TYPE_FEAR) {
  2134. ((Entity*)spawn)->AddFearSpell(luaspell);
  2135. if (!(luaspell->effect_bitmask & EFFECT_FLAG_FEAR))
  2136. luaspell->effect_bitmask += EFFECT_FLAG_FEAR;
  2137. }
  2138. else if (type == CONTROL_EFFECT_TYPE_WALKUNDERWATER) {
  2139. ((Entity*)spawn)->AddWaterwalkSpell(luaspell);
  2140. if (!(luaspell->effect_bitmask & EFFECT_FLAG_WATERWALK))
  2141. luaspell->effect_bitmask += EFFECT_FLAG_WATERWALK;
  2142. }
  2143. else if (type == CONTROL_EFFECT_TYPE_JUMPUNDERWATER) {
  2144. ((Entity*)spawn)->AddWaterjumpSpell(luaspell);
  2145. if (!(luaspell->effect_bitmask & EFFECT_FLAG_WATERJUMP))
  2146. luaspell->effect_bitmask += EFFECT_FLAG_WATERJUMP;
  2147. }
  2148. else if (type == CONTROL_EFFECT_TYPE_SNARE) {
  2149. ((Entity*)spawn)->AddSnareSpell(luaspell);
  2150. if (!(luaspell->effect_bitmask & EFFECT_FLAG_SNARE))
  2151. luaspell->effect_bitmask += EFFECT_FLAG_SNARE;
  2152. }
  2153. else if (type == CONTROL_EFFECT_TYPE_FLIGHT) {
  2154. ((Entity*)spawn)->AddFlightSpell(luaspell);
  2155. if (!(luaspell->effect_bitmask & EFFECT_FLAG_FLIGHT))
  2156. luaspell->effect_bitmask += EFFECT_FLAG_FLIGHT;
  2157. }
  2158. else if (type == CONTROL_EFFECT_TYPE_GLIDE) {
  2159. ((Entity*)spawn)->AddGlideSpell(luaspell);
  2160. if (!(luaspell->effect_bitmask & EFFECT_FLAG_GLIDE))
  2161. luaspell->effect_bitmask += EFFECT_FLAG_GLIDE;
  2162. }
  2163. else if (type == CONTROL_EFFECT_TYPE_SAFEFALL) {
  2164. ((Entity*)spawn)->AddSafefallSpell(luaspell);
  2165. if (!(luaspell->effect_bitmask & EFFECT_FLAG_SAFEFALL))
  2166. luaspell->effect_bitmask += EFFECT_FLAG_SAFEFALL;
  2167. }
  2168. else
  2169. lua_interface->LogError("%s: Unhandled control effect type of %u.", lua_interface->GetScriptName(state), type);
  2170. }
  2171. else
  2172. lua_interface->LogError("%s: Error applying control effect on non entity '%s'.", lua_interface->GetScriptName(state), spawn->GetName());
  2173. return 0;
  2174. }
  2175. int EQ2Emu_lua_RemoveControlEffect(lua_State* state) {
  2176. if (!lua_interface)
  2177. return 0;
  2178. Spawn* spawn = lua_interface->GetSpawn(state);
  2179. int8 type = lua_interface->GetInt8Value(state, 2);
  2180. bool only_remove_spawn = lua_interface->GetInt8Value(state, 3) == 1;
  2181. LuaSpell* luaspell = lua_interface->GetCurrentSpell(state);
  2182. if (spawn && spawn->IsEntity()) {
  2183. if (!only_remove_spawn && luaspell && luaspell->spell) {
  2184. ZoneServer* zone = luaspell->caster->GetZone();
  2185. Spawn* target = 0;
  2186. luaspell->MSpellTargets.readlock(__FUNCTION__, __LINE__);
  2187. for (int32 i = 0; i < luaspell->targets.size(); i++) {
  2188. target = zone->GetSpawnByID(luaspell->targets[i]);
  2189. if (target) {
  2190. if (type == CONTROL_EFFECT_TYPE_MEZ)
  2191. ((Entity*)target)->RemoveMezSpell(luaspell);
  2192. else if (type == CONTROL_EFFECT_TYPE_STIFLE)
  2193. ((Entity*)target)->RemoveStifleSpell(luaspell);
  2194. else if (type == CONTROL_EFFECT_TYPE_DAZE)
  2195. ((Entity*)target)->RemoveDazeSpell(luaspell);
  2196. else if (type == CONTROL_EFFECT_TYPE_STUN)
  2197. ((Entity*)target)->RemoveStunSpell(luaspell);
  2198. else if (type == CONTROL_EFFECT_TYPE_ROOT)
  2199. ((Entity*)target)->RemoveRootSpell(luaspell);
  2200. else if (type == CONTROL_EFFECT_TYPE_FEAR)
  2201. ((Entity*)target)->RemoveFearSpell(luaspell);
  2202. else if (type == CONTROL_EFFECT_TYPE_WALKUNDERWATER)
  2203. ((Entity*)target)->RemoveWaterwalkSpell(luaspell);
  2204. else if (type == CONTROL_EFFECT_TYPE_JUMPUNDERWATER)
  2205. ((Entity*)target)->RemoveWaterjumpSpell(luaspell);
  2206. else if (type == CONTROL_EFFECT_TYPE_SNARE)
  2207. ((Entity*)target)->RemoveSnareSpell(luaspell);
  2208. else if (type == CONTROL_EFFECT_TYPE_FLIGHT)
  2209. ((Entity*)target)->RemoveFlightSpell(luaspell);
  2210. else if (type == CONTROL_EFFECT_TYPE_GLIDE)
  2211. ((Entity*)target)->RemoveGlideSpell(luaspell);
  2212. else if (type == CONTROL_EFFECT_TYPE_SAFEFALL)
  2213. ((Entity*)target)->RemoveGlideSpell(luaspell);
  2214. else
  2215. lua_interface->LogError("%s: Unhandled control effect type of %u.", lua_interface->GetScriptName(state), type);
  2216. }
  2217. }
  2218. luaspell->MSpellTargets.releasereadlock(__FUNCTION__, __LINE__);
  2219. }
  2220. else if (only_remove_spawn) {
  2221. if (type == CONTROL_EFFECT_TYPE_MEZ)
  2222. ((Entity*)spawn)->RemoveMezSpell(luaspell);
  2223. else if (type == CONTROL_EFFECT_TYPE_STIFLE)
  2224. ((Entity*)spawn)->RemoveStifleSpell(luaspell);
  2225. else if (type == CONTROL_EFFECT_TYPE_DAZE)
  2226. ((Entity*)spawn)->RemoveDazeSpell(luaspell);
  2227. else if (type == CONTROL_EFFECT_TYPE_STUN)
  2228. ((Entity*)spawn)->RemoveStunSpell(luaspell);
  2229. else if (type == CONTROL_EFFECT_TYPE_ROOT)
  2230. ((Entity*)spawn)->RemoveRootSpell(luaspell);
  2231. else if (type == CONTROL_EFFECT_TYPE_FEAR)
  2232. ((Entity*)spawn)->RemoveFearSpell(luaspell);
  2233. else if (type == CONTROL_EFFECT_TYPE_WALKUNDERWATER)
  2234. ((Entity*)spawn)->RemoveWaterwalkSpell(luaspell);
  2235. else if (type == CONTROL_EFFECT_TYPE_JUMPUNDERWATER)
  2236. ((Entity*)spawn)->RemoveWaterjumpSpell(luaspell);
  2237. else if (type == CONTROL_EFFECT_TYPE_SNARE)
  2238. ((Entity*)spawn)->RemoveSnareSpell(luaspell);
  2239. else if (type == CONTROL_EFFECT_TYPE_FLIGHT)
  2240. ((Entity*)spawn)->RemoveFlightSpell(luaspell);
  2241. else if (type == CONTROL_EFFECT_TYPE_GLIDE)
  2242. ((Entity*)spawn)->RemoveGlideSpell(luaspell);
  2243. else if (type == CONTROL_EFFECT_TYPE_SAFEFALL)
  2244. ((Entity*)spawn)->RemoveSafefallSpell(luaspell);
  2245. else
  2246. lua_interface->LogError("%s: Unhandled control effect type of %u.", lua_interface->GetScriptName(state), type);
  2247. }
  2248. }
  2249. return 0;
  2250. }
  2251. int EQ2Emu_lua_SetIntBase(lua_State* state) {
  2252. if (!lua_interface)
  2253. return 0;
  2254. Spawn* spawn = lua_interface->GetSpawn(state);
  2255. int16 value = lua_interface->GetInt16Value(state, 2);
  2256. if (spawn && spawn->IsEntity()) {
  2257. ((Entity*)spawn)->GetInfoStruct()->intel_base = value;
  2258. if (spawn->IsPlayer())
  2259. ((Player*)spawn)->SetCharSheetChanged(true);
  2260. }
  2261. return 0;
  2262. }
  2263. int EQ2Emu_lua_SetAgiBase(lua_State* state) {
  2264. if (!lua_interface)
  2265. return 0;
  2266. Spawn* spawn = lua_interface->GetSpawn(state);
  2267. int16 value = lua_interface->GetInt16Value(state, 2);
  2268. if (spawn && spawn->IsEntity()) {
  2269. ((Entity*)spawn)->GetInfoStruct()->agi_base = value;
  2270. if (spawn->IsPlayer())
  2271. ((Player*)spawn)->SetCharSheetChanged(true);
  2272. }
  2273. return 0;
  2274. }
  2275. int EQ2Emu_lua_SetWisBase(lua_State* state) {
  2276. if (!lua_interface)
  2277. return 0;
  2278. Spawn* spawn = lua_interface->GetSpawn(state);
  2279. int16 value = lua_interface->GetInt16Value(state, 2);
  2280. if (spawn && spawn->IsEntity()) {
  2281. ((Entity*)spawn)->GetInfoStruct()->wis_base = value;
  2282. if (spawn->IsPlayer())
  2283. ((Player*)spawn)->SetCharSheetChanged(true);
  2284. }
  2285. return 0;
  2286. }
  2287. int EQ2Emu_lua_SetStaBase(lua_State* state) {
  2288. if (!lua_interface)
  2289. return 0;
  2290. Spawn* spawn = lua_interface->GetSpawn(state);
  2291. int16 value = lua_interface->GetInt16Value(state, 2);
  2292. if (spawn && spawn->IsEntity()) {
  2293. ((Entity*)spawn)->GetInfoStruct()->sta_base = value;
  2294. if (spawn->IsPlayer())
  2295. ((Player*)spawn)->SetCharSheetChanged(true);
  2296. }
  2297. return 0;
  2298. }
  2299. int EQ2Emu_lua_SetStrBase(lua_State* state) {
  2300. if (!lua_interface)
  2301. return 0;
  2302. Spawn* spawn = lua_interface->GetSpawn(state);
  2303. int16 value = lua_interface->GetInt16Value(state, 2);
  2304. if (spawn && spawn->IsEntity()) {
  2305. ((Entity*)spawn)->GetInfoStruct()->str_base = value;
  2306. if (spawn->IsPlayer())
  2307. ((Player*)spawn)->SetCharSheetChanged(true);
  2308. }
  2309. return 0;
  2310. }
  2311. int EQ2Emu_lua_SetInt(lua_State* state) {
  2312. if (!lua_interface)
  2313. return 0;
  2314. Spawn* spawn = lua_interface->GetSpawn(state);
  2315. sint32 value = lua_interface->GetSInt32Value(state, 2);
  2316. if (spawn && spawn->IsEntity()) {
  2317. ((Entity*)spawn)->AddSpellBonus(0, ITEM_STAT_INT, value);
  2318. if (spawn->IsPlayer())
  2319. ((Player*)spawn)->SetCharSheetChanged(true);
  2320. }
  2321. return 0;
  2322. }
  2323. int EQ2Emu_lua_SetWis(lua_State* state) {
  2324. if (!lua_interface)
  2325. return 0;
  2326. Spawn* spawn = lua_interface->GetSpawn(state);
  2327. float value = lua_interface->GetFloatValue(state, 2);
  2328. if (spawn && spawn->IsEntity()) {
  2329. ((Entity*)spawn)->AddSpellBonus(0, ITEM_STAT_WIS, value);
  2330. if (spawn->IsPlayer())
  2331. ((Player*)spawn)->SetCharSheetChanged(true);
  2332. }
  2333. return 0;
  2334. }
  2335. int EQ2Emu_lua_SetSta(lua_State* state) {
  2336. if (!lua_interface)
  2337. return 0;
  2338. Spawn* spawn = lua_interface->GetSpawn(state);
  2339. float value = lua_interface->GetFloatValue(state, 2);
  2340. if (spawn && spawn->IsEntity()) {
  2341. ((Entity*)spawn)->AddSpellBonus(0, ITEM_STAT_STA, value);
  2342. if (spawn->IsPlayer())
  2343. ((Player*)spawn)->SetCharSheetChanged(true);
  2344. }
  2345. return 0;
  2346. }
  2347. int EQ2Emu_lua_SetStr(lua_State* state) {
  2348. if (!lua_interface)
  2349. return 0;
  2350. Spawn* spawn = lua_interface->GetSpawn(state);
  2351. float value = lua_interface->GetFloatValue(state, 2);
  2352. if (spawn && spawn->IsEntity()) {
  2353. ((Entity*)spawn)->AddSpellBonus(0, ITEM_STAT_STR, value);
  2354. if (spawn->IsPlayer())
  2355. ((Player*)spawn)->SetCharSheetChanged(true);
  2356. }
  2357. return 0;
  2358. }
  2359. int EQ2Emu_lua_SetAgi(lua_State* state) {
  2360. if (!lua_interface)
  2361. return 0;
  2362. Spawn* spawn = lua_interface->GetSpawn(state);
  2363. float value = lua_interface->GetFloatValue(state, 2);
  2364. if (spawn && spawn->IsEntity()) {
  2365. ((Entity*)spawn)->AddSpellBonus(0, ITEM_STAT_AGI, value);
  2366. if (spawn->IsPlayer())
  2367. ((Player*)spawn)->SetCharSheetChanged(true);
  2368. }
  2369. return 0;
  2370. }
  2371. int EQ2Emu_lua_GetCurrentHP(lua_State* state) {
  2372. if (!lua_interface)
  2373. return 0;
  2374. Spawn* spawn = lua_interface->GetSpawn(state);
  2375. if (spawn) {
  2376. lua_interface->SetInt32Value(state, spawn->GetHP());
  2377. return 1;
  2378. }
  2379. return 0;
  2380. }
  2381. int EQ2Emu_lua_GetMaxHP(lua_State* state) {
  2382. if (!lua_interface)
  2383. return 0;
  2384. Spawn* spawn = lua_interface->GetSpawn(state);
  2385. if (spawn) {
  2386. lua_interface->SetInt32Value(state, spawn->GetTotalHP());
  2387. return 1;
  2388. }
  2389. return 0;
  2390. }
  2391. int EQ2Emu_lua_GetMaxHPBase(lua_State* state) {
  2392. if (!lua_interface)
  2393. return 0;
  2394. Spawn* spawn = lua_interface->GetSpawn(state);
  2395. if (spawn) {
  2396. lua_interface->SetInt32Value(state, spawn->GetTotalHPBase());
  2397. return 1;
  2398. }
  2399. return 0;
  2400. }
  2401. int EQ2Emu_lua_GetName(lua_State* state) {
  2402. if (!lua_interface)
  2403. return 0;
  2404. Spawn* spawn = lua_interface->GetSpawn(state);
  2405. if (spawn) {
  2406. lua_interface->SetStringValue(state, spawn->GetName());
  2407. return 1;
  2408. }
  2409. return 0;
  2410. }
  2411. int EQ2Emu_lua_GetLevel(lua_State* state) {
  2412. Spawn* spawn = lua_interface->GetSpawn(state);
  2413. if (spawn) {
  2414. lua_interface->SetInt32Value(state, spawn->GetLevel());
  2415. return 1;
  2416. }
  2417. return 0;
  2418. }
  2419. int EQ2Emu_lua_GetCurrentPower(lua_State* state) {
  2420. if (!lua_interface)
  2421. return 0;
  2422. Spawn* spawn = lua_interface->GetSpawn(state);
  2423. if (spawn) {
  2424. lua_interface->SetInt32Value(state, spawn->GetPower());
  2425. return 1;
  2426. }
  2427. return 0;
  2428. }
  2429. int EQ2Emu_lua_GetMaxPower(lua_State* state) {
  2430. if (!lua_interface)
  2431. return 0;
  2432. Spawn* spawn = lua_interface->GetSpawn(state);
  2433. if (spawn) {
  2434. lua_interface->SetInt32Value(state, spawn->GetTotalPower());
  2435. return 1;
  2436. }
  2437. return 0;
  2438. }
  2439. int EQ2Emu_lua_GetMaxPowerBase(lua_State* state) {
  2440. if (!lua_interface)
  2441. return 0;
  2442. Spawn* spawn = lua_interface->GetSpawn(state);
  2443. if (spawn) {
  2444. lua_interface->SetInt32Value(state, spawn->GetTotalPowerBase());
  2445. return 1;
  2446. }
  2447. return 0;
  2448. }
  2449. int EQ2Emu_lua_GetDistance(lua_State* state) {
  2450. if (!lua_interface)
  2451. return 0;
  2452. Spawn* spawn = lua_interface->GetSpawn(state);
  2453. Spawn* spawn2 = lua_interface->GetSpawn(state, 2);
  2454. bool include_radius = lua_interface->GetInt8Value(state, 3) == 1;
  2455. if (spawn && spawn2) {
  2456. float distance = spawn->GetDistance(spawn2, false, include_radius);
  2457. lua_interface->SetFloatValue(state, distance);
  2458. return 1;
  2459. }
  2460. return 0;
  2461. }
  2462. int EQ2Emu_lua_GetX(lua_State* state) {
  2463. if (!lua_interface)
  2464. return 0;
  2465. Spawn* spawn = lua_interface->GetSpawn(state);
  2466. if (spawn) {
  2467. lua_interface->SetFloatValue(state, spawn->GetX());
  2468. return 1;
  2469. }
  2470. return 0;
  2471. }
  2472. int EQ2Emu_lua_GetY(lua_State* state) {
  2473. if (!lua_interface)
  2474. return 0;
  2475. Spawn* spawn = lua_interface->GetSpawn(state);
  2476. if (spawn) {
  2477. lua_interface->SetFloatValue(state, spawn->GetY());
  2478. return 1;
  2479. }
  2480. return 0;
  2481. }
  2482. int EQ2Emu_lua_GetZ(lua_State* state) {
  2483. if (!lua_interface)
  2484. return 0;
  2485. Spawn* spawn = lua_interface->GetSpawn(state);
  2486. if (spawn) {
  2487. lua_interface->SetFloatValue(state, spawn->GetZ());
  2488. return 1;
  2489. }
  2490. return 0;
  2491. }
  2492. int EQ2Emu_lua_GetHeading(lua_State* state) {
  2493. if (!lua_interface)
  2494. return 0;
  2495. Spawn* spawn = lua_interface->GetSpawn(state);
  2496. if (spawn) {
  2497. lua_interface->SetFloatValue(state, spawn->GetHeading());
  2498. return 1;
  2499. }
  2500. return 0;
  2501. }
  2502. int EQ2Emu_lua_GetModelType(lua_State* state) {
  2503. if (!lua_interface)
  2504. return 0;
  2505. Spawn* spawn = lua_interface->GetSpawn(state);
  2506. if (spawn) {
  2507. lua_interface->SetInt32Value(state, spawn->GetModelType());
  2508. return 1;
  2509. }
  2510. return 0;
  2511. }
  2512. int EQ2Emu_lua_GetSpeed(lua_State* state) {
  2513. if (!lua_interface)
  2514. return 0;
  2515. Spawn* spawn = lua_interface->GetSpawn(state);
  2516. if (spawn) {
  2517. lua_interface->SetFloatValue(state, spawn->GetSpeed());
  2518. return 1;
  2519. }
  2520. return 0;
  2521. }
  2522. int EQ2Emu_lua_HasMoved(lua_State* state) {
  2523. if (!lua_interface)
  2524. return 0;
  2525. Spawn* spawn = lua_interface->GetSpawn(state);
  2526. if (spawn && spawn->IsEntity()) {
  2527. lua_interface->SetBooleanValue(state, ((Entity*)spawn)->HasMoved(false));
  2528. return 1;
  2529. }
  2530. return 0;
  2531. }
  2532. int EQ2Emu_lua_GetInt(lua_State* state) {
  2533. if (!lua_interface)
  2534. return 0;
  2535. Spawn* spawn = lua_interface->GetSpawn(state);
  2536. if (spawn && spawn->IsEntity()) {
  2537. lua_interface->SetInt32Value(state, ((Entity*)spawn)->GetInt());
  2538. return 1;
  2539. }
  2540. return 0;
  2541. }
  2542. int EQ2Emu_lua_GetWis(lua_State* state) {
  2543. if (!lua_interface)
  2544. return 0;
  2545. Spawn* spawn = lua_interface->GetSpawn(state);
  2546. if (spawn && spawn->IsEntity()) {
  2547. lua_interface->SetInt32Value(state, ((Entity*)spawn)->GetWis());
  2548. return 1;
  2549. }
  2550. return 0;
  2551. }
  2552. int EQ2Emu_lua_GetSta(lua_State* state) {
  2553. if (!lua_interface)
  2554. return 0;
  2555. Spawn* spawn = lua_interface->GetSpawn(state);
  2556. if (spawn && spawn->IsEntity()) {
  2557. lua_interface->SetInt32Value(state, ((Entity*)spawn)->GetSta());
  2558. return 1;
  2559. }
  2560. return 0;
  2561. }
  2562. int EQ2Emu_lua_GetStr(lua_State* state) {
  2563. if (!lua_interface)
  2564. return 0;
  2565. Spawn* spawn = lua_interface->GetSpawn(state);
  2566. if (spawn && spawn->IsEntity()) {
  2567. lua_interface->SetInt32Value(state, ((Entity*)spawn)->GetStr());
  2568. return 1;
  2569. }
  2570. return 0;
  2571. }
  2572. int EQ2Emu_lua_GetAgi(lua_State* state) {
  2573. if (!lua_interface)
  2574. return 0;
  2575. Spawn* spawn = lua_interface->GetSpawn(state);
  2576. if (spawn && spawn->IsEntity()) {
  2577. lua_interface->SetInt32Value(state, ((Entity*)spawn)->GetAgi());
  2578. return 1;
  2579. }
  2580. return 0;
  2581. }
  2582. int EQ2Emu_lua_GetIntBase(lua_State* state) {
  2583. if (!lua_interface)
  2584. return 0;
  2585. Spawn* spawn = lua_interface->GetSpawn(state);
  2586. if (spawn && spawn->IsEntity()) {
  2587. lua_interface->SetInt32Value(state, ((Entity*)spawn)->GetIntBase());
  2588. return 1;
  2589. }
  2590. return 0;
  2591. }
  2592. int EQ2Emu_lua_GetWisBase(lua_State* state) {
  2593. if (!lua_interface)
  2594. return 0;
  2595. Spawn* spawn = lua_interface->GetSpawn(state);
  2596. if (spawn && spawn->IsEntity()) {
  2597. lua_interface->SetInt32Value(state, ((Entity*)spawn)->GetWisBase());
  2598. return 1;
  2599. }
  2600. return 0;
  2601. }
  2602. int EQ2Emu_lua_GetStaBase(lua_State* state) {
  2603. if (!lua_interface)
  2604. return 0;
  2605. Spawn* spawn = lua_interface->GetSpawn(state);
  2606. if (spawn && spawn->IsEntity()) {
  2607. lua_interface->SetInt32Value(state, ((Entity*)spawn)->GetStaBase());
  2608. return 1;
  2609. }
  2610. return 0;
  2611. }
  2612. int EQ2Emu_lua_GetStrBase(lua_State* state) {
  2613. if (!lua_interface)
  2614. return 0;
  2615. Spawn* spawn = lua_interface->GetSpawn(state);
  2616. if (spawn && spawn->IsEntity()) {
  2617. lua_interface->SetInt32Value(state, ((Entity*)spawn)->GetStrBase());
  2618. return 1;
  2619. }
  2620. return 0;
  2621. }
  2622. int EQ2Emu_lua_GetAgiBase(lua_State* state) {
  2623. if (!lua_interface)
  2624. return 0;
  2625. Spawn* spawn = lua_interface->GetSpawn(state);
  2626. if (spawn && spawn->IsEntity()) {
  2627. lua_interface->SetInt32Value(state, ((Entity*)spawn)->GetAgiBase());
  2628. return 1;
  2629. }
  2630. return 0;
  2631. }
  2632. int EQ2Emu_lua_SetStepComplete(lua_State* state) {
  2633. if (!lua_interface)
  2634. return 0;
  2635. Spawn* player = lua_interface->GetSpawn(state);
  2636. if (!player || !player->IsPlayer()) {
  2637. lua_interface->LogError("%s: LUA SetStepComplete command error: player is not valid", lua_interface->GetScriptName(state));
  2638. return 0;
  2639. }
  2640. int32 quest_id = lua_interface->GetInt32Value(state, 2);
  2641. if (quest_id <= 0) {
  2642. lua_interface->LogError("%s: LUA SetStepComplete command error: quest_id is not valid", lua_interface->GetScriptName(state));
  2643. return 0;
  2644. } else if ((((Player*)player)->player_quests.count(quest_id) <= 0)) {
  2645. lua_interface->LogError("%s: LUA SetStepComplete command error: player does not have quest", lua_interface->GetScriptName(state));
  2646. return 0;
  2647. }
  2648. int32 step = lua_interface->GetInt32Value(state, 3);
  2649. if (step > 0) {
  2650. Client* client = player->GetZone()->GetClientBySpawn(player);
  2651. if (client)
  2652. client->AddPendingQuestUpdate(quest_id, step);
  2653. } else {
  2654. lua_interface->LogError("%s: LUA SetStepComplete command error: step is not valid", lua_interface->GetScriptName(state));
  2655. }
  2656. return 0;
  2657. }
  2658. int EQ2Emu_lua_AddStepProgress(lua_State* state) {
  2659. Spawn* player = lua_interface->GetSpawn(state);
  2660. int32 quest_id = lua_interface->GetInt32Value(state, 2);
  2661. int32 step = lua_interface->GetInt32Value(state, 3);
  2662. int32 progress = lua_interface->GetInt32Value(state, 4);
  2663. if (player && player->IsPlayer() && quest_id > 0 && step > 0 && progress > 0 && (((Player*)player)->player_quests.count(quest_id) > 0)) {
  2664. Client* client = player->GetZone()->GetClientBySpawn(player);
  2665. if (client)
  2666. client->AddPendingQuestUpdate(quest_id, step, progress);
  2667. }
  2668. return 0;
  2669. }
  2670. int EQ2Emu_lua_GetTaskGroupStep(lua_State* state) {
  2671. if (!lua_interface)
  2672. return 0;
  2673. Spawn* player = lua_interface->GetSpawn(state);
  2674. int32 quest_id = lua_interface->GetInt32Value(state, 2);
  2675. if (player && player->IsPlayer() && quest_id > 0) {
  2676. lua_interface->SetInt32Value(state, ((Player*)player)->GetTaskGroupStep(quest_id));
  2677. return 1;
  2678. }
  2679. return 0;
  2680. }
  2681. int EQ2Emu_lua_QuestStepIsComplete(lua_State* state) {
  2682. if (!lua_interface)
  2683. return 0;
  2684. Spawn* player = lua_interface->GetSpawn(state);
  2685. int32 quest_id = lua_interface->GetInt32Value(state, 2);
  2686. int32 step_id = lua_interface->GetInt32Value(state, 3);
  2687. if (player && player->IsPlayer() && quest_id > 0) {
  2688. lua_interface->SetBooleanValue(state, ((Player*)player)->GetQuestStepComplete(quest_id, step_id));
  2689. return 1;
  2690. }
  2691. return 0;
  2692. }
  2693. int EQ2Emu_lua_GetQuestStep(lua_State* state) {
  2694. if (!lua_interface)
  2695. return 0;
  2696. Spawn* player = lua_interface->GetSpawn(state);
  2697. int32 quest_id = lua_interface->GetInt32Value(state, 2);
  2698. if (player && player->IsPlayer() && quest_id > 0) {
  2699. lua_interface->SetInt32Value(state, ((Player*)player)->GetQuestStep(quest_id));
  2700. return 1;
  2701. }
  2702. return 0;
  2703. }
  2704. int EQ2Emu_lua_RegisterQuest(lua_State* state) {
  2705. if (!lua_interface)
  2706. return 0;
  2707. Quest* quest = lua_interface->GetQuest(state);
  2708. string name = lua_interface->GetStringValue(state, 2);
  2709. string type = lua_interface->GetStringValue(state, 3);
  2710. string zone = lua_interface->GetStringValue(state, 4);
  2711. int16 level = lua_interface->GetInt16Value(state, 5);
  2712. string description = lua_interface->GetStringValue(state, 6);
  2713. bool load = true;
  2714. if (!quest) {
  2715. lua_interface->LogError("%s: Quest not given in RegisterQuest!", lua_interface->GetScriptName(state));
  2716. load = false;
  2717. }
  2718. if (load && name.length() == 0) {
  2719. lua_interface->LogError("%s: Name not given in RegisterQuest!", lua_interface->GetScriptName(state));
  2720. load = false;
  2721. }
  2722. if (load && type.length() == 0) {
  2723. lua_interface->LogError("%s: Type not given in RegisterQuest for '%s'!", lua_interface->GetScriptName(state), name.c_str());
  2724. load = false;
  2725. }
  2726. if (load && zone.length() == 0) {
  2727. lua_interface->LogError("%s: Zone not given in RegisterQuest for '%s'!", lua_interface->GetScriptName(state), name.c_str());
  2728. load = false;
  2729. }
  2730. if (load && description.length() == 0) {
  2731. lua_interface->LogError("%s: Description not given in RegisterQuest for '%s'!", lua_interface->GetScriptName(state), name.c_str());
  2732. load = false;
  2733. }
  2734. if (load && level == 0) {
  2735. lua_interface->LogError("%s: Level not given in RegisterQuest for '%s'!", lua_interface->GetScriptName(state), name.c_str());
  2736. load = false;
  2737. }
  2738. if (load)
  2739. quest->RegisterQuest(name, type, zone, level, description);
  2740. return 0;
  2741. }
  2742. int EQ2Emu_lua_SetQuestPrereqLevel(lua_State* state) {
  2743. if (!lua_interface)
  2744. return 0;
  2745. Quest* quest = lua_interface->GetQuest(state);
  2746. if (quest) {
  2747. int8 level = lua_interface->GetInt16Value(state, 2);
  2748. quest->SetPrereqLevel(level);
  2749. }
  2750. return 0;
  2751. }
  2752. int EQ2Emu_lua_AddQuestPrereqQuest(lua_State* state) {
  2753. if (!lua_interface)
  2754. return 0;
  2755. Quest* quest = lua_interface->GetQuest(state);
  2756. if (quest) {
  2757. int32 quest_id = lua_interface->GetInt32Value(state, 2);
  2758. quest->AddPrereqQuest(quest_id);
  2759. }
  2760. return 0;
  2761. }
  2762. int EQ2Emu_lua_AddQuestPrereqItem(lua_State* state) {
  2763. if (!lua_interface)
  2764. return 0;
  2765. Quest* quest = lua_interface->GetQuest(state);
  2766. if (quest) {
  2767. int32 item_id = lua_interface->GetInt32Value(state, 2);
  2768. int8 quantity = lua_interface->GetInt32Value(state, 3);
  2769. if (quantity == 0)
  2770. quantity = 1;
  2771. Item* master_item = master_item_list.GetItem(item_id);
  2772. if (master_item) {
  2773. Item* item = new Item(master_item);
  2774. item->details.count = quantity;
  2775. quest->AddPrereqItem(item);
  2776. }
  2777. }
  2778. return 0;
  2779. }
  2780. int EQ2Emu_lua_HasQuest(lua_State* state) {
  2781. if (!lua_interface)
  2782. return 0;
  2783. Spawn* player = lua_interface->GetSpawn(state);
  2784. if(!player || !player->IsPlayer()) {
  2785. lua_interface->LogError("%s: LUA HasQuest command error: player is not valid", lua_interface->GetScriptName(state));
  2786. return 0;
  2787. }
  2788. int32 quest_id = lua_interface->GetInt32Value(state, 2);
  2789. if (quest_id > 0) {
  2790. lua_interface->SetBooleanValue(state, (((Player*)player)->player_quests.count(quest_id) > 0));
  2791. return 1;
  2792. } else {
  2793. lua_interface->LogError("%s: LUA HasQuest command error: quest_id is not valid", lua_interface->GetScriptName(state));
  2794. }
  2795. return 0;
  2796. }
  2797. int EQ2Emu_lua_QuestReturnNPC(lua_State* state) {
  2798. if (!lua_interface)
  2799. return 0;
  2800. Quest* quest = lua_interface->GetQuest(state);
  2801. int32 spawn_id = lua_interface->GetInt32Value(state, 2);
  2802. if (quest && spawn_id > 0)
  2803. quest->SetQuestReturnNPC(spawn_id);
  2804. return 0;
  2805. }
  2806. int EQ2Emu_lua_AddTimer(lua_State* state) {
  2807. if (!lua_interface)
  2808. return 0;
  2809. Spawn* spawn = lua_interface->GetSpawn(state);
  2810. if (!spawn) {
  2811. lua_interface->LogError("%s: LUA AddTimer command error: spawn is not valid", lua_interface->GetScriptName(state));
  2812. return 0;
  2813. }
  2814. int32 time = lua_interface->GetInt32Value(state, 2);
  2815. if (time <= 0) {
  2816. lua_interface->LogError("%s: LUA AddTimer command error: time is not set", lua_interface->GetScriptName(state));
  2817. return 0;
  2818. }
  2819. string function = lua_interface->GetStringValue(state, 3);
  2820. if (function.length() == 0) {
  2821. lua_interface->LogError("%s: LUA AddTimer command error: function is not set", lua_interface->GetScriptName(state));
  2822. return 0;
  2823. }
  2824. int32 max_count = lua_interface->GetInt32Value(state, 4);
  2825. Spawn* player = lua_interface->GetSpawn(state, 5);
  2826. SpawnScriptTimer* timer = new SpawnScriptTimer;
  2827. timer->timer = Timer::GetCurrentTime2() + time;
  2828. timer->function = function;
  2829. timer->spawn = spawn->GetID();
  2830. timer->player = player ? player->GetID() : 0;
  2831. if (max_count == 0)
  2832. max_count = 1;
  2833. timer->max_count = max_count;
  2834. timer->current_count = 0;
  2835. spawn->GetZone()->AddSpawnScriptTimer(timer);
  2836. return 0;
  2837. }
  2838. int EQ2Emu_lua_GetQuest(lua_State* state) {
  2839. if (!lua_interface)
  2840. return 0;
  2841. Spawn* player = lua_interface->GetSpawn(state);
  2842. int32 quest_id = lua_interface->GetInt32Value(state, 2);
  2843. if (player && player->IsPlayer() && quest_id > 0) {
  2844. lua_interface->SetQuestValue(state, ((Player*)player)->player_quests[quest_id]);
  2845. return 1;
  2846. }
  2847. return 0;
  2848. }
  2849. int EQ2Emu_lua_QuestIsComplete(lua_State* state) {
  2850. if (!lua_interface)
  2851. return 0;
  2852. Spawn* player = lua_interface->GetSpawn(state);
  2853. int32 quest_id = lua_interface->GetInt32Value(state, 2);
  2854. if (player && player->IsPlayer() && quest_id > 0 && (((Player*)player)->player_quests.count(quest_id) > 0)) {
  2855. Quest* quest = ((Player*)player)->player_quests[quest_id];
  2856. if (quest)
  2857. lua_interface->SetBooleanValue(state, quest->GetCompleted());
  2858. return 1;
  2859. }
  2860. return 0;
  2861. }
  2862. int EQ2Emu_lua_HasCompletedQuest(lua_State* state) {
  2863. if (!lua_interface)
  2864. return 0;
  2865. Spawn* player = lua_interface->GetSpawn(state);
  2866. int32 quest_id = lua_interface->GetInt32Value(state, 2);
  2867. if (player && player->IsPlayer() && quest_id > 0) {
  2868. lua_interface->SetBooleanValue(state, (((Player*)player)->GetCompletedQuest(quest_id) != 0));
  2869. return 1;
  2870. }
  2871. return 0;
  2872. }
  2873. int EQ2Emu_lua_ProvidesQuest(lua_State* state) {
  2874. if (!lua_interface)
  2875. return 0;
  2876. Spawn* npc = lua_interface->GetSpawn(state);
  2877. int32 quest_id = lua_interface->GetInt32Value(state, 2);
  2878. if (npc && !npc->IsPlayer() && quest_id > 0)
  2879. npc->AddProvidedQuest(quest_id);
  2880. return 0;
  2881. }
  2882. int EQ2Emu_lua_OfferQuest(lua_State* state) {
  2883. if (!lua_interface)
  2884. return 0;
  2885. Spawn* npc = lua_interface->GetSpawn(state);
  2886. Spawn* player = lua_interface->GetSpawn(state, 2);
  2887. int32 quest_id = lua_interface->GetInt32Value(state, 3);
  2888. bool forced = lua_interface->GetBooleanValue(state, 4);
  2889. /* NPC is allowed to be null */
  2890. if (player && player->IsPlayer() && quest_id > 0) {
  2891. Quest* master_quest = master_quest_list.GetQuest(quest_id);
  2892. if (master_quest) {
  2893. Client* client = player->GetZone()->GetClientBySpawn(player);
  2894. if (!client) {
  2895. lua_interface->LogError("%s: LUA OfferQuest command error: client is not set", lua_interface->GetScriptName(state));
  2896. }
  2897. Quest* quest = new Quest(master_quest);
  2898. if (!quest) {
  2899. lua_interface->LogError("%s: LUA OfferQuest command error: new Quest() failed.", lua_interface->GetScriptName(state));
  2900. }
  2901. if (client && quest) {
  2902. if (npc)
  2903. quest->SetQuestGiver(npc->GetDatabaseID());
  2904. else
  2905. quest->SetQuestGiver(0);
  2906. client->AddPendingQuest(quest, forced);
  2907. }
  2908. }
  2909. else {
  2910. lua_interface->LogError("%s: LUA OfferQuest command error: failed to get quest %d", lua_interface->GetScriptName(state), quest_id);
  2911. }
  2912. }
  2913. else {
  2914. lua_interface->LogError("%s: LUA OfferQuest command error: player is not set or bad quest id %p %d", lua_interface->GetScriptName(state), player, quest_id);
  2915. }
  2916. return 0;
  2917. }
  2918. int EQ2Emu_lua_AddQuestPrereqClass(lua_State* state) {
  2919. if (!lua_interface)
  2920. return 0;
  2921. Quest* quest = lua_interface->GetQuest(state);
  2922. if (quest) {
  2923. int8 class_id = lua_interface->GetInt8Value(state, 2);
  2924. quest->AddPrereqClass(class_id);
  2925. }
  2926. return 0;
  2927. }
  2928. int EQ2Emu_lua_AddQuestPrereqRace(lua_State* state) {
  2929. if (!lua_interface)
  2930. return 0;
  2931. Quest* quest = lua_interface->GetQuest(state);
  2932. if (quest) {
  2933. int8 race = lua_interface->GetInt8Value(state, 2);
  2934. quest->AddPrereqRace(race);
  2935. }
  2936. return 0;
  2937. }
  2938. int EQ2Emu_lua_AddQuestPrereqModelType(lua_State* state) {
  2939. if (!lua_interface)
  2940. return 0;
  2941. Quest* quest = lua_interface->GetQuest(state);
  2942. if (quest) {
  2943. int16 model_type = lua_interface->GetInt16Value(state, 2);
  2944. quest->AddPrereqModelType(model_type);
  2945. }
  2946. return 0;
  2947. }
  2948. int EQ2Emu_lua_AddQuestPrereqTradeskillLevel(lua_State* state) {
  2949. if (!lua_interface)
  2950. return 0;
  2951. Quest* quest = lua_interface->GetQuest(state);
  2952. if (!quest) {
  2953. lua_interface->LogError("%s: LUA AddQuestPrereqTradeskillLevel command error: quest is not valid", lua_interface->GetScriptName(state));
  2954. return 0;
  2955. }
  2956. int8 level = lua_interface->GetInt8Value(state, 2);
  2957. quest->SetPrereqTSLevel(level);
  2958. return 0;
  2959. }
  2960. int EQ2Emu_lua_AddQuestPrereqTradeskillClass(lua_State* state) {
  2961. if (!lua_interface)
  2962. return 0;
  2963. Quest* quest = lua_interface->GetQuest(state);
  2964. if (!quest) {
  2965. lua_interface->LogError("%s: LUA AddQuestPrereqTradeskillClass command error: quest is not valid", lua_interface->GetScriptName(state));
  2966. return 0;
  2967. }
  2968. int8 class_id = lua_interface->GetInt8Value(state, 2);
  2969. quest->AddPrereqTradeskillClass(class_id);
  2970. return 0;
  2971. }
  2972. int EQ2Emu_lua_AddQuestPrereqFaction(lua_State* state) {
  2973. if (!lua_interface)
  2974. return 0;
  2975. Quest* quest = lua_interface->GetQuest(state);
  2976. if (quest) {
  2977. int32 faction_id = lua_interface->GetInt32Value(state, 2);
  2978. sint32 min = lua_interface->GetSInt32Value(state, 3);
  2979. sint32 max = lua_interface->GetSInt32Value(state, 4);
  2980. quest->AddPrereqFaction(faction_id, min, max);
  2981. }
  2982. return 0;
  2983. }
  2984. int EQ2Emu_lua_AddQuestSelectableRewardItem(lua_State* state) {
  2985. if (!lua_interface)
  2986. return 0;
  2987. Quest* quest = lua_interface->GetQuest(state);
  2988. if (quest) {
  2989. int32 item_id = lua_interface->GetInt32Value(state, 2);
  2990. int8 quantity = lua_interface->GetInt8Value(state, 3);
  2991. if (quantity == 0)
  2992. quantity = 1;
  2993. Item* master_item = master_item_list.GetItem(item_id);
  2994. if (master_item) {
  2995. Item* item = new Item(master_item);
  2996. item->details.count = quantity;
  2997. quest->AddSelectableRewardItem(item);
  2998. }
  2999. }
  3000. return 0;
  3001. }
  3002. int EQ2Emu_lua_AddQuestRewardItem(lua_State* state) {
  3003. if (!lua_interface)
  3004. return 0;
  3005. Quest* quest = lua_interface->GetQuest(state);
  3006. if (quest) {
  3007. int32 item_id = lua_interface->GetInt32Value(state, 2);
  3008. int8 quantity = lua_interface->GetInt32Value(state, 3);
  3009. if (quantity == 0)
  3010. quantity = 1;
  3011. Item* master_item = master_item_list.GetItem(item_id);
  3012. if (master_item) {
  3013. Item* item = new Item(master_item);
  3014. item->details.count = quantity;
  3015. quest->AddRewardItem(item);
  3016. }
  3017. }
  3018. return 0;
  3019. }
  3020. int EQ2Emu_lua_AddQuestRewardCoin(lua_State* state) {
  3021. if (!lua_interface)
  3022. return 0;
  3023. Quest* quest = lua_interface->GetQuest(state);
  3024. if (quest) {
  3025. int32 copper = lua_interface->GetInt32Value(state, 2);
  3026. int32 silver = lua_interface->GetInt32Value(state, 3);
  3027. int32 gold = lua_interface->GetInt32Value(state, 4);
  3028. int32 plat = lua_interface->GetInt32Value(state, 5);
  3029. quest->AddRewardCoins(copper, silver, gold, plat);
  3030. }
  3031. return 0;
  3032. }
  3033. int EQ2Emu_lua_AddQuestRewardFaction(lua_State* state) {
  3034. if (!lua_interface)
  3035. return 0;
  3036. Quest* quest = lua_interface->GetQuest(state);
  3037. int32 faction_id = lua_interface->GetInt32Value(state, 2);
  3038. sint32 amount = lua_interface->GetSInt32Value(state, 3);
  3039. if (quest && faction_id > 0 && amount != 0)
  3040. quest->AddRewardFaction(faction_id, amount);
  3041. return 0;
  3042. }
  3043. int EQ2Emu_lua_SetQuestRewardStatus(lua_State* state) {
  3044. if (!lua_interface)
  3045. return 0;
  3046. Quest* quest = lua_interface->GetQuest(state);
  3047. if (quest) {
  3048. int32 status = lua_interface->GetInt32Value(state, 2);
  3049. quest->SetRewardStatus(status);
  3050. }
  3051. return 0;
  3052. }
  3053. int EQ2Emu_lua_SetQuestRewardComment(lua_State* state) {
  3054. if (!lua_interface)
  3055. return 0;
  3056. Quest* quest = lua_interface->GetQuest(state);
  3057. if (quest) {
  3058. string comment = lua_interface->GetStringValue(state, 2);
  3059. quest->SetRewardComment(comment);
  3060. }
  3061. return 0;
  3062. }
  3063. int EQ2Emu_lua_SetQuestRewardExp(lua_State* state) {
  3064. if (!lua_interface)
  3065. return 0;
  3066. Quest* quest = lua_interface->GetQuest(state);
  3067. if (quest) {
  3068. int32 exp = lua_interface->GetInt32Value(state, 2);
  3069. quest->SetRewardXP(exp);
  3070. }
  3071. return 0;
  3072. }
  3073. int EQ2Emu_lua_AddQuestStep(lua_State* state) {
  3074. Quest* quest = lua_interface->GetQuest(state);
  3075. if (quest) {
  3076. int32 step = lua_interface->GetInt32Value(state, 2);
  3077. string description = lua_interface->GetStringValue(state, 3);
  3078. int32 quantity = lua_interface->GetInt32Value(state, 4);
  3079. float percentage = lua_interface->GetFloatValue(state, 5);
  3080. string str_taskgroup = lua_interface->GetStringValue(state, 6);
  3081. int16 icon = lua_interface->GetInt16Value(state, 7);
  3082. int32 usableitemid = lua_interface->GetInt32Value(state, 8);
  3083. const char* taskgroup = 0;
  3084. if (str_taskgroup.length() > 0)
  3085. taskgroup = str_taskgroup.c_str();
  3086. QuestStep* quest_step = quest->AddQuestStep(step, QUEST_STEP_TYPE_NORMAL, description, 0, quantity, taskgroup, 0, 0, percentage, usableitemid);
  3087. if (quest_step && icon && quantity > 0)
  3088. quest_step->SetIcon(icon);
  3089. }
  3090. return 0;
  3091. }
  3092. int EQ2Emu_lua_AddQuestStepKill(lua_State* state) {
  3093. if (!lua_interface)
  3094. return 0;
  3095. Quest* quest = lua_interface->GetQuest(state);
  3096. if (quest) {
  3097. int32 step = lua_interface->GetInt32Value(state, 2);
  3098. string description = lua_interface->GetStringValue(state, 3);
  3099. int32 quantity = lua_interface->GetInt32Value(state, 4);
  3100. float percentage = lua_interface->GetFloatValue(state, 5);
  3101. string str_taskgroup = lua_interface->GetStringValue(state, 6);
  3102. int16 icon = lua_interface->GetInt16Value(state, 7);
  3103. const char* taskgroup = 0;
  3104. if (str_taskgroup.length() > 0)
  3105. taskgroup = str_taskgroup.c_str();
  3106. int32 npc_id = 0;
  3107. vector<int32>* ids = 0;
  3108. Spawn* spawn = nullptr;
  3109. int i = 0;
  3110. while ((npc_id = lua_interface->GetInt32Value(state, 8 + i))) {
  3111. if (ids == 0)
  3112. ids = new vector<int32>;
  3113. ids->push_back(npc_id);
  3114. i++;
  3115. }
  3116. QuestStep* quest_step = quest->AddQuestStep(step, QUEST_STEP_TYPE_KILL, description, ids, quantity, taskgroup, 0, 0, percentage, 0);
  3117. if (quest_step && icon > 0 && quantity > 0)
  3118. quest_step->SetIcon(icon);
  3119. if (quest->GetPlayer()) {
  3120. Client* client = quest->GetPlayer()->GetZone()->GetClientBySpawn(quest->GetPlayer());
  3121. quest->GetPlayer()->GetZone()->SendQuestUpdates(client);
  3122. }
  3123. }
  3124. return 0;
  3125. }
  3126. int EQ2Emu_lua_AddQuestStepChat(lua_State* state) {
  3127. if (!lua_interface)
  3128. return 0;
  3129. Quest* quest = lua_interface->GetQuest(state);
  3130. if (quest) {
  3131. int32 step = lua_interface->GetInt32Value(state, 2);
  3132. string description = lua_interface->GetStringValue(state, 3);
  3133. int32 quantity = lua_interface->GetInt32Value(state, 4);
  3134. string str_taskgroup = lua_interface->GetStringValue(state, 5);
  3135. int16 icon = lua_interface->GetInt16Value(state, 6);
  3136. const char* taskgroup = 0;
  3137. if (str_taskgroup.length() > 0)
  3138. taskgroup = str_taskgroup.c_str();
  3139. int32 npc_id = 0;
  3140. vector<int32>* ids = 0;
  3141. int i = 0;
  3142. while ((npc_id = lua_interface->GetInt32Value(state, 7 + i))) {
  3143. if (ids == 0)
  3144. ids = new vector<int32>;
  3145. ids->push_back(npc_id);
  3146. i++;
  3147. }
  3148. QuestStep* quest_step = quest->AddQuestStep(step, QUEST_STEP_TYPE_CHAT, description, ids, quantity, taskgroup);
  3149. if (quest_step && icon > 0)
  3150. quest_step->SetIcon(icon);
  3151. if (quest->GetPlayer()) {
  3152. Client* client = quest->GetPlayer()->GetZone()->GetClientBySpawn(quest->GetPlayer());
  3153. quest->GetPlayer()->GetZone()->SendQuestUpdates(client);
  3154. }
  3155. }
  3156. return 0;
  3157. }
  3158. int EQ2Emu_lua_AddQuestStepObtainItem(lua_State* state) {
  3159. if (!lua_interface)
  3160. return 0;
  3161. Quest* quest = lua_interface->GetQuest(state);
  3162. if (quest) {
  3163. int32 step = lua_interface->GetInt32Value(state, 2);
  3164. string description = lua_interface->GetStringValue(state, 3);
  3165. int32 quantity = lua_interface->GetInt32Value(state, 4);
  3166. float percentage = lua_interface->GetFloatValue(state, 5);
  3167. string str_taskgroup = lua_interface->GetStringValue(state, 6);
  3168. int16 icon = lua_interface->GetInt16Value(state, 7);
  3169. const char* taskgroup = 0;
  3170. if (str_taskgroup.length() > 0)
  3171. taskgroup = str_taskgroup.c_str();
  3172. int32 item_id = 0;
  3173. vector<int32>* ids = 0;
  3174. int i = 0;
  3175. while ((item_id = lua_interface->GetInt32Value(state, 8 + i))) {
  3176. if (ids == 0)
  3177. ids = new vector<int32>;
  3178. ids->push_back(item_id);
  3179. i++;
  3180. }
  3181. QuestStep* quest_step = quest->AddQuestStep(step, QUEST_STEP_TYPE_OBTAIN_ITEM, description, ids, quantity, taskgroup, 0, 0, percentage, 0);
  3182. if (quest_step && icon > 0 && quantity > 0)
  3183. quest_step->SetIcon(icon);
  3184. if (quest->GetPlayer()) {
  3185. Client* client = quest->GetPlayer()->GetZone()->GetClientBySpawn(quest->GetPlayer());
  3186. quest->GetPlayer()->GetZone()->SendQuestUpdates(client);
  3187. }
  3188. }
  3189. return 0;
  3190. }
  3191. int EQ2Emu_lua_AddQuestStepLocation(lua_State* state) {
  3192. if (!lua_interface)
  3193. return 0;
  3194. Quest* quest = lua_interface->GetQuest(state);
  3195. if (quest) {
  3196. int32 step = lua_interface->GetInt32Value(state, 2);
  3197. string description = lua_interface->GetStringValue(state, 3);
  3198. float max_variation = lua_interface->GetFloatValue(state, 4);
  3199. string str_taskgroup = lua_interface->GetStringValue(state, 5);
  3200. int16 icon = lua_interface->GetInt16Value(state, 6);
  3201. const char* taskgroup = 0;
  3202. if (str_taskgroup.length() > 0)
  3203. taskgroup = str_taskgroup.c_str();
  3204. vector<Location>* locations = 0;
  3205. int i = 7;
  3206. while (true) {
  3207. Location loc;
  3208. loc.x = lua_interface->GetFloatValue(state, i);
  3209. loc.y = lua_interface->GetFloatValue(state, i + 1);
  3210. loc.z = lua_interface->GetFloatValue(state, i + 2);
  3211. if (loc.x == 0 && loc.y == 0 && loc.z == 0)
  3212. break;
  3213. if (locations == 0)
  3214. locations = new vector<Location>;
  3215. locations->push_back(loc);
  3216. i += 3;
  3217. }
  3218. QuestStep* quest_step = quest->AddQuestStep(step, QUEST_STEP_TYPE_LOCATION, description, 0, 1, taskgroup, locations, max_variation);
  3219. if (quest_step && icon > 0)
  3220. quest_step->SetIcon(icon);
  3221. if (quest->GetPlayer()) {
  3222. Client* client = quest->GetPlayer()->GetZone()->GetClientBySpawn(quest->GetPlayer());
  3223. quest->GetPlayer()->GetZone()->SendQuestUpdates(client);
  3224. }
  3225. }
  3226. return 0;
  3227. }
  3228. int EQ2Emu_lua_AddQuestUsableItem(lua_State* state) {
  3229. if (!lua_interface)
  3230. return 0;
  3231. Quest* quest = lua_interface->GetQuest(state);
  3232. if (quest) {
  3233. int32 step = lua_interface->GetInt32Value(state, 2);
  3234. string description = lua_interface->GetStringValue(state, 3);
  3235. float max_variation = lua_interface->GetFloatValue(state, 4);
  3236. string str_taskgroup = lua_interface->GetStringValue(state, 5);
  3237. int16 icon = lua_interface->GetInt16Value(state, 6);
  3238. const char* taskgroup = 0;
  3239. if (str_taskgroup.length() > 0)
  3240. taskgroup = str_taskgroup.c_str();
  3241. vector<Location>* locations = 0;
  3242. int i = 7;
  3243. while (true) {
  3244. Location loc;
  3245. loc.x = lua_interface->GetFloatValue(state, i);
  3246. loc.y = lua_interface->GetFloatValue(state, i + 1);
  3247. loc.z = lua_interface->GetFloatValue(state, i + 2);
  3248. if (loc.x == 0 && loc.y == 0 && loc.z == 0)
  3249. break;
  3250. if (locations == 0)
  3251. locations = new vector<Location>;
  3252. locations->push_back(loc);
  3253. i += 3;
  3254. }
  3255. QuestStep* quest_step = quest->AddQuestStep(step, QUEST_STEP_TYPE_LOCATION, description, 0, 1, taskgroup, locations, max_variation);
  3256. if (quest_step && icon > 0)
  3257. quest_step->SetIcon(icon);
  3258. if (quest->GetPlayer()) {
  3259. Client* client = quest->GetPlayer()->GetZone()->GetClientBySpawn(quest->GetPlayer());
  3260. quest->GetPlayer()->GetZone()->SendQuestUpdates(client);
  3261. }
  3262. }
  3263. return 0;
  3264. }
  3265. int EQ2Emu_lua_AddQuestStepSpell(lua_State* state) {
  3266. Quest* quest = lua_interface->GetQuest(state);
  3267. if (quest) {
  3268. int32 step = lua_interface->GetInt32Value(state, 2);
  3269. string description = lua_interface->GetStringValue(state, 3);
  3270. int32 quantity = lua_interface->GetInt32Value(state, 4);
  3271. float percentage = lua_interface->GetFloatValue(state, 5);
  3272. string str_taskgroup = lua_interface->GetStringValue(state, 6);
  3273. int16 icon = lua_interface->GetInt16Value(state, 7);
  3274. const char* taskgroup = 0;
  3275. if (str_taskgroup.length() > 0)
  3276. taskgroup = str_taskgroup.c_str();
  3277. int32 spell_id = 0;
  3278. vector<int32>* ids = 0;
  3279. int i = 0;
  3280. while ((spell_id = lua_interface->GetInt32Value(state, 8 + i))) {
  3281. if (ids == 0)
  3282. ids = new vector<int32>;
  3283. ids->push_back(spell_id);
  3284. i++;
  3285. }
  3286. QuestStep* quest_step = quest->AddQuestStep(step, QUEST_STEP_TYPE_SPELL, description, ids, quantity, taskgroup, 0, 0, percentage, 0);
  3287. if (quest_step && icon > 0 && quantity > 0)
  3288. quest_step->SetIcon(icon);
  3289. if (quest->GetPlayer()) {
  3290. Client* client = quest->GetPlayer()->GetZone()->GetClientBySpawn(quest->GetPlayer());
  3291. quest->GetPlayer()->GetZone()->SendQuestUpdates(client);
  3292. }
  3293. }
  3294. return 0;
  3295. }
  3296. int EQ2Emu_lua_AddQuestStepCraft(lua_State* state) {
  3297. if (!lua_interface)
  3298. return 0;
  3299. Quest* quest = lua_interface->GetQuest(state);
  3300. if (quest) {
  3301. int32 step = lua_interface->GetInt32Value(state, 2);
  3302. string description = lua_interface->GetStringValue(state, 3);
  3303. int32 quantity = lua_interface->GetInt32Value(state, 4);
  3304. float percentage = lua_interface->GetFloatValue(state, 5);
  3305. string str_taskgroup = lua_interface->GetStringValue(state, 6);
  3306. int16 icon = lua_interface->GetInt16Value(state, 7);
  3307. const char* taskgroup = 0;
  3308. if (str_taskgroup.length() > 0)
  3309. taskgroup = str_taskgroup.c_str();
  3310. int32 item_id = 0;
  3311. vector<int32>* ids = 0;
  3312. int i = 0;
  3313. while ((item_id = lua_interface->GetInt32Value(state, 8 + i))) {
  3314. if (ids == 0)
  3315. ids = new vector<int32>;
  3316. ids->push_back(item_id);
  3317. i++;
  3318. }
  3319. QuestStep* quest_step = quest->AddQuestStep(step, QUEST_STEP_TYPE_CRAFT, description, ids, quantity, taskgroup, 0, 0, percentage, 0);
  3320. if (quest_step && icon > 0 && quantity > 0)
  3321. quest_step->SetIcon(icon);
  3322. if (quest->GetPlayer()) {
  3323. Client* client = quest->GetPlayer()->GetZone()->GetClientBySpawn(quest->GetPlayer());
  3324. quest->GetPlayer()->GetZone()->SendQuestUpdates(client);
  3325. }
  3326. }
  3327. return 0;
  3328. }
  3329. int EQ2Emu_lua_AddQuestStepHarvest(lua_State* state) {
  3330. if (!lua_interface)
  3331. return 0;
  3332. Quest* quest = lua_interface->GetQuest(state);
  3333. if (quest) {
  3334. int32 step = lua_interface->GetInt32Value(state, 2);
  3335. string description = lua_interface->GetStringValue(state, 3);
  3336. int32 quantity = lua_interface->GetInt32Value(state, 4);
  3337. float percentage = lua_interface->GetFloatValue(state, 5);
  3338. string str_taskgroup = lua_interface->GetStringValue(state, 6);
  3339. int16 icon = lua_interface->GetInt16Value(state, 7);
  3340. const char* taskgroup = 0;
  3341. if (str_taskgroup.length() > 0)
  3342. taskgroup = str_taskgroup.c_str();
  3343. int32 item_id = 0;
  3344. vector<int32>* ids = 0;
  3345. int i = 0;
  3346. while ((item_id = lua_interface->GetInt32Value(state, 8 + i))) {
  3347. if (ids == 0)
  3348. ids = new vector<int32>;
  3349. ids->push_back(item_id);
  3350. i++;
  3351. }
  3352. QuestStep* quest_step = quest->AddQuestStep(step, QUEST_STEP_TYPE_HARVEST, description, ids, quantity, taskgroup, 0, 0, percentage, 0);
  3353. if (quest_step && icon > 0 && quantity > 0)
  3354. quest_step->SetIcon(icon);
  3355. if (quest->GetPlayer()) {
  3356. Client* client = quest->GetPlayer()->GetZone()->GetClientBySpawn(quest->GetPlayer());
  3357. quest->GetPlayer()->GetZone()->SendQuestUpdates(client);
  3358. }
  3359. }
  3360. return 0;
  3361. }
  3362. int EQ2Emu_lua_SetQuestCompleteAction(lua_State* state) {
  3363. if (!lua_interface)
  3364. return 0;
  3365. Quest* quest = lua_interface->GetQuest(state);
  3366. if (quest) {
  3367. string action = lua_interface->GetStringValue(state, 2);
  3368. if (action.length() > 0)
  3369. quest->SetCompleteAction(action);
  3370. }
  3371. return 0;
  3372. }
  3373. int EQ2Emu_lua_AddQuestStepCompleteAction(lua_State* state) {
  3374. if (!lua_interface)
  3375. return 0;
  3376. Quest* quest = lua_interface->GetQuest(state);
  3377. if (quest) {
  3378. int32 step = lua_interface->GetInt32Value(state, 2);
  3379. string action = lua_interface->GetStringValue(state, 3);
  3380. if (step > 0 && action.length() > 0)
  3381. quest->AddCompleteAction(step, action);
  3382. }
  3383. return 0;
  3384. }
  3385. int EQ2Emu_lua_AddQuestStepProgressAction(lua_State* state) {
  3386. if (!lua_interface)
  3387. return 0;
  3388. Quest* quest = lua_interface->GetQuest(state);
  3389. if (quest) {
  3390. int32 step = lua_interface->GetInt32Value(state, 2);
  3391. string action = lua_interface->GetStringValue(state, 3);
  3392. if (step > 0 && action.length() > 0)
  3393. quest->AddProgressAction(step, action);
  3394. }
  3395. return 0;
  3396. }
  3397. int EQ2Emu_lua_UpdateQuestDescription(lua_State* state) {
  3398. if (!lua_interface)
  3399. return 0;
  3400. Quest* quest = lua_interface->GetQuest(state);
  3401. string description = lua_interface->GetStringValue(state, 2);
  3402. if (quest && description.length() > 0)
  3403. quest->SetDescription(description);
  3404. return 0;
  3405. }
  3406. int EQ2Emu_lua_SetCompletedDescription(lua_State* state) {
  3407. if (!lua_interface)
  3408. return 0;
  3409. Quest* quest = lua_interface->GetQuest(state);
  3410. string description = lua_interface->GetStringValue(state, 2);
  3411. if (quest && description.length() > 0)
  3412. quest->SetCompletedDescription(description);
  3413. return 0;
  3414. }
  3415. int EQ2Emu_lua_UpdateQuestTaskGroupDescription(lua_State* state) {
  3416. if (!lua_interface)
  3417. return 0;
  3418. Quest* quest = lua_interface->GetQuest(state);
  3419. int32 step = lua_interface->GetInt32Value(state, 2);
  3420. string description = lua_interface->GetStringValue(state, 3);
  3421. bool display_bullets = (lua_interface->GetInt8Value(state, 4) == 1);
  3422. if (quest && step > 0 && description.length() > 0) {
  3423. quest->SetTaskGroupDescription(step, description, display_bullets);
  3424. if (quest->GetPlayer()) {
  3425. Client* client = quest->GetPlayer()->GetZone()->GetClientBySpawn(quest->GetPlayer());
  3426. if (client)
  3427. client->SendQuestUpdateStep(quest, step, false);
  3428. }
  3429. }
  3430. return 0;
  3431. }
  3432. int EQ2Emu_lua_UpdateQuestStepDescription(lua_State* state) {
  3433. if (!lua_interface)
  3434. return 0;
  3435. Quest* quest = lua_interface->GetQuest(state);
  3436. int32 step = lua_interface->GetInt32Value(state, 2);
  3437. string description = lua_interface->GetStringValue(state, 3);
  3438. if (quest && step > 0 && description.length() > 0) {
  3439. quest->SetStepDescription(step, description);
  3440. if (quest->GetPlayer()) {
  3441. Client* client = quest->GetPlayer()->GetZone()->GetClientBySpawn(quest->GetPlayer());
  3442. if (client)
  3443. client->SendQuestUpdateStepImmediately(quest, step);
  3444. }
  3445. }
  3446. return 0;
  3447. }
  3448. int EQ2Emu_lua_UpdateQuestZone(lua_State* state) {
  3449. Quest* quest = lua_interface->GetQuest(state);
  3450. string zone = lua_interface->GetStringValue(state, 2);
  3451. if (quest && zone.length() > 0)
  3452. quest->SetZone(zone);
  3453. return 0;
  3454. }
  3455. int EQ2Emu_lua_GiveImmediateQuestReward(lua_State* state) {
  3456. if (!lua_interface)
  3457. return 0;
  3458. Quest* quest = lua_interface->GetQuest(state);
  3459. Spawn* playerSpawn = lua_interface->GetSpawn(state, 2);
  3460. int32 coin = lua_interface->GetInt32Value(state, 3);
  3461. int32 status_points = lua_interface->GetInt32Value(state, 4);
  3462. string rewards_str = lua_interface->GetStringValue(state, 5);
  3463. string select_rewards_str = lua_interface->GetStringValue(state, 6);
  3464. string factions_map_str = lua_interface->GetStringValue(state, 7);
  3465. string text = lua_interface->GetStringValue(state, 8);
  3466. int32 source_id = 0;
  3467. if (quest)
  3468. source_id = quest->GetQuestID();
  3469. if (playerSpawn && playerSpawn->IsPlayer()) {
  3470. Player* player = (Player*)playerSpawn;
  3471. Client* client = player->GetZone()->GetClientBySpawn(player);
  3472. if (client) {
  3473. vector<Item*> reward_items;
  3474. vector<Item*> selectable_reward_items;
  3475. if (rewards_str.length() > 0) {
  3476. map<unsigned int, unsigned short> rewards = ParseIntMap(rewards_str);
  3477. map<unsigned int, unsigned short>::iterator itr;
  3478. for (itr = rewards.begin(); itr != rewards.end(); itr++) {
  3479. if (itr->first > 0) {
  3480. Item* item = new Item(master_item_list.GetItem(itr->first));
  3481. if (item) {
  3482. if (itr->second > 0)
  3483. item->stack_count = itr->second;
  3484. reward_items.push_back(item);
  3485. }
  3486. }
  3487. }
  3488. }
  3489. if (select_rewards_str.length() > 0) {
  3490. map<unsigned int, unsigned short> rewards = ParseIntMap(select_rewards_str);
  3491. map<unsigned int, unsigned short>::iterator itr;
  3492. for (itr = rewards.begin(); itr != rewards.end(); itr++) {
  3493. if (itr->first > 0) {
  3494. Item* item = new Item(master_item_list.GetItem(itr->first));
  3495. if (item) {
  3496. if (itr->second > 0)
  3497. item->stack_count = itr->second;
  3498. selectable_reward_items.push_back(item);
  3499. }
  3500. }
  3501. }
  3502. }
  3503. map<unsigned int, signed int> faction_rewards = ParseSInt32Map(factions_map_str);
  3504. const char* reward_type = "Quest Reward!";
  3505. if (!quest)
  3506. reward_type = "Reward!";
  3507. client->DisplayQuestRewards(0, coin, &reward_items, &selectable_reward_items, &faction_rewards, reward_type, status_points, text.c_str());
  3508. }
  3509. }
  3510. /*PacketStruct* packet2 = configReader.getStruct("WS_QuestRewardPackMsg", client->GetVersion());
  3511. if (packet2) {
  3512. player->AddCoins(coin);
  3513. client->PlaySound("coin_cha_ching");
  3514. packet2->setSubstructDataByName("reward_data", "unknown1", 255);
  3515. if(quest)
  3516. packet2->setSubstructDataByName("reward_data", "reward", "Quest Reward!");
  3517. else
  3518. packet2->setSubstructDataByName("reward_data", "reward", "Reward!");
  3519. packet2->setSubstructDataByName("reward_data", "coin", coin);
  3520. if (player->GetGuild()) {
  3521. player->GetInfoStruct()->status_points += status_points;
  3522. packet2->setSubstructDataByName("reward_data", "status_points", status_points);
  3523. }
  3524. if (rewards_str.length() > 0) {
  3525. map<unsigned int, unsigned short> rewards = ParseIntMap(rewards_str);
  3526. vector<Item*> reward_items;
  3527. map<unsigned int, unsigned short>::iterator itr;
  3528. for (itr = rewards.begin(); itr != rewards.end(); itr++) {
  3529. if (itr->first > 0) {
  3530. Item* item = new Item(master_item_list.GetItem(itr->first));
  3531. if (item) {
  3532. if (itr->second > 0)
  3533. item->stack_count = itr->second;
  3534. reward_items.push_back(item);
  3535. }
  3536. }
  3537. }
  3538. packet2->setSubstructArrayLengthByName("reward_data", "num_rewards", reward_items.size());
  3539. for (int i = 0; i < reward_items.size(); i++) {
  3540. Item* item = reward_items[i];
  3541. packet2->setArrayDataByName("reward_id", item->details.item_id, i);
  3542. packet2->setItemArrayDataByName("item", item, client->GetPlayer(), i, 0, -1);
  3543. player->AddPendingItemReward(item); //item reference will be deleted after the player accepts it
  3544. }
  3545. }
  3546. if (select_rewards_str.length() > 0) {
  3547. map<unsigned int, unsigned short> rewards = ParseIntMap(select_rewards_str);
  3548. vector<Item*> reward_items;
  3549. map<unsigned int, unsigned short>::iterator itr;
  3550. for (itr = rewards.begin(); itr != rewards.end(); itr++) {
  3551. if (itr->first > 0) {
  3552. Item* item = new Item(master_item_list.GetItem(itr->first));
  3553. if (item) {
  3554. if (itr->second > 0)
  3555. item->stack_count = itr->second;
  3556. reward_items.push_back(item);
  3557. }
  3558. }
  3559. }
  3560. packet2->setSubstructArrayLengthByName("reward_data", "num_select_rewards", reward_items.size());
  3561. for (int i = 0; i < reward_items.size(); i++) {
  3562. Item* item = reward_items[i];
  3563. packet2->setArrayDataByName("select_reward_id", item->details.item_id, i);
  3564. packet2->setItemArrayDataByName("select_item", item, client->GetPlayer(), i, 0, -1);
  3565. player->AddPendingSelectableItemReward(source_id, item); //item reference will be deleted after the player selects one
  3566. }
  3567. }
  3568. if (factions_map_str.length() > 0) {
  3569. map<unsigned int, signed int> faction_rewards = ParseSInt32Map(factions_map_str);
  3570. map<unsigned int, signed int>::iterator itr;
  3571. map<Faction*, signed int> factions;
  3572. for (itr = faction_rewards.begin(); itr != faction_rewards.end(); itr++) {
  3573. Faction* faction = master_faction_list.GetFaction(itr->first);
  3574. if (faction)
  3575. factions[faction] = itr->second;
  3576. }
  3577. packet2->setSubstructArrayLengthByName("reward_data", "num_factions", factions.size());
  3578. map<Faction*, signed int>::iterator faction_itr;
  3579. int8 i = 0;
  3580. for (faction_itr = factions.begin(); faction_itr != factions.end(); faction_itr++) {
  3581. packet2->setArrayDataByName("faction_name", faction_itr->first->name.c_str(), i);
  3582. sint32 amount = faction_itr->second;
  3583. packet2->setArrayDataByName("amount", amount, i);
  3584. if (amount > 0)
  3585. player->GetFactions()->IncreaseFaction(faction_itr->first->id, amount);
  3586. else
  3587. player->GetFactions()->DecreaseFaction(faction_itr->first->id, (amount * -1));
  3588. i++;
  3589. }
  3590. }
  3591. client->QueuePacket(packet2->serialize());
  3592. safe_delete(packet2);
  3593. }*/
  3594. return 0;
  3595. }
  3596. int EQ2Emu_lua_GiveQuestReward(lua_State* state) {
  3597. if (!lua_interface)
  3598. return 0;
  3599. Quest* quest = lua_interface->GetQuest(state);
  3600. Spawn* spawn = lua_interface->GetSpawn(state, 2);
  3601. if (quest && spawn) {
  3602. if (spawn->IsPlayer()) {
  3603. Client* client = spawn->GetZone()->GetClientBySpawn(spawn);
  3604. if (client)
  3605. client->AddPendingQuestReward(quest);
  3606. }
  3607. }
  3608. return 0;
  3609. }
  3610. int EQ2Emu_lua_Harvest(lua_State* state) {
  3611. if (!lua_interface)
  3612. return 0;
  3613. Spawn* player = lua_interface->GetSpawn(state);
  3614. Spawn* node = lua_interface->GetSpawn(state, 2);
  3615. if (player && node && player->IsPlayer() && node->IsGroundSpawn()) {
  3616. Client* client = player->GetZone()->GetClientBySpawn(player);
  3617. if (client) {
  3618. LogWrite(MISC__TODO, 1, "TODO", "Cancel harvest if skill insufficient; Func: %s, Line: %i", __FUNCTION__, __LINE__);
  3619. ((GroundSpawn*)node)->ProcessHarvest(client);
  3620. if (((GroundSpawn*)node)->GetNumberHarvests() == 0)
  3621. player->GetZone()->RemoveSpawn(true, node, true);
  3622. }
  3623. }
  3624. else if (player && player->IsPlayer()) {
  3625. Client* client = player->GetZone()->GetClientBySpawn(player);
  3626. if (client)
  3627. client->Message(CHANNEL_COLOR_RED, "Invalid target for this spell.");
  3628. }
  3629. return 0;
  3630. }
  3631. int EQ2Emu_lua_Bind(lua_State* state) {
  3632. if (!lua_interface)
  3633. return 0;
  3634. Spawn* spawn = lua_interface->GetSpawn(state);
  3635. int32 zone_id = lua_interface->GetInt32Value(state, 2);
  3636. float x = lua_interface->GetFloatValue(state, 3);
  3637. float y = lua_interface->GetFloatValue(state, 4);
  3638. float z = lua_interface->GetFloatValue(state, 5);
  3639. float h = lua_interface->GetFloatValue(state, 6);
  3640. if (!spawn) {
  3641. lua_interface->LogError("%s: LUA Bind command error: spawn is not valid", lua_interface->GetScriptName(state));
  3642. return 0;
  3643. }
  3644. if (!spawn->IsPlayer()) {
  3645. lua_interface->LogError("%s: LUA Bind command error: spawn is not a player", lua_interface->GetScriptName(state));
  3646. return 0;
  3647. }
  3648. if (zone_id == 0) {
  3649. Client* client = spawn->GetZone()->GetClientBySpawn(spawn);
  3650. if (!client) {
  3651. lua_interface->LogError("%s: LUA Bind command error: unable to get client from spawn", lua_interface->GetScriptName(state));
  3652. return 0;
  3653. }
  3654. if (!client->Bind())
  3655. client->SimpleMessage(CHANNEL_COLOR_RED, "Unable to set bind point.");
  3656. }
  3657. else {
  3658. Player* player = (Player*)spawn;
  3659. player->GetPlayerInfo()->SetBindZone(zone_id);
  3660. player->GetPlayerInfo()->SetBindX(x);
  3661. player->GetPlayerInfo()->SetBindY(y);
  3662. player->GetPlayerInfo()->SetBindZ(z);
  3663. player->GetPlayerInfo()->SetBindHeading(h);
  3664. }
  3665. return 0;
  3666. }
  3667. int EQ2Emu_lua_Gate(lua_State* state) {
  3668. if (!lua_interface)
  3669. return 0;
  3670. Spawn* spawn = lua_interface->GetSpawn(state);
  3671. if (spawn) {
  3672. if (spawn->IsPlayer()) {
  3673. Client* client = spawn->GetZone()->GetClientBySpawn(spawn);
  3674. if (client) {
  3675. if (!client->Gate())
  3676. client->SimpleMessage(CHANNEL_COLOR_RED, "Unable to gate.");
  3677. }
  3678. }
  3679. }
  3680. return 0;
  3681. }
  3682. int EQ2Emu_lua_IsBindAllowed(lua_State* state) {
  3683. if (!lua_interface)
  3684. return 0;
  3685. bool ret = false;
  3686. Spawn* spawn = lua_interface->GetSpawn(state);
  3687. if (spawn) {
  3688. if (spawn->IsPlayer()) {
  3689. Client* client = spawn->GetZone()->GetClientBySpawn(spawn);
  3690. if (client)
  3691. ret = client->BindAllowed();
  3692. }
  3693. }
  3694. lua_interface->SetBooleanValue(state, ret);
  3695. return 1;
  3696. }
  3697. int EQ2Emu_lua_IsGateAllowed(lua_State* state) {
  3698. if (!lua_interface)
  3699. return 0;
  3700. bool ret = false;
  3701. Spawn* spawn = lua_interface->GetSpawn(state);
  3702. if (spawn) {
  3703. if (spawn->IsPlayer()) {
  3704. Client* client = spawn->GetZone()->GetClientBySpawn(spawn);
  3705. if (client)
  3706. ret = client->GateAllowed();
  3707. }
  3708. }
  3709. lua_interface->SetBooleanValue(state, ret);
  3710. return 1;
  3711. }
  3712. int EQ2Emu_lua_IsAlive(lua_State* state) {
  3713. Spawn* spawn = lua_interface->GetSpawn(state);
  3714. if (spawn) {
  3715. lua_interface->SetBooleanValue(state, spawn->Alive());
  3716. return 1;
  3717. }
  3718. return 0;
  3719. }
  3720. int EQ2Emu_lua_IsInCombat(lua_State* state) {
  3721. if (!lua_interface)
  3722. return 0;
  3723. Spawn* spawn = lua_interface->GetSpawn(state);
  3724. if (spawn && spawn->IsEntity()) {
  3725. lua_interface->SetBooleanValue(state, ((Entity*)spawn)->EngagedInCombat());
  3726. return 1;
  3727. }
  3728. return 0;
  3729. }
  3730. int EQ2Emu_lua_SendMessage(lua_State* state) {
  3731. Spawn* spawn = lua_interface->GetSpawn(state);
  3732. string message = lua_interface->GetStringValue(state, 2);
  3733. string color_str = lua_interface->GetStringValue(state, 3);
  3734. int8 color = CHANNEL_NARRATIVE;
  3735. if (spawn && spawn->IsPlayer() && message.length() > 0) {
  3736. Client* client = spawn->GetZone()->GetClientBySpawn(spawn);
  3737. if (client) {
  3738. if (color_str.length() > 0) {
  3739. // leave for backwards compat, but all future should just use the number
  3740. if (strncasecmp(color_str.c_str(), "red", 3) == 0)
  3741. color = CHANNEL_COLOR_RED;
  3742. else if (strncasecmp(color_str.c_str(), "yellow", 6) == 0)
  3743. color = CHANNEL_COLOR_YELLOW;
  3744. else
  3745. {
  3746. // use a number to specify the channel as per Commands/Commands.h defines
  3747. color = (int8)atoul(color_str.c_str());
  3748. }
  3749. }
  3750. client->SimpleMessage(color, message.c_str());
  3751. }
  3752. }
  3753. return 0;
  3754. }
  3755. int EQ2Emu_lua_SendPopUpMessage(lua_State* state) {
  3756. Spawn* spawn = lua_interface->GetSpawn(state);
  3757. string message = lua_interface->GetStringValue(state, 2);
  3758. int8 red = lua_interface->GetInt8Value(state, 3);
  3759. int8 green = lua_interface->GetInt8Value(state, 4);
  3760. int8 blue = lua_interface->GetInt8Value(state, 5);
  3761. if (!spawn) {
  3762. lua_interface->LogError("%s: LUA SendPopUpMessage command error: Spawn is not valid.", lua_interface->GetScriptName(state));
  3763. return 0;
  3764. }
  3765. int32 words = ::CountWordsInString(message.c_str());
  3766. if (words < 5)
  3767. words = 5;
  3768. Client* client = spawn->GetZone()->GetClientBySpawn(spawn);
  3769. if (client)
  3770. client->SendPopupMessage(10, message.c_str(), "ui_harvested_normal", words, red, green, blue);
  3771. return 0;
  3772. }
  3773. int EQ2Emu_lua_SetServerControlFlag(lua_State* state) {
  3774. Spawn* spawn = lua_interface->GetSpawn(state);
  3775. int8 param = lua_interface->GetInt8Value(state, 2);
  3776. int8 param_value = lua_interface->GetInt8Value(state, 3);
  3777. int8 value = lua_interface->GetInt8Value(state, 4);
  3778. if (spawn && spawn->IsPlayer() && (param >= 1 && param <= 5)) {
  3779. Client* client = spawn->GetZone()->GetClientBySpawn(spawn);
  3780. if (client) {
  3781. PacketStruct* packet = configReader.getStruct("WS_ServerControlFlags", client->GetVersion());
  3782. switch (param) {
  3783. case 1: {
  3784. packet->setDataByName("parameter1", param_value);
  3785. break;
  3786. }
  3787. case 2: {
  3788. packet->setDataByName("parameter2", param_value);
  3789. break;
  3790. }
  3791. case 3: {
  3792. packet->setDataByName("parameter3", param_value);
  3793. break;
  3794. }
  3795. case 4: {
  3796. packet->setDataByName("parameter4", param_value);
  3797. break;
  3798. }
  3799. case 5: {
  3800. packet->setDataByName("parameter5", param_value);
  3801. break;
  3802. }
  3803. }
  3804. packet->setDataByName("value", value);
  3805. client->QueuePacket(packet->serialize());
  3806. safe_delete(packet);
  3807. }
  3808. }
  3809. return 0;
  3810. }
  3811. int EQ2Emu_lua_ToggleTracking(lua_State* state) {
  3812. Spawn* spawn = lua_interface->GetSpawn(state);
  3813. if (spawn && spawn->IsPlayer()) {
  3814. if (((Player*)spawn)->GetIsTracking())
  3815. spawn->GetZone()->AddPlayerTracking((Player*)spawn);
  3816. else
  3817. spawn->GetZone()->RemovePlayerTracking((Player*)spawn, TRACKING_STOP);
  3818. }
  3819. return 0;
  3820. }
  3821. int EQ2Emu_lua_AddPrimaryEntityCommand(lua_State* state) {
  3822. Spawn* player = lua_interface->GetSpawn(state);
  3823. Spawn* spawn = lua_interface->GetSpawn(state, 2);
  3824. string name = lua_interface->GetStringValue(state, 3);
  3825. float distance = lua_interface->GetFloatValue(state, 4);
  3826. string command = lua_interface->GetStringValue(state, 5);
  3827. string error_text = lua_interface->GetStringValue(state, 6);
  3828. int16 cast_time = lua_interface->GetInt16Value(state, 7);
  3829. int32 spell_visual = lua_interface->GetInt32Value(state, 8);
  3830. bool denyListDefault = (lua_interface->GetInt8Value(state, 9) == 1);
  3831. if (spawn) {
  3832. if (distance == 0)
  3833. distance = 10.0f;
  3834. if (command.length() == 0)
  3835. command = name;
  3836. if (command.length() < 1 && name.length() < 1)
  3837. {
  3838. // have to run this first to send a 'blank' default command, then remove all commands from the list
  3839. spawn->GetZone()->SendUpdateDefaultCommand(spawn, command.c_str(), distance);
  3840. spawn->RemovePrimaryCommands();
  3841. }
  3842. else
  3843. {
  3844. spawn->AddPrimaryEntityCommand(name.c_str(), distance, command.c_str(), error_text.c_str(), cast_time, spell_visual, denyListDefault, (player && player->IsPlayer()) ? (Player*)player : NULL);
  3845. }
  3846. }
  3847. return 0;
  3848. }
  3849. int EQ2Emu_lua_AddSpellBookEntry(lua_State* state) {
  3850. if (!lua_interface)
  3851. return 0;
  3852. Spawn* player = lua_interface->GetSpawn(state);
  3853. int32 spellid = lua_interface->GetInt32Value(state, 2);
  3854. int16 tier = lua_interface->GetInt16Value(state, 3);
  3855. Spell* spell = master_spell_list.GetSpell(spellid, tier);
  3856. if (player && spell && player->IsPlayer()) {
  3857. Client* client = player->GetZone()->GetClientBySpawn(player);
  3858. if (client) {
  3859. if (!client->GetPlayer()->HasSpell(spellid, tier - 1, true))
  3860. {
  3861. Spell* spell = master_spell_list.GetSpell(spellid, tier);
  3862. client->GetPlayer()->AddSpellBookEntry(spellid, 1, client->GetPlayer()->GetFreeSpellBookSlot(spell->GetSpellData()->spell_book_type), spell->GetSpellData()->spell_book_type, spell->GetSpellData()->linked_timer, true);
  3863. client->GetPlayer()->UnlockSpell(spell);
  3864. client->SendSpellUpdate(spell);
  3865. }
  3866. else
  3867. {
  3868. Spell* spell = master_spell_list.GetSpell(spellid, tier);
  3869. int8 old_slot = client->GetPlayer()->GetSpellSlot(spell->GetSpellID());
  3870. client->GetPlayer()->RemoveSpellBookEntry(spell->GetSpellID());
  3871. client->GetPlayer()->AddSpellBookEntry(spell->GetSpellID(), spell->GetSpellTier(), old_slot, spell->GetSpellData()->spell_book_type, spell->GetSpellData()->linked_timer, true);
  3872. client->GetPlayer()->UnlockSpell(spell);
  3873. client->SendSpellUpdate(spell);
  3874. }
  3875. //if (client ) {
  3876. // ((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);
  3877. EQ2Packet* outapp = ((Player*)player)->GetSpellBookUpdatePacket(client->GetVersion());
  3878. if (outapp)
  3879. client->QueuePacket(outapp);
  3880. }
  3881. }
  3882. return 0;
  3883. }
  3884. int EQ2Emu_lua_HasFreeSlot(lua_State* state) {
  3885. if (!lua_interface)
  3886. return 0;
  3887. Spawn* player = lua_interface->GetSpawn(state);
  3888. if (player && player->IsPlayer()) {
  3889. lua_interface->SetBooleanValue(state, ((Player*)player)->item_list.HasFreeSlot());
  3890. return 1;
  3891. }
  3892. return 0;
  3893. }
  3894. int EQ2Emu_lua_Attack(lua_State* state) {
  3895. if (lua_interface) {
  3896. Spawn* npc = lua_interface->GetSpawn(state);
  3897. Spawn* player = lua_interface->GetSpawn(state, 2);
  3898. if (npc && player && npc->IsNPC() && player->IsPlayer())
  3899. ((NPC*)npc)->AddHate((Entity*)player, 100);
  3900. }
  3901. return 0;
  3902. }
  3903. int EQ2Emu_lua_ApplySpellVisual(lua_State* state) {
  3904. if (lua_interface) {
  3905. Spawn* target = lua_interface->GetSpawn(state);
  3906. int32 spell_visual = lua_interface->GetInt32Value(state, 2);
  3907. if (target && target->GetZone())
  3908. target->GetZone()->SendCastSpellPacket(spell_visual, target);
  3909. }
  3910. return 0;
  3911. }
  3912. int EQ2Emu_lua_HasCollectionsToHandIn(lua_State* state) {
  3913. Spawn* player;
  3914. if (lua_interface) {
  3915. player = lua_interface->GetSpawn(state);
  3916. if (player && player->IsPlayer()) {
  3917. lua_interface->SetBooleanValue(state, ((Player*)player)->GetCollectionList()->HasCollectionsToHandIn());
  3918. return 1;
  3919. }
  3920. }
  3921. return 0;
  3922. }
  3923. int EQ2Emu_lua_HandInCollections(lua_State* state) {
  3924. Spawn* player;
  3925. Client* client;
  3926. if (lua_interface) {
  3927. player = lua_interface->GetSpawn(state);
  3928. if (player && ((Player*)player)->IsPlayer() && ((Player*)player)->GetCollectionList()->HasCollectionsToHandIn())
  3929. if ((client = player->GetZone()->GetClientBySpawn(player)))
  3930. client->HandInCollections();
  3931. }
  3932. return 0;
  3933. }
  3934. int EQ2Emu_lua_UseWidget(lua_State* state) {
  3935. Spawn* widget;
  3936. if (lua_interface) {
  3937. widget = lua_interface->GetSpawn(state);
  3938. if (widget && widget->IsWidget())
  3939. ((Widget*)widget)->HandleUse(NULL, "");
  3940. }
  3941. return 0;
  3942. }
  3943. int EQ2Emu_lua_SetSpellList(lua_State* state) {
  3944. Spawn* spawn = 0;
  3945. int32 primary_list = 0;
  3946. int32 secondary_list = 0;
  3947. if (lua_interface) {
  3948. spawn = lua_interface->GetSpawn(state);
  3949. primary_list = lua_interface->GetInt32Value(state, 2);
  3950. secondary_list = lua_interface->GetInt32Value(state, 3);
  3951. if (!spawn->IsNPC()) {
  3952. lua_interface->LogError("%s: LUA SetSpellList command error: Spawn was not a valid NPC", lua_interface->GetScriptName(state));
  3953. return 0;
  3954. }
  3955. NPC* npc = (NPC*)spawn;
  3956. npc->SetPrimarySpellList(primary_list);
  3957. npc->SetSecondarySpellList(secondary_list);
  3958. npc->SetSpells(npc->GetZone()->GetNPCSpells(npc->GetPrimarySpellList(), npc->GetSecondarySpellList()));
  3959. }
  3960. return 0;
  3961. }
  3962. int EQ2Emu_lua_GetPet(lua_State* state) {
  3963. if (!lua_interface)
  3964. return 0;
  3965. Spawn* spawn = lua_interface->GetSpawn(state);
  3966. if (spawn) {
  3967. if (spawn->IsEntity() && ((Entity*)spawn)->GetPet()) {
  3968. lua_interface->SetSpawnValue(state, ((Entity*)spawn)->GetPet());
  3969. return 1;
  3970. }
  3971. }
  3972. return 0;
  3973. }
  3974. int EQ2Emu_lua_GetCharmedPet(lua_State* state) {
  3975. if (!lua_interface)
  3976. return 0;
  3977. Spawn* spawn = lua_interface->GetSpawn(state);
  3978. if (spawn) {
  3979. if (spawn->IsEntity() && ((Entity*)spawn)->GetCharmedPet()) {
  3980. lua_interface->SetSpawnValue(state, ((Entity*)spawn)->GetCharmedPet());
  3981. return 1;
  3982. }
  3983. }
  3984. return 0;
  3985. }
  3986. int EQ2Emu_lua_GetDeityPet(lua_State* state) {
  3987. if (!lua_interface)
  3988. return 0;
  3989. Spawn* spawn = lua_interface->GetSpawn(state);
  3990. if (spawn) {
  3991. if (spawn->IsEntity() && ((Entity*)spawn)->GetDeityPet()) {
  3992. lua_interface->SetSpawnValue(state, ((Entity*)spawn)->GetDeityPet());
  3993. return 1;
  3994. }
  3995. }
  3996. return 0;
  3997. }
  3998. int EQ2Emu_lua_GetCosmeticPet(lua_State* state) {
  3999. if (!lua_interface)
  4000. return 0;
  4001. Spawn* spawn = lua_interface->GetSpawn(state);
  4002. if (spawn) {
  4003. if (spawn->IsEntity() && ((Entity*)spawn)->GetCosmeticPet()) {
  4004. lua_interface->SetSpawnValue(state, ((Entity*)spawn)->GetCosmeticPet());
  4005. return 1;
  4006. }
  4007. }
  4008. return 0;
  4009. }
  4010. int EQ2Emu_lua_Charm(lua_State* state) {
  4011. if (!lua_interface)
  4012. return 0;
  4013. Spawn* owner = lua_interface->GetSpawn(state);
  4014. Spawn* pet = lua_interface->GetSpawn(state, 2);
  4015. LuaSpell* luaspell = lua_interface->GetCurrentSpell(state);
  4016. if (!luaspell) {
  4017. lua_interface->LogError("%s: LUA Charm command error: Spell is not valid, charm can only be used in spell scripts.", lua_interface->GetScriptName(state));
  4018. return 0;
  4019. }
  4020. if (owner && pet && owner->IsEntity() && pet->IsNPC()) {
  4021. ((Entity*)owner)->SetCharmedPet((Entity*)pet);
  4022. pet->SetPet(true);
  4023. ((NPC*)pet)->SetPetType(PET_TYPE_CHARMED);
  4024. ((NPC*)pet)->SetOwner((Entity*)owner);
  4025. // If owner is player and player does not have a summoned pet set the players charsheet
  4026. if (owner->IsPlayer() && !((Entity*)owner)->GetPet()) {
  4027. Player* player = (Player*)owner;
  4028. player->GetInfoStruct()->pet_id = player->GetIDWithPlayerSpawn(pet);
  4029. strcpy(player->GetInfoStruct()->pet_name, pet->GetName());
  4030. player->GetInfoStruct()->pet_movement = 2;
  4031. player->GetInfoStruct()->pet_behavior = 3;
  4032. player->GetInfoStruct()->pet_health_pct = 1.0f;
  4033. player->GetInfoStruct()->pet_power_pct = 1.0f;
  4034. // Make sure the values get sent to the client
  4035. player->SetCharSheetChanged(true);
  4036. }
  4037. // Clear the spawns script so the charmed mob doesn't try to do anything like random walks
  4038. pet->SetSpawnScript("");
  4039. // Set faction to the same as the owner
  4040. pet->SetFactionID(owner->GetFactionID());
  4041. ((NPC*)pet)->SetPetSpellID(luaspell->spell->GetSpellData()->id);
  4042. ((NPC*)pet)->SetPetSpellTier(luaspell->spell->GetSpellData()->tier);
  4043. // Clear hate list
  4044. ((NPC*)pet)->Brain()->ClearHate();
  4045. // Set the brain to a pet brain
  4046. ((NPC*)pet)->SetBrain(new CombatPetBrain((NPC*)pet));
  4047. }
  4048. return 0;
  4049. }
  4050. int EQ2Emu_lua_GetGroup(lua_State* state) {
  4051. if (!lua_interface)
  4052. return 0;
  4053. Spawn* spawn = lua_interface->GetSpawn(state);
  4054. if (!spawn) {
  4055. lua_interface->LogError("%s: LUA GetGroup command error: spawn is not valid", lua_interface->GetScriptName(state));
  4056. return 0;
  4057. }
  4058. vector<Spawn*> groupMembers;
  4059. if (!spawn->IsPlayer() && spawn->HasSpawnGroup()) {
  4060. groupMembers = *spawn->GetSpawnGroup();
  4061. }
  4062. else if (spawn->IsPlayer() && ((Player*)spawn)->GetGroupMemberInfo()) {
  4063. world.GetGroupManager()->GroupLock(__FUNCTION__, __LINE__);
  4064. deque<GroupMemberInfo*>::iterator itr;
  4065. PlayerGroup* group = world.GetGroupManager()->GetGroup(((Player*)spawn)->GetGroupMemberInfo()->group_id);
  4066. if (group)
  4067. {
  4068. group->MGroupMembers.readlock(__FUNCTION__, __LINE__);
  4069. deque<GroupMemberInfo*>* members = group->GetMembers();
  4070. GroupMemberInfo* info = 0;
  4071. for (itr = members->begin(); itr != members->end(); itr++) {
  4072. info = *itr;
  4073. if (info->client)
  4074. groupMembers.push_back(info->client->GetPlayer());
  4075. }
  4076. group->MGroupMembers.releasereadlock(__FUNCTION__, __LINE__);
  4077. }
  4078. world.GetGroupManager()->ReleaseGroupLock(__FUNCTION__, __LINE__);
  4079. }
  4080. else
  4081. return 0;
  4082. lua_createtable(state, groupMembers.size(), 0);
  4083. int newTable = lua_gettop(state);
  4084. for (int32 i = 0; i < groupMembers.size(); i++) {
  4085. lua_interface->SetSpawnValue(state, groupMembers.at(i));
  4086. lua_rawseti(state, newTable, i + 1);
  4087. }
  4088. return 1;
  4089. }
  4090. int EQ2Emu_lua_CreateOptionWindow(lua_State* state) {
  4091. if (!lua_interface)
  4092. return 0;
  4093. vector<OptionWindowOption>* option_window = new vector<OptionWindowOption>();
  4094. lua_interface->SetOptionWindowValue(state, option_window);
  4095. return 1;
  4096. }
  4097. int EQ2Emu_lua_AddOptionWindowOption(lua_State* state) {
  4098. if (!lua_interface)
  4099. return 0;
  4100. vector<OptionWindowOption>* option_window = lua_interface->GetOptionWindow(state);
  4101. if (option_window) {
  4102. OptionWindowOption option_window_option;
  4103. option_window_option.optionName = lua_interface->GetStringValue(state, 2);
  4104. option_window_option.optionDescription = lua_interface->GetStringValue(state, 3);
  4105. option_window_option.optionIconSheet = lua_interface->GetInt32Value(state, 4);
  4106. option_window_option.optionIconID = lua_interface->GetInt16Value(state, 5);
  4107. option_window_option.optionCommand = lua_interface->GetStringValue(state, 6);
  4108. option_window_option.optionConfirmTitle = lua_interface->GetStringValue(state, 7);
  4109. if (option_window_option.optionName.length() > 0 && option_window_option.optionDescription.length() > 0)
  4110. option_window->push_back(option_window_option);
  4111. }
  4112. return 0;
  4113. }
  4114. int EQ2Emu_lua_SendOptionWindow(lua_State* state) {
  4115. if (!lua_interface)
  4116. return 0;
  4117. vector<OptionWindowOption>* option_window = lua_interface->GetOptionWindow(state);
  4118. Spawn* player = lua_interface->GetSpawn(state, 2);
  4119. string window_title = lua_interface->GetStringValue(state, 3);
  4120. string cancel_command = lua_interface->GetStringValue(state, 4);
  4121. Client* client = player->GetZone()->GetClientBySpawn(player);
  4122. if (option_window && window_title.length() > 0 && client) {
  4123. PacketStruct* packet = configReader.getStruct("WS_SelectTradeskill", client->GetVersion());
  4124. if (!packet)
  4125. return 0;
  4126. packet->setDataByName("title_text", window_title.c_str());
  4127. if (cancel_command.length() > 0)
  4128. packet->setDataByName("command_text_cancel", cancel_command.c_str());
  4129. packet->setArrayLengthByName("num_selections", option_window->size());
  4130. vector<OptionWindowOption>::iterator itr;
  4131. int8 i = 0;
  4132. for (itr = option_window->begin(); itr != option_window->end(); itr++) {
  4133. OptionWindowOption opt = *itr;
  4134. packet->setArrayDataByName("tradeskill_name", opt.optionName.c_str(), i);
  4135. packet->setArrayDataByName("tradeskill_description", opt.optionDescription.c_str(), i);
  4136. packet->setArrayDataByName("icon_sheet", opt.optionIconSheet, i);
  4137. packet->setArrayDataByName("icon_id", opt.optionIconID, i);
  4138. if (opt.optionCommand.length() > 0)
  4139. packet->setArrayDataByName("command_text", opt.optionCommand.c_str(), i);
  4140. if (opt.optionConfirmTitle.length() > 0)
  4141. packet->setArrayDataByName("confirm_window_title", opt.optionConfirmTitle.c_str(), i);
  4142. i++;
  4143. }
  4144. client->QueuePacket(packet->serialize());
  4145. safe_delete(option_window);
  4146. safe_delete(packet);
  4147. }
  4148. return 0;
  4149. }
  4150. int EQ2Emu_lua_GetTradeskillClass(lua_State* state) {
  4151. if (!lua_interface)
  4152. return 0;
  4153. Spawn* spawn = lua_interface->GetSpawn(state);
  4154. if (spawn) {
  4155. lua_interface->SetInt32Value(state, spawn->GetTradeskillClass());
  4156. return 1;
  4157. }
  4158. else
  4159. lua_interface->LogError("%s: LUA GetTradeskillClass command error: Spawn was not valid", lua_interface->GetScriptName(state));
  4160. return 0;
  4161. }
  4162. int EQ2Emu_lua_GetTradeskillLevel(lua_State* state) {
  4163. if (!lua_interface)
  4164. return 0;
  4165. Spawn* spawn = lua_interface->GetSpawn(state);
  4166. if (spawn) {
  4167. lua_interface->SetInt32Value(state, spawn->GetTSLevel());
  4168. return 1;
  4169. }
  4170. else
  4171. lua_interface->LogError("%s: LUA GetTradeskillLevel command error: Spawns was not valid", lua_interface->GetScriptName(state));
  4172. return 0;
  4173. }
  4174. int EQ2Emu_lua_GetTradeskillClassName(lua_State* state) {
  4175. if (!lua_interface)
  4176. return 0;
  4177. Spawn* spawn = lua_interface->GetSpawn(state);
  4178. if (spawn) {
  4179. int8 class_id = spawn->GetTradeskillClass();
  4180. // Need to add 42 for the offset in the array
  4181. class_id += 44;
  4182. lua_interface->SetStringValue(state, classes.GetClassNameCase(class_id).c_str());
  4183. return 1;
  4184. }
  4185. else
  4186. lua_interface->LogError("%s: LUA GetTradeskillClassName command error: Spawn was not valid", lua_interface->GetScriptName(state));
  4187. return 0;
  4188. }
  4189. int EQ2Emu_lua_SetTradeskillLevel(lua_State* state) {
  4190. if (!lua_interface)
  4191. return 0;
  4192. Spawn* spawn = lua_interface->GetSpawn(state);
  4193. int16 level = lua_interface->GetInt8Value(state, 2);
  4194. if (spawn) {
  4195. if (spawn->IsPlayer())
  4196. spawn->GetZone()->GetClientBySpawn(spawn)->ChangeTSLevel(spawn->GetTSLevel(), level);
  4197. else
  4198. spawn->SetTSLevel(level);
  4199. }
  4200. else
  4201. lua_interface->LogError("%s: LUA SetTradeskillLevel command error: Spawn was not valid", lua_interface->GetScriptName(state));
  4202. return 0;
  4203. }
  4204. int EQ2Emu_lua_SetAttackable(lua_State* state) {
  4205. if (!lua_interface)
  4206. return 0;
  4207. Spawn* spawn = lua_interface->GetSpawn(state);
  4208. int8 attackable = lua_interface->GetInt8Value(state, 2);
  4209. if (spawn) {
  4210. spawn->SetAttackable(attackable);
  4211. spawn->vis_changed = true; //some clients store this in vis instead of info, need to make sense both are updated
  4212. }
  4213. return 0;
  4214. }
  4215. int EQ2Emu_lua_SummonPet(lua_State* state) {
  4216. // Check to see if we have a valid lua_interface
  4217. if (!lua_interface)
  4218. return 0;
  4219. // Get the spawn that is getting the pet
  4220. Spawn* spawn = lua_interface->GetSpawn(state);
  4221. // Get the DB ID of the pet
  4222. int32 pet_id = lua_interface->GetInt32Value(state, 2);
  4223. // The max level the pet can gain
  4224. int8 max_level = lua_interface->GetInt8Value(state, 3);
  4225. // Get the spell that this command was called from
  4226. LuaSpell* luaspell = lua_interface->GetCurrentSpell(state);
  4227. // Check to make sure the spawn pointer is valid
  4228. if (!spawn) {
  4229. lua_interface->LogError("%s: LUA SummonPet command error: Spawn is not valid", lua_interface->GetScriptName(state));
  4230. return 0;
  4231. }
  4232. // Check to make sure the spawn is an entity
  4233. if (!spawn->IsEntity()) {
  4234. lua_interface->LogError("%s: LUA SummonPet command error: Spawn is not an entity", lua_interface->GetScriptName(state));
  4235. return 0;
  4236. }
  4237. // Check to make sure the spawn doesn't already have a pet of this type
  4238. if (((Entity*)spawn)->GetPet()) {
  4239. if (spawn->IsPlayer()) {
  4240. Client* client = spawn->GetZone()->GetClientBySpawn(spawn);
  4241. if (client)
  4242. client->SimpleMessage(CHANNEL_COLOR_YELLOW, "You already have a pet.");
  4243. }
  4244. lua_interface->LogError("%s: LUA SummonPet command error: spawn already has a pet of this type", lua_interface->GetScriptName(state));
  4245. return 0;
  4246. }
  4247. // Check to see if the DB ID for the pet is set
  4248. if (pet_id == 0) {
  4249. lua_interface->LogError("%s: LUA SummonPet command error: pet_id can not be set to 0", lua_interface->GetScriptName(state));
  4250. return 0;
  4251. }
  4252. // Check to see if the pointer to the spell is valid
  4253. if (!luaspell) {
  4254. lua_interface->LogError("%s: LUA SummonPet command error: valid spell not found, SummonPet can only be used in spell scripts", lua_interface->GetScriptName(state));
  4255. return 0;
  4256. }
  4257. // Get a pointer to a spawn with the given DB ID and check if the pointer is valid
  4258. Spawn* pet = spawn->GetZone()->GetSpawn(pet_id);
  4259. if (!pet) {
  4260. lua_interface->LogError("%s: LUA SummonPet command error: Could not find spawn with id of %u.", lua_interface->GetScriptName(state), pet_id);
  4261. return 0;
  4262. }
  4263. // Check to make sure the pet is an npc
  4264. if (!pet->IsNPC()) {
  4265. lua_interface->LogError("%s: LUA SummonPet command error: id (%u) did not point to a npc", lua_interface->GetScriptName(state), pet_id);
  4266. return 0;
  4267. }
  4268. // Spawn the pet at the same location as the owner
  4269. pet->SetX(spawn->GetX());
  4270. pet->SetY(spawn->GetY());
  4271. pet->SetZ(spawn->GetZ());
  4272. pet->SetLocation(spawn->GetLocation());
  4273. pet->SetHeading(spawn->GetHeading());
  4274. spawn->GetZone()->AddSpawn(pet);
  4275. /*
  4276. const char* spawn_script = world.GetSpawnScript(pet_id);
  4277. if(spawn_script && lua_interface->GetSpawnScript(spawn_script) != 0){
  4278. spawn->SetSpawnScript(string(spawn_script));
  4279. zone->CallSpawnScript(spawn, SPAWN_SCRIPT_SPAWN);
  4280. }*/
  4281. // Get a random pet name
  4282. string random_pet_name;
  4283. int16 rand_index = MakeRandomInt(0, spawn->GetZone()->pet_names.size() - 1);
  4284. random_pet_name = spawn->GetZone()->pet_names.at(rand_index);
  4285. LogWrite(PET__DEBUG, 0, "Pets", "Randomize Pet Name: '%s' (rand: %i)", random_pet_name.c_str(), rand_index);
  4286. // If player set various values for the char sheet (pet window)
  4287. if (spawn->IsPlayer()) {
  4288. Player* player = (Player*)spawn;
  4289. player->GetInfoStruct()->pet_id = player->GetIDWithPlayerSpawn(pet);
  4290. strcpy(player->GetInfoStruct()->pet_name, random_pet_name.c_str());
  4291. player->GetInfoStruct()->pet_movement = 2;
  4292. player->GetInfoStruct()->pet_behavior = 3;
  4293. player->GetInfoStruct()->pet_health_pct = 1.0f;
  4294. player->GetInfoStruct()->pet_power_pct = 1.0f;
  4295. // Make sure the values get sent to the client
  4296. player->SetCharSheetChanged(true);
  4297. }
  4298. // Set the pets name
  4299. pet->SetName(random_pet_name.c_str());
  4300. // Set the level of the pet to the owners level or max level(if set) if owners level is greater
  4301. if (max_level > 0)
  4302. pet->SetLevel(spawn->GetLevel() >= max_level ? max_level : spawn->GetLevel());
  4303. else
  4304. pet->SetLevel(spawn->GetLevel());
  4305. // Set the max level this pet can reach
  4306. ((NPC*)pet)->SetMaxPetLevel(max_level);
  4307. // Set the faction of the pet to the same faction as the owner
  4308. pet->SetFactionID(spawn->GetFactionID());
  4309. // Set the spawn as a pet
  4310. pet->SetPet(true);
  4311. // Give a pointer of the owner to the pet
  4312. ((NPC*)pet)->SetOwner((Entity*)spawn);
  4313. // Give a pointer of the pet to the owner
  4314. ((Entity*)spawn)->SetCombatPet((Entity*)pet);
  4315. // Set the pet type
  4316. ((NPC*)pet)->SetPetType(PET_TYPE_COMBAT);
  4317. // Set the spell id used to create this pet
  4318. ((NPC*)pet)->SetPetSpellID(luaspell->spell->GetSpellData()->id);
  4319. // Set the spell tier used to create this pet
  4320. ((NPC*)pet)->SetPetSpellTier(luaspell->spell->GetSpellData()->tier);
  4321. // Set the pets spawn type to 6
  4322. pet->SetSpawnType(6);
  4323. // Set the pets brain
  4324. ((NPC*)pet)->SetBrain(new CombatPetBrain((NPC*)pet));
  4325. // Check to see if the pet has a subtitle
  4326. if (strlen(pet->GetSubTitle()) > 0) {
  4327. // Add the players name to the front of the sub title
  4328. string pet_subtitle;
  4329. pet_subtitle.append(spawn->GetName()).append("'s ").append(pet->GetSubTitle());
  4330. LogWrite(PET__DEBUG, 0, "Pets", "Pet Subtitle: '%s'", pet_subtitle.c_str());
  4331. // Set the pets subtitle to the new one
  4332. pet->SetSubTitle(pet_subtitle.c_str());
  4333. }
  4334. // Add the "Pet Options" entity command to the pet
  4335. pet->AddSecondaryEntityCommand("Pet Options", 10.0f, "petoptions", "", 0, 0);
  4336. // Set the pet as the return value for this function
  4337. lua_interface->SetSpawnValue(state, pet);
  4338. return 1;
  4339. }
  4340. int EQ2Emu_lua_SummonDeityPet(lua_State* state) {
  4341. if (!lua_interface)
  4342. return 0;
  4343. Spawn* spawn = lua_interface->GetSpawn(state);
  4344. int32 pet_id = lua_interface->GetInt32Value(state, 2);
  4345. LuaSpell* luaspell = lua_interface->GetCurrentSpell(state);
  4346. if (!spawn) {
  4347. lua_interface->LogError("%s: LUA SummonDeityPet command error: Spawn is not valid", lua_interface->GetScriptName(state));
  4348. return 0;
  4349. }
  4350. if (!spawn->IsEntity()) {
  4351. lua_interface->LogError("%s: LUA SummonDeityPet command error: Spawn is not an entity", lua_interface->GetScriptName(state));
  4352. return 0;
  4353. }
  4354. if (((Entity*)spawn)->GetDeityPet()) {
  4355. if (spawn->IsPlayer()) {
  4356. Client* client = spawn->GetZone()->GetClientBySpawn(spawn);
  4357. if (client)
  4358. client->SimpleMessage(CHANNEL_COLOR_YELLOW, "You already have a deity pet.");
  4359. }
  4360. lua_interface->LogError("%s: LUA SummonDeityPet command error: spawn already has a pet of this type", lua_interface->GetScriptName(state));
  4361. return 0;
  4362. }
  4363. if (pet_id == 0) {
  4364. lua_interface->LogError("%s: LUA SummonDeityPet command error: pet_id can not be set to 0", lua_interface->GetScriptName(state));
  4365. return 0;
  4366. }
  4367. if (!luaspell) {
  4368. lua_interface->LogError("%s: LUA SummonDeityPet command error: valid spell not found, SummonDeityPet can only be used in spell scripts", lua_interface->GetScriptName(state));
  4369. return 0;
  4370. }
  4371. Spawn* pet = spawn->GetZone()->GetSpawn(pet_id);
  4372. if (!pet) {
  4373. lua_interface->LogError("%s: LUA SummonDeityPet command error: Could not find spawn with id of %u.", lua_interface->GetScriptName(state), pet_id);
  4374. return 0;
  4375. }
  4376. if (!pet->IsNPC()) {
  4377. lua_interface->LogError("%s: LUA SummonDeityPet command error: spawn with id of %u is not a npc", lua_interface->GetScriptName(state), pet_id);
  4378. return 0;
  4379. }
  4380. pet->SetX(spawn->GetX());
  4381. pet->SetY(spawn->GetY());
  4382. pet->SetZ(spawn->GetZ());
  4383. pet->SetLocation(spawn->GetLocation());
  4384. pet->SetHeading(spawn->GetHeading());
  4385. spawn->GetZone()->AddSpawn(pet);
  4386. string random_pet_name;
  4387. int16 rand_index = MakeRandomInt(0, spawn->GetZone()->pet_names.size() - 1);
  4388. random_pet_name = spawn->GetZone()->pet_names.at(rand_index);
  4389. LogWrite(PET__DEBUG, 0, "Pets", "Randomize Pet Name: '%s' (rand: %i)", random_pet_name.c_str(), rand_index);
  4390. pet->SetName(random_pet_name.c_str());
  4391. pet->SetLevel(spawn->GetLevel());
  4392. pet->SetFactionID(spawn->GetFactionID());
  4393. pet->SetPet(true);
  4394. ((NPC*)pet)->SetPetType(PET_TYPE_DEITY);
  4395. ((NPC*)pet)->SetOwner((Entity*)spawn);
  4396. ((Entity*)spawn)->SetDeityPet((Entity*)pet);
  4397. pet->SetSpawnType(6);
  4398. ((NPC*)pet)->SetBrain(new NonCombatPetBrain((NPC*)pet));
  4399. ((NPC*)pet)->SetPetSpellID(luaspell->spell->GetSpellData()->id);
  4400. ((NPC*)pet)->SetPetSpellTier(luaspell->spell->GetSpellData()->tier);
  4401. if (strlen(pet->GetSubTitle()) > 0) {
  4402. string pet_subtitle;
  4403. pet_subtitle.append(spawn->GetName()).append("'s ").append(pet->GetSubTitle());
  4404. LogWrite(PET__DEBUG, 0, "Pets", "Pet Subtitle: '%s'", pet_subtitle.c_str());
  4405. pet->SetSubTitle(pet_subtitle.c_str());
  4406. }
  4407. // deity and cosmetic pets are not attackable
  4408. pet->SetAttackable(false);
  4409. pet->AddSecondaryEntityCommand("Pet Options", 10.0f, "petoptions", "", 0, 0);
  4410. lua_interface->SetSpawnValue(state, pet);
  4411. return 1;
  4412. }
  4413. int EQ2Emu_lua_SummonCosmeticPet(lua_State* state) {
  4414. if (!lua_interface)
  4415. return 0;
  4416. Spawn* spawn = lua_interface->GetSpawn(state);
  4417. int32 pet_id = lua_interface->GetInt32Value(state, 2);
  4418. LuaSpell* luaspell = lua_interface->GetCurrentSpell(state);
  4419. if (!spawn) {
  4420. lua_interface->LogError("%s: LUA SummonCosmeticPet command error: Spawn is not valid", lua_interface->GetScriptName(state));
  4421. return 0;
  4422. }
  4423. if (!spawn->IsEntity()) {
  4424. lua_interface->LogError("%s: LUA SummonCosmeticPet command error: Spawn is not an entity", lua_interface->GetScriptName(state));
  4425. return 0;
  4426. }
  4427. if (((Entity*)spawn)->GetCosmeticPet()) {
  4428. if (spawn->IsPlayer()) {
  4429. Client* client = spawn->GetZone()->GetClientBySpawn(spawn);
  4430. if (client)
  4431. client->SimpleMessage(CHANNEL_COLOR_YELLOW, "You already have a cosmetic pet.");
  4432. }
  4433. lua_interface->LogError("%s: LUA SummonCosmeticPet command error: spawn already has a pet of this type", lua_interface->GetScriptName(state));
  4434. return 0;
  4435. }
  4436. if (pet_id == 0) {
  4437. lua_interface->LogError("%s: LUA SummonCosmeticPet command error: pet_id can not be set to 0", lua_interface->GetScriptName(state));
  4438. return 0;
  4439. }
  4440. if (!luaspell) {
  4441. lua_interface->LogError("%s: LUA SummonCosmeticPet command error: valid spell not found, SummonCosmeticPet can only be used in spell scripts", lua_interface->GetScriptName(state));
  4442. return 0;
  4443. }
  4444. Spawn* pet = spawn->GetZone()->GetSpawn(pet_id);
  4445. if (!pet) {
  4446. lua_interface->LogError("%s: LUA SummonCosmeticPet command error: Could not find spawn with id of %u.", lua_interface->GetScriptName(state), pet_id);
  4447. return 0;
  4448. }
  4449. if (!pet->IsNPC()) {
  4450. lua_interface->LogError("%s: LUA SummonCosmeticPet command error: spawn with id of %u is not a npc", lua_interface->GetScriptName(state), pet_id);
  4451. return 0;
  4452. }
  4453. pet->SetX(spawn->GetX());
  4454. pet->SetY(spawn->GetY());
  4455. pet->SetZ(spawn->GetZ());
  4456. pet->SetLocation(spawn->GetLocation());
  4457. pet->SetHeading(spawn->GetHeading());
  4458. spawn->GetZone()->AddSpawn(pet);
  4459. string random_pet_name;
  4460. int16 rand_index = MakeRandomInt(0, spawn->GetZone()->pet_names.size() - 1);
  4461. random_pet_name = spawn->GetZone()->pet_names.at(rand_index);
  4462. LogWrite(PET__DEBUG, 0, "Pets", "Randomize Pet Name: '%s' (rand: %i)", random_pet_name.c_str(), rand_index);
  4463. pet->SetName(random_pet_name.c_str());
  4464. pet->SetLevel(spawn->GetLevel());
  4465. pet->SetFactionID(spawn->GetFactionID());
  4466. pet->SetPet(true);
  4467. ((NPC*)pet)->SetPetType(PET_TYPE_COSMETIC);
  4468. ((NPC*)pet)->SetOwner((Entity*)spawn);
  4469. ((Entity*)spawn)->SetCosmeticPet((Entity*)pet);
  4470. pet->SetSpawnType(6);
  4471. ((NPC*)pet)->SetBrain(new NonCombatPetBrain((NPC*)pet));
  4472. ((NPC*)pet)->SetPetSpellID(luaspell->spell->GetSpellData()->id);
  4473. ((NPC*)pet)->SetPetSpellTier(luaspell->spell->GetSpellData()->tier);
  4474. if (strlen(pet->GetSubTitle()) > 0) {
  4475. string pet_subtitle;
  4476. pet_subtitle.append(spawn->GetName()).append("'s ").append(pet->GetSubTitle());
  4477. LogWrite(PET__DEBUG, 0, "Pets", "Pet Subtitle: '%s'", pet_subtitle.c_str());
  4478. pet->SetSubTitle(pet_subtitle.c_str());
  4479. }
  4480. pet->SetAttackable(false);
  4481. pet->AddSecondaryEntityCommand("Pet Options", 10.0f, "petoptions", "", 0, 0);
  4482. lua_interface->SetSpawnValue(state, pet);
  4483. return 1;
  4484. }
  4485. int EQ2Emu_lua_DismissPet(lua_State* state) {
  4486. if (!lua_interface)
  4487. return 0;
  4488. Spawn* spawn = lua_interface->GetSpawn(state);
  4489. if (!spawn) {
  4490. lua_interface->LogError("%s: LUA DismissPet command error: spawn is not valid", lua_interface->GetScriptName(state));
  4491. return 0;
  4492. }
  4493. if (!spawn->IsPet()) {
  4494. lua_interface->LogError("%s: LUA DismissPet command error: spawn is not a pet", lua_interface->GetScriptName(state));
  4495. return 0;
  4496. }
  4497. if (!((NPC*)spawn)->IsDismissing())
  4498. ((NPC*)spawn)->GetOwner()->DismissPet((NPC*)spawn);
  4499. return 0;
  4500. }
  4501. int EQ2Emu_lua_SetQuestFeatherColor(lua_State* state) {
  4502. if (!lua_interface)
  4503. return 0;
  4504. Quest* quest = lua_interface->GetQuest(state);
  4505. if (!quest) {
  4506. 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));
  4507. return 0;
  4508. }
  4509. int8 feather_color = lua_interface->GetInt8Value(state, 2);
  4510. if (feather_color > 0)
  4511. quest->SetFeatherColor(feather_color);
  4512. return 0;
  4513. }
  4514. int EQ2Emu_lua_RemoveSpawnAccess(lua_State* state) {
  4515. if (!lua_interface)
  4516. return 0;
  4517. Spawn* spawn = lua_interface->GetSpawn(state);
  4518. Spawn* spawn2 = lua_interface->GetSpawn(state, 2);
  4519. if (!spawn) {
  4520. lua_interface->LogError("%s: LUA RemoveSpawnAccess command error: first spawn is not valid", lua_interface->GetScriptName(state));
  4521. return 0;
  4522. }
  4523. if (!spawn2) {
  4524. lua_interface->LogError("%s: LUA RemoveSpawnAccess command error: second spawn is not valid", lua_interface->GetScriptName(state));
  4525. return 0;
  4526. }
  4527. spawn->RemoveSpawnAccess(spawn2);
  4528. return 0;
  4529. }
  4530. int EQ2Emu_lua_SpawnByLocationID(lua_State* state) {
  4531. if (!lua_interface)
  4532. return 0;
  4533. ZoneServer* zone = lua_interface->GetZone(state);
  4534. int32 location_id = lua_interface->GetInt32Value(state, 2);
  4535. if (!zone) {
  4536. lua_interface->LogError("%s: LUA SpawnByLocationID command error: zone is not valid", lua_interface->GetScriptName(state));
  4537. return 0;
  4538. }
  4539. if (location_id == 0) {
  4540. lua_interface->LogError("%s: LUA SpawnByLocationID command error: location id can not be 0", lua_interface->GetScriptName(state));
  4541. return 0;
  4542. }
  4543. SpawnLocation* location = zone->GetSpawnLocation(location_id);
  4544. if (!location) {
  4545. lua_interface->LogError("%s: LUA SpawnByLocationID command error: no location found for the given ID (%u)", lua_interface->GetScriptName(state), location_id);
  4546. return 0;
  4547. }
  4548. Spawn* spawn = 0;
  4549. if (location->entities[0]) {
  4550. if (location->entities[0]->spawn_type == SPAWN_ENTRY_TYPE_NPC)
  4551. spawn = zone->AddNPCSpawn(location, location->entities[0]);
  4552. else if (location->entities[0]->spawn_type == SPAWN_ENTRY_TYPE_GROUNDSPAWN)
  4553. spawn = zone->AddGroundSpawn(location, location->entities[0]);
  4554. else if (location->entities[0]->spawn_type == SPAWN_ENTRY_TYPE_OBJECT)
  4555. spawn = zone->AddObjectSpawn(location, location->entities[0]);
  4556. else if (location->entities[0]->spawn_type == SPAWN_ENTRY_TYPE_WIDGET)
  4557. spawn = zone->AddWidgetSpawn(location, location->entities[0]);
  4558. else if (location->entities[0]->spawn_type == SPAWN_ENTRY_TYPE_SIGN)
  4559. spawn = zone->AddSignSpawn(location, location->entities[0]);
  4560. if (spawn) {
  4561. const char* script = 0;
  4562. for (int x = 0; x < 3; x++) {
  4563. switch (x) {
  4564. case 0:
  4565. script = world.GetSpawnEntryScript(location->entities[0]->spawn_entry_id);
  4566. break;
  4567. case 1:
  4568. script = world.GetSpawnLocationScript(location->entities[0]->spawn_location_id);
  4569. break;
  4570. case 2:
  4571. script = world.GetSpawnScript(location->entities[0]->spawn_id);
  4572. break;
  4573. }
  4574. if (script && lua_interface->GetSpawnScript(script) != 0) {
  4575. spawn->SetSpawnScript(string(script));
  4576. break;
  4577. }
  4578. }
  4579. zone->CallSpawnScript(spawn, SPAWN_SCRIPT_SPAWN);
  4580. lua_interface->SetSpawnValue(state, spawn);
  4581. return 1;
  4582. }
  4583. else {
  4584. LogWrite(ZONE__ERROR, 0, "Zone", "Error adding spawn to zone");
  4585. safe_delete(spawn);
  4586. }
  4587. }
  4588. return 0;
  4589. }
  4590. int EQ2Emu_lua_CastEntityCommand(lua_State* state) {
  4591. if (!lua_interface)
  4592. return 0;
  4593. Spawn* caster = lua_interface->GetSpawn(state);
  4594. Spawn* target = lua_interface->GetSpawn(state, 2);
  4595. int32 id = lua_interface->GetInt32Value(state, 3);
  4596. string command = lua_interface->GetStringValue(state, 4);
  4597. if (!caster) {
  4598. lua_interface->LogError("%s: LUA CastEntityCommand command error: caster is not valid", lua_interface->GetScriptName(state));
  4599. return 0;
  4600. }
  4601. if (!target) {
  4602. lua_interface->LogError("%s: LUA CastEntityCommand command error: target is not valid", lua_interface->GetScriptName(state));
  4603. return 0;
  4604. }
  4605. if (!caster->IsPlayer()) {
  4606. lua_interface->LogError("%s: LUA CastEntityCommand command error: caster is not a player", lua_interface->GetScriptName(state));
  4607. return 0;
  4608. }
  4609. EntityCommand* entity_command = caster->GetZone()->GetEntityCommand(id, command);
  4610. if (!entity_command) {
  4611. 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());
  4612. return 0;
  4613. }
  4614. Client* client = caster->GetZone()->GetClientBySpawn(caster);
  4615. if (!client) {
  4616. lua_interface->LogError("%s: LUA CastEntityCommand command error: unable to get a valid client for the given caster", lua_interface->GetScriptName(state));
  4617. return 0;
  4618. }
  4619. client->GetCurrentZone()->ProcessEntityCommand(entity_command, (Player*)caster, target);
  4620. return 0;
  4621. }
  4622. int EQ2Emu_lua_SetLuaBrain(lua_State* state) {
  4623. if (!lua_interface)
  4624. return 0;
  4625. Spawn* spawn = lua_interface->GetSpawn(state);
  4626. if (!spawn) {
  4627. lua_interface->LogError("%s: LUA SetLuaBrain command error: spawn is not valid", lua_interface->GetScriptName(state));
  4628. return 0;
  4629. }
  4630. if (!spawn->IsNPC()) {
  4631. lua_interface->LogError("%s: LUA SetLuaBrain command error: spawn is not a npc", lua_interface->GetScriptName(state));
  4632. return 0;
  4633. }
  4634. ((NPC*)spawn)->SetBrain(new LuaBrain((NPC*)spawn));
  4635. return 0;
  4636. }
  4637. int EQ2Emu_lua_SetBrainTick(lua_State* state) {
  4638. if (!lua_interface)
  4639. return 0;
  4640. Spawn* spawn = lua_interface->GetSpawn(state);
  4641. int16 tick = lua_interface->GetInt16Value(state, 2);
  4642. if (!spawn) {
  4643. lua_interface->LogError("%s: LUA SetBrainTick command error: spawn is not valid", lua_interface->GetScriptName(state));
  4644. return 0;
  4645. }
  4646. if (!spawn->IsNPC()) {
  4647. lua_interface->LogError("%s: LUA SetBrainTick command error: spawn is not a valid npc", lua_interface->GetScriptName(state));
  4648. return 0;
  4649. }
  4650. if (tick < 20) {
  4651. lua_interface->LogError("%s: LUA SetBrainTick command error: tick can not be set below 20 milliseconds", lua_interface->GetScriptName(state));
  4652. return 0;
  4653. }
  4654. ((NPC*)spawn)->Brain()->SetTick(tick);
  4655. return 0;
  4656. }
  4657. int EQ2Emu_lua_SetFollowTarget(lua_State* state) {
  4658. if (!lua_interface)
  4659. return 0;
  4660. Spawn* spawn = lua_interface->GetSpawn(state);
  4661. Spawn* target = lua_interface->GetSpawn(state, 2);
  4662. int32 follow_distance = lua_interface->GetInt32Value(state, 3);
  4663. if (!spawn) {
  4664. lua_interface->LogError("%s: LUA SetFollowTarget command error: spawn is not valid", lua_interface->GetScriptName(state));
  4665. return 0;
  4666. }
  4667. // Target can be null, setting follow target to 0 clears it and will cancel follow, so no need to check it
  4668. spawn->SetFollowTarget(target, follow_distance);
  4669. return 0;
  4670. }
  4671. int EQ2Emu_lua_GetFollowTarget(lua_State* state) {
  4672. if (!lua_interface)
  4673. return 0;
  4674. Spawn* spawn = lua_interface->GetSpawn(state);
  4675. if (!spawn) {
  4676. lua_interface->LogError("%s: LUA GetFollowTarget command error: spawn is not valid", lua_interface->GetScriptName(state));
  4677. return 0;
  4678. }
  4679. Spawn* target = spawn->GetFollowTarget();
  4680. if (target) {
  4681. lua_interface->SetSpawnValue(state, target);
  4682. return 1;
  4683. }
  4684. return 0;
  4685. }
  4686. int EQ2Emu_lua_ToggleFollow(lua_State* state) {
  4687. if (!lua_interface)
  4688. return 0;
  4689. Spawn* spawn = lua_interface->GetSpawn(state);
  4690. if (!spawn) {
  4691. lua_interface->LogError("%s: LUA ToggleFollow command error: spawn is not valid", lua_interface->GetScriptName(state));
  4692. return 0;
  4693. }
  4694. if (spawn->following)
  4695. spawn->following = false;
  4696. else
  4697. spawn->following = true;
  4698. return 0;
  4699. }
  4700. int EQ2Emu_lua_IsFollowing(lua_State* state) {
  4701. if (!lua_interface)
  4702. return 0;
  4703. Spawn* spawn = lua_interface->GetSpawn(state);
  4704. if (!spawn) {
  4705. lua_interface->LogError("%s: LUA IsFollowing command error: spawn is not valid", lua_interface->GetScriptName(state));
  4706. return 0;
  4707. }
  4708. lua_interface->SetBooleanValue(state, spawn->following);
  4709. return 1;
  4710. }
  4711. int EQ2Emu_lua_SetTempVariable(lua_State* state) {
  4712. // As this is unique among the rest of our lua functions as the 3rd param can be of multiple types
  4713. // I will attempt to explain how this function works for future refrence
  4714. // Fist lets make sure lua_interface is valid, if not return out
  4715. if (!lua_interface)
  4716. return 0;
  4717. // Next we grab the first 2 params same as we usually would
  4718. Spawn* spawn = lua_interface->GetSpawn(state);
  4719. string var = lua_interface->GetStringValue(state, 2);
  4720. // 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
  4721. // 1 = Spawn
  4722. // 2 = Zone
  4723. // 3 = Item
  4724. // 4 = Quest
  4725. // 5 = String
  4726. // 6 = nil (null)
  4727. int8 dataType = 0;
  4728. // Define pointers for each potential type
  4729. Spawn* spawnVal = 0;
  4730. ZoneServer* zone = 0;
  4731. Item* item = 0;
  4732. Quest* quest = 0;
  4733. string val;
  4734. // Finally we get to grabbing the third param, we will first check to see if it is light user data
  4735. // which is custom data types, in this case it can be Spawn, Zone, Item, or Quest. Conversation and
  4736. // options window are also light user data be we do not handle those.
  4737. // We check with lua_islightuserdata(lua_State*, index)
  4738. if (lua_islightuserdata(state, 3)) {
  4739. // It is light user data so we will grab the param with lua_touserdata(lua_State*, index)
  4740. // and convert it to LUAUserData*
  4741. LUAUserData* data = (LUAUserData*)lua_touserdata(state, 3);
  4742. // Check to make sure the data we got is valid, if not give an error
  4743. if (!data || !data->IsCorrectlyInitialized()) {
  4744. lua_interface->LogError("%s: LUA SetTempVariable command error while processing %s", lua_interface->GetScriptName(state), lua_tostring(state, -1));
  4745. }
  4746. // Check if data is a Spawn, if so set our Spawn pointer and the dataType variable
  4747. else if (data->IsSpawn()) {
  4748. spawnVal = data->spawn;
  4749. dataType = 1;
  4750. }
  4751. // Check if data is a Zone, if so set our Zone pointer and the dataType variable
  4752. else if (data->IsZone()) {
  4753. zone = data->zone;
  4754. dataType = 2;
  4755. }
  4756. // Check if data is a Item, if so set our Item pointer and the dataType variable
  4757. else if (data->IsItem()) {
  4758. item = data->item;
  4759. dataType = 3;
  4760. }
  4761. // Check if data is a Ques, if so set our Quest pointer and the dataType variable
  4762. else if (data->IsQuest()) {
  4763. quest = data->quest;
  4764. dataType = 4;
  4765. }
  4766. }
  4767. // Wasn't light user data, check if it is nil(null)
  4768. else if (lua_isnil(state, 3)) {
  4769. // It is nil (null) set the dataType variable, no need to set a pointer in this case
  4770. dataType = 6;
  4771. }
  4772. // Wasn't light user data or nil (null), must be a string
  4773. else {
  4774. // Set the string and dataType variable
  4775. val = lua_interface->GetStringValue(state, 3);
  4776. dataType = 5;
  4777. }
  4778. // We now have all the params, lets check to make sure they are valid
  4779. if (!spawn) {
  4780. lua_interface->LogError("%s: LUA SetTempVariable command error: spawn is not valid", lua_interface->GetScriptName(state));
  4781. return 0;
  4782. }
  4783. if (var.length() == 0) {
  4784. lua_interface->LogError("%s: LUA SetTempVariable command error: var must be set", lua_interface->GetScriptName(state));
  4785. return 0;
  4786. }
  4787. if (dataType == 0) {
  4788. lua_interface->LogError("%s: LUA SetTempVariable command error: unknown data type", lua_interface->GetScriptName(state));
  4789. return 0;
  4790. }
  4791. // All params are valid, lets set the spawns temp variable, this is where dataType variable comes in.
  4792. // AddTempVariable has overloads for all the types of data we support, we need to make sure the third
  4793. // param gets sent to the correct list so we check the value of dataType to know where it should go.
  4794. switch (dataType) {
  4795. case 1:
  4796. // 1 = Spawn
  4797. spawn->AddTempVariable(var, spawnVal);
  4798. break;
  4799. case 2:
  4800. // 2 = Zone
  4801. spawn->AddTempVariable(var, zone);
  4802. break;
  4803. case 3:
  4804. // 3 = Item
  4805. spawn->AddTempVariable(var, item);
  4806. break;
  4807. case 4:
  4808. // 4 = Quest
  4809. spawn->AddTempVariable(var, quest);
  4810. break;
  4811. case 5:
  4812. // 5 = String
  4813. spawn->AddTempVariable(var, val);
  4814. break;
  4815. case 6:
  4816. // 6 = nil (null) so the variable is no longer set, lets remove it from the spawn
  4817. spawn->DeleteTempVariable(var);
  4818. break;
  4819. }
  4820. // And we are done so return out
  4821. return 0;
  4822. }
  4823. int EQ2Emu_lua_GetTempVariable(lua_State* state) {
  4824. if (!lua_interface)
  4825. return 0;
  4826. Spawn* spawn = lua_interface->GetSpawn(state);
  4827. string var = lua_interface->GetStringValue(state, 2);
  4828. if (!spawn) {
  4829. lua_interface->LogError("%s: LUA GetTempVariable command error: spawn is not valid", lua_interface->GetScriptName(state));
  4830. return 0;
  4831. }
  4832. if (var.length() == 0) {
  4833. lua_interface->LogError("%s: LUA GetTempVariable command error: var must be set", lua_interface->GetScriptName(state));
  4834. return 0;
  4835. }
  4836. // This will tell us the type of data this variable contains, uses the same values as the previous function
  4837. int8 type = spawn->GetTempVariableType(var);
  4838. Spawn* spawn2 = 0;
  4839. ZoneServer* zone = 0;
  4840. Item* item = 0;
  4841. Quest* quest = 0;
  4842. // Set the lua function return value based on the type of data the variable contains
  4843. switch (type) {
  4844. case 1:
  4845. spawn2 = spawn->GetTempVariableSpawn(var);
  4846. if (!spawn2)
  4847. return 0;
  4848. lua_interface->SetSpawnValue(state, spawn2);
  4849. break;
  4850. case 2:
  4851. zone = spawn->GetTempVariableZone(var);
  4852. if (!zone)
  4853. return 0;
  4854. lua_interface->SetZoneValue(state, zone);
  4855. break;
  4856. case 3:
  4857. item = spawn->GetTempVariableItem(var);
  4858. if (!item)
  4859. return 0;
  4860. lua_interface->SetItemValue(state, item);
  4861. break;
  4862. case 4:
  4863. quest = spawn->GetTempVariableQuest(var);
  4864. if (!quest)
  4865. return 0;
  4866. lua_interface->SetQuestValue(state, quest);
  4867. break;
  4868. case 5:
  4869. lua_interface->SetStringValue(state, spawn->GetTempVariable(var).c_str());
  4870. break;
  4871. default:
  4872. // Not a valid type then the variable was not set so return out
  4873. return 0;
  4874. }
  4875. // Return value was set so return out
  4876. return 1;
  4877. }
  4878. int EQ2Emu_lua_GiveQuestItem(lua_State* state)
  4879. {
  4880. if (!lua_interface)
  4881. return 0;
  4882. Quest* quest = lua_interface->GetQuest(state);
  4883. Spawn* spawn = lua_interface->GetSpawn(state, 2);
  4884. string description = lua_interface->GetStringValue(state, 3);
  4885. int32 item_id = lua_interface->GetInt32Value(state, 4);
  4886. if (!quest) {
  4887. lua_interface->LogError("%s: LUA GiveQuestItem command error: quest is not valid", lua_interface->GetScriptName(state));
  4888. lua_interface->SetBooleanValue(state, false);
  4889. return 1;
  4890. }
  4891. if (!spawn) {
  4892. lua_interface->LogError("%s: LUA GiveQuestItem command error: spawn is not valid", lua_interface->GetScriptName(state));
  4893. lua_interface->SetBooleanValue(state, false);
  4894. return 1;
  4895. }
  4896. if (!spawn->IsPlayer()) {
  4897. lua_interface->LogError("%s: LUA GiveQuestItem command error: spawn must be a player", lua_interface->GetScriptName(state));
  4898. lua_interface->SetBooleanValue(state, false);
  4899. return 1;
  4900. }
  4901. if (item_id == 0) {
  4902. lua_interface->LogError("%s: LUA GiveQuestItem command error: item_id is not valid", lua_interface->GetScriptName(state));
  4903. lua_interface->SetBooleanValue(state, false);
  4904. return 1;
  4905. }
  4906. Client* client = spawn->GetZone()->GetClientBySpawn(spawn);
  4907. if (!client) {
  4908. lua_interface->LogError("%s: LUA GiveQuestItem command error: unable to get a valid client from the given spawn", lua_interface->GetScriptName(state));
  4909. lua_interface->SetBooleanValue(state, false);
  4910. return 1;
  4911. }
  4912. Item* item = master_item_list.GetItem(item_id);
  4913. if (!item) {
  4914. lua_interface->LogError("%s: LUA GiveQuestItem command error: unable to get an item from the given id (%u)", lua_interface->GetScriptName(state), item_id);
  4915. lua_interface->SetBooleanValue(state, false);
  4916. return 1;
  4917. }
  4918. PacketStruct* packet = configReader.getStruct("WS_QuestComplete", client->GetVersion());
  4919. if (packet) {
  4920. packet->setDataByName("title", "Quest Reward!");
  4921. packet->setDataByName("name", quest->GetName());
  4922. packet->setDataByName("description", description.c_str());
  4923. packet->setDataByName("level", quest->GetLevel());
  4924. packet->setArrayLengthByName("num_rewards", 1);
  4925. packet->setArrayDataByName("reward_id", item->details.item_id);
  4926. if (client->GetVersion() < 860)
  4927. packet->setItemArrayDataByName("item", item, (Player*)spawn, 0, 0, -1);
  4928. else if (client->GetVersion() < 1193)
  4929. packet->setItemArrayDataByName("item", item, (Player*)spawn);
  4930. else
  4931. packet->setItemArrayDataByName("item", item, (Player*)spawn, 0, 0, 2);
  4932. client->QueuePacket(packet->serialize());
  4933. safe_delete(packet);
  4934. lua_interface->SetBooleanValue(state, client->AddItem(item_id, 1));
  4935. client->Message(CHANNEL_COLOR_YELLOW, "You receive %s.", item->CreateItemLink(client->GetVersion()).c_str());
  4936. return 1;
  4937. }
  4938. lua_interface->SetBooleanValue(state, false);
  4939. return 1;
  4940. }
  4941. int EQ2Emu_lua_SetQuestRepeatable(lua_State* state) {
  4942. if (!lua_interface)
  4943. return 0;
  4944. Quest* quest = lua_interface->GetQuest(state);
  4945. if (!quest) {
  4946. lua_interface->LogError("%s: LUA SetQuestRepeatable command error: quest is not valid", lua_interface->GetScriptName(state));
  4947. return 0;
  4948. }
  4949. quest->SetRepeatable(true);
  4950. return 0;
  4951. }
  4952. int EQ2Emu_lua_GetArchetypeName(lua_State* state) {
  4953. if (!lua_interface)
  4954. return 0;
  4955. Spawn* spawn = lua_interface->GetSpawn(state);
  4956. if (!spawn) {
  4957. lua_interface->LogError("%s: LUA GetArchetypeName command error: spawn is not valid", lua_interface->GetScriptName(state));
  4958. return 0;
  4959. }
  4960. int8 base_class = classes.GetBaseClass(spawn->GetAdventureClass());
  4961. string ret = classes.GetClassNameCase(base_class);
  4962. if (ret.length() > 0) {
  4963. lua_interface->SetStringValue(state, ret.c_str());
  4964. return 1;
  4965. }
  4966. return 0;
  4967. }
  4968. int EQ2Emu_lua_AddWard(lua_State* state) {
  4969. if (!lua_interface)
  4970. return 0;
  4971. int32 damage = lua_interface->GetInt32Value(state);
  4972. bool keepWard = (lua_interface->GetInt8Value(state, 2) == 1);
  4973. int8 wardType = lua_interface->GetInt8Value(state, 3);
  4974. int8 damageTypes = lua_interface->GetInt8Value(state, 4);
  4975. int32 damageAbsorptionPercent = lua_interface->GetInt32Value(state, 5);
  4976. int32 damageAbsorptionMaxHealthPercent = lua_interface->GetInt32Value(state, 6);
  4977. int32 redirectDamagePercent = lua_interface->GetInt32Value(state, 7);
  4978. int32 maxHitCount = lua_interface->GetInt32Value(state, 8);
  4979. LuaSpell* spell = lua_interface->GetCurrentSpell(state);
  4980. bool ward_was_added = false;
  4981. ZoneServer* zone = spell->caster->GetZone();
  4982. spell->MSpellTargets.readlock(__FUNCTION__, __LINE__);
  4983. for (int32 i = 0; i < spell->targets.size(); i++) {
  4984. Spawn* target = zone->GetSpawnByID(spell->targets.at(i));
  4985. if (!target)
  4986. continue;
  4987. if (target->IsEntity()) {
  4988. // If the ward is already active remove it
  4989. if (((Entity*)target)->GetWard(spell->spell->GetSpellID()))
  4990. ((Entity*)target)->RemoveWard(spell->spell->GetSpellID());
  4991. // Create new ward info
  4992. WardInfo* ward = new WardInfo;
  4993. ward->Spell = spell;
  4994. ward->BaseDamage = damage;
  4995. ward->DamageLeft = damage;
  4996. ward->AbsorbAllDamage = (damage == 0) ? true : false;
  4997. ward->keepWard = keepWard;
  4998. ward->WardType = wardType;
  4999. if (damageAbsorptionPercent > 100)
  5000. damageAbsorptionPercent = 100;
  5001. ward->DamageAbsorptionPercentage = damageAbsorptionPercent;
  5002. if (damageAbsorptionMaxHealthPercent > 100)
  5003. damageAbsorptionMaxHealthPercent = 100;
  5004. ward->DamageAbsorptionMaxHealthPercent = damageAbsorptionMaxHealthPercent;
  5005. ward->RedirectDamagePercent = redirectDamagePercent;
  5006. ward->LastRedirectDamage = 0;
  5007. ward->LastAbsorbedDamage = 0;
  5008. ward->HitCount = 0;
  5009. spell->num_triggers = maxHitCount;
  5010. spell->had_triggers = true;
  5011. spell->cancel_after_all_triggers = false;
  5012. ward->MaxHitCount = maxHitCount;
  5013. if (wardType == WARD_TYPE_MAGICAL)
  5014. ward->DamageType = damageTypes;
  5015. // Add the ward to the entity
  5016. ((Entity*)target)->AddWard(spell->spell->GetSpellID(), ward);
  5017. ward_was_added = true;
  5018. }
  5019. }
  5020. spell->MSpellTargets.releasereadlock(__FUNCTION__, __LINE__);
  5021. if (ward_was_added && spell->caster->IsPlayer()) {
  5022. spell->had_dmg_remaining = true;
  5023. ClientPacketFunctions::SendMaintainedExamineUpdate(spell->caster->GetZone()->GetClientBySpawn(spell->caster), spell->slot_pos, damage, 1);
  5024. }
  5025. return 0;
  5026. }
  5027. int EQ2Emu_lua_AddToWard(lua_State* state) {
  5028. if (!lua_interface)
  5029. return 0;
  5030. int32 amount = lua_interface->GetInt32Value(state);
  5031. LuaSpell* spell = lua_interface->GetCurrentSpell(state);
  5032. WardInfo* ward = 0;
  5033. ZoneServer* zone = spell->caster->GetZone();
  5034. spell->MSpellTargets.readlock(__FUNCTION__, __LINE__);
  5035. if (zone->GetSpawnByID(spell->targets.at(0))->IsEntity()) {
  5036. Entity* target = (Entity*)zone->GetSpawnByID(spell->targets.at(0));
  5037. ward = target->GetWard(spell->spell->GetSpellID());
  5038. if (ward) {
  5039. ward->DamageLeft += amount;
  5040. if (ward->DamageLeft > ward->BaseDamage)
  5041. ward->DamageLeft = ward->BaseDamage;
  5042. for (int32 i = 0; i < spell->targets.size(); i++) {
  5043. if (Spawn* spawn = zone->GetSpawnByID(spell->targets.at(i)))
  5044. zone->SendHealPacket(ward->Spell->caster, spawn, HEAL_PACKET_TYPE_REGEN_ABSORB, amount, ward->Spell->spell->GetName());
  5045. }
  5046. }
  5047. }
  5048. spell->MSpellTargets.releasereadlock(__FUNCTION__, __LINE__);
  5049. if (ward && spell->caster->IsPlayer())
  5050. ClientPacketFunctions::SendMaintainedExamineUpdate(spell->caster->GetZone()->GetClientBySpawn(spell->caster), spell->slot_pos, ward->DamageLeft, 1);
  5051. return 0;
  5052. }
  5053. int EQ2Emu_lua_GetWardAmountLeft(lua_State* state) {
  5054. if (!lua_interface)
  5055. return 0;
  5056. LuaSpell* spell = lua_interface->GetCurrentSpell(state);
  5057. if (!spell) {
  5058. lua_interface->LogError("%s: LUA GetWardAmountLeft command error: this command can only be used in a spell script", lua_interface->GetScriptName(state));
  5059. return 0;
  5060. }
  5061. if (spell->caster->GetZone()->GetSpawnByID(spell->targets.at(0))->IsEntity()) {
  5062. Entity* target = (Entity*)spell->caster->GetZone()->GetSpawnByID(spell->targets.at(0));
  5063. WardInfo* ward = target->GetWard(spell->spell->GetSpellID());
  5064. if (ward) {
  5065. lua_interface->SetInt32Value(state, ward->DamageLeft);
  5066. return 1;
  5067. }
  5068. }
  5069. return 0;
  5070. }
  5071. int EQ2Emu_lua_GetWardValue(lua_State* state) {
  5072. if (!lua_interface)
  5073. return 0;
  5074. LuaSpell* spell = lua_interface->GetCurrentSpell(state);
  5075. if (!spell) {
  5076. lua_interface->LogError("%s: LUA GetWardValue command error: this command can only be used in a spell script", lua_interface->GetScriptName(state));
  5077. return 0;
  5078. }
  5079. string type = lua_interface->GetStringValue(state, 2);
  5080. if (spell->caster->GetZone()->GetSpawnByID(spell->targets.at(0))->IsEntity()) {
  5081. Entity* target = (Entity*)spell->caster->GetZone()->GetSpawnByID(spell->targets.at(0));
  5082. WardInfo* ward = target->GetWard(spell->spell->GetSpellID());
  5083. if (ward) {
  5084. if (boost::iequals(type, "damageleft"))
  5085. lua_interface->SetInt32Value(state, ward->DamageLeft);
  5086. else if (boost::iequals(type, "basedamage"))
  5087. lua_interface->SetInt32Value(state, ward->BaseDamage);
  5088. else if (boost::iequals(type, "keepward"))
  5089. lua_interface->SetBooleanValue(state, ward->keepWard);
  5090. else if (boost::iequals(type, "wardtype"))
  5091. lua_interface->SetInt32Value(state, ward->WardType);
  5092. else if (boost::iequals(type, "dmgabsorptionpct"))
  5093. lua_interface->SetInt32Value(state, ward->DamageAbsorptionPercentage);
  5094. else if (boost::iequals(type, "dmgabsorptionmaxhealthpct"))
  5095. lua_interface->SetInt32Value(state, ward->DamageAbsorptionMaxHealthPercent);
  5096. else if (boost::iequals(type, "redirectdamagepercent"))
  5097. lua_interface->SetInt32Value(state, ward->RedirectDamagePercent);
  5098. else if (boost::iequals(type, "lastredirectdamage"))
  5099. lua_interface->SetInt32Value(state, ward->LastRedirectDamage);
  5100. else if (boost::iequals(type, "lastabsorbeddamage"))
  5101. lua_interface->SetInt32Value(state, ward->LastAbsorbedDamage);
  5102. else if (boost::iequals(type, "hitcount"))
  5103. lua_interface->SetInt32Value(state, ward->HitCount);
  5104. else if (boost::iequals(type, "maxhitcount"))
  5105. lua_interface->SetInt32Value(state, ward->MaxHitCount);
  5106. else
  5107. lua_interface->LogError("%s: LUA GetWardValue command argument type '%s' did not match any options", lua_interface->GetScriptName(state), type);
  5108. return 1;
  5109. }
  5110. }
  5111. return 0;
  5112. }
  5113. int EQ2Emu_lua_RemoveWard(lua_State* state) {
  5114. if (!lua_interface)
  5115. return 0;
  5116. LuaSpell* spell = lua_interface->GetCurrentSpell(state);
  5117. ZoneServer* zone = spell->caster->GetZone();
  5118. Spawn* target = 0;
  5119. spell->MSpellTargets.readlock(__FUNCTION__, __LINE__);
  5120. for (int32 i = 0; i < spell->targets.size(); i++) {
  5121. target = zone->GetSpawnByID(spell->targets.at(i));
  5122. if (target && target->IsEntity()) {
  5123. ((Entity*)target)->RemoveWard(spell->spell->GetSpellID());
  5124. }
  5125. }
  5126. spell->MSpellTargets.releasereadlock(__FUNCTION__, __LINE__);
  5127. return 0;
  5128. }
  5129. int EQ2Emu_lua_Interrupt(lua_State* state)
  5130. {
  5131. if (!lua_interface)
  5132. return 0;
  5133. Spawn* caster = lua_interface->GetSpawn(state); // Second param in lua_interface->get functions defaults to 1
  5134. Spawn* target = lua_interface->GetSpawn(state, 2);
  5135. LuaSpell* spell = lua_interface->GetCurrentSpell(state);
  5136. if (!caster)
  5137. {
  5138. lua_interface->LogError("%s: LUA Interrupt command error: caster is not a valid spawn", lua_interface->GetScriptName(state));
  5139. return 0;
  5140. }
  5141. if (!target)
  5142. {
  5143. lua_interface->LogError("%s: LUA Interrupt command error: target is not a valid spawn", lua_interface->GetScriptName(state));
  5144. return 0;
  5145. }
  5146. if (!spell) {
  5147. lua_interface->LogError("%s: LUA Interrupt command error: spell is not a valid spawn", lua_interface->GetScriptName(state));
  5148. return 0;
  5149. }
  5150. if (!target->IsEntity() && !spell)
  5151. {
  5152. lua_interface->LogError("%s: LUA Interrupt command error: Target is not an entity", lua_interface->GetScriptName(state));
  5153. return 0;
  5154. }
  5155. if (!target && spell) {
  5156. spell->MSpellTargets.readlock(__FUNCTION__, __LINE__);
  5157. for (int8 i = 0; i < spell->targets.size(); i++) {
  5158. target = caster->GetZone()->GetSpawnByID(spell->targets.at(i));
  5159. if (!target || !target->IsEntity())
  5160. continue;
  5161. ((Entity*)target)->GetZone()->GetSpellProcess()->Interrupted((Entity*)target, caster, SPELL_ERROR_INTERRUPTED);
  5162. }
  5163. spell->MSpellTargets.releasereadlock(__FUNCTION__, __LINE__);
  5164. }
  5165. else
  5166. caster->GetZone()->GetSpellProcess()->Interrupted((Entity*)target, caster, SPELL_ERROR_INTERRUPTED);
  5167. return 0;
  5168. }
  5169. int EQ2Emu_lua_Stealth(lua_State* state) {
  5170. if (!lua_interface)
  5171. return 0;
  5172. int8 type = lua_interface->GetInt8Value(state);
  5173. Spawn* spawn = lua_interface->GetSpawn(state, 2);
  5174. LuaSpell* spell = lua_interface->GetCurrentSpell(state);
  5175. if (!spell) {
  5176. lua_interface->LogError("%s: LUA Stealth command error: must be used from spell script", lua_interface->GetScriptName(state));
  5177. return 0;
  5178. }
  5179. ZoneServer* zone = spell->caster->GetZone();
  5180. if (spawn) {
  5181. if (spawn->IsEntity()) {
  5182. if (type == 1) {
  5183. ((Entity*)spawn)->AddStealthSpell(spell);
  5184. if (!(spell->effect_bitmask & EFFECT_FLAG_STEALTH))
  5185. spell->effect_bitmask += EFFECT_FLAG_STEALTH;
  5186. }
  5187. else if (type == 2) {
  5188. ((Entity*)spawn)->AddInvisSpell(spell);
  5189. if (!(spell->effect_bitmask & EFFECT_FLAG_INVIS))
  5190. spell->effect_bitmask += EFFECT_FLAG_INVIS;
  5191. }
  5192. return 0;
  5193. }
  5194. else {
  5195. lua_interface->LogError("%s: LUA Stealth command error: target override is not Entity", lua_interface->GetScriptName(state));
  5196. return 0;
  5197. }
  5198. }
  5199. else {
  5200. spell->MSpellTargets.readlock(__FUNCTION__, __LINE__);
  5201. for (int32 i = 0; i < spell->targets.size(); i++) {
  5202. spawn = zone->GetSpawnByID(spell->targets.at(i));
  5203. if (!spawn || !spawn->IsEntity())
  5204. continue;
  5205. if (type == 1) {
  5206. ((Entity*)spawn)->AddStealthSpell(spell);
  5207. if (!(spell->effect_bitmask & EFFECT_FLAG_STEALTH))
  5208. spell->effect_bitmask += EFFECT_FLAG_STEALTH;
  5209. }
  5210. else if (type == 2) {
  5211. ((Entity*)spawn)->AddInvisSpell(spell);
  5212. if (!(spell->effect_bitmask & EFFECT_FLAG_INVIS))
  5213. spell->effect_bitmask += EFFECT_FLAG_INVIS;
  5214. }
  5215. else {
  5216. lua_interface->LogError("%s: LUA Stealth command error: invalid stealth type given", lua_interface->GetScriptName(state));
  5217. break;
  5218. }
  5219. }
  5220. spell->MSpellTargets.releasereadlock(__FUNCTION__, __LINE__);
  5221. }
  5222. return 0;
  5223. }
  5224. int EQ2Emu_lua_IsStealthed(lua_State* state) {
  5225. if (!lua_interface)
  5226. return 0;
  5227. Spawn* spawn = lua_interface->GetSpawn(state);
  5228. if (!spawn) {
  5229. lua_interface->LogError("%s: LUA IsStealthed command error: spawn is not valid", lua_interface->GetScriptName(state));
  5230. return 0;
  5231. }
  5232. if (spawn->IsEntity()) {
  5233. lua_interface->SetBooleanValue(state, ((Entity*)spawn)->IsStealthed());
  5234. return 1;
  5235. }
  5236. else
  5237. lua_interface->LogError("%s: LUA IsStealthed command error: spawn is not entity", lua_interface->GetScriptName(state));
  5238. return 0;
  5239. }
  5240. int EQ2Emu_lua_IsInvis(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 IsInvis command error: spawn is not valid", lua_interface->GetScriptName(state));
  5246. return 0;
  5247. }
  5248. if (spawn->IsEntity()) {
  5249. lua_interface->SetBooleanValue(state, ((Entity*)spawn)->IsInvis());
  5250. return 1;
  5251. }
  5252. else
  5253. lua_interface->LogError("%s: LUA IsInvis command error: spawn is not entity", lua_interface->GetScriptName(state));
  5254. return 0;
  5255. }
  5256. int EQ2Emu_lua_HasItemEquipped(lua_State* state) {
  5257. if (!lua_interface)
  5258. return 0;
  5259. Spawn* player = lua_interface->GetSpawn(state);
  5260. int32 item_id = lua_interface->GetInt32Value(state, 2);
  5261. if (!player->IsPlayer()) {
  5262. lua_interface->LogError("%s: LUA HasItemEquipped command error: spawn is not player", lua_interface->GetScriptName(state));
  5263. return 0;
  5264. }
  5265. lua_interface->SetBooleanValue(state, ((Player*)player)->GetEquipmentList()->HasItem(item_id));
  5266. return 1;
  5267. }
  5268. int EQ2Emu_lua_GetEquippedItemBySlot(lua_State* state) {
  5269. if (!lua_interface)
  5270. return 0;
  5271. Spawn* player = lua_interface->GetSpawn(state);
  5272. int8 slot = lua_interface->GetInt8Value(state, 2);
  5273. if (!player) {
  5274. lua_interface->LogError("%s: LUA GetEquippedItemBySlot command error: spawn is not valid", lua_interface->GetScriptName(state));
  5275. return 0;
  5276. }
  5277. if (!player->IsPlayer()) {
  5278. lua_interface->LogError("%s: LUA GetEquippedItemBySlot command error: spawn is not player", lua_interface->GetScriptName(state));
  5279. return 0;
  5280. }
  5281. Item* item = ((Player*)player)->GetEquipmentList()->GetItem(slot);
  5282. if (!item) {
  5283. lua_interface->LogError("%s: LUA GetEquippedItemBySlot command error: item was not found in slot", lua_interface->GetScriptName(state));
  5284. return 0;
  5285. }
  5286. lua_interface->SetItemValue(state, item);
  5287. return 1;
  5288. }
  5289. int EQ2Emu_lua_GetEquippedItemByID(lua_State* state) {
  5290. if (!lua_interface)
  5291. return 0;
  5292. Spawn* player = lua_interface->GetSpawn(state);
  5293. int32 id = lua_interface->GetInt32Value(state, 2);
  5294. if (!player) {
  5295. lua_interface->LogError("%s: LUA GetEquippedItemByID command error: spawn is not valid", lua_interface->GetScriptName(state));
  5296. return 0;
  5297. }
  5298. if (!player->IsPlayer()) {
  5299. lua_interface->LogError("%s: LUA GetEquippedItemByID command error: spawn is not player", lua_interface->GetScriptName(state));
  5300. return 0;
  5301. }
  5302. Item* item = ((Player*)player)->GetEquipmentList()->GetItemFromItemID(id);
  5303. if (!item) {
  5304. lua_interface->LogError("%s: LUA GetEquippedItemByID command error: equipped item with used id not found", lua_interface->GetScriptName(state));
  5305. return 0;
  5306. }
  5307. lua_interface->SetItemValue(state, item);
  5308. return 1;
  5309. }
  5310. int EQ2Emu_lua_GetItemByID(lua_State* state) {
  5311. if (!lua_interface)
  5312. return 0;
  5313. Spawn* player = lua_interface->GetSpawn(state);
  5314. int32 id = lua_interface->GetInt32Value(state, 2);
  5315. int8 count = lua_interface->GetInt8Value(state, 3);
  5316. bool include_bank = lua_interface->GetInt8Value(state, 4);
  5317. if (!player) {
  5318. lua_interface->LogError("%s: LUA GetItemByID command error: spawn is not valid", lua_interface->GetScriptName(state));
  5319. return 0;
  5320. }
  5321. if (!player->IsPlayer()) {
  5322. lua_interface->LogError("%s: LUA GetItemByID command error: spawn is not player", lua_interface->GetScriptName(state));
  5323. return 0;
  5324. }
  5325. if (!count)
  5326. count = 1;
  5327. Item* item = ((Player*)player)->GetPlayerItemList()->GetItemFromID(id, count, include_bank);
  5328. if (!item) {
  5329. lua_interface->LogError("%s: LUA GetItemByID command error: item with used id not found", lua_interface->GetScriptName(state));
  5330. return 0;
  5331. }
  5332. lua_interface->SetItemValue(state, item);
  5333. return 1;
  5334. }
  5335. int EQ2Emu_lua_PlayAnimation(lua_State* state) {
  5336. if (!lua_interface)
  5337. return 0;
  5338. Spawn* spawn = lua_interface->GetSpawn(state);
  5339. int32 anim = lua_interface->GetInt32Value(state, 2);
  5340. Spawn* spawn2 = lua_interface->GetSpawn(state, 3);
  5341. int8 type = lua_interface->GetInt8Value(state, 4);
  5342. if (!spawn) {
  5343. lua_interface->LogError("%s: LUA PlayAnimation command error: spawn is not valid", lua_interface->GetScriptName(state));
  5344. return 0;
  5345. }
  5346. if (spawn2) {
  5347. if (spawn2->IsPlayer()) {
  5348. if (type != 1 && type != 2)
  5349. spawn->GetZone()->PlayAnimation(spawn, anim, spawn2);
  5350. else
  5351. spawn->GetZone()->PlayAnimation(spawn, anim, spawn2, type);
  5352. return 0;
  5353. }
  5354. else {
  5355. lua_interface->LogError("%s: LUA PlayAnimation command error: second spawn not a player", lua_interface->GetScriptName(state));
  5356. return 0;
  5357. }
  5358. }
  5359. else
  5360. spawn->GetZone()->PlayAnimation(spawn, anim);
  5361. return 0;
  5362. }
  5363. int EQ2Emu_lua_IsPet(lua_State* state) {
  5364. if (!lua_interface)
  5365. return 0;
  5366. Spawn* spawn = lua_interface->GetSpawn(state);
  5367. if (!spawn) {
  5368. lua_interface->LogError("%s: LUA IsPet command error: spawn is not valid", lua_interface->GetScriptName(state));
  5369. return 0;
  5370. }
  5371. lua_interface->SetBooleanValue(state, spawn->IsPet());
  5372. return 1;
  5373. }
  5374. int EQ2Emu_lua_GetOwner(lua_State* state) {
  5375. if (!lua_interface)
  5376. return 0;
  5377. Spawn* spawn = lua_interface->GetSpawn(state);
  5378. if (!spawn) {
  5379. lua_interface->LogError("%s: LUA GetOwner command error: spawn is not valid", lua_interface->GetScriptName(state));
  5380. return 0;
  5381. }
  5382. if (!spawn->IsNPC()) {
  5383. lua_interface->LogError("%s: LUA GetOwner command error: spawn is not a NPC", lua_interface->GetScriptName(state));
  5384. return 0;
  5385. }
  5386. if (((NPC*)spawn)->GetOwner()) {
  5387. lua_interface->SetSpawnValue(state, ((NPC*)spawn)->GetOwner());
  5388. return 1;
  5389. }
  5390. return 0;
  5391. }
  5392. int EQ2Emu_lua_SetTarget(lua_State* state) {
  5393. if (!lua_interface)
  5394. return 0;
  5395. Spawn* spawn = lua_interface->GetSpawn(state);
  5396. Spawn* target = lua_interface->GetSpawn(state, 2);
  5397. if (!spawn) {
  5398. lua_interface->LogError("%s: LUA SetTarget command error: spawn is not valid", lua_interface->GetScriptName(state));
  5399. return 0;
  5400. }
  5401. if (!spawn) {
  5402. lua_interface->LogError("%s: LUA SetTarget command error: target is not valid", lua_interface->GetScriptName(state));
  5403. return 0;
  5404. }
  5405. spawn->SetTarget(target);
  5406. return 0;
  5407. }
  5408. int EQ2Emu_lua_SetInCombat(lua_State* state) {
  5409. if (!lua_interface)
  5410. return 0;
  5411. Spawn* spawn = lua_interface->GetSpawn(state);
  5412. bool val = lua_interface->GetBooleanValue(state, 2);
  5413. if (!spawn) {
  5414. lua_interface->LogError("%s: LUA SetInCombat command error: spawn is not valid", lua_interface->GetScriptName(state));
  5415. return 0;
  5416. }
  5417. if (!spawn->IsEntity()) {
  5418. lua_interface->LogError("%s: LUA SetInCombat command error: spawn is not an entity", lua_interface->GetScriptName(state));
  5419. return 0;
  5420. }
  5421. ((Entity*)spawn)->InCombat(val);
  5422. if (val) {
  5423. spawn->ClearRunningLocations();
  5424. spawn->CalculateRunningLocation(true);
  5425. }
  5426. return 0;
  5427. }
  5428. int EQ2Emu_lua_CompareSpawns(lua_State* state) {
  5429. if (!lua_interface)
  5430. return 0;
  5431. Spawn* spawn1 = lua_interface->GetSpawn(state);
  5432. Spawn* spawn2 = lua_interface->GetSpawn(state, 2);
  5433. if (!spawn1) {
  5434. lua_interface->LogError("%s: LUA CompareSpawns command error: first spawn is not valid", lua_interface->GetScriptName(state));
  5435. return 0;
  5436. }
  5437. if (!spawn2) {
  5438. lua_interface->LogError("%s: LUA CompareSpawns command error: second spawn is not valid", lua_interface->GetScriptName(state));
  5439. return 0;
  5440. }
  5441. lua_interface->SetBooleanValue(state, (spawn1 == spawn2));
  5442. return 1;
  5443. }
  5444. int EQ2Emu_lua_Runback(lua_State* state) {
  5445. if (!lua_interface)
  5446. return 0;
  5447. Spawn* spawn = lua_interface->GetSpawn(state);
  5448. if (!spawn) {
  5449. lua_interface->LogError("%s: LUA Runback command error: spawn is not valid", lua_interface->GetScriptName(state));
  5450. return 0;
  5451. }
  5452. if (!spawn->IsNPC()) {
  5453. lua_interface->LogError("%s: LUA Runback command error: spawn is not an NPC", lua_interface->GetScriptName(state));
  5454. return 0;
  5455. }
  5456. ((NPC*)spawn)->Runback();
  5457. return 0;
  5458. }
  5459. int EQ2Emu_lua_GetRunbackDistance(lua_State* state) {
  5460. if (!lua_interface)
  5461. return 0;
  5462. Spawn* spawn = lua_interface->GetSpawn(state);
  5463. if (!spawn) {
  5464. lua_interface->LogError("%s: LUA GetRunbackDistance command error: spawn is not valid", lua_interface->GetScriptName(state));
  5465. return 0;
  5466. }
  5467. if (!spawn->IsNPC()) {
  5468. lua_interface->LogError("%s: LUA GetRunbackDistance command error: spawn is not an NPC", lua_interface->GetScriptName(state));
  5469. return 0;
  5470. }
  5471. lua_interface->SetFloatValue(state, ((NPC*)spawn)->GetRunbackDistance());
  5472. return 1;
  5473. }
  5474. int EQ2Emu_lua_IsCasting(lua_State* state) {
  5475. if (!lua_interface)
  5476. return 0;
  5477. Spawn* spawn = lua_interface->GetSpawn(state);
  5478. if (!spawn) {
  5479. lua_interface->LogError("%s: LUA IsCasting command error: spawn is not valid", lua_interface->GetScriptName(state));
  5480. return 0;
  5481. }
  5482. if (!spawn->IsEntity()) {
  5483. lua_interface->LogError("%s: LUA IsCasting command error: spawn is not an entity", lua_interface->GetScriptName(state));
  5484. return 0;
  5485. }
  5486. lua_interface->SetBooleanValue(state, ((Entity*)spawn)->IsCasting());
  5487. return 1;
  5488. }
  5489. int EQ2Emu_lua_IsMezzed(lua_State* state) {
  5490. if (!lua_interface)
  5491. return 0;
  5492. Spawn* spawn = lua_interface->GetSpawn(state);
  5493. if (!spawn) {
  5494. lua_interface->LogError("%s: LUA IsMezzed command error: spawn is not valid", lua_interface->GetScriptName(state));
  5495. return 0;
  5496. }
  5497. if (!spawn->IsEntity()) {
  5498. lua_interface->LogError("%s: LUA IsMezzed command error: spawn is not an entity", lua_interface->GetScriptName(state));
  5499. return 0;
  5500. }
  5501. lua_interface->SetBooleanValue(state, ((Entity*)spawn)->IsMezzed());
  5502. return 1;
  5503. }
  5504. int EQ2Emu_lua_IsStunned(lua_State* state) {
  5505. if (!lua_interface)
  5506. return 0;
  5507. Spawn* spawn = lua_interface->GetSpawn(state);
  5508. if (!spawn) {
  5509. lua_interface->LogError("%s: LUA IsStunned command error: spawn is not valid", lua_interface->GetScriptName(state));
  5510. return 0;
  5511. }
  5512. if (!spawn->IsEntity()) {
  5513. lua_interface->LogError("%s: LUA IsStunned command error: spawn is not an entity", lua_interface->GetScriptName(state));
  5514. return 0;
  5515. }
  5516. lua_interface->SetBooleanValue(state, ((Entity*)spawn)->IsStunned());
  5517. return 1;
  5518. }
  5519. int EQ2Emu_lua_IsMezzedOrStunned(lua_State* state) {
  5520. if (!lua_interface)
  5521. return 0;
  5522. Spawn* spawn = lua_interface->GetSpawn(state);
  5523. if (!spawn) {
  5524. lua_interface->LogError("%s: LUA IsMezzedOrStunned command error: spawn is not valid", lua_interface->GetScriptName(state));
  5525. return 0;
  5526. }
  5527. if (!spawn->IsEntity()) {
  5528. lua_interface->LogError("%s: LUA IsMezzedOrStunned command error: spawn is not an entity", lua_interface->GetScriptName(state));
  5529. return 0;
  5530. }
  5531. lua_interface->SetBooleanValue(state, ((Entity*)spawn)->IsMezzedOrStunned());
  5532. return 1;
  5533. }
  5534. int EQ2Emu_lua_ProcessSpell(lua_State* state) {
  5535. if (!lua_interface)
  5536. return 0;
  5537. Spawn* spawn = lua_interface->GetSpawn(state);
  5538. Spawn* target = lua_interface->GetSpawn(state, 2);
  5539. float distance = lua_interface->GetFloatValue(state, 3);
  5540. if (!spawn) {
  5541. lua_interface->LogError("%s: LUA ProcessSpell command error: spawn is not valid", lua_interface->GetScriptName(state));
  5542. return 0;
  5543. }
  5544. if (!target) {
  5545. lua_interface->LogError("%s: LUA ProcessSpell command error: spawn is not an entity", lua_interface->GetScriptName(state));
  5546. return 0;
  5547. }
  5548. if (!spawn->IsNPC()) {
  5549. lua_interface->LogError("%s: LUA ProcessSpell command error: spawn is not an NPC", lua_interface->GetScriptName(state));
  5550. return 0;
  5551. }
  5552. if (!target->IsEntity()) {
  5553. lua_interface->LogError("%s: LUA ProcessSpell command error: target is not an entity", lua_interface->GetScriptName(state));
  5554. return 0;
  5555. }
  5556. lua_interface->SetBooleanValue(state, ((NPC*)spawn)->Brain()->ProcessSpell(((Entity*)target), distance));
  5557. return 1;
  5558. }
  5559. int EQ2Emu_lua_ProcessMelee(lua_State* state) {
  5560. if (!lua_interface)
  5561. return 0;
  5562. Spawn* spawn = lua_interface->GetSpawn(state);
  5563. Spawn* target = lua_interface->GetSpawn(state, 2);
  5564. float distance = lua_interface->GetFloatValue(state, 3);
  5565. if (!spawn) {
  5566. lua_interface->LogError("%s: LUA ProcessMelee command error: spawn is not valid", lua_interface->GetScriptName(state));
  5567. return 0;
  5568. }
  5569. if (!target) {
  5570. lua_interface->LogError("%s: LUA ProcessMelee command error: target is not valid", lua_interface->GetScriptName(state));
  5571. return 0;
  5572. }
  5573. if (!spawn->IsNPC()) {
  5574. lua_interface->LogError("%s: LUA ProcessMelee command error: spawn is not an NPC", lua_interface->GetScriptName(state));
  5575. return 0;
  5576. }
  5577. if (!target->IsEntity()) {
  5578. lua_interface->LogError("%s: LUA ProcessMelee command error: target is not an entity", lua_interface->GetScriptName(state));
  5579. return 0;
  5580. }
  5581. ((NPC*)spawn)->Brain()->ProcessMelee(((Entity*)target), distance);
  5582. return 0;
  5583. }
  5584. int EQ2Emu_lua_HasRecovered(lua_State* state) {
  5585. if (!lua_interface)
  5586. return 0;
  5587. Spawn* spawn = lua_interface->GetSpawn(state);
  5588. if (!spawn) {
  5589. lua_interface->LogError("%s: LUA HasRecovered command error: spawn is not valid", lua_interface->GetScriptName(state));
  5590. return 0;
  5591. }
  5592. if (!spawn->IsNPC()) {
  5593. lua_interface->LogError("%s: LUA HasRecovered command error: spawn is not an NPC", lua_interface->GetScriptName(state));
  5594. return 0;
  5595. }
  5596. lua_interface->SetBooleanValue(state, ((NPC*)spawn)->Brain()->HasRecovered());
  5597. return 1;
  5598. }
  5599. int EQ2Emu_lua_GetEncounterSize(lua_State* state) {
  5600. if (!lua_interface)
  5601. return 0;
  5602. Spawn* spawn = lua_interface->GetSpawn(state);
  5603. if (!spawn) {
  5604. lua_interface->LogError("%s: LUA GetEncounterSize command error: spawn is not valid", lua_interface->GetScriptName(state));
  5605. return 0;
  5606. }
  5607. if (!spawn->IsNPC()) {
  5608. lua_interface->LogError("%s: LUA GetEncounterSize command error: spawn is not an NPC", lua_interface->GetScriptName(state));
  5609. return 0;
  5610. }
  5611. lua_interface->SetInt32Value(state, ((NPC*)spawn)->Brain()->GetEncounterSize());
  5612. return 1;
  5613. }
  5614. int EQ2Emu_lua_GetMostHated(lua_State* state) {
  5615. if (!lua_interface)
  5616. return 0;
  5617. Spawn* spawn = lua_interface->GetSpawn(state);
  5618. if (!spawn) {
  5619. lua_interface->LogError("%s: LUA GetMostHated command error: spawn is not valid", lua_interface->GetScriptName(state));
  5620. return 0;
  5621. }
  5622. if (!spawn->IsNPC()) {
  5623. lua_interface->LogError("%s: LUA GetMostHated command error: spawn is not an NPC", lua_interface->GetScriptName(state));
  5624. return 0;
  5625. }
  5626. Entity* hated = ((NPC*)spawn)->Brain()->GetMostHated();
  5627. if (hated) {
  5628. lua_interface->SetSpawnValue(state, hated);
  5629. return 1;
  5630. }
  5631. return 0;
  5632. }
  5633. int EQ2Emu_lua_ClearHate(lua_State* state) {
  5634. if (!lua_interface)
  5635. return 0;
  5636. Spawn* spawn = lua_interface->GetSpawn(state);
  5637. Spawn* hated = lua_interface->GetSpawn(state, 2);
  5638. if (!spawn) {
  5639. lua_interface->LogError("%s: LUA ClearHate command error: spawn is not valid", lua_interface->GetScriptName(state));
  5640. return 0;
  5641. }
  5642. if (!spawn->IsNPC()) {
  5643. lua_interface->LogError("%s: LUA ClearHate command error: spawn is not NPC", lua_interface->GetScriptName(state));
  5644. return 0;
  5645. }
  5646. if (!hated) {
  5647. ((NPC*)spawn)->Brain()->ClearHate();
  5648. return 0;
  5649. }
  5650. else
  5651. {
  5652. if (!hated->IsEntity()) {
  5653. lua_interface->LogError("%s: LUA ClearHate command error: second param is not entity", lua_interface->GetScriptName(state));
  5654. return 0;
  5655. }
  5656. ((NPC*)spawn)->Brain()->ClearHate(((Entity*)hated));
  5657. return 0;
  5658. }
  5659. return 0;
  5660. }
  5661. int EQ2Emu_lua_ClearEncounter(lua_State* state) {
  5662. if (!lua_interface)
  5663. return 0;
  5664. Spawn* spawn = lua_interface->GetSpawn(state);
  5665. if (!spawn) {
  5666. lua_interface->LogError("%s: LUA ClearEncounter command error: spawn is not valid", lua_interface->GetScriptName(state));
  5667. return 0;
  5668. }
  5669. if (!spawn->IsNPC()) {
  5670. lua_interface->LogError("%s: LUA ClearEncounter command error: spawn is not an NPC", lua_interface->GetScriptName(state));
  5671. return 0;
  5672. }
  5673. ((NPC*)spawn)->Brain()->ClearEncounter();
  5674. return 0;
  5675. }
  5676. int EQ2Emu_lua_GetEncounter(lua_State* state) {
  5677. if (!lua_interface)
  5678. return 0;
  5679. Spawn* spawn = lua_interface->GetSpawn(state);
  5680. if (!spawn) {
  5681. lua_interface->LogError("%s: LUA GetEncounter command error: Spawn is not valid", lua_interface->GetScriptName(state));
  5682. return 0;
  5683. }
  5684. if (!spawn->IsNPC()) {
  5685. lua_interface->LogError("%s: LUA GetEncounter command error: spawn is not a NPC", lua_interface->GetScriptName(state));
  5686. return 0;
  5687. }
  5688. // Temp list to store hate list
  5689. vector<int32>* encounterList = ((NPC*)spawn)->Brain()->GetEncounter();
  5690. if (encounterList->size() == 0) {
  5691. safe_delete(encounterList);
  5692. return 0;
  5693. }
  5694. lua_createtable(state, encounterList->size(), 0);
  5695. int newTable = lua_gettop(state);
  5696. for (int32 i = 0; i < encounterList->size(); i++) {
  5697. Spawn* temp = spawn->GetZone()->GetSpawnByID(encounterList->at(i));
  5698. if (temp)
  5699. lua_interface->SetSpawnValue(state, temp);
  5700. lua_rawseti(state, newTable, i + 1);
  5701. }
  5702. safe_delete(encounterList);
  5703. return 1;
  5704. }
  5705. int EQ2Emu_lua_GetHateList(lua_State* state) {
  5706. if (!lua_interface)
  5707. return 0;
  5708. Spawn* spawn = lua_interface->GetSpawn(state);
  5709. if (!spawn) {
  5710. lua_interface->LogError("%s: LUA GetHateList command error: spawn is not valid", lua_interface->GetScriptName(state));
  5711. return 0;
  5712. }
  5713. if (!spawn->IsNPC()) {
  5714. lua_interface->LogError("%s: LUA GetHateList command error: spawn is not a NPC", lua_interface->GetScriptName(state));
  5715. return 0;
  5716. }
  5717. // Temp list to store hate list
  5718. vector<Entity*>* hateList = ((NPC*)spawn)->Brain()->GetHateList();
  5719. if (hateList->size() == 0) {
  5720. safe_delete(hateList);
  5721. return 0;
  5722. }
  5723. lua_createtable(state, hateList->size(), 0);
  5724. int newTable = lua_gettop(state);
  5725. for (int32 i = 0; i < hateList->size(); i++) {
  5726. lua_interface->SetSpawnValue(state, hateList->at(i));
  5727. lua_rawseti(state, newTable, i + 1);
  5728. }
  5729. safe_delete(hateList);
  5730. return 1;
  5731. }
  5732. int EQ2Emu_lua_HasGroup(lua_State* state) {
  5733. if (!lua_interface)
  5734. return 0;
  5735. Spawn* spawn = lua_interface->GetSpawn(state);
  5736. if (!spawn) {
  5737. lua_interface->LogError("%s: LUA HasGroup command error: spawn is not valid", lua_interface->GetScriptName(state));
  5738. return 0;
  5739. }
  5740. if (spawn->IsPlayer()) {
  5741. if (((Player*)spawn)->GetGroupMemberInfo() && world.GetGroupManager()->GetGroupSize(((Player*)spawn)->GetGroupMemberInfo()->group_id) > 1)
  5742. lua_interface->SetBooleanValue(state, true);
  5743. else
  5744. lua_interface->SetBooleanValue(state, false);
  5745. return 1;
  5746. }
  5747. else {
  5748. lua_interface->SetBooleanValue(state, spawn->HasSpawnGroup());
  5749. return 1;
  5750. }
  5751. }
  5752. int EQ2Emu_lua_SetCompleteFlag(lua_State* state) {
  5753. if (!lua_interface)
  5754. return 0;
  5755. Quest* quest = lua_interface->GetQuest(state);
  5756. if (!quest) {
  5757. lua_interface->LogError("%s: LUA SetCompleteFlag command error: quest is not valid", lua_interface->GetScriptName(state));
  5758. return 0;
  5759. }
  5760. quest->SetCompletedFlag(true);
  5761. return 0;
  5762. }
  5763. int EQ2Emu_lua_HasSpellEffect(lua_State* state) {
  5764. if (!lua_interface)
  5765. return 0;
  5766. Spawn* spawn = lua_interface->GetSpawn(state);
  5767. int32 spellID = lua_interface->GetInt32Value(state, 2);
  5768. int8 tier = lua_interface->GetInt8Value(state, 3);
  5769. if (!spawn) {
  5770. lua_interface->LogError("%s: LUA HasSpellEffect command error: spawn is not valid", lua_interface->GetScriptName(state));
  5771. return 0;
  5772. }
  5773. if (!spawn->IsEntity()) {
  5774. lua_interface->LogError("%s: LUA HasSpellEffect command error: spawn is not an entity", lua_interface->GetScriptName(state));
  5775. return 0;
  5776. }
  5777. if (spellID == 0) {
  5778. lua_interface->LogError("%s: LUA HasSpellEffect command error: spell id is not valid", lua_interface->GetScriptName(state));
  5779. return 0;
  5780. }
  5781. SpellEffects* effect = ((Entity*)spawn)->GetSpellEffect(spellID);
  5782. if (effect) {
  5783. if (tier > 0) {
  5784. // If a tier was passed chec to see if it is the same as the effect
  5785. if (tier == effect->tier)
  5786. lua_interface->SetBooleanValue(state, true);
  5787. else
  5788. lua_interface->SetBooleanValue(state, false);
  5789. return 1;
  5790. }
  5791. else {
  5792. // Have an effect but no tier was passed so return true
  5793. lua_interface->SetBooleanValue(state, true);
  5794. }
  5795. return 1;
  5796. }
  5797. // no effect so return false
  5798. lua_interface->SetBooleanValue(state, false);
  5799. return 1;
  5800. }
  5801. int EQ2Emu_lua_AddSpawnIDAccess(lua_State* state) {
  5802. if (!lua_interface)
  5803. return 0;
  5804. Spawn* spawn = lua_interface->GetSpawn(state);
  5805. int32 id = lua_interface->GetInt32Value(state, 2);
  5806. ZoneServer* zone = lua_interface->GetZone(state, 3);
  5807. Spawn* spawn2 = 0;
  5808. vector<Spawn*> list;
  5809. if (!spawn) {
  5810. lua_interface->LogError("%s: LUA AddSpawnIDAccess command error: spawn is not valid", lua_interface->GetScriptName(state));
  5811. return 0;
  5812. }
  5813. //If zone not provided, use spawn's zone
  5814. if (!zone)
  5815. zone = spawn->GetZone();
  5816. list = zone->GetSpawnsByID(id);
  5817. if (list.size() == 0) {
  5818. lua_interface->LogError("%s: LUA AddSpawnIDAccess command error: GetSpawnsByID returned no spawns", lua_interface->GetScriptName(state));
  5819. return 0;
  5820. }
  5821. vector<Spawn*>::iterator itr = list.begin();
  5822. for (int8 i = 0; i < list.size(); i++) {
  5823. spawn2 = itr[i];
  5824. if (spawn2)
  5825. spawn2->AddAllowAccessSpawn(spawn);
  5826. }
  5827. return 0;
  5828. }
  5829. int EQ2Emu_lua_RemoveSpawnIDAccess(lua_State* state) {
  5830. if (!lua_interface)
  5831. return 0;
  5832. Spawn* spawn = lua_interface->GetSpawn(state);
  5833. int32 id = lua_interface->GetInt32Value(state, 2);
  5834. ZoneServer* zone = lua_interface->GetZone(state, 3);
  5835. Spawn* spawn2 = 0;
  5836. if (!spawn) {
  5837. lua_interface->LogError("%s: LUA RemoveSpawnIDAccess command error: spawn is not valid", lua_interface->GetScriptName(state));
  5838. return 0;
  5839. }
  5840. //If zone not provided, use spawn's zone
  5841. if (!zone)
  5842. zone = spawn->GetZone();
  5843. vector<Spawn*> list = zone->GetSpawnsByID(id);
  5844. vector<Spawn*>::iterator itr = list.begin();
  5845. if (list.size() == 0) {
  5846. lua_interface->LogError("%s: LUA RemoveSpawnIDAccess command error: GetSpawnsByID returned no spawns", lua_interface->GetScriptName(state));
  5847. return 0;
  5848. }
  5849. for (int8 i = 0; i < list.size(); i++) {
  5850. spawn2 = itr[i];
  5851. if (spawn2)
  5852. spawn2->RemoveSpawnAccess(spawn);
  5853. }
  5854. return 0;
  5855. }
  5856. int EQ2Emu_lua_SetQuestYellow(lua_State* state) {
  5857. if (!lua_interface)
  5858. return 0;
  5859. Quest* quest = lua_interface->GetQuest(state);
  5860. if (!quest) {
  5861. lua_interface->LogError("%s: LUA SetQuestYellow command error: quest is not valid", lua_interface->GetScriptName(state));
  5862. return 0;
  5863. }
  5864. quest->SetYellowName(true);
  5865. return 0;
  5866. }
  5867. int EQ2Emu_lua_CanReceiveQuest(lua_State* state) {
  5868. if (!lua_interface)
  5869. return 0;
  5870. Spawn* spawn = lua_interface->GetSpawn(state);
  5871. int32 quest_id = lua_interface->GetInt32Value(state, 2);
  5872. if (!spawn) {
  5873. lua_interface->LogError("%s: LUA CanReceieveQuest command error: spawn is not valid", lua_interface->GetScriptName(state));
  5874. return 0;
  5875. }
  5876. if (!spawn->IsPlayer()) {
  5877. lua_interface->LogError("%s: LUA CanReceieveQuest command error: spawn is not player", lua_interface->GetScriptName(state));
  5878. return 0;
  5879. }
  5880. lua_interface->SetBooleanValue(state, ((Player*)spawn)->CanReceiveQuest(quest_id));
  5881. return 1;
  5882. }
  5883. int EQ2Emu_lua_SetSuccessTimer(lua_State* state) {
  5884. if (!lua_interface)
  5885. return 0;
  5886. Spawn* spawn = lua_interface->GetSpawn(state);
  5887. if (!spawn) {
  5888. lua_interface->LogError("%s: LUA SetSuccessTimer command error: spawn is not valid", lua_interface->GetScriptName(state));
  5889. return 0;
  5890. }
  5891. if (!spawn->IsPlayer()) {
  5892. lua_interface->LogError("%s: LUA SetSuccessTimer command error: spawn is not a player", lua_interface->GetScriptName(state));
  5893. return 0;
  5894. }
  5895. ZoneServer* zone = spawn->GetZone();
  5896. if (!zone) {
  5897. lua_interface->LogError("%s: LUA SetSuccessTimer command error: unable to get a valid zone for the given spawn", lua_interface->GetScriptName(state));
  5898. return 0;
  5899. }
  5900. Instance_Type iType = zone->GetInstanceType();
  5901. if (iType == SOLO_LOCKOUT_INSTANCE ||
  5902. iType == GROUP_LOCKOUT_INSTANCE ||
  5903. iType == RAID_LOCKOUT_INSTANCE ||
  5904. iType == SOLO_PERSIST_INSTANCE ||
  5905. iType == GROUP_PERSIST_INSTANCE ||
  5906. iType == RAID_PERSIST_INSTANCE) {
  5907. InstanceData* data = ((Player*)spawn)->GetCharacterInstances()->FindInstanceByZoneID(spawn->GetZone()->GetZoneID());
  5908. if (data) {
  5909. // Check to see if the timer has already been set, if it has return out.
  5910. if (Timer::GetUnixTimeStamp() <= data->last_success_timestamp + data->success_lockout_time)
  5911. return 0;
  5912. database.UpdateCharacterInstance(((Player*)spawn)->GetCharacterID(), string(spawn->GetZone()->GetZoneName()), spawn->GetZone()->GetInstanceID(), 1, Timer::GetUnixTimeStamp());
  5913. data->last_success_timestamp = Timer::GetUnixTimeStamp();
  5914. Client* client = spawn->GetZone()->GetClientBySpawn(spawn);
  5915. if (client) {
  5916. string time_msg = "";
  5917. int32 time = data->success_lockout_time;
  5918. int16 hour;
  5919. int8 min;
  5920. int8 sec;
  5921. hour = time / 3600;
  5922. time = time % 3600;
  5923. min = time / 60;
  5924. time = time % 60;
  5925. sec = time;
  5926. if (hour > 0) {
  5927. char temp[10];
  5928. sprintf(temp, " %i", hour);
  5929. time_msg.append(temp);
  5930. time_msg.append(" hour");
  5931. time_msg.append((hour > 1) ? "s" : "");
  5932. }
  5933. if (min > 0) {
  5934. char temp[5];
  5935. sprintf(temp, " %i", min);
  5936. time_msg.append(temp);
  5937. time_msg.append(" minute");
  5938. time_msg.append((min > 1) ? "s" : "");
  5939. }
  5940. // Only add seconds if minutes and hours are 0
  5941. if (hour == 0 && min == 0 && sec > 0) {
  5942. char temp[5];
  5943. sprintf(temp, " %i", sec);
  5944. time_msg.append(temp);
  5945. time_msg.append(" second");
  5946. time_msg.append((sec > 1) ? "s" : "");
  5947. }
  5948. 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());
  5949. }
  5950. }
  5951. else
  5952. lua_interface->LogError("LUA SetSuccessTimer command error: unable to get instance data for instance %u for player %s", spawn->GetZone()->GetInstanceID(), spawn->GetName());
  5953. }
  5954. else
  5955. lua_interface->LogError("%s: LUA SetSuccessTimer command error: current zone for given spawn is not a lockout or persistent instance", lua_interface->GetScriptName(state));
  5956. return 0;
  5957. }
  5958. int EQ2Emu_lua_SetFailureTimer(lua_State* state) {
  5959. if (!lua_interface)
  5960. return 0;
  5961. Spawn* spawn = lua_interface->GetSpawn(state);
  5962. if (!spawn) {
  5963. lua_interface->LogError("%s: LUA SetFailureTimer command error: spawn is not valid", lua_interface->GetScriptName(state));
  5964. return 0;
  5965. }
  5966. if (!spawn->IsPlayer()) {
  5967. lua_interface->LogError("%s: LUA SetFailureTimer command error: spawn is not a player", lua_interface->GetScriptName(state));
  5968. return 0;
  5969. }
  5970. ZoneServer* zone = spawn->GetZone();
  5971. if (!zone) {
  5972. lua_interface->LogError("%s: LUA SetFailureTimer command error: unable to get a valid zone for the given spawn", lua_interface->GetScriptName(state));
  5973. return 0;
  5974. }
  5975. Instance_Type iType = zone->GetInstanceType();
  5976. if (iType == SOLO_LOCKOUT_INSTANCE ||
  5977. iType == GROUP_LOCKOUT_INSTANCE ||
  5978. iType == RAID_LOCKOUT_INSTANCE ||
  5979. iType == SOLO_PERSIST_INSTANCE ||
  5980. iType == GROUP_PERSIST_INSTANCE ||
  5981. iType == RAID_PERSIST_INSTANCE) {
  5982. InstanceData* data = ((Player*)spawn)->GetCharacterInstances()->FindInstanceByZoneID(spawn->GetZone()->GetZoneID());
  5983. if (data) {
  5984. // Check to see if the timer has already been set, if it has return out.
  5985. if (Timer::GetUnixTimeStamp() <= data->last_failure_timestamp + data->failure_lockout_time)
  5986. return 0;
  5987. database.UpdateCharacterInstance(((Player*)spawn)->GetCharacterID(), string(spawn->GetZone()->GetZoneName()), spawn->GetZone()->GetInstanceID(), 2, Timer::GetUnixTimeStamp());
  5988. data->last_failure_timestamp = Timer::GetUnixTimeStamp();
  5989. Client* client = spawn->GetZone()->GetClientBySpawn(spawn);
  5990. if (client) {
  5991. string time_msg = "";
  5992. int32 time = data->failure_lockout_time;
  5993. int16 hour;
  5994. int8 min;
  5995. int8 sec;
  5996. hour = time / 3600;
  5997. time = time % 3600;
  5998. min = time / 60;
  5999. time = time % 60;
  6000. sec = time;
  6001. if (hour > 0) {
  6002. char temp[10];
  6003. sprintf(temp, " %i", hour);
  6004. time_msg.append(temp);
  6005. time_msg.append(" hour");
  6006. time_msg.append((hour > 1) ? "s" : "");
  6007. }
  6008. if (min > 0) {
  6009. char temp[5];
  6010. sprintf(temp, " %i", min);
  6011. time_msg.append(temp);
  6012. time_msg.append(" minute");
  6013. time_msg.append((min > 1) ? "s" : "");
  6014. }
  6015. // Only add seconds if minutes and hours are 0
  6016. if (hour == 0 && min == 0 && sec > 0) {
  6017. char temp[5];
  6018. sprintf(temp, " %i", sec);
  6019. time_msg.append(temp);
  6020. time_msg.append(" second");
  6021. time_msg.append((sec > 1) ? "s" : "");
  6022. }
  6023. 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());
  6024. }
  6025. }
  6026. else
  6027. lua_interface->LogError("LUA SetFailureTimer command error: unable to get instance data for instance %u for player %s", spawn->GetZone()->GetInstanceID(), spawn->GetName());
  6028. }
  6029. else
  6030. lua_interface->LogError("%s: LUA SetFailureTimer command error: current zone for given spawn is not a lockout or persistent instance", lua_interface->GetScriptName(state));
  6031. return 0;
  6032. }
  6033. int EQ2Emu_lua_IsGroundSpawn(lua_State* state) {
  6034. if (!lua_interface)
  6035. return 0;
  6036. Spawn* spawn = lua_interface->GetSpawn(state);
  6037. if (!spawn) {
  6038. lua_interface->LogError("%s: LUA IsGroundSpawn command error: not a valid spawn", lua_interface->GetScriptName(state));
  6039. return 0;
  6040. }
  6041. lua_interface->SetBooleanValue(state, spawn->IsGroundSpawn());
  6042. return 1;
  6043. }
  6044. int EQ2Emu_lua_CanHarvest(lua_State* state) {
  6045. if (!lua_interface)
  6046. return 0;
  6047. Spawn* player = lua_interface->GetSpawn(state);
  6048. Spawn* ground = lua_interface->GetSpawn(state, 2);
  6049. if (!player) {
  6050. lua_interface->LogError("%s: LUA CanHarvest command error: not a valid spawn", lua_interface->GetScriptName(state));
  6051. return 0;
  6052. }
  6053. if (!player->IsPlayer()) {
  6054. lua_interface->LogError("%s: LUA CanHarvest command error: spawn is not a player", lua_interface->GetScriptName(state));
  6055. return 0;
  6056. }
  6057. if (!ground) {
  6058. lua_interface->LogError("%s: LUA CanHarvest command error: not a valid spawn", lua_interface->GetScriptName(state));
  6059. return 0;
  6060. }
  6061. if (!ground->IsGroundSpawn()) {
  6062. lua_interface->LogError("%s: LUA CanHarvest command error: spawn is not a ground spawn", lua_interface->GetScriptName(state));
  6063. return 0;
  6064. }
  6065. vector<GroundSpawnEntry*>* groundspawn_entries = player->GetZone()->GetGroundSpawnEntries(((GroundSpawn*)ground)->GetGroundSpawnEntryID());
  6066. if (!groundspawn_entries) {
  6067. lua_interface->LogError("LUA CanHarvest command error: No groundspawn entries assigned to groundspawn id: %u", ((GroundSpawn*)ground)->GetGroundSpawnEntryID());
  6068. return 0;
  6069. }
  6070. Skill* skill = 0;
  6071. string collection_skill = string(((GroundSpawn*)ground)->GetCollectionSkill());
  6072. if (collection_skill == "Collecting")
  6073. skill = ((Player*)player)->GetSkillByName("Gathering");
  6074. else
  6075. skill = ((Player*)player)->GetSkillByName(collection_skill.c_str());
  6076. if (!skill) {
  6077. lua_interface->LogError("LUA CanHarvest command error: Player '%s' lacks the skill: '%s'", player->GetName(), collection_skill.c_str());
  6078. return 0;
  6079. }
  6080. vector<GroundSpawnEntry*>::iterator itr;
  6081. GroundSpawnEntry* entry = 0;
  6082. bool can_harvest = false;
  6083. sint32 min_skill = -1;
  6084. // first, iterate through groundspawn_entries, discard tables player cannot use
  6085. for (itr = groundspawn_entries->begin(); itr != groundspawn_entries->end(); itr++)
  6086. {
  6087. entry = *itr;
  6088. if (min_skill == -1 || entry->min_skill_level < min_skill)
  6089. min_skill = entry->min_skill_level;
  6090. // if player lacks skill, skip table
  6091. if (entry->min_skill_level > skill->current_val)
  6092. continue;
  6093. // if bonus, but player lacks level, skip table
  6094. if (entry->bonus_table && (player->GetLevel() < entry->min_adventure_level))
  6095. continue;
  6096. can_harvest = true;
  6097. break;
  6098. }
  6099. lua_interface->SetBooleanValue(state, can_harvest);
  6100. // If false, send the message to the client
  6101. if (!can_harvest) {
  6102. Client* client = player->GetZone()->GetClientBySpawn(player);
  6103. if (client) {
  6104. string msg = "You do not have enough skill to ";
  6105. if (collection_skill == "Gathering" || collection_skill == "Collecting")
  6106. msg.append("gather");
  6107. else if (collection_skill == "Mining")
  6108. msg.append("mine");
  6109. else if (collection_skill == "Trapping")
  6110. msg.append("trap");
  6111. else if (collection_skill == "Foresting")
  6112. msg.append("forest");
  6113. else if (collection_skill == "Fishing")
  6114. msg.append("catch");
  6115. msg.append(" the %s. It requires %i %s skill, and your skill is %i.");
  6116. client->Message(CHANNEL_HARVESTING_WARNINGS, msg.c_str(), ground->GetName(), min_skill, skill->name.data.c_str(), skill->current_val);
  6117. // You do not have enough skill to catch the band of fish. It requires 20 Fishing skill, and your skill is 12.
  6118. }
  6119. }
  6120. return 1;
  6121. }
  6122. int EQ2Emu_lua_HasRecipeBook(lua_State* state) {
  6123. if (!lua_interface)
  6124. return 0;
  6125. Spawn* player = lua_interface->GetSpawn(state);
  6126. int32 recipe_id = lua_interface->GetInt32Value(state, 2);
  6127. if (!player) {
  6128. lua_interface->LogError("%s: LUA HasRecipeBook command error, Spawn is not valid", lua_interface->GetScriptName(state));
  6129. return 0;
  6130. }
  6131. if (!player->IsPlayer()) {
  6132. lua_interface->LogError("%s: LUA HasRecipeBook command error, Spawn is not a player", lua_interface->GetScriptName(state));
  6133. return 0;
  6134. }
  6135. bool ret = ((Player*)player)->HasRecipeBook(recipe_id);
  6136. lua_interface->SetBooleanValue(state, ret);
  6137. return 1;
  6138. }
  6139. int EQ2Emu_lua_SummonDumbFirePet(lua_State* state) {
  6140. // Check to see if we have a valid lua_interface
  6141. if (!lua_interface)
  6142. return 0;
  6143. // Get the spawn that is getting the pet
  6144. Spawn* spawn = lua_interface->GetSpawn(state);
  6145. Spawn* target = lua_interface->GetSpawn(state, 2);
  6146. // Get the DB ID of the pet
  6147. int32 pet_id = lua_interface->GetInt32Value(state, 3);
  6148. float x = lua_interface->GetFloatValue(state, 4);
  6149. float y = lua_interface->GetFloatValue(state, 5);
  6150. float z = lua_interface->GetFloatValue(state, 6);
  6151. // Get the spell that this command was called from
  6152. LuaSpell* luaspell = lua_interface->GetCurrentSpell(state);
  6153. // Check to make sure the spawn pointer is valid
  6154. if (!spawn) {
  6155. lua_interface->LogError("%s: LUA SummonDumbFirePet command error: Spawn is not valid", lua_interface->GetScriptName(state));
  6156. return 0;
  6157. }
  6158. // Check to make sure the spawn is an entity
  6159. if (!spawn->IsEntity()) {
  6160. lua_interface->LogError("%s: LUA SummonDumbFirePet command error: Spawn is not an entity", lua_interface->GetScriptName(state));
  6161. return 0;
  6162. }
  6163. if (!target) {
  6164. lua_interface->LogError("%s: LUA SummonDumbFirePet command error: target is not valid", lua_interface->GetScriptName(state));
  6165. return 0;
  6166. }
  6167. if (!target->IsEntity()) {
  6168. lua_interface->LogError("%s: LUA SummonDumbFirePet command error: target is not an entity", lua_interface->GetScriptName(state));
  6169. return 0;
  6170. }
  6171. // Check to see if the DB ID for the pet is set
  6172. if (pet_id == 0) {
  6173. lua_interface->LogError("%s: LUA SummonDumbFirePet command error: pet_id can not be set to 0", lua_interface->GetScriptName(state));
  6174. return 0;
  6175. }
  6176. // Check to see if the pointer to the spell is valid
  6177. if (!luaspell) {
  6178. lua_interface->LogError("%s: LUA SummonDumbFirePet command error: valid spell not found, SummonPet can only be used in spell scripts", lua_interface->GetScriptName(state));
  6179. return 0;
  6180. }
  6181. // Get a pointer to a spawn with the given DB ID and check if the pointer is valid
  6182. Spawn* pet = spawn->GetZone()->GetSpawn(pet_id);
  6183. if (!pet) {
  6184. lua_interface->LogError("LUA SummonDumbFirePet command error: Could not find spawn with id of %u.", pet_id);
  6185. return 0;
  6186. }
  6187. // Check to make sure the pet is an npc
  6188. if (!pet->IsNPC()) {
  6189. lua_interface->LogError("LUA SummonDumbFirePet command error: id (%u) did not point to a npc", pet_id);
  6190. return 0;
  6191. }
  6192. if (x == 0)
  6193. x = spawn->GetX();
  6194. if (y == 0)
  6195. y = spawn->GetY();
  6196. if (z == 0)
  6197. z = spawn->GetZ();
  6198. // Spawn the pet at the same location as the owner
  6199. pet->SetX(x);
  6200. pet->SetY(y);
  6201. pet->SetZ(z);
  6202. pet->SetLocation(spawn->GetLocation());
  6203. pet->SetHeading(spawn->GetHeading());
  6204. spawn->GetZone()->AddSpawn(pet);
  6205. /*
  6206. const char* spawn_script = world.GetSpawnScript(pet_id);
  6207. if(spawn_script && lua_interface->GetSpawnScript(spawn_script) != 0){
  6208. spawn->SetSpawnScript(string(spawn_script));
  6209. zone->CallSpawnScript(spawn, SPAWN_SCRIPT_SPAWN);
  6210. }*/
  6211. // Get a random pet name
  6212. string random_pet_name;
  6213. int16 rand_index = MakeRandomInt(0, spawn->GetZone()->pet_names.size() - 1);
  6214. random_pet_name = spawn->GetZone()->pet_names.at(rand_index);
  6215. LogWrite(PET__DEBUG, 0, "Pets", "Randomize Pet Name: '%s' (rand: %i)", random_pet_name.c_str(), rand_index);
  6216. // Set the pets name
  6217. pet->SetName(random_pet_name.c_str());
  6218. // Set the level of the pet to the owners level
  6219. pet->SetLevel(spawn->GetLevel());
  6220. // Set the faction of the pet to the same faction as the owner
  6221. pet->SetFactionID(spawn->GetFactionID());
  6222. // Set the spawn as a pet
  6223. pet->SetPet(true);
  6224. // Give a pointer of the owner to the pet
  6225. ((NPC*)pet)->SetOwner((Entity*)spawn);
  6226. // Set the pet type
  6227. ((NPC*)pet)->SetPetType(PET_TYPE_DUMBFIRE);
  6228. // Set the spell id used to create this pet
  6229. ((NPC*)pet)->SetPetSpellID(luaspell->spell->GetSpellData()->id);
  6230. // Set the spell tier used to create this pet
  6231. ((NPC*)pet)->SetPetSpellTier(luaspell->spell->GetSpellData()->tier);
  6232. // Set the pets spawn type to 6
  6233. pet->SetSpawnType(6);
  6234. // Set the pets brain
  6235. ((NPC*)pet)->SetBrain(new DumbFirePetBrain((NPC*)pet, (Entity*)target, luaspell->spell->GetSpellDuration() * 100));
  6236. // Check to see if the pet has a subtitle
  6237. if (strlen(pet->GetSubTitle()) > 0) {
  6238. // Add the players name to the front of the sub title
  6239. string pet_subtitle;
  6240. pet_subtitle.append(spawn->GetName()).append("'s ").append(pet->GetSubTitle());
  6241. LogWrite(PET__DEBUG, 0, "Pets", "Pet Subtitle: '%s'", pet_subtitle.c_str());
  6242. // Set the pets subtitle to the new one
  6243. pet->SetSubTitle(pet_subtitle.c_str());
  6244. }
  6245. // Set the pet as the return value for this function
  6246. lua_interface->SetSpawnValue(state, pet);
  6247. return 1;
  6248. }
  6249. int EQ2Emu_lua_SpawnMove(lua_State* state) {
  6250. if (!lua_interface)
  6251. return 0;
  6252. Spawn* spawn = lua_interface->GetSpawn(state);
  6253. Spawn* player = lua_interface->GetSpawn(state, 2);
  6254. float max_distance = lua_interface->GetFloatValue(state, 3);
  6255. string type = lua_interface->GetStringValue(state, 4);
  6256. if (!spawn || (spawn && spawn->IsPlayer())) {
  6257. lua_interface->LogError("%s: LUA SpawnMove command error: first param spawn is not valid or is player", lua_interface->GetScriptName(state));
  6258. return 0;
  6259. }
  6260. if (!player || (player && !player->IsPlayer())) {
  6261. lua_interface->LogError("%s: LUA SpawnMove command error: second param is not player", lua_interface->GetScriptName(state));
  6262. return 0;
  6263. }
  6264. Client* client = 0;
  6265. if (player->GetZone())
  6266. client = player->GetZone()->GetClientBySpawn(player);
  6267. if (!client) {
  6268. lua_interface->LogError("%s: LUA SpawnMove command error: could not find client", lua_interface->GetScriptName(state));
  6269. return 0;
  6270. }
  6271. //Set max_distance to default if not set or not proper value
  6272. if (max_distance <= 0)
  6273. max_distance = 500;
  6274. PacketStruct* packet = configReader.getStruct("WS_MoveObjectMode", client->GetVersion());
  6275. if (packet) {
  6276. float unknown2_3 = 0;
  6277. int8 placement_mode = 0;
  6278. if (type == "wall") {
  6279. placement_mode = 2;
  6280. unknown2_3 = 150;
  6281. }
  6282. else if (type == "ceiling")
  6283. placement_mode = 1;
  6284. packet->setDataByName("placement_mode", placement_mode);
  6285. packet->setDataByName("spawn_id", ((Player*)player)->GetIDWithPlayerSpawn(spawn));
  6286. packet->setDataByName("model_type", spawn->GetModelType());
  6287. packet->setDataByName("unknown", 1); //size
  6288. packet->setDataByName("unknown2", 1); //size 2
  6289. packet->setDataByName("unknown2", .5, 1); //size 3
  6290. packet->setDataByName("unknown2", 3, 2);
  6291. packet->setDataByName("unknown2", unknown2_3, 3);
  6292. packet->setDataByName("max_distance", max_distance);
  6293. packet->setDataByName("CoEunknown", 0xFFFFFFFF);
  6294. client->QueuePacket(packet->serialize());
  6295. safe_delete(packet);
  6296. }
  6297. return 0;
  6298. }
  6299. int EQ2Emu_lua_GetItemType(lua_State* state) {
  6300. if (!lua_interface)
  6301. return 0;
  6302. Item* item = lua_interface->GetItem(state);
  6303. if (!item) {
  6304. lua_interface->LogError("%s: LUA GetItemType command error: item pointer is not valid", lua_interface->GetScriptName(state));
  6305. return 0;
  6306. }
  6307. lua_interface->SetInt32Value(state, item->generic_info.item_type);
  6308. return 1;
  6309. }
  6310. int EQ2Emu_lua_AddTransportSpawn(lua_State* state) {
  6311. if (!lua_interface)
  6312. return 0;
  6313. Spawn* spawn = lua_interface->GetSpawn(state);
  6314. if (!spawn) {
  6315. lua_interface->LogError("%s: LUA AddTransportSpawn command error: spawn is not valid", lua_interface->GetScriptName(state));
  6316. return 0;
  6317. }
  6318. if (spawn->GetZone())
  6319. spawn->GetZone()->AddTransportSpawn(spawn);
  6320. return 0;
  6321. }
  6322. int EQ2Emu_lua_GetSkillValue(lua_State* state) {
  6323. if (!lua_interface)
  6324. return 0;
  6325. Skill* skill = lua_interface->GetSkill(state);
  6326. if (!skill) {
  6327. lua_interface->LogError("%s: LUA GetSkillValue command error: skill is not valid", lua_interface->GetScriptName(state));
  6328. return 0;
  6329. }
  6330. lua_interface->SetInt32Value(state, skill->current_val);
  6331. return 1;
  6332. }
  6333. int EQ2Emu_lua_GetSkillMaxValue(lua_State* state) {
  6334. if (!lua_interface)
  6335. return 0;
  6336. Skill* skill = lua_interface->GetSkill(state);
  6337. if (!skill) {
  6338. lua_interface->LogError("%s: LUA GetSkillMaxValue command error: skill is not valid", lua_interface->GetScriptName(state));
  6339. return 0;
  6340. }
  6341. lua_interface->SetInt32Value(state, skill->max_val);
  6342. return 1;
  6343. }
  6344. int EQ2Emu_lua_GetSkillName(lua_State* state) {
  6345. if (!lua_interface)
  6346. return 0;
  6347. Skill* skill = lua_interface->GetSkill(state);
  6348. if (!skill) {
  6349. lua_interface->LogError("%s: LUA GetSkillName command error: skill is not valid", lua_interface->GetScriptName(state));
  6350. return 0;
  6351. }
  6352. lua_interface->SetStringValue(state, skill->name.data.c_str());
  6353. return 1;
  6354. }
  6355. int EQ2Emu_lua_SetSkillMaxValue(lua_State* state) {
  6356. if (!lua_interface)
  6357. return 0;
  6358. Skill* skill = lua_interface->GetSkill(state);
  6359. int16 value = lua_interface->GetInt16Value(state, 2);
  6360. if (!skill) {
  6361. lua_interface->LogError("%s: LUA SetSkillMaxValue command error: skill is not valid", lua_interface->GetScriptName(state));
  6362. return 0;
  6363. }
  6364. skill->max_val = value;
  6365. if (skill->max_val < skill->current_val)
  6366. skill->current_val = skill->max_val;
  6367. return 0;
  6368. }
  6369. int EQ2Emu_lua_SetSkillValue(lua_State* state) {
  6370. if (!lua_interface)
  6371. return 0;
  6372. Skill* skill = lua_interface->GetSkill(state);
  6373. int16 value = lua_interface->GetInt16Value(state, 2);
  6374. if (!skill) {
  6375. lua_interface->LogError("%s: LUA SetSkillValue command error: skill is not valid", lua_interface->GetScriptName(state));
  6376. return 0;
  6377. }
  6378. if (value > skill->max_val)
  6379. skill->current_val = skill->max_val;
  6380. else
  6381. skill->current_val = value;
  6382. return 0;
  6383. }
  6384. int EQ2Emu_lua_GetSkill(lua_State* state) {
  6385. if (!lua_interface)
  6386. return 0;
  6387. Spawn* spawn = lua_interface->GetSpawn(state);
  6388. string name = lua_interface->GetStringValue(state, 2);
  6389. if (!spawn) {
  6390. lua_interface->LogError("%s: LUA GetSkill command error: spawn is not valid", lua_interface->GetScriptName(state));
  6391. return 0;
  6392. }
  6393. if (!spawn->IsEntity()) {
  6394. lua_interface->LogError("%s: LUA GetSkill command error: spawn is not a valid entity", lua_interface->GetScriptName(state));
  6395. return 0;
  6396. }
  6397. Skill* skill = ((Entity*)spawn)->GetSkillByName(name.c_str());
  6398. if (skill) {
  6399. lua_interface->SetSkillValue(state, skill);
  6400. return 1;
  6401. }
  6402. return 0;
  6403. }
  6404. int EQ2Emu_lua_AddProc(lua_State* state) {
  6405. if (!lua_interface)
  6406. return 0;
  6407. Spawn* spawn = lua_interface->GetSpawn(state);
  6408. int8 type = lua_interface->GetInt8Value(state, 2);
  6409. float chance = lua_interface->GetFloatValue(state, 3);
  6410. Item* item = lua_interface->GetItem(state, 4);
  6411. bool use_all_spelltargets = (lua_interface->GetInt8Value(state, 5) == 1);
  6412. LuaSpell* spell = 0;
  6413. if (!spawn && (!spell || !use_all_spelltargets)) {
  6414. lua_interface->LogError("%s: LUA AddProc command error: spawn is not valid", lua_interface->GetScriptName(state));
  6415. return 0;
  6416. }
  6417. if ((!spell || use_all_spelltargets) && spawn && !spawn->IsEntity()) {
  6418. lua_interface->LogError("%s: LUA AddProc command error: spawn is not a valid entity", lua_interface->GetScriptName(state));
  6419. return 0;
  6420. }
  6421. if (!item)
  6422. spell = lua_interface->GetCurrentSpell(state);
  6423. if (!item && !spell) {
  6424. lua_interface->LogError("%s: LUA AddProc command error: can only use with an item provided or inside a spell script", lua_interface->GetScriptName(state));
  6425. return 0;
  6426. }
  6427. if (spell && use_all_spelltargets) {
  6428. Spawn* target;
  6429. spell->MSpellTargets.readlock(__FUNCTION__, __LINE__);
  6430. for (int8 i = 0; i < spell->targets.size(); i++) {
  6431. target = spell->caster->GetZone()->GetSpawnByID(spell->targets.at(i));
  6432. if (!target || !target->IsEntity())
  6433. continue;
  6434. ((Entity*)target)->AddProc(type, chance, item, spell);
  6435. }
  6436. spell->MSpellTargets.releasereadlock(__FUNCTION__, __LINE__);
  6437. }
  6438. else
  6439. ((Entity*)spawn)->AddProc(type, chance, item, spell);
  6440. return 0;
  6441. }
  6442. int EQ2Emu_lua_RemoveProc(lua_State* state) {
  6443. if (!lua_interface)
  6444. return 0;
  6445. Spawn* spawn = lua_interface->GetSpawn(state);
  6446. Item* item = lua_interface->GetItem(state, 2);
  6447. LuaSpell* spell = 0;
  6448. if (!spawn) {
  6449. lua_interface->LogError("%s: LUA RemoveProc command error: spawn is not valid", lua_interface->GetScriptName(state));
  6450. return 0;
  6451. }
  6452. if (!spawn->IsEntity()) {
  6453. lua_interface->LogError("%s: LUA RemoveProc command error: spawn is not a valid entity", lua_interface->GetScriptName(state));
  6454. return 0;
  6455. }
  6456. if (!item)
  6457. spell = lua_interface->GetCurrentSpell(state);
  6458. if (!item && !spell) {
  6459. lua_interface->LogError("%s: LUA RemoveProc command error: can only use with an item provided or inside a spell script", lua_interface->GetScriptName(state));
  6460. return 0;
  6461. }
  6462. if (spell) {
  6463. Spawn* target;
  6464. spell->MSpellTargets.readlock(__FUNCTION__, __LINE__);
  6465. for (int8 i = 0; i < spell->targets.size(); i++) {
  6466. target = spell->caster->GetZone()->GetSpawnByID(spell->targets.at(i));
  6467. if (!target || !target->IsEntity())
  6468. continue;
  6469. ((Entity*)target)->RemoveProc(item, spell);
  6470. }
  6471. spell->MSpellTargets.releasereadlock(__FUNCTION__, __LINE__);
  6472. spell->caster->RemoveProc(item, spell);
  6473. }
  6474. else
  6475. ((Entity*)spawn)->RemoveProc(item, spell);
  6476. return 0;
  6477. }
  6478. int EQ2Emu_lua_Knockback(lua_State* state) {
  6479. if (!lua_interface)
  6480. return 0;
  6481. Spawn* target_spawn = lua_interface->GetSpawn(state);
  6482. Spawn* spawn = lua_interface->GetSpawn(state, 2);
  6483. int32 duration = lua_interface->GetInt32Value(state, 3);
  6484. float vertical = lua_interface->GetFloatValue(state, 4);
  6485. float horizontal = lua_interface->GetFloatValue(state, 5);
  6486. bool use_heading = lua_interface->GetInt8Value(state, 6) == 1 ? true : false;
  6487. LuaSpell* spell = lua_interface->GetCurrentSpell(state);
  6488. if (!target_spawn) {
  6489. lua_interface->LogError("%s: LUA Knockback command error: target_spawn is not valid", lua_interface->GetScriptName(state));
  6490. return 0;
  6491. }
  6492. if (!spawn) {
  6493. lua_interface->LogError("%s: LUA Knockback command error: spawn is not valid", lua_interface->GetScriptName(state));
  6494. return 0;
  6495. }
  6496. if (spawn->IsPlayer() && (vertical != 0 || horizontal != 0)) {
  6497. Client* client = spawn->GetZone()->GetClientBySpawn(spawn);
  6498. PacketStruct* packet = configReader.getStruct("WS_PlayerKnockback", client->GetVersion());
  6499. if (packet) {
  6500. packet->setDataByName("target_x", target_spawn->GetX());
  6501. packet->setDataByName("target_y", target_spawn->GetY());
  6502. packet->setDataByName("target_z", target_spawn->GetZ());
  6503. packet->setDataByName("vertical_movement", vertical);
  6504. packet->setDataByName("horizontal_movement", horizontal);
  6505. if (use_heading)
  6506. packet->setDataByName("use_player_heading", 1);
  6507. client->QueuePacket(packet->serialize());
  6508. }
  6509. safe_delete(packet);
  6510. }
  6511. return 0;
  6512. }
  6513. int EQ2Emu_lua_IsEpic(lua_State* state) {
  6514. if (!lua_interface)
  6515. return 0;
  6516. Spawn* spawn = lua_interface->GetSpawn(state);
  6517. if (!spawn) {
  6518. lua_interface->LogError("%s: LUA IsEpic command error: spawn is not valid", lua_interface->GetScriptName(state));
  6519. return 0;
  6520. }
  6521. lua_interface->SetBooleanValue(state, (spawn->GetHeroic() >= 2));
  6522. return 1;
  6523. }
  6524. int EQ2Emu_lua_ProcDamage(lua_State* state) {
  6525. if (!lua_interface)
  6526. return 0;
  6527. Spawn* caster = lua_interface->GetSpawn(state);
  6528. Spawn* target = lua_interface->GetSpawn(state, 2);
  6529. string name = lua_interface->GetStringValue(state, 3);
  6530. int8 dmg_type = lua_interface->GetInt8Value(state, 4);
  6531. int32 low_damage = lua_interface->GetInt32Value(state, 5);
  6532. int32 high_damage = lua_interface->GetInt32Value(state, 6);
  6533. string success_msg = lua_interface->GetStringValue(state, 7);
  6534. string effect_msg = lua_interface->GetStringValue(state, 8);
  6535. if (!caster) {
  6536. lua_interface->LogError("%s: LUA ProcDamage command error: caster is not a valid spawn", lua_interface->GetScriptName(state));
  6537. return 0;
  6538. }
  6539. if (!caster->IsEntity()) {
  6540. lua_interface->LogError("%s: LUA ProcDamage command error: caster is not an entity", lua_interface->GetScriptName(state));
  6541. return 0;
  6542. }
  6543. if (!target) {
  6544. lua_interface->LogError("%s: LUA ProcDamage command error: target is not a valid spawn", lua_interface->GetScriptName(state));
  6545. return 0;
  6546. }
  6547. if (!target->IsEntity()) {
  6548. lua_interface->LogError("%s: LUA ProcDamage command error: target is not an entity", lua_interface->GetScriptName(state));
  6549. return 0;
  6550. }
  6551. if (name.length() == 0) {
  6552. lua_interface->LogError("%s: LUA ProcDamage command error: name is empty", lua_interface->GetScriptName(state));
  6553. return 0;
  6554. }
  6555. ((Entity*)caster)->ProcAttack(target, dmg_type, low_damage, high_damage, name, success_msg, effect_msg);
  6556. return 0;
  6557. }
  6558. int EQ2Emu_lua_GetSkillIDByName(lua_State* state) {
  6559. if (!lua_interface)
  6560. return 0;
  6561. string name = lua_interface->GetStringValue(state);
  6562. if (name.length() == 0) {
  6563. lua_interface->LogError("%s: LUA GetSkillIDByName command error: name param was not set", lua_interface->GetScriptName(state));
  6564. return 0;
  6565. }
  6566. Skill* skill = master_skill_list.GetSkillByName(name.c_str());
  6567. if (!skill) {
  6568. lua_interface->LogError("LUA GetSkillIDByName command error: skill with name of %s not found", name.c_str());
  6569. return 0;
  6570. }
  6571. lua_interface->SetInt32Value(state, skill->skill_id);
  6572. return 1;
  6573. }
  6574. int EQ2Emu_lua_IsHeroic(lua_State* state) {
  6575. if (!lua_interface)
  6576. return 0;
  6577. Spawn* spawn = lua_interface->GetSpawn(state);
  6578. if (!spawn) {
  6579. lua_interface->LogError("%s: LUA IsHeroic command error: spawn is not valid", lua_interface->GetScriptName(state));
  6580. return 0;
  6581. }
  6582. lua_interface->SetBooleanValue(state, spawn->GetHeroic() == 1);
  6583. return 1;
  6584. }
  6585. int EQ2Emu_lua_LastSpellAttackHit(lua_State* state) {
  6586. if (!lua_interface)
  6587. return 0;
  6588. LuaSpell* luaspell = lua_interface->GetCurrentSpell(state);
  6589. if (!luaspell) {
  6590. lua_interface->LogError("%s: LUA LastSpellAttackHit command error: this must be called from a spellscript", lua_interface->GetScriptName(state));
  6591. return 0;
  6592. }
  6593. lua_interface->SetBooleanValue(state, luaspell->last_spellattack_hit);
  6594. return 1;
  6595. }
  6596. int EQ2Emu_lua_IsBehind(lua_State* state) {
  6597. if (!lua_interface)
  6598. return 0;
  6599. Spawn* spawn = lua_interface->GetSpawn(state);
  6600. Spawn* target = lua_interface->GetSpawn(state, 2);
  6601. if (!spawn) {
  6602. lua_interface->LogError("%s: LUA IsBehind command error: spawn is not valid", lua_interface->GetScriptName(state));
  6603. return 0;
  6604. }
  6605. if (!spawn->IsEntity()) {
  6606. lua_interface->LogError("%s: LUA IsBehind command error: spawn is not an entity", lua_interface->GetScriptName(state));
  6607. return 0;
  6608. }
  6609. if (!target) {
  6610. lua_interface->LogError("%s: LUA IsBehind command error: target is not valid", lua_interface->GetScriptName(state));
  6611. return 0;
  6612. }
  6613. lua_interface->SetBooleanValue(state, ((Entity*)spawn)->BehindTarget(target));
  6614. return 1;
  6615. }
  6616. int EQ2Emu_lua_IsFlanking(lua_State* state) {
  6617. if (!lua_interface)
  6618. return 0;
  6619. Spawn* spawn = lua_interface->GetSpawn(state);
  6620. Spawn* target = lua_interface->GetSpawn(state, 2);
  6621. if (!spawn) {
  6622. lua_interface->LogError("%s: LUA IsFlanking command error: spawn is not valid", lua_interface->GetScriptName(state));
  6623. return 0;
  6624. }
  6625. if (!spawn->IsEntity()) {
  6626. lua_interface->LogError("%s: LUA IsFlanking command error: spawn is not an entity", lua_interface->GetScriptName(state));
  6627. return 0;
  6628. }
  6629. if (!target) {
  6630. lua_interface->LogError("%s: LUA IsFlanking command error: target is not valid", lua_interface->GetScriptName(state));
  6631. return 0;
  6632. }
  6633. lua_interface->SetBooleanValue(state, ((Entity*)spawn)->FlankingTarget(target));
  6634. return 1;
  6635. }
  6636. int EQ2Emu_lua_GetItemCount(lua_State* state) {
  6637. if (!lua_interface)
  6638. return 0;
  6639. Item* item = lua_interface->GetItem(state);
  6640. if (!item) {
  6641. lua_interface->LogError("%s: LUA GetItemCount command error: item not valid", lua_interface->GetScriptName(state));
  6642. return 0;
  6643. }
  6644. lua_interface->SetInt32Value(state, item->details.count);
  6645. return 1;
  6646. }
  6647. int EQ2Emu_lua_SetItemCount(lua_State* state) {
  6648. if (!lua_interface)
  6649. return 0;
  6650. Item* item = lua_interface->GetItem(state);
  6651. Spawn* owner = lua_interface->GetSpawn(state, 2);
  6652. int16 new_count = lua_interface->GetInt32Value(state, 3);
  6653. if (!item) {
  6654. lua_interface->LogError("%s: LUA SetItemCount command error: item not valid", lua_interface->GetScriptName(state));
  6655. return 0;
  6656. }
  6657. if (!owner) {
  6658. lua_interface->LogError("%s: LUA SetItemCount command error: spawn not valid", lua_interface->GetScriptName(state));
  6659. return 0;
  6660. }
  6661. if (!owner->IsPlayer()) {
  6662. lua_interface->LogError("%s: LUA SetItemCount command error: spawn is not a player", lua_interface->GetScriptName(state));
  6663. return 0;
  6664. }
  6665. if (item->stack_count < new_count) {
  6666. lua_interface->LogError("%s: LUA SetItemCount command error: new item count cannot be more than max stack count", lua_interface->GetScriptName(state));
  6667. return 0;
  6668. }
  6669. if (new_count > 0) {
  6670. item->details.count = new_count;
  6671. item->save_needed = true;
  6672. }
  6673. else if (((Player*)owner)->GetEquipmentList()->GetItem(item->details.slot_id) == item)
  6674. ((Player*)owner)->GetEquipmentList()->RemoveItem(item->details.slot_id, true);
  6675. else if (((Player*)owner)->GetPlayerItemList()->GetItemFromUniqueID(item->details.unique_id) == item)
  6676. ((Player*)owner)->GetPlayerItemList()->RemoveItem(item, true);
  6677. else
  6678. {
  6679. lua_interface->LogError("%s: LUA SetItemCount command error: could not remove item from player", lua_interface->GetScriptName(state));
  6680. return 0;
  6681. }
  6682. Client* client = owner->GetZone()->GetClientBySpawn(owner);
  6683. if (!client)
  6684. return 0;
  6685. ((Player*)owner)->SendInventoryUpdate(client->GetVersion());
  6686. EQ2Packet* app = ((Player*)owner)->GetEquipmentList()->serialize(client->GetVersion());
  6687. if (app)
  6688. client->QueuePacket(app);
  6689. return 0;
  6690. }
  6691. int EQ2Emu_lua_AddSpellTimer(lua_State* state) {
  6692. if (!lua_interface)
  6693. return 0;
  6694. int32 time = lua_interface->GetInt32Value(state);
  6695. string function = lua_interface->GetStringValue(state, 2);
  6696. Spawn* caster = lua_interface->GetSpawn(state, 3);
  6697. Spawn* target = lua_interface->GetSpawn(state, 4);
  6698. LuaSpell* spell = lua_interface->GetCurrentSpell(state);
  6699. if (time == 0) {
  6700. lua_interface->LogError("%s: LUA AddSpellTimer command error: time must be set", lua_interface->GetScriptName(state));
  6701. return 0;
  6702. }
  6703. if (function.length() == 0) {
  6704. lua_interface->LogError("%s: LUA AddSpellTimer command error: function name must be set", lua_interface->GetScriptName(state));
  6705. return 0;
  6706. }
  6707. if (!spell) {
  6708. lua_interface->LogError("%s: LUA AddSpellTimer command error: spell not found, AddSpellTimer must be used in a spell script", lua_interface->GetScriptName(state));
  6709. return 0;
  6710. }
  6711. SpellScriptTimer* timer = new SpellScriptTimer;
  6712. /* //Google tells me memsetting a string is bad, manually setting just in case - Foof
  6713. #ifdef WIN32
  6714. ZeroMemory(timer, sizeof(SpellScriptTimer));
  6715. #else
  6716. bzero(timer, sizeof(SpellScriptTimer));
  6717. #endif*/
  6718. timer->caster = 0;
  6719. timer->deleteWhenDone = false;
  6720. timer->target = 0;
  6721. timer->time = Timer::GetCurrentTime2() + time;
  6722. timer->customFunction = function;
  6723. timer->spell = spell;
  6724. if (caster)
  6725. timer->caster = caster->GetID();
  6726. if (target)
  6727. timer->target = target->GetID();
  6728. spell->caster->GetZone()->GetSpellProcess()->AddSpellScriptTimer(timer);
  6729. return 0;
  6730. }
  6731. int EQ2Emu_lua_Resurrect(lua_State* state) {
  6732. if (!lua_interface)
  6733. return 0;
  6734. float hp_perc = lua_interface->GetFloatValue(state);
  6735. float power_perc = lua_interface->GetFloatValue(state, 2);
  6736. bool send_window = lua_interface->GetInt32Value(state, 3) == 1;
  6737. Spawn* target = lua_interface->GetSpawn(state, 4);
  6738. string heal_name = lua_interface->GetStringValue(state, 5);
  6739. int8 crit_mod = lua_interface->GetInt32Value(state, 6);
  6740. bool no_calcs = lua_interface->GetInt32Value(state, 7) == 1;
  6741. LuaSpell* spell = lua_interface->GetCurrentSpell(state);
  6742. if (!spell) {
  6743. lua_interface->LogError("%s: LUA command error: this function must be used in a spellscript", lua_interface->GetScriptName(state));
  6744. return 0;
  6745. }
  6746. Entity* caster = spell->caster;
  6747. if (!caster) {
  6748. lua_interface->LogError("%s: LUA command error: could not find caster", lua_interface->GetScriptName(state));
  6749. return 0;
  6750. }
  6751. Client* client = 0;
  6752. PendingResurrection* rez = 0;
  6753. ZoneServer* zone = spell->caster->GetZone();
  6754. if (!target) {
  6755. spell->MSpellTargets.readlock(__FUNCTION__, __LINE__);
  6756. if (spell->targets.size() > 0) {
  6757. vector<int32> spell_targets = spell->targets;
  6758. for (int8 i = 0; i < spell_targets.size(); i++) {
  6759. target = zone->GetSpawnByID(spell_targets.at(i));
  6760. if (!target)
  6761. continue;
  6762. if (!target->IsPlayer())
  6763. continue;
  6764. client = target->GetZone()->GetClientBySpawn(target);
  6765. if (!client)
  6766. continue;
  6767. rez = client->GetCurrentRez();
  6768. if (rez->active)
  6769. continue;
  6770. client->GetResurrectMutex()->writelock(__FUNCTION__, __LINE__);
  6771. rez->active = true;
  6772. rez->caster = caster;
  6773. rez->expire_timer = new Timer;
  6774. int32 duration = spell->spell->GetSpellDuration();
  6775. rez->expire_timer->Start(duration * 100);
  6776. rez->hp_perc = hp_perc;
  6777. rez->mp_perc = power_perc;
  6778. rez->range = spell->spell->GetSpellData()->range;
  6779. rez->spell_name = spell->spell->GetName();
  6780. if (heal_name.length() > 0)
  6781. rez->heal_name = heal_name;
  6782. else
  6783. rez->heal_name = rez->spell_name;
  6784. rez->no_calcs = no_calcs;
  6785. rez->crit_mod = crit_mod;
  6786. rez->spell_visual = spell->spell->GetSpellData()->spell_visual;
  6787. if (send_window)
  6788. client->SendResurrectionWindow();
  6789. else {
  6790. target->GetZone()->ResurrectSpawn(target, client);
  6791. rez->should_delete = true;
  6792. }
  6793. client->GetResurrectMutex()->releasewritelock(__FUNCTION__, __LINE__);
  6794. }
  6795. }
  6796. spell->MSpellTargets.releasereadlock(__FUNCTION__, __LINE__);
  6797. }
  6798. else {
  6799. client = target->GetZone()->GetClientBySpawn(target);
  6800. if (!client)
  6801. return 0;
  6802. rez = client->GetCurrentRez();
  6803. if (rez->active)
  6804. return 0;
  6805. client->GetResurrectMutex()->writelock(__FUNCTION__, __LINE__);
  6806. rez->active = true;
  6807. rez->caster = caster;
  6808. rez->expire_timer = new Timer;
  6809. int32 duration = spell->spell->GetSpellDuration();
  6810. rez->expire_timer->Start(duration * 100);
  6811. rez->hp_perc = hp_perc;
  6812. rez->mp_perc = power_perc;
  6813. rez->range = spell->spell->GetSpellData()->range;
  6814. rez->spell_name = spell->spell->GetName();
  6815. if (heal_name.length() > 0)
  6816. rez->heal_name = heal_name;
  6817. else
  6818. rez->heal_name = rez->spell_name;
  6819. rez->no_calcs = no_calcs;
  6820. rez->crit_mod = crit_mod;
  6821. rez->spell_visual = spell->spell->GetSpellData()->spell_visual;
  6822. if (send_window)
  6823. client->SendResurrectionWindow();
  6824. else {
  6825. target->GetZone()->ResurrectSpawn(target, client);
  6826. rez->should_delete = true;
  6827. }
  6828. client->GetResurrectMutex()->releasewritelock(__FUNCTION__, __LINE__);
  6829. }
  6830. return 0;
  6831. }
  6832. int EQ2Emu_lua_SetVision(lua_State* state) {
  6833. if (!lua_interface)
  6834. return 0;
  6835. Spawn* spawn = lua_interface->GetSpawn(state);
  6836. int8 vision = lua_interface->GetInt8Value(state, 2);
  6837. LuaSpell* spell = lua_interface->GetCurrentSpell(state);
  6838. if (!spawn) {
  6839. lua_interface->LogError("%s: LUA SetVision command error: spawn is not valid", lua_interface->GetScriptName(state));
  6840. return 0;
  6841. }
  6842. if (!spawn->IsEntity()) {
  6843. lua_interface->LogError("%s: LUA SetVision command error: spawn is not an entity", lua_interface->GetScriptName(state));
  6844. return 0;
  6845. }
  6846. if (spell && spell->targets.size() > 0) {
  6847. ZoneServer* zone = spell->caster->GetZone();
  6848. for (int8 i = 0; i < spell->targets.size(); i++) {
  6849. Spawn* target = zone->GetSpawnByID(spell->targets.at(i));
  6850. if (target->IsEntity()) {
  6851. ((Entity*)target)->GetInfoStruct()->vision = vision;
  6852. if (target->IsPlayer())
  6853. ((Player*)target)->SetCharSheetChanged(true);
  6854. }
  6855. }
  6856. }
  6857. else {
  6858. ((Entity*)spawn)->GetInfoStruct()->vision = vision;
  6859. if (spawn->IsPlayer())
  6860. ((Player*)spawn)->SetCharSheetChanged(true);
  6861. }
  6862. return 0;
  6863. }
  6864. int EQ2Emu_lua_BlurVision(lua_State* state) {
  6865. if (!lua_interface)
  6866. return 0;
  6867. Spawn* spawn = lua_interface->GetSpawn(state);
  6868. float intensity = lua_interface->GetFloatValue(state, 2);
  6869. LuaSpell* spell = lua_interface->GetCurrentSpell(state);
  6870. if (!spawn) {
  6871. lua_interface->LogError("%s: LUA BlurVision command error: spawn is not valid", lua_interface->GetScriptName(state));
  6872. return 0;
  6873. }
  6874. if (!spawn->IsEntity()) {
  6875. lua_interface->LogError("%s: LUA BlurVision command error: spawn is not an entity", lua_interface->GetScriptName(state));
  6876. return 0;
  6877. }
  6878. if (spell && spell->targets.size() > 0) {
  6879. ZoneServer* zone = spell->caster->GetZone();
  6880. for (int8 i = 0; i < spell->targets.size(); i++) {
  6881. Spawn* target = zone->GetSpawnByID(spell->targets.at(i));
  6882. if (target && target->IsEntity()) {
  6883. ((Entity*)target)->GetInfoStruct()->drunk = intensity;
  6884. if (target->IsPlayer())
  6885. ((Player*)target)->SetCharSheetChanged(true);
  6886. }
  6887. }
  6888. }
  6889. else {
  6890. ((Entity*)spawn)->GetInfoStruct()->drunk = intensity;
  6891. if (spawn->IsPlayer())
  6892. ((Player*)spawn)->SetCharSheetChanged(true);
  6893. }
  6894. return 0;
  6895. }
  6896. int EQ2Emu_lua_BreatheUnderwater(lua_State* state) {
  6897. if (!lua_interface)
  6898. return 0;
  6899. Spawn* spawn = lua_interface->GetSpawn(state);
  6900. bool breatheUnderwater = lua_interface->GetBooleanValue(state, 2);
  6901. LuaSpell* spell = lua_interface->GetCurrentSpell(state);
  6902. if (!spawn) {
  6903. lua_interface->LogError("%s: LUA BreathUnderwater command error: spawn is not valid", lua_interface->GetScriptName(state));
  6904. return 0;
  6905. }
  6906. if (!spawn->IsEntity()) {
  6907. lua_interface->LogError("%s: LUA BreathUnderwater command error: spawn is not en entity", lua_interface->GetScriptName(state));
  6908. return 0;
  6909. }
  6910. if (spell && spell->targets.size() > 0) {
  6911. ZoneServer* zone = spell->caster->GetZone();
  6912. for (int8 i = 0; i < spell->targets.size(); i++) {
  6913. Spawn* target = zone->GetSpawnByID(spell->targets.at(i));
  6914. if (target->IsEntity()) {
  6915. ((Entity*)target)->GetInfoStruct()->breathe_underwater = breatheUnderwater;
  6916. if (target->IsPlayer())
  6917. ((Player*)target)->SetCharSheetChanged(true);
  6918. }
  6919. }
  6920. }
  6921. else {
  6922. ((Entity*)spawn)->GetInfoStruct()->breathe_underwater = breatheUnderwater;
  6923. if (spawn->IsPlayer())
  6924. ((Player*)spawn)->SetCharSheetChanged(true);
  6925. }
  6926. return 0;
  6927. }
  6928. int EQ2Emu_lua_GetItemSkillReq(lua_State* state) {
  6929. if (!lua_interface)
  6930. return 0;
  6931. Item* item = lua_interface->GetItem(state);
  6932. int8 type = lua_interface->GetInt32Value(state, 2);
  6933. if (!item) {
  6934. lua_interface->LogError("%s: LUA GetItemSkillReq command error: item not valid", lua_interface->GetScriptName(state));
  6935. return 0;
  6936. }
  6937. if (type == 1)
  6938. lua_interface->SetInt32Value(state, item->generic_info.skill_req1);
  6939. else if (type == 2)
  6940. lua_interface->SetInt32Value(state, item->generic_info.skill_req2);
  6941. return 1;
  6942. }
  6943. int EQ2Emu_lua_SetSpeedMultiplier(lua_State* state) {
  6944. if (!lua_interface)
  6945. return 0;
  6946. Spawn* target = lua_interface->GetSpawn(state);
  6947. float val = lua_interface->GetFloatValue(state, 2);
  6948. LuaSpell* spell = lua_interface->GetCurrentSpell(state);
  6949. // Added from Gangrenous post
  6950. if (spell && spell->resisted)
  6951. return 0;
  6952. // if its a percentage of 100 its a slow, we want to go at a fraction of the speed
  6953. if (val > 1.0f)
  6954. val = 1.0f - (val / 100.0f);
  6955. if (spell && spell->spell && spell->targets.size() > 0) {
  6956. ZoneServer* zone = spell->caster->GetZone();
  6957. for (int32 i = 0; i != spell->targets.size(); i++) {
  6958. Spawn* spawn = zone->GetSpawnByID(spell->targets.at(i));
  6959. if (spawn && spawn->IsEntity()) {
  6960. ((Entity*)spawn)->SetSpeedMultiplier(val);
  6961. if (spawn->IsPlayer())
  6962. ((Player*)spawn)->SetCharSheetChanged(true);
  6963. }
  6964. }
  6965. }
  6966. else {
  6967. if (target && target->IsEntity()) {
  6968. ((Entity*)target)->SetSpeedMultiplier(val);
  6969. if (target->IsPlayer())
  6970. ((Player*)target)->SetCharSheetChanged(true);
  6971. }
  6972. }
  6973. return 0;
  6974. }
  6975. int EQ2Emu_lua_SetIllusion(lua_State* state) {
  6976. if (!lua_interface)
  6977. return 0;
  6978. Spawn* spawn = lua_interface->GetSpawn(state);
  6979. int16 model = lua_interface->GetInt16Value(state, 2);
  6980. LuaSpell* spell = lua_interface->GetCurrentSpell(state);
  6981. if (spell && spell->spell && spell->targets.size() > 0) {
  6982. ZoneServer* zone = spell->caster->GetZone();
  6983. for (int32 i = 0; i < spell->targets.size(); i++) {
  6984. Spawn* target = zone->GetSpawnByID(spell->targets.at(i));
  6985. if (target)
  6986. target->SetIllusionModel(model);
  6987. }
  6988. }
  6989. else {
  6990. if (!spawn) {
  6991. lua_interface->LogError("%s: LUA SetIllusion command error: spawn is not valid", lua_interface->GetScriptName(state));
  6992. return 0;
  6993. }
  6994. spawn->SetIllusionModel(model);
  6995. }
  6996. return 0;
  6997. }
  6998. int EQ2Emu_lua_ResetIllusion(lua_State* state) {
  6999. if (!lua_interface)
  7000. return 0;
  7001. Spawn* spawn = lua_interface->GetSpawn(state);
  7002. LuaSpell* spell = lua_interface->GetCurrentSpell(state);
  7003. if (spell && spell->spell && spell->targets.size() > 0) {
  7004. ZoneServer* zone = spell->caster->GetZone();
  7005. for (int32 i = 0; i < spell->targets.size(); i++) {
  7006. Spawn* target = zone->GetSpawnByID(spell->targets.at(i));
  7007. if (target)
  7008. target->SetIllusionModel(0);
  7009. }
  7010. }
  7011. else {
  7012. if (!spawn) {
  7013. lua_interface->LogError("%s: LUA ResetIllusion command error: spawn is not valid", lua_interface->GetScriptName(state));
  7014. return 0;
  7015. }
  7016. spawn->SetIllusionModel(0);
  7017. }
  7018. return 0;
  7019. }
  7020. int EQ2Emu_lua_AddThreatTransfer(lua_State* state) {
  7021. if (!lua_interface)
  7022. return 0;
  7023. Spawn* caster = lua_interface->GetSpawn(state);
  7024. Spawn* target = lua_interface->GetSpawn(state, 2);
  7025. float chance = lua_interface->GetFloatValue(state, 3);
  7026. LuaSpell* spell = lua_interface->GetCurrentSpell(state);
  7027. if (!caster) {
  7028. lua_interface->LogError("%s: LUA AddThreatTransfer command error: caster is not a valid spawn", lua_interface->GetScriptName(state));
  7029. return 0;
  7030. }
  7031. if (!caster->IsEntity()) {
  7032. lua_interface->LogError("%s: LUA AddThreatTransfer command error: caster is not an entity", lua_interface->GetScriptName(state));
  7033. return 0;
  7034. }
  7035. if (!target) {
  7036. lua_interface->LogError("%s: LUA AddThreatTransfer command error: target is not a valid spawn", lua_interface->GetScriptName(state));
  7037. return 0;
  7038. }
  7039. if (!target->IsEntity()) {
  7040. lua_interface->LogError("%s: LUA AddThreatTransfer command error: target is not an entity", lua_interface->GetScriptName(state));
  7041. return 0;
  7042. }
  7043. if (chance <= 0) {
  7044. lua_interface->LogError("%s: LUA AddThreatTransfer command error: chance must be greater then 0%", lua_interface->GetScriptName(state));
  7045. return 0;
  7046. }
  7047. if (!spell) {
  7048. lua_interface->LogError("%s: LUA AddThreatTransfer command error: can only be used in a spell script", lua_interface->GetScriptName(state));
  7049. return 0;
  7050. }
  7051. if (((Entity*)caster)->GetThreatTransfer()) {
  7052. return 0;
  7053. }
  7054. ThreatTransfer* transfer = new ThreatTransfer;
  7055. transfer->Target = target->GetID();
  7056. transfer->Amount = chance;
  7057. transfer->Spell = spell;
  7058. ((Entity*)caster)->SetThreatTransfer(transfer);
  7059. return 0;
  7060. }
  7061. int EQ2Emu_lua_RemoveThreatTransfer(lua_State* state) {
  7062. if (!lua_interface)
  7063. return 0;
  7064. Spawn* spawn = lua_interface->GetSpawn(state);
  7065. LuaSpell* spell = lua_interface->GetCurrentSpell(state);
  7066. if (!spawn) {
  7067. lua_interface->LogError("%s: LUA RemoveThreatTransfer command error: spawn is not valid", lua_interface->GetScriptName(state));
  7068. return 0;
  7069. }
  7070. if (!spell) {
  7071. lua_interface->LogError("%s: LUA RemoveThreatTransfer command error: can only be used in a spell script", lua_interface->GetScriptName(state));
  7072. return 0;
  7073. }
  7074. if (((Entity*)spawn)->GetThreatTransfer() && ((Entity*)spawn)->GetThreatTransfer()->Spell == spell) {
  7075. ThreatTransfer* transfer = ((Entity*)spawn)->GetThreatTransfer();
  7076. ((Entity*)spawn)->SetThreatTransfer(0);
  7077. safe_delete(transfer);
  7078. }
  7079. return 0;
  7080. }
  7081. int EQ2Emu_lua_CureByType(lua_State* state) {
  7082. if (!lua_interface)
  7083. return 0;
  7084. LuaSpell* spell = lua_interface->GetCurrentSpell(state);
  7085. if (!spell) {
  7086. lua_interface->LogError("%s: LUA CureByType command error: can only be used in a spell script", lua_interface->GetScriptName(state));
  7087. return 0;
  7088. }
  7089. int8 cure_count = lua_interface->GetInt8Value(state);
  7090. int8 cure_type = lua_interface->GetInt8Value(state, 2);
  7091. string cure_name = lua_interface->GetStringValue(state, 3);
  7092. int8 cure_level = lua_interface->GetInt8Value(state, 4);
  7093. Spawn* target = lua_interface->GetSpawn(state, 5);
  7094. if (target) {
  7095. if (!target->IsEntity()) {
  7096. lua_interface->LogError("%s: LUA CureByType command error: spawn override must be entity if used", lua_interface->GetScriptName(state));
  7097. return 0;
  7098. }
  7099. if (((Entity*)target)->GetDetTypeCount(cure_type) > 0)
  7100. ((Entity*)target)->CureDetrimentByType(cure_count, cure_type, cure_name.length() > 0 ? cure_name : (string)spell->spell->GetName(), spell->caster, cure_level);
  7101. }
  7102. else {
  7103. ZoneServer* zone = spell->caster->GetZone();
  7104. vector<int32> targets = spell->targets;
  7105. spell->MSpellTargets.readlock(__FUNCTION__, __LINE__);
  7106. for (int8 i = 0; i < targets.size(); i++) {
  7107. target = zone->GetSpawnByID(targets.at(i));
  7108. if (!target || !target->IsEntity())
  7109. continue;
  7110. if (((Entity*)target)->GetDetTypeCount(cure_type) > 0)
  7111. ((Entity*)target)->CureDetrimentByType(cure_count, cure_type, cure_name.length() > 0 ? cure_name : (string)spell->spell->GetName(), spell->caster, cure_level);
  7112. }
  7113. spell->MSpellTargets.releasereadlock(__FUNCTION__, __LINE__);
  7114. }
  7115. return 0;
  7116. }
  7117. int EQ2Emu_lua_CureByControlEffect(lua_State* state) {
  7118. if (!lua_interface)
  7119. return 0;
  7120. LuaSpell* spell = lua_interface->GetCurrentSpell(state);
  7121. if (!spell) {
  7122. lua_interface->LogError("%s: LUA CureByControlEffect command error: can only be used in a spell script", lua_interface->GetScriptName(state));
  7123. return 0;
  7124. }
  7125. int8 cure_count = lua_interface->GetInt8Value(state);
  7126. int8 cure_type = lua_interface->GetInt8Value(state, 2);
  7127. string cure_name = lua_interface->GetStringValue(state, 3);
  7128. int8 cure_level = lua_interface->GetInt8Value(state, 4);
  7129. Spawn* target = lua_interface->GetSpawn(state, 5);
  7130. if (target) {
  7131. if (!target->IsEntity()) {
  7132. lua_interface->LogError("%s: LUA CureByControlEffect command error: spawn override must be entity if used", lua_interface->GetScriptName(state));
  7133. return 0;
  7134. }
  7135. if (((Entity*)target)->GetDetCount() > 0)
  7136. ((Entity*)target)->CureDetrimentByControlEffect(cure_count, cure_type, cure_name.length() > 0 ? cure_name : (string)spell->spell->GetName(), spell->caster, cure_level);
  7137. }
  7138. else {
  7139. ZoneServer* zone = spell->caster->GetZone();
  7140. vector<int32> targets = spell->targets;
  7141. spell->MSpellTargets.readlock(__FUNCTION__, __LINE__);
  7142. for (int8 i = 0; i < targets.size(); i++) {
  7143. target = zone->GetSpawnByID(targets.at(i));
  7144. if (!target || !target->IsEntity())
  7145. continue;
  7146. if (((Entity*)target)->GetDetCount() > 0)
  7147. ((Entity*)target)->CureDetrimentByControlEffect(cure_count, cure_type, cure_name.length() > 0 ? cure_name : (string)spell->spell->GetName(), spell->caster, cure_level);
  7148. }
  7149. spell->MSpellTargets.releasereadlock(__FUNCTION__, __LINE__);
  7150. }
  7151. return 0;
  7152. }
  7153. int EQ2Emu_lua_CancelSpell(lua_State* state) {
  7154. if (!lua_interface)
  7155. return 0;
  7156. LuaSpell* spell = lua_interface->GetCurrentSpell(state);
  7157. if (!spell) {
  7158. lua_interface->LogError("%s: LUA CancelSpell command error: can only be use in a spell script", lua_interface->GetScriptName(state));
  7159. return 0;
  7160. }
  7161. if (!spell->caster) {
  7162. lua_interface->LogError("%s: LUA CancelSpell command error: unable to get the caster of the spell", lua_interface->GetScriptName(state));
  7163. return 0;
  7164. }
  7165. if (!spell->caster->GetZone()) {
  7166. lua_interface->LogError("%s: LUA CancelSpell command error: unable to get the zone of the caster", lua_interface->GetScriptName(state));
  7167. return 0;
  7168. }
  7169. spell->caster->GetZone()->GetSpellProcess()->AddSpellCancel(spell);
  7170. return 0;
  7171. }
  7172. int EQ2Emu_lua_RemoveStealth(lua_State* state) {
  7173. if (!lua_interface)
  7174. return 0;
  7175. Spawn* spawn = lua_interface->GetSpawn(state);
  7176. LuaSpell* spell = lua_interface->GetCurrentSpell(state);
  7177. if (!spell) {
  7178. lua_interface->LogError("%s: LUA RemoveStealth command error: must be used from spell script", lua_interface->GetScriptName(state));
  7179. return 0;
  7180. }
  7181. if (spawn && spawn->IsEntity())
  7182. ((Entity*)spawn)->RemoveStealthSpell(spell);
  7183. else {
  7184. ZoneServer* zone = spell->caster->GetZone();
  7185. spell->MSpellTargets.readlock(__FUNCTION__, __LINE__);
  7186. for (int32 i = 0; i < spell->targets.size(); i++) {
  7187. spawn = zone->GetSpawnByID(spell->targets.at(i));
  7188. if (!spawn || !spawn->IsEntity())
  7189. continue;
  7190. ((Entity*)spawn)->RemoveStealthSpell(spell);
  7191. }
  7192. spell->MSpellTargets.releasereadlock(__FUNCTION__, __LINE__);
  7193. }
  7194. return 0;
  7195. }
  7196. int EQ2Emu_lua_RemoveInvis(lua_State* state) {
  7197. if (!lua_interface)
  7198. return 0;
  7199. Spawn* spawn = lua_interface->GetSpawn(state);
  7200. LuaSpell* spell = lua_interface->GetCurrentSpell(state);
  7201. if (!spell) {
  7202. lua_interface->LogError("%s: LUA RemoveInvis command error: must be used from spell script", lua_interface->GetScriptName(state));
  7203. return 0;
  7204. }
  7205. if (spawn && spawn->IsEntity())
  7206. ((Entity*)spawn)->RemoveInvisSpell(spell);
  7207. else {
  7208. ZoneServer* zone = spell->caster->GetZone();
  7209. spell->MSpellTargets.readlock(__FUNCTION__, __LINE__);
  7210. for (int32 i = 0; i < spell->targets.size(); i++) {
  7211. spawn = zone->GetSpawnByID(spell->targets.at(i));
  7212. if (!spawn || !spawn->IsEntity())
  7213. continue;
  7214. ((Entity*)spawn)->RemoveInvisSpell(spell);
  7215. }
  7216. spell->MSpellTargets.releasereadlock(__FUNCTION__, __LINE__);
  7217. }
  7218. return 0;
  7219. }
  7220. int EQ2Emu_lua_StartHeroicOpportunity(lua_State* state) {
  7221. if (!lua_interface)
  7222. return 0;
  7223. Spawn* caster = lua_interface->GetSpawn(state);
  7224. int8 class_id = lua_interface->GetInt8Value(state, 2);
  7225. if (!caster) {
  7226. lua_interface->LogError("%s: LUA StartHeroicOpportunity command error: caster is not valid", lua_interface->GetScriptName(state));
  7227. return 0;
  7228. }
  7229. if (!caster->IsPlayer()) {
  7230. lua_interface->LogError("%s: LUA StartHeroicOpportunity command error: caster must be a player", lua_interface->GetScriptName(state));
  7231. return 0;
  7232. }
  7233. Spawn* target = caster->GetTarget();
  7234. if (!target) {
  7235. lua_interface->LogError("%s: LUA StartHeroicOpportunity command error: target is not valid", lua_interface->GetScriptName(state));
  7236. return 0;
  7237. }
  7238. Client* client = caster->GetZone()->GetClientBySpawn(caster);
  7239. if (!client) {
  7240. lua_interface->LogError("%s: LUA StartHeroicOpportunity command error: unable to get a client for the given caster", lua_interface->GetScriptName(state));
  7241. return 0;
  7242. }
  7243. HeroicOP* ho = master_ho_list.GetHeroicOP(class_id);
  7244. if (ho) {
  7245. ho->SetTarget(target->GetID());
  7246. LogWrite(SPELL__ERROR, 0, "HO", "caster: %u", caster->GetID());
  7247. LogWrite(SPELL__ERROR, 0, "HO", "target: %u", target->GetID());
  7248. if (((Entity*)caster)->GetGroupMemberInfo()) {
  7249. if (caster->GetZone()->GetSpellProcess()->AddHO(client, ho)) {
  7250. world.GetGroupManager()->GroupLock(__FUNCTION__, __LINE__);
  7251. deque<GroupMemberInfo*>::iterator itr;
  7252. PlayerGroup* group = world.GetGroupManager()->GetGroup(((Entity*)caster)->GetGroupMemberInfo()->group_id);
  7253. if (group)
  7254. {
  7255. group->MGroupMembers.readlock(__FUNCTION__, __LINE__);
  7256. deque<GroupMemberInfo*>* members = group->GetMembers();
  7257. for (itr = members->begin(); itr != members->end(); itr++) {
  7258. if ((*itr)->client)
  7259. ClientPacketFunctions::SendHeroicOPUpdate((*itr)->client, ho);
  7260. }
  7261. group->MGroupMembers.releasereadlock(__FUNCTION__, __LINE__);
  7262. }
  7263. world.GetGroupManager()->ReleaseGroupLock(__FUNCTION__, __LINE__);
  7264. }
  7265. else
  7266. safe_delete(ho);
  7267. }
  7268. else {
  7269. if (caster->GetZone()->GetSpellProcess()->AddHO(client, ho)) {
  7270. ClientPacketFunctions::SendHeroicOPUpdate(client, ho);
  7271. }
  7272. else
  7273. safe_delete(ho);
  7274. }
  7275. }
  7276. return 0;
  7277. }
  7278. int EQ2Emu_lua_SetSpellTriggerCount(lua_State* state) {
  7279. if (!lua_interface)
  7280. return 0;
  7281. LuaSpell* spell = lua_interface->GetCurrentSpell(state);
  7282. if (!spell) {
  7283. lua_interface->LogError("%s: LUA SetSpellTriggerCount command error: you must use this function in a spellscript!", lua_interface->GetScriptName(state));
  7284. return 0;
  7285. }
  7286. int16 triggerCount = lua_interface->GetInt16Value(state);
  7287. bool cancel_after_triggers = (lua_interface->GetInt8Value(state, 2) == 1);
  7288. if (!triggerCount) {
  7289. lua_interface->LogError("%s: LUA SetSpellTriggerCount command error: used trigger value equals zero!", lua_interface->GetScriptName(state));
  7290. return 0;
  7291. }
  7292. spell->num_triggers = triggerCount;
  7293. spell->had_triggers = true;
  7294. spell->cancel_after_all_triggers = cancel_after_triggers;
  7295. return 0;
  7296. }
  7297. int EQ2Emu_lua_GetSpellTriggerCount(lua_State* state) {
  7298. if (!lua_interface)
  7299. return 0;
  7300. LuaSpell* spell = lua_interface->GetCurrentSpell(state);
  7301. if (!spell) {
  7302. lua_interface->LogError("%s: LUA GetSpellTriggerCount command error: you must use this function in a spellscript!", lua_interface->GetScriptName(state));
  7303. return 0;
  7304. }
  7305. lua_interface->SetInt32Value(state, spell->num_triggers);
  7306. return 1;
  7307. }
  7308. int EQ2Emu_lua_RemoveTriggerFromSpell(lua_State* state) {
  7309. if (!lua_interface)
  7310. return 0;
  7311. LuaSpell* spell = lua_interface->GetCurrentSpell(state);
  7312. if (!spell) {
  7313. lua_interface->LogError("%s: LUA RemoveTriggerFromSpell command error: you must use this function in a spellscript!", lua_interface->GetScriptName(state));
  7314. return 0;
  7315. }
  7316. int16 remove_count = lua_interface->GetInt16Value(state);
  7317. if (!remove_count)
  7318. remove_count = 1;
  7319. if (remove_count >= spell->num_triggers) {
  7320. spell->num_triggers = 0;
  7321. if (spell->cancel_after_all_triggers)
  7322. spell->caster->GetZone()->GetSpellProcess()->AddSpellCancel(spell);
  7323. }
  7324. else {
  7325. spell->num_triggers -= remove_count;
  7326. ClientPacketFunctions::SendMaintainedExamineUpdate(spell->caster->GetZone()->GetClientBySpawn(spell->caster), spell->slot_pos, spell->num_triggers, 0);
  7327. }
  7328. return 0;
  7329. }
  7330. int EQ2Emu_lua_CopySpawnAppearance(lua_State* state) {
  7331. if (!lua_interface)
  7332. return 0;
  7333. Spawn* spawn = lua_interface->GetSpawn(state);
  7334. Spawn* copy_spawn = lua_interface->GetSpawn(state, 2);
  7335. if (!spawn) {
  7336. lua_interface->LogError("%s: LUA CopySpawnAppearance command error: the first spawn used was not valid!", lua_interface->GetScriptName(state));
  7337. return 0;
  7338. }
  7339. if (!copy_spawn) {
  7340. lua_interface->LogError("%s: LUA CopySpawnAppearance command error: the second spawn used was not valid!", lua_interface->GetScriptName(state));
  7341. return 0;
  7342. }
  7343. spawn->CopySpawnAppearance(copy_spawn);
  7344. return 0;
  7345. }
  7346. int EQ2Emu_lua_AddImmunitySpell(lua_State* state) {
  7347. if (!lua_interface)
  7348. return 0;
  7349. LuaSpell* spell = lua_interface->GetCurrentSpell(state);
  7350. int8 type = lua_interface->GetInt8Value(state);
  7351. Spawn* spawn = lua_interface->GetSpawn(state, 2);
  7352. if (!spell) {
  7353. lua_interface->LogError("%s: LUA AddImmunitySpell command error: This must be used in a spellscript", lua_interface->GetScriptName(state));
  7354. return 0;
  7355. }
  7356. if (spawn) {
  7357. if (!spawn->IsEntity()) {
  7358. lua_interface->LogError("%s: LUA AddImmunitySpell command error: The spawn provided is not an entity", lua_interface->GetScriptName(state));
  7359. return 0;
  7360. }
  7361. Entity* entity = ((Entity*)spawn);
  7362. switch (type) {
  7363. case IMMUNITY_TYPE_AOE:
  7364. entity->AddAOEImmunity(spell);
  7365. if (!(spell->effect_bitmask & EFFECT_FLAG_AOE_IMMUNE))
  7366. spell->effect_bitmask += EFFECT_FLAG_AOE_IMMUNE;
  7367. break;
  7368. case IMMUNITY_TYPE_STUN:
  7369. entity->AddStunImmunity(spell);
  7370. if (!(spell->effect_bitmask & EFFECT_FLAG_STUN_IMMUNE))
  7371. spell->effect_bitmask += EFFECT_FLAG_STUN_IMMUNE;
  7372. break;
  7373. case IMMUNITY_TYPE_ROOT:
  7374. entity->AddRootImmunity(spell);
  7375. if (!(spell->effect_bitmask & EFFECT_FLAG_ROOT_IMMUNE))
  7376. spell->effect_bitmask += EFFECT_FLAG_ROOT_IMMUNE;
  7377. break;
  7378. case IMMUNITY_TYPE_DAZE:
  7379. entity->AddDazeImmunity(spell);
  7380. if (!(spell->effect_bitmask & EFFECT_FLAG_DAZE_IMMUNE))
  7381. spell->effect_bitmask += EFFECT_FLAG_DAZE_IMMUNE;
  7382. break;
  7383. case IMMUNITY_TYPE_FEAR:
  7384. entity->AddFearImmunity(spell);
  7385. if (!(spell->effect_bitmask & EFFECT_FLAG_FEAR_IMMUNE))
  7386. spell->effect_bitmask += EFFECT_FLAG_FEAR_IMMUNE;
  7387. break;
  7388. case IMMUNITY_TYPE_MEZ:
  7389. entity->AddMezImmunity(spell);
  7390. if (!(spell->effect_bitmask & EFFECT_FLAG_MEZ_IMMUNE))
  7391. spell->effect_bitmask += EFFECT_FLAG_MEZ_IMMUNE;
  7392. break;
  7393. case IMMUNITY_TYPE_STIFLE:
  7394. entity->AddStifleImmunity(spell);
  7395. if (!(spell->effect_bitmask & EFFECT_FLAG_STIFLE_IMMUNE))
  7396. spell->effect_bitmask += EFFECT_FLAG_STIFLE_IMMUNE;
  7397. break;
  7398. default:
  7399. lua_interface->LogError("%s: LUA AddImmunitySpell command error: invalid immunity type", lua_interface->GetScriptName(state));
  7400. }
  7401. }
  7402. else {
  7403. bool should_break = false;
  7404. spell->MSpellTargets.readlock(__FUNCTION__, __LINE__);
  7405. for (int8 i = 0; i < spell->targets.size(); i++) {
  7406. spawn = spell->caster->GetZone()->GetSpawnByID(spell->targets.at(i));
  7407. if (!spawn || !spawn->IsEntity())
  7408. continue;
  7409. Entity* entity = ((Entity*)spawn);
  7410. switch (type) {
  7411. case IMMUNITY_TYPE_AOE:
  7412. entity->AddAOEImmunity(spell);
  7413. if (!(spell->effect_bitmask & EFFECT_FLAG_AOE_IMMUNE))
  7414. spell->effect_bitmask += EFFECT_FLAG_AOE_IMMUNE;
  7415. break;
  7416. case IMMUNITY_TYPE_STUN:
  7417. entity->AddStunImmunity(spell);
  7418. if (!(spell->effect_bitmask & EFFECT_FLAG_STUN_IMMUNE))
  7419. spell->effect_bitmask += EFFECT_FLAG_STUN_IMMUNE;
  7420. break;
  7421. case IMMUNITY_TYPE_ROOT:
  7422. entity->AddRootImmunity(spell);
  7423. if (!(spell->effect_bitmask & EFFECT_FLAG_ROOT_IMMUNE))
  7424. spell->effect_bitmask += EFFECT_FLAG_ROOT_IMMUNE;
  7425. break;
  7426. case IMMUNITY_TYPE_DAZE:
  7427. entity->AddDazeImmunity(spell);
  7428. if (!(spell->effect_bitmask & EFFECT_FLAG_DAZE_IMMUNE))
  7429. spell->effect_bitmask += EFFECT_FLAG_DAZE_IMMUNE;
  7430. break;
  7431. case IMMUNITY_TYPE_FEAR:
  7432. entity->AddFearImmunity(spell);
  7433. if (!(spell->effect_bitmask & EFFECT_FLAG_FEAR_IMMUNE))
  7434. spell->effect_bitmask += EFFECT_FLAG_FEAR_IMMUNE;
  7435. break;
  7436. case IMMUNITY_TYPE_MEZ:
  7437. entity->AddMezImmunity(spell);
  7438. if (!(spell->effect_bitmask & EFFECT_FLAG_MEZ_IMMUNE))
  7439. spell->effect_bitmask += EFFECT_FLAG_MEZ_IMMUNE;
  7440. break;
  7441. case IMMUNITY_TYPE_STIFLE:
  7442. entity->AddStifleImmunity(spell);
  7443. if (!(spell->effect_bitmask & EFFECT_FLAG_STIFLE_IMMUNE))
  7444. spell->effect_bitmask += EFFECT_FLAG_STIFLE_IMMUNE;
  7445. break;
  7446. default:
  7447. lua_interface->LogError("%s: LUA AddImmunitySpell command error: invalid immunity type", lua_interface->GetScriptName(state));
  7448. should_break = true;
  7449. }
  7450. if (should_break)
  7451. break;
  7452. }
  7453. spell->MSpellTargets.releasereadlock(__FUNCTION__, __LINE__);
  7454. }
  7455. return 0;
  7456. }
  7457. int EQ2Emu_lua_RemoveImmunitySpell(lua_State* state) {
  7458. if (!lua_interface)
  7459. return 0;
  7460. LuaSpell* spell = lua_interface->GetCurrentSpell(state);
  7461. int8 type = lua_interface->GetInt8Value(state);
  7462. Spawn* spawn = lua_interface->GetSpawn(state, 2);
  7463. if (!spell) {
  7464. lua_interface->LogError("%s: LUA RemoveImmunitySpell command error: This must be used in a spellscript", lua_interface->GetScriptName(state));
  7465. return 0;
  7466. }
  7467. if (spawn) {
  7468. if (!spawn->IsEntity()) {
  7469. lua_interface->LogError("%s: LUA RemoveImmunitySpell command error: The spawn provided is not an entity", lua_interface->GetScriptName(state));
  7470. return 0;
  7471. }
  7472. Entity* entity = ((Entity*)spawn);
  7473. switch (type) {
  7474. case IMMUNITY_TYPE_AOE:
  7475. entity->RemoveAOEImmunity(spell);
  7476. break;
  7477. case IMMUNITY_TYPE_STUN:
  7478. entity->RemoveStunImmunity(spell);
  7479. break;
  7480. case IMMUNITY_TYPE_ROOT:
  7481. entity->RemoveRootImmunity(spell);
  7482. break;
  7483. case IMMUNITY_TYPE_DAZE:
  7484. entity->RemoveDazeImmunity(spell);
  7485. break;
  7486. case IMMUNITY_TYPE_FEAR:
  7487. entity->RemoveFearImmunity(spell);
  7488. break;
  7489. case IMMUNITY_TYPE_MEZ:
  7490. entity->RemoveMezImmunity(spell);
  7491. break;
  7492. case IMMUNITY_TYPE_STIFLE:
  7493. entity->RemoveStifleImmunity(spell);
  7494. break;
  7495. default:
  7496. lua_interface->LogError("%s: LUA RemoveImmunitySpell command error: invalid immunity type", lua_interface->GetScriptName(state));
  7497. }
  7498. }
  7499. else {
  7500. bool should_break = false;
  7501. spell->MSpellTargets.readlock(__FUNCTION__, __LINE__);
  7502. for (int8 i = 0; i < spell->targets.size(); i++) {
  7503. spawn = spell->caster->GetZone()->GetSpawnByID(spell->targets.at(i));
  7504. if (!spawn || !spawn->IsEntity())
  7505. continue;
  7506. Entity* entity = ((Entity*)spawn);
  7507. switch (type) {
  7508. case IMMUNITY_TYPE_AOE:
  7509. entity->RemoveAOEImmunity(spell);
  7510. break;
  7511. case IMMUNITY_TYPE_STUN:
  7512. entity->RemoveStunImmunity(spell);
  7513. break;
  7514. case IMMUNITY_TYPE_ROOT:
  7515. entity->RemoveRootImmunity(spell);
  7516. break;
  7517. case IMMUNITY_TYPE_DAZE:
  7518. entity->RemoveDazeImmunity(spell);
  7519. break;
  7520. case IMMUNITY_TYPE_FEAR:
  7521. entity->RemoveFearImmunity(spell);
  7522. break;
  7523. case IMMUNITY_TYPE_MEZ:
  7524. entity->RemoveMezImmunity(spell);
  7525. break;
  7526. case IMMUNITY_TYPE_STIFLE:
  7527. entity->RemoveStifleImmunity(spell);
  7528. break;
  7529. default:
  7530. lua_interface->LogError("%s: LUA RemoveImmunitySpell command error: invalid immunity type", lua_interface->GetScriptName(state));
  7531. should_break = true;
  7532. }
  7533. if (should_break)
  7534. break;
  7535. }
  7536. spell->MSpellTargets.releasereadlock(__FUNCTION__, __LINE__);
  7537. }
  7538. return 0;
  7539. }
  7540. int EQ2Emu_lua_SetSpellSnareValue(lua_State* state) {
  7541. if (!lua_interface)
  7542. return 0;
  7543. LuaSpell* spell = lua_interface->GetCurrentSpell(state);
  7544. if (!spell) {
  7545. lua_interface->LogError("%s: LUA SetSpellSnareValue command error: This can only be used in a spell script!", lua_interface->GetScriptName(state));
  7546. return 0;
  7547. }
  7548. float snare = lua_interface->GetFloatValue(state);
  7549. Spawn* spawn = lua_interface->GetSpawn(state, 2);
  7550. // convert the val to the speed multipler value (100 - val)
  7551. float val = 100.0 - snare;
  7552. val /= 100.0;
  7553. if (spawn) {
  7554. if (!spawn->IsEntity()) {
  7555. lua_interface->LogError("%s: LUA SetSpellSnareValue command error: spawn must be an entity.", lua_interface->GetScriptName(state));
  7556. return 0;
  7557. }
  7558. ((Entity*)spawn)->SetSnareValue(spell, val);
  7559. }
  7560. else {
  7561. spell->MSpellTargets.readlock(__FUNCTION__, __LINE__);
  7562. for (int8 i = 0; i < spell->targets.size(); i++) {
  7563. spawn = spell->caster->GetZone()->GetSpawnByID(spell->targets.at(i));
  7564. if (!spawn || !spawn->IsEntity())
  7565. continue;
  7566. ((Entity*)spawn)->SetSnareValue(spell, val);
  7567. }
  7568. spell->MSpellTargets.releasereadlock(__FUNCTION__, __LINE__);
  7569. }
  7570. return 0;
  7571. }
  7572. int EQ2Emu_lua_CheckRaceType(lua_State* state) {
  7573. if (!lua_interface)
  7574. return 0;
  7575. Spawn* spawn = lua_interface->GetSpawn(state);
  7576. int16 race_id = lua_interface->GetInt16Value(state, 2);
  7577. if (!spawn) {
  7578. lua_interface->LogError("%s: LUA CheckRaceType command error: spawn is not valid", lua_interface->GetScriptName(state));
  7579. return 0;
  7580. }
  7581. if (race_id == 0) {
  7582. lua_interface->LogError("%s: LUA CheckRaceType command error: race id must be set", lua_interface->GetScriptName(state));
  7583. return 0;
  7584. }
  7585. lua_interface->SetBooleanValue(state, (race_id == race_types_list.GetRaceType(spawn->GetModelType()) || race_id == race_types_list.GetRaceBaseType(spawn->GetModelType())));
  7586. return 1;
  7587. }
  7588. int EQ2Emu_lua_GetRaceType(lua_State* state) {
  7589. if (!lua_interface)
  7590. return 0;
  7591. Spawn* spawn = lua_interface->GetSpawn(state);
  7592. if (!spawn) {
  7593. lua_interface->LogError("%s: LUA GetRaceType command error: spawn is not valid", lua_interface->GetScriptName(state));
  7594. return 0;
  7595. }
  7596. lua_interface->SetInt32Value(state, race_types_list.GetRaceType(spawn->GetModelType()));
  7597. return 1;
  7598. }
  7599. int EQ2Emu_lua_GetRaceBaseType(lua_State* state) {
  7600. if (!lua_interface)
  7601. return 0;
  7602. Spawn* spawn = lua_interface->GetSpawn(state);
  7603. if (!spawn) {
  7604. lua_interface->LogError("%s: LUA GetRaceBaseType command error: spawn is not valid", lua_interface->GetScriptName(state));
  7605. return 0;
  7606. }
  7607. lua_interface->SetInt32Value(state, race_types_list.GetRaceBaseType(spawn->GetModelType()));
  7608. return 1;
  7609. }
  7610. int EQ2Emu_lua_GetSpellName(lua_State* state) {
  7611. if (!lua_interface)
  7612. return 0;
  7613. LuaSpell* spell = lua_interface->GetCurrentSpell(state);
  7614. if (!spell) {
  7615. lua_interface->LogError("%s: LUA GetSpellName command error: this function must be used from a spell script!", lua_interface->GetScriptName(state));
  7616. return 0;
  7617. }
  7618. lua_interface->SetStringValue(state, spell->spell->GetName());
  7619. return 1;
  7620. }
  7621. int EQ2Emu_lua_GetQuestFlags(lua_State* state) {
  7622. if (!lua_interface)
  7623. return 0;
  7624. Quest* quest = lua_interface->GetQuest(state);
  7625. if (!quest) {
  7626. lua_interface->LogError("%s: LUA GetQuestFlags command error: quest is not valid.", lua_interface->GetScriptName(state));
  7627. return 0;
  7628. }
  7629. lua_interface->SetInt32Value(state, quest->GetQuestFlags());
  7630. return 1;
  7631. }
  7632. int EQ2Emu_lua_SetQuestFlags(lua_State* state) {
  7633. if (!lua_interface)
  7634. return 0;
  7635. Quest* quest = lua_interface->GetQuest(state);
  7636. int32 flags = lua_interface->GetInt32Value(state, 2);
  7637. if (!quest) {
  7638. lua_interface->LogError("%s: LUA SetQuestFlags command error: quest is not valid.", lua_interface->GetScriptName(state));
  7639. return 0;
  7640. }
  7641. quest->SetQuestFlags(flags);
  7642. return 0;
  7643. }
  7644. int EQ2Emu_lua_SetQuestTimer(lua_State* state) {
  7645. if (!lua_interface)
  7646. return 0;
  7647. Quest* quest = lua_interface->GetQuest(state);
  7648. Spawn* player = lua_interface->GetSpawn(state, 2);
  7649. int32 step = lua_interface->GetInt32Value(state, 3);
  7650. int32 duration = lua_interface->GetInt32Value(state, 4);
  7651. string action = lua_interface->GetStringValue(state, 5);
  7652. if (!quest) {
  7653. lua_interface->LogError("%s: LUA SetQuestTimer command error: quest is not valid.", lua_interface->GetScriptName(state));
  7654. return 0;
  7655. }
  7656. if (!player) {
  7657. lua_interface->LogError("%s: LUA SetQuestTimer command error: player is not a valid spawn.", lua_interface->GetScriptName(state));
  7658. return 0;
  7659. }
  7660. if (!player->IsPlayer()) {
  7661. lua_interface->LogError("%s: LUA SetQuestTimer command error: player is not a valid player.", lua_interface->GetScriptName(state));
  7662. return 0;
  7663. }
  7664. if (step == 0) {
  7665. lua_interface->LogError("%s: LUA SetQuestTimer command error: step must be set.", lua_interface->GetScriptName(state));
  7666. return 0;
  7667. }
  7668. if (duration == 0) {
  7669. lua_interface->LogError("%s: LUA SetQuestTimer command error: duration must be set.", lua_interface->GetScriptName(state));
  7670. return 0;
  7671. }
  7672. if (action.length() == 0) {
  7673. lua_interface->LogError("%s: LUA SetQuestTimer command error: failed action must be set.", lua_interface->GetScriptName(state));
  7674. return 0;
  7675. }
  7676. Client* client = player->GetZone()->GetClientBySpawn(player);
  7677. if (!client) {
  7678. lua_interface->LogError("%s: LUA SetQuestTimer command error: failed to get a valid client pointer for the given player", lua_interface->GetScriptName(state));
  7679. return 0;
  7680. }
  7681. quest->SetTimerStep(step);
  7682. quest->AddFailedAction(step, action);
  7683. quest->SetStepTimer(duration);
  7684. client->AddQuestTimer(quest->GetQuestID());
  7685. return 0;
  7686. }
  7687. int EQ2Emu_lua_SetQuestTimerComplete(lua_State* state) {
  7688. if (!lua_interface)
  7689. return 0;
  7690. Quest* quest = lua_interface->GetQuest(state);
  7691. Spawn* player = lua_interface->GetSpawn(state, 2);
  7692. if (!quest) {
  7693. lua_interface->LogError("%s: LUA SetQuestTimerComplete command error: quest is not valid.", lua_interface->GetScriptName(state));
  7694. return 0;
  7695. }
  7696. if (!player) {
  7697. lua_interface->LogError("%s: LUA SetQuestTimerComplete command error: player is not a valid spawn.", lua_interface->GetScriptName(state));
  7698. return 0;
  7699. }
  7700. if (!player->IsPlayer()) {
  7701. lua_interface->LogError("%s: LUA SetQuestTimerComplete command error: player is not a valid player.", lua_interface->GetScriptName(state));
  7702. return 0;
  7703. }
  7704. Client* client = player->GetZone()->GetClientBySpawn(player);
  7705. if (!client) {
  7706. lua_interface->LogError("%s: LUA SetQuestTimerComplete command error: failed to get a valid client pointer for the given player", lua_interface->GetScriptName(state));
  7707. return 0;
  7708. }
  7709. quest->SetTimerStep(0);
  7710. quest->SetStepTimer(0);
  7711. client->RemoveQuestTimer(quest->GetQuestID());
  7712. return 0;
  7713. }
  7714. int EQ2Emu_lua_RemoveQuestStep(lua_State* state) {
  7715. if (!lua_interface)
  7716. return 0;
  7717. Spawn* player = lua_interface->GetSpawn(state);
  7718. Quest* quest = lua_interface->GetQuest(state, 2);
  7719. int32 step = lua_interface->GetInt32Value(state, 3);
  7720. if (!player) {
  7721. lua_interface->LogError("%s: LUA RemoveQuestStep command error: player is not a valid spawn.", lua_interface->GetScriptName(state));
  7722. return 0;
  7723. }
  7724. if (!player->IsPlayer()) {
  7725. lua_interface->LogError("%s: LUA RemoveQuestStep command error: player is not a valid player.", lua_interface->GetScriptName(state));
  7726. return 0;
  7727. }
  7728. if (!quest) {
  7729. lua_interface->LogError("%s: LUA RemoveQuestStep command error: quest is not valid.", lua_interface->GetScriptName(state));
  7730. return 0;
  7731. }
  7732. if (step == 0) {
  7733. lua_interface->LogError("%s: LUA RemoveQuestStep command error: step must be set.", lua_interface->GetScriptName(state));
  7734. return 0;
  7735. }
  7736. Client* client = player->GetZone()->GetClientBySpawn(player);
  7737. if (!client) {
  7738. lua_interface->LogError("%s: LUA RemoveQuestStep command error: unable to get a valid client pointer from the given player.", lua_interface->GetScriptName(state));
  7739. return 0;
  7740. }
  7741. if (quest->RemoveQuestStep(step, client)) {
  7742. client->QueuePacket(quest->QuestJournalReply(client->GetVersion(), client->GetNameCRC(), (Player*)player, 0, 0, 0, true));
  7743. client->GetCurrentZone()->SendQuestUpdates(client);
  7744. }
  7745. else
  7746. lua_interface->LogError("LUA RemoveQuestStep command error: unable to remove the step (%u) from the quest (%s).", step, quest->GetName());
  7747. return 0;
  7748. }
  7749. int EQ2Emu_lua_ResetQuestStep(lua_State* state) {
  7750. if (!lua_interface)
  7751. return 0;
  7752. Quest* quest = lua_interface->GetQuest(state, 1);
  7753. int32 step = lua_interface->GetInt32Value(state, 2);
  7754. string desc = lua_interface->GetStringValue(state, 3);
  7755. string task_group = lua_interface->GetStringValue(state, 4);
  7756. if (!quest) {
  7757. lua_interface->LogError("%s: LUA ResetQuestStep command error: quest is not valid.", lua_interface->GetScriptName(state));
  7758. return 0;
  7759. }
  7760. if (step == 0) {
  7761. lua_interface->LogError("%s: LUA ResetQuestStep command error: step must be set.", lua_interface->GetScriptName(state));
  7762. return 0;
  7763. }
  7764. QuestStep* quest_step = quest->GetQuestStep(step);
  7765. if (!quest_step) {
  7766. lua_interface->LogError("%s: LUA ResetQuestStep command error: unable to get a valid quest step.", lua_interface->GetScriptName(state));
  7767. return 0;
  7768. }
  7769. quest_step->SetStepProgress(0);
  7770. quest_step->SetTaskGroup(task_group);
  7771. quest_step->SetDescription(desc);
  7772. return 0;
  7773. }
  7774. int EQ2Emu_lua_AddQuestStepFailureAction(lua_State* state) {
  7775. if (!lua_interface)
  7776. return 0;
  7777. Quest* quest = lua_interface->GetQuest(state);
  7778. int32 step = lua_interface->GetInt32Value(state, 2);
  7779. string action = lua_interface->GetStringValue(state, 3);
  7780. if (!quest) {
  7781. lua_interface->LogError("%s: LUA AddQuestStepFailureAction command error: quest is not valid.", lua_interface->GetScriptName(state));
  7782. return 0;
  7783. }
  7784. if (step == 0) {
  7785. lua_interface->LogError("%s: LUA AddQuestStepFailureAction command error: step must be set.", lua_interface->GetScriptName(state));
  7786. return 0;
  7787. }
  7788. if (action.length() == 0) {
  7789. lua_interface->LogError("%s: LUA AddQuestStepFailureAction command error: action must be set.", lua_interface->GetScriptName(state));
  7790. return 0;
  7791. }
  7792. quest->AddFailedAction(step, action);
  7793. return 0;
  7794. }
  7795. int EQ2Emu_lua_SetStepFailed(lua_State* state) {
  7796. if (!lua_interface)
  7797. return 0;
  7798. Spawn* player = lua_interface->GetSpawn(state);
  7799. int32 quest_id = lua_interface->GetInt32Value(state, 2);
  7800. int32 step = lua_interface->GetInt32Value(state, 3);
  7801. if (!player) {
  7802. lua_interface->LogError("%s: LUA SetStepFailed command error: player is not a valid spawn.", lua_interface->GetScriptName(state));
  7803. return 0;
  7804. }
  7805. if (!player->IsPlayer()) {
  7806. lua_interface->LogError("%s: LUA SetStepFailed command error: player is not a valid player.", lua_interface->GetScriptName(state));
  7807. return 0;
  7808. }
  7809. if (quest_id == 0) {
  7810. lua_interface->LogError("%s: LUA SetStepFailed command error: quest_id must be set.", lua_interface->GetScriptName(state));
  7811. return 0;
  7812. }
  7813. if (step == 0) {
  7814. lua_interface->LogError("%s: LUA SetStepFailed command error: step must be set.", lua_interface->GetScriptName(state));
  7815. return 0;
  7816. }
  7817. Quest* quest = ((Player*)player)->GetQuest(quest_id);
  7818. if (!quest) {
  7819. lua_interface->LogError("LUA SetStepFailed command error: unable to get a valid quest from the given id (%u).", quest_id);
  7820. return 0;
  7821. }
  7822. quest->StepFailed(step);
  7823. return 0;
  7824. }
  7825. int EQ2Emu_lua_GetQuestCompleteCount(lua_State* state) {
  7826. if (!lua_interface)
  7827. return 0;
  7828. Spawn* player = lua_interface->GetSpawn(state);
  7829. int32 quest_id = lua_interface->GetInt32Value(state, 2);
  7830. if (!player) {
  7831. lua_interface->LogError("%s: LUA GetQuestCompleteCount command error: player is not a valid spawn", lua_interface->GetScriptName(state));
  7832. return 0;
  7833. }
  7834. if (!player->IsPlayer()) {
  7835. lua_interface->LogError("%s: LUA GetQuestCompleteCount command error: player is not a valid player", lua_interface->GetScriptName(state));
  7836. return 0;
  7837. }
  7838. if (quest_id == 0) {
  7839. lua_interface->LogError("%s: LUA GetQuestCompleteCount command error: quest id is not valid", lua_interface->GetScriptName(state));
  7840. return 0;
  7841. }
  7842. Quest* quest = ((Player*)player)->GetCompletedQuest(quest_id);
  7843. if (!quest) {
  7844. lua_interface->SetInt32Value(state, 0);
  7845. return 1;
  7846. }
  7847. lua_interface->SetInt32Value(state, quest->GetCompleteCount());
  7848. return 1;
  7849. }
  7850. int EQ2Emu_lua_SetServerVariable(lua_State* state) {
  7851. if (!lua_interface)
  7852. return 0;
  7853. string name = lua_interface->GetStringValue(state);
  7854. string value = lua_interface->GetStringValue(state, 2);
  7855. string comment = lua_interface->GetStringValue(state, 3);
  7856. if (name.length() == 0) {
  7857. lua_interface->LogError("%s: LUA SetServerVariable command error: name is not valid", lua_interface->GetScriptName(state));
  7858. return 0;
  7859. }
  7860. if (value.length() == 0) {
  7861. lua_interface->LogError("%s: LUA SetServerVariable command error: value is not valid", lua_interface->GetScriptName(state));
  7862. return 0;
  7863. }
  7864. string varname = string("lua_").append(name);
  7865. Variable* var = variables.FindVariable(varname);
  7866. if (var)
  7867. var->SetValue(value.c_str());
  7868. else {
  7869. var = new Variable(varname.c_str(), value.c_str(), comment.c_str());
  7870. variables.AddVariable(var);
  7871. }
  7872. database.SaveVariable(var->GetName(), var->GetValue(), var->GetComment());
  7873. return 0;
  7874. }
  7875. int EQ2Emu_lua_GetServerVariable(lua_State* state) {
  7876. if (!lua_interface)
  7877. return 0;
  7878. string name = lua_interface->GetStringValue(state);
  7879. if (name.length() == 0) {
  7880. lua_interface->LogError("%s: LUA GetServerVariable command error: name is not valid", lua_interface->GetScriptName(state));
  7881. return 0;
  7882. }
  7883. string varname = string("lua_").append(name);
  7884. Variable* var = variables.FindVariable(varname);
  7885. if (var)
  7886. lua_interface->SetStringValue(state, var->GetValue());
  7887. else
  7888. lua_interface->SetStringValue(state, "NULL");
  7889. return 1;
  7890. }
  7891. int EQ2Emu_lua_HasLanguage(lua_State* state) {
  7892. if (!lua_interface)
  7893. return 0;
  7894. Spawn* player = lua_interface->GetSpawn(state);
  7895. int32 language_id = lua_interface->GetInt32Value(state, 2);
  7896. if (!player) {
  7897. lua_interface->LogError("%s: LUA HasLanguage command error: player is not valid", lua_interface->GetScriptName(state));
  7898. return 0;
  7899. }
  7900. if (!player->IsPlayer()) {
  7901. lua_interface->LogError("%s: LUA HasLanguage command error: player is not a valid player", lua_interface->GetScriptName(state));
  7902. return 0;
  7903. }
  7904. lua_interface->SetBooleanValue(state, ((Player*)player)->HasLanguage(language_id));
  7905. return 1;
  7906. }
  7907. int EQ2Emu_lua_AddLanguage(lua_State* state) {
  7908. if (!lua_interface)
  7909. return 0;
  7910. Spawn* player = lua_interface->GetSpawn(state);
  7911. int32 language_id = lua_interface->GetInt32Value(state, 2);
  7912. if (!player) {
  7913. lua_interface->LogError("%s: LUA AddLanguage command error: player is not valid", lua_interface->GetScriptName(state));
  7914. return 0;
  7915. }
  7916. if (!player->IsPlayer()) {
  7917. lua_interface->LogError("%s: LUA AddLanguage command error: player is not a valid player", lua_interface->GetScriptName(state));
  7918. return 0;
  7919. }
  7920. Language* language = master_languages_list.GetLanguage(language_id);
  7921. if (language)
  7922. ((Player*)player)->AddLanguage(language->GetID(), language->GetName(), true);
  7923. return 0;
  7924. }
  7925. int EQ2Emu_lua_IsNight(lua_State* state) {
  7926. if (!lua_interface)
  7927. return 0;
  7928. ZoneServer* zone = lua_interface->GetZone(state);
  7929. if (!zone) {
  7930. lua_interface->LogError("%s: LUA IsNight command error: zone is not valid", lua_interface->GetScriptName(state));
  7931. return 0;
  7932. }
  7933. lua_interface->SetBooleanValue(state, zone->IsDusk());
  7934. return 1;
  7935. }
  7936. int EQ2Emu_lua_AddMultiFloorLift(lua_State* state) {
  7937. if (!lua_interface)
  7938. return 0;
  7939. Spawn* spawn = lua_interface->GetSpawn(state);
  7940. if (!spawn) {
  7941. lua_interface->LogError("%s: LUA AddMultiFloorLift command error: spawn is not valid", lua_interface->GetScriptName(state));
  7942. return 0;
  7943. }
  7944. if (!spawn->IsWidget()) {
  7945. lua_interface->LogError("%s: LUA AddMultiFloorLift command error: spawn is not a widget", lua_interface->GetScriptName(state));
  7946. return 0;
  7947. }
  7948. ((Widget*)spawn)->SetMultiFloorLift(true);
  7949. if (spawn->GetZone())
  7950. spawn->GetZone()->AddTransportSpawn(spawn);
  7951. return 0;
  7952. }
  7953. int EQ2Emu_lua_StartAutoMount(lua_State* state) {
  7954. if (!lua_interface)
  7955. return 0;
  7956. Spawn* player = lua_interface->GetSpawn(state);
  7957. int32 path = lua_interface->GetInt32Value(state, 2);
  7958. if (!player) {
  7959. lua_interface->LogError("%s: LUA StartAutoMount command error: spawn is not valid", lua_interface->GetScriptName(state));
  7960. return 0;
  7961. }
  7962. if (!player->IsPlayer()) {
  7963. lua_interface->LogError("%s: LUA StartAutoMount command error: spawn is not a player", lua_interface->GetScriptName(state));
  7964. return 0;
  7965. }
  7966. if (path == 0) {
  7967. lua_interface->LogError("%s: LUA StartAutoMount command error: path must be greater then zero", lua_interface->GetScriptName(state));
  7968. return 0;
  7969. }
  7970. Client* client = player->GetZone()->GetClientBySpawn(player);
  7971. if (!client) {
  7972. lua_interface->LogError("%s: LUA StartAutoMount command error: unable to get a client from the given player", lua_interface->GetScriptName(state));
  7973. return 0;
  7974. }
  7975. client->SendFlightAutoMount(path);
  7976. return 0;
  7977. }
  7978. int EQ2Emu_lua_EndAutoMount(lua_State* state) {
  7979. if (!lua_interface)
  7980. return 0;
  7981. Spawn* player = lua_interface->GetSpawn(state);
  7982. if (!player) {
  7983. lua_interface->LogError("%s: LUA EndAutoMount command error: spawn is not valid", lua_interface->GetScriptName(state));
  7984. return 0;
  7985. }
  7986. if (!player->IsPlayer()) {
  7987. lua_interface->LogError("%s: LUA EndAutoMount command error: spawn is not a player", lua_interface->GetScriptName(state));
  7988. return 0;
  7989. }
  7990. Client* client = player->GetZone()->GetClientBySpawn(player);
  7991. if (!client) {
  7992. lua_interface->LogError("%s: LUA EndAutoMount command error: unable to get a client from the given player", lua_interface->GetScriptName(state));
  7993. return 0;
  7994. }
  7995. client->EndAutoMount();
  7996. return 0;
  7997. }
  7998. int EQ2Emu_lua_IsOnAutoMount(lua_State* state) {
  7999. if (!lua_interface)
  8000. return 0;
  8001. Spawn* player = lua_interface->GetSpawn(state);
  8002. if (!player) {
  8003. lua_interface->LogError("%s: LUA IsOnAutoMount command error: spawn in not valid", lua_interface->GetScriptName(state));
  8004. return 0;
  8005. }
  8006. if (!player->IsPlayer()) {
  8007. lua_interface->LogError("%s: LUA IsOnAutoMount command error: spawn is not a player", lua_interface->GetScriptName(state));
  8008. return 0;
  8009. }
  8010. Client* client = player->GetZone()->GetClientBySpawn(player);
  8011. if (!client) {
  8012. lua_interface->LogError("%s: LUA IsOnAutoMount command error: unable to get a client from the given player", lua_interface->GetScriptName(state));
  8013. return 0;
  8014. }
  8015. lua_interface->SetBooleanValue(state, client->GetOnAutoMount());
  8016. return 1;
  8017. }
  8018. int EQ2Emu_lua_SetPlayerHistory(lua_State* state) {
  8019. if (!lua_interface)
  8020. return 0;
  8021. Spawn* player = lua_interface->GetSpawn(state);
  8022. int32 event_id = lua_interface->GetInt32Value(state, 2);
  8023. int32 value = lua_interface->GetInt32Value(state, 3);
  8024. int32 value2 = lua_interface->GetInt32Value(state, 4);
  8025. if (!player) {
  8026. lua_interface->LogError("%s: LUA SetPlayerHistory command error: spawn is not valid", lua_interface->GetScriptName(state));
  8027. return 0;
  8028. }
  8029. if (!player->IsPlayer()) {
  8030. lua_interface->LogError("%s: LUA SetPlayerHistory command error: spawn is not a player", lua_interface->GetScriptName(state));
  8031. return 0;
  8032. }
  8033. ((Player*)player)->UpdateLUAHistory(event_id, value, value2);
  8034. return 0;
  8035. }
  8036. int EQ2Emu_lua_GetPlayerHistory(lua_State* state) {
  8037. if (!lua_interface)
  8038. return 0;
  8039. Spawn* player = lua_interface->GetSpawn(state);
  8040. int32 event_id = lua_interface->GetInt32Value(state, 2);
  8041. if (!player) {
  8042. lua_interface->LogError("%s: LUA GetPlayerHistory command error: spawn is not valid", lua_interface->GetScriptName(state));
  8043. return 0;
  8044. }
  8045. if (!player->IsPlayer()) {
  8046. lua_interface->LogError("%s: LUA GetPlayerHistory command error: spawn is not a player", lua_interface->GetScriptName(state));
  8047. return 0;
  8048. }
  8049. LUAHistory* hd = ((Player*)player)->GetLUAHistory(event_id);
  8050. if (!hd)
  8051. return 0;
  8052. lua_interface->SetInt32Value(state, hd->Value);
  8053. lua_interface->SetInt32Value(state, hd->Value2);
  8054. return 2;
  8055. }
  8056. int EQ2Emu_lua_SetGridID(lua_State* state) {
  8057. if (!lua_interface)
  8058. return 0;
  8059. Spawn* spawn = lua_interface->GetSpawn(state);
  8060. int32 grid = lua_interface->GetInt32Value(state, 2);
  8061. if (!spawn) {
  8062. lua_interface->LogError("%s: LUA SetGridID command error: spawn is not valid", lua_interface->GetScriptName(state));
  8063. return 0;
  8064. }
  8065. if (grid == 0) {
  8066. lua_interface->LogError("%s: LUA SetGridID command error: grid is not valid", lua_interface->GetScriptName(state));
  8067. return 0;
  8068. }
  8069. spawn->SetPos(&(spawn->appearance.pos.grid_id), grid);
  8070. return 0;
  8071. }
  8072. int EQ2Emu_lua_SetRequiredHistory(lua_State* state) {
  8073. if (!lua_interface)
  8074. return 0;
  8075. Spawn* spawn = lua_interface->GetSpawn(state);
  8076. int32 event_id = lua_interface->GetInt32Value(state, 2);
  8077. int32 value1 = lua_interface->GetInt32Value(state, 3);
  8078. int32 value2 = lua_interface->GetInt32Value(state, 4);
  8079. bool private_spawn = (lua_interface->GetInt8Value(state, 5) == 1);
  8080. int16 flag_override = lua_interface->GetInt16Value(state, 6);
  8081. if (!spawn) {
  8082. lua_interface->LogError("%s: LUA SetRequiredHistory command error: spawn is not valid", lua_interface->GetScriptName(state));
  8083. return 0;
  8084. }
  8085. //Add this quest to the list of required quests for this spawn
  8086. spawn->SetRequiredHistory(event_id, value1, value2);
  8087. //If private spawn value set
  8088. if (private_spawn) {
  8089. //Set the spawn to be private when not granted access via history
  8090. spawn->AddAllowAccessSpawn(spawn);
  8091. spawn->SetPrivateQuestSpawn(true);
  8092. }
  8093. //This value will override vis_flags in the vis packet
  8094. if (flag_override > 0)
  8095. spawn->SetQuestsRequiredOverride(flag_override);
  8096. return 0;
  8097. }
  8098. int EQ2Emu_lua_GetQuestStepProgress(lua_State* state) {
  8099. if (!lua_interface)
  8100. return 0;
  8101. Spawn* player = lua_interface->GetSpawn(state);
  8102. int32 quest_id = lua_interface->GetInt32Value(state, 2);
  8103. int32 step_id = lua_interface->GetInt32Value(state, 3);
  8104. if (!player) {
  8105. lua_interface->LogError("%s: LUA GetQuestStepProgress command error: spawn is not valid", lua_interface->GetScriptName(state));
  8106. return 0;
  8107. }
  8108. if (!player->IsPlayer()) {
  8109. lua_interface->LogError("%s: LUA GetQuestStepProgress command error: spawn is not a player", lua_interface->GetScriptName(state));
  8110. return 0;
  8111. }
  8112. lua_interface->SetInt32Value(state, ((Player*)player)->GetStepProgress(quest_id, step_id));
  8113. return 1;
  8114. }
  8115. int EQ2Emu_lua_SetPlayerLevel(lua_State* state) {
  8116. if (!lua_interface)
  8117. return 0;
  8118. Spawn* player = lua_interface->GetSpawn(state);
  8119. int8 level = lua_interface->GetInt8Value(state, 2);
  8120. if (!player) {
  8121. lua_interface->LogError("%s: LUA SetPlayerLevel command error: spawn is not valid", lua_interface->GetScriptName(state));
  8122. return 0;
  8123. }
  8124. if (!player->IsPlayer()) {
  8125. lua_interface->LogError("%s: LUA SetPlayerLevel command error: spawn is not a player", lua_interface->GetScriptName(state));
  8126. return 0;
  8127. }
  8128. if (level == 0) {
  8129. lua_interface->LogError("%s: LUA SetPlayerLevel command error: new level can't be 0", lua_interface->GetScriptName(state));
  8130. return 0;
  8131. }
  8132. Client* client = player->GetZone()->GetClientBySpawn(player);
  8133. if (!client) {
  8134. lua_interface->LogError("%s: LUA SetPlayerLevel command error: unable to get a client from the given spawn", lua_interface->GetScriptName(state));
  8135. return 0;
  8136. }
  8137. client->ChangeLevel(client->GetPlayer()->GetLevel(), level);
  8138. client->GetPlayer()->SetXP(1);
  8139. client->GetPlayer()->SetNeededXP();
  8140. return 0;
  8141. }
  8142. int EQ2Emu_lua_AddCoin(lua_State* state) {
  8143. if (!lua_interface)
  8144. return 0;
  8145. Spawn* player = lua_interface->GetSpawn(state);
  8146. int32 amount = lua_interface->GetInt32Value(state, 2);
  8147. if (!player) {
  8148. lua_interface->LogError("%s: LUA AddCoin command error: spawn is not valid", lua_interface->GetScriptName(state));
  8149. return 0;
  8150. }
  8151. if (!player->IsPlayer()) {
  8152. lua_interface->LogError("%s: LUA AddCoin command error: spawn is not a player", lua_interface->GetScriptName(state));
  8153. return 0;
  8154. }
  8155. if (amount == 0) {
  8156. lua_interface->LogError("%s: LUA AddCoin command error: amount must be greater then 0", lua_interface->GetScriptName(state));
  8157. return 0;
  8158. }
  8159. ((Player*)player)->AddCoins(amount);
  8160. return 0;
  8161. }
  8162. int EQ2Emu_lua_RemoveCoin(lua_State* state) {
  8163. if (!lua_interface)
  8164. return 0;
  8165. Spawn* player = lua_interface->GetSpawn(state);
  8166. int32 amount = lua_interface->GetInt32Value(state, 2);
  8167. if (!player) {
  8168. lua_interface->LogError("%s: LUA RemoveCoin command error: spawn is not valid", lua_interface->GetScriptName(state));
  8169. return 0;
  8170. }
  8171. if (!player->IsPlayer()) {
  8172. lua_interface->LogError("%s: LUA RemoveCoin command error: spawn is not a player", lua_interface->GetScriptName(state));
  8173. return 0;
  8174. }
  8175. if (amount == 0) {
  8176. lua_interface->LogError("%s: LUA RemoveCoin command error: amount must be greater then 0", lua_interface->GetScriptName(state));
  8177. return 0;
  8178. }
  8179. lua_interface->SetBooleanValue(state, ((Player*)player)->RemoveCoins(amount));
  8180. return 1;
  8181. }
  8182. int EQ2Emu_lua_GetPlayersInZone(lua_State* state) {
  8183. if (!lua_interface)
  8184. return 0;
  8185. ZoneServer* zone = lua_interface->GetZone(state);
  8186. if (!zone) {
  8187. lua_interface->LogError("%s: LUA GetPlayersInZone command error: zone is not valid", lua_interface->GetScriptName(state));
  8188. return 0;
  8189. }
  8190. vector<Entity*> players = zone->GetPlayers();
  8191. if (players.size() == 0)
  8192. return 0;
  8193. lua_createtable(state, players.size(), 0);
  8194. int newTable = lua_gettop(state);
  8195. for (int32 i = 0; i < players.size(); i++) {
  8196. lua_interface->SetSpawnValue(state, players.at(i));
  8197. lua_rawseti(state, newTable, i + 1);
  8198. }
  8199. return 1;
  8200. }
  8201. int EQ2Emu_lua_SpawnGroupByID(lua_State* state) {
  8202. if (!lua_interface)
  8203. return 0;
  8204. ZoneServer* zone = lua_interface->GetZone(state, 1);
  8205. if (!zone) {
  8206. lua_interface->LogError("%s: LUA GetPlayersInZone command error: zone is not valid", lua_interface->GetScriptName(state));
  8207. return 0;
  8208. }
  8209. int32 group_id = lua_interface->GetInt32Value(state, 2);
  8210. //Map of <placement_id, location_id>
  8211. map<int32, int32>* locs = zone->GetSpawnLocationsByGroup(group_id);
  8212. map<int32, int32>::iterator itr;
  8213. vector<Spawn*> group;
  8214. for (itr = locs->begin(); itr != locs->end(); itr++) {
  8215. SpawnLocation* location = zone->GetSpawnLocation(itr->second);
  8216. if (!location) {
  8217. lua_interface->LogError("LUA SpawnByLocationID command error: no location found for the given ID (%u)", itr->second);
  8218. return 0;
  8219. }
  8220. Spawn* spawn = 0;
  8221. if (location->entities[0]) {
  8222. if (location->entities[0]->spawn_type == SPAWN_ENTRY_TYPE_NPC)
  8223. spawn = zone->AddNPCSpawn(location, location->entities[0]);
  8224. else if (location->entities[0]->spawn_type == SPAWN_ENTRY_TYPE_GROUNDSPAWN)
  8225. spawn = zone->AddGroundSpawn(location, location->entities[0]);
  8226. else if (location->entities[0]->spawn_type == SPAWN_ENTRY_TYPE_OBJECT)
  8227. spawn = zone->AddObjectSpawn(location, location->entities[0]);
  8228. else if (location->entities[0]->spawn_type == SPAWN_ENTRY_TYPE_WIDGET)
  8229. spawn = zone->AddWidgetSpawn(location, location->entities[0]);
  8230. else if (location->entities[0]->spawn_type == SPAWN_ENTRY_TYPE_SIGN)
  8231. spawn = zone->AddSignSpawn(location, location->entities[0]);
  8232. if (spawn) {
  8233. const char* script = 0;
  8234. for (int x = 0; x < 3; x++) {
  8235. switch (x) {
  8236. case 0:
  8237. script = world.GetSpawnEntryScript(location->entities[0]->spawn_entry_id);
  8238. break;
  8239. case 1:
  8240. script = world.GetSpawnLocationScript(location->entities[0]->spawn_location_id);
  8241. break;
  8242. case 2:
  8243. script = world.GetSpawnScript(location->entities[0]->spawn_id);
  8244. break;
  8245. }
  8246. if (script && lua_interface->GetSpawnScript(script) != 0) {
  8247. spawn->SetSpawnScript(string(script));
  8248. break;
  8249. }
  8250. }
  8251. zone->CallSpawnScript(spawn, SPAWN_SCRIPT_SPAWN);
  8252. lua_interface->SetSpawnValue(state, spawn);
  8253. group.push_back(spawn);
  8254. }
  8255. else {
  8256. LogWrite(ZONE__ERROR, 0, "Zone", "Error adding spawn to zone");
  8257. safe_delete(spawn);
  8258. }
  8259. }
  8260. }
  8261. if (!group.empty()) {
  8262. lua_createtable(state, group.size(), 0);
  8263. int newTable = lua_gettop(state);
  8264. for (int32 i = 0; i < group.size(); i++) {
  8265. lua_interface->SetSpawnValue(state, group[i]);
  8266. lua_rawseti(state, newTable, i + 1);
  8267. }
  8268. }
  8269. else
  8270. lua_pushnil(state);
  8271. return 1;
  8272. }
  8273. int EQ2Emu_lua_SetSpawnAnimation(lua_State* state) {
  8274. if (!lua_interface)
  8275. return 0;
  8276. Spawn* spawn = lua_interface->GetSpawn(state, 1);
  8277. int32 anim_id = lua_interface->GetInt32Value(state, 2);
  8278. int16 leeway = lua_interface->GetInt16Value(state, 3);
  8279. if (!spawn) {
  8280. lua_interface->LogError("%s: LUA SetSpawnAnimation command error: spawn is not valid", lua_interface->GetScriptName(state));
  8281. return 0;
  8282. }
  8283. if (anim_id == 0) {
  8284. lua_interface->LogError("%s: LUA SetSpawnAnimation command error: anim_id is not valid", lua_interface->GetScriptName(state));
  8285. return 0;
  8286. }
  8287. if (leeway == 0)
  8288. leeway = 5000;
  8289. spawn->SetSpawnAnim(anim_id);
  8290. spawn->SetSpawnAnimLeeway(leeway);
  8291. return 0;
  8292. }
  8293. int EQ2Emu_lua_GetClientVersion(lua_State* state) {
  8294. if (!lua_interface)
  8295. return 0;
  8296. Spawn* player = lua_interface->GetSpawn(state);
  8297. if (!player) {
  8298. return 0;
  8299. }
  8300. Client* client = player->GetZone()->GetClientBySpawn(player);
  8301. if (!client) {
  8302. return 0;
  8303. }
  8304. lua_interface->SetInt32Value(state, client->GetVersion());
  8305. return 1;
  8306. }
  8307. int EQ2Emu_lua_GetItemID(lua_State* state) {
  8308. if (!lua_interface)
  8309. return 0;
  8310. Item* item = lua_interface->GetItem(state);
  8311. if (!item) {
  8312. lua_interface->LogError("%s: LUA GetItemID command error: item is not valid", lua_interface->GetScriptName(state));
  8313. return 0;
  8314. }
  8315. lua_interface->SetInt32Value(state, item->details.item_id);
  8316. return 1;
  8317. }
  8318. int EQ2Emu_lua_IsEntity(lua_State* state) {
  8319. if (!lua_interface)
  8320. return 0;
  8321. Spawn* spawn = lua_interface->GetSpawn(state);
  8322. if (!spawn) {
  8323. lua_interface->LogError("%s: LUA IsEntity command error: spawn is not valid", lua_interface->GetScriptName(state));
  8324. return 0;
  8325. }
  8326. lua_interface->SetBooleanValue(state, spawn->IsEntity());
  8327. return 1;
  8328. }
  8329. int EQ2Emu_lua_GetOrigX(lua_State* state) {
  8330. if (!lua_interface)
  8331. return 0;
  8332. Spawn* spawn = lua_interface->GetSpawn(state);
  8333. if (!spawn) {
  8334. lua_interface->LogError("%s: LUA GetOrigX command error: spawn is not valid", lua_interface->GetScriptName(state));
  8335. return 0;
  8336. }
  8337. lua_interface->SetFloatValue(state, spawn->GetSpawnOrigX());
  8338. return 1;
  8339. }
  8340. int EQ2Emu_lua_GetOrigY(lua_State* state) {
  8341. if (!lua_interface)
  8342. return 0;
  8343. Spawn* spawn = lua_interface->GetSpawn(state);
  8344. if (!spawn) {
  8345. lua_interface->LogError("%s: LUA GetOrigY command error: spawn is not valid", lua_interface->GetScriptName(state));
  8346. return 0;
  8347. }
  8348. lua_interface->SetFloatValue(state, spawn->GetSpawnOrigY());
  8349. return 1;
  8350. }
  8351. int EQ2Emu_lua_GetOrigZ(lua_State* state) {
  8352. if (!lua_interface)
  8353. return 0;
  8354. Spawn* spawn = lua_interface->GetSpawn(state);
  8355. if (!spawn) {
  8356. lua_interface->LogError("%s: LUA GetOrigZ command error: spawn is not valid", lua_interface->GetScriptName(state));
  8357. return 0;
  8358. }
  8359. lua_interface->SetFloatValue(state, spawn->GetSpawnOrigZ());
  8360. return 1;
  8361. }
  8362. int EQ2Emu_lua_GetPCTOfHP(lua_State* state) {
  8363. if (!lua_interface)
  8364. return 0;
  8365. Spawn* spawn = lua_interface->GetSpawn(state);
  8366. float pct = lua_interface->GetFloatValue(state, 2);
  8367. if (!spawn) {
  8368. lua_interface->LogError("%s: LUA GetPCTOfHP command error: spawn is not valid", lua_interface->GetScriptName(state));
  8369. return 0;
  8370. }
  8371. if (pct == 0) {
  8372. lua_interface->LogError("%s: LUA GetPCTOfHP command error: pct is not valid", lua_interface->GetScriptName(state));
  8373. return 0;
  8374. }
  8375. int32 amount = std::round(spawn->GetTotalHP() * (pct / 100));
  8376. lua_interface->SetInt32Value(state, amount);
  8377. return 1;
  8378. }
  8379. int EQ2Emu_lua_GetPCTOfPower(lua_State* state) {
  8380. if (!lua_interface)
  8381. return 0;
  8382. Spawn* spawn = lua_interface->GetSpawn(state);
  8383. float pct = lua_interface->GetFloatValue(state, 2);
  8384. if (!spawn) {
  8385. lua_interface->LogError("%s: LUA GetPCTOfPower command error: spawn is not valid", lua_interface->GetScriptName(state));
  8386. return 0;
  8387. }
  8388. if (pct == 0) {
  8389. lua_interface->LogError("%s: LUA GetPCTOfPower command error: pct is not valid", lua_interface->GetScriptName(state));
  8390. return 0;
  8391. }
  8392. int32 amount = std::round(spawn->GetTotalPower() * (pct / 100));
  8393. lua_interface->SetInt32Value(state, amount);
  8394. return 1;
  8395. }
  8396. int EQ2Emu_lua_GetBoundZoneID(lua_State* state) {
  8397. if (!lua_interface)
  8398. return 0;
  8399. Spawn* spawn = lua_interface->GetSpawn(state);
  8400. if (!spawn) {
  8401. lua_interface->LogError("%s: LUA GetBoundZoneID command error: spawn is not valid", lua_interface->GetScriptName(state));
  8402. return 0;
  8403. }
  8404. if (!spawn->IsPlayer()) {
  8405. lua_interface->LogError("%s: LUA GetBoundZoneID command error: spawn is not a player", lua_interface->GetScriptName(state));
  8406. return 0;
  8407. }
  8408. lua_interface->SetInt32Value(state, ((Player*)spawn)->GetPlayerInfo()->GetBindZoneID());
  8409. return 1;
  8410. }
  8411. int EQ2Emu_lua_Evac(lua_State* state) {
  8412. if (!lua_interface)
  8413. return 0;
  8414. Spawn* target = lua_interface->GetSpawn(state);
  8415. if (target) {
  8416. float x = target->GetZone()->GetSafeX();
  8417. float y = target->GetZone()->GetSafeY();
  8418. float z = target->GetZone()->GetSafeZ();
  8419. float h = target->GetZone()->GetSafeHeading();
  8420. target->SetX(x);
  8421. target->SetY(y);
  8422. target->SetZ(z);
  8423. target->SetHeading(h);
  8424. target->SetSpawnOrigX(target->GetX());
  8425. target->SetSpawnOrigY(target->GetY());
  8426. target->SetSpawnOrigZ(target->GetZ());
  8427. target->SetSpawnOrigHeading(target->GetHeading());
  8428. if (target->IsPlayer()) {
  8429. Client* client = target->GetZone()->GetClientBySpawn(target);
  8430. if (client) {
  8431. PacketStruct* packet = configReader.getStruct("WS_TeleportWithinZone", client->GetVersion());
  8432. if (packet)
  8433. {
  8434. packet->setDataByName("x", x);
  8435. packet->setDataByName("y", y);
  8436. packet->setDataByName("z", z);
  8437. client->QueuePacket(packet->serialize());
  8438. safe_delete(packet);
  8439. }
  8440. }
  8441. }
  8442. }
  8443. else {
  8444. LuaSpell* spell = lua_interface->GetCurrentSpell(state);
  8445. ZoneServer* zone = spell->caster->GetZone();
  8446. float x = spell->caster->GetZone()->GetSafeX();
  8447. float y = spell->caster->GetZone()->GetSafeY();
  8448. float z = spell->caster->GetZone()->GetSafeZ();
  8449. float h = spell->caster->GetZone()->GetSafeHeading();
  8450. spell->MSpellTargets.readlock(__FUNCTION__, __LINE__);
  8451. for (int32 i = 0; i < spell->targets.size(); i++) {
  8452. Spawn* target2 = zone->GetSpawnByID(spell->targets.at(i));
  8453. if (!target2)
  8454. continue;
  8455. target2->SetX(x);
  8456. target2->SetY(y);
  8457. target2->SetZ(z);
  8458. target2->SetHeading(h);
  8459. target2->SetSpawnOrigX(target2->GetX());
  8460. target2->SetSpawnOrigY(target2->GetY());
  8461. target2->SetSpawnOrigZ(target2->GetZ());
  8462. target2->SetSpawnOrigHeading(target2->GetHeading());
  8463. if (target2->IsPlayer()) {
  8464. Client* client = target2->GetZone()->GetClientBySpawn(target2);
  8465. if (client) {
  8466. PacketStruct* packet = configReader.getStruct("WS_TeleportWithinZone", client->GetVersion());
  8467. if (packet)
  8468. {
  8469. packet->setDataByName("x", x);
  8470. packet->setDataByName("y", y);
  8471. packet->setDataByName("z", z);
  8472. client->QueuePacket(packet->serialize());
  8473. safe_delete(packet);
  8474. }
  8475. }
  8476. }
  8477. }
  8478. spell->MSpellTargets.releasereadlock(__FUNCTION__, __LINE__);
  8479. }
  8480. return 0;
  8481. }
  8482. int EQ2Emu_lua_GetSpellTier(lua_State* state) {
  8483. if (!lua_interface)
  8484. return 0;
  8485. LuaSpell* luaspell = lua_interface->GetCurrentSpell(state);
  8486. if (!luaspell) {
  8487. lua_interface->LogError("%s: LUA GetSpellTier command error: must be used in a spell script", lua_interface->GetScriptName(state));
  8488. return 0;
  8489. }
  8490. int8 tier = luaspell->spell->GetSpellTier();
  8491. lua_interface->SetInt32Value(state, tier);
  8492. return 1;
  8493. }
  8494. int EQ2Emu_lua_GetSpellID(lua_State* state) {
  8495. if (!lua_interface)
  8496. return 0;
  8497. LuaSpell* luaspell = lua_interface->GetCurrentSpell(state);
  8498. if (!luaspell) {
  8499. lua_interface->LogError("%s: LUA GetSpellID command error: must be used in a spell script", lua_interface->GetScriptName(state));
  8500. return 0;
  8501. }
  8502. int32 spell_id = luaspell->spell->GetSpellID();
  8503. lua_interface->SetInt32Value(state, spell_id);
  8504. return 1;
  8505. }
  8506. int EQ2Emu_lua_StartTransmute(lua_State* state) {
  8507. if (!lua_interface)
  8508. return 0;
  8509. Spawn* spawn = lua_interface->GetSpawn(state, 1);
  8510. if (!spawn) {
  8511. lua_interface->LogError("%s: Lua StartTransmute command error: no spawn", lua_interface->GetScriptName(state));
  8512. return 0;
  8513. }
  8514. if (!spawn->IsPlayer()) {
  8515. lua_interface->LogError("%s: Lua StartTransmute command error: spawn is not a player", lua_interface->GetScriptName(state));
  8516. return 0;
  8517. }
  8518. ZoneServer* zone = spawn->GetZone();
  8519. if (!zone) {
  8520. return 0;
  8521. }
  8522. Client* client = zone->GetClientBySpawn(spawn);
  8523. if (!client) {
  8524. return 0;
  8525. }
  8526. Transmute::CreateItemRequest(client, static_cast<Player*>(spawn));
  8527. return 0;
  8528. }
  8529. int EQ2Emu_lua_CompleteTransmute(lua_State* state) {
  8530. if (!lua_interface)
  8531. return 0;
  8532. Spawn* spawn = lua_interface->GetSpawn(state, 1);
  8533. if (!spawn) {
  8534. lua_interface->LogError("%s: Lua CompleteTransmute command error: no spawn", lua_interface->GetScriptName(state));
  8535. return 0;
  8536. }
  8537. if (!spawn->IsPlayer()) {
  8538. lua_interface->LogError("%s: Lua CompleteTransmute command error: spawn is not a player", lua_interface->GetScriptName(state));
  8539. return 0;
  8540. }
  8541. ZoneServer* zone = spawn->GetZone();
  8542. if (!zone) {
  8543. return 0;
  8544. }
  8545. Client* client = zone->GetClientBySpawn(spawn);
  8546. if (!client) {
  8547. return 0;
  8548. }
  8549. Transmute::CompleteTransmutation(client, static_cast<Player*>(spawn));
  8550. return 0;
  8551. }
  8552. int EQ2Emu_lua_ProcHate(lua_State* state) {
  8553. if (!lua_interface)
  8554. return 0;
  8555. Spawn* caster = lua_interface->GetSpawn(state);
  8556. Spawn* target = lua_interface->GetSpawn(state, 2);
  8557. int32 threat_amt = lua_interface->GetInt32Value(state, 3);
  8558. string spell_name = lua_interface->GetStringValue(state, 4);
  8559. if (!caster) {
  8560. lua_interface->LogError("%s: LUA ProcHate command error: caster is not a valid spawn", lua_interface->GetScriptName(state));
  8561. return 0;
  8562. }
  8563. if (!caster->IsEntity()) {
  8564. lua_interface->LogError("%s: LUA ProcHate command error: caster is not an entity", lua_interface->GetScriptName(state));
  8565. return 0;
  8566. }
  8567. if (!target) {
  8568. lua_interface->LogError("%s: LUA ProcHate command error: target is not a valid spawn", lua_interface->GetScriptName(state));
  8569. return 0;
  8570. }
  8571. if (!target->IsEntity()) {
  8572. lua_interface->LogError("%s: LUA ProcHate command error: target is not an entity", lua_interface->GetScriptName(state));
  8573. return 0;
  8574. }
  8575. static_cast<Entity*>(target)->AddHate(static_cast<Entity*>(caster), threat_amt);
  8576. caster->GetZone()->SendThreatPacket(static_cast<Entity*>(caster), target, threat_amt, spell_name.c_str());
  8577. return 0;
  8578. }
  8579. int EQ2Emu_lua_AddLootToObject(lua_State* state) {
  8580. if (!lua_interface)
  8581. return 0;
  8582. Spawn* object = lua_interface->GetSpawn(state);
  8583. Spawn* player = lua_interface->GetSpawn(state, 2);
  8584. if (object && player && player->IsPlayer()) {
  8585. int32 coins = lua_interface->GetInt32Value(state, 3);
  8586. vector<Item*>* items = 0;
  8587. int i = 0;
  8588. int32 item_id = 0;
  8589. while ((item_id = lua_interface->GetInt32Value(state, 4 + i))) {
  8590. if (items == 0)
  8591. items = new vector<Item*>;
  8592. if (master_item_list.GetItem(item_id))
  8593. items->push_back(master_item_list.GetItem(item_id));
  8594. i++;
  8595. }
  8596. Client* client = 0;
  8597. client = object->GetZone()->GetClientBySpawn(player);
  8598. if (client) {
  8599. ((Player*)player)->AddPendingLootItems(object->GetID(), items);
  8600. }
  8601. safe_delete(items);
  8602. }
  8603. return 0;
  8604. }
  8605. int EQ2Emu_lua_GiveExp(lua_State* state) {
  8606. if (!lua_interface)
  8607. return 0;
  8608. Spawn* player = lua_interface->GetSpawn(state);
  8609. int32 amount = lua_interface->GetInt32Value(state, 2);
  8610. if (player && player->IsPlayer() && amount > 0) {
  8611. ((Player*)player)->AddXP(amount);
  8612. ((Player*)player)->SetCharSheetChanged(true);
  8613. Client* client = player->GetZone()->GetClientBySpawn(player);
  8614. if (client) {
  8615. client->SimpleMessage(CHANNEL_REWARD, "You gain experience!");
  8616. }
  8617. }
  8618. return 0;
  8619. }
  8620. int EQ2Emu_lua_DisplayText(lua_State* state) {
  8621. if (!lua_interface)
  8622. return 0;
  8623. Spawn* player = lua_interface->GetSpawn(state);
  8624. int8 type = lua_interface->GetInt8Value(state, 2);
  8625. string text = lua_interface->GetStringValue(state, 3);
  8626. Client* client = 0;
  8627. if (player && player->IsPlayer())
  8628. client = player->GetZone()->GetClientBySpawn(player);
  8629. if (!client || text.length() == 0) {
  8630. lua_interface->LogError("%s: LUA DisplayText required parameters not given", lua_interface->GetScriptName(state));
  8631. return 0;
  8632. }
  8633. client->SimpleMessage(type, text.c_str());
  8634. return 0;
  8635. }
  8636. int EQ2Emu_lua_ShowLootWindow(lua_State* state) {
  8637. if (!lua_interface)
  8638. return 0;
  8639. Spawn* player = lua_interface->GetSpawn(state);
  8640. Spawn* spawn = lua_interface->GetSpawn(state, 2);
  8641. Client* client = 0;
  8642. if (player && player->IsPlayer())
  8643. client = player->GetZone()->GetClientBySpawn(player);
  8644. if (!client || !spawn) {
  8645. lua_interface->LogError("%s: LUA ShowLootWindow required parameters not given", lua_interface->GetScriptName(state));
  8646. return 0;
  8647. }
  8648. vector<Item*>* items = ((Player*)player)->GetPendingLootItems(spawn->GetID());
  8649. if (!items) {
  8650. lua_interface->LogError("%s: LUA ShowLootWindow has no items", lua_interface->GetScriptName(state));
  8651. return 0;
  8652. }
  8653. client->Loot(spawn->GetLootCoins(), items, spawn);
  8654. return 0;
  8655. }
  8656. int EQ2Emu_lua_GetRandomSpawnByID(lua_State* state) {
  8657. if (!lua_interface)
  8658. return 0;
  8659. Spawn* spawnref = lua_interface->GetSpawn(state);
  8660. int32 spawn_id = lua_interface->GetInt32Value(state, 2);
  8661. if (spawn_id > 0 && spawnref) {
  8662. vector<Spawn*> spawns = spawnref->GetZone()->GetSpawnsByID(spawn_id);
  8663. if (spawns.size() == 0) {
  8664. lua_interface->LogError("%s: LUA EQ2Emu_lua_GetRandomSpawnByID command error: GetSpawnsByID returned no spawns", lua_interface->GetScriptName(state));
  8665. return 0;
  8666. }
  8667. Spawn* spawn = 0;
  8668. int16 index = MakeRandomInt(0, spawns.size());
  8669. if (index >= spawns.size() || index < 0)
  8670. index = 0;
  8671. spawn = spawns[index];
  8672. lua_interface->SetSpawnValue(state, spawn);
  8673. return 1;
  8674. }
  8675. else {
  8676. lua_interface->LogError("%s: LUA GetRandomSpawnByID required parameters not given", lua_interface->GetScriptName(state));
  8677. }
  8678. return 0;
  8679. }
  8680. int EQ2Emu_lua_AddPrimaryEntityCommandAllSpawns(lua_State* state) {
  8681. Spawn* player = lua_interface->GetSpawn(state);
  8682. int32 spawn_id = lua_interface->GetInt32Value(state, 2);
  8683. string name = lua_interface->GetStringValue(state, 3);
  8684. float distance = lua_interface->GetFloatValue(state, 4);
  8685. string command = lua_interface->GetStringValue(state, 5);
  8686. string error_text = lua_interface->GetStringValue(state, 6);
  8687. int16 cast_time = lua_interface->GetInt16Value(state, 7);
  8688. int32 spell_visual = lua_interface->GetInt32Value(state, 8);
  8689. if (spawn_id && player && player->IsPlayer() && name.length() > 0) {
  8690. if (distance == 0)
  8691. distance = 10.0f;
  8692. if (command.length() == 0)
  8693. command = name;
  8694. vector<Spawn*> spawns = player->GetZone()->GetSpawnsByID(spawn_id);
  8695. if (spawns.size() == 0) {
  8696. lua_interface->LogError("%s: LUA AddPrimaryEntityCommandAllSpawns command error: GetSpawnsByID returned no spawns", lua_interface->GetScriptName(state));
  8697. return 0;
  8698. }
  8699. Spawn* spawn = 0;
  8700. for (vector<Spawn*>::iterator itr = spawns.begin(); itr != spawns.end(); itr++) {
  8701. spawn = *itr;
  8702. if (spawn) {
  8703. spawn->AddPrimaryEntityCommand(name.c_str(), distance, command.c_str(), error_text.c_str(), cast_time, spell_visual);
  8704. player->GetZone()->SendUpdateDefaultCommand(spawn, command.c_str(), distance);
  8705. }
  8706. }
  8707. }
  8708. return 0;
  8709. }
  8710. int EQ2Emu_lua_InstructionWindowGoal(lua_State* state) {
  8711. if (!lua_interface)
  8712. return 0;
  8713. Client* client = 0;
  8714. Spawn* player = lua_interface->GetSpawn(state);
  8715. int8 goal_num = lua_interface->GetInt8Value(state, 2);
  8716. if (player && player->IsPlayer() && player->GetZone())
  8717. client = player->GetZone()->GetClientBySpawn(player);
  8718. else{
  8719. lua_interface->LogError("LUA InstructionWindowGoal command error: player is not valid");
  8720. return 0;
  8721. }
  8722. if (client) {
  8723. PacketStruct* packet = configReader.getStruct("WS_InstructionWindow", client->GetVersion());
  8724. if (packet) {
  8725. packet->setDataByName("goal_num", goal_num);
  8726. client->QueuePacket(packet->serialize());
  8727. safe_delete(packet);
  8728. }
  8729. }
  8730. return 0;
  8731. }
  8732. int EQ2Emu_lua_InstructionWindowClose(lua_State* state) {
  8733. if (!lua_interface)
  8734. return 0;
  8735. Client* client = 0;
  8736. Spawn* player = lua_interface->GetSpawn(state);
  8737. if (player && player->IsPlayer() && player->GetZone())
  8738. client = player->GetZone()->GetClientBySpawn(player);
  8739. else {
  8740. lua_interface->LogError("LUA InstructionWindowClose command error: player is not valid");
  8741. return 0;
  8742. }
  8743. if (client) {
  8744. client->QueuePacket(new EQ2Packet(OP_EqInstructionWindowCloseCmd, 0, 0));
  8745. }
  8746. return 0;
  8747. }
  8748. int EQ2Emu_lua_InstructionWindow(lua_State* state) {
  8749. if (!lua_interface)
  8750. return 0;
  8751. Client* client = 0;
  8752. Spawn* player = lua_interface->GetSpawn(state);
  8753. float duration = lua_interface->GetFloatValue(state, 2);
  8754. string text = lua_interface->GetStringValue(state, 3);
  8755. string voice = lua_interface->GetStringValue(state, 4);
  8756. int32 voice_key1 = lua_interface->GetInt32Value(state, 5);
  8757. int32 voice_key2 = lua_interface->GetInt32Value(state, 6);
  8758. string signal = lua_interface->GetStringValue(state, 7);
  8759. string goal1 = lua_interface->GetStringValue(state, 8);
  8760. string task1 = lua_interface->GetStringValue(state, 9);
  8761. string goal2 = lua_interface->GetStringValue(state, 10);
  8762. string task2 = lua_interface->GetStringValue(state, 11);
  8763. string goal3 = lua_interface->GetStringValue(state, 12);
  8764. string task3 = lua_interface->GetStringValue(state, 13);
  8765. string goal4 = lua_interface->GetStringValue(state, 14);
  8766. string task4 = lua_interface->GetStringValue(state, 15);
  8767. if (!player) {
  8768. lua_interface->LogError("LUA InstructionWindow command error: spawn is not valid");
  8769. return 0;
  8770. }
  8771. if (!player->IsPlayer()) {
  8772. lua_interface->LogError("LUA InstructionWindow command error: spawn is not a player");
  8773. return 0;
  8774. }
  8775. else
  8776. client = ((Player*)player)->GetClient();
  8777. if (!client) {
  8778. lua_interface->LogError("LUA InstructionWindow command error: could not find client");
  8779. return 0;
  8780. }
  8781. if (text.length() == 0 || task1.length() == 0 || signal.length() == 0) {
  8782. lua_interface->LogError("LUA InstructionWindow required parameters not given");
  8783. return 0;
  8784. }
  8785. if (duration >= 0 && duration < 2)
  8786. duration = 2;
  8787. PacketStruct* packet = configReader.getStruct("WS_InstructionWindow", client->GetVersion());
  8788. if (packet) {
  8789. packet->setDataByName("open_seconds_max", duration);
  8790. packet->setDataByName("text", text.c_str());
  8791. packet->setDataByName("voice", voice.c_str());
  8792. int8 num_goals = 1;
  8793. if (task2.length() > 0)
  8794. num_goals++;
  8795. if (task3.length() > 0)
  8796. num_goals++;
  8797. if (task4.length() > 0)
  8798. num_goals++;
  8799. packet->setArrayLengthByName("num_goals", num_goals);
  8800. for (int8 i = 0; i < num_goals; i++) {
  8801. packet->setSubArrayLengthByName("num_tasks", 1, i);
  8802. }
  8803. if (goal1.length() > 0)
  8804. packet->setArrayDataByName("goal_text", goal1.c_str());
  8805. if (goal2.length() > 0)
  8806. packet->setArrayDataByName("goal_text", goal2.c_str(), 1);
  8807. if (goal3.length() > 0)
  8808. packet->setArrayDataByName("goal_text", goal3.c_str(), 2);
  8809. if (goal4.length() > 0)
  8810. packet->setArrayDataByName("goal_text", goal4.c_str(), 3);
  8811. packet->setSubArrayDataByName("task_text", task1.c_str());
  8812. if (task2.length() > 0)
  8813. packet->setSubArrayDataByName("task_text", task2.c_str(), 1);
  8814. if (task3.length() > 0)
  8815. packet->setSubArrayDataByName("task_text", task3.c_str(), 2);
  8816. if (task4.length() > 0)
  8817. packet->setSubArrayDataByName("task_text", task4.c_str(), 3);
  8818. packet->setDataByName("complete_sound", "click");
  8819. packet->setDataByName("signal", signal.c_str());
  8820. packet->setDataByName("voice_key1", voice_key1);
  8821. packet->setDataByName("voice_key2", voice_key2);
  8822. client->QueuePacket(packet->serialize());
  8823. safe_delete(packet);
  8824. }
  8825. return 0;
  8826. }
  8827. int EQ2Emu_lua_ShowWindow(lua_State* state) {
  8828. if (!lua_interface)
  8829. return 0;
  8830. Client* client = 0;
  8831. Spawn* player = lua_interface->GetSpawn(state);
  8832. string window = lua_interface->GetStringValue(state, 2);
  8833. int8 show = lua_interface->GetInt8Value(state, 3);
  8834. if (!player) {
  8835. lua_interface->LogError("LUA ShowWindow command error: spawn is not valid");
  8836. return 0;
  8837. }
  8838. if (!player->IsPlayer()) {
  8839. lua_interface->LogError("LUA ShowWindow command error: spawn is not a player");
  8840. return 0;
  8841. }
  8842. else
  8843. client = ((Player*)player)->GetClient();
  8844. if (!client) {
  8845. lua_interface->LogError("LUA ShowWindow command error: could not find client");
  8846. return 0;
  8847. }
  8848. if (window.length() == 0) {
  8849. lua_interface->LogError("LUA ShowWindow required parameters not given");
  8850. return 0;
  8851. }
  8852. PacketStruct* packet = configReader.getStruct("WS_ShowWindow", client->GetVersion());
  8853. if (packet) {
  8854. packet->setDataByName("window", window.c_str());
  8855. packet->setDataByName("show", show);
  8856. client->QueuePacket(packet->serialize());
  8857. safe_delete(packet);
  8858. }
  8859. return 0;
  8860. }
  8861. int EQ2Emu_lua_EnableGameEvent(lua_State* state) {
  8862. //See GameEvents.txt for options that can be used for this function
  8863. if (!lua_interface)
  8864. return 0;
  8865. Client* client = 0;
  8866. Spawn* player = lua_interface->GetSpawn(state);
  8867. string event_name = lua_interface->GetStringValue(state, 2);
  8868. int8 enabled = lua_interface->GetInt8Value(state, 3);
  8869. if (!player || !player->IsPlayer()) {
  8870. lua_interface->LogError("LUA EnableGameEvent error: player is not valid");
  8871. return 0;
  8872. }
  8873. if (player->GetZone())
  8874. client = player->GetZone()->GetClientBySpawn(player);
  8875. if (!client) {
  8876. lua_interface->LogError("LUA EnableGameEvent error: could not find client");
  8877. return 0;
  8878. }
  8879. PacketStruct* packet = configReader.getStruct("WS_EnableGameEvent", client->GetVersion());
  8880. if (packet) {
  8881. packet->setDataByName("event_name", event_name.c_str());
  8882. packet->setDataByName("enabled", enabled);
  8883. client->QueuePacket(packet->serialize());
  8884. safe_delete(packet);
  8885. }
  8886. return 0;
  8887. }
  8888. int EQ2Emu_lua_GetTutorialStep(lua_State* state) {
  8889. if (!lua_interface)
  8890. return 0;
  8891. Spawn* player = lua_interface->GetSpawn(state);
  8892. if (player && player->IsPlayer()) {
  8893. lua_interface->SetInt32Value(state, ((Player*)player)->GetTutorialStep());
  8894. return 1;
  8895. }
  8896. return 0;
  8897. }
  8898. int EQ2Emu_lua_SetTutorialStep(lua_State* state) {
  8899. if (!lua_interface)
  8900. return 0;
  8901. Spawn* player = lua_interface->GetSpawn(state);
  8902. int8 step = lua_interface->GetInt8Value(state, 2);
  8903. if (player && player->IsPlayer() && step > 0) {
  8904. ((Player*)player)->SetTutorialStep(step);
  8905. }
  8906. return 0;
  8907. }
  8908. int EQ2Emu_lua_FlashWindow(lua_State* state) {
  8909. if (!lua_interface)
  8910. return 0;
  8911. Client* client = 0;
  8912. Spawn* player = lua_interface->GetSpawn(state);
  8913. string window = lua_interface->GetStringValue(state, 2);
  8914. float flash_seconds = lua_interface->GetFloatValue(state, 3);
  8915. if (!player) {
  8916. lua_interface->LogError("LUA FlashWindow command error: spawn is not valid");
  8917. return 0;
  8918. }
  8919. if (!player->IsPlayer()) {
  8920. lua_interface->LogError("LUA FlashWindow command error: spawn is not a player");
  8921. return 0;
  8922. }
  8923. else
  8924. client = ((Player*)player)->GetClient();
  8925. if (!client) {
  8926. lua_interface->LogError("LUA FlashWindow command error: could not find client");
  8927. return 0;
  8928. }
  8929. if (window.length() == 0) {
  8930. lua_interface->LogError("LUA FlashWindow required parameters not given");
  8931. return 0;
  8932. }
  8933. PacketStruct* packet = configReader.getStruct("WS_FlashWindow", client->GetVersion());
  8934. if (packet) {
  8935. packet->setDataByName("window", window.c_str());
  8936. packet->setDataByName("flash_seconds", flash_seconds);
  8937. client->QueuePacket(packet->serialize());
  8938. safe_delete(packet);
  8939. }
  8940. return 0;
  8941. }
  8942. int EQ2Emu_lua_CheckLOS(lua_State* state) {
  8943. if (!lua_interface)
  8944. return 0;
  8945. Spawn* spawn = lua_interface->GetSpawn(state);
  8946. Spawn* target = lua_interface->GetSpawn(state, 2);
  8947. if (spawn && target)
  8948. return spawn->CheckLoS(target);
  8949. return 0;
  8950. }
  8951. int EQ2Emu_lua_CheckLOSByCoordinates(lua_State* state) {
  8952. if (!lua_interface)
  8953. return 0;
  8954. Spawn* spawn = lua_interface->GetSpawn(state);
  8955. float x = lua_interface->GetFloatValue(state, 2);
  8956. float y = lua_interface->GetFloatValue(state, 3);
  8957. float z = lua_interface->GetFloatValue(state, 4);
  8958. if (spawn)
  8959. return spawn->CheckLoS(glm::vec3(spawn->GetX(), spawn->GetZ(), spawn->GetY() + 1.0f), glm::vec3(x, z, y+1.0f));
  8960. return 0;
  8961. }
  8962. int EQ2Emu_lua_SetZoneExpansionFlag(lua_State* state) {
  8963. if (!lua_interface)
  8964. return 0;
  8965. ZoneServer* zone = lua_interface->GetZone(state);
  8966. int32 xpackFlag = lua_interface->GetInt32Value(state, 2);
  8967. if (zone)
  8968. zone->SetExpansionFlag(xpackFlag);
  8969. return 0;
  8970. }
  8971. int EQ2Emu_lua_GetZoneExpansionFlag(lua_State* state) {
  8972. if (!lua_interface)
  8973. return 0;
  8974. ZoneServer* zone = lua_interface->GetZone(state);
  8975. if (zone) {
  8976. lua_interface->SetInt32Value(state, zone->GetExpansionFlag());
  8977. return 1;
  8978. }
  8979. return 0;
  8980. }
  8981. int EQ2Emu_lua_SetZoneHolidayFlag(lua_State* state) {
  8982. if (!lua_interface)
  8983. return 0;
  8984. ZoneServer* zone = lua_interface->GetZone(state);
  8985. int32 holidayFlag = lua_interface->GetInt32Value(state, 2);
  8986. if (zone)
  8987. zone->SetHolidayFlag(holidayFlag);
  8988. return 0;
  8989. }
  8990. int EQ2Emu_lua_GetZoneHolidayFlag(lua_State* state) {
  8991. if (!lua_interface)
  8992. return 0;
  8993. ZoneServer* zone = lua_interface->GetZone(state);
  8994. if (zone) {
  8995. lua_interface->SetInt32Value(state, zone->GetHolidayFlag());
  8996. return 1;
  8997. }
  8998. return 0;
  8999. }
  9000. int EQ2Emu_lua_AddSpawnProximity(lua_State* state) {
  9001. if (!lua_interface)
  9002. return 0;
  9003. Spawn* spawn = lua_interface->GetSpawn(state);
  9004. int32 spawn_value = lua_interface->GetInt32Value(state, 2);
  9005. int8 spawn_type = lua_interface->GetInt8Value(state, 3);
  9006. float distance = lua_interface->GetFloatValue(state, 4);
  9007. string in_range_function = lua_interface->GetStringValue(state, 5);
  9008. string leaving_range_function = lua_interface->GetStringValue(state, 6);
  9009. if (spawn && distance > 0 && in_range_function.length() > 0)
  9010. spawn->AddLUASpawnProximity(spawn_value, (Spawn::SpawnProximityType)spawn_type, distance, in_range_function, leaving_range_function);
  9011. return 0;
  9012. }
  9013. int EQ2Emu_lua_CanSeeInvis(lua_State* state) {
  9014. if (!lua_interface)
  9015. return 0;
  9016. Spawn* spawn = lua_interface->GetSpawn(state);
  9017. Spawn* target = lua_interface->GetSpawn(state, 2);
  9018. if (spawn && target)
  9019. {
  9020. if (spawn->IsPlayer() && target->IsEntity())
  9021. {
  9022. lua_interface->SetBooleanValue(state, ((Player*)spawn)->CanSeeInvis((Entity*)target));
  9023. return 1;
  9024. }
  9025. else if (spawn->IsEntity() && target->IsEntity())
  9026. {
  9027. lua_interface->SetBooleanValue(state, ((Entity*)spawn)->CanSeeInvis((Entity*)target));
  9028. return 1;
  9029. }
  9030. }
  9031. return 0;
  9032. }
  9033. int EQ2Emu_lua_SetSeeInvis(lua_State* state) {
  9034. if (!lua_interface)
  9035. return 0;
  9036. Spawn* spawn = lua_interface->GetSpawn(state);
  9037. bool val = (lua_interface->GetInt8Value(state, 2) == 1);
  9038. if (spawn && spawn->IsEntity())
  9039. {
  9040. ((Entity*)spawn)->SetSeeInvisSpell(val);
  9041. if (spawn->IsPlayer())
  9042. {
  9043. Client* client = spawn->GetZone()->GetClientBySpawn((Player*)spawn);
  9044. if (client)
  9045. ((Player*)spawn)->GetZone()->SendAllSpawnsForSeeInvisChange(client);
  9046. }
  9047. }
  9048. return 0;
  9049. }
  9050. int EQ2Emu_lua_SetSeeHide(lua_State* state) {
  9051. if (!lua_interface)
  9052. return 0;
  9053. Spawn* spawn = lua_interface->GetSpawn(state);
  9054. bool val = (lua_interface->GetInt8Value(state, 2) == 1);
  9055. if (spawn && spawn->IsEntity())
  9056. {
  9057. ((Entity*)spawn)->SetSeeHideSpell(val);
  9058. if (spawn->IsPlayer())
  9059. {
  9060. Client* client = spawn->GetZone()->GetClientBySpawn((Player*)spawn);
  9061. if (client)
  9062. ((Player*)spawn)->GetZone()->SendAllSpawnsForVisChange(client);
  9063. }
  9064. }
  9065. return 0;
  9066. }
  9067. int EQ2Emu_lua_SetAccessToEntityCommand(lua_State* state)
  9068. {
  9069. if (!lua_interface)
  9070. return 0;
  9071. Spawn* player = lua_interface->GetSpawn(state);
  9072. Spawn* spawn = lua_interface->GetSpawn(state, 2);
  9073. string command = lua_interface->GetStringValue(state, 3);
  9074. bool val = (lua_interface->GetInt8Value(state, 4) == 1);
  9075. if (spawn && player && player->IsPlayer())
  9076. {
  9077. EntityCommand* cmd = spawn->FindEntityCommand(string(command), true);
  9078. bool res = false;
  9079. if (cmd)
  9080. res = spawn->SetPermissionToEntityCommand(cmd, (Player*)player, val);
  9081. lua_interface->SetBooleanValue(state, res);
  9082. return 1;
  9083. }
  9084. return 0;
  9085. }
  9086. int EQ2Emu_lua_RemovePrimaryEntityCommand(lua_State* state)
  9087. {
  9088. if (!lua_interface)
  9089. return 0;
  9090. Spawn* spawn = lua_interface->GetSpawn(state);
  9091. string command = lua_interface->GetStringValue(state, 2);
  9092. if (spawn && command.length() > 0)
  9093. spawn->RemovePrimaryEntityCommand(command.c_str());
  9094. return 0;
  9095. }
  9096. int EQ2Emu_lua_SendUpdateDefaultCommand(lua_State* state) {
  9097. if (!lua_interface)
  9098. return 0;
  9099. Spawn* spawn = lua_interface->GetSpawn(state);
  9100. float distance = lua_interface->GetFloatValue(state, 2);
  9101. string command = lua_interface->GetStringValue(state, 3);
  9102. Spawn* player = lua_interface->GetSpawn(state, 4);
  9103. if (spawn) {
  9104. spawn->GetZone()->SendUpdateDefaultCommand(spawn, command.c_str(), distance, player);
  9105. }
  9106. return 0;
  9107. }
  9108. int EQ2Emu_lua_SendTransporters(lua_State* state) {
  9109. if (!lua_interface)
  9110. return 0;
  9111. Spawn* spawn = lua_interface->GetSpawn(state);
  9112. Spawn* player = lua_interface->GetSpawn(state, 2);
  9113. int32 transport_id = lua_interface->GetInt32Value(state, 3);
  9114. if (spawn && player && transport_id && player->IsPlayer()) {
  9115. Client* client = 0;
  9116. if (player && player->IsPlayer())
  9117. client = player->GetZone()->GetClientBySpawn(player);
  9118. if (!client)
  9119. return 0;
  9120. vector<TransportDestination*> destinations;
  9121. player->GetZone()->GetTransporters(&destinations, client, transport_id);
  9122. if (destinations.size())
  9123. {
  9124. client->SetTemporaryTransportID(transport_id);
  9125. client->ProcessTeleport(spawn, &destinations, transport_id);
  9126. }
  9127. else
  9128. client->Message(CHANNEL_COLOR_RED, "There are no transporters available (ID: %u)", transport_id);
  9129. }
  9130. return 0;
  9131. }
  9132. int EQ2Emu_lua_SetTemporaryTransportID(lua_State* state) {
  9133. if (!lua_interface)
  9134. return 0;
  9135. Spawn* player = lua_interface->GetSpawn(state);
  9136. int32 transport_id = lua_interface->GetInt32Value(state, 2);
  9137. if (player && player->IsPlayer()) {
  9138. Client* client = 0;
  9139. if (player && player->IsPlayer())
  9140. client = player->GetZone()->GetClientBySpawn(player);
  9141. if (!client)
  9142. return 0;
  9143. client->SetTemporaryTransportID(transport_id);
  9144. }
  9145. return 0;
  9146. }
  9147. int EQ2Emu_lua_GetTemporaryTransportID(lua_State* state) {
  9148. if (!lua_interface)
  9149. return 0;
  9150. Spawn* player = lua_interface->GetSpawn(state);
  9151. if (player && player->IsPlayer()) {
  9152. Client* client = 0;
  9153. if (player && player->IsPlayer())
  9154. client = player->GetZone()->GetClientBySpawn(player);
  9155. if (!client)
  9156. return 0;
  9157. lua_interface->SetInt32Value(state, client->GetTemporaryTransportID());
  9158. return 1;
  9159. }
  9160. return 0;
  9161. }
  9162. int EQ2Emu_lua_SetAlignment(lua_State* state) {
  9163. if (!lua_interface)
  9164. return 0;
  9165. Spawn* spawn = lua_interface->GetSpawn(state);
  9166. sint32 alignment = lua_interface->GetSInt32Value(state, 2);
  9167. LuaSpell* spell = lua_interface->GetCurrentSpell(state);
  9168. if (!spawn) {
  9169. lua_interface->LogError("%s: LUA SetAlignment command error: spawn is not valid", lua_interface->GetScriptName(state));
  9170. return 0;
  9171. }
  9172. if (!spawn->IsEntity()) {
  9173. lua_interface->LogError("%s: LUA SetAlignment command error: spawn is not an entity", lua_interface->GetScriptName(state));
  9174. return 0;
  9175. }
  9176. if (alignment < SCHAR_MIN || alignment > SCHAR_MAX)
  9177. {
  9178. lua_interface->LogError("%s: LUA SetAlignment command error: alignment value beyond supported min: %i and max: %i", lua_interface->GetScriptName(state), SCHAR_MIN, SCHAR_MAX);
  9179. return 0;
  9180. }
  9181. if (spell && spell->targets.size() > 0) {
  9182. ZoneServer* zone = spell->caster->GetZone();
  9183. for (int8 i = 0; i < spell->targets.size(); i++) {
  9184. Spawn* target = zone->GetSpawnByID(spell->targets.at(i));
  9185. if (target->IsEntity()) {
  9186. ((Entity*)target)->GetInfoStruct()->alignment = (sint8)alignment;
  9187. if (target->IsPlayer())
  9188. ((Player*)target)->SetCharSheetChanged(true);
  9189. }
  9190. }
  9191. }
  9192. else {
  9193. ((Entity*)spawn)->GetInfoStruct()->alignment = (sint8)alignment;
  9194. if (spawn->IsPlayer())
  9195. ((Player*)spawn)->SetCharSheetChanged(true);
  9196. }
  9197. return 0;
  9198. }
  9199. int EQ2Emu_lua_GetAlignment(lua_State* state) {
  9200. if (!lua_interface)
  9201. return 0;
  9202. Spawn* spawn = lua_interface->GetSpawn(state);
  9203. if (!spawn) {
  9204. lua_interface->LogError("%s: LUA SetAlignment command error: spawn is not valid", lua_interface->GetScriptName(state));
  9205. return 0;
  9206. }
  9207. if (!spawn->IsEntity()) {
  9208. lua_interface->LogError("%s: LUA SetAlignment command error: spawn is not an entity", lua_interface->GetScriptName(state));
  9209. return 0;
  9210. }
  9211. lua_interface->SetInt32Value(state, ((Entity*)spawn)->GetAlignment());
  9212. return 1;
  9213. }
  9214. int EQ2Emu_lua_GetSpell(lua_State* state) {
  9215. if (!lua_interface)
  9216. return 0;
  9217. int32 spell_id = lua_interface->GetInt32Value(state);
  9218. int8 spell_tier = lua_interface->GetInt8Value(state, 2);
  9219. if (spell_id > 0) {
  9220. if (spell_tier == 0)
  9221. spell_tier = 1;
  9222. Spell* spell = master_spell_list.GetSpell(spell_id, spell_tier);
  9223. LuaSpell* lua_spell = 0;
  9224. if (lua_interface)
  9225. lua_spell = lua_interface->GetSpell(spell->GetSpellData()->lua_script.c_str());
  9226. if (!lua_spell)
  9227. return 0;
  9228. lua_spell->spell = new Spell(spell);
  9229. lua_interface->SetSpellValue(state, lua_spell);
  9230. return 1;
  9231. }
  9232. return 0;
  9233. }
  9234. int EQ2Emu_lua_GetSpellData(lua_State* state) {
  9235. if (!lua_interface)
  9236. return 0;
  9237. LuaSpell* spell = lua_interface->GetSpell(state);
  9238. string field = lua_interface->GetStringValue(state, 2);
  9239. if (!spell) {
  9240. lua_interface->LogError("%s: Spell not given in GetSpellData!", lua_interface->GetScriptName(state));
  9241. return 0;
  9242. }
  9243. if (!spell->spell || !spell->spell->GetSpellData()) {
  9244. lua_interface->LogError("%s: Inner Spell or SpellData not given in GetSpellData!", lua_interface->GetScriptName(state));
  9245. return 0;
  9246. }
  9247. boost::to_lower(field);
  9248. return spell->spell->GetSpellData(state, field);
  9249. }
  9250. int EQ2Emu_lua_SetSpellData(lua_State* state) {
  9251. if (!lua_interface)
  9252. return 0;
  9253. LuaSpell* spell = lua_interface->GetSpell(state);
  9254. string field = lua_interface->GetStringValue(state, 2);
  9255. int8 fieldArg = 3; // field value after the initial set
  9256. if (!spell) {
  9257. lua_interface->LogError("%s: Spell not given in SetSpellData!", lua_interface->GetScriptName(state));
  9258. return 0;
  9259. }
  9260. if (!spell->spell || !spell->spell->GetSpellData()) {
  9261. lua_interface->LogError("%s: Inner Spell or SpellData not given in SetSpellData!", lua_interface->GetScriptName(state));
  9262. return 0;
  9263. }
  9264. boost::to_lower(field);
  9265. bool valSet = false;
  9266. spell->spell->SetSpellData(state, field, fieldArg);
  9267. return valSet;
  9268. }
  9269. int EQ2Emu_lua_SetSpellDataIndex(lua_State* state) {
  9270. if (!lua_interface)
  9271. return 0;
  9272. LuaSpell* spell = lua_interface->GetSpell(state);
  9273. int8 idx = lua_interface->GetInt32Value(state, 2);
  9274. if (!spell) {
  9275. lua_interface->LogError("%s: Spell not given in SetSpellDataIndex!", lua_interface->GetScriptName(state));
  9276. return 0;
  9277. }
  9278. if (!spell->spell || !spell->spell->GetSpellData()) {
  9279. lua_interface->LogError("%s: Inner Spell or SpellData not given in SetSpellDataIndex!", lua_interface->GetScriptName(state));
  9280. return 0;
  9281. }
  9282. if (spell->spell->lua_data.size() <= idx)
  9283. {
  9284. lua_interface->LogError("%s: lua_data size %i <= %i (idx passed) SetSpellDataIndex!", lua_interface->GetScriptName(state), spell->spell->lua_data.size(), idx);
  9285. return 0;
  9286. }
  9287. bool setVal = true;
  9288. LUAData* data = spell->spell->lua_data[idx];
  9289. switch (data->type)
  9290. {
  9291. case 0:
  9292. {
  9293. sint32 value = lua_interface->GetSInt32Value(state, 3);
  9294. sint32 value2 = lua_interface->GetSInt32Value(state, 4);
  9295. data->int_value = value;
  9296. data->int_value2 = value2;
  9297. break;
  9298. }
  9299. case 1:
  9300. {
  9301. float value = lua_interface->GetFloatValue(state, 3);
  9302. float value2 = lua_interface->GetFloatValue(state, 4);
  9303. data->float_value = value;
  9304. data->float_value2 = value2;
  9305. break;
  9306. }
  9307. case 2:
  9308. {
  9309. bool value = lua_interface->GetBooleanValue(state, 3);
  9310. data->bool_value = value;
  9311. break;
  9312. }
  9313. case 3:
  9314. {
  9315. string value = lua_interface->GetStringValue(state, 3);
  9316. string value2 = lua_interface->GetStringValue(state, 4);
  9317. data->string_value = value;
  9318. data->string_value2 = value2;
  9319. break;
  9320. }
  9321. default:
  9322. setVal = false;
  9323. }
  9324. return setVal;
  9325. }
  9326. int EQ2Emu_lua_GetSpellDataIndex(lua_State* state) {
  9327. if (!lua_interface)
  9328. return 0;
  9329. LuaSpell* spell = lua_interface->GetSpell(state);
  9330. int8 idx = lua_interface->GetInt32Value(state, 2);
  9331. bool secondfield = lua_interface->GetBooleanValue(state, 3);
  9332. if (!spell) {
  9333. lua_interface->LogError("%s: Spell not given in GetSpellDataIndex!", lua_interface->GetScriptName(state));
  9334. return 0;
  9335. }
  9336. if (!spell->spell || !spell->spell->GetSpellData()) {
  9337. lua_interface->LogError("%s: Inner Spell or SpellData not given in GetSpellDataIndex!", lua_interface->GetScriptName(state));
  9338. return 0;
  9339. }
  9340. if (spell->spell->lua_data.size() <= idx)
  9341. {
  9342. lua_interface->LogError("%s: lua_data size %i <= %i (idx passed) GetSpellDataIndex!", lua_interface->GetScriptName(state), spell->spell->lua_data.size(), idx);
  9343. return 0;
  9344. }
  9345. bool setVal = true;
  9346. LUAData* data = spell->spell->lua_data[idx];
  9347. switch (data->type)
  9348. {
  9349. case 0:
  9350. {
  9351. if(!secondfield)
  9352. lua_interface->SetSInt32Value(state, data->int_value);
  9353. else
  9354. lua_interface->SetSInt32Value(state, data->int_value2);
  9355. break;
  9356. }
  9357. case 1:
  9358. {
  9359. if (!secondfield)
  9360. lua_interface->SetFloatValue(state, data->float_value);
  9361. else
  9362. lua_interface->SetFloatValue(state, data->float_value2);
  9363. break;
  9364. }
  9365. case 2:
  9366. {
  9367. lua_interface->SetBooleanValue(state, data->bool_value);
  9368. break;
  9369. }
  9370. case 3:
  9371. {
  9372. if (!secondfield)
  9373. lua_interface->SetStringValue(state, data->string_value.c_str());
  9374. else
  9375. lua_interface->SetStringValue(state, data->string_value2.c_str());
  9376. break;
  9377. }
  9378. default:
  9379. setVal = false;
  9380. }
  9381. return setVal;
  9382. }
  9383. int EQ2Emu_lua_CastCustomSpell(lua_State* state) {
  9384. if (!lua_interface)
  9385. return 0;
  9386. LuaSpell* spell = lua_interface->GetSpell(state);
  9387. Spawn* caster = lua_interface->GetSpawn(state, 2);
  9388. Spawn* target = lua_interface->GetSpawn(state, 3);
  9389. if (!target) {
  9390. lua_interface->LogError("%s: LUA CastCustomSpell command error: target is not a valid spawn", lua_interface->GetScriptName(state));
  9391. return 0;
  9392. }
  9393. if (!target->IsEntity()) {
  9394. lua_interface->LogError("%s: LUA CastCustomSpell command error: target (%s) is not an entity", lua_interface->GetScriptName(state), target->GetName());
  9395. return 0;
  9396. }
  9397. if (!spell) {
  9398. lua_interface->LogError("%s: LUA CastCustomSpell command error: spell is not valid", lua_interface->GetScriptName(state));
  9399. return 0;
  9400. }
  9401. if (caster && !caster->IsEntity()) {
  9402. lua_interface->LogError("%s: LUA CastSpell command error: caster (%s) is not an entity", lua_interface->GetScriptName(state), caster->GetName());
  9403. return 0;
  9404. }
  9405. target->GetZone()->ProcessSpell(NULL, (Entity*)caster, (Entity*)target, true, false, spell, 0);
  9406. return 0;
  9407. }