9
3

LuaFunctions.cpp 304 KB

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