LuaFunctions.cpp 306 KB

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