LuaFunctions.cpp 281 KB

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