LuaFunctions.cpp 335 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921
  1. /*
  2. EQ2Emulator: Everquest II Server Emulator
  3. Copyright (C) 2007 EQ2EMulator Development Team (http://www.eq2emulator.net)
  4. This file is part of EQ2Emulator.
  5. EQ2Emulator is free software: you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation, either version 3 of the License, or
  8. (at your option) any later version.
  9. EQ2Emulator is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with EQ2Emulator. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #include "LuaFunctions.h"
  17. #include "Spawn.h"
  18. #include "WorldDatabase.h"
  19. #include "LuaInterface.h"
  20. #include "../common/ConfigReader.h"
  21. #include "client.h"
  22. #include "World.h"
  23. #include "Commands/Commands.h"
  24. #include "races.h"
  25. #include "classes.h"
  26. #include "Variables.h"
  27. #include "SpellProcess.h"
  28. #include "../common/Log.h"
  29. #include <math.h>
  30. #include "HeroicOp/HeroicOp.h"
  31. #include "RaceTypes/RaceTypes.h"
  32. #include "ClientPacketFunctions.h"
  33. #include "Transmute.h"
  34. #include <boost/algorithm/string/predicate.hpp>
  35. #include <sstream>
  36. #include <boost/algorithm/string.hpp>
  37. extern MasterFactionList master_faction_list;
  38. extern WorldDatabase database;
  39. extern LuaInterface* lua_interface;
  40. extern ConfigReader configReader;
  41. extern MasterQuestList master_quest_list;
  42. extern MasterItemList master_item_list;
  43. extern MasterSpellList master_spell_list;
  44. extern World world;
  45. extern Commands commands;
  46. extern ZoneList zone_list;
  47. extern Races races;
  48. extern Classes classes;
  49. extern Variables variables;
  50. extern MasterSkillList master_skill_list;
  51. extern MasterHeroicOPList master_ho_list;
  52. extern MasterRaceTypeList race_types_list;
  53. extern MasterLanguagesList master_languages_list;
  54. vector<string> ParseString(string strVal, char delim) {
  55. stringstream ss(strVal);
  56. vector<string> ret;
  57. while (ss.good())
  58. {
  59. string substr;
  60. getline(ss, substr, delim);
  61. ret.push_back(substr);
  62. }
  63. return ret;
  64. }
  65. vector<unsigned int> ParseStringToInt32(string strVal, char delim) {
  66. stringstream ss(strVal);
  67. vector<unsigned int> ret;
  68. while (ss.good())
  69. {
  70. string substr;
  71. getline(ss, substr, delim);
  72. stringstream valss(substr);
  73. unsigned int val = 0;
  74. valss >> val;
  75. ret.push_back(val);
  76. }
  77. return ret;
  78. }
  79. map<string, signed int> ParseStringMap(string strVal, char delim) {
  80. vector<string> pairs = ParseString(strVal, delim);
  81. vector<string>::iterator itr;
  82. map<string, signed int> ret;
  83. for (itr = pairs.begin(); itr != pairs.end(); itr++) {
  84. vector<string> keyvaluepair = ParseString(*itr, ':');
  85. if (keyvaluepair.size() == 2) {
  86. stringstream valss(keyvaluepair[1]);
  87. int32 val = 0;
  88. valss >> val;
  89. ret[keyvaluepair[0]] = val;
  90. }
  91. }
  92. return ret;
  93. }
  94. map<unsigned int, unsigned short> ParseIntMap(string strVal, char delim) {
  95. vector<string> pairs = ParseString(strVal, delim);
  96. vector<string>::iterator itr;
  97. map<unsigned int, unsigned short> ret;
  98. for (itr = pairs.begin(); itr != pairs.end(); itr++) {
  99. vector<string> keyvaluepair = ParseString(*itr, ':');
  100. int32 key = 0;
  101. if (keyvaluepair.size() > 0) {
  102. stringstream keyss(keyvaluepair[0]);
  103. keyss >> key;
  104. }
  105. if (keyvaluepair.size() == 1) {
  106. ret[key] = 1;
  107. }
  108. else if (keyvaluepair.size() == 2) {
  109. stringstream valss(keyvaluepair[1]);
  110. unsigned short val = 0;
  111. valss >> val;
  112. ret[key] = val;
  113. }
  114. }
  115. return ret;
  116. }
  117. map<unsigned int, signed int> ParseSInt32Map(string strVal, char delim) {
  118. vector<string> pairs = ParseString(strVal, delim);
  119. vector<string>::iterator itr;
  120. map<unsigned int, signed int> ret;
  121. for (itr = pairs.begin(); itr != pairs.end(); itr++) {
  122. vector<string> keyvaluepair = ParseString(*itr, ':');
  123. int32 key = 0;
  124. if (keyvaluepair.size() > 0) {
  125. stringstream keyss(keyvaluepair[0]);
  126. keyss >> key;
  127. }
  128. if (keyvaluepair.size() == 1) {
  129. ret[key] = 1;
  130. }
  131. else if (keyvaluepair.size() == 2) {
  132. stringstream valss(keyvaluepair[1]);
  133. signed int val = 0;
  134. valss >> val;
  135. ret[key] = val;
  136. }
  137. }
  138. return ret;
  139. }
  140. int EQ2Emu_lua_PlayFlavor(lua_State* state) {
  141. if (!lua_interface)
  142. return 0;
  143. Spawn* spawn = lua_interface->GetSpawn(state);
  144. string mp3_string = lua_interface->GetStringValue(state, 2);
  145. string text_string = lua_interface->GetStringValue(state, 3);
  146. string emote_string = lua_interface->GetStringValue(state, 4);
  147. int32 key1 = lua_interface->GetInt32Value(state, 5);
  148. int32 key2 = lua_interface->GetInt32Value(state, 6);
  149. Spawn* player = lua_interface->GetSpawn(state, 7);
  150. int8 language = lua_interface->GetInt8Value(state, 8);
  151. if (spawn) {
  152. const char* mp3 = 0;
  153. const char* text = 0;
  154. const char* emote = 0;
  155. if (mp3_string.length() > 0)
  156. mp3 = mp3_string.c_str();
  157. if (text_string.length() > 0)
  158. text = text_string.c_str();
  159. if (emote_string.length() > 0)
  160. emote = emote_string.c_str();
  161. Client* client = 0;
  162. if (player && player->IsPlayer())
  163. client = spawn->GetZone()->GetClientBySpawn(player);
  164. if (client) {
  165. if (((Player*)player)->WasSentSpawn(spawn->GetID()) && !((Player*)player)->WasSpawnRemoved(spawn))
  166. spawn->GetZone()->PlayFlavor(client, spawn, mp3, text, emote, key1, key2, language);
  167. }
  168. else
  169. spawn->GetZone()->PlayFlavor(spawn, mp3, text, emote, key1, key2, language);
  170. }
  171. return 0;
  172. }
  173. int EQ2Emu_lua_PlaySound(lua_State* state) {
  174. if (!lua_interface)
  175. return 0;
  176. Spawn* spawn = lua_interface->GetSpawn(state);
  177. string sound_string = lua_interface->GetStringValue(state, 2);
  178. float x = lua_interface->GetFloatValue(state, 3);
  179. float y = lua_interface->GetFloatValue(state, 4);
  180. float z = lua_interface->GetFloatValue(state, 5);
  181. Spawn* player = lua_interface->GetSpawn(state, 6);
  182. if (spawn && sound_string.length() > 0) {
  183. Client* client = 0;
  184. if (player && player->IsPlayer())
  185. client = spawn->GetZone()->GetClientBySpawn(player);
  186. if (client)
  187. spawn->GetZone()->PlaySoundFile(client, sound_string.c_str(), x, y, z);
  188. else
  189. spawn->GetZone()->PlaySoundFile(0, sound_string.c_str(), x, y, z);
  190. }
  191. return 0;
  192. }
  193. int EQ2Emu_lua_SetRequiredQuest(lua_State* state) {
  194. if (!lua_interface)
  195. return 0;
  196. Spawn* spawn = lua_interface->GetSpawn(state);
  197. int32 quest_id = lua_interface->GetInt32Value(state, 2);
  198. int16 quest_step = lua_interface->GetInt16Value(state, 3);
  199. bool private_spawn = (lua_interface->GetInt8Value(state, 4) == 1);
  200. bool continued_access = (lua_interface->GetInt8Value(state, 5) == 1);
  201. int16 flag_override = lua_interface->GetInt16Value(state, 6);
  202. if (!spawn) {
  203. lua_interface->LogError("%s: LUA SetRequiredQuest command error: spawn is not valid", lua_interface->GetScriptName(state));
  204. return 0;
  205. }
  206. if (quest_id > 0) {
  207. //Add this quest to the list of required quests for this spawn
  208. spawn->SetQuestsRequired(quest_id, quest_step);
  209. //If private spawn value set
  210. if (private_spawn) {
  211. //Set the spawn to be private when not granted access through this quest
  212. spawn->AddAllowAccessSpawn(spawn);
  213. spawn->SetPrivateQuestSpawn(true);
  214. }
  215. //This value allows access after a quest step, or the whole quest has been completed
  216. if (continued_access)
  217. spawn->SetQuestsRequiredContinuedAccess(true);
  218. //This value will override vis_flags in the vis packet
  219. if (flag_override > 0)
  220. spawn->SetQuestsRequiredOverride(flag_override);
  221. }
  222. return 0;
  223. }
  224. int EQ2Emu_lua_SpawnSetByDistance(lua_State* state) {
  225. if (!lua_interface)
  226. return 0;
  227. Spawn* spawn = lua_interface->GetSpawn(state);
  228. float max_distance = lua_interface->GetFloatValue(state, 2);
  229. string variable = lua_interface->GetStringValue(state, 3);
  230. string value = lua_interface->GetStringValue(state, 4);
  231. if (max_distance > 0 && spawn && value.length() > 0 && variable.length() > 0 && spawn->GetZone())
  232. spawn->GetZone()->SpawnSetByDistance(spawn, max_distance, variable, value);
  233. return 0;
  234. }
  235. int EQ2Emu_lua_PerformCameraShake(lua_State* state) {
  236. if (!lua_interface)
  237. return 0;
  238. Client* client = 0;
  239. Spawn* player = lua_interface->GetSpawn(state);
  240. if (!player) {
  241. lua_interface->LogError("LUA PerformCameraShake command error: spawn is not valid");
  242. return 0;
  243. }
  244. if (!player->IsPlayer()) {
  245. lua_interface->LogError("LUA PerformCameraShake command error: spawn is not a player");
  246. return 0;
  247. }
  248. if (player->GetZone())
  249. client = player->GetZone()->GetClientBySpawn(player);
  250. if (!client) {
  251. lua_interface->LogError("LUA PerformCameraShake command error: could not find client");
  252. return 0;
  253. }
  254. float intensity = lua_interface->GetFloatValue(state, 2);
  255. int8 direction = lua_interface->GetInt8Value(state, 3);
  256. PacketStruct* packet = configReader.getStruct("WS_PerformCameraShakeMsg", client->GetVersion());
  257. if (packet) {
  258. /* Client Intensity Logic (does not restrict service side, but expect .01 - 1.0 range)
  259. v1 = *(float *)(a1 + 4);
  260. if ( v1 > 0.0 )
  261. v2 = fminf(v1, 1.0);
  262. else
  263. v2 = 0.1;
  264. */
  265. packet->setDataByName("intensity", intensity);
  266. if ( client->GetVersion() > 546 )
  267. packet->setDataByName("direction", direction);
  268. client->QueuePacket(packet->serialize());
  269. safe_delete(packet);
  270. }
  271. return 0;
  272. }
  273. int EQ2Emu_lua_KillSpawn(lua_State* state) {
  274. if (!lua_interface)
  275. return 0;
  276. Spawn* dead = lua_interface->GetSpawn(state);
  277. Spawn* killer = lua_interface->GetSpawn(state, 2);
  278. bool send_packet = (lua_interface->GetInt8Value(state, 3) == 1);
  279. if (dead && dead->Alive() && dead->GetZone())
  280. dead->GetZone()->KillSpawn(false, dead, killer, send_packet);
  281. return 0;
  282. }
  283. int EQ2Emu_lua_KillSpawnByDistance(lua_State* state) {
  284. if (!lua_interface)
  285. return 0;
  286. Spawn* spawn = lua_interface->GetSpawn(state);
  287. float max_distance = lua_interface->GetFloatValue(state, 2);
  288. bool include_players = lua_interface->GetInt8Value(state, 3);
  289. bool send_packet = (lua_interface->GetInt8Value(state, 4) == 1);
  290. if (max_distance > 0 && spawn && spawn->GetZone())
  291. spawn->GetZone()->KillSpawnByDistance(spawn, max_distance, include_players, send_packet);
  292. return 0;
  293. }
  294. int EQ2Emu_lua_Despawn(lua_State* state) {
  295. if (!lua_interface)
  296. return 0;
  297. Spawn* spawn = lua_interface->GetSpawn(state);
  298. int32 delay = lua_interface->GetInt32Value(state, 2);
  299. if (spawn && spawn->GetZone())
  300. spawn->GetZone()->Despawn(spawn, delay);
  301. return 0;
  302. }
  303. int EQ2Emu_lua_ChangeHandIcon(lua_State* state) {
  304. if (!lua_interface)
  305. return 0;
  306. Spawn* spawn = lua_interface->GetSpawn(state);
  307. int8 displayHandIcon = lua_interface->GetInt8Value(state, 2);
  308. if (spawn) {
  309. spawn->info_changed = true;
  310. spawn->SetShowHandIcon(displayHandIcon);
  311. }
  312. return 0;
  313. }
  314. //this function is used to force an update packet to be sent.
  315. //Useful if certain calculated things change after the player is sent the spawn packet, like quest flags or player has access to an object now
  316. int EQ2Emu_lua_SetVisualFlag(lua_State* state) {
  317. if (!lua_interface)
  318. return 0;
  319. Spawn* spawn = lua_interface->GetSpawn(state);
  320. if (spawn) {
  321. spawn->vis_changed = true;
  322. }
  323. return 0;
  324. }
  325. //this function is used to force an update packet to be sent.
  326. //Useful if certain calculated things change after the player is sent the spawn packet, like quest flags or player has access to an object now
  327. int EQ2Emu_lua_SetInfoFlag(lua_State* state) {
  328. if (!lua_interface)
  329. return 0;
  330. Spawn* spawn = lua_interface->GetSpawn(state);
  331. if (spawn) {
  332. spawn->info_changed = true;
  333. }
  334. return 0;
  335. }
  336. int EQ2Emu_lua_SendStateCommand(lua_State* state) {
  337. if (!lua_interface)
  338. return 0;
  339. Spawn* spawn = lua_interface->GetSpawn(state);
  340. int32 new_state = lua_interface->GetInt32Value(state, 2);
  341. if (spawn) {
  342. spawn->GetZone()->SendStateCommand(spawn, new_state);
  343. }
  344. return 0;
  345. }
  346. int EQ2Emu_lua_SpawnSet(lua_State* state) {
  347. if (!lua_interface)
  348. return 0;
  349. Spawn* spawn = lua_interface->GetSpawn(state);
  350. string variable = lua_interface->GetStringValue(state, 2);
  351. string value = lua_interface->GetStringValue(state, 3);
  352. bool no_update = lua_interface->GetBooleanValue(state, 4); // send update is true by default in SetSpawnCommand, so allow user to specify 'true' to disable send update.
  353. bool temporary_flag = lua_interface->GetBooleanValue(state, 5); // default false as originally designed, allow user to set temporary_flag true to not update DB
  354. int32 type = commands.GetSpawnSetType(variable);
  355. if (type != 0xFFFFFFFF && value.length() > 0 && spawn)
  356. commands.SetSpawnCommand(0, spawn, type, value.c_str(), !no_update, temporary_flag);
  357. return 0;
  358. }
  359. int EQ2Emu_lua_GetSpawn(lua_State* state) {
  360. if (!lua_interface)
  361. return 0;
  362. Spawn* spawn = lua_interface->GetSpawn(state);
  363. int32 spawn_id = lua_interface->GetInt32Value(state, 2);
  364. if (spawn && spawn_id > 0) {
  365. Spawn* closest_spawn = spawn->GetZone()->GetClosestSpawn(spawn, spawn_id);
  366. if (closest_spawn) {
  367. lua_interface->SetSpawnValue(state, closest_spawn);
  368. return 1;
  369. }
  370. }
  371. return 0;
  372. }
  373. int EQ2Emu_lua_GetSpawnFromList(lua_State* state) {
  374. if (!lua_interface)
  375. return 0;
  376. vector<Spawn*>* spawnList = lua_interface->GetSpawnList(state);
  377. int32 position = lua_interface->GetInt32Value(state, 2);
  378. if (spawnList) {
  379. if (spawnList->size() > position) {
  380. lua_interface->SetSpawnValue(state, spawnList->at(position));
  381. return 1;
  382. }
  383. else {
  384. return 0;
  385. }
  386. return spawnList->size();
  387. }
  388. return 0;
  389. }
  390. int EQ2Emu_lua_GetSpawnListSize(lua_State* state) {
  391. if (!lua_interface)
  392. return 0;
  393. vector<Spawn*>* spawnList = lua_interface->GetSpawnList(state);
  394. if (spawnList) {
  395. return spawnList->size();
  396. }
  397. return 0;
  398. }
  399. int EQ2Emu_lua_CreateSpawnList(lua_State* state) {
  400. if (!lua_interface)
  401. return 0;
  402. vector<Spawn*>* spawnList = new vector<Spawn*>();
  403. lua_interface->SetSpawnListValue(state, spawnList);
  404. return 1;
  405. }
  406. int EQ2Emu_lua_AddSpawnToSpawnList(lua_State* state) {
  407. if (!lua_interface)
  408. return 0;
  409. vector<Spawn*>* spawnList = lua_interface->GetSpawnList(state);
  410. Spawn* spawn = lua_interface->GetSpawn(state, 2);
  411. if (spawnList) {
  412. auto it = std::find(spawnList->begin(), spawnList->end(), spawn);
  413. if (it == spawnList->end())
  414. spawnList->push_back(spawn);
  415. }
  416. return 0;
  417. }
  418. int EQ2Emu_lua_RemoveSpawnFromSpawnList(lua_State* state) {
  419. if (!lua_interface)
  420. return 0;
  421. vector<Spawn*>* spawnList = lua_interface->GetSpawnList(state);
  422. Spawn* spawn = lua_interface->GetSpawn(state, 2);
  423. if (spawnList) {
  424. auto it = std::find(spawnList->begin(), spawnList->end(), spawn);
  425. if(it != spawnList->end())
  426. spawnList->erase(it);
  427. }
  428. return 0;
  429. }
  430. int EQ2Emu_lua_GetSpawnListBySpawnID(lua_State* state) {
  431. if (!lua_interface)
  432. return 0;
  433. Spawn* spawn = lua_interface->GetSpawn(state);
  434. int32 spawn_id = lua_interface->GetInt32Value(state, 2);
  435. if (spawn) {
  436. vector<Spawn*> spawns = spawn->GetZone()->GetSpawnsByID(spawn_id);
  437. if (spawns.size() > 0) {
  438. vector<Spawn*>* spawnList = new vector<Spawn*>();
  439. vector<Spawn*>::iterator itr;
  440. for (itr = spawns.begin(); itr != spawns.end(); itr++) {
  441. spawnList->push_back(*itr);
  442. }
  443. lua_interface->SetSpawnListValue(state, spawnList);
  444. return 1;
  445. }
  446. }
  447. return 0;
  448. }
  449. int EQ2Emu_lua_GetVariableValue(lua_State* state) {
  450. if (!lua_interface)
  451. return 0;
  452. string variable_name = lua_interface->GetStringValue(state);
  453. Variable* var = variables.FindVariable(variable_name);
  454. if (var) {
  455. lua_interface->SetStringValue(state, var->GetValue());
  456. return 1;
  457. }
  458. return 0;
  459. }
  460. int EQ2Emu_lua_GetCoinMessage(lua_State* state) {
  461. if (!lua_interface)
  462. return 0;
  463. int32 total_coins = lua_interface->GetInt32Value(state);
  464. if (total_coins == 0) {
  465. lua_interface->SetStringValue(state, "0 copper");
  466. return 1;
  467. }
  468. char tmp[64] = { 0 };
  469. string message = "";
  470. int32 val = 0;
  471. if (total_coins >= 1000000) {
  472. val = total_coins / 1000000;
  473. total_coins -= 1000000 * val;
  474. sprintf(tmp, " %u Platinum", val);
  475. message.append(tmp);
  476. memset(tmp, 0, 64);
  477. }
  478. if (total_coins >= 10000) {
  479. val = total_coins / 10000;
  480. total_coins -= 10000 * val;
  481. sprintf(tmp, " %u Gold", val);
  482. message.append(tmp);
  483. memset(tmp, 0, 64);
  484. }
  485. if (total_coins >= 100) {
  486. val = total_coins / 100;
  487. total_coins -= 100 * val;
  488. sprintf(tmp, " %u Silver", val);
  489. message.append(tmp);
  490. memset(tmp, 0, 64);
  491. }
  492. if (total_coins > 0) {
  493. sprintf(tmp, " %u Copper", (int32)total_coins);
  494. message.append(tmp);
  495. }
  496. lua_interface->SetStringValue(state, message.c_str());
  497. return 1;
  498. }
  499. int EQ2Emu_lua_GetSpawnByGroupID(lua_State* state) {
  500. ZoneServer* zone = lua_interface->GetZone(state);
  501. int32 group_id = lua_interface->GetInt32Value(state, 2);
  502. if (zone) {
  503. Spawn* spawn = zone->GetSpawnGroup(group_id);
  504. if (spawn) {
  505. lua_interface->SetSpawnValue(state, spawn);
  506. return 1;
  507. }
  508. }
  509. return 0;
  510. }
  511. int EQ2Emu_lua_GetSpawnByLocationID(lua_State* state) {
  512. ZoneServer* zone = lua_interface->GetZone(state);
  513. int32 location_id = lua_interface->GetInt32Value(state, 2);
  514. if (zone) {
  515. Spawn* spawn = zone->GetSpawnByLocationID(location_id);
  516. if (spawn) {
  517. lua_interface->SetSpawnValue(state, spawn);
  518. return 1;
  519. }
  520. }
  521. return 0;
  522. }
  523. int EQ2Emu_lua_GetSpawnID(lua_State* state) {
  524. Spawn* spawn = lua_interface->GetSpawn(state);
  525. if (spawn) {
  526. lua_interface->SetInt32Value(state, spawn->GetDatabaseID());
  527. return 1;
  528. }
  529. return 0;
  530. }
  531. int EQ2Emu_lua_GetSpawnGroupID(lua_State* state) {
  532. Spawn* spawn = lua_interface->GetSpawn(state);
  533. if (spawn) {
  534. lua_interface->SetInt32Value(state, spawn->GetSpawnGroupID());
  535. return 1;
  536. }
  537. return 0;
  538. }
  539. int EQ2Emu_lua_GetSpawnLocationID(lua_State* state) {
  540. Spawn* spawn = lua_interface->GetSpawn(state);
  541. if (spawn) {
  542. lua_interface->SetInt32Value(state, spawn->GetSpawnLocationID());
  543. return 1;
  544. }
  545. return 0;
  546. }
  547. int EQ2Emu_lua_GetSpawnLocationPlacementID(lua_State* state) {
  548. Spawn* spawn = lua_interface->GetSpawn(state);
  549. if (spawn) {
  550. lua_interface->SetInt32Value(state, spawn->GetSpawnLocationPlacementID());
  551. return 1;
  552. }
  553. return 0;
  554. }
  555. int EQ2Emu_lua_GetFactionAmount(lua_State* state) {
  556. Player* player = (Player*)lua_interface->GetSpawn(state);
  557. int32 faction_id = lua_interface->GetInt32Value(state, 2);
  558. if (player && player->IsPlayer() && faction_id > 0) {
  559. lua_interface->SetInt32Value(state, player->GetFactions()->GetFactionValue(faction_id));
  560. return 1;
  561. }
  562. return 0;
  563. }
  564. int EQ2Emu_lua_SetFactionID(lua_State* state) {
  565. if (!lua_interface)
  566. return 0;
  567. Spawn* spawn = lua_interface->GetSpawn(state);
  568. int32 value = lua_interface->GetInt32Value(state, 2);
  569. if (spawn) {
  570. spawn->SetFactionID(value);
  571. }
  572. return 0;
  573. }
  574. int EQ2Emu_lua_GetFactionID(lua_State* state) {
  575. Spawn* spawn = lua_interface->GetSpawn(state);
  576. if (spawn) {
  577. lua_interface->SetInt32Value(state, spawn->GetFactionID());
  578. return 1;
  579. }
  580. return 0;
  581. }
  582. int EQ2Emu_lua_GetGender(lua_State* state) {
  583. Spawn* spawn = lua_interface->GetSpawn(state);
  584. if (spawn) {
  585. lua_interface->SetInt32Value(state, spawn->GetGender());
  586. return 1;
  587. }
  588. return 0;
  589. }
  590. int EQ2Emu_lua_GetTarget(lua_State* state) {
  591. Spawn* spawn = lua_interface->GetSpawn(state);
  592. if (spawn && spawn->IsEntity() && ((Entity*)spawn)->GetTarget()) {
  593. lua_interface->SetSpawnValue(state, ((Entity*)spawn)->GetTarget());
  594. return 1;
  595. }
  596. return 0;
  597. }
  598. int EQ2Emu_lua_PlayVoice(lua_State* state) {
  599. if (!lua_interface)
  600. return 0;
  601. Spawn* spawn = lua_interface->GetSpawn(state);
  602. string mp3_string = lua_interface->GetStringValue(state, 2);
  603. int32 key1 = lua_interface->GetInt32Value(state, 3);
  604. int32 key2 = lua_interface->GetInt32Value(state, 4);
  605. Spawn* player = lua_interface->GetSpawn(state, 5);
  606. if (spawn && mp3_string.length() > 0) {
  607. Client* client = 0;
  608. if (player && player->IsPlayer())
  609. client = spawn->GetZone()->GetClientBySpawn(player);
  610. if (client) {
  611. if (((Player*)player)->WasSentSpawn(spawn->GetID()) && !((Player*)player)->WasSpawnRemoved(spawn))
  612. spawn->GetZone()->PlayVoice(client, spawn, mp3_string.c_str(), key1, key2);
  613. }
  614. else
  615. spawn->GetZone()->PlayVoice(spawn, mp3_string.c_str(), key1, key2);
  616. }
  617. return 0;
  618. }
  619. int EQ2Emu_lua_GetCurrentZoneSafeLocation(lua_State* state) {
  620. if (!lua_interface)
  621. return 0;
  622. Spawn* spawn = lua_interface->GetSpawn(state);
  623. if (spawn) {
  624. lua_interface->SetFloatValue(state, spawn->GetZone()->GetSafeX());
  625. lua_interface->SetFloatValue(state, spawn->GetZone()->GetSafeY());
  626. lua_interface->SetFloatValue(state, spawn->GetZone()->GetSafeZ());
  627. return 3;
  628. }
  629. return 0;
  630. }
  631. int EQ2Emu_lua_HasLootItem(lua_State* state) {
  632. if (!lua_interface)
  633. return 0;
  634. Spawn* spawn = lua_interface->GetSpawn(state);
  635. if (spawn) {
  636. int32 item_id = lua_interface->GetInt32Value(state, 2);
  637. lua_interface->SetBooleanValue(state, spawn->HasLootItemID(item_id));
  638. return 1;
  639. }
  640. return 0;
  641. }
  642. int EQ2Emu_lua_AddLootItem(lua_State* state) {
  643. if (!lua_interface)
  644. return 0;
  645. Spawn* spawn = lua_interface->GetSpawn(state);
  646. if (spawn && spawn->IsEntity()) {
  647. int32 item_id = lua_interface->GetInt32Value(state, 2);
  648. int16 charges = lua_interface->GetInt16Value(state, 3);
  649. if (charges == 0)
  650. charges = 1;
  651. ((Entity*)spawn)->AddLootItem(item_id, charges);
  652. }
  653. return 0;
  654. }
  655. int EQ2Emu_lua_RemoveLootItem(lua_State* state) {
  656. if (!lua_interface)
  657. return 0;
  658. Spawn* spawn = lua_interface->GetSpawn(state);
  659. if (spawn && spawn->IsEntity()) {
  660. int32 item_id = lua_interface->GetInt32Value(state, 2);
  661. spawn->LootItem(item_id);
  662. }
  663. return 0;
  664. }
  665. int EQ2Emu_lua_AddLootCoin(lua_State* state) {
  666. if (!lua_interface)
  667. return 0;
  668. Spawn* spawn = lua_interface->GetSpawn(state);
  669. if (spawn) {
  670. int32 val = lua_interface->GetInt32Value(state, 2);
  671. spawn->AddLootCoins(val);
  672. }
  673. return 0;
  674. }
  675. int EQ2Emu_lua_GiveLoot(lua_State* state) {
  676. if (!lua_interface)
  677. return 0;
  678. Spawn* entity = lua_interface->GetSpawn(state);
  679. Spawn* player = lua_interface->GetSpawn(state, 2);
  680. if (entity && player && player->IsPlayer()) {
  681. int32 coins = lua_interface->GetInt32Value(state, 3);
  682. vector<Item*>* items = 0;
  683. int i = 0;
  684. int32 item_id = 0;
  685. while ((item_id = lua_interface->GetInt32Value(state, 4 + i))) {
  686. if (items == 0)
  687. items = new vector<Item*>;
  688. if (master_item_list.GetItem(item_id))
  689. items->push_back(master_item_list.GetItem(item_id));
  690. i++;
  691. }
  692. Client* client = 0;
  693. client = player->GetZone()->GetClientBySpawn(player);
  694. if (client)
  695. ((Player*)player)->AddPendingLootItems(entity->GetID(), items);
  696. if(coins > 0)
  697. entity->AddLootCoins(coins);
  698. safe_delete(items);
  699. }
  700. return 0;
  701. }
  702. int EQ2Emu_lua_HasPendingLootItem(lua_State* state) {
  703. if (!lua_interface)
  704. return 0;
  705. Spawn* entity = lua_interface->GetSpawn(state);
  706. Spawn* player = lua_interface->GetSpawn(state, 2);
  707. int32 item_id = lua_interface->GetInt32Value(state, 3);
  708. if (entity && entity->IsEntity() && player && player->IsPlayer() && item_id > 0) {
  709. lua_interface->SetBooleanValue(state, ((Player*)player)->HasPendingLootItem(entity->GetID(), item_id));
  710. return 1;
  711. }
  712. return 0;
  713. }
  714. int EQ2Emu_lua_HasPendingLoot(lua_State* state) {
  715. if (!lua_interface)
  716. return 0;
  717. Spawn* entity = lua_interface->GetSpawn(state);
  718. Spawn* player = lua_interface->GetSpawn(state, 2);
  719. if (entity && player && player->IsPlayer()) {
  720. lua_interface->SetBooleanValue(state, ((Player*)player)->HasPendingLootItems(entity->GetID()));
  721. return 1;
  722. }
  723. return 0;
  724. }
  725. int EQ2Emu_lua_CreateConversation(lua_State* state) {
  726. if (!lua_interface)
  727. return 0;
  728. vector<ConversationOption>* conversation = lua_interface->GetConversation(state);
  729. safe_delete(conversation);
  730. conversation = new vector<ConversationOption>();
  731. lua_interface->SetConversationValue(state, conversation);
  732. return 1;
  733. }
  734. int EQ2Emu_lua_AddConversationOption(lua_State* state) {
  735. if (!lua_interface)
  736. return 0;
  737. vector<ConversationOption>* conversation = lua_interface->GetConversation(state);
  738. if (conversation) {
  739. ConversationOption conv_option;
  740. conv_option.option = lua_interface->GetStringValue(state, 2);
  741. conv_option.function = lua_interface->GetStringValue(state, 3);
  742. if (conv_option.option.length() > 0)
  743. conversation->push_back(conv_option);
  744. }
  745. return 0;
  746. }
  747. int EQ2Emu_lua_CloseConversation(lua_State* state) {
  748. if (!lua_interface)
  749. return 0;
  750. Spawn* npc = lua_interface->GetSpawn(state);
  751. Spawn* player = lua_interface->GetSpawn(state, 2);
  752. if (npc && player && player->IsPlayer() && player->GetZone()) {
  753. Client* client = player->GetZone()->GetClientBySpawn(player);
  754. if (client) {
  755. int32 conversation_id = client->GetConversationID(npc, 0);
  756. client->CloseDialog(conversation_id);
  757. }
  758. }
  759. return 0;
  760. }
  761. int EQ2Emu_lua_CloseItemConversation(lua_State* state) {
  762. if (!lua_interface)
  763. return 0;
  764. Item* item = lua_interface->GetItem(state);
  765. Spawn* player = lua_interface->GetSpawn(state, 2);
  766. if (item && player && player->IsPlayer() && player->GetZone()) {
  767. Client* client = player->GetZone()->GetClientBySpawn(player);
  768. if (client) {
  769. int32 conversation_id = client->GetConversationID(0, item);
  770. client->CloseDialog(conversation_id);
  771. }
  772. }
  773. return 0;
  774. }
  775. int EQ2Emu_lua_StartDialogConversation(lua_State* state) {
  776. if (!lua_interface)
  777. return 0;
  778. vector<ConversationOption>* conversation = lua_interface->GetConversation(state);
  779. Spawn* spawn = 0;
  780. Item* item = 0;
  781. int8 type = lua_interface->GetInt8Value(state, 2);
  782. if (type == 1 || type == 3)
  783. spawn = lua_interface->GetSpawn(state, 3);
  784. else if (type == 2 || type == 4)
  785. item = lua_interface->GetItem(state, 3);
  786. Spawn* player = lua_interface->GetSpawn(state, 4);
  787. string text = lua_interface->GetStringValue(state, 5);
  788. string mp3 = lua_interface->GetStringValue(state, 6);
  789. int32 key1 = lua_interface->GetInt32Value(state, 7);
  790. int32 key2 = lua_interface->GetInt32Value(state, 8);
  791. if (conversation && text.length() > 0 && (spawn || item) && player && player->IsPlayer()) {
  792. Client* client = player->GetZone()->GetClientBySpawn(player);
  793. if (client) {
  794. if (spawn) {
  795. // Need to do this so the function works the same as it did before
  796. if (type == 1)
  797. type++;
  798. if (mp3.length() > 0)
  799. client->DisplayConversation((Entity*)spawn, type, conversation, const_cast<char*>(text.c_str()), mp3.c_str(), key1, key2);
  800. else
  801. client->DisplayConversation((Entity*)spawn, type, conversation, const_cast<char*>(text.c_str()));
  802. }
  803. else {
  804. if (mp3.length() > 0)
  805. client->DisplayConversation(item, conversation, const_cast<char*>(text.c_str()), type, mp3.c_str(), key1, key2);
  806. else
  807. client->DisplayConversation(item, conversation, const_cast<char*>(text.c_str()), type);
  808. }
  809. }
  810. }
  811. safe_delete(conversation);
  812. lua_interface->SetConversationValue(state, NULL);
  813. return 0;
  814. }
  815. /*int EQ2Emu_lua_StartItemConversation(lua_State* state){
  816. if(!lua_interface)
  817. return 0;
  818. vector<ConversationOption>* conversation = lua_interface->GetConversation(state);
  819. Item* item = lua_interface->GetItem(state, 2);
  820. Spawn* player = lua_interface->GetSpawn(state, 3);
  821. string text = lua_interface->GetStringValue(state, 4);
  822. string mp3 = lua_interface->GetStringValue(state, 5);
  823. int32 key1 = lua_interface->GetInt32Value(state, 6);
  824. int32 key2 = lua_interface->GetInt32Value(state, 7);
  825. if(conversation && text.length() > 0 && item && player && player->IsPlayer()){
  826. Client* client = player->GetZone()->GetClientBySpawn(player);
  827. if(client){
  828. if(mp3.length() > 0)
  829. client->DisplayConversation(item, conversation, (char*)text.c_str(), mp3.c_str(), key1, key2);
  830. else
  831. client->DisplayConversation(item, conversation, (char*)text.c_str());
  832. }
  833. safe_delete(conversation);
  834. }
  835. return 0;
  836. }*/
  837. int EQ2Emu_lua_StartConversation(lua_State* state) {
  838. if (!lua_interface)
  839. return 0;
  840. vector<ConversationOption>* conversation = lua_interface->GetConversation(state);
  841. Spawn* npc = lua_interface->GetSpawn(state, 2);
  842. Spawn* player = lua_interface->GetSpawn(state, 3);
  843. string text = lua_interface->GetStringValue(state, 4);
  844. string mp3 = lua_interface->GetStringValue(state, 5);
  845. int32 key1 = lua_interface->GetInt32Value(state, 6);
  846. int32 key2 = lua_interface->GetInt32Value(state, 7);
  847. if (conversation && conversation->size() > 0 && text.length() > 0 && npc && npc->IsEntity() && player && player->IsPlayer()) {
  848. Client* client = npc->GetZone()->GetClientBySpawn(player);
  849. if (mp3.length() > 0)
  850. client->DisplayConversation((Entity*)npc, 1, conversation, text.c_str(), mp3.c_str(), key1, key2);
  851. else
  852. client->DisplayConversation((Entity*)npc, 1, conversation, text.c_str());
  853. safe_delete(conversation);
  854. lua_interface->SetConversationValue(state, NULL);
  855. }
  856. else
  857. 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);
  858. return 0;
  859. }
  860. int EQ2Emu_lua_SetPlayerProximityFunction(lua_State* state) {
  861. if (!lua_interface)
  862. return 0;
  863. Spawn* spawn = lua_interface->GetSpawn(state);
  864. float distance = lua_interface->GetFloatValue(state, 2);
  865. string in_range_function = lua_interface->GetStringValue(state, 3);
  866. string leaving_range_function = lua_interface->GetStringValue(state, 4);
  867. if (spawn && !spawn->IsPlayer() && distance > 0 && in_range_function.length() > 0)
  868. spawn->GetZone()->AddPlayerProximity(spawn, distance, in_range_function, leaving_range_function);
  869. return 0;
  870. }
  871. int EQ2Emu_lua_SetLocationProximityFunction(lua_State* state) {
  872. ZoneServer* zone = lua_interface->GetZone(state);
  873. float x = lua_interface->GetFloatValue(state, 2);
  874. float y = lua_interface->GetFloatValue(state, 3);
  875. float z = lua_interface->GetFloatValue(state, 4);
  876. float max_variation = lua_interface->GetFloatValue(state, 5);
  877. string in_range_function = lua_interface->GetStringValue(state, 6);
  878. string leaving_range_function = lua_interface->GetStringValue(state, 7);
  879. if (zone && in_range_function.length() > 0)
  880. zone->AddLocationProximity(x, y, z, max_variation, in_range_function, leaving_range_function);
  881. return 0;
  882. }
  883. int EQ2Emu_lua_SetLootCoin(lua_State* state) {
  884. if (!lua_interface)
  885. return 0;
  886. Spawn* spawn = lua_interface->GetSpawn(state);
  887. if (spawn && spawn->IsEntity()) {
  888. int32 val = lua_interface->GetInt32Value(state, 2);
  889. ((Entity*)spawn)->SetLootCoins(val);
  890. }
  891. return 0;
  892. }
  893. int EQ2Emu_lua_GetLootCoin(lua_State* state) {
  894. if (!lua_interface)
  895. return 0;
  896. Spawn* spawn = lua_interface->GetSpawn(state);
  897. if (spawn && spawn->IsEntity()) {
  898. lua_interface->SetInt32Value(state, ((Entity*)spawn)->GetLootCoins());
  899. return 1;
  900. }
  901. return 0;
  902. }
  903. int EQ2Emu_lua_MovementLoopAdd(lua_State* state) {
  904. if (!lua_interface)
  905. return 0;
  906. Spawn* spawn = lua_interface->GetSpawn(state);
  907. float x = lua_interface->GetFloatValue(state, 2);
  908. float y = lua_interface->GetFloatValue(state, 3);
  909. float z = lua_interface->GetFloatValue(state, 4);
  910. float speed = lua_interface->GetFloatValue(state, 5);
  911. int32 delay = lua_interface->GetInt32Value(state, 6); //this should be given as seconds, as it is converted to ms later
  912. string function = lua_interface->GetStringValue(state, 7);
  913. if (spawn) {
  914. spawn->AddMovementLocation(x, y, z, speed, delay, function.c_str());
  915. spawn->GetZone()->AddMovementNPC(spawn);
  916. }
  917. lua_interface->ResetFunctionStack(state);
  918. return 0;
  919. }
  920. int EQ2Emu_lua_IsPlayer(lua_State* state) {
  921. if (!lua_interface)
  922. return 0;
  923. Spawn* spawn = lua_interface->GetSpawn(state);
  924. if (spawn) {
  925. lua_interface->SetBooleanValue(state, spawn->IsPlayer());
  926. return 1;
  927. }
  928. return 0;
  929. }
  930. int EQ2Emu_lua_FaceTarget(lua_State* state) {
  931. if (!lua_interface)
  932. return 0;
  933. Spawn* spawn = lua_interface->GetSpawn(state);
  934. Spawn* target = lua_interface->GetSpawn(state, 2);
  935. if (spawn && target) {
  936. if (spawn->IsEntity())
  937. // ((Entity*)spawn)->FaceTarget(target);
  938. static_cast<Entity*>(spawn)->FaceTarget(target);
  939. }
  940. lua_interface->ResetFunctionStack(state);
  941. return 0;
  942. }
  943. int EQ2Emu_lua_MoveToLocation(lua_State* state) {
  944. if (!lua_interface)
  945. return 0;
  946. Spawn* spawn = lua_interface->GetSpawn(state);
  947. float x = lua_interface->GetFloatValue(state, 2);
  948. float y = lua_interface->GetFloatValue(state, 3);
  949. float z = lua_interface->GetFloatValue(state, 4);
  950. float speed = lua_interface->GetFloatValue(state, 5);
  951. string lua_function = lua_interface->GetStringValue(state, 6);
  952. bool more_points = lua_interface->GetBooleanValue(state, 7);
  953. if (spawn) {
  954. if (speed == 0)
  955. speed = spawn->GetSpeed();
  956. spawn->AddRunningLocation(x, y, z, speed, 0.0f, true, !more_points, lua_function);
  957. }
  958. lua_interface->ResetFunctionStack(state);
  959. return 0;
  960. }
  961. int EQ2Emu_lua_ClearRunningLocations(lua_State* state) {
  962. if (!lua_interface)
  963. return 0;
  964. Spawn* spawn = lua_interface->GetSpawn(state);
  965. if (spawn) {
  966. spawn->ClearRunningLocations();
  967. }
  968. return 0;
  969. }
  970. int EQ2Emu_lua_Say(lua_State* state) {
  971. if (!lua_interface)
  972. return 0;
  973. Spawn* spawn = lua_interface->GetSpawn(state);
  974. string message = lua_interface->GetStringValue(state, 2);
  975. Spawn* player = lua_interface->GetSpawn(state, 3);
  976. int32 language = lua_interface->GetInt32Value(state, 4);
  977. if (spawn && message.length() > 0) {
  978. Client* client = 0;
  979. if (player && player->IsPlayer())
  980. client = spawn->GetZone()->GetClientBySpawn(player);
  981. if (client)
  982. spawn->GetZone()->HandleChatMessage(client, spawn, 0, CHANNEL_SAY, message.c_str(), 30, 0, true, language);
  983. else
  984. spawn->GetZone()->HandleChatMessage(spawn, 0, CHANNEL_SAY, message.c_str(), 30, 0, true, language);
  985. }
  986. lua_interface->ResetFunctionStack(state);
  987. return 0;
  988. }
  989. int EQ2Emu_lua_Shout(lua_State* state) {
  990. if (!lua_interface)
  991. return 0;
  992. Spawn* spawn = lua_interface->GetSpawn(state);
  993. string message = lua_interface->GetStringValue(state, 2);
  994. Spawn* player = lua_interface->GetSpawn(state, 3);
  995. if (spawn && message.length() > 0) {
  996. Client* client = 0;
  997. if (player && player->IsPlayer())
  998. client = spawn->GetZone()->GetClientBySpawn(player);
  999. if (client)
  1000. spawn->GetZone()->HandleChatMessage(client, spawn, 0, CHANNEL_SHOUT, message.c_str(), 30);
  1001. else
  1002. spawn->GetZone()->HandleChatMessage(spawn, 0, CHANNEL_SHOUT, message.c_str(), 30);
  1003. }
  1004. lua_interface->ResetFunctionStack(state);
  1005. return 0;
  1006. }
  1007. int EQ2Emu_lua_SayOOC(lua_State* state) {
  1008. if (!lua_interface)
  1009. return 0;
  1010. Spawn* spawn = lua_interface->GetSpawn(state);
  1011. string message = lua_interface->GetStringValue(state, 2);
  1012. Spawn* player = lua_interface->GetSpawn(state, 3);
  1013. if (spawn && message.length() > 0) {
  1014. Client* client = 0;
  1015. if (player && player->IsPlayer())
  1016. client = spawn->GetZone()->GetClientBySpawn(player);
  1017. if (client)
  1018. spawn->GetZone()->HandleChatMessage(client, spawn, 0, CHANNEL_OUT_OF_CHARACTER, message.c_str(), 30);
  1019. else
  1020. spawn->GetZone()->HandleChatMessage(spawn, 0, CHANNEL_OUT_OF_CHARACTER, message.c_str(), 30);
  1021. }
  1022. lua_interface->ResetFunctionStack(state);
  1023. return 0;
  1024. }
  1025. int EQ2Emu_lua_Emote(lua_State* state) {
  1026. if (!lua_interface)
  1027. return 0;
  1028. Spawn* spawn = lua_interface->GetSpawn(state);
  1029. string message = lua_interface->GetStringValue(state, 2);
  1030. Spawn* spawn2 = lua_interface->GetSpawn(state, 3);
  1031. Spawn* player = lua_interface->GetSpawn(state, 4);
  1032. char* to = 0;
  1033. if (spawn2)
  1034. to = spawn2->GetName();
  1035. if (spawn && message.length() > 0) {
  1036. Client* client = 0;
  1037. if (player && player->IsPlayer())
  1038. client = spawn->GetZone()->GetClientBySpawn(player);
  1039. if (client)
  1040. spawn->GetZone()->HandleChatMessage(client, spawn, to, CHANNEL_EMOTE, message.c_str(), 30);
  1041. else
  1042. spawn->GetZone()->HandleChatMessage(spawn, to, CHANNEL_EMOTE, message.c_str(), 30);
  1043. }
  1044. lua_interface->ResetFunctionStack(state);
  1045. return 0;
  1046. }
  1047. int EQ2Emu_lua_SpellHeal(lua_State* state) {
  1048. if (!lua_interface)
  1049. return 0;
  1050. LuaSpell* luaspell = lua_interface->GetCurrentSpell(state);
  1051. if (!luaspell)
  1052. return 0;
  1053. Spawn* caster = luaspell->caster;
  1054. string heal_type = lua_interface->GetStringValue(state);//power, heal ect
  1055. int32 min_heal = lua_interface->GetInt32Value(state, 2);
  1056. int32 max_heal = lua_interface->GetInt32Value(state, 3);
  1057. Spawn* target = lua_interface->GetSpawn(state, 4);
  1058. int8 crit_mod = lua_interface->GetInt32Value(state, 5);
  1059. bool no_calcs = lua_interface->GetInt32Value(state, 6) == 1;
  1060. string custom_spell_name = lua_interface->GetStringValue(state, 7);//custom spell name
  1061. lua_interface->ResetFunctionStack(state);
  1062. if (caster && caster->IsEntity()) {
  1063. bool success = false;
  1064. luaspell->resisted = false;
  1065. if (target) {
  1066. float distance = caster->GetDistance(target, true);
  1067. if (((Entity*)caster)->SpellHeal(target, distance, luaspell, heal_type, min_heal, max_heal, crit_mod, no_calcs, custom_spell_name))
  1068. success = true;
  1069. }
  1070. if (luaspell->targets.size() > 0) {
  1071. Spawn* target = 0;
  1072. ZoneServer* zone = luaspell->caster->GetZone();
  1073. luaspell->MSpellTargets.readlock(__FUNCTION__, __LINE__);
  1074. for (int32 i = 0; i < luaspell->targets.size(); i++) {
  1075. if ((target = zone->GetSpawnByID(luaspell->targets[i]))) {
  1076. float distance = caster->GetDistance(target, true);
  1077. ((Entity*)caster)->SpellHeal(target, distance, luaspell, heal_type, min_heal, max_heal, crit_mod, no_calcs, custom_spell_name);
  1078. }
  1079. }
  1080. luaspell->MSpellTargets.releasereadlock(__FUNCTION__, __LINE__);
  1081. success = true;
  1082. }
  1083. if (success) {
  1084. if (caster->GetZone())
  1085. caster->GetZone()->TriggerCharSheetTimer();
  1086. }
  1087. }
  1088. return 0;
  1089. }
  1090. int EQ2Emu_lua_AddItem(lua_State* state) {
  1091. if (!lua_interface)
  1092. return 0;
  1093. Spawn* spawn = lua_interface->GetSpawn(state);
  1094. int32 item_id = lua_interface->GetInt32Value(state, 2);
  1095. int16 quantity = lua_interface->GetInt32Value(state, 3);
  1096. // default of 1 quantity to add
  1097. if (quantity == 0)
  1098. quantity = 1;
  1099. if (spawn && spawn->IsPlayer()) {
  1100. Client* client = spawn->GetZone()->GetClientBySpawn(spawn);
  1101. if (client && item_id > 0) {
  1102. lua_interface->SetBooleanValue(state, client->AddItem(item_id, quantity));
  1103. return 1;
  1104. }
  1105. }
  1106. lua_interface->SetBooleanValue(state, false);
  1107. return 1;
  1108. }
  1109. int EQ2Emu_lua_SummonItem(lua_State* state) {
  1110. if (!lua_interface)
  1111. return 0;
  1112. Spawn* spawn = lua_interface->GetSpawn(state);
  1113. int32 item_id = lua_interface->GetInt32Value(state, 2);
  1114. bool send_messages = (lua_interface->GetInt8Value(state, 3) == 1);
  1115. string location = lua_interface->GetStringValue(state, 4);
  1116. if (spawn && spawn->IsPlayer()) {
  1117. Client* client = spawn->GetZone()->GetClientBySpawn(spawn);
  1118. if (client && item_id > 0) {
  1119. if (strncasecmp(location.c_str(), "bank", 4) == 0)
  1120. lua_interface->SetBooleanValue(state, client->AddItemToBank(item_id, 1));
  1121. else
  1122. lua_interface->SetBooleanValue(state, client->AddItem(item_id, 1));
  1123. if (send_messages) {
  1124. Item* item = master_item_list.GetItem(item_id);
  1125. if (item) {
  1126. client->Message(CHANNEL_COLOR_YELLOW, "You receive %s.", item->CreateItemLink(client->GetVersion()).c_str());
  1127. string popup_text = "You receive " + item->name;
  1128. client->SendPopupMessage(10, popup_text.c_str(), "ui_harvested_normal", 3, 0xFF, 0xFF, 0xFF);
  1129. }
  1130. }
  1131. return 1;
  1132. }
  1133. }
  1134. lua_interface->SetBooleanValue(state, false);
  1135. return 1;
  1136. }
  1137. int EQ2Emu_lua_RemoveItem(lua_State* state) {
  1138. Spawn* spawn = lua_interface->GetSpawn(state);
  1139. int32 item_id = lua_interface->GetInt32Value(state, 2);
  1140. int16 quantity = lua_interface->GetInt16Value(state, 3);
  1141. // default of 1 to remove
  1142. if (quantity == 0)
  1143. quantity = 1;
  1144. Client* client;
  1145. Item* item;
  1146. if (spawn && spawn->IsPlayer() && item_id > 0) {
  1147. if ((client = spawn->GetZone()->GetClientBySpawn(spawn))) {
  1148. if ((item = client->GetPlayer()->item_list.GetItemFromID(item_id))) {
  1149. if (client->RemoveItem(item, quantity)) {
  1150. lua_interface->SetBooleanValue(state, true);
  1151. return 1;
  1152. }
  1153. }
  1154. }
  1155. }
  1156. lua_interface->SetBooleanValue(state, false);
  1157. return 1;
  1158. }
  1159. int EQ2Emu_lua_HasItem(lua_State* state) {
  1160. Spawn* player = lua_interface->GetSpawn(state);
  1161. int32 item_id = lua_interface->GetInt32Value(state, 2);
  1162. bool include_bank = lua_interface->GetInt8Value(state, 3);
  1163. if (player && player->IsPlayer()) {
  1164. bool hasItem = false;
  1165. hasItem = ((Player*)player)->item_list.HasItem(item_id, include_bank);
  1166. if (!hasItem)
  1167. hasItem = ((Player*)player)->GetEquipmentList()->HasItem(item_id);
  1168. lua_interface->SetBooleanValue(state, hasItem);
  1169. return 1;
  1170. }
  1171. lua_interface->SetBooleanValue(state, false);
  1172. return 1;
  1173. }
  1174. int EQ2Emu_lua_Spawn(lua_State* state) {
  1175. if (!lua_interface)
  1176. return 0;
  1177. ZoneServer* zone = lua_interface->GetZone(state);
  1178. int32 spawn_id = lua_interface->GetInt32Value(state, 2);
  1179. bool restricted_npc = (lua_interface->GetInt8Value(state, 3) == 1);
  1180. float x = lua_interface->GetFloatValue(state, 4);
  1181. float y = lua_interface->GetFloatValue(state, 5);
  1182. float z = lua_interface->GetFloatValue(state, 6);
  1183. float heading = lua_interface->GetFloatValue(state, 7);
  1184. if (zone && spawn_id > 0 && (x != 0 || y != 0 || z != 0)) {
  1185. Spawn* spawn = zone->GetSpawn(spawn_id);
  1186. if (!spawn)
  1187. lua_interface->LogError("%s: LUA Spawn command error: Could not find spawn with id of %u.", lua_interface->GetScriptName(state), spawn_id);
  1188. else {
  1189. spawn->SetX(x);
  1190. spawn->SetZ(z);
  1191. spawn->SetY(y,true,true);
  1192. spawn->SetLocation(zone->GetClosestLocation(spawn));
  1193. spawn->SetHeading(heading);
  1194. if (restricted_npc)
  1195. spawn->AddAllowAccessSpawn(spawn);
  1196. const char* spawn_script = world.GetSpawnScript(spawn_id);
  1197. bool scriptActive = false;
  1198. if (spawn_script && lua_interface->GetSpawnScript(spawn_script) != 0) {
  1199. scriptActive = true;
  1200. spawn->SetSpawnScript(string(spawn_script));
  1201. }
  1202. zone->CallSpawnScript(spawn, SPAWN_SCRIPT_PRESPAWN);
  1203. zone->AddSpawn(spawn);
  1204. if (scriptActive) {
  1205. zone->CallSpawnScript(spawn, SPAWN_SCRIPT_SPAWN);
  1206. }
  1207. lua_interface->SetSpawnValue(state, spawn);
  1208. return 1;
  1209. }
  1210. }
  1211. else {
  1212. string output = "Invalid paramaters to LUA Spawn command: \n";
  1213. if (!zone)
  1214. output = output.append("\t").append("Missing zone reference. \n");
  1215. if (spawn_id == 0)
  1216. output = output.append("\t").append("Missing spawn_id.");
  1217. lua_interface->LogError("%s: Error in EQ2Emu_lua_Spawn - %s", lua_interface->GetScriptName(state), output.c_str());
  1218. }
  1219. return 0;
  1220. }
  1221. int EQ2Emu_lua_GetZoneName(lua_State* state) {
  1222. if (!lua_interface)
  1223. return 0;
  1224. ZoneServer* zone = lua_interface->GetZone(state);
  1225. if (zone) {
  1226. lua_interface->SetStringValue(state, zone->GetZoneName());
  1227. return 1;
  1228. }
  1229. return 0;
  1230. }
  1231. int EQ2Emu_lua_GetZoneID(lua_State* state) {
  1232. if (!lua_interface)
  1233. return 0;
  1234. ZoneServer* zone = lua_interface->GetZone(state);
  1235. if (zone) {
  1236. lua_interface->SetInt32Value(state, zone->GetZoneID());
  1237. return 1;
  1238. }
  1239. return 0;
  1240. }
  1241. int EQ2Emu_lua_GetZone(lua_State* state) {
  1242. if (!lua_interface)
  1243. return 0;
  1244. int32 zone_id = lua_interface->GetInt32Value(state);
  1245. ZoneServer* zone = 0;
  1246. if (zone_id > 0)
  1247. zone = zone_list.Get(zone_id);
  1248. else {
  1249. string zone_name = lua_interface->GetStringValue(state);
  1250. if (zone_name.length() > 0) {
  1251. zone = zone_list.Get(zone_name.c_str());
  1252. }
  1253. else {
  1254. Spawn* spawn = lua_interface->GetSpawn(state);
  1255. if (spawn)
  1256. zone = spawn->GetZone();
  1257. }
  1258. }
  1259. if (zone) {
  1260. lua_interface->SetZoneValue(state, zone);
  1261. return 1;
  1262. }
  1263. return 0;
  1264. }
  1265. int EQ2Emu_lua_AddHate(lua_State* state) {
  1266. Spawn* entity = lua_interface->GetSpawn(state);
  1267. Spawn* npc = lua_interface->GetSpawn(state, 2);
  1268. sint32 amount = lua_interface->GetSInt32Value(state, 3);
  1269. bool send_packet = lua_interface->GetInt8Value(state, 4) == 1 ? true : false;
  1270. LuaSpell* luaspell = lua_interface->GetCurrentSpell(state);
  1271. if (entity && entity->IsEntity() && amount != 0) {
  1272. if (luaspell) {
  1273. ZoneServer* zone = luaspell->caster->GetZone();
  1274. luaspell->MSpellTargets.readlock(__FUNCTION__, __LINE__);
  1275. for (int32 i = 0; i < luaspell->targets.size(); i++) {
  1276. Spawn* spawn = zone->GetSpawnByID(luaspell->targets.at(i));
  1277. if (spawn && spawn->IsNPC() && spawn->Alive() && spawn->GetZone()) {
  1278. ((NPC*)spawn)->AddHate((Entity*)entity, amount);
  1279. if (send_packet)
  1280. entity->GetZone()->SendThreatPacket(entity, npc, amount, luaspell->spell->GetName());
  1281. }
  1282. }
  1283. luaspell->MSpellTargets.releasereadlock(__FUNCTION__, __LINE__);
  1284. }
  1285. else if (npc && npc->IsNPC() && npc->GetZone())
  1286. ((NPC*)npc)->AddHate((Entity*)entity, amount);
  1287. }
  1288. return 0;
  1289. }
  1290. int EQ2Emu_lua_Zone(lua_State* state) {
  1291. if (!lua_interface)
  1292. return 0;
  1293. ZoneServer* zone = lua_interface->GetZone(state);
  1294. Spawn* player = lua_interface->GetSpawn(state, 2);
  1295. Client* client = 0;
  1296. if (player && player->IsPlayer())
  1297. client = player->GetZone()->GetClientBySpawn(player);
  1298. float x = lua_interface->GetFloatValue(state, 3);
  1299. float y = lua_interface->GetFloatValue(state, 4);
  1300. float z = lua_interface->GetFloatValue(state, 5);
  1301. float heading = lua_interface->GetFloatValue(state, 6);
  1302. if (zone && client) {
  1303. LogWrite(LUA__DEBUG, 0, "LUA", "LUA Zone Request by Player: '%s' (%u)", player->GetName(), player->GetID());
  1304. LogWrite(LUA__DEBUG, 5, "LUA", "\tTo Zone: '%s' (%u)", zone->GetZoneName(), zone->GetZoneID());
  1305. if (!client->CheckZoneAccess(zone->GetZoneName()))
  1306. {
  1307. LogWrite(LUA__WARNING, 0, "LUA", "CheckZoneAccess() FAILED! LUA Zone Request Denied!");
  1308. return 0;
  1309. }
  1310. if (x != 0 || y != 0 || z != 0) {
  1311. LogWrite(LUA__DEBUG, 5, "LUA", "\tTo Coordinates: %2f, %2f, %2f, %2f", x, y, z, heading);
  1312. player->SetX(x);
  1313. player->SetY(y);
  1314. player->SetZ(z);
  1315. player->SetHeading(heading);
  1316. client->Zone(zone->GetZoneName(), false);
  1317. }
  1318. else
  1319. client->Zone(zone->GetZoneName());
  1320. }
  1321. else
  1322. lua_interface->LogError("%s: Error in EQ2Emu_lua_Zone: invalid zone or spawn input.", lua_interface->GetScriptName(state));
  1323. return 0;
  1324. }
  1325. int EQ2Emu_lua_AddSpawnAccess(lua_State* state) {
  1326. if (!lua_interface)
  1327. return 0;
  1328. Spawn* spawn = lua_interface->GetSpawn(state);
  1329. Spawn* spawn2 = lua_interface->GetSpawn(state, 2);
  1330. if (spawn && spawn2)
  1331. spawn->AddAllowAccessSpawn(spawn2);
  1332. return 0;
  1333. }
  1334. int EQ2Emu_lua_CastSpell(lua_State* state) {
  1335. if (!lua_interface)
  1336. return 0;
  1337. Spawn* target = lua_interface->GetSpawn(state);
  1338. int32 spell_id = lua_interface->GetInt32Value(state, 2);
  1339. int8 spell_tier = lua_interface->GetInt8Value(state, 3);
  1340. Spawn* caster = lua_interface->GetSpawn(state, 4);
  1341. int16 custom_cast_time = lua_interface->GetInt16Value(state, 5);
  1342. if (!target) {
  1343. lua_interface->LogError("%s: LUA CastSpell command error: target is not a valid spawn", lua_interface->GetScriptName(state));
  1344. return 0;
  1345. }
  1346. if (!target->IsEntity()) {
  1347. lua_interface->LogError("%s: LUA CastSpell command error: target (%s) is not an entity", lua_interface->GetScriptName(state), target->GetName());
  1348. return 0;
  1349. }
  1350. if (spell_id <= 0) {
  1351. lua_interface->LogError("%s: LUA CastSpell command error: spell id is not valid", lua_interface->GetScriptName(state));
  1352. return 0;
  1353. }
  1354. if (caster && !caster->IsEntity()) {
  1355. lua_interface->LogError("%s: LUA CastSpell command error: caster (%s) is not an entity", lua_interface->GetScriptName(state), caster->GetName());
  1356. return 0;
  1357. }
  1358. if (spell_tier == 0)
  1359. spell_tier = 1;
  1360. if (!caster)
  1361. caster = target;
  1362. target->GetZone()->ProcessSpell(master_spell_list.GetSpell(spell_id, spell_tier), (Entity*)caster, (Entity*)target, true, false, NULL, custom_cast_time);
  1363. return 0;
  1364. }
  1365. int EQ2Emu_lua_SpellDamage(lua_State* state) {
  1366. if (!lua_interface)
  1367. return 0;
  1368. Spawn* target = lua_interface->GetSpawn(state);
  1369. int32 target_id = 0;
  1370. if (target)
  1371. target_id = target->GetID();
  1372. LuaSpell* luaspell = lua_interface->GetCurrentSpell(state);
  1373. if (!luaspell)
  1374. return 0;
  1375. Spawn* caster = luaspell->caster;
  1376. sint32 type = lua_interface->GetSInt32Value(state, 2);
  1377. int32 min_damage = lua_interface->GetInt32Value(state, 3);
  1378. int32 max_damage = lua_interface->GetInt32Value(state, 4);
  1379. int8 crit_mod = lua_interface->GetInt32Value(state, 5);
  1380. bool no_calcs = lua_interface->GetInt32Value(state, 6) == 1;
  1381. //lua_interface->ResetFunctionStack(state);
  1382. int32 class_id = lua_interface->GetInt32Value(state, 7);
  1383. vector<int16> faction_req;
  1384. vector<int16> race_req;
  1385. int32 class_req = 0;
  1386. int32 i = 0;
  1387. int8 f = 0;
  1388. int8 r = 0;
  1389. while ((class_id = lua_interface->GetInt32Value(state, 7 + i))) {
  1390. if (class_id < 100) {
  1391. class_req += pow(2.0, double(class_id - 1));
  1392. }
  1393. else if (class_id > 100 && class_id < 1000) {
  1394. race_req.push_back(class_id);
  1395. r++;
  1396. }
  1397. else {
  1398. faction_req.push_back(class_id);
  1399. f++;
  1400. }
  1401. i++;
  1402. }
  1403. if (caster && caster->IsEntity()) {
  1404. bool race_match = false;
  1405. bool success = false;
  1406. luaspell->resisted = false;
  1407. if (luaspell->initial_target == target_id) {
  1408. int xxx = 0;
  1409. }
  1410. if (luaspell->targets.size() > 0) {
  1411. ZoneServer* zone = luaspell->caster->GetZone();
  1412. Spawn* target = 0;
  1413. luaspell->MSpellTargets.readlock(__FUNCTION__, __LINE__);
  1414. for (int32 i = 0; i < luaspell->targets.size(); i++) {
  1415. if ((target = zone->GetSpawnByID(luaspell->targets[i]))) {
  1416. if (race_req.size() > 0) {
  1417. for (int8 i = 0; i < race_req.size(); i++) {
  1418. int32 xxx = target->GetLuaRaceId();
  1419. if (target->GetLuaRaceId() == race_req[i]) {
  1420. race_match = true;
  1421. }
  1422. }
  1423. }
  1424. else
  1425. race_match = true; // if the race_req.size = 0 then there is no race requirement and the race_match will be true
  1426. if (race_match == true) {
  1427. float distance = caster->GetDistance(target, true);
  1428. ((Entity*)caster)->SpellAttack(target, distance, luaspell, type, min_damage, max_damage, crit_mod, no_calcs);
  1429. }
  1430. }
  1431. }
  1432. success = true;
  1433. luaspell->MSpellTargets.releasereadlock(__FUNCTION__, __LINE__);
  1434. }
  1435. else if (target) {
  1436. //check class and race/faction here
  1437. if (race_req.size() > 0) {
  1438. for (int8 i = 0; i < race_req.size(); i++) {
  1439. if (target->GetLuaRaceId() == race_req[i]) {
  1440. race_match = true;
  1441. }
  1442. }
  1443. }
  1444. else
  1445. race_match = true; // if the race_req.size = 0 then there is no race requirement and the race_match will be true
  1446. if (race_match == true) {
  1447. float distance = caster->GetDistance(target, true);
  1448. if (((Entity*)caster)->SpellAttack(target, distance, luaspell, type, min_damage, max_damage, crit_mod, no_calcs))
  1449. success = true;
  1450. }
  1451. }
  1452. if (success) {
  1453. Spell* spell = luaspell->spell;
  1454. if (caster->IsPlayer() && spell && spell->GetSpellData()->target_type == 1 && spell->GetSpellData()->spell_book_type == 1) { //offense combat art
  1455. ((Player*)caster)->InCombat(true);
  1456. if (caster->GetZone())
  1457. caster->GetZone()->TriggerCharSheetTimer();
  1458. }
  1459. }
  1460. }
  1461. return 0;
  1462. }
  1463. int EQ2Emu_lua_ModifyPower(lua_State* state) {
  1464. if (!lua_interface)
  1465. return 0;
  1466. Spawn* spawn = lua_interface->GetSpawn(state);
  1467. sint32 value = lua_interface->GetSInt32Value(state, 2);
  1468. lua_interface->ResetFunctionStack(state);
  1469. if (spawn && value != 0) {
  1470. if (spawn->GetPower() + value > spawn->GetTotalPower())
  1471. spawn->SetPower(spawn->GetTotalPower());
  1472. else
  1473. spawn->SetPower(spawn->GetPower() + value);
  1474. }
  1475. return 0;
  1476. }
  1477. int EQ2Emu_lua_ModifyHP(lua_State* state) {
  1478. if (!lua_interface)
  1479. return 0;
  1480. Spawn* spawn = lua_interface->GetSpawn(state);
  1481. sint32 value = lua_interface->GetSInt32Value(state, 2);
  1482. lua_interface->ResetFunctionStack(state);
  1483. if (spawn && value != 0) {
  1484. if (spawn->GetHP() + value > spawn->GetTotalHP())
  1485. spawn->SetHP(spawn->GetTotalHP());
  1486. else
  1487. spawn->SetHP(spawn->GetHP() + value);
  1488. }
  1489. return 0;
  1490. }
  1491. int EQ2Emu_lua_ModifyMaxPower(lua_State* state) {
  1492. if (!lua_interface)
  1493. return 0;
  1494. Spawn* spawn = lua_interface->GetSpawn(state);
  1495. sint32 value = lua_interface->GetSInt32Value(state, 2);
  1496. lua_interface->ResetFunctionStack(state);
  1497. if (spawn && value != 0) {
  1498. spawn->SetPower(spawn->GetPower() + value);
  1499. if (value > spawn->GetTotalHPBase())
  1500. spawn->SetTotalPower(spawn->GetTotalPower() + value);
  1501. }
  1502. return 0;
  1503. }
  1504. int EQ2Emu_lua_ModifyMaxHP(lua_State* state) {
  1505. if (!lua_interface)
  1506. return 0;
  1507. Spawn* spawn = lua_interface->GetSpawn(state);
  1508. sint32 value = lua_interface->GetSInt32Value(state, 2);
  1509. lua_interface->ResetFunctionStack(state);
  1510. if (spawn && value != 0) {
  1511. spawn->SetHP(spawn->GetHP() + value);
  1512. if (value > spawn->GetTotalHPBase())
  1513. spawn->SetTotalHP(spawn->GetTotalHP() + value);
  1514. }
  1515. return 0;
  1516. }
  1517. int EQ2Emu_lua_SetCurrentHP(lua_State* state) {
  1518. if (!lua_interface)
  1519. return 0;
  1520. Spawn* spawn = lua_interface->GetSpawn(state);
  1521. sint32 value = lua_interface->GetSInt32Value(state, 2);
  1522. lua_interface->ResetFunctionStack(state);
  1523. if (spawn && value > 0) {
  1524. spawn->SetHP(value);
  1525. if (value > spawn->GetTotalHPBase())
  1526. spawn->SetTotalHP(value);
  1527. }
  1528. return 0;
  1529. }
  1530. int EQ2Emu_lua_SetMaxHP(lua_State* state) {
  1531. if (!lua_interface)
  1532. return 0;
  1533. Spawn* spawn = lua_interface->GetSpawn(state);
  1534. float value = lua_interface->GetFloatValue(state, 2);
  1535. lua_interface->ResetFunctionStack(state);
  1536. if (spawn && spawn->IsEntity() && value > 0)
  1537. ((Entity*)spawn)->AddSpellBonus(0, ITEM_STAT_HEALTH, value - spawn->GetTotalHP());
  1538. if (spawn->IsPlayer())
  1539. ((Player*)spawn)->SetCharSheetChanged(true);
  1540. return 0;
  1541. }
  1542. int EQ2Emu_lua_SetMaxHPBase(lua_State* state) {
  1543. if (!lua_interface)
  1544. return 0;
  1545. Spawn* spawn = lua_interface->GetSpawn(state);
  1546. sint32 value = lua_interface->GetSInt32Value(state, 2);
  1547. lua_interface->ResetFunctionStack(state);
  1548. if (spawn && spawn->IsEntity() && value > 0)
  1549. ((Entity*)spawn)->SetTotalHPBase(value);
  1550. return 0;
  1551. }
  1552. int EQ2Emu_lua_SetCurrentPower(lua_State* state) {
  1553. if (!lua_interface)
  1554. return 0;
  1555. Spawn* spawn = lua_interface->GetSpawn(state);
  1556. sint32 value = lua_interface->GetSInt32Value(state, 2);
  1557. lua_interface->ResetFunctionStack(state);
  1558. if (spawn && value > 0) {
  1559. spawn->SetPower(value);
  1560. if (value > spawn->GetTotalPowerBase())
  1561. spawn->SetTotalPower(value);
  1562. }
  1563. return 0;
  1564. }
  1565. int EQ2Emu_lua_SetMaxPower(lua_State* state) {
  1566. if (!lua_interface)
  1567. return 0;
  1568. Spawn* spawn = lua_interface->GetSpawn(state);
  1569. sint32 value = lua_interface->GetSInt32Value(state, 2);
  1570. lua_interface->ResetFunctionStack(state);
  1571. if (spawn && spawn->IsEntity() && value > 0)
  1572. ((Entity*)spawn)->AddSpellBonus(0, ITEM_STAT_POWER, value - spawn->GetTotalPower());
  1573. return 0;
  1574. }
  1575. int EQ2Emu_lua_SetMaxPowerBase(lua_State* state) {
  1576. if (!lua_interface)
  1577. return 0;
  1578. Spawn* spawn = lua_interface->GetSpawn(state);
  1579. sint32 value = lua_interface->GetSInt32Value(state, 2);
  1580. lua_interface->ResetFunctionStack(state);
  1581. if (spawn && spawn->IsEntity() && value > 0)
  1582. ((Entity*)spawn)->SetTotalPowerBase(value);
  1583. return 0;
  1584. }
  1585. int EQ2Emu_lua_SetPosition(lua_State* state) {
  1586. if (!lua_interface)
  1587. return 0;
  1588. Spawn* spawn = lua_interface->GetSpawn(state);
  1589. float x = lua_interface->GetFloatValue(state, 2);
  1590. float y = lua_interface->GetFloatValue(state, 3);
  1591. float z = lua_interface->GetFloatValue(state, 4);
  1592. float heading = lua_interface->GetFloatValue(state, 5);
  1593. lua_interface->ResetFunctionStack(state);
  1594. if (spawn) {
  1595. spawn->SetX(x);
  1596. spawn->SetY(y);
  1597. spawn->SetZ(z);
  1598. if (heading != 0)
  1599. spawn->SetHeading(heading);
  1600. spawn->SetSpawnOrigX(spawn->GetX());
  1601. spawn->SetSpawnOrigY(spawn->GetY());
  1602. spawn->SetSpawnOrigZ(spawn->GetZ());
  1603. spawn->SetSpawnOrigHeading(spawn->GetHeading());
  1604. if (spawn->IsPlayer()) {
  1605. Client* client = spawn->GetZone()->GetClientBySpawn(spawn);
  1606. if (client) {
  1607. EQ2Packet* packet = client->GetPlayer()->Move(x, y, z, client->GetVersion(), (heading == 0 ? -1.0f : (heading + 180.0f)));
  1608. client->QueuePacket(packet);
  1609. }
  1610. }
  1611. }
  1612. return 0;
  1613. }
  1614. int EQ2Emu_lua_SetHeading(lua_State* state) {
  1615. if (!lua_interface)
  1616. return 0;
  1617. Spawn* spawn = lua_interface->GetSpawn(state);
  1618. float value = lua_interface->GetFloatValue(state, 2);
  1619. lua_interface->ResetFunctionStack(state);
  1620. if (spawn) {
  1621. spawn->SetHeading(value);
  1622. if (spawn->IsPlayer()) {
  1623. Client* client = spawn->GetZone()->GetClientBySpawn(spawn);
  1624. if (client) {
  1625. EQ2Packet* packet = client->GetPlayer()->Move(spawn->GetX(), spawn->GetY(), spawn->GetZ(), client->GetVersion(), value + 180.0f);
  1626. client->QueuePacket(packet);
  1627. }
  1628. }
  1629. }
  1630. return 0;
  1631. }
  1632. int EQ2Emu_lua_SetModelType(lua_State* state) {
  1633. if (!lua_interface)
  1634. return 0;
  1635. Spawn* spawn = lua_interface->GetSpawn(state);
  1636. int16 value = lua_interface->GetInt16Value(state, 2);
  1637. lua_interface->ResetFunctionStack(state);
  1638. if (spawn)
  1639. spawn->SetModelType(value);
  1640. return 0;
  1641. }
  1642. int EQ2Emu_lua_SetAdventureClass(lua_State* state) {
  1643. if (!lua_interface)
  1644. return 0;
  1645. Spawn* spawn = lua_interface->GetSpawn(state);
  1646. int8 value = lua_interface->GetInt8Value(state, 2);
  1647. lua_interface->ResetFunctionStack(state);
  1648. if (spawn) {
  1649. if (spawn->IsPlayer())
  1650. ((Player*)spawn)->SetPlayerAdventureClass(value);
  1651. else
  1652. spawn->SetAdventureClass(value);
  1653. }
  1654. return 0;
  1655. }
  1656. int EQ2Emu_lua_SetTradeskillClass(lua_State* state) {
  1657. if (!lua_interface)
  1658. return 0;
  1659. Spawn* spawn = lua_interface->GetSpawn(state);
  1660. int8 value = lua_interface->GetInt8Value(state, 2);
  1661. lua_interface->ResetFunctionStack(state);
  1662. if (spawn) {
  1663. spawn->SetTradeskillClass(value);
  1664. if (spawn->IsEntity()) {
  1665. ((Entity*)spawn)->GetInfoStruct()->tradeskill_class1 = classes.GetTSBaseClass(spawn->GetTradeskillClass());
  1666. ((Entity*)spawn)->GetInfoStruct()->tradeskill_class2 = classes.GetSecondaryTSBaseClass(spawn->GetTradeskillClass());
  1667. ((Entity*)spawn)->GetInfoStruct()->tradeskill_class3 = spawn->GetTradeskillClass();
  1668. }
  1669. if (spawn->IsPlayer())
  1670. ((Player*)spawn)->SetCharSheetChanged(true);
  1671. }
  1672. return 0;
  1673. }
  1674. int EQ2Emu_lua_SetMount(lua_State* state) {
  1675. if (!lua_interface)
  1676. return 0;
  1677. Spawn* spawn = lua_interface->GetSpawn(state);
  1678. int16 value = lua_interface->GetInt16Value(state, 2);
  1679. if (spawn && spawn->IsEntity()) {
  1680. ((Entity*)spawn)->SetMount(value);
  1681. EQ2_Color color;
  1682. color.red = 255;
  1683. color.green = 255;
  1684. color.blue = 255;
  1685. ((Entity*)spawn)->SetMountColor(&color);
  1686. ((Entity*)spawn)->SetMountSaddleColor(&color);
  1687. }
  1688. return 0;
  1689. }
  1690. int EQ2Emu_lua_SetMountColor(lua_State* state) {
  1691. if (!lua_interface)
  1692. return 0;
  1693. Spawn* spawn = lua_interface->GetSpawn(state);
  1694. EQ2_Color mount_color;
  1695. EQ2_Color saddle_color;
  1696. mount_color.red = lua_interface->GetInt8Value(state, 2);
  1697. mount_color.green = lua_interface->GetInt8Value(state, 3);
  1698. mount_color.blue = lua_interface->GetInt8Value(state, 4);
  1699. saddle_color.red = lua_interface->GetInt8Value(state, 5);
  1700. saddle_color.green = lua_interface->GetInt8Value(state, 6);
  1701. saddle_color.blue = lua_interface->GetInt8Value(state, 7);
  1702. if (spawn && spawn->IsEntity()) {
  1703. ((Entity*)spawn)->SetMountColor(&mount_color);
  1704. ((Entity*)spawn)->SetMountSaddleColor(&saddle_color);
  1705. }
  1706. return 0;
  1707. }
  1708. int EQ2Emu_lua_GetMount(lua_State* state) {
  1709. if (!lua_interface)
  1710. return 0;
  1711. Spawn* spawn = lua_interface->GetSpawn(state);
  1712. if (spawn && spawn->IsEntity()) {
  1713. lua_interface->SetInt32Value(state, ((Entity*)spawn)->GetMount());
  1714. return 1;
  1715. }
  1716. return 0;
  1717. }
  1718. int EQ2Emu_lua_GetRace(lua_State* state) {
  1719. if (!lua_interface)
  1720. return 0;
  1721. Spawn* spawn = lua_interface->GetSpawn(state);
  1722. if (spawn)
  1723. {
  1724. LogWrite(LUA__DEBUG, 0, "LUA", "%s - Race: %i", __FUNCTION__, spawn->GetRace());
  1725. lua_interface->SetInt32Value(state, spawn->GetRace());
  1726. return 1;
  1727. }
  1728. return 0;
  1729. }
  1730. int EQ2Emu_lua_GetRaceName(lua_State* state) {
  1731. if (!lua_interface)
  1732. return 0;
  1733. Spawn* spawn = lua_interface->GetSpawn(state);
  1734. if (spawn) {
  1735. lua_interface->SetStringValue(state, races.GetRaceName(spawn->GetRace()));
  1736. return 1;
  1737. }
  1738. return 0;
  1739. }
  1740. int EQ2Emu_lua_GetClass(lua_State* state) {
  1741. Spawn* spawn = lua_interface->GetSpawn(state);
  1742. if (spawn) {
  1743. lua_interface->SetInt32Value(state, spawn->GetAdventureClass());
  1744. return 1;
  1745. }
  1746. return 0;
  1747. }
  1748. int EQ2Emu_lua_GetClassName(lua_State* state) {
  1749. Spawn* spawn = lua_interface->GetSpawn(state);
  1750. if (spawn) {
  1751. lua_interface->SetStringValue(state, classes.GetClassName(spawn->GetAdventureClass()));
  1752. return 1;
  1753. }
  1754. return 0;
  1755. }
  1756. int EQ2Emu_lua_SetSpeed(lua_State* state) {
  1757. if (!lua_interface)
  1758. return 0;
  1759. Spawn* spawn = lua_interface->GetSpawn(state);
  1760. float value = lua_interface->GetFloatValue(state, 2);
  1761. lua_interface->ResetFunctionStack(state);
  1762. if (spawn) {
  1763. spawn->SetSpeed(value);
  1764. ((Entity*)spawn)->SetSpeed(value);
  1765. if (spawn->IsPlayer()) {
  1766. Client* client = spawn->GetZone()->GetClientBySpawn(spawn);
  1767. if (client) {
  1768. PacketStruct* packet = configReader.getStruct("WS_SetControlGhost", client->GetVersion());
  1769. if (packet) {
  1770. packet->setDataByName("spawn_id", 0xFFFFFFFF);
  1771. packet->setDataByName("speed", value);
  1772. packet->setDataByName("size", 0.51);
  1773. EQ2Packet* app = packet->serialize();
  1774. client->QueuePacket(app);
  1775. safe_delete(packet);
  1776. }
  1777. }
  1778. }
  1779. }
  1780. return 0;
  1781. }
  1782. int EQ2Emu_lua_AddSpellBonus(lua_State* state) {
  1783. if (!lua_interface)
  1784. return 0;
  1785. Spawn* spawn = lua_interface->GetSpawn(state);
  1786. const int16 type = lua_interface->GetInt16Value(state, 2);
  1787. const float value = lua_interface->GetFloatValue(state, 3);
  1788. LuaSpell* luaspell = lua_interface->GetCurrentSpell(state);
  1789. int64 class_req = 0;
  1790. int32 class_id = 0;
  1791. vector<int16> faction_req;
  1792. vector<int16> race_req;
  1793. int32 i = 0;
  1794. int8 f = 0;
  1795. int8 r = 0;
  1796. while ((class_id = lua_interface->GetInt32Value(state, 4 + i))) {
  1797. if (class_id < 100) {
  1798. class_req += pow(2.0, double(class_id - 1));
  1799. }
  1800. else if (class_id > 100 && class_id < 1000) {
  1801. race_req.push_back(class_id);
  1802. r++;
  1803. }
  1804. else {
  1805. faction_req.push_back(class_id);
  1806. f++;
  1807. }
  1808. i++;
  1809. }
  1810. if (value != 0 && type >= 0) {
  1811. if (luaspell && luaspell->spell && luaspell->caster) {
  1812. ZoneServer* zone = luaspell->caster->GetZone();
  1813. luaspell->MSpellTargets.readlock(__FUNCTION__, __LINE__);
  1814. for (int32 i = 0; i < luaspell->targets.size(); i++) {
  1815. Spawn* target = zone->GetSpawnByID(luaspell->targets[i]);
  1816. if (target) {
  1817. if (target->IsPlayer()) {
  1818. ((Player*)target)->AddSpellBonus(luaspell, type, value, class_req, race_req, faction_req);
  1819. LogWrite(LUA__DEBUG, 0, "LUA", "Applying Spell Bonus to Player '%s'. Is a Group Member.", ((Player*)target)->GetName());
  1820. if (((Player*)target)->GetGroupMemberInfo())
  1821. ((Player*)target)->UpdateGroupMemberInfo();
  1822. ((Player*)target)->SetCharSheetChanged(true);
  1823. }
  1824. else if (target->IsNPC())
  1825. ((NPC*)target)->AddSpellBonus(luaspell, type, value, class_req, race_req, faction_req);
  1826. else
  1827. lua_interface->LogError("%s: Error applying spell bonus on non entity.", lua_interface->GetScriptName(state));
  1828. }
  1829. }
  1830. luaspell->MSpellTargets.releasereadlock(__FUNCTION__, __LINE__);
  1831. if (!(luaspell->effect_bitmask & EFFECT_FLAG_SPELLBONUS))
  1832. luaspell->effect_bitmask += EFFECT_FLAG_SPELLBONUS;
  1833. }
  1834. else if (spawn && spawn->IsEntity()) {
  1835. ((Entity*)spawn)->AddSpellBonus(luaspell, type, value, class_req, race_req, faction_req);
  1836. LogWrite(LUA__DEBUG, 0, "LUA", "Applying Spell Bonus to Entity '%s'. Is a Group Member.", ((Entity*)spawn)->GetName());
  1837. if (spawn->IsPlayer())
  1838. ((Player*)spawn)->SetCharSheetChanged(true);
  1839. }
  1840. else
  1841. lua_interface->LogError("%s: Unable to apply spell bonus in AddSpellBonus.", lua_interface->GetScriptName(state));
  1842. }
  1843. else
  1844. lua_interface->LogError("%s: Invalid parameters for AddSpellBonus.", lua_interface->GetScriptName(state));
  1845. return 0;
  1846. }
  1847. int EQ2Emu_lua_AddSpawnSpellBonus(lua_State* state) {
  1848. if (!lua_interface)
  1849. return 0;
  1850. Spawn* spawn = lua_interface->GetSpawn(state);
  1851. int16 type = lua_interface->GetInt16Value(state, 2);
  1852. sint32 value = lua_interface->GetSInt32Value(state, 3);
  1853. LuaSpell* luaspell = lua_interface->GetCurrentSpell(state);
  1854. if (!spawn) {
  1855. lua_interface->LogError("%s: LUA AddSpawnSpellBonus command error: spawn is not valid", lua_interface->GetScriptName(state));
  1856. return 0;
  1857. }
  1858. if (!spawn->IsEntity()) {
  1859. lua_interface->LogError("%s: LUA AddSpawnSpellBonus command error: spawn is not an entity", lua_interface->GetScriptName(state));
  1860. return 0;
  1861. }
  1862. if (value == 0) {
  1863. lua_interface->LogError("%s: LUA AddSpawnSpellBonus command error: value must be set", lua_interface->GetScriptName(state));
  1864. return 0;
  1865. }
  1866. if (!luaspell || !luaspell->spell) {
  1867. lua_interface->LogError("%s: LUA AddSpawnSpellBonus command error: can only be used in a spell script", lua_interface->GetScriptName(state));
  1868. return 0;
  1869. }
  1870. int32 class_req = 0;
  1871. vector<int16> faction_req;
  1872. vector<int16> race_req;
  1873. int32 class_id = 0;
  1874. int32 i = 0;
  1875. int8 f = 0;
  1876. int8 r = 0;
  1877. while ((class_id = lua_interface->GetInt32Value(state, 4 + i))) {
  1878. if (class_id < 100) {
  1879. class_req += pow(2.0, double(class_id - 1));
  1880. }
  1881. else if (class_id > 100 && class_id < 1000) {
  1882. race_req.push_back(class_id);
  1883. r++;
  1884. }
  1885. else {
  1886. faction_req.push_back(class_id);
  1887. f++;
  1888. }
  1889. i++;
  1890. }
  1891. ((Entity*)spawn)->AddSpellBonus(luaspell, type, value, class_req, race_req, faction_req);
  1892. if (!(luaspell->effect_bitmask & EFFECT_FLAG_SPELLBONUS))
  1893. luaspell->effect_bitmask += EFFECT_FLAG_SPELLBONUS;
  1894. if (spawn->IsPlayer())
  1895. ((Player*)spawn)->SetCharSheetChanged(true);
  1896. return 0;
  1897. }
  1898. int EQ2Emu_lua_RemoveSpellBonus(lua_State* state) {
  1899. if (!lua_interface)
  1900. return 0;
  1901. Spawn* spawn = lua_interface->GetSpawn(state);
  1902. LuaSpell* luaspell = lua_interface->GetCurrentSpell(state);
  1903. if (luaspell && luaspell->spell) {
  1904. ZoneServer* zone = luaspell->caster->GetZone();
  1905. Spawn* target = 0;
  1906. luaspell->MSpellTargets.readlock(__FUNCTION__, __LINE__);
  1907. for (int32 i = 0; i < luaspell->targets.size(); i++) {
  1908. target = zone->GetSpawnByID(luaspell->targets[i]);
  1909. if (target && target->IsEntity()) {
  1910. ((Entity*)target)->RemoveSpellBonus(luaspell);
  1911. if (target->IsPlayer())
  1912. ((Player*)target)->SetCharSheetChanged(true);
  1913. }
  1914. }
  1915. luaspell->MSpellTargets.releasereadlock(__FUNCTION__, __LINE__);
  1916. }
  1917. else if (spawn && spawn->IsEntity()) {
  1918. ((Entity*)spawn)->RemoveSpellBonus(luaspell);
  1919. if (spawn->IsPlayer())
  1920. ((Player*)spawn)->SetCharSheetChanged(true);
  1921. }
  1922. return 0;
  1923. }
  1924. int EQ2Emu_lua_AddSkillBonus(lua_State* state) {
  1925. if (!lua_interface)
  1926. return 0;
  1927. Spawn* spawn = lua_interface->GetSpawn(state);
  1928. int32 skill_id = lua_interface->GetInt32Value(state, 2);
  1929. float value = lua_interface->GetFloatValue(state, 3);
  1930. LuaSpell* luaspell = lua_interface->GetCurrentSpell(state);
  1931. if (value != 0) {
  1932. int32 spell_id = 0;
  1933. if (luaspell && luaspell->spell && luaspell->caster) {
  1934. spell_id = luaspell->spell->GetSpellID();
  1935. ZoneServer* zone = luaspell->caster->GetZone();
  1936. Spawn* target = 0;
  1937. luaspell->MSpellTargets.readlock(__FUNCTION__, __LINE__);
  1938. for (int32 i = 0; i < luaspell->targets.size(); i++) {
  1939. target = zone->GetSpawnByID(luaspell->targets[i]);
  1940. if (target && target->Alive()) {
  1941. if (target->IsPlayer()) {
  1942. ((Player*)target)->AddSkillBonus(spell_id, skill_id, value);
  1943. Client* client = target->GetZone()->GetClientBySpawn(target);
  1944. if (client) {
  1945. EQ2Packet* packet = ((Player*)target)->GetSkills()->GetSkillPacket(client->GetVersion());
  1946. if (packet)
  1947. client->QueuePacket(packet);
  1948. }
  1949. if (!(luaspell->effect_bitmask & EFFECT_FLAG_SKILLBONUS))
  1950. luaspell->effect_bitmask += EFFECT_FLAG_SKILLBONUS;
  1951. }
  1952. else if (target->IsNPC()) {
  1953. ((NPC*)target)->AddSkillBonus(spell_id, skill_id, value);
  1954. if (!(luaspell->effect_bitmask & EFFECT_FLAG_SKILLBONUS))
  1955. luaspell->effect_bitmask += EFFECT_FLAG_SKILLBONUS;
  1956. }
  1957. else
  1958. LogWrite(LUA__ERROR, 0, "LUA", "Error applying bonus buff on '%s'. Not a NPC or player.", target->GetName());
  1959. }
  1960. }
  1961. luaspell->MSpellTargets.releasereadlock(__FUNCTION__, __LINE__);
  1962. }
  1963. else if (spawn) {
  1964. if (spawn->IsPlayer()) {
  1965. ((Player*)spawn)->AddSkillBonus(spell_id, skill_id, value);
  1966. Client* client = spawn->GetZone()->GetClientBySpawn(spawn);
  1967. if (client) {
  1968. EQ2Packet* packet = ((Player*)spawn)->GetSkills()->GetSkillPacket(client->GetVersion());
  1969. if (packet)
  1970. client->QueuePacket(packet);
  1971. }
  1972. }
  1973. else if (spawn->IsNPC())
  1974. ((NPC*)spawn)->AddSkillBonus(spell_id, skill_id, value);
  1975. else
  1976. LogWrite(LUA__ERROR, 0, "LUA", "Error applying skill bonus on '%s'. Not a NPC or player.", spawn->GetName());
  1977. }
  1978. }
  1979. else
  1980. lua_interface->LogError("%s: Invalid parameters for AddSkillBonus.", lua_interface->GetScriptName(state));
  1981. return 0;
  1982. }
  1983. int EQ2Emu_lua_RemoveSkillBonus(lua_State* state) {
  1984. if (!lua_interface)
  1985. return 0;
  1986. Spawn* spawn = lua_interface->GetSpawn(state);
  1987. LuaSpell* luaspell = lua_interface->GetCurrentSpell(state);
  1988. if (spawn && spawn->IsPlayer()) {
  1989. int32 spell_id = 0;
  1990. if (luaspell && luaspell->spell) {
  1991. spell_id = luaspell->spell->GetSpellID();
  1992. ZoneServer* zone = luaspell->caster->GetZone();
  1993. Spawn* target = 0;
  1994. luaspell->MSpellTargets.readlock(__FUNCTION__, __LINE__);
  1995. for (int32 i = 0; i < luaspell->targets.size(); i++) {
  1996. target = zone->GetSpawnByID(luaspell->targets[i]);
  1997. if (target) {
  1998. if (target->IsPlayer()) {
  1999. ((Player*)target)->RemoveSkillBonus(spell_id);
  2000. Client* client = target->GetZone()->GetClientBySpawn(target);
  2001. if (client) {
  2002. EQ2Packet* packet = ((Player*)target)->GetSkills()->GetSkillPacket(client->GetVersion());
  2003. if (packet)
  2004. client->QueuePacket(packet);
  2005. }
  2006. }
  2007. else if (target->IsNPC())
  2008. ((NPC*)target)->RemoveSkillBonus(spell_id);
  2009. else
  2010. LogWrite(LUA__ERROR, 0, "LUA", "Error removing skill bonus on '%s'. Not a NPC or player.", spawn->GetName());
  2011. }
  2012. }
  2013. luaspell->MSpellTargets.releasereadlock(__FUNCTION__, __LINE__);
  2014. }
  2015. else if (spawn) {
  2016. if (spawn->IsPlayer()) {
  2017. ((Player*)spawn)->RemoveSkillBonus(spell_id);
  2018. Client* client = spawn->GetZone()->GetClientBySpawn(spawn);
  2019. if (client) {
  2020. EQ2Packet* packet = ((Player*)spawn)->GetSkills()->GetSkillPacket(client->GetVersion());
  2021. if (packet)
  2022. client->QueuePacket(packet);
  2023. }
  2024. }
  2025. else if (spawn->IsNPC())
  2026. ((NPC*)spawn)->RemoveSkillBonus(spell_id);
  2027. else
  2028. LogWrite(LUA__ERROR, 0, "LUA", "Error removing skill bonus on '%s'. Not a NPC or player.", spawn->GetName());
  2029. }
  2030. }
  2031. return 0;
  2032. }
  2033. int EQ2Emu_lua_AddControlEffect(lua_State* state) {
  2034. if (!lua_interface)
  2035. return 0;
  2036. Spawn* spawn = lua_interface->GetSpawn(state);
  2037. int8 type = lua_interface->GetInt32Value(state, 2);
  2038. bool only_add_spawn = lua_interface->GetInt8Value(state, 3) == 1;
  2039. LuaSpell* luaspell = lua_interface->GetCurrentSpell(state);
  2040. if (!only_add_spawn && luaspell && luaspell->spell && luaspell->caster && type != 0) {
  2041. ZoneServer* zone = luaspell->caster->GetZone();
  2042. Spawn* target = 0;
  2043. luaspell->MSpellTargets.readlock(__FUNCTION__, __LINE__);
  2044. for (int32 i = 0; i < luaspell->targets.size(); i++) {
  2045. target = zone->GetSpawnByID(luaspell->targets[i]);
  2046. if (target && target->IsEntity()) {
  2047. if (type == CONTROL_EFFECT_TYPE_MEZ) {
  2048. ((Entity*)target)->AddMezSpell(luaspell);
  2049. if (!(luaspell->effect_bitmask & EFFECT_FLAG_MEZ))
  2050. luaspell->effect_bitmask += EFFECT_FLAG_MEZ;
  2051. if (target->IsNPC())
  2052. ((NPC*)target)->Brain()->AddHate(luaspell->caster, 5);
  2053. }
  2054. else if (type == CONTROL_EFFECT_TYPE_STIFLE) {
  2055. ((Entity*)target)->AddStifleSpell(luaspell);
  2056. if (!(luaspell->effect_bitmask & EFFECT_FLAG_STIFLE))
  2057. luaspell->effect_bitmask += EFFECT_FLAG_STIFLE;
  2058. if (target->IsNPC())
  2059. ((NPC*)target)->Brain()->AddHate(luaspell->caster, 5);
  2060. }
  2061. else if (type == CONTROL_EFFECT_TYPE_DAZE) {
  2062. ((Entity*)target)->AddDazeSpell(luaspell);
  2063. if (!(luaspell->effect_bitmask & EFFECT_FLAG_DAZE))
  2064. luaspell->effect_bitmask += EFFECT_FLAG_DAZE;
  2065. if (target->IsNPC())
  2066. ((NPC*)target)->Brain()->AddHate(luaspell->caster, 5);
  2067. }
  2068. else if (type == CONTROL_EFFECT_TYPE_STUN) {
  2069. if (!(luaspell->effect_bitmask & EFFECT_FLAG_STUN))
  2070. luaspell->effect_bitmask += EFFECT_FLAG_STUN;
  2071. ((Entity*)target)->AddStunSpell(luaspell);
  2072. if (target->IsNPC())
  2073. ((NPC*)target)->Brain()->AddHate(luaspell->caster, 5);
  2074. }
  2075. else if (type == CONTROL_EFFECT_TYPE_ROOT) {
  2076. if (!(luaspell->effect_bitmask & EFFECT_FLAG_ROOT))
  2077. luaspell->effect_bitmask += EFFECT_FLAG_ROOT;
  2078. ((Entity*)target)->AddRootSpell(luaspell);
  2079. if (target->IsNPC())
  2080. ((NPC*)target)->Brain()->AddHate(luaspell->caster, 5);
  2081. }
  2082. else if (type == CONTROL_EFFECT_TYPE_FEAR) {
  2083. if (!(luaspell->effect_bitmask & EFFECT_FLAG_FEAR))
  2084. luaspell->effect_bitmask += EFFECT_FLAG_FEAR;
  2085. ((Entity*)target)->AddFearSpell(luaspell);
  2086. if (target->IsNPC())
  2087. ((NPC*)target)->Brain()->AddHate(luaspell->caster, 5);
  2088. }
  2089. else if (type == CONTROL_EFFECT_TYPE_WALKUNDERWATER) {
  2090. ((Entity*)target)->AddWaterwalkSpell(luaspell);
  2091. if (!(luaspell->effect_bitmask & EFFECT_FLAG_WATERWALK))
  2092. luaspell->effect_bitmask += EFFECT_FLAG_WATERWALK;
  2093. }
  2094. else if (type == CONTROL_EFFECT_TYPE_JUMPUNDERWATER) {
  2095. ((Entity*)target)->AddWaterjumpSpell(luaspell);
  2096. if (!(luaspell->effect_bitmask & EFFECT_FLAG_WATERJUMP))
  2097. luaspell->effect_bitmask += EFFECT_FLAG_WATERJUMP;
  2098. }
  2099. else if (type == CONTROL_EFFECT_TYPE_SNARE) {
  2100. ((Entity*)target)->AddSnareSpell(luaspell);
  2101. if (!(luaspell->effect_bitmask & EFFECT_FLAG_SNARE))
  2102. luaspell->effect_bitmask += EFFECT_FLAG_SNARE;
  2103. if (target->IsNPC())
  2104. ((NPC*)target)->Brain()->AddHate(luaspell->caster, 5);
  2105. }
  2106. else if (type == CONTROL_EFFECT_TYPE_FLIGHT) {
  2107. ((Entity*)target)->AddFlightSpell(luaspell);
  2108. if (!(luaspell->effect_bitmask & EFFECT_FLAG_FLIGHT))
  2109. luaspell->effect_bitmask += EFFECT_FLAG_FLIGHT;
  2110. }
  2111. else if (type == CONTROL_EFFECT_TYPE_GLIDE) {
  2112. ((Entity*)target)->AddGlideSpell(luaspell);
  2113. if (!(luaspell->effect_bitmask & EFFECT_FLAG_GLIDE))
  2114. luaspell->effect_bitmask += EFFECT_FLAG_GLIDE;
  2115. }
  2116. else if (type == CONTROL_EFFECT_TYPE_SAFEFALL) {
  2117. ((Entity*)target)->AddSafefallSpell(luaspell);
  2118. if (!(luaspell->effect_bitmask & EFFECT_FLAG_SAFEFALL))
  2119. luaspell->effect_bitmask += EFFECT_FLAG_SAFEFALL;
  2120. }
  2121. else
  2122. lua_interface->LogError("%s: Unhandled control effect type of %u.", lua_interface->GetScriptName(state), type);
  2123. }
  2124. else
  2125. lua_interface->LogError("%s: Error applying control effect on non entity '%s'.", lua_interface->GetScriptName(state), target->GetName());
  2126. }
  2127. luaspell->MSpellTargets.releasereadlock(__FUNCTION__, __LINE__);
  2128. }
  2129. else if (only_add_spawn && spawn && spawn->IsEntity()) {
  2130. if (type == CONTROL_EFFECT_TYPE_MEZ) {
  2131. ((Entity*)spawn)->AddMezSpell(luaspell);
  2132. if (!(luaspell->effect_bitmask & EFFECT_FLAG_MEZ))
  2133. luaspell->effect_bitmask += EFFECT_FLAG_MEZ;
  2134. }
  2135. else if (type == CONTROL_EFFECT_TYPE_STIFLE) {
  2136. ((Entity*)spawn)->AddStifleSpell(luaspell);
  2137. if (!(luaspell->effect_bitmask & EFFECT_FLAG_STIFLE))
  2138. luaspell->effect_bitmask += EFFECT_FLAG_STIFLE;
  2139. }
  2140. else if (type == CONTROL_EFFECT_TYPE_DAZE) {
  2141. ((Entity*)spawn)->AddDazeSpell(luaspell);
  2142. if (!(luaspell->effect_bitmask & EFFECT_FLAG_DAZE))
  2143. luaspell->effect_bitmask += EFFECT_FLAG_DAZE;
  2144. }
  2145. else if (type == CONTROL_EFFECT_TYPE_STUN) {
  2146. ((Entity*)spawn)->AddStunSpell(luaspell);
  2147. if (!(luaspell->effect_bitmask & EFFECT_FLAG_STUN))
  2148. luaspell->effect_bitmask += EFFECT_FLAG_STUN;
  2149. }
  2150. else if (type == CONTROL_EFFECT_TYPE_ROOT) {
  2151. ((Entity*)spawn)->AddRootSpell(luaspell);
  2152. if (!(luaspell->effect_bitmask & EFFECT_FLAG_ROOT))
  2153. luaspell->effect_bitmask += EFFECT_FLAG_ROOT;
  2154. }
  2155. else if (type == CONTROL_EFFECT_TYPE_FEAR) {
  2156. ((Entity*)spawn)->AddFearSpell(luaspell);
  2157. if (!(luaspell->effect_bitmask & EFFECT_FLAG_FEAR))
  2158. luaspell->effect_bitmask += EFFECT_FLAG_FEAR;
  2159. }
  2160. else if (type == CONTROL_EFFECT_TYPE_WALKUNDERWATER) {
  2161. ((Entity*)spawn)->AddWaterwalkSpell(luaspell);
  2162. if (!(luaspell->effect_bitmask & EFFECT_FLAG_WATERWALK))
  2163. luaspell->effect_bitmask += EFFECT_FLAG_WATERWALK;
  2164. }
  2165. else if (type == CONTROL_EFFECT_TYPE_JUMPUNDERWATER) {
  2166. ((Entity*)spawn)->AddWaterjumpSpell(luaspell);
  2167. if (!(luaspell->effect_bitmask & EFFECT_FLAG_WATERJUMP))
  2168. luaspell->effect_bitmask += EFFECT_FLAG_WATERJUMP;
  2169. }
  2170. else if (type == CONTROL_EFFECT_TYPE_SNARE) {
  2171. ((Entity*)spawn)->AddSnareSpell(luaspell);
  2172. if (!(luaspell->effect_bitmask & EFFECT_FLAG_SNARE))
  2173. luaspell->effect_bitmask += EFFECT_FLAG_SNARE;
  2174. }
  2175. else if (type == CONTROL_EFFECT_TYPE_FLIGHT) {
  2176. ((Entity*)spawn)->AddFlightSpell(luaspell);
  2177. if (!(luaspell->effect_bitmask & EFFECT_FLAG_FLIGHT))
  2178. luaspell->effect_bitmask += EFFECT_FLAG_FLIGHT;
  2179. }
  2180. else if (type == CONTROL_EFFECT_TYPE_GLIDE) {
  2181. ((Entity*)spawn)->AddGlideSpell(luaspell);
  2182. if (!(luaspell->effect_bitmask & EFFECT_FLAG_GLIDE))
  2183. luaspell->effect_bitmask += EFFECT_FLAG_GLIDE;
  2184. }
  2185. else if (type == CONTROL_EFFECT_TYPE_SAFEFALL) {
  2186. ((Entity*)spawn)->AddSafefallSpell(luaspell);
  2187. if (!(luaspell->effect_bitmask & EFFECT_FLAG_SAFEFALL))
  2188. luaspell->effect_bitmask += EFFECT_FLAG_SAFEFALL;
  2189. }
  2190. else
  2191. lua_interface->LogError("%s: Unhandled control effect type of %u.", lua_interface->GetScriptName(state), type);
  2192. }
  2193. else
  2194. lua_interface->LogError("%s: Error applying control effect on non entity '%s'.", lua_interface->GetScriptName(state), spawn->GetName());
  2195. return 0;
  2196. }
  2197. int EQ2Emu_lua_RemoveControlEffect(lua_State* state) {
  2198. if (!lua_interface)
  2199. return 0;
  2200. Spawn* spawn = lua_interface->GetSpawn(state);
  2201. int8 type = lua_interface->GetInt8Value(state, 2);
  2202. bool only_remove_spawn = lua_interface->GetInt8Value(state, 3) == 1;
  2203. LuaSpell* luaspell = lua_interface->GetCurrentSpell(state);
  2204. if (spawn && spawn->IsEntity()) {
  2205. if (!only_remove_spawn && luaspell && luaspell->spell) {
  2206. ZoneServer* zone = luaspell->caster->GetZone();
  2207. Spawn* target = 0;
  2208. luaspell->MSpellTargets.readlock(__FUNCTION__, __LINE__);
  2209. for (int32 i = 0; i < luaspell->targets.size(); i++) {
  2210. target = zone->GetSpawnByID(luaspell->targets[i]);
  2211. if (target) {
  2212. if (type == CONTROL_EFFECT_TYPE_MEZ)
  2213. ((Entity*)target)->RemoveMezSpell(luaspell);
  2214. else if (type == CONTROL_EFFECT_TYPE_STIFLE)
  2215. ((Entity*)target)->RemoveStifleSpell(luaspell);
  2216. else if (type == CONTROL_EFFECT_TYPE_DAZE)
  2217. ((Entity*)target)->RemoveDazeSpell(luaspell);
  2218. else if (type == CONTROL_EFFECT_TYPE_STUN)
  2219. ((Entity*)target)->RemoveStunSpell(luaspell);
  2220. else if (type == CONTROL_EFFECT_TYPE_ROOT)
  2221. ((Entity*)target)->RemoveRootSpell(luaspell);
  2222. else if (type == CONTROL_EFFECT_TYPE_FEAR)
  2223. ((Entity*)target)->RemoveFearSpell(luaspell);
  2224. else if (type == CONTROL_EFFECT_TYPE_WALKUNDERWATER)
  2225. ((Entity*)target)->RemoveWaterwalkSpell(luaspell);
  2226. else if (type == CONTROL_EFFECT_TYPE_JUMPUNDERWATER)
  2227. ((Entity*)target)->RemoveWaterjumpSpell(luaspell);
  2228. else if (type == CONTROL_EFFECT_TYPE_SNARE)
  2229. ((Entity*)target)->RemoveSnareSpell(luaspell);
  2230. else if (type == CONTROL_EFFECT_TYPE_FLIGHT)
  2231. ((Entity*)target)->RemoveFlightSpell(luaspell);
  2232. else if (type == CONTROL_EFFECT_TYPE_GLIDE)
  2233. ((Entity*)target)->RemoveGlideSpell(luaspell);
  2234. else if (type == CONTROL_EFFECT_TYPE_SAFEFALL)
  2235. ((Entity*)target)->RemoveGlideSpell(luaspell);
  2236. else
  2237. lua_interface->LogError("%s: Unhandled control effect type of %u.", lua_interface->GetScriptName(state), type);
  2238. }
  2239. }
  2240. luaspell->MSpellTargets.releasereadlock(__FUNCTION__, __LINE__);
  2241. }
  2242. else if (only_remove_spawn) {
  2243. if (type == CONTROL_EFFECT_TYPE_MEZ)
  2244. ((Entity*)spawn)->RemoveMezSpell(luaspell);
  2245. else if (type == CONTROL_EFFECT_TYPE_STIFLE)
  2246. ((Entity*)spawn)->RemoveStifleSpell(luaspell);
  2247. else if (type == CONTROL_EFFECT_TYPE_DAZE)
  2248. ((Entity*)spawn)->RemoveDazeSpell(luaspell);
  2249. else if (type == CONTROL_EFFECT_TYPE_STUN)
  2250. ((Entity*)spawn)->RemoveStunSpell(luaspell);
  2251. else if (type == CONTROL_EFFECT_TYPE_ROOT)
  2252. ((Entity*)spawn)->RemoveRootSpell(luaspell);
  2253. else if (type == CONTROL_EFFECT_TYPE_FEAR)
  2254. ((Entity*)spawn)->RemoveFearSpell(luaspell);
  2255. else if (type == CONTROL_EFFECT_TYPE_WALKUNDERWATER)
  2256. ((Entity*)spawn)->RemoveWaterwalkSpell(luaspell);
  2257. else if (type == CONTROL_EFFECT_TYPE_JUMPUNDERWATER)
  2258. ((Entity*)spawn)->RemoveWaterjumpSpell(luaspell);
  2259. else if (type == CONTROL_EFFECT_TYPE_SNARE)
  2260. ((Entity*)spawn)->RemoveSnareSpell(luaspell);
  2261. else if (type == CONTROL_EFFECT_TYPE_FLIGHT)
  2262. ((Entity*)spawn)->RemoveFlightSpell(luaspell);
  2263. else if (type == CONTROL_EFFECT_TYPE_GLIDE)
  2264. ((Entity*)spawn)->RemoveGlideSpell(luaspell);
  2265. else if (type == CONTROL_EFFECT_TYPE_SAFEFALL)
  2266. ((Entity*)spawn)->RemoveSafefallSpell(luaspell);
  2267. else
  2268. lua_interface->LogError("%s: Unhandled control effect type of %u.", lua_interface->GetScriptName(state), type);
  2269. }
  2270. }
  2271. return 0;
  2272. }
  2273. int EQ2Emu_lua_SetIntBase(lua_State* state) {
  2274. if (!lua_interface)
  2275. return 0;
  2276. Spawn* spawn = lua_interface->GetSpawn(state);
  2277. int16 value = lua_interface->GetInt16Value(state, 2);
  2278. if (spawn && spawn->IsEntity()) {
  2279. ((Entity*)spawn)->GetInfoStruct()->intel_base = value;
  2280. if (spawn->IsPlayer())
  2281. ((Player*)spawn)->SetCharSheetChanged(true);
  2282. }
  2283. return 0;
  2284. }
  2285. int EQ2Emu_lua_SetAgiBase(lua_State* state) {
  2286. if (!lua_interface)
  2287. return 0;
  2288. Spawn* spawn = lua_interface->GetSpawn(state);
  2289. int16 value = lua_interface->GetInt16Value(state, 2);
  2290. if (spawn && spawn->IsEntity()) {
  2291. ((Entity*)spawn)->GetInfoStruct()->agi_base = value;
  2292. if (spawn->IsPlayer())
  2293. ((Player*)spawn)->SetCharSheetChanged(true);
  2294. }
  2295. return 0;
  2296. }
  2297. int EQ2Emu_lua_SetWisBase(lua_State* state) {
  2298. if (!lua_interface)
  2299. return 0;
  2300. Spawn* spawn = lua_interface->GetSpawn(state);
  2301. int16 value = lua_interface->GetInt16Value(state, 2);
  2302. if (spawn && spawn->IsEntity()) {
  2303. ((Entity*)spawn)->GetInfoStruct()->wis_base = value;
  2304. if (spawn->IsPlayer())
  2305. ((Player*)spawn)->SetCharSheetChanged(true);
  2306. }
  2307. return 0;
  2308. }
  2309. int EQ2Emu_lua_SetStaBase(lua_State* state) {
  2310. if (!lua_interface)
  2311. return 0;
  2312. Spawn* spawn = lua_interface->GetSpawn(state);
  2313. int16 value = lua_interface->GetInt16Value(state, 2);
  2314. if (spawn && spawn->IsEntity()) {
  2315. ((Entity*)spawn)->GetInfoStruct()->sta_base = value;
  2316. if (spawn->IsPlayer())
  2317. ((Player*)spawn)->SetCharSheetChanged(true);
  2318. }
  2319. return 0;
  2320. }
  2321. int EQ2Emu_lua_SetStrBase(lua_State* state) {
  2322. if (!lua_interface)
  2323. return 0;
  2324. Spawn* spawn = lua_interface->GetSpawn(state);
  2325. int16 value = lua_interface->GetInt16Value(state, 2);
  2326. if (spawn && spawn->IsEntity()) {
  2327. ((Entity*)spawn)->GetInfoStruct()->str_base = value;
  2328. if (spawn->IsPlayer())
  2329. ((Player*)spawn)->SetCharSheetChanged(true);
  2330. }
  2331. return 0;
  2332. }
  2333. int EQ2Emu_lua_SetInt(lua_State* state) {
  2334. if (!lua_interface)
  2335. return 0;
  2336. Spawn* spawn = lua_interface->GetSpawn(state);
  2337. sint32 value = lua_interface->GetSInt32Value(state, 2);
  2338. if (spawn && spawn->IsEntity()) {
  2339. ((Entity*)spawn)->AddSpellBonus(0, ITEM_STAT_INT, value);
  2340. if (spawn->IsPlayer())
  2341. ((Player*)spawn)->SetCharSheetChanged(true);
  2342. }
  2343. return 0;
  2344. }
  2345. int EQ2Emu_lua_SetWis(lua_State* state) {
  2346. if (!lua_interface)
  2347. return 0;
  2348. Spawn* spawn = lua_interface->GetSpawn(state);
  2349. float value = lua_interface->GetFloatValue(state, 2);
  2350. if (spawn && spawn->IsEntity()) {
  2351. ((Entity*)spawn)->AddSpellBonus(0, ITEM_STAT_WIS, value);
  2352. if (spawn->IsPlayer())
  2353. ((Player*)spawn)->SetCharSheetChanged(true);
  2354. }
  2355. return 0;
  2356. }
  2357. int EQ2Emu_lua_SetSta(lua_State* state) {
  2358. if (!lua_interface)
  2359. return 0;
  2360. Spawn* spawn = lua_interface->GetSpawn(state);
  2361. float value = lua_interface->GetFloatValue(state, 2);
  2362. if (spawn && spawn->IsEntity()) {
  2363. ((Entity*)spawn)->AddSpellBonus(0, ITEM_STAT_STA, value);
  2364. if (spawn->IsPlayer())
  2365. ((Player*)spawn)->SetCharSheetChanged(true);
  2366. }
  2367. return 0;
  2368. }
  2369. int EQ2Emu_lua_SetStr(lua_State* state) {
  2370. if (!lua_interface)
  2371. return 0;
  2372. Spawn* spawn = lua_interface->GetSpawn(state);
  2373. float value = lua_interface->GetFloatValue(state, 2);
  2374. if (spawn && spawn->IsEntity()) {
  2375. ((Entity*)spawn)->AddSpellBonus(0, ITEM_STAT_STR, value);
  2376. if (spawn->IsPlayer())
  2377. ((Player*)spawn)->SetCharSheetChanged(true);
  2378. }
  2379. return 0;
  2380. }
  2381. int EQ2Emu_lua_SetAgi(lua_State* state) {
  2382. if (!lua_interface)
  2383. return 0;
  2384. Spawn* spawn = lua_interface->GetSpawn(state);
  2385. float value = lua_interface->GetFloatValue(state, 2);
  2386. if (spawn && spawn->IsEntity()) {
  2387. ((Entity*)spawn)->AddSpellBonus(0, ITEM_STAT_AGI, value);
  2388. if (spawn->IsPlayer())
  2389. ((Player*)spawn)->SetCharSheetChanged(true);
  2390. }
  2391. return 0;
  2392. }
  2393. int EQ2Emu_lua_GetCurrentHP(lua_State* state) {
  2394. if (!lua_interface)
  2395. return 0;
  2396. Spawn* spawn = lua_interface->GetSpawn(state);
  2397. if (spawn) {
  2398. lua_interface->SetInt32Value(state, spawn->GetHP());
  2399. return 1;
  2400. }
  2401. return 0;
  2402. }
  2403. int EQ2Emu_lua_GetMaxHP(lua_State* state) {
  2404. if (!lua_interface)
  2405. return 0;
  2406. Spawn* spawn = lua_interface->GetSpawn(state);
  2407. if (spawn) {
  2408. lua_interface->SetInt32Value(state, spawn->GetTotalHP());
  2409. return 1;
  2410. }
  2411. return 0;
  2412. }
  2413. int EQ2Emu_lua_GetMaxHPBase(lua_State* state) {
  2414. if (!lua_interface)
  2415. return 0;
  2416. Spawn* spawn = lua_interface->GetSpawn(state);
  2417. if (spawn) {
  2418. lua_interface->SetInt32Value(state, spawn->GetTotalHPBase());
  2419. return 1;
  2420. }
  2421. return 0;
  2422. }
  2423. int EQ2Emu_lua_GetName(lua_State* state) {
  2424. if (!lua_interface)
  2425. return 0;
  2426. Spawn* spawn = lua_interface->GetSpawn(state);
  2427. if (spawn) {
  2428. lua_interface->SetStringValue(state, spawn->GetName());
  2429. return 1;
  2430. }
  2431. return 0;
  2432. }
  2433. int EQ2Emu_lua_GetLevel(lua_State* state) {
  2434. Spawn* spawn = lua_interface->GetSpawn(state);
  2435. if (spawn) {
  2436. lua_interface->SetInt32Value(state, spawn->GetLevel());
  2437. return 1;
  2438. }
  2439. return 0;
  2440. }
  2441. int EQ2Emu_lua_GetCurrentPower(lua_State* state) {
  2442. if (!lua_interface)
  2443. return 0;
  2444. Spawn* spawn = lua_interface->GetSpawn(state);
  2445. if (spawn) {
  2446. lua_interface->SetInt32Value(state, spawn->GetPower());
  2447. return 1;
  2448. }
  2449. return 0;
  2450. }
  2451. int EQ2Emu_lua_GetMaxPower(lua_State* state) {
  2452. if (!lua_interface)
  2453. return 0;
  2454. Spawn* spawn = lua_interface->GetSpawn(state);
  2455. if (spawn) {
  2456. lua_interface->SetInt32Value(state, spawn->GetTotalPower());
  2457. return 1;
  2458. }
  2459. return 0;
  2460. }
  2461. int EQ2Emu_lua_GetMaxPowerBase(lua_State* state) {
  2462. if (!lua_interface)
  2463. return 0;
  2464. Spawn* spawn = lua_interface->GetSpawn(state);
  2465. if (spawn) {
  2466. lua_interface->SetInt32Value(state, spawn->GetTotalPowerBase());
  2467. return 1;
  2468. }
  2469. return 0;
  2470. }
  2471. int EQ2Emu_lua_GetDistance(lua_State* state) {
  2472. if (!lua_interface)
  2473. return 0;
  2474. Spawn* spawn = lua_interface->GetSpawn(state);
  2475. Spawn* spawn2 = lua_interface->GetSpawn(state, 2);
  2476. bool include_radius = lua_interface->GetInt8Value(state, 3) == 1;
  2477. if (spawn && spawn2) {
  2478. float distance = spawn->GetDistance(spawn2, false, include_radius);
  2479. lua_interface->SetFloatValue(state, distance);
  2480. return 1;
  2481. }
  2482. return 0;
  2483. }
  2484. int EQ2Emu_lua_GetX(lua_State* state) {
  2485. if (!lua_interface)
  2486. return 0;
  2487. Spawn* spawn = lua_interface->GetSpawn(state);
  2488. if (spawn) {
  2489. lua_interface->SetFloatValue(state, spawn->GetX());
  2490. return 1;
  2491. }
  2492. return 0;
  2493. }
  2494. int EQ2Emu_lua_GetY(lua_State* state) {
  2495. if (!lua_interface)
  2496. return 0;
  2497. Spawn* spawn = lua_interface->GetSpawn(state);
  2498. if (spawn) {
  2499. lua_interface->SetFloatValue(state, spawn->GetY());
  2500. return 1;
  2501. }
  2502. return 0;
  2503. }
  2504. int EQ2Emu_lua_GetZ(lua_State* state) {
  2505. if (!lua_interface)
  2506. return 0;
  2507. Spawn* spawn = lua_interface->GetSpawn(state);
  2508. if (spawn) {
  2509. lua_interface->SetFloatValue(state, spawn->GetZ());
  2510. return 1;
  2511. }
  2512. return 0;
  2513. }
  2514. int EQ2Emu_lua_GetHeading(lua_State* state) {
  2515. if (!lua_interface)
  2516. return 0;
  2517. Spawn* spawn = lua_interface->GetSpawn(state);
  2518. if (spawn) {
  2519. lua_interface->SetFloatValue(state, spawn->GetHeading());
  2520. return 1;
  2521. }
  2522. return 0;
  2523. }
  2524. int EQ2Emu_lua_GetModelType(lua_State* state) {
  2525. if (!lua_interface)
  2526. return 0;
  2527. Spawn* spawn = lua_interface->GetSpawn(state);
  2528. if (spawn) {
  2529. lua_interface->SetInt32Value(state, spawn->GetModelType());
  2530. return 1;
  2531. }
  2532. return 0;
  2533. }
  2534. int EQ2Emu_lua_GetSpeed(lua_State* state) {
  2535. if (!lua_interface)
  2536. return 0;
  2537. Spawn* spawn = lua_interface->GetSpawn(state);
  2538. if (spawn) {
  2539. lua_interface->SetFloatValue(state, spawn->GetSpeed());
  2540. return 1;
  2541. }
  2542. return 0;
  2543. }
  2544. int EQ2Emu_lua_HasMoved(lua_State* state) {
  2545. if (!lua_interface)
  2546. return 0;
  2547. Spawn* spawn = lua_interface->GetSpawn(state);
  2548. if (spawn && spawn->IsEntity()) {
  2549. lua_interface->SetBooleanValue(state, ((Entity*)spawn)->HasMoved(false));
  2550. return 1;
  2551. }
  2552. return 0;
  2553. }
  2554. int EQ2Emu_lua_GetInt(lua_State* state) {
  2555. if (!lua_interface)
  2556. return 0;
  2557. Spawn* spawn = lua_interface->GetSpawn(state);
  2558. if (spawn && spawn->IsEntity()) {
  2559. lua_interface->SetInt32Value(state, ((Entity*)spawn)->GetInt());
  2560. return 1;
  2561. }
  2562. return 0;
  2563. }
  2564. int EQ2Emu_lua_GetWis(lua_State* state) {
  2565. if (!lua_interface)
  2566. return 0;
  2567. Spawn* spawn = lua_interface->GetSpawn(state);
  2568. if (spawn && spawn->IsEntity()) {
  2569. lua_interface->SetInt32Value(state, ((Entity*)spawn)->GetWis());
  2570. return 1;
  2571. }
  2572. return 0;
  2573. }
  2574. int EQ2Emu_lua_GetSta(lua_State* state) {
  2575. if (!lua_interface)
  2576. return 0;
  2577. Spawn* spawn = lua_interface->GetSpawn(state);
  2578. if (spawn && spawn->IsEntity()) {
  2579. lua_interface->SetInt32Value(state, ((Entity*)spawn)->GetSta());
  2580. return 1;
  2581. }
  2582. return 0;
  2583. }
  2584. int EQ2Emu_lua_GetStr(lua_State* state) {
  2585. if (!lua_interface)
  2586. return 0;
  2587. Spawn* spawn = lua_interface->GetSpawn(state);
  2588. if (spawn && spawn->IsEntity()) {
  2589. lua_interface->SetInt32Value(state, ((Entity*)spawn)->GetStr());
  2590. return 1;
  2591. }
  2592. return 0;
  2593. }
  2594. int EQ2Emu_lua_GetAgi(lua_State* state) {
  2595. if (!lua_interface)
  2596. return 0;
  2597. Spawn* spawn = lua_interface->GetSpawn(state);
  2598. if (spawn && spawn->IsEntity()) {
  2599. lua_interface->SetInt32Value(state, ((Entity*)spawn)->GetAgi());
  2600. return 1;
  2601. }
  2602. return 0;
  2603. }
  2604. int EQ2Emu_lua_GetIntBase(lua_State* state) {
  2605. if (!lua_interface)
  2606. return 0;
  2607. Spawn* spawn = lua_interface->GetSpawn(state);
  2608. if (spawn && spawn->IsEntity()) {
  2609. lua_interface->SetInt32Value(state, ((Entity*)spawn)->GetIntBase());
  2610. return 1;
  2611. }
  2612. return 0;
  2613. }
  2614. int EQ2Emu_lua_GetWisBase(lua_State* state) {
  2615. if (!lua_interface)
  2616. return 0;
  2617. Spawn* spawn = lua_interface->GetSpawn(state);
  2618. if (spawn && spawn->IsEntity()) {
  2619. lua_interface->SetInt32Value(state, ((Entity*)spawn)->GetWisBase());
  2620. return 1;
  2621. }
  2622. return 0;
  2623. }
  2624. int EQ2Emu_lua_GetStaBase(lua_State* state) {
  2625. if (!lua_interface)
  2626. return 0;
  2627. Spawn* spawn = lua_interface->GetSpawn(state);
  2628. if (spawn && spawn->IsEntity()) {
  2629. lua_interface->SetInt32Value(state, ((Entity*)spawn)->GetStaBase());
  2630. return 1;
  2631. }
  2632. return 0;
  2633. }
  2634. int EQ2Emu_lua_GetStrBase(lua_State* state) {
  2635. if (!lua_interface)
  2636. return 0;
  2637. Spawn* spawn = lua_interface->GetSpawn(state);
  2638. if (spawn && spawn->IsEntity()) {
  2639. lua_interface->SetInt32Value(state, ((Entity*)spawn)->GetStrBase());
  2640. return 1;
  2641. }
  2642. return 0;
  2643. }
  2644. int EQ2Emu_lua_GetAgiBase(lua_State* state) {
  2645. if (!lua_interface)
  2646. return 0;
  2647. Spawn* spawn = lua_interface->GetSpawn(state);
  2648. if (spawn && spawn->IsEntity()) {
  2649. lua_interface->SetInt32Value(state, ((Entity*)spawn)->GetAgiBase());
  2650. return 1;
  2651. }
  2652. return 0;
  2653. }
  2654. int EQ2Emu_lua_SetStepComplete(lua_State* state) {
  2655. if (!lua_interface)
  2656. return 0;
  2657. Spawn* player = lua_interface->GetSpawn(state);
  2658. if (!player || !player->IsPlayer()) {
  2659. lua_interface->LogError("%s: LUA SetStepComplete command error: player is not valid", lua_interface->GetScriptName(state));
  2660. return 0;
  2661. }
  2662. int32 quest_id = lua_interface->GetInt32Value(state, 2);
  2663. if (quest_id <= 0) {
  2664. lua_interface->LogError("%s: LUA SetStepComplete command error: quest_id is not valid", lua_interface->GetScriptName(state));
  2665. return 0;
  2666. } else if ((((Player*)player)->player_quests.count(quest_id) <= 0)) {
  2667. lua_interface->LogError("%s: LUA SetStepComplete command error: player does not have quest", lua_interface->GetScriptName(state));
  2668. return 0;
  2669. }
  2670. int32 step = lua_interface->GetInt32Value(state, 3);
  2671. if (step > 0) {
  2672. Client* client = player->GetZone()->GetClientBySpawn(player);
  2673. if (client)
  2674. client->AddPendingQuestUpdate(quest_id, step);
  2675. } else {
  2676. lua_interface->LogError("%s: LUA SetStepComplete command error: step is not valid", lua_interface->GetScriptName(state));
  2677. }
  2678. return 0;
  2679. }
  2680. int EQ2Emu_lua_AddStepProgress(lua_State* state) {
  2681. Spawn* player = lua_interface->GetSpawn(state);
  2682. int32 quest_id = lua_interface->GetInt32Value(state, 2);
  2683. int32 step = lua_interface->GetInt32Value(state, 3);
  2684. int32 progress = lua_interface->GetInt32Value(state, 4);
  2685. if (player && player->IsPlayer() && quest_id > 0 && step > 0 && progress > 0 && (((Player*)player)->player_quests.count(quest_id) > 0)) {
  2686. Client* client = player->GetZone()->GetClientBySpawn(player);
  2687. if (client)
  2688. client->AddPendingQuestUpdate(quest_id, step, progress);
  2689. }
  2690. return 0;
  2691. }
  2692. int EQ2Emu_lua_GetTaskGroupStep(lua_State* state) {
  2693. if (!lua_interface)
  2694. return 0;
  2695. Spawn* player = lua_interface->GetSpawn(state);
  2696. int32 quest_id = lua_interface->GetInt32Value(state, 2);
  2697. if (player && player->IsPlayer() && quest_id > 0) {
  2698. lua_interface->SetInt32Value(state, ((Player*)player)->GetTaskGroupStep(quest_id));
  2699. return 1;
  2700. }
  2701. return 0;
  2702. }
  2703. int EQ2Emu_lua_QuestStepIsComplete(lua_State* state) {
  2704. if (!lua_interface)
  2705. return 0;
  2706. Spawn* player = lua_interface->GetSpawn(state);
  2707. int32 quest_id = lua_interface->GetInt32Value(state, 2);
  2708. int32 step_id = lua_interface->GetInt32Value(state, 3);
  2709. if (player && player->IsPlayer() && quest_id > 0) {
  2710. lua_interface->SetBooleanValue(state, ((Player*)player)->GetQuestStepComplete(quest_id, step_id));
  2711. return 1;
  2712. }
  2713. return 0;
  2714. }
  2715. int EQ2Emu_lua_GetQuestStep(lua_State* state) {
  2716. if (!lua_interface)
  2717. return 0;
  2718. Spawn* player = lua_interface->GetSpawn(state);
  2719. int32 quest_id = lua_interface->GetInt32Value(state, 2);
  2720. if (player && player->IsPlayer() && quest_id > 0) {
  2721. lua_interface->SetInt32Value(state, ((Player*)player)->GetQuestStep(quest_id));
  2722. return 1;
  2723. }
  2724. return 0;
  2725. }
  2726. int EQ2Emu_lua_RegisterQuest(lua_State* state) {
  2727. if (!lua_interface)
  2728. return 0;
  2729. Quest* quest = lua_interface->GetQuest(state);
  2730. string name = lua_interface->GetStringValue(state, 2);
  2731. string type = lua_interface->GetStringValue(state, 3);
  2732. string zone = lua_interface->GetStringValue(state, 4);
  2733. int16 level = lua_interface->GetInt16Value(state, 5);
  2734. string description = lua_interface->GetStringValue(state, 6);
  2735. bool load = true;
  2736. if (!quest) {
  2737. lua_interface->LogError("%s: Quest not given in RegisterQuest!", lua_interface->GetScriptName(state));
  2738. load = false;
  2739. }
  2740. if (load && name.length() == 0) {
  2741. lua_interface->LogError("%s: Name not given in RegisterQuest!", lua_interface->GetScriptName(state));
  2742. load = false;
  2743. }
  2744. if (load && type.length() == 0) {
  2745. lua_interface->LogError("%s: Type not given in RegisterQuest for '%s'!", lua_interface->GetScriptName(state), name.c_str());
  2746. load = false;
  2747. }
  2748. if (load && zone.length() == 0) {
  2749. lua_interface->LogError("%s: Zone not given in RegisterQuest for '%s'!", lua_interface->GetScriptName(state), name.c_str());
  2750. load = false;
  2751. }
  2752. if (load && description.length() == 0) {
  2753. lua_interface->LogError("%s: Description not given in RegisterQuest for '%s'!", lua_interface->GetScriptName(state), name.c_str());
  2754. load = false;
  2755. }
  2756. if (load && level == 0) {
  2757. lua_interface->LogError("%s: Level not given in RegisterQuest for '%s'!", lua_interface->GetScriptName(state), name.c_str());
  2758. load = false;
  2759. }
  2760. if (load)
  2761. quest->RegisterQuest(name, type, zone, level, description);
  2762. return 0;
  2763. }
  2764. int EQ2Emu_lua_SetQuestPrereqLevel(lua_State* state) {
  2765. if (!lua_interface)
  2766. return 0;
  2767. Quest* quest = lua_interface->GetQuest(state);
  2768. if (quest) {
  2769. int8 level = lua_interface->GetInt16Value(state, 2);
  2770. quest->SetPrereqLevel(level);
  2771. }
  2772. return 0;
  2773. }
  2774. int EQ2Emu_lua_AddQuestPrereqQuest(lua_State* state) {
  2775. if (!lua_interface)
  2776. return 0;
  2777. Quest* quest = lua_interface->GetQuest(state);
  2778. if (quest) {
  2779. int32 quest_id = lua_interface->GetInt32Value(state, 2);
  2780. quest->AddPrereqQuest(quest_id);
  2781. }
  2782. return 0;
  2783. }
  2784. int EQ2Emu_lua_AddQuestPrereqItem(lua_State* state) {
  2785. if (!lua_interface)
  2786. return 0;
  2787. Quest* quest = lua_interface->GetQuest(state);
  2788. if (quest) {
  2789. int32 item_id = lua_interface->GetInt32Value(state, 2);
  2790. int8 quantity = lua_interface->GetInt32Value(state, 3);
  2791. if (quantity == 0)
  2792. quantity = 1;
  2793. Item* master_item = master_item_list.GetItem(item_id);
  2794. if (master_item) {
  2795. Item* item = new Item(master_item);
  2796. item->details.count = quantity;
  2797. quest->AddPrereqItem(item);
  2798. }
  2799. }
  2800. return 0;
  2801. }
  2802. int EQ2Emu_lua_HasQuest(lua_State* state) {
  2803. if (!lua_interface)
  2804. return 0;
  2805. Spawn* player = lua_interface->GetSpawn(state);
  2806. if(!player || !player->IsPlayer()) {
  2807. lua_interface->LogError("%s: LUA HasQuest command error: player is not valid", lua_interface->GetScriptName(state));
  2808. return 0;
  2809. }
  2810. int32 quest_id = lua_interface->GetInt32Value(state, 2);
  2811. if (quest_id > 0) {
  2812. lua_interface->SetBooleanValue(state, (((Player*)player)->player_quests.count(quest_id) > 0));
  2813. return 1;
  2814. } else {
  2815. lua_interface->LogError("%s: LUA HasQuest command error: quest_id is not valid", lua_interface->GetScriptName(state));
  2816. }
  2817. return 0;
  2818. }
  2819. int EQ2Emu_lua_QuestReturnNPC(lua_State* state) {
  2820. if (!lua_interface)
  2821. return 0;
  2822. Quest* quest = lua_interface->GetQuest(state);
  2823. int32 spawn_id = lua_interface->GetInt32Value(state, 2);
  2824. if (quest && spawn_id > 0)
  2825. quest->SetQuestReturnNPC(spawn_id);
  2826. return 0;
  2827. }
  2828. int EQ2Emu_lua_AddTimer(lua_State* state) {
  2829. if (!lua_interface)
  2830. return 0;
  2831. Spawn* spawn = lua_interface->GetSpawn(state);
  2832. if (!spawn) {
  2833. lua_interface->LogError("%s: LUA AddTimer command error: spawn is not valid", lua_interface->GetScriptName(state));
  2834. return 0;
  2835. }
  2836. int32 time = lua_interface->GetInt32Value(state, 2);
  2837. if (time <= 0) {
  2838. lua_interface->LogError("%s: LUA AddTimer command error: time is not set", lua_interface->GetScriptName(state));
  2839. return 0;
  2840. }
  2841. string function = lua_interface->GetStringValue(state, 3);
  2842. if (function.length() == 0) {
  2843. lua_interface->LogError("%s: LUA AddTimer command error: function is not set", lua_interface->GetScriptName(state));
  2844. return 0;
  2845. }
  2846. int32 max_count = lua_interface->GetInt32Value(state, 4);
  2847. Spawn* player = lua_interface->GetSpawn(state, 5);
  2848. SpawnScriptTimer* timer = new SpawnScriptTimer;
  2849. timer->timer = Timer::GetCurrentTime2() + time;
  2850. timer->function = function;
  2851. timer->spawn = spawn->GetID();
  2852. timer->player = player ? player->GetID() : 0;
  2853. if (max_count == 0)
  2854. max_count = 1;
  2855. timer->max_count = max_count;
  2856. timer->current_count = 0;
  2857. spawn->GetZone()->AddSpawnScriptTimer(timer);
  2858. return 0;
  2859. }
  2860. int EQ2Emu_lua_GetQuest(lua_State* state) {
  2861. if (!lua_interface)
  2862. return 0;
  2863. Spawn* player = lua_interface->GetSpawn(state);
  2864. int32 quest_id = lua_interface->GetInt32Value(state, 2);
  2865. if (player && player->IsPlayer() && quest_id > 0) {
  2866. lua_interface->SetQuestValue(state, ((Player*)player)->player_quests[quest_id]);
  2867. return 1;
  2868. }
  2869. return 0;
  2870. }
  2871. int EQ2Emu_lua_QuestIsComplete(lua_State* state) {
  2872. if (!lua_interface)
  2873. return 0;
  2874. Spawn* player = lua_interface->GetSpawn(state);
  2875. int32 quest_id = lua_interface->GetInt32Value(state, 2);
  2876. if (player && player->IsPlayer() && quest_id > 0 && (((Player*)player)->player_quests.count(quest_id) > 0)) {
  2877. Quest* quest = ((Player*)player)->player_quests[quest_id];
  2878. if (quest)
  2879. lua_interface->SetBooleanValue(state, quest->GetCompleted());
  2880. return 1;
  2881. }
  2882. return 0;
  2883. }
  2884. int EQ2Emu_lua_HasCompletedQuest(lua_State* state) {
  2885. if (!lua_interface)
  2886. return 0;
  2887. Spawn* player = lua_interface->GetSpawn(state);
  2888. int32 quest_id = lua_interface->GetInt32Value(state, 2);
  2889. if (player && player->IsPlayer() && quest_id > 0) {
  2890. lua_interface->SetBooleanValue(state, (((Player*)player)->GetCompletedQuest(quest_id) != 0));
  2891. return 1;
  2892. }
  2893. return 0;
  2894. }
  2895. int EQ2Emu_lua_ProvidesQuest(lua_State* state) {
  2896. if (!lua_interface)
  2897. return 0;
  2898. Spawn* npc = lua_interface->GetSpawn(state);
  2899. int32 quest_id = lua_interface->GetInt32Value(state, 2);
  2900. if (npc && !npc->IsPlayer() && quest_id > 0)
  2901. npc->AddProvidedQuest(quest_id);
  2902. return 0;
  2903. }
  2904. int EQ2Emu_lua_OfferQuest(lua_State* state) {
  2905. if (!lua_interface)
  2906. return 0;
  2907. Spawn* npc = lua_interface->GetSpawn(state);
  2908. Spawn* player = lua_interface->GetSpawn(state, 2);
  2909. int32 quest_id = lua_interface->GetInt32Value(state, 3);
  2910. bool forced = lua_interface->GetBooleanValue(state, 4);
  2911. /* NPC is allowed to be null */
  2912. if (player && player->IsPlayer() && quest_id > 0) {
  2913. Quest* master_quest = master_quest_list.GetQuest(quest_id);
  2914. if (master_quest) {
  2915. Client* client = player->GetZone()->GetClientBySpawn(player);
  2916. if (!client) {
  2917. lua_interface->LogError("%s: LUA OfferQuest command error: client is not set", lua_interface->GetScriptName(state));
  2918. }
  2919. Quest* quest = new Quest(master_quest);
  2920. if (!quest) {
  2921. lua_interface->LogError("%s: LUA OfferQuest command error: new Quest() failed.", lua_interface->GetScriptName(state));
  2922. }
  2923. if (client && quest) {
  2924. if (npc)
  2925. quest->SetQuestGiver(npc->GetDatabaseID());
  2926. else
  2927. quest->SetQuestGiver(0);
  2928. client->AddPendingQuest(quest, forced);
  2929. }
  2930. }
  2931. else {
  2932. lua_interface->LogError("%s: LUA OfferQuest command error: failed to get quest %d", lua_interface->GetScriptName(state), quest_id);
  2933. }
  2934. }
  2935. else {
  2936. lua_interface->LogError("%s: LUA OfferQuest command error: player is not set or bad quest id %p %d", lua_interface->GetScriptName(state), player, quest_id);
  2937. }
  2938. return 0;
  2939. }
  2940. int EQ2Emu_lua_AddQuestPrereqClass(lua_State* state) {
  2941. if (!lua_interface)
  2942. return 0;
  2943. Quest* quest = lua_interface->GetQuest(state);
  2944. if (quest) {
  2945. int8 class_id = lua_interface->GetInt8Value(state, 2);
  2946. quest->AddPrereqClass(class_id);
  2947. }
  2948. return 0;
  2949. }
  2950. int EQ2Emu_lua_AddQuestPrereqRace(lua_State* state) {
  2951. if (!lua_interface)
  2952. return 0;
  2953. Quest* quest = lua_interface->GetQuest(state);
  2954. if (quest) {
  2955. int8 race = lua_interface->GetInt8Value(state, 2);
  2956. quest->AddPrereqRace(race);
  2957. }
  2958. return 0;
  2959. }
  2960. int EQ2Emu_lua_AddQuestPrereqModelType(lua_State* state) {
  2961. if (!lua_interface)
  2962. return 0;
  2963. Quest* quest = lua_interface->GetQuest(state);
  2964. if (quest) {
  2965. int16 model_type = lua_interface->GetInt16Value(state, 2);
  2966. quest->AddPrereqModelType(model_type);
  2967. }
  2968. return 0;
  2969. }
  2970. int EQ2Emu_lua_AddQuestPrereqTradeskillLevel(lua_State* state) {
  2971. if (!lua_interface)
  2972. return 0;
  2973. Quest* quest = lua_interface->GetQuest(state);
  2974. if (!quest) {
  2975. lua_interface->LogError("%s: LUA AddQuestPrereqTradeskillLevel command error: quest is not valid", lua_interface->GetScriptName(state));
  2976. return 0;
  2977. }
  2978. int8 level = lua_interface->GetInt8Value(state, 2);
  2979. quest->SetPrereqTSLevel(level);
  2980. return 0;
  2981. }
  2982. int EQ2Emu_lua_AddQuestPrereqTradeskillClass(lua_State* state) {
  2983. if (!lua_interface)
  2984. return 0;
  2985. Quest* quest = lua_interface->GetQuest(state);
  2986. if (!quest) {
  2987. lua_interface->LogError("%s: LUA AddQuestPrereqTradeskillClass command error: quest is not valid", lua_interface->GetScriptName(state));
  2988. return 0;
  2989. }
  2990. int8 class_id = lua_interface->GetInt8Value(state, 2);
  2991. quest->AddPrereqTradeskillClass(class_id);
  2992. return 0;
  2993. }
  2994. int EQ2Emu_lua_AddQuestPrereqFaction(lua_State* state) {
  2995. if (!lua_interface)
  2996. return 0;
  2997. Quest* quest = lua_interface->GetQuest(state);
  2998. if (quest) {
  2999. int32 faction_id = lua_interface->GetInt32Value(state, 2);
  3000. sint32 min = lua_interface->GetSInt32Value(state, 3);
  3001. sint32 max = lua_interface->GetSInt32Value(state, 4);
  3002. quest->AddPrereqFaction(faction_id, min, max);
  3003. }
  3004. return 0;
  3005. }
  3006. int EQ2Emu_lua_AddQuestSelectableRewardItem(lua_State* state) {
  3007. if (!lua_interface)
  3008. return 0;
  3009. Quest* quest = lua_interface->GetQuest(state);
  3010. if (quest) {
  3011. int32 item_id = lua_interface->GetInt32Value(state, 2);
  3012. int8 quantity = lua_interface->GetInt8Value(state, 3);
  3013. if (quantity == 0)
  3014. quantity = 1;
  3015. Item* master_item = master_item_list.GetItem(item_id);
  3016. if (master_item) {
  3017. Item* item = new Item(master_item);
  3018. item->details.count = quantity;
  3019. quest->AddSelectableRewardItem(item);
  3020. }
  3021. }
  3022. return 0;
  3023. }
  3024. int EQ2Emu_lua_HasQuestRewardItem(lua_State* state) {
  3025. if (!lua_interface)
  3026. return 0;
  3027. Quest* quest = lua_interface->GetQuest(state);
  3028. if (quest) {
  3029. int32 item_id = lua_interface->GetInt32Value(state, 2);
  3030. vector<Item*>* items = quest->GetRewardItems();
  3031. if (items) {
  3032. vector<Item*>::iterator itr;
  3033. for (itr = items->begin(); itr != items->end(); itr++) {
  3034. if (*itr && (*itr)->details.item_id == item_id) {
  3035. lua_interface->SetBooleanValue(state, true);
  3036. return 1;
  3037. }
  3038. }
  3039. }
  3040. }
  3041. lua_interface->SetBooleanValue(state, false);
  3042. return 1;
  3043. }
  3044. int EQ2Emu_lua_AddQuestRewardItem(lua_State* state) {
  3045. if (!lua_interface)
  3046. return 0;
  3047. Quest* quest = lua_interface->GetQuest(state);
  3048. if (quest) {
  3049. int32 item_id = lua_interface->GetInt32Value(state, 2);
  3050. int8 quantity = lua_interface->GetInt32Value(state, 3);
  3051. if (quantity == 0)
  3052. quantity = 1;
  3053. Item* master_item = master_item_list.GetItem(item_id);
  3054. if (master_item) {
  3055. Item* item = new Item(master_item);
  3056. item->details.count = quantity;
  3057. quest->AddRewardItem(item);
  3058. }
  3059. }
  3060. return 0;
  3061. }
  3062. int EQ2Emu_lua_AddQuestRewardCoin(lua_State* state) {
  3063. if (!lua_interface)
  3064. return 0;
  3065. Quest* quest = lua_interface->GetQuest(state);
  3066. if (quest) {
  3067. int32 copper = lua_interface->GetInt32Value(state, 2);
  3068. int32 silver = lua_interface->GetInt32Value(state, 3);
  3069. int32 gold = lua_interface->GetInt32Value(state, 4);
  3070. int32 plat = lua_interface->GetInt32Value(state, 5);
  3071. quest->AddRewardCoins(copper, silver, gold, plat);
  3072. }
  3073. return 0;
  3074. }
  3075. int EQ2Emu_lua_AddQuestRewardFaction(lua_State* state) {
  3076. if (!lua_interface)
  3077. return 0;
  3078. Quest* quest = lua_interface->GetQuest(state);
  3079. int32 faction_id = lua_interface->GetInt32Value(state, 2);
  3080. sint32 amount = lua_interface->GetSInt32Value(state, 3);
  3081. if (quest && faction_id > 0 && amount != 0)
  3082. quest->AddRewardFaction(faction_id, amount);
  3083. return 0;
  3084. }
  3085. int EQ2Emu_lua_SetQuestRewardStatus(lua_State* state) {
  3086. if (!lua_interface)
  3087. return 0;
  3088. Quest* quest = lua_interface->GetQuest(state);
  3089. if (quest) {
  3090. int32 status = lua_interface->GetInt32Value(state, 2);
  3091. quest->SetRewardStatus(status);
  3092. }
  3093. return 0;
  3094. }
  3095. int EQ2Emu_lua_SetQuestRewardComment(lua_State* state) {
  3096. if (!lua_interface)
  3097. return 0;
  3098. Quest* quest = lua_interface->GetQuest(state);
  3099. if (quest) {
  3100. string comment = lua_interface->GetStringValue(state, 2);
  3101. quest->SetRewardComment(comment);
  3102. }
  3103. return 0;
  3104. }
  3105. int EQ2Emu_lua_SetQuestRewardExp(lua_State* state) {
  3106. if (!lua_interface)
  3107. return 0;
  3108. Quest* quest = lua_interface->GetQuest(state);
  3109. if (quest) {
  3110. int32 exp = lua_interface->GetInt32Value(state, 2);
  3111. quest->SetRewardXP(exp);
  3112. }
  3113. return 0;
  3114. }
  3115. int EQ2Emu_lua_AddQuestStep(lua_State* state) {
  3116. Quest* quest = lua_interface->GetQuest(state);
  3117. if (quest) {
  3118. int32 step = lua_interface->GetInt32Value(state, 2);
  3119. string description = lua_interface->GetStringValue(state, 3);
  3120. int32 quantity = lua_interface->GetInt32Value(state, 4);
  3121. float percentage = lua_interface->GetFloatValue(state, 5);
  3122. string str_taskgroup = lua_interface->GetStringValue(state, 6);
  3123. int16 icon = lua_interface->GetInt16Value(state, 7);
  3124. int32 usableitemid = lua_interface->GetInt32Value(state, 8);
  3125. const char* taskgroup = 0;
  3126. if (str_taskgroup.length() > 0)
  3127. taskgroup = str_taskgroup.c_str();
  3128. QuestStep* quest_step = quest->AddQuestStep(step, QUEST_STEP_TYPE_NORMAL, description, 0, quantity, taskgroup, 0, 0, percentage, usableitemid);
  3129. if (quest_step && icon && quantity > 0)
  3130. quest_step->SetIcon(icon);
  3131. }
  3132. return 0;
  3133. }
  3134. int EQ2Emu_lua_AddQuestStepKill(lua_State* state) {
  3135. if (!lua_interface)
  3136. return 0;
  3137. Quest* quest = lua_interface->GetQuest(state);
  3138. if (quest) {
  3139. int32 step = lua_interface->GetInt32Value(state, 2);
  3140. string description = lua_interface->GetStringValue(state, 3);
  3141. int32 quantity = lua_interface->GetInt32Value(state, 4);
  3142. float percentage = lua_interface->GetFloatValue(state, 5);
  3143. string str_taskgroup = lua_interface->GetStringValue(state, 6);
  3144. int16 icon = lua_interface->GetInt16Value(state, 7);
  3145. const char* taskgroup = 0;
  3146. if (str_taskgroup.length() > 0)
  3147. taskgroup = str_taskgroup.c_str();
  3148. int32 npc_id = 0;
  3149. vector<int32>* ids = 0;
  3150. Spawn* spawn = nullptr;
  3151. int i = 0;
  3152. while ((npc_id = lua_interface->GetInt32Value(state, 8 + i))) {
  3153. if (ids == 0)
  3154. ids = new vector<int32>;
  3155. ids->push_back(npc_id);
  3156. i++;
  3157. }
  3158. QuestStep* quest_step = quest->AddQuestStep(step, QUEST_STEP_TYPE_KILL, description, ids, quantity, taskgroup, 0, 0, percentage, 0);
  3159. if (quest_step && icon > 0 && quantity > 0)
  3160. quest_step->SetIcon(icon);
  3161. if (quest->GetPlayer()) {
  3162. Client* client = quest->GetPlayer()->GetZone()->GetClientBySpawn(quest->GetPlayer());
  3163. quest->GetPlayer()->GetZone()->SendQuestUpdates(client);
  3164. }
  3165. }
  3166. return 0;
  3167. }
  3168. int EQ2Emu_lua_AddQuestStepChat(lua_State* state) {
  3169. if (!lua_interface)
  3170. return 0;
  3171. Quest* quest = lua_interface->GetQuest(state);
  3172. if (quest) {
  3173. int32 step = lua_interface->GetInt32Value(state, 2);
  3174. string description = lua_interface->GetStringValue(state, 3);
  3175. int32 quantity = lua_interface->GetInt32Value(state, 4);
  3176. string str_taskgroup = lua_interface->GetStringValue(state, 5);
  3177. int16 icon = lua_interface->GetInt16Value(state, 6);
  3178. const char* taskgroup = 0;
  3179. if (str_taskgroup.length() > 0)
  3180. taskgroup = str_taskgroup.c_str();
  3181. int32 npc_id = 0;
  3182. vector<int32>* ids = 0;
  3183. int i = 0;
  3184. while ((npc_id = lua_interface->GetInt32Value(state, 7 + i))) {
  3185. if (ids == 0)
  3186. ids = new vector<int32>;
  3187. ids->push_back(npc_id);
  3188. i++;
  3189. }
  3190. QuestStep* quest_step = quest->AddQuestStep(step, QUEST_STEP_TYPE_CHAT, description, ids, quantity, taskgroup);
  3191. if (quest_step && icon > 0)
  3192. quest_step->SetIcon(icon);
  3193. if (quest->GetPlayer()) {
  3194. Client* client = quest->GetPlayer()->GetZone()->GetClientBySpawn(quest->GetPlayer());
  3195. quest->GetPlayer()->GetZone()->SendQuestUpdates(client);
  3196. }
  3197. }
  3198. return 0;
  3199. }
  3200. int EQ2Emu_lua_AddQuestStepObtainItem(lua_State* state) {
  3201. if (!lua_interface)
  3202. return 0;
  3203. Quest* quest = lua_interface->GetQuest(state);
  3204. if (quest) {
  3205. int32 step = lua_interface->GetInt32Value(state, 2);
  3206. string description = lua_interface->GetStringValue(state, 3);
  3207. int32 quantity = lua_interface->GetInt32Value(state, 4);
  3208. float percentage = lua_interface->GetFloatValue(state, 5);
  3209. string str_taskgroup = lua_interface->GetStringValue(state, 6);
  3210. int16 icon = lua_interface->GetInt16Value(state, 7);
  3211. const char* taskgroup = 0;
  3212. if (str_taskgroup.length() > 0)
  3213. taskgroup = str_taskgroup.c_str();
  3214. int32 item_id = 0;
  3215. vector<int32>* ids = 0;
  3216. int i = 0;
  3217. while ((item_id = lua_interface->GetInt32Value(state, 8 + i))) {
  3218. if (ids == 0)
  3219. ids = new vector<int32>;
  3220. ids->push_back(item_id);
  3221. i++;
  3222. }
  3223. QuestStep* quest_step = quest->AddQuestStep(step, QUEST_STEP_TYPE_OBTAIN_ITEM, description, ids, quantity, taskgroup, 0, 0, percentage, 0);
  3224. if (quest_step && icon > 0 && quantity > 0)
  3225. quest_step->SetIcon(icon);
  3226. if (quest->GetPlayer()) {
  3227. Client* client = quest->GetPlayer()->GetZone()->GetClientBySpawn(quest->GetPlayer());
  3228. quest->GetPlayer()->GetZone()->SendQuestUpdates(client);
  3229. }
  3230. }
  3231. return 0;
  3232. }
  3233. int EQ2Emu_lua_AddQuestStepLocation(lua_State* state) {
  3234. if (!lua_interface)
  3235. return 0;
  3236. Quest* quest = lua_interface->GetQuest(state);
  3237. if (quest) {
  3238. int32 step = lua_interface->GetInt32Value(state, 2);
  3239. string description = lua_interface->GetStringValue(state, 3);
  3240. float max_variation = lua_interface->GetFloatValue(state, 4);
  3241. string str_taskgroup = lua_interface->GetStringValue(state, 5);
  3242. int16 icon = lua_interface->GetInt16Value(state, 6);
  3243. const char* taskgroup = 0;
  3244. if (str_taskgroup.length() > 0)
  3245. taskgroup = str_taskgroup.c_str();
  3246. vector<Location>* locations = 0;
  3247. int i = 7;
  3248. while (true) {
  3249. Location loc;
  3250. loc.x = lua_interface->GetFloatValue(state, i);
  3251. loc.y = lua_interface->GetFloatValue(state, i + 1);
  3252. loc.z = lua_interface->GetFloatValue(state, i + 2);
  3253. if (loc.x == 0 && loc.y == 0 && loc.z == 0)
  3254. break;
  3255. if (locations == 0)
  3256. locations = new vector<Location>;
  3257. locations->push_back(loc);
  3258. i += 3;
  3259. }
  3260. QuestStep* quest_step = quest->AddQuestStep(step, QUEST_STEP_TYPE_LOCATION, description, 0, 1, taskgroup, locations, max_variation);
  3261. if (quest_step && icon > 0)
  3262. quest_step->SetIcon(icon);
  3263. if (quest->GetPlayer()) {
  3264. Client* client = quest->GetPlayer()->GetZone()->GetClientBySpawn(quest->GetPlayer());
  3265. quest->GetPlayer()->GetZone()->SendQuestUpdates(client);
  3266. }
  3267. }
  3268. return 0;
  3269. }
  3270. int EQ2Emu_lua_AddQuestUsableItem(lua_State* state) {
  3271. if (!lua_interface)
  3272. return 0;
  3273. Quest* quest = lua_interface->GetQuest(state);
  3274. if (quest) {
  3275. int32 step = lua_interface->GetInt32Value(state, 2);
  3276. string description = lua_interface->GetStringValue(state, 3);
  3277. float max_variation = lua_interface->GetFloatValue(state, 4);
  3278. string str_taskgroup = lua_interface->GetStringValue(state, 5);
  3279. int16 icon = lua_interface->GetInt16Value(state, 6);
  3280. const char* taskgroup = 0;
  3281. if (str_taskgroup.length() > 0)
  3282. taskgroup = str_taskgroup.c_str();
  3283. vector<Location>* locations = 0;
  3284. int i = 7;
  3285. while (true) {
  3286. Location loc;
  3287. loc.x = lua_interface->GetFloatValue(state, i);
  3288. loc.y = lua_interface->GetFloatValue(state, i + 1);
  3289. loc.z = lua_interface->GetFloatValue(state, i + 2);
  3290. if (loc.x == 0 && loc.y == 0 && loc.z == 0)
  3291. break;
  3292. if (locations == 0)
  3293. locations = new vector<Location>;
  3294. locations->push_back(loc);
  3295. i += 3;
  3296. }
  3297. QuestStep* quest_step = quest->AddQuestStep(step, QUEST_STEP_TYPE_LOCATION, description, 0, 1, taskgroup, locations, max_variation);
  3298. if (quest_step && icon > 0)
  3299. quest_step->SetIcon(icon);
  3300. if (quest->GetPlayer()) {
  3301. Client* client = quest->GetPlayer()->GetZone()->GetClientBySpawn(quest->GetPlayer());
  3302. quest->GetPlayer()->GetZone()->SendQuestUpdates(client);
  3303. }
  3304. }
  3305. return 0;
  3306. }
  3307. int EQ2Emu_lua_AddQuestStepSpell(lua_State* state) {
  3308. Quest* quest = lua_interface->GetQuest(state);
  3309. if (quest) {
  3310. int32 step = lua_interface->GetInt32Value(state, 2);
  3311. string description = lua_interface->GetStringValue(state, 3);
  3312. int32 quantity = lua_interface->GetInt32Value(state, 4);
  3313. float percentage = lua_interface->GetFloatValue(state, 5);
  3314. string str_taskgroup = lua_interface->GetStringValue(state, 6);
  3315. int16 icon = lua_interface->GetInt16Value(state, 7);
  3316. const char* taskgroup = 0;
  3317. if (str_taskgroup.length() > 0)
  3318. taskgroup = str_taskgroup.c_str();
  3319. int32 spell_id = 0;
  3320. vector<int32>* ids = 0;
  3321. int i = 0;
  3322. while ((spell_id = lua_interface->GetInt32Value(state, 8 + i))) {
  3323. if (ids == 0)
  3324. ids = new vector<int32>;
  3325. ids->push_back(spell_id);
  3326. i++;
  3327. }
  3328. QuestStep* quest_step = quest->AddQuestStep(step, QUEST_STEP_TYPE_SPELL, description, ids, quantity, taskgroup, 0, 0, percentage, 0);
  3329. if (quest_step && icon > 0 && quantity > 0)
  3330. quest_step->SetIcon(icon);
  3331. if (quest->GetPlayer()) {
  3332. Client* client = quest->GetPlayer()->GetZone()->GetClientBySpawn(quest->GetPlayer());
  3333. quest->GetPlayer()->GetZone()->SendQuestUpdates(client);
  3334. }
  3335. }
  3336. return 0;
  3337. }
  3338. int EQ2Emu_lua_AddQuestStepCraft(lua_State* state) {
  3339. if (!lua_interface)
  3340. return 0;
  3341. Quest* quest = lua_interface->GetQuest(state);
  3342. if (quest) {
  3343. int32 step = lua_interface->GetInt32Value(state, 2);
  3344. string description = lua_interface->GetStringValue(state, 3);
  3345. int32 quantity = lua_interface->GetInt32Value(state, 4);
  3346. float percentage = lua_interface->GetFloatValue(state, 5);
  3347. string str_taskgroup = lua_interface->GetStringValue(state, 6);
  3348. int16 icon = lua_interface->GetInt16Value(state, 7);
  3349. const char* taskgroup = 0;
  3350. if (str_taskgroup.length() > 0)
  3351. taskgroup = str_taskgroup.c_str();
  3352. int32 item_id = 0;
  3353. vector<int32>* ids = 0;
  3354. int i = 0;
  3355. while ((item_id = lua_interface->GetInt32Value(state, 8 + i))) {
  3356. if (ids == 0)
  3357. ids = new vector<int32>;
  3358. ids->push_back(item_id);
  3359. i++;
  3360. }
  3361. QuestStep* quest_step = quest->AddQuestStep(step, QUEST_STEP_TYPE_CRAFT, description, ids, quantity, taskgroup, 0, 0, percentage, 0);
  3362. if (quest_step && icon > 0 && quantity > 0)
  3363. quest_step->SetIcon(icon);
  3364. if (quest->GetPlayer()) {
  3365. Client* client = quest->GetPlayer()->GetZone()->GetClientBySpawn(quest->GetPlayer());
  3366. quest->GetPlayer()->GetZone()->SendQuestUpdates(client);
  3367. }
  3368. }
  3369. return 0;
  3370. }
  3371. int EQ2Emu_lua_AddQuestStepHarvest(lua_State* state) {
  3372. if (!lua_interface)
  3373. return 0;
  3374. Quest* quest = lua_interface->GetQuest(state);
  3375. if (quest) {
  3376. int32 step = lua_interface->GetInt32Value(state, 2);
  3377. string description = lua_interface->GetStringValue(state, 3);
  3378. int32 quantity = lua_interface->GetInt32Value(state, 4);
  3379. float percentage = lua_interface->GetFloatValue(state, 5);
  3380. string str_taskgroup = lua_interface->GetStringValue(state, 6);
  3381. int16 icon = lua_interface->GetInt16Value(state, 7);
  3382. const char* taskgroup = 0;
  3383. if (str_taskgroup.length() > 0)
  3384. taskgroup = str_taskgroup.c_str();
  3385. int32 item_id = 0;
  3386. vector<int32>* ids = 0;
  3387. int i = 0;
  3388. while ((item_id = lua_interface->GetInt32Value(state, 8 + i))) {
  3389. if (ids == 0)
  3390. ids = new vector<int32>;
  3391. ids->push_back(item_id);
  3392. i++;
  3393. }
  3394. QuestStep* quest_step = quest->AddQuestStep(step, QUEST_STEP_TYPE_HARVEST, description, ids, quantity, taskgroup, 0, 0, percentage, 0);
  3395. if (quest_step && icon > 0 && quantity > 0)
  3396. quest_step->SetIcon(icon);
  3397. if (quest->GetPlayer()) {
  3398. Client* client = quest->GetPlayer()->GetZone()->GetClientBySpawn(quest->GetPlayer());
  3399. quest->GetPlayer()->GetZone()->SendQuestUpdates(client);
  3400. }
  3401. }
  3402. return 0;
  3403. }
  3404. int EQ2Emu_lua_SetQuestCompleteAction(lua_State* state) {
  3405. if (!lua_interface)
  3406. return 0;
  3407. Quest* quest = lua_interface->GetQuest(state);
  3408. if (quest) {
  3409. string action = lua_interface->GetStringValue(state, 2);
  3410. if (action.length() > 0)
  3411. quest->SetCompleteAction(action);
  3412. }
  3413. return 0;
  3414. }
  3415. int EQ2Emu_lua_AddQuestStepCompleteAction(lua_State* state) {
  3416. if (!lua_interface)
  3417. return 0;
  3418. Quest* quest = lua_interface->GetQuest(state);
  3419. if (quest) {
  3420. int32 step = lua_interface->GetInt32Value(state, 2);
  3421. string action = lua_interface->GetStringValue(state, 3);
  3422. if (step > 0 && action.length() > 0)
  3423. quest->AddCompleteAction(step, action);
  3424. }
  3425. return 0;
  3426. }
  3427. int EQ2Emu_lua_AddQuestStepProgressAction(lua_State* state) {
  3428. if (!lua_interface)
  3429. return 0;
  3430. Quest* quest = lua_interface->GetQuest(state);
  3431. if (quest) {
  3432. int32 step = lua_interface->GetInt32Value(state, 2);
  3433. string action = lua_interface->GetStringValue(state, 3);
  3434. if (step > 0 && action.length() > 0)
  3435. quest->AddProgressAction(step, action);
  3436. }
  3437. return 0;
  3438. }
  3439. int EQ2Emu_lua_UpdateQuestDescription(lua_State* state) {
  3440. if (!lua_interface)
  3441. return 0;
  3442. Quest* quest = lua_interface->GetQuest(state);
  3443. string description = lua_interface->GetStringValue(state, 2);
  3444. if (quest && description.length() > 0)
  3445. quest->SetDescription(description);
  3446. return 0;
  3447. }
  3448. int EQ2Emu_lua_SetCompletedDescription(lua_State* state) {
  3449. if (!lua_interface)
  3450. return 0;
  3451. Quest* quest = lua_interface->GetQuest(state);
  3452. string description = lua_interface->GetStringValue(state, 2);
  3453. if (quest && description.length() > 0)
  3454. quest->SetCompletedDescription(description);
  3455. return 0;
  3456. }
  3457. int EQ2Emu_lua_UpdateQuestTaskGroupDescription(lua_State* state) {
  3458. if (!lua_interface)
  3459. return 0;
  3460. Quest* quest = lua_interface->GetQuest(state);
  3461. int32 step = lua_interface->GetInt32Value(state, 2);
  3462. string description = lua_interface->GetStringValue(state, 3);
  3463. bool display_bullets = (lua_interface->GetInt8Value(state, 4) == 1);
  3464. if (quest && step > 0 && description.length() > 0) {
  3465. quest->SetTaskGroupDescription(step, description, display_bullets);
  3466. if (quest->GetPlayer()) {
  3467. Client* client = quest->GetPlayer()->GetZone()->GetClientBySpawn(quest->GetPlayer());
  3468. if (client)
  3469. client->SendQuestUpdateStep(quest, step, false);
  3470. }
  3471. }
  3472. return 0;
  3473. }
  3474. int EQ2Emu_lua_UpdateQuestStepDescription(lua_State* state) {
  3475. if (!lua_interface)
  3476. return 0;
  3477. Quest* quest = lua_interface->GetQuest(state);
  3478. int32 step = lua_interface->GetInt32Value(state, 2);
  3479. string description = lua_interface->GetStringValue(state, 3);
  3480. if (quest && step > 0 && description.length() > 0) {
  3481. quest->SetStepDescription(step, description);
  3482. if (quest->GetPlayer()) {
  3483. Client* client = quest->GetPlayer()->GetZone()->GetClientBySpawn(quest->GetPlayer());
  3484. if (client)
  3485. client->SendQuestUpdateStepImmediately(quest, step);
  3486. }
  3487. }
  3488. return 0;
  3489. }
  3490. int EQ2Emu_lua_UpdateQuestZone(lua_State* state) {
  3491. Quest* quest = lua_interface->GetQuest(state);
  3492. string zone = lua_interface->GetStringValue(state, 2);
  3493. if (quest && zone.length() > 0)
  3494. quest->SetZone(zone);
  3495. return 0;
  3496. }
  3497. int EQ2Emu_lua_GiveImmediateQuestReward(lua_State* state) {
  3498. if (!lua_interface)
  3499. return 0;
  3500. Quest* quest = lua_interface->GetQuest(state);
  3501. Spawn* playerSpawn = lua_interface->GetSpawn(state, 2);
  3502. int32 coin = lua_interface->GetInt32Value(state, 3);
  3503. int32 status_points = lua_interface->GetInt32Value(state, 4);
  3504. string rewards_str = lua_interface->GetStringValue(state, 5);
  3505. string select_rewards_str = lua_interface->GetStringValue(state, 6);
  3506. string factions_map_str = lua_interface->GetStringValue(state, 7);
  3507. string text = lua_interface->GetStringValue(state, 8);
  3508. int32 source_id = 0;
  3509. if (playerSpawn && playerSpawn->IsPlayer()) {
  3510. Player* player = (Player*)playerSpawn;
  3511. Client* client = player->GetZone()->GetClientBySpawn(player);
  3512. if (client) {
  3513. vector<Item*> reward_items;
  3514. vector<Item*> selectable_reward_items;
  3515. if (rewards_str.length() > 0) {
  3516. map<unsigned int, unsigned short> rewards = ParseIntMap(rewards_str);
  3517. map<unsigned int, unsigned short>::iterator itr;
  3518. for (itr = rewards.begin(); itr != rewards.end(); itr++) {
  3519. if (itr->first > 0) {
  3520. Item* item = new Item(master_item_list.GetItem(itr->first));
  3521. if (item) {
  3522. if (itr->second > 0)
  3523. item->details.count = itr->second;
  3524. reward_items.push_back(item);
  3525. }
  3526. }
  3527. }
  3528. }
  3529. if (select_rewards_str.length() > 0) {
  3530. map<unsigned int, unsigned short> rewards = ParseIntMap(select_rewards_str);
  3531. map<unsigned int, unsigned short>::iterator itr;
  3532. for (itr = rewards.begin(); itr != rewards.end(); itr++) {
  3533. if (itr->first > 0) {
  3534. Item* item = new Item(master_item_list.GetItem(itr->first));
  3535. if (item) {
  3536. if (itr->second > 0)
  3537. item->stack_count = itr->second;
  3538. selectable_reward_items.push_back(item);
  3539. }
  3540. }
  3541. }
  3542. }
  3543. map<unsigned int, signed int> faction_rewards = ParseSInt32Map(factions_map_str);
  3544. const char* reward_type = "Quest Reward!";
  3545. if (!quest)
  3546. reward_type = "Reward!";
  3547. client->DisplayQuestRewards(quest, coin, &reward_items, &selectable_reward_items, &faction_rewards, reward_type, status_points, text.c_str());
  3548. }
  3549. }
  3550. return 0;
  3551. }
  3552. int EQ2Emu_lua_GiveQuestReward(lua_State* state) {
  3553. if (!lua_interface)
  3554. return 0;
  3555. Quest* quest = lua_interface->GetQuest(state);
  3556. Spawn* spawn = lua_interface->GetSpawn(state, 2);
  3557. if (quest && spawn) {
  3558. if (spawn->IsPlayer()) {
  3559. Client* client = spawn->GetZone()->GetClientBySpawn(spawn);
  3560. if (client)
  3561. client->AddPendingQuestReward(quest);
  3562. }
  3563. }
  3564. return 0;
  3565. }
  3566. int EQ2Emu_lua_Harvest(lua_State* state) {
  3567. if (!lua_interface)
  3568. return 0;
  3569. Spawn* player = lua_interface->GetSpawn(state);
  3570. Spawn* node = lua_interface->GetSpawn(state, 2);
  3571. if (player && node && player->IsPlayer() && node->IsGroundSpawn()) {
  3572. Client* client = player->GetZone()->GetClientBySpawn(player);
  3573. if (client) {
  3574. LogWrite(MISC__TODO, 1, "TODO", "Cancel harvest if skill insufficient; Func: %s, Line: %i", __FUNCTION__, __LINE__);
  3575. ((GroundSpawn*)node)->ProcessHarvest(client);
  3576. if (((GroundSpawn*)node)->GetNumberHarvests() == 0)
  3577. player->GetZone()->RemoveSpawn(true, node, true);
  3578. }
  3579. }
  3580. else if (player && player->IsPlayer()) {
  3581. Client* client = player->GetZone()->GetClientBySpawn(player);
  3582. if (client)
  3583. client->Message(CHANNEL_COLOR_RED, "Invalid target for this spell.");
  3584. }
  3585. return 0;
  3586. }
  3587. int EQ2Emu_lua_Bind(lua_State* state) {
  3588. if (!lua_interface)
  3589. return 0;
  3590. Spawn* spawn = lua_interface->GetSpawn(state);
  3591. int32 zone_id = lua_interface->GetInt32Value(state, 2);
  3592. float x = lua_interface->GetFloatValue(state, 3);
  3593. float y = lua_interface->GetFloatValue(state, 4);
  3594. float z = lua_interface->GetFloatValue(state, 5);
  3595. float h = lua_interface->GetFloatValue(state, 6);
  3596. if (!spawn) {
  3597. lua_interface->LogError("%s: LUA Bind command error: spawn is not valid", lua_interface->GetScriptName(state));
  3598. return 0;
  3599. }
  3600. if (!spawn->IsPlayer()) {
  3601. lua_interface->LogError("%s: LUA Bind command error: spawn is not a player", lua_interface->GetScriptName(state));
  3602. return 0;
  3603. }
  3604. if (zone_id == 0) {
  3605. Client* client = spawn->GetZone()->GetClientBySpawn(spawn);
  3606. if (!client) {
  3607. lua_interface->LogError("%s: LUA Bind command error: unable to get client from spawn", lua_interface->GetScriptName(state));
  3608. return 0;
  3609. }
  3610. if (!client->Bind())
  3611. client->SimpleMessage(CHANNEL_COLOR_RED, "Unable to set bind point.");
  3612. }
  3613. else {
  3614. Player* player = (Player*)spawn;
  3615. player->GetPlayerInfo()->SetBindZone(zone_id);
  3616. player->GetPlayerInfo()->SetBindX(x);
  3617. player->GetPlayerInfo()->SetBindY(y);
  3618. player->GetPlayerInfo()->SetBindZ(z);
  3619. player->GetPlayerInfo()->SetBindHeading(h);
  3620. }
  3621. return 0;
  3622. }
  3623. int EQ2Emu_lua_Gate(lua_State* state) {
  3624. if (!lua_interface)
  3625. return 0;
  3626. Spawn* spawn = lua_interface->GetSpawn(state);
  3627. if (spawn) {
  3628. if (spawn->IsPlayer()) {
  3629. Client* client = spawn->GetZone()->GetClientBySpawn(spawn);
  3630. if (client) {
  3631. if (!client->Gate())
  3632. client->SimpleMessage(CHANNEL_COLOR_RED, "Unable to gate.");
  3633. }
  3634. }
  3635. }
  3636. return 0;
  3637. }
  3638. int EQ2Emu_lua_IsBindAllowed(lua_State* state) {
  3639. if (!lua_interface)
  3640. return 0;
  3641. bool ret = false;
  3642. Spawn* spawn = lua_interface->GetSpawn(state);
  3643. if (spawn) {
  3644. if (spawn->IsPlayer()) {
  3645. Client* client = spawn->GetZone()->GetClientBySpawn(spawn);
  3646. if (client)
  3647. ret = client->BindAllowed();
  3648. }
  3649. }
  3650. lua_interface->SetBooleanValue(state, ret);
  3651. return 1;
  3652. }
  3653. int EQ2Emu_lua_IsGateAllowed(lua_State* state) {
  3654. if (!lua_interface)
  3655. return 0;
  3656. bool ret = false;
  3657. Spawn* spawn = lua_interface->GetSpawn(state);
  3658. if (spawn) {
  3659. if (spawn->IsPlayer()) {
  3660. Client* client = spawn->GetZone()->GetClientBySpawn(spawn);
  3661. if (client)
  3662. ret = client->GateAllowed();
  3663. }
  3664. }
  3665. lua_interface->SetBooleanValue(state, ret);
  3666. return 1;
  3667. }
  3668. int EQ2Emu_lua_IsAlive(lua_State* state) {
  3669. Spawn* spawn = lua_interface->GetSpawn(state);
  3670. if (spawn) {
  3671. lua_interface->SetBooleanValue(state, spawn->Alive());
  3672. return 1;
  3673. }
  3674. return 0;
  3675. }
  3676. int EQ2Emu_lua_IsInCombat(lua_State* state) {
  3677. if (!lua_interface)
  3678. return 0;
  3679. Spawn* spawn = lua_interface->GetSpawn(state);
  3680. if (spawn && spawn->IsEntity()) {
  3681. lua_interface->SetBooleanValue(state, ((Entity*)spawn)->EngagedInCombat());
  3682. return 1;
  3683. }
  3684. return 0;
  3685. }
  3686. int EQ2Emu_lua_SendMessage(lua_State* state) {
  3687. Spawn* spawn = lua_interface->GetSpawn(state);
  3688. string message = lua_interface->GetStringValue(state, 2);
  3689. string color_str = lua_interface->GetStringValue(state, 3);
  3690. int8 color = CHANNEL_NARRATIVE;
  3691. if (spawn && spawn->IsPlayer() && message.length() > 0) {
  3692. Client* client = spawn->GetZone()->GetClientBySpawn(spawn);
  3693. if (client) {
  3694. if (color_str.length() > 0) {
  3695. // leave for backwards compat, but all future should just use the number
  3696. if (strncasecmp(color_str.c_str(), "red", 3) == 0)
  3697. color = CHANNEL_COLOR_RED;
  3698. else if (strncasecmp(color_str.c_str(), "yellow", 6) == 0)
  3699. color = CHANNEL_COLOR_YELLOW;
  3700. else
  3701. {
  3702. // use a number to specify the channel as per Commands/Commands.h defines
  3703. color = (int8)atoul(color_str.c_str());
  3704. }
  3705. }
  3706. client->SimpleMessage(color, message.c_str());
  3707. }
  3708. }
  3709. return 0;
  3710. }
  3711. int EQ2Emu_lua_SendPopUpMessage(lua_State* state) {
  3712. Spawn* spawn = lua_interface->GetSpawn(state);
  3713. string message = lua_interface->GetStringValue(state, 2);
  3714. int8 red = lua_interface->GetInt8Value(state, 3);
  3715. int8 green = lua_interface->GetInt8Value(state, 4);
  3716. int8 blue = lua_interface->GetInt8Value(state, 5);
  3717. if (!spawn) {
  3718. lua_interface->LogError("%s: LUA SendPopUpMessage command error: Spawn is not valid.", lua_interface->GetScriptName(state));
  3719. return 0;
  3720. }
  3721. int32 words = ::CountWordsInString(message.c_str());
  3722. if (words < 5)
  3723. words = 5;
  3724. Client* client = spawn->GetZone()->GetClientBySpawn(spawn);
  3725. if (client)
  3726. client->SendPopupMessage(10, message.c_str(), "ui_harvested_normal", words, red, green, blue);
  3727. return 0;
  3728. }
  3729. int EQ2Emu_lua_SetServerControlFlag(lua_State* state) {
  3730. Spawn* spawn = lua_interface->GetSpawn(state);
  3731. int8 param = lua_interface->GetInt8Value(state, 2);
  3732. int8 param_value = lua_interface->GetInt8Value(state, 3);
  3733. int8 value = lua_interface->GetInt8Value(state, 4);
  3734. if (spawn && spawn->IsPlayer() && (param >= 1 && param <= 5)) {
  3735. Client* client = spawn->GetZone()->GetClientBySpawn(spawn);
  3736. if (client) {
  3737. PacketStruct* packet = configReader.getStruct("WS_ServerControlFlags", client->GetVersion());
  3738. switch (param) {
  3739. case 1: {
  3740. packet->setDataByName("parameter1", param_value);
  3741. break;
  3742. }
  3743. case 2: {
  3744. packet->setDataByName("parameter2", param_value);
  3745. break;
  3746. }
  3747. case 3: {
  3748. packet->setDataByName("parameter3", param_value);
  3749. break;
  3750. }
  3751. case 4: {
  3752. packet->setDataByName("parameter4", param_value);
  3753. break;
  3754. }
  3755. case 5: {
  3756. packet->setDataByName("parameter5", param_value);
  3757. break;
  3758. }
  3759. }
  3760. packet->setDataByName("value", value);
  3761. client->QueuePacket(packet->serialize());
  3762. safe_delete(packet);
  3763. }
  3764. }
  3765. return 0;
  3766. }
  3767. int EQ2Emu_lua_ToggleTracking(lua_State* state) {
  3768. Spawn* spawn = lua_interface->GetSpawn(state);
  3769. if (spawn && spawn->IsPlayer()) {
  3770. if (((Player*)spawn)->GetIsTracking())
  3771. spawn->GetZone()->AddPlayerTracking((Player*)spawn);
  3772. else
  3773. spawn->GetZone()->RemovePlayerTracking((Player*)spawn, TRACKING_STOP);
  3774. }
  3775. return 0;
  3776. }
  3777. int EQ2Emu_lua_AddPrimaryEntityCommand(lua_State* state) {
  3778. Spawn* player = lua_interface->GetSpawn(state);
  3779. Spawn* spawn = lua_interface->GetSpawn(state, 2);
  3780. string name = lua_interface->GetStringValue(state, 3);
  3781. float distance = lua_interface->GetFloatValue(state, 4);
  3782. string command = lua_interface->GetStringValue(state, 5);
  3783. string error_text = lua_interface->GetStringValue(state, 6);
  3784. int16 cast_time = lua_interface->GetInt16Value(state, 7);
  3785. int32 spell_visual = lua_interface->GetInt32Value(state, 8);
  3786. bool denyListDefault = (lua_interface->GetInt8Value(state, 9) == 1);
  3787. if (spawn) {
  3788. if (distance == 0)
  3789. distance = 10.0f;
  3790. if (command.length() == 0)
  3791. command = name;
  3792. if (command.length() < 1 && name.length() < 1)
  3793. {
  3794. // have to run this first to send a 'blank' default command, then remove all commands from the list
  3795. spawn->GetZone()->SendUpdateDefaultCommand(spawn, command.c_str(), distance);
  3796. spawn->RemovePrimaryCommands();
  3797. }
  3798. else
  3799. {
  3800. spawn->AddPrimaryEntityCommand(name.c_str(), distance, command.c_str(), error_text.c_str(), cast_time, spell_visual, denyListDefault, (player && player->IsPlayer()) ? (Player*)player : NULL);
  3801. }
  3802. }
  3803. return 0;
  3804. }
  3805. int EQ2Emu_lua_HasSpell(lua_State* state) {
  3806. if (!lua_interface)
  3807. return 0;
  3808. Spawn* player = lua_interface->GetSpawn(state);
  3809. int32 spellid = lua_interface->GetInt32Value(state, 2);
  3810. int16 tier = lua_interface->GetInt16Value(state, 3);
  3811. if (player && player->IsPlayer()) {
  3812. lua_interface->SetBooleanValue(state, ((Player*)player)->HasSpell(spellid, tier, true));
  3813. return 1;
  3814. }
  3815. return 0;
  3816. }
  3817. int EQ2Emu_lua_AddSpellBookEntry(lua_State* state) {
  3818. if (!lua_interface)
  3819. return 0;
  3820. Spawn* player = lua_interface->GetSpawn(state);
  3821. int32 spellid = lua_interface->GetInt32Value(state, 2);
  3822. int16 tier = lua_interface->GetInt16Value(state, 3);
  3823. int8 num_args = (int8)lua_interface->GetNumberOfArgs(state);
  3824. bool add_silently = lua_interface->GetBooleanValue(state, 4);
  3825. bool add_to_hotbar = true;
  3826. if (num_args > 4) {
  3827. add_to_hotbar = lua_interface->GetBooleanValue(state, 5);
  3828. }
  3829. Spell* spell = master_spell_list.GetSpell(spellid, tier);
  3830. if (player && spell && player->IsPlayer()) {
  3831. Client* client = player->GetClient();
  3832. if (client) {
  3833. if (!client->GetPlayer()->HasSpell(spellid, tier - 1, true))
  3834. {
  3835. Spell* spell = master_spell_list.GetSpell(spellid, tier);
  3836. client->GetPlayer()->AddSpellBookEntry(spellid, 1, client->GetPlayer()->GetFreeSpellBookSlot(spell->GetSpellData()->spell_book_type), spell->GetSpellData()->spell_book_type, spell->GetSpellData()->linked_timer, true);
  3837. client->GetPlayer()->UnlockSpell(spell);
  3838. client->SendSpellUpdate(spell, add_silently, add_to_hotbar);
  3839. }
  3840. else
  3841. {
  3842. Spell* spell = master_spell_list.GetSpell(spellid, tier);
  3843. int8 old_slot = client->GetPlayer()->GetSpellSlot(spell->GetSpellID());
  3844. client->GetPlayer()->RemoveSpellBookEntry(spell->GetSpellID());
  3845. client->GetPlayer()->AddSpellBookEntry(spell->GetSpellID(), spell->GetSpellTier(), old_slot, spell->GetSpellData()->spell_book_type, spell->GetSpellData()->linked_timer, true);
  3846. client->GetPlayer()->UnlockSpell(spell);
  3847. client->SendSpellUpdate(spell, add_silently, add_to_hotbar);
  3848. }
  3849. //if (client ) {
  3850. // ((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);
  3851. EQ2Packet* outapp = ((Player*)player)->GetSpellBookUpdatePacket(client->GetVersion());
  3852. if (outapp)
  3853. client->QueuePacket(outapp);
  3854. }
  3855. }
  3856. return 0;
  3857. }
  3858. int EQ2Emu_lua_HasFreeSlot(lua_State* state) {
  3859. if (!lua_interface)
  3860. return 0;
  3861. Spawn* player = lua_interface->GetSpawn(state);
  3862. if (player && player->IsPlayer()) {
  3863. lua_interface->SetBooleanValue(state, ((Player*)player)->item_list.HasFreeSlot());
  3864. return 1;
  3865. }
  3866. return 0;
  3867. }
  3868. int EQ2Emu_lua_Attack(lua_State* state) {
  3869. if (lua_interface) {
  3870. Spawn* npc = lua_interface->GetSpawn(state);
  3871. Spawn* player = lua_interface->GetSpawn(state, 2);
  3872. if (npc && player && npc->IsNPC() && player->IsPlayer())
  3873. ((NPC*)npc)->AddHate((Entity*)player, 100);
  3874. }
  3875. return 0;
  3876. }
  3877. int EQ2Emu_lua_ApplySpellVisual(lua_State* state) {
  3878. if (lua_interface) {
  3879. Spawn* target = lua_interface->GetSpawn(state);
  3880. int32 spell_visual = lua_interface->GetInt32Value(state, 2);
  3881. if (target && target->GetZone())
  3882. target->GetZone()->SendCastSpellPacket(spell_visual, target);
  3883. }
  3884. return 0;
  3885. }
  3886. int EQ2Emu_lua_HasCollectionsToHandIn(lua_State* state) {
  3887. Spawn* player;
  3888. if (lua_interface) {
  3889. player = lua_interface->GetSpawn(state);
  3890. if (player && player->IsPlayer()) {
  3891. lua_interface->SetBooleanValue(state, ((Player*)player)->GetCollectionList()->HasCollectionsToHandIn());
  3892. return 1;
  3893. }
  3894. }
  3895. return 0;
  3896. }
  3897. int EQ2Emu_lua_HandInCollections(lua_State* state) {
  3898. Spawn* player;
  3899. Client* client;
  3900. if (lua_interface) {
  3901. player = lua_interface->GetSpawn(state);
  3902. if (player && ((Player*)player)->IsPlayer() && ((Player*)player)->GetCollectionList()->HasCollectionsToHandIn())
  3903. if ((client = player->GetZone()->GetClientBySpawn(player)))
  3904. client->HandInCollections();
  3905. }
  3906. return 0;
  3907. }
  3908. int EQ2Emu_lua_UseWidget(lua_State* state) {
  3909. Spawn* widget;
  3910. if (lua_interface) {
  3911. widget = lua_interface->GetSpawn(state);
  3912. if (widget && widget->IsWidget())
  3913. ((Widget*)widget)->HandleUse(NULL, "");
  3914. }
  3915. return 0;
  3916. }
  3917. int EQ2Emu_lua_SetSpellList(lua_State* state) {
  3918. Spawn* spawn = 0;
  3919. int32 primary_list = 0;
  3920. int32 secondary_list = 0;
  3921. if (lua_interface) {
  3922. spawn = lua_interface->GetSpawn(state);
  3923. primary_list = lua_interface->GetInt32Value(state, 2);
  3924. secondary_list = lua_interface->GetInt32Value(state, 3);
  3925. if (!spawn->IsNPC()) {
  3926. lua_interface->LogError("%s: LUA SetSpellList command error: Spawn was not a valid NPC", lua_interface->GetScriptName(state));
  3927. return 0;
  3928. }
  3929. NPC* npc = (NPC*)spawn;
  3930. npc->SetPrimarySpellList(primary_list);
  3931. npc->SetSecondarySpellList(secondary_list);
  3932. npc->SetSpells(npc->GetZone()->GetNPCSpells(npc->GetPrimarySpellList(), npc->GetSecondarySpellList()));
  3933. }
  3934. return 0;
  3935. }
  3936. int EQ2Emu_lua_GetPet(lua_State* state) {
  3937. if (!lua_interface)
  3938. return 0;
  3939. Spawn* spawn = lua_interface->GetSpawn(state);
  3940. if (spawn) {
  3941. if (spawn->IsEntity() && ((Entity*)spawn)->GetPet()) {
  3942. lua_interface->SetSpawnValue(state, ((Entity*)spawn)->GetPet());
  3943. return 1;
  3944. }
  3945. }
  3946. return 0;
  3947. }
  3948. int EQ2Emu_lua_GetCharmedPet(lua_State* state) {
  3949. if (!lua_interface)
  3950. return 0;
  3951. Spawn* spawn = lua_interface->GetSpawn(state);
  3952. if (spawn) {
  3953. if (spawn->IsEntity() && ((Entity*)spawn)->GetCharmedPet()) {
  3954. lua_interface->SetSpawnValue(state, ((Entity*)spawn)->GetCharmedPet());
  3955. return 1;
  3956. }
  3957. }
  3958. return 0;
  3959. }
  3960. int EQ2Emu_lua_GetDeityPet(lua_State* state) {
  3961. if (!lua_interface)
  3962. return 0;
  3963. Spawn* spawn = lua_interface->GetSpawn(state);
  3964. if (spawn) {
  3965. if (spawn->IsEntity() && ((Entity*)spawn)->GetDeityPet()) {
  3966. lua_interface->SetSpawnValue(state, ((Entity*)spawn)->GetDeityPet());
  3967. return 1;
  3968. }
  3969. }
  3970. return 0;
  3971. }
  3972. int EQ2Emu_lua_GetCosmeticPet(lua_State* state) {
  3973. if (!lua_interface)
  3974. return 0;
  3975. Spawn* spawn = lua_interface->GetSpawn(state);
  3976. if (spawn) {
  3977. if (spawn->IsEntity() && ((Entity*)spawn)->GetCosmeticPet()) {
  3978. lua_interface->SetSpawnValue(state, ((Entity*)spawn)->GetCosmeticPet());
  3979. return 1;
  3980. }
  3981. }
  3982. return 0;
  3983. }
  3984. int EQ2Emu_lua_Charm(lua_State* state) {
  3985. if (!lua_interface)
  3986. return 0;
  3987. Spawn* owner = lua_interface->GetSpawn(state);
  3988. Spawn* pet = lua_interface->GetSpawn(state, 2);
  3989. LuaSpell* luaspell = lua_interface->GetCurrentSpell(state);
  3990. if (!luaspell) {
  3991. lua_interface->LogError("%s: LUA Charm command error: Spell is not valid, charm can only be used in spell scripts.", lua_interface->GetScriptName(state));
  3992. return 0;
  3993. }
  3994. if (owner && pet && owner->IsEntity() && pet->IsNPC()) {
  3995. ((Entity*)owner)->SetCharmedPet((Entity*)pet);
  3996. pet->SetPet(true);
  3997. ((NPC*)pet)->SetPetType(PET_TYPE_CHARMED);
  3998. ((NPC*)pet)->SetOwner((Entity*)owner);
  3999. // If owner is player and player does not have a summoned pet set the players charsheet
  4000. if (owner->IsPlayer() && !((Entity*)owner)->GetPet()) {
  4001. Player* player = (Player*)owner;
  4002. player->GetInfoStruct()->pet_id = player->GetIDWithPlayerSpawn(pet);
  4003. strcpy(player->GetInfoStruct()->pet_name, pet->GetName());
  4004. player->GetInfoStruct()->pet_movement = 2;
  4005. player->GetInfoStruct()->pet_behavior = 3;
  4006. player->GetInfoStruct()->pet_health_pct = 1.0f;
  4007. player->GetInfoStruct()->pet_power_pct = 1.0f;
  4008. // Make sure the values get sent to the client
  4009. player->SetCharSheetChanged(true);
  4010. }
  4011. // Clear the spawns script so the charmed mob doesn't try to do anything like random walks
  4012. pet->SetSpawnScript("");
  4013. // Set faction to the same as the owner
  4014. pet->SetFactionID(owner->GetFactionID());
  4015. ((NPC*)pet)->SetPetSpellID(luaspell->spell->GetSpellData()->id);
  4016. ((NPC*)pet)->SetPetSpellTier(luaspell->spell->GetSpellData()->tier);
  4017. // Clear hate list
  4018. ((NPC*)pet)->Brain()->ClearHate();
  4019. // Set the brain to a pet brain
  4020. ((NPC*)pet)->SetBrain(new CombatPetBrain((NPC*)pet));
  4021. }
  4022. return 0;
  4023. }
  4024. int EQ2Emu_lua_GetGroup(lua_State* state) {
  4025. if (!lua_interface)
  4026. return 0;
  4027. Spawn* spawn = lua_interface->GetSpawn(state);
  4028. if (!spawn) {
  4029. lua_interface->LogError("%s: LUA GetGroup command error: spawn is not valid", lua_interface->GetScriptName(state));
  4030. return 0;
  4031. }
  4032. vector<Spawn*> groupMembers;
  4033. if (!spawn->IsPlayer() && spawn->HasSpawnGroup()) {
  4034. groupMembers = *spawn->GetSpawnGroup();
  4035. }
  4036. else if (spawn->IsPlayer() && ((Player*)spawn)->GetGroupMemberInfo()) {
  4037. world.GetGroupManager()->GroupLock(__FUNCTION__, __LINE__);
  4038. deque<GroupMemberInfo*>::iterator itr;
  4039. PlayerGroup* group = world.GetGroupManager()->GetGroup(((Player*)spawn)->GetGroupMemberInfo()->group_id);
  4040. if (group)
  4041. {
  4042. group->MGroupMembers.readlock(__FUNCTION__, __LINE__);
  4043. deque<GroupMemberInfo*>* members = group->GetMembers();
  4044. GroupMemberInfo* info = 0;
  4045. for (itr = members->begin(); itr != members->end(); itr++) {
  4046. info = *itr;
  4047. if (info->client)
  4048. groupMembers.push_back(info->client->GetPlayer());
  4049. }
  4050. group->MGroupMembers.releasereadlock(__FUNCTION__, __LINE__);
  4051. }
  4052. world.GetGroupManager()->ReleaseGroupLock(__FUNCTION__, __LINE__);
  4053. }
  4054. else
  4055. return 0;
  4056. lua_createtable(state, groupMembers.size(), 0);
  4057. int newTable = lua_gettop(state);
  4058. for (int32 i = 0; i < groupMembers.size(); i++) {
  4059. lua_interface->SetSpawnValue(state, groupMembers.at(i));
  4060. lua_rawseti(state, newTable, i + 1);
  4061. }
  4062. return 1;
  4063. }
  4064. int EQ2Emu_lua_CreateOptionWindow(lua_State* state) {
  4065. if (!lua_interface)
  4066. return 0;
  4067. vector<OptionWindowOption>* option_window = new vector<OptionWindowOption>();
  4068. lua_interface->SetOptionWindowValue(state, option_window);
  4069. return 1;
  4070. }
  4071. int EQ2Emu_lua_AddOptionWindowOption(lua_State* state) {
  4072. if (!lua_interface)
  4073. return 0;
  4074. vector<OptionWindowOption>* option_window = lua_interface->GetOptionWindow(state);
  4075. if (option_window) {
  4076. OptionWindowOption option_window_option;
  4077. option_window_option.optionName = lua_interface->GetStringValue(state, 2);
  4078. option_window_option.optionDescription = lua_interface->GetStringValue(state, 3);
  4079. option_window_option.optionIconSheet = lua_interface->GetInt32Value(state, 4);
  4080. option_window_option.optionIconID = lua_interface->GetInt16Value(state, 5);
  4081. option_window_option.optionCommand = lua_interface->GetStringValue(state, 6);
  4082. option_window_option.optionConfirmTitle = lua_interface->GetStringValue(state, 7);
  4083. if (option_window_option.optionName.length() > 0 && option_window_option.optionDescription.length() > 0)
  4084. option_window->push_back(option_window_option);
  4085. }
  4086. return 0;
  4087. }
  4088. int EQ2Emu_lua_SendOptionWindow(lua_State* state) {
  4089. if (!lua_interface)
  4090. return 0;
  4091. vector<OptionWindowOption>* option_window = lua_interface->GetOptionWindow(state);
  4092. Spawn* player = lua_interface->GetSpawn(state, 2);
  4093. string window_title = lua_interface->GetStringValue(state, 3);
  4094. string cancel_command = lua_interface->GetStringValue(state, 4);
  4095. Client* client = player->GetZone()->GetClientBySpawn(player);
  4096. if (option_window && window_title.length() > 0 && client) {
  4097. PacketStruct* packet = configReader.getStruct("WS_SelectTradeskill", client->GetVersion());
  4098. if (!packet)
  4099. return 0;
  4100. packet->setDataByName("title_text", window_title.c_str());
  4101. if (cancel_command.length() > 0)
  4102. packet->setDataByName("command_text_cancel", cancel_command.c_str());
  4103. packet->setArrayLengthByName("num_selections", option_window->size());
  4104. vector<OptionWindowOption>::iterator itr;
  4105. int8 i = 0;
  4106. for (itr = option_window->begin(); itr != option_window->end(); itr++) {
  4107. OptionWindowOption opt = *itr;
  4108. packet->setArrayDataByName("tradeskill_name", opt.optionName.c_str(), i);
  4109. packet->setArrayDataByName("tradeskill_description", opt.optionDescription.c_str(), i);
  4110. packet->setArrayDataByName("icon_sheet", opt.optionIconSheet, i);
  4111. packet->setArrayDataByName("icon_id", opt.optionIconID, i);
  4112. if (opt.optionCommand.length() > 0)
  4113. packet->setArrayDataByName("command_text", opt.optionCommand.c_str(), i);
  4114. if (opt.optionConfirmTitle.length() > 0)
  4115. packet->setArrayDataByName("confirm_window_title", opt.optionConfirmTitle.c_str(), i);
  4116. i++;
  4117. }
  4118. client->QueuePacket(packet->serialize());
  4119. safe_delete(option_window);
  4120. safe_delete(packet);
  4121. }
  4122. return 0;
  4123. }
  4124. int EQ2Emu_lua_GetTradeskillClass(lua_State* state) {
  4125. if (!lua_interface)
  4126. return 0;
  4127. Spawn* spawn = lua_interface->GetSpawn(state);
  4128. if (spawn) {
  4129. lua_interface->SetInt32Value(state, spawn->GetTradeskillClass());
  4130. return 1;
  4131. }
  4132. else
  4133. lua_interface->LogError("%s: LUA GetTradeskillClass command error: Spawn was not valid", lua_interface->GetScriptName(state));
  4134. return 0;
  4135. }
  4136. int EQ2Emu_lua_GetTradeskillLevel(lua_State* state) {
  4137. if (!lua_interface)
  4138. return 0;
  4139. Spawn* spawn = lua_interface->GetSpawn(state);
  4140. if (spawn) {
  4141. lua_interface->SetInt32Value(state, spawn->GetTSLevel());
  4142. return 1;
  4143. }
  4144. else
  4145. lua_interface->LogError("%s: LUA GetTradeskillLevel command error: Spawns was not valid", lua_interface->GetScriptName(state));
  4146. return 0;
  4147. }
  4148. int EQ2Emu_lua_GetTradeskillClassName(lua_State* state) {
  4149. if (!lua_interface)
  4150. return 0;
  4151. Spawn* spawn = lua_interface->GetSpawn(state);
  4152. if (spawn) {
  4153. int8 class_id = spawn->GetTradeskillClass();
  4154. // Need to add 42 for the offset in the array
  4155. class_id += 44;
  4156. lua_interface->SetStringValue(state, classes.GetClassNameCase(class_id).c_str());
  4157. return 1;
  4158. }
  4159. else
  4160. lua_interface->LogError("%s: LUA GetTradeskillClassName command error: Spawn was not valid", lua_interface->GetScriptName(state));
  4161. return 0;
  4162. }
  4163. int EQ2Emu_lua_SetTradeskillLevel(lua_State* state) {
  4164. if (!lua_interface)
  4165. return 0;
  4166. Spawn* spawn = lua_interface->GetSpawn(state);
  4167. int16 level = lua_interface->GetInt8Value(state, 2);
  4168. if (spawn) {
  4169. if (spawn->IsPlayer())
  4170. spawn->GetZone()->GetClientBySpawn(spawn)->ChangeTSLevel(spawn->GetTSLevel(), level);
  4171. else
  4172. spawn->SetTSLevel(level);
  4173. }
  4174. else
  4175. lua_interface->LogError("%s: LUA SetTradeskillLevel command error: Spawn was not valid", lua_interface->GetScriptName(state));
  4176. return 0;
  4177. }
  4178. int EQ2Emu_lua_SetAttackable(lua_State* state) {
  4179. if (!lua_interface)
  4180. return 0;
  4181. Spawn* spawn = lua_interface->GetSpawn(state);
  4182. int8 attackable = lua_interface->GetInt8Value(state, 2);
  4183. if (spawn) {
  4184. spawn->SetAttackable(attackable);
  4185. spawn->vis_changed = true; //some clients store this in vis instead of info, need to make sense both are updated
  4186. }
  4187. return 0;
  4188. }
  4189. int EQ2Emu_lua_SummonPet(lua_State* state) {
  4190. // Check to see if we have a valid lua_interface
  4191. if (!lua_interface)
  4192. return 0;
  4193. // Get the spawn that is getting the pet
  4194. Spawn* spawn = lua_interface->GetSpawn(state);
  4195. // Get the DB ID of the pet
  4196. int32 pet_id = lua_interface->GetInt32Value(state, 2);
  4197. // The max level the pet can gain
  4198. int8 max_level = lua_interface->GetInt8Value(state, 3);
  4199. // Get the spell that this command was called from
  4200. LuaSpell* luaspell = lua_interface->GetCurrentSpell(state);
  4201. // Check to make sure the spawn pointer is valid
  4202. if (!spawn) {
  4203. lua_interface->LogError("%s: LUA SummonPet command error: Spawn is not valid", lua_interface->GetScriptName(state));
  4204. return 0;
  4205. }
  4206. // Check to make sure the spawn is an entity
  4207. if (!spawn->IsEntity()) {
  4208. lua_interface->LogError("%s: LUA SummonPet command error: Spawn is not an entity", lua_interface->GetScriptName(state));
  4209. return 0;
  4210. }
  4211. // Check to make sure the spawn doesn't already have a pet of this type
  4212. if (((Entity*)spawn)->GetPet()) {
  4213. if (spawn->IsPlayer()) {
  4214. Client* client = spawn->GetZone()->GetClientBySpawn(spawn);
  4215. if (client)
  4216. client->SimpleMessage(CHANNEL_COLOR_YELLOW, "You already have a pet.");
  4217. }
  4218. lua_interface->LogError("%s: LUA SummonPet command error: spawn already has a pet of this type", lua_interface->GetScriptName(state));
  4219. return 0;
  4220. }
  4221. // Check to see if the DB ID for the pet is set
  4222. if (pet_id == 0) {
  4223. lua_interface->LogError("%s: LUA SummonPet command error: pet_id can not be set to 0", lua_interface->GetScriptName(state));
  4224. return 0;
  4225. }
  4226. // Check to see if the pointer to the spell is valid
  4227. if (!luaspell) {
  4228. lua_interface->LogError("%s: LUA SummonPet command error: valid spell not found, SummonPet can only be used in spell scripts", lua_interface->GetScriptName(state));
  4229. return 0;
  4230. }
  4231. // Get a pointer to a spawn with the given DB ID and check if the pointer is valid
  4232. Spawn* pet = spawn->GetZone()->GetSpawn(pet_id);
  4233. if (!pet) {
  4234. lua_interface->LogError("%s: LUA SummonPet command error: Could not find spawn with id of %u.", lua_interface->GetScriptName(state), pet_id);
  4235. return 0;
  4236. }
  4237. // Check to make sure the pet is an npc
  4238. if (!pet->IsNPC()) {
  4239. lua_interface->LogError("%s: LUA SummonPet command error: id (%u) did not point to a npc", lua_interface->GetScriptName(state), pet_id);
  4240. return 0;
  4241. }
  4242. // Spawn the pet at the same location as the owner
  4243. pet->SetX(spawn->GetX());
  4244. pet->SetY(spawn->GetY());
  4245. pet->SetZ(spawn->GetZ());
  4246. pet->SetLocation(spawn->GetLocation());
  4247. pet->SetHeading(spawn->GetHeading());
  4248. spawn->GetZone()->AddSpawn(pet);
  4249. /*
  4250. const char* spawn_script = world.GetSpawnScript(pet_id);
  4251. if(spawn_script && lua_interface->GetSpawnScript(spawn_script) != 0){
  4252. spawn->SetSpawnScript(string(spawn_script));
  4253. zone->CallSpawnScript(spawn, SPAWN_SCRIPT_SPAWN);
  4254. }*/
  4255. // Get a random pet name
  4256. string random_pet_name;
  4257. int16 rand_index = MakeRandomInt(0, spawn->GetZone()->pet_names.size() - 1);
  4258. random_pet_name = spawn->GetZone()->pet_names.at(rand_index);
  4259. LogWrite(PET__DEBUG, 0, "Pets", "Randomize Pet Name: '%s' (rand: %i)", random_pet_name.c_str(), rand_index);
  4260. // If player set various values for the char sheet (pet window)
  4261. if (spawn->IsPlayer()) {
  4262. Player* player = (Player*)spawn;
  4263. player->GetInfoStruct()->pet_id = player->GetIDWithPlayerSpawn(pet);
  4264. strcpy(player->GetInfoStruct()->pet_name, random_pet_name.c_str());
  4265. player->GetInfoStruct()->pet_movement = 2;
  4266. player->GetInfoStruct()->pet_behavior = 3;
  4267. player->GetInfoStruct()->pet_health_pct = 1.0f;
  4268. player->GetInfoStruct()->pet_power_pct = 1.0f;
  4269. // Make sure the values get sent to the client
  4270. player->SetCharSheetChanged(true);
  4271. }
  4272. // Set the pets name
  4273. pet->SetName(random_pet_name.c_str());
  4274. // Set the level of the pet to the owners level or max level(if set) if owners level is greater
  4275. if (max_level > 0)
  4276. pet->SetLevel(spawn->GetLevel() >= max_level ? max_level : spawn->GetLevel());
  4277. else
  4278. pet->SetLevel(spawn->GetLevel());
  4279. // Set the max level this pet can reach
  4280. ((NPC*)pet)->SetMaxPetLevel(max_level);
  4281. // Set the faction of the pet to the same faction as the owner
  4282. pet->SetFactionID(spawn->GetFactionID());
  4283. // Set the spawn as a pet
  4284. pet->SetPet(true);
  4285. // Give a pointer of the owner to the pet
  4286. ((NPC*)pet)->SetOwner((Entity*)spawn);
  4287. // Give a pointer of the pet to the owner
  4288. ((Entity*)spawn)->SetCombatPet((Entity*)pet);
  4289. // Set the pet type
  4290. ((NPC*)pet)->SetPetType(PET_TYPE_COMBAT);
  4291. // Set the spell id used to create this pet
  4292. ((NPC*)pet)->SetPetSpellID(luaspell->spell->GetSpellData()->id);
  4293. // Set the spell tier used to create this pet
  4294. ((NPC*)pet)->SetPetSpellTier(luaspell->spell->GetSpellData()->tier);
  4295. // Set the pets spawn type to 6
  4296. pet->SetSpawnType(6);
  4297. // Set the pets brain
  4298. ((NPC*)pet)->SetBrain(new CombatPetBrain((NPC*)pet));
  4299. // Check to see if the pet has a subtitle
  4300. if (strlen(pet->GetSubTitle()) > 0) {
  4301. // Add the players name to the front of the sub title
  4302. string pet_subtitle;
  4303. pet_subtitle.append(spawn->GetName()).append("'s ").append(pet->GetSubTitle());
  4304. LogWrite(PET__DEBUG, 0, "Pets", "Pet Subtitle: '%s'", pet_subtitle.c_str());
  4305. // Set the pets subtitle to the new one
  4306. pet->SetSubTitle(pet_subtitle.c_str());
  4307. }
  4308. // Add the "Pet Options" entity command to the pet
  4309. pet->AddSecondaryEntityCommand("Pet Options", 10.0f, "petoptions", "", 0, 0);
  4310. // Set the pet as the return value for this function
  4311. lua_interface->SetSpawnValue(state, pet);
  4312. return 1;
  4313. }
  4314. int EQ2Emu_lua_SummonDeityPet(lua_State* state) {
  4315. if (!lua_interface)
  4316. return 0;
  4317. Spawn* spawn = lua_interface->GetSpawn(state);
  4318. int32 pet_id = lua_interface->GetInt32Value(state, 2);
  4319. LuaSpell* luaspell = lua_interface->GetCurrentSpell(state);
  4320. if (!spawn) {
  4321. lua_interface->LogError("%s: LUA SummonDeityPet command error: Spawn is not valid", lua_interface->GetScriptName(state));
  4322. return 0;
  4323. }
  4324. if (!spawn->IsEntity()) {
  4325. lua_interface->LogError("%s: LUA SummonDeityPet command error: Spawn is not an entity", lua_interface->GetScriptName(state));
  4326. return 0;
  4327. }
  4328. if (((Entity*)spawn)->GetDeityPet()) {
  4329. if (spawn->IsPlayer()) {
  4330. Client* client = spawn->GetZone()->GetClientBySpawn(spawn);
  4331. if (client)
  4332. client->SimpleMessage(CHANNEL_COLOR_YELLOW, "You already have a deity pet.");
  4333. }
  4334. lua_interface->LogError("%s: LUA SummonDeityPet command error: spawn already has a pet of this type", lua_interface->GetScriptName(state));
  4335. return 0;
  4336. }
  4337. if (pet_id == 0) {
  4338. lua_interface->LogError("%s: LUA SummonDeityPet command error: pet_id can not be set to 0", lua_interface->GetScriptName(state));
  4339. return 0;
  4340. }
  4341. if (!luaspell) {
  4342. lua_interface->LogError("%s: LUA SummonDeityPet command error: valid spell not found, SummonDeityPet can only be used in spell scripts", lua_interface->GetScriptName(state));
  4343. return 0;
  4344. }
  4345. Spawn* pet = spawn->GetZone()->GetSpawn(pet_id);
  4346. if (!pet) {
  4347. lua_interface->LogError("%s: LUA SummonDeityPet command error: Could not find spawn with id of %u.", lua_interface->GetScriptName(state), pet_id);
  4348. return 0;
  4349. }
  4350. if (!pet->IsNPC()) {
  4351. lua_interface->LogError("%s: LUA SummonDeityPet command error: spawn with id of %u is not a npc", lua_interface->GetScriptName(state), pet_id);
  4352. return 0;
  4353. }
  4354. pet->SetX(spawn->GetX());
  4355. pet->SetY(spawn->GetY());
  4356. pet->SetZ(spawn->GetZ());
  4357. pet->SetLocation(spawn->GetLocation());
  4358. pet->SetHeading(spawn->GetHeading());
  4359. spawn->GetZone()->AddSpawn(pet);
  4360. string random_pet_name;
  4361. int16 rand_index = MakeRandomInt(0, spawn->GetZone()->pet_names.size() - 1);
  4362. random_pet_name = spawn->GetZone()->pet_names.at(rand_index);
  4363. LogWrite(PET__DEBUG, 0, "Pets", "Randomize Pet Name: '%s' (rand: %i)", random_pet_name.c_str(), rand_index);
  4364. pet->SetName(random_pet_name.c_str());
  4365. pet->SetLevel(spawn->GetLevel());
  4366. pet->SetFactionID(spawn->GetFactionID());
  4367. pet->SetPet(true);
  4368. ((NPC*)pet)->SetPetType(PET_TYPE_DEITY);
  4369. ((NPC*)pet)->SetOwner((Entity*)spawn);
  4370. ((Entity*)spawn)->SetDeityPet((Entity*)pet);
  4371. pet->SetSpawnType(6);
  4372. ((NPC*)pet)->SetBrain(new NonCombatPetBrain((NPC*)pet));
  4373. ((NPC*)pet)->SetPetSpellID(luaspell->spell->GetSpellData()->id);
  4374. ((NPC*)pet)->SetPetSpellTier(luaspell->spell->GetSpellData()->tier);
  4375. if (strlen(pet->GetSubTitle()) > 0) {
  4376. string pet_subtitle;
  4377. pet_subtitle.append(spawn->GetName()).append("'s ").append(pet->GetSubTitle());
  4378. LogWrite(PET__DEBUG, 0, "Pets", "Pet Subtitle: '%s'", pet_subtitle.c_str());
  4379. pet->SetSubTitle(pet_subtitle.c_str());
  4380. }
  4381. // deity and cosmetic pets are not attackable
  4382. pet->SetAttackable(false);
  4383. pet->AddSecondaryEntityCommand("Pet Options", 10.0f, "petoptions", "", 0, 0);
  4384. lua_interface->SetSpawnValue(state, pet);
  4385. return 1;
  4386. }
  4387. int EQ2Emu_lua_SummonCosmeticPet(lua_State* state) {
  4388. if (!lua_interface)
  4389. return 0;
  4390. Spawn* spawn = lua_interface->GetSpawn(state);
  4391. int32 pet_id = lua_interface->GetInt32Value(state, 2);
  4392. LuaSpell* luaspell = lua_interface->GetCurrentSpell(state);
  4393. if (!spawn) {
  4394. lua_interface->LogError("%s: LUA SummonCosmeticPet command error: Spawn is not valid", lua_interface->GetScriptName(state));
  4395. return 0;
  4396. }
  4397. if (!spawn->IsEntity()) {
  4398. lua_interface->LogError("%s: LUA SummonCosmeticPet command error: Spawn is not an entity", lua_interface->GetScriptName(state));
  4399. return 0;
  4400. }
  4401. if (((Entity*)spawn)->GetCosmeticPet()) {
  4402. if (spawn->IsPlayer()) {
  4403. Client* client = spawn->GetZone()->GetClientBySpawn(spawn);
  4404. if (client)
  4405. client->SimpleMessage(CHANNEL_COLOR_YELLOW, "You already have a cosmetic pet.");
  4406. }
  4407. lua_interface->LogError("%s: LUA SummonCosmeticPet command error: spawn already has a pet of this type", lua_interface->GetScriptName(state));
  4408. return 0;
  4409. }
  4410. if (pet_id == 0) {
  4411. lua_interface->LogError("%s: LUA SummonCosmeticPet command error: pet_id can not be set to 0", lua_interface->GetScriptName(state));
  4412. return 0;
  4413. }
  4414. if (!luaspell) {
  4415. lua_interface->LogError("%s: LUA SummonCosmeticPet command error: valid spell not found, SummonCosmeticPet can only be used in spell scripts", lua_interface->GetScriptName(state));
  4416. return 0;
  4417. }
  4418. Spawn* pet = spawn->GetZone()->GetSpawn(pet_id);
  4419. if (!pet) {
  4420. lua_interface->LogError("%s: LUA SummonCosmeticPet command error: Could not find spawn with id of %u.", lua_interface->GetScriptName(state), pet_id);
  4421. return 0;
  4422. }
  4423. if (!pet->IsNPC()) {
  4424. lua_interface->LogError("%s: LUA SummonCosmeticPet command error: spawn with id of %u is not a npc", lua_interface->GetScriptName(state), pet_id);
  4425. return 0;
  4426. }
  4427. pet->SetX(spawn->GetX());
  4428. pet->SetY(spawn->GetY());
  4429. pet->SetZ(spawn->GetZ());
  4430. pet->SetLocation(spawn->GetLocation());
  4431. pet->SetHeading(spawn->GetHeading());
  4432. spawn->GetZone()->AddSpawn(pet);
  4433. string random_pet_name;
  4434. int16 rand_index = MakeRandomInt(0, spawn->GetZone()->pet_names.size() - 1);
  4435. random_pet_name = spawn->GetZone()->pet_names.at(rand_index);
  4436. LogWrite(PET__DEBUG, 0, "Pets", "Randomize Pet Name: '%s' (rand: %i)", random_pet_name.c_str(), rand_index);
  4437. pet->SetName(random_pet_name.c_str());
  4438. pet->SetLevel(spawn->GetLevel());
  4439. pet->SetFactionID(spawn->GetFactionID());
  4440. pet->SetPet(true);
  4441. ((NPC*)pet)->SetPetType(PET_TYPE_COSMETIC);
  4442. ((NPC*)pet)->SetOwner((Entity*)spawn);
  4443. ((Entity*)spawn)->SetCosmeticPet((Entity*)pet);
  4444. pet->SetSpawnType(6);
  4445. ((NPC*)pet)->SetBrain(new NonCombatPetBrain((NPC*)pet));
  4446. ((NPC*)pet)->SetPetSpellID(luaspell->spell->GetSpellData()->id);
  4447. ((NPC*)pet)->SetPetSpellTier(luaspell->spell->GetSpellData()->tier);
  4448. if (strlen(pet->GetSubTitle()) > 0) {
  4449. string pet_subtitle;
  4450. pet_subtitle.append(spawn->GetName()).append("'s ").append(pet->GetSubTitle());
  4451. LogWrite(PET__DEBUG, 0, "Pets", "Pet Subtitle: '%s'", pet_subtitle.c_str());
  4452. pet->SetSubTitle(pet_subtitle.c_str());
  4453. }
  4454. pet->SetAttackable(false);
  4455. pet->AddSecondaryEntityCommand("Pet Options", 10.0f, "petoptions", "", 0, 0);
  4456. lua_interface->SetSpawnValue(state, pet);
  4457. return 1;
  4458. }
  4459. int EQ2Emu_lua_DismissPet(lua_State* state) {
  4460. if (!lua_interface)
  4461. return 0;
  4462. Spawn* spawn = lua_interface->GetSpawn(state);
  4463. if (!spawn) {
  4464. lua_interface->LogError("%s: LUA DismissPet command error: spawn is not valid", lua_interface->GetScriptName(state));
  4465. return 0;
  4466. }
  4467. if (!spawn->IsPet()) {
  4468. lua_interface->LogError("%s: LUA DismissPet command error: spawn is not a pet", lua_interface->GetScriptName(state));
  4469. return 0;
  4470. }
  4471. if (!((NPC*)spawn)->IsDismissing())
  4472. ((NPC*)spawn)->GetOwner()->DismissPet((NPC*)spawn);
  4473. return 0;
  4474. }
  4475. int EQ2Emu_lua_SetQuestFeatherColor(lua_State* state) {
  4476. if (!lua_interface)
  4477. return 0;
  4478. Quest* quest = lua_interface->GetQuest(state);
  4479. if (!quest) {
  4480. 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));
  4481. return 0;
  4482. }
  4483. int8 feather_color = lua_interface->GetInt8Value(state, 2);
  4484. if (feather_color > 0)
  4485. quest->SetFeatherColor(feather_color);
  4486. return 0;
  4487. }
  4488. int EQ2Emu_lua_RemoveSpawnAccess(lua_State* state) {
  4489. if (!lua_interface)
  4490. return 0;
  4491. Spawn* spawn = lua_interface->GetSpawn(state);
  4492. Spawn* spawn2 = lua_interface->GetSpawn(state, 2);
  4493. if (!spawn) {
  4494. lua_interface->LogError("%s: LUA RemoveSpawnAccess command error: first spawn is not valid", lua_interface->GetScriptName(state));
  4495. return 0;
  4496. }
  4497. if (!spawn2) {
  4498. lua_interface->LogError("%s: LUA RemoveSpawnAccess command error: second spawn is not valid", lua_interface->GetScriptName(state));
  4499. return 0;
  4500. }
  4501. spawn->RemoveSpawnAccess(spawn2);
  4502. return 0;
  4503. }
  4504. int EQ2Emu_lua_SpawnByLocationID(lua_State* state) {
  4505. if (!lua_interface)
  4506. return 0;
  4507. ZoneServer* zone = lua_interface->GetZone(state);
  4508. int32 location_id = lua_interface->GetInt32Value(state, 2);
  4509. if (!zone) {
  4510. lua_interface->LogError("%s: LUA SpawnByLocationID command error: zone is not valid", lua_interface->GetScriptName(state));
  4511. return 0;
  4512. }
  4513. if (location_id == 0) {
  4514. lua_interface->LogError("%s: LUA SpawnByLocationID command error: location id can not be 0", lua_interface->GetScriptName(state));
  4515. return 0;
  4516. }
  4517. SpawnLocation* location = zone->GetSpawnLocation(location_id);
  4518. if (!location) {
  4519. lua_interface->LogError("%s: LUA SpawnByLocationID command error: no location found for the given ID (%u)", lua_interface->GetScriptName(state), location_id);
  4520. return 0;
  4521. }
  4522. Spawn* spawn = 0;
  4523. if (location->entities[0]) {
  4524. if (location->entities[0]->spawn_type == SPAWN_ENTRY_TYPE_NPC)
  4525. spawn = zone->AddNPCSpawn(location, location->entities[0]);
  4526. else if (location->entities[0]->spawn_type == SPAWN_ENTRY_TYPE_GROUNDSPAWN)
  4527. spawn = zone->AddGroundSpawn(location, location->entities[0]);
  4528. else if (location->entities[0]->spawn_type == SPAWN_ENTRY_TYPE_OBJECT)
  4529. spawn = zone->AddObjectSpawn(location, location->entities[0]);
  4530. else if (location->entities[0]->spawn_type == SPAWN_ENTRY_TYPE_WIDGET)
  4531. spawn = zone->AddWidgetSpawn(location, location->entities[0]);
  4532. else if (location->entities[0]->spawn_type == SPAWN_ENTRY_TYPE_SIGN)
  4533. spawn = zone->AddSignSpawn(location, location->entities[0]);
  4534. if (spawn) {
  4535. const char* script = 0;
  4536. for (int x = 0; x < 3; x++) {
  4537. switch (x) {
  4538. case 0:
  4539. script = world.GetSpawnEntryScript(location->entities[0]->spawn_entry_id);
  4540. break;
  4541. case 1:
  4542. script = world.GetSpawnLocationScript(location->entities[0]->spawn_location_id);
  4543. break;
  4544. case 2:
  4545. script = world.GetSpawnScript(location->entities[0]->spawn_id);
  4546. break;
  4547. }
  4548. if (script && lua_interface->GetSpawnScript(script) != 0) {
  4549. spawn->SetSpawnScript(string(script));
  4550. break;
  4551. }
  4552. }
  4553. zone->CallSpawnScript(spawn, SPAWN_SCRIPT_SPAWN);
  4554. lua_interface->SetSpawnValue(state, spawn);
  4555. return 1;
  4556. }
  4557. else {
  4558. LogWrite(ZONE__ERROR, 0, "Zone", "Error adding spawn to zone");
  4559. safe_delete(spawn);
  4560. }
  4561. }
  4562. return 0;
  4563. }
  4564. int EQ2Emu_lua_CastEntityCommand(lua_State* state) {
  4565. if (!lua_interface)
  4566. return 0;
  4567. Spawn* caster = lua_interface->GetSpawn(state);
  4568. Spawn* target = lua_interface->GetSpawn(state, 2);
  4569. int32 id = lua_interface->GetInt32Value(state, 3);
  4570. string command = lua_interface->GetStringValue(state, 4);
  4571. if (!caster) {
  4572. lua_interface->LogError("%s: LUA CastEntityCommand command error: caster is not valid", lua_interface->GetScriptName(state));
  4573. return 0;
  4574. }
  4575. if (!target) {
  4576. lua_interface->LogError("%s: LUA CastEntityCommand command error: target is not valid", lua_interface->GetScriptName(state));
  4577. return 0;
  4578. }
  4579. if (!caster->IsPlayer()) {
  4580. lua_interface->LogError("%s: LUA CastEntityCommand command error: caster is not a player", lua_interface->GetScriptName(state));
  4581. return 0;
  4582. }
  4583. EntityCommand* entity_command = caster->GetZone()->GetEntityCommand(id, command);
  4584. if (!entity_command) {
  4585. 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());
  4586. return 0;
  4587. }
  4588. Client* client = caster->GetZone()->GetClientBySpawn(caster);
  4589. if (!client) {
  4590. lua_interface->LogError("%s: LUA CastEntityCommand command error: unable to get a valid client for the given caster", lua_interface->GetScriptName(state));
  4591. return 0;
  4592. }
  4593. client->GetCurrentZone()->ProcessEntityCommand(entity_command, (Player*)caster, target);
  4594. return 0;
  4595. }
  4596. int EQ2Emu_lua_SetLuaBrain(lua_State* state) {
  4597. if (!lua_interface)
  4598. return 0;
  4599. Spawn* spawn = lua_interface->GetSpawn(state);
  4600. if (!spawn) {
  4601. lua_interface->LogError("%s: LUA SetLuaBrain command error: spawn is not valid", lua_interface->GetScriptName(state));
  4602. return 0;
  4603. }
  4604. if (!spawn->IsNPC()) {
  4605. lua_interface->LogError("%s: LUA SetLuaBrain command error: spawn is not a npc", lua_interface->GetScriptName(state));
  4606. return 0;
  4607. }
  4608. ((NPC*)spawn)->SetBrain(new LuaBrain((NPC*)spawn));
  4609. return 0;
  4610. }
  4611. int EQ2Emu_lua_SetBrainTick(lua_State* state) {
  4612. if (!lua_interface)
  4613. return 0;
  4614. Spawn* spawn = lua_interface->GetSpawn(state);
  4615. int16 tick = lua_interface->GetInt16Value(state, 2);
  4616. if (!spawn) {
  4617. lua_interface->LogError("%s: LUA SetBrainTick command error: spawn is not valid", lua_interface->GetScriptName(state));
  4618. return 0;
  4619. }
  4620. if (!spawn->IsNPC()) {
  4621. lua_interface->LogError("%s: LUA SetBrainTick command error: spawn is not a valid npc", lua_interface->GetScriptName(state));
  4622. return 0;
  4623. }
  4624. if (tick < 20) {
  4625. lua_interface->LogError("%s: LUA SetBrainTick command error: tick can not be set below 20 milliseconds", lua_interface->GetScriptName(state));
  4626. return 0;
  4627. }
  4628. ((NPC*)spawn)->Brain()->SetTick(tick);
  4629. return 0;
  4630. }
  4631. int EQ2Emu_lua_SetFollowTarget(lua_State* state) {
  4632. if (!lua_interface)
  4633. return 0;
  4634. Spawn* spawn = lua_interface->GetSpawn(state);
  4635. Spawn* target = lua_interface->GetSpawn(state, 2);
  4636. int32 follow_distance = lua_interface->GetInt32Value(state, 3);
  4637. if (!spawn) {
  4638. lua_interface->LogError("%s: LUA SetFollowTarget command error: spawn is not valid", lua_interface->GetScriptName(state));
  4639. return 0;
  4640. }
  4641. // Target can be null, setting follow target to 0 clears it and will cancel follow, so no need to check it
  4642. spawn->SetFollowTarget(target, follow_distance);
  4643. return 0;
  4644. }
  4645. int EQ2Emu_lua_GetFollowTarget(lua_State* state) {
  4646. if (!lua_interface)
  4647. return 0;
  4648. Spawn* spawn = lua_interface->GetSpawn(state);
  4649. if (!spawn) {
  4650. lua_interface->LogError("%s: LUA GetFollowTarget command error: spawn is not valid", lua_interface->GetScriptName(state));
  4651. return 0;
  4652. }
  4653. Spawn* target = spawn->GetFollowTarget();
  4654. if (target) {
  4655. lua_interface->SetSpawnValue(state, target);
  4656. return 1;
  4657. }
  4658. return 0;
  4659. }
  4660. int EQ2Emu_lua_ToggleFollow(lua_State* state) {
  4661. if (!lua_interface)
  4662. return 0;
  4663. Spawn* spawn = lua_interface->GetSpawn(state);
  4664. if (!spawn) {
  4665. lua_interface->LogError("%s: LUA ToggleFollow command error: spawn is not valid", lua_interface->GetScriptName(state));
  4666. return 0;
  4667. }
  4668. if (spawn->following)
  4669. spawn->following = false;
  4670. else
  4671. spawn->following = true;
  4672. return 0;
  4673. }
  4674. int EQ2Emu_lua_IsFollowing(lua_State* state) {
  4675. if (!lua_interface)
  4676. return 0;
  4677. Spawn* spawn = lua_interface->GetSpawn(state);
  4678. if (!spawn) {
  4679. lua_interface->LogError("%s: LUA IsFollowing command error: spawn is not valid", lua_interface->GetScriptName(state));
  4680. return 0;
  4681. }
  4682. lua_interface->SetBooleanValue(state, spawn->following);
  4683. return 1;
  4684. }
  4685. int EQ2Emu_lua_SetTempVariable(lua_State* state) {
  4686. // As this is unique among the rest of our lua functions as the 3rd param can be of multiple types
  4687. // I will attempt to explain how this function works for future refrence
  4688. // Fist lets make sure lua_interface is valid, if not return out
  4689. if (!lua_interface)
  4690. return 0;
  4691. // Next we grab the first 2 params same as we usually would
  4692. Spawn* spawn = lua_interface->GetSpawn(state);
  4693. string var = lua_interface->GetStringValue(state, 2);
  4694. // 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
  4695. // 1 = Spawn
  4696. // 2 = Zone
  4697. // 3 = Item
  4698. // 4 = Quest
  4699. // 5 = String
  4700. // 6 = nil (null)
  4701. int8 dataType = 0;
  4702. // Define pointers for each potential type
  4703. Spawn* spawnVal = 0;
  4704. ZoneServer* zone = 0;
  4705. Item* item = 0;
  4706. Quest* quest = 0;
  4707. string val;
  4708. // Finally we get to grabbing the third param, we will first check to see if it is light user data
  4709. // which is custom data types, in this case it can be Spawn, Zone, Item, or Quest. Conversation and
  4710. // options window are also light user data be we do not handle those.
  4711. // We check with lua_islightuserdata(lua_State*, index)
  4712. if (lua_islightuserdata(state, 3)) {
  4713. // It is light user data so we will grab the param with lua_touserdata(lua_State*, index)
  4714. // and convert it to LUAUserData*
  4715. LUAUserData* data = (LUAUserData*)lua_touserdata(state, 3);
  4716. // Check to make sure the data we got is valid, if not give an error
  4717. if (!data || !data->IsCorrectlyInitialized()) {
  4718. lua_interface->LogError("%s: LUA SetTempVariable command error while processing %s", lua_interface->GetScriptName(state), lua_tostring(state, -1));
  4719. }
  4720. // Check if data is a Spawn, if so set our Spawn pointer and the dataType variable
  4721. else if (data->IsSpawn()) {
  4722. spawnVal = data->spawn;
  4723. dataType = 1;
  4724. }
  4725. // Check if data is a Zone, if so set our Zone pointer and the dataType variable
  4726. else if (data->IsZone()) {
  4727. zone = data->zone;
  4728. dataType = 2;
  4729. }
  4730. // Check if data is a Item, if so set our Item pointer and the dataType variable
  4731. else if (data->IsItem()) {
  4732. item = data->item;
  4733. dataType = 3;
  4734. }
  4735. // Check if data is a Ques, if so set our Quest pointer and the dataType variable
  4736. else if (data->IsQuest()) {
  4737. quest = data->quest;
  4738. dataType = 4;
  4739. }
  4740. }
  4741. // Wasn't light user data, check if it is nil(null)
  4742. else if (lua_isnil(state, 3)) {
  4743. // It is nil (null) set the dataType variable, no need to set a pointer in this case
  4744. dataType = 6;
  4745. }
  4746. // Wasn't light user data or nil (null), must be a string
  4747. else {
  4748. // Set the string and dataType variable
  4749. val = lua_interface->GetStringValue(state, 3);
  4750. dataType = 5;
  4751. }
  4752. // We now have all the params, lets check to make sure they are valid
  4753. if (!spawn) {
  4754. lua_interface->LogError("%s: LUA SetTempVariable command error: spawn is not valid", lua_interface->GetScriptName(state));
  4755. return 0;
  4756. }
  4757. if (var.length() == 0) {
  4758. lua_interface->LogError("%s: LUA SetTempVariable command error: var must be set", lua_interface->GetScriptName(state));
  4759. return 0;
  4760. }
  4761. if (dataType == 0) {
  4762. lua_interface->LogError("%s: LUA SetTempVariable command error: unknown data type", lua_interface->GetScriptName(state));
  4763. return 0;
  4764. }
  4765. // All params are valid, lets set the spawns temp variable, this is where dataType variable comes in.
  4766. // AddTempVariable has overloads for all the types of data we support, we need to make sure the third
  4767. // param gets sent to the correct list so we check the value of dataType to know where it should go.
  4768. switch (dataType) {
  4769. case 1:
  4770. // 1 = Spawn
  4771. spawn->AddTempVariable(var, spawnVal);
  4772. break;
  4773. case 2:
  4774. // 2 = Zone
  4775. spawn->AddTempVariable(var, zone);
  4776. break;
  4777. case 3:
  4778. // 3 = Item
  4779. spawn->AddTempVariable(var, item);
  4780. break;
  4781. case 4:
  4782. // 4 = Quest
  4783. spawn->AddTempVariable(var, quest);
  4784. break;
  4785. case 5:
  4786. // 5 = String
  4787. spawn->AddTempVariable(var, val);
  4788. break;
  4789. case 6:
  4790. // 6 = nil (null) so the variable is no longer set, lets remove it from the spawn
  4791. spawn->DeleteTempVariable(var);
  4792. break;
  4793. }
  4794. // And we are done so return out
  4795. return 0;
  4796. }
  4797. int EQ2Emu_lua_GetTempVariable(lua_State* state) {
  4798. if (!lua_interface)
  4799. return 0;
  4800. Spawn* spawn = lua_interface->GetSpawn(state);
  4801. string var = lua_interface->GetStringValue(state, 2);
  4802. if (!spawn) {
  4803. lua_interface->LogError("%s: LUA GetTempVariable command error: spawn is not valid", lua_interface->GetScriptName(state));
  4804. return 0;
  4805. }
  4806. if (var.length() == 0) {
  4807. lua_interface->LogError("%s: LUA GetTempVariable command error: var must be set", lua_interface->GetScriptName(state));
  4808. return 0;
  4809. }
  4810. // This will tell us the type of data this variable contains, uses the same values as the previous function
  4811. int8 type = spawn->GetTempVariableType(var);
  4812. Spawn* spawn2 = 0;
  4813. ZoneServer* zone = 0;
  4814. Item* item = 0;
  4815. Quest* quest = 0;
  4816. // Set the lua function return value based on the type of data the variable contains
  4817. switch (type) {
  4818. case 1:
  4819. spawn2 = spawn->GetTempVariableSpawn(var);
  4820. if (!spawn2)
  4821. return 0;
  4822. lua_interface->SetSpawnValue(state, spawn2);
  4823. break;
  4824. case 2:
  4825. zone = spawn->GetTempVariableZone(var);
  4826. if (!zone)
  4827. return 0;
  4828. lua_interface->SetZoneValue(state, zone);
  4829. break;
  4830. case 3:
  4831. item = spawn->GetTempVariableItem(var);
  4832. if (!item)
  4833. return 0;
  4834. lua_interface->SetItemValue(state, item);
  4835. break;
  4836. case 4:
  4837. quest = spawn->GetTempVariableQuest(var);
  4838. if (!quest)
  4839. return 0;
  4840. lua_interface->SetQuestValue(state, quest);
  4841. break;
  4842. case 5:
  4843. lua_interface->SetStringValue(state, spawn->GetTempVariable(var).c_str());
  4844. break;
  4845. default:
  4846. // Not a valid type then the variable was not set so return out
  4847. return 0;
  4848. }
  4849. // Return value was set so return out
  4850. return 1;
  4851. }
  4852. int EQ2Emu_lua_GiveQuestItem(lua_State* state)
  4853. {
  4854. if (!lua_interface)
  4855. return 0;
  4856. Quest* quest = lua_interface->GetQuest(state);
  4857. Spawn* spawn = lua_interface->GetSpawn(state, 2);
  4858. string description = lua_interface->GetStringValue(state, 3);
  4859. int32 item_id = lua_interface->GetInt32Value(state, 4);
  4860. if (!quest) {
  4861. lua_interface->LogError("%s: LUA GiveQuestItem command error: quest is not valid", lua_interface->GetScriptName(state));
  4862. lua_interface->SetBooleanValue(state, false);
  4863. return 1;
  4864. }
  4865. if (!spawn) {
  4866. lua_interface->LogError("%s: LUA GiveQuestItem command error: spawn is not valid", lua_interface->GetScriptName(state));
  4867. lua_interface->SetBooleanValue(state, false);
  4868. return 1;
  4869. }
  4870. if (!spawn->IsPlayer()) {
  4871. lua_interface->LogError("%s: LUA GiveQuestItem command error: spawn must be a player", lua_interface->GetScriptName(state));
  4872. lua_interface->SetBooleanValue(state, false);
  4873. return 1;
  4874. }
  4875. if (item_id == 0) {
  4876. lua_interface->LogError("%s: LUA GiveQuestItem command error: item_id is not valid", lua_interface->GetScriptName(state));
  4877. lua_interface->SetBooleanValue(state, false);
  4878. return 1;
  4879. }
  4880. Client* client = spawn->GetZone()->GetClientBySpawn(spawn);
  4881. if (!client) {
  4882. lua_interface->LogError("%s: LUA GiveQuestItem command error: unable to get a valid client from the given spawn", lua_interface->GetScriptName(state));
  4883. lua_interface->SetBooleanValue(state, false);
  4884. return 1;
  4885. }
  4886. Item* item = master_item_list.GetItem(item_id);
  4887. if (!item) {
  4888. lua_interface->LogError("%s: LUA GiveQuestItem command error: unable to get an item from the given id (%u)", lua_interface->GetScriptName(state), item_id);
  4889. lua_interface->SetBooleanValue(state, false);
  4890. return 1;
  4891. }
  4892. PacketStruct* packet = configReader.getStruct("WS_QuestComplete", client->GetVersion());
  4893. if (packet) {
  4894. packet->setDataByName("title", "Quest Reward!");
  4895. packet->setDataByName("name", quest->GetName());
  4896. packet->setDataByName("description", description.c_str());
  4897. packet->setDataByName("level", quest->GetLevel());
  4898. packet->setArrayLengthByName("num_rewards", 1);
  4899. packet->setArrayDataByName("reward_id", item->details.item_id);
  4900. if (client->GetVersion() < 860)
  4901. packet->setItemArrayDataByName("item", item, (Player*)spawn, 0, 0, -1);
  4902. else if (client->GetVersion() < 1193)
  4903. packet->setItemArrayDataByName("item", item, (Player*)spawn);
  4904. else
  4905. packet->setItemArrayDataByName("item", item, (Player*)spawn, 0, 0, 2);
  4906. client->QueuePacket(packet->serialize());
  4907. safe_delete(packet);
  4908. lua_interface->SetBooleanValue(state, client->AddItem(item_id, 1));
  4909. client->Message(CHANNEL_COLOR_YELLOW, "You receive %s.", item->CreateItemLink(client->GetVersion()).c_str());
  4910. return 1;
  4911. }
  4912. lua_interface->SetBooleanValue(state, false);
  4913. return 1;
  4914. }
  4915. int EQ2Emu_lua_SetQuestRepeatable(lua_State* state) {
  4916. if (!lua_interface)
  4917. return 0;
  4918. Quest* quest = lua_interface->GetQuest(state);
  4919. if (!quest) {
  4920. lua_interface->LogError("%s: LUA SetQuestRepeatable command error: quest is not valid", lua_interface->GetScriptName(state));
  4921. return 0;
  4922. }
  4923. quest->SetRepeatable(true);
  4924. return 0;
  4925. }
  4926. int EQ2Emu_lua_GetArchetypeName(lua_State* state) {
  4927. if (!lua_interface)
  4928. return 0;
  4929. Spawn* spawn = lua_interface->GetSpawn(state);
  4930. if (!spawn) {
  4931. lua_interface->LogError("%s: LUA GetArchetypeName command error: spawn is not valid", lua_interface->GetScriptName(state));
  4932. return 0;
  4933. }
  4934. int8 base_class = classes.GetBaseClass(spawn->GetAdventureClass());
  4935. string ret = classes.GetClassNameCase(base_class);
  4936. if (ret.length() > 0) {
  4937. lua_interface->SetStringValue(state, ret.c_str());
  4938. return 1;
  4939. }
  4940. return 0;
  4941. }
  4942. int EQ2Emu_lua_SendWaypoints(lua_State* state) {
  4943. if (!lua_interface)
  4944. return 0;
  4945. Spawn* player = lua_interface->GetSpawn(state);
  4946. if (player && player->IsPlayer()) {
  4947. Client* client = player->GetClient();
  4948. if (client)
  4949. client->SendWaypoints();
  4950. }
  4951. return 0;
  4952. }
  4953. int EQ2Emu_lua_AddWaypoint(lua_State* state) {
  4954. if (!lua_interface)
  4955. return 0;
  4956. Spawn* player = lua_interface->GetSpawn(state);
  4957. string name = lua_interface->GetStringValue(state, 2);
  4958. int32 type = lua_interface->GetInt32Value(state, 3);
  4959. if (type == 0)
  4960. type = 2;
  4961. if (name.length() > 0) {
  4962. if (player && player->IsPlayer()) {
  4963. Client* client = player->GetClient();
  4964. if (client)
  4965. client->AddWaypoint(name, type);
  4966. }
  4967. }
  4968. return 0;
  4969. }
  4970. int EQ2Emu_lua_RemoveWaypoint(lua_State* state) {
  4971. if (!lua_interface)
  4972. return 0;
  4973. Spawn* player = lua_interface->GetSpawn(state);
  4974. string name = lua_interface->GetStringValue(state, 2);
  4975. if (name.length() > 0) {
  4976. if (player && player->IsPlayer()) {
  4977. Client* client = player->GetClient();
  4978. if (client)
  4979. client->RemoveWaypoint(name);
  4980. }
  4981. }
  4982. return 0;
  4983. }
  4984. int EQ2Emu_lua_AddWard(lua_State* state) {
  4985. if (!lua_interface)
  4986. return 0;
  4987. int32 damage = lua_interface->GetInt32Value(state);
  4988. bool keepWard = (lua_interface->GetInt8Value(state, 2) == 1);
  4989. int8 wardType = lua_interface->GetInt8Value(state, 3);
  4990. int8 damageTypes = lua_interface->GetInt8Value(state, 4);
  4991. int32 damageAbsorptionPercent = lua_interface->GetInt32Value(state, 5);
  4992. int32 damageAbsorptionMaxHealthPercent = lua_interface->GetInt32Value(state, 6);
  4993. int32 redirectDamagePercent = lua_interface->GetInt32Value(state, 7);
  4994. int32 maxHitCount = lua_interface->GetInt32Value(state, 8);
  4995. LuaSpell* spell = lua_interface->GetCurrentSpell(state);
  4996. bool ward_was_added = false;
  4997. ZoneServer* zone = spell->caster->GetZone();
  4998. spell->MSpellTargets.readlock(__FUNCTION__, __LINE__);
  4999. for (int32 i = 0; i < spell->targets.size(); i++) {
  5000. Spawn* target = zone->GetSpawnByID(spell->targets.at(i));
  5001. if (!target)
  5002. continue;
  5003. if (target->IsEntity()) {
  5004. // If the ward is already active remove it
  5005. if (((Entity*)target)->GetWard(spell->spell->GetSpellID()))
  5006. ((Entity*)target)->RemoveWard(spell->spell->GetSpellID());
  5007. // Create new ward info
  5008. WardInfo* ward = new WardInfo;
  5009. ward->Spell = spell;
  5010. ward->BaseDamage = damage;
  5011. ward->DamageLeft = damage;
  5012. ward->AbsorbAllDamage = (damage == 0) ? true : false;
  5013. ward->keepWard = keepWard;
  5014. ward->WardType = wardType;
  5015. if (damageAbsorptionPercent > 100)
  5016. damageAbsorptionPercent = 100;
  5017. ward->DamageAbsorptionPercentage = damageAbsorptionPercent;
  5018. if (damageAbsorptionMaxHealthPercent > 100)
  5019. damageAbsorptionMaxHealthPercent = 100;
  5020. ward->DamageAbsorptionMaxHealthPercent = damageAbsorptionMaxHealthPercent;
  5021. ward->RedirectDamagePercent = redirectDamagePercent;
  5022. ward->LastRedirectDamage = 0;
  5023. ward->LastAbsorbedDamage = 0;
  5024. ward->HitCount = 0;
  5025. spell->num_triggers = maxHitCount;
  5026. spell->had_triggers = true;
  5027. spell->cancel_after_all_triggers = false;
  5028. ward->MaxHitCount = maxHitCount;
  5029. if (wardType == WARD_TYPE_MAGICAL)
  5030. ward->DamageType = damageTypes;
  5031. // Add the ward to the entity
  5032. ((Entity*)target)->AddWard(spell->spell->GetSpellID(), ward);
  5033. ward_was_added = true;
  5034. }
  5035. }
  5036. spell->MSpellTargets.releasereadlock(__FUNCTION__, __LINE__);
  5037. if (ward_was_added && spell->caster->IsPlayer()) {
  5038. spell->had_dmg_remaining = true;
  5039. ClientPacketFunctions::SendMaintainedExamineUpdate(spell->caster->GetZone()->GetClientBySpawn(spell->caster), spell->slot_pos, damage, 1);
  5040. }
  5041. return 0;
  5042. }
  5043. int EQ2Emu_lua_AddToWard(lua_State* state) {
  5044. if (!lua_interface)
  5045. return 0;
  5046. int32 amount = lua_interface->GetInt32Value(state);
  5047. LuaSpell* spell = lua_interface->GetCurrentSpell(state);
  5048. WardInfo* ward = 0;
  5049. ZoneServer* zone = spell->caster->GetZone();
  5050. spell->MSpellTargets.readlock(__FUNCTION__, __LINE__);
  5051. if (zone->GetSpawnByID(spell->targets.at(0))->IsEntity()) {
  5052. Entity* target = (Entity*)zone->GetSpawnByID(spell->targets.at(0));
  5053. ward = target->GetWard(spell->spell->GetSpellID());
  5054. if (ward) {
  5055. ward->DamageLeft += amount;
  5056. if (ward->DamageLeft > ward->BaseDamage)
  5057. ward->DamageLeft = ward->BaseDamage;
  5058. for (int32 i = 0; i < spell->targets.size(); i++) {
  5059. if (Spawn* spawn = zone->GetSpawnByID(spell->targets.at(i)))
  5060. zone->SendHealPacket(ward->Spell->caster, spawn, HEAL_PACKET_TYPE_REGEN_ABSORB, amount, ward->Spell->spell->GetName());
  5061. }
  5062. }
  5063. }
  5064. spell->MSpellTargets.releasereadlock(__FUNCTION__, __LINE__);
  5065. if (ward && spell->caster->IsPlayer())
  5066. ClientPacketFunctions::SendMaintainedExamineUpdate(spell->caster->GetZone()->GetClientBySpawn(spell->caster), spell->slot_pos, ward->DamageLeft, 1);
  5067. return 0;
  5068. }
  5069. int EQ2Emu_lua_GetWardAmountLeft(lua_State* state) {
  5070. if (!lua_interface)
  5071. return 0;
  5072. LuaSpell* spell = lua_interface->GetCurrentSpell(state);
  5073. if (!spell) {
  5074. lua_interface->LogError("%s: LUA GetWardAmountLeft command error: this command can only be used in a spell script", lua_interface->GetScriptName(state));
  5075. return 0;
  5076. }
  5077. if (spell->caster->GetZone()->GetSpawnByID(spell->targets.at(0))->IsEntity()) {
  5078. Entity* target = (Entity*)spell->caster->GetZone()->GetSpawnByID(spell->targets.at(0));
  5079. WardInfo* ward = target->GetWard(spell->spell->GetSpellID());
  5080. if (ward) {
  5081. lua_interface->SetInt32Value(state, ward->DamageLeft);
  5082. return 1;
  5083. }
  5084. }
  5085. return 0;
  5086. }
  5087. int EQ2Emu_lua_GetWardValue(lua_State* state) {
  5088. if (!lua_interface)
  5089. return 0;
  5090. LuaSpell* spell = lua_interface->GetCurrentSpell(state);
  5091. if (!spell) {
  5092. lua_interface->LogError("%s: LUA GetWardValue command error: this command can only be used in a spell script", lua_interface->GetScriptName(state));
  5093. return 0;
  5094. }
  5095. string type = lua_interface->GetStringValue(state, 2);
  5096. if (spell->caster->GetZone()->GetSpawnByID(spell->targets.at(0))->IsEntity()) {
  5097. Entity* target = (Entity*)spell->caster->GetZone()->GetSpawnByID(spell->targets.at(0));
  5098. WardInfo* ward = target->GetWard(spell->spell->GetSpellID());
  5099. if (ward) {
  5100. if (boost::iequals(type, "damageleft"))
  5101. lua_interface->SetInt32Value(state, ward->DamageLeft);
  5102. else if (boost::iequals(type, "basedamage"))
  5103. lua_interface->SetInt32Value(state, ward->BaseDamage);
  5104. else if (boost::iequals(type, "keepward"))
  5105. lua_interface->SetBooleanValue(state, ward->keepWard);
  5106. else if (boost::iequals(type, "wardtype"))
  5107. lua_interface->SetInt32Value(state, ward->WardType);
  5108. else if (boost::iequals(type, "dmgabsorptionpct"))
  5109. lua_interface->SetInt32Value(state, ward->DamageAbsorptionPercentage);
  5110. else if (boost::iequals(type, "dmgabsorptionmaxhealthpct"))
  5111. lua_interface->SetInt32Value(state, ward->DamageAbsorptionMaxHealthPercent);
  5112. else if (boost::iequals(type, "redirectdamagepercent"))
  5113. lua_interface->SetInt32Value(state, ward->RedirectDamagePercent);
  5114. else if (boost::iequals(type, "lastredirectdamage"))
  5115. lua_interface->SetInt32Value(state, ward->LastRedirectDamage);
  5116. else if (boost::iequals(type, "lastabsorbeddamage"))
  5117. lua_interface->SetInt32Value(state, ward->LastAbsorbedDamage);
  5118. else if (boost::iequals(type, "hitcount"))
  5119. lua_interface->SetInt32Value(state, ward->HitCount);
  5120. else if (boost::iequals(type, "maxhitcount"))
  5121. lua_interface->SetInt32Value(state, ward->MaxHitCount);
  5122. else
  5123. lua_interface->LogError("%s: LUA GetWardValue command argument type '%s' did not match any options", lua_interface->GetScriptName(state), type);
  5124. return 1;
  5125. }
  5126. }
  5127. return 0;
  5128. }
  5129. int EQ2Emu_lua_RemoveWard(lua_State* state) {
  5130. if (!lua_interface)
  5131. return 0;
  5132. LuaSpell* spell = lua_interface->GetCurrentSpell(state);
  5133. ZoneServer* zone = spell->caster->GetZone();
  5134. Spawn* target = 0;
  5135. spell->MSpellTargets.readlock(__FUNCTION__, __LINE__);
  5136. for (int32 i = 0; i < spell->targets.size(); i++) {
  5137. target = zone->GetSpawnByID(spell->targets.at(i));
  5138. if (target && target->IsEntity()) {
  5139. ((Entity*)target)->RemoveWard(spell->spell->GetSpellID());
  5140. }
  5141. }
  5142. spell->MSpellTargets.releasereadlock(__FUNCTION__, __LINE__);
  5143. return 0;
  5144. }
  5145. int EQ2Emu_lua_Interrupt(lua_State* state)
  5146. {
  5147. if (!lua_interface)
  5148. return 0;
  5149. Spawn* caster = lua_interface->GetSpawn(state); // Second param in lua_interface->get functions defaults to 1
  5150. Spawn* target = lua_interface->GetSpawn(state, 2);
  5151. LuaSpell* spell = lua_interface->GetCurrentSpell(state);
  5152. if (!caster)
  5153. {
  5154. lua_interface->LogError("%s: LUA Interrupt command error: caster is not a valid spawn", lua_interface->GetScriptName(state));
  5155. return 0;
  5156. }
  5157. if (!target)
  5158. {
  5159. lua_interface->LogError("%s: LUA Interrupt command error: target is not a valid spawn", lua_interface->GetScriptName(state));
  5160. return 0;
  5161. }
  5162. if (!spell) {
  5163. lua_interface->LogError("%s: LUA Interrupt command error: spell is not a valid spawn", lua_interface->GetScriptName(state));
  5164. return 0;
  5165. }
  5166. if (!target->IsEntity() && !spell)
  5167. {
  5168. lua_interface->LogError("%s: LUA Interrupt command error: Target is not an entity", lua_interface->GetScriptName(state));
  5169. return 0;
  5170. }
  5171. if (!target && spell) {
  5172. spell->MSpellTargets.readlock(__FUNCTION__, __LINE__);
  5173. for (int8 i = 0; i < spell->targets.size(); i++) {
  5174. target = caster->GetZone()->GetSpawnByID(spell->targets.at(i));
  5175. if (!target || !target->IsEntity())
  5176. continue;
  5177. ((Entity*)target)->GetZone()->GetSpellProcess()->Interrupted((Entity*)target, caster, SPELL_ERROR_INTERRUPTED);
  5178. }
  5179. spell->MSpellTargets.releasereadlock(__FUNCTION__, __LINE__);
  5180. }
  5181. else
  5182. caster->GetZone()->GetSpellProcess()->Interrupted((Entity*)target, caster, SPELL_ERROR_INTERRUPTED);
  5183. return 0;
  5184. }
  5185. int EQ2Emu_lua_Stealth(lua_State* state) {
  5186. if (!lua_interface)
  5187. return 0;
  5188. int8 type = lua_interface->GetInt8Value(state);
  5189. Spawn* spawn = lua_interface->GetSpawn(state, 2);
  5190. LuaSpell* spell = lua_interface->GetCurrentSpell(state);
  5191. if (!spell) {
  5192. lua_interface->LogError("%s: LUA Stealth command error: must be used from spell script", lua_interface->GetScriptName(state));
  5193. return 0;
  5194. }
  5195. ZoneServer* zone = spell->caster->GetZone();
  5196. if (spawn) {
  5197. if (spawn->IsEntity()) {
  5198. if (type == 1) {
  5199. ((Entity*)spawn)->AddStealthSpell(spell);
  5200. if (!(spell->effect_bitmask & EFFECT_FLAG_STEALTH))
  5201. spell->effect_bitmask += EFFECT_FLAG_STEALTH;
  5202. }
  5203. else if (type == 2) {
  5204. ((Entity*)spawn)->AddInvisSpell(spell);
  5205. if (!(spell->effect_bitmask & EFFECT_FLAG_INVIS))
  5206. spell->effect_bitmask += EFFECT_FLAG_INVIS;
  5207. }
  5208. return 0;
  5209. }
  5210. else {
  5211. lua_interface->LogError("%s: LUA Stealth command error: target override is not Entity", lua_interface->GetScriptName(state));
  5212. return 0;
  5213. }
  5214. }
  5215. else {
  5216. spell->MSpellTargets.readlock(__FUNCTION__, __LINE__);
  5217. for (int32 i = 0; i < spell->targets.size(); i++) {
  5218. spawn = zone->GetSpawnByID(spell->targets.at(i));
  5219. if (!spawn || !spawn->IsEntity())
  5220. continue;
  5221. if (type == 1) {
  5222. ((Entity*)spawn)->AddStealthSpell(spell);
  5223. if (!(spell->effect_bitmask & EFFECT_FLAG_STEALTH))
  5224. spell->effect_bitmask += EFFECT_FLAG_STEALTH;
  5225. }
  5226. else if (type == 2) {
  5227. ((Entity*)spawn)->AddInvisSpell(spell);
  5228. if (!(spell->effect_bitmask & EFFECT_FLAG_INVIS))
  5229. spell->effect_bitmask += EFFECT_FLAG_INVIS;
  5230. }
  5231. else {
  5232. lua_interface->LogError("%s: LUA Stealth command error: invalid stealth type given", lua_interface->GetScriptName(state));
  5233. break;
  5234. }
  5235. }
  5236. spell->MSpellTargets.releasereadlock(__FUNCTION__, __LINE__);
  5237. }
  5238. return 0;
  5239. }
  5240. int EQ2Emu_lua_IsStealthed(lua_State* state) {
  5241. if (!lua_interface)
  5242. return 0;
  5243. Spawn* spawn = lua_interface->GetSpawn(state);
  5244. if (!spawn) {
  5245. lua_interface->LogError("%s: LUA IsStealthed command error: spawn is not valid", lua_interface->GetScriptName(state));
  5246. return 0;
  5247. }
  5248. if (spawn->IsEntity()) {
  5249. lua_interface->SetBooleanValue(state, ((Entity*)spawn)->IsStealthed());
  5250. return 1;
  5251. }
  5252. else
  5253. lua_interface->LogError("%s: LUA IsStealthed command error: spawn is not entity", lua_interface->GetScriptName(state));
  5254. return 0;
  5255. }
  5256. int EQ2Emu_lua_IsInvis(lua_State* state) {
  5257. if (!lua_interface)
  5258. return 0;
  5259. Spawn* spawn = lua_interface->GetSpawn(state);
  5260. if (!spawn) {
  5261. lua_interface->LogError("%s: LUA IsInvis command error: spawn is not valid", lua_interface->GetScriptName(state));
  5262. return 0;
  5263. }
  5264. if (spawn->IsEntity()) {
  5265. lua_interface->SetBooleanValue(state, ((Entity*)spawn)->IsInvis());
  5266. return 1;
  5267. }
  5268. else
  5269. lua_interface->LogError("%s: LUA IsInvis command error: spawn is not entity", lua_interface->GetScriptName(state));
  5270. return 0;
  5271. }
  5272. int EQ2Emu_lua_HasItemEquipped(lua_State* state) {
  5273. if (!lua_interface)
  5274. return 0;
  5275. Spawn* player = lua_interface->GetSpawn(state);
  5276. int32 item_id = lua_interface->GetInt32Value(state, 2);
  5277. if (!player->IsPlayer()) {
  5278. lua_interface->LogError("%s: LUA HasItemEquipped command error: spawn is not player", lua_interface->GetScriptName(state));
  5279. return 0;
  5280. }
  5281. lua_interface->SetBooleanValue(state, ((Player*)player)->GetEquipmentList()->HasItem(item_id));
  5282. return 1;
  5283. }
  5284. int EQ2Emu_lua_GetEquippedItemBySlot(lua_State* state) {
  5285. if (!lua_interface)
  5286. return 0;
  5287. Spawn* player = lua_interface->GetSpawn(state);
  5288. int8 slot = lua_interface->GetInt8Value(state, 2);
  5289. if (!player) {
  5290. lua_interface->LogError("%s: LUA GetEquippedItemBySlot command error: spawn is not valid", lua_interface->GetScriptName(state));
  5291. return 0;
  5292. }
  5293. if (!player->IsPlayer()) {
  5294. lua_interface->LogError("%s: LUA GetEquippedItemBySlot command error: spawn is not player", lua_interface->GetScriptName(state));
  5295. return 0;
  5296. }
  5297. Item* item = ((Player*)player)->GetEquipmentList()->GetItem(slot);
  5298. if (!item) {
  5299. lua_interface->LogError("%s: LUA GetEquippedItemBySlot command error: item was not found in slot", lua_interface->GetScriptName(state));
  5300. return 0;
  5301. }
  5302. lua_interface->SetItemValue(state, item);
  5303. return 1;
  5304. }
  5305. int EQ2Emu_lua_GetEquippedItemByID(lua_State* state) {
  5306. if (!lua_interface)
  5307. return 0;
  5308. Spawn* player = lua_interface->GetSpawn(state);
  5309. int32 id = lua_interface->GetInt32Value(state, 2);
  5310. if (!player) {
  5311. lua_interface->LogError("%s: LUA GetEquippedItemByID command error: spawn is not valid", lua_interface->GetScriptName(state));
  5312. return 0;
  5313. }
  5314. if (!player->IsPlayer()) {
  5315. lua_interface->LogError("%s: LUA GetEquippedItemByID command error: spawn is not player", lua_interface->GetScriptName(state));
  5316. return 0;
  5317. }
  5318. Item* item = ((Player*)player)->GetEquipmentList()->GetItemFromItemID(id);
  5319. if (!item) {
  5320. lua_interface->LogError("%s: LUA GetEquippedItemByID command error: equipped item with used id not found", lua_interface->GetScriptName(state));
  5321. return 0;
  5322. }
  5323. lua_interface->SetItemValue(state, item);
  5324. return 1;
  5325. }
  5326. int EQ2Emu_lua_GetItemByID(lua_State* state) {
  5327. if (!lua_interface)
  5328. return 0;
  5329. Spawn* player = lua_interface->GetSpawn(state);
  5330. int32 id = lua_interface->GetInt32Value(state, 2);
  5331. int8 count = lua_interface->GetInt8Value(state, 3);
  5332. bool include_bank = lua_interface->GetInt8Value(state, 4);
  5333. if (!player) {
  5334. lua_interface->LogError("%s: LUA GetItemByID command error: spawn is not valid", lua_interface->GetScriptName(state));
  5335. return 0;
  5336. }
  5337. if (!player->IsPlayer()) {
  5338. lua_interface->LogError("%s: LUA GetItemByID command error: spawn is not player", lua_interface->GetScriptName(state));
  5339. return 0;
  5340. }
  5341. if (!count)
  5342. count = 1;
  5343. Item* item = ((Player*)player)->GetPlayerItemList()->GetItemFromID(id, count, include_bank);
  5344. if (!item) {
  5345. lua_interface->LogError("%s: LUA GetItemByID command error: item with used id not found", lua_interface->GetScriptName(state));
  5346. return 0;
  5347. }
  5348. lua_interface->SetItemValue(state, item);
  5349. return 1;
  5350. }
  5351. int EQ2Emu_lua_PlayAnimation(lua_State* state) {
  5352. if (!lua_interface)
  5353. return 0;
  5354. Spawn* spawn = lua_interface->GetSpawn(state);
  5355. int32 anim = lua_interface->GetInt32Value(state, 2);
  5356. Spawn* spawn2 = lua_interface->GetSpawn(state, 3);
  5357. int8 type = lua_interface->GetInt8Value(state, 4);
  5358. if (!spawn) {
  5359. lua_interface->LogError("%s: LUA PlayAnimation command error: spawn is not valid", lua_interface->GetScriptName(state));
  5360. return 0;
  5361. }
  5362. if (spawn2) {
  5363. if (spawn2->IsPlayer()) {
  5364. if (type != 1 && type != 2)
  5365. spawn->GetZone()->PlayAnimation(spawn, anim, spawn2);
  5366. else
  5367. spawn->GetZone()->PlayAnimation(spawn, anim, spawn2, type);
  5368. return 0;
  5369. }
  5370. else {
  5371. lua_interface->LogError("%s: LUA PlayAnimation command error: second spawn not a player", lua_interface->GetScriptName(state));
  5372. return 0;
  5373. }
  5374. }
  5375. else
  5376. spawn->GetZone()->PlayAnimation(spawn, anim);
  5377. return 0;
  5378. }
  5379. int EQ2Emu_lua_IsPet(lua_State* state) {
  5380. if (!lua_interface)
  5381. return 0;
  5382. Spawn* spawn = lua_interface->GetSpawn(state);
  5383. if (!spawn) {
  5384. lua_interface->LogError("%s: LUA IsPet command error: spawn is not valid", lua_interface->GetScriptName(state));
  5385. return 0;
  5386. }
  5387. lua_interface->SetBooleanValue(state, spawn->IsPet());
  5388. return 1;
  5389. }
  5390. int EQ2Emu_lua_GetOwner(lua_State* state) {
  5391. if (!lua_interface)
  5392. return 0;
  5393. Spawn* spawn = lua_interface->GetSpawn(state);
  5394. if (!spawn) {
  5395. lua_interface->LogError("%s: LUA GetOwner command error: spawn is not valid", lua_interface->GetScriptName(state));
  5396. return 0;
  5397. }
  5398. if (!spawn->IsNPC()) {
  5399. lua_interface->LogError("%s: LUA GetOwner command error: spawn is not a NPC", lua_interface->GetScriptName(state));
  5400. return 0;
  5401. }
  5402. if (((NPC*)spawn)->GetOwner()) {
  5403. lua_interface->SetSpawnValue(state, ((NPC*)spawn)->GetOwner());
  5404. return 1;
  5405. }
  5406. return 0;
  5407. }
  5408. int EQ2Emu_lua_SetTarget(lua_State* state) {
  5409. if (!lua_interface)
  5410. return 0;
  5411. Spawn* spawn = lua_interface->GetSpawn(state);
  5412. Spawn* target = lua_interface->GetSpawn(state, 2);
  5413. if (!spawn) {
  5414. lua_interface->LogError("%s: LUA SetTarget command error: spawn is not valid", lua_interface->GetScriptName(state));
  5415. return 0;
  5416. }
  5417. if (!spawn) {
  5418. lua_interface->LogError("%s: LUA SetTarget command error: target is not valid", lua_interface->GetScriptName(state));
  5419. return 0;
  5420. }
  5421. spawn->SetTarget(target);
  5422. return 0;
  5423. }
  5424. int EQ2Emu_lua_SetInCombat(lua_State* state) {
  5425. if (!lua_interface)
  5426. return 0;
  5427. Spawn* spawn = lua_interface->GetSpawn(state);
  5428. bool val = lua_interface->GetBooleanValue(state, 2);
  5429. if (!spawn) {
  5430. lua_interface->LogError("%s: LUA SetInCombat command error: spawn is not valid", lua_interface->GetScriptName(state));
  5431. return 0;
  5432. }
  5433. if (!spawn->IsEntity()) {
  5434. lua_interface->LogError("%s: LUA SetInCombat command error: spawn is not an entity", lua_interface->GetScriptName(state));
  5435. return 0;
  5436. }
  5437. ((Entity*)spawn)->InCombat(val);
  5438. if (val) {
  5439. spawn->ClearRunningLocations();
  5440. spawn->CalculateRunningLocation(true);
  5441. }
  5442. return 0;
  5443. }
  5444. int EQ2Emu_lua_CompareSpawns(lua_State* state) {
  5445. if (!lua_interface)
  5446. return 0;
  5447. Spawn* spawn1 = lua_interface->GetSpawn(state);
  5448. Spawn* spawn2 = lua_interface->GetSpawn(state, 2);
  5449. if (!spawn1) {
  5450. lua_interface->LogError("%s: LUA CompareSpawns command error: first spawn is not valid", lua_interface->GetScriptName(state));
  5451. return 0;
  5452. }
  5453. if (!spawn2) {
  5454. lua_interface->LogError("%s: LUA CompareSpawns command error: second spawn is not valid", lua_interface->GetScriptName(state));
  5455. return 0;
  5456. }
  5457. lua_interface->SetBooleanValue(state, (spawn1 == spawn2));
  5458. return 1;
  5459. }
  5460. int EQ2Emu_lua_Runback(lua_State* state) {
  5461. if (!lua_interface)
  5462. return 0;
  5463. Spawn* spawn = lua_interface->GetSpawn(state);
  5464. if (!spawn) {
  5465. lua_interface->LogError("%s: LUA Runback command error: spawn is not valid", lua_interface->GetScriptName(state));
  5466. return 0;
  5467. }
  5468. if (!spawn->IsNPC()) {
  5469. lua_interface->LogError("%s: LUA Runback command error: spawn is not an NPC", lua_interface->GetScriptName(state));
  5470. return 0;
  5471. }
  5472. ((NPC*)spawn)->Runback();
  5473. return 0;
  5474. }
  5475. int EQ2Emu_lua_GetRunbackDistance(lua_State* state) {
  5476. if (!lua_interface)
  5477. return 0;
  5478. Spawn* spawn = lua_interface->GetSpawn(state);
  5479. if (!spawn) {
  5480. lua_interface->LogError("%s: LUA GetRunbackDistance command error: spawn is not valid", lua_interface->GetScriptName(state));
  5481. return 0;
  5482. }
  5483. if (!spawn->IsNPC()) {
  5484. lua_interface->LogError("%s: LUA GetRunbackDistance command error: spawn is not an NPC", lua_interface->GetScriptName(state));
  5485. return 0;
  5486. }
  5487. lua_interface->SetFloatValue(state, ((NPC*)spawn)->GetRunbackDistance());
  5488. return 1;
  5489. }
  5490. int EQ2Emu_lua_IsCasting(lua_State* state) {
  5491. if (!lua_interface)
  5492. return 0;
  5493. Spawn* spawn = lua_interface->GetSpawn(state);
  5494. if (!spawn) {
  5495. lua_interface->LogError("%s: LUA IsCasting command error: spawn is not valid", lua_interface->GetScriptName(state));
  5496. return 0;
  5497. }
  5498. if (!spawn->IsEntity()) {
  5499. lua_interface->LogError("%s: LUA IsCasting command error: spawn is not an entity", lua_interface->GetScriptName(state));
  5500. return 0;
  5501. }
  5502. lua_interface->SetBooleanValue(state, ((Entity*)spawn)->IsCasting());
  5503. return 1;
  5504. }
  5505. int EQ2Emu_lua_IsMezzed(lua_State* state) {
  5506. if (!lua_interface)
  5507. return 0;
  5508. Spawn* spawn = lua_interface->GetSpawn(state);
  5509. if (!spawn) {
  5510. lua_interface->LogError("%s: LUA IsMezzed command error: spawn is not valid", lua_interface->GetScriptName(state));
  5511. return 0;
  5512. }
  5513. if (!spawn->IsEntity()) {
  5514. lua_interface->LogError("%s: LUA IsMezzed command error: spawn is not an entity", lua_interface->GetScriptName(state));
  5515. return 0;
  5516. }
  5517. lua_interface->SetBooleanValue(state, ((Entity*)spawn)->IsMezzed());
  5518. return 1;
  5519. }
  5520. int EQ2Emu_lua_IsStunned(lua_State* state) {
  5521. if (!lua_interface)
  5522. return 0;
  5523. Spawn* spawn = lua_interface->GetSpawn(state);
  5524. if (!spawn) {
  5525. lua_interface->LogError("%s: LUA IsStunned command error: spawn is not valid", lua_interface->GetScriptName(state));
  5526. return 0;
  5527. }
  5528. if (!spawn->IsEntity()) {
  5529. lua_interface->LogError("%s: LUA IsStunned command error: spawn is not an entity", lua_interface->GetScriptName(state));
  5530. return 0;
  5531. }
  5532. lua_interface->SetBooleanValue(state, ((Entity*)spawn)->IsStunned());
  5533. return 1;
  5534. }
  5535. int EQ2Emu_lua_IsMezzedOrStunned(lua_State* state) {
  5536. if (!lua_interface)
  5537. return 0;
  5538. Spawn* spawn = lua_interface->GetSpawn(state);
  5539. if (!spawn) {
  5540. lua_interface->LogError("%s: LUA IsMezzedOrStunned command error: spawn is not valid", lua_interface->GetScriptName(state));
  5541. return 0;
  5542. }
  5543. if (!spawn->IsEntity()) {
  5544. lua_interface->LogError("%s: LUA IsMezzedOrStunned command error: spawn is not an entity", lua_interface->GetScriptName(state));
  5545. return 0;
  5546. }
  5547. lua_interface->SetBooleanValue(state, ((Entity*)spawn)->IsMezzedOrStunned());
  5548. return 1;
  5549. }
  5550. int EQ2Emu_lua_ProcessSpell(lua_State* state) {
  5551. if (!lua_interface)
  5552. return 0;
  5553. Spawn* spawn = lua_interface->GetSpawn(state);
  5554. Spawn* target = lua_interface->GetSpawn(state, 2);
  5555. float distance = lua_interface->GetFloatValue(state, 3);
  5556. if (!spawn) {
  5557. lua_interface->LogError("%s: LUA ProcessSpell command error: spawn is not valid", lua_interface->GetScriptName(state));
  5558. return 0;
  5559. }
  5560. if (!target) {
  5561. lua_interface->LogError("%s: LUA ProcessSpell command error: spawn is not an entity", lua_interface->GetScriptName(state));
  5562. return 0;
  5563. }
  5564. if (!spawn->IsNPC()) {
  5565. lua_interface->LogError("%s: LUA ProcessSpell command error: spawn is not an NPC", lua_interface->GetScriptName(state));
  5566. return 0;
  5567. }
  5568. if (!target->IsEntity()) {
  5569. lua_interface->LogError("%s: LUA ProcessSpell command error: target is not an entity", lua_interface->GetScriptName(state));
  5570. return 0;
  5571. }
  5572. lua_interface->SetBooleanValue(state, ((NPC*)spawn)->Brain()->ProcessSpell(((Entity*)target), distance));
  5573. return 1;
  5574. }
  5575. int EQ2Emu_lua_ProcessMelee(lua_State* state) {
  5576. if (!lua_interface)
  5577. return 0;
  5578. Spawn* spawn = lua_interface->GetSpawn(state);
  5579. Spawn* target = lua_interface->GetSpawn(state, 2);
  5580. float distance = lua_interface->GetFloatValue(state, 3);
  5581. if (!spawn) {
  5582. lua_interface->LogError("%s: LUA ProcessMelee command error: spawn is not valid", lua_interface->GetScriptName(state));
  5583. return 0;
  5584. }
  5585. if (!target) {
  5586. lua_interface->LogError("%s: LUA ProcessMelee command error: target is not valid", lua_interface->GetScriptName(state));
  5587. return 0;
  5588. }
  5589. if (!spawn->IsNPC()) {
  5590. lua_interface->LogError("%s: LUA ProcessMelee command error: spawn is not an NPC", lua_interface->GetScriptName(state));
  5591. return 0;
  5592. }
  5593. if (!target->IsEntity()) {
  5594. lua_interface->LogError("%s: LUA ProcessMelee command error: target is not an entity", lua_interface->GetScriptName(state));
  5595. return 0;
  5596. }
  5597. ((NPC*)spawn)->Brain()->ProcessMelee(((Entity*)target), distance);
  5598. return 0;
  5599. }
  5600. int EQ2Emu_lua_HasRecovered(lua_State* state) {
  5601. if (!lua_interface)
  5602. return 0;
  5603. Spawn* spawn = lua_interface->GetSpawn(state);
  5604. if (!spawn) {
  5605. lua_interface->LogError("%s: LUA HasRecovered command error: spawn is not valid", lua_interface->GetScriptName(state));
  5606. return 0;
  5607. }
  5608. if (!spawn->IsNPC()) {
  5609. lua_interface->LogError("%s: LUA HasRecovered command error: spawn is not an NPC", lua_interface->GetScriptName(state));
  5610. return 0;
  5611. }
  5612. lua_interface->SetBooleanValue(state, ((NPC*)spawn)->Brain()->HasRecovered());
  5613. return 1;
  5614. }
  5615. int EQ2Emu_lua_GetEncounterSize(lua_State* state) {
  5616. if (!lua_interface)
  5617. return 0;
  5618. Spawn* spawn = lua_interface->GetSpawn(state);
  5619. if (!spawn) {
  5620. lua_interface->LogError("%s: LUA GetEncounterSize command error: spawn is not valid", lua_interface->GetScriptName(state));
  5621. return 0;
  5622. }
  5623. if (!spawn->IsNPC()) {
  5624. lua_interface->LogError("%s: LUA GetEncounterSize command error: spawn is not an NPC", lua_interface->GetScriptName(state));
  5625. return 0;
  5626. }
  5627. lua_interface->SetInt32Value(state, ((NPC*)spawn)->Brain()->GetEncounterSize());
  5628. return 1;
  5629. }
  5630. int EQ2Emu_lua_GetMostHated(lua_State* state) {
  5631. if (!lua_interface)
  5632. return 0;
  5633. Spawn* spawn = lua_interface->GetSpawn(state);
  5634. if (!spawn) {
  5635. lua_interface->LogError("%s: LUA GetMostHated command error: spawn is not valid", lua_interface->GetScriptName(state));
  5636. return 0;
  5637. }
  5638. if (!spawn->IsNPC()) {
  5639. lua_interface->LogError("%s: LUA GetMostHated command error: spawn is not an NPC", lua_interface->GetScriptName(state));
  5640. return 0;
  5641. }
  5642. Entity* hated = ((NPC*)spawn)->Brain()->GetMostHated();
  5643. if (hated) {
  5644. lua_interface->SetSpawnValue(state, hated);
  5645. return 1;
  5646. }
  5647. return 0;
  5648. }
  5649. int EQ2Emu_lua_ClearHate(lua_State* state) {
  5650. if (!lua_interface)
  5651. return 0;
  5652. Spawn* spawn = lua_interface->GetSpawn(state);
  5653. Spawn* hated = lua_interface->GetSpawn(state, 2);
  5654. if (!spawn) {
  5655. lua_interface->LogError("%s: LUA ClearHate command error: spawn is not valid", lua_interface->GetScriptName(state));
  5656. return 0;
  5657. }
  5658. if (!spawn->IsNPC()) {
  5659. lua_interface->LogError("%s: LUA ClearHate command error: spawn is not NPC", lua_interface->GetScriptName(state));
  5660. return 0;
  5661. }
  5662. if (!hated) {
  5663. ((NPC*)spawn)->Brain()->ClearHate();
  5664. return 0;
  5665. }
  5666. else
  5667. {
  5668. if (!hated->IsEntity()) {
  5669. lua_interface->LogError("%s: LUA ClearHate command error: second param is not entity", lua_interface->GetScriptName(state));
  5670. return 0;
  5671. }
  5672. ((NPC*)spawn)->Brain()->ClearHate(((Entity*)hated));
  5673. return 0;
  5674. }
  5675. return 0;
  5676. }
  5677. int EQ2Emu_lua_ClearEncounter(lua_State* state) {
  5678. if (!lua_interface)
  5679. return 0;
  5680. Spawn* spawn = lua_interface->GetSpawn(state);
  5681. if (!spawn) {
  5682. lua_interface->LogError("%s: LUA ClearEncounter command error: spawn is not valid", lua_interface->GetScriptName(state));
  5683. return 0;
  5684. }
  5685. if (!spawn->IsNPC()) {
  5686. lua_interface->LogError("%s: LUA ClearEncounter command error: spawn is not an NPC", lua_interface->GetScriptName(state));
  5687. return 0;
  5688. }
  5689. ((NPC*)spawn)->Brain()->ClearEncounter();
  5690. return 0;
  5691. }
  5692. int EQ2Emu_lua_GetEncounter(lua_State* state) {
  5693. if (!lua_interface)
  5694. return 0;
  5695. Spawn* spawn = lua_interface->GetSpawn(state);
  5696. if (!spawn) {
  5697. lua_interface->LogError("%s: LUA GetEncounter command error: Spawn is not valid", lua_interface->GetScriptName(state));
  5698. return 0;
  5699. }
  5700. if (!spawn->IsNPC()) {
  5701. lua_interface->LogError("%s: LUA GetEncounter command error: spawn is not a NPC", lua_interface->GetScriptName(state));
  5702. return 0;
  5703. }
  5704. // Temp list to store hate list
  5705. vector<int32>* encounterList = ((NPC*)spawn)->Brain()->GetEncounter();
  5706. if (encounterList->size() == 0) {
  5707. safe_delete(encounterList);
  5708. return 0;
  5709. }
  5710. lua_createtable(state, encounterList->size(), 0);
  5711. int newTable = lua_gettop(state);
  5712. for (int32 i = 0; i < encounterList->size(); i++) {
  5713. Spawn* temp = spawn->GetZone()->GetSpawnByID(encounterList->at(i));
  5714. if (temp)
  5715. lua_interface->SetSpawnValue(state, temp);
  5716. lua_rawseti(state, newTable, i + 1);
  5717. }
  5718. safe_delete(encounterList);
  5719. return 1;
  5720. }
  5721. int EQ2Emu_lua_GetHateList(lua_State* state) {
  5722. if (!lua_interface)
  5723. return 0;
  5724. Spawn* spawn = lua_interface->GetSpawn(state);
  5725. if (!spawn) {
  5726. lua_interface->LogError("%s: LUA GetHateList command error: spawn is not valid", lua_interface->GetScriptName(state));
  5727. return 0;
  5728. }
  5729. if (!spawn->IsNPC()) {
  5730. lua_interface->LogError("%s: LUA GetHateList command error: spawn is not a NPC", lua_interface->GetScriptName(state));
  5731. return 0;
  5732. }
  5733. // Temp list to store hate list
  5734. vector<Entity*>* hateList = ((NPC*)spawn)->Brain()->GetHateList();
  5735. if (hateList->size() == 0) {
  5736. safe_delete(hateList);
  5737. return 0;
  5738. }
  5739. lua_createtable(state, hateList->size(), 0);
  5740. int newTable = lua_gettop(state);
  5741. for (int32 i = 0; i < hateList->size(); i++) {
  5742. lua_interface->SetSpawnValue(state, hateList->at(i));
  5743. lua_rawseti(state, newTable, i + 1);
  5744. }
  5745. safe_delete(hateList);
  5746. return 1;
  5747. }
  5748. int EQ2Emu_lua_HasGroup(lua_State* state) {
  5749. if (!lua_interface)
  5750. return 0;
  5751. Spawn* spawn = lua_interface->GetSpawn(state);
  5752. if (!spawn) {
  5753. lua_interface->LogError("%s: LUA HasGroup command error: spawn is not valid", lua_interface->GetScriptName(state));
  5754. return 0;
  5755. }
  5756. if (spawn->IsPlayer()) {
  5757. if (((Player*)spawn)->GetGroupMemberInfo() && world.GetGroupManager()->GetGroupSize(((Player*)spawn)->GetGroupMemberInfo()->group_id) > 1)
  5758. lua_interface->SetBooleanValue(state, true);
  5759. else
  5760. lua_interface->SetBooleanValue(state, false);
  5761. return 1;
  5762. }
  5763. else {
  5764. lua_interface->SetBooleanValue(state, spawn->HasSpawnGroup());
  5765. return 1;
  5766. }
  5767. }
  5768. int EQ2Emu_lua_SetCompleteFlag(lua_State* state) {
  5769. if (!lua_interface)
  5770. return 0;
  5771. Quest* quest = lua_interface->GetQuest(state);
  5772. if (!quest) {
  5773. lua_interface->LogError("%s: LUA SetCompleteFlag command error: quest is not valid", lua_interface->GetScriptName(state));
  5774. return 0;
  5775. }
  5776. quest->SetCompletedFlag(true);
  5777. return 0;
  5778. }
  5779. int EQ2Emu_lua_HasSpellEffect(lua_State* state) {
  5780. if (!lua_interface)
  5781. return 0;
  5782. Spawn* spawn = lua_interface->GetSpawn(state);
  5783. int32 spellID = lua_interface->GetInt32Value(state, 2);
  5784. int8 tier = lua_interface->GetInt8Value(state, 3);
  5785. if (!spawn) {
  5786. lua_interface->LogError("%s: LUA HasSpellEffect command error: spawn is not valid", lua_interface->GetScriptName(state));
  5787. return 0;
  5788. }
  5789. if (!spawn->IsEntity()) {
  5790. lua_interface->LogError("%s: LUA HasSpellEffect command error: spawn is not an entity", lua_interface->GetScriptName(state));
  5791. return 0;
  5792. }
  5793. if (spellID == 0) {
  5794. lua_interface->LogError("%s: LUA HasSpellEffect command error: spell id is not valid", lua_interface->GetScriptName(state));
  5795. return 0;
  5796. }
  5797. SpellEffects* effect = ((Entity*)spawn)->GetSpellEffect(spellID);
  5798. if (effect) {
  5799. if (tier > 0) {
  5800. // If a tier was passed chec to see if it is the same as the effect
  5801. if (tier == effect->tier)
  5802. lua_interface->SetBooleanValue(state, true);
  5803. else
  5804. lua_interface->SetBooleanValue(state, false);
  5805. return 1;
  5806. }
  5807. else {
  5808. // Have an effect but no tier was passed so return true
  5809. lua_interface->SetBooleanValue(state, true);
  5810. }
  5811. return 1;
  5812. }
  5813. // no effect so return false
  5814. lua_interface->SetBooleanValue(state, false);
  5815. return 1;
  5816. }
  5817. int EQ2Emu_lua_AddSpawnIDAccess(lua_State* state) {
  5818. if (!lua_interface)
  5819. return 0;
  5820. Spawn* spawn = lua_interface->GetSpawn(state);
  5821. int32 id = lua_interface->GetInt32Value(state, 2);
  5822. ZoneServer* zone = lua_interface->GetZone(state, 3);
  5823. Spawn* spawn2 = 0;
  5824. vector<Spawn*> list;
  5825. if (!spawn) {
  5826. lua_interface->LogError("%s: LUA AddSpawnIDAccess command error: spawn is not valid", lua_interface->GetScriptName(state));
  5827. return 0;
  5828. }
  5829. //If zone not provided, use spawn's zone
  5830. if (!zone)
  5831. zone = spawn->GetZone();
  5832. list = zone->GetSpawnsByID(id);
  5833. if (list.size() == 0) {
  5834. lua_interface->LogError("%s: LUA AddSpawnIDAccess command error: GetSpawnsByID returned no spawns", lua_interface->GetScriptName(state));
  5835. return 0;
  5836. }
  5837. vector<Spawn*>::iterator itr = list.begin();
  5838. for (int8 i = 0; i < list.size(); i++) {
  5839. spawn2 = itr[i];
  5840. if (spawn2)
  5841. spawn2->AddAllowAccessSpawn(spawn);
  5842. }
  5843. return 0;
  5844. }
  5845. int EQ2Emu_lua_RemoveSpawnIDAccess(lua_State* state) {
  5846. if (!lua_interface)
  5847. return 0;
  5848. Spawn* spawn = lua_interface->GetSpawn(state);
  5849. int32 id = lua_interface->GetInt32Value(state, 2);
  5850. ZoneServer* zone = lua_interface->GetZone(state, 3);
  5851. Spawn* spawn2 = 0;
  5852. if (!spawn) {
  5853. lua_interface->LogError("%s: LUA RemoveSpawnIDAccess command error: spawn is not valid", lua_interface->GetScriptName(state));
  5854. return 0;
  5855. }
  5856. //If zone not provided, use spawn's zone
  5857. if (!zone)
  5858. zone = spawn->GetZone();
  5859. vector<Spawn*> list = zone->GetSpawnsByID(id);
  5860. vector<Spawn*>::iterator itr = list.begin();
  5861. if (list.size() == 0) {
  5862. lua_interface->LogError("%s: LUA RemoveSpawnIDAccess command error: GetSpawnsByID returned no spawns", lua_interface->GetScriptName(state));
  5863. return 0;
  5864. }
  5865. for (int8 i = 0; i < list.size(); i++) {
  5866. spawn2 = itr[i];
  5867. if (spawn2)
  5868. spawn2->RemoveSpawnAccess(spawn);
  5869. }
  5870. return 0;
  5871. }
  5872. int EQ2Emu_lua_SetQuestYellow(lua_State* state) {
  5873. if (!lua_interface)
  5874. return 0;
  5875. Quest* quest = lua_interface->GetQuest(state);
  5876. if (!quest) {
  5877. lua_interface->LogError("%s: LUA SetQuestYellow command error: quest is not valid", lua_interface->GetScriptName(state));
  5878. return 0;
  5879. }
  5880. quest->SetYellowName(true);
  5881. return 0;
  5882. }
  5883. int EQ2Emu_lua_CanReceiveQuest(lua_State* state) {
  5884. if (!lua_interface)
  5885. return 0;
  5886. Spawn* spawn = lua_interface->GetSpawn(state);
  5887. int32 quest_id = lua_interface->GetInt32Value(state, 2);
  5888. if (!spawn) {
  5889. lua_interface->LogError("%s: LUA CanReceieveQuest command error: spawn is not valid", lua_interface->GetScriptName(state));
  5890. return 0;
  5891. }
  5892. if (!spawn->IsPlayer()) {
  5893. lua_interface->LogError("%s: LUA CanReceieveQuest command error: spawn is not player", lua_interface->GetScriptName(state));
  5894. return 0;
  5895. }
  5896. lua_interface->SetBooleanValue(state, ((Player*)spawn)->CanReceiveQuest(quest_id));
  5897. return 1;
  5898. }
  5899. int EQ2Emu_lua_SetSuccessTimer(lua_State* state) {
  5900. if (!lua_interface)
  5901. return 0;
  5902. Spawn* spawn = lua_interface->GetSpawn(state);
  5903. if (!spawn) {
  5904. lua_interface->LogError("%s: LUA SetSuccessTimer command error: spawn is not valid", lua_interface->GetScriptName(state));
  5905. return 0;
  5906. }
  5907. if (!spawn->IsPlayer()) {
  5908. lua_interface->LogError("%s: LUA SetSuccessTimer command error: spawn is not a player", lua_interface->GetScriptName(state));
  5909. return 0;
  5910. }
  5911. ZoneServer* zone = spawn->GetZone();
  5912. if (!zone) {
  5913. lua_interface->LogError("%s: LUA SetSuccessTimer command error: unable to get a valid zone for the given spawn", lua_interface->GetScriptName(state));
  5914. return 0;
  5915. }
  5916. Instance_Type iType = zone->GetInstanceType();
  5917. if (iType == SOLO_LOCKOUT_INSTANCE ||
  5918. iType == GROUP_LOCKOUT_INSTANCE ||
  5919. iType == RAID_LOCKOUT_INSTANCE ||
  5920. iType == SOLO_PERSIST_INSTANCE ||
  5921. iType == GROUP_PERSIST_INSTANCE ||
  5922. iType == RAID_PERSIST_INSTANCE) {
  5923. InstanceData* data = ((Player*)spawn)->GetCharacterInstances()->FindInstanceByZoneID(spawn->GetZone()->GetZoneID());
  5924. if (data) {
  5925. // Check to see if the timer has already been set, if it has return out.
  5926. if (Timer::GetUnixTimeStamp() <= data->last_success_timestamp + data->success_lockout_time)
  5927. return 0;
  5928. database.UpdateCharacterInstance(((Player*)spawn)->GetCharacterID(), string(spawn->GetZone()->GetZoneName()), spawn->GetZone()->GetInstanceID(), 1, Timer::GetUnixTimeStamp());
  5929. data->last_success_timestamp = Timer::GetUnixTimeStamp();
  5930. Client* client = spawn->GetZone()->GetClientBySpawn(spawn);
  5931. if (client) {
  5932. string time_msg = "";
  5933. int32 time = data->success_lockout_time;
  5934. int16 hour;
  5935. int8 min;
  5936. int8 sec;
  5937. hour = time / 3600;
  5938. time = time % 3600;
  5939. min = time / 60;
  5940. time = time % 60;
  5941. sec = time;
  5942. if (hour > 0) {
  5943. char temp[10];
  5944. sprintf(temp, " %i", hour);
  5945. time_msg.append(temp);
  5946. time_msg.append(" hour");
  5947. time_msg.append((hour > 1) ? "s" : "");
  5948. }
  5949. if (min > 0) {
  5950. char temp[5];
  5951. sprintf(temp, " %i", min);
  5952. time_msg.append(temp);
  5953. time_msg.append(" minute");
  5954. time_msg.append((min > 1) ? "s" : "");
  5955. }
  5956. // Only add seconds if minutes and hours are 0
  5957. if (hour == 0 && min == 0 && sec > 0) {
  5958. char temp[5];
  5959. sprintf(temp, " %i", sec);
  5960. time_msg.append(temp);
  5961. time_msg.append(" second");
  5962. time_msg.append((sec > 1) ? "s" : "");
  5963. }
  5964. 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());
  5965. }
  5966. }
  5967. else
  5968. lua_interface->LogError("LUA SetSuccessTimer command error: unable to get instance data for instance %u for player %s", spawn->GetZone()->GetInstanceID(), spawn->GetName());
  5969. }
  5970. else
  5971. lua_interface->LogError("%s: LUA SetSuccessTimer command error: current zone for given spawn is not a lockout or persistent instance", lua_interface->GetScriptName(state));
  5972. return 0;
  5973. }
  5974. int EQ2Emu_lua_SetFailureTimer(lua_State* state) {
  5975. if (!lua_interface)
  5976. return 0;
  5977. Spawn* spawn = lua_interface->GetSpawn(state);
  5978. if (!spawn) {
  5979. lua_interface->LogError("%s: LUA SetFailureTimer command error: spawn is not valid", lua_interface->GetScriptName(state));
  5980. return 0;
  5981. }
  5982. if (!spawn->IsPlayer()) {
  5983. lua_interface->LogError("%s: LUA SetFailureTimer command error: spawn is not a player", lua_interface->GetScriptName(state));
  5984. return 0;
  5985. }
  5986. ZoneServer* zone = spawn->GetZone();
  5987. if (!zone) {
  5988. lua_interface->LogError("%s: LUA SetFailureTimer command error: unable to get a valid zone for the given spawn", lua_interface->GetScriptName(state));
  5989. return 0;
  5990. }
  5991. Instance_Type iType = zone->GetInstanceType();
  5992. if (iType == SOLO_LOCKOUT_INSTANCE ||
  5993. iType == GROUP_LOCKOUT_INSTANCE ||
  5994. iType == RAID_LOCKOUT_INSTANCE ||
  5995. iType == SOLO_PERSIST_INSTANCE ||
  5996. iType == GROUP_PERSIST_INSTANCE ||
  5997. iType == RAID_PERSIST_INSTANCE) {
  5998. InstanceData* data = ((Player*)spawn)->GetCharacterInstances()->FindInstanceByZoneID(spawn->GetZone()->GetZoneID());
  5999. if (data) {
  6000. // Check to see if the timer has already been set, if it has return out.
  6001. if (Timer::GetUnixTimeStamp() <= data->last_failure_timestamp + data->failure_lockout_time)
  6002. return 0;
  6003. database.UpdateCharacterInstance(((Player*)spawn)->GetCharacterID(), string(spawn->GetZone()->GetZoneName()), spawn->GetZone()->GetInstanceID(), 2, Timer::GetUnixTimeStamp());
  6004. data->last_failure_timestamp = Timer::GetUnixTimeStamp();
  6005. Client* client = spawn->GetZone()->GetClientBySpawn(spawn);
  6006. if (client) {
  6007. string time_msg = "";
  6008. int32 time = data->failure_lockout_time;
  6009. int16 hour;
  6010. int8 min;
  6011. int8 sec;
  6012. hour = time / 3600;
  6013. time = time % 3600;
  6014. min = time / 60;
  6015. time = time % 60;
  6016. sec = time;
  6017. if (hour > 0) {
  6018. char temp[10];
  6019. sprintf(temp, " %i", hour);
  6020. time_msg.append(temp);
  6021. time_msg.append(" hour");
  6022. time_msg.append((hour > 1) ? "s" : "");
  6023. }
  6024. if (min > 0) {
  6025. char temp[5];
  6026. sprintf(temp, " %i", min);
  6027. time_msg.append(temp);
  6028. time_msg.append(" minute");
  6029. time_msg.append((min > 1) ? "s" : "");
  6030. }
  6031. // Only add seconds if minutes and hours are 0
  6032. if (hour == 0 && min == 0 && sec > 0) {
  6033. char temp[5];
  6034. sprintf(temp, " %i", sec);
  6035. time_msg.append(temp);
  6036. time_msg.append(" second");
  6037. time_msg.append((sec > 1) ? "s" : "");
  6038. }
  6039. 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());
  6040. }
  6041. }
  6042. else
  6043. lua_interface->LogError("LUA SetFailureTimer command error: unable to get instance data for instance %u for player %s", spawn->GetZone()->GetInstanceID(), spawn->GetName());
  6044. }
  6045. else
  6046. lua_interface->LogError("%s: LUA SetFailureTimer command error: current zone for given spawn is not a lockout or persistent instance", lua_interface->GetScriptName(state));
  6047. return 0;
  6048. }
  6049. int EQ2Emu_lua_IsGroundSpawn(lua_State* state) {
  6050. if (!lua_interface)
  6051. return 0;
  6052. Spawn* spawn = lua_interface->GetSpawn(state);
  6053. if (!spawn) {
  6054. lua_interface->LogError("%s: LUA IsGroundSpawn command error: not a valid spawn", lua_interface->GetScriptName(state));
  6055. return 0;
  6056. }
  6057. lua_interface->SetBooleanValue(state, spawn->IsGroundSpawn());
  6058. return 1;
  6059. }
  6060. int EQ2Emu_lua_CanHarvest(lua_State* state) {
  6061. if (!lua_interface)
  6062. return 0;
  6063. Spawn* player = lua_interface->GetSpawn(state);
  6064. Spawn* ground = lua_interface->GetSpawn(state, 2);
  6065. if (!player) {
  6066. lua_interface->LogError("%s: LUA CanHarvest command error: not a valid spawn", lua_interface->GetScriptName(state));
  6067. return 0;
  6068. }
  6069. if (!player->IsPlayer()) {
  6070. lua_interface->LogError("%s: LUA CanHarvest command error: spawn is not a player", lua_interface->GetScriptName(state));
  6071. return 0;
  6072. }
  6073. if (!ground) {
  6074. lua_interface->LogError("%s: LUA CanHarvest command error: not a valid spawn", lua_interface->GetScriptName(state));
  6075. return 0;
  6076. }
  6077. if (!ground->IsGroundSpawn()) {
  6078. lua_interface->LogError("%s: LUA CanHarvest command error: spawn is not a ground spawn", lua_interface->GetScriptName(state));
  6079. return 0;
  6080. }
  6081. vector<GroundSpawnEntry*>* groundspawn_entries = player->GetZone()->GetGroundSpawnEntries(((GroundSpawn*)ground)->GetGroundSpawnEntryID());
  6082. if (!groundspawn_entries) {
  6083. lua_interface->LogError("LUA CanHarvest command error: No groundspawn entries assigned to groundspawn id: %u", ((GroundSpawn*)ground)->GetGroundSpawnEntryID());
  6084. return 0;
  6085. }
  6086. Skill* skill = 0;
  6087. string collection_skill = string(((GroundSpawn*)ground)->GetCollectionSkill());
  6088. if (collection_skill == "Collecting")
  6089. skill = ((Player*)player)->GetSkillByName("Gathering");
  6090. else
  6091. skill = ((Player*)player)->GetSkillByName(collection_skill.c_str());
  6092. if (!skill) {
  6093. lua_interface->LogError("LUA CanHarvest command error: Player '%s' lacks the skill: '%s'", player->GetName(), collection_skill.c_str());
  6094. return 0;
  6095. }
  6096. vector<GroundSpawnEntry*>::iterator itr;
  6097. GroundSpawnEntry* entry = 0;
  6098. bool can_harvest = false;
  6099. sint32 min_skill = -1;
  6100. // first, iterate through groundspawn_entries, discard tables player cannot use
  6101. for (itr = groundspawn_entries->begin(); itr != groundspawn_entries->end(); itr++)
  6102. {
  6103. entry = *itr;
  6104. if (min_skill == -1 || entry->min_skill_level < min_skill)
  6105. min_skill = entry->min_skill_level;
  6106. // if player lacks skill, skip table
  6107. if (entry->min_skill_level > skill->current_val)
  6108. continue;
  6109. // if bonus, but player lacks level, skip table
  6110. if (entry->bonus_table && (player->GetLevel() < entry->min_adventure_level))
  6111. continue;
  6112. can_harvest = true;
  6113. break;
  6114. }
  6115. lua_interface->SetBooleanValue(state, can_harvest);
  6116. // If false, send the message to the client
  6117. if (!can_harvest) {
  6118. Client* client = player->GetZone()->GetClientBySpawn(player);
  6119. if (client) {
  6120. string msg = "You do not have enough skill to ";
  6121. if (collection_skill == "Gathering" || collection_skill == "Collecting")
  6122. msg.append("gather");
  6123. else if (collection_skill == "Mining")
  6124. msg.append("mine");
  6125. else if (collection_skill == "Trapping")
  6126. msg.append("trap");
  6127. else if (collection_skill == "Foresting")
  6128. msg.append("forest");
  6129. else if (collection_skill == "Fishing")
  6130. msg.append("catch");
  6131. msg.append(" the %s. It requires %i %s skill, and your skill is %i.");
  6132. client->Message(CHANNEL_HARVESTING_WARNINGS, msg.c_str(), ground->GetName(), min_skill, skill->name.data.c_str(), skill->current_val);
  6133. // You do not have enough skill to catch the band of fish. It requires 20 Fishing skill, and your skill is 12.
  6134. }
  6135. }
  6136. return 1;
  6137. }
  6138. int EQ2Emu_lua_HasRecipeBook(lua_State* state) {
  6139. if (!lua_interface)
  6140. return 0;
  6141. Spawn* player = lua_interface->GetSpawn(state);
  6142. int32 recipe_id = lua_interface->GetInt32Value(state, 2);
  6143. if (!player) {
  6144. lua_interface->LogError("%s: LUA HasRecipeBook command error, Spawn is not valid", lua_interface->GetScriptName(state));
  6145. return 0;
  6146. }
  6147. if (!player->IsPlayer()) {
  6148. lua_interface->LogError("%s: LUA HasRecipeBook command error, Spawn is not a player", lua_interface->GetScriptName(state));
  6149. return 0;
  6150. }
  6151. bool ret = ((Player*)player)->HasRecipeBook(recipe_id);
  6152. lua_interface->SetBooleanValue(state, ret);
  6153. return 1;
  6154. }
  6155. int EQ2Emu_lua_SummonDumbFirePet(lua_State* state) {
  6156. // Check to see if we have a valid lua_interface
  6157. if (!lua_interface)
  6158. return 0;
  6159. // Get the spawn that is getting the pet
  6160. Spawn* spawn = lua_interface->GetSpawn(state);
  6161. Spawn* target = lua_interface->GetSpawn(state, 2);
  6162. // Get the DB ID of the pet
  6163. int32 pet_id = lua_interface->GetInt32Value(state, 3);
  6164. float x = lua_interface->GetFloatValue(state, 4);
  6165. float y = lua_interface->GetFloatValue(state, 5);
  6166. float z = lua_interface->GetFloatValue(state, 6);
  6167. // Get the spell that this command was called from
  6168. LuaSpell* luaspell = lua_interface->GetCurrentSpell(state);
  6169. // Check to make sure the spawn pointer is valid
  6170. if (!spawn) {
  6171. lua_interface->LogError("%s: LUA SummonDumbFirePet command error: Spawn is not valid", lua_interface->GetScriptName(state));
  6172. return 0;
  6173. }
  6174. // Check to make sure the spawn is an entity
  6175. if (!spawn->IsEntity()) {
  6176. lua_interface->LogError("%s: LUA SummonDumbFirePet command error: Spawn is not an entity", lua_interface->GetScriptName(state));
  6177. return 0;
  6178. }
  6179. if (!target) {
  6180. lua_interface->LogError("%s: LUA SummonDumbFirePet command error: target is not valid", lua_interface->GetScriptName(state));
  6181. return 0;
  6182. }
  6183. if (!target->IsEntity()) {
  6184. lua_interface->LogError("%s: LUA SummonDumbFirePet command error: target is not an entity", lua_interface->GetScriptName(state));
  6185. return 0;
  6186. }
  6187. // Check to see if the DB ID for the pet is set
  6188. if (pet_id == 0) {
  6189. lua_interface->LogError("%s: LUA SummonDumbFirePet command error: pet_id can not be set to 0", lua_interface->GetScriptName(state));
  6190. return 0;
  6191. }
  6192. // Check to see if the pointer to the spell is valid
  6193. if (!luaspell) {
  6194. lua_interface->LogError("%s: LUA SummonDumbFirePet command error: valid spell not found, SummonPet can only be used in spell scripts", lua_interface->GetScriptName(state));
  6195. return 0;
  6196. }
  6197. // Get a pointer to a spawn with the given DB ID and check if the pointer is valid
  6198. Spawn* pet = spawn->GetZone()->GetSpawn(pet_id);
  6199. if (!pet) {
  6200. lua_interface->LogError("LUA SummonDumbFirePet command error: Could not find spawn with id of %u.", pet_id);
  6201. return 0;
  6202. }
  6203. // Check to make sure the pet is an npc
  6204. if (!pet->IsNPC()) {
  6205. lua_interface->LogError("LUA SummonDumbFirePet command error: id (%u) did not point to a npc", pet_id);
  6206. return 0;
  6207. }
  6208. if (x == 0)
  6209. x = spawn->GetX();
  6210. if (y == 0)
  6211. y = spawn->GetY();
  6212. if (z == 0)
  6213. z = spawn->GetZ();
  6214. // Spawn the pet at the same location as the owner
  6215. pet->SetX(x);
  6216. pet->SetY(y);
  6217. pet->SetZ(z);
  6218. pet->SetLocation(spawn->GetLocation());
  6219. pet->SetHeading(spawn->GetHeading());
  6220. spawn->GetZone()->AddSpawn(pet);
  6221. /*
  6222. const char* spawn_script = world.GetSpawnScript(pet_id);
  6223. if(spawn_script && lua_interface->GetSpawnScript(spawn_script) != 0){
  6224. spawn->SetSpawnScript(string(spawn_script));
  6225. zone->CallSpawnScript(spawn, SPAWN_SCRIPT_SPAWN);
  6226. }*/
  6227. // Get a random pet name
  6228. string random_pet_name;
  6229. int16 rand_index = MakeRandomInt(0, spawn->GetZone()->pet_names.size() - 1);
  6230. random_pet_name = spawn->GetZone()->pet_names.at(rand_index);
  6231. LogWrite(PET__DEBUG, 0, "Pets", "Randomize Pet Name: '%s' (rand: %i)", random_pet_name.c_str(), rand_index);
  6232. // Set the pets name
  6233. pet->SetName(random_pet_name.c_str());
  6234. // Set the level of the pet to the owners level
  6235. pet->SetLevel(spawn->GetLevel());
  6236. // Set the faction of the pet to the same faction as the owner
  6237. pet->SetFactionID(spawn->GetFactionID());
  6238. // Set the spawn as a pet
  6239. pet->SetPet(true);
  6240. // Give a pointer of the owner to the pet
  6241. ((NPC*)pet)->SetOwner((Entity*)spawn);
  6242. // Set the pet type
  6243. ((NPC*)pet)->SetPetType(PET_TYPE_DUMBFIRE);
  6244. // Set the spell id used to create this pet
  6245. ((NPC*)pet)->SetPetSpellID(luaspell->spell->GetSpellData()->id);
  6246. // Set the spell tier used to create this pet
  6247. ((NPC*)pet)->SetPetSpellTier(luaspell->spell->GetSpellData()->tier);
  6248. // Set the pets spawn type to 6
  6249. pet->SetSpawnType(6);
  6250. // Set the pets brain
  6251. ((NPC*)pet)->SetBrain(new DumbFirePetBrain((NPC*)pet, (Entity*)target, luaspell->spell->GetSpellDuration() * 100));
  6252. // Check to see if the pet has a subtitle
  6253. if (strlen(pet->GetSubTitle()) > 0) {
  6254. // Add the players name to the front of the sub title
  6255. string pet_subtitle;
  6256. pet_subtitle.append(spawn->GetName()).append("'s ").append(pet->GetSubTitle());
  6257. LogWrite(PET__DEBUG, 0, "Pets", "Pet Subtitle: '%s'", pet_subtitle.c_str());
  6258. // Set the pets subtitle to the new one
  6259. pet->SetSubTitle(pet_subtitle.c_str());
  6260. }
  6261. // Set the pet as the return value for this function
  6262. lua_interface->SetSpawnValue(state, pet);
  6263. return 1;
  6264. }
  6265. int EQ2Emu_lua_SpawnMove(lua_State* state) {
  6266. if (!lua_interface)
  6267. return 0;
  6268. Spawn* spawn = lua_interface->GetSpawn(state);
  6269. Spawn* player = lua_interface->GetSpawn(state, 2);
  6270. float max_distance = lua_interface->GetFloatValue(state, 3);
  6271. string type = lua_interface->GetStringValue(state, 4);
  6272. if (!spawn || (spawn && spawn->IsPlayer())) {
  6273. lua_interface->LogError("%s: LUA SpawnMove command error: first param spawn is not valid or is player", lua_interface->GetScriptName(state));
  6274. return 0;
  6275. }
  6276. if (!player || (player && !player->IsPlayer())) {
  6277. lua_interface->LogError("%s: LUA SpawnMove command error: second param is not player", lua_interface->GetScriptName(state));
  6278. return 0;
  6279. }
  6280. Client* client = 0;
  6281. if (player->GetZone())
  6282. client = player->GetZone()->GetClientBySpawn(player);
  6283. if (!client) {
  6284. lua_interface->LogError("%s: LUA SpawnMove command error: could not find client", lua_interface->GetScriptName(state));
  6285. return 0;
  6286. }
  6287. //Set max_distance to default if not set or not proper value
  6288. if (max_distance <= 0)
  6289. max_distance = 500;
  6290. PacketStruct* packet = configReader.getStruct("WS_MoveObjectMode", client->GetVersion());
  6291. if (packet) {
  6292. float unknown2_3 = 0;
  6293. int8 placement_mode = 0;
  6294. if (type == "wall") {
  6295. placement_mode = 2;
  6296. unknown2_3 = 150;
  6297. }
  6298. else if (type == "ceiling")
  6299. placement_mode = 1;
  6300. packet->setDataByName("placement_mode", placement_mode);
  6301. packet->setDataByName("spawn_id", ((Player*)player)->GetIDWithPlayerSpawn(spawn));
  6302. packet->setDataByName("model_type", spawn->GetModelType());
  6303. packet->setDataByName("unknown", 1); //size
  6304. packet->setDataByName("unknown2", 1); //size 2
  6305. packet->setDataByName("unknown2", .5, 1); //size 3
  6306. packet->setDataByName("unknown2", 3, 2);
  6307. packet->setDataByName("unknown2", unknown2_3, 3);
  6308. packet->setDataByName("max_distance", max_distance);
  6309. packet->setDataByName("CoEunknown", 0xFFFFFFFF);
  6310. client->QueuePacket(packet->serialize());
  6311. safe_delete(packet);
  6312. }
  6313. return 0;
  6314. }
  6315. int EQ2Emu_lua_GetItemType(lua_State* state) {
  6316. if (!lua_interface)
  6317. return 0;
  6318. Item* item = lua_interface->GetItem(state);
  6319. if (!item) {
  6320. lua_interface->LogError("%s: LUA GetItemType command error: item pointer is not valid", lua_interface->GetScriptName(state));
  6321. return 0;
  6322. }
  6323. lua_interface->SetInt32Value(state, item->generic_info.item_type);
  6324. return 1;
  6325. }
  6326. int EQ2Emu_lua_AddTransportSpawn(lua_State* state) {
  6327. if (!lua_interface)
  6328. return 0;
  6329. Spawn* spawn = lua_interface->GetSpawn(state);
  6330. if (!spawn) {
  6331. lua_interface->LogError("%s: LUA AddTransportSpawn command error: spawn is not valid", lua_interface->GetScriptName(state));
  6332. return 0;
  6333. }
  6334. if (spawn->GetZone())
  6335. spawn->GetZone()->AddTransportSpawn(spawn);
  6336. return 0;
  6337. }
  6338. int EQ2Emu_lua_GetSkillValue(lua_State* state) {
  6339. if (!lua_interface)
  6340. return 0;
  6341. Skill* skill = lua_interface->GetSkill(state);
  6342. if (!skill) {
  6343. lua_interface->LogError("%s: LUA GetSkillValue command error: skill is not valid", lua_interface->GetScriptName(state));
  6344. return 0;
  6345. }
  6346. lua_interface->SetInt32Value(state, skill->current_val);
  6347. return 1;
  6348. }
  6349. int EQ2Emu_lua_GetSkillMaxValue(lua_State* state) {
  6350. if (!lua_interface)
  6351. return 0;
  6352. Skill* skill = lua_interface->GetSkill(state);
  6353. if (!skill) {
  6354. lua_interface->LogError("%s: LUA GetSkillMaxValue command error: skill is not valid", lua_interface->GetScriptName(state));
  6355. return 0;
  6356. }
  6357. lua_interface->SetInt32Value(state, skill->max_val);
  6358. return 1;
  6359. }
  6360. int EQ2Emu_lua_GetSkillName(lua_State* state) {
  6361. if (!lua_interface)
  6362. return 0;
  6363. Skill* skill = lua_interface->GetSkill(state);
  6364. if (!skill) {
  6365. lua_interface->LogError("%s: LUA GetSkillName command error: skill is not valid", lua_interface->GetScriptName(state));
  6366. return 0;
  6367. }
  6368. lua_interface->SetStringValue(state, skill->name.data.c_str());
  6369. return 1;
  6370. }
  6371. int EQ2Emu_lua_SetSkillMaxValue(lua_State* state) {
  6372. if (!lua_interface)
  6373. return 0;
  6374. Skill* skill = lua_interface->GetSkill(state);
  6375. int16 value = lua_interface->GetInt16Value(state, 2);
  6376. if (!skill) {
  6377. lua_interface->LogError("%s: LUA SetSkillMaxValue command error: skill is not valid", lua_interface->GetScriptName(state));
  6378. return 0;
  6379. }
  6380. skill->max_val = value;
  6381. if (skill->max_val < skill->current_val)
  6382. skill->current_val = skill->max_val;
  6383. return 0;
  6384. }
  6385. int EQ2Emu_lua_SetSkillValue(lua_State* state) {
  6386. if (!lua_interface)
  6387. return 0;
  6388. Skill* skill = lua_interface->GetSkill(state);
  6389. int16 value = lua_interface->GetInt16Value(state, 2);
  6390. if (!skill) {
  6391. lua_interface->LogError("%s: LUA SetSkillValue command error: skill is not valid", lua_interface->GetScriptName(state));
  6392. return 0;
  6393. }
  6394. if (value > skill->max_val)
  6395. skill->current_val = skill->max_val;
  6396. else
  6397. skill->current_val = value;
  6398. return 0;
  6399. }
  6400. int EQ2Emu_lua_HasSkill(lua_State* state) {
  6401. if (!lua_interface)
  6402. return 0;
  6403. Spawn* player = lua_interface->GetSpawn(state);
  6404. int32 skill_id = lua_interface->GetInt32Value(state, 2);
  6405. if (skill_id > 0 && player && player->IsPlayer()) {
  6406. lua_interface->SetBooleanValue(state, ((Player*)player)->skill_list.HasSkill(skill_id));
  6407. return 1;
  6408. }
  6409. return 0;
  6410. }
  6411. int EQ2Emu_lua_AddSkill(lua_State* state) {
  6412. if (!lua_interface)
  6413. return 0;
  6414. Spawn* player_spawn = lua_interface->GetSpawn(state);
  6415. int32 skill_id = lua_interface->GetInt32Value(state, 2);
  6416. int16 current_val = lua_interface->GetInt16Value(state, 3);
  6417. int16 max_val = lua_interface->GetInt16Value(state, 4);
  6418. bool more_to_add = lua_interface->GetBooleanValue(state, 5);
  6419. if (skill_id > 0 && current_val > 0 && max_val > 0) {
  6420. if (player_spawn && player_spawn->IsPlayer()) {
  6421. Player* player = (Player*)player_spawn;
  6422. bool added = false;
  6423. if (!player->skill_list.HasSkill(skill_id)) {
  6424. player->AddSkill(skill_id, current_val, max_val, true);
  6425. added = true;
  6426. }
  6427. if (!more_to_add) { //need to send update regardless, even if THIS skill wasn't added, otherwise if you have a list and the last item wasn't added but the previous ones were, it wouldn't send the update
  6428. Client* client = player->GetClient();
  6429. if (client) {
  6430. EQ2Packet* packet = player->GetSkills()->GetSkillPacket(client->GetVersion());
  6431. if (packet)
  6432. client->QueuePacket(packet);
  6433. }
  6434. }
  6435. if (added) {
  6436. lua_interface->SetBooleanValue(state, true);
  6437. return 1;
  6438. }
  6439. }
  6440. else {
  6441. lua_interface->LogError("%s: LUA AddSkill command error: Given spawn is not a player", lua_interface->GetScriptName(state));
  6442. }
  6443. }
  6444. else {
  6445. lua_interface->LogError("%s: LUA AddSkill command error: Required parameters not set", lua_interface->GetScriptName(state));
  6446. }
  6447. lua_interface->SetBooleanValue(state, false);
  6448. return 1;
  6449. }
  6450. int EQ2Emu_lua_RemoveSkill(lua_State* state) {
  6451. if (!lua_interface)
  6452. return 0;
  6453. Spawn* player_spawn = lua_interface->GetSpawn(state);
  6454. int32 skill_id = lua_interface->GetInt32Value(state, 2);
  6455. bool more_to_remove = lua_interface->GetBooleanValue(state, 3);
  6456. if (skill_id > 0) {
  6457. if (player_spawn && player_spawn->IsPlayer()) {
  6458. Player* player = (Player*)player_spawn;
  6459. if (player->skill_list.HasSkill(skill_id)) {
  6460. player->RemovePlayerSkill(skill_id);
  6461. if (!more_to_remove) {
  6462. Client* client = player->GetClient();
  6463. if (client) {
  6464. EQ2Packet* packet = player->GetSkills()->GetSkillPacket(client->GetVersion());
  6465. if (packet)
  6466. client->QueuePacket(packet);
  6467. }
  6468. }
  6469. }
  6470. }
  6471. else {
  6472. lua_interface->LogError("%s: LUA RemoveSkill command error: Given spawn is not a player", lua_interface->GetScriptName(state));
  6473. }
  6474. }
  6475. else {
  6476. lua_interface->LogError("%s: LUA RemoveSkill command error: skill_id not set", lua_interface->GetScriptName(state));
  6477. }
  6478. return 0;
  6479. }
  6480. int EQ2Emu_lua_IncreaseSkillCapsByType(lua_State* state) {
  6481. if (!lua_interface)
  6482. return 0;
  6483. Spawn* player_spawn = lua_interface->GetSpawn(state);
  6484. int8 skill_type = lua_interface->GetInt8Value(state, 2);
  6485. int16 amount = lua_interface->GetInt8Value(state, 3);
  6486. bool more_to_increase = lua_interface->GetBooleanValue(state, 4);
  6487. if (amount > 0 && skill_type < 100) {
  6488. if (player_spawn && player_spawn->IsPlayer()) {
  6489. Player* player = (Player*)player_spawn;
  6490. player->skill_list.IncreaseSkillCapsByType(skill_type, amount);
  6491. if (!more_to_increase) {
  6492. Client* client = player->GetClient();
  6493. if (client) {
  6494. EQ2Packet* packet = player->GetSkills()->GetSkillPacket(client->GetVersion());
  6495. if (packet)
  6496. client->QueuePacket(packet);
  6497. }
  6498. }
  6499. }
  6500. else {
  6501. lua_interface->LogError("%s: LUA IncreaseSkillCapsByType command error: Given spawn is not a player", lua_interface->GetScriptName(state));
  6502. }
  6503. }
  6504. else {
  6505. lua_interface->LogError("%s: LUA IncreaseSkillCapsByType command error: Invalid parameters", lua_interface->GetScriptName(state));
  6506. }
  6507. return 0;
  6508. }
  6509. int EQ2Emu_lua_GetSkill(lua_State* state) {
  6510. if (!lua_interface)
  6511. return 0;
  6512. Spawn* spawn = lua_interface->GetSpawn(state);
  6513. string name = lua_interface->GetStringValue(state, 2);
  6514. if (!spawn) {
  6515. lua_interface->LogError("%s: LUA GetSkill command error: spawn is not valid", lua_interface->GetScriptName(state));
  6516. return 0;
  6517. }
  6518. if (!spawn->IsEntity()) {
  6519. lua_interface->LogError("%s: LUA GetSkill command error: spawn is not a valid entity", lua_interface->GetScriptName(state));
  6520. return 0;
  6521. }
  6522. Skill* skill = ((Entity*)spawn)->GetSkillByName(name.c_str());
  6523. if (skill) {
  6524. lua_interface->SetSkillValue(state, skill);
  6525. return 1;
  6526. }
  6527. return 0;
  6528. }
  6529. int EQ2Emu_lua_AddProc(lua_State* state) {
  6530. if (!lua_interface)
  6531. return 0;
  6532. Spawn* spawn = lua_interface->GetSpawn(state);
  6533. int8 type = lua_interface->GetInt8Value(state, 2);
  6534. float chance = lua_interface->GetFloatValue(state, 3);
  6535. Item* item = lua_interface->GetItem(state, 4);
  6536. bool use_all_spelltargets = (lua_interface->GetInt8Value(state, 5) == 1);
  6537. LuaSpell* spell = 0;
  6538. if (!spawn && (!spell || !use_all_spelltargets)) {
  6539. lua_interface->LogError("%s: LUA AddProc command error: spawn is not valid", lua_interface->GetScriptName(state));
  6540. return 0;
  6541. }
  6542. if ((!spell || use_all_spelltargets) && spawn && !spawn->IsEntity()) {
  6543. lua_interface->LogError("%s: LUA AddProc command error: spawn is not a valid entity", lua_interface->GetScriptName(state));
  6544. return 0;
  6545. }
  6546. if (!item)
  6547. spell = lua_interface->GetCurrentSpell(state);
  6548. if (!item && !spell) {
  6549. lua_interface->LogError("%s: LUA AddProc command error: can only use with an item provided or inside a spell script", lua_interface->GetScriptName(state));
  6550. return 0;
  6551. }
  6552. if (spell && use_all_spelltargets) {
  6553. Spawn* target;
  6554. spell->MSpellTargets.readlock(__FUNCTION__, __LINE__);
  6555. for (int8 i = 0; i < spell->targets.size(); i++) {
  6556. target = spell->caster->GetZone()->GetSpawnByID(spell->targets.at(i));
  6557. if (!target || !target->IsEntity())
  6558. continue;
  6559. ((Entity*)target)->AddProc(type, chance, item, spell);
  6560. }
  6561. spell->MSpellTargets.releasereadlock(__FUNCTION__, __LINE__);
  6562. }
  6563. else
  6564. ((Entity*)spawn)->AddProc(type, chance, item, spell);
  6565. return 0;
  6566. }
  6567. int EQ2Emu_lua_RemoveProc(lua_State* state) {
  6568. if (!lua_interface)
  6569. return 0;
  6570. Spawn* spawn = lua_interface->GetSpawn(state);
  6571. Item* item = lua_interface->GetItem(state, 2);
  6572. LuaSpell* spell = 0;
  6573. if (!spawn) {
  6574. lua_interface->LogError("%s: LUA RemoveProc command error: spawn is not valid", lua_interface->GetScriptName(state));
  6575. return 0;
  6576. }
  6577. if (!spawn->IsEntity()) {
  6578. lua_interface->LogError("%s: LUA RemoveProc command error: spawn is not a valid entity", lua_interface->GetScriptName(state));
  6579. return 0;
  6580. }
  6581. if (!item)
  6582. spell = lua_interface->GetCurrentSpell(state);
  6583. if (!item && !spell) {
  6584. lua_interface->LogError("%s: LUA RemoveProc command error: can only use with an item provided or inside a spell script", lua_interface->GetScriptName(state));
  6585. return 0;
  6586. }
  6587. if (spell) {
  6588. Spawn* target;
  6589. spell->MSpellTargets.readlock(__FUNCTION__, __LINE__);
  6590. for (int8 i = 0; i < spell->targets.size(); i++) {
  6591. target = spell->caster->GetZone()->GetSpawnByID(spell->targets.at(i));
  6592. if (!target || !target->IsEntity())
  6593. continue;
  6594. ((Entity*)target)->RemoveProc(item, spell);
  6595. }
  6596. spell->MSpellTargets.releasereadlock(__FUNCTION__, __LINE__);
  6597. spell->caster->RemoveProc(item, spell);
  6598. }
  6599. else
  6600. ((Entity*)spawn)->RemoveProc(item, spell);
  6601. return 0;
  6602. }
  6603. int EQ2Emu_lua_Knockback(lua_State* state) {
  6604. if (!lua_interface)
  6605. return 0;
  6606. Spawn* target_spawn = lua_interface->GetSpawn(state);
  6607. Spawn* spawn = lua_interface->GetSpawn(state, 2);
  6608. int32 duration = lua_interface->GetInt32Value(state, 3);
  6609. float vertical = lua_interface->GetFloatValue(state, 4);
  6610. float horizontal = lua_interface->GetFloatValue(state, 5);
  6611. bool use_heading = lua_interface->GetInt8Value(state, 6) == 1 ? true : false;
  6612. LuaSpell* spell = lua_interface->GetCurrentSpell(state);
  6613. if (!target_spawn) {
  6614. lua_interface->LogError("%s: LUA Knockback command error: target_spawn is not valid", lua_interface->GetScriptName(state));
  6615. return 0;
  6616. }
  6617. if (!spawn) {
  6618. lua_interface->LogError("%s: LUA Knockback command error: spawn is not valid", lua_interface->GetScriptName(state));
  6619. return 0;
  6620. }
  6621. if (spawn->IsPlayer() && (vertical != 0 || horizontal != 0)) {
  6622. Client* client = spawn->GetZone()->GetClientBySpawn(spawn);
  6623. PacketStruct* packet = configReader.getStruct("WS_PlayerKnockback", client->GetVersion());
  6624. if (packet) {
  6625. packet->setDataByName("target_x", target_spawn->GetX());
  6626. packet->setDataByName("target_y", target_spawn->GetY());
  6627. packet->setDataByName("target_z", target_spawn->GetZ());
  6628. packet->setDataByName("vertical_movement", vertical);
  6629. packet->setDataByName("horizontal_movement", horizontal);
  6630. if (use_heading)
  6631. packet->setDataByName("use_player_heading", 1);
  6632. client->QueuePacket(packet->serialize());
  6633. }
  6634. safe_delete(packet);
  6635. }
  6636. return 0;
  6637. }
  6638. int EQ2Emu_lua_IsEpic(lua_State* state) {
  6639. if (!lua_interface)
  6640. return 0;
  6641. Spawn* spawn = lua_interface->GetSpawn(state);
  6642. if (!spawn) {
  6643. lua_interface->LogError("%s: LUA IsEpic command error: spawn is not valid", lua_interface->GetScriptName(state));
  6644. return 0;
  6645. }
  6646. lua_interface->SetBooleanValue(state, (spawn->GetHeroic() >= 2));
  6647. return 1;
  6648. }
  6649. int EQ2Emu_lua_ProcDamage(lua_State* state) {
  6650. if (!lua_interface)
  6651. return 0;
  6652. Spawn* caster = lua_interface->GetSpawn(state);
  6653. Spawn* target = lua_interface->GetSpawn(state, 2);
  6654. string name = lua_interface->GetStringValue(state, 3);
  6655. int8 dmg_type = lua_interface->GetInt8Value(state, 4);
  6656. int32 low_damage = lua_interface->GetInt32Value(state, 5);
  6657. int32 high_damage = lua_interface->GetInt32Value(state, 6);
  6658. string success_msg = lua_interface->GetStringValue(state, 7);
  6659. string effect_msg = lua_interface->GetStringValue(state, 8);
  6660. if (!caster) {
  6661. lua_interface->LogError("%s: LUA ProcDamage command error: caster is not a valid spawn", lua_interface->GetScriptName(state));
  6662. return 0;
  6663. }
  6664. if (!caster->IsEntity()) {
  6665. lua_interface->LogError("%s: LUA ProcDamage command error: caster is not an entity", lua_interface->GetScriptName(state));
  6666. return 0;
  6667. }
  6668. if (!target) {
  6669. lua_interface->LogError("%s: LUA ProcDamage command error: target is not a valid spawn", lua_interface->GetScriptName(state));
  6670. return 0;
  6671. }
  6672. if (!target->IsEntity()) {
  6673. lua_interface->LogError("%s: LUA ProcDamage command error: target is not an entity", lua_interface->GetScriptName(state));
  6674. return 0;
  6675. }
  6676. if (name.length() == 0) {
  6677. lua_interface->LogError("%s: LUA ProcDamage command error: name is empty", lua_interface->GetScriptName(state));
  6678. return 0;
  6679. }
  6680. ((Entity*)caster)->ProcAttack(target, dmg_type, low_damage, high_damage, name, success_msg, effect_msg);
  6681. return 0;
  6682. }
  6683. int EQ2Emu_lua_GetSkillIDByName(lua_State* state) {
  6684. if (!lua_interface)
  6685. return 0;
  6686. string name = lua_interface->GetStringValue(state);
  6687. if (name.length() == 0) {
  6688. lua_interface->LogError("%s: LUA GetSkillIDByName command error: name param was not set", lua_interface->GetScriptName(state));
  6689. return 0;
  6690. }
  6691. Skill* skill = master_skill_list.GetSkillByName(name.c_str());
  6692. if (!skill) {
  6693. lua_interface->LogError("LUA GetSkillIDByName command error: skill with name of %s not found", name.c_str());
  6694. return 0;
  6695. }
  6696. lua_interface->SetInt32Value(state, skill->skill_id);
  6697. return 1;
  6698. }
  6699. int EQ2Emu_lua_IsHeroic(lua_State* state) {
  6700. if (!lua_interface)
  6701. return 0;
  6702. Spawn* spawn = lua_interface->GetSpawn(state);
  6703. if (!spawn) {
  6704. lua_interface->LogError("%s: LUA IsHeroic command error: spawn is not valid", lua_interface->GetScriptName(state));
  6705. return 0;
  6706. }
  6707. lua_interface->SetBooleanValue(state, spawn->GetHeroic() == 1);
  6708. return 1;
  6709. }
  6710. int EQ2Emu_lua_LastSpellAttackHit(lua_State* state) {
  6711. if (!lua_interface)
  6712. return 0;
  6713. LuaSpell* luaspell = lua_interface->GetCurrentSpell(state);
  6714. if (!luaspell) {
  6715. lua_interface->LogError("%s: LUA LastSpellAttackHit command error: this must be called from a spellscript", lua_interface->GetScriptName(state));
  6716. return 0;
  6717. }
  6718. lua_interface->SetBooleanValue(state, luaspell->last_spellattack_hit);
  6719. return 1;
  6720. }
  6721. int EQ2Emu_lua_IsBehind(lua_State* state) {
  6722. if (!lua_interface)
  6723. return 0;
  6724. Spawn* spawn = lua_interface->GetSpawn(state);
  6725. Spawn* target = lua_interface->GetSpawn(state, 2);
  6726. if (!spawn) {
  6727. lua_interface->LogError("%s: LUA IsBehind command error: spawn is not valid", lua_interface->GetScriptName(state));
  6728. return 0;
  6729. }
  6730. if (!spawn->IsEntity()) {
  6731. lua_interface->LogError("%s: LUA IsBehind command error: spawn is not an entity", lua_interface->GetScriptName(state));
  6732. return 0;
  6733. }
  6734. if (!target) {
  6735. lua_interface->LogError("%s: LUA IsBehind command error: target is not valid", lua_interface->GetScriptName(state));
  6736. return 0;
  6737. }
  6738. lua_interface->SetBooleanValue(state, ((Entity*)spawn)->BehindTarget(target));
  6739. return 1;
  6740. }
  6741. int EQ2Emu_lua_IsFlanking(lua_State* state) {
  6742. if (!lua_interface)
  6743. return 0;
  6744. Spawn* spawn = lua_interface->GetSpawn(state);
  6745. Spawn* target = lua_interface->GetSpawn(state, 2);
  6746. if (!spawn) {
  6747. lua_interface->LogError("%s: LUA IsFlanking command error: spawn is not valid", lua_interface->GetScriptName(state));
  6748. return 0;
  6749. }
  6750. if (!spawn->IsEntity()) {
  6751. lua_interface->LogError("%s: LUA IsFlanking command error: spawn is not an entity", lua_interface->GetScriptName(state));
  6752. return 0;
  6753. }
  6754. if (!target) {
  6755. lua_interface->LogError("%s: LUA IsFlanking command error: target is not valid", lua_interface->GetScriptName(state));
  6756. return 0;
  6757. }
  6758. lua_interface->SetBooleanValue(state, ((Entity*)spawn)->FlankingTarget(target));
  6759. return 1;
  6760. }
  6761. int EQ2Emu_lua_GetItemCount(lua_State* state) {
  6762. if (!lua_interface)
  6763. return 0;
  6764. Item* item = lua_interface->GetItem(state);
  6765. if (!item) {
  6766. lua_interface->LogError("%s: LUA GetItemCount command error: item not valid", lua_interface->GetScriptName(state));
  6767. return 0;
  6768. }
  6769. lua_interface->SetInt32Value(state, item->details.count);
  6770. return 1;
  6771. }
  6772. int EQ2Emu_lua_SetItemCount(lua_State* state) {
  6773. if (!lua_interface)
  6774. return 0;
  6775. Item* item = lua_interface->GetItem(state);
  6776. Spawn* owner = lua_interface->GetSpawn(state, 2);
  6777. int16 new_count = lua_interface->GetInt32Value(state, 3);
  6778. if (!item) {
  6779. lua_interface->LogError("%s: LUA SetItemCount command error: item not valid", lua_interface->GetScriptName(state));
  6780. return 0;
  6781. }
  6782. if (!owner) {
  6783. lua_interface->LogError("%s: LUA SetItemCount command error: spawn not valid", lua_interface->GetScriptName(state));
  6784. return 0;
  6785. }
  6786. if (!owner->IsPlayer()) {
  6787. lua_interface->LogError("%s: LUA SetItemCount command error: spawn is not a player", lua_interface->GetScriptName(state));
  6788. return 0;
  6789. }
  6790. if (item->stack_count < new_count) {
  6791. lua_interface->LogError("%s: LUA SetItemCount command error: new item count cannot be more than max stack count", lua_interface->GetScriptName(state));
  6792. return 0;
  6793. }
  6794. if (new_count > 0) {
  6795. item->details.count = new_count;
  6796. item->save_needed = true;
  6797. }
  6798. else if (((Player*)owner)->GetEquipmentList()->GetItem(item->details.slot_id) == item)
  6799. ((Player*)owner)->GetEquipmentList()->RemoveItem(item->details.slot_id, true);
  6800. else if (((Player*)owner)->GetPlayerItemList()->GetItemFromUniqueID(item->details.unique_id) == item)
  6801. ((Player*)owner)->GetPlayerItemList()->RemoveItem(item, true);
  6802. else
  6803. {
  6804. lua_interface->LogError("%s: LUA SetItemCount command error: could not remove item from player", lua_interface->GetScriptName(state));
  6805. return 0;
  6806. }
  6807. Client* client = owner->GetZone()->GetClientBySpawn(owner);
  6808. if (!client)
  6809. return 0;
  6810. ((Player*)owner)->SendInventoryUpdate(client->GetVersion());
  6811. EQ2Packet* app = ((Player*)owner)->GetEquipmentList()->serialize(client->GetVersion());
  6812. if (app)
  6813. client->QueuePacket(app);
  6814. return 0;
  6815. }
  6816. int EQ2Emu_lua_AddSpellTimer(lua_State* state) {
  6817. if (!lua_interface)
  6818. return 0;
  6819. int32 time = lua_interface->GetInt32Value(state);
  6820. string function = lua_interface->GetStringValue(state, 2);
  6821. Spawn* caster = lua_interface->GetSpawn(state, 3);
  6822. Spawn* target = lua_interface->GetSpawn(state, 4);
  6823. LuaSpell* spell = lua_interface->GetCurrentSpell(state);
  6824. if (time == 0) {
  6825. lua_interface->LogError("%s: LUA AddSpellTimer command error: time must be set", lua_interface->GetScriptName(state));
  6826. return 0;
  6827. }
  6828. if (function.length() == 0) {
  6829. lua_interface->LogError("%s: LUA AddSpellTimer command error: function name must be set", lua_interface->GetScriptName(state));
  6830. return 0;
  6831. }
  6832. if (!spell) {
  6833. lua_interface->LogError("%s: LUA AddSpellTimer command error: spell not found, AddSpellTimer must be used in a spell script", lua_interface->GetScriptName(state));
  6834. return 0;
  6835. }
  6836. SpellScriptTimer* timer = new SpellScriptTimer;
  6837. /* //Google tells me memsetting a string is bad, manually setting just in case - Foof
  6838. #ifdef WIN32
  6839. ZeroMemory(timer, sizeof(SpellScriptTimer));
  6840. #else
  6841. bzero(timer, sizeof(SpellScriptTimer));
  6842. #endif*/
  6843. timer->caster = 0;
  6844. timer->deleteWhenDone = false;
  6845. timer->target = 0;
  6846. timer->time = Timer::GetCurrentTime2() + time;
  6847. timer->customFunction = function;
  6848. timer->spell = spell;
  6849. if (caster)
  6850. timer->caster = caster->GetID();
  6851. if (target)
  6852. timer->target = target->GetID();
  6853. spell->caster->GetZone()->GetSpellProcess()->AddSpellScriptTimer(timer);
  6854. return 0;
  6855. }
  6856. int EQ2Emu_lua_Resurrect(lua_State* state) {
  6857. if (!lua_interface)
  6858. return 0;
  6859. float hp_perc = lua_interface->GetFloatValue(state);
  6860. float power_perc = lua_interface->GetFloatValue(state, 2);
  6861. bool send_window = lua_interface->GetInt32Value(state, 3) == 1;
  6862. Spawn* target = lua_interface->GetSpawn(state, 4);
  6863. string heal_name = lua_interface->GetStringValue(state, 5);
  6864. int8 crit_mod = lua_interface->GetInt32Value(state, 6);
  6865. bool no_calcs = lua_interface->GetInt32Value(state, 7) == 1;
  6866. LuaSpell* spell = lua_interface->GetCurrentSpell(state);
  6867. if (!spell) {
  6868. lua_interface->LogError("%s: LUA command error: this function must be used in a spellscript", lua_interface->GetScriptName(state));
  6869. return 0;
  6870. }
  6871. Entity* caster = spell->caster;
  6872. if (!caster) {
  6873. lua_interface->LogError("%s: LUA command error: could not find caster", lua_interface->GetScriptName(state));
  6874. return 0;
  6875. }
  6876. Client* client = 0;
  6877. PendingResurrection* rez = 0;
  6878. ZoneServer* zone = spell->caster->GetZone();
  6879. if (!target) {
  6880. spell->MSpellTargets.readlock(__FUNCTION__, __LINE__);
  6881. if (spell->targets.size() > 0) {
  6882. vector<int32> spell_targets = spell->targets;
  6883. for (int8 i = 0; i < spell_targets.size(); i++) {
  6884. target = zone->GetSpawnByID(spell_targets.at(i));
  6885. if (!target)
  6886. continue;
  6887. if (!target->IsPlayer())
  6888. continue;
  6889. client = target->GetZone()->GetClientBySpawn(target);
  6890. if (!client)
  6891. continue;
  6892. rez = client->GetCurrentRez();
  6893. if (rez->active)
  6894. continue;
  6895. client->GetResurrectMutex()->writelock(__FUNCTION__, __LINE__);
  6896. rez->active = true;
  6897. rez->caster = caster;
  6898. rez->expire_timer = new Timer;
  6899. int32 duration = spell->spell->GetSpellDuration();
  6900. rez->expire_timer->Start(duration * 100);
  6901. rez->hp_perc = hp_perc;
  6902. rez->mp_perc = power_perc;
  6903. rez->range = spell->spell->GetSpellData()->range;
  6904. rez->spell_name = spell->spell->GetName();
  6905. if (heal_name.length() > 0)
  6906. rez->heal_name = heal_name;
  6907. else
  6908. rez->heal_name = rez->spell_name;
  6909. rez->no_calcs = no_calcs;
  6910. rez->crit_mod = crit_mod;
  6911. rez->spell_visual = spell->spell->GetSpellData()->spell_visual;
  6912. if (send_window)
  6913. client->SendResurrectionWindow();
  6914. else {
  6915. target->GetZone()->ResurrectSpawn(target, client);
  6916. rez->should_delete = true;
  6917. }
  6918. client->GetResurrectMutex()->releasewritelock(__FUNCTION__, __LINE__);
  6919. }
  6920. }
  6921. spell->MSpellTargets.releasereadlock(__FUNCTION__, __LINE__);
  6922. }
  6923. else {
  6924. client = target->GetZone()->GetClientBySpawn(target);
  6925. if (!client)
  6926. return 0;
  6927. rez = client->GetCurrentRez();
  6928. if (rez->active)
  6929. return 0;
  6930. client->GetResurrectMutex()->writelock(__FUNCTION__, __LINE__);
  6931. rez->active = true;
  6932. rez->caster = caster;
  6933. rez->expire_timer = new Timer;
  6934. int32 duration = spell->spell->GetSpellDuration();
  6935. rez->expire_timer->Start(duration * 100);
  6936. rez->hp_perc = hp_perc;
  6937. rez->mp_perc = power_perc;
  6938. rez->range = spell->spell->GetSpellData()->range;
  6939. rez->spell_name = spell->spell->GetName();
  6940. if (heal_name.length() > 0)
  6941. rez->heal_name = heal_name;
  6942. else
  6943. rez->heal_name = rez->spell_name;
  6944. rez->no_calcs = no_calcs;
  6945. rez->crit_mod = crit_mod;
  6946. rez->spell_visual = spell->spell->GetSpellData()->spell_visual;
  6947. if (send_window)
  6948. client->SendResurrectionWindow();
  6949. else {
  6950. target->GetZone()->ResurrectSpawn(target, client);
  6951. rez->should_delete = true;
  6952. }
  6953. client->GetResurrectMutex()->releasewritelock(__FUNCTION__, __LINE__);
  6954. }
  6955. return 0;
  6956. }
  6957. int EQ2Emu_lua_SetVision(lua_State* state) {
  6958. if (!lua_interface)
  6959. return 0;
  6960. Spawn* spawn = lua_interface->GetSpawn(state);
  6961. int8 vision = lua_interface->GetInt8Value(state, 2);
  6962. LuaSpell* spell = lua_interface->GetCurrentSpell(state);
  6963. if (!spawn) {
  6964. lua_interface->LogError("%s: LUA SetVision command error: spawn is not valid", lua_interface->GetScriptName(state));
  6965. return 0;
  6966. }
  6967. if (!spawn->IsEntity()) {
  6968. lua_interface->LogError("%s: LUA SetVision command error: spawn is not an entity", lua_interface->GetScriptName(state));
  6969. return 0;
  6970. }
  6971. if (spell && spell->targets.size() > 0) {
  6972. ZoneServer* zone = spell->caster->GetZone();
  6973. for (int8 i = 0; i < spell->targets.size(); i++) {
  6974. Spawn* target = zone->GetSpawnByID(spell->targets.at(i));
  6975. if (target->IsEntity()) {
  6976. ((Entity*)target)->GetInfoStruct()->vision = vision;
  6977. if (target->IsPlayer())
  6978. ((Player*)target)->SetCharSheetChanged(true);
  6979. }
  6980. }
  6981. }
  6982. else {
  6983. ((Entity*)spawn)->GetInfoStruct()->vision = vision;
  6984. if (spawn->IsPlayer())
  6985. ((Player*)spawn)->SetCharSheetChanged(true);
  6986. }
  6987. return 0;
  6988. }
  6989. int EQ2Emu_lua_BlurVision(lua_State* state) {
  6990. if (!lua_interface)
  6991. return 0;
  6992. Spawn* spawn = lua_interface->GetSpawn(state);
  6993. float intensity = lua_interface->GetFloatValue(state, 2);
  6994. LuaSpell* spell = lua_interface->GetCurrentSpell(state);
  6995. if (!spawn) {
  6996. lua_interface->LogError("%s: LUA BlurVision command error: spawn is not valid", lua_interface->GetScriptName(state));
  6997. return 0;
  6998. }
  6999. if (!spawn->IsEntity()) {
  7000. lua_interface->LogError("%s: LUA BlurVision command error: spawn is not an entity", lua_interface->GetScriptName(state));
  7001. return 0;
  7002. }
  7003. if (spell && spell->targets.size() > 0) {
  7004. ZoneServer* zone = spell->caster->GetZone();
  7005. for (int8 i = 0; i < spell->targets.size(); i++) {
  7006. Spawn* target = zone->GetSpawnByID(spell->targets.at(i));
  7007. if (target && target->IsEntity()) {
  7008. ((Entity*)target)->GetInfoStruct()->drunk = intensity;
  7009. if (target->IsPlayer())
  7010. ((Player*)target)->SetCharSheetChanged(true);
  7011. }
  7012. }
  7013. }
  7014. else {
  7015. ((Entity*)spawn)->GetInfoStruct()->drunk = intensity;
  7016. if (spawn->IsPlayer())
  7017. ((Player*)spawn)->SetCharSheetChanged(true);
  7018. }
  7019. return 0;
  7020. }
  7021. int EQ2Emu_lua_BreatheUnderwater(lua_State* state) {
  7022. if (!lua_interface)
  7023. return 0;
  7024. Spawn* spawn = lua_interface->GetSpawn(state);
  7025. bool breatheUnderwater = lua_interface->GetBooleanValue(state, 2);
  7026. LuaSpell* spell = lua_interface->GetCurrentSpell(state);
  7027. if (!spawn) {
  7028. lua_interface->LogError("%s: LUA BreathUnderwater command error: spawn is not valid", lua_interface->GetScriptName(state));
  7029. return 0;
  7030. }
  7031. if (!spawn->IsEntity()) {
  7032. lua_interface->LogError("%s: LUA BreathUnderwater command error: spawn is not en entity", lua_interface->GetScriptName(state));
  7033. return 0;
  7034. }
  7035. if (spell && spell->targets.size() > 0) {
  7036. ZoneServer* zone = spell->caster->GetZone();
  7037. for (int8 i = 0; i < spell->targets.size(); i++) {
  7038. Spawn* target = zone->GetSpawnByID(spell->targets.at(i));
  7039. if (target->IsEntity()) {
  7040. ((Entity*)target)->GetInfoStruct()->breathe_underwater = breatheUnderwater;
  7041. if (target->IsPlayer())
  7042. ((Player*)target)->SetCharSheetChanged(true);
  7043. }
  7044. }
  7045. }
  7046. else {
  7047. ((Entity*)spawn)->GetInfoStruct()->breathe_underwater = breatheUnderwater;
  7048. if (spawn->IsPlayer())
  7049. ((Player*)spawn)->SetCharSheetChanged(true);
  7050. }
  7051. return 0;
  7052. }
  7053. int EQ2Emu_lua_GetItemSkillReq(lua_State* state) {
  7054. if (!lua_interface)
  7055. return 0;
  7056. Item* item = lua_interface->GetItem(state);
  7057. int8 type = lua_interface->GetInt32Value(state, 2);
  7058. if (!item) {
  7059. lua_interface->LogError("%s: LUA GetItemSkillReq command error: item not valid", lua_interface->GetScriptName(state));
  7060. return 0;
  7061. }
  7062. if (type == 1)
  7063. lua_interface->SetInt32Value(state, item->generic_info.skill_req1);
  7064. else if (type == 2)
  7065. lua_interface->SetInt32Value(state, item->generic_info.skill_req2);
  7066. return 1;
  7067. }
  7068. int EQ2Emu_lua_SetSpeedMultiplier(lua_State* state) {
  7069. if (!lua_interface)
  7070. return 0;
  7071. Spawn* target = lua_interface->GetSpawn(state);
  7072. float val = lua_interface->GetFloatValue(state, 2);
  7073. LuaSpell* spell = lua_interface->GetCurrentSpell(state);
  7074. // Added from Gangrenous post
  7075. if (spell && spell->resisted)
  7076. return 0;
  7077. // if its a percentage of 100 its a slow, we want to go at a fraction of the speed
  7078. if (val > 1.0f)
  7079. val = 1.0f - (val / 100.0f);
  7080. if (spell && spell->spell && spell->targets.size() > 0) {
  7081. ZoneServer* zone = spell->caster->GetZone();
  7082. for (int32 i = 0; i != spell->targets.size(); i++) {
  7083. Spawn* spawn = zone->GetSpawnByID(spell->targets.at(i));
  7084. if (spawn && spawn->IsEntity()) {
  7085. ((Entity*)spawn)->SetSpeedMultiplier(val);
  7086. if (spawn->IsPlayer())
  7087. ((Player*)spawn)->SetCharSheetChanged(true);
  7088. }
  7089. }
  7090. }
  7091. else {
  7092. if (target && target->IsEntity()) {
  7093. ((Entity*)target)->SetSpeedMultiplier(val);
  7094. if (target->IsPlayer())
  7095. ((Player*)target)->SetCharSheetChanged(true);
  7096. }
  7097. }
  7098. return 0;
  7099. }
  7100. int EQ2Emu_lua_SetIllusion(lua_State* state) {
  7101. if (!lua_interface)
  7102. return 0;
  7103. Spawn* spawn = lua_interface->GetSpawn(state);
  7104. int16 model = lua_interface->GetInt16Value(state, 2);
  7105. LuaSpell* spell = lua_interface->GetCurrentSpell(state);
  7106. if (spell && spell->spell && spell->targets.size() > 0) {
  7107. ZoneServer* zone = spell->caster->GetZone();
  7108. for (int32 i = 0; i < spell->targets.size(); i++) {
  7109. Spawn* target = zone->GetSpawnByID(spell->targets.at(i));
  7110. if (target)
  7111. target->SetIllusionModel(model);
  7112. }
  7113. }
  7114. else {
  7115. if (!spawn) {
  7116. lua_interface->LogError("%s: LUA SetIllusion command error: spawn is not valid", lua_interface->GetScriptName(state));
  7117. return 0;
  7118. }
  7119. spawn->SetIllusionModel(model);
  7120. }
  7121. return 0;
  7122. }
  7123. int EQ2Emu_lua_ResetIllusion(lua_State* state) {
  7124. if (!lua_interface)
  7125. return 0;
  7126. Spawn* spawn = lua_interface->GetSpawn(state);
  7127. LuaSpell* spell = lua_interface->GetCurrentSpell(state);
  7128. if (spell && spell->spell && spell->targets.size() > 0) {
  7129. ZoneServer* zone = spell->caster->GetZone();
  7130. for (int32 i = 0; i < spell->targets.size(); i++) {
  7131. Spawn* target = zone->GetSpawnByID(spell->targets.at(i));
  7132. if (target)
  7133. target->SetIllusionModel(0);
  7134. }
  7135. }
  7136. else {
  7137. if (!spawn) {
  7138. lua_interface->LogError("%s: LUA ResetIllusion command error: spawn is not valid", lua_interface->GetScriptName(state));
  7139. return 0;
  7140. }
  7141. spawn->SetIllusionModel(0);
  7142. }
  7143. return 0;
  7144. }
  7145. int EQ2Emu_lua_AddThreatTransfer(lua_State* state) {
  7146. if (!lua_interface)
  7147. return 0;
  7148. Spawn* caster = lua_interface->GetSpawn(state);
  7149. Spawn* target = lua_interface->GetSpawn(state, 2);
  7150. float chance = lua_interface->GetFloatValue(state, 3);
  7151. LuaSpell* spell = lua_interface->GetCurrentSpell(state);
  7152. if (!caster) {
  7153. lua_interface->LogError("%s: LUA AddThreatTransfer command error: caster is not a valid spawn", lua_interface->GetScriptName(state));
  7154. return 0;
  7155. }
  7156. if (!caster->IsEntity()) {
  7157. lua_interface->LogError("%s: LUA AddThreatTransfer command error: caster is not an entity", lua_interface->GetScriptName(state));
  7158. return 0;
  7159. }
  7160. if (!target) {
  7161. lua_interface->LogError("%s: LUA AddThreatTransfer command error: target is not a valid spawn", lua_interface->GetScriptName(state));
  7162. return 0;
  7163. }
  7164. if (!target->IsEntity()) {
  7165. lua_interface->LogError("%s: LUA AddThreatTransfer command error: target is not an entity", lua_interface->GetScriptName(state));
  7166. return 0;
  7167. }
  7168. if (chance <= 0) {
  7169. lua_interface->LogError("%s: LUA AddThreatTransfer command error: chance must be greater then 0%", lua_interface->GetScriptName(state));
  7170. return 0;
  7171. }
  7172. if (!spell) {
  7173. lua_interface->LogError("%s: LUA AddThreatTransfer command error: can only be used in a spell script", lua_interface->GetScriptName(state));
  7174. return 0;
  7175. }
  7176. if (((Entity*)caster)->GetThreatTransfer()) {
  7177. return 0;
  7178. }
  7179. ThreatTransfer* transfer = new ThreatTransfer;
  7180. transfer->Target = target->GetID();
  7181. transfer->Amount = chance;
  7182. transfer->Spell = spell;
  7183. ((Entity*)caster)->SetThreatTransfer(transfer);
  7184. return 0;
  7185. }
  7186. int EQ2Emu_lua_RemoveThreatTransfer(lua_State* state) {
  7187. if (!lua_interface)
  7188. return 0;
  7189. Spawn* spawn = lua_interface->GetSpawn(state);
  7190. LuaSpell* spell = lua_interface->GetCurrentSpell(state);
  7191. if (!spawn) {
  7192. lua_interface->LogError("%s: LUA RemoveThreatTransfer command error: spawn is not valid", lua_interface->GetScriptName(state));
  7193. return 0;
  7194. }
  7195. if (!spell) {
  7196. lua_interface->LogError("%s: LUA RemoveThreatTransfer command error: can only be used in a spell script", lua_interface->GetScriptName(state));
  7197. return 0;
  7198. }
  7199. if (((Entity*)spawn)->GetThreatTransfer() && ((Entity*)spawn)->GetThreatTransfer()->Spell == spell) {
  7200. ThreatTransfer* transfer = ((Entity*)spawn)->GetThreatTransfer();
  7201. ((Entity*)spawn)->SetThreatTransfer(0);
  7202. safe_delete(transfer);
  7203. }
  7204. return 0;
  7205. }
  7206. int EQ2Emu_lua_CureByType(lua_State* state) {
  7207. if (!lua_interface)
  7208. return 0;
  7209. LuaSpell* spell = lua_interface->GetCurrentSpell(state);
  7210. if (!spell) {
  7211. lua_interface->LogError("%s: LUA CureByType command error: can only be used in a spell script", lua_interface->GetScriptName(state));
  7212. return 0;
  7213. }
  7214. int8 cure_count = lua_interface->GetInt8Value(state);
  7215. int8 cure_type = lua_interface->GetInt8Value(state, 2);
  7216. string cure_name = lua_interface->GetStringValue(state, 3);
  7217. int8 cure_level = lua_interface->GetInt8Value(state, 4);
  7218. Spawn* target = lua_interface->GetSpawn(state, 5);
  7219. if (target) {
  7220. if (!target->IsEntity()) {
  7221. lua_interface->LogError("%s: LUA CureByType command error: spawn override must be entity if used", lua_interface->GetScriptName(state));
  7222. return 0;
  7223. }
  7224. if (((Entity*)target)->GetDetTypeCount(cure_type) > 0)
  7225. ((Entity*)target)->CureDetrimentByType(cure_count, cure_type, cure_name.length() > 0 ? cure_name : (string)spell->spell->GetName(), spell->caster, cure_level);
  7226. }
  7227. else {
  7228. ZoneServer* zone = spell->caster->GetZone();
  7229. vector<int32> targets = spell->targets;
  7230. spell->MSpellTargets.readlock(__FUNCTION__, __LINE__);
  7231. for (int8 i = 0; i < targets.size(); i++) {
  7232. target = zone->GetSpawnByID(targets.at(i));
  7233. if (!target || !target->IsEntity())
  7234. continue;
  7235. if (((Entity*)target)->GetDetTypeCount(cure_type) > 0)
  7236. ((Entity*)target)->CureDetrimentByType(cure_count, cure_type, cure_name.length() > 0 ? cure_name : (string)spell->spell->GetName(), spell->caster, cure_level);
  7237. }
  7238. spell->MSpellTargets.releasereadlock(__FUNCTION__, __LINE__);
  7239. }
  7240. return 0;
  7241. }
  7242. int EQ2Emu_lua_CureByControlEffect(lua_State* state) {
  7243. if (!lua_interface)
  7244. return 0;
  7245. LuaSpell* spell = lua_interface->GetCurrentSpell(state);
  7246. if (!spell) {
  7247. lua_interface->LogError("%s: LUA CureByControlEffect command error: can only be used in a spell script", lua_interface->GetScriptName(state));
  7248. return 0;
  7249. }
  7250. int8 cure_count = lua_interface->GetInt8Value(state);
  7251. int8 cure_type = lua_interface->GetInt8Value(state, 2);
  7252. string cure_name = lua_interface->GetStringValue(state, 3);
  7253. int8 cure_level = lua_interface->GetInt8Value(state, 4);
  7254. Spawn* target = lua_interface->GetSpawn(state, 5);
  7255. if (target) {
  7256. if (!target->IsEntity()) {
  7257. lua_interface->LogError("%s: LUA CureByControlEffect command error: spawn override must be entity if used", lua_interface->GetScriptName(state));
  7258. return 0;
  7259. }
  7260. if (((Entity*)target)->GetDetCount() > 0)
  7261. ((Entity*)target)->CureDetrimentByControlEffect(cure_count, cure_type, cure_name.length() > 0 ? cure_name : (string)spell->spell->GetName(), spell->caster, cure_level);
  7262. }
  7263. else {
  7264. ZoneServer* zone = spell->caster->GetZone();
  7265. vector<int32> targets = spell->targets;
  7266. spell->MSpellTargets.readlock(__FUNCTION__, __LINE__);
  7267. for (int8 i = 0; i < targets.size(); i++) {
  7268. target = zone->GetSpawnByID(targets.at(i));
  7269. if (!target || !target->IsEntity())
  7270. continue;
  7271. if (((Entity*)target)->GetDetCount() > 0)
  7272. ((Entity*)target)->CureDetrimentByControlEffect(cure_count, cure_type, cure_name.length() > 0 ? cure_name : (string)spell->spell->GetName(), spell->caster, cure_level);
  7273. }
  7274. spell->MSpellTargets.releasereadlock(__FUNCTION__, __LINE__);
  7275. }
  7276. return 0;
  7277. }
  7278. int EQ2Emu_lua_CancelSpell(lua_State* state) {
  7279. if (!lua_interface)
  7280. return 0;
  7281. LuaSpell* spell = lua_interface->GetCurrentSpell(state);
  7282. if (!spell) {
  7283. lua_interface->LogError("%s: LUA CancelSpell command error: can only be use in a spell script", lua_interface->GetScriptName(state));
  7284. return 0;
  7285. }
  7286. if (!spell->caster) {
  7287. lua_interface->LogError("%s: LUA CancelSpell command error: unable to get the caster of the spell", lua_interface->GetScriptName(state));
  7288. return 0;
  7289. }
  7290. if (!spell->caster->GetZone()) {
  7291. lua_interface->LogError("%s: LUA CancelSpell command error: unable to get the zone of the caster", lua_interface->GetScriptName(state));
  7292. return 0;
  7293. }
  7294. spell->caster->GetZone()->GetSpellProcess()->AddSpellCancel(spell);
  7295. return 0;
  7296. }
  7297. int EQ2Emu_lua_RemoveStealth(lua_State* state) {
  7298. if (!lua_interface)
  7299. return 0;
  7300. Spawn* spawn = lua_interface->GetSpawn(state);
  7301. LuaSpell* spell = lua_interface->GetCurrentSpell(state);
  7302. if (!spell) {
  7303. lua_interface->LogError("%s: LUA RemoveStealth command error: must be used from spell script", lua_interface->GetScriptName(state));
  7304. return 0;
  7305. }
  7306. if (spawn && spawn->IsEntity())
  7307. ((Entity*)spawn)->RemoveStealthSpell(spell);
  7308. else {
  7309. ZoneServer* zone = spell->caster->GetZone();
  7310. spell->MSpellTargets.readlock(__FUNCTION__, __LINE__);
  7311. for (int32 i = 0; i < spell->targets.size(); i++) {
  7312. spawn = zone->GetSpawnByID(spell->targets.at(i));
  7313. if (!spawn || !spawn->IsEntity())
  7314. continue;
  7315. ((Entity*)spawn)->RemoveStealthSpell(spell);
  7316. }
  7317. spell->MSpellTargets.releasereadlock(__FUNCTION__, __LINE__);
  7318. }
  7319. return 0;
  7320. }
  7321. int EQ2Emu_lua_RemoveInvis(lua_State* state) {
  7322. if (!lua_interface)
  7323. return 0;
  7324. Spawn* spawn = lua_interface->GetSpawn(state);
  7325. LuaSpell* spell = lua_interface->GetCurrentSpell(state);
  7326. if (!spell) {
  7327. lua_interface->LogError("%s: LUA RemoveInvis command error: must be used from spell script", lua_interface->GetScriptName(state));
  7328. return 0;
  7329. }
  7330. if (spawn && spawn->IsEntity())
  7331. ((Entity*)spawn)->RemoveInvisSpell(spell);
  7332. else {
  7333. ZoneServer* zone = spell->caster->GetZone();
  7334. spell->MSpellTargets.readlock(__FUNCTION__, __LINE__);
  7335. for (int32 i = 0; i < spell->targets.size(); i++) {
  7336. spawn = zone->GetSpawnByID(spell->targets.at(i));
  7337. if (!spawn || !spawn->IsEntity())
  7338. continue;
  7339. ((Entity*)spawn)->RemoveInvisSpell(spell);
  7340. }
  7341. spell->MSpellTargets.releasereadlock(__FUNCTION__, __LINE__);
  7342. }
  7343. return 0;
  7344. }
  7345. int EQ2Emu_lua_StartHeroicOpportunity(lua_State* state) {
  7346. if (!lua_interface)
  7347. return 0;
  7348. Spawn* caster = lua_interface->GetSpawn(state);
  7349. int8 class_id = lua_interface->GetInt8Value(state, 2);
  7350. if (!caster) {
  7351. lua_interface->LogError("%s: LUA StartHeroicOpportunity command error: caster is not valid", lua_interface->GetScriptName(state));
  7352. return 0;
  7353. }
  7354. if (!caster->IsPlayer()) {
  7355. lua_interface->LogError("%s: LUA StartHeroicOpportunity command error: caster must be a player", lua_interface->GetScriptName(state));
  7356. return 0;
  7357. }
  7358. Spawn* target = caster->GetTarget();
  7359. if (!target) {
  7360. lua_interface->LogError("%s: LUA StartHeroicOpportunity command error: target is not valid", lua_interface->GetScriptName(state));
  7361. return 0;
  7362. }
  7363. Client* client = caster->GetZone()->GetClientBySpawn(caster);
  7364. if (!client) {
  7365. lua_interface->LogError("%s: LUA StartHeroicOpportunity command error: unable to get a client for the given caster", lua_interface->GetScriptName(state));
  7366. return 0;
  7367. }
  7368. HeroicOP* ho = master_ho_list.GetHeroicOP(class_id);
  7369. if (ho) {
  7370. ho->SetTarget(target->GetID());
  7371. LogWrite(SPELL__ERROR, 0, "HO", "caster: %u", caster->GetID());
  7372. LogWrite(SPELL__ERROR, 0, "HO", "target: %u", target->GetID());
  7373. if (((Entity*)caster)->GetGroupMemberInfo()) {
  7374. if (caster->GetZone()->GetSpellProcess()->AddHO(client, ho)) {
  7375. world.GetGroupManager()->GroupLock(__FUNCTION__, __LINE__);
  7376. deque<GroupMemberInfo*>::iterator itr;
  7377. PlayerGroup* group = world.GetGroupManager()->GetGroup(((Entity*)caster)->GetGroupMemberInfo()->group_id);
  7378. if (group)
  7379. {
  7380. group->MGroupMembers.readlock(__FUNCTION__, __LINE__);
  7381. deque<GroupMemberInfo*>* members = group->GetMembers();
  7382. for (itr = members->begin(); itr != members->end(); itr++) {
  7383. if ((*itr)->client)
  7384. ClientPacketFunctions::SendHeroicOPUpdate((*itr)->client, ho);
  7385. }
  7386. group->MGroupMembers.releasereadlock(__FUNCTION__, __LINE__);
  7387. }
  7388. world.GetGroupManager()->ReleaseGroupLock(__FUNCTION__, __LINE__);
  7389. }
  7390. else
  7391. safe_delete(ho);
  7392. }
  7393. else {
  7394. if (caster->GetZone()->GetSpellProcess()->AddHO(client, ho)) {
  7395. ClientPacketFunctions::SendHeroicOPUpdate(client, ho);
  7396. }
  7397. else
  7398. safe_delete(ho);
  7399. }
  7400. }
  7401. return 0;
  7402. }
  7403. int EQ2Emu_lua_SetSpellTriggerCount(lua_State* state) {
  7404. if (!lua_interface)
  7405. return 0;
  7406. LuaSpell* spell = lua_interface->GetCurrentSpell(state);
  7407. if (!spell) {
  7408. lua_interface->LogError("%s: LUA SetSpellTriggerCount command error: you must use this function in a spellscript!", lua_interface->GetScriptName(state));
  7409. return 0;
  7410. }
  7411. int16 triggerCount = lua_interface->GetInt16Value(state);
  7412. bool cancel_after_triggers = (lua_interface->GetInt8Value(state, 2) == 1);
  7413. if (!triggerCount) {
  7414. lua_interface->LogError("%s: LUA SetSpellTriggerCount command error: used trigger value equals zero!", lua_interface->GetScriptName(state));
  7415. return 0;
  7416. }
  7417. spell->num_triggers = triggerCount;
  7418. spell->had_triggers = true;
  7419. spell->cancel_after_all_triggers = cancel_after_triggers;
  7420. return 0;
  7421. }
  7422. int EQ2Emu_lua_GetSpellTriggerCount(lua_State* state) {
  7423. if (!lua_interface)
  7424. return 0;
  7425. LuaSpell* spell = lua_interface->GetCurrentSpell(state);
  7426. if (!spell) {
  7427. lua_interface->LogError("%s: LUA GetSpellTriggerCount command error: you must use this function in a spellscript!", lua_interface->GetScriptName(state));
  7428. return 0;
  7429. }
  7430. lua_interface->SetInt32Value(state, spell->num_triggers);
  7431. return 1;
  7432. }
  7433. int EQ2Emu_lua_RemoveTriggerFromSpell(lua_State* state) {
  7434. if (!lua_interface)
  7435. return 0;
  7436. LuaSpell* spell = lua_interface->GetCurrentSpell(state);
  7437. if (!spell) {
  7438. lua_interface->LogError("%s: LUA RemoveTriggerFromSpell command error: you must use this function in a spellscript!", lua_interface->GetScriptName(state));
  7439. return 0;
  7440. }
  7441. int16 remove_count = lua_interface->GetInt16Value(state);
  7442. if (!remove_count)
  7443. remove_count = 1;
  7444. if (remove_count >= spell->num_triggers) {
  7445. spell->num_triggers = 0;
  7446. if (spell->cancel_after_all_triggers)
  7447. spell->caster->GetZone()->GetSpellProcess()->AddSpellCancel(spell);
  7448. }
  7449. else {
  7450. spell->num_triggers -= remove_count;
  7451. ClientPacketFunctions::SendMaintainedExamineUpdate(spell->caster->GetZone()->GetClientBySpawn(spell->caster), spell->slot_pos, spell->num_triggers, 0);
  7452. }
  7453. return 0;
  7454. }
  7455. int EQ2Emu_lua_CopySpawnAppearance(lua_State* state) {
  7456. if (!lua_interface)
  7457. return 0;
  7458. Spawn* spawn = lua_interface->GetSpawn(state);
  7459. Spawn* copy_spawn = lua_interface->GetSpawn(state, 2);
  7460. if (!spawn) {
  7461. lua_interface->LogError("%s: LUA CopySpawnAppearance command error: the first spawn used was not valid!", lua_interface->GetScriptName(state));
  7462. return 0;
  7463. }
  7464. if (!copy_spawn) {
  7465. lua_interface->LogError("%s: LUA CopySpawnAppearance command error: the second spawn used was not valid!", lua_interface->GetScriptName(state));
  7466. return 0;
  7467. }
  7468. spawn->CopySpawnAppearance(copy_spawn);
  7469. return 0;
  7470. }
  7471. int EQ2Emu_lua_AddImmunitySpell(lua_State* state) {
  7472. if (!lua_interface)
  7473. return 0;
  7474. LuaSpell* spell = lua_interface->GetCurrentSpell(state);
  7475. int8 type = lua_interface->GetInt8Value(state);
  7476. Spawn* spawn = lua_interface->GetSpawn(state, 2);
  7477. if (!spell) {
  7478. lua_interface->LogError("%s: LUA AddImmunitySpell command error: This must be used in a spellscript", lua_interface->GetScriptName(state));
  7479. return 0;
  7480. }
  7481. if (spawn) {
  7482. if (!spawn->IsEntity()) {
  7483. lua_interface->LogError("%s: LUA AddImmunitySpell command error: The spawn provided is not an entity", lua_interface->GetScriptName(state));
  7484. return 0;
  7485. }
  7486. Entity* entity = ((Entity*)spawn);
  7487. switch (type) {
  7488. case IMMUNITY_TYPE_AOE:
  7489. entity->AddAOEImmunity(spell);
  7490. if (!(spell->effect_bitmask & EFFECT_FLAG_AOE_IMMUNE))
  7491. spell->effect_bitmask += EFFECT_FLAG_AOE_IMMUNE;
  7492. break;
  7493. case IMMUNITY_TYPE_STUN:
  7494. entity->AddStunImmunity(spell);
  7495. if (!(spell->effect_bitmask & EFFECT_FLAG_STUN_IMMUNE))
  7496. spell->effect_bitmask += EFFECT_FLAG_STUN_IMMUNE;
  7497. break;
  7498. case IMMUNITY_TYPE_ROOT:
  7499. entity->AddRootImmunity(spell);
  7500. if (!(spell->effect_bitmask & EFFECT_FLAG_ROOT_IMMUNE))
  7501. spell->effect_bitmask += EFFECT_FLAG_ROOT_IMMUNE;
  7502. break;
  7503. case IMMUNITY_TYPE_DAZE:
  7504. entity->AddDazeImmunity(spell);
  7505. if (!(spell->effect_bitmask & EFFECT_FLAG_DAZE_IMMUNE))
  7506. spell->effect_bitmask += EFFECT_FLAG_DAZE_IMMUNE;
  7507. break;
  7508. case IMMUNITY_TYPE_FEAR:
  7509. entity->AddFearImmunity(spell);
  7510. if (!(spell->effect_bitmask & EFFECT_FLAG_FEAR_IMMUNE))
  7511. spell->effect_bitmask += EFFECT_FLAG_FEAR_IMMUNE;
  7512. break;
  7513. case IMMUNITY_TYPE_MEZ:
  7514. entity->AddMezImmunity(spell);
  7515. if (!(spell->effect_bitmask & EFFECT_FLAG_MEZ_IMMUNE))
  7516. spell->effect_bitmask += EFFECT_FLAG_MEZ_IMMUNE;
  7517. break;
  7518. case IMMUNITY_TYPE_STIFLE:
  7519. entity->AddStifleImmunity(spell);
  7520. if (!(spell->effect_bitmask & EFFECT_FLAG_STIFLE_IMMUNE))
  7521. spell->effect_bitmask += EFFECT_FLAG_STIFLE_IMMUNE;
  7522. break;
  7523. default:
  7524. lua_interface->LogError("%s: LUA AddImmunitySpell command error: invalid immunity type", lua_interface->GetScriptName(state));
  7525. }
  7526. }
  7527. else {
  7528. bool should_break = false;
  7529. spell->MSpellTargets.readlock(__FUNCTION__, __LINE__);
  7530. for (int8 i = 0; i < spell->targets.size(); i++) {
  7531. spawn = spell->caster->GetZone()->GetSpawnByID(spell->targets.at(i));
  7532. if (!spawn || !spawn->IsEntity())
  7533. continue;
  7534. Entity* entity = ((Entity*)spawn);
  7535. switch (type) {
  7536. case IMMUNITY_TYPE_AOE:
  7537. entity->AddAOEImmunity(spell);
  7538. if (!(spell->effect_bitmask & EFFECT_FLAG_AOE_IMMUNE))
  7539. spell->effect_bitmask += EFFECT_FLAG_AOE_IMMUNE;
  7540. break;
  7541. case IMMUNITY_TYPE_STUN:
  7542. entity->AddStunImmunity(spell);
  7543. if (!(spell->effect_bitmask & EFFECT_FLAG_STUN_IMMUNE))
  7544. spell->effect_bitmask += EFFECT_FLAG_STUN_IMMUNE;
  7545. break;
  7546. case IMMUNITY_TYPE_ROOT:
  7547. entity->AddRootImmunity(spell);
  7548. if (!(spell->effect_bitmask & EFFECT_FLAG_ROOT_IMMUNE))
  7549. spell->effect_bitmask += EFFECT_FLAG_ROOT_IMMUNE;
  7550. break;
  7551. case IMMUNITY_TYPE_DAZE:
  7552. entity->AddDazeImmunity(spell);
  7553. if (!(spell->effect_bitmask & EFFECT_FLAG_DAZE_IMMUNE))
  7554. spell->effect_bitmask += EFFECT_FLAG_DAZE_IMMUNE;
  7555. break;
  7556. case IMMUNITY_TYPE_FEAR:
  7557. entity->AddFearImmunity(spell);
  7558. if (!(spell->effect_bitmask & EFFECT_FLAG_FEAR_IMMUNE))
  7559. spell->effect_bitmask += EFFECT_FLAG_FEAR_IMMUNE;
  7560. break;
  7561. case IMMUNITY_TYPE_MEZ:
  7562. entity->AddMezImmunity(spell);
  7563. if (!(spell->effect_bitmask & EFFECT_FLAG_MEZ_IMMUNE))
  7564. spell->effect_bitmask += EFFECT_FLAG_MEZ_IMMUNE;
  7565. break;
  7566. case IMMUNITY_TYPE_STIFLE:
  7567. entity->AddStifleImmunity(spell);
  7568. if (!(spell->effect_bitmask & EFFECT_FLAG_STIFLE_IMMUNE))
  7569. spell->effect_bitmask += EFFECT_FLAG_STIFLE_IMMUNE;
  7570. break;
  7571. default:
  7572. lua_interface->LogError("%s: LUA AddImmunitySpell command error: invalid immunity type", lua_interface->GetScriptName(state));
  7573. should_break = true;
  7574. }
  7575. if (should_break)
  7576. break;
  7577. }
  7578. spell->MSpellTargets.releasereadlock(__FUNCTION__, __LINE__);
  7579. }
  7580. return 0;
  7581. }
  7582. int EQ2Emu_lua_RemoveImmunitySpell(lua_State* state) {
  7583. if (!lua_interface)
  7584. return 0;
  7585. LuaSpell* spell = lua_interface->GetCurrentSpell(state);
  7586. int8 type = lua_interface->GetInt8Value(state);
  7587. Spawn* spawn = lua_interface->GetSpawn(state, 2);
  7588. if (!spell) {
  7589. lua_interface->LogError("%s: LUA RemoveImmunitySpell command error: This must be used in a spellscript", lua_interface->GetScriptName(state));
  7590. return 0;
  7591. }
  7592. if (spawn) {
  7593. if (!spawn->IsEntity()) {
  7594. lua_interface->LogError("%s: LUA RemoveImmunitySpell command error: The spawn provided is not an entity", lua_interface->GetScriptName(state));
  7595. return 0;
  7596. }
  7597. Entity* entity = ((Entity*)spawn);
  7598. switch (type) {
  7599. case IMMUNITY_TYPE_AOE:
  7600. entity->RemoveAOEImmunity(spell);
  7601. break;
  7602. case IMMUNITY_TYPE_STUN:
  7603. entity->RemoveStunImmunity(spell);
  7604. break;
  7605. case IMMUNITY_TYPE_ROOT:
  7606. entity->RemoveRootImmunity(spell);
  7607. break;
  7608. case IMMUNITY_TYPE_DAZE:
  7609. entity->RemoveDazeImmunity(spell);
  7610. break;
  7611. case IMMUNITY_TYPE_FEAR:
  7612. entity->RemoveFearImmunity(spell);
  7613. break;
  7614. case IMMUNITY_TYPE_MEZ:
  7615. entity->RemoveMezImmunity(spell);
  7616. break;
  7617. case IMMUNITY_TYPE_STIFLE:
  7618. entity->RemoveStifleImmunity(spell);
  7619. break;
  7620. default:
  7621. lua_interface->LogError("%s: LUA RemoveImmunitySpell command error: invalid immunity type", lua_interface->GetScriptName(state));
  7622. }
  7623. }
  7624. else {
  7625. bool should_break = false;
  7626. spell->MSpellTargets.readlock(__FUNCTION__, __LINE__);
  7627. for (int8 i = 0; i < spell->targets.size(); i++) {
  7628. spawn = spell->caster->GetZone()->GetSpawnByID(spell->targets.at(i));
  7629. if (!spawn || !spawn->IsEntity())
  7630. continue;
  7631. Entity* entity = ((Entity*)spawn);
  7632. switch (type) {
  7633. case IMMUNITY_TYPE_AOE:
  7634. entity->RemoveAOEImmunity(spell);
  7635. break;
  7636. case IMMUNITY_TYPE_STUN:
  7637. entity->RemoveStunImmunity(spell);
  7638. break;
  7639. case IMMUNITY_TYPE_ROOT:
  7640. entity->RemoveRootImmunity(spell);
  7641. break;
  7642. case IMMUNITY_TYPE_DAZE:
  7643. entity->RemoveDazeImmunity(spell);
  7644. break;
  7645. case IMMUNITY_TYPE_FEAR:
  7646. entity->RemoveFearImmunity(spell);
  7647. break;
  7648. case IMMUNITY_TYPE_MEZ:
  7649. entity->RemoveMezImmunity(spell);
  7650. break;
  7651. case IMMUNITY_TYPE_STIFLE:
  7652. entity->RemoveStifleImmunity(spell);
  7653. break;
  7654. default:
  7655. lua_interface->LogError("%s: LUA RemoveImmunitySpell command error: invalid immunity type", lua_interface->GetScriptName(state));
  7656. should_break = true;
  7657. }
  7658. if (should_break)
  7659. break;
  7660. }
  7661. spell->MSpellTargets.releasereadlock(__FUNCTION__, __LINE__);
  7662. }
  7663. return 0;
  7664. }
  7665. int EQ2Emu_lua_SetSpellSnareValue(lua_State* state) {
  7666. if (!lua_interface)
  7667. return 0;
  7668. LuaSpell* spell = lua_interface->GetCurrentSpell(state);
  7669. if (!spell) {
  7670. lua_interface->LogError("%s: LUA SetSpellSnareValue command error: This can only be used in a spell script!", lua_interface->GetScriptName(state));
  7671. return 0;
  7672. }
  7673. float snare = lua_interface->GetFloatValue(state);
  7674. Spawn* spawn = lua_interface->GetSpawn(state, 2);
  7675. // convert the val to the speed multipler value (100 - val)
  7676. float val = 100.0 - snare;
  7677. val /= 100.0;
  7678. if (spawn) {
  7679. if (!spawn->IsEntity()) {
  7680. lua_interface->LogError("%s: LUA SetSpellSnareValue command error: spawn must be an entity.", lua_interface->GetScriptName(state));
  7681. return 0;
  7682. }
  7683. ((Entity*)spawn)->SetSnareValue(spell, val);
  7684. }
  7685. else {
  7686. spell->MSpellTargets.readlock(__FUNCTION__, __LINE__);
  7687. for (int8 i = 0; i < spell->targets.size(); i++) {
  7688. spawn = spell->caster->GetZone()->GetSpawnByID(spell->targets.at(i));
  7689. if (!spawn || !spawn->IsEntity())
  7690. continue;
  7691. ((Entity*)spawn)->SetSnareValue(spell, val);
  7692. }
  7693. spell->MSpellTargets.releasereadlock(__FUNCTION__, __LINE__);
  7694. }
  7695. return 0;
  7696. }
  7697. int EQ2Emu_lua_CheckRaceType(lua_State* state) {
  7698. if (!lua_interface)
  7699. return 0;
  7700. Spawn* spawn = lua_interface->GetSpawn(state);
  7701. int16 race_id = lua_interface->GetInt16Value(state, 2);
  7702. if (!spawn) {
  7703. lua_interface->LogError("%s: LUA CheckRaceType command error: spawn is not valid", lua_interface->GetScriptName(state));
  7704. return 0;
  7705. }
  7706. if (race_id == 0) {
  7707. lua_interface->LogError("%s: LUA CheckRaceType command error: race id must be set", lua_interface->GetScriptName(state));
  7708. return 0;
  7709. }
  7710. lua_interface->SetBooleanValue(state, (race_id == race_types_list.GetRaceType(spawn->GetModelType()) || race_id == race_types_list.GetRaceBaseType(spawn->GetModelType())));
  7711. return 1;
  7712. }
  7713. int EQ2Emu_lua_GetRaceType(lua_State* state) {
  7714. if (!lua_interface)
  7715. return 0;
  7716. Spawn* spawn = lua_interface->GetSpawn(state);
  7717. if (!spawn) {
  7718. lua_interface->LogError("%s: LUA GetRaceType command error: spawn is not valid", lua_interface->GetScriptName(state));
  7719. return 0;
  7720. }
  7721. lua_interface->SetInt32Value(state, race_types_list.GetRaceType(spawn->GetModelType()));
  7722. return 1;
  7723. }
  7724. int EQ2Emu_lua_GetRaceBaseType(lua_State* state) {
  7725. if (!lua_interface)
  7726. return 0;
  7727. Spawn* spawn = lua_interface->GetSpawn(state);
  7728. if (!spawn) {
  7729. lua_interface->LogError("%s: LUA GetRaceBaseType command error: spawn is not valid", lua_interface->GetScriptName(state));
  7730. return 0;
  7731. }
  7732. lua_interface->SetInt32Value(state, race_types_list.GetRaceBaseType(spawn->GetModelType()));
  7733. return 1;
  7734. }
  7735. int EQ2Emu_lua_GetSpellName(lua_State* state) {
  7736. if (!lua_interface)
  7737. return 0;
  7738. LuaSpell* spell = lua_interface->GetCurrentSpell(state);
  7739. if (!spell) {
  7740. lua_interface->LogError("%s: LUA GetSpellName command error: this function must be used from a spell script!", lua_interface->GetScriptName(state));
  7741. return 0;
  7742. }
  7743. lua_interface->SetStringValue(state, spell->spell->GetName());
  7744. return 1;
  7745. }
  7746. int EQ2Emu_lua_GetQuestFlags(lua_State* state) {
  7747. if (!lua_interface)
  7748. return 0;
  7749. Quest* quest = lua_interface->GetQuest(state);
  7750. if (!quest) {
  7751. lua_interface->LogError("%s: LUA GetQuestFlags command error: quest is not valid.", lua_interface->GetScriptName(state));
  7752. return 0;
  7753. }
  7754. lua_interface->SetInt32Value(state, quest->GetQuestFlags());
  7755. return 1;
  7756. }
  7757. int EQ2Emu_lua_SetQuestFlags(lua_State* state) {
  7758. if (!lua_interface)
  7759. return 0;
  7760. Quest* quest = lua_interface->GetQuest(state);
  7761. int32 flags = lua_interface->GetInt32Value(state, 2);
  7762. if (!quest) {
  7763. lua_interface->LogError("%s: LUA SetQuestFlags command error: quest is not valid.", lua_interface->GetScriptName(state));
  7764. return 0;
  7765. }
  7766. quest->SetQuestFlags(flags);
  7767. return 0;
  7768. }
  7769. int EQ2Emu_lua_SetQuestTimer(lua_State* state) {
  7770. if (!lua_interface)
  7771. return 0;
  7772. Quest* quest = lua_interface->GetQuest(state);
  7773. Spawn* player = lua_interface->GetSpawn(state, 2);
  7774. int32 step = lua_interface->GetInt32Value(state, 3);
  7775. int32 duration = lua_interface->GetInt32Value(state, 4);
  7776. string action = lua_interface->GetStringValue(state, 5);
  7777. if (!quest) {
  7778. lua_interface->LogError("%s: LUA SetQuestTimer command error: quest is not valid.", lua_interface->GetScriptName(state));
  7779. return 0;
  7780. }
  7781. if (!player) {
  7782. lua_interface->LogError("%s: LUA SetQuestTimer command error: player is not a valid spawn.", lua_interface->GetScriptName(state));
  7783. return 0;
  7784. }
  7785. if (!player->IsPlayer()) {
  7786. lua_interface->LogError("%s: LUA SetQuestTimer command error: player is not a valid player.", lua_interface->GetScriptName(state));
  7787. return 0;
  7788. }
  7789. if (step == 0) {
  7790. lua_interface->LogError("%s: LUA SetQuestTimer command error: step must be set.", lua_interface->GetScriptName(state));
  7791. return 0;
  7792. }
  7793. if (duration == 0) {
  7794. lua_interface->LogError("%s: LUA SetQuestTimer command error: duration must be set.", lua_interface->GetScriptName(state));
  7795. return 0;
  7796. }
  7797. if (action.length() == 0) {
  7798. lua_interface->LogError("%s: LUA SetQuestTimer command error: failed action must be set.", lua_interface->GetScriptName(state));
  7799. return 0;
  7800. }
  7801. Client* client = player->GetZone()->GetClientBySpawn(player);
  7802. if (!client) {
  7803. lua_interface->LogError("%s: LUA SetQuestTimer command error: failed to get a valid client pointer for the given player", lua_interface->GetScriptName(state));
  7804. return 0;
  7805. }
  7806. quest->SetTimerStep(step);
  7807. quest->AddFailedAction(step, action);
  7808. quest->SetStepTimer(duration);
  7809. client->AddQuestTimer(quest->GetQuestID());
  7810. return 0;
  7811. }
  7812. int EQ2Emu_lua_SetQuestTimerComplete(lua_State* state) {
  7813. if (!lua_interface)
  7814. return 0;
  7815. Quest* quest = lua_interface->GetQuest(state);
  7816. Spawn* player = lua_interface->GetSpawn(state, 2);
  7817. if (!quest) {
  7818. lua_interface->LogError("%s: LUA SetQuestTimerComplete command error: quest is not valid.", lua_interface->GetScriptName(state));
  7819. return 0;
  7820. }
  7821. if (!player) {
  7822. lua_interface->LogError("%s: LUA SetQuestTimerComplete command error: player is not a valid spawn.", lua_interface->GetScriptName(state));
  7823. return 0;
  7824. }
  7825. if (!player->IsPlayer()) {
  7826. lua_interface->LogError("%s: LUA SetQuestTimerComplete command error: player is not a valid player.", lua_interface->GetScriptName(state));
  7827. return 0;
  7828. }
  7829. Client* client = player->GetZone()->GetClientBySpawn(player);
  7830. if (!client) {
  7831. lua_interface->LogError("%s: LUA SetQuestTimerComplete command error: failed to get a valid client pointer for the given player", lua_interface->GetScriptName(state));
  7832. return 0;
  7833. }
  7834. quest->SetTimerStep(0);
  7835. quest->SetStepTimer(0);
  7836. client->RemoveQuestTimer(quest->GetQuestID());
  7837. return 0;
  7838. }
  7839. int EQ2Emu_lua_RemoveQuestStep(lua_State* state) {
  7840. if (!lua_interface)
  7841. return 0;
  7842. Spawn* player = lua_interface->GetSpawn(state);
  7843. Quest* quest = lua_interface->GetQuest(state, 2);
  7844. int32 step = lua_interface->GetInt32Value(state, 3);
  7845. if (!player) {
  7846. lua_interface->LogError("%s: LUA RemoveQuestStep command error: player is not a valid spawn.", lua_interface->GetScriptName(state));
  7847. return 0;
  7848. }
  7849. if (!player->IsPlayer()) {
  7850. lua_interface->LogError("%s: LUA RemoveQuestStep command error: player is not a valid player.", lua_interface->GetScriptName(state));
  7851. return 0;
  7852. }
  7853. if (!quest) {
  7854. lua_interface->LogError("%s: LUA RemoveQuestStep command error: quest is not valid.", lua_interface->GetScriptName(state));
  7855. return 0;
  7856. }
  7857. if (step == 0) {
  7858. lua_interface->LogError("%s: LUA RemoveQuestStep command error: step must be set.", lua_interface->GetScriptName(state));
  7859. return 0;
  7860. }
  7861. Client* client = player->GetZone()->GetClientBySpawn(player);
  7862. if (!client) {
  7863. lua_interface->LogError("%s: LUA RemoveQuestStep command error: unable to get a valid client pointer from the given player.", lua_interface->GetScriptName(state));
  7864. return 0;
  7865. }
  7866. if (quest->RemoveQuestStep(step, client)) {
  7867. client->QueuePacket(quest->QuestJournalReply(client->GetVersion(), client->GetNameCRC(), (Player*)player, 0, 0, 0, true));
  7868. client->GetCurrentZone()->SendQuestUpdates(client);
  7869. }
  7870. else
  7871. lua_interface->LogError("LUA RemoveQuestStep command error: unable to remove the step (%u) from the quest (%s).", step, quest->GetName());
  7872. return 0;
  7873. }
  7874. int EQ2Emu_lua_ResetQuestStep(lua_State* state) {
  7875. if (!lua_interface)
  7876. return 0;
  7877. Quest* quest = lua_interface->GetQuest(state, 1);
  7878. int32 step = lua_interface->GetInt32Value(state, 2);
  7879. string desc = lua_interface->GetStringValue(state, 3);
  7880. string task_group = lua_interface->GetStringValue(state, 4);
  7881. if (!quest) {
  7882. lua_interface->LogError("%s: LUA ResetQuestStep command error: quest is not valid.", lua_interface->GetScriptName(state));
  7883. return 0;
  7884. }
  7885. if (step == 0) {
  7886. lua_interface->LogError("%s: LUA ResetQuestStep command error: step must be set.", lua_interface->GetScriptName(state));
  7887. return 0;
  7888. }
  7889. QuestStep* quest_step = quest->GetQuestStep(step);
  7890. if (!quest_step) {
  7891. lua_interface->LogError("%s: LUA ResetQuestStep command error: unable to get a valid quest step.", lua_interface->GetScriptName(state));
  7892. return 0;
  7893. }
  7894. quest_step->SetStepProgress(0);
  7895. quest_step->SetTaskGroup(task_group);
  7896. quest_step->SetDescription(desc);
  7897. return 0;
  7898. }
  7899. int EQ2Emu_lua_AddQuestStepFailureAction(lua_State* state) {
  7900. if (!lua_interface)
  7901. return 0;
  7902. Quest* quest = lua_interface->GetQuest(state);
  7903. int32 step = lua_interface->GetInt32Value(state, 2);
  7904. string action = lua_interface->GetStringValue(state, 3);
  7905. if (!quest) {
  7906. lua_interface->LogError("%s: LUA AddQuestStepFailureAction command error: quest is not valid.", lua_interface->GetScriptName(state));
  7907. return 0;
  7908. }
  7909. if (step == 0) {
  7910. lua_interface->LogError("%s: LUA AddQuestStepFailureAction command error: step must be set.", lua_interface->GetScriptName(state));
  7911. return 0;
  7912. }
  7913. if (action.length() == 0) {
  7914. lua_interface->LogError("%s: LUA AddQuestStepFailureAction command error: action must be set.", lua_interface->GetScriptName(state));
  7915. return 0;
  7916. }
  7917. quest->AddFailedAction(step, action);
  7918. return 0;
  7919. }
  7920. int EQ2Emu_lua_SetStepFailed(lua_State* state) {
  7921. if (!lua_interface)
  7922. return 0;
  7923. Spawn* player = lua_interface->GetSpawn(state);
  7924. int32 quest_id = lua_interface->GetInt32Value(state, 2);
  7925. int32 step = lua_interface->GetInt32Value(state, 3);
  7926. if (!player) {
  7927. lua_interface->LogError("%s: LUA SetStepFailed command error: player is not a valid spawn.", lua_interface->GetScriptName(state));
  7928. return 0;
  7929. }
  7930. if (!player->IsPlayer()) {
  7931. lua_interface->LogError("%s: LUA SetStepFailed command error: player is not a valid player.", lua_interface->GetScriptName(state));
  7932. return 0;
  7933. }
  7934. if (quest_id == 0) {
  7935. lua_interface->LogError("%s: LUA SetStepFailed command error: quest_id must be set.", lua_interface->GetScriptName(state));
  7936. return 0;
  7937. }
  7938. if (step == 0) {
  7939. lua_interface->LogError("%s: LUA SetStepFailed command error: step must be set.", lua_interface->GetScriptName(state));
  7940. return 0;
  7941. }
  7942. Quest* quest = ((Player*)player)->GetQuest(quest_id);
  7943. if (!quest) {
  7944. lua_interface->LogError("LUA SetStepFailed command error: unable to get a valid quest from the given id (%u).", quest_id);
  7945. return 0;
  7946. }
  7947. quest->StepFailed(step);
  7948. return 0;
  7949. }
  7950. int EQ2Emu_lua_GetQuestCompleteCount(lua_State* state) {
  7951. if (!lua_interface)
  7952. return 0;
  7953. Spawn* player = lua_interface->GetSpawn(state);
  7954. int32 quest_id = lua_interface->GetInt32Value(state, 2);
  7955. if (!player) {
  7956. lua_interface->LogError("%s: LUA GetQuestCompleteCount command error: player is not a valid spawn", lua_interface->GetScriptName(state));
  7957. return 0;
  7958. }
  7959. if (!player->IsPlayer()) {
  7960. lua_interface->LogError("%s: LUA GetQuestCompleteCount command error: player is not a valid player", lua_interface->GetScriptName(state));
  7961. return 0;
  7962. }
  7963. if (quest_id == 0) {
  7964. lua_interface->LogError("%s: LUA GetQuestCompleteCount command error: quest id is not valid", lua_interface->GetScriptName(state));
  7965. return 0;
  7966. }
  7967. Quest* quest = ((Player*)player)->GetCompletedQuest(quest_id);
  7968. if (!quest) {
  7969. lua_interface->SetInt32Value(state, 0);
  7970. return 1;
  7971. }
  7972. lua_interface->SetInt32Value(state, quest->GetCompleteCount());
  7973. return 1;
  7974. }
  7975. int EQ2Emu_lua_SetServerVariable(lua_State* state) {
  7976. if (!lua_interface)
  7977. return 0;
  7978. string name = lua_interface->GetStringValue(state);
  7979. string value = lua_interface->GetStringValue(state, 2);
  7980. string comment = lua_interface->GetStringValue(state, 3);
  7981. if (name.length() == 0) {
  7982. lua_interface->LogError("%s: LUA SetServerVariable command error: name is not valid", lua_interface->GetScriptName(state));
  7983. return 0;
  7984. }
  7985. if (value.length() == 0) {
  7986. lua_interface->LogError("%s: LUA SetServerVariable command error: value is not valid", lua_interface->GetScriptName(state));
  7987. return 0;
  7988. }
  7989. string varname = string("lua_").append(name);
  7990. Variable* var = variables.FindVariable(varname);
  7991. if (var)
  7992. var->SetValue(value.c_str());
  7993. else {
  7994. var = new Variable(varname.c_str(), value.c_str(), comment.c_str());
  7995. variables.AddVariable(var);
  7996. }
  7997. database.SaveVariable(var->GetName(), var->GetValue(), var->GetComment());
  7998. return 0;
  7999. }
  8000. int EQ2Emu_lua_GetServerVariable(lua_State* state) {
  8001. if (!lua_interface)
  8002. return 0;
  8003. string name = lua_interface->GetStringValue(state);
  8004. if (name.length() == 0) {
  8005. lua_interface->LogError("%s: LUA GetServerVariable command error: name is not valid", lua_interface->GetScriptName(state));
  8006. return 0;
  8007. }
  8008. string varname = string("lua_").append(name);
  8009. Variable* var = variables.FindVariable(varname);
  8010. if (var)
  8011. lua_interface->SetStringValue(state, var->GetValue());
  8012. else
  8013. lua_interface->SetStringValue(state, "NULL");
  8014. return 1;
  8015. }
  8016. int EQ2Emu_lua_HasLanguage(lua_State* state) {
  8017. if (!lua_interface)
  8018. return 0;
  8019. Spawn* player = lua_interface->GetSpawn(state);
  8020. int32 language_id = lua_interface->GetInt32Value(state, 2);
  8021. if (!player) {
  8022. lua_interface->LogError("%s: LUA HasLanguage command error: player is not valid", lua_interface->GetScriptName(state));
  8023. return 0;
  8024. }
  8025. if (!player->IsPlayer()) {
  8026. lua_interface->LogError("%s: LUA HasLanguage command error: player is not a valid player", lua_interface->GetScriptName(state));
  8027. return 0;
  8028. }
  8029. lua_interface->SetBooleanValue(state, ((Player*)player)->HasLanguage(language_id));
  8030. return 1;
  8031. }
  8032. int EQ2Emu_lua_AddLanguage(lua_State* state) {
  8033. if (!lua_interface)
  8034. return 0;
  8035. Spawn* player = lua_interface->GetSpawn(state);
  8036. int32 language_id = lua_interface->GetInt32Value(state, 2);
  8037. if (!player) {
  8038. lua_interface->LogError("%s: LUA AddLanguage command error: player is not valid", lua_interface->GetScriptName(state));
  8039. return 0;
  8040. }
  8041. if (!player->IsPlayer()) {
  8042. lua_interface->LogError("%s: LUA AddLanguage command error: player is not a valid player", lua_interface->GetScriptName(state));
  8043. return 0;
  8044. }
  8045. Language* language = master_languages_list.GetLanguage(language_id);
  8046. if (language)
  8047. ((Player*)player)->AddLanguage(language->GetID(), language->GetName(), true);
  8048. return 0;
  8049. }
  8050. int EQ2Emu_lua_IsNight(lua_State* state) {
  8051. if (!lua_interface)
  8052. return 0;
  8053. ZoneServer* zone = lua_interface->GetZone(state);
  8054. if (!zone) {
  8055. lua_interface->LogError("%s: LUA IsNight command error: zone is not valid", lua_interface->GetScriptName(state));
  8056. return 0;
  8057. }
  8058. lua_interface->SetBooleanValue(state, zone->IsDusk());
  8059. return 1;
  8060. }
  8061. int EQ2Emu_lua_AddMultiFloorLift(lua_State* state) {
  8062. if (!lua_interface)
  8063. return 0;
  8064. Spawn* spawn = lua_interface->GetSpawn(state);
  8065. if (!spawn) {
  8066. lua_interface->LogError("%s: LUA AddMultiFloorLift command error: spawn is not valid", lua_interface->GetScriptName(state));
  8067. return 0;
  8068. }
  8069. if (!spawn->IsWidget()) {
  8070. lua_interface->LogError("%s: LUA AddMultiFloorLift command error: spawn is not a widget", lua_interface->GetScriptName(state));
  8071. return 0;
  8072. }
  8073. ((Widget*)spawn)->SetMultiFloorLift(true);
  8074. if (spawn->GetZone())
  8075. spawn->GetZone()->AddTransportSpawn(spawn);
  8076. return 0;
  8077. }
  8078. int EQ2Emu_lua_StartAutoMount(lua_State* state) {
  8079. if (!lua_interface)
  8080. return 0;
  8081. Spawn* player = lua_interface->GetSpawn(state);
  8082. int32 path = lua_interface->GetInt32Value(state, 2);
  8083. if (!player) {
  8084. lua_interface->LogError("%s: LUA StartAutoMount command error: spawn is not valid", lua_interface->GetScriptName(state));
  8085. return 0;
  8086. }
  8087. if (!player->IsPlayer()) {
  8088. lua_interface->LogError("%s: LUA StartAutoMount command error: spawn is not a player", lua_interface->GetScriptName(state));
  8089. return 0;
  8090. }
  8091. if (path == 0) {
  8092. lua_interface->LogError("%s: LUA StartAutoMount command error: path must be greater then zero", lua_interface->GetScriptName(state));
  8093. return 0;
  8094. }
  8095. Client* client = player->GetZone()->GetClientBySpawn(player);
  8096. if (!client) {
  8097. lua_interface->LogError("%s: LUA StartAutoMount command error: unable to get a client from the given player", lua_interface->GetScriptName(state));
  8098. return 0;
  8099. }
  8100. client->SendFlightAutoMount(path);
  8101. return 0;
  8102. }
  8103. int EQ2Emu_lua_EndAutoMount(lua_State* state) {
  8104. if (!lua_interface)
  8105. return 0;
  8106. Spawn* player = lua_interface->GetSpawn(state);
  8107. if (!player) {
  8108. lua_interface->LogError("%s: LUA EndAutoMount command error: spawn is not valid", lua_interface->GetScriptName(state));
  8109. return 0;
  8110. }
  8111. if (!player->IsPlayer()) {
  8112. lua_interface->LogError("%s: LUA EndAutoMount command error: spawn is not a player", lua_interface->GetScriptName(state));
  8113. return 0;
  8114. }
  8115. Client* client = player->GetZone()->GetClientBySpawn(player);
  8116. if (!client) {
  8117. lua_interface->LogError("%s: LUA EndAutoMount command error: unable to get a client from the given player", lua_interface->GetScriptName(state));
  8118. return 0;
  8119. }
  8120. client->EndAutoMount();
  8121. return 0;
  8122. }
  8123. int EQ2Emu_lua_IsOnAutoMount(lua_State* state) {
  8124. if (!lua_interface)
  8125. return 0;
  8126. Spawn* player = lua_interface->GetSpawn(state);
  8127. if (!player) {
  8128. lua_interface->LogError("%s: LUA IsOnAutoMount command error: spawn in not valid", lua_interface->GetScriptName(state));
  8129. return 0;
  8130. }
  8131. if (!player->IsPlayer()) {
  8132. lua_interface->LogError("%s: LUA IsOnAutoMount command error: spawn is not a player", lua_interface->GetScriptName(state));
  8133. return 0;
  8134. }
  8135. Client* client = player->GetZone()->GetClientBySpawn(player);
  8136. if (!client) {
  8137. lua_interface->LogError("%s: LUA IsOnAutoMount command error: unable to get a client from the given player", lua_interface->GetScriptName(state));
  8138. return 0;
  8139. }
  8140. lua_interface->SetBooleanValue(state, client->GetOnAutoMount());
  8141. return 1;
  8142. }
  8143. int EQ2Emu_lua_SetPlayerHistory(lua_State* state) {
  8144. if (!lua_interface)
  8145. return 0;
  8146. Spawn* player = lua_interface->GetSpawn(state);
  8147. int32 event_id = lua_interface->GetInt32Value(state, 2);
  8148. int32 value = lua_interface->GetInt32Value(state, 3);
  8149. int32 value2 = lua_interface->GetInt32Value(state, 4);
  8150. if (!player) {
  8151. lua_interface->LogError("%s: LUA SetPlayerHistory command error: spawn is not valid", lua_interface->GetScriptName(state));
  8152. return 0;
  8153. }
  8154. if (!player->IsPlayer()) {
  8155. lua_interface->LogError("%s: LUA SetPlayerHistory command error: spawn is not a player", lua_interface->GetScriptName(state));
  8156. return 0;
  8157. }
  8158. ((Player*)player)->UpdateLUAHistory(event_id, value, value2);
  8159. return 0;
  8160. }
  8161. int EQ2Emu_lua_GetPlayerHistory(lua_State* state) {
  8162. if (!lua_interface)
  8163. return 0;
  8164. Spawn* player = lua_interface->GetSpawn(state);
  8165. int32 event_id = lua_interface->GetInt32Value(state, 2);
  8166. if (!player) {
  8167. lua_interface->LogError("%s: LUA GetPlayerHistory command error: spawn is not valid", lua_interface->GetScriptName(state));
  8168. return 0;
  8169. }
  8170. if (!player->IsPlayer()) {
  8171. lua_interface->LogError("%s: LUA GetPlayerHistory command error: spawn is not a player", lua_interface->GetScriptName(state));
  8172. return 0;
  8173. }
  8174. LUAHistory* hd = ((Player*)player)->GetLUAHistory(event_id);
  8175. if (!hd)
  8176. return 0;
  8177. lua_interface->SetInt32Value(state, hd->Value);
  8178. lua_interface->SetInt32Value(state, hd->Value2);
  8179. return 2;
  8180. }
  8181. int EQ2Emu_lua_SetGridID(lua_State* state) {
  8182. if (!lua_interface)
  8183. return 0;
  8184. Spawn* spawn = lua_interface->GetSpawn(state);
  8185. int32 grid = lua_interface->GetInt32Value(state, 2);
  8186. if (!spawn) {
  8187. lua_interface->LogError("%s: LUA SetGridID command error: spawn is not valid", lua_interface->GetScriptName(state));
  8188. return 0;
  8189. }
  8190. if (grid == 0) {
  8191. lua_interface->LogError("%s: LUA SetGridID command error: grid is not valid", lua_interface->GetScriptName(state));
  8192. return 0;
  8193. }
  8194. spawn->SetPos(&(spawn->appearance.pos.grid_id), grid);
  8195. return 0;
  8196. }
  8197. int EQ2Emu_lua_SetRequiredHistory(lua_State* state) {
  8198. if (!lua_interface)
  8199. return 0;
  8200. Spawn* spawn = lua_interface->GetSpawn(state);
  8201. int32 event_id = lua_interface->GetInt32Value(state, 2);
  8202. int32 value1 = lua_interface->GetInt32Value(state, 3);
  8203. int32 value2 = lua_interface->GetInt32Value(state, 4);
  8204. bool private_spawn = (lua_interface->GetInt8Value(state, 5) == 1);
  8205. int16 flag_override = lua_interface->GetInt16Value(state, 6);
  8206. if (!spawn) {
  8207. lua_interface->LogError("%s: LUA SetRequiredHistory command error: spawn is not valid", lua_interface->GetScriptName(state));
  8208. return 0;
  8209. }
  8210. //Add this quest to the list of required quests for this spawn
  8211. spawn->SetRequiredHistory(event_id, value1, value2);
  8212. //If private spawn value set
  8213. if (private_spawn) {
  8214. //Set the spawn to be private when not granted access via history
  8215. spawn->AddAllowAccessSpawn(spawn);
  8216. spawn->SetPrivateQuestSpawn(true);
  8217. }
  8218. //This value will override vis_flags in the vis packet
  8219. if (flag_override > 0)
  8220. spawn->SetQuestsRequiredOverride(flag_override);
  8221. return 0;
  8222. }
  8223. int EQ2Emu_lua_GetQuestStepProgress(lua_State* state) {
  8224. if (!lua_interface)
  8225. return 0;
  8226. Spawn* player = lua_interface->GetSpawn(state);
  8227. int32 quest_id = lua_interface->GetInt32Value(state, 2);
  8228. int32 step_id = lua_interface->GetInt32Value(state, 3);
  8229. if (!player) {
  8230. lua_interface->LogError("%s: LUA GetQuestStepProgress command error: spawn is not valid", lua_interface->GetScriptName(state));
  8231. return 0;
  8232. }
  8233. if (!player->IsPlayer()) {
  8234. lua_interface->LogError("%s: LUA GetQuestStepProgress command error: spawn is not a player", lua_interface->GetScriptName(state));
  8235. return 0;
  8236. }
  8237. lua_interface->SetInt32Value(state, ((Player*)player)->GetStepProgress(quest_id, step_id));
  8238. return 1;
  8239. }
  8240. int EQ2Emu_lua_SetPlayerLevel(lua_State* state) {
  8241. if (!lua_interface)
  8242. return 0;
  8243. Spawn* player = lua_interface->GetSpawn(state);
  8244. int8 level = lua_interface->GetInt8Value(state, 2);
  8245. if (!player) {
  8246. lua_interface->LogError("%s: LUA SetPlayerLevel command error: spawn is not valid", lua_interface->GetScriptName(state));
  8247. return 0;
  8248. }
  8249. if (!player->IsPlayer()) {
  8250. lua_interface->LogError("%s: LUA SetPlayerLevel command error: spawn is not a player", lua_interface->GetScriptName(state));
  8251. return 0;
  8252. }
  8253. if (level == 0) {
  8254. lua_interface->LogError("%s: LUA SetPlayerLevel command error: new level can't be 0", lua_interface->GetScriptName(state));
  8255. return 0;
  8256. }
  8257. Client* client = player->GetZone()->GetClientBySpawn(player);
  8258. if (!client) {
  8259. lua_interface->LogError("%s: LUA SetPlayerLevel command error: unable to get a client from the given spawn", lua_interface->GetScriptName(state));
  8260. return 0;
  8261. }
  8262. client->ChangeLevel(client->GetPlayer()->GetLevel(), level);
  8263. return 0;
  8264. }
  8265. int EQ2Emu_lua_AddCoin(lua_State* state) {
  8266. if (!lua_interface)
  8267. return 0;
  8268. Spawn* player = lua_interface->GetSpawn(state);
  8269. int32 amount = lua_interface->GetInt32Value(state, 2);
  8270. if (!player) {
  8271. lua_interface->LogError("%s: LUA AddCoin command error: spawn is not valid", lua_interface->GetScriptName(state));
  8272. return 0;
  8273. }
  8274. if (!player->IsPlayer()) {
  8275. lua_interface->LogError("%s: LUA AddCoin command error: spawn is not a player", lua_interface->GetScriptName(state));
  8276. return 0;
  8277. }
  8278. if (amount == 0) {
  8279. lua_interface->LogError("%s: LUA AddCoin command error: amount must be greater then 0", lua_interface->GetScriptName(state));
  8280. return 0;
  8281. }
  8282. ((Player*)player)->AddCoins(amount);
  8283. return 0;
  8284. }
  8285. int EQ2Emu_lua_RemoveCoin(lua_State* state) {
  8286. if (!lua_interface)
  8287. return 0;
  8288. Spawn* player = lua_interface->GetSpawn(state);
  8289. int32 amount = lua_interface->GetInt32Value(state, 2);
  8290. if (!player) {
  8291. lua_interface->LogError("%s: LUA RemoveCoin command error: spawn is not valid", lua_interface->GetScriptName(state));
  8292. return 0;
  8293. }
  8294. if (!player->IsPlayer()) {
  8295. lua_interface->LogError("%s: LUA RemoveCoin command error: spawn is not a player", lua_interface->GetScriptName(state));
  8296. return 0;
  8297. }
  8298. if (amount == 0) {
  8299. lua_interface->LogError("%s: LUA RemoveCoin command error: amount must be greater then 0", lua_interface->GetScriptName(state));
  8300. return 0;
  8301. }
  8302. lua_interface->SetBooleanValue(state, ((Player*)player)->RemoveCoins(amount));
  8303. return 1;
  8304. }
  8305. int EQ2Emu_lua_GetPlayersInZone(lua_State* state) {
  8306. if (!lua_interface)
  8307. return 0;
  8308. ZoneServer* zone = lua_interface->GetZone(state);
  8309. if (!zone) {
  8310. lua_interface->LogError("%s: LUA GetPlayersInZone command error: zone is not valid", lua_interface->GetScriptName(state));
  8311. return 0;
  8312. }
  8313. vector<Entity*> players = zone->GetPlayers();
  8314. if (players.size() == 0)
  8315. return 0;
  8316. lua_createtable(state, players.size(), 0);
  8317. int newTable = lua_gettop(state);
  8318. for (int32 i = 0; i < players.size(); i++) {
  8319. lua_interface->SetSpawnValue(state, players.at(i));
  8320. lua_rawseti(state, newTable, i + 1);
  8321. }
  8322. return 1;
  8323. }
  8324. int EQ2Emu_lua_SpawnGroupByID(lua_State* state) {
  8325. if (!lua_interface)
  8326. return 0;
  8327. ZoneServer* zone = lua_interface->GetZone(state, 1);
  8328. if (!zone) {
  8329. lua_interface->LogError("%s: LUA GetPlayersInZone command error: zone is not valid", lua_interface->GetScriptName(state));
  8330. return 0;
  8331. }
  8332. int32 group_id = lua_interface->GetInt32Value(state, 2);
  8333. //Map of <placement_id, location_id>
  8334. map<int32, int32>* locs = zone->GetSpawnLocationsByGroup(group_id);
  8335. map<int32, int32>::iterator itr;
  8336. vector<Spawn*> group;
  8337. for (itr = locs->begin(); itr != locs->end(); itr++) {
  8338. SpawnLocation* location = zone->GetSpawnLocation(itr->second);
  8339. if (!location) {
  8340. lua_interface->LogError("LUA SpawnByLocationID command error: no location found for the given ID (%u)", itr->second);
  8341. return 0;
  8342. }
  8343. Spawn* spawn = 0;
  8344. if (location->entities[0]) {
  8345. if (location->entities[0]->spawn_type == SPAWN_ENTRY_TYPE_NPC)
  8346. spawn = zone->AddNPCSpawn(location, location->entities[0]);
  8347. else if (location->entities[0]->spawn_type == SPAWN_ENTRY_TYPE_GROUNDSPAWN)
  8348. spawn = zone->AddGroundSpawn(location, location->entities[0]);
  8349. else if (location->entities[0]->spawn_type == SPAWN_ENTRY_TYPE_OBJECT)
  8350. spawn = zone->AddObjectSpawn(location, location->entities[0]);
  8351. else if (location->entities[0]->spawn_type == SPAWN_ENTRY_TYPE_WIDGET)
  8352. spawn = zone->AddWidgetSpawn(location, location->entities[0]);
  8353. else if (location->entities[0]->spawn_type == SPAWN_ENTRY_TYPE_SIGN)
  8354. spawn = zone->AddSignSpawn(location, location->entities[0]);
  8355. if (spawn) {
  8356. const char* script = 0;
  8357. for (int x = 0; x < 3; x++) {
  8358. switch (x) {
  8359. case 0:
  8360. script = world.GetSpawnEntryScript(location->entities[0]->spawn_entry_id);
  8361. break;
  8362. case 1:
  8363. script = world.GetSpawnLocationScript(location->entities[0]->spawn_location_id);
  8364. break;
  8365. case 2:
  8366. script = world.GetSpawnScript(location->entities[0]->spawn_id);
  8367. break;
  8368. }
  8369. if (script && lua_interface->GetSpawnScript(script) != 0) {
  8370. spawn->SetSpawnScript(string(script));
  8371. break;
  8372. }
  8373. }
  8374. zone->CallSpawnScript(spawn, SPAWN_SCRIPT_SPAWN);
  8375. lua_interface->SetSpawnValue(state, spawn);
  8376. group.push_back(spawn);
  8377. }
  8378. else {
  8379. LogWrite(ZONE__ERROR, 0, "Zone", "Error adding spawn to zone");
  8380. safe_delete(spawn);
  8381. }
  8382. }
  8383. }
  8384. if (!group.empty()) {
  8385. lua_createtable(state, group.size(), 0);
  8386. int newTable = lua_gettop(state);
  8387. for (int32 i = 0; i < group.size(); i++) {
  8388. lua_interface->SetSpawnValue(state, group[i]);
  8389. lua_rawseti(state, newTable, i + 1);
  8390. }
  8391. }
  8392. else
  8393. lua_pushnil(state);
  8394. return 1;
  8395. }
  8396. int EQ2Emu_lua_SetSpawnAnimation(lua_State* state) {
  8397. if (!lua_interface)
  8398. return 0;
  8399. Spawn* spawn = lua_interface->GetSpawn(state, 1);
  8400. int32 anim_id = lua_interface->GetInt32Value(state, 2);
  8401. int16 leeway = lua_interface->GetInt16Value(state, 3);
  8402. if (!spawn) {
  8403. lua_interface->LogError("%s: LUA SetSpawnAnimation command error: spawn is not valid", lua_interface->GetScriptName(state));
  8404. return 0;
  8405. }
  8406. if (anim_id == 0) {
  8407. lua_interface->LogError("%s: LUA SetSpawnAnimation command error: anim_id is not valid", lua_interface->GetScriptName(state));
  8408. return 0;
  8409. }
  8410. if (leeway == 0)
  8411. leeway = 5000;
  8412. spawn->SetSpawnAnim(anim_id);
  8413. spawn->SetSpawnAnimLeeway(leeway);
  8414. return 0;
  8415. }
  8416. int EQ2Emu_lua_GetClientVersion(lua_State* state) {
  8417. if (!lua_interface)
  8418. return 0;
  8419. Spawn* player = lua_interface->GetSpawn(state);
  8420. if (!player) {
  8421. return 0;
  8422. }
  8423. Client* client = player->GetZone()->GetClientBySpawn(player);
  8424. if (!client) {
  8425. return 0;
  8426. }
  8427. lua_interface->SetInt32Value(state, client->GetVersion());
  8428. return 1;
  8429. }
  8430. int EQ2Emu_lua_GetItemID(lua_State* state) {
  8431. if (!lua_interface)
  8432. return 0;
  8433. Item* item = lua_interface->GetItem(state);
  8434. if (!item) {
  8435. lua_interface->LogError("%s: LUA GetItemID command error: item is not valid", lua_interface->GetScriptName(state));
  8436. return 0;
  8437. }
  8438. lua_interface->SetInt32Value(state, item->details.item_id);
  8439. return 1;
  8440. }
  8441. int EQ2Emu_lua_IsEntity(lua_State* state) {
  8442. if (!lua_interface)
  8443. return 0;
  8444. Spawn* spawn = lua_interface->GetSpawn(state);
  8445. if (!spawn) {
  8446. lua_interface->LogError("%s: LUA IsEntity command error: spawn is not valid", lua_interface->GetScriptName(state));
  8447. return 0;
  8448. }
  8449. lua_interface->SetBooleanValue(state, spawn->IsEntity());
  8450. return 1;
  8451. }
  8452. int EQ2Emu_lua_GetOrigX(lua_State* state) {
  8453. if (!lua_interface)
  8454. return 0;
  8455. Spawn* spawn = lua_interface->GetSpawn(state);
  8456. if (!spawn) {
  8457. lua_interface->LogError("%s: LUA GetOrigX command error: spawn is not valid", lua_interface->GetScriptName(state));
  8458. return 0;
  8459. }
  8460. lua_interface->SetFloatValue(state, spawn->GetSpawnOrigX());
  8461. return 1;
  8462. }
  8463. int EQ2Emu_lua_GetOrigY(lua_State* state) {
  8464. if (!lua_interface)
  8465. return 0;
  8466. Spawn* spawn = lua_interface->GetSpawn(state);
  8467. if (!spawn) {
  8468. lua_interface->LogError("%s: LUA GetOrigY command error: spawn is not valid", lua_interface->GetScriptName(state));
  8469. return 0;
  8470. }
  8471. lua_interface->SetFloatValue(state, spawn->GetSpawnOrigY());
  8472. return 1;
  8473. }
  8474. int EQ2Emu_lua_GetOrigZ(lua_State* state) {
  8475. if (!lua_interface)
  8476. return 0;
  8477. Spawn* spawn = lua_interface->GetSpawn(state);
  8478. if (!spawn) {
  8479. lua_interface->LogError("%s: LUA GetOrigZ command error: spawn is not valid", lua_interface->GetScriptName(state));
  8480. return 0;
  8481. }
  8482. lua_interface->SetFloatValue(state, spawn->GetSpawnOrigZ());
  8483. return 1;
  8484. }
  8485. int EQ2Emu_lua_GetPCTOfHP(lua_State* state) {
  8486. if (!lua_interface)
  8487. return 0;
  8488. Spawn* spawn = lua_interface->GetSpawn(state);
  8489. float pct = lua_interface->GetFloatValue(state, 2);
  8490. if (!spawn) {
  8491. lua_interface->LogError("%s: LUA GetPCTOfHP command error: spawn is not valid", lua_interface->GetScriptName(state));
  8492. return 0;
  8493. }
  8494. if (pct == 0) {
  8495. lua_interface->LogError("%s: LUA GetPCTOfHP command error: pct is not valid", lua_interface->GetScriptName(state));
  8496. return 0;
  8497. }
  8498. int32 amount = std::round(spawn->GetTotalHP() * (pct / 100));
  8499. lua_interface->SetInt32Value(state, amount);
  8500. return 1;
  8501. }
  8502. int EQ2Emu_lua_GetPCTOfPower(lua_State* state) {
  8503. if (!lua_interface)
  8504. return 0;
  8505. Spawn* spawn = lua_interface->GetSpawn(state);
  8506. float pct = lua_interface->GetFloatValue(state, 2);
  8507. if (!spawn) {
  8508. lua_interface->LogError("%s: LUA GetPCTOfPower command error: spawn is not valid", lua_interface->GetScriptName(state));
  8509. return 0;
  8510. }
  8511. if (pct == 0) {
  8512. lua_interface->LogError("%s: LUA GetPCTOfPower command error: pct is not valid", lua_interface->GetScriptName(state));
  8513. return 0;
  8514. }
  8515. int32 amount = std::round(spawn->GetTotalPower() * (pct / 100));
  8516. lua_interface->SetInt32Value(state, amount);
  8517. return 1;
  8518. }
  8519. int EQ2Emu_lua_GetBoundZoneID(lua_State* state) {
  8520. if (!lua_interface)
  8521. return 0;
  8522. Spawn* spawn = lua_interface->GetSpawn(state);
  8523. if (!spawn) {
  8524. lua_interface->LogError("%s: LUA GetBoundZoneID command error: spawn is not valid", lua_interface->GetScriptName(state));
  8525. return 0;
  8526. }
  8527. if (!spawn->IsPlayer()) {
  8528. lua_interface->LogError("%s: LUA GetBoundZoneID command error: spawn is not a player", lua_interface->GetScriptName(state));
  8529. return 0;
  8530. }
  8531. lua_interface->SetInt32Value(state, ((Player*)spawn)->GetPlayerInfo()->GetBindZoneID());
  8532. return 1;
  8533. }
  8534. int EQ2Emu_lua_Evac(lua_State* state) {
  8535. if (!lua_interface)
  8536. return 0;
  8537. Spawn* target = lua_interface->GetSpawn(state);
  8538. if (target) {
  8539. float x = target->GetZone()->GetSafeX();
  8540. float y = target->GetZone()->GetSafeY();
  8541. float z = target->GetZone()->GetSafeZ();
  8542. float h = target->GetZone()->GetSafeHeading();
  8543. target->SetX(x);
  8544. target->SetY(y);
  8545. target->SetZ(z);
  8546. target->SetHeading(h);
  8547. target->SetSpawnOrigX(target->GetX());
  8548. target->SetSpawnOrigY(target->GetY());
  8549. target->SetSpawnOrigZ(target->GetZ());
  8550. target->SetSpawnOrigHeading(target->GetHeading());
  8551. if (target->IsPlayer()) {
  8552. Client* client = target->GetZone()->GetClientBySpawn(target);
  8553. if (client) {
  8554. PacketStruct* packet = configReader.getStruct("WS_TeleportWithinZone", client->GetVersion());
  8555. if (packet)
  8556. {
  8557. packet->setDataByName("x", x);
  8558. packet->setDataByName("y", y);
  8559. packet->setDataByName("z", z);
  8560. client->QueuePacket(packet->serialize());
  8561. safe_delete(packet);
  8562. }
  8563. }
  8564. }
  8565. }
  8566. else {
  8567. LuaSpell* spell = lua_interface->GetCurrentSpell(state);
  8568. ZoneServer* zone = spell->caster->GetZone();
  8569. float x = spell->caster->GetZone()->GetSafeX();
  8570. float y = spell->caster->GetZone()->GetSafeY();
  8571. float z = spell->caster->GetZone()->GetSafeZ();
  8572. float h = spell->caster->GetZone()->GetSafeHeading();
  8573. spell->MSpellTargets.readlock(__FUNCTION__, __LINE__);
  8574. for (int32 i = 0; i < spell->targets.size(); i++) {
  8575. Spawn* target2 = zone->GetSpawnByID(spell->targets.at(i));
  8576. if (!target2)
  8577. continue;
  8578. target2->SetX(x);
  8579. target2->SetY(y);
  8580. target2->SetZ(z);
  8581. target2->SetHeading(h);
  8582. target2->SetSpawnOrigX(target2->GetX());
  8583. target2->SetSpawnOrigY(target2->GetY());
  8584. target2->SetSpawnOrigZ(target2->GetZ());
  8585. target2->SetSpawnOrigHeading(target2->GetHeading());
  8586. if (target2->IsPlayer()) {
  8587. Client* client = target2->GetZone()->GetClientBySpawn(target2);
  8588. if (client) {
  8589. PacketStruct* packet = configReader.getStruct("WS_TeleportWithinZone", client->GetVersion());
  8590. if (packet)
  8591. {
  8592. packet->setDataByName("x", x);
  8593. packet->setDataByName("y", y);
  8594. packet->setDataByName("z", z);
  8595. client->QueuePacket(packet->serialize());
  8596. safe_delete(packet);
  8597. }
  8598. }
  8599. }
  8600. }
  8601. spell->MSpellTargets.releasereadlock(__FUNCTION__, __LINE__);
  8602. }
  8603. return 0;
  8604. }
  8605. int EQ2Emu_lua_GetSpellTier(lua_State* state) {
  8606. if (!lua_interface)
  8607. return 0;
  8608. LuaSpell* luaspell = lua_interface->GetCurrentSpell(state);
  8609. if (!luaspell) {
  8610. lua_interface->LogError("%s: LUA GetSpellTier command error: must be used in a spell script", lua_interface->GetScriptName(state));
  8611. return 0;
  8612. }
  8613. int8 tier = luaspell->spell->GetSpellTier();
  8614. lua_interface->SetInt32Value(state, tier);
  8615. return 1;
  8616. }
  8617. int EQ2Emu_lua_GetSpellID(lua_State* state) {
  8618. if (!lua_interface)
  8619. return 0;
  8620. LuaSpell* luaspell = lua_interface->GetCurrentSpell(state);
  8621. if (!luaspell) {
  8622. lua_interface->LogError("%s: LUA GetSpellID command error: must be used in a spell script", lua_interface->GetScriptName(state));
  8623. return 0;
  8624. }
  8625. int32 spell_id = luaspell->spell->GetSpellID();
  8626. lua_interface->SetInt32Value(state, spell_id);
  8627. return 1;
  8628. }
  8629. int EQ2Emu_lua_StartTransmute(lua_State* state) {
  8630. if (!lua_interface)
  8631. return 0;
  8632. Spawn* spawn = lua_interface->GetSpawn(state, 1);
  8633. if (!spawn) {
  8634. lua_interface->LogError("%s: Lua StartTransmute command error: no spawn", lua_interface->GetScriptName(state));
  8635. return 0;
  8636. }
  8637. if (!spawn->IsPlayer()) {
  8638. lua_interface->LogError("%s: Lua StartTransmute command error: spawn is not a player", lua_interface->GetScriptName(state));
  8639. return 0;
  8640. }
  8641. ZoneServer* zone = spawn->GetZone();
  8642. if (!zone) {
  8643. return 0;
  8644. }
  8645. Client* client = zone->GetClientBySpawn(spawn);
  8646. if (!client) {
  8647. return 0;
  8648. }
  8649. Transmute::CreateItemRequest(client, static_cast<Player*>(spawn));
  8650. return 0;
  8651. }
  8652. int EQ2Emu_lua_CompleteTransmute(lua_State* state) {
  8653. if (!lua_interface)
  8654. return 0;
  8655. Spawn* spawn = lua_interface->GetSpawn(state, 1);
  8656. if (!spawn) {
  8657. lua_interface->LogError("%s: Lua CompleteTransmute command error: no spawn", lua_interface->GetScriptName(state));
  8658. return 0;
  8659. }
  8660. if (!spawn->IsPlayer()) {
  8661. lua_interface->LogError("%s: Lua CompleteTransmute command error: spawn is not a player", lua_interface->GetScriptName(state));
  8662. return 0;
  8663. }
  8664. ZoneServer* zone = spawn->GetZone();
  8665. if (!zone) {
  8666. return 0;
  8667. }
  8668. Client* client = zone->GetClientBySpawn(spawn);
  8669. if (!client) {
  8670. return 0;
  8671. }
  8672. Transmute::CompleteTransmutation(client, static_cast<Player*>(spawn));
  8673. return 0;
  8674. }
  8675. int EQ2Emu_lua_ProcHate(lua_State* state) {
  8676. if (!lua_interface)
  8677. return 0;
  8678. Spawn* caster = lua_interface->GetSpawn(state);
  8679. Spawn* target = lua_interface->GetSpawn(state, 2);
  8680. int32 threat_amt = lua_interface->GetInt32Value(state, 3);
  8681. string spell_name = lua_interface->GetStringValue(state, 4);
  8682. if (!caster) {
  8683. lua_interface->LogError("%s: LUA ProcHate command error: caster is not a valid spawn", lua_interface->GetScriptName(state));
  8684. return 0;
  8685. }
  8686. if (!caster->IsEntity()) {
  8687. lua_interface->LogError("%s: LUA ProcHate command error: caster is not an entity", lua_interface->GetScriptName(state));
  8688. return 0;
  8689. }
  8690. if (!target) {
  8691. lua_interface->LogError("%s: LUA ProcHate command error: target is not a valid spawn", lua_interface->GetScriptName(state));
  8692. return 0;
  8693. }
  8694. if (!target->IsEntity()) {
  8695. lua_interface->LogError("%s: LUA ProcHate command error: target is not an entity", lua_interface->GetScriptName(state));
  8696. return 0;
  8697. }
  8698. static_cast<Entity*>(target)->AddHate(static_cast<Entity*>(caster), threat_amt);
  8699. caster->GetZone()->SendThreatPacket(static_cast<Entity*>(caster), target, threat_amt, spell_name.c_str());
  8700. return 0;
  8701. }
  8702. int EQ2Emu_lua_AddLootToObject(lua_State* state) {
  8703. if (!lua_interface)
  8704. return 0;
  8705. Spawn* object = lua_interface->GetSpawn(state);
  8706. Spawn* player = lua_interface->GetSpawn(state, 2);
  8707. if (object && player && player->IsPlayer()) {
  8708. int32 coins = lua_interface->GetInt32Value(state, 3);
  8709. vector<Item*>* items = 0;
  8710. int i = 0;
  8711. int32 item_id = 0;
  8712. while ((item_id = lua_interface->GetInt32Value(state, 4 + i))) {
  8713. if (items == 0)
  8714. items = new vector<Item*>;
  8715. if (master_item_list.GetItem(item_id))
  8716. items->push_back(master_item_list.GetItem(item_id));
  8717. i++;
  8718. }
  8719. Client* client = 0;
  8720. client = object->GetZone()->GetClientBySpawn(player);
  8721. if (client) {
  8722. ((Player*)player)->AddPendingLootItems(object->GetID(), items);
  8723. }
  8724. safe_delete(items);
  8725. }
  8726. return 0;
  8727. }
  8728. int EQ2Emu_lua_GiveExp(lua_State* state) {
  8729. if (!lua_interface)
  8730. return 0;
  8731. Spawn* player = lua_interface->GetSpawn(state);
  8732. int32 amount = lua_interface->GetInt32Value(state, 2);
  8733. if (player && player->IsPlayer() && amount > 0) {
  8734. ((Player*)player)->AddXP(amount);
  8735. ((Player*)player)->SetCharSheetChanged(true);
  8736. Client* client = player->GetZone()->GetClientBySpawn(player);
  8737. if (client) {
  8738. client->SimpleMessage(CHANNEL_REWARD, "You gain experience!");
  8739. }
  8740. }
  8741. return 0;
  8742. }
  8743. int EQ2Emu_lua_DisplayText(lua_State* state) {
  8744. if (!lua_interface)
  8745. return 0;
  8746. Spawn* player = lua_interface->GetSpawn(state);
  8747. int8 type = lua_interface->GetInt8Value(state, 2);
  8748. string text = lua_interface->GetStringValue(state, 3);
  8749. Client* client = 0;
  8750. if (player && player->IsPlayer())
  8751. client = player->GetZone()->GetClientBySpawn(player);
  8752. if (!client || text.length() == 0) {
  8753. lua_interface->LogError("%s: LUA DisplayText required parameters not given", lua_interface->GetScriptName(state));
  8754. return 0;
  8755. }
  8756. client->SimpleMessage(type, text.c_str());
  8757. return 0;
  8758. }
  8759. int EQ2Emu_lua_ShowLootWindow(lua_State* state) {
  8760. if (!lua_interface)
  8761. return 0;
  8762. Spawn* player = lua_interface->GetSpawn(state);
  8763. Spawn* spawn = lua_interface->GetSpawn(state, 2);
  8764. Client* client = 0;
  8765. if (player && player->IsPlayer())
  8766. client = player->GetZone()->GetClientBySpawn(player);
  8767. if (!client || !spawn) {
  8768. lua_interface->LogError("%s: LUA ShowLootWindow required parameters not given", lua_interface->GetScriptName(state));
  8769. return 0;
  8770. }
  8771. vector<Item*>* items = ((Player*)player)->GetPendingLootItems(spawn->GetID());
  8772. if (!items) {
  8773. lua_interface->LogError("%s: LUA ShowLootWindow has no items", lua_interface->GetScriptName(state));
  8774. return 0;
  8775. }
  8776. client->Loot(spawn->GetLootCoins(), items, spawn);
  8777. return 0;
  8778. }
  8779. int EQ2Emu_lua_GetRandomSpawnByID(lua_State* state) {
  8780. if (!lua_interface)
  8781. return 0;
  8782. Spawn* spawnref = lua_interface->GetSpawn(state);
  8783. int32 spawn_id = lua_interface->GetInt32Value(state, 2);
  8784. if (spawn_id > 0 && spawnref) {
  8785. vector<Spawn*> spawns = spawnref->GetZone()->GetSpawnsByID(spawn_id);
  8786. if (spawns.size() == 0) {
  8787. lua_interface->LogError("%s: LUA EQ2Emu_lua_GetRandomSpawnByID command error: GetSpawnsByID returned no spawns", lua_interface->GetScriptName(state));
  8788. return 0;
  8789. }
  8790. Spawn* spawn = 0;
  8791. int16 index = MakeRandomInt(0, spawns.size());
  8792. if (index >= spawns.size() || index < 0)
  8793. index = 0;
  8794. spawn = spawns[index];
  8795. lua_interface->SetSpawnValue(state, spawn);
  8796. return 1;
  8797. }
  8798. else {
  8799. lua_interface->LogError("%s: LUA GetRandomSpawnByID required parameters not given", lua_interface->GetScriptName(state));
  8800. }
  8801. return 0;
  8802. }
  8803. int EQ2Emu_lua_AddPrimaryEntityCommandAllSpawns(lua_State* state) {
  8804. Spawn* player = lua_interface->GetSpawn(state);
  8805. int32 spawn_id = lua_interface->GetInt32Value(state, 2);
  8806. string name = lua_interface->GetStringValue(state, 3);
  8807. float distance = lua_interface->GetFloatValue(state, 4);
  8808. string command = lua_interface->GetStringValue(state, 5);
  8809. string error_text = lua_interface->GetStringValue(state, 6);
  8810. int16 cast_time = lua_interface->GetInt16Value(state, 7);
  8811. int32 spell_visual = lua_interface->GetInt32Value(state, 8);
  8812. if (spawn_id && player && player->IsPlayer() && name.length() > 0) {
  8813. if (distance == 0)
  8814. distance = 10.0f;
  8815. if (command.length() == 0)
  8816. command = name;
  8817. vector<Spawn*> spawns = player->GetZone()->GetSpawnsByID(spawn_id);
  8818. if (spawns.size() == 0) {
  8819. lua_interface->LogError("%s: LUA AddPrimaryEntityCommandAllSpawns command error: GetSpawnsByID returned no spawns", lua_interface->GetScriptName(state));
  8820. return 0;
  8821. }
  8822. Spawn* spawn = 0;
  8823. for (vector<Spawn*>::iterator itr = spawns.begin(); itr != spawns.end(); itr++) {
  8824. spawn = *itr;
  8825. if (spawn) {
  8826. spawn->AddPrimaryEntityCommand(name.c_str(), distance, command.c_str(), error_text.c_str(), cast_time, spell_visual);
  8827. player->GetZone()->SendUpdateDefaultCommand(spawn, command.c_str(), distance);
  8828. }
  8829. }
  8830. }
  8831. return 0;
  8832. }
  8833. int EQ2Emu_lua_InstructionWindowGoal(lua_State* state) {
  8834. if (!lua_interface)
  8835. return 0;
  8836. Client* client = 0;
  8837. Spawn* player = lua_interface->GetSpawn(state);
  8838. int8 goal_num = lua_interface->GetInt8Value(state, 2);
  8839. if (player && player->IsPlayer() && player->GetZone())
  8840. client = player->GetZone()->GetClientBySpawn(player);
  8841. else{
  8842. lua_interface->LogError("LUA InstructionWindowGoal command error: player is not valid");
  8843. return 0;
  8844. }
  8845. if (client) {
  8846. PacketStruct* packet = configReader.getStruct("WS_InstructionWindow", client->GetVersion());
  8847. if (packet) {
  8848. packet->setDataByName("goal_num", goal_num);
  8849. client->QueuePacket(packet->serialize());
  8850. safe_delete(packet);
  8851. }
  8852. }
  8853. return 0;
  8854. }
  8855. int EQ2Emu_lua_InstructionWindowClose(lua_State* state) {
  8856. if (!lua_interface)
  8857. return 0;
  8858. Client* client = 0;
  8859. Spawn* player = lua_interface->GetSpawn(state);
  8860. if (player && player->IsPlayer() && player->GetZone())
  8861. client = player->GetZone()->GetClientBySpawn(player);
  8862. else {
  8863. lua_interface->LogError("LUA InstructionWindowClose command error: player is not valid");
  8864. return 0;
  8865. }
  8866. if (client) {
  8867. client->QueuePacket(new EQ2Packet(OP_EqInstructionWindowCloseCmd, 0, 0));
  8868. }
  8869. return 0;
  8870. }
  8871. int EQ2Emu_lua_InstructionWindow(lua_State* state) {
  8872. if (!lua_interface)
  8873. return 0;
  8874. Client* client = 0;
  8875. Spawn* player = lua_interface->GetSpawn(state);
  8876. float duration = lua_interface->GetFloatValue(state, 2);
  8877. string text = lua_interface->GetStringValue(state, 3);
  8878. string voice = lua_interface->GetStringValue(state, 4);
  8879. int32 voice_key1 = lua_interface->GetInt32Value(state, 5);
  8880. int32 voice_key2 = lua_interface->GetInt32Value(state, 6);
  8881. string signal = lua_interface->GetStringValue(state, 7);
  8882. string goal1 = lua_interface->GetStringValue(state, 8);
  8883. string task1 = lua_interface->GetStringValue(state, 9);
  8884. string goal2 = lua_interface->GetStringValue(state, 10);
  8885. string task2 = lua_interface->GetStringValue(state, 11);
  8886. string goal3 = lua_interface->GetStringValue(state, 12);
  8887. string task3 = lua_interface->GetStringValue(state, 13);
  8888. string goal4 = lua_interface->GetStringValue(state, 14);
  8889. string task4 = lua_interface->GetStringValue(state, 15);
  8890. if (!player) {
  8891. lua_interface->LogError("LUA InstructionWindow command error: spawn is not valid");
  8892. return 0;
  8893. }
  8894. if (!player->IsPlayer()) {
  8895. lua_interface->LogError("LUA InstructionWindow command error: spawn is not a player");
  8896. return 0;
  8897. }
  8898. else
  8899. client = ((Player*)player)->GetClient();
  8900. if (!client) {
  8901. lua_interface->LogError("LUA InstructionWindow command error: could not find client");
  8902. return 0;
  8903. }
  8904. if (text.length() == 0) {
  8905. lua_interface->LogError("LUA InstructionWindow required parameters not given");
  8906. return 0;
  8907. }
  8908. if (duration >= 0 && duration < 2)
  8909. duration = 2;
  8910. PacketStruct* packet = configReader.getStruct("WS_InstructionWindow", client->GetVersion());
  8911. if (packet) {
  8912. packet->setDataByName("open_seconds_max", duration);
  8913. packet->setDataByName("text", text.c_str());
  8914. packet->setDataByName("voice", voice.c_str());
  8915. int8 num_goals = 1;
  8916. if (task2.length() > 0)
  8917. num_goals++;
  8918. if (task3.length() > 0)
  8919. num_goals++;
  8920. if (task4.length() > 0)
  8921. num_goals++;
  8922. packet->setArrayLengthByName("num_goals", num_goals);
  8923. for (int8 i = 0; i < num_goals; i++) {
  8924. packet->setSubArrayLengthByName("num_tasks", 1, i);
  8925. }
  8926. if (goal1.length() > 0)
  8927. packet->setArrayDataByName("goal_text", goal1.c_str());
  8928. if (goal2.length() > 0)
  8929. packet->setArrayDataByName("goal_text", goal2.c_str(), 1);
  8930. if (goal3.length() > 0)
  8931. packet->setArrayDataByName("goal_text", goal3.c_str(), 2);
  8932. if (goal4.length() > 0)
  8933. packet->setArrayDataByName("goal_text", goal4.c_str(), 3);
  8934. packet->setSubArrayDataByName("task_text", task1.c_str());
  8935. if (task2.length() > 0)
  8936. packet->setSubArrayDataByName("task_text", task2.c_str(), 1);
  8937. if (task3.length() > 0)
  8938. packet->setSubArrayDataByName("task_text", task3.c_str(), 2);
  8939. if (task4.length() > 0)
  8940. packet->setSubArrayDataByName("task_text", task4.c_str(), 3);
  8941. packet->setDataByName("complete_sound", "click");
  8942. packet->setDataByName("signal", signal.c_str());
  8943. packet->setDataByName("voice_key1", voice_key1);
  8944. packet->setDataByName("voice_key2", voice_key2);
  8945. client->QueuePacket(packet->serialize());
  8946. safe_delete(packet);
  8947. }
  8948. return 0;
  8949. }
  8950. int EQ2Emu_lua_ShowWindow(lua_State* state) {
  8951. if (!lua_interface)
  8952. return 0;
  8953. Client* client = 0;
  8954. Spawn* player = lua_interface->GetSpawn(state);
  8955. string window = lua_interface->GetStringValue(state, 2);
  8956. int8 show = lua_interface->GetInt8Value(state, 3);
  8957. if (!player) {
  8958. lua_interface->LogError("LUA ShowWindow command error: spawn is not valid");
  8959. return 0;
  8960. }
  8961. if (!player->IsPlayer()) {
  8962. lua_interface->LogError("LUA ShowWindow command error: spawn is not a player");
  8963. return 0;
  8964. }
  8965. else
  8966. client = ((Player*)player)->GetClient();
  8967. if (!client) {
  8968. lua_interface->LogError("LUA ShowWindow command error: could not find client");
  8969. return 0;
  8970. }
  8971. if (window.length() == 0) {
  8972. lua_interface->LogError("LUA ShowWindow required parameters not given");
  8973. return 0;
  8974. }
  8975. PacketStruct* packet = configReader.getStruct("WS_ShowWindow", client->GetVersion());
  8976. if (packet) {
  8977. packet->setDataByName("window", window.c_str());
  8978. packet->setDataByName("show", show);
  8979. client->QueuePacket(packet->serialize());
  8980. safe_delete(packet);
  8981. }
  8982. return 0;
  8983. }
  8984. int EQ2Emu_lua_EnableGameEvent(lua_State* state) {
  8985. //See GameEvents.txt for options that can be used for this function
  8986. if (!lua_interface)
  8987. return 0;
  8988. Client* client = 0;
  8989. Spawn* player = lua_interface->GetSpawn(state);
  8990. string event_name = lua_interface->GetStringValue(state, 2);
  8991. int8 enabled = lua_interface->GetInt8Value(state, 3);
  8992. if (!player || !player->IsPlayer()) {
  8993. lua_interface->LogError("LUA EnableGameEvent error: player is not valid");
  8994. return 0;
  8995. }
  8996. if (player->GetZone())
  8997. client = player->GetZone()->GetClientBySpawn(player);
  8998. if (!client) {
  8999. lua_interface->LogError("LUA EnableGameEvent error: could not find client");
  9000. return 0;
  9001. }
  9002. PacketStruct* packet = configReader.getStruct("WS_EnableGameEvent", client->GetVersion());
  9003. if (packet) {
  9004. packet->setDataByName("event_name", event_name.c_str());
  9005. packet->setDataByName("enabled", enabled);
  9006. client->QueuePacket(packet->serialize());
  9007. safe_delete(packet);
  9008. }
  9009. return 0;
  9010. }
  9011. int EQ2Emu_lua_GetTutorialStep(lua_State* state) {
  9012. if (!lua_interface)
  9013. return 0;
  9014. Spawn* player = lua_interface->GetSpawn(state);
  9015. if (player && player->IsPlayer()) {
  9016. lua_interface->SetInt32Value(state, ((Player*)player)->GetTutorialStep());
  9017. return 1;
  9018. }
  9019. return 0;
  9020. }
  9021. int EQ2Emu_lua_SetTutorialStep(lua_State* state) {
  9022. if (!lua_interface)
  9023. return 0;
  9024. Spawn* player = lua_interface->GetSpawn(state);
  9025. int8 step = lua_interface->GetInt8Value(state, 2);
  9026. if (player && player->IsPlayer() && step > 0) {
  9027. ((Player*)player)->SetTutorialStep(step);
  9028. }
  9029. return 0;
  9030. }
  9031. int EQ2Emu_lua_FlashWindow(lua_State* state) {
  9032. if (!lua_interface)
  9033. return 0;
  9034. Client* client = 0;
  9035. Spawn* player = lua_interface->GetSpawn(state);
  9036. string window = lua_interface->GetStringValue(state, 2);
  9037. float flash_seconds = lua_interface->GetFloatValue(state, 3);
  9038. if (!player) {
  9039. lua_interface->LogError("LUA FlashWindow command error: spawn is not valid");
  9040. return 0;
  9041. }
  9042. if (!player->IsPlayer()) {
  9043. lua_interface->LogError("LUA FlashWindow command error: spawn is not a player");
  9044. return 0;
  9045. }
  9046. else
  9047. client = ((Player*)player)->GetClient();
  9048. if (!client) {
  9049. lua_interface->LogError("LUA FlashWindow command error: could not find client");
  9050. return 0;
  9051. }
  9052. if (window.length() == 0) {
  9053. lua_interface->LogError("LUA FlashWindow required parameters not given");
  9054. return 0;
  9055. }
  9056. PacketStruct* packet = configReader.getStruct("WS_FlashWindow", client->GetVersion());
  9057. if (packet) {
  9058. packet->setDataByName("window", window.c_str());
  9059. packet->setDataByName("flash_seconds", flash_seconds);
  9060. client->QueuePacket(packet->serialize());
  9061. safe_delete(packet);
  9062. }
  9063. return 0;
  9064. }
  9065. int EQ2Emu_lua_CheckLOS(lua_State* state) {
  9066. if (!lua_interface)
  9067. return 0;
  9068. Spawn* spawn = lua_interface->GetSpawn(state);
  9069. Spawn* target = lua_interface->GetSpawn(state, 2);
  9070. if (spawn && target)
  9071. return spawn->CheckLoS(target);
  9072. return 0;
  9073. }
  9074. int EQ2Emu_lua_CheckLOSByCoordinates(lua_State* state) {
  9075. if (!lua_interface)
  9076. return 0;
  9077. Spawn* spawn = lua_interface->GetSpawn(state);
  9078. float x = lua_interface->GetFloatValue(state, 2);
  9079. float y = lua_interface->GetFloatValue(state, 3);
  9080. float z = lua_interface->GetFloatValue(state, 4);
  9081. if (spawn)
  9082. return spawn->CheckLoS(glm::vec3(spawn->GetX(), spawn->GetZ(), spawn->GetY() + 1.0f), glm::vec3(x, z, y+1.0f));
  9083. return 0;
  9084. }
  9085. int EQ2Emu_lua_SetZoneExpansionFlag(lua_State* state) {
  9086. if (!lua_interface)
  9087. return 0;
  9088. ZoneServer* zone = lua_interface->GetZone(state);
  9089. int32 xpackFlag = lua_interface->GetInt32Value(state, 2);
  9090. if (zone)
  9091. zone->SetExpansionFlag(xpackFlag);
  9092. return 0;
  9093. }
  9094. int EQ2Emu_lua_GetZoneExpansionFlag(lua_State* state) {
  9095. if (!lua_interface)
  9096. return 0;
  9097. ZoneServer* zone = lua_interface->GetZone(state);
  9098. if (zone) {
  9099. lua_interface->SetInt32Value(state, zone->GetExpansionFlag());
  9100. return 1;
  9101. }
  9102. return 0;
  9103. }
  9104. int EQ2Emu_lua_SetZoneHolidayFlag(lua_State* state) {
  9105. if (!lua_interface)
  9106. return 0;
  9107. ZoneServer* zone = lua_interface->GetZone(state);
  9108. int32 holidayFlag = lua_interface->GetInt32Value(state, 2);
  9109. if (zone)
  9110. zone->SetHolidayFlag(holidayFlag);
  9111. return 0;
  9112. }
  9113. int EQ2Emu_lua_GetZoneHolidayFlag(lua_State* state) {
  9114. if (!lua_interface)
  9115. return 0;
  9116. ZoneServer* zone = lua_interface->GetZone(state);
  9117. if (zone) {
  9118. lua_interface->SetInt32Value(state, zone->GetHolidayFlag());
  9119. return 1;
  9120. }
  9121. return 0;
  9122. }
  9123. int EQ2Emu_lua_AddSpawnProximity(lua_State* state) {
  9124. if (!lua_interface)
  9125. return 0;
  9126. Spawn* spawn = lua_interface->GetSpawn(state);
  9127. int32 spawn_value = lua_interface->GetInt32Value(state, 2);
  9128. int8 spawn_type = lua_interface->GetInt8Value(state, 3);
  9129. float distance = lua_interface->GetFloatValue(state, 4);
  9130. string in_range_function = lua_interface->GetStringValue(state, 5);
  9131. string leaving_range_function = lua_interface->GetStringValue(state, 6);
  9132. if (spawn && distance > 0 && in_range_function.length() > 0)
  9133. spawn->AddLUASpawnProximity(spawn_value, (Spawn::SpawnProximityType)spawn_type, distance, in_range_function, leaving_range_function);
  9134. return 0;
  9135. }
  9136. int EQ2Emu_lua_CanSeeInvis(lua_State* state) {
  9137. if (!lua_interface)
  9138. return 0;
  9139. Spawn* spawn = lua_interface->GetSpawn(state);
  9140. Spawn* target = lua_interface->GetSpawn(state, 2);
  9141. if (spawn && target)
  9142. {
  9143. if (spawn->IsPlayer() && target->IsEntity())
  9144. {
  9145. lua_interface->SetBooleanValue(state, ((Player*)spawn)->CanSeeInvis((Entity*)target));
  9146. return 1;
  9147. }
  9148. else if (spawn->IsEntity() && target->IsEntity())
  9149. {
  9150. lua_interface->SetBooleanValue(state, ((Entity*)spawn)->CanSeeInvis((Entity*)target));
  9151. return 1;
  9152. }
  9153. }
  9154. return 0;
  9155. }
  9156. int EQ2Emu_lua_SetSeeInvis(lua_State* state) {
  9157. if (!lua_interface)
  9158. return 0;
  9159. Spawn* spawn = lua_interface->GetSpawn(state);
  9160. bool val = (lua_interface->GetInt8Value(state, 2) == 1);
  9161. if (spawn && spawn->IsEntity())
  9162. {
  9163. ((Entity*)spawn)->SetSeeInvisSpell(val);
  9164. if (spawn->IsPlayer())
  9165. {
  9166. Client* client = spawn->GetZone()->GetClientBySpawn((Player*)spawn);
  9167. if (client)
  9168. ((Player*)spawn)->GetZone()->SendAllSpawnsForSeeInvisChange(client);
  9169. }
  9170. }
  9171. return 0;
  9172. }
  9173. int EQ2Emu_lua_SetSeeHide(lua_State* state) {
  9174. if (!lua_interface)
  9175. return 0;
  9176. Spawn* spawn = lua_interface->GetSpawn(state);
  9177. bool val = (lua_interface->GetInt8Value(state, 2) == 1);
  9178. if (spawn && spawn->IsEntity())
  9179. {
  9180. ((Entity*)spawn)->SetSeeHideSpell(val);
  9181. if (spawn->IsPlayer())
  9182. {
  9183. Client* client = spawn->GetZone()->GetClientBySpawn((Player*)spawn);
  9184. if (client)
  9185. ((Player*)spawn)->GetZone()->SendAllSpawnsForVisChange(client);
  9186. }
  9187. }
  9188. return 0;
  9189. }
  9190. int EQ2Emu_lua_SetAccessToEntityCommand(lua_State* state)
  9191. {
  9192. if (!lua_interface)
  9193. return 0;
  9194. Spawn* player = lua_interface->GetSpawn(state);
  9195. Spawn* spawn = lua_interface->GetSpawn(state, 2);
  9196. string command = lua_interface->GetStringValue(state, 3);
  9197. bool val = (lua_interface->GetInt8Value(state, 4) == 1);
  9198. if (spawn && player && player->IsPlayer())
  9199. {
  9200. EntityCommand* cmd = spawn->FindEntityCommand(string(command), true);
  9201. bool res = false;
  9202. if (cmd)
  9203. res = spawn->SetPermissionToEntityCommand(cmd, (Player*)player, val);
  9204. lua_interface->SetBooleanValue(state, res);
  9205. return 1;
  9206. }
  9207. return 0;
  9208. }
  9209. int EQ2Emu_lua_RemovePrimaryEntityCommand(lua_State* state)
  9210. {
  9211. if (!lua_interface)
  9212. return 0;
  9213. Spawn* spawn = lua_interface->GetSpawn(state);
  9214. string command = lua_interface->GetStringValue(state, 2);
  9215. if (spawn && command.length() > 0)
  9216. spawn->RemovePrimaryEntityCommand(command.c_str());
  9217. return 0;
  9218. }
  9219. int EQ2Emu_lua_SendUpdateDefaultCommand(lua_State* state) {
  9220. if (!lua_interface)
  9221. return 0;
  9222. Spawn* spawn = lua_interface->GetSpawn(state);
  9223. float distance = lua_interface->GetFloatValue(state, 2);
  9224. string command = lua_interface->GetStringValue(state, 3);
  9225. Spawn* player = lua_interface->GetSpawn(state, 4);
  9226. if (spawn) {
  9227. spawn->GetZone()->SendUpdateDefaultCommand(spawn, command.c_str(), distance, player);
  9228. }
  9229. return 0;
  9230. }
  9231. int EQ2Emu_lua_SendTransporters(lua_State* state) {
  9232. if (!lua_interface)
  9233. return 0;
  9234. Spawn* spawn = lua_interface->GetSpawn(state);
  9235. Spawn* player = lua_interface->GetSpawn(state, 2);
  9236. int32 transport_id = lua_interface->GetInt32Value(state, 3);
  9237. if (spawn && player && transport_id && player->IsPlayer()) {
  9238. Client* client = 0;
  9239. if (player && player->IsPlayer())
  9240. client = player->GetZone()->GetClientBySpawn(player);
  9241. if (!client)
  9242. return 0;
  9243. vector<TransportDestination*> destinations;
  9244. player->GetZone()->GetTransporters(&destinations, client, transport_id);
  9245. if (destinations.size())
  9246. {
  9247. client->SetTemporaryTransportID(transport_id);
  9248. client->ProcessTeleport(spawn, &destinations, transport_id);
  9249. }
  9250. else
  9251. client->Message(CHANNEL_COLOR_RED, "There are no transporters available (ID: %u)", transport_id);
  9252. }
  9253. return 0;
  9254. }
  9255. int EQ2Emu_lua_SetTemporaryTransportID(lua_State* state) {
  9256. if (!lua_interface)
  9257. return 0;
  9258. Spawn* player = lua_interface->GetSpawn(state);
  9259. int32 transport_id = lua_interface->GetInt32Value(state, 2);
  9260. if (player && player->IsPlayer()) {
  9261. Client* client = 0;
  9262. if (player && player->IsPlayer())
  9263. client = player->GetZone()->GetClientBySpawn(player);
  9264. if (!client)
  9265. return 0;
  9266. client->SetTemporaryTransportID(transport_id);
  9267. }
  9268. return 0;
  9269. }
  9270. int EQ2Emu_lua_GetTemporaryTransportID(lua_State* state) {
  9271. if (!lua_interface)
  9272. return 0;
  9273. Spawn* player = lua_interface->GetSpawn(state);
  9274. if (player && player->IsPlayer()) {
  9275. Client* client = 0;
  9276. if (player && player->IsPlayer())
  9277. client = player->GetZone()->GetClientBySpawn(player);
  9278. if (!client)
  9279. return 0;
  9280. lua_interface->SetInt32Value(state, client->GetTemporaryTransportID());
  9281. return 1;
  9282. }
  9283. return 0;
  9284. }
  9285. int EQ2Emu_lua_SetAlignment(lua_State* state) {
  9286. if (!lua_interface)
  9287. return 0;
  9288. Spawn* spawn = lua_interface->GetSpawn(state);
  9289. sint32 alignment = lua_interface->GetSInt32Value(state, 2);
  9290. LuaSpell* spell = lua_interface->GetCurrentSpell(state);
  9291. if (!spawn) {
  9292. lua_interface->LogError("%s: LUA SetAlignment command error: spawn is not valid", lua_interface->GetScriptName(state));
  9293. return 0;
  9294. }
  9295. if (!spawn->IsEntity()) {
  9296. lua_interface->LogError("%s: LUA SetAlignment command error: spawn is not an entity", lua_interface->GetScriptName(state));
  9297. return 0;
  9298. }
  9299. if (alignment < SCHAR_MIN || alignment > SCHAR_MAX)
  9300. {
  9301. lua_interface->LogError("%s: LUA SetAlignment command error: alignment value beyond supported min: %i and max: %i", lua_interface->GetScriptName(state), SCHAR_MIN, SCHAR_MAX);
  9302. return 0;
  9303. }
  9304. if (spell && spell->targets.size() > 0) {
  9305. ZoneServer* zone = spell->caster->GetZone();
  9306. for (int8 i = 0; i < spell->targets.size(); i++) {
  9307. Spawn* target = zone->GetSpawnByID(spell->targets.at(i));
  9308. if (target->IsEntity()) {
  9309. ((Entity*)target)->GetInfoStruct()->alignment = (sint8)alignment;
  9310. if (target->IsPlayer())
  9311. ((Player*)target)->SetCharSheetChanged(true);
  9312. }
  9313. }
  9314. }
  9315. else {
  9316. ((Entity*)spawn)->GetInfoStruct()->alignment = (sint8)alignment;
  9317. if (spawn->IsPlayer())
  9318. ((Player*)spawn)->SetCharSheetChanged(true);
  9319. }
  9320. return 0;
  9321. }
  9322. int EQ2Emu_lua_GetAlignment(lua_State* state) {
  9323. if (!lua_interface)
  9324. return 0;
  9325. Spawn* spawn = lua_interface->GetSpawn(state);
  9326. if (!spawn) {
  9327. lua_interface->LogError("%s: LUA GetAlignment command error: spawn is not valid", lua_interface->GetScriptName(state));
  9328. return 0;
  9329. }
  9330. if (!spawn->IsEntity()) {
  9331. lua_interface->LogError("%s: LUA GetAlignment command error: spawn is not an entity", lua_interface->GetScriptName(state));
  9332. return 0;
  9333. }
  9334. lua_interface->SetSInt32Value(state, ((Entity*)spawn)->GetAlignment());
  9335. return 1;
  9336. }
  9337. int EQ2Emu_lua_GetSpell(lua_State* state) {
  9338. if (!lua_interface)
  9339. return 0;
  9340. int32 spell_id = lua_interface->GetInt32Value(state);
  9341. int8 spell_tier = lua_interface->GetInt8Value(state, 2);
  9342. if (spell_id > 0) {
  9343. if (spell_tier == 0)
  9344. spell_tier = 1;
  9345. Spell* spell = master_spell_list.GetSpell(spell_id, spell_tier);
  9346. LuaSpell* lua_spell = 0;
  9347. if (lua_interface)
  9348. lua_spell = lua_interface->GetSpell(spell->GetSpellData()->lua_script.c_str());
  9349. if (!lua_spell)
  9350. return 0;
  9351. lua_spell->spell = new Spell(spell);
  9352. lua_interface->AddCustomSpell(lua_spell);
  9353. lua_interface->SetSpellValue(state, lua_spell);
  9354. return 1;
  9355. }
  9356. return 0;
  9357. }
  9358. int EQ2Emu_lua_GetSpellData(lua_State* state) {
  9359. if (!lua_interface)
  9360. return 0;
  9361. LuaSpell* spell = lua_interface->GetSpell(state);
  9362. string field = lua_interface->GetStringValue(state, 2);
  9363. if (!spell) {
  9364. lua_interface->LogError("%s: Spell not given in GetSpellData!", lua_interface->GetScriptName(state));
  9365. return 0;
  9366. }
  9367. if (!spell->spell || !spell->spell->GetSpellData()) {
  9368. lua_interface->LogError("%s: Inner Spell or SpellData not given in GetSpellData!", lua_interface->GetScriptName(state));
  9369. return 0;
  9370. }
  9371. boost::to_lower(field);
  9372. return spell->spell->GetSpellData(state, field);
  9373. }
  9374. int EQ2Emu_lua_SetSpellData(lua_State* state) {
  9375. if (!lua_interface)
  9376. return 0;
  9377. LuaSpell* spell = lua_interface->GetSpell(state);
  9378. string field = lua_interface->GetStringValue(state, 2);
  9379. int8 fieldArg = 3; // field value after the initial set
  9380. if (!spell) {
  9381. lua_interface->LogError("%s: Spell not given in SetSpellData!", lua_interface->GetScriptName(state));
  9382. return 0;
  9383. }
  9384. if (!spell->spell || !spell->spell->GetSpellData()) {
  9385. lua_interface->LogError("%s: Inner Spell or SpellData not given in SetSpellData!", lua_interface->GetScriptName(state));
  9386. return 0;
  9387. }
  9388. boost::to_lower(field);
  9389. bool valSet = false;
  9390. spell->spell->SetSpellData(state, field, fieldArg);
  9391. return valSet;
  9392. }
  9393. int EQ2Emu_lua_SetSpellDataIndex(lua_State* state) {
  9394. if (!lua_interface)
  9395. return 0;
  9396. LuaSpell* spell = lua_interface->GetSpell(state);
  9397. int8 idx = lua_interface->GetInt32Value(state, 2);
  9398. if (!spell) {
  9399. lua_interface->LogError("%s: Spell not given in SetSpellDataIndex!", lua_interface->GetScriptName(state));
  9400. return 0;
  9401. }
  9402. if (!spell->spell || !spell->spell->GetSpellData()) {
  9403. lua_interface->LogError("%s: Inner Spell or SpellData not given in SetSpellDataIndex!", lua_interface->GetScriptName(state));
  9404. return 0;
  9405. }
  9406. if (spell->spell->lua_data.size() <= idx)
  9407. {
  9408. lua_interface->LogError("%s: lua_data size %i <= %i (idx passed) SetSpellDataIndex!", lua_interface->GetScriptName(state), spell->spell->lua_data.size(), idx);
  9409. return 0;
  9410. }
  9411. bool setVal = true;
  9412. LUAData* data = spell->spell->lua_data[idx];
  9413. switch (data->type)
  9414. {
  9415. case 0:
  9416. {
  9417. sint32 value = lua_interface->GetSInt32Value(state, 3);
  9418. sint32 value2 = lua_interface->GetSInt32Value(state, 4);
  9419. data->int_value = value;
  9420. data->int_value2 = value2;
  9421. break;
  9422. }
  9423. case 1:
  9424. {
  9425. float value = lua_interface->GetFloatValue(state, 3);
  9426. float value2 = lua_interface->GetFloatValue(state, 4);
  9427. data->float_value = value;
  9428. data->float_value2 = value2;
  9429. break;
  9430. }
  9431. case 2:
  9432. {
  9433. bool value = lua_interface->GetBooleanValue(state, 3);
  9434. data->bool_value = value;
  9435. break;
  9436. }
  9437. case 3:
  9438. {
  9439. string value = lua_interface->GetStringValue(state, 3);
  9440. string value2 = lua_interface->GetStringValue(state, 4);
  9441. data->string_value = value;
  9442. data->string_value2 = value2;
  9443. break;
  9444. }
  9445. default:
  9446. setVal = false;
  9447. }
  9448. return setVal;
  9449. }
  9450. int EQ2Emu_lua_GetSpellDataIndex(lua_State* state) {
  9451. if (!lua_interface)
  9452. return 0;
  9453. LuaSpell* spell = lua_interface->GetSpell(state);
  9454. int8 idx = lua_interface->GetInt32Value(state, 2);
  9455. bool secondfield = lua_interface->GetBooleanValue(state, 3);
  9456. if (!spell) {
  9457. lua_interface->LogError("%s: Spell not given in GetSpellDataIndex!", lua_interface->GetScriptName(state));
  9458. return 0;
  9459. }
  9460. if (!spell->spell || !spell->spell->GetSpellData()) {
  9461. lua_interface->LogError("%s: Inner Spell or SpellData not given in GetSpellDataIndex!", lua_interface->GetScriptName(state));
  9462. return 0;
  9463. }
  9464. if (spell->spell->lua_data.size() <= idx)
  9465. {
  9466. lua_interface->LogError("%s: lua_data size %i <= %i (idx passed) GetSpellDataIndex!", lua_interface->GetScriptName(state), spell->spell->lua_data.size(), idx);
  9467. return 0;
  9468. }
  9469. bool setVal = true;
  9470. LUAData* data = spell->spell->lua_data[idx];
  9471. switch (data->type)
  9472. {
  9473. case 0:
  9474. {
  9475. if(!secondfield)
  9476. lua_interface->SetSInt32Value(state, data->int_value);
  9477. else
  9478. lua_interface->SetSInt32Value(state, data->int_value2);
  9479. break;
  9480. }
  9481. case 1:
  9482. {
  9483. if (!secondfield)
  9484. lua_interface->SetFloatValue(state, data->float_value);
  9485. else
  9486. lua_interface->SetFloatValue(state, data->float_value2);
  9487. break;
  9488. }
  9489. case 2:
  9490. {
  9491. lua_interface->SetBooleanValue(state, data->bool_value);
  9492. break;
  9493. }
  9494. case 3:
  9495. {
  9496. if (!secondfield)
  9497. lua_interface->SetStringValue(state, data->string_value.c_str());
  9498. else
  9499. lua_interface->SetStringValue(state, data->string_value2.c_str());
  9500. break;
  9501. }
  9502. default:
  9503. setVal = false;
  9504. }
  9505. return setVal;
  9506. }
  9507. int EQ2Emu_lua_SetSpellDisplayEffect(lua_State* state) {
  9508. if (!lua_interface)
  9509. return 0;
  9510. LuaSpell* spell = lua_interface->GetSpell(state);
  9511. int8 idx = lua_interface->GetInt32Value(state, 2);
  9512. string field = lua_interface->GetStringValue(state, 3);
  9513. boost::to_lower(field);
  9514. if (!spell) {
  9515. lua_interface->LogError("%s: Spell not given in SetSpellDisplayEffect!", lua_interface->GetScriptName(state));
  9516. return 0;
  9517. }
  9518. if (!spell->spell || !spell->spell->GetSpellData()) {
  9519. lua_interface->LogError("%s: Inner Spell or SpellData not given in SetSpellDisplayEffect!", lua_interface->GetScriptName(state));
  9520. return 0;
  9521. }
  9522. if (spell->spell->effects.size() <= idx)
  9523. {
  9524. lua_interface->LogError("%s: lua_data size %i <= %i (idx passed) SetSpellDisplayEffect!", lua_interface->GetScriptName(state), spell->spell->lua_data.size(), idx);
  9525. return 0;
  9526. }
  9527. // do we need to lock? eh probably not this should only be used before use of the custom spell
  9528. SpellDisplayEffect* effect = spell->spell->effects[idx];
  9529. if (field == "description")
  9530. effect->description = string(lua_interface->GetStringValue(state, 4));
  9531. else if (field == "bullet")
  9532. effect->subbullet = lua_interface->GetInt8Value(state, 4);
  9533. else if (field == "percentage")
  9534. effect->percentage = lua_interface->GetInt8Value(state, 4);
  9535. else // no match
  9536. return 0;
  9537. return 1;
  9538. }
  9539. int EQ2Emu_lua_GetSpellDisplayEffect(lua_State* state) {
  9540. if (!lua_interface)
  9541. return 0;
  9542. LuaSpell* spell = lua_interface->GetSpell(state);
  9543. int8 idx = lua_interface->GetInt32Value(state, 2);
  9544. string field = lua_interface->GetStringValue(state, 3);
  9545. boost::to_lower(field);
  9546. if (!spell) {
  9547. lua_interface->LogError("%s: Spell not given in GetSpellDisplayEffect!", lua_interface->GetScriptName(state));
  9548. return 0;
  9549. }
  9550. if (!spell->spell || !spell->spell->GetSpellData()) {
  9551. lua_interface->LogError("%s: Inner Spell or SpellData not given in GetSpellDisplayEffect!", lua_interface->GetScriptName(state));
  9552. return 0;
  9553. }
  9554. if (spell->spell->effects.size() <= idx)
  9555. {
  9556. lua_interface->LogError("%s: lua_data size %i <= %i (idx passed) GetSpellDisplayEffect!", lua_interface->GetScriptName(state), spell->spell->lua_data.size(), idx);
  9557. return 0;
  9558. }
  9559. // do we need to lock? eh probably not this should only be used before use of the custom spell
  9560. SpellDisplayEffect* effect = spell->spell->effects[idx];
  9561. if (field == "description")
  9562. lua_interface->SetStringValue(state, effect->description.c_str());
  9563. else if (field == "bullet")
  9564. lua_interface->SetInt32Value(state, effect->subbullet);
  9565. else if (field == "percentage")
  9566. lua_interface->SetInt32Value(state, effect->percentage);
  9567. else // no match
  9568. return 0;
  9569. return 1;
  9570. }
  9571. int EQ2Emu_lua_CastCustomSpell(lua_State* state) {
  9572. if (!lua_interface)
  9573. return 0;
  9574. LuaSpell* spell = lua_interface->GetSpell(state);
  9575. Spawn* caster = lua_interface->GetSpawn(state, 2);
  9576. Spawn* target = lua_interface->GetSpawn(state, 3);
  9577. if (!target) {
  9578. lua_interface->LogError("%s: LUA CastCustomSpell command error: target is not a valid spawn", lua_interface->GetScriptName(state));
  9579. return 0;
  9580. }
  9581. if (!target->IsEntity()) {
  9582. lua_interface->LogError("%s: LUA CastCustomSpell command error: target (%s) is not an entity", lua_interface->GetScriptName(state), target->GetName());
  9583. return 0;
  9584. }
  9585. if (!spell) {
  9586. lua_interface->LogError("%s: LUA CastCustomSpell command error: spell is not valid", lua_interface->GetScriptName(state));
  9587. return 0;
  9588. }
  9589. if (caster && !caster->IsEntity()) {
  9590. lua_interface->LogError("%s: LUA CastSpell command error: caster (%s) is not an entity", lua_interface->GetScriptName(state), caster->GetName());
  9591. return 0;
  9592. }
  9593. target->GetZone()->ProcessSpell(NULL, (Entity*)caster, (Entity*)target, true, false, spell, 0);
  9594. return 0;
  9595. }