LuaFunctions.cpp 305 KB

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