LuaFunctions.cpp 282 KB

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