LuaFunctions.cpp 284 KB

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